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