`fw'-specific configuration code moved out. This file might become part
[fwd] / scan.c
1 /* -*-c-*-
2 *
3 * $Id: scan.c,v 1.5 2002/01/30 09:29:34 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.5 2002/01/30 09:29:34 mdw
33 * Initialize scanner properly.
34 *
35 * Revision 1.4 2001/02/03 20:30:03 mdw
36 * Support re-reading config files on SIGHUP.
37 *
38 * Revision 1.3 2000/08/01 17:58:10 mdw
39 * Fix subtleties with <ctype.h> functions.
40 *
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.
44 *
45 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
46 * Initial revision.
47 *
48 */
49
50 /*----- Header files ------------------------------------------------------*/
51
52 #include "config.h"
53
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57
58 #include <mLib/dstr.h>
59 #include <mLib/sub.h>
60
61 #include "scan.h"
62
63 /*----- File scanner source -----------------------------------------------*/
64
65 /* --- File scanner block --- */
66
67 typedef struct fscan {
68 scansrc ss;
69 FILE *fp;
70 unsigned f;
71 } fscan;
72
73 /* --- @scan@ --- */
74
75 static int fscan_scan(scansrc *ss)
76 {
77 fscan *fs = (fscan *)ss;
78 int ch = getc(fs->fp);
79 if (ch == '\n')
80 fs->ss.line++;
81 return (ch);
82 }
83
84 /* --- @destroy@ --- */
85
86 static void fscan_destroy(scansrc *ss)
87 {
88 fscan *fs = (fscan *)ss;
89 if (!(fs->f & SCF_NOCLOSE))
90 fclose(fs->fp);
91 if (fs->f & SCF_FREENAME)
92 free(fs->ss.src);
93 DESTROY(fs);
94 }
95
96 /* --- File scanner operations --- */
97
98 static scansrc_ops fscan_ops = { fscan_scan, fscan_destroy };
99
100 /* --- @scan_file@ --- *
101 *
102 * Arguments: @FILE *fp@ = pointer to file descriptor
103 * @char *name@ = pointer to source file name
104 * @unsigned f@ = flags
105 *
106 * Returns: A scanner source.
107 *
108 * Use: Creates a new scanner source for reading from a file.
109 */
110
111 scansrc *scan_file(FILE *fp, char *name, unsigned f)
112 {
113 fscan *fs = CREATE(fscan);
114 fs->ss.ops = &fscan_ops;
115 fs->ss.src = name;
116 fs->ss.line = 1;
117 fs->fp = fp;
118 fs->f = f;
119 return (&fs->ss);
120 }
121
122 /*---- Argv scanner source ------------------------------------------------*/
123
124 /* --- Argv scanner block --- */
125
126 typedef struct avscan {
127 scansrc ss;
128 char **av;
129 char *p;
130 } avscan;
131
132 /* --- @scan@ --- */
133
134 static int avscan_scan(scansrc *ss)
135 {
136 avscan *as = (avscan *)ss;
137 int ch;
138 if (!as->p)
139 ch = EOF;
140 else if ((ch = (unsigned char)*as->p++) == 0) {
141 as->ss.line++;
142 as->p = *as->av++;
143 ch = '\n';
144 }
145 return (ch);
146 }
147
148 /* --- @destroy@ --- */
149
150 static void avscan_destroy(scansrc *ss)
151 {
152 avscan *as = (avscan *)ss;
153 DESTROY(as);
154 }
155
156 /* --- Argv scanner operations --- */
157
158 static scansrc_ops avscan_ops = { avscan_scan, avscan_destroy };
159
160 /* --- @scan_argv@ --- *
161 *
162 * Arguments: @char **av@ = pointer to argument array (null terminated)
163 *
164 * Returns: A scanner source.
165 *
166 * Use: Creates a new scanner source for reading from an @argv@
167 * array.
168 */
169
170 scansrc *scan_argv(char **av)
171 {
172 avscan *as = CREATE(avscan);
173 as->ss.ops = &avscan_ops;
174 as->ss.src = "<argv>";
175 as->ss.line = 1;
176 as->p = *av++;
177 as->av = av;
178 return (&as->ss);
179 }
180
181 /*----- End-of-file sentinel block ----------------------------------------*/
182
183 /* --- @scan@ --- */
184
185 static int eof_scan(scansrc *ss)
186 {
187 return (EOF);
188 }
189
190 /* --- @destroy@ --- */
191
192 static void eof_destroy(scansrc *ss)
193 {
194 ;
195 }
196
197 /* --- Eof scanner operations --- */
198
199 static scansrc_ops eof_ops = { eof_scan, eof_destroy };
200
201 /* --- The end of file marker --- */
202
203 static scansrc scan_eof = { &scan_eof, &eof_ops, "<eof>", 0, EOF };
204
205 /*----- General scanner handling ------------------------------------------*/
206
207 /* --- @scan@ --- *
208 *
209 * Arguments: @scanner *sc@ = pointer to main scanner context
210 *
211 * Returns: Character read, or end-of-file.
212 *
213 * Use: Scans a character from a source of characters.
214 */
215
216 int scan(scanner *sc)
217 {
218 int ch;
219 if (sc->head->pushback != EOF) {
220 ch = sc->head->pushback;
221 sc->head->pushback = EOF;
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
247 void unscan(scanner *sc, int ch)
248 {
249 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
262 void 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 ss->pushback = EOF;
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
283 void scan_add(scanner *sc, scansrc *ss)
284 {
285 ss->next = &scan_eof;
286 *sc->tail = ss;
287 sc->tail = &ss->next;
288 ss->pushback = EOF;
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
302 void 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
319 void 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 free(sss->tok);
327 sss->ops->destroy(sss);
328 }
329 dstr_destroy(&sc->d);
330 if (scan_eof.tok)
331 free(scan_eof.tok);
332 scan_eof.tok = 0;
333 sc->head = &scan_eof;
334 sc->tail = &sc->head;
335 }
336
337 /*----- That's all, folks -------------------------------------------------*/