2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.12 1999-05-26 07:49:12 adam
10 * Revision 1.11 1999/02/02 14:50:03 adam
11 * Updated WIN32 code specific sections. Changed header.
13 * Revision 1.10 1997/09/09 13:37:57 adam
14 * Partial port to WIN95/NT.
16 * Revision 1.9 1996/10/29 13:57:18 adam
17 * Include of zebrautl.h instead of alexutil.h.
19 * Revision 1.8 1996/01/08 09:09:16 adam
20 * Function dfa_parse got 'const' string argument.
21 * New functions to define char mappings made public.
23 * Revision 1.7 1995/10/16 09:31:24 adam
26 * Revision 1.6 1995/09/28 09:18:51 adam
27 * Removed various preprocessor defines.
29 * Revision 1.5 1995/09/04 12:33:25 adam
30 * Various cleanup. YAZ util used instead.
32 * Revision 1.4 1995/01/24 16:00:21 adam
33 * Added -ansi to CFLAGS.
34 * Some changes to the dfa module.
36 * Revision 1.3 1994/09/27 16:31:18 adam
37 * First version of grepper: grep with error correction.
39 * Revision 1.2 1994/09/26 16:30:56 adam
40 * Minor changes. imalloc uses xmalloc now.
42 * Revision 1.1 1994/09/26 10:16:52 adam
43 * First version of dfa module in alex. This version uses yacc to parse
44 * regular expressions. This should be hand-made instead.
53 #include <sys/types.h>
75 void error (const char *format, ...)
78 va_start (argptr, format);
79 fprintf (stderr, "%s error: ", prog);
80 (void) vfprintf (stderr, format, argptr);
85 static int show_lines = 0;
87 int agrep_options (argc, argv)
98 fprintf (stderr, "%s: %s %s\n", prog, __DATE__, __TIME__);
116 debug_dfa_followpos = 1;
121 debug_dfa_followpos = 1;
126 fprintf (stderr, "%s: unknown option `-%s'\n", prog, *argv);
134 #define INF_BUF_SIZE 32768U
135 static char *inf_buf;
136 static char *inf_ptr, *inf_flsh;
137 static int inf_eof, line_no;
139 static int inf_flush (fd)
145 r = (unsigned) (inf_buf+INF_BUF_SIZE - inf_ptr); /* no of `wrap' bytes */
147 memcpy (inf_buf, inf_ptr, r);
148 inf_ptr = p = inf_buf + r;
149 b = INF_BUF_SIZE - r;
151 if ((r = read (fd, p, b)) == (unsigned) -1)
161 while ((b -= r) > 0);
162 while (p != inf_buf && *--p != '\n')
164 while (p != inf_buf && *--p != '\n')
170 static char *prline (p)
176 while (p != inf_buf && p[-1] != '\n')
183 printf ("%5d:\t%s\n", line_no, p0);
190 static int go (fd, dfaar)
192 struct DFA_state **dfaar;
194 struct DFA_state *s = dfaar[0];
203 for (c = *inf_ptr++, t=s->trans, i=s->tran_no; --i >= 0; t++)
204 if (c >= t->ch[0] && c <= t->ch[1])
209 if ((s = dfaar[t->to])->rule_no &&
210 (start_line || s->rule_nno))
212 inf_ptr = prline (inf_ptr);
216 for (t=s->trans, i=s->tran_no; --i >= 0; t++)
217 if ((unsigned) *p >= t->ch[0]
218 && (unsigned) *p <= t->ch[1])
229 if (inf_ptr == inf_flsh)
236 fprintf (stderr, "%s: read error\n", prog);
248 struct DFA_state **dfas;
251 inf_buf = imalloc (sizeof(char)*INF_BUF_SIZE);
253 inf_ptr = inf_buf+INF_BUF_SIZE;
264 int main (argc, argv)
268 const char *pattern = NULL;
271 struct DFA *dfa = dfa_init();
276 fprintf (stderr, "usage: agrep [options] pattern file..\n");
277 fprintf (stderr, " -v dfa verbose\n");
278 fprintf (stderr, " -n show lines\n");
279 fprintf (stderr, " -d debug\n");
280 fprintf (stderr, " -V show version\n");
283 setbuf (stdout, outbuf);
284 i = agrep_options (argc, argv);
288 if (**++argv != '-' && **argv)
293 i = dfa_parse (dfa, &pattern);
296 fprintf (stderr, "%s: illegal pattern\n", prog);
304 fd = open (*argv, O_RDONLY | O_BINARY);
307 fprintf (stderr, "%s: couldn't open `%s'\n", prog, *argv);
310 i = agrep (dfa->states, fd);
318 fprintf (stderr, "usage:\n "
319 " %s [-d] [-v] [-n] [-f] pattern file ..\n", prog);