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