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