3 * $Id: conf.c,v 1.4 1999/10/15 21:12:36 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.4 1999/10/15 21:12:36 mdw
33 * Remove redundant debugging code.
35 * Revision 1.3 1999/08/19 18:32:48 mdw
36 * Improve lexical analysis. In particular, `chmod' patterns don't have to
39 * Revision 1.2 1999/07/26 23:28:39 mdw
40 * Major reconstruction work for new design.
42 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
47 /*----- Header files ------------------------------------------------------*/
58 #include <mLib/dstr.h>
59 #include <mLib/quis.h>
60 #include <mLib/report.h>
71 /*----- Source and target tables ------------------------------------------*/
73 static source_ops
*sources
[] = { &xsource_ops
, &fsource_ops
, &ssource_ops
};
74 static target_ops
*targets
[] = { &xtarget_ops
, &ftarget_ops
, &starget_ops
};
76 static const char *notword
= 0;
77 static const char *notdelim
= 0;
79 /*----- Main code ---------------------------------------------------------*/
81 /* --- @undelim@ --- *
83 * Arguments: @const char *d, dd@ = pointer to characters to escape
87 * Use: Modifies the tokenizer. Characters in the first list will
88 * always be considered to begin a word. Characters in the
89 * second list will always be allowed to continue a word.
92 void undelim(const char *d
, const char *dd
) { notword
= d
; notdelim
= dd
; }
96 * Arguments: @scanner *sc@ = pointer to scanner definition
98 * Returns: Type of token scanned.
100 * Use: Reads the next token from the character scanner.
103 int token(scanner
*sc
)
106 '{': case '}': case '/': case ',': \
107 case '=': case ':': case ';': \
108 case '.': case '[': case ']'
114 /* --- Main tokenization --- */
119 /* --- Deal with pushed-back tokens --- */
122 dstr_puts(&sc
->d
, sc
->head
->tok
);
129 else if (isspace((unsigned char)ch
))
133 /* --- End of file --- */
139 /* --- Comment character --- */
142 do ch
= scan(sc
); while (ch
!= EOF
&& ch
!= '\n');
145 /* --- Various self-delimiting characters --- */
148 if (!notword
|| strchr(notword
, ch
) == 0) {
149 dstr_putc(&sc
->d
, ch
);
155 /* --- Bare words --- *
157 * These aren't as bare any more. You can now backslash-escape
158 * individual characters, and enclose sections in double-quotes.
178 if (q
|| (notdelim
&& strchr(notdelim
, ch
)))
182 if (!q
&& isspace((unsigned char)(ch
)))
203 /* --- @pushback@ --- *
205 * Arguments: @scanner *sc@ = pointer to scanner definition
209 * Use: Pushes the current token back. This is normally a precursor
210 * to pushing a new scanner source.
213 static void pushback(scanner
*sc
)
215 sc
->head
->tok
= xstrdup(sc
->d
.buf
);
221 * Arguments: @scanner *sc@ = pointer to scanner definition
222 * @const char *msg@ = message skeleton string
223 * @...@ = extra arguments for the skeleton
227 * Use: Reports an error at the current scanner location.
230 void error(scanner
*sc
, const char *msg
, ...)
234 fprintf(stderr
, "%s: %s:%i: ", QUIS
, sc
->head
->src
, sc
->head
->line
);
235 vfprintf(stderr
, msg
, ap
);
240 /* --- @conf_enum@ --- *
242 * Arguments: @scanner *sc@ = pointer to a scanner object
243 * @const char *list@ = comma-separated things to allow
244 * @unsigned f@ = flags for the search
245 * @const char *err@ = error message if not found
247 * Returns: Index into list, zero-based, or @-1@.
249 * Use: Checks whether the current token is a string which matches
250 * one of the comma-separated items given. If not, an error is
251 * reported; otherwise the index of the matched item is
255 int conf_enum(scanner
*sc
, const char *list
, unsigned f
, const char *err
)
262 /* --- Make sure it's a string --- */
264 if (sc
->t
!= CTOK_WORD
)
265 error(sc
, "parse error, expected %s", err
);
267 /* --- Grind through the list --- */
279 } else if (chosen
!= -1) {
283 else if (f
& ENUM_NONE
)
286 error(sc
, "unknown %s `%s'", err
, sc
->d
.buf
);
300 if ((f
& ENUM_ABBREV
) && !*q
) {
302 error(sc
, "ambiguous %s `%s'", err
, sc
->d
.buf
);
316 /* --- @conf_prefix@ --- *
318 * Arguments: @scanner *sc@ = pointer to a scanner object
319 * @const char *p@ = pointer to prefix string to check
321 * Returns: Nonzero if the prefix matches.
323 * Use: If the current token is a word matching the given prefix
324 * string, then it and an optional `.' character are removed and
325 * a nonzero result is returned. Otherwise the current token is
326 * left as it is, and zero is returned.
328 * Typical options parsing code would remove an expected prefix,
329 * scan an option anyway (since qualifying prefixes are
330 * optional) and if a match is found, claim the option. If no
331 * match is found, and a prefix was stripped, then an error
332 * should be reported.
335 int conf_prefix(scanner
*sc
, const char *p
)
337 if (sc
->t
== CTOK_WORD
&& strcmp(p
, sc
->d
.buf
) == 0) {
346 /* --- @conf_name@ --- *
348 * Arguments: @scanner *sc@ = pointer to scanner
349 * @char delim@ = delimiter character to look for
350 * @dstr *d@ = pointer to dynamic string for output
354 * Use: Reads in a compound name consisting of words separated by
355 * delimiters. Leading and trailing delimiters are permitted,
356 * although they'll probably cause confusion if used. The name
357 * may be enclosed in square brackets if that helps at all.
359 * Examples of compound names are filenames (delimited by `/')
360 * and IP addresses (delimited by `.').
363 void conf_name(scanner
*sc
, char delim
, dstr
*d
)
371 /* --- Read an optional opening bracket --- */
378 /* --- Do the main reading sequence --- */
381 if (sc
->t
== delim
) {
386 if (sc
->t
== CTOK_WORD
) {
391 } while (sc
->t
== delim
);
393 /* --- Check that the string was OK --- */
396 error(sc
, "parse error, name expected");
398 /* --- Read a closing bracket --- */
404 error(sc
, "parse error, missing `]'");
409 /* --- @conf_parse@ --- *
411 * Arguments: @scanner *sc@ = pointer to scanner definition
415 * Use: Parses a configuration file from the scanner.
418 void conf_parse(scanner
*sc
)
423 if (sc
->t
== CTOK_EOF
)
425 if (sc
->t
!= CTOK_WORD
)
426 error(sc
, "parse error, keyword expected");
428 /* --- Handle a forwarding request --- */
430 if (strcmp(sc
->d
.buf
, "forward") == 0 ||
431 strcmp(sc
->d
.buf
, "fw") == 0 ||
432 strcmp(sc
->d
.buf
, "from") == 0) {
438 /* --- Read a source description --- */
443 /* --- Try to find a source type which understands --- */
446 for (sops
= sources
; *sops
; sops
++) {
447 if ((s
= (*sops
)->read(sc
)) != 0)
450 error(sc
, "unknown source name `%s'", sc
->d
.buf
);
452 /* --- Read any source-specific options --- */
457 while (sc
->t
== CTOK_WORD
) {
458 if (!s
->ops
->option
|| !s
->ops
->option(s
, sc
)) {
459 error(sc
, "unknown %s source option `%s'",
460 s
->ops
->name
, sc
->d
.buf
);
466 error(sc
, "parse error, missing `}'");
471 /* --- Read a destination description --- */
473 if (sc
->t
== CTOK_WORD
&& (strcmp(sc
->d
.buf
, "to") == 0 ||
474 strcmp(sc
->d
.buf
, "->") == 0))
480 /* --- Try to find a target which understands --- */
483 for (tops
= targets
; *tops
; tops
++) {
484 if ((t
= (*tops
)->read(sc
)) != 0)
487 error(sc
, "unknown target name `%s'", sc
->d
.buf
);
489 /* --- Read any target-specific options --- */
494 while (sc
->t
== CTOK_WORD
) {
495 if (!t
->ops
->option
|| !t
->ops
->option(t
, sc
)) {
496 error(sc
, "unknown %s target option `%s'",
497 t
->ops
->name
, sc
->d
.buf
);
503 error(sc
, "parse error, `}' expected");
508 /* --- Combine the source and target --- */
510 s
->ops
->attach(s
, sc
, t
);
513 /* --- Include configuration from a file --- *
515 * Slightly tricky. Scan the optional semicolon from the including
516 * stream, not the included one.
519 else if (strcmp(sc
->d
.buf
, "include") == 0) {
524 conf_name(sc
, '/', &d
);
525 if ((fp
= fopen(d
.buf
, "r")) == 0)
526 error(sc
, "can't include `%s': %s", d
.buf
, strerror(errno
));
530 scan_push(sc
, scan_file(fp
, xstrdup(d
.buf
), SCF_FREENAME
));
533 continue; /* Don't parse a trailing `;' */
536 /* --- Other configuration is handled elsewhere --- */
540 /* --- First try among the sources --- */
545 for (sops
= sources
; *sops
; sops
++) {
546 if ((*sops
)->option
&& (*sops
)->option(0, sc
))
551 /* --- Then try among the targets --- */
556 for (tops
= targets
; *tops
; tops
++) {
557 if ((*tops
)->option
&& (*tops
)->option(0, sc
))
562 /* --- Nobody wants the option --- */
564 error(sc
, "unknown global option or prefix `%s'", sc
->d
.buf
);
574 /*----- That's all, folks -------------------------------------------------*/