File: readfile.c

package info (click to toggle)
idzebra 2.2.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,572 kB
  • sloc: ansic: 54,389; xml: 27,058; sh: 5,892; makefile: 1,102; perl: 210; tcl: 64
file content (175 lines) | stat: -rw-r--r-- 3,877 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* This file is part of the Zebra server.
   Copyright (C) Index Data

Zebra is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.

Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/


#if HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <assert.h>

#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include <idzebra/util.h>
#include <dfa.h>
#include "lexer.h"

#define MAXLINE 512

static FILE *inf;
static FILE *outf;
static const char *inf_name;
static int line_no;
static int err_no;

static void
    prep        (char **s),
    read_defs   (void),
    read_rules  (struct DFA *dfap),
    read_tail   (void);

static char
    *read_line  (void);

static void prep (char **s)
{
    static char expr_buf[MAXLINE+1];
    char *dst = expr_buf;
    const char *src = *s;
    int c;

    while ((c = *src++))
        *dst++ = c;

    *dst = '\0';
    *s = expr_buf;
}

static char *read_line (void)
{
    static char linebuf[MAXLINE+1];
    ++line_no;
    return fgets (linebuf, MAXLINE, inf);
}

static void read_defs (void)
{
    const char *s;
    while ((s=read_line()))
    {
        if (*s == '%' && s[1] == '%')
            return;
        else if (*s == '\0' || isspace (*s))
            fputs (s, outf);
    }
    error ("missing rule section");
}

static void read_rules (struct DFA *dfa)
{
    char *s;
    const char *sc;
    int i;
    int no = 0;

    fputs ("\n#ifndef YY_BREAK\n#define YY_BREAK break;\n#endif\n", outf);
    fputs ("void lexact (int no)\n{\n", outf);
    fputs (  "\tswitch (no)\n\t{\n", outf);
    while ((s=read_line()))
    {
        if (*s == '%' && s[1] == '%')
            break;
        else if (*s == '\0' || isspace (*s))
            /* copy rest of line to output */
            fputs (s, outf);
        else
        {
            /* preprocess regular expression */
            prep (&s);
            /* now parse regular expression */
            sc = s;
            i = dfa_parse (dfa, &sc);
            if (i)
            {
                fprintf (stderr, "%s #%d: regular expression syntax error\n",
                        inf_name, line_no);
                assert (0);
                err_no++;
            }
            else
            {
                if (no)
                    fputs ("\t\tYY_BREAK\n", outf);
                no++;
                fprintf (outf, "\tcase %d:\n#line %d\n\t\t", no, line_no);
            }
            while (*sc == '\t' || *sc == ' ')
                sc++;
            fputs (sc, outf);
        }
    }
    fputs ("\tYY_BREAK\n\t}\n}\n", outf);
    if (!no)
        error ("no regular expressions in rule section");
}

static void read_tail (void)
{
    const char *s;
    while ((s=read_line()))
        fputs (s, outf);
}

int read_file (const char *s, struct DFA *dfa)
{
    inf_name = s;
    if (!(inf=fopen (s,"r")))
    {
        error ("cannot open `%s'", s);
        return -1;
    }

    if (!(outf=fopen ("lex.yy.c", "w")))
    {
        error ("cannot open `%s'", "lex.yy.c");
        return -2;
    }

    line_no = 0;
    err_no = 0;

    read_defs ();
    read_rules (dfa);
    read_tail ();

    fclose (outf);
    fclose (inf);
    return err_no;
}
/*
 * Local variables:
 * c-basic-offset: 4
 * c-file-style: "Stroustrup"
 * indent-tabs-mode: nil
 * End:
 * vim: shiftwidth=4 tabstop=8 expandtab
 */