xref: /csrg-svn/old/refer/mkey/mkey2.c (revision 34097)
112300Stut #ifndef lint
2*34097Sbostic static char *sccsid = "@(#)mkey2.c	4.2 (Berkeley) 04/24/88";
312300Stut #endif
412300Stut 
512300Stut #include <stdio.h>
612300Stut #include <ctype.h>
712300Stut #define MAXLINE 500
812300Stut 
912300Stut static int eof = 0;
1012300Stut static long lp, lim;
1112300Stut static int alph, used, prevc;
1212300Stut static char *p, key[20];
1312300Stut 
1412300Stut dofile(f, name)
1512300Stut FILE *f;
1612300Stut char *name;
1712300Stut {
1812300Stut 	/* read file f & spit out keys & ptrs */
1912300Stut 
2012300Stut 	char line[MAXLINE], *s;
21*34097Sbostic 	extern int keycount;
2212300Stut 	int c;
2312300Stut 	long grec();
2412300Stut 	extern int wholefile;
2512300Stut 	alph=used=prevc=eof=0;
2612300Stut 
2712300Stut 	lp=0;
2812300Stut 	if (wholefile==0)
2912300Stut 		while (lim = grec(line,f))
3012300Stut 		{
3112300Stut # if D1
3212300Stut 			fprintf(stderr, "line: /%s",line);
3312300Stut # endif
3412300Stut 			used=alph=0;
3512300Stut 			p = key;
3612300Stut 			for(s=line; (c= *s) && (used<keycount); s++)
3712300Stut 				chkey(c, name);
3812300Stut 			lp += lim;
3912300Stut 			if (used) putchar('\n');
4012300Stut 		}
4112300Stut 	else
4212300Stut 	{
4312300Stut 		p=key;
4412300Stut 		used=alph=0;
4512300Stut 		while ( (c=getc(f)) != EOF && used<keycount)
4612300Stut 			chkey (c, name);
4712300Stut 		if (used) putchar('\n');
4812300Stut 	}
4912300Stut 	fclose(f);
5012300Stut }
5112300Stut 
5212300Stut outkey( ky, lead, trail)
5312300Stut char *ky;
5412300Stut {
55*34097Sbostic 	extern int minlen;
5612300Stut 	int n;
5712300Stut 	n = strlen(ky);
5812300Stut 	if (n<minlen) return (0);
5912300Stut 	if (n<3)
6012300Stut 	{
6112300Stut 		if (trail == '.') return(0);
6212300Stut 		if (mindex(".%,!#$%&'();+:*", lead)!=0) return(0);
6312300Stut 	}
6412300Stut 	if (isdigit(ky[0]))
6512300Stut 		if (ky[0] != '1' || ky[1] != '9' || n!= 4) return(0);
6612300Stut 	if (common(ky))
6712300Stut 		return(0);
6812300Stut 	return(1);
6912300Stut }
7012300Stut 
7112300Stut long
7212300Stut grec (s, f)
7312300Stut char *s;
7412300Stut FILE *f;
7512300Stut {
7612300Stut 	char tm[200];
7712300Stut 	int curtype = 0;
7812300Stut 	long len = 0L, tlen = 0L;
7912300Stut 	extern int wholefile;
80*34097Sbostic 	extern char *iglist;
81*34097Sbostic 
8212300Stut 	if (eof) return(0);
8312300Stut 	*s = 0;
8412300Stut 	while (fgets(tm, 200, f))
8512300Stut 	{
8612300Stut 		tlen += strlen(tm);
8712300Stut 		if (tm[0] == '%' || tm[0] == '.')
8812300Stut 			curtype = tm[1];
8912300Stut 		if (tlen < MAXLINE && mindex(iglist,curtype)==0)
9012300Stut 			strcat(s, tm);
9112300Stut 		len = tlen;
9212300Stut 		if (wholefile==0 && tm[0] == '\n')
9312300Stut 			return(len);
9412300Stut 		if (wholefile>0 && len >= MAXLINE)
9512300Stut 		{
9612300Stut 			fseek (f, 0L, 2);
9712300Stut 			return(ftell(f));
9812300Stut 		}
9912300Stut 	}
10012300Stut 	eof=1;
10112300Stut 	return(s[0] ? len : 0L);
10212300Stut }
10312300Stut 
10412300Stut char *
10512300Stut trimnl(ln)
10612300Stut char *ln;
10712300Stut {
10812300Stut 	register char *p = ln;
10912300Stut 	while (*p) p++;
11012300Stut 	p--;
11112300Stut 	if (*p == '\n') *p=0;
11212300Stut 	return(ln);
11312300Stut }
11412300Stut 
11512300Stut chkey (c, name)
11612300Stut {
117*34097Sbostic 	extern int labels;
118*34097Sbostic 	extern int wholefile;
119*34097Sbostic 
12012300Stut 	if (isalpha(c) || isdigit(c))
12112300Stut 	{
12212300Stut 		if (alph++ < 6)
12312300Stut 			*p++ = c;
12412300Stut 	}
12512300Stut 	else
12612300Stut 	{
12712300Stut 		*p = 0;
12812300Stut 		for(p=key; *p; p++)
12912300Stut 			*p |= 040;
13012300Stut 		if (outkey(p=key,prevc,c))
13112300Stut 		{
13212300Stut 			if (used==0)
13312300Stut 			{
13412300Stut 				if (labels)
13512300Stut 				{
13612300Stut 					if (wholefile==0)
13712300Stut 						printf("%s:%ld,%ld\t", name, lp, lim);
13812300Stut 					else
13912300Stut 						printf("%s\t", name);
14012300Stut 				}
14112300Stut 			}
14212300Stut 			else
14312300Stut 				putchar(' ');
14412300Stut 			fputs(key, stdout);
14512300Stut 			used++;
14612300Stut 		}
14712300Stut 		prevc=c;
14812300Stut 		alph=0;
14912300Stut 	}
15012300Stut }
151