aa6e3c61ea3d79d3ecd45c01900a5fe27805cf33
[idzebra-moved-to-github.git] / dfa / lexer.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1995-2008 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20
21 #include <stdio.h>
22 #include <assert.h>
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdarg.h>
27
28 #include <idzebra/util.h>
29 #include <dfa.h>
30 #include "imalloc.h"
31 #include "lexer.h"
32
33 static char *prog;
34
35
36 void error (const char *format, ...)
37 {
38     va_list argptr;
39     va_start (argptr, format);
40     fprintf (stderr, "%s error: ", prog);
41     (void) vfprintf (stderr, format, argptr);
42     putc ('\n', stderr);
43     exit (1);
44 }
45
46 int ccluse = 0;
47
48 static int lexer_options (int argc, char **argv)
49 {
50     while (--argc > 0)
51         if (**++argv == '-')
52             while (*++*argv)
53             {
54                 switch (**argv)
55                 {
56                 case 'V':
57                     fprintf (stderr, "%s: %s %s\n", prog, __DATE__, __TIME__);
58                     continue;
59                 case 's':
60                     dfa_verbose = 1;
61                     continue;
62                 case 'c':
63                     ccluse = 1;
64                     continue;
65                 case 'd':
66                     switch (*++*argv)
67                     {
68                     case 's':
69                         debug_dfa_tran = 1;
70                         break;
71                     case 't':
72                         debug_dfa_trav = 1;
73                         break;
74                     case 'f':
75                         debug_dfa_followpos = 1;
76                         break;
77                     default:
78                         --*argv;
79                         debug_dfa_tran = 1;
80                         debug_dfa_followpos = 1;
81                         debug_dfa_trav = 1;
82                     }
83                     continue;
84                 default:
85                     fprintf (stderr, "%s: unknown option `-%s'\n",
86                              prog, *argv);
87                     return 1;
88                 }
89                 break;
90             }
91     return 0;
92 }
93
94 int main (int argc, char **argv)
95 {
96     int i, no = 0;
97     struct DFA *dfa;
98
99     prog = *argv;
100     dfa = dfa_init ();
101     i = lexer_options (argc, argv);
102     if (i)
103         return i;
104
105     if (argc < 2)
106     {
107         fprintf (stderr, "usage\n  %s [-c] [-V] [-s] [-t] [-d[stf]] file\n",
108                  prog);
109         return 1;
110     }
111     else while (--argc > 0)
112         if (**++argv != '-' && **argv)
113         {
114             ++no;
115             
116             i = read_file (*argv, dfa);
117             if (i)
118                 return i;
119             dfa_mkstate (dfa);
120
121 #if MEMDEBUG
122     imemstat();
123 #endif
124         }
125     dfa_delete (&dfa);
126 #if MEMDEBUG
127     imemstat();
128 #endif
129     if (!no)
130     {
131         fprintf (stderr, "%s: no files specified\n", prog);
132         return 2;
133     }
134     return 0;
135 }
136
137 /*
138  * Local variables:
139  * c-basic-offset: 4
140  * indent-tabs-mode: nil
141  * End:
142  * vim: shiftwidth=4 tabstop=8 expandtab
143  */
144