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