3 * $Id: conf.c,v 1.7 2000/08/01 17:58:10 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.7 2000/08/01 17:58:10 mdw
33 * Fix subtleties with <ctype.h> functions.
35 * Revision 1.6 2000/02/12 18:13:20 mdw
36 * Terminate tables of sources and targets.
38 * Revision 1.5 1999/10/22 22:46:44 mdw
39 * Improve documentation for conf_enum.
41 * Revision 1.4 1999/10/15 21:12:36 mdw
42 * Remove redundant debugging code.
44 * Revision 1.3 1999/08/19 18:32:48 mdw
45 * Improve lexical analysis. In particular, `chmod' patterns don't have to
48 * Revision 1.2 1999/07/26 23:28:39 mdw
49 * Major reconstruction work for new design.
51 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
56 /*----- Header files ------------------------------------------------------*/
67 #include <mLib/dstr.h>
68 #include <mLib/quis.h>
69 #include <mLib/report.h>
80 /*----- Source and target tables ------------------------------------------*/
82 static source_ops *sources[] =
83 { &xsource_ops, &fsource_ops, &ssource_ops, 0 };
84 static target_ops *targets[] =
85 { &xtarget_ops, &ftarget_ops, &starget_ops, 0 };
87 static const char *notword = 0;
88 static const char *notdelim = 0;
90 /*----- Main code ---------------------------------------------------------*/
92 /* --- @undelim@ --- *
94 * Arguments: @const char *d, dd@ = pointer to characters to escape
98 * Use: Modifies the tokenizer. Characters in the first list will
99 * always be considered to begin a word. Characters in the
100 * second list will always be allowed to continue a word.
103 void undelim(const char *d, const char *dd) { notword = d; notdelim = dd; }
107 * Arguments: @scanner *sc@ = pointer to scanner definition
109 * Returns: Type of token scanned.
111 * Use: Reads the next token from the character scanner.
114 int token(scanner *sc)
117 '{': case '}': case '/': case ',': \
118 case '=': case ':': case ';': \
119 case '.': case '[': case ']'
125 /* --- Main tokenization --- */
130 /* --- Deal with pushed-back tokens --- */
133 dstr_puts(&sc->d, sc->head->tok);
140 else if (isspace(ch))
144 /* --- End of file --- */
150 /* --- Comment character --- */
153 do ch = scan(sc); while (ch != EOF && ch != '\n');
156 /* --- Various self-delimiting characters --- */
159 if (!notword || strchr(notword, ch) == 0) {
160 dstr_putc(&sc->d, ch);
166 /* --- Bare words --- *
168 * These aren't as bare any more. You can now backslash-escape
169 * individual characters, and enclose sections in double-quotes.
189 if (q || (notdelim && strchr(notdelim, ch)))
193 if (!q && isspace(ch))
214 /* --- @pushback@ --- *
216 * Arguments: @scanner *sc@ = pointer to scanner definition
220 * Use: Pushes the current token back. This is normally a precursor
221 * to pushing a new scanner source.
224 static void pushback(scanner *sc)
226 sc->head->tok = xstrdup(sc->d.buf);
232 * Arguments: @scanner *sc@ = pointer to scanner definition
233 * @const char *msg@ = message skeleton string
234 * @...@ = extra arguments for the skeleton
238 * Use: Reports an error at the current scanner location.
241 void error(scanner *sc, const char *msg, ...)
245 fprintf(stderr, "%s: %s:%i: ", QUIS, sc->head->src, sc->head->line);
246 vfprintf(stderr, msg, ap);
251 /* --- @conf_enum@ --- *
253 * Arguments: @scanner *sc@ = pointer to a scanner object
254 * @const char *list@ = comma-separated things to allow
255 * @unsigned f@ = flags for the search
256 * @const char *err@ = error message if not found
258 * Returns: Index into list, zero-based, or @-1@.
260 * Use: Checks whether the current token is a string which matches
261 * one of the comma-separated items given. The return value is
262 * the index (zero-based) of the matched string in the list.
264 * The flags control the behaviour if no exact match is found.
265 * If @ENUM_ABBREV@ is set, and the current token is a left
266 * substring of exactly one of the possibilities, then that one
267 * is chosen. If @ENUM_NONE@ is set, the value @-1@ is
268 * returned; otherwise an error is reported and the program is
272 int conf_enum(scanner *sc, const char *list, unsigned f, const char *err)
279 /* --- Make sure it's a string --- */
281 if (sc->t != CTOK_WORD)
282 error(sc, "parse error, expected %s", err);
284 /* --- Grind through the list --- */
296 } else if (chosen != -1) {
300 else if (f & ENUM_NONE)
303 error(sc, "unknown %s `%s'", err, sc->d.buf);
317 if ((f & ENUM_ABBREV) && !*q) {
319 error(sc, "ambiguous %s `%s'", err, sc->d.buf);
333 /* --- @conf_prefix@ --- *
335 * Arguments: @scanner *sc@ = pointer to a scanner object
336 * @const char *p@ = pointer to prefix string to check
338 * Returns: Nonzero if the prefix matches.
340 * Use: If the current token is a word matching the given prefix
341 * string, then it and an optional `.' character are removed and
342 * a nonzero result is returned. Otherwise the current token is
343 * left as it is, and zero is returned.
345 * Typical options parsing code would remove an expected prefix,
346 * scan an option anyway (since qualifying prefixes are
347 * optional) and if a match is found, claim the option. If no
348 * match is found, and a prefix was stripped, then an error
349 * should be reported.
352 int conf_prefix(scanner *sc, const char *p)
354 if (sc->t == CTOK_WORD && strcmp(p, sc->d.buf) == 0) {
363 /* --- @conf_name@ --- *
365 * Arguments: @scanner *sc@ = pointer to scanner
366 * @char delim@ = delimiter character to look for
367 * @dstr *d@ = pointer to dynamic string for output
371 * Use: Reads in a compound name consisting of words separated by
372 * delimiters. Leading and trailing delimiters are permitted,
373 * although they'll probably cause confusion if used. The name
374 * may be enclosed in square brackets if that helps at all.
376 * Examples of compound names are filenames (delimited by `/')
377 * and IP addresses (delimited by `.').
380 void conf_name(scanner *sc, char delim, dstr *d)
388 /* --- Read an optional opening bracket --- */
395 /* --- Do the main reading sequence --- */
398 if (sc->t == delim) {
403 if (sc->t == CTOK_WORD) {
408 } while (sc->t == delim);
410 /* --- Check that the string was OK --- */
413 error(sc, "parse error, name expected");
415 /* --- Read a closing bracket --- */
421 error(sc, "parse error, missing `]'");
426 /* --- @conf_parse@ --- *
428 * Arguments: @scanner *sc@ = pointer to scanner definition
432 * Use: Parses a configuration file from the scanner.
435 void conf_parse(scanner *sc)
440 if (sc->t == CTOK_EOF)
442 if (sc->t != CTOK_WORD)
443 error(sc, "parse error, keyword expected");
445 /* --- Handle a forwarding request --- */
447 if (strcmp(sc->d.buf, "forward") == 0 ||
448 strcmp(sc->d.buf, "fw") == 0 ||
449 strcmp(sc->d.buf, "from") == 0) {
455 /* --- Read a source description --- */
460 /* --- Try to find a source type which understands --- */
463 for (sops = sources; *sops; sops++) {
464 if ((s = (*sops)->read(sc)) != 0)
467 error(sc, "unknown source name `%s'", sc->d.buf);
469 /* --- Read any source-specific options --- */
474 while (sc->t == CTOK_WORD) {
475 if (!s->ops->option || !s->ops->option(s, sc)) {
476 error(sc, "unknown %s source option `%s'",
477 s->ops->name, sc->d.buf);
483 error(sc, "parse error, missing `}'");
488 /* --- Read a destination description --- */
490 if (sc->t == CTOK_WORD && (strcmp(sc->d.buf, "to") == 0 ||
491 strcmp(sc->d.buf, "->") == 0))
497 /* --- Try to find a target which understands --- */
500 for (tops = targets; *tops; tops++) {
501 if ((t = (*tops)->read(sc)) != 0)
504 error(sc, "unknown target name `%s'", sc->d.buf);
506 /* --- Read any target-specific options --- */
511 while (sc->t == CTOK_WORD) {
512 if (!t->ops->option || !t->ops->option(t, sc)) {
513 error(sc, "unknown %s target option `%s'",
514 t->ops->name, sc->d.buf);
520 error(sc, "parse error, `}' expected");
525 /* --- Combine the source and target --- */
527 s->ops->attach(s, sc, t);
530 /* --- Include configuration from a file --- *
532 * Slightly tricky. Scan the optional semicolon from the including
533 * stream, not the included one.
536 else if (strcmp(sc->d.buf, "include") == 0) {
541 conf_name(sc, '/', &d);
542 if ((fp = fopen(d.buf, "r")) == 0)
543 error(sc, "can't include `%s': %s", d.buf, strerror(errno));
547 scan_push(sc, scan_file(fp, xstrdup(d.buf), SCF_FREENAME));
550 continue; /* Don't parse a trailing `;' */
553 /* --- Other configuration is handled elsewhere --- */
557 /* --- First try among the sources --- */
562 for (sops = sources; *sops; sops++) {
563 if ((*sops)->option && (*sops)->option(0, sc))
568 /* --- Then try among the targets --- */
573 for (tops = targets; *tops; tops++) {
574 if ((*tops)->option && (*tops)->option(0, sc))
579 /* --- Nobody wants the option --- */
581 error(sc, "unknown global option or prefix `%s'", sc->d.buf);
591 /*----- That's all, folks -------------------------------------------------*/