Minor tidying and typo correction.
[fwd] / conf.h
CommitLineData
e82f7154 1/* -*-c-*-
2 *
022008ac 3 * $Id: conf.h,v 1.3 1999/07/27 18:30:14 mdw Exp $
e82f7154 4 *
5 * Configuration parsing
6 *
61e3dbdf 7 * (c) 1999 Straylight/Edgeware
e82f7154 8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the `fw' port forwarder.
13 *
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.
18 *
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.
23 *
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.
27 */
28
29/*----- Revision history --------------------------------------------------*
30 *
31 * $Log: conf.h,v $
022008ac 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'.
35 *
61e3dbdf 36 * Revision 1.2 1999/07/26 23:28:39 mdw
37 * Major reconstruction work for new design.
38 *
39 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
40 * Initial revision.
e82f7154 41 *
42 */
43
44#ifndef CONF_H
45#define CONF_H
46
47#ifdef __cplusplus
48 extern "C" {
49#endif
50
51/*----- Header files ------------------------------------------------------*/
52
61e3dbdf 53#include <mLib/dstr.h>
54
e82f7154 55#ifndef SCAN_H
56# include "scan.h"
57#endif
58
61e3dbdf 59/*----- Magic numbers -----------------------------------------------------*/
60
61#define CTOK_EOF (-1)
62#define CTOK_WORD 256
63
e82f7154 64/*----- Functions provided ------------------------------------------------*/
65
61e3dbdf 66/* --- @token@ --- *
67 *
68 * Arguments: @scanner *sc@ = pointer to scanner definition
69 *
70 * Returns: Type of token scanned.
71 *
72 * Use: Reads the next token from the character scanner.
73 */
74
75extern int token(scanner */*sc*/);
76
77/* --- @error@ --- *
78 *
79 * Arguments: @scanner *sc@ = pointer to scanner definition
80 * @const char *msg@ = message skeleton string
81 * @...@ = extra arguments for the skeleton
82 *
83 * Returns: Doesn't
84 *
85 * Use: Reports an error at the current scanner location.
86 */
87
88extern void error(scanner */*sc*/, const char */*msg*/, ...);
89
90/* --- @conf_enum@ --- *
91 *
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
96 *
97 * Returns: Index into list, zero-based, or @-1@.
98 *
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
102 * returned.
103 */
104
105#define ENUM_ABBREV 1u
106#define ENUM_NONE 2u
107
108extern int conf_enum(scanner */*sc*/, const char */*list*/,
109 unsigned /*flags*/, const char */*err*/);
110
111/* --- @conf_prefix@ --- *
112 *
113 * Arguments: @scanner *sc@ = pointer to a scanner object
114 * @const char *p@ = pointer to prefix string to check
115 *
116 * Returns: Nonzero if the prefix matches.
117 *
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.
122 *
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.
128 */
129
130extern int conf_prefix(scanner */*sc*/, const char */*p*/);
131
022008ac 132/* --- @CONF_BEGIN@, @CONF_END@ --- *
61e3dbdf 133 *
134 * Arguments: @sc@ = scanner to read from
135 * @prefix@ = prefix to scan for
136 * @desc@ = description of what we're parsing
137 *
022008ac 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
146 * made.
61e3dbdf 147 */
148
149#define CS_PLAIN 0
150#define CS_PREFIX 1
151#define CS_BRACE 2
152#define CS_UNKNOWN 3
153
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; \
022008ac 158 \
159 /* --- Read the initial prefix --- */ \
160 \
61e3dbdf 161 if (_conf_sc->t == CTOK_WORD && \
162 strcmp(_conf_sc->d.buf, (prefix)) == 0) { \
163 token(_conf_sc); \
164 _conf_state = CS_PREFIX; \
165 if (_conf_sc->t == '.') \
166 token(_conf_sc); \
167 else if (_conf_sc->t == '{') { \
168 token(_conf_sc); \
169 _conf_state = CS_BRACE; \
170 } \
171 } \
022008ac 172 \
173 /* --- Ensure the next token is a word --- */ \
174 \
61e3dbdf 175 if (_conf_sc->t != CTOK_WORD) \
176 error(_conf_sc, "parse error, expected option keyword"); \
177 do {
178
61e3dbdf 179#define CONF_END \
022008ac 180 \
181 /* --- Reject an option --- * \
182 * \
183 * We could get here as a result of an explicit @CONF_REJECT@ or \
184 * because the option wasn't accepted. \
185 */ \
186 \
187 goto _conf_reject; \
61e3dbdf 188 _conf_reject: \
189 if (_conf_state == CS_PLAIN) \
190 _conf_state = CS_UNKNOWN; \
191 else { \
192 error(_conf_sc, "unknown %s option `%s'", \
193 _conf_desc, _conf_sc->d.buf); \
194 } \
022008ac 195 \
196 /* --- Accept an option --- * \
197 * \
198 * It's safe to drop through from above. Either an error will have \
199 * been reported, or the state is not @CS_BRACE@. \
200 */ \
201 \
61e3dbdf 202 _conf_accept: \
203 if (_conf_state == CS_BRACE && _conf_sc->t == ';') \
204 token(_conf_sc); \
205 } while (_conf_state == CS_BRACE && _conf_sc->t == CTOK_WORD); \
022008ac 206 \
207 /* --- Check for a closing brace --- */ \
208 \
61e3dbdf 209 if (_conf_state == CS_BRACE) { \
210 if (_conf_sc->t == '}') \
211 token(_conf_sc); \
212 else \
213 error(_conf_sc, "parse error, expected `}'"); \
214 } \
022008ac 215 \
216 /* --- Return an appropriate value --- */ \
217 \
61e3dbdf 218 return (_conf_state != CS_UNKNOWN); \
219} while (0)
220
022008ac 221/* --- @CONF_ACCEPT@, @CONF_REJECT@ --- *
222 *
223 * Arguments: ---
224 *
225 * Use: Within an options parser (between @CONF_BEGIN@ and
226 * @CONF_END@), accept or reject an option.
227 */
228
229#define CONF_ACCEPT goto _conf_accept
230#define CONF_REJECT goto _conf_reject
231
232/* --- @CONF_QUAL@ --- *
233 *
234 * Arguments: ---
235 *
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.
239 */
240
241#define CONF_QUAL (_conf_state != CS_PLAIN)
242
61e3dbdf 243/* --- @conf_name@ --- *
244 *
245 * Arguments: @scanner *sc@ = pointer to scanner
246 * @char delim@ = delimiter character to look for
247 * @dstr *d@ = pointer to dynamic string for output
248 *
249 * Returns: ---
250 *
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.
255 *
256 * Examples of compound names are filenames (delimited by `/')
257 * and IP addresses (delimited by `.').
258 */
259
260extern void conf_name(scanner */*sc*/, char /*delim*/, dstr */*d*/);
261
e82f7154 262/* --- @conf_parse@ --- *
263 *
61e3dbdf 264 * Arguments: @scanner *sc@ = pointer to a scanner structure
e82f7154 265 *
266 * Returns: ---
267 *
268 * Use: Parses a configuration file fragment from the scanner
269 */
270
61e3dbdf 271extern void conf_parse(scanner *sc);
e82f7154 272
273/*----- That's all, folks -------------------------------------------------*/
274
275#ifdef __cplusplus
276 }
277#endif
278
279#endif