chiark / gitweb /
Fix the manpage too.
[fwd] / scan.h
1 /* -*-c-*-
2  *
3  * $Id: scan.h,v 1.5 2004/04/08 01:36:25 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 #ifndef SCAN_H
30 #define SCAN_H
31
32 #ifdef __cplusplus
33   extern "C" {
34 #endif
35
36 /*----- Header files ------------------------------------------------------*/
37
38 #include <stdio.h>
39
40 #include <mLib/dstr.h>
41
42 /*----- Data structures ---------------------------------------------------*/
43
44 /* --- A low-level scanner source --- */
45
46 typedef struct scansrc {
47   struct scansrc *next;                 /* Next one in the list */
48   struct scansrc_ops *ops;              /* Pointer to operations table */
49   char *src;                            /* Name of this source */
50   int line;                             /* Current line number */
51   dstr pushback;                        /* Pushback characters */
52   char *tok;                            /* Token pushback */
53   unsigned t;                           /* Token type pushback */
54 } scansrc;
55
56 /* --- Scanner source operations --- */
57
58 typedef struct scansrc_ops {
59   int (*scan)(scansrc */*ss*/);         /* Read another character */
60   void (*destroy)(scansrc */*ss*/);     /* Destroy an unwanted source */
61 } scansrc_ops;
62
63 /* --- A character scanner --- */
64
65 typedef struct scanner {
66   scansrc *head, **tail;                /* Scanner list head and tail */
67   int t;                                /* Token type */
68   dstr d;                               /* Current token value */
69   const char *wbegin, *wcont;           /* Parsing exception strings */
70 } scanner;
71
72 /*----- Particular scanner types ------------------------------------------*/
73
74 /* --- @scan_file@ --- *
75  *
76  * Arguments:   @FILE *fp@ = pointer to file descriptor
77  *              @const char *name@ = pointer to source file name
78  *              @unsigned f@ = flags
79  *
80  * Returns:     A scanner source.
81  *
82  * Use:         Creates a new scanner source for reading from a file.
83  */
84
85 #define SCF_NOCLOSE 1u                  /* Don't close @fp@ when finished */
86
87 extern scansrc *scan_file(FILE */*fp*/, const char */*name*/,
88                           unsigned /*f*/);
89
90 /* --- @scan_argv@ --- *
91  *
92  * Arguments:   @char **av@ = pointer to argument array (null terminated)
93  *
94  * Returns:     A scanner source.
95  *
96  * Use:         Creates a new scanner source for reading from an @argv@
97  *              array.
98  */
99
100 extern scansrc *scan_argv(char **/*av*/);
101
102 /*----- General scanner handling ------------------------------------------*/
103
104 /* --- @scan@ --- *
105  *
106  * Arguments:   @scanner *sc@ = pointer to main scanner context
107  *
108  * Returns:     Character read, or end-of-file.
109  *
110  * Use:         Scans a character from a source of characters.
111  */
112
113 extern int scan(scanner */*sc*/);    
114
115 /* --- @unscan@ --- *
116  *
117  * Arguments:   @scanner *sc@ = pointer to main scanner context
118  *              @int ch@ = character to unscan
119  *
120  * Returns:     ---
121  *
122  * Use:         Scans a character from a source of characters.
123  */
124
125 extern void unscan(scanner */*sc*/, int /*ch*/);
126
127 /* --- @scan_push@ --- *
128  *
129  * Arguments:   @scanner *sc@ = pointer to main scanner context
130  *              @scansrc *ss@ = souorce to push
131  *
132  * Returns:     ---
133  *
134  * Use:         Pushes a scanner source onto the front of the queue.
135  */
136
137 extern void scan_push(scanner */*sc*/, scansrc */*ss*/);
138
139 /* --- @scan_add@ --- *
140  *
141  * Arguments:   @scanner *sc@ = pointer to main scanner context
142  *              @scansrc *ss@ = souorce to push
143  *
144  * Returns:     ---
145  *
146  * Use:         Adds a scanner source onto the end of the queue.
147  */
148
149 extern void scan_add(scanner */*sc*/, scansrc */*ss*/);
150
151 /* --- @scan_create@ --- *
152  *
153  * Arguments:   @scanner *sc@ = scanner context to initialize
154  *
155  * Returns:     ---
156  *
157  * Use:         Initializes a scanner block ready for use.
158  */
159
160 extern void scan_create(scanner */*sc*/);
161
162 /* --- @scan_destroy@ --- *
163  *
164  * Arguments:   @scanner *sc@ = pointer to scanner context
165  *
166  * Returns:     ---
167  *
168  * Use:         Destroys a scanner and all the sources attached to it.
169  */
170
171 extern void scan_destroy(scanner */*sc*/);
172
173 /*----- That's all, folks -------------------------------------------------*/
174
175 #ifdef __cplusplus
176   }
177 #endif
178
179 #endif