Improve lexical analysis. In particular, `chmod' patterns don't have to
[fwd] / conf.h
1 /* -*-c-*-
2 *
3 * $Id: conf.h,v 1.4 1999/08/19 18:32:48 mdw Exp $
4 *
5 * Configuration parsing
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: conf.h,v $
32 * Revision 1.4 1999/08/19 18:32:48 mdw
33 * Improve lexical analysis. In particular, `chmod' patterns don't have to
34 * be quoted any more.
35 *
36 * Revision 1.3 1999/07/27 18:30:14 mdw
37 * Improve documentation and layout for @CONF_BEGIN@ and friends. Remove
38 * irritating warning about unused label by introducing a spurious `goto'.
39 *
40 * Revision 1.2 1999/07/26 23:28:39 mdw
41 * Major reconstruction work for new design.
42 *
43 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
44 * Initial revision.
45 *
46 */
47
48 #ifndef CONF_H
49 #define CONF_H
50
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54
55 /*----- Header files ------------------------------------------------------*/
56
57 #include <mLib/dstr.h>
58
59 #ifndef SCAN_H
60 # include "scan.h"
61 #endif
62
63 /*----- Magic numbers -----------------------------------------------------*/
64
65 #define CTOK_EOF (-1)
66 #define CTOK_WORD 256
67
68 /*----- Functions provided ------------------------------------------------*/
69
70 /* --- @undelim@ --- *
71 *
72 * Arguments: @const char *d, dd@ = pointer to characters to escape
73 *
74 * Returns: ---
75 *
76 * Use: Modifies the tokenizer. Characters in the first list will
77 * always be considered to begin a word. Characters in the
78 * second list will always be allowed to continue a word.
79 */
80
81 extern void undelim(const char */*d*/, const char */*dd*/);
82
83 /* --- @token@ --- *
84 *
85 * Arguments: @scanner *sc@ = pointer to scanner definition
86 *
87 * Returns: Type of token scanned.
88 *
89 * Use: Reads the next token from the character scanner.
90 */
91
92 extern int token(scanner */*sc*/);
93
94 /* --- @error@ --- *
95 *
96 * Arguments: @scanner *sc@ = pointer to scanner definition
97 * @const char *msg@ = message skeleton string
98 * @...@ = extra arguments for the skeleton
99 *
100 * Returns: Doesn't
101 *
102 * Use: Reports an error at the current scanner location.
103 */
104
105 extern void error(scanner */*sc*/, const char */*msg*/, ...);
106
107 /* --- @conf_enum@ --- *
108 *
109 * Arguments: @scanner *sc@ = pointer to a scanner object
110 * @const char *list@ = comma-separated things to allow
111 * @unsigned @f = flags for the search
112 * @const char *err@ = error message if not found
113 *
114 * Returns: Index into list, zero-based, or @-1@.
115 *
116 * Use: Checks whether the current token is a string which matches
117 * one of the comma-separated items given. If not, an error is
118 * reported; otherwise the index of the matched item is
119 * returned.
120 */
121
122 #define ENUM_ABBREV 1u
123 #define ENUM_NONE 2u
124
125 extern int conf_enum(scanner */*sc*/, const char */*list*/,
126 unsigned /*flags*/, const char */*err*/);
127
128 /* --- @conf_prefix@ --- *
129 *
130 * Arguments: @scanner *sc@ = pointer to a scanner object
131 * @const char *p@ = pointer to prefix string to check
132 *
133 * Returns: Nonzero if the prefix matches.
134 *
135 * Use: If the current token is a word matching the given prefix
136 * string, then it and an optional `.' character are removed and
137 * a nonzero result is returned. Otherwise the current token is
138 * left as it is, and zero is returned.
139 *
140 * Typical options parsing code would remove an expected prefix,
141 * scan an option anyway (since qualifying prefixes are
142 * optional) and if a match is found, claim the option. If no
143 * match is found, and a prefix was stripped, then an error
144 * should be reported.
145 */
146
147 extern int conf_prefix(scanner */*sc*/, const char */*p*/);
148
149 /* --- @CONF_BEGIN@, @CONF_END@ --- *
150 *
151 * Arguments: @sc@ = scanner to read from
152 * @prefix@ = prefix to scan for
153 * @desc@ = description of what we're parsing
154 *
155 * Use: Bracket an options parsing routine. The current token is
156 * checked to see whether it matches the prefix. If so, it is
157 * removed and the following token examined. If that's a `.'
158 * then it's removed. If it's a `{' then the enclosed
159 * option-parsing code is executed in a loop until a matching
160 * '}' is found. If the options parser doesn't accept an
161 * option, the behaviour is dependent on whether a prefix was
162 * seen: if so, an error is reported; otherwse a zero return is
163 * made.
164 */
165
166 #define CS_PLAIN 0
167 #define CS_PREFIX 1
168 #define CS_BRACE 2
169 #define CS_UNKNOWN 3
170
171 #define CONF_BEGIN(sc, prefix, desc) do { \
172 scanner *_conf_sc = (sc); \
173 const char *_conf_desc = (desc); \
174 int _conf_state = CS_PLAIN; \
175 \
176 /* --- Read the initial prefix --- */ \
177 \
178 if (_conf_sc->t == CTOK_WORD && \
179 strcmp(_conf_sc->d.buf, (prefix)) == 0) { \
180 token(_conf_sc); \
181 _conf_state = CS_PREFIX; \
182 if (_conf_sc->t == '.') \
183 token(_conf_sc); \
184 else if (_conf_sc->t == '{') { \
185 token(_conf_sc); \
186 _conf_state = CS_BRACE; \
187 } \
188 } \
189 \
190 /* --- Ensure the next token is a word --- */ \
191 \
192 if (_conf_sc->t != CTOK_WORD) \
193 error(_conf_sc, "parse error, expected option keyword"); \
194 do {
195
196 #define CONF_END \
197 \
198 /* --- Reject an option --- * \
199 * \
200 * We could get here as a result of an explicit @CONF_REJECT@ or \
201 * because the option wasn't accepted. \
202 */ \
203 \
204 goto _conf_reject; \
205 _conf_reject: \
206 if (_conf_state == CS_PLAIN) \
207 _conf_state = CS_UNKNOWN; \
208 else { \
209 error(_conf_sc, "unknown %s option `%s'", \
210 _conf_desc, _conf_sc->d.buf); \
211 } \
212 \
213 /* --- Accept an option --- * \
214 * \
215 * It's safe to drop through from above. Either an error will have \
216 * been reported, or the state is not @CS_BRACE@. \
217 */ \
218 \
219 _conf_accept: \
220 if (_conf_state == CS_BRACE && _conf_sc->t == ';') \
221 token(_conf_sc); \
222 } while (_conf_state == CS_BRACE && _conf_sc->t == CTOK_WORD); \
223 \
224 /* --- Check for a closing brace --- */ \
225 \
226 if (_conf_state == CS_BRACE) { \
227 if (_conf_sc->t == '}') \
228 token(_conf_sc); \
229 else \
230 error(_conf_sc, "parse error, expected `}'"); \
231 } \
232 \
233 /* --- Return an appropriate value --- */ \
234 \
235 return (_conf_state != CS_UNKNOWN); \
236 } while (0)
237
238 /* --- @CONF_ACCEPT@, @CONF_REJECT@ --- *
239 *
240 * Arguments: ---
241 *
242 * Use: Within an options parser (between @CONF_BEGIN@ and
243 * @CONF_END@), accept or reject an option.
244 */
245
246 #define CONF_ACCEPT goto _conf_accept
247 #define CONF_REJECT goto _conf_reject
248
249 /* --- @CONF_QUAL@ --- *
250 *
251 * Arguments: ---
252 *
253 * Use: Evaluates to a nonzero value if the current option is
254 * qualified. This can be used to decide whether abbreviations
255 * for options should be accepted.
256 */
257
258 #define CONF_QUAL (_conf_state != CS_PLAIN)
259
260 /* --- @conf_name@ --- *
261 *
262 * Arguments: @scanner *sc@ = pointer to scanner
263 * @char delim@ = delimiter character to look for
264 * @dstr *d@ = pointer to dynamic string for output
265 *
266 * Returns: ---
267 *
268 * Use: Reads in a compound name consisting of words separated by
269 * delimiters. Leading and trailing delimiters are permitted,
270 * although they'll probably cause confusion if used. The name
271 * may be enclosed in square brackets if that helps at all.
272 *
273 * Examples of compound names are filenames (delimited by `/')
274 * and IP addresses (delimited by `.').
275 */
276
277 extern void conf_name(scanner */*sc*/, char /*delim*/, dstr */*d*/);
278
279 /* --- @conf_parse@ --- *
280 *
281 * Arguments: @scanner *sc@ = pointer to a scanner structure
282 *
283 * Returns: ---
284 *
285 * Use: Parses a configuration file fragment from the scanner
286 */
287
288 extern void conf_parse(scanner *sc);
289
290 /*----- That's all, folks -------------------------------------------------*/
291
292 #ifdef __cplusplus
293 }
294 #endif
295
296 #endif