3 * $Id: conf.c,v 1.10 2002/02/22 23:42:56 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.10 2002/02/22 23:42:56 mdw
33 * `fw'-specific configuration code moved out. This file might become part
34 * of a library some day.
36 * Revision 1.9 2002/01/13 14:48:16 mdw
37 * Make delimiters be a property of a scanner. Change the delimiter-
38 * changing functions' names.
40 * Revision 1.8 2001/02/03 20:33:26 mdw
41 * Fix flags to be unsigned.
43 * Revision 1.7 2000/08/01 17:58:10 mdw
44 * Fix subtleties with <ctype.h> functions.
46 * Revision 1.6 2000/02/12 18:13:20 mdw
47 * Terminate tables of sources and targets.
49 * Revision 1.5 1999/10/22 22:46:44 mdw
50 * Improve documentation for conf_enum.
52 * Revision 1.4 1999/10/15 21:12:36 mdw
53 * Remove redundant debugging code.
55 * Revision 1.3 1999/08/19 18:32:48 mdw
56 * Improve lexical analysis. In particular, `chmod' patterns don't have to
59 * Revision 1.2 1999/07/26 23:28:39 mdw
60 * Major reconstruction work for new design.
62 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
67 /*----- Header files ------------------------------------------------------*/
76 #include <mLib/dstr.h>
77 #include <mLib/quis.h>
78 #include <mLib/report.h>
83 /*----- Main code ---------------------------------------------------------*/
85 /* --- @conf_undelim@ --- *
87 * Arguments: @scanner *sc@ = pointer to scanner definition
88 * @const char *d, *dd@ = pointer to characters to escape
92 * Use: Modifies the tokenizer. Characters in the first list will
93 * always be considered to begin a word. Characters in the
94 * second list will always be allowed to continue a word.
97 void conf_undelim(scanner
*sc
, const char *d
, const char *dd
)
105 * Arguments: @scanner *sc@ = pointer to scanner definition
107 * Returns: Type of token scanned.
109 * Use: Reads the next token from the character scanner.
112 int token(scanner
*sc
)
115 '{': case '}': case '/': case ',': \
116 case '=': case ':': case ';': \
117 case '.': case '[': case ']'
123 /* --- Main tokenization --- */
128 /* --- Deal with pushed-back tokens --- */
131 dstr_puts(&sc
->d
, sc
->head
->tok
);
132 xfree(sc
->head
->tok
);
138 else if (isspace(ch
))
142 /* --- End of file --- */
148 /* --- Comment character --- */
151 do ch
= scan(sc
); while (ch
!= EOF
&& ch
!= '\n');
154 /* --- Various self-delimiting characters --- */
157 if (!sc
->wbegin
|| strchr(sc
->wbegin
, ch
) == 0) {
158 dstr_putc(&sc
->d
, ch
);
164 /* --- Bare words --- *
166 * These aren't as bare any more. You can now backslash-escape
167 * individual characters, and enclose sections in double-quotes.
187 if (q
|| (sc
->wcont
&& strchr(sc
->wcont
, ch
)))
191 if (!q
&& isspace(ch
))
212 /* --- @pushback@ --- *
214 * Arguments: @scanner *sc@ = pointer to scanner definition
218 * Use: Pushes the current token back. This is normally a precursor
219 * to pushing a new scanner source.
222 void pushback(scanner
*sc
)
224 sc
->head
->tok
= xstrdup(sc
->d
.buf
);
230 * Arguments: @scanner *sc@ = pointer to scanner definition
231 * @const char *msg@ = message skeleton string
232 * @...@ = extra arguments for the skeleton
236 * Use: Reports an error at the current scanner location.
239 void error(scanner
*sc
, const char *msg
, ...)
243 fprintf(stderr
, "%s: %s:%i: ", QUIS
, sc
->head
->src
, sc
->head
->line
);
244 vfprintf(stderr
, msg
, ap
);
249 /* --- @conf_enum@ --- *
251 * Arguments: @scanner *sc@ = pointer to a scanner object
252 * @const char *list@ = comma-separated things to allow
253 * @unsigned f@ = flags for the search
254 * @const char *err@ = error message if not found
256 * Returns: Index into list, zero-based, or @-1@.
258 * Use: Checks whether the current token is a string which matches
259 * one of the comma-separated items given. The return value is
260 * the index (zero-based) of the matched string in the list.
262 * The flags control the behaviour if no exact match is found.
263 * If @ENUM_ABBREV@ is set, and the current token is a left
264 * substring of exactly one of the possibilities, then that one
265 * is chosen. If @ENUM_NONE@ is set, the value @-1@ is
266 * returned; otherwise an error is reported and the program is
270 int conf_enum(scanner
*sc
, const char *list
, unsigned f
, const char *err
)
277 /* --- Make sure it's a string --- */
279 if (sc
->t
!= CTOK_WORD
)
280 error(sc
, "parse error, expected %s", err
);
282 /* --- Grind through the list --- */
294 } else if (chosen
!= -1) {
298 else if (f
& ENUM_NONE
)
301 error(sc
, "unknown %s `%s'", err
, sc
->d
.buf
);
315 if ((f
& ENUM_ABBREV
) && !*q
) {
317 error(sc
, "ambiguous %s `%s'", err
, sc
->d
.buf
);
331 /* --- @conf_prefix@ --- *
333 * Arguments: @scanner *sc@ = pointer to a scanner object
334 * @const char *p@ = pointer to prefix string to check
336 * Returns: Nonzero if the prefix matches.
338 * Use: If the current token is a word matching the given prefix
339 * string, then it and an optional `.' character are removed and
340 * a nonzero result is returned. Otherwise the current token is
341 * left as it is, and zero is returned.
343 * Typical options parsing code would remove an expected prefix,
344 * scan an option anyway (since qualifying prefixes are
345 * optional) and if a match is found, claim the option. If no
346 * match is found, and a prefix was stripped, then an error
347 * should be reported.
350 int conf_prefix(scanner
*sc
, const char *p
)
352 if (sc
->t
== CTOK_WORD
&& strcmp(p
, sc
->d
.buf
) == 0) {
361 /* --- @conf_name@ --- *
363 * Arguments: @scanner *sc@ = pointer to scanner
364 * @char delim@ = delimiter character to look for
365 * @dstr *d@ = pointer to dynamic string for output
369 * Use: Reads in a compound name consisting of words separated by
370 * delimiters. Leading and trailing delimiters are permitted,
371 * although they'll probably cause confusion if used. The name
372 * may be enclosed in square brackets if that helps at all.
374 * Examples of compound names are filenames (delimited by `/')
375 * and IP addresses (delimited by `.').
378 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 `]'");
426 /*----- That's all, folks -------------------------------------------------*/