2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.1 1994-09-26 10:16:52 adam
8 * First version of dfa module in alex. This version uses yacc to parse
9 * regular expressions. This should be hand-made instead.
18 #include <sys/types.h>
34 void error( const char *format, ... )
37 va_start( argptr, format );
38 fprintf( stderr, "%s error: ", prog );
39 (void) vfprintf( stderr, format, argptr );
52 static int show_lines = 0;
54 int agrep_options( argc, argv )
66 fprintf( stderr, "%s: %s %s\n", prog, __DATE__, __TIME__ );
94 debug_dfa_followpos = 1;
99 debug_dfa_followpos = 1;
104 fprintf( stderr, "%s: unknown option `-%s'\n", prog, *argv );
112 #define INF_BUF_SIZE 32768U
113 static char *inf_buf;
114 static char *inf_ptr, *inf_flsh;
115 static int inf_eof, line_no;
117 static int inf_flush( fd )
123 r = (unsigned) (inf_buf+INF_BUF_SIZE - inf_ptr); /* no of `wrap' bytes */
125 memcpy( inf_buf, inf_ptr, r );
126 inf_ptr = p = inf_buf + r;
127 b = INF_BUF_SIZE - r;
129 if( (r = read( fd, p, b ) ) == (unsigned) -1 )
139 while( (b -= r) > 0 );
140 while( p != inf_buf && *--p != '\n' )
142 while( p != inf_buf && *--p != '\n' )
148 static char *prline( p )
154 while( p != inf_buf && p[-1] != '\n' )
157 while( *p++ != '\n' )
161 printf( "%5d:\t%s\n", line_no, p0 );
168 static int go( fd, dfaar )
172 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 )
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] )
204 if( inf_ptr == inf_flsh )
209 if( inf_flush( fd ) )
211 fprintf( stderr, "%s: read error\n", prog );
220 int agrep( dfas, fd )
224 inf_buf = imalloc( sizeof(char)*INF_BUF_SIZE );
226 inf_ptr = inf_buf+INF_BUF_SIZE;
230 go( fd, dfas->sortarray);
237 int main( argc, argv )
241 char *pattern = NULL;
244 DFA *dfa = init_dfa();
255 setbuf( stdout, outbuf );
256 i = agrep_options( argc, argv );
260 if( **++argv != '-' && **argv )
264 i = parse_dfa( dfa, &pattern, grep_chars );
267 fprintf( stderr, "%s: illegal pattern\n", prog );
270 dfa->root = dfa->top;
271 dfas = mk_dfas( dfa, 200 );
277 fd = open( *argv, O_RDONLY | O_BINARY);
280 fprintf( stderr, "%s: couldn't open `%s'\n", prog, *argv );
283 i = agrep( dfas, fd );
290 fprintf( stderr, "%s: no files specified\n", prog );