e82f7154 |
1 | /* -*-c-*- |
2 | * |
7481fc9c |
3 | * $Id: conf.h,v 1.9 2004/04/08 01:36:25 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 | |
e82f7154 |
29 | #ifndef CONF_H |
30 | #define CONF_H |
31 | |
32 | #ifdef __cplusplus |
33 | extern "C" { |
34 | #endif |
35 | |
36 | /*----- Header files ------------------------------------------------------*/ |
37 | |
61e3dbdf |
38 | #include <mLib/dstr.h> |
39 | |
e82f7154 |
40 | #ifndef SCAN_H |
41 | # include "scan.h" |
42 | #endif |
43 | |
61e3dbdf |
44 | /*----- Magic numbers -----------------------------------------------------*/ |
45 | |
46 | #define CTOK_EOF (-1) |
47 | #define CTOK_WORD 256 |
48 | |
e82f7154 |
49 | /*----- Functions provided ------------------------------------------------*/ |
50 | |
2234f01d |
51 | /* --- @conf_undelim@ --- * |
e73034b0 |
52 | * |
2234f01d |
53 | * Arguments: @scanner *sc@ = pointer to scanner definition |
54 | * @const char *d, *dd@ = pointer to characters to escape |
e73034b0 |
55 | * |
56 | * Returns: --- |
57 | * |
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. |
61 | */ |
62 | |
2234f01d |
63 | extern void conf_undelim(scanner */*sc*/, |
64 | const char */*d*/, const char */*dd*/); |
e73034b0 |
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 | |
75 | extern 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 | |
88 | extern void error(scanner */*sc*/, const char */*msg*/, ...); |
89 | |
c36e735a |
90 | /* --- @pushback@ --- * |
91 | * |
92 | * Arguments: @scanner *sc@ = pointer to scanner definition |
93 | * |
94 | * Returns: --- |
95 | * |
96 | * Use: Pushes the current token back. This is normally a precursor |
97 | * to pushing a new scanner source. |
98 | */ |
99 | |
100 | extern void pushback(scanner */*sc*/); |
101 | |
61e3dbdf |
102 | /* --- @conf_enum@ --- * |
103 | * |
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 |
108 | * |
109 | * Returns: Index into list, zero-based, or @-1@. |
110 | * |
111 | * Use: Checks whether the current token is a string which matches |
82793759 |
112 | * one of the comma-separated items given. The return value is |
113 | * the index (zero-based) of the matched string in the list. |
114 | * |
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 |
120 | * terminated. |
61e3dbdf |
121 | */ |
122 | |
123 | #define ENUM_ABBREV 1u |
124 | #define ENUM_NONE 2u |
125 | |
126 | extern int conf_enum(scanner */*sc*/, const char */*list*/, |
127 | unsigned /*flags*/, const char */*err*/); |
128 | |
129 | /* --- @conf_prefix@ --- * |
130 | * |
131 | * Arguments: @scanner *sc@ = pointer to a scanner object |
132 | * @const char *p@ = pointer to prefix string to check |
133 | * |
134 | * Returns: Nonzero if the prefix matches. |
135 | * |
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. |
140 | * |
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. |
146 | */ |
147 | |
148 | extern int conf_prefix(scanner */*sc*/, const char */*p*/); |
149 | |
022008ac |
150 | /* --- @CONF_BEGIN@, @CONF_END@ --- * |
61e3dbdf |
151 | * |
152 | * Arguments: @sc@ = scanner to read from |
153 | * @prefix@ = prefix to scan for |
154 | * @desc@ = description of what we're parsing |
155 | * |
022008ac |
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 |
164 | * made. |
61e3dbdf |
165 | */ |
166 | |
167 | #define CS_PLAIN 0 |
168 | #define CS_PREFIX 1 |
169 | #define CS_BRACE 2 |
170 | #define CS_UNKNOWN 3 |
171 | |
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; \ |
022008ac |
176 | \ |
177 | /* --- Read the initial prefix --- */ \ |
178 | \ |
61e3dbdf |
179 | if (_conf_sc->t == CTOK_WORD && \ |
180 | strcmp(_conf_sc->d.buf, (prefix)) == 0) { \ |
181 | token(_conf_sc); \ |
182 | _conf_state = CS_PREFIX; \ |
183 | if (_conf_sc->t == '.') \ |
184 | token(_conf_sc); \ |
185 | else if (_conf_sc->t == '{') { \ |
186 | token(_conf_sc); \ |
187 | _conf_state = CS_BRACE; \ |
188 | } \ |
189 | } \ |
022008ac |
190 | \ |
191 | /* --- Ensure the next token is a word --- */ \ |
192 | \ |
61e3dbdf |
193 | if (_conf_sc->t != CTOK_WORD) \ |
194 | error(_conf_sc, "parse error, expected option keyword"); \ |
195 | do { |
196 | |
61e3dbdf |
197 | #define CONF_END \ |
022008ac |
198 | \ |
199 | /* --- Reject an option --- * \ |
200 | * \ |
201 | * We could get here as a result of an explicit @CONF_REJECT@ or \ |
202 | * because the option wasn't accepted. \ |
203 | */ \ |
204 | \ |
0ac54f22 |
205 | goto _conf_reject; \ |
61e3dbdf |
206 | _conf_reject: \ |
207 | if (_conf_state == CS_PLAIN) \ |
208 | _conf_state = CS_UNKNOWN; \ |
209 | else { \ |
210 | error(_conf_sc, "unknown %s option `%s'", \ |
211 | _conf_desc, _conf_sc->d.buf); \ |
212 | } \ |
022008ac |
213 | \ |
214 | /* --- Accept an option --- * \ |
215 | * \ |
216 | * It's safe to drop through from above. Either an error will have \ |
217 | * been reported, or the state is not @CS_BRACE@. \ |
218 | */ \ |
219 | \ |
61e3dbdf |
220 | _conf_accept: \ |
221 | if (_conf_state == CS_BRACE && _conf_sc->t == ';') \ |
222 | token(_conf_sc); \ |
223 | } while (_conf_state == CS_BRACE && _conf_sc->t == CTOK_WORD); \ |
022008ac |
224 | \ |
225 | /* --- Check for a closing brace --- */ \ |
226 | \ |
61e3dbdf |
227 | if (_conf_state == CS_BRACE) { \ |
228 | if (_conf_sc->t == '}') \ |
229 | token(_conf_sc); \ |
230 | else \ |
231 | error(_conf_sc, "parse error, expected `}'"); \ |
232 | } \ |
022008ac |
233 | \ |
234 | /* --- Return an appropriate value --- */ \ |
235 | \ |
61e3dbdf |
236 | return (_conf_state != CS_UNKNOWN); \ |
237 | } while (0) |
238 | |
022008ac |
239 | /* --- @CONF_ACCEPT@, @CONF_REJECT@ --- * |
240 | * |
241 | * Arguments: --- |
242 | * |
243 | * Use: Within an options parser (between @CONF_BEGIN@ and |
244 | * @CONF_END@), accept or reject an option. |
245 | */ |
246 | |
247 | #define CONF_ACCEPT goto _conf_accept |
248 | #define CONF_REJECT goto _conf_reject |
249 | |
250 | /* --- @CONF_QUAL@ --- * |
251 | * |
252 | * Arguments: --- |
253 | * |
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. |
257 | */ |
258 | |
259 | #define CONF_QUAL (_conf_state != CS_PLAIN) |
260 | |
61e3dbdf |
261 | /* --- @conf_name@ --- * |
262 | * |
263 | * Arguments: @scanner *sc@ = pointer to scanner |
264 | * @char delim@ = delimiter character to look for |
265 | * @dstr *d@ = pointer to dynamic string for output |
266 | * |
267 | * Returns: --- |
268 | * |
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. |
273 | * |
274 | * Examples of compound names are filenames (delimited by `/') |
275 | * and IP addresses (delimited by `.'). |
276 | */ |
277 | |
278 | extern void conf_name(scanner */*sc*/, char /*delim*/, dstr */*d*/); |
279 | |
e82f7154 |
280 | /*----- That's all, folks -------------------------------------------------*/ |
281 | |
282 | #ifdef __cplusplus |
283 | } |
284 | #endif |
285 | |
286 | #endif |