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