3 * $Id: conf.h,v 1.3 1999/07/27 18:30:14 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.3 1999/07/27 18:30:14 mdw
33 * Improve documentation and layout for @CONF_BEGIN@ and friends. Remove
34 * irritating warning about unused label by introducing a spurious `goto'.
36 * Revision 1.2 1999/07/26 23:28:39 mdw
37 * Major reconstruction work for new design.
39 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
51 /*----- Header files ------------------------------------------------------*/
53 #include <mLib/dstr.h>
59 /*----- Magic numbers -----------------------------------------------------*/
64 /*----- Functions provided ------------------------------------------------*/
68 * Arguments: @scanner *sc@ = pointer to scanner definition
70 * Returns: Type of token scanned.
72 * Use: Reads the next token from the character scanner.
75 extern int token(scanner */*sc*/);
79 * Arguments: @scanner *sc@ = pointer to scanner definition
80 * @const char *msg@ = message skeleton string
81 * @...@ = extra arguments for the skeleton
85 * Use: Reports an error at the current scanner location.
88 extern void error(scanner */*sc*/, const char */*msg*/, ...);
90 /* --- @conf_enum@ --- *
92 * Arguments: @scanner *sc@ = pointer to a scanner object
93 * @const char *list@ = comma-separated things to allow
94 * @unsigned @f = flags for the search
95 * @const char *err@ = error message if not found
97 * Returns: Index into list, zero-based, or @-1@.
99 * Use: Checks whether the current token is a string which matches
100 * one of the comma-separated items given. If not, an error is
101 * reported; otherwise the index of the matched item is
105 #define ENUM_ABBREV 1u
108 extern int conf_enum(scanner */*sc*/, const char */*list*/,
109 unsigned /*flags*/, const char */*err*/);
111 /* --- @conf_prefix@ --- *
113 * Arguments: @scanner *sc@ = pointer to a scanner object
114 * @const char *p@ = pointer to prefix string to check
116 * Returns: Nonzero if the prefix matches.
118 * Use: If the current token is a word matching the given prefix
119 * string, then it and an optional `.' character are removed and
120 * a nonzero result is returned. Otherwise the current token is
121 * left as it is, and zero is returned.
123 * Typical options parsing code would remove an expected prefix,
124 * scan an option anyway (since qualifying prefixes are
125 * optional) and if a match is found, claim the option. If no
126 * match is found, and a prefix was stripped, then an error
127 * should be reported.
130 extern int conf_prefix(scanner */*sc*/, const char */*p*/);
132 /* --- @CONF_BEGIN@, @CONF_END@ --- *
134 * Arguments: @sc@ = scanner to read from
135 * @prefix@ = prefix to scan for
136 * @desc@ = description of what we're parsing
138 * Use: Bracket an options parsing routine. The current token is
139 * checked to see whether it matches the prefix. If so, it is
140 * removed and the following token examined. If that's a `.'
141 * then it's removed. If it's a `{' then the enclosed
142 * option-parsing code is executed in a loop until a matching
143 * '}' is found. If the options parser doesn't accept an
144 * option, the behaviour is dependent on whether a prefix was
145 * seen: if so, an error is reported; otherwse a zero return is
154 #define CONF_BEGIN(sc, prefix, desc) do { \
155 scanner *_conf_sc = (sc); \
156 const char *_conf_desc = (desc); \
157 int _conf_state = CS_PLAIN; \
159 /* --- Read the initial prefix --- */ \
161 if (_conf_sc->t == CTOK_WORD && \
162 strcmp(_conf_sc->d.buf, (prefix)) == 0) { \
164 _conf_state = CS_PREFIX; \
165 if (_conf_sc->t == '.') \
167 else if (_conf_sc->t == '{') { \
169 _conf_state = CS_BRACE; \
173 /* --- Ensure the next token is a word --- */ \
175 if (_conf_sc->t != CTOK_WORD) \
176 error(_conf_sc, "parse error, expected option keyword"); \
181 /* --- Reject an option --- * \
183 * We could get here as a result of an explicit @CONF_REJECT@ or \
184 * because the option wasn't accepted. \
189 if (_conf_state == CS_PLAIN) \
190 _conf_state = CS_UNKNOWN; \
192 error(_conf_sc, "unknown %s option `%s'", \
193 _conf_desc, _conf_sc->d.buf); \
196 /* --- Accept an option --- * \
198 * It's safe to drop through from above. Either an error will have \
199 * been reported, or the state is not @CS_BRACE@. \
203 if (_conf_state == CS_BRACE && _conf_sc->t == ';') \
205 } while (_conf_state == CS_BRACE && _conf_sc->t == CTOK_WORD); \
207 /* --- Check for a closing brace --- */ \
209 if (_conf_state == CS_BRACE) { \
210 if (_conf_sc->t == '}') \
213 error(_conf_sc, "parse error, expected `}'"); \
216 /* --- Return an appropriate value --- */ \
218 return (_conf_state != CS_UNKNOWN); \
221 /* --- @CONF_ACCEPT@, @CONF_REJECT@ --- *
225 * Use: Within an options parser (between @CONF_BEGIN@ and
226 * @CONF_END@), accept or reject an option.
229 #define CONF_ACCEPT goto _conf_accept
230 #define CONF_REJECT goto _conf_reject
232 /* --- @CONF_QUAL@ --- *
236 * Use: Evaluates to a nonzero value if the current option is
237 * qualified. This can be used to decide whether abbreviations
238 * for options should be accepted.
241 #define CONF_QUAL (_conf_state != CS_PLAIN)
243 /* --- @conf_name@ --- *
245 * Arguments: @scanner *sc@ = pointer to scanner
246 * @char delim@ = delimiter character to look for
247 * @dstr *d@ = pointer to dynamic string for output
251 * Use: Reads in a compound name consisting of words separated by
252 * delimiters. Leading and trailing delimiters are permitted,
253 * although they'll probably cause confusion if used. The name
254 * may be enclosed in square brackets if that helps at all.
256 * Examples of compound names are filenames (delimited by `/')
257 * and IP addresses (delimited by `.').
260 extern void conf_name(scanner */*sc*/, char /*delim*/, dstr */*d*/);
262 /* --- @conf_parse@ --- *
264 * Arguments: @scanner *sc@ = pointer to a scanner structure
268 * Use: Parses a configuration file fragment from the scanner
271 extern void conf_parse(scanner *sc);
273 /*----- That's all, folks -------------------------------------------------*/