3 * $Id: conf.h,v 1.2 1999/07/26 23:28:39 mdw Exp $
5 * Configuration parsing
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the `fw' port forwarder.
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.2 1999/07/26 23:28:39 mdw
33 * Major reconstruction work for new design.
35 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
47 /*----- Header files ------------------------------------------------------*/
49 #include <mLib/dstr.h>
55 /*----- Magic numbers -----------------------------------------------------*/
60 /*----- Functions provided ------------------------------------------------*/
64 * Arguments: @scanner *sc@ = pointer to scanner definition
66 * Returns: Type of token scanned.
68 * Use: Reads the next token from the character scanner.
71 extern int token(scanner */
*sc*/
);
75 * Arguments: @scanner *sc@ = pointer to scanner definition
76 * @const char *msg@ = message skeleton string
77 * @...@ = extra arguments for the skeleton
81 * Use: Reports an error at the current scanner location.
84 extern void error(scanner */
*sc*/
, const char */
*msg*/
, ...);
86 /* --- @conf_enum@ --- *
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
93 * Returns: Index into list, zero-based, or @-1@.
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
101 #define ENUM_ABBREV 1u
104 extern int conf_enum(scanner */
*sc*/
, const char */
*list*/
,
105 unsigned /*flags*/, const char */
*err*/
);
107 /* --- @conf_prefix@ --- *
109 * Arguments: @scanner *sc@ = pointer to a scanner object
110 * @const char *p@ = pointer to prefix string to check
112 * Returns: Nonzero if the prefix matches.
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.
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.
126 extern int conf_prefix(scanner */
*sc*/
, const char */
*p*/
);
128 /* --- @CONF_BEGIN@, @CONF_ACCEPT@, @CONF_END@ --- *
130 * Arguments: @sc@ = scanner to read from
131 * @prefix@ = prefix to scan for
132 * @desc@ = description of what we're parsing
134 * Use: Bracket an options parsing routine.
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) { \
149 _conf_state = CS_PREFIX; \
150 if (_conf_sc->t == '.') \
152 else if (_conf_sc->t == '{') { \
154 _conf_state = CS_BRACE; \
157 if (_conf_sc->t != CTOK_WORD) \
158 error(_conf_sc, "parse error, expected option keyword"); \
161 #define CONF_ACCEPT goto _conf_accept
162 #define CONF_REJECT goto _conf_reject
163 #define CONF_QUAL (_conf_state != CS_PLAIN)
167 if (_conf_state == CS_PLAIN) \
168 _conf_state = CS_UNKNOWN; \
170 error(_conf_sc, "unknown %s option `%s'", \
171 _conf_desc, _conf_sc->d.buf); \
174 if (_conf_state == CS_BRACE && _conf_sc->t == ';') \
176 } while (_conf_state == CS_BRACE && _conf_sc->t == CTOK_WORD); \
177 if (_conf_state == CS_BRACE) { \
178 if (_conf_sc->t == '}') \
181 error(_conf_sc, "parse error, expected `}'"); \
183 return (_conf_state != CS_UNKNOWN); \
186 /* --- @conf_name@ --- *
188 * Arguments: @scanner *sc@ = pointer to scanner
189 * @char delim@ = delimiter character to look for
190 * @dstr *d@ = pointer to dynamic string for output
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.
199 * Examples of compound names are filenames (delimited by `/')
200 * and IP addresses (delimited by `.').
203 extern void conf_name(scanner */
*sc*/
, char /*delim*/, dstr */
*d*/
);
205 /* --- @conf_parse@ --- *
207 * Arguments: @scanner *sc@ = pointer to a scanner structure
211 * Use: Parses a configuration file fragment from the scanner
214 extern void conf_parse(scanner
*sc
);
216 /*----- That's all, folks -------------------------------------------------*/