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.
27 /*----- Header files ------------------------------------------------------*/
36 #include <mLib/dstr.h>
37 #include <mLib/quis.h>
38 #include <mLib/report.h>
43 /*----- Main code ---------------------------------------------------------*/
45 /* --- @conf_undelim@ --- *
47 * Arguments: @scanner *sc@ = pointer to scanner definition
48 * @const char *d, *dd@ = pointer to characters to escape
52 * Use: Modifies the tokenizer. Characters in the first list will
53 * always be considered to begin a word. Characters in the
54 * second list will always be allowed to continue a word.
57 void conf_undelim(scanner *sc, const char *d, const char *dd)
65 * Arguments: @scanner *sc@ = pointer to scanner definition
67 * Returns: Type of token scanned.
69 * Use: Reads the next token from the character scanner.
72 int token(scanner *sc)
75 '{': case '}': case '/': case ',': \
76 case '=': case ':': case ';': \
77 case '.': case '[': case ']'
83 /* --- Main tokenization --- */
88 /* --- Deal with pushed-back tokens --- */
91 dstr_puts(&sc->d, sc->head->tok);
102 /* --- End of file --- */
108 /* --- Comment character --- */
111 do ch = scan(sc); while (ch != EOF && ch != '\n');
114 /* --- Various self-delimiting characters --- */
117 if (!sc->wbegin || strchr(sc->wbegin, ch) == 0) {
118 dstr_putc(&sc->d, ch);
124 /* --- Bare words --- *
126 * These aren't as bare any more. You can now backslash-escape
127 * individual characters, and enclose sections in double-quotes.
147 if (q || (sc->wcont && strchr(sc->wcont, ch)))
151 if (!q && isspace(ch))
172 /* --- @pushback@ --- *
174 * Arguments: @scanner *sc@ = pointer to scanner definition
178 * Use: Pushes the current token back. This is normally a precursor
179 * to pushing a new scanner source.
182 void pushback(scanner *sc)
184 sc->head->tok = xstrdup(sc->d.buf);
190 * Arguments: @scanner *sc@ = pointer to scanner definition
191 * @const char *msg@ = message skeleton string
192 * @...@ = extra arguments for the skeleton
196 * Use: Reports an error at the current scanner location.
199 void error(scanner *sc, const char *msg, ...)
203 fprintf(stderr, "%s: %s:%i: ", QUIS, sc->head->src, sc->head->line);
204 vfprintf(stderr, msg, ap);
209 /* --- @conf_enum@ --- *
211 * Arguments: @scanner *sc@ = pointer to a scanner object
212 * @const char *list@ = comma-separated things to allow
213 * @unsigned f@ = flags for the search
214 * @const char *err@ = error message if not found
216 * Returns: Index into list, zero-based, or @-1@.
218 * Use: Checks whether the current token is a string which matches
219 * one of the comma-separated items given. The return value is
220 * the index (zero-based) of the matched string in the list.
222 * The flags control the behaviour if no exact match is found.
223 * If @ENUM_ABBREV@ is set, and the current token is a left
224 * substring of exactly one of the possibilities, then that one
225 * is chosen. If @ENUM_NONE@ is set, the value @-1@ is
226 * returned; otherwise an error is reported and the program is
230 int conf_enum(scanner *sc, const char *list, unsigned f, const char *err)
237 /* --- Make sure it's a string --- */
239 if (sc->t != CTOK_WORD)
240 error(sc, "parse error, expected %s", err);
242 /* --- Grind through the list --- */
254 } else if (chosen != -1) {
258 else if (f & ENUM_NONE)
261 error(sc, "unknown %s `%s'", err, sc->d.buf);
275 if ((f & ENUM_ABBREV) && !*q) {
277 error(sc, "ambiguous %s `%s'", err, sc->d.buf);
291 /* --- @conf_prefix@ --- *
293 * Arguments: @scanner *sc@ = pointer to a scanner object
294 * @const char *p@ = pointer to prefix string to check
296 * Returns: Nonzero if the prefix matches.
298 * Use: If the current token is a word matching the given prefix
299 * string, then it and an optional `.' character are removed and
300 * a nonzero result is returned. Otherwise the current token is
301 * left as it is, and zero is returned.
303 * Typical options parsing code would remove an expected prefix,
304 * scan an option anyway (since qualifying prefixes are
305 * optional) and if a match is found, claim the option. If no
306 * match is found, and a prefix was stripped, then an error
307 * should be reported.
310 int conf_prefix(scanner *sc, const char *p)
312 if (sc->t == CTOK_WORD && strcmp(p, sc->d.buf) == 0) {
321 /* --- @conf_name@ --- *
323 * Arguments: @scanner *sc@ = pointer to scanner
324 * @char delim@ = delimiter character to look for
325 * @dstr *d@ = pointer to dynamic string for output
329 * Use: Reads in a compound name consisting of words separated by
330 * delimiters. Leading and trailing delimiters are permitted,
331 * although they'll probably cause confusion if used. The name
332 * may be enclosed in square brackets if that helps at all.
334 * Examples of compound names are filenames (delimited by `/')
335 * and IP addresses (delimited by `.').
338 void conf_name(scanner *sc, char delim, dstr *d)
345 /* --- Read an optional opening bracket --- */
352 /* --- Do the main reading sequence --- */
355 if (sc->t == delim) {
360 if (sc->t == CTOK_WORD) {
365 } while (sc->t == delim);
367 /* --- Check that the string was OK --- */
370 error(sc, "parse error, name expected");
372 /* --- Read a closing bracket --- */
378 error(sc, "parse error, missing `]'");
386 /*----- That's all, folks -------------------------------------------------*/