3 * $Id: conf.c,v 1.9 2002/01/13 14:48:16 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.9 2002/01/13 14:48:16 mdw
33 * Make delimiters be a property of a scanner. Change the delimiter-
34 * changing functions' names.
36 * Revision 1.8 2001/02/03 20:33:26 mdw
37 * Fix flags to be unsigned.
39 * Revision 1.7 2000/08/01 17:58:10 mdw
40 * Fix subtleties with <ctype.h> functions.
42 * Revision 1.6 2000/02/12 18:13:20 mdw
43 * Terminate tables of sources and targets.
45 * Revision 1.5 1999/10/22 22:46:44 mdw
46 * Improve documentation for conf_enum.
48 * Revision 1.4 1999/10/15 21:12:36 mdw
49 * Remove redundant debugging code.
51 * Revision 1.3 1999/08/19 18:32:48 mdw
52 * Improve lexical analysis. In particular, `chmod' patterns don't have to
55 * Revision 1.2 1999/07/26 23:28:39 mdw
56 * Major reconstruction work for new design.
58 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
63 /*----- Header files ------------------------------------------------------*/
74 #include <mLib/dstr.h>
75 #include <mLib/quis.h>
76 #include <mLib/report.h>
87 /*----- Source and target tables ------------------------------------------*/
89 static source_ops
*sources
[] =
90 { &xsource_ops
, &fsource_ops
, &ssource_ops
, 0 };
91 static target_ops
*targets
[] =
92 { &xtarget_ops
, &ftarget_ops
, &starget_ops
, 0 };
94 /*----- Main code ---------------------------------------------------------*/
96 /* --- @conf_undelim@ --- *
98 * Arguments: @scanner *sc@ = pointer to scanner definition
99 * @const char *d, *dd@ = pointer to characters to escape
103 * Use: Modifies the tokenizer. Characters in the first list will
104 * always be considered to begin a word. Characters in the
105 * second list will always be allowed to continue a word.
108 void conf_undelim(scanner
*sc
, const char *d
, const char *dd
)
116 * Arguments: @scanner *sc@ = pointer to scanner definition
118 * Returns: Type of token scanned.
120 * Use: Reads the next token from the character scanner.
123 int token(scanner
*sc
)
126 '{': case '}': case '/': case ',': \
127 case '=': case ':': case ';': \
128 case '.': case '[': case ']'
134 /* --- Main tokenization --- */
139 /* --- Deal with pushed-back tokens --- */
142 dstr_puts(&sc
->d
, sc
->head
->tok
);
149 else if (isspace(ch
))
153 /* --- End of file --- */
159 /* --- Comment character --- */
162 do ch
= scan(sc
); while (ch
!= EOF
&& ch
!= '\n');
165 /* --- Various self-delimiting characters --- */
168 if (!sc
->wbegin
|| strchr(sc
->wbegin
, ch
) == 0) {
169 dstr_putc(&sc
->d
, ch
);
175 /* --- Bare words --- *
177 * These aren't as bare any more. You can now backslash-escape
178 * individual characters, and enclose sections in double-quotes.
198 if (q
|| (sc
->wcont
&& strchr(sc
->wcont
, ch
)))
202 if (!q
&& isspace(ch
))
223 /* --- @pushback@ --- *
225 * Arguments: @scanner *sc@ = pointer to scanner definition
229 * Use: Pushes the current token back. This is normally a precursor
230 * to pushing a new scanner source.
233 static void pushback(scanner
*sc
)
235 sc
->head
->tok
= xstrdup(sc
->d
.buf
);
241 * Arguments: @scanner *sc@ = pointer to scanner definition
242 * @const char *msg@ = message skeleton string
243 * @...@ = extra arguments for the skeleton
247 * Use: Reports an error at the current scanner location.
250 void error(scanner
*sc
, const char *msg
, ...)
254 fprintf(stderr
, "%s: %s:%i: ", QUIS
, sc
->head
->src
, sc
->head
->line
);
255 vfprintf(stderr
, msg
, ap
);
260 /* --- @conf_enum@ --- *
262 * Arguments: @scanner *sc@ = pointer to a scanner object
263 * @const char *list@ = comma-separated things to allow
264 * @unsigned f@ = flags for the search
265 * @const char *err@ = error message if not found
267 * Returns: Index into list, zero-based, or @-1@.
269 * Use: Checks whether the current token is a string which matches
270 * one of the comma-separated items given. The return value is
271 * the index (zero-based) of the matched string in the list.
273 * The flags control the behaviour if no exact match is found.
274 * If @ENUM_ABBREV@ is set, and the current token is a left
275 * substring of exactly one of the possibilities, then that one
276 * is chosen. If @ENUM_NONE@ is set, the value @-1@ is
277 * returned; otherwise an error is reported and the program is
281 int conf_enum(scanner
*sc
, const char *list
, unsigned f
, const char *err
)
288 /* --- Make sure it's a string --- */
290 if (sc
->t
!= CTOK_WORD
)
291 error(sc
, "parse error, expected %s", err
);
293 /* --- Grind through the list --- */
305 } else if (chosen
!= -1) {
309 else if (f
& ENUM_NONE
)
312 error(sc
, "unknown %s `%s'", err
, sc
->d
.buf
);
326 if ((f
& ENUM_ABBREV
) && !*q
) {
328 error(sc
, "ambiguous %s `%s'", err
, sc
->d
.buf
);
342 /* --- @conf_prefix@ --- *
344 * Arguments: @scanner *sc@ = pointer to a scanner object
345 * @const char *p@ = pointer to prefix string to check
347 * Returns: Nonzero if the prefix matches.
349 * Use: If the current token is a word matching the given prefix
350 * string, then it and an optional `.' character are removed and
351 * a nonzero result is returned. Otherwise the current token is
352 * left as it is, and zero is returned.
354 * Typical options parsing code would remove an expected prefix,
355 * scan an option anyway (since qualifying prefixes are
356 * optional) and if a match is found, claim the option. If no
357 * match is found, and a prefix was stripped, then an error
358 * should be reported.
361 int conf_prefix(scanner
*sc
, const char *p
)
363 if (sc
->t
== CTOK_WORD
&& strcmp(p
, sc
->d
.buf
) == 0) {
372 /* --- @conf_name@ --- *
374 * Arguments: @scanner *sc@ = pointer to scanner
375 * @char delim@ = delimiter character to look for
376 * @dstr *d@ = pointer to dynamic string for output
380 * Use: Reads in a compound name consisting of words separated by
381 * delimiters. Leading and trailing delimiters are permitted,
382 * although they'll probably cause confusion if used. The name
383 * may be enclosed in square brackets if that helps at all.
385 * Examples of compound names are filenames (delimited by `/')
386 * and IP addresses (delimited by `.').
389 void conf_name(scanner
*sc
, char delim
, dstr
*d
)
396 /* --- Read an optional opening bracket --- */
403 /* --- Do the main reading sequence --- */
406 if (sc
->t
== delim
) {
411 if (sc
->t
== CTOK_WORD
) {
416 } while (sc
->t
== delim
);
418 /* --- Check that the string was OK --- */
421 error(sc
, "parse error, name expected");
423 /* --- Read a closing bracket --- */
429 error(sc
, "parse error, missing `]'");
437 /* --- @conf_parse@ --- *
439 * Arguments: @scanner *sc@ = pointer to scanner definition
443 * Use: Parses a configuration file from the scanner.
446 void conf_parse(scanner
*sc
)
451 if (sc
->t
== CTOK_EOF
)
453 if (sc
->t
!= CTOK_WORD
)
454 error(sc
, "parse error, keyword expected");
456 /* --- Handle a forwarding request --- */
458 if (strcmp(sc
->d
.buf
, "forward") == 0 ||
459 strcmp(sc
->d
.buf
, "fw") == 0 ||
460 strcmp(sc
->d
.buf
, "from") == 0) {
466 /* --- Read a source description --- */
471 /* --- Try to find a source type which understands --- */
474 for (sops
= sources
; *sops
; sops
++) {
475 if ((s
= (*sops
)->read(sc
)) != 0)
478 error(sc
, "unknown source name `%s'", sc
->d
.buf
);
480 /* --- Read any source-specific options --- */
485 while (sc
->t
== CTOK_WORD
) {
486 if (!s
->ops
->option
|| !s
->ops
->option(s
, sc
)) {
487 error(sc
, "unknown %s source option `%s'",
488 s
->ops
->name
, sc
->d
.buf
);
494 error(sc
, "parse error, missing `}'");
499 /* --- Read a destination description --- */
501 if (sc
->t
== CTOK_WORD
&& (strcmp(sc
->d
.buf
, "to") == 0 ||
502 strcmp(sc
->d
.buf
, "->") == 0))
508 /* --- Try to find a target which understands --- */
511 for (tops
= targets
; *tops
; tops
++) {
512 if ((t
= (*tops
)->read(sc
)) != 0)
515 error(sc
, "unknown target name `%s'", sc
->d
.buf
);
517 /* --- Read any target-specific options --- */
522 while (sc
->t
== CTOK_WORD
) {
523 if (!t
->ops
->option
|| !t
->ops
->option(t
, sc
)) {
524 error(sc
, "unknown %s target option `%s'",
525 t
->ops
->name
, sc
->d
.buf
);
531 error(sc
, "parse error, `}' expected");
536 /* --- Combine the source and target --- */
538 s
->ops
->attach(s
, sc
, t
);
541 /* --- Include configuration from a file --- *
543 * Slightly tricky. Scan the optional semicolon from the including
544 * stream, not the included one.
547 else if (strcmp(sc
->d
.buf
, "include") == 0) {
552 conf_name(sc
, '/', &d
);
553 if ((fp
= fopen(d
.buf
, "r")) == 0)
554 error(sc
, "can't include `%s': %s", d
.buf
, strerror(errno
));
558 scan_push(sc
, scan_file(fp
, xstrdup(d
.buf
), SCF_FREENAME
));
561 continue; /* Don't parse a trailing `;' */
564 /* --- Other configuration is handled elsewhere --- */
568 /* --- First try among the sources --- */
573 for (sops
= sources
; *sops
; sops
++) {
574 if ((*sops
)->option
&& (*sops
)->option(0, sc
))
579 /* --- Then try among the targets --- */
584 for (tops
= targets
; *tops
; tops
++) {
585 if ((*tops
)->option
&& (*tops
)->option(0, sc
))
590 /* --- Nobody wants the option --- */
592 error(sc
, "unknown global option or prefix `%s'", sc
->d
.buf
);
602 /*----- That's all, folks -------------------------------------------------*/