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