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