First version of grepper: grep with error correction.
[idzebra-moved-to-github.git] / dfa / lexer.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: lexer.c,v $
7  * Revision 1.2  1994-09-27 16:31:20  adam
8  * First version of grepper: grep with error correction.
9  *
10  * Revision 1.1  1994/09/26  10:16:55  adam
11  * First version of dfa module in alex. This version uses yacc to parse
12  * regular expressions. This should be hand-made instead.
13  *
14  *
15  * Adam Dickmeiss.      1992-1993
16  * This module is actually very old...
17  */
18 #include <stdio.h>
19 #include <assert.h>
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdarg.h>
24
25 #include <util.h>
26 #include <dfa.h>
27 #include "imalloc.h"
28 #include "lexer.h"
29
30 static char *prog;
31
32
33 void error (const char *format, ...)
34 {
35     va_list argptr;
36     va_start (argptr, format);
37     fprintf (stderr, "%s error: ", prog);
38     (void) vfprintf (stderr, format, argptr);
39     putc ('\n', stderr);
40     exit (1);
41 }
42
43 #ifdef YACC
44 extern int yydebug;
45 #else
46 extern int alexdebug;
47 #endif
48 int ccluse = 0;
49
50 static int lexer_options (int argc, char **argv)
51 {
52     while (--argc > 0)
53         if (**++argv == '-')
54             while (*++*argv)
55             {
56                 switch (**argv)
57                 {
58                 case 'V':
59                     fprintf (stderr, "%s: %s %s\n", prog, __DATE__, __TIME__);
60                     continue;
61                 case 'v':
62                     dfa_verbose = 1;
63                     continue;
64                 case 't':
65 #ifdef YACC
66                     yydebug = 1;
67 #else
68                     alexdebug = 1;
69 #endif
70                     continue;
71                 case 'c':
72                     ccluse = 1;
73                     continue;
74                 case 'd':
75                     switch (*++*argv)
76                     {
77                     case 's':
78                         debug_dfa_tran = 1;
79                         break;
80                     case 't':
81                         debug_dfa_trav = 1;
82                         break;
83                     case 'f':
84                         debug_dfa_followpos = 1;
85                         break;
86                     default:
87                         --*argv;
88                         debug_dfa_tran = 1;
89                         debug_dfa_followpos = 1;
90                         debug_dfa_trav = 1;
91                     }
92                     continue;
93                 default:
94                     fprintf (stderr, "%s: unknown option `-%s'\n",
95                              prog, *argv);
96                     return 1;
97                 }
98                 break;
99             }
100     return 0;
101 }
102
103 int main (int argc, char **argv)
104 {
105     int i, no = 0;
106     DFA *dfa;
107     DFA_states *dfas;
108
109     prog = *argv;
110 #ifdef YACC
111     yydebug = 0;
112 #else
113     alexdebug = 0;
114 #endif
115     i = lexer_options (argc, argv);
116     if (i)
117         return i;
118
119     if (argc < 2)
120     {
121         fprintf (stderr, "usage\n  %s [-c] [-V] [-v] [-t] [-d[stf]] file\n",
122                  prog);
123         return 1;
124     }
125     else while (--argc > 0)
126             if (**++argv != '-' && **argv)
127             {
128                 ++no;
129                 i = read_file (*argv, &dfa);
130                 if (i)
131                     return i;
132                 dfas = mk_dfas (dfa, 2000);
133                 rm_dfa (&dfa);
134                 rm_dfas (&dfas);
135             }
136 #ifdef MEMDEBUG
137     imemstat();
138 #endif
139     if (!no)
140     {
141         fprintf (stderr, "%s: no files specified\n", prog);
142         return 2;
143     }
144     return 0;
145 }
146