e82f7154 |
1 | /* -*-c-*- |
2 | * |
c36e735a |
3 | * $Id: conf.c,v 1.10 2002/02/22 23:42:56 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.c,v $ |
c36e735a |
32 | * Revision 1.10 2002/02/22 23:42:56 mdw |
33 | * `fw'-specific configuration code moved out. This file might become part |
34 | * of a library some day. |
35 | * |
2234f01d |
36 | * Revision 1.9 2002/01/13 14:48:16 mdw |
37 | * Make delimiters be a property of a scanner. Change the delimiter- |
38 | * changing functions' names. |
39 | * |
a8b9c5eb |
40 | * Revision 1.8 2001/02/03 20:33:26 mdw |
41 | * Fix flags to be unsigned. |
42 | * |
9e1c09df |
43 | * Revision 1.7 2000/08/01 17:58:10 mdw |
44 | * Fix subtleties with <ctype.h> functions. |
45 | * |
5835a4a8 |
46 | * Revision 1.6 2000/02/12 18:13:20 mdw |
47 | * Terminate tables of sources and targets. |
48 | * |
82793759 |
49 | * Revision 1.5 1999/10/22 22:46:44 mdw |
50 | * Improve documentation for conf_enum. |
51 | * |
a6eddf54 |
52 | * Revision 1.4 1999/10/15 21:12:36 mdw |
53 | * Remove redundant debugging code. |
54 | * |
e73034b0 |
55 | * Revision 1.3 1999/08/19 18:32:48 mdw |
56 | * Improve lexical analysis. In particular, `chmod' patterns don't have to |
57 | * be quoted any more. |
58 | * |
61e3dbdf |
59 | * Revision 1.2 1999/07/26 23:28:39 mdw |
60 | * Major reconstruction work for new design. |
61 | * |
62 | * Revision 1.1.1.1 1999/07/01 08:56:23 mdw |
63 | * Initial revision. |
e82f7154 |
64 | * |
65 | */ |
66 | |
67 | /*----- Header files ------------------------------------------------------*/ |
68 | |
e82f7154 |
69 | #include <ctype.h> |
70 | #include <errno.h> |
71 | #include <stdarg.h> |
72 | #include <stdio.h> |
73 | #include <stdlib.h> |
74 | #include <string.h> |
75 | |
e82f7154 |
76 | #include <mLib/dstr.h> |
77 | #include <mLib/quis.h> |
78 | #include <mLib/report.h> |
79 | |
61e3dbdf |
80 | #include "conf.h" |
e82f7154 |
81 | #include "scan.h" |
e82f7154 |
82 | |
83 | /*----- Main code ---------------------------------------------------------*/ |
84 | |
2234f01d |
85 | /* --- @conf_undelim@ --- * |
e73034b0 |
86 | * |
2234f01d |
87 | * Arguments: @scanner *sc@ = pointer to scanner definition |
88 | * @const char *d, *dd@ = pointer to characters to escape |
e73034b0 |
89 | * |
90 | * Returns: --- |
91 | * |
92 | * Use: Modifies the tokenizer. Characters in the first list will |
93 | * always be considered to begin a word. Characters in the |
94 | * second list will always be allowed to continue a word. |
95 | */ |
96 | |
2234f01d |
97 | void conf_undelim(scanner *sc, const char *d, const char *dd) |
98 | { |
99 | sc->wbegin = d; |
100 | sc->wcont = dd; |
101 | } |
e73034b0 |
102 | |
e82f7154 |
103 | /* --- @token@ --- * |
104 | * |
105 | * Arguments: @scanner *sc@ = pointer to scanner definition |
106 | * |
107 | * Returns: Type of token scanned. |
108 | * |
109 | * Use: Reads the next token from the character scanner. |
110 | */ |
111 | |
61e3dbdf |
112 | int token(scanner *sc) |
e82f7154 |
113 | { |
61e3dbdf |
114 | #define SELFDELIM \ |
115 | '{': case '}': case '/': case ',': \ |
116 | case '=': case ':': case ';': \ |
117 | case '.': case '[': case ']' |
e82f7154 |
118 | |
119 | int ch; |
120 | |
61e3dbdf |
121 | DRESET(&sc->d); |
122 | |
123 | /* --- Main tokenization --- */ |
124 | |
e82f7154 |
125 | for (;;) { |
61e3dbdf |
126 | ch = scan(sc); |
127 | |
128 | /* --- Deal with pushed-back tokens --- */ |
129 | |
130 | if (sc->head->tok) { |
131 | dstr_puts(&sc->d, sc->head->tok); |
c36e735a |
132 | xfree(sc->head->tok); |
61e3dbdf |
133 | sc->head->tok = 0; |
134 | sc->t = sc->head->t; |
135 | goto done; |
136 | } |
137 | |
9e1c09df |
138 | else if (isspace(ch)) |
e82f7154 |
139 | ; |
140 | else switch (ch) { |
61e3dbdf |
141 | |
142 | /* --- End of file --- */ |
143 | |
e82f7154 |
144 | case EOF: |
61e3dbdf |
145 | sc->t = CTOK_EOF; |
146 | goto done; |
147 | |
148 | /* --- Comment character --- */ |
149 | |
e82f7154 |
150 | case '#': |
61e3dbdf |
151 | do ch = scan(sc); while (ch != EOF && ch != '\n'); |
e82f7154 |
152 | break; |
61e3dbdf |
153 | |
154 | /* --- Various self-delimiting characters --- */ |
155 | |
156 | case SELFDELIM: |
2234f01d |
157 | if (!sc->wbegin || strchr(sc->wbegin, ch) == 0) { |
e73034b0 |
158 | dstr_putc(&sc->d, ch); |
159 | dstr_putz(&sc->d); |
160 | sc->t = ch; |
161 | goto done; |
162 | } |
61e3dbdf |
163 | |
164 | /* --- Bare words --- * |
165 | * |
166 | * These aren't as bare any more. You can now backslash-escape |
167 | * individual characters, and enclose sections in double-quotes. |
168 | */ |
169 | |
170 | default: { |
171 | int q = 0; |
172 | |
173 | for (;;) { |
174 | switch (ch) { |
175 | case EOF: |
176 | goto word; |
177 | case '\\': |
178 | ch = scan(sc); |
179 | if (ch == EOF) |
180 | goto word; |
181 | DPUTC(&sc->d, ch); |
182 | break; |
183 | case '\"': |
184 | q = !q; |
185 | break; |
186 | case SELFDELIM: |
2234f01d |
187 | if (q || (sc->wcont && strchr(sc->wcont, ch))) |
61e3dbdf |
188 | goto insert; |
189 | goto word; |
190 | default: |
9e1c09df |
191 | if (!q && isspace(ch)) |
61e3dbdf |
192 | goto word; |
193 | insert: |
194 | DPUTC(&sc->d, ch); |
195 | break; |
196 | } |
197 | ch = scan(sc); |
198 | } |
199 | word: |
200 | unscan(sc, ch); |
e82f7154 |
201 | DPUTZ(&sc->d); |
61e3dbdf |
202 | sc->t = CTOK_WORD; |
203 | goto done; |
204 | } |
e82f7154 |
205 | } |
206 | } |
207 | |
61e3dbdf |
208 | done: |
61e3dbdf |
209 | return (sc->t); |
210 | } |
211 | |
212 | /* --- @pushback@ --- * |
213 | * |
214 | * Arguments: @scanner *sc@ = pointer to scanner definition |
215 | * |
216 | * Returns: --- |
217 | * |
218 | * Use: Pushes the current token back. This is normally a precursor |
219 | * to pushing a new scanner source. |
220 | */ |
221 | |
c36e735a |
222 | void pushback(scanner *sc) |
61e3dbdf |
223 | { |
224 | sc->head->tok = xstrdup(sc->d.buf); |
225 | sc->head->t = sc->t; |
e82f7154 |
226 | } |
227 | |
228 | /* --- @error@ --- * |
229 | * |
230 | * Arguments: @scanner *sc@ = pointer to scanner definition |
231 | * @const char *msg@ = message skeleton string |
232 | * @...@ = extra arguments for the skeleton |
233 | * |
234 | * Returns: Doesn't |
235 | * |
236 | * Use: Reports an error at the current scanner location. |
237 | */ |
238 | |
61e3dbdf |
239 | void error(scanner *sc, const char *msg, ...) |
e82f7154 |
240 | { |
241 | va_list ap; |
242 | va_start(ap, msg); |
61e3dbdf |
243 | fprintf(stderr, "%s: %s:%i: ", QUIS, sc->head->src, sc->head->line); |
e82f7154 |
244 | vfprintf(stderr, msg, ap); |
245 | fputc('\n', stderr); |
246 | exit(1); |
247 | } |
248 | |
61e3dbdf |
249 | /* --- @conf_enum@ --- * |
e82f7154 |
250 | * |
61e3dbdf |
251 | * Arguments: @scanner *sc@ = pointer to a scanner object |
252 | * @const char *list@ = comma-separated things to allow |
253 | * @unsigned f@ = flags for the search |
254 | * @const char *err@ = error message if not found |
e82f7154 |
255 | * |
61e3dbdf |
256 | * Returns: Index into list, zero-based, or @-1@. |
e82f7154 |
257 | * |
61e3dbdf |
258 | * Use: Checks whether the current token is a string which matches |
82793759 |
259 | * one of the comma-separated items given. The return value is |
260 | * the index (zero-based) of the matched string in the list. |
261 | * |
262 | * The flags control the behaviour if no exact match is found. |
263 | * If @ENUM_ABBREV@ is set, and the current token is a left |
264 | * substring of exactly one of the possibilities, then that one |
265 | * is chosen. If @ENUM_NONE@ is set, the value @-1@ is |
266 | * returned; otherwise an error is reported and the program is |
267 | * terminated. |
e82f7154 |
268 | */ |
269 | |
61e3dbdf |
270 | int conf_enum(scanner *sc, const char *list, unsigned f, const char *err) |
e82f7154 |
271 | { |
61e3dbdf |
272 | const char *p, *q; |
273 | int chosen = -1; |
274 | int ok; |
275 | int index; |
276 | |
277 | /* --- Make sure it's a string --- */ |
278 | |
279 | if (sc->t != CTOK_WORD) |
280 | error(sc, "parse error, expected %s", err); |
281 | |
282 | /* --- Grind through the list --- */ |
283 | |
284 | q = sc->d.buf; |
285 | ok = 1; |
286 | index = 0; |
287 | p = list; |
288 | for (;;) { |
289 | switch (*p) { |
290 | case 0: |
291 | if (ok && !*q) { |
292 | token(sc); |
293 | return (index); |
294 | } else if (chosen != -1) { |
295 | token(sc); |
296 | return (chosen); |
297 | } |
298 | else if (f & ENUM_NONE) |
299 | return (-1); |
300 | else |
301 | error(sc, "unknown %s `%s'", err, sc->d.buf); |
302 | break; |
303 | case ',': |
304 | if (ok && !*q) { |
305 | token(sc); |
306 | return (index); |
307 | } |
308 | ok = 1; |
309 | q = sc->d.buf; |
310 | index++; |
311 | break; |
312 | default: |
313 | if (!ok) |
314 | break; |
315 | if ((f & ENUM_ABBREV) && !*q) { |
316 | if (chosen != -1) |
317 | error(sc, "ambiguous %s `%s'", err, sc->d.buf); |
318 | chosen = index; |
319 | ok = 0; |
320 | } |
321 | if (*p == *q) |
322 | q++; |
323 | else |
324 | ok = 0; |
325 | break; |
326 | } |
327 | p++; |
328 | } |
e82f7154 |
329 | } |
330 | |
61e3dbdf |
331 | /* --- @conf_prefix@ --- * |
e82f7154 |
332 | * |
61e3dbdf |
333 | * Arguments: @scanner *sc@ = pointer to a scanner object |
334 | * @const char *p@ = pointer to prefix string to check |
e82f7154 |
335 | * |
61e3dbdf |
336 | * Returns: Nonzero if the prefix matches. |
e82f7154 |
337 | * |
61e3dbdf |
338 | * Use: If the current token is a word matching the given prefix |
339 | * string, then it and an optional `.' character are removed and |
340 | * a nonzero result is returned. Otherwise the current token is |
341 | * left as it is, and zero is returned. |
342 | * |
343 | * Typical options parsing code would remove an expected prefix, |
344 | * scan an option anyway (since qualifying prefixes are |
345 | * optional) and if a match is found, claim the option. If no |
346 | * match is found, and a prefix was stripped, then an error |
347 | * should be reported. |
e82f7154 |
348 | */ |
349 | |
61e3dbdf |
350 | int conf_prefix(scanner *sc, const char *p) |
e82f7154 |
351 | { |
61e3dbdf |
352 | if (sc->t == CTOK_WORD && strcmp(p, sc->d.buf) == 0) { |
353 | token(sc); |
354 | if (sc->t == '.') |
355 | token(sc); |
356 | return (1); |
357 | } |
358 | return (0); |
359 | } |
e82f7154 |
360 | |
61e3dbdf |
361 | /* --- @conf_name@ --- * |
362 | * |
363 | * Arguments: @scanner *sc@ = pointer to scanner |
364 | * @char delim@ = delimiter character to look for |
365 | * @dstr *d@ = pointer to dynamic string for output |
366 | * |
367 | * Returns: --- |
368 | * |
369 | * Use: Reads in a compound name consisting of words separated by |
370 | * delimiters. Leading and trailing delimiters are permitted, |
371 | * although they'll probably cause confusion if used. The name |
372 | * may be enclosed in square brackets if that helps at all. |
373 | * |
374 | * Examples of compound names are filenames (delimited by `/') |
375 | * and IP addresses (delimited by `.'). |
376 | */ |
e82f7154 |
377 | |
61e3dbdf |
378 | void conf_name(scanner *sc, char delim, dstr *d) |
379 | { |
380 | unsigned f = 0; |
a8b9c5eb |
381 | |
382 | #define f_ok 1u |
383 | #define f_bra 2u |
e82f7154 |
384 | |
61e3dbdf |
385 | /* --- Read an optional opening bracket --- */ |
e82f7154 |
386 | |
61e3dbdf |
387 | if (sc->t == '[') { |
e82f7154 |
388 | token(sc); |
2234f01d |
389 | f |= f_bra | f_ok; |
61e3dbdf |
390 | } |
e82f7154 |
391 | |
61e3dbdf |
392 | /* --- Do the main reading sequence --- */ |
e82f7154 |
393 | |
61e3dbdf |
394 | do { |
395 | if (sc->t == delim) { |
396 | DPUTC(d, delim); |
397 | f |= f_ok; |
e82f7154 |
398 | token(sc); |
61e3dbdf |
399 | } |
400 | if (sc->t == CTOK_WORD) { |
401 | DPUTD(d, &sc->d); |
402 | f |= f_ok; |
e82f7154 |
403 | token(sc); |
404 | } |
61e3dbdf |
405 | } while (sc->t == delim); |
e82f7154 |
406 | |
61e3dbdf |
407 | /* --- Check that the string was OK --- */ |
e82f7154 |
408 | |
61e3dbdf |
409 | if (!(f & f_ok)) |
410 | error(sc, "parse error, name expected"); |
e82f7154 |
411 | |
61e3dbdf |
412 | /* --- Read a closing bracket --- */ |
e82f7154 |
413 | |
61e3dbdf |
414 | if (f & f_bra) { |
415 | if (sc->t == ']') |
416 | token(sc); |
417 | else |
418 | error(sc, "parse error, missing `]'"); |
419 | } |
420 | DPUTZ(d); |
a8b9c5eb |
421 | |
422 | #undef f_ok |
423 | #undef f_bra |
e82f7154 |
424 | } |
425 | |
e82f7154 |
426 | /*----- That's all, folks -------------------------------------------------*/ |