xref: /titanic_51/usr/src/cmd/refer/mkey2.c (revision 11a8fa6cb17403e630122ac19b39a323c6e64142)
1*11a8fa6cSceastha /*
2*11a8fa6cSceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*11a8fa6cSceastha  * Use is subject to license terms.
4*11a8fa6cSceastha  */
5*11a8fa6cSceastha 
67c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
77c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate  */
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate #include <stdio.h>
187c478bd9Sstevel@tonic-gate #include <ctype.h>
197c478bd9Sstevel@tonic-gate #define	MAXLINE 500
207c478bd9Sstevel@tonic-gate 
217c478bd9Sstevel@tonic-gate static int eof = 0;
227c478bd9Sstevel@tonic-gate static long lp, lim;
237c478bd9Sstevel@tonic-gate static int alph, used, prevc;
247c478bd9Sstevel@tonic-gate static char *p, key[20];
257c478bd9Sstevel@tonic-gate 
26*11a8fa6cSceastha extern int common();
27*11a8fa6cSceastha extern char *mindex();
28*11a8fa6cSceastha 
29*11a8fa6cSceastha static void chkey(int, char *);
30*11a8fa6cSceastha static long grec(char *, FILE *);
31*11a8fa6cSceastha 
32*11a8fa6cSceastha void
33*11a8fa6cSceastha dofile(FILE *f, char *name)
347c478bd9Sstevel@tonic-gate {
357c478bd9Sstevel@tonic-gate 	/* read file f & spit out keys & ptrs */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate 	char line[MAXLINE], *s;
387c478bd9Sstevel@tonic-gate 	extern int minlen, keycount, labels;
397c478bd9Sstevel@tonic-gate 	int c;
407c478bd9Sstevel@tonic-gate 	extern int wholefile;
417c478bd9Sstevel@tonic-gate 	extern char *iglist;
427c478bd9Sstevel@tonic-gate 	alph = used = prevc = eof = 0;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate 	lp = 0;
45*11a8fa6cSceastha 	if (wholefile == 0) {
46*11a8fa6cSceastha 		while (lim = grec(line, f)) {
477c478bd9Sstevel@tonic-gate #if D1
487c478bd9Sstevel@tonic-gate 			fprintf(stderr, "line: /%s", line);
497c478bd9Sstevel@tonic-gate #endif
507c478bd9Sstevel@tonic-gate 			used = alph = 0;
517c478bd9Sstevel@tonic-gate 			p = key;
527c478bd9Sstevel@tonic-gate 			for (s = line; (c = *s) && (used < keycount); s++)
537c478bd9Sstevel@tonic-gate 				chkey(c, name);
547c478bd9Sstevel@tonic-gate 			lp += lim;
557c478bd9Sstevel@tonic-gate 			if (used) putchar('\n');
567c478bd9Sstevel@tonic-gate 		}
57*11a8fa6cSceastha 	} else {
587c478bd9Sstevel@tonic-gate 		p = key;
597c478bd9Sstevel@tonic-gate 		used = alph = 0;
607c478bd9Sstevel@tonic-gate 		while ((c = getc(f)) != EOF && used < keycount)
617c478bd9Sstevel@tonic-gate 			chkey(c, name);
627c478bd9Sstevel@tonic-gate 		if (used) putchar('\n');
637c478bd9Sstevel@tonic-gate 	}
647c478bd9Sstevel@tonic-gate 	fclose(f);
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate 
67*11a8fa6cSceastha static int
68*11a8fa6cSceastha outkey(char *ky, int lead, int trail)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	int n;
717c478bd9Sstevel@tonic-gate 	extern int minlen;
727c478bd9Sstevel@tonic-gate 	n = strlen(ky);
73*11a8fa6cSceastha 	if (n < minlen)
74*11a8fa6cSceastha 		return (0);
75*11a8fa6cSceastha 	if (n < 3) {
76*11a8fa6cSceastha 		if (trail == '.')
77*11a8fa6cSceastha 			return (0);
78*11a8fa6cSceastha 		if (mindex(".%,!#$%&'();+:*", lead) != 0)
79*11a8fa6cSceastha 			return (0);
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 	if (isdigit(ky[0]))
827c478bd9Sstevel@tonic-gate 		/* Allow years 1000 - 2099 */
837c478bd9Sstevel@tonic-gate 		if (!(ky[0] == '1' || (ky[0] == '2' && ky[1] == '0')) || n != 4)
847c478bd9Sstevel@tonic-gate 			return (0);
857c478bd9Sstevel@tonic-gate 	if (common(ky))
867c478bd9Sstevel@tonic-gate 		return (0);
877c478bd9Sstevel@tonic-gate 	return (1);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
90*11a8fa6cSceastha static long
91*11a8fa6cSceastha grec(char *s, FILE *f)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	char tm[200];
947c478bd9Sstevel@tonic-gate 	int curtype = 0;
957c478bd9Sstevel@tonic-gate 	long len = 0L, tlen = 0L;
967c478bd9Sstevel@tonic-gate 	extern int wholefile;
977c478bd9Sstevel@tonic-gate 	extern char *iglist;
98*11a8fa6cSceastha 	if (eof)
99*11a8fa6cSceastha 		return (0);
1007c478bd9Sstevel@tonic-gate 	*s = 0;
101*11a8fa6cSceastha 	while (fgets(tm, 200, f)) {
1027c478bd9Sstevel@tonic-gate 		tlen += strlen(tm);
1037c478bd9Sstevel@tonic-gate 		if (tm[0] == '%' || tm[0] == '.')
1047c478bd9Sstevel@tonic-gate 			curtype = tm[1];
1057c478bd9Sstevel@tonic-gate 		if (tlen < MAXLINE && mindex(iglist, curtype) == 0)
1067c478bd9Sstevel@tonic-gate 			strcat(s, tm);
1077c478bd9Sstevel@tonic-gate 		len = tlen;
1087c478bd9Sstevel@tonic-gate 		if (wholefile == 0 && tm[0] == '\n')
1097c478bd9Sstevel@tonic-gate 			return (len);
110*11a8fa6cSceastha 		if (wholefile > 0 && len >= MAXLINE) {
1117c478bd9Sstevel@tonic-gate 			fseek(f, 0L, 2);
1127c478bd9Sstevel@tonic-gate 			return (ftell(f));
1137c478bd9Sstevel@tonic-gate 		}
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 	eof = 1;
1167c478bd9Sstevel@tonic-gate 	return (s[0] ? len : 0L);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate char *
120*11a8fa6cSceastha trimnl(char *ln)
1217c478bd9Sstevel@tonic-gate {
122*11a8fa6cSceastha 	char *p = ln;
1237c478bd9Sstevel@tonic-gate 	while (*p) p++;
1247c478bd9Sstevel@tonic-gate 	p--;
1257c478bd9Sstevel@tonic-gate 	if (*p == '\n') *p = 0;
1267c478bd9Sstevel@tonic-gate 	return (ln);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
129*11a8fa6cSceastha static void
130*11a8fa6cSceastha chkey(int c, char *name)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	extern int labels;
1337c478bd9Sstevel@tonic-gate 	extern int wholefile;
134*11a8fa6cSceastha 	if (isalpha(c) || isdigit(c)) {
1357c478bd9Sstevel@tonic-gate 		if (alph++ < 6)
1367c478bd9Sstevel@tonic-gate 			*p++ = c;
137*11a8fa6cSceastha 	} else {
1387c478bd9Sstevel@tonic-gate 		*p = 0;
1397c478bd9Sstevel@tonic-gate 		for (p = key; *p; p++)
1407c478bd9Sstevel@tonic-gate 			*p |= 040;
141*11a8fa6cSceastha 		if (outkey(p = key, prevc, c)) {
142*11a8fa6cSceastha 			if (used == 0) {
143*11a8fa6cSceastha 				if (labels) {
1447c478bd9Sstevel@tonic-gate 					if (wholefile == 0)
145*11a8fa6cSceastha 						printf("%s:%ld,%ld\t", name,
146*11a8fa6cSceastha 						    lp, lim);
1477c478bd9Sstevel@tonic-gate 					else
1487c478bd9Sstevel@tonic-gate 						printf("%s\t", name);
1497c478bd9Sstevel@tonic-gate 				}
150*11a8fa6cSceastha 			} else
1517c478bd9Sstevel@tonic-gate 				putchar(' ');
1527c478bd9Sstevel@tonic-gate 			fputs(key, stdout);
1537c478bd9Sstevel@tonic-gate 			used++;
1547c478bd9Sstevel@tonic-gate 		}
1557c478bd9Sstevel@tonic-gate 		prevc = c;
1567c478bd9Sstevel@tonic-gate 		alph = 0;
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate }
159