Research V7 development
[unix-history] / usr / src / cmd / refer / mkey2.c
CommitLineData
52960f2f
ML
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}
95trimnl(ln)
96 char *ln;
97{
98register char *p ln;
99while (*p) p++;
100p--;
101if (*p == '\n') *p=0;
102return(ln);
103}
104chkey (c, name)
105 {
106 if (isalpha(c) || isdigit(c))
107 {
108 if (alph++ < 6)
109 *p++ = c;
110 }
111 else
112 {
113 *p = 0;
114 for(p=key; *p; p++)
115 *p |= 040;
116 if (outkey(p=key,prevc,c))
117 {
118 if (used==0)
119 {
120 if (labels)
121 {
122 if (wholefile==0)
123 printf("%s:%ld,%ld\t", name, lp, lim);
124 else
125 printf("%s\t", name);
126 }
127 }
128 else
129 putchar(' ');
130 fputs(key, stdout);
131 used++;
132 }
133 prevc=c;
134 alph=0;
135 }
136 }