1 /* $Id: agrep.c,v 1.13 2002-08-02 19:26:55 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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>
52 void error (const char *format, ...)
55 va_start (argptr, format);
56 fprintf (stderr, "%s error: ", prog);
57 (void) vfprintf (stderr, format, argptr);
62 static int show_lines = 0;
64 int agrep_options (argc, argv)
75 fprintf (stderr, "%s: %s %s\n", prog, __DATE__, __TIME__);
93 debug_dfa_followpos = 1;
98 debug_dfa_followpos = 1;
103 fprintf (stderr, "%s: unknown option `-%s'\n", prog, *argv);
111 #define INF_BUF_SIZE 32768U
112 static char *inf_buf;
113 static char *inf_ptr, *inf_flsh;
114 static int inf_eof, line_no;
116 static int inf_flush (fd)
122 r = (unsigned) (inf_buf+INF_BUF_SIZE - inf_ptr); /* no of `wrap' bytes */
124 memcpy (inf_buf, inf_ptr, r);
125 inf_ptr = p = inf_buf + r;
126 b = INF_BUF_SIZE - r;
128 if ((r = read (fd, p, b)) == (unsigned) -1)
138 while ((b -= r) > 0);
139 while (p != inf_buf && *--p != '\n')
141 while (p != inf_buf && *--p != '\n')
147 static char *prline (p)
153 while (p != inf_buf && p[-1] != '\n')
160 printf ("%5d:\t%s\n", line_no, p0);
167 static int go (fd, dfaar)
169 struct DFA_state **dfaar;
171 struct DFA_state *s = dfaar[0];
180 for (c = *inf_ptr++, t=s->trans, i=s->tran_no; --i >= 0; t++)
181 if (c >= t->ch[0] && c <= t->ch[1])
186 if ((s = dfaar[t->to])->rule_no &&
187 (start_line || s->rule_nno))
189 inf_ptr = prline (inf_ptr);
193 for (t=s->trans, i=s->tran_no; --i >= 0; t++)
194 if ((unsigned) *p >= t->ch[0]
195 && (unsigned) *p <= t->ch[1])
206 if (inf_ptr == inf_flsh)
213 fprintf (stderr, "%s: read error\n", prog);
225 struct DFA_state **dfas;
228 inf_buf = imalloc (sizeof(char)*INF_BUF_SIZE);
230 inf_ptr = inf_buf+INF_BUF_SIZE;
241 int main (argc, argv)
245 const char *pattern = NULL;
248 struct DFA *dfa = dfa_init();
253 fprintf (stderr, "usage: agrep [options] pattern file..\n");
254 fprintf (stderr, " -v dfa verbose\n");
255 fprintf (stderr, " -n show lines\n");
256 fprintf (stderr, " -d debug\n");
257 fprintf (stderr, " -V show version\n");
260 setbuf (stdout, outbuf);
261 i = agrep_options (argc, argv);
265 if (**++argv != '-' && **argv)
270 i = dfa_parse (dfa, &pattern);
273 fprintf (stderr, "%s: illegal pattern\n", prog);
281 fd = open (*argv, O_RDONLY | O_BINARY);
284 fprintf (stderr, "%s: couldn't open `%s'\n", prog, *argv);
287 i = agrep (dfa->states, fd);
295 fprintf (stderr, "usage:\n "
296 " %s [-d] [-v] [-n] [-f] pattern file ..\n", prog);