Initialize scanner properly.
[fwd] / scan.c
CommitLineData
e82f7154 1/* -*-c-*-
2 *
e91b672f 3 * $Id: scan.c,v 1.5 2002/01/30 09:29:34 mdw Exp $
e82f7154 4 *
5 * Character scanners
6 *
77d0e3dc 7 * (c) 1999 Straylight/Edgeware
e82f7154 8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the `fw' port forwarder.
13 *
14 * `fw' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * `fw' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with `fw'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29/*----- Revision history --------------------------------------------------*
30 *
31 * $Log: scan.c,v $
e91b672f 32 * Revision 1.5 2002/01/30 09:29:34 mdw
33 * Initialize scanner properly.
34 *
372a98e2 35 * Revision 1.4 2001/02/03 20:30:03 mdw
36 * Support re-reading config files on SIGHUP.
37 *
9e1c09df 38 * Revision 1.3 2000/08/01 17:58:10 mdw
39 * Fix subtleties with <ctype.h> functions.
40 *
77d0e3dc 41 * Revision 1.2 1999/07/26 23:24:33 mdw
42 * Complete rewrite. Allow a list of character sources to enable changes
43 * during parsing of syntactic constructs.
44 *
45 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
46 * Initial revision.
e82f7154 47 *
48 */
49
50/*----- Header files ------------------------------------------------------*/
51
52#include "config.h"
53
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57
58#include <mLib/dstr.h>
77d0e3dc 59#include <mLib/sub.h>
e82f7154 60
61#include "scan.h"
62
77d0e3dc 63/*----- File scanner source -----------------------------------------------*/
64
65/* --- File scanner block --- */
66
67typedef struct fscan {
68 scansrc ss;
69 FILE *fp;
70 unsigned f;
71} fscan;
e82f7154 72
77d0e3dc 73/* --- @scan@ --- */
e82f7154 74
77d0e3dc 75static int fscan_scan(scansrc *ss)
e82f7154 76{
77d0e3dc 77 fscan *fs = (fscan *)ss;
78 int ch = getc(fs->fp);
79 if (ch == '\n')
80 fs->ss.line++;
81 return (ch);
e82f7154 82}
83
77d0e3dc 84/* --- @destroy@ --- */
e82f7154 85
77d0e3dc 86static void fscan_destroy(scansrc *ss)
e82f7154 87{
77d0e3dc 88 fscan *fs = (fscan *)ss;
89 if (!(fs->f & SCF_NOCLOSE))
90 fclose(fs->fp);
91 if (fs->f & SCF_FREENAME)
92 free(fs->ss.src);
93 DESTROY(fs);
94}
e82f7154 95
77d0e3dc 96/* --- File scanner operations --- */
97
98static scansrc_ops fscan_ops = { fscan_scan, fscan_destroy };
99
100/* --- @scan_file@ --- *
101 *
102 * Arguments: @FILE *fp@ = pointer to file descriptor
103 * @char *name@ = pointer to source file name
104 * @unsigned f@ = flags
105 *
106 * Returns: A scanner source.
107 *
108 * Use: Creates a new scanner source for reading from a file.
109 */
110
111scansrc *scan_file(FILE *fp, char *name, unsigned f)
112{
372a98e2 113 fscan *fs = CREATE(fscan);
77d0e3dc 114 fs->ss.ops = &fscan_ops;
115 fs->ss.src = name;
116 fs->ss.line = 1;
117 fs->fp = fp;
118 fs->f = f;
119 return (&fs->ss);
120}
121
122/*---- Argv scanner source ------------------------------------------------*/
123
124/* --- Argv scanner block --- */
125
126typedef struct avscan {
127 scansrc ss;
128 char **av;
129 char *p;
130} avscan;
131
132/* --- @scan@ --- */
e82f7154 133
77d0e3dc 134static int avscan_scan(scansrc *ss)
135{
136 avscan *as = (avscan *)ss;
137 int ch;
138 if (!as->p)
139 ch = EOF;
9e1c09df 140 else if ((ch = (unsigned char)*as->p++) == 0) {
77d0e3dc 141 as->ss.line++;
142 as->p = *as->av++;
143 ch = '\n';
144 }
e82f7154 145 return (ch);
146}
147
77d0e3dc 148/* --- @destroy@ --- */
149
150static void avscan_destroy(scansrc *ss)
e82f7154 151{
77d0e3dc 152 avscan *as = (avscan *)ss;
153 DESTROY(as);
e82f7154 154}
155
77d0e3dc 156/* --- Argv scanner operations --- */
157
158static scansrc_ops avscan_ops = { avscan_scan, avscan_destroy };
159
160/* --- @scan_argv@ --- *
161 *
162 * Arguments: @char **av@ = pointer to argument array (null terminated)
163 *
164 * Returns: A scanner source.
165 *
166 * Use: Creates a new scanner source for reading from an @argv@
167 * array.
168 */
169
170scansrc *scan_argv(char **av)
e82f7154 171{
77d0e3dc 172 avscan *as = CREATE(avscan);
173 as->ss.ops = &avscan_ops;
174 as->ss.src = "<argv>";
175 as->ss.line = 1;
176 as->p = *av++;
177 as->av = av;
178 return (&as->ss);
e82f7154 179}
180
77d0e3dc 181/*----- End-of-file sentinel block ----------------------------------------*/
182
183/* --- @scan@ --- */
e82f7154 184
77d0e3dc 185static int eof_scan(scansrc *ss)
e82f7154 186{
77d0e3dc 187 return (EOF);
e82f7154 188}
189
77d0e3dc 190/* --- @destroy@ --- */
191
192static void eof_destroy(scansrc *ss)
e82f7154 193{
77d0e3dc 194 ;
e82f7154 195}
196
77d0e3dc 197/* --- Eof scanner operations --- */
198
199static scansrc_ops eof_ops = { eof_scan, eof_destroy };
200
201/* --- The end of file marker --- */
202
203static scansrc scan_eof = { &scan_eof, &eof_ops, "<eof>", 0, EOF };
204
205/*----- General scanner handling ------------------------------------------*/
206
207/* --- @scan@ --- *
208 *
209 * Arguments: @scanner *sc@ = pointer to main scanner context
210 *
211 * Returns: Character read, or end-of-file.
212 *
213 * Use: Scans a character from a source of characters.
214 */
215
216int scan(scanner *sc)
217{
218 int ch;
219 if (sc->head->pushback != EOF) {
220 ch = sc->head->pushback;
221 sc->head->pushback = EOF;
222 } else {
223 scansrc *ss = sc->head;
224 if (ss == &scan_eof)
225 ch = EOF;
226 else if ((ch = ss->ops->scan(ss)) == EOF) {
227 sc->head = ss->next;
228 if (sc->head == &scan_eof)
229 sc->tail = &sc->head;
230 ss->ops->destroy(ss);
231 ch = '\n';
232 }
233 }
234 return (ch);
235}
236
237/* --- @unscan@ --- *
238 *
239 * Arguments: @scanner *sc@ = pointer to main scanner context
240 * @int ch@ = character to unscan
241 *
242 * Returns: ---
243 *
244 * Use: Scans a character from a source of characters.
245 */
246
247void unscan(scanner *sc, int ch)
248{
249 sc->head->pushback = ch;
250}
251
252/* --- @scan_push@ --- *
253 *
254 * Arguments: @scanner *sc@ = pointer to main scanner context
255 * @scansrc *ss@ = souorce to push
256 *
257 * Returns: ---
258 *
259 * Use: Pushes a scanner source onto the front of the queue.
260 */
261
262void scan_push(scanner *sc, scansrc *ss)
e82f7154 263{
77d0e3dc 264 ss->next = sc->head;
265 if (sc->head == &scan_eof)
266 sc->tail = &ss->next;
267 sc->head = ss;
268 ss->pushback = EOF;
269 ss->tok = 0;
270 ss->t = 0;
e82f7154 271}
272
77d0e3dc 273/* --- @scan_add@ --- *
274 *
275 * Arguments: @scanner *sc@ = pointer to main scanner context
276 * @scansrc *ss@ = souorce to push
277 *
278 * Returns: ---
279 *
280 * Use: Adds a scanner source onto the end of the queue.
281 */
282
283void scan_add(scanner *sc, scansrc *ss)
284{
285 ss->next = &scan_eof;
286 *sc->tail = ss;
287 sc->tail = &ss->next;
288 ss->pushback = EOF;
289 ss->tok = 0;
290 ss->t = 0;
291}
292
293/* --- @scan_create@ --- *
294 *
295 * Arguments: @scanner *sc@ = scanner context to initialize
296 *
297 * Returns: ---
298 *
299 * Use: Initializes a scanner block ready for use.
300 */
301
302void scan_create(scanner *sc)
303{
304 sc->head = &scan_eof;
305 sc->tail = &sc->head;
306 dstr_create(&sc->d);
e91b672f 307 sc->wbegin = sc->wcont = 0;
77d0e3dc 308}
309
310/* --- @scan_destroy@ --- *
311 *
312 * Arguments: @scanner *sc@ = pointer to scanner context
313 *
314 * Returns: ---
315 *
316 * Use: Destroys a scanner and all the sources attached to it.
317 */
e82f7154 318
77d0e3dc 319void scan_destroy(scanner *sc)
e82f7154 320{
77d0e3dc 321 scansrc *ss = sc->head;
322 while (ss != &scan_eof) {
323 scansrc *sss = ss;
324 ss = ss->next;
325 if (sss->tok)
326 free(sss->tok);
327 sss->ops->destroy(sss);
328 }
e82f7154 329 dstr_destroy(&sc->d);
77d0e3dc 330 if (scan_eof.tok)
331 free(scan_eof.tok);
332 scan_eof.tok = 0;
333 sc->head = &scan_eof;
334 sc->tail = &sc->head;
e82f7154 335}
336
337/*----- That's all, folks -------------------------------------------------*/