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