3 * $Id: scan.c,v 1.2 1999/07/26 23:24:33 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.2 1999/07/26 23:24:33 mdw
33 * Complete rewrite. Allow a list of character sources to enable changes
34 * during parsing of syntactic constructs.
36 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
41 /*----- Header files ------------------------------------------------------*/
49 #include <mLib/dstr.h>
54 /*----- File scanner source -----------------------------------------------*/
56 /* --- File scanner block --- */
58 typedef struct fscan {
66 static int fscan_scan(scansrc *ss)
68 fscan *fs = (fscan *)ss;
69 int ch = getc(fs->fp);
75 /* --- @destroy@ --- */
77 static void fscan_destroy(scansrc *ss)
79 fscan *fs = (fscan *)ss;
80 if (!(fs->f & SCF_NOCLOSE))
82 if (fs->f & SCF_FREENAME)
87 /* --- File scanner operations --- */
89 static scansrc_ops fscan_ops = { fscan_scan, fscan_destroy };
91 /* --- @scan_file@ --- *
93 * Arguments: @FILE *fp@ = pointer to file descriptor
94 * @char *name@ = pointer to source file name
95 * @unsigned f@ = flags
97 * Returns: A scanner source.
99 * Use: Creates a new scanner source for reading from a file.
102 scansrc *scan_file(FILE *fp, char *name, unsigned f)
104 fscan *fs = CREATE(fs);
105 fs->ss.ops = &fscan_ops;
113 /*---- Argv scanner source ------------------------------------------------*/
115 /* --- Argv scanner block --- */
117 typedef struct avscan {
125 static int avscan_scan(scansrc *ss)
127 avscan *as = (avscan *)ss;
131 else if ((ch = *as->p++) == 0) {
139 /* --- @destroy@ --- */
141 static void avscan_destroy(scansrc *ss)
143 avscan *as = (avscan *)ss;
147 /* --- Argv scanner operations --- */
149 static scansrc_ops avscan_ops = { avscan_scan, avscan_destroy };
151 /* --- @scan_argv@ --- *
153 * Arguments: @char **av@ = pointer to argument array (null terminated)
155 * Returns: A scanner source.
157 * Use: Creates a new scanner source for reading from an @argv@
161 scansrc *scan_argv(char **av)
163 avscan *as = CREATE(avscan);
164 as->ss.ops = &avscan_ops;
165 as->ss.src = "<argv>";
172 /*----- End-of-file sentinel block ----------------------------------------*/
176 static int eof_scan(scansrc *ss)
181 /* --- @destroy@ --- */
183 static void eof_destroy(scansrc *ss)
188 /* --- Eof scanner operations --- */
190 static scansrc_ops eof_ops = { eof_scan, eof_destroy };
192 /* --- The end of file marker --- */
194 static scansrc scan_eof = { &scan_eof, &eof_ops, "<eof>", 0, EOF };
196 /*----- General scanner handling ------------------------------------------*/
200 * Arguments: @scanner *sc@ = pointer to main scanner context
202 * Returns: Character read, or end-of-file.
204 * Use: Scans a character from a source of characters.
207 int scan(scanner *sc)
210 if (sc->head->pushback != EOF) {
211 ch = sc->head->pushback;
212 sc->head->pushback = EOF;
214 scansrc *ss = sc->head;
217 else if ((ch = ss->ops->scan(ss)) == EOF) {
219 if (sc->head == &scan_eof)
220 sc->tail = &sc->head;
221 ss->ops->destroy(ss);
228 /* --- @unscan@ --- *
230 * Arguments: @scanner *sc@ = pointer to main scanner context
231 * @int ch@ = character to unscan
235 * Use: Scans a character from a source of characters.
238 void unscan(scanner *sc, int ch)
240 sc->head->pushback = ch;
243 /* --- @scan_push@ --- *
245 * Arguments: @scanner *sc@ = pointer to main scanner context
246 * @scansrc *ss@ = souorce to push
250 * Use: Pushes a scanner source onto the front of the queue.
253 void scan_push(scanner *sc, scansrc *ss)
256 if (sc->head == &scan_eof)
257 sc->tail = &ss->next;
264 /* --- @scan_add@ --- *
266 * Arguments: @scanner *sc@ = pointer to main scanner context
267 * @scansrc *ss@ = souorce to push
271 * Use: Adds a scanner source onto the end of the queue.
274 void scan_add(scanner *sc, scansrc *ss)
276 ss->next = &scan_eof;
278 sc->tail = &ss->next;
284 /* --- @scan_create@ --- *
286 * Arguments: @scanner *sc@ = scanner context to initialize
290 * Use: Initializes a scanner block ready for use.
293 void scan_create(scanner *sc)
295 sc->head = &scan_eof;
296 sc->tail = &sc->head;
300 /* --- @scan_destroy@ --- *
302 * Arguments: @scanner *sc@ = pointer to scanner context
306 * Use: Destroys a scanner and all the sources attached to it.
309 void scan_destroy(scanner *sc)
311 scansrc *ss = sc->head;
312 while (ss != &scan_eof) {
317 sss->ops->destroy(sss);
319 dstr_destroy(&sc->d);
323 sc->head = &scan_eof;
324 sc->tail = &sc->head;
327 /*----- That's all, folks -------------------------------------------------*/