3 * $Id: conf.h,v 1.9 2004/04/08 01:36:25 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.
36 /*----- Header files ------------------------------------------------------*/
38 #include <mLib/dstr.h>
44 /*----- Magic numbers -----------------------------------------------------*/
49 /*----- Functions provided ------------------------------------------------*/
51 /* --- @conf_undelim@ --- *
53 * Arguments: @scanner *sc@ = pointer to scanner definition
54 * @const char *d, *dd@ = pointer to characters to escape
58 * Use: Modifies the tokenizer. Characters in the first list will
59 * always be considered to begin a word. Characters in the
60 * second list will always be allowed to continue a word.
63 extern void conf_undelim(scanner */*sc*/,
64 const char */*d*/, const char */*dd*/);
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 /* --- @pushback@ --- *
92 * Arguments: @scanner *sc@ = pointer to scanner definition
96 * Use: Pushes the current token back. This is normally a precursor
97 * to pushing a new scanner source.
100 extern void pushback(scanner */*sc*/);
102 /* --- @conf_enum@ --- *
104 * Arguments: @scanner *sc@ = pointer to a scanner object
105 * @const char *list@ = comma-separated things to allow
106 * @unsigned @f = flags for the search
107 * @const char *err@ = error message if not found
109 * Returns: Index into list, zero-based, or @-1@.
111 * Use: Checks whether the current token is a string which matches
112 * one of the comma-separated items given. The return value is
113 * the index (zero-based) of the matched string in the list.
115 * The flags control the behaviour if no exact match is found.
116 * If @ENUM_ABBREV@ is set, and the current token is a left
117 * substring of exactly one of the possibilities, then that one
118 * is chosen. If @ENUM_NONE@ is set, the value @-1@ is
119 * returned; otherwise an error is reported and the program is
123 #define ENUM_ABBREV 1u
126 extern int conf_enum(scanner */*sc*/, const char */*list*/,
127 unsigned /*flags*/, const char */*err*/);
129 /* --- @conf_prefix@ --- *
131 * Arguments: @scanner *sc@ = pointer to a scanner object
132 * @const char *p@ = pointer to prefix string to check
134 * Returns: Nonzero if the prefix matches.
136 * Use: If the current token is a word matching the given prefix
137 * string, then it and an optional `.' character are removed and
138 * a nonzero result is returned. Otherwise the current token is
139 * left as it is, and zero is returned.
141 * Typical options parsing code would remove an expected prefix,
142 * scan an option anyway (since qualifying prefixes are
143 * optional) and if a match is found, claim the option. If no
144 * match is found, and a prefix was stripped, then an error
145 * should be reported.
148 extern int conf_prefix(scanner */*sc*/, const char */*p*/);
150 /* --- @CONF_BEGIN@, @CONF_END@ --- *
152 * Arguments: @sc@ = scanner to read from
153 * @prefix@ = prefix to scan for
154 * @desc@ = description of what we're parsing
156 * Use: Bracket an options parsing routine. The current token is
157 * checked to see whether it matches the prefix. If so, it is
158 * removed and the following token examined. If that's a `.'
159 * then it's removed. If it's a `{' then the enclosed
160 * option-parsing code is executed in a loop until a matching
161 * '}' is found. If the options parser doesn't accept an
162 * option, the behaviour is dependent on whether a prefix was
163 * seen: if so, an error is reported; otherwse a zero return is
172 #define CONF_BEGIN(sc, prefix, desc) do { \
173 scanner *_conf_sc = (sc); \
174 const char *_conf_desc = (desc); \
175 int _conf_state = CS_PLAIN; \
177 /* --- Read the initial prefix --- */ \
179 if (_conf_sc->t == CTOK_WORD && \
180 strcmp(_conf_sc->d.buf, (prefix)) == 0) { \
182 _conf_state = CS_PREFIX; \
183 if (_conf_sc->t == '.') \
185 else if (_conf_sc->t == '{') { \
187 _conf_state = CS_BRACE; \
191 /* --- Ensure the next token is a word --- */ \
193 if (_conf_sc->t != CTOK_WORD) \
194 error(_conf_sc, "parse error, expected option keyword"); \
199 /* --- Reject an option --- * \
201 * We could get here as a result of an explicit @CONF_REJECT@ or \
202 * because the option wasn't accepted. \
207 if (_conf_state == CS_PLAIN) \
208 _conf_state = CS_UNKNOWN; \
210 error(_conf_sc, "unknown %s option `%s'", \
211 _conf_desc, _conf_sc->d.buf); \
214 /* --- Accept an option --- * \
216 * It's safe to drop through from above. Either an error will have \
217 * been reported, or the state is not @CS_BRACE@. \
221 if (_conf_state == CS_BRACE && _conf_sc->t == ';') \
223 } while (_conf_state == CS_BRACE && _conf_sc->t == CTOK_WORD); \
225 /* --- Check for a closing brace --- */ \
227 if (_conf_state == CS_BRACE) { \
228 if (_conf_sc->t == '}') \
231 error(_conf_sc, "parse error, expected `}'"); \
234 /* --- Return an appropriate value --- */ \
236 return (_conf_state != CS_UNKNOWN); \
239 /* --- @CONF_ACCEPT@, @CONF_REJECT@ --- *
243 * Use: Within an options parser (between @CONF_BEGIN@ and
244 * @CONF_END@), accept or reject an option.
247 #define CONF_ACCEPT goto _conf_accept
248 #define CONF_REJECT goto _conf_reject
250 /* --- @CONF_QUAL@ --- *
254 * Use: Evaluates to a nonzero value if the current option is
255 * qualified. This can be used to decide whether abbreviations
256 * for options should be accepted.
259 #define CONF_QUAL (_conf_state != CS_PLAIN)
261 /* --- @conf_name@ --- *
263 * Arguments: @scanner *sc@ = pointer to scanner
264 * @char delim@ = delimiter character to look for
265 * @dstr *d@ = pointer to dynamic string for output
269 * Use: Reads in a compound name consisting of words separated by
270 * delimiters. Leading and trailing delimiters are permitted,
271 * although they'll probably cause confusion if used. The name
272 * may be enclosed in square brackets if that helps at all.
274 * Examples of compound names are filenames (delimited by `/')
275 * and IP addresses (delimited by `.').
278 extern void conf_name(scanner */*sc*/, char /*delim*/, dstr */*d*/);
280 /*----- That's all, folks -------------------------------------------------*/