3 * $Id: conf.c,v 1.3 1999/08/19 18:32:48 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.3 1999/08/19 18:32:48 mdw
33 * Improve lexical analysis. In particular, `chmod' patterns don't have to
36 * Revision 1.2 1999/07/26 23:28:39 mdw
37 * Major reconstruction work for new design.
39 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
44 /*----- Header files ------------------------------------------------------*/
55 #include <mLib/dstr.h>
56 #include <mLib/quis.h>
57 #include <mLib/report.h>
68 /*----- Source and target tables ------------------------------------------*/
70 static source_ops *sources[] = { &xsource_ops, &fsource_ops, &ssource_ops };
71 static target_ops *targets[] = { &xtarget_ops, &ftarget_ops, &starget_ops };
73 static const char *notword = 0;
74 static const char *notdelim = 0;
76 /*----- Main code ---------------------------------------------------------*/
78 /* --- @undelim@ --- *
80 * Arguments: @const char *d, dd@ = pointer to characters to escape
84 * Use: Modifies the tokenizer. Characters in the first list will
85 * always be considered to begin a word. Characters in the
86 * second list will always be allowed to continue a word.
89 void undelim(const char *d, const char *dd) { notword = d; notdelim = dd; }
93 * Arguments: @scanner *sc@ = pointer to scanner definition
95 * Returns: Type of token scanned.
97 * Use: Reads the next token from the character scanner.
100 int token(scanner *sc)
103 '{': case '}': case '/': case ',': \
104 case '=': case ':': case ';': \
105 case '.': case '[': case ']'
111 /* --- Main tokenization --- */
116 /* --- Deal with pushed-back tokens --- */
119 dstr_puts(&sc->d, sc->head->tok);
126 else if (isspace((unsigned char)ch))
130 /* --- End of file --- */
136 /* --- Comment character --- */
139 do ch = scan(sc); while (ch != EOF && ch != '\n');
142 /* --- Various self-delimiting characters --- */
145 if (!notword || strchr(notword, ch) == 0) {
146 dstr_putc(&sc->d, ch);
152 /* --- Bare words --- *
154 * These aren't as bare any more. You can now backslash-escape
155 * individual characters, and enclose sections in double-quotes.
175 if (q || (notdelim && strchr(notdelim, ch)))
179 if (!q && isspace((unsigned char)(ch)))
197 /* printf("token `%s'\n", sc->d.buf); */
201 /* --- @pushback@ --- *
203 * Arguments: @scanner *sc@ = pointer to scanner definition
207 * Use: Pushes the current token back. This is normally a precursor
208 * to pushing a new scanner source.
211 static void pushback(scanner *sc)
213 sc->head->tok = xstrdup(sc->d.buf);
219 * Arguments: @scanner *sc@ = pointer to scanner definition
220 * @const char *msg@ = message skeleton string
221 * @...@ = extra arguments for the skeleton
225 * Use: Reports an error at the current scanner location.
228 void error(scanner *sc, const char *msg, ...)
232 fprintf(stderr, "%s: %s:%i: ", QUIS, sc->head->src, sc->head->line);
233 vfprintf(stderr, msg, ap);
238 /* --- @conf_enum@ --- *
240 * Arguments: @scanner *sc@ = pointer to a scanner object
241 * @const char *list@ = comma-separated things to allow
242 * @unsigned f@ = flags for the search
243 * @const char *err@ = error message if not found
245 * Returns: Index into list, zero-based, or @-1@.
247 * Use: Checks whether the current token is a string which matches
248 * one of the comma-separated items given. If not, an error is
249 * reported; otherwise the index of the matched item is
253 int conf_enum(scanner *sc, const char *list, unsigned f, const char *err)
260 /* --- Make sure it's a string --- */
262 if (sc->t != CTOK_WORD)
263 error(sc, "parse error, expected %s", err);
265 /* --- Grind through the list --- */
277 } else if (chosen != -1) {
281 else if (f & ENUM_NONE)
284 error(sc, "unknown %s `%s'", err, sc->d.buf);
298 if ((f & ENUM_ABBREV) && !*q) {
300 error(sc, "ambiguous %s `%s'", err, sc->d.buf);
314 /* --- @conf_prefix@ --- *
316 * Arguments: @scanner *sc@ = pointer to a scanner object
317 * @const char *p@ = pointer to prefix string to check
319 * Returns: Nonzero if the prefix matches.
321 * Use: If the current token is a word matching the given prefix
322 * string, then it and an optional `.' character are removed and
323 * a nonzero result is returned. Otherwise the current token is
324 * left as it is, and zero is returned.
326 * Typical options parsing code would remove an expected prefix,
327 * scan an option anyway (since qualifying prefixes are
328 * optional) and if a match is found, claim the option. If no
329 * match is found, and a prefix was stripped, then an error
330 * should be reported.
333 int conf_prefix(scanner *sc, const char *p)
335 if (sc->t == CTOK_WORD && strcmp(p, sc->d.buf) == 0) {
344 /* --- @conf_name@ --- *
346 * Arguments: @scanner *sc@ = pointer to scanner
347 * @char delim@ = delimiter character to look for
348 * @dstr *d@ = pointer to dynamic string for output
352 * Use: Reads in a compound name consisting of words separated by
353 * delimiters. Leading and trailing delimiters are permitted,
354 * although they'll probably cause confusion if used. The name
355 * may be enclosed in square brackets if that helps at all.
357 * Examples of compound names are filenames (delimited by `/')
358 * and IP addresses (delimited by `.').
361 void conf_name(scanner *sc, char delim, dstr *d)
369 /* --- Read an optional opening bracket --- */
376 /* --- Do the main reading sequence --- */
379 if (sc->t == delim) {
384 if (sc->t == CTOK_WORD) {
389 } while (sc->t == delim);
391 /* --- Check that the string was OK --- */
394 error(sc, "parse error, name expected");
396 /* --- Read a closing bracket --- */
402 error(sc, "parse error, missing `]'");
407 /* --- @conf_parse@ --- *
409 * Arguments: @scanner *sc@ = pointer to scanner definition
413 * Use: Parses a configuration file from the scanner.
416 void conf_parse(scanner *sc)
421 if (sc->t == CTOK_EOF)
423 if (sc->t != CTOK_WORD)
424 error(sc, "parse error, keyword expected");
426 /* --- Handle a forwarding request --- */
428 if (strcmp(sc->d.buf, "forward") == 0 ||
429 strcmp(sc->d.buf, "fw") == 0 ||
430 strcmp(sc->d.buf, "from") == 0) {
436 /* --- Read a source description --- */
441 /* --- Try to find a source type which understands --- */
444 for (sops = sources; *sops; sops++) {
445 if ((s = (*sops)->read(sc)) != 0)
448 error(sc, "unknown source name `%s'", sc->d.buf);
450 /* --- Read any source-specific options --- */
455 while (sc->t == CTOK_WORD) {
456 if (!s->ops->option || !s->ops->option(s, sc)) {
457 error(sc, "unknown %s source option `%s'",
458 s->ops->name, sc->d.buf);
464 error(sc, "parse error, missing `}'");
469 /* --- Read a destination description --- */
471 if (sc->t == CTOK_WORD && (strcmp(sc->d.buf, "to") == 0 ||
472 strcmp(sc->d.buf, "->") == 0))
478 /* --- Try to find a target which understands --- */
481 for (tops = targets; *tops; tops++) {
482 if ((t = (*tops)->read(sc)) != 0)
485 error(sc, "unknown target name `%s'", sc->d.buf);
487 /* --- Read any target-specific options --- */
492 while (sc->t == CTOK_WORD) {
493 if (!t->ops->option || !t->ops->option(t, sc)) {
494 error(sc, "unknown %s target option `%s'",
495 t->ops->name, sc->d.buf);
501 error(sc, "parse error, `}' expected");
506 /* --- Combine the source and target --- */
508 s->ops->attach(s, sc, t);
511 /* --- Include configuration from a file --- *
513 * Slightly tricky. Scan the optional semicolon from the including
514 * stream, not the included one.
517 else if (strcmp(sc->d.buf, "include") == 0) {
522 conf_name(sc, '/', &d);
523 if ((fp = fopen(d.buf, "r")) == 0)
524 error(sc, "can't include `%s': %s", d.buf, strerror(errno));
528 scan_push(sc, scan_file(fp, xstrdup(d.buf), SCF_FREENAME));
531 continue; /* Don't parse a trailing `;' */
534 /* --- Other configuration is handled elsewhere --- */
538 /* --- First try among the sources --- */
543 for (sops = sources; *sops; sops++) {
544 if ((*sops)->option && (*sops)->option(0, sc))
549 /* --- Then try among the targets --- */
554 for (tops = targets; *tops; tops++) {
555 if ((*tops)->option && (*tops)->option(0, sc))
560 /* --- Nobody wants the option --- */
562 error(sc, "unknown global option or prefix `%s'", sc->d.buf);
572 /*----- That's all, folks -------------------------------------------------*/