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