3 * $Id: scan.c,v 1.7 2004/04/08 01:36:25 mdw Exp $
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the `fw' port forwarder.
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.
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.
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.
29 /*----- Header files ------------------------------------------------------*/
35 #include <mLib/dstr.h>
40 /*----- File scanner source -----------------------------------------------*/
42 /* --- File scanner block --- */
44 typedef struct fscan {
52 static int fscan_scan(scansrc *ss)
54 fscan *fs = (fscan *)ss;
55 int ch = getc(fs->fp);
61 /* --- @destroy@ --- */
63 static void fscan_destroy(scansrc *ss)
65 fscan *fs = (fscan *)ss;
66 if (!(fs->f & SCF_NOCLOSE))
72 /* --- File scanner operations --- */
74 static scansrc_ops fscan_ops = { fscan_scan, fscan_destroy };
76 /* --- @scan_file@ --- *
78 * Arguments: @FILE *fp@ = pointer to file descriptor
79 * @const char *name@ = pointer to source file name
80 * @unsigned f@ = flags
82 * Returns: A scanner source.
84 * Use: Creates a new scanner source for reading from a file.
87 scansrc *scan_file(FILE *fp, const char *name, unsigned f)
89 fscan *fs = CREATE(fscan);
90 fs->ss.ops = &fscan_ops;
91 fs->ss.src = xstrdup(name);
98 /*---- Argv scanner source ------------------------------------------------*/
100 /* --- Argv scanner block --- */
102 typedef struct avscan {
110 static int avscan_scan(scansrc *ss)
112 avscan *as = (avscan *)ss;
116 else if ((ch = (unsigned char)*as->p++) == 0) {
124 /* --- @destroy@ --- */
126 static void avscan_destroy(scansrc *ss)
128 avscan *as = (avscan *)ss;
132 /* --- Argv scanner operations --- */
134 static scansrc_ops avscan_ops = { avscan_scan, avscan_destroy };
136 /* --- @scan_argv@ --- *
138 * Arguments: @char **av@ = pointer to argument array (null terminated)
140 * Returns: A scanner source.
142 * Use: Creates a new scanner source for reading from an @argv@
146 scansrc *scan_argv(char **av)
148 avscan *as = CREATE(avscan);
149 as->ss.ops = &avscan_ops;
150 as->ss.src = "<argv>";
157 /*----- End-of-file sentinel block ----------------------------------------*/
161 static int eof_scan(scansrc *ss)
166 /* --- @destroy@ --- */
168 static void eof_destroy(scansrc *ss)
173 /* --- Eof scanner operations --- */
175 static scansrc_ops eof_ops = { eof_scan, eof_destroy };
177 /* --- The end of file marker --- */
179 static scansrc scan_eof = { &scan_eof, &eof_ops, "<eof>", 0, DSTR_INIT };
181 /*----- General scanner handling ------------------------------------------*/
185 * Arguments: @scanner *sc@ = pointer to main scanner context
187 * Returns: Character read, or end-of-file.
189 * Use: Scans a character from a source of characters.
192 int scan(scanner *sc)
195 if (sc->head->pushback.len)
196 ch = sc->head->pushback.buf[--sc->head->pushback.len];
198 scansrc *ss = sc->head;
201 else if ((ch = ss->ops->scan(ss)) == EOF) {
203 if (sc->head == &scan_eof)
204 sc->tail = &sc->head;
205 ss->ops->destroy(ss);
212 /* --- @unscan@ --- *
214 * Arguments: @scanner *sc@ = pointer to main scanner context
215 * @int ch@ = character to unscan
219 * Use: Scans a character from a source of characters.
222 void unscan(scanner *sc, int ch)
224 DPUTC(&sc->head->pushback, ch);
227 /* --- @scan_push@ --- *
229 * Arguments: @scanner *sc@ = pointer to main scanner context
230 * @scansrc *ss@ = souorce to push
234 * Use: Pushes a scanner source onto the front of the queue.
237 void scan_push(scanner *sc, scansrc *ss)
240 if (sc->head == &scan_eof)
241 sc->tail = &ss->next;
243 dstr_create(&ss->pushback);
248 /* --- @scan_add@ --- *
250 * Arguments: @scanner *sc@ = pointer to main scanner context
251 * @scansrc *ss@ = souorce to push
255 * Use: Adds a scanner source onto the end of the queue.
258 void scan_add(scanner *sc, scansrc *ss)
260 ss->next = &scan_eof;
262 sc->tail = &ss->next;
263 dstr_create(&ss->pushback);
268 /* --- @scan_create@ --- *
270 * Arguments: @scanner *sc@ = scanner context to initialize
274 * Use: Initializes a scanner block ready for use.
277 void scan_create(scanner *sc)
279 sc->head = &scan_eof;
280 sc->tail = &sc->head;
282 sc->wbegin = sc->wcont = 0;
285 /* --- @scan_destroy@ --- *
287 * Arguments: @scanner *sc@ = pointer to scanner context
291 * Use: Destroys a scanner and all the sources attached to it.
294 void scan_destroy(scanner *sc)
296 scansrc *ss = sc->head;
297 while (ss != &scan_eof) {
302 dstr_destroy(&sss->pushback);
303 sss->ops->destroy(sss);
305 dstr_destroy(&sc->d);
309 sc->head = &scan_eof;
310 sc->tail = &sc->head;
313 /*----- That's all, folks -------------------------------------------------*/