1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2009 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <sys/types.h>
38 #include <idzebra/util.h>
48 void error (const char *format, ...)
51 va_start (argptr, format);
52 fprintf (stderr, "%s error: ", prog);
53 (void) vfprintf (stderr, format, argptr);
58 static int show_lines = 0;
60 int agrep_options (int argc, char **argv)
69 fprintf (stderr, "%s: %s %s\n", prog, __DATE__, __TIME__);
87 debug_dfa_followpos = 1;
92 debug_dfa_followpos = 1;
97 fprintf (stderr, "%s: unknown option `-%s'\n", prog, *argv);
105 #define INF_BUF_SIZE 32768U
106 static char *inf_buf;
107 static char *inf_ptr, *inf_flsh;
108 static int inf_eof, line_no;
110 static int inf_flush (int fd)
115 r = (unsigned) (inf_buf+INF_BUF_SIZE - inf_ptr); /* no of `wrap' bytes */
117 memcpy (inf_buf, inf_ptr, r);
118 inf_ptr = p = inf_buf + r;
119 b = INF_BUF_SIZE - r;
121 if ((r = read (fd, p, b)) == (unsigned) -1)
131 while ((b -= r) > 0);
132 while (p != inf_buf && *--p != '\n')
134 while (p != inf_buf && *--p != '\n')
140 static char *prline (char *p)
145 while (p != inf_buf && p[-1] != '\n')
152 printf ("%5d:\t%s\n", line_no, p0);
159 static int go (int fd, struct DFA_state **dfaar)
161 struct DFA_state *s = dfaar[0];
170 for (c = *inf_ptr++, t=s->trans, i=s->tran_no; --i >= 0; t++)
171 if (c >= t->ch[0] && c <= t->ch[1])
176 if ((s = dfaar[t->to])->rule_no &&
177 (start_line || s->rule_nno))
179 inf_ptr = prline (inf_ptr);
183 for (t=s->trans, i=s->tran_no; --i >= 0; t++)
184 if ((unsigned) *p >= t->ch[0]
185 && (unsigned) *p <= t->ch[1])
196 if (inf_ptr == inf_flsh)
203 fprintf (stderr, "%s: read error\n", prog);
214 int agrep (struct DFA_state **dfas, int fd)
216 inf_buf = imalloc (sizeof(char)*INF_BUF_SIZE);
218 inf_ptr = inf_buf+INF_BUF_SIZE;
229 int main (int argc, char **argv)
231 const char *pattern = NULL;
234 struct DFA *dfa = dfa_init();
239 fprintf (stderr, "usage: agrep [options] pattern file..\n");
240 fprintf (stderr, " -v dfa verbose\n");
241 fprintf (stderr, " -n show lines\n");
242 fprintf (stderr, " -d debug\n");
243 fprintf (stderr, " -V show version\n");
246 setbuf (stdout, outbuf);
247 i = agrep_options (argc, argv);
251 if (**++argv != '-' && **argv)
256 i = dfa_parse (dfa, &pattern);
259 fprintf (stderr, "%s: illegal pattern\n", prog);
267 fd = open (*argv, O_RDONLY | O_BINARY);
270 fprintf (stderr, "%s: couldn't open `%s'\n", prog, *argv);
273 i = agrep (dfa->states, fd);
281 fprintf (stderr, "usage:\n "
282 " %s [-d] [-v] [-n] [-f] pattern file ..\n", prog);
292 * c-file-style: "Stroustrup"
293 * indent-tabs-mode: nil
295 * vim: shiftwidth=4 tabstop=8 expandtab