2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.3 1994-09-27 16:31:18 adam
8 * First version of grepper: grep with error correction.
10 * Revision 1.2 1994/09/26 16:30:56 adam
11 * Minor changes. imalloc uses xmalloc now.
13 * Revision 1.1 1994/09/26 10:16:52 adam
14 * First version of dfa module in alex. This version uses yacc to parse
15 * regular expressions. This should be hand-made instead.
24 #include <sys/types.h>
40 void error (const char *format, ...)
43 va_start (argptr, format);
44 fprintf (stderr, "%s error: ", prog);
45 (void) vfprintf (stderr, format, argptr);
58 static int show_lines = 0;
60 int agrep_options (argc, argv)
71 fprintf (stderr, "%s: %s %s\n", prog, __DATE__, __TIME__);
98 debug_dfa_followpos = 1;
103 debug_dfa_followpos = 1;
108 fprintf (stderr, "%s: unknown option `-%s'\n", prog, *argv);
116 #define INF_BUF_SIZE 32768U
117 static char *inf_buf;
118 static char *inf_ptr, *inf_flsh;
119 static int inf_eof, line_no;
121 static int inf_flush (fd)
127 r = (unsigned) (inf_buf+INF_BUF_SIZE - inf_ptr); /* no of `wrap' bytes */
129 memcpy (inf_buf, inf_ptr, r);
130 inf_ptr = p = inf_buf + r;
131 b = INF_BUF_SIZE - r;
133 if ((r = read (fd, p, b)) == (unsigned) -1)
143 while ((b -= r) > 0);
144 while (p != inf_buf && *--p != '\n')
146 while (p != inf_buf && *--p != '\n')
152 static char *prline (p)
158 while (p != inf_buf && p[-1] != '\n')
165 printf ("%5d:\t%s\n", line_no, p0);
172 static int go (fd, dfaar)
176 DFA_state *s = dfaar[0];
184 for (c = *inf_ptr++, t=s->trans, i=s->tran_no; --i >= 0; t++)
185 if (c >= t->ch[0] && c <= t->ch[1])
190 if ((s = dfaar[t->to])->rule_no)
192 inf_ptr = prline (inf_ptr);
196 for (t=s->trans, i=s->tran_no; --i >= 0; t++)
197 if ((unsigned) *p >= t->ch[0]
198 && (unsigned) *p <= t->ch[1])
208 if (inf_ptr == inf_flsh)
215 fprintf (stderr, "%s: read error\n", prog);
228 inf_buf = imalloc (sizeof(char)*INF_BUF_SIZE);
230 inf_ptr = inf_buf+INF_BUF_SIZE;
234 go (fd, dfas->sortarray);
241 int main (argc, argv)
245 char *pattern = NULL;
248 DFA *dfa = init_dfa();
259 setbuf (stdout, outbuf);
260 i = agrep_options (argc, argv);
264 if (**++argv != '-' && **argv)
268 i = parse_dfa (dfa, &pattern, dfa_thompson_chars);
271 fprintf (stderr, "%s: illegal pattern\n", prog);
274 dfa->root = dfa->top;
275 dfas = mk_dfas (dfa, 200);
281 fd = open (*argv, O_RDONLY | O_BINARY);
284 fprintf (stderr, "%s: couldn't open `%s'\n", prog, *argv);
287 i = agrep (dfas, fd);
294 fprintf (stderr, "%s: no files specified\n", prog);