Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * config file parser helper |
| 4 | * |
| 5 | * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com> |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Bernhard Reutner-Fischer | f3b39a2 | 2009-02-23 16:21:53 +0000 | [diff] [blame] | 8 | * Also for use in uClibc (http://uclibc.org/) licensed under LGPLv2.1 or later. |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Denys Vlasenko | 3a64936 | 2011-06-18 09:23:09 +0200 | [diff] [blame] | 11 | /* Uncomment to enable test applet */ |
| 12 | ////config:config PARSE |
| 13 | ////config: bool "Uniform config file parser debugging applet: parse" |
| 14 | ////config: default n |
| 15 | ////config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 16 | ////config: Typical usage of parse API: |
Denys Vlasenko | 3a64936 | 2011-06-18 09:23:09 +0200 | [diff] [blame] | 17 | ////config: char *t[3]; |
| 18 | ////config: parser_t *p = config_open(filename); |
| 19 | ////config: while (config_read(p, t, 3, 0, delimiters, flags)) { // 1..3 tokens |
| 20 | ////config: bb_error_msg("TOKENS: '%s''%s''%s'", t[0], t[1], t[2]); |
| 21 | ////config: } |
| 22 | ////config: config_close(p); |
| 23 | |
| 24 | ////applet:IF_PARSE(APPLET(parse, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 25 | |
| 26 | //kbuild:lib-y += parse_config.o |
| 27 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 28 | //usage:#define parse_trivial_usage |
Denys Vlasenko | 3a64936 | 2011-06-18 09:23:09 +0200 | [diff] [blame] | 29 | //usage: "[-x] [-n MAXTOKENS] [-m MINTOKENS] [-d DELIMS] [-f FLAGS] FILE..." |
| 30 | //usage:#define parse_full_usage "\n\n" |
| 31 | //usage: " -x Suppress output (for benchmarking)" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 32 | |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 33 | #include "libbb.h" |
| 34 | |
Denis Vlasenko | 2d5bd80 | 2008-10-24 10:49:49 +0000 | [diff] [blame] | 35 | #if defined ENABLE_PARSE && ENABLE_PARSE |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 36 | int parse_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 37 | int parse_main(int argc UNUSED_PARAM, char **argv) |
| 38 | { |
| 39 | const char *delims = "# \t"; |
Denys Vlasenko | 3a64936 | 2011-06-18 09:23:09 +0200 | [diff] [blame] | 40 | char **t; |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 41 | unsigned flags = PARSE_NORMAL; |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 42 | int mintokens = 0, ntokens = 128; |
Denys Vlasenko | 3a64936 | 2011-06-18 09:23:09 +0200 | [diff] [blame] | 43 | unsigned noout; |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 44 | |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame^] | 45 | noout = 1 & getopt32(argv, "^" "xn:+m:+d:f:+" "\0" "-1", |
| 46 | &ntokens, &mintokens, &delims, &flags |
| 47 | ); |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 48 | //argc -= optind; |
| 49 | argv += optind; |
Denys Vlasenko | 3a64936 | 2011-06-18 09:23:09 +0200 | [diff] [blame] | 50 | |
| 51 | t = xmalloc(sizeof(t[0]) * ntokens); |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 52 | while (*argv) { |
Denys Vlasenko | 3a64936 | 2011-06-18 09:23:09 +0200 | [diff] [blame] | 53 | int n; |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 54 | parser_t *p = config_open(*argv); |
Denys Vlasenko | 3a64936 | 2011-06-18 09:23:09 +0200 | [diff] [blame] | 55 | while ((n = config_read(p, t, ntokens, mintokens, delims, flags)) != 0) { |
| 56 | if (!noout) { |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 57 | for (int i = 0; i < n; ++i) |
| 58 | printf("[%s]", t[i]); |
| 59 | puts(""); |
| 60 | } |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 61 | } |
Denys Vlasenko | 3a64936 | 2011-06-18 09:23:09 +0200 | [diff] [blame] | 62 | config_close(p); |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 63 | argv++; |
| 64 | } |
| 65 | return EXIT_SUCCESS; |
| 66 | } |
| 67 | #endif |
| 68 | |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 69 | parser_t* FAST_FUNC config_open2(const char *filename, FILE* FAST_FUNC (*fopen_func)(const char *path)) |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 70 | { |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 71 | FILE* fp; |
| 72 | parser_t *parser; |
| 73 | |
| 74 | fp = fopen_func(filename); |
| 75 | if (!fp) |
| 76 | return NULL; |
| 77 | parser = xzalloc(sizeof(*parser)); |
| 78 | parser->fp = fp; |
| 79 | return parser; |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 82 | parser_t* FAST_FUNC config_open(const char *filename) |
| 83 | { |
| 84 | return config_open2(filename, fopen_or_warn_stdin); |
| 85 | } |
| 86 | |
Bernhard Reutner-Fischer | 6792128 | 2008-07-17 11:59:13 +0000 | [diff] [blame] | 87 | void FAST_FUNC config_close(parser_t *parser) |
| 88 | { |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 89 | if (parser) { |
Timo Teras | adcabf3 | 2011-06-20 09:49:56 +0200 | [diff] [blame] | 90 | if (PARSE_KEEP_COPY) /* compile-time constant */ |
| 91 | free(parser->data); |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 92 | fclose(parser->fp); |
Timo Teras | adcabf3 | 2011-06-20 09:49:56 +0200 | [diff] [blame] | 93 | free(parser->line); |
| 94 | free(parser->nline); |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 95 | free(parser); |
| 96 | } |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Timo Teras | adcabf3 | 2011-06-20 09:49:56 +0200 | [diff] [blame] | 99 | /* This function reads an entire line from a text file, |
| 100 | * up to a newline, exclusive. |
Denys Vlasenko | a1a4483 | 2011-06-17 03:37:43 +0200 | [diff] [blame] | 101 | * Trailing '\' is recognized as line continuation. |
Timo Teras | adcabf3 | 2011-06-20 09:49:56 +0200 | [diff] [blame] | 102 | * Returns -1 if EOF/error. |
Denys Vlasenko | a1a4483 | 2011-06-17 03:37:43 +0200 | [diff] [blame] | 103 | */ |
Timo Teras | adcabf3 | 2011-06-20 09:49:56 +0200 | [diff] [blame] | 104 | static int get_line_with_continuation(parser_t *parser) |
Denys Vlasenko | a1a4483 | 2011-06-17 03:37:43 +0200 | [diff] [blame] | 105 | { |
Timo Teras | adcabf3 | 2011-06-20 09:49:56 +0200 | [diff] [blame] | 106 | ssize_t len, nlen; |
| 107 | char *line; |
Denys Vlasenko | a1a4483 | 2011-06-17 03:37:43 +0200 | [diff] [blame] | 108 | |
Timo Teras | adcabf3 | 2011-06-20 09:49:56 +0200 | [diff] [blame] | 109 | len = getline(&parser->line, &parser->line_alloc, parser->fp); |
| 110 | if (len <= 0) |
| 111 | return len; |
| 112 | |
| 113 | line = parser->line; |
| 114 | for (;;) { |
| 115 | parser->lineno++; |
| 116 | if (line[len - 1] == '\n') |
| 117 | len--; |
| 118 | if (len == 0 || line[len - 1] != '\\') |
| 119 | break; |
| 120 | len--; |
| 121 | |
| 122 | nlen = getline(&parser->nline, &parser->nline_alloc, parser->fp); |
| 123 | if (nlen <= 0) |
| 124 | break; |
| 125 | |
| 126 | if (parser->line_alloc < len + nlen + 1) { |
| 127 | parser->line_alloc = len + nlen + 1; |
| 128 | line = parser->line = xrealloc(line, parser->line_alloc); |
Denys Vlasenko | a1a4483 | 2011-06-17 03:37:43 +0200 | [diff] [blame] | 129 | } |
Timo Teras | adcabf3 | 2011-06-20 09:49:56 +0200 | [diff] [blame] | 130 | memcpy(&line[len], parser->nline, nlen); |
| 131 | len += nlen; |
Denys Vlasenko | a1a4483 | 2011-06-17 03:37:43 +0200 | [diff] [blame] | 132 | } |
Timo Teras | adcabf3 | 2011-06-20 09:49:56 +0200 | [diff] [blame] | 133 | |
| 134 | line[len] = '\0'; |
| 135 | return len; |
Denys Vlasenko | a1a4483 | 2011-06-17 03:37:43 +0200 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 139 | /* |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 140 | 0. If parser is NULL return 0. |
| 141 | 1. Read a line from config file. If nothing to read then return 0. |
| 142 | Handle continuation character. Advance lineno for each physical line. |
Denys Vlasenko | 5370bfb | 2009-09-06 02:58:59 +0200 | [diff] [blame] | 143 | Discard everything past comment character. |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 144 | 2. if PARSE_TRIM is set (default), remove leading and trailing delimiters. |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 145 | 3. If resulting line is empty goto 1. |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 146 | 4. Look for first delimiter. If !PARSE_COLLAPSE or !PARSE_TRIM is set then |
| 147 | remember the token as empty. |
| 148 | 5. Else (default) if number of seen tokens is equal to max number of tokens |
| 149 | (token is the last one) and PARSE_GREEDY is set then the remainder |
| 150 | of the line is the last token. |
| 151 | Else (token is not last or PARSE_GREEDY is not set) just replace |
| 152 | first delimiter with '\0' thus delimiting the token. |
| 153 | 6. Advance line pointer past the end of token. If number of seen tokens |
| 154 | is less than required number of tokens then goto 4. |
| 155 | 7. Check the number of seen tokens is not less the min number of tokens. |
| 156 | Complain or die otherwise depending on PARSE_MIN_DIE. |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 157 | 8. Return the number of seen tokens. |
| 158 | |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 159 | mintokens > 0 make config_read() print error message if less than mintokens |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 160 | (but more than 0) are found. Empty lines are always skipped (not warned about). |
| 161 | */ |
| 162 | #undef config_read |
| 163 | int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const char *delims) |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 164 | { |
Denys Vlasenko | 50db1f2 | 2017-08-05 18:20:34 +0200 | [diff] [blame] | 165 | char *line, *p; |
Denis Vlasenko | 69f4f9a | 2008-08-09 17:16:40 +0000 | [diff] [blame] | 166 | int ntokens, mintokens; |
Denys Vlasenko | a1a4483 | 2011-06-17 03:37:43 +0200 | [diff] [blame] | 167 | int t; |
Denys Vlasenko | 50db1f2 | 2017-08-05 18:20:34 +0200 | [diff] [blame] | 168 | char alt_comment_ch; |
Denys Vlasenko | a1a4483 | 2011-06-17 03:37:43 +0200 | [diff] [blame] | 169 | |
| 170 | if (!parser) |
| 171 | return 0; |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 172 | |
Denys Vlasenko | 50db1f2 | 2017-08-05 18:20:34 +0200 | [diff] [blame] | 173 | alt_comment_ch = '\0'; |
| 174 | if (flags & PARSE_ALT_COMMENTS) |
| 175 | alt_comment_ch = *delims++; |
| 176 | |
Denys Vlasenko | 63144be | 2010-06-26 04:00:52 +0200 | [diff] [blame] | 177 | ntokens = (uint8_t)flags; |
| 178 | mintokens = (uint8_t)(flags >> 8); |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 179 | |
Timo Teras | adcabf3 | 2011-06-20 09:49:56 +0200 | [diff] [blame] | 180 | again: |
Denis Vlasenko | 69f4f9a | 2008-08-09 17:16:40 +0000 | [diff] [blame] | 181 | memset(tokens, 0, sizeof(tokens[0]) * ntokens); |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 182 | |
Denis Vlasenko | 69f4f9a | 2008-08-09 17:16:40 +0000 | [diff] [blame] | 183 | /* Read one line (handling continuations with backslash) */ |
Timo Teras | adcabf3 | 2011-06-20 09:49:56 +0200 | [diff] [blame] | 184 | if (get_line_with_continuation(parser) < 0) |
Denis Vlasenko | 69f4f9a | 2008-08-09 17:16:40 +0000 | [diff] [blame] | 185 | return 0; |
Timo Teras | adcabf3 | 2011-06-20 09:49:56 +0200 | [diff] [blame] | 186 | |
| 187 | line = parser->line; |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 188 | |
Denis Vlasenko | 69f4f9a | 2008-08-09 17:16:40 +0000 | [diff] [blame] | 189 | /* Skip token in the start of line? */ |
| 190 | if (flags & PARSE_TRIM) |
| 191 | line += strspn(line, delims + 1); |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 192 | |
Denys Vlasenko | 50db1f2 | 2017-08-05 18:20:34 +0200 | [diff] [blame] | 193 | p = line; |
| 194 | if (flags & PARSE_WS_COMMENTS) |
| 195 | p = skip_whitespace(p); |
| 196 | if (p[0] == '\0' || p[0] == delims[0] || p[0] == alt_comment_ch) |
Denis Vlasenko | 69f4f9a | 2008-08-09 17:16:40 +0000 | [diff] [blame] | 197 | goto again; |
| 198 | |
Timo Teras | adcabf3 | 2011-06-20 09:49:56 +0200 | [diff] [blame] | 199 | if (flags & PARSE_KEEP_COPY) { |
| 200 | free(parser->data); |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 201 | parser->data = xstrdup(line); |
Timo Teras | adcabf3 | 2011-06-20 09:49:56 +0200 | [diff] [blame] | 202 | } |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 203 | |
Denis Vlasenko | 69f4f9a | 2008-08-09 17:16:40 +0000 | [diff] [blame] | 204 | /* Tokenize the line */ |
Denys Vlasenko | 63144be | 2010-06-26 04:00:52 +0200 | [diff] [blame] | 205 | t = 0; |
| 206 | do { |
Denis Vlasenko | 69f4f9a | 2008-08-09 17:16:40 +0000 | [diff] [blame] | 207 | /* Pin token */ |
| 208 | tokens[t] = line; |
| 209 | |
| 210 | /* Combine remaining arguments? */ |
| 211 | if ((t != (ntokens-1)) || !(flags & PARSE_GREEDY)) { |
| 212 | /* Vanilla token, find next delimiter */ |
Denys Vlasenko | 9cf89cd | 2017-08-05 13:45:22 +0200 | [diff] [blame] | 213 | line += strcspn(line, (delims[0] && (flags & PARSE_EOL_COMMENTS)) ? delims : delims + 1); |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 214 | } else { |
Denis Vlasenko | 69f4f9a | 2008-08-09 17:16:40 +0000 | [diff] [blame] | 215 | /* Combining, find comment char if any */ |
Denys Vlasenko | 9cf89cd | 2017-08-05 13:45:22 +0200 | [diff] [blame] | 216 | line = strchrnul(line, (flags & PARSE_EOL_COMMENTS) ? delims[0] : '\0'); |
Denis Vlasenko | 69f4f9a | 2008-08-09 17:16:40 +0000 | [diff] [blame] | 217 | |
| 218 | /* Trim any extra delimiters from the end */ |
| 219 | if (flags & PARSE_TRIM) { |
| 220 | while (strchr(delims + 1, line[-1]) != NULL) |
| 221 | line--; |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 222 | } |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 223 | } |
Denis Vlasenko | 69f4f9a | 2008-08-09 17:16:40 +0000 | [diff] [blame] | 224 | |
| 225 | /* Token not terminated? */ |
Denys Vlasenko | 9cf89cd | 2017-08-05 13:45:22 +0200 | [diff] [blame] | 226 | if ((flags & PARSE_EOL_COMMENTS) && *line == delims[0]) |
| 227 | *line = '\0'; /* ends with comment char: this line is done */ |
Denys Vlasenko | 63144be | 2010-06-26 04:00:52 +0200 | [diff] [blame] | 228 | else if (*line != '\0') |
Denys Vlasenko | 9cf89cd | 2017-08-05 13:45:22 +0200 | [diff] [blame] | 229 | *line++ = '\0'; /* token is done, continue parsing line */ |
Denis Vlasenko | 69f4f9a | 2008-08-09 17:16:40 +0000 | [diff] [blame] | 230 | |
| 231 | #if 0 /* unused so far */ |
| 232 | if (flags & PARSE_ESCAPE) { |
Denys Vlasenko | 5360059 | 2010-10-23 21:06:06 +0200 | [diff] [blame] | 233 | strcpy_and_process_escape_sequences(tokens[t], tokens[t]); |
Denis Vlasenko | 69f4f9a | 2008-08-09 17:16:40 +0000 | [diff] [blame] | 234 | } |
| 235 | #endif |
Denis Vlasenko | 69f4f9a | 2008-08-09 17:16:40 +0000 | [diff] [blame] | 236 | /* Skip possible delimiters */ |
| 237 | if (flags & PARSE_COLLAPSE) |
| 238 | line += strspn(line, delims + 1); |
Denys Vlasenko | 63144be | 2010-06-26 04:00:52 +0200 | [diff] [blame] | 239 | |
| 240 | t++; |
| 241 | } while (*line && *line != delims[0] && t < ntokens); |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 242 | |
Denis Vlasenko | 69f4f9a | 2008-08-09 17:16:40 +0000 | [diff] [blame] | 243 | if (t < mintokens) { |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 244 | bb_error_msg("bad line %u: %d tokens found, %d needed", |
Denis Vlasenko | 69f4f9a | 2008-08-09 17:16:40 +0000 | [diff] [blame] | 245 | parser->lineno, t, mintokens); |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 246 | if (flags & PARSE_MIN_DIE) |
| 247 | xfunc_die(); |
| 248 | goto again; |
| 249 | } |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 250 | |
Denis Vlasenko | 69f4f9a | 2008-08-09 17:16:40 +0000 | [diff] [blame] | 251 | return t; |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 252 | } |