3 * $Id: scan.c,v 1.4 2001/02/03 20:30:03 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.4 2001/02/03 20:30:03 mdw
33 * Support re-reading config files on SIGHUP.
35 * Revision 1.3 2000/08/01 17:58:10 mdw
36 * Fix subtleties with <ctype.h> functions.
38 * Revision 1.2 1999/07/26 23:24:33 mdw
39 * Complete rewrite. Allow a list of character sources to enable changes
40 * during parsing of syntactic constructs.
42 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
47 /*----- Header files ------------------------------------------------------*/
55 #include <mLib/dstr.h>
60 /*----- File scanner source -----------------------------------------------*/
62 /* --- File scanner block --- */
64 typedef struct fscan
{
72 static int fscan_scan(scansrc
*ss
)
74 fscan
*fs
= (fscan
*)ss
;
75 int ch
= getc(fs
->fp
);
81 /* --- @destroy@ --- */
83 static void fscan_destroy(scansrc
*ss
)
85 fscan
*fs
= (fscan
*)ss
;
86 if (!(fs
->f
& SCF_NOCLOSE
))
88 if (fs
->f
& SCF_FREENAME
)
93 /* --- File scanner operations --- */
95 static scansrc_ops fscan_ops
= { fscan_scan
, fscan_destroy
};
97 /* --- @scan_file@ --- *
99 * Arguments: @FILE *fp@ = pointer to file descriptor
100 * @char *name@ = pointer to source file name
101 * @unsigned f@ = flags
103 * Returns: A scanner source.
105 * Use: Creates a new scanner source for reading from a file.
108 scansrc
*scan_file(FILE *fp
, char *name
, unsigned f
)
110 fscan
*fs
= CREATE(fscan
);
111 fs
->ss
.ops
= &fscan_ops
;
119 /*---- Argv scanner source ------------------------------------------------*/
121 /* --- Argv scanner block --- */
123 typedef struct avscan
{
131 static int avscan_scan(scansrc
*ss
)
133 avscan
*as
= (avscan
*)ss
;
137 else if ((ch
= (unsigned char)*as
->p
++) == 0) {
145 /* --- @destroy@ --- */
147 static void avscan_destroy(scansrc
*ss
)
149 avscan
*as
= (avscan
*)ss
;
153 /* --- Argv scanner operations --- */
155 static scansrc_ops avscan_ops
= { avscan_scan
, avscan_destroy
};
157 /* --- @scan_argv@ --- *
159 * Arguments: @char **av@ = pointer to argument array (null terminated)
161 * Returns: A scanner source.
163 * Use: Creates a new scanner source for reading from an @argv@
167 scansrc
*scan_argv(char **av
)
169 avscan
*as
= CREATE(avscan
);
170 as
->ss
.ops
= &avscan_ops
;
171 as
->ss
.src
= "<argv>";
178 /*----- End-of-file sentinel block ----------------------------------------*/
182 static int eof_scan(scansrc
*ss
)
187 /* --- @destroy@ --- */
189 static void eof_destroy(scansrc
*ss
)
194 /* --- Eof scanner operations --- */
196 static scansrc_ops eof_ops
= { eof_scan
, eof_destroy
};
198 /* --- The end of file marker --- */
200 static scansrc scan_eof
= { &scan_eof
, &eof_ops
, "<eof>", 0, EOF
};
202 /*----- General scanner handling ------------------------------------------*/
206 * Arguments: @scanner *sc@ = pointer to main scanner context
208 * Returns: Character read, or end-of-file.
210 * Use: Scans a character from a source of characters.
213 int scan(scanner
*sc
)
216 if (sc
->head
->pushback
!= EOF
) {
217 ch
= sc
->head
->pushback
;
218 sc
->head
->pushback
= EOF
;
220 scansrc
*ss
= sc
->head
;
223 else if ((ch
= ss
->ops
->scan(ss
)) == EOF
) {
225 if (sc
->head
== &scan_eof
)
226 sc
->tail
= &sc
->head
;
227 ss
->ops
->destroy(ss
);
234 /* --- @unscan@ --- *
236 * Arguments: @scanner *sc@ = pointer to main scanner context
237 * @int ch@ = character to unscan
241 * Use: Scans a character from a source of characters.
244 void unscan(scanner
*sc
, int ch
)
246 sc
->head
->pushback
= ch
;
249 /* --- @scan_push@ --- *
251 * Arguments: @scanner *sc@ = pointer to main scanner context
252 * @scansrc *ss@ = souorce to push
256 * Use: Pushes a scanner source onto the front of the queue.
259 void scan_push(scanner
*sc
, scansrc
*ss
)
262 if (sc
->head
== &scan_eof
)
263 sc
->tail
= &ss
->next
;
270 /* --- @scan_add@ --- *
272 * Arguments: @scanner *sc@ = pointer to main scanner context
273 * @scansrc *ss@ = souorce to push
277 * Use: Adds a scanner source onto the end of the queue.
280 void scan_add(scanner
*sc
, scansrc
*ss
)
282 ss
->next
= &scan_eof
;
284 sc
->tail
= &ss
->next
;
290 /* --- @scan_create@ --- *
292 * Arguments: @scanner *sc@ = scanner context to initialize
296 * Use: Initializes a scanner block ready for use.
299 void scan_create(scanner
*sc
)
301 sc
->head
= &scan_eof
;
302 sc
->tail
= &sc
->head
;
306 /* --- @scan_destroy@ --- *
308 * Arguments: @scanner *sc@ = pointer to scanner context
312 * Use: Destroys a scanner and all the sources attached to it.
315 void scan_destroy(scanner
*sc
)
317 scansrc
*ss
= sc
->head
;
318 while (ss
!= &scan_eof
) {
323 sss
->ops
->destroy(sss
);
325 dstr_destroy(&sc
->d
);
329 sc
->head
= &scan_eof
;
330 sc
->tail
= &sc
->head
;
333 /*----- That's all, folks -------------------------------------------------*/