3 * $Id: conf.c,v 1.2 1999/07/26 23:28:39 mdw Exp $
5 * Configuration parsing
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the `fw' port forwarder.
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.2 1999/07/26 23:28:39 mdw
33 * Major reconstruction work for new design.
35 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
40 /*----- Header files ------------------------------------------------------*/
51 #include <mLib/dstr.h>
52 #include <mLib/quis.h>
53 #include <mLib/report.h>
64 /*----- Source and target tables ------------------------------------------*/
66 static source_ops *sources[] = { &xsource_ops, &fsource_ops, &ssource_ops };
67 static target_ops *targets[] = { &xtarget_ops, &ftarget_ops, &starget_ops };
69 /*----- Main code ---------------------------------------------------------*/
73 * Arguments: @scanner *sc@ = pointer to scanner definition
75 * Returns: Type of token scanned.
77 * Use: Reads the next token from the character scanner.
80 int token(scanner *sc)
83 '{': case '}': case '/': case ',': \
84 case '=': case ':': case ';': \
85 case '.': case '[': case ']'
91 /* --- Main tokenization --- */
96 /* --- Deal with pushed-back tokens --- */
99 dstr_puts(&sc->d, sc->head->tok);
106 else if (isspace((unsigned char)ch))
110 /* --- End of file --- */
116 /* --- Comment character --- */
119 do ch = scan(sc); while (ch != EOF && ch != '\n');
122 /* --- Various self-delimiting characters --- */
125 dstr_putc(&sc->d, ch);
130 /* --- Bare words --- *
132 * These aren't as bare any more. You can now backslash-escape
133 * individual characters, and enclose sections in double-quotes.
157 if (!q && isspace((unsigned char)(ch)))
175 /* printf("token `%s'\n", sc->d.buf); */
179 /* --- @pushback@ --- *
181 * Arguments: @scanner *sc@ = pointer to scanner definition
185 * Use: Pushes the current token back. This is normally a precursor
186 * to pushing a new scanner source.
189 static void pushback(scanner *sc)
191 sc->head->tok = xstrdup(sc->d.buf);
197 * Arguments: @scanner *sc@ = pointer to scanner definition
198 * @const char *msg@ = message skeleton string
199 * @...@ = extra arguments for the skeleton
203 * Use: Reports an error at the current scanner location.
206 void error(scanner *sc, const char *msg, ...)
210 fprintf(stderr, "%s: %s:%i: ", QUIS, sc->head->src, sc->head->line);
211 vfprintf(stderr, msg, ap);
216 /* --- @conf_enum@ --- *
218 * Arguments: @scanner *sc@ = pointer to a scanner object
219 * @const char *list@ = comma-separated things to allow
220 * @unsigned f@ = flags for the search
221 * @const char *err@ = error message if not found
223 * Returns: Index into list, zero-based, or @-1@.
225 * Use: Checks whether the current token is a string which matches
226 * one of the comma-separated items given. If not, an error is
227 * reported; otherwise the index of the matched item is
231 int conf_enum(scanner *sc, const char *list, unsigned f, const char *err)
238 /* --- Make sure it's a string --- */
240 if (sc->t != CTOK_WORD)
241 error(sc, "parse error, expected %s", err);
243 /* --- Grind through the list --- */
255 } else if (chosen != -1) {
259 else if (f & ENUM_NONE)
262 error(sc, "unknown %s `%s'", err, sc->d.buf);
276 if ((f & ENUM_ABBREV) && !*q) {
278 error(sc, "ambiguous %s `%s'", err, sc->d.buf);
292 /* --- @conf_prefix@ --- *
294 * Arguments: @scanner *sc@ = pointer to a scanner object
295 * @const char *p@ = pointer to prefix string to check
297 * Returns: Nonzero if the prefix matches.
299 * Use: If the current token is a word matching the given prefix
300 * string, then it and an optional `.' character are removed and
301 * a nonzero result is returned. Otherwise the current token is
302 * left as it is, and zero is returned.
304 * Typical options parsing code would remove an expected prefix,
305 * scan an option anyway (since qualifying prefixes are
306 * optional) and if a match is found, claim the option. If no
307 * match is found, and a prefix was stripped, then an error
308 * should be reported.
311 int conf_prefix(scanner *sc, const char *p)
313 if (sc->t == CTOK_WORD && strcmp(p, sc->d.buf) == 0) {
322 /* --- @conf_name@ --- *
324 * Arguments: @scanner *sc@ = pointer to scanner
325 * @char delim@ = delimiter character to look for
326 * @dstr *d@ = pointer to dynamic string for output
330 * Use: Reads in a compound name consisting of words separated by
331 * delimiters. Leading and trailing delimiters are permitted,
332 * although they'll probably cause confusion if used. The name
333 * may be enclosed in square brackets if that helps at all.
335 * Examples of compound names are filenames (delimited by `/')
336 * and IP addresses (delimited by `.').
339 void conf_name(scanner *sc, char delim, dstr *d)
347 /* --- Read an optional opening bracket --- */
354 /* --- Do the main reading sequence --- */
357 if (sc->t == delim) {
362 if (sc->t == CTOK_WORD) {
367 } while (sc->t == delim);
369 /* --- Check that the string was OK --- */
372 error(sc, "parse error, name expected");
374 /* --- Read a closing bracket --- */
380 error(sc, "parse error, missing `]'");
385 /* --- @conf_parse@ --- *
387 * Arguments: @scanner *sc@ = pointer to scanner definition
391 * Use: Parses a configuration file from the scanner.
394 void conf_parse(scanner *sc)
399 if (sc->t == CTOK_EOF)
401 if (sc->t != CTOK_WORD)
402 error(sc, "parse error, keyword expected");
404 /* --- Handle a forwarding request --- */
406 if (strcmp(sc->d.buf, "forward") == 0 ||
407 strcmp(sc->d.buf, "fw") == 0 ||
408 strcmp(sc->d.buf, "from") == 0) {
414 /* --- Read a source description --- */
419 /* --- Try to find a source type which understands --- */
422 for (sops = sources; *sops; sops++) {
423 if ((s = (*sops)->read(sc)) != 0)
426 error(sc, "unknown source name `%s'", sc->d.buf);
428 /* --- Read any source-specific options --- */
433 while (sc->t == CTOK_WORD) {
434 if (!s->ops->option || !s->ops->option(s, sc)) {
435 error(sc, "unknown %s source option `%s'",
436 s->ops->name, sc->d.buf);
442 error(sc, "parse error, missing `}'");
447 /* --- Read a destination description --- */
449 if (sc->t == CTOK_WORD && (strcmp(sc->d.buf, "to") == 0 ||
450 strcmp(sc->d.buf, "->") == 0))
456 /* --- Try to find a target which understands --- */
459 for (tops = targets; *tops; tops++) {
460 if ((t = (*tops)->read(sc)) != 0)
463 error(sc, "unknown target name `%s'", sc->d.buf);
465 /* --- Read any target-specific options --- */
470 while (sc->t == CTOK_WORD) {
471 if (!t->ops->option || !t->ops->option(t, sc)) {
472 error(sc, "unknown %s target option `%s'",
473 t->ops->name, sc->d.buf);
479 error(sc, "parse error, `}' expected");
484 /* --- Combine the source and target --- */
486 s->ops->attach(s, sc, t);
489 /* --- Include configuration from a file --- *
491 * Slightly tricky. Scan the optional semicolon from the including
492 * stream, not the included one.
495 else if (strcmp(sc->d.buf, "include") == 0) {
500 conf_name(sc, '/', &d);
501 if ((fp = fopen(d.buf, "r")) == 0)
502 error(sc, "can't include `%s': %s", d.buf, strerror(errno));
506 scan_push(sc, scan_file(fp, xstrdup(d.buf), SCF_FREENAME));
509 continue; /* Don't parse a trailing `;' */
512 /* --- Other configuration is handled elsewhere --- */
516 /* --- First try among the sources --- */
521 for (sops = sources; *sops; sops++) {
522 if ((*sops)->option && (*sops)->option(0, sc))
527 /* --- Then try among the targets --- */
532 for (tops = targets; *tops; tops++) {
533 if ((*tops)->option && (*tops)->option(0, sc))
538 /* --- Nobody wants the option --- */
540 error(sc, "unknown global option or prefix `%s'", sc->d.buf);
550 /*----- That's all, folks -------------------------------------------------*/