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