xref: /titanic_44/usr/src/cmd/refer/mkey2.c (revision f56c1286e5113aa46bd6e723da14d30c123153f2)
1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
7 /*	  All Rights Reserved  	*/
8 
9 /*
10  * Copyright (c) 1980 Regents of the University of California.
11  * All rights reserved. The Berkeley software License Agreement
12  * specifies the terms and conditions for redistribution.
13  */
14 
15 #pragma ident	"%Z%%M%	%I%	%E% SMI"
16 
17 #include <stdio.h>
18 #include <ctype.h>
19 #define	MAXLINE 500
20 
21 static int eof = 0;
22 static long lp, lim;
23 static int alph, used, prevc;
24 static char *p, key[20];
25 
26 extern int common();
27 extern char *mindex();
28 
29 static void chkey(int, char *);
30 static long grec(char *, FILE *);
31 
32 void
33 dofile(FILE *f, char *name)
34 {
35 	/* read file f & spit out keys & ptrs */
36 
37 	char line[MAXLINE], *s;
38 	extern int minlen, keycount, labels;
39 	int c;
40 	extern int wholefile;
41 	extern char *iglist;
42 	alph = used = prevc = eof = 0;
43 
44 	lp = 0;
45 	if (wholefile == 0) {
46 		while (lim = grec(line, f)) {
47 #if D1
48 			fprintf(stderr, "line: /%s", line);
49 #endif
50 			used = alph = 0;
51 			p = key;
52 			for (s = line; (c = *s) && (used < keycount); s++)
53 				chkey(c, name);
54 			lp += lim;
55 			if (used) putchar('\n');
56 		}
57 	} else {
58 		p = key;
59 		used = alph = 0;
60 		while ((c = getc(f)) != EOF && used < keycount)
61 			chkey(c, name);
62 		if (used) putchar('\n');
63 	}
64 	fclose(f);
65 }
66 
67 static int
68 outkey(char *ky, int lead, int trail)
69 {
70 	int n;
71 	extern int minlen;
72 	n = strlen(ky);
73 	if (n < minlen)
74 		return (0);
75 	if (n < 3) {
76 		if (trail == '.')
77 			return (0);
78 		if (mindex(".%,!#$%&'();+:*", lead) != 0)
79 			return (0);
80 	}
81 	if (isdigit(ky[0]))
82 		/* Allow years 1000 - 2099 */
83 		if (!(ky[0] == '1' || (ky[0] == '2' && ky[1] == '0')) || n != 4)
84 			return (0);
85 	if (common(ky))
86 		return (0);
87 	return (1);
88 }
89 
90 static long
91 grec(char *s, FILE *f)
92 {
93 	char tm[200];
94 	int curtype = 0;
95 	long len = 0L, tlen = 0L;
96 	extern int wholefile;
97 	extern char *iglist;
98 	if (eof)
99 		return (0);
100 	*s = 0;
101 	while (fgets(tm, 200, f)) {
102 		tlen += strlen(tm);
103 		if (tm[0] == '%' || tm[0] == '.')
104 			curtype = tm[1];
105 		if (tlen < MAXLINE && mindex(iglist, curtype) == 0)
106 			strcat(s, tm);
107 		len = tlen;
108 		if (wholefile == 0 && tm[0] == '\n')
109 			return (len);
110 		if (wholefile > 0 && len >= MAXLINE) {
111 			fseek(f, 0L, 2);
112 			return (ftell(f));
113 		}
114 	}
115 	eof = 1;
116 	return (s[0] ? len : 0L);
117 }
118 
119 char *
120 trimnl(char *ln)
121 {
122 	char *p = ln;
123 	while (*p) p++;
124 	p--;
125 	if (*p == '\n') *p = 0;
126 	return (ln);
127 }
128 
129 static void
130 chkey(int c, char *name)
131 {
132 	extern int labels;
133 	extern int wholefile;
134 	if (isalpha(c) || isdigit(c)) {
135 		if (alph++ < 6)
136 			*p++ = c;
137 	} else {
138 		*p = 0;
139 		for (p = key; *p; p++)
140 			*p |= 040;
141 		if (outkey(p = key, prevc, c)) {
142 			if (used == 0) {
143 				if (labels) {
144 					if (wholefile == 0)
145 						printf("%s:%ld,%ld\t", name,
146 						    lp, lim);
147 					else
148 						printf("%s\t", name);
149 				}
150 			} else
151 				putchar(' ');
152 			fputs(key, stdout);
153 			used++;
154 		}
155 		prevc = c;
156 		alph = 0;
157 	}
158 }
159