3 * $Id: scan.c,v 1.5 2002/01/30 09:29:34 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.5 2002/01/30 09:29:34 mdw
33 * Initialize scanner properly.
35 * Revision 1.4 2001/02/03 20:30:03 mdw
36 * Support re-reading config files on SIGHUP.
38 * Revision 1.3 2000/08/01 17:58:10 mdw
39 * Fix subtleties with <ctype.h> functions.
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.
45 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
50 /*----- Header files ------------------------------------------------------*/
58 #include <mLib/dstr.h>
63 /*----- File scanner source -----------------------------------------------*/
65 /* --- File scanner block --- */
67 typedef struct fscan
{
75 static int fscan_scan(scansrc
*ss
)
77 fscan
*fs
= (fscan
*)ss
;
78 int ch
= getc(fs
->fp
);
84 /* --- @destroy@ --- */
86 static void fscan_destroy(scansrc
*ss
)
88 fscan
*fs
= (fscan
*)ss
;
89 if (!(fs
->f
& SCF_NOCLOSE
))
91 if (fs
->f
& SCF_FREENAME
)
96 /* --- File scanner operations --- */
98 static scansrc_ops fscan_ops
= { fscan_scan
, fscan_destroy
};
100 /* --- @scan_file@ --- *
102 * Arguments: @FILE *fp@ = pointer to file descriptor
103 * @char *name@ = pointer to source file name
104 * @unsigned f@ = flags
106 * Returns: A scanner source.
108 * Use: Creates a new scanner source for reading from a file.
111 scansrc
*scan_file(FILE *fp
, char *name
, unsigned f
)
113 fscan
*fs
= CREATE(fscan
);
114 fs
->ss
.ops
= &fscan_ops
;
122 /*---- Argv scanner source ------------------------------------------------*/
124 /* --- Argv scanner block --- */
126 typedef struct avscan
{
134 static int avscan_scan(scansrc
*ss
)
136 avscan
*as
= (avscan
*)ss
;
140 else if ((ch
= (unsigned char)*as
->p
++) == 0) {
148 /* --- @destroy@ --- */
150 static void avscan_destroy(scansrc
*ss
)
152 avscan
*as
= (avscan
*)ss
;
156 /* --- Argv scanner operations --- */
158 static scansrc_ops avscan_ops
= { avscan_scan
, avscan_destroy
};
160 /* --- @scan_argv@ --- *
162 * Arguments: @char **av@ = pointer to argument array (null terminated)
164 * Returns: A scanner source.
166 * Use: Creates a new scanner source for reading from an @argv@
170 scansrc
*scan_argv(char **av
)
172 avscan
*as
= CREATE(avscan
);
173 as
->ss
.ops
= &avscan_ops
;
174 as
->ss
.src
= "<argv>";
181 /*----- End-of-file sentinel block ----------------------------------------*/
185 static int eof_scan(scansrc
*ss
)
190 /* --- @destroy@ --- */
192 static void eof_destroy(scansrc
*ss
)
197 /* --- Eof scanner operations --- */
199 static scansrc_ops eof_ops
= { eof_scan
, eof_destroy
};
201 /* --- The end of file marker --- */
203 static scansrc scan_eof
= { &scan_eof
, &eof_ops
, "<eof>", 0, EOF
};
205 /*----- General scanner handling ------------------------------------------*/
209 * Arguments: @scanner *sc@ = pointer to main scanner context
211 * Returns: Character read, or end-of-file.
213 * Use: Scans a character from a source of characters.
216 int scan(scanner
*sc
)
219 if (sc
->head
->pushback
!= EOF
) {
220 ch
= sc
->head
->pushback
;
221 sc
->head
->pushback
= EOF
;
223 scansrc
*ss
= sc
->head
;
226 else if ((ch
= ss
->ops
->scan(ss
)) == EOF
) {
228 if (sc
->head
== &scan_eof
)
229 sc
->tail
= &sc
->head
;
230 ss
->ops
->destroy(ss
);
237 /* --- @unscan@ --- *
239 * Arguments: @scanner *sc@ = pointer to main scanner context
240 * @int ch@ = character to unscan
244 * Use: Scans a character from a source of characters.
247 void unscan(scanner
*sc
, int ch
)
249 sc
->head
->pushback
= ch
;
252 /* --- @scan_push@ --- *
254 * Arguments: @scanner *sc@ = pointer to main scanner context
255 * @scansrc *ss@ = souorce to push
259 * Use: Pushes a scanner source onto the front of the queue.
262 void scan_push(scanner
*sc
, scansrc
*ss
)
265 if (sc
->head
== &scan_eof
)
266 sc
->tail
= &ss
->next
;
273 /* --- @scan_add@ --- *
275 * Arguments: @scanner *sc@ = pointer to main scanner context
276 * @scansrc *ss@ = souorce to push
280 * Use: Adds a scanner source onto the end of the queue.
283 void scan_add(scanner
*sc
, scansrc
*ss
)
285 ss
->next
= &scan_eof
;
287 sc
->tail
= &ss
->next
;
293 /* --- @scan_create@ --- *
295 * Arguments: @scanner *sc@ = scanner context to initialize
299 * Use: Initializes a scanner block ready for use.
302 void scan_create(scanner
*sc
)
304 sc
->head
= &scan_eof
;
305 sc
->tail
= &sc
->head
;
307 sc
->wbegin
= sc
->wcont
= 0;
310 /* --- @scan_destroy@ --- *
312 * Arguments: @scanner *sc@ = pointer to scanner context
316 * Use: Destroys a scanner and all the sources attached to it.
319 void scan_destroy(scanner
*sc
)
321 scansrc
*ss
= sc
->head
;
322 while (ss
!= &scan_eof
) {
327 sss
->ops
->destroy(sss
);
329 dstr_destroy(&sc
->d
);
333 sc
->head
= &scan_eof
;
334 sc
->tail
= &sc
->head
;
337 /*----- That's all, folks -------------------------------------------------*/