xref: /illumos-gate/usr/src/cmd/refer/mkey2.c (revision 2a8bcb4e)
1*11a8fa6cSceastha /*
2*11a8fa6cSceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*11a8fa6cSceastha  * Use is subject to license terms.
4*11a8fa6cSceastha  */
5*11a8fa6cSceastha 
67c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
77c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate  */
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate #include <stdio.h>
167c478bd9Sstevel@tonic-gate #include <ctype.h>
17*11a8fa6cSceastha #define	MAXLINE 500
187c478bd9Sstevel@tonic-gate 
197c478bd9Sstevel@tonic-gate static int eof = 0;
207c478bd9Sstevel@tonic-gate static long lp, lim;
217c478bd9Sstevel@tonic-gate static int alph, used, prevc;
227c478bd9Sstevel@tonic-gate static char *p, key[20];
237c478bd9Sstevel@tonic-gate 
24*11a8fa6cSceastha extern int common();
25*11a8fa6cSceastha extern char *mindex();
26*11a8fa6cSceastha 
27*11a8fa6cSceastha static void chkey(int, char *);
28*11a8fa6cSceastha static long grec(char *, FILE *);
29*11a8fa6cSceastha 
30*11a8fa6cSceastha void
dofile(FILE * f,char * name)31*11a8fa6cSceastha dofile(FILE *f, char *name)
327c478bd9Sstevel@tonic-gate {
337c478bd9Sstevel@tonic-gate 	/* read file f & spit out keys & ptrs */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate 	char line[MAXLINE], *s;
367c478bd9Sstevel@tonic-gate 	extern int minlen, keycount, labels;
377c478bd9Sstevel@tonic-gate 	int c;
387c478bd9Sstevel@tonic-gate 	extern int wholefile;
397c478bd9Sstevel@tonic-gate 	extern char *iglist;
40*11a8fa6cSceastha 	alph = used = prevc = eof = 0;
417c478bd9Sstevel@tonic-gate 
42*11a8fa6cSceastha 	lp = 0;
43*11a8fa6cSceastha 	if (wholefile == 0) {
44*11a8fa6cSceastha 		while (lim = grec(line, f)) {
45*11a8fa6cSceastha #if D1
46*11a8fa6cSceastha 			fprintf(stderr, "line: /%s", line);
47*11a8fa6cSceastha #endif
48*11a8fa6cSceastha 			used = alph = 0;
497c478bd9Sstevel@tonic-gate 			p = key;
50*11a8fa6cSceastha 			for (s = line; (c = *s) && (used < keycount); s++)
517c478bd9Sstevel@tonic-gate 				chkey(c, name);
527c478bd9Sstevel@tonic-gate 			lp += lim;
537c478bd9Sstevel@tonic-gate 			if (used) putchar('\n');
547c478bd9Sstevel@tonic-gate 		}
55*11a8fa6cSceastha 	} else {
56*11a8fa6cSceastha 		p = key;
57*11a8fa6cSceastha 		used = alph = 0;
58*11a8fa6cSceastha 		while ((c = getc(f)) != EOF && used < keycount)
59*11a8fa6cSceastha 			chkey(c, name);
607c478bd9Sstevel@tonic-gate 		if (used) putchar('\n');
617c478bd9Sstevel@tonic-gate 	}
627c478bd9Sstevel@tonic-gate 	fclose(f);
637c478bd9Sstevel@tonic-gate }
647c478bd9Sstevel@tonic-gate 
65*11a8fa6cSceastha static int
outkey(char * ky,int lead,int trail)66*11a8fa6cSceastha outkey(char *ky, int lead, int trail)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	int n;
697c478bd9Sstevel@tonic-gate 	extern int minlen;
707c478bd9Sstevel@tonic-gate 	n = strlen(ky);
71*11a8fa6cSceastha 	if (n < minlen)
72*11a8fa6cSceastha 		return (0);
73*11a8fa6cSceastha 	if (n < 3) {
74*11a8fa6cSceastha 		if (trail == '.')
75*11a8fa6cSceastha 			return (0);
76*11a8fa6cSceastha 		if (mindex(".%,!#$%&'();+:*", lead) != 0)
77*11a8fa6cSceastha 			return (0);
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 	if (isdigit(ky[0]))
807c478bd9Sstevel@tonic-gate 		/* Allow years 1000 - 2099 */
817c478bd9Sstevel@tonic-gate 		if (!(ky[0] == '1' || (ky[0] == '2' && ky[1] == '0')) || n != 4)
82*11a8fa6cSceastha 			return (0);
837c478bd9Sstevel@tonic-gate 	if (common(ky))
84*11a8fa6cSceastha 		return (0);
85*11a8fa6cSceastha 	return (1);
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate 
88*11a8fa6cSceastha static long
grec(char * s,FILE * f)89*11a8fa6cSceastha grec(char *s, FILE *f)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	char tm[200];
927c478bd9Sstevel@tonic-gate 	int curtype = 0;
937c478bd9Sstevel@tonic-gate 	long len = 0L, tlen = 0L;
947c478bd9Sstevel@tonic-gate 	extern int wholefile;
957c478bd9Sstevel@tonic-gate 	extern char *iglist;
96*11a8fa6cSceastha 	if (eof)
97*11a8fa6cSceastha 		return (0);
987c478bd9Sstevel@tonic-gate 	*s = 0;
99*11a8fa6cSceastha 	while (fgets(tm, 200, f)) {
1007c478bd9Sstevel@tonic-gate 		tlen += strlen(tm);
1017c478bd9Sstevel@tonic-gate 		if (tm[0] == '%' || tm[0] == '.')
1027c478bd9Sstevel@tonic-gate 			curtype = tm[1];
103*11a8fa6cSceastha 		if (tlen < MAXLINE && mindex(iglist, curtype) == 0)
1047c478bd9Sstevel@tonic-gate 			strcat(s, tm);
1057c478bd9Sstevel@tonic-gate 		len = tlen;
106*11a8fa6cSceastha 		if (wholefile == 0 && tm[0] == '\n')
107*11a8fa6cSceastha 			return (len);
108*11a8fa6cSceastha 		if (wholefile > 0 && len >= MAXLINE) {
109*11a8fa6cSceastha 			fseek(f, 0L, 2);
110*11a8fa6cSceastha 			return (ftell(f));
1117c478bd9Sstevel@tonic-gate 		}
1127c478bd9Sstevel@tonic-gate 	}
113*11a8fa6cSceastha 	eof = 1;
114*11a8fa6cSceastha 	return (s[0] ? len : 0L);
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate char *
trimnl(char * ln)118*11a8fa6cSceastha trimnl(char *ln)
1197c478bd9Sstevel@tonic-gate {
120*11a8fa6cSceastha 	char *p = ln;
1217c478bd9Sstevel@tonic-gate 	while (*p) p++;
1227c478bd9Sstevel@tonic-gate 	p--;
123*11a8fa6cSceastha 	if (*p == '\n') *p = 0;
124*11a8fa6cSceastha 	return (ln);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
127*11a8fa6cSceastha static void
chkey(int c,char * name)128*11a8fa6cSceastha chkey(int c, char *name)
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate 	extern int labels;
1317c478bd9Sstevel@tonic-gate 	extern int wholefile;
132*11a8fa6cSceastha 	if (isalpha(c) || isdigit(c)) {
1337c478bd9Sstevel@tonic-gate 		if (alph++ < 6)
1347c478bd9Sstevel@tonic-gate 			*p++ = c;
135*11a8fa6cSceastha 	} else {
1367c478bd9Sstevel@tonic-gate 		*p = 0;
137*11a8fa6cSceastha 		for (p = key; *p; p++)
1387c478bd9Sstevel@tonic-gate 			*p |= 040;
139*11a8fa6cSceastha 		if (outkey(p = key, prevc, c)) {
140*11a8fa6cSceastha 			if (used == 0) {
141*11a8fa6cSceastha 				if (labels) {
142*11a8fa6cSceastha 					if (wholefile == 0)
143*11a8fa6cSceastha 						printf("%s:%ld,%ld\t", name,
144*11a8fa6cSceastha 						    lp, lim);
1457c478bd9Sstevel@tonic-gate 					else
1467c478bd9Sstevel@tonic-gate 						printf("%s\t", name);
1477c478bd9Sstevel@tonic-gate 				}
148*11a8fa6cSceastha 			} else
1497c478bd9Sstevel@tonic-gate 				putchar(' ');
1507c478bd9Sstevel@tonic-gate 			fputs(key, stdout);
1517c478bd9Sstevel@tonic-gate 			used++;
1527c478bd9Sstevel@tonic-gate 		}
153*11a8fa6cSceastha 		prevc = c;
154*11a8fa6cSceastha 		alph = 0;
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate }
157