chiark / gitweb /
.gitignore: Autoconf machinery banished to the `config' directory.
[fwd] / conf.h
1 /* -*-c-*-
2  *
3  * Configuration parsing
4  *
5  * (c) 1999 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the `fwd' port forwarder.
11  *
12  * `fwd' is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * `fwd' is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with `fwd'; if not, write to the Free Software Foundation,
24  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  */
26
27 #ifndef CONF_H
28 #define CONF_H
29
30 #ifdef __cplusplus
31   extern "C" {
32 #endif
33
34 /*----- Header files ------------------------------------------------------*/
35
36 #include <mLib/dstr.h>
37
38 #ifndef SCAN_H
39 #  include "scan.h"
40 #endif
41
42 /*----- Magic numbers -----------------------------------------------------*/
43
44 #define CTOK_EOF (-1)
45 #define CTOK_WORD 256
46
47 /*----- Functions provided ------------------------------------------------*/
48
49 /* --- @conf_undelim@ --- *
50  *
51  * Arguments:   @scanner *sc@ = pointer to scanner definition
52  *              @const char *d, *dd@ = pointer to characters to escape
53  *
54  * Returns:     ---
55  *
56  * Use:         Modifies the tokenizer.  Characters in the first list will
57  *              always be considered to begin a word.  Characters in the
58  *              second list will always be allowed to continue a word.
59  */
60
61 extern void conf_undelim(scanner */*sc*/,
62                          const char */*d*/, const char */*dd*/);
63
64 /* --- @token@ --- *
65  *
66  * Arguments:   @scanner *sc@ = pointer to scanner definition
67  *
68  * Returns:     Type of token scanned.
69  *
70  * Use:         Reads the next token from the character scanner.
71  */
72
73 extern int token(scanner */*sc*/);
74
75 /* --- @error@ --- *
76  *
77  * Arguments:   @scanner *sc@ = pointer to scanner definition
78  *              @const char *msg@ = message skeleton string
79  *              @...@ = extra arguments for the skeleton
80  *
81  * Returns:     Doesn't
82  *
83  * Use:         Reports an error at the current scanner location.
84  */
85
86 extern void error(scanner */*sc*/, const char */*msg*/, ...);
87
88 /* --- @pushback@ --- *
89  *
90  * Arguments:   @scanner *sc@ = pointer to scanner definition
91  *
92  * Returns:     ---
93  *
94  * Use:         Pushes the current token back.  This is normally a precursor
95  *              to pushing a new scanner source.
96  */
97
98 extern void pushback(scanner */*sc*/);
99
100 /* --- @conf_enum@ --- *
101  *
102  * Arguments:   @scanner *sc@ = pointer to a scanner object
103  *              @const char *list@ = comma-separated things to allow
104  *              @unsigned @f = flags for the search
105  *              @const char *err@ = error message if not found
106  *
107  * Returns:     Index into list, zero-based, or @-1@.
108  *
109  * Use:         Checks whether the current token is a string which matches
110  *              one of the comma-separated items given.  The return value is
111  *              the index (zero-based) of the matched string in the list.
112  *
113  *              The flags control the behaviour if no exact match is found.
114  *              If @ENUM_ABBREV@ is set, and the current token is a left
115  *              substring of exactly one of the possibilities, then that one
116  *              is chosen.  If @ENUM_NONE@ is set, the value @-1@ is
117  *              returned; otherwise an error is reported and the program is
118  *              terminated.
119  */
120
121 #define ENUM_ABBREV 1u
122 #define ENUM_NONE 2u
123
124 extern int conf_enum(scanner */*sc*/, const char */*list*/,
125                      unsigned /*flags*/, const char */*err*/);
126
127 /* --- @conf_prefix@ --- *
128  *
129  * Arguments:   @scanner *sc@ = pointer to a scanner object
130  *              @const char *p@ = pointer to prefix string to check
131  *
132  * Returns:     Nonzero if the prefix matches.
133  *
134  * Use:         If the current token is a word matching the given prefix
135  *              string, then it and an optional `.' character are removed and
136  *              a nonzero result is returned.  Otherwise the current token is
137  *              left as it is, and zero is returned.
138  *
139  *              Typical options parsing code would remove an expected prefix,
140  *              scan an option anyway (since qualifying prefixes are
141  *              optional) and if a match is found, claim the option.  If no
142  *              match is found, and a prefix was stripped, then an error
143  *              should be reported.
144  */
145
146 extern int conf_prefix(scanner */*sc*/, const char */*p*/);
147
148 /* --- @CONF_BEGIN@, @CONF_END@ --- *
149  *
150  * Arguments:   @sc@ = scanner to read from
151  *              @prefix@ = prefix to scan for
152  *              @desc@ = description of what we're parsing
153  *
154  * Use:         Bracket an options parsing routine.  The current token is
155  *              checked to see whether it matches the prefix.  If so, it is
156  *              removed and the following token examined.  If that's a `.'
157  *              then it's removed.  If it's a `{' then the enclosed
158  *              option-parsing code is executed in a loop until a matching
159  *              '}' is found.  If the options parser doesn't accept an
160  *              option, the behaviour is dependent on whether a prefix was
161  *              seen: if so, an error is reported; otherwse a zero return is
162  *              made.
163  */
164
165 #define CS_PLAIN 0
166 #define CS_PREFIX 1
167 #define CS_BRACE 2
168 #define CS_UNKNOWN 3
169
170 #define CONF_BEGIN(sc, prefix, desc) do {                               \
171   scanner *_conf_sc = (sc);                                             \
172   const char *_conf_desc = (desc);                                      \
173   int _conf_state = CS_PLAIN;                                           \
174                                                                         \
175   /* --- Read the initial prefix --- */                                 \
176                                                                         \
177   if (_conf_sc->t == CTOK_WORD &&                                       \
178       strcmp(_conf_sc->d.buf, (prefix)) == 0) {                         \
179     token(_conf_sc);                                                    \
180     _conf_state = CS_PREFIX;                                            \
181     if (_conf_sc->t == '.')                                             \
182       token(_conf_sc);                                                  \
183     else if (_conf_sc->t == '{') {                                      \
184       token(_conf_sc);                                                  \
185       _conf_state = CS_BRACE;                                           \
186     }                                                                   \
187   }                                                                     \
188                                                                         \
189   /* --- Ensure the next token is a word --- */                         \
190                                                                         \
191   if (_conf_sc->t != CTOK_WORD)                                         \
192     error(_conf_sc, "parse error, expected option keyword");            \
193   do {
194
195 #define CONF_END                                                        \
196                                                                         \
197     /* --- Reject an option --- *                                       \
198      *                                                                  \
199      * We could get here as a result of an explicit @CONF_REJECT@ or    \
200      * because the option wasn't accepted.                              \
201      */                                                                 \
202                                                                         \
203     goto _conf_reject;                                                  \
204   _conf_reject:                                                         \
205     if (_conf_state == CS_PLAIN)                                        \
206       _conf_state = CS_UNKNOWN;                                         \
207     else {                                                              \
208       error(_conf_sc, "unknown %s option `%s'",                         \
209             _conf_desc, _conf_sc->d.buf);                               \
210     }                                                                   \
211                                                                         \
212     /* --- Accept an option --- *                                       \
213      *                                                                  \
214      * It's safe to drop through from above.  Either an error will have \
215      * been reported, or the state is not @CS_BRACE@.                   \
216      */                                                                 \
217                                                                         \
218   _conf_accept:                                                         \
219     if (_conf_state == CS_BRACE && _conf_sc->t == ';')                  \
220       token(_conf_sc);                                                  \
221   } while (_conf_state == CS_BRACE && _conf_sc->t == CTOK_WORD);        \
222                                                                         \
223   /* --- Check for a closing brace --- */                               \
224                                                                         \
225   if (_conf_state == CS_BRACE) {                                        \
226     if (_conf_sc->t == '}')                                             \
227       token(_conf_sc);                                                  \
228     else                                                                \
229       error(_conf_sc, "parse error, expected `}'");                     \
230   }                                                                     \
231                                                                         \
232   /* --- Return an appropriate value --- */                             \
233                                                                         \
234   return (_conf_state != CS_UNKNOWN);                                   \
235 } while (0)
236
237 /* --- @CONF_ACCEPT@, @CONF_REJECT@ --- *
238  *
239  * Arguments:   ---
240  *
241  * Use:         Within an options parser (between @CONF_BEGIN@ and
242  *              @CONF_END@), accept or reject an option.
243  */
244
245 #define CONF_ACCEPT goto _conf_accept
246 #define CONF_REJECT goto _conf_reject
247
248 /* --- @CONF_QUAL@ --- *
249  *
250  * Arguments:   ---
251  *
252  * Use:         Evaluates to a nonzero value if the current option is
253  *              qualified.  This can be used to decide whether abbreviations
254  *              for options should be accepted.
255  */
256
257 #define CONF_QUAL (_conf_state != CS_PLAIN)
258
259 /* --- @conf_name@ --- *
260  *
261  * Arguments:   @scanner *sc@ = pointer to scanner
262  *              @char delim@ = delimiter character to look for
263  *              @dstr *d@ = pointer to dynamic string for output
264  *
265  * Returns:     ---
266  *
267  * Use:         Reads in a compound name consisting of words separated by
268  *              delimiters.  Leading and trailing delimiters are permitted,
269  *              although they'll probably cause confusion if used.  The name
270  *              may be enclosed in square brackets if that helps at all.
271  *
272  *              Examples of compound names are filenames (delimited by `/')
273  *              and IP addresses (delimited by `.').
274  */
275
276 extern void conf_name(scanner */*sc*/, char /*delim*/, dstr */*d*/);
277
278 /*----- That's all, folks -------------------------------------------------*/
279
280 #ifdef __cplusplus
281   }
282 #endif
283
284 #endif