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