e82f7154 |
1 | /* -*-c-*- |
2 | * |
7bb7c50b |
3 | * $Id: scan.c,v 1.6 2002/02/22 23:44:16 mdw Exp $ |
e82f7154 |
4 | * |
5 | * Character scanners |
6 | * |
77d0e3dc |
7 | * (c) 1999 Straylight/Edgeware |
e82f7154 |
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 $ |
7bb7c50b |
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 | * |
e91b672f |
36 | * Revision 1.5 2002/01/30 09:29:34 mdw |
37 | * Initialize scanner properly. |
38 | * |
372a98e2 |
39 | * Revision 1.4 2001/02/03 20:30:03 mdw |
40 | * Support re-reading config files on SIGHUP. |
41 | * |
9e1c09df |
42 | * Revision 1.3 2000/08/01 17:58:10 mdw |
43 | * Fix subtleties with <ctype.h> functions. |
44 | * |
77d0e3dc |
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. |
e82f7154 |
51 | * |
52 | */ |
53 | |
54 | /*----- Header files ------------------------------------------------------*/ |
55 | |
e82f7154 |
56 | #include <stdio.h> |
57 | #include <stdlib.h> |
58 | #include <string.h> |
59 | |
60 | #include <mLib/dstr.h> |
77d0e3dc |
61 | #include <mLib/sub.h> |
e82f7154 |
62 | |
63 | #include "scan.h" |
64 | |
77d0e3dc |
65 | /*----- File scanner source -----------------------------------------------*/ |
66 | |
67 | /* --- File scanner block --- */ |
68 | |
69 | typedef struct fscan { |
70 | scansrc ss; |
71 | FILE *fp; |
72 | unsigned f; |
73 | } fscan; |
e82f7154 |
74 | |
77d0e3dc |
75 | /* --- @scan@ --- */ |
e82f7154 |
76 | |
77d0e3dc |
77 | static int fscan_scan(scansrc *ss) |
e82f7154 |
78 | { |
77d0e3dc |
79 | fscan *fs = (fscan *)ss; |
80 | int ch = getc(fs->fp); |
81 | if (ch == '\n') |
82 | fs->ss.line++; |
83 | return (ch); |
e82f7154 |
84 | } |
85 | |
77d0e3dc |
86 | /* --- @destroy@ --- */ |
e82f7154 |
87 | |
77d0e3dc |
88 | static void fscan_destroy(scansrc *ss) |
e82f7154 |
89 | { |
77d0e3dc |
90 | fscan *fs = (fscan *)ss; |
91 | if (!(fs->f & SCF_NOCLOSE)) |
92 | fclose(fs->fp); |
7bb7c50b |
93 | xfree(fs->ss.src); |
77d0e3dc |
94 | DESTROY(fs); |
95 | } |
e82f7154 |
96 | |
77d0e3dc |
97 | /* --- File scanner operations --- */ |
98 | |
99 | static scansrc_ops fscan_ops = { fscan_scan, fscan_destroy }; |
100 | |
101 | /* --- @scan_file@ --- * |
102 | * |
103 | * Arguments: @FILE *fp@ = pointer to file descriptor |
7bb7c50b |
104 | * @const char *name@ = pointer to source file name |
77d0e3dc |
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 | |
7bb7c50b |
112 | scansrc *scan_file(FILE *fp, const char *name, unsigned f) |
77d0e3dc |
113 | { |
372a98e2 |
114 | fscan *fs = CREATE(fscan); |
77d0e3dc |
115 | fs->ss.ops = &fscan_ops; |
7bb7c50b |
116 | fs->ss.src = xstrdup(name); |
77d0e3dc |
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 | |
127 | typedef struct avscan { |
128 | scansrc ss; |
129 | char **av; |
130 | char *p; |
131 | } avscan; |
132 | |
133 | /* --- @scan@ --- */ |
e82f7154 |
134 | |
77d0e3dc |
135 | static int avscan_scan(scansrc *ss) |
136 | { |
137 | avscan *as = (avscan *)ss; |
138 | int ch; |
139 | if (!as->p) |
140 | ch = EOF; |
9e1c09df |
141 | else if ((ch = (unsigned char)*as->p++) == 0) { |
77d0e3dc |
142 | as->ss.line++; |
143 | as->p = *as->av++; |
144 | ch = '\n'; |
145 | } |
e82f7154 |
146 | return (ch); |
147 | } |
148 | |
77d0e3dc |
149 | /* --- @destroy@ --- */ |
150 | |
151 | static void avscan_destroy(scansrc *ss) |
e82f7154 |
152 | { |
77d0e3dc |
153 | avscan *as = (avscan *)ss; |
154 | DESTROY(as); |
e82f7154 |
155 | } |
156 | |
77d0e3dc |
157 | /* --- Argv scanner operations --- */ |
158 | |
159 | static 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 | |
171 | scansrc *scan_argv(char **av) |
e82f7154 |
172 | { |
77d0e3dc |
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); |
e82f7154 |
180 | } |
181 | |
77d0e3dc |
182 | /*----- End-of-file sentinel block ----------------------------------------*/ |
183 | |
184 | /* --- @scan@ --- */ |
e82f7154 |
185 | |
77d0e3dc |
186 | static int eof_scan(scansrc *ss) |
e82f7154 |
187 | { |
77d0e3dc |
188 | return (EOF); |
e82f7154 |
189 | } |
190 | |
77d0e3dc |
191 | /* --- @destroy@ --- */ |
192 | |
193 | static void eof_destroy(scansrc *ss) |
e82f7154 |
194 | { |
77d0e3dc |
195 | ; |
e82f7154 |
196 | } |
197 | |
77d0e3dc |
198 | /* --- Eof scanner operations --- */ |
199 | |
200 | static scansrc_ops eof_ops = { eof_scan, eof_destroy }; |
201 | |
202 | /* --- The end of file marker --- */ |
203 | |
7bb7c50b |
204 | static scansrc scan_eof = { &scan_eof, &eof_ops, "<eof>", 0, DSTR_INIT }; |
77d0e3dc |
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 | |
217 | int scan(scanner *sc) |
218 | { |
219 | int ch; |
7bb7c50b |
220 | if (sc->head->pushback.len) |
221 | ch = sc->head->pushback.buf[--sc->head->pushback.len]; |
222 | else { |
77d0e3dc |
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 | { |
7bb7c50b |
249 | DPUTC(&sc->head->pushback, ch); |
77d0e3dc |
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) |
e82f7154 |
263 | { |
77d0e3dc |
264 | ss->next = sc->head; |
265 | if (sc->head == &scan_eof) |
266 | sc->tail = &ss->next; |
267 | sc->head = ss; |
7bb7c50b |
268 | dstr_create(&ss->pushback); |
77d0e3dc |
269 | ss->tok = 0; |
270 | ss->t = 0; |
e82f7154 |
271 | } |
272 | |
77d0e3dc |
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; |
7bb7c50b |
288 | dstr_create(&ss->pushback); |
77d0e3dc |
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); |
e91b672f |
307 | sc->wbegin = sc->wcont = 0; |
77d0e3dc |
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 | */ |
e82f7154 |
318 | |
77d0e3dc |
319 | void scan_destroy(scanner *sc) |
e82f7154 |
320 | { |
77d0e3dc |
321 | scansrc *ss = sc->head; |
322 | while (ss != &scan_eof) { |
323 | scansrc *sss = ss; |
324 | ss = ss->next; |
325 | if (sss->tok) |
7bb7c50b |
326 | xfree(sss->tok); |
327 | dstr_destroy(&sss->pushback); |
77d0e3dc |
328 | sss->ops->destroy(sss); |
329 | } |
e82f7154 |
330 | dstr_destroy(&sc->d); |
77d0e3dc |
331 | if (scan_eof.tok) |
7bb7c50b |
332 | xfree(scan_eof.tok); |
77d0e3dc |
333 | scan_eof.tok = 0; |
334 | sc->head = &scan_eof; |
335 | sc->tail = &sc->head; |
e82f7154 |
336 | } |
337 | |
338 | /*----- That's all, folks -------------------------------------------------*/ |