3 * $Id: conf.c,v 1.11 2004/04/08 01:36:25 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 /*----- Header files ------------------------------------------------------*/
38 #include <mLib/dstr.h>
39 #include <mLib/quis.h>
40 #include <mLib/report.h>
45 /*----- Main code ---------------------------------------------------------*/
47 /* --- @conf_undelim@ --- *
49 * Arguments: @scanner *sc@ = pointer to scanner definition
50 * @const char *d, *dd@ = pointer to characters to escape
54 * Use: Modifies the tokenizer. Characters in the first list will
55 * always be considered to begin a word. Characters in the
56 * second list will always be allowed to continue a word.
59 void conf_undelim(scanner *sc, const char *d, const char *dd)
67 * Arguments: @scanner *sc@ = pointer to scanner definition
69 * Returns: Type of token scanned.
71 * Use: Reads the next token from the character scanner.
74 int token(scanner *sc)
77 '{': case '}': case '/': case ',': \
78 case '=': case ':': case ';': \
79 case '.': case '[': case ']'
85 /* --- Main tokenization --- */
90 /* --- Deal with pushed-back tokens --- */
93 dstr_puts(&sc->d, sc->head->tok);
100 else if (isspace(ch))
104 /* --- End of file --- */
110 /* --- Comment character --- */
113 do ch = scan(sc); while (ch != EOF && ch != '\n');
116 /* --- Various self-delimiting characters --- */
119 if (!sc->wbegin || strchr(sc->wbegin, ch) == 0) {
120 dstr_putc(&sc->d, ch);
126 /* --- Bare words --- *
128 * These aren't as bare any more. You can now backslash-escape
129 * individual characters, and enclose sections in double-quotes.
149 if (q || (sc->wcont && strchr(sc->wcont, ch)))
153 if (!q && isspace(ch))
174 /* --- @pushback@ --- *
176 * Arguments: @scanner *sc@ = pointer to scanner definition
180 * Use: Pushes the current token back. This is normally a precursor
181 * to pushing a new scanner source.
184 void pushback(scanner *sc)
186 sc->head->tok = xstrdup(sc->d.buf);
192 * Arguments: @scanner *sc@ = pointer to scanner definition
193 * @const char *msg@ = message skeleton string
194 * @...@ = extra arguments for the skeleton
198 * Use: Reports an error at the current scanner location.
201 void error(scanner *sc, const char *msg, ...)
205 fprintf(stderr, "%s: %s:%i: ", QUIS, sc->head->src, sc->head->line);
206 vfprintf(stderr, msg, ap);
211 /* --- @conf_enum@ --- *
213 * Arguments: @scanner *sc@ = pointer to a scanner object
214 * @const char *list@ = comma-separated things to allow
215 * @unsigned f@ = flags for the search
216 * @const char *err@ = error message if not found
218 * Returns: Index into list, zero-based, or @-1@.
220 * Use: Checks whether the current token is a string which matches
221 * one of the comma-separated items given. The return value is
222 * the index (zero-based) of the matched string in the list.
224 * The flags control the behaviour if no exact match is found.
225 * If @ENUM_ABBREV@ is set, and the current token is a left
226 * substring of exactly one of the possibilities, then that one
227 * is chosen. If @ENUM_NONE@ is set, the value @-1@ is
228 * returned; otherwise an error is reported and the program is
232 int conf_enum(scanner *sc, const char *list, unsigned f, const char *err)
239 /* --- Make sure it's a string --- */
241 if (sc->t != CTOK_WORD)
242 error(sc, "parse error, expected %s", err);
244 /* --- Grind through the list --- */
256 } else if (chosen != -1) {
260 else if (f & ENUM_NONE)
263 error(sc, "unknown %s `%s'", err, sc->d.buf);
277 if ((f & ENUM_ABBREV) && !*q) {
279 error(sc, "ambiguous %s `%s'", err, sc->d.buf);
293 /* --- @conf_prefix@ --- *
295 * Arguments: @scanner *sc@ = pointer to a scanner object
296 * @const char *p@ = pointer to prefix string to check
298 * Returns: Nonzero if the prefix matches.
300 * Use: If the current token is a word matching the given prefix
301 * string, then it and an optional `.' character are removed and
302 * a nonzero result is returned. Otherwise the current token is
303 * left as it is, and zero is returned.
305 * Typical options parsing code would remove an expected prefix,
306 * scan an option anyway (since qualifying prefixes are
307 * optional) and if a match is found, claim the option. If no
308 * match is found, and a prefix was stripped, then an error
309 * should be reported.
312 int conf_prefix(scanner *sc, const char *p)
314 if (sc->t == CTOK_WORD && strcmp(p, sc->d.buf) == 0) {
323 /* --- @conf_name@ --- *
325 * Arguments: @scanner *sc@ = pointer to scanner
326 * @char delim@ = delimiter character to look for
327 * @dstr *d@ = pointer to dynamic string for output
331 * Use: Reads in a compound name consisting of words separated by
332 * delimiters. Leading and trailing delimiters are permitted,
333 * although they'll probably cause confusion if used. The name
334 * may be enclosed in square brackets if that helps at all.
336 * Examples of compound names are filenames (delimited by `/')
337 * and IP addresses (delimited by `.').
340 void conf_name(scanner *sc, char delim, dstr *d)
347 /* --- Read an optional opening bracket --- */
354 /* --- Do the main reading sequence --- */
357 if (sc->t == delim) {
362 if (sc->t == CTOK_WORD) {
367 } while (sc->t == delim);
369 /* --- Check that the string was OK --- */
372 error(sc, "parse error, name expected");
374 /* --- Read a closing bracket --- */
380 error(sc, "parse error, missing `]'");
388 /*----- That's all, folks -------------------------------------------------*/