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