2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.9 1996-10-29 13:57:18 adam
8 * Include of zebrautl.h instead of alexutil.h.
10 * Revision 1.8 1996/01/08 09:09:16 adam
11 * Function dfa_parse got 'const' string argument.
12 * New functions to define char mappings made public.
14 * Revision 1.7 1995/10/16 09:31:24 adam
17 * Revision 1.6 1995/09/28 09:18:51 adam
18 * Removed various preprocessor defines.
20 * Revision 1.5 1995/09/04 12:33:25 adam
21 * Various cleanup. YAZ util used instead.
23 * Revision 1.4 1995/01/24 16:00:21 adam
24 * Added -ansi to CFLAGS.
25 * Some changes to the dfa module.
27 * Revision 1.3 1994/09/27 16:31:18 adam
28 * First version of grepper: grep with error correction.
30 * Revision 1.2 1994/09/26 16:30:56 adam
31 * Minor changes. imalloc uses xmalloc now.
33 * Revision 1.1 1994/09/26 10:16:52 adam
34 * First version of dfa module in alex. This version uses yacc to parse
35 * regular expressions. This should be hand-made instead.
44 #include <sys/types.h>
60 void error (const char *format, ...)
63 va_start (argptr, format);
64 fprintf (stderr, "%s error: ", prog);
65 (void) vfprintf (stderr, format, argptr);
70 static int show_lines = 0;
72 int agrep_options (argc, argv)
83 fprintf (stderr, "%s: %s %s\n", prog, __DATE__, __TIME__);
101 debug_dfa_followpos = 1;
106 debug_dfa_followpos = 1;
111 fprintf (stderr, "%s: unknown option `-%s'\n", prog, *argv);
119 #define INF_BUF_SIZE 32768U
120 static char *inf_buf;
121 static char *inf_ptr, *inf_flsh;
122 static int inf_eof, line_no;
124 static int inf_flush (fd)
130 r = (unsigned) (inf_buf+INF_BUF_SIZE - inf_ptr); /* no of `wrap' bytes */
132 memcpy (inf_buf, inf_ptr, r);
133 inf_ptr = p = inf_buf + r;
134 b = INF_BUF_SIZE - r;
136 if ((r = read (fd, p, b)) == (unsigned) -1)
146 while ((b -= r) > 0);
147 while (p != inf_buf && *--p != '\n')
149 while (p != inf_buf && *--p != '\n')
155 static char *prline (p)
161 while (p != inf_buf && p[-1] != '\n')
168 printf ("%5d:\t%s\n", line_no, p0);
175 static int go (fd, dfaar)
177 struct DFA_state **dfaar;
179 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 &&
195 (start_line || s->rule_nno))
197 inf_ptr = prline (inf_ptr);
201 for (t=s->trans, i=s->tran_no; --i >= 0; t++)
202 if ((unsigned) *p >= t->ch[0]
203 && (unsigned) *p <= t->ch[1])
214 if (inf_ptr == inf_flsh)
221 fprintf (stderr, "%s: read error\n", prog);
233 struct DFA_state **dfas;
236 inf_buf = imalloc (sizeof(char)*INF_BUF_SIZE);
238 inf_ptr = inf_buf+INF_BUF_SIZE;
249 int main (argc, argv)
253 const char *pattern = NULL;
256 struct DFA *dfa = dfa_init();
261 fprintf (stderr, "usage: agrep [options] pattern file..\n");
262 fprintf (stderr, " -v dfa verbose\n");
263 fprintf (stderr, " -n show lines\n");
264 fprintf (stderr, " -d debug\n");
265 fprintf (stderr, " -V show version\n");
268 setbuf (stdout, outbuf);
269 i = agrep_options (argc, argv);
273 if (**++argv != '-' && **argv)
277 i = dfa_parse (dfa, &pattern);
280 fprintf (stderr, "%s: illegal pattern\n", prog);
288 fd = open (*argv, O_RDONLY | O_BINARY);
291 fprintf (stderr, "%s: couldn't open `%s'\n", prog, *argv);
294 i = agrep (dfa->states, fd);
301 fprintf (stderr, "usage:\n "
302 " %s [-d] [-v] [-n] [-f] pattern file ..\n", prog);