Research V7 development
[unix-history] / usr / src / cmd / prep / prep1.c
CommitLineData
569c15a3
KT
1# include <stdio.h>
2# include "prep.h"
3# define SKIP 0
4# define COLLECT 1
5# define SKIP2 2
6
7int tlno = 1;
8
9coll()
10{
11 cs = COLLECT;
12 temp[t1].beg = &line[l];
13 return;
14}
15
16save()
17{
18 extern only;
19 char *pt1,*pt2,cbuf[30];
20 int val;
21
22 if(cs != COLLECT) {
23 cs = SKIP;
24 return;
25 }
26 cs = SKIP;
27 line[l] = '\0';
28 temp[t1].ct = &line[l] - temp[t1].beg;
29 pt1 = temp[t1].beg-1;
30 pt2 = cbuf-1;
31
32 wdnum++;
33 while(*++pt2 = *++pt1)
34 if(*pt2 >= 'A' && *pt2 <= 'Z')
35 *pt2 |= 040;
36
37 if(igflg){
38 val = search(cbuf,temp[t1].ct,&itab,0);
39 if(!val == !only) goto yes;
40
41 line[l] = c;
42 return;
43 }
44yes:
45 temp[t1++].wdno = wdnum;
46 line[l] = c;
47 return;
48}
49
50ctout()
51{
52 register int ct;
53 register char *t2;
54 int t3;
55 long xxx;
56
57 if(lflag) {
58 fprintf(stderr, "line too long: line %d\n",lno);
59 lflag = 0;
60 }
61 if(cs == COLLECT) save();
62
63 t3 = t1;
64 t1 = -1;
65 while(++t1 < t3) {
66 if(wdflg){
67 xxx = temp[t1].wdno;
68 conf(xxx, WIDTH, num);
69 put(num,WIDTH+1);
70 }
71 ct = temp[t1].ct;
72 t2 = temp[t1].beg - 1;
73/* fprintf(stderr, "out: %s %d\n", temp[t1].beg, ct); /*DEBUG*/
74 while(ct--)
75 if(*++t2 >= 'A' && *t2 <= 'Z')
76 *t2 |= 040;
77
78 ct = temp[t1].ct;
79 while(*--t2 == '\'' && san == 0)
80 ct--;
81
82 put(temp[t1].beg, ct);
83 put("\n", 1);
84 }
85 t1 = 0;
86 l = -1;
87 lno += tlno;
88 tlno = 1;
89 cs = SKIP;
90 return;
91}
92
93conf(n,width,buf)
94 long n;
95 char *buf;
96{
97 long a;
98 auto i;
99
100 i = width;
101 while(i--) buf[i] = ' ';
102
103 a = n/10;
104 if(a)
105 width = conf(a, --width, buf);
106 else
107 width--;
108 buf[width] = n%10 + '0';
109
110 return(++width);
111}
112
113hyphen()
114{
115/* fprintf(stderr, "hyphen\n"); /*DEBUG*/
116 flag[++fl] = hyp1;
117 return(1);
118}
119
120hyp1()
121{
122/* fprintf(stderr, "hyp1 c = %o\n",c); /*DEBUG*/
123 if(c != '\n') {
124 fl--;
125 l--;
126 save();
127 l++;
128 punc();
129 return(0);
130 } else {
131 l -= 2;
132 flag[fl] = hyp2;
133 hsw = 1;
134 return(1);
135 }
136}
137
138hyp2()
139{
140 extern (*acts[])();
141/* fprintf(stderr, "hyp2 c = %o l = %d\n",c,l); /*DEBUG*/
142 if(hsw && (tab[2][c] == 0)) {
143 l--;
144 if(c == '\n') tlno++;
145 return(1);
146 }
147 hsw = 0;
148 if(c == '\n'){
149 l--;
150 return(1);
151 }
152 if(tab[cs][c]) {
153 line[l] = '\n';
154 (*acts[OUT])();
155 fl--;
156 return(0);
157 }
158 return(1);
159}
160
161gobble2()
162{
163 static ct2;
164
165 if(cs == COLLECT) save();
166
167 if(flag[fl] != gobble2) {
168 ct2 = 1;
169 flag[++fl] = gobble2;
170 return(1);
171 }
172 if(ct2--) return(1);
173
174 fl--;
175 cs = SKIP;
176 return(1);
177}
178
179bslash()
180{
181 if(cs == COLLECT) save();
182 cs = SKIP2;
183 return(1);
184}
185
186bsp()
187{
188 flag[++fl] = bsp1;
189 return(1);
190}
191
192bsp1()
193{
194 fl--;
195 if(c == '"') return(1);
196
197 line[--l] = c;
198 return(0);
199}
200
201punc()
202{
203
204 if(cs == COLLECT)
205 save();
206 if(puncfl) {
207 temp[t1].beg = &line[l];
208 temp[t1].ct = 1;
209 temp[t1++].wdno = 0;
210 }
211}
212
213search(symbol,length,params,install)
214 char *symbol;
215 int length;
216 struct htab *params;
217 int install;
218{
219 char *sp,*p;
220 static int *hptr,hsiz,nsym;
221 static int ssiz;
222 static int curb;
223 static char *symt;
224 auto h,i,j;
225
226 if(hptr != params->hptr) {
227 hptr = params->hptr;
228 hsiz = params->hsiz;
229 symt = params->symt;
230 ssiz = params->ssiz;
231 curb = params->curb;
232 nsym = params->nsym;
233 }
234
235 symbol[length] = '\0';
236/*fprintf(stderr, "ssiz = %d; nsym = %d; %s\n", ssiz, nsym, symbol);/*DEBUG*/
237 sp = symbol;
238
239 i = length;
240 h = 1;
241 while(i--)
242 h *= *sp++;
243
244 if(h == 0100000) {
245 h = 1;
246 } else {
247 h = h<0?(-h)%hsiz:h%hsiz;
248 }
249 if(h == 0) h++;
250/* fprintf(stderr, "%s %d\n",symbol,h); /*DEBUG*/
251
252 while((p = &symt[hptr[h]]) > symt) {
253 j = length + 2;
254 sp = symbol;
255 while(--j) {
256 if(*p++ != *sp++) goto no;
257 }
258 return(*p);
259no:
260 h++;
261 if(h >= hsiz) h -= hsiz;
262 }
263 if(install) {
264 if(++nsym >= hsiz) {
265 fprintf(stderr, "Too many symbols in ignore/only file.\n");
266 exit(1);
267 }
268
269 hptr[h] = curb;
270 length++;
271 if((curb + length) >= ssiz) {
272 fprintf(stderr, "i/o file too big; ssiz = %d\n", ssiz);
273 exit(1);
274 }
275
276 while(length--)
277 symt[curb++] = *symbol++;
278 symt[curb++] = install;
279 params->curb = curb;
280 params->nsym = nsym;
281 }
282 return(0);
283}