Version bump.
[fwd] / scan.c
... / ...
CommitLineData
1/* -*-c-*-
2 *
3 * $Id: scan.c,v 1.6 2002/02/22 23:44:16 mdw Exp $
4 *
5 * Character scanners
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the `fw' port forwarder.
13 *
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.
18 *
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.
23 *
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.
27 */
28
29/*----- Revision history --------------------------------------------------*
30 *
31 * $Log: scan.c,v $
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.
35 *
36 * Revision 1.5 2002/01/30 09:29:34 mdw
37 * Initialize scanner properly.
38 *
39 * Revision 1.4 2001/02/03 20:30:03 mdw
40 * Support re-reading config files on SIGHUP.
41 *
42 * Revision 1.3 2000/08/01 17:58:10 mdw
43 * Fix subtleties with <ctype.h> functions.
44 *
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.
48 *
49 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
50 * Initial revision.
51 *
52 */
53
54/*----- Header files ------------------------------------------------------*/
55
56#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
59
60#include <mLib/dstr.h>
61#include <mLib/sub.h>
62
63#include "scan.h"
64
65/*----- File scanner source -----------------------------------------------*/
66
67/* --- File scanner block --- */
68
69typedef struct fscan {
70 scansrc ss;
71 FILE *fp;
72 unsigned f;
73} fscan;
74
75/* --- @scan@ --- */
76
77static int fscan_scan(scansrc *ss)
78{
79 fscan *fs = (fscan *)ss;
80 int ch = getc(fs->fp);
81 if (ch == '\n')
82 fs->ss.line++;
83 return (ch);
84}
85
86/* --- @destroy@ --- */
87
88static void fscan_destroy(scansrc *ss)
89{
90 fscan *fs = (fscan *)ss;
91 if (!(fs->f & SCF_NOCLOSE))
92 fclose(fs->fp);
93 xfree(fs->ss.src);
94 DESTROY(fs);
95}
96
97/* --- File scanner operations --- */
98
99static scansrc_ops fscan_ops = { fscan_scan, fscan_destroy };
100
101/* --- @scan_file@ --- *
102 *
103 * Arguments: @FILE *fp@ = pointer to file descriptor
104 * @const char *name@ = pointer to source file name
105 * @unsigned f@ = flags
106 *
107 * Returns: A scanner source.
108 *
109 * Use: Creates a new scanner source for reading from a file.
110 */
111
112scansrc *scan_file(FILE *fp, const char *name, unsigned f)
113{
114 fscan *fs = CREATE(fscan);
115 fs->ss.ops = &fscan_ops;
116 fs->ss.src = xstrdup(name);
117 fs->ss.line = 1;
118 fs->fp = fp;
119 fs->f = f;
120 return (&fs->ss);
121}
122
123/*---- Argv scanner source ------------------------------------------------*/
124
125/* --- Argv scanner block --- */
126
127typedef struct avscan {
128 scansrc ss;
129 char **av;
130 char *p;
131} avscan;
132
133/* --- @scan@ --- */
134
135static int avscan_scan(scansrc *ss)
136{
137 avscan *as = (avscan *)ss;
138 int ch;
139 if (!as->p)
140 ch = EOF;
141 else if ((ch = (unsigned char)*as->p++) == 0) {
142 as->ss.line++;
143 as->p = *as->av++;
144 ch = '\n';
145 }
146 return (ch);
147}
148
149/* --- @destroy@ --- */
150
151static void avscan_destroy(scansrc *ss)
152{
153 avscan *as = (avscan *)ss;
154 DESTROY(as);
155}
156
157/* --- Argv scanner operations --- */
158
159static scansrc_ops avscan_ops = { avscan_scan, avscan_destroy };
160
161/* --- @scan_argv@ --- *
162 *
163 * Arguments: @char **av@ = pointer to argument array (null terminated)
164 *
165 * Returns: A scanner source.
166 *
167 * Use: Creates a new scanner source for reading from an @argv@
168 * array.
169 */
170
171scansrc *scan_argv(char **av)
172{
173 avscan *as = CREATE(avscan);
174 as->ss.ops = &avscan_ops;
175 as->ss.src = "<argv>";
176 as->ss.line = 1;
177 as->p = *av++;
178 as->av = av;
179 return (&as->ss);
180}
181
182/*----- End-of-file sentinel block ----------------------------------------*/
183
184/* --- @scan@ --- */
185
186static int eof_scan(scansrc *ss)
187{
188 return (EOF);
189}
190
191/* --- @destroy@ --- */
192
193static void eof_destroy(scansrc *ss)
194{
195 ;
196}
197
198/* --- Eof scanner operations --- */
199
200static scansrc_ops eof_ops = { eof_scan, eof_destroy };
201
202/* --- The end of file marker --- */
203
204static scansrc scan_eof = { &scan_eof, &eof_ops, "<eof>", 0, DSTR_INIT };
205
206/*----- General scanner handling ------------------------------------------*/
207
208/* --- @scan@ --- *
209 *
210 * Arguments: @scanner *sc@ = pointer to main scanner context
211 *
212 * Returns: Character read, or end-of-file.
213 *
214 * Use: Scans a character from a source of characters.
215 */
216
217int scan(scanner *sc)
218{
219 int ch;
220 if (sc->head->pushback.len)
221 ch = sc->head->pushback.buf[--sc->head->pushback.len];
222 else {
223 scansrc *ss = sc->head;
224 if (ss == &scan_eof)
225 ch = EOF;
226 else if ((ch = ss->ops->scan(ss)) == EOF) {
227 sc->head = ss->next;
228 if (sc->head == &scan_eof)
229 sc->tail = &sc->head;
230 ss->ops->destroy(ss);
231 ch = '\n';
232 }
233 }
234 return (ch);
235}
236
237/* --- @unscan@ --- *
238 *
239 * Arguments: @scanner *sc@ = pointer to main scanner context
240 * @int ch@ = character to unscan
241 *
242 * Returns: ---
243 *
244 * Use: Scans a character from a source of characters.
245 */
246
247void unscan(scanner *sc, int ch)
248{
249 DPUTC(&sc->head->pushback, ch);
250}
251
252/* --- @scan_push@ --- *
253 *
254 * Arguments: @scanner *sc@ = pointer to main scanner context
255 * @scansrc *ss@ = souorce to push
256 *
257 * Returns: ---
258 *
259 * Use: Pushes a scanner source onto the front of the queue.
260 */
261
262void scan_push(scanner *sc, scansrc *ss)
263{
264 ss->next = sc->head;
265 if (sc->head == &scan_eof)
266 sc->tail = &ss->next;
267 sc->head = ss;
268 dstr_create(&ss->pushback);
269 ss->tok = 0;
270 ss->t = 0;
271}
272
273/* --- @scan_add@ --- *
274 *
275 * Arguments: @scanner *sc@ = pointer to main scanner context
276 * @scansrc *ss@ = souorce to push
277 *
278 * Returns: ---
279 *
280 * Use: Adds a scanner source onto the end of the queue.
281 */
282
283void scan_add(scanner *sc, scansrc *ss)
284{
285 ss->next = &scan_eof;
286 *sc->tail = ss;
287 sc->tail = &ss->next;
288 dstr_create(&ss->pushback);
289 ss->tok = 0;
290 ss->t = 0;
291}
292
293/* --- @scan_create@ --- *
294 *
295 * Arguments: @scanner *sc@ = scanner context to initialize
296 *
297 * Returns: ---
298 *
299 * Use: Initializes a scanner block ready for use.
300 */
301
302void scan_create(scanner *sc)
303{
304 sc->head = &scan_eof;
305 sc->tail = &sc->head;
306 dstr_create(&sc->d);
307 sc->wbegin = sc->wcont = 0;
308}
309
310/* --- @scan_destroy@ --- *
311 *
312 * Arguments: @scanner *sc@ = pointer to scanner context
313 *
314 * Returns: ---
315 *
316 * Use: Destroys a scanner and all the sources attached to it.
317 */
318
319void scan_destroy(scanner *sc)
320{
321 scansrc *ss = sc->head;
322 while (ss != &scan_eof) {
323 scansrc *sss = ss;
324 ss = ss->next;
325 if (sss->tok)
326 xfree(sss->tok);
327 dstr_destroy(&sss->pushback);
328 sss->ops->destroy(sss);
329 }
330 dstr_destroy(&sc->d);
331 if (scan_eof.tok)
332 xfree(scan_eof.tok);
333 scan_eof.tok = 0;
334 sc->head = &scan_eof;
335 sc->tail = &sc->head;
336}
337
338/*----- That's all, folks -------------------------------------------------*/