IDZEBRA 2.2.8
readfile.c
Go to the documentation of this file.
1/* This file is part of the Zebra server.
2 Copyright (C) Index Data
3
4Zebra is free software; you can redistribute it and/or modify it under
5the terms of the GNU General Public License as published by the Free
6Software Foundation; either version 2, or (at your option) any later
7version.
8
9Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10WARRANTY; without even the implied warranty of MERCHANTABILITY or
11FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
18*/
19
20
21#if HAVE_CONFIG_H
22#include <config.h>
23#endif
24#include <stdio.h>
25#include <assert.h>
26
27#include <stdlib.h>
28#include <string.h>
29#include <ctype.h>
30
31#include <idzebra/util.h>
32#include <dfa.h>
33#include "lexer.h"
34
35#define MAXLINE 512
36
37static FILE *inf;
38static FILE *outf;
39static const char *inf_name;
40static int line_no;
41static int err_no;
42
43static void
44 prep (char **s),
45 read_defs (void),
46 read_rules (struct DFA *dfap),
47 read_tail (void);
48
49static char
50 *read_line (void);
51
52static void prep (char **s)
53{
54 static char expr_buf[MAXLINE+1];
55 char *dst = expr_buf;
56 const char *src = *s;
57 int c;
58
59 while ((c = *src++))
60 *dst++ = c;
61
62 *dst = '\0';
63 *s = expr_buf;
64}
65
66static char *read_line (void)
67{
68 static char linebuf[MAXLINE+1];
69 ++line_no;
70 return fgets (linebuf, MAXLINE, inf);
71}
72
73static void read_defs (void)
74{
75 const char *s;
76 while ((s=read_line()))
77 {
78 if (*s == '%' && s[1] == '%')
79 return;
80 else if (*s == '\0' || isspace (*s))
81 fputs (s, outf);
82 }
83 error ("missing rule section");
84}
85
86static void read_rules (struct DFA *dfa)
87{
88 char *s;
89 const char *sc;
90 int i;
91 int no = 0;
92
93 fputs ("\n#ifndef YY_BREAK\n#define YY_BREAK break;\n#endif\n", outf);
94 fputs ("void lexact (int no)\n{\n", outf);
95 fputs ( "\tswitch (no)\n\t{\n", outf);
96 while ((s=read_line()))
97 {
98 if (*s == '%' && s[1] == '%')
99 break;
100 else if (*s == '\0' || isspace (*s))
101 /* copy rest of line to output */
102 fputs (s, outf);
103 else
104 {
105 /* preprocess regular expression */
106 prep (&s);
107 /* now parse regular expression */
108 sc = s;
109 i = dfa_parse (dfa, &sc);
110 if (i)
111 {
112 fprintf (stderr, "%s #%d: regular expression syntax error\n",
114 assert (0);
115 err_no++;
116 }
117 else
118 {
119 if (no)
120 fputs ("\t\tYY_BREAK\n", outf);
121 no++;
122 fprintf (outf, "\tcase %d:\n#line %d\n\t\t", no, line_no);
123 }
124 while (*sc == '\t' || *sc == ' ')
125 sc++;
126 fputs (sc, outf);
127 }
128 }
129 fputs ("\tYY_BREAK\n\t}\n}\n", outf);
130 if (!no)
131 error ("no regular expressions in rule section");
132}
133
134static void read_tail (void)
135{
136 const char *s;
137 while ((s=read_line()))
138 fputs (s, outf);
139}
140
141int read_file (const char *s, struct DFA *dfa)
142{
143 inf_name = s;
144 if (!(inf=fopen (s,"r")))
145 {
146 error ("cannot open `%s'", s);
147 return -1;
148 }
149
150 if (!(outf=fopen ("lex.yy.c", "w")))
151 {
152 error ("cannot open `%s'", "lex.yy.c");
153 return -2;
154 }
155
156 line_no = 0;
157 err_no = 0;
158
159 read_defs ();
160 read_rules (dfa);
161 read_tail ();
162
163 fclose (outf);
164 fclose (inf);
165 return err_no;
166}
167/*
168 * Local variables:
169 * c-basic-offset: 4
170 * c-file-style: "Stroustrup"
171 * indent-tabs-mode: nil
172 * End:
173 * vim: shiftwidth=4 tabstop=8 expandtab
174 */
175
void error(const char *format,...)
Definition agrep.c:51
int dfa_parse(struct DFA *, const char **)
Definition dfa.c:1121
static FILE * outf
Definition readfile.c:38
static void read_tail(void)
Definition readfile.c:134
static FILE * inf
Definition readfile.c:37
#define MAXLINE
Definition readfile.c:35
static int err_no
Definition readfile.c:41
static const char * inf_name
Definition readfile.c:39
static void read_rules(struct DFA *dfap)
Definition readfile.c:86
int read_file(const char *s, struct DFA *dfa)
Definition readfile.c:141
static int line_no
Definition readfile.c:40
static void prep(char **s)
Definition readfile.c:52
static void read_defs(void)
Definition readfile.c:73
static char * read_line(void)
Definition readfile.c:66
Definition dfa.h:53