3 * $Id: conf.c,v 1.5 1999/10/22 22:46:44 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.5 1999/10/22 22:46:44 mdw
33 * Improve documentation for conf_enum.
35 * Revision 1.4 1999/10/15 21:12:36 mdw
36 * Remove redundant debugging code.
38 * Revision 1.3 1999/08/19 18:32:48 mdw
39 * Improve lexical analysis. In particular, `chmod' patterns don't have to
42 * Revision 1.2 1999/07/26 23:28:39 mdw
43 * Major reconstruction work for new design.
45 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
50 /*----- Header files ------------------------------------------------------*/
61 #include <mLib/dstr.h>
62 #include <mLib/quis.h>
63 #include <mLib/report.h>
74 /*----- Source and target tables ------------------------------------------*/
76 static source_ops
*sources
[] = { &xsource_ops
, &fsource_ops
, &ssource_ops
};
77 static target_ops
*targets
[] = { &xtarget_ops
, &ftarget_ops
, &starget_ops
};
79 static const char *notword
= 0;
80 static const char *notdelim
= 0;
82 /*----- Main code ---------------------------------------------------------*/
84 /* --- @undelim@ --- *
86 * Arguments: @const char *d, dd@ = pointer to characters to escape
90 * Use: Modifies the tokenizer. Characters in the first list will
91 * always be considered to begin a word. Characters in the
92 * second list will always be allowed to continue a word.
95 void undelim(const char *d
, const char *dd
) { notword
= d
; notdelim
= dd
; }
99 * Arguments: @scanner *sc@ = pointer to scanner definition
101 * Returns: Type of token scanned.
103 * Use: Reads the next token from the character scanner.
106 int token(scanner
*sc
)
109 '{': case '}': case '/': case ',': \
110 case '=': case ':': case ';': \
111 case '.': case '[': case ']'
117 /* --- Main tokenization --- */
122 /* --- Deal with pushed-back tokens --- */
125 dstr_puts(&sc
->d
, sc
->head
->tok
);
132 else if (isspace((unsigned char)ch
))
136 /* --- End of file --- */
142 /* --- Comment character --- */
145 do ch
= scan(sc
); while (ch
!= EOF
&& ch
!= '\n');
148 /* --- Various self-delimiting characters --- */
151 if (!notword
|| strchr(notword
, ch
) == 0) {
152 dstr_putc(&sc
->d
, ch
);
158 /* --- Bare words --- *
160 * These aren't as bare any more. You can now backslash-escape
161 * individual characters, and enclose sections in double-quotes.
181 if (q
|| (notdelim
&& strchr(notdelim
, ch
)))
185 if (!q
&& isspace((unsigned char)(ch
)))
206 /* --- @pushback@ --- *
208 * Arguments: @scanner *sc@ = pointer to scanner definition
212 * Use: Pushes the current token back. This is normally a precursor
213 * to pushing a new scanner source.
216 static void pushback(scanner
*sc
)
218 sc
->head
->tok
= xstrdup(sc
->d
.buf
);
224 * Arguments: @scanner *sc@ = pointer to scanner definition
225 * @const char *msg@ = message skeleton string
226 * @...@ = extra arguments for the skeleton
230 * Use: Reports an error at the current scanner location.
233 void error(scanner
*sc
, const char *msg
, ...)
237 fprintf(stderr
, "%s: %s:%i: ", QUIS
, sc
->head
->src
, sc
->head
->line
);
238 vfprintf(stderr
, msg
, ap
);
243 /* --- @conf_enum@ --- *
245 * Arguments: @scanner *sc@ = pointer to a scanner object
246 * @const char *list@ = comma-separated things to allow
247 * @unsigned f@ = flags for the search
248 * @const char *err@ = error message if not found
250 * Returns: Index into list, zero-based, or @-1@.
252 * Use: Checks whether the current token is a string which matches
253 * one of the comma-separated items given. The return value is
254 * the index (zero-based) of the matched string in the list.
256 * The flags control the behaviour if no exact match is found.
257 * If @ENUM_ABBREV@ is set, and the current token is a left
258 * substring of exactly one of the possibilities, then that one
259 * is chosen. If @ENUM_NONE@ is set, the value @-1@ is
260 * returned; otherwise an error is reported and the program is
264 int conf_enum(scanner
*sc
, const char *list
, unsigned f
, const char *err
)
271 /* --- Make sure it's a string --- */
273 if (sc
->t
!= CTOK_WORD
)
274 error(sc
, "parse error, expected %s", err
);
276 /* --- Grind through the list --- */
288 } else if (chosen
!= -1) {
292 else if (f
& ENUM_NONE
)
295 error(sc
, "unknown %s `%s'", err
, sc
->d
.buf
);
309 if ((f
& ENUM_ABBREV
) && !*q
) {
311 error(sc
, "ambiguous %s `%s'", err
, sc
->d
.buf
);
325 /* --- @conf_prefix@ --- *
327 * Arguments: @scanner *sc@ = pointer to a scanner object
328 * @const char *p@ = pointer to prefix string to check
330 * Returns: Nonzero if the prefix matches.
332 * Use: If the current token is a word matching the given prefix
333 * string, then it and an optional `.' character are removed and
334 * a nonzero result is returned. Otherwise the current token is
335 * left as it is, and zero is returned.
337 * Typical options parsing code would remove an expected prefix,
338 * scan an option anyway (since qualifying prefixes are
339 * optional) and if a match is found, claim the option. If no
340 * match is found, and a prefix was stripped, then an error
341 * should be reported.
344 int conf_prefix(scanner
*sc
, const char *p
)
346 if (sc
->t
== CTOK_WORD
&& strcmp(p
, sc
->d
.buf
) == 0) {
355 /* --- @conf_name@ --- *
357 * Arguments: @scanner *sc@ = pointer to scanner
358 * @char delim@ = delimiter character to look for
359 * @dstr *d@ = pointer to dynamic string for output
363 * Use: Reads in a compound name consisting of words separated by
364 * delimiters. Leading and trailing delimiters are permitted,
365 * although they'll probably cause confusion if used. The name
366 * may be enclosed in square brackets if that helps at all.
368 * Examples of compound names are filenames (delimited by `/')
369 * and IP addresses (delimited by `.').
372 void conf_name(scanner
*sc
, char delim
, dstr
*d
)
380 /* --- Read an optional opening bracket --- */
387 /* --- Do the main reading sequence --- */
390 if (sc
->t
== delim
) {
395 if (sc
->t
== CTOK_WORD
) {
400 } while (sc
->t
== delim
);
402 /* --- Check that the string was OK --- */
405 error(sc
, "parse error, name expected");
407 /* --- Read a closing bracket --- */
413 error(sc
, "parse error, missing `]'");
418 /* --- @conf_parse@ --- *
420 * Arguments: @scanner *sc@ = pointer to scanner definition
424 * Use: Parses a configuration file from the scanner.
427 void conf_parse(scanner
*sc
)
432 if (sc
->t
== CTOK_EOF
)
434 if (sc
->t
!= CTOK_WORD
)
435 error(sc
, "parse error, keyword expected");
437 /* --- Handle a forwarding request --- */
439 if (strcmp(sc
->d
.buf
, "forward") == 0 ||
440 strcmp(sc
->d
.buf
, "fw") == 0 ||
441 strcmp(sc
->d
.buf
, "from") == 0) {
447 /* --- Read a source description --- */
452 /* --- Try to find a source type which understands --- */
455 for (sops
= sources
; *sops
; sops
++) {
456 if ((s
= (*sops
)->read(sc
)) != 0)
459 error(sc
, "unknown source name `%s'", sc
->d
.buf
);
461 /* --- Read any source-specific options --- */
466 while (sc
->t
== CTOK_WORD
) {
467 if (!s
->ops
->option
|| !s
->ops
->option(s
, sc
)) {
468 error(sc
, "unknown %s source option `%s'",
469 s
->ops
->name
, sc
->d
.buf
);
475 error(sc
, "parse error, missing `}'");
480 /* --- Read a destination description --- */
482 if (sc
->t
== CTOK_WORD
&& (strcmp(sc
->d
.buf
, "to") == 0 ||
483 strcmp(sc
->d
.buf
, "->") == 0))
489 /* --- Try to find a target which understands --- */
492 for (tops
= targets
; *tops
; tops
++) {
493 if ((t
= (*tops
)->read(sc
)) != 0)
496 error(sc
, "unknown target name `%s'", sc
->d
.buf
);
498 /* --- Read any target-specific options --- */
503 while (sc
->t
== CTOK_WORD
) {
504 if (!t
->ops
->option
|| !t
->ops
->option(t
, sc
)) {
505 error(sc
, "unknown %s target option `%s'",
506 t
->ops
->name
, sc
->d
.buf
);
512 error(sc
, "parse error, `}' expected");
517 /* --- Combine the source and target --- */
519 s
->ops
->attach(s
, sc
, t
);
522 /* --- Include configuration from a file --- *
524 * Slightly tricky. Scan the optional semicolon from the including
525 * stream, not the included one.
528 else if (strcmp(sc
->d
.buf
, "include") == 0) {
533 conf_name(sc
, '/', &d
);
534 if ((fp
= fopen(d
.buf
, "r")) == 0)
535 error(sc
, "can't include `%s': %s", d
.buf
, strerror(errno
));
539 scan_push(sc
, scan_file(fp
, xstrdup(d
.buf
), SCF_FREENAME
));
542 continue; /* Don't parse a trailing `;' */
545 /* --- Other configuration is handled elsewhere --- */
549 /* --- First try among the sources --- */
554 for (sops
= sources
; *sops
; sops
++) {
555 if ((*sops
)->option
&& (*sops
)->option(0, sc
))
560 /* --- Then try among the targets --- */
565 for (tops
= targets
; *tops
; tops
++) {
566 if ((*tops
)->option
&& (*tops
)->option(0, sc
))
571 /* --- Nobody wants the option --- */
573 error(sc
, "unknown global option or prefix `%s'", sc
->d
.buf
);
583 /*----- That's all, folks -------------------------------------------------*/