ace18cc3e6439d5e6cf3dfde0a49789512fbf016
[idzebra-moved-to-github.git] / dfa / lexer.c
1 /*
2  * Copyright (C) 1994-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: lexer.c,v $
7  * Revision 1.11  1999-02-02 14:50:10  adam
8  * Updated WIN32 code specific sections. Changed header.
9  *
10  * Revision 1.10  1996/10/29 13:57:27  adam
11  * Include of zebrautl.h instead of alexutil.h.
12  *
13  * Revision 1.9  1996/05/14 11:33:41  adam
14  * MEMDEBUG turned off by default.
15  *
16  * Revision 1.8  1995/09/28  09:18:54  adam
17  * Removed various preprocessor defines.
18  *
19  * Revision 1.7  1995/09/04  12:33:27  adam
20  * Various cleanup. YAZ util used instead.
21  *
22  * Revision 1.6  1995/01/25  11:30:51  adam
23  * Simple error reporting when parsing regular expressions.
24  * Memory usage reduced.
25  *
26  * Revision 1.5  1995/01/24  16:00:22  adam
27  * Added -ansi to CFLAGS.
28  * Some changes to the dfa module.
29  *
30  * Revision 1.4  1994/10/04  17:46:44  adam
31  * Function options now returns arg with error option.
32  *
33  * Revision 1.3  1994/10/03  17:22:19  adam
34  * Optimization of grepper.
35  *
36  * Revision 1.2  1994/09/27  16:31:20  adam
37  * First version of grepper: grep with error correction.
38  *
39  * Revision 1.1  1994/09/26  10:16:55  adam
40  * First version of dfa module in alex. This version uses yacc to parse
41  * regular expressions. This should be hand-made instead.
42  *
43  *
44  * Adam Dickmeiss.      1992-1993
45  * This module is actually very old...
46  */
47 #include <stdio.h>
48 #include <assert.h>
49
50 #include <stdlib.h>
51 #include <string.h>
52 #include <stdarg.h>
53
54 #include <zebrautl.h>
55 #include <dfa.h>
56 #include "imalloc.h"
57 #include "lexer.h"
58
59 static char *prog;
60
61
62 void error (const char *format, ...)
63 {
64     va_list argptr;
65     va_start (argptr, format);
66     fprintf (stderr, "%s error: ", prog);
67     (void) vfprintf (stderr, format, argptr);
68     putc ('\n', stderr);
69     exit (1);
70 }
71
72 int ccluse = 0;
73
74 static int lexer_options (int argc, char **argv)
75 {
76     while (--argc > 0)
77         if (**++argv == '-')
78             while (*++*argv)
79             {
80                 switch (**argv)
81                 {
82                 case 'V':
83                     fprintf (stderr, "%s: %s %s\n", prog, __DATE__, __TIME__);
84                     continue;
85                 case 's':
86                     dfa_verbose = 1;
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     dfa = dfa_init ();
127     i = lexer_options (argc, argv);
128     if (i)
129         return i;
130
131     if (argc < 2)
132     {
133         fprintf (stderr, "usage\n  %s [-c] [-V] [-s] [-t] [-d[stf]] file\n",
134                  prog);
135         return 1;
136     }
137     else while (--argc > 0)
138         if (**++argv != '-' && **argv)
139         {
140             ++no;
141             
142             i = read_file (*argv, dfa);
143             if (i)
144                 return i;
145             dfa_mkstate (dfa);
146
147 #if MEMDEBUG
148     imemstat();
149 #endif
150         }
151     dfa_delete (&dfa);
152 #if MEMDEBUG
153     imemstat();
154 #endif
155     if (!no)
156     {
157         fprintf (stderr, "%s: no files specified\n", prog);
158         return 2;
159     }
160     return 0;
161 }
162