| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: conf.c,v 1.10 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.c,v $ |
| 32 | * Revision 1.10 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.9 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.8 2001/02/03 20:33:26 mdw |
| 41 | * Fix flags to be unsigned. |
| 42 | * |
| 43 | * Revision 1.7 2000/08/01 17:58:10 mdw |
| 44 | * Fix subtleties with <ctype.h> functions. |
| 45 | * |
| 46 | * Revision 1.6 2000/02/12 18:13:20 mdw |
| 47 | * Terminate tables of sources and targets. |
| 48 | * |
| 49 | * Revision 1.5 1999/10/22 22:46:44 mdw |
| 50 | * Improve documentation for conf_enum. |
| 51 | * |
| 52 | * Revision 1.4 1999/10/15 21:12:36 mdw |
| 53 | * Remove redundant debugging code. |
| 54 | * |
| 55 | * Revision 1.3 1999/08/19 18:32:48 mdw |
| 56 | * Improve lexical analysis. In particular, `chmod' patterns don't have to |
| 57 | * be quoted any more. |
| 58 | * |
| 59 | * Revision 1.2 1999/07/26 23:28:39 mdw |
| 60 | * Major reconstruction work for new design. |
| 61 | * |
| 62 | * Revision 1.1.1.1 1999/07/01 08:56:23 mdw |
| 63 | * Initial revision. |
| 64 | * |
| 65 | */ |
| 66 | |
| 67 | /*----- Header files ------------------------------------------------------*/ |
| 68 | |
| 69 | #include <ctype.h> |
| 70 | #include <errno.h> |
| 71 | #include <stdarg.h> |
| 72 | #include <stdio.h> |
| 73 | #include <stdlib.h> |
| 74 | #include <string.h> |
| 75 | |
| 76 | #include <mLib/dstr.h> |
| 77 | #include <mLib/quis.h> |
| 78 | #include <mLib/report.h> |
| 79 | |
| 80 | #include "conf.h" |
| 81 | #include "scan.h" |
| 82 | |
| 83 | /*----- Main code ---------------------------------------------------------*/ |
| 84 | |
| 85 | /* --- @conf_undelim@ --- * |
| 86 | * |
| 87 | * Arguments: @scanner *sc@ = pointer to scanner definition |
| 88 | * @const char *d, *dd@ = pointer to characters to escape |
| 89 | * |
| 90 | * Returns: --- |
| 91 | * |
| 92 | * Use: Modifies the tokenizer. Characters in the first list will |
| 93 | * always be considered to begin a word. Characters in the |
| 94 | * second list will always be allowed to continue a word. |
| 95 | */ |
| 96 | |
| 97 | void conf_undelim(scanner *sc, const char *d, const char *dd) |
| 98 | { |
| 99 | sc->wbegin = d; |
| 100 | sc->wcont = dd; |
| 101 | } |
| 102 | |
| 103 | /* --- @token@ --- * |
| 104 | * |
| 105 | * Arguments: @scanner *sc@ = pointer to scanner definition |
| 106 | * |
| 107 | * Returns: Type of token scanned. |
| 108 | * |
| 109 | * Use: Reads the next token from the character scanner. |
| 110 | */ |
| 111 | |
| 112 | int token(scanner *sc) |
| 113 | { |
| 114 | #define SELFDELIM \ |
| 115 | '{': case '}': case '/': case ',': \ |
| 116 | case '=': case ':': case ';': \ |
| 117 | case '.': case '[': case ']' |
| 118 | |
| 119 | int ch; |
| 120 | |
| 121 | DRESET(&sc->d); |
| 122 | |
| 123 | /* --- Main tokenization --- */ |
| 124 | |
| 125 | for (;;) { |
| 126 | ch = scan(sc); |
| 127 | |
| 128 | /* --- Deal with pushed-back tokens --- */ |
| 129 | |
| 130 | if (sc->head->tok) { |
| 131 | dstr_puts(&sc->d, sc->head->tok); |
| 132 | xfree(sc->head->tok); |
| 133 | sc->head->tok = 0; |
| 134 | sc->t = sc->head->t; |
| 135 | goto done; |
| 136 | } |
| 137 | |
| 138 | else if (isspace(ch)) |
| 139 | ; |
| 140 | else switch (ch) { |
| 141 | |
| 142 | /* --- End of file --- */ |
| 143 | |
| 144 | case EOF: |
| 145 | sc->t = CTOK_EOF; |
| 146 | goto done; |
| 147 | |
| 148 | /* --- Comment character --- */ |
| 149 | |
| 150 | case '#': |
| 151 | do ch = scan(sc); while (ch != EOF && ch != '\n'); |
| 152 | break; |
| 153 | |
| 154 | /* --- Various self-delimiting characters --- */ |
| 155 | |
| 156 | case SELFDELIM: |
| 157 | if (!sc->wbegin || strchr(sc->wbegin, ch) == 0) { |
| 158 | dstr_putc(&sc->d, ch); |
| 159 | dstr_putz(&sc->d); |
| 160 | sc->t = ch; |
| 161 | goto done; |
| 162 | } |
| 163 | |
| 164 | /* --- Bare words --- * |
| 165 | * |
| 166 | * These aren't as bare any more. You can now backslash-escape |
| 167 | * individual characters, and enclose sections in double-quotes. |
| 168 | */ |
| 169 | |
| 170 | default: { |
| 171 | int q = 0; |
| 172 | |
| 173 | for (;;) { |
| 174 | switch (ch) { |
| 175 | case EOF: |
| 176 | goto word; |
| 177 | case '\\': |
| 178 | ch = scan(sc); |
| 179 | if (ch == EOF) |
| 180 | goto word; |
| 181 | DPUTC(&sc->d, ch); |
| 182 | break; |
| 183 | case '\"': |
| 184 | q = !q; |
| 185 | break; |
| 186 | case SELFDELIM: |
| 187 | if (q || (sc->wcont && strchr(sc->wcont, ch))) |
| 188 | goto insert; |
| 189 | goto word; |
| 190 | default: |
| 191 | if (!q && isspace(ch)) |
| 192 | goto word; |
| 193 | insert: |
| 194 | DPUTC(&sc->d, ch); |
| 195 | break; |
| 196 | } |
| 197 | ch = scan(sc); |
| 198 | } |
| 199 | word: |
| 200 | unscan(sc, ch); |
| 201 | DPUTZ(&sc->d); |
| 202 | sc->t = CTOK_WORD; |
| 203 | goto done; |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | done: |
| 209 | return (sc->t); |
| 210 | } |
| 211 | |
| 212 | /* --- @pushback@ --- * |
| 213 | * |
| 214 | * Arguments: @scanner *sc@ = pointer to scanner definition |
| 215 | * |
| 216 | * Returns: --- |
| 217 | * |
| 218 | * Use: Pushes the current token back. This is normally a precursor |
| 219 | * to pushing a new scanner source. |
| 220 | */ |
| 221 | |
| 222 | void pushback(scanner *sc) |
| 223 | { |
| 224 | sc->head->tok = xstrdup(sc->d.buf); |
| 225 | sc->head->t = sc->t; |
| 226 | } |
| 227 | |
| 228 | /* --- @error@ --- * |
| 229 | * |
| 230 | * Arguments: @scanner *sc@ = pointer to scanner definition |
| 231 | * @const char *msg@ = message skeleton string |
| 232 | * @...@ = extra arguments for the skeleton |
| 233 | * |
| 234 | * Returns: Doesn't |
| 235 | * |
| 236 | * Use: Reports an error at the current scanner location. |
| 237 | */ |
| 238 | |
| 239 | void error(scanner *sc, const char *msg, ...) |
| 240 | { |
| 241 | va_list ap; |
| 242 | va_start(ap, msg); |
| 243 | fprintf(stderr, "%s: %s:%i: ", QUIS, sc->head->src, sc->head->line); |
| 244 | vfprintf(stderr, msg, ap); |
| 245 | fputc('\n', stderr); |
| 246 | exit(1); |
| 247 | } |
| 248 | |
| 249 | /* --- @conf_enum@ --- * |
| 250 | * |
| 251 | * Arguments: @scanner *sc@ = pointer to a scanner object |
| 252 | * @const char *list@ = comma-separated things to allow |
| 253 | * @unsigned f@ = flags for the search |
| 254 | * @const char *err@ = error message if not found |
| 255 | * |
| 256 | * Returns: Index into list, zero-based, or @-1@. |
| 257 | * |
| 258 | * Use: Checks whether the current token is a string which matches |
| 259 | * one of the comma-separated items given. The return value is |
| 260 | * the index (zero-based) of the matched string in the list. |
| 261 | * |
| 262 | * The flags control the behaviour if no exact match is found. |
| 263 | * If @ENUM_ABBREV@ is set, and the current token is a left |
| 264 | * substring of exactly one of the possibilities, then that one |
| 265 | * is chosen. If @ENUM_NONE@ is set, the value @-1@ is |
| 266 | * returned; otherwise an error is reported and the program is |
| 267 | * terminated. |
| 268 | */ |
| 269 | |
| 270 | int conf_enum(scanner *sc, const char *list, unsigned f, const char *err) |
| 271 | { |
| 272 | const char *p, *q; |
| 273 | int chosen = -1; |
| 274 | int ok; |
| 275 | int index; |
| 276 | |
| 277 | /* --- Make sure it's a string --- */ |
| 278 | |
| 279 | if (sc->t != CTOK_WORD) |
| 280 | error(sc, "parse error, expected %s", err); |
| 281 | |
| 282 | /* --- Grind through the list --- */ |
| 283 | |
| 284 | q = sc->d.buf; |
| 285 | ok = 1; |
| 286 | index = 0; |
| 287 | p = list; |
| 288 | for (;;) { |
| 289 | switch (*p) { |
| 290 | case 0: |
| 291 | if (ok && !*q) { |
| 292 | token(sc); |
| 293 | return (index); |
| 294 | } else if (chosen != -1) { |
| 295 | token(sc); |
| 296 | return (chosen); |
| 297 | } |
| 298 | else if (f & ENUM_NONE) |
| 299 | return (-1); |
| 300 | else |
| 301 | error(sc, "unknown %s `%s'", err, sc->d.buf); |
| 302 | break; |
| 303 | case ',': |
| 304 | if (ok && !*q) { |
| 305 | token(sc); |
| 306 | return (index); |
| 307 | } |
| 308 | ok = 1; |
| 309 | q = sc->d.buf; |
| 310 | index++; |
| 311 | break; |
| 312 | default: |
| 313 | if (!ok) |
| 314 | break; |
| 315 | if ((f & ENUM_ABBREV) && !*q) { |
| 316 | if (chosen != -1) |
| 317 | error(sc, "ambiguous %s `%s'", err, sc->d.buf); |
| 318 | chosen = index; |
| 319 | ok = 0; |
| 320 | } |
| 321 | if (*p == *q) |
| 322 | q++; |
| 323 | else |
| 324 | ok = 0; |
| 325 | break; |
| 326 | } |
| 327 | p++; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /* --- @conf_prefix@ --- * |
| 332 | * |
| 333 | * Arguments: @scanner *sc@ = pointer to a scanner object |
| 334 | * @const char *p@ = pointer to prefix string to check |
| 335 | * |
| 336 | * Returns: Nonzero if the prefix matches. |
| 337 | * |
| 338 | * Use: If the current token is a word matching the given prefix |
| 339 | * string, then it and an optional `.' character are removed and |
| 340 | * a nonzero result is returned. Otherwise the current token is |
| 341 | * left as it is, and zero is returned. |
| 342 | * |
| 343 | * Typical options parsing code would remove an expected prefix, |
| 344 | * scan an option anyway (since qualifying prefixes are |
| 345 | * optional) and if a match is found, claim the option. If no |
| 346 | * match is found, and a prefix was stripped, then an error |
| 347 | * should be reported. |
| 348 | */ |
| 349 | |
| 350 | int conf_prefix(scanner *sc, const char *p) |
| 351 | { |
| 352 | if (sc->t == CTOK_WORD && strcmp(p, sc->d.buf) == 0) { |
| 353 | token(sc); |
| 354 | if (sc->t == '.') |
| 355 | token(sc); |
| 356 | return (1); |
| 357 | } |
| 358 | return (0); |
| 359 | } |
| 360 | |
| 361 | /* --- @conf_name@ --- * |
| 362 | * |
| 363 | * Arguments: @scanner *sc@ = pointer to scanner |
| 364 | * @char delim@ = delimiter character to look for |
| 365 | * @dstr *d@ = pointer to dynamic string for output |
| 366 | * |
| 367 | * Returns: --- |
| 368 | * |
| 369 | * Use: Reads in a compound name consisting of words separated by |
| 370 | * delimiters. Leading and trailing delimiters are permitted, |
| 371 | * although they'll probably cause confusion if used. The name |
| 372 | * may be enclosed in square brackets if that helps at all. |
| 373 | * |
| 374 | * Examples of compound names are filenames (delimited by `/') |
| 375 | * and IP addresses (delimited by `.'). |
| 376 | */ |
| 377 | |
| 378 | void conf_name(scanner *sc, char delim, dstr *d) |
| 379 | { |
| 380 | unsigned f = 0; |
| 381 | |
| 382 | #define f_ok 1u |
| 383 | #define f_bra 2u |
| 384 | |
| 385 | /* --- Read an optional opening bracket --- */ |
| 386 | |
| 387 | if (sc->t == '[') { |
| 388 | token(sc); |
| 389 | f |= f_bra | f_ok; |
| 390 | } |
| 391 | |
| 392 | /* --- Do the main reading sequence --- */ |
| 393 | |
| 394 | do { |
| 395 | if (sc->t == delim) { |
| 396 | DPUTC(d, delim); |
| 397 | f |= f_ok; |
| 398 | token(sc); |
| 399 | } |
| 400 | if (sc->t == CTOK_WORD) { |
| 401 | DPUTD(d, &sc->d); |
| 402 | f |= f_ok; |
| 403 | token(sc); |
| 404 | } |
| 405 | } while (sc->t == delim); |
| 406 | |
| 407 | /* --- Check that the string was OK --- */ |
| 408 | |
| 409 | if (!(f & f_ok)) |
| 410 | error(sc, "parse error, name expected"); |
| 411 | |
| 412 | /* --- Read a closing bracket --- */ |
| 413 | |
| 414 | if (f & f_bra) { |
| 415 | if (sc->t == ']') |
| 416 | token(sc); |
| 417 | else |
| 418 | error(sc, "parse error, missing `]'"); |
| 419 | } |
| 420 | DPUTZ(d); |
| 421 | |
| 422 | #undef f_ok |
| 423 | #undef f_bra |
| 424 | } |
| 425 | |
| 426 | /*----- That's all, folks -------------------------------------------------*/ |