2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.2 1994-09-26 16:30:56 adam
8 * Minor changes. imalloc uses xmalloc now.
10 * Revision 1.1 1994/09/26 10:16:52 adam
11 * First version of dfa module in alex. This version uses yacc to parse
12 * regular expressions. This should be hand-made instead.
21 #include <sys/types.h>
37 void error( const char *format, ... )
40 va_start( argptr, format );
41 fprintf( stderr, "%s error: ", prog );
42 (void) vfprintf( stderr, format, argptr );
55 static int show_lines = 0;
57 int agrep_options( argc, argv )
68 fprintf( stderr, "%s: %s %s\n", prog, __DATE__, __TIME__ );
95 debug_dfa_followpos = 1;
100 debug_dfa_followpos = 1;
105 fprintf( stderr, "%s: unknown option `-%s'\n", prog, *argv );
113 #define INF_BUF_SIZE 32768U
114 static char *inf_buf;
115 static char *inf_ptr, *inf_flsh;
116 static int inf_eof, line_no;
118 static int inf_flush( fd )
124 r = (unsigned) (inf_buf+INF_BUF_SIZE - inf_ptr); /* no of `wrap' bytes */
126 memcpy( inf_buf, inf_ptr, r );
127 inf_ptr = p = inf_buf + r;
128 b = INF_BUF_SIZE - r;
130 if( (r = read( fd, p, b ) ) == (unsigned) -1 )
140 while( (b -= r) > 0 );
141 while( p != inf_buf && *--p != '\n' )
143 while( p != inf_buf && *--p != '\n' )
149 static char *prline( p )
155 while( p != inf_buf && p[-1] != '\n' )
158 while( *p++ != '\n' )
162 printf( "%5d:\t%s\n", line_no, p0 );
169 static int go( fd, dfaar )
173 DFA_state *s = dfaar[0];
181 for( c = *inf_ptr++, t=s->trans, i=s->tran_no; --i >= 0; t++ )
182 if( c >= t->ch[0] && c <= t->ch[1] )
187 if( (s = dfaar[t->to] )->rule_no )
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] )
205 if( inf_ptr == inf_flsh )
210 if( inf_flush( fd ) )
212 fprintf( stderr, "%s: read error\n", prog );
221 int agrep( dfas, fd )
225 inf_buf = imalloc( sizeof(char)*INF_BUF_SIZE );
227 inf_ptr = inf_buf+INF_BUF_SIZE;
231 go( fd, dfas->sortarray);
238 int main( argc, argv )
242 char *pattern = NULL;
245 DFA *dfa = init_dfa();
256 setbuf( stdout, outbuf );
257 i = agrep_options( argc, argv );
261 if( **++argv != '-' && **argv )
265 i = parse_dfa( dfa, &pattern, dfa_thompson_chars );
268 fprintf( stderr, "%s: illegal pattern\n", prog );
271 dfa->root = dfa->top;
272 dfas = mk_dfas( dfa, 200 );
278 fd = open( *argv, O_RDONLY | O_BINARY);
281 fprintf( stderr, "%s: couldn't open `%s'\n", prog, *argv );
284 i = agrep( dfas, fd );
291 fprintf( stderr, "%s: no files specified\n", prog );