1 /* This file is part of the Zebra server.
2 Copyright (C) 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
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 (int argc, char **argv)
72 fprintf (stderr, "%s: %s %s\n", prog, __DATE__, __TIME__);
90 debug_dfa_followpos = 1;
95 debug_dfa_followpos = 1;
100 fprintf (stderr, "%s: unknown option `-%s'\n", prog, *argv);
108 #define INF_BUF_SIZE 32768U
109 static char *inf_buf;
110 static char *inf_ptr, *inf_flsh;
111 static int inf_eof, line_no;
113 static int inf_flush (int fd)
118 r = (unsigned) (inf_buf+INF_BUF_SIZE - inf_ptr); /* no of `wrap' bytes */
120 memcpy (inf_buf, inf_ptr, r);
121 inf_ptr = p = inf_buf + r;
122 b = INF_BUF_SIZE - r;
124 if ((r = read (fd, p, b)) == (unsigned) -1)
134 while ((b -= r) > 0);
135 while (p != inf_buf && *--p != '\n')
137 while (p != inf_buf && *--p != '\n')
143 static char *prline (char *p)
148 while (p != inf_buf && p[-1] != '\n')
155 printf ("%5d:\t%s\n", line_no, p0);
162 static int go (int fd, struct DFA_state **dfaar)
164 struct DFA_state *s = dfaar[0];
173 for (c = *inf_ptr++, t=s->trans, i=s->tran_no; --i >= 0; t++)
174 if (c >= t->ch[0] && c <= t->ch[1])
179 if ((s = dfaar[t->to])->rule_no &&
180 (start_line || s->rule_nno))
182 inf_ptr = prline (inf_ptr);
186 for (t=s->trans, i=s->tran_no; --i >= 0; t++)
187 if ((unsigned) *p >= t->ch[0]
188 && (unsigned) *p <= t->ch[1])
199 if (inf_ptr == inf_flsh)
206 fprintf (stderr, "%s: read error\n", prog);
217 int agrep (struct DFA_state **dfas, int fd)
219 inf_buf = imalloc (sizeof(char)*INF_BUF_SIZE);
221 inf_ptr = inf_buf+INF_BUF_SIZE;
232 int main (int argc, char **argv)
234 const char *pattern = NULL;
237 struct DFA *dfa = dfa_init();
242 fprintf (stderr, "usage: agrep [options] pattern file..\n");
243 fprintf (stderr, " -v dfa verbose\n");
244 fprintf (stderr, " -n show lines\n");
245 fprintf (stderr, " -d debug\n");
246 fprintf (stderr, " -V show version\n");
249 setbuf (stdout, outbuf);
250 i = agrep_options (argc, argv);
254 if (**++argv != '-' && **argv)
259 i = dfa_parse (dfa, &pattern);
262 fprintf (stderr, "%s: illegal pattern\n", prog);
270 fd = open (*argv, O_RDONLY | O_BINARY);
273 fprintf (stderr, "%s: couldn't open `%s'\n", prog, *argv);
276 i = agrep (dfa->states, fd);
284 fprintf (stderr, "usage:\n "
285 " %s [-d] [-v] [-n] [-f] pattern file ..\n", prog);
295 * c-file-style: "Stroustrup"
296 * indent-tabs-mode: nil
298 * vim: shiftwidth=4 tabstop=8 expandtab