2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.4 1995-01-24 16:00:21 adam
8 * Added -ansi to CFLAGS.
9 * Some changes to the dfa module.
11 * Revision 1.3 1994/09/27 16:31:18 adam
12 * First version of grepper: grep with error correction.
14 * Revision 1.2 1994/09/26 16:30:56 adam
15 * Minor changes. imalloc uses xmalloc now.
17 * Revision 1.1 1994/09/26 10:16:52 adam
18 * First version of dfa module in alex. This version uses yacc to parse
19 * regular expressions. This should be hand-made instead.
28 #include <sys/types.h>
44 void error (const char *format, ...)
47 va_start (argptr, format);
48 fprintf (stderr, "%s error: ", prog);
49 (void) vfprintf (stderr, format, argptr);
62 static int show_lines = 0;
64 int agrep_options (argc, argv)
75 fprintf (stderr, "%s: %s %s\n", prog, __DATE__, __TIME__);
102 debug_dfa_followpos = 1;
107 debug_dfa_followpos = 1;
112 fprintf (stderr, "%s: unknown option `-%s'\n", prog, *argv);
120 #define INF_BUF_SIZE 32768U
121 static char *inf_buf;
122 static char *inf_ptr, *inf_flsh;
123 static int inf_eof, line_no;
125 static int inf_flush (fd)
131 r = (unsigned) (inf_buf+INF_BUF_SIZE - inf_ptr); /* no of `wrap' bytes */
133 memcpy (inf_buf, inf_ptr, r);
134 inf_ptr = p = inf_buf + r;
135 b = INF_BUF_SIZE - r;
137 if ((r = read (fd, p, b)) == (unsigned) -1)
147 while ((b -= r) > 0);
148 while (p != inf_buf && *--p != '\n')
150 while (p != inf_buf && *--p != '\n')
156 static char *prline (p)
162 while (p != inf_buf && p[-1] != '\n')
169 printf ("%5d:\t%s\n", line_no, p0);
176 static int go (fd, dfaar)
178 struct DFA_state **dfaar;
180 struct DFA_state *s = dfaar[0];
188 for (c = *inf_ptr++, t=s->trans, i=s->tran_no; --i >= 0; t++)
189 if (c >= t->ch[0] && c <= t->ch[1])
194 if ((s = dfaar[t->to])->rule_no)
196 inf_ptr = prline (inf_ptr);
200 for (t=s->trans, i=s->tran_no; --i >= 0; t++)
201 if ((unsigned) *p >= t->ch[0]
202 && (unsigned) *p <= t->ch[1])
212 if (inf_ptr == inf_flsh)
219 fprintf (stderr, "%s: read error\n", prog);
229 struct DFA_state **dfas;
232 inf_buf = imalloc (sizeof(char)*INF_BUF_SIZE);
234 inf_ptr = inf_buf+INF_BUF_SIZE;
245 int main (argc, argv)
249 char *pattern = NULL;
252 struct DFA *dfa = dfa_init();
262 setbuf (stdout, outbuf);
263 i = agrep_options (argc, argv);
267 if (**++argv != '-' && **argv)
271 i = dfa_parse (dfa, &pattern);
274 fprintf (stderr, "%s: illegal pattern\n", prog);
282 fd = open (*argv, O_RDONLY | O_BINARY);
285 fprintf (stderr, "%s: couldn't open `%s'\n", prog, *argv);
288 i = agrep (dfa->states, fd);
295 fprintf (stderr, "%s: no files specified\n", prog);