chiark / gitweb /
Major reconstruction work for new design.
[fwd] / conf.h
1 /* -*-c-*-
2  *
3  * $Id: conf.h,v 1.2 1999/07/26 23:28:39 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.2  1999/07/26 23:28:39  mdw
33  * Major reconstruction work for new design.
34  *
35  * Revision 1.1.1.1  1999/07/01 08:56:23  mdw
36  * Initial revision.
37  *
38  */
39
40 #ifndef CONF_H
41 #define CONF_H
42
43 #ifdef __cplusplus
44   extern "C" {
45 #endif
46
47 /*----- Header files ------------------------------------------------------*/
48
49 #include <mLib/dstr.h>
50
51 #ifndef SCAN_H
52 #  include "scan.h"
53 #endif
54
55 /*----- Magic numbers -----------------------------------------------------*/
56
57 #define CTOK_EOF (-1)
58 #define CTOK_WORD 256
59
60 /*----- Functions provided ------------------------------------------------*/
61
62 /* --- @token@ --- *
63  *
64  * Arguments:   @scanner *sc@ = pointer to scanner definition
65  *
66  * Returns:     Type of token scanned.
67  *
68  * Use:         Reads the next token from the character scanner.
69  */
70
71 extern int token(scanner */*sc*/);
72
73 /* --- @error@ --- *
74  *
75  * Arguments:   @scanner *sc@ = pointer to scanner definition
76  *              @const char *msg@ = message skeleton string
77  *              @...@ = extra arguments for the skeleton
78  *
79  * Returns:     Doesn't
80  *
81  * Use:         Reports an error at the current scanner location.
82  */
83
84 extern void error(scanner */*sc*/, const char */*msg*/, ...);
85
86 /* --- @conf_enum@ --- *
87  *
88  * Arguments:   @scanner *sc@ = pointer to a scanner object
89  *              @const char *list@ = comma-separated things to allow
90  *              @unsigned @f = flags for the search
91  *              @const char *err@ = error message if not found
92  *
93  * Returns:     Index into list, zero-based, or @-1@.
94  *
95  * Use:         Checks whether the current token is a string which matches
96  *              one of the comma-separated items given.  If not, an error is
97  *              reported; otherwise the index of the matched item is
98  *              returned.
99  */
100
101 #define ENUM_ABBREV 1u
102 #define ENUM_NONE 2u
103
104 extern int conf_enum(scanner */*sc*/, const char */*list*/,
105                      unsigned /*flags*/, const char */*err*/);
106
107 /* --- @conf_prefix@ --- *
108  *
109  * Arguments:   @scanner *sc@ = pointer to a scanner object
110  *              @const char *p@ = pointer to prefix string to check
111  *
112  * Returns:     Nonzero if the prefix matches.
113  *
114  * Use:         If the current token is a word matching the given prefix
115  *              string, then it and an optional `.' character are removed and
116  *              a nonzero result is returned.  Otherwise the current token is
117  *              left as it is, and zero is returned.
118  *
119  *              Typical options parsing code would remove an expected prefix,
120  *              scan an option anyway (since qualifying prefixes are
121  *              optional) and if a match is found, claim the option.  If no
122  *              match is found, and a prefix was stripped, then an error
123  *              should be reported.
124  */
125
126 extern int conf_prefix(scanner */*sc*/, const char */*p*/);
127
128 /* --- @CONF_BEGIN@, @CONF_ACCEPT@, @CONF_END@ --- *
129  *
130  * Arguments:   @sc@ = scanner to read from
131  *              @prefix@ = prefix to scan for
132  *              @desc@ = description of what we're parsing
133  *
134  * Use:         Bracket an options parsing routine.
135  */
136
137 #define CS_PLAIN 0
138 #define CS_PREFIX 1
139 #define CS_BRACE 2
140 #define CS_UNKNOWN 3
141
142 #define CONF_BEGIN(sc, prefix, desc) do {                               \
143   scanner *_conf_sc = (sc);                                             \
144   const char *_conf_desc = (desc);                                      \
145   int _conf_state = CS_PLAIN;                                           \
146   if (_conf_sc->t == CTOK_WORD &&                                       \
147       strcmp(_conf_sc->d.buf, (prefix)) == 0) {                         \
148     token(_conf_sc);                                                    \
149     _conf_state = CS_PREFIX;                                            \
150     if (_conf_sc->t == '.')                                             \
151       token(_conf_sc);                                                  \
152     else if (_conf_sc->t == '{') {                                      \
153       token(_conf_sc);                                                  \
154       _conf_state = CS_BRACE;                                           \
155     }                                                                   \
156   }                                                                     \
157   if (_conf_sc->t != CTOK_WORD)                                         \
158     error(_conf_sc, "parse error, expected option keyword");            \
159   do {
160
161 #define CONF_ACCEPT goto _conf_accept
162 #define CONF_REJECT goto _conf_reject
163 #define CONF_QUAL (_conf_state != CS_PLAIN)
164
165 #define CONF_END                                                        \
166   _conf_reject:                                                         \
167     if (_conf_state == CS_PLAIN)                                        \
168       _conf_state = CS_UNKNOWN;                                         \
169     else {                                                              \
170       error(_conf_sc, "unknown %s option `%s'",                         \
171             _conf_desc, _conf_sc->d.buf);                               \
172     }                                                                   \
173   _conf_accept:                                                         \
174     if (_conf_state == CS_BRACE && _conf_sc->t == ';')                  \
175       token(_conf_sc);                                                  \
176   } while (_conf_state == CS_BRACE && _conf_sc->t == CTOK_WORD);        \
177   if (_conf_state == CS_BRACE) {                                        \
178     if (_conf_sc->t == '}')                                             \
179       token(_conf_sc);                                                  \
180     else                                                                \
181       error(_conf_sc, "parse error, expected `}'");                     \
182   }                                                                     \
183   return (_conf_state != CS_UNKNOWN);                                   \
184 } while (0)
185
186 /* --- @conf_name@ --- *
187  *
188  * Arguments:   @scanner *sc@ = pointer to scanner
189  *              @char delim@ = delimiter character to look for
190  *              @dstr *d@ = pointer to dynamic string for output
191  *
192  * Returns:     ---
193  *
194  * Use:         Reads in a compound name consisting of words separated by
195  *              delimiters.  Leading and trailing delimiters are permitted,
196  *              although they'll probably cause confusion if used.  The name
197  *              may be enclosed in square brackets if that helps at all.
198  *
199  *              Examples of compound names are filenames (delimited by `/')
200  *              and IP addresses (delimited by `.').
201  */
202
203 extern void conf_name(scanner */*sc*/, char /*delim*/, dstr */*d*/);
204
205 /* --- @conf_parse@ --- *
206  *
207  * Arguments:   @scanner *sc@ = pointer to a scanner structure
208  *
209  * Returns:     ---
210  *
211  * Use:         Parses a configuration file fragment from the scanner
212  */
213
214 extern void conf_parse(scanner *sc);
215
216 /*----- That's all, folks -------------------------------------------------*/
217
218 #ifdef __cplusplus
219   }
220 #endif
221
222 #endif