3 * $Id: conf.c,v 1.6 2000/02/12 18:13:20 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.6 2000/02/12 18:13:20 mdw
33 * Terminate tables of sources and targets.
35 * Revision 1.5 1999/10/22 22:46:44 mdw
36 * Improve documentation for conf_enum.
38 * Revision 1.4 1999/10/15 21:12:36 mdw
39 * Remove redundant debugging code.
41 * Revision 1.3 1999/08/19 18:32:48 mdw
42 * Improve lexical analysis. In particular, `chmod' patterns don't have to
45 * Revision 1.2 1999/07/26 23:28:39 mdw
46 * Major reconstruction work for new design.
48 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
53 /*----- Header files ------------------------------------------------------*/
64 #include <mLib/dstr.h>
65 #include <mLib/quis.h>
66 #include <mLib/report.h>
77 /*----- Source and target tables ------------------------------------------*/
79 static source_ops
*sources
[] =
80 { &xsource_ops
, &fsource_ops
, &ssource_ops
, 0 };
81 static target_ops
*targets
[] =
82 { &xtarget_ops
, &ftarget_ops
, &starget_ops
, 0 };
84 static const char *notword
= 0;
85 static const char *notdelim
= 0;
87 /*----- Main code ---------------------------------------------------------*/
89 /* --- @undelim@ --- *
91 * Arguments: @const char *d, dd@ = pointer to characters to escape
95 * Use: Modifies the tokenizer. Characters in the first list will
96 * always be considered to begin a word. Characters in the
97 * second list will always be allowed to continue a word.
100 void undelim(const char *d
, const char *dd
) { notword
= d
; notdelim
= dd
; }
104 * Arguments: @scanner *sc@ = pointer to scanner definition
106 * Returns: Type of token scanned.
108 * Use: Reads the next token from the character scanner.
111 int token(scanner
*sc
)
114 '{': case '}': case '/': case ',': \
115 case '=': case ':': case ';': \
116 case '.': case '[': case ']'
122 /* --- Main tokenization --- */
127 /* --- Deal with pushed-back tokens --- */
130 dstr_puts(&sc
->d
, sc
->head
->tok
);
137 else if (isspace((unsigned char)ch
))
141 /* --- End of file --- */
147 /* --- Comment character --- */
150 do ch
= scan(sc
); while (ch
!= EOF
&& ch
!= '\n');
153 /* --- Various self-delimiting characters --- */
156 if (!notword
|| strchr(notword
, ch
) == 0) {
157 dstr_putc(&sc
->d
, ch
);
163 /* --- Bare words --- *
165 * These aren't as bare any more. You can now backslash-escape
166 * individual characters, and enclose sections in double-quotes.
186 if (q
|| (notdelim
&& strchr(notdelim
, ch
)))
190 if (!q
&& isspace((unsigned char)(ch
)))
211 /* --- @pushback@ --- *
213 * Arguments: @scanner *sc@ = pointer to scanner definition
217 * Use: Pushes the current token back. This is normally a precursor
218 * to pushing a new scanner source.
221 static void pushback(scanner
*sc
)
223 sc
->head
->tok
= xstrdup(sc
->d
.buf
);
229 * Arguments: @scanner *sc@ = pointer to scanner definition
230 * @const char *msg@ = message skeleton string
231 * @...@ = extra arguments for the skeleton
235 * Use: Reports an error at the current scanner location.
238 void error(scanner
*sc
, const char *msg
, ...)
242 fprintf(stderr
, "%s: %s:%i: ", QUIS
, sc
->head
->src
, sc
->head
->line
);
243 vfprintf(stderr
, msg
, ap
);
248 /* --- @conf_enum@ --- *
250 * Arguments: @scanner *sc@ = pointer to a scanner object
251 * @const char *list@ = comma-separated things to allow
252 * @unsigned f@ = flags for the search
253 * @const char *err@ = error message if not found
255 * Returns: Index into list, zero-based, or @-1@.
257 * Use: Checks whether the current token is a string which matches
258 * one of the comma-separated items given. The return value is
259 * the index (zero-based) of the matched string in the list.
261 * The flags control the behaviour if no exact match is found.
262 * If @ENUM_ABBREV@ is set, and the current token is a left
263 * substring of exactly one of the possibilities, then that one
264 * is chosen. If @ENUM_NONE@ is set, the value @-1@ is
265 * returned; otherwise an error is reported and the program is
269 int conf_enum(scanner
*sc
, const char *list
, unsigned f
, const char *err
)
276 /* --- Make sure it's a string --- */
278 if (sc
->t
!= CTOK_WORD
)
279 error(sc
, "parse error, expected %s", err
);
281 /* --- Grind through the list --- */
293 } else if (chosen
!= -1) {
297 else if (f
& ENUM_NONE
)
300 error(sc
, "unknown %s `%s'", err
, sc
->d
.buf
);
314 if ((f
& ENUM_ABBREV
) && !*q
) {
316 error(sc
, "ambiguous %s `%s'", err
, sc
->d
.buf
);
330 /* --- @conf_prefix@ --- *
332 * Arguments: @scanner *sc@ = pointer to a scanner object
333 * @const char *p@ = pointer to prefix string to check
335 * Returns: Nonzero if the prefix matches.
337 * Use: If the current token is a word matching the given prefix
338 * string, then it and an optional `.' character are removed and
339 * a nonzero result is returned. Otherwise the current token is
340 * left as it is, and zero is returned.
342 * Typical options parsing code would remove an expected prefix,
343 * scan an option anyway (since qualifying prefixes are
344 * optional) and if a match is found, claim the option. If no
345 * match is found, and a prefix was stripped, then an error
346 * should be reported.
349 int conf_prefix(scanner
*sc
, const char *p
)
351 if (sc
->t
== CTOK_WORD
&& strcmp(p
, sc
->d
.buf
) == 0) {
360 /* --- @conf_name@ --- *
362 * Arguments: @scanner *sc@ = pointer to scanner
363 * @char delim@ = delimiter character to look for
364 * @dstr *d@ = pointer to dynamic string for output
368 * Use: Reads in a compound name consisting of words separated by
369 * delimiters. Leading and trailing delimiters are permitted,
370 * although they'll probably cause confusion if used. The name
371 * may be enclosed in square brackets if that helps at all.
373 * Examples of compound names are filenames (delimited by `/')
374 * and IP addresses (delimited by `.').
377 void conf_name(scanner
*sc
, char delim
, dstr
*d
)
385 /* --- Read an optional opening bracket --- */
392 /* --- Do the main reading sequence --- */
395 if (sc
->t
== delim
) {
400 if (sc
->t
== CTOK_WORD
) {
405 } while (sc
->t
== delim
);
407 /* --- Check that the string was OK --- */
410 error(sc
, "parse error, name expected");
412 /* --- Read a closing bracket --- */
418 error(sc
, "parse error, missing `]'");
423 /* --- @conf_parse@ --- *
425 * Arguments: @scanner *sc@ = pointer to scanner definition
429 * Use: Parses a configuration file from the scanner.
432 void conf_parse(scanner
*sc
)
437 if (sc
->t
== CTOK_EOF
)
439 if (sc
->t
!= CTOK_WORD
)
440 error(sc
, "parse error, keyword expected");
442 /* --- Handle a forwarding request --- */
444 if (strcmp(sc
->d
.buf
, "forward") == 0 ||
445 strcmp(sc
->d
.buf
, "fw") == 0 ||
446 strcmp(sc
->d
.buf
, "from") == 0) {
452 /* --- Read a source description --- */
457 /* --- Try to find a source type which understands --- */
460 for (sops
= sources
; *sops
; sops
++) {
461 if ((s
= (*sops
)->read(sc
)) != 0)
464 error(sc
, "unknown source name `%s'", sc
->d
.buf
);
466 /* --- Read any source-specific options --- */
471 while (sc
->t
== CTOK_WORD
) {
472 if (!s
->ops
->option
|| !s
->ops
->option(s
, sc
)) {
473 error(sc
, "unknown %s source option `%s'",
474 s
->ops
->name
, sc
->d
.buf
);
480 error(sc
, "parse error, missing `}'");
485 /* --- Read a destination description --- */
487 if (sc
->t
== CTOK_WORD
&& (strcmp(sc
->d
.buf
, "to") == 0 ||
488 strcmp(sc
->d
.buf
, "->") == 0))
494 /* --- Try to find a target which understands --- */
497 for (tops
= targets
; *tops
; tops
++) {
498 if ((t
= (*tops
)->read(sc
)) != 0)
501 error(sc
, "unknown target name `%s'", sc
->d
.buf
);
503 /* --- Read any target-specific options --- */
508 while (sc
->t
== CTOK_WORD
) {
509 if (!t
->ops
->option
|| !t
->ops
->option(t
, sc
)) {
510 error(sc
, "unknown %s target option `%s'",
511 t
->ops
->name
, sc
->d
.buf
);
517 error(sc
, "parse error, `}' expected");
522 /* --- Combine the source and target --- */
524 s
->ops
->attach(s
, sc
, t
);
527 /* --- Include configuration from a file --- *
529 * Slightly tricky. Scan the optional semicolon from the including
530 * stream, not the included one.
533 else if (strcmp(sc
->d
.buf
, "include") == 0) {
538 conf_name(sc
, '/', &d
);
539 if ((fp
= fopen(d
.buf
, "r")) == 0)
540 error(sc
, "can't include `%s': %s", d
.buf
, strerror(errno
));
544 scan_push(sc
, scan_file(fp
, xstrdup(d
.buf
), SCF_FREENAME
));
547 continue; /* Don't parse a trailing `;' */
550 /* --- Other configuration is handled elsewhere --- */
554 /* --- First try among the sources --- */
559 for (sops
= sources
; *sops
; sops
++) {
560 if ((*sops
)->option
&& (*sops
)->option(0, sc
))
565 /* --- Then try among the targets --- */
570 for (tops
= targets
; *tops
; tops
++) {
571 if ((*tops
)->option
&& (*tops
)->option(0, sc
))
576 /* --- Nobody wants the option --- */
578 error(sc
, "unknown global option or prefix `%s'", sc
->d
.buf
);
588 /*----- That's all, folks -------------------------------------------------*/