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 | * |
| 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 8 | */ |
| 9 | |
| 10 | #include "libbb.h" |
| 11 | |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 12 | #if ENABLE_PARSE |
| 13 | int parse_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 14 | int parse_main(int argc UNUSED_PARAM, char **argv) |
| 15 | { |
| 16 | const char *delims = "# \t"; |
| 17 | unsigned flags = 0; |
| 18 | int mintokens = 0, ntokens = 128; |
| 19 | opt_complementary = "-1:n+:m+:f+"; |
| 20 | getopt32(argv, "n:m:d:f:", &ntokens, &mintokens, &delims, &flags); |
| 21 | //argc -= optind; |
| 22 | argv += optind; |
| 23 | while (*argv) { |
| 24 | parser_t *p = config_open(*argv); |
| 25 | if (p) { |
| 26 | int n; |
| 27 | char **t = xmalloc(sizeof(char *) * ntokens); |
Denis Vlasenko | 4a717e0 | 2008-07-20 13:01:56 +0000 | [diff] [blame] | 28 | while ((n = config_read(p, t, ntokens, mintokens, delims, flags)) != 0) { |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 29 | for (int i = 0; i < n; ++i) |
| 30 | printf("[%s]", t[i]); |
| 31 | puts(""); |
| 32 | } |
| 33 | config_close(p); |
| 34 | } |
| 35 | argv++; |
| 36 | } |
| 37 | return EXIT_SUCCESS; |
| 38 | } |
| 39 | #endif |
| 40 | |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 41 | /* |
| 42 | |
| 43 | Typical usage: |
| 44 | |
| 45 | ----- CUT ----- |
| 46 | char *t[3]; // tokens placeholder |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 47 | parser_t *p = config_open(filename); |
| 48 | if (p) { |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 49 | // parse line-by-line |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 50 | while (config_read(p, t, 3, 0, delimiters, flags)) { // 1..3 tokens |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 51 | // use tokens |
| 52 | bb_error_msg("TOKENS: [%s][%s][%s]", t[0], t[1], t[2]); |
| 53 | } |
| 54 | ... |
| 55 | // free parser |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 56 | config_close(p); |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 57 | } |
| 58 | ----- CUT ----- |
| 59 | |
| 60 | */ |
| 61 | |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame^] | 62 | 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] | 63 | { |
Bernhard Reutner-Fischer | 6792128 | 2008-07-17 11:59:13 +0000 | [diff] [blame] | 64 | parser_t *parser = xzalloc(sizeof(parser_t)); |
| 65 | /* empty file configures nothing */ |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame^] | 66 | parser->fp = fopen_func(filename); |
Bernhard Reutner-Fischer | 6792128 | 2008-07-17 11:59:13 +0000 | [diff] [blame] | 67 | if (parser->fp) |
| 68 | return parser; |
Bernhard Reutner-Fischer | 6792128 | 2008-07-17 11:59:13 +0000 | [diff] [blame] | 69 | if (ENABLE_FEATURE_CLEAN_UP) |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 70 | free(parser); |
Bernhard Reutner-Fischer | 6792128 | 2008-07-17 11:59:13 +0000 | [diff] [blame] | 71 | return NULL; |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame^] | 74 | parser_t* FAST_FUNC config_open(const char *filename) |
| 75 | { |
| 76 | return config_open2(filename, fopen_or_warn_stdin); |
| 77 | } |
| 78 | |
Bernhard Reutner-Fischer | 6792128 | 2008-07-17 11:59:13 +0000 | [diff] [blame] | 79 | static void config_free_data(parser_t *const parser) |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 80 | { |
Denis Vlasenko | c01340f | 2008-07-16 22:12:18 +0000 | [diff] [blame] | 81 | free(parser->line); |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 82 | parser->line = NULL; |
Denis Vlasenko | dcb3fcb | 2008-07-19 22:57:00 +0000 | [diff] [blame] | 83 | if (PARSE_KEEP_COPY) { /* compile-time constant */ |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 84 | free(parser->data); |
| 85 | parser->data = NULL; |
Denis Vlasenko | dcb3fcb | 2008-07-19 22:57:00 +0000 | [diff] [blame] | 86 | } |
Bernhard Reutner-Fischer | 6792128 | 2008-07-17 11:59:13 +0000 | [diff] [blame] | 87 | } |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 88 | |
Bernhard Reutner-Fischer | 6792128 | 2008-07-17 11:59:13 +0000 | [diff] [blame] | 89 | void FAST_FUNC config_close(parser_t *parser) |
| 90 | { |
| 91 | config_free_data(parser); |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 92 | fclose(parser->fp); |
| 93 | } |
| 94 | |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 95 | /* |
| 96 | 1. Read a line from config file. If nothing to read then bail out returning 0. |
| 97 | Handle continuation character. Advance lineno for each physical line. Cut comments. |
| 98 | 2. if PARSE_DONT_TRIM is not set (default) skip leading and cut trailing delimiters, if any. |
| 99 | 3. If resulting line is empty goto 1. |
| 100 | 4. Look for first delimiter. If PARSE_DONT_REDUCE or PARSE_DONT_TRIM is set then pin empty token. |
| 101 | 5. Else (default) if number of seen tokens is equal to max number of tokens (token is the last one) |
| 102 | and PARSE_LAST_IS_GREEDY is set then pin the remainder of the line as the last token. |
| 103 | Else (token is not last or PARSE_LAST_IS_GREEDY is not set) just replace first delimiter with '\0' |
| 104 | thus delimiting token and pin it. |
| 105 | 6. Advance line pointer past the end of token. If number of seen tokens is less than required number |
| 106 | of tokens then goto 4. |
| 107 | 7. Control the number of seen tokens is not less the min number of tokens. Die if condition is not met. |
| 108 | 8. Return the number of seen tokens. |
| 109 | |
| 110 | mintokens > 0 make config_read() exit with error message if less than mintokens |
| 111 | (but more than 0) are found. Empty lines are always skipped (not warned about). |
| 112 | */ |
| 113 | #undef config_read |
| 114 | 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] | 115 | { |
| 116 | char *line, *q; |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 117 | char comment = *delims++; |
| 118 | int ii; |
| 119 | int ntokens = flags & 0xFF; |
| 120 | int mintokens = (flags & 0xFF00) >> 8; |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 121 | |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame^] | 122 | again: |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 123 | // N.B. this could only be used in read-in-one-go version, or when tokens use xstrdup(). TODO |
Denis Vlasenko | 4a717e0 | 2008-07-20 13:01:56 +0000 | [diff] [blame] | 124 | //if (!parser->lineno || !(flags & PARSE_DONT_NULL)) |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 125 | memset(tokens, 0, sizeof(tokens[0]) * ntokens); |
Bernhard Reutner-Fischer | 6792128 | 2008-07-17 11:59:13 +0000 | [diff] [blame] | 126 | config_free_data(parser); |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 127 | |
| 128 | while (1) { |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 129 | //TODO: speed up xmalloc_fgetline by internally using fgets, not fgetc |
| 130 | line = xmalloc_fgetline(parser->fp); |
| 131 | if (!line) |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 132 | return 0; |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 133 | |
| 134 | parser->lineno++; |
| 135 | // handle continuations. Tito's code stolen :) |
| 136 | while (1) { |
Bernhard Reutner-Fischer | 6792128 | 2008-07-17 11:59:13 +0000 | [diff] [blame] | 137 | ii = strlen(line); |
| 138 | if (!ii) |
| 139 | goto next_line; |
| 140 | if (line[ii - 1] != '\\') |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 141 | break; |
| 142 | // multi-line object |
Bernhard Reutner-Fischer | 6792128 | 2008-07-17 11:59:13 +0000 | [diff] [blame] | 143 | line[--ii] = '\0'; |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 144 | //TODO: add xmalloc_fgetline-like iface but with appending to existing str |
| 145 | q = xmalloc_fgetline(parser->fp); |
| 146 | if (q) { |
| 147 | parser->lineno++; |
| 148 | line = xasprintf("%s%s", line, q); |
| 149 | free(q); |
| 150 | } |
| 151 | } |
| 152 | // comments mean EOLs |
| 153 | if (comment) { |
| 154 | q = strchrnul(line, comment); |
| 155 | *q = '\0'; |
Bernhard Reutner-Fischer | 6792128 | 2008-07-17 11:59:13 +0000 | [diff] [blame] | 156 | ii = q - line; |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 157 | } |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 158 | // skip leading and trailing delimiters |
| 159 | if (!(flags & PARSE_DONT_TRIM)) { |
| 160 | // skip leading |
| 161 | int n = strspn(line, delims); |
| 162 | if (n) { |
| 163 | ii -= n; |
| 164 | strcpy(line, line + n); |
| 165 | } |
| 166 | // cut trailing |
| 167 | if (ii) { |
| 168 | while (strchr(delims, line[--ii])) |
| 169 | continue; |
| 170 | line[++ii] = '\0'; |
| 171 | } |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 172 | } |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 173 | // if something still remains -> return it |
Bernhard Reutner-Fischer | 6792128 | 2008-07-17 11:59:13 +0000 | [diff] [blame] | 174 | if (ii) |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 175 | break; |
Bernhard Reutner-Fischer | 6792128 | 2008-07-17 11:59:13 +0000 | [diff] [blame] | 176 | |
| 177 | next_line: |
Denis Vlasenko | 4a717e0 | 2008-07-20 13:01:56 +0000 | [diff] [blame] | 178 | // skip empty line |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 179 | free(line); |
| 180 | } |
| 181 | |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 182 | // non-empty line found, parse and return the number of tokens |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 183 | |
| 184 | // store line |
Bernhard Reutner-Fischer | 6792128 | 2008-07-17 11:59:13 +0000 | [diff] [blame] | 185 | parser->line = line = xrealloc(line, ii + 1); |
Denis Vlasenko | dcb3fcb | 2008-07-19 22:57:00 +0000 | [diff] [blame] | 186 | if (flags & PARSE_KEEP_COPY) { |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 187 | parser->data = xstrdup(line); |
Denis Vlasenko | dcb3fcb | 2008-07-19 22:57:00 +0000 | [diff] [blame] | 188 | } |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 189 | |
Denis Vlasenko | 4a717e0 | 2008-07-20 13:01:56 +0000 | [diff] [blame] | 190 | // split line to tokens |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 191 | ntokens--; // now it's max allowed token no |
Denis Vlasenko | 4a717e0 | 2008-07-20 13:01:56 +0000 | [diff] [blame] | 192 | // N.B. non-empty remainder is also a token, |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 193 | // so if ntokens <= 1, we just return the whole line |
| 194 | // N.B. if PARSE_LAST_IS_GREEDY is set the remainder of the line is stuck to the last token |
| 195 | for (ii = 0; *line && ii <= ntokens; ) { |
| 196 | //bb_info_msg("L[%s]", line); |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 197 | // get next token |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 198 | // at the last token and need greedy token -> |
| 199 | if ((flags & PARSE_LAST_IS_GREEDY) && (ii == ntokens)) { |
Denis Vlasenko | 4a717e0 | 2008-07-20 13:01:56 +0000 | [diff] [blame] | 200 | // skip possible delimiters |
| 201 | if (!(flags & PARSE_DONT_REDUCE)) |
| 202 | line += strspn(line, delims); |
| 203 | // don't cut the line |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 204 | q = line + strlen(line); |
| 205 | } else { |
| 206 | // vanilla token. cut the line at the first delim |
| 207 | q = line + strcspn(line, delims); |
Denis Vlasenko | 9b366f4 | 2008-07-20 17:50:58 +0000 | [diff] [blame] | 208 | if (*q) // watch out: do not step past the line end! |
| 209 | *q++ = '\0'; |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 210 | } |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 211 | // pin token |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 212 | if ((flags & (PARSE_DONT_REDUCE|PARSE_DONT_TRIM)) || *line) { |
| 213 | //bb_info_msg("N[%d] T[%s]", ii, line); |
Bernhard Reutner-Fischer | 6792128 | 2008-07-17 11:59:13 +0000 | [diff] [blame] | 214 | tokens[ii++] = line; |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 215 | } |
| 216 | line = q; |
Denis Vlasenko | 9b366f4 | 2008-07-20 17:50:58 +0000 | [diff] [blame] | 217 | //bb_info_msg("A[%s]", line); |
Bernhard Reutner-Fischer | 6792128 | 2008-07-17 11:59:13 +0000 | [diff] [blame] | 218 | } |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 219 | |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame^] | 220 | if (ii < mintokens) { |
| 221 | bb_error_msg("bad line %u: %d tokens found, %d needed", |
| 222 | parser->lineno, ii, mintokens); |
| 223 | if (flags & PARSE_MIN_DIE) |
| 224 | xfunc_die(); |
| 225 | goto again; |
| 226 | } |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 227 | |
Bernhard Reutner-Fischer | 6792128 | 2008-07-17 11:59:13 +0000 | [diff] [blame] | 228 | return ii; |
Denis Vlasenko | e559e0a | 2008-07-15 21:09:30 +0000 | [diff] [blame] | 229 | } |