2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
7 /* All Rights Reserved */
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.
15 #pragma ident "%Z%%M% %I% %E% SMI"
21 * fgrep -- print all lines containing any of a set of keywords
24 * 0 - ok, and some matches
25 * 1 - ok, but no matches
52 extern void *zalloc();
54 static void cfail(void);
55 static void cgotofn(void);
56 static void execute(void);
57 static char gch(void);
58 static int new(struct words
*x
);
59 static void overflo(void);
62 fgrep(int argc
, char **argv
)
64 nsucc
= need
= inct
= rflag
= numwords
= nfound
= 0;
68 www
= (struct words
*)zalloc(MAXSIZ
, sizeof (*www
));
70 err(gettext("Can't get space for machines"), 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;
76 while (xargc
> 0 && xargv
[0][0] == '-') {
77 switch (xargv
[0][1]) {
78 case 'r': /* return value only */
81 case 'n': /* number of answers needed */
87 inct
= (int)xargv
[2]+2;
89 fprintf(stderr
, "inct %d xargv.2. %o %d\n", inct
, xargv
[2], xargv
[2]);
91 xargv
+= 2; xargc
-= 2;
97 write(2, "bad fgrep call\n", 15);
101 fprintf(stderr
, "before cgoto\n");
105 fprintf(stderr
, "before cfail\n");
109 fprintf(stderr
, "before execute instr %.20s\n", instr
? instr
: "");
110 fprintf(stderr
, "end of string %d %c %c %c\n", inct
,
111 instr
? instr
[inct
-3] : '\0',
112 instr
? instr
[inct
-2] : '\0',
113 instr
? instr
[inct
-1] : '\0');
117 fprintf(stderr
, "returning nsucc %d\n", nsucc
);
118 fprintf(stderr
, "fgrep done www %o\n", www
);
133 ccount
= instr
? inct
: 0;
135 p
= instr
? instr
: buf
;
136 if (need
== 0) need
= numwords
;
140 fprintf(stderr
, "in execute ccount %d inct %d\n", ccount
, inct
);
144 fprintf(stderr
, "down ccount\n");
148 fprintf(stderr
, "ex loop ccount %d instr %o\n", ccount
, instr
);
151 if (p
== &buf
[2*BUFSIZ
]) p
= buf
;
152 if (p
> &buf
[BUFSIZ
]) {
153 if ((ccount
= read(f
, p
,
154 &buf
[2*BUFSIZ
] - p
)) <= 0)
156 } else if ((ccount
= read(f
, p
, BUFSIZ
)) <= 0) break;
158 fprintf(stderr
, " normal read %d bytres\n", ccount
);
161 sprintf(xx
, "they are %%.%ds\n", ccount
);
162 fprintf(stderr
, xx
, p
);
169 fprintf(stderr
, "roaming along in ex ch %c c %o\n", ch
, c
);
171 if (isupper(ch
)) ch
|= 040;
174 } else if (c
->link
!= 0) {
184 } else if (c
->link
!= 0) {
190 if (c
->out
&& new(c
)) {
192 fprintf(stderr
, " found: nfound %d need %d\n", nfound
, need
);
194 if (++nfound
>= need
) {
196 fprintf(stderr
, "found, p %o nlp %o ccount %d buf %o buf[2*BUFSIZ] %o\n",
197 p
, nlp
, ccount
, buf
, buf
+2*BUFSIZ
);
200 while (*p
++ != '\n') {
202 fprintf(stderr
, "down ccount2\n");
205 if (p
== &buf
[2*BUFSIZ
])
207 if (p
> &buf
[BUFSIZ
]) {
208 if ((ccount
= read(f
, p
,
212 } else if ((ccount
= read(f
, p
,
216 fprintf(stderr
, " read %d bytes\n", ccount
);
217 { char xx
[20]; sprintf(xx
, "they are %%.%ds\n", ccount
);
218 fprintf(stderr
, xx
, p
);
226 fprintf(stderr
, "p %o nlp %o buf %o\n", p
, nlp
, buf
);
228 {write(2, "XX\n", 3); write(2, nlp
, p
-nlp
); write(2, "XX\n", 3); }
230 if (p
> nlp
) write(1, nlp
, p
-nlp
);
233 &buf
[2*BUFSIZ
] - nlp
);
234 write(1, buf
, p
-&buf
[0]);
236 if (p
[-1] != '\n') write(1, "\n", 1);
248 fprintf(stderr
, "nr end loop p %o\n", p
);
273 fprintf(stderr
, " in for loop c now %o %c\n", c
, c
> ' ' ? c
: ' ');
275 if ((c
= gch()) == 0)
277 else if (c
== '\n') {
286 if (s
->inp
== 0) goto enter
;
288 if (smax
>= &www
[MAXSIZ
- 1]) overflo();
301 if (smax
>= &www
[MAXSIZ
- 1]) overflo();
305 while ((c
= gch()) != '\n')
322 fprintf(stderr
, "next arg is %s xargc %d\n", xargc
> 0 ? s
: "", xargc
);
329 for (flag
= 0; flag
< 2*BUFSIZ
; flag
++)
338 write(2, "wordlist too large\n", 19);
345 struct words
*queue
[QSIZE
];
346 struct words
**front
, **rear
;
351 front
= rear
= queue
;
355 if (rear
>= &queue
[QSIZE
- 1]) overflo();
357 if ((s
= s
->link
) != 0) {
361 while (rear
!= front
) {
363 if (front
== &queue
[QSIZE
-1])
367 if ((c
= s
->inp
) != 0) {
368 *rear
= (q
= s
->nst
);
370 if (rear
>= &queue
[QSIZE
-1])
371 if (front
== queue
) overflo();
375 if (++rear
== front
) overflo();
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;
383 } else if ((state
= state
->link
) != 0)
386 if ((s
= s
->link
) != 0)
391 static struct words
*seen
[50];
397 for (i
= 0; i
< nfound
; i
++)