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