3 * Configuration parsing
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the `fw' port forwarder.
12 * `fw' is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * `fw' is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with `fw'; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Main code ---------------------------------------------------------*/
31 /* --- @conf_undelim@ --- *
33 * Arguments: @scanner *sc@ = pointer to scanner definition
34 * @const char *d, *dd@ = pointer to characters to escape
38 * Use: Modifies the tokenizer. Characters in the first list will
39 * always be considered to begin a word. Characters in the
40 * second list will always be allowed to continue a word.
43 void conf_undelim(scanner *sc, const char *d, const char *dd)
51 * Arguments: @scanner *sc@ = pointer to scanner definition
53 * Returns: Type of token scanned.
55 * Use: Reads the next token from the character scanner.
58 int token(scanner *sc)
61 '{': case '}': case '/': case ',': \
62 case '=': case ':': case ';': \
63 case '.': case '[': case ']'
69 /* --- Main tokenization --- */
74 /* --- Deal with pushed-back tokens --- */
77 dstr_puts(&sc->d, sc->head->tok);
88 /* --- End of file --- */
94 /* --- Comment character --- */
97 do ch = scan(sc); while (ch != EOF && ch != '\n');
100 /* --- Various self-delimiting characters --- */
103 if (!sc->wbegin || strchr(sc->wbegin, ch) == 0) {
104 dstr_putc(&sc->d, ch);
110 /* --- Bare words --- *
112 * These aren't as bare any more. You can now backslash-escape
113 * individual characters, and enclose sections in double-quotes.
133 if (q || (sc->wcont && strchr(sc->wcont, ch)))
137 if (!q && isspace(ch))
158 /* --- @pushback@ --- *
160 * Arguments: @scanner *sc@ = pointer to scanner definition
164 * Use: Pushes the current token back. This is normally a precursor
165 * to pushing a new scanner source.
168 void pushback(scanner *sc)
170 sc->head->tok = xstrdup(sc->d.buf);
176 * Arguments: @scanner *sc@ = pointer to scanner definition
177 * @const char *msg@ = message skeleton string
178 * @...@ = extra arguments for the skeleton
182 * Use: Reports an error at the current scanner location.
185 void error(scanner *sc, const char *msg, ...)
189 fprintf(stderr, "%s: %s:%i: ", QUIS, sc->head->src, sc->head->line);
190 vfprintf(stderr, msg, ap);
195 /* --- @conf_enum@ --- *
197 * Arguments: @scanner *sc@ = pointer to a scanner object
198 * @const char *list@ = comma-separated things to allow
199 * @unsigned f@ = flags for the search
200 * @const char *err@ = error message if not found
202 * Returns: Index into list, zero-based, or @-1@.
204 * Use: Checks whether the current token is a string which matches
205 * one of the comma-separated items given. The return value is
206 * the index (zero-based) of the matched string in the list.
208 * The flags control the behaviour if no exact match is found.
209 * If @ENUM_ABBREV@ is set, and the current token is a left
210 * substring of exactly one of the possibilities, then that one
211 * is chosen. If @ENUM_NONE@ is set, the value @-1@ is
212 * returned; otherwise an error is reported and the program is
216 int conf_enum(scanner *sc, const char *list, unsigned f, const char *err)
223 /* --- Make sure it's a string --- */
225 if (sc->t != CTOK_WORD)
226 error(sc, "parse error, expected %s", err);
228 /* --- Grind through the list --- */
240 } else if (chosen != -1) {
244 else if (f & ENUM_NONE)
247 error(sc, "unknown %s `%s'", err, sc->d.buf);
261 if ((f & ENUM_ABBREV) && !*q) {
263 error(sc, "ambiguous %s `%s'", err, sc->d.buf);
277 /* --- @conf_prefix@ --- *
279 * Arguments: @scanner *sc@ = pointer to a scanner object
280 * @const char *p@ = pointer to prefix string to check
282 * Returns: Nonzero if the prefix matches.
284 * Use: If the current token is a word matching the given prefix
285 * string, then it and an optional `.' character are removed and
286 * a nonzero result is returned. Otherwise the current token is
287 * left as it is, and zero is returned.
289 * Typical options parsing code would remove an expected prefix,
290 * scan an option anyway (since qualifying prefixes are
291 * optional) and if a match is found, claim the option. If no
292 * match is found, and a prefix was stripped, then an error
293 * should be reported.
296 int conf_prefix(scanner *sc, const char *p)
298 if (sc->t == CTOK_WORD && strcmp(p, sc->d.buf) == 0) {
307 /* --- @conf_name@ --- *
309 * Arguments: @scanner *sc@ = pointer to scanner
310 * @char delim@ = delimiter character to look for
311 * @dstr *d@ = pointer to dynamic string for output
315 * Use: Reads in a compound name consisting of words separated by
316 * delimiters. Leading and trailing delimiters are permitted,
317 * although they'll probably cause confusion if used. The name
318 * may be enclosed in square brackets if that helps at all.
320 * Examples of compound names are filenames (delimited by `/')
321 * and IP addresses (delimited by `.').
324 void conf_name(scanner *sc, char delim, dstr *d)
331 /* --- Read an optional opening bracket --- */
338 /* --- Do the main reading sequence --- */
341 if (sc->t == delim) {
346 if (sc->t == CTOK_WORD) {
351 } while (sc->t == delim);
353 /* --- Check that the string was OK --- */
356 error(sc, "parse error, name expected");
358 /* --- Read a closing bracket --- */
364 error(sc, "parse error, missing `]'");
372 /*----- That's all, folks -------------------------------------------------*/