3 * $Id: scan.c,v 1.3 2000/08/01 17:58:10 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 /*----- Revision history --------------------------------------------------*
32 * Revision 1.3 2000/08/01 17:58:10 mdw
33 * Fix subtleties with <ctype.h> functions.
35 * Revision 1.2 1999/07/26 23:24:33 mdw
36 * Complete rewrite. Allow a list of character sources to enable changes
37 * during parsing of syntactic constructs.
39 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
44 /*----- Header files ------------------------------------------------------*/
52 #include <mLib/dstr.h>
57 /*----- File scanner source -----------------------------------------------*/
59 /* --- File scanner block --- */
61 typedef struct fscan {
69 static int fscan_scan(scansrc *ss)
71 fscan *fs = (fscan *)ss;
72 int ch = getc(fs->fp);
78 /* --- @destroy@ --- */
80 static void fscan_destroy(scansrc *ss)
82 fscan *fs = (fscan *)ss;
83 if (!(fs->f & SCF_NOCLOSE))
85 if (fs->f & SCF_FREENAME)
90 /* --- File scanner operations --- */
92 static scansrc_ops fscan_ops = { fscan_scan, fscan_destroy };
94 /* --- @scan_file@ --- *
96 * Arguments: @FILE *fp@ = pointer to file descriptor
97 * @char *name@ = pointer to source file name
98 * @unsigned f@ = flags
100 * Returns: A scanner source.
102 * Use: Creates a new scanner source for reading from a file.
105 scansrc *scan_file(FILE *fp, char *name, unsigned f)
107 fscan *fs = CREATE(fs);
108 fs->ss.ops = &fscan_ops;
116 /*---- Argv scanner source ------------------------------------------------*/
118 /* --- Argv scanner block --- */
120 typedef struct avscan {
128 static int avscan_scan(scansrc *ss)
130 avscan *as = (avscan *)ss;
134 else if ((ch = (unsigned char)*as->p++) == 0) {
142 /* --- @destroy@ --- */
144 static void avscan_destroy(scansrc *ss)
146 avscan *as = (avscan *)ss;
150 /* --- Argv scanner operations --- */
152 static scansrc_ops avscan_ops = { avscan_scan, avscan_destroy };
154 /* --- @scan_argv@ --- *
156 * Arguments: @char **av@ = pointer to argument array (null terminated)
158 * Returns: A scanner source.
160 * Use: Creates a new scanner source for reading from an @argv@
164 scansrc *scan_argv(char **av)
166 avscan *as = CREATE(avscan);
167 as->ss.ops = &avscan_ops;
168 as->ss.src = "<argv>";
175 /*----- End-of-file sentinel block ----------------------------------------*/
179 static int eof_scan(scansrc *ss)
184 /* --- @destroy@ --- */
186 static void eof_destroy(scansrc *ss)
191 /* --- Eof scanner operations --- */
193 static scansrc_ops eof_ops = { eof_scan, eof_destroy };
195 /* --- The end of file marker --- */
197 static scansrc scan_eof = { &scan_eof, &eof_ops, "<eof>", 0, EOF };
199 /*----- General scanner handling ------------------------------------------*/
203 * Arguments: @scanner *sc@ = pointer to main scanner context
205 * Returns: Character read, or end-of-file.
207 * Use: Scans a character from a source of characters.
210 int scan(scanner *sc)
213 if (sc->head->pushback != EOF) {
214 ch = sc->head->pushback;
215 sc->head->pushback = EOF;
217 scansrc *ss = sc->head;
220 else if ((ch = ss->ops->scan(ss)) == EOF) {
222 if (sc->head == &scan_eof)
223 sc->tail = &sc->head;
224 ss->ops->destroy(ss);
231 /* --- @unscan@ --- *
233 * Arguments: @scanner *sc@ = pointer to main scanner context
234 * @int ch@ = character to unscan
238 * Use: Scans a character from a source of characters.
241 void unscan(scanner *sc, int ch)
243 sc->head->pushback = ch;
246 /* --- @scan_push@ --- *
248 * Arguments: @scanner *sc@ = pointer to main scanner context
249 * @scansrc *ss@ = souorce to push
253 * Use: Pushes a scanner source onto the front of the queue.
256 void scan_push(scanner *sc, scansrc *ss)
259 if (sc->head == &scan_eof)
260 sc->tail = &ss->next;
267 /* --- @scan_add@ --- *
269 * Arguments: @scanner *sc@ = pointer to main scanner context
270 * @scansrc *ss@ = souorce to push
274 * Use: Adds a scanner source onto the end of the queue.
277 void scan_add(scanner *sc, scansrc *ss)
279 ss->next = &scan_eof;
281 sc->tail = &ss->next;
287 /* --- @scan_create@ --- *
289 * Arguments: @scanner *sc@ = scanner context to initialize
293 * Use: Initializes a scanner block ready for use.
296 void scan_create(scanner *sc)
298 sc->head = &scan_eof;
299 sc->tail = &sc->head;
303 /* --- @scan_destroy@ --- *
305 * Arguments: @scanner *sc@ = pointer to scanner context
309 * Use: Destroys a scanner and all the sources attached to it.
312 void scan_destroy(scanner *sc)
314 scansrc *ss = sc->head;
315 while (ss != &scan_eof) {
320 sss->ops->destroy(sss);
322 dstr_destroy(&sc->d);
326 sc->head = &scan_eof;
327 sc->tail = &sc->head;
330 /*----- That's all, folks -------------------------------------------------*/