2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 1999-02-02 14:50:03 adam
8 * Updated WIN32 code specific sections. Changed header.
10 * Revision 1.10 1997/09/09 13:37:57 adam
11 * Partial port to WIN95/NT.
13 * Revision 1.9 1996/10/29 13:57:18 adam
14 * Include of zebrautl.h instead of alexutil.h.
16 * Revision 1.8 1996/01/08 09:09:16 adam
17 * Function dfa_parse got 'const' string argument.
18 * New functions to define char mappings made public.
20 * Revision 1.7 1995/10/16 09:31:24 adam
23 * Revision 1.6 1995/09/28 09:18:51 adam
24 * Removed various preprocessor defines.
26 * Revision 1.5 1995/09/04 12:33:25 adam
27 * Various cleanup. YAZ util used instead.
29 * Revision 1.4 1995/01/24 16:00:21 adam
30 * Added -ansi to CFLAGS.
31 * Some changes to the dfa module.
33 * Revision 1.3 1994/09/27 16:31:18 adam
34 * First version of grepper: grep with error correction.
36 * Revision 1.2 1994/09/26 16:30:56 adam
37 * Minor changes. imalloc uses xmalloc now.
39 * Revision 1.1 1994/09/26 10:16:52 adam
40 * First version of dfa module in alex. This version uses yacc to parse
41 * regular expressions. This should be hand-made instead.
50 #include <sys/types.h>
72 void error (const char *format, ...)
75 va_start (argptr, format);
76 fprintf (stderr, "%s error: ", prog);
77 (void) vfprintf (stderr, format, argptr);
82 static int show_lines = 0;
84 int agrep_options (argc, argv)
95 fprintf (stderr, "%s: %s %s\n", prog, __DATE__, __TIME__);
113 debug_dfa_followpos = 1;
118 debug_dfa_followpos = 1;
123 fprintf (stderr, "%s: unknown option `-%s'\n", prog, *argv);
131 #define INF_BUF_SIZE 32768U
132 static char *inf_buf;
133 static char *inf_ptr, *inf_flsh;
134 static int inf_eof, line_no;
136 static int inf_flush (fd)
142 r = (unsigned) (inf_buf+INF_BUF_SIZE - inf_ptr); /* no of `wrap' bytes */
144 memcpy (inf_buf, inf_ptr, r);
145 inf_ptr = p = inf_buf + r;
146 b = INF_BUF_SIZE - r;
148 if ((r = read (fd, p, b)) == (unsigned) -1)
158 while ((b -= r) > 0);
159 while (p != inf_buf && *--p != '\n')
161 while (p != inf_buf && *--p != '\n')
167 static char *prline (p)
173 while (p != inf_buf && p[-1] != '\n')
180 printf ("%5d:\t%s\n", line_no, p0);
187 static int go (fd, dfaar)
189 struct DFA_state **dfaar;
191 struct DFA_state *s = dfaar[0];
200 for (c = *inf_ptr++, t=s->trans, i=s->tran_no; --i >= 0; t++)
201 if (c >= t->ch[0] && c <= t->ch[1])
206 if ((s = dfaar[t->to])->rule_no &&
207 (start_line || s->rule_nno))
209 inf_ptr = prline (inf_ptr);
213 for (t=s->trans, i=s->tran_no; --i >= 0; t++)
214 if ((unsigned) *p >= t->ch[0]
215 && (unsigned) *p <= t->ch[1])
226 if (inf_ptr == inf_flsh)
233 fprintf (stderr, "%s: read error\n", prog);
245 struct DFA_state **dfas;
248 inf_buf = imalloc (sizeof(char)*INF_BUF_SIZE);
250 inf_ptr = inf_buf+INF_BUF_SIZE;
261 int main (argc, argv)
265 const char *pattern = NULL;
268 struct DFA *dfa = dfa_init();
273 fprintf (stderr, "usage: agrep [options] pattern file..\n");
274 fprintf (stderr, " -v dfa verbose\n");
275 fprintf (stderr, " -n show lines\n");
276 fprintf (stderr, " -d debug\n");
277 fprintf (stderr, " -V show version\n");
280 setbuf (stdout, outbuf);
281 i = agrep_options (argc, argv);
285 if (**++argv != '-' && **argv)
289 i = dfa_parse (dfa, &pattern);
292 fprintf (stderr, "%s: illegal pattern\n", prog);
300 fd = open (*argv, O_RDONLY | O_BINARY);
303 fprintf (stderr, "%s: couldn't open `%s'\n", prog, *argv);
306 i = agrep (dfa->states, fd);
313 fprintf (stderr, "usage:\n "
314 " %s [-d] [-v] [-n] [-f] pattern file ..\n", prog);