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