3 * $Id: conf.c,v 1.8 2001/02/03 20:33:26 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.8 2001/02/03 20:33:26 mdw
33 * Fix flags to be unsigned.
35 * Revision 1.7 2000/08/01 17:58:10 mdw
36 * Fix subtleties with <ctype.h> functions.
38 * Revision 1.6 2000/02/12 18:13:20 mdw
39 * Terminate tables of sources and targets.
41 * Revision 1.5 1999/10/22 22:46:44 mdw
42 * Improve documentation for conf_enum.
44 * Revision 1.4 1999/10/15 21:12:36 mdw
45 * Remove redundant debugging code.
47 * Revision 1.3 1999/08/19 18:32:48 mdw
48 * Improve lexical analysis. In particular, `chmod' patterns don't have to
51 * Revision 1.2 1999/07/26 23:28:39 mdw
52 * Major reconstruction work for new design.
54 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
59 /*----- Header files ------------------------------------------------------*/
70 #include <mLib/dstr.h>
71 #include <mLib/quis.h>
72 #include <mLib/report.h>
83 /*----- Source and target tables ------------------------------------------*/
85 static source_ops
*sources
[] =
86 { &xsource_ops
, &fsource_ops
, &ssource_ops
, 0 };
87 static target_ops
*targets
[] =
88 { &xtarget_ops
, &ftarget_ops
, &starget_ops
, 0 };
90 static const char *notword
= 0;
91 static const char *notdelim
= 0;
93 /*----- Main code ---------------------------------------------------------*/
95 /* --- @undelim@ --- *
97 * Arguments: @const char *d, dd@ = pointer to characters to escape
101 * Use: Modifies the tokenizer. Characters in the first list will
102 * always be considered to begin a word. Characters in the
103 * second list will always be allowed to continue a word.
106 void undelim(const char *d
, const char *dd
) { notword
= d
; notdelim
= dd
; }
110 * Arguments: @scanner *sc@ = pointer to scanner definition
112 * Returns: Type of token scanned.
114 * Use: Reads the next token from the character scanner.
117 int token(scanner
*sc
)
120 '{': case '}': case '/': case ',': \
121 case '=': case ':': case ';': \
122 case '.': case '[': case ']'
128 /* --- Main tokenization --- */
133 /* --- Deal with pushed-back tokens --- */
136 dstr_puts(&sc
->d
, sc
->head
->tok
);
143 else if (isspace(ch
))
147 /* --- End of file --- */
153 /* --- Comment character --- */
156 do ch
= scan(sc
); while (ch
!= EOF
&& ch
!= '\n');
159 /* --- Various self-delimiting characters --- */
162 if (!notword
|| strchr(notword
, ch
) == 0) {
163 dstr_putc(&sc
->d
, ch
);
169 /* --- Bare words --- *
171 * These aren't as bare any more. You can now backslash-escape
172 * individual characters, and enclose sections in double-quotes.
192 if (q
|| (notdelim
&& strchr(notdelim
, ch
)))
196 if (!q
&& isspace(ch
))
217 /* --- @pushback@ --- *
219 * Arguments: @scanner *sc@ = pointer to scanner definition
223 * Use: Pushes the current token back. This is normally a precursor
224 * to pushing a new scanner source.
227 static void pushback(scanner
*sc
)
229 sc
->head
->tok
= xstrdup(sc
->d
.buf
);
235 * Arguments: @scanner *sc@ = pointer to scanner definition
236 * @const char *msg@ = message skeleton string
237 * @...@ = extra arguments for the skeleton
241 * Use: Reports an error at the current scanner location.
244 void error(scanner
*sc
, const char *msg
, ...)
248 fprintf(stderr
, "%s: %s:%i: ", QUIS
, sc
->head
->src
, sc
->head
->line
);
249 vfprintf(stderr
, msg
, ap
);
254 /* --- @conf_enum@ --- *
256 * Arguments: @scanner *sc@ = pointer to a scanner object
257 * @const char *list@ = comma-separated things to allow
258 * @unsigned f@ = flags for the search
259 * @const char *err@ = error message if not found
261 * Returns: Index into list, zero-based, or @-1@.
263 * Use: Checks whether the current token is a string which matches
264 * one of the comma-separated items given. The return value is
265 * the index (zero-based) of the matched string in the list.
267 * The flags control the behaviour if no exact match is found.
268 * If @ENUM_ABBREV@ is set, and the current token is a left
269 * substring of exactly one of the possibilities, then that one
270 * is chosen. If @ENUM_NONE@ is set, the value @-1@ is
271 * returned; otherwise an error is reported and the program is
275 int conf_enum(scanner
*sc
, const char *list
, unsigned f
, const char *err
)
282 /* --- Make sure it's a string --- */
284 if (sc
->t
!= CTOK_WORD
)
285 error(sc
, "parse error, expected %s", err
);
287 /* --- Grind through the list --- */
299 } else if (chosen
!= -1) {
303 else if (f
& ENUM_NONE
)
306 error(sc
, "unknown %s `%s'", err
, sc
->d
.buf
);
320 if ((f
& ENUM_ABBREV
) && !*q
) {
322 error(sc
, "ambiguous %s `%s'", err
, sc
->d
.buf
);
336 /* --- @conf_prefix@ --- *
338 * Arguments: @scanner *sc@ = pointer to a scanner object
339 * @const char *p@ = pointer to prefix string to check
341 * Returns: Nonzero if the prefix matches.
343 * Use: If the current token is a word matching the given prefix
344 * string, then it and an optional `.' character are removed and
345 * a nonzero result is returned. Otherwise the current token is
346 * left as it is, and zero is returned.
348 * Typical options parsing code would remove an expected prefix,
349 * scan an option anyway (since qualifying prefixes are
350 * optional) and if a match is found, claim the option. If no
351 * match is found, and a prefix was stripped, then an error
352 * should be reported.
355 int conf_prefix(scanner
*sc
, const char *p
)
357 if (sc
->t
== CTOK_WORD
&& strcmp(p
, sc
->d
.buf
) == 0) {
366 /* --- @conf_name@ --- *
368 * Arguments: @scanner *sc@ = pointer to scanner
369 * @char delim@ = delimiter character to look for
370 * @dstr *d@ = pointer to dynamic string for output
374 * Use: Reads in a compound name consisting of words separated by
375 * delimiters. Leading and trailing delimiters are permitted,
376 * although they'll probably cause confusion if used. The name
377 * may be enclosed in square brackets if that helps at all.
379 * Examples of compound names are filenames (delimited by `/')
380 * and IP addresses (delimited by `.').
383 void conf_name(scanner
*sc
, char delim
, dstr
*d
)
390 /* --- Read an optional opening bracket --- */
397 /* --- Do the main reading sequence --- */
400 if (sc
->t
== delim
) {
405 if (sc
->t
== CTOK_WORD
) {
410 } while (sc
->t
== delim
);
412 /* --- Check that the string was OK --- */
415 error(sc
, "parse error, name expected");
417 /* --- Read a closing bracket --- */
423 error(sc
, "parse error, missing `]'");
431 /* --- @conf_parse@ --- *
433 * Arguments: @scanner *sc@ = pointer to scanner definition
437 * Use: Parses a configuration file from the scanner.
440 void conf_parse(scanner
*sc
)
445 if (sc
->t
== CTOK_EOF
)
447 if (sc
->t
!= CTOK_WORD
)
448 error(sc
, "parse error, keyword expected");
450 /* --- Handle a forwarding request --- */
452 if (strcmp(sc
->d
.buf
, "forward") == 0 ||
453 strcmp(sc
->d
.buf
, "fw") == 0 ||
454 strcmp(sc
->d
.buf
, "from") == 0) {
460 /* --- Read a source description --- */
465 /* --- Try to find a source type which understands --- */
468 for (sops
= sources
; *sops
; sops
++) {
469 if ((s
= (*sops
)->read(sc
)) != 0)
472 error(sc
, "unknown source name `%s'", sc
->d
.buf
);
474 /* --- Read any source-specific options --- */
479 while (sc
->t
== CTOK_WORD
) {
480 if (!s
->ops
->option
|| !s
->ops
->option(s
, sc
)) {
481 error(sc
, "unknown %s source option `%s'",
482 s
->ops
->name
, sc
->d
.buf
);
488 error(sc
, "parse error, missing `}'");
493 /* --- Read a destination description --- */
495 if (sc
->t
== CTOK_WORD
&& (strcmp(sc
->d
.buf
, "to") == 0 ||
496 strcmp(sc
->d
.buf
, "->") == 0))
502 /* --- Try to find a target which understands --- */
505 for (tops
= targets
; *tops
; tops
++) {
506 if ((t
= (*tops
)->read(sc
)) != 0)
509 error(sc
, "unknown target name `%s'", sc
->d
.buf
);
511 /* --- Read any target-specific options --- */
516 while (sc
->t
== CTOK_WORD
) {
517 if (!t
->ops
->option
|| !t
->ops
->option(t
, sc
)) {
518 error(sc
, "unknown %s target option `%s'",
519 t
->ops
->name
, sc
->d
.buf
);
525 error(sc
, "parse error, `}' expected");
530 /* --- Combine the source and target --- */
532 s
->ops
->attach(s
, sc
, t
);
535 /* --- Include configuration from a file --- *
537 * Slightly tricky. Scan the optional semicolon from the including
538 * stream, not the included one.
541 else if (strcmp(sc
->d
.buf
, "include") == 0) {
546 conf_name(sc
, '/', &d
);
547 if ((fp
= fopen(d
.buf
, "r")) == 0)
548 error(sc
, "can't include `%s': %s", d
.buf
, strerror(errno
));
552 scan_push(sc
, scan_file(fp
, xstrdup(d
.buf
), SCF_FREENAME
));
555 continue; /* Don't parse a trailing `;' */
558 /* --- Other configuration is handled elsewhere --- */
562 /* --- First try among the sources --- */
567 for (sops
= sources
; *sops
; sops
++) {
568 if ((*sops
)->option
&& (*sops
)->option(0, sc
))
573 /* --- Then try among the targets --- */
578 for (tops
= targets
; *tops
; tops
++) {
579 if ((*tops
)->option
&& (*tops
)->option(0, sc
))
584 /* --- Nobody wants the option --- */
586 error(sc
, "unknown global option or prefix `%s'", sc
->d
.buf
);
596 /*----- That's all, folks -------------------------------------------------*/