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