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