3 * $Id: scan.c,v 1.6 2002/02/22 23:44:16 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.6 2002/02/22 23:44:16 mdw
33 * Miscellaneous tidying up, to make this code independent of `fw'. It
34 * might end up in a library somewhere.
36 * Revision 1.5 2002/01/30 09:29:34 mdw
37 * Initialize scanner properly.
39 * Revision 1.4 2001/02/03 20:30:03 mdw
40 * Support re-reading config files on SIGHUP.
42 * Revision 1.3 2000/08/01 17:58:10 mdw
43 * Fix subtleties with <ctype.h> functions.
45 * Revision 1.2 1999/07/26 23:24:33 mdw
46 * Complete rewrite. Allow a list of character sources to enable changes
47 * during parsing of syntactic constructs.
49 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
54 /*----- Header files ------------------------------------------------------*/
60 #include <mLib/dstr.h>
65 /*----- File scanner source -----------------------------------------------*/
67 /* --- File scanner block --- */
69 typedef struct fscan
{
77 static int fscan_scan(scansrc
*ss
)
79 fscan
*fs
= (fscan
*)ss
;
80 int ch
= getc(fs
->fp
);
86 /* --- @destroy@ --- */
88 static void fscan_destroy(scansrc
*ss
)
90 fscan
*fs
= (fscan
*)ss
;
91 if (!(fs
->f
& SCF_NOCLOSE
))
97 /* --- File scanner operations --- */
99 static scansrc_ops fscan_ops
= { fscan_scan
, fscan_destroy
};
101 /* --- @scan_file@ --- *
103 * Arguments: @FILE *fp@ = pointer to file descriptor
104 * @const char *name@ = pointer to source file name
105 * @unsigned f@ = flags
107 * Returns: A scanner source.
109 * Use: Creates a new scanner source for reading from a file.
112 scansrc
*scan_file(FILE *fp
, const char *name
, unsigned f
)
114 fscan
*fs
= CREATE(fscan
);
115 fs
->ss
.ops
= &fscan_ops
;
116 fs
->ss
.src
= xstrdup(name
);
123 /*---- Argv scanner source ------------------------------------------------*/
125 /* --- Argv scanner block --- */
127 typedef struct avscan
{
135 static int avscan_scan(scansrc
*ss
)
137 avscan
*as
= (avscan
*)ss
;
141 else if ((ch
= (unsigned char)*as
->p
++) == 0) {
149 /* --- @destroy@ --- */
151 static void avscan_destroy(scansrc
*ss
)
153 avscan
*as
= (avscan
*)ss
;
157 /* --- Argv scanner operations --- */
159 static scansrc_ops avscan_ops
= { avscan_scan
, avscan_destroy
};
161 /* --- @scan_argv@ --- *
163 * Arguments: @char **av@ = pointer to argument array (null terminated)
165 * Returns: A scanner source.
167 * Use: Creates a new scanner source for reading from an @argv@
171 scansrc
*scan_argv(char **av
)
173 avscan
*as
= CREATE(avscan
);
174 as
->ss
.ops
= &avscan_ops
;
175 as
->ss
.src
= "<argv>";
182 /*----- End-of-file sentinel block ----------------------------------------*/
186 static int eof_scan(scansrc
*ss
)
191 /* --- @destroy@ --- */
193 static void eof_destroy(scansrc
*ss
)
198 /* --- Eof scanner operations --- */
200 static scansrc_ops eof_ops
= { eof_scan
, eof_destroy
};
202 /* --- The end of file marker --- */
204 static scansrc scan_eof
= { &scan_eof
, &eof_ops
, "<eof>", 0, DSTR_INIT
};
206 /*----- General scanner handling ------------------------------------------*/
210 * Arguments: @scanner *sc@ = pointer to main scanner context
212 * Returns: Character read, or end-of-file.
214 * Use: Scans a character from a source of characters.
217 int scan(scanner
*sc
)
220 if (sc
->head
->pushback
.len
)
221 ch
= sc
->head
->pushback
.buf
[--sc
->head
->pushback
.len
];
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 DPUTC(&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
;
268 dstr_create(&ss
->pushback
);
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
;
288 dstr_create(&ss
->pushback
);
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 dstr_destroy(&sss
->pushback
);
328 sss
->ops
->destroy(sss
);
330 dstr_destroy(&sc
->d
);
334 sc
->head
= &scan_eof
;
335 sc
->tail
= &sc
->head
;
338 /*----- That's all, folks -------------------------------------------------*/