2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1996-01-08 09:09:16 adam
8 * Function dfa_parse got 'const' string argument.
9 * New functions to define char mappings made public.
11 * Revision 1.7 1995/10/16 09:31:24 adam
14 * Revision 1.6 1995/09/28 09:18:51 adam
15 * Removed various preprocessor defines.
17 * Revision 1.5 1995/09/04 12:33:25 adam
18 * Various cleanup. YAZ util used instead.
20 * Revision 1.4 1995/01/24 16:00:21 adam
21 * Added -ansi to CFLAGS.
22 * Some changes to the dfa module.
24 * Revision 1.3 1994/09/27 16:31:18 adam
25 * First version of grepper: grep with error correction.
27 * Revision 1.2 1994/09/26 16:30:56 adam
28 * Minor changes. imalloc uses xmalloc now.
30 * Revision 1.1 1994/09/26 10:16:52 adam
31 * First version of dfa module in alex. This version uses yacc to parse
32 * regular expressions. This should be hand-made instead.
41 #include <sys/types.h>
57 void error (const char *format, ...)
60 va_start (argptr, format);
61 fprintf (stderr, "%s error: ", prog);
62 (void) vfprintf (stderr, format, argptr);
67 static int show_lines = 0;
69 int agrep_options (argc, argv)
80 fprintf (stderr, "%s: %s %s\n", prog, __DATE__, __TIME__);
98 debug_dfa_followpos = 1;
103 debug_dfa_followpos = 1;
108 fprintf (stderr, "%s: unknown option `-%s'\n", prog, *argv);
116 #define INF_BUF_SIZE 32768U
117 static char *inf_buf;
118 static char *inf_ptr, *inf_flsh;
119 static int inf_eof, line_no;
121 static int inf_flush (fd)
127 r = (unsigned) (inf_buf+INF_BUF_SIZE - inf_ptr); /* no of `wrap' bytes */
129 memcpy (inf_buf, inf_ptr, r);
130 inf_ptr = p = inf_buf + r;
131 b = INF_BUF_SIZE - r;
133 if ((r = read (fd, p, b)) == (unsigned) -1)
143 while ((b -= r) > 0);
144 while (p != inf_buf && *--p != '\n')
146 while (p != inf_buf && *--p != '\n')
152 static char *prline (p)
158 while (p != inf_buf && p[-1] != '\n')
165 printf ("%5d:\t%s\n", line_no, p0);
172 static int go (fd, dfaar)
174 struct DFA_state **dfaar;
176 struct DFA_state *s = dfaar[0];
185 for (c = *inf_ptr++, t=s->trans, i=s->tran_no; --i >= 0; t++)
186 if (c >= t->ch[0] && c <= t->ch[1])
191 if ((s = dfaar[t->to])->rule_no &&
192 (start_line || s->rule_nno))
194 inf_ptr = prline (inf_ptr);
198 for (t=s->trans, i=s->tran_no; --i >= 0; t++)
199 if ((unsigned) *p >= t->ch[0]
200 && (unsigned) *p <= t->ch[1])
211 if (inf_ptr == inf_flsh)
218 fprintf (stderr, "%s: read error\n", prog);
230 struct DFA_state **dfas;
233 inf_buf = imalloc (sizeof(char)*INF_BUF_SIZE);
235 inf_ptr = inf_buf+INF_BUF_SIZE;
246 int main (argc, argv)
250 const char *pattern = NULL;
253 struct DFA *dfa = dfa_init();
258 fprintf (stderr, "usage: agrep [options] pattern file..\n");
259 fprintf (stderr, " -v dfa verbose\n");
260 fprintf (stderr, " -n show lines\n");
261 fprintf (stderr, " -d debug\n");
262 fprintf (stderr, " -V show version\n");
265 setbuf (stdout, outbuf);
266 i = agrep_options (argc, argv);
270 if (**++argv != '-' && **argv)
274 i = dfa_parse (dfa, &pattern);
277 fprintf (stderr, "%s: illegal pattern\n", prog);
285 fd = open (*argv, O_RDONLY | O_BINARY);
288 fprintf (stderr, "%s: couldn't open `%s'\n", prog, *argv);
291 i = agrep (dfa->states, fd);
298 fprintf (stderr, "usage:\n "
299 " %s [-d] [-v] [-n] [-f] pattern file ..\n", prog);