2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 1997-09-09 13:37:57 adam
8 * Partial port to WIN95/NT.
10 * Revision 1.9 1996/10/29 13:57:18 adam
11 * Include of zebrautl.h instead of alexutil.h.
13 * Revision 1.8 1996/01/08 09:09:16 adam
14 * Function dfa_parse got 'const' string argument.
15 * New functions to define char mappings made public.
17 * Revision 1.7 1995/10/16 09:31:24 adam
20 * Revision 1.6 1995/09/28 09:18:51 adam
21 * Removed various preprocessor defines.
23 * Revision 1.5 1995/09/04 12:33:25 adam
24 * Various cleanup. YAZ util used instead.
26 * Revision 1.4 1995/01/24 16:00:21 adam
27 * Added -ansi to CFLAGS.
28 * Some changes to the dfa module.
30 * Revision 1.3 1994/09/27 16:31:18 adam
31 * First version of grepper: grep with error correction.
33 * Revision 1.2 1994/09/26 16:30:56 adam
34 * Minor changes. imalloc uses xmalloc now.
36 * Revision 1.1 1994/09/26 10:16:52 adam
37 * First version of dfa module in alex. This version uses yacc to parse
38 * regular expressions. This should be hand-made instead.
47 #include <sys/types.h>
66 void error (const char *format, ...)
69 va_start (argptr, format);
70 fprintf (stderr, "%s error: ", prog);
71 (void) vfprintf (stderr, format, argptr);
76 static int show_lines = 0;
78 int agrep_options (argc, argv)
89 fprintf (stderr, "%s: %s %s\n", prog, __DATE__, __TIME__);
107 debug_dfa_followpos = 1;
112 debug_dfa_followpos = 1;
117 fprintf (stderr, "%s: unknown option `-%s'\n", prog, *argv);
125 #define INF_BUF_SIZE 32768U
126 static char *inf_buf;
127 static char *inf_ptr, *inf_flsh;
128 static int inf_eof, line_no;
130 static int inf_flush (fd)
136 r = (unsigned) (inf_buf+INF_BUF_SIZE - inf_ptr); /* no of `wrap' bytes */
138 memcpy (inf_buf, inf_ptr, r);
139 inf_ptr = p = inf_buf + r;
140 b = INF_BUF_SIZE - r;
142 if ((r = read (fd, p, b)) == (unsigned) -1)
152 while ((b -= r) > 0);
153 while (p != inf_buf && *--p != '\n')
155 while (p != inf_buf && *--p != '\n')
161 static char *prline (p)
167 while (p != inf_buf && p[-1] != '\n')
174 printf ("%5d:\t%s\n", line_no, p0);
181 static int go (fd, dfaar)
183 struct DFA_state **dfaar;
185 struct DFA_state *s = dfaar[0];
194 for (c = *inf_ptr++, t=s->trans, i=s->tran_no; --i >= 0; t++)
195 if (c >= t->ch[0] && c <= t->ch[1])
200 if ((s = dfaar[t->to])->rule_no &&
201 (start_line || s->rule_nno))
203 inf_ptr = prline (inf_ptr);
207 for (t=s->trans, i=s->tran_no; --i >= 0; t++)
208 if ((unsigned) *p >= t->ch[0]
209 && (unsigned) *p <= t->ch[1])
220 if (inf_ptr == inf_flsh)
227 fprintf (stderr, "%s: read error\n", prog);
239 struct DFA_state **dfas;
242 inf_buf = imalloc (sizeof(char)*INF_BUF_SIZE);
244 inf_ptr = inf_buf+INF_BUF_SIZE;
255 int main (argc, argv)
259 const char *pattern = NULL;
262 struct DFA *dfa = dfa_init();
267 fprintf (stderr, "usage: agrep [options] pattern file..\n");
268 fprintf (stderr, " -v dfa verbose\n");
269 fprintf (stderr, " -n show lines\n");
270 fprintf (stderr, " -d debug\n");
271 fprintf (stderr, " -V show version\n");
274 setbuf (stdout, outbuf);
275 i = agrep_options (argc, argv);
279 if (**++argv != '-' && **argv)
283 i = dfa_parse (dfa, &pattern);
286 fprintf (stderr, "%s: illegal pattern\n", prog);
294 fd = open (*argv, O_RDONLY | O_BINARY);
297 fprintf (stderr, "%s: couldn't open `%s'\n", prog, *argv);
300 i = agrep (dfa->states, fd);
307 fprintf (stderr, "usage:\n "
308 " %s [-d] [-v] [-n] [-f] pattern file ..\n", prog);