3 * $Id: conf.h,v 1.4 1999/08/19 18:32:48 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/08/19 18:32:48 mdw
33 * Improve lexical analysis. In particular, `chmod' patterns don't have to
36 * Revision 1.3 1999/07/27 18:30:14 mdw
37 * Improve documentation and layout for @CONF_BEGIN@ and friends. Remove
38 * irritating warning about unused label by introducing a spurious `goto'.
40 * Revision 1.2 1999/07/26 23:28:39 mdw
41 * Major reconstruction work for new design.
43 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
55 /*----- Header files ------------------------------------------------------*/
57 #include <mLib/dstr.h>
63 /*----- Magic numbers -----------------------------------------------------*/
68 /*----- Functions provided ------------------------------------------------*/
70 /* --- @undelim@ --- *
72 * Arguments: @const char *d, dd@ = pointer to characters to escape
76 * Use: Modifies the tokenizer. Characters in the first list will
77 * always be considered to begin a word. Characters in the
78 * second list will always be allowed to continue a word.
81 extern void undelim(const char */*d*/, const char */*dd*/);
85 * Arguments: @scanner *sc@ = pointer to scanner definition
87 * Returns: Type of token scanned.
89 * Use: Reads the next token from the character scanner.
92 extern int token(scanner */*sc*/);
96 * Arguments: @scanner *sc@ = pointer to scanner definition
97 * @const char *msg@ = message skeleton string
98 * @...@ = extra arguments for the skeleton
102 * Use: Reports an error at the current scanner location.
105 extern void error(scanner */*sc*/, const char */*msg*/, ...);
107 /* --- @conf_enum@ --- *
109 * Arguments: @scanner *sc@ = pointer to a scanner object
110 * @const char *list@ = comma-separated things to allow
111 * @unsigned @f = flags for the search
112 * @const char *err@ = error message if not found
114 * Returns: Index into list, zero-based, or @-1@.
116 * Use: Checks whether the current token is a string which matches
117 * one of the comma-separated items given. If not, an error is
118 * reported; otherwise the index of the matched item is
122 #define ENUM_ABBREV 1u
125 extern int conf_enum(scanner */*sc*/, const char */*list*/,
126 unsigned /*flags*/, const char */*err*/);
128 /* --- @conf_prefix@ --- *
130 * Arguments: @scanner *sc@ = pointer to a scanner object
131 * @const char *p@ = pointer to prefix string to check
133 * Returns: Nonzero if the prefix matches.
135 * Use: If the current token is a word matching the given prefix
136 * string, then it and an optional `.' character are removed and
137 * a nonzero result is returned. Otherwise the current token is
138 * left as it is, and zero is returned.
140 * Typical options parsing code would remove an expected prefix,
141 * scan an option anyway (since qualifying prefixes are
142 * optional) and if a match is found, claim the option. If no
143 * match is found, and a prefix was stripped, then an error
144 * should be reported.
147 extern int conf_prefix(scanner */*sc*/, const char */*p*/);
149 /* --- @CONF_BEGIN@, @CONF_END@ --- *
151 * Arguments: @sc@ = scanner to read from
152 * @prefix@ = prefix to scan for
153 * @desc@ = description of what we're parsing
155 * Use: Bracket an options parsing routine. The current token is
156 * checked to see whether it matches the prefix. If so, it is
157 * removed and the following token examined. If that's a `.'
158 * then it's removed. If it's a `{' then the enclosed
159 * option-parsing code is executed in a loop until a matching
160 * '}' is found. If the options parser doesn't accept an
161 * option, the behaviour is dependent on whether a prefix was
162 * seen: if so, an error is reported; otherwse a zero return is
171 #define CONF_BEGIN(sc, prefix, desc) do { \
172 scanner *_conf_sc = (sc); \
173 const char *_conf_desc = (desc); \
174 int _conf_state = CS_PLAIN; \
176 /* --- Read the initial prefix --- */ \
178 if (_conf_sc->t == CTOK_WORD && \
179 strcmp(_conf_sc->d.buf, (prefix)) == 0) { \
181 _conf_state = CS_PREFIX; \
182 if (_conf_sc->t == '.') \
184 else if (_conf_sc->t == '{') { \
186 _conf_state = CS_BRACE; \
190 /* --- Ensure the next token is a word --- */ \
192 if (_conf_sc->t != CTOK_WORD) \
193 error(_conf_sc, "parse error, expected option keyword"); \
198 /* --- Reject an option --- * \
200 * We could get here as a result of an explicit @CONF_REJECT@ or \
201 * because the option wasn't accepted. \
206 if (_conf_state == CS_PLAIN) \
207 _conf_state = CS_UNKNOWN; \
209 error(_conf_sc, "unknown %s option `%s'", \
210 _conf_desc, _conf_sc->d.buf); \
213 /* --- Accept an option --- * \
215 * It's safe to drop through from above. Either an error will have \
216 * been reported, or the state is not @CS_BRACE@. \
220 if (_conf_state == CS_BRACE && _conf_sc->t == ';') \
222 } while (_conf_state == CS_BRACE && _conf_sc->t == CTOK_WORD); \
224 /* --- Check for a closing brace --- */ \
226 if (_conf_state == CS_BRACE) { \
227 if (_conf_sc->t == '}') \
230 error(_conf_sc, "parse error, expected `}'"); \
233 /* --- Return an appropriate value --- */ \
235 return (_conf_state != CS_UNKNOWN); \
238 /* --- @CONF_ACCEPT@, @CONF_REJECT@ --- *
242 * Use: Within an options parser (between @CONF_BEGIN@ and
243 * @CONF_END@), accept or reject an option.
246 #define CONF_ACCEPT goto _conf_accept
247 #define CONF_REJECT goto _conf_reject
249 /* --- @CONF_QUAL@ --- *
253 * Use: Evaluates to a nonzero value if the current option is
254 * qualified. This can be used to decide whether abbreviations
255 * for options should be accepted.
258 #define CONF_QUAL (_conf_state != CS_PLAIN)
260 /* --- @conf_name@ --- *
262 * Arguments: @scanner *sc@ = pointer to scanner
263 * @char delim@ = delimiter character to look for
264 * @dstr *d@ = pointer to dynamic string for output
268 * Use: Reads in a compound name consisting of words separated by
269 * delimiters. Leading and trailing delimiters are permitted,
270 * although they'll probably cause confusion if used. The name
271 * may be enclosed in square brackets if that helps at all.
273 * Examples of compound names are filenames (delimited by `/')
274 * and IP addresses (delimited by `.').
277 extern void conf_name(scanner */*sc*/, char /*delim*/, dstr */*d*/);
279 /* --- @conf_parse@ --- *
281 * Arguments: @scanner *sc@ = pointer to a scanner structure
285 * Use: Parses a configuration file fragment from the scanner
288 extern void conf_parse(scanner *sc);
290 /*----- That's all, folks -------------------------------------------------*/