1 /* $Id: agrep.c,v 1.16 2005-06-14 20:28:53 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
30 #include <sys/types.h>
41 #include <idzebra/util.h>
51 void error (const char *format, ...)
54 va_start (argptr, format);
55 fprintf (stderr, "%s error: ", prog);
56 (void) vfprintf (stderr, format, argptr);
61 static int show_lines = 0;
63 int agrep_options (argc, argv)
74 fprintf (stderr, "%s: %s %s\n", prog, __DATE__, __TIME__);
92 debug_dfa_followpos = 1;
97 debug_dfa_followpos = 1;
102 fprintf (stderr, "%s: unknown option `-%s'\n", prog, *argv);
110 #define INF_BUF_SIZE 32768U
111 static char *inf_buf;
112 static char *inf_ptr, *inf_flsh;
113 static int inf_eof, line_no;
115 static int inf_flush (fd)
121 r = (unsigned) (inf_buf+INF_BUF_SIZE - inf_ptr); /* no of `wrap' bytes */
123 memcpy (inf_buf, inf_ptr, r);
124 inf_ptr = p = inf_buf + r;
125 b = INF_BUF_SIZE - r;
127 if ((r = read (fd, p, b)) == (unsigned) -1)
137 while ((b -= r) > 0);
138 while (p != inf_buf && *--p != '\n')
140 while (p != inf_buf && *--p != '\n')
146 static char *prline (p)
152 while (p != inf_buf && p[-1] != '\n')
159 printf ("%5d:\t%s\n", line_no, p0);
166 static int go (fd, dfaar)
168 struct DFA_state **dfaar;
170 struct DFA_state *s = dfaar[0];
179 for (c = *inf_ptr++, t=s->trans, i=s->tran_no; --i >= 0; t++)
180 if (c >= t->ch[0] && c <= t->ch[1])
185 if ((s = dfaar[t->to])->rule_no &&
186 (start_line || s->rule_nno))
188 inf_ptr = prline (inf_ptr);
192 for (t=s->trans, i=s->tran_no; --i >= 0; t++)
193 if ((unsigned) *p >= t->ch[0]
194 && (unsigned) *p <= t->ch[1])
205 if (inf_ptr == inf_flsh)
212 fprintf (stderr, "%s: read error\n", prog);
224 struct DFA_state **dfas;
227 inf_buf = imalloc (sizeof(char)*INF_BUF_SIZE);
229 inf_ptr = inf_buf+INF_BUF_SIZE;
240 int main (argc, argv)
244 const char *pattern = NULL;
247 struct DFA *dfa = dfa_init();
252 fprintf (stderr, "usage: agrep [options] pattern file..\n");
253 fprintf (stderr, " -v dfa verbose\n");
254 fprintf (stderr, " -n show lines\n");
255 fprintf (stderr, " -d debug\n");
256 fprintf (stderr, " -V show version\n");
259 setbuf (stdout, outbuf);
260 i = agrep_options (argc, argv);
264 if (**++argv != '-' && **argv)
269 i = dfa_parse (dfa, &pattern);
272 fprintf (stderr, "%s: illegal pattern\n", prog);
280 fd = open (*argv, O_RDONLY | O_BINARY);
283 fprintf (stderr, "%s: couldn't open `%s'\n", prog, *argv);
286 i = agrep (dfa->states, fd);
294 fprintf (stderr, "usage:\n "
295 " %s [-d] [-v] [-n] [-f] pattern file ..\n", prog);