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