chiark / gitweb /
Track @lbuf@ changes in mLib.
[fwd] / conf.h
1 /* -*-c-*-
2  *
3  * $Id: conf.h,v 1.6 2002/01/13 14:48:16 mdw Exp $
4  *
5  * Configuration parsing
6  *
7  * (c) 1999 Straylight/Edgeware
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 $
32  * Revision 1.6  2002/01/13 14:48:16  mdw
33  * Make delimiters be a property of a scanner.  Change the delimiter-
34  * changing functions' names.
35  *
36  * Revision 1.5  1999/10/22 22:46:44  mdw
37  * Improve documentation for conf_enum.
38  *
39  * Revision 1.4  1999/08/19 18:32:48  mdw
40  * Improve lexical analysis.  In particular, `chmod' patterns don't have to
41  * be quoted any more.
42  *
43  * Revision 1.3  1999/07/27 18:30:14  mdw
44  * Improve documentation and layout for @CONF_BEGIN@ and friends.  Remove
45  * irritating warning about unused label by introducing a spurious `goto'.
46  *
47  * Revision 1.2  1999/07/26 23:28:39  mdw
48  * Major reconstruction work for new design.
49  *
50  * Revision 1.1.1.1  1999/07/01 08:56:23  mdw
51  * Initial revision.
52  *
53  */
54
55 #ifndef CONF_H
56 #define CONF_H
57
58 #ifdef __cplusplus
59   extern "C" {
60 #endif
61
62 /*----- Header files ------------------------------------------------------*/
63
64 #include <mLib/dstr.h>
65
66 #ifndef SCAN_H
67 #  include "scan.h"
68 #endif
69
70 /*----- Magic numbers -----------------------------------------------------*/
71
72 #define CTOK_EOF (-1)
73 #define CTOK_WORD 256
74
75 /*----- Functions provided ------------------------------------------------*/
76
77 /* --- @conf_undelim@ --- *
78  *
79  * Arguments:   @scanner *sc@ = pointer to scanner definition
80  *              @const char *d, *dd@ = pointer to characters to escape
81  *
82  * Returns:     ---
83  *
84  * Use:         Modifies the tokenizer.  Characters in the first list will
85  *              always be considered to begin a word.  Characters in the
86  *              second list will always be allowed to continue a word.
87  */
88
89 extern void conf_undelim(scanner */*sc*/,
90                          const char */*d*/, const char */*dd*/);
91
92 /* --- @token@ --- *
93  *
94  * Arguments:   @scanner *sc@ = pointer to scanner definition
95  *
96  * Returns:     Type of token scanned.
97  *
98  * Use:         Reads the next token from the character scanner.
99  */
100
101 extern int token(scanner */*sc*/);
102
103 /* --- @error@ --- *
104  *
105  * Arguments:   @scanner *sc@ = pointer to scanner definition
106  *              @const char *msg@ = message skeleton string
107  *              @...@ = extra arguments for the skeleton
108  *
109  * Returns:     Doesn't
110  *
111  * Use:         Reports an error at the current scanner location.
112  */
113
114 extern void error(scanner */*sc*/, const char */*msg*/, ...);
115
116 /* --- @conf_enum@ --- *
117  *
118  * Arguments:   @scanner *sc@ = pointer to a scanner object
119  *              @const char *list@ = comma-separated things to allow
120  *              @unsigned @f = flags for the search
121  *              @const char *err@ = error message if not found
122  *
123  * Returns:     Index into list, zero-based, or @-1@.
124  *
125  * Use:         Checks whether the current token is a string which matches
126  *              one of the comma-separated items given.  The return value is
127  *              the index (zero-based) of the matched string in the list.
128  *
129  *              The flags control the behaviour if no exact match is found.
130  *              If @ENUM_ABBREV@ is set, and the current token is a left
131  *              substring of exactly one of the possibilities, then that one
132  *              is chosen.  If @ENUM_NONE@ is set, the value @-1@ is
133  *              returned; otherwise an error is reported and the program is
134  *              terminated.
135  */
136
137 #define ENUM_ABBREV 1u
138 #define ENUM_NONE 2u
139
140 extern int conf_enum(scanner */*sc*/, const char */*list*/,
141                      unsigned /*flags*/, const char */*err*/);
142
143 /* --- @conf_prefix@ --- *
144  *
145  * Arguments:   @scanner *sc@ = pointer to a scanner object
146  *              @const char *p@ = pointer to prefix string to check
147  *
148  * Returns:     Nonzero if the prefix matches.
149  *
150  * Use:         If the current token is a word matching the given prefix
151  *              string, then it and an optional `.' character are removed and
152  *              a nonzero result is returned.  Otherwise the current token is
153  *              left as it is, and zero is returned.
154  *
155  *              Typical options parsing code would remove an expected prefix,
156  *              scan an option anyway (since qualifying prefixes are
157  *              optional) and if a match is found, claim the option.  If no
158  *              match is found, and a prefix was stripped, then an error
159  *              should be reported.
160  */
161
162 extern int conf_prefix(scanner */*sc*/, const char */*p*/);
163
164 /* --- @CONF_BEGIN@, @CONF_END@ --- *
165  *
166  * Arguments:   @sc@ = scanner to read from
167  *              @prefix@ = prefix to scan for
168  *              @desc@ = description of what we're parsing
169  *
170  * Use:         Bracket an options parsing routine.  The current token is
171  *              checked to see whether it matches the prefix.  If so, it is
172  *              removed and the following token examined.  If that's a `.'
173  *              then it's removed.  If it's a `{' then the enclosed
174  *              option-parsing code is executed in a loop until a matching
175  *              '}' is found.  If the options parser doesn't accept an
176  *              option, the behaviour is dependent on whether a prefix was
177  *              seen: if so, an error is reported; otherwse a zero return is
178  *              made.
179  */
180
181 #define CS_PLAIN 0
182 #define CS_PREFIX 1
183 #define CS_BRACE 2
184 #define CS_UNKNOWN 3
185
186 #define CONF_BEGIN(sc, prefix, desc) do {                               \
187   scanner *_conf_sc = (sc);                                             \
188   const char *_conf_desc = (desc);                                      \
189   int _conf_state = CS_PLAIN;                                           \
190                                                                         \
191   /* --- Read the initial prefix --- */                                 \
192                                                                         \
193   if (_conf_sc->t == CTOK_WORD &&                                       \
194       strcmp(_conf_sc->d.buf, (prefix)) == 0) {                         \
195     token(_conf_sc);                                                    \
196     _conf_state = CS_PREFIX;                                            \
197     if (_conf_sc->t == '.')                                             \
198       token(_conf_sc);                                                  \
199     else if (_conf_sc->t == '{') {                                      \
200       token(_conf_sc);                                                  \
201       _conf_state = CS_BRACE;                                           \
202     }                                                                   \
203   }                                                                     \
204                                                                         \
205   /* --- Ensure the next token is a word --- */                         \
206                                                                         \
207   if (_conf_sc->t != CTOK_WORD)                                         \
208     error(_conf_sc, "parse error, expected option keyword");            \
209   do {
210
211 #define CONF_END                                                        \
212                                                                         \
213     /* --- Reject an option --- *                                       \
214      *                                                                  \
215      * We could get here as a result of an explicit @CONF_REJECT@ or    \
216      * because the option wasn't accepted.                              \
217      */                                                                 \
218                                                                         \
219      goto _conf_reject;                                                 \
220   _conf_reject:                                                         \
221     if (_conf_state == CS_PLAIN)                                        \
222       _conf_state = CS_UNKNOWN;                                         \
223     else {                                                              \
224       error(_conf_sc, "unknown %s option `%s'",                         \
225             _conf_desc, _conf_sc->d.buf);                               \
226     }                                                                   \
227                                                                         \
228     /* --- Accept an option --- *                                       \
229      *                                                                  \
230      * It's safe to drop through from above.  Either an error will have \
231      * been reported, or the state is not @CS_BRACE@.                   \
232      */                                                                 \
233                                                                         \
234   _conf_accept:                                                         \
235     if (_conf_state == CS_BRACE && _conf_sc->t == ';')                  \
236       token(_conf_sc);                                                  \
237   } while (_conf_state == CS_BRACE && _conf_sc->t == CTOK_WORD);        \
238                                                                         \
239   /* --- Check for a closing brace --- */                               \
240                                                                         \
241   if (_conf_state == CS_BRACE) {                                        \
242     if (_conf_sc->t == '}')                                             \
243       token(_conf_sc);                                                  \
244     else                                                                \
245       error(_conf_sc, "parse error, expected `}'");                     \
246   }                                                                     \
247                                                                         \
248   /* --- Return an appropriate value --- */                             \
249                                                                         \
250   return (_conf_state != CS_UNKNOWN);                                   \
251 } while (0)
252
253 /* --- @CONF_ACCEPT@, @CONF_REJECT@ --- *
254  *
255  * Arguments:   ---
256  *
257  * Use:         Within an options parser (between @CONF_BEGIN@ and
258  *              @CONF_END@), accept or reject an option.
259  */
260
261 #define CONF_ACCEPT goto _conf_accept
262 #define CONF_REJECT goto _conf_reject
263
264 /* --- @CONF_QUAL@ --- *
265  *
266  * Arguments:   ---
267  *
268  * Use:         Evaluates to a nonzero value if the current option is
269  *              qualified.  This can be used to decide whether abbreviations
270  *              for options should be accepted.
271  */
272
273 #define CONF_QUAL (_conf_state != CS_PLAIN)
274
275 /* --- @conf_name@ --- *
276  *
277  * Arguments:   @scanner *sc@ = pointer to scanner
278  *              @char delim@ = delimiter character to look for
279  *              @dstr *d@ = pointer to dynamic string for output
280  *
281  * Returns:     ---
282  *
283  * Use:         Reads in a compound name consisting of words separated by
284  *              delimiters.  Leading and trailing delimiters are permitted,
285  *              although they'll probably cause confusion if used.  The name
286  *              may be enclosed in square brackets if that helps at all.
287  *
288  *              Examples of compound names are filenames (delimited by `/')
289  *              and IP addresses (delimited by `.').
290  */
291
292 extern void conf_name(scanner */*sc*/, char /*delim*/, dstr */*d*/);
293
294 /* --- @conf_parse@ --- *
295  *
296  * Arguments:   @scanner *sc@ = pointer to a scanner structure
297  *
298  * Returns:     ---
299  *
300  * Use:         Parses a configuration file fragment from the scanner
301  */
302
303 extern void conf_parse(scanner *sc);
304
305 /*----- That's all, folks -------------------------------------------------*/
306
307 #ifdef __cplusplus
308   }
309 #endif
310
311 #endif