glue5.c (0:68f95e015346) glue5.c (719:6c26331bc6b8)
1/*
2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
5
1/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
2/* All Rights Reserved */
3
6/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
7/* All Rights Reserved */
8
4
5/*
6 * Copyright (c) 1980 Regents of the University of California.
7 * All rights reserved. The Berkeley software License Agreement
8 * specifies the terms and conditions for redistribution.
9 */
10
9/*
10 * Copyright (c) 1980 Regents of the University of California.
11 * All rights reserved. The Berkeley software License Agreement
12 * specifies the terms and conditions for redistribution.
13 */
14
11/*
12 * Copyright (c) 1983, 1984 1985, 1986, 1987, 1988, Sun Microsystems, Inc.
13 * All Rights Reserved.
14 */
15#pragma ident "%Z%%M% %I% %E% SMI"
15
16
16#pragma ident "%Z%%M% %I% %E% SMI"
17
17
18
19#include <stdio.h>
20#include <ctype.h>
21/*
22 * fgrep -- print all lines containing any of a set of keywords
23 *
24 * status returns:
25 * 0 - ok, and some matches
26 * 1 - ok, but no matches
27 * 2 - some error
28 */
29#define MAXSIZ 700
18#include <stdio.h>
19#include <ctype.h>
20/*
21 * fgrep -- print all lines containing any of a set of keywords
22 *
23 * status returns:
24 * 0 - ok, and some matches
25 * 1 - ok, but no matches
26 * 2 - some error
27 */
28#define MAXSIZ 700
30#define QSIZE 400
29#define QSIZE 400
31struct words {
32 char inp;
33 char out;
34 struct words *nst;
35 struct words *link;
36 struct words *fail;
30struct words {
31 char inp;
32 char out;
33 struct words *nst;
34 struct words *link;
35 struct words *fail;
37}
36}
38*www, *smax, *q;
39
40char buf[2*BUFSIZ];
41int nsucc;
42int need;
43char *instr;
44int inct;
45int rflag;
46int xargc;
47char **xargv;
48int numwords;
49int nfound;
50static int flag = 0;
51
37*www, *smax, *q;
38
39char buf[2*BUFSIZ];
40int nsucc;
41int need;
42char *instr;
43int inct;
44int rflag;
45int xargc;
46char **xargv;
47int numwords;
48int nfound;
49static int flag = 0;
50
52fgrep(argc, argv)
53char **argv;
51extern void err();
52extern void *zalloc();
53
54static void cfail(void);
55static void cgotofn(void);
56static void execute(void);
57static char gch(void);
58static int new(struct words *x);
59static void overflo(void);
60
61int
62fgrep(int argc, char **argv)
54{
55 nsucc = need = inct = rflag = numwords = nfound = 0;
56 instr = 0;
57 flag = 0;
63{
64 nsucc = need = inct = rflag = numwords = nfound = 0;
65 instr = 0;
66 flag = 0;
58 if (www==0)
59 www = (struct words *) zalloc(MAXSIZ, sizeof (*www));
60 if (www==NULL)
67 if (www == 0)
68 www = (struct words *)zalloc(MAXSIZ, sizeof (*www));
69 if (www == NULL)
61 err(gettext("Can't get space for machines"), 0);
70 err(gettext("Can't get space for machines"), 0);
62 for (q=www; q<www+MAXSIZ; q++) {
63 q->inp =0; q->out =0; q->nst =0; q->link =0; q->fail =0;
71 for (q = www; q < www+MAXSIZ; q++) {
72 q->inp = 0; q->out = 0; q->nst = 0; q->link = 0; q->fail = 0;
64 }
65 xargc = argc-1;
66 xargv = argv+1;
73 }
74 xargc = argc-1;
75 xargv = argv+1;
67 while (xargc>0 && xargv[0][0]=='-')
68 {
69 switch(xargv[0][1])
70 {
76 while (xargc > 0 && xargv[0][0] == '-') {
77 switch (xargv[0][1]) {
71 case 'r': /* return value only */
72 rflag++;
73 break;
74 case 'n': /* number of answers needed */
78 case 'r': /* return value only */
79 rflag++;
80 break;
81 case 'n': /* number of answers needed */
75 need = (int) xargv[1];
82 need = (int)xargv[1];
76 xargv++; xargc--;
77 break;
78 case 'i':
79 instr = xargv[1];
83 xargv++; xargc--;
84 break;
85 case 'i':
86 instr = xargv[1];
80 inct = (int) xargv[2]+2;
81# if D2
82fprintf(stderr,"inct %d xargv.2. %o %d\n",inct, xargv[2],xargv[2]);
83# endif
87 inct = (int)xargv[2]+2;
88#if D2
89fprintf(stderr, "inct %d xargv.2. %o %d\n", inct, xargv[2], xargv[2]);
90#endif
84 xargv += 2; xargc -= 2;
85 break;
91 xargv += 2; xargc -= 2;
92 break;
86 }
87 xargv++; xargc--;
88 }
93 }
89 if (xargc<=0)
90 {
91 write (2, "bad fgrep call\n", 15);
94 xargv++; xargc--;
95 }
96 if (xargc <= 0) {
97 write(2, "bad fgrep call\n", 15);
92 exit(2);
98 exit(2);
93 }
94# if D1
99 }
100#if D1
95 fprintf(stderr, "before cgoto\n");
101 fprintf(stderr, "before cgoto\n");
96# endif
102#endif
97 cgotofn();
103 cgotofn();
98# if D1
104#if D1
99 fprintf(stderr, "before cfail\n");
105 fprintf(stderr, "before cfail\n");
100# endif
106#endif
101 cfail();
107 cfail();
102# if D1
108#if D1
103 fprintf(stderr, "before execute instr %.20s\n", instr? instr: "");
104 fprintf(stderr, "end of string %d %c %c %c\n", inct,
109 fprintf(stderr, "before execute instr %.20s\n", instr? instr: "");
110 fprintf(stderr, "end of string %d %c %c %c\n", inct,
105 instr ? instr[inct-3] : '\0',
106 instr ? instr[inct-2] : '\0',
107 instr ? instr[inct-1] : '\0');
108# endif
111 instr ? instr[inct-3] : '\0',
112 instr ? instr[inct-2] : '\0',
113 instr ? instr[inct-1] : '\0');
114#endif
109 execute();
115 execute();
110# if D1
116#if D1
111 fprintf(stderr, "returning nsucc %d\n", nsucc);
117 fprintf(stderr, "returning nsucc %d\n", nsucc);
112 fprintf(stderr, "fgrep done www %o\n",www);
113# endif
114 return(nsucc == 0);
118 fprintf(stderr, "fgrep done www %o\n", www);
119#endif
120 return (nsucc == 0);
115}
116
121}
122
117execute()
123static void
124execute(void)
118{
125{
119 register char *p;
120 register struct words *c;
121 register ch;
122 register ccount;
126 char *p;
127 struct words *c;
128 char ch;
129 int ccount;
123 int f;
124 char *nlp;
130 int f;
131 char *nlp;
125 f=0;
132 f = 0;
126 ccount = instr ? inct : 0;
133 ccount = instr ? inct : 0;
127 nfound=0;
134 nfound = 0;
128 p = instr ? instr : buf;
129 if (need == 0) need = numwords;
130 nlp = p;
131 c = www;
135 p = instr ? instr : buf;
136 if (need == 0) need = numwords;
137 nlp = p;
138 c = www;
132# if D2
133fprintf(stderr, "in execute ccount %d inct %d\n",ccount, inct );
134# endif
139#if D2
140fprintf(stderr, "in execute ccount %d inct %d\n", ccount, inct);
141#endif
135 for (;;) {
142 for (;;) {
136# if D3
143#if D3
137fprintf(stderr, "down ccount\n");
144fprintf(stderr, "down ccount\n");
138# endif
145#endif
139 if (--ccount <= 0) {
146 if (--ccount <= 0) {
140# if D2
141fprintf(stderr, "ex loop ccount %d instr %o\n",ccount, instr);
142# endif
147#if D2
148fprintf(stderr, "ex loop ccount %d instr %o\n", ccount, instr);
149#endif
143 if (instr) break;
144 if (p == &buf[2*BUFSIZ]) p = buf;
145 if (p > &buf[BUFSIZ]) {
150 if (instr) break;
151 if (p == &buf[2*BUFSIZ]) p = buf;
152 if (p > &buf[BUFSIZ]) {
146 if ((ccount = read(f, p, &buf[2*BUFSIZ] - p)) <= 0) break;
147 }
148 else if ((ccount = read(f, p, BUFSIZ)) <= 0) break;
149# if D2
153 if ((ccount = read(f, p,
154 &buf[2*BUFSIZ] - p)) <= 0)
155 break;
156 } else if ((ccount = read(f, p, BUFSIZ)) <= 0) break;
157#if D2
150fprintf(stderr, " normal read %d bytres\n", ccount);
158fprintf(stderr, " normal read %d bytres\n", ccount);
151{char xx[20]; sprintf(xx, "they are %%.%ds\n", ccount);
152fprintf(stderr, xx, p);
159{
160 char xx[20];
161 sprintf(xx, "they are %%.%ds\n", ccount);
162 fprintf(stderr, xx, p);
153}
163}
154# endif
164#endif
155 }
156nstate:
157 ch = *p;
165 }
166nstate:
167 ch = *p;
158# if D2
159fprintf(stderr, "roaming along in ex ch %c c %o\n",ch,c);
160# endif
168#if D2
169fprintf(stderr, "roaming along in ex ch %c c %o\n", ch, c);
170#endif
161 if (isupper(ch)) ch |= 040;
162 if (c->inp == ch) {
163 c = c->nst;
171 if (isupper(ch)) ch |= 040;
172 if (c->inp == ch) {
173 c = c->nst;
164 }
165 else if (c->link != 0) {
174 } else if (c->link != 0) {
166 c = c->link;
167 goto nstate;
175 c = c->link;
176 goto nstate;
168 }
169 else {
177 } else {
170 c = c->fail;
178 c = c->fail;
171 if (c==0) {
179 if (c == 0) {
172 c = www;
173istate:
174 if (c->inp == ch) {
175 c = c->nst;
180 c = www;
181istate:
182 if (c->inp == ch) {
183 c = c->nst;
176 }
177 else if (c->link != 0) {
184 } else if (c->link != 0) {
178 c = c->link;
179 goto istate;
180 }
185 c = c->link;
186 goto istate;
187 }
181 }
182 else goto nstate;
188 } else goto nstate;
183 }
189 }
184 if (c->out && new (c)) {
185# if D2
186fprintf(stderr, " found: nfound %d need %d\n",nfound,need);
187# endif
188 if (++nfound >= need)
189 {
190# if D1
191fprintf(stderr, "found, p %o nlp %o ccount %d buf %o buf[2*BUFSIZ] %o\n",p,nlp,ccount,buf,buf+2*BUFSIZ);
192# endif
193 if (instr==0)
190 if (c->out && new(c)) {
191#if D2
192fprintf(stderr, " found: nfound %d need %d\n", nfound, need);
193#endif
194 if (++nfound >= need) {
195#if D1
196fprintf(stderr, "found, p %o nlp %o ccount %d buf %o buf[2*BUFSIZ] %o\n",
197 p, nlp, ccount, buf, buf+2*BUFSIZ);
198#endif
199 if (instr == 0)
194 while (*p++ != '\n') {
200 while (*p++ != '\n') {
195# if D3
201#if D3
196fprintf(stderr, "down ccount2\n");
202fprintf(stderr, "down ccount2\n");
197# endif
203#endif
198 if (--ccount <= 0) {
204 if (--ccount <= 0) {
199 if (p == &buf[2*BUFSIZ]) p = buf;
205 if (p == &buf[2*BUFSIZ])
206 p = buf;
200 if (p > &buf[BUFSIZ]) {
207 if (p > &buf[BUFSIZ]) {
201 if ((ccount = read(f, p, &buf[2*BUFSIZ] - p)) <= 0) break;
202 }
203 else if ((ccount = read(f, p, BUFSIZ)) <= 0) break;
204# if D2
205fprintf(stderr, " read %d bytes\n",ccount);
208 if ((ccount = read(f, p,
209 &buf[2*BUFSIZ] - p))
210 <= 0)
211 break;
212 } else if ((ccount = read(f, p,
213 BUFSIZ)) <= 0)
214 break;
215#if D2
216fprintf(stderr, " read %d bytes\n", ccount);
206{ char xx[20]; sprintf(xx, "they are %%.%ds\n", ccount);
207fprintf(stderr, xx, p);
208}
217{ char xx[20]; sprintf(xx, "they are %%.%ds\n", ccount);
218fprintf(stderr, xx, p);
219}
209# endif
220#endif
210 }
211 }
212 nsucc = 1;
221 }
222 }
223 nsucc = 1;
213 if (rflag==0)
214 {
215# if D2
216fprintf(stderr, "p %o nlp %o buf %o\n",p,nlp,buf);
217if (p>nlp)
218{write (2, "XX\n", 3); write (2, nlp, p-nlp); write (2, "XX\n", 3);}
219# endif
224 if (rflag == 0) {
225#if D2
226fprintf(stderr, "p %o nlp %o buf %o\n", p, nlp, buf);
227if (p > nlp)
228{write(2, "XX\n", 3); write(2, nlp, p-nlp); write(2, "XX\n", 3); }
229#endif
220 if (p > nlp) write(1, nlp, p-nlp);
221 else {
230 if (p > nlp) write(1, nlp, p-nlp);
231 else {
222 write(1, nlp, &buf[2*BUFSIZ] - nlp);
232 write(1, nlp,
233 &buf[2*BUFSIZ] - nlp);
223 write(1, buf, p-&buf[0]);
234 write(1, buf, p-&buf[0]);
224 }
225 if (p[-1]!= '\n') write (1, "\n", 1);
226 }
235 }
227 if (instr==0)
228 {
236 if (p[-1] != '\n') write(1, "\n", 1);
237 }
238 if (instr == 0) {
229 nlp = p;
230 c = www;
239 nlp = p;
240 c = www;
231 nfound=0;
232 }
233 }
234 else
241 nfound = 0;
242 }
243 } else
235 ccount++;
236 continue;
237 }
244 ccount++;
245 continue;
246 }
238# if D2
239fprintf(stderr, "nr end loop p %o\n",p);
240# endif
247#if D2
248fprintf(stderr, "nr end loop p %o\n", p);
249#endif
241 if (instr)
242 p++;
243 else
244 if (*p++ == '\n')
245 {
246 nlp = p;
247 c = www;
250 if (instr)
251 p++;
252 else
253 if (*p++ == '\n')
254 {
255 nlp = p;
256 c = www;
248 nfound=0;
257 nfound = 0;
249 }
250 }
258 }
259 }
251 if (instr==0)
260 if (instr == 0)
252 close(f);
253}
254
261 close(f);
262}
263
255cgotofn() {
256 register c;
257 register struct words *s;
264static void
265cgotofn(void)
266{
267 char c;
268 struct words *s;
258 s = smax = www;
269 s = smax = www;
259nword:
260 for(;;) {
261# if D1
262 fprintf(stderr, " in for loop c now %o %c\n",c, c>' ' ? c : ' ');
263# endif
264 if ((c = gch())==0) return;
270nword:
271 for (;;) {
272#if D1
273 fprintf(stderr, " in for loop c now %o %c\n", c, c > ' ' ? c : ' ');
274#endif
275 if ((c = gch()) == 0)
276 return;
265 else if (c == '\n') {
266 s->out = 1;
267 s = www;
277 else if (c == '\n') {
278 s->out = 1;
279 s = www;
268 }
269 else {
270loop:
280 } else {
281loop:
271 if (s->inp == c) {
272 s = s->nst;
273 continue;
274 }
275 if (s->inp == 0) goto enter;
276 if (s->link == 0) {
277 if (smax >= &www[MAXSIZ - 1]) overflo();
278 s->link = ++smax;

--- 6 unchanged lines hidden (view full) ---

285 }
286
287enter:
288 do {
289 s->inp = c;
290 if (smax >= &www[MAXSIZ - 1]) overflo();
291 s->nst = ++smax;
292 s = smax;
282 if (s->inp == c) {
283 s = s->nst;
284 continue;
285 }
286 if (s->inp == 0) goto enter;
287 if (s->link == 0) {
288 if (smax >= &www[MAXSIZ - 1]) overflo();
289 s->link = ++smax;

--- 6 unchanged lines hidden (view full) ---

296 }
297
298enter:
299 do {
300 s->inp = c;
301 if (smax >= &www[MAXSIZ - 1]) overflo();
302 s->nst = ++smax;
303 s = smax;
293 }
294 while ((c = gch()) != '\n');
304 }
305 while ((c = gch()) != '\n')
306 ;
295 smax->out = 1;
296 s = www;
297 numwords++;
298 goto nword;
299
300}
301
307 smax->out = 1;
308 s = www;
309 numwords++;
310 goto nword;
311
312}
313
302gch()
314static char
315gch(void)
303{
304 static char *s;
316{
317 static char *s;
305 if (flag==0)
306 {
307 flag=1;
318 if (flag == 0) {
319 flag = 1;
308 s = *xargv++;
320 s = *xargv++;
309# if D1
321#if D1
310 fprintf(stderr, "next arg is %s xargc %d\n", xargc > 0 ? s : "", xargc);
322 fprintf(stderr, "next arg is %s xargc %d\n", xargc > 0 ? s : "", xargc);
311# endif
312 if (xargc-- <=0) return(0);
323#endif
324 if (xargc-- <= 0)
325 return (0);
313 }
326 }
314 if (*s) return(*s++);
315 for(flag=0; flag<2*BUFSIZ; flag++)
316 buf[flag]=0;
317 flag=0;
318 return('\n');
327 if (*s)
328 return (*s++);
329 for (flag = 0; flag < 2*BUFSIZ; flag++)
330 buf[flag] = 0;
331 flag = 0;
332 return ('\n');
319}
320
333}
334
321overflo() {
322 write(2,"wordlist too large\n", 19);
335static void
336overflo(void)
337{
338 write(2, "wordlist too large\n", 19);
323 exit(2);
324}
339 exit(2);
340}
325cfail() {
341
342static void
343cfail(void)
344{
326 struct words *queue[QSIZE];
327 struct words **front, **rear;
328 struct words *state;
345 struct words *queue[QSIZE];
346 struct words **front, **rear;
347 struct words *state;
329 register char c;
330 register struct words *s;
348 char c;
349 struct words *s;
331 s = www;
332 front = rear = queue;
350 s = www;
351 front = rear = queue;
333init:
352init:
334 if ((s->inp) != 0) {
335 *rear++ = s->nst;
336 if (rear >= &queue[QSIZE - 1]) overflo();
337 }
338 if ((s = s->link) != 0) {
339 goto init;
340 }
341
353 if ((s->inp) != 0) {
354 *rear++ = s->nst;
355 if (rear >= &queue[QSIZE - 1]) overflo();
356 }
357 if ((s = s->link) != 0) {
358 goto init;
359 }
360
342 while (rear!=front) {
361 while (rear != front) {
343 s = *front;
344 if (front == &queue[QSIZE-1])
345 front = queue;
346 else front++;
362 s = *front;
363 if (front == &queue[QSIZE-1])
364 front = queue;
365 else front++;
347cloop:
366cloop:
348 if ((c = s->inp) != 0) {
349 *rear = (q = s->nst);
350 if (front < rear)
351 if (rear >= &queue[QSIZE-1])
352 if (front == queue) overflo();
353 else rear = queue;
354 else rear++;
355 else
356 if (++rear == front) overflo();
357 state = s->fail;
367 if ((c = s->inp) != 0) {
368 *rear = (q = s->nst);
369 if (front < rear)
370 if (rear >= &queue[QSIZE-1])
371 if (front == queue) overflo();
372 else rear = queue;
373 else rear++;
374 else
375 if (++rear == front) overflo();
376 state = s->fail;
358floop:
377floop:
359 if (state == 0) state = www;
360 if (state->inp == c) {
361 q->fail = state->nst;
362 if ((state->nst)->out == 1) q->out = 1;
363 continue;
378 if (state == 0) state = www;
379 if (state->inp == c) {
380 q->fail = state->nst;
381 if ((state->nst)->out == 1) q->out = 1;
382 continue;
364 }
365 else if ((state = state->link) != 0)
383 } else if ((state = state->link) != 0)
366 goto floop;
367 }
368 if ((s = s->link) != 0)
369 goto cloop;
370 }
371}
372
384 goto floop;
385 }
386 if ((s = s->link) != 0)
387 goto cloop;
388 }
389}
390
373static int seen[50];
374new (x)
391static struct words *seen[50];
392
393static int
394new(struct words *x)
375{
376 int i;
395{
396 int i;
377 for(i=0; i<nfound; i++)
378 if (seen[i]==x)
379 return(0);
380 seen[i]=x;
381 return(1);
397 for (i = 0; i < nfound; i++)
398 if (seen[i] == x)
399 return (0);
400 seen[i] = x;
401 return (1);
382}
402}