1 /* vi: set sw=4 ts=4: */
3 * config file parser helper
5 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
13 int parse_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
14 int parse_main(int argc UNUSED_PARAM, char **argv)
16 const char *delims = "# \t";
17 unsigned flags = PARSE_NORMAL;
18 int mintokens = 0, ntokens = 128;
20 opt_complementary = "-1:n+:m+:f+";
21 getopt32(argv, "n:m:d:f:", &ntokens, &mintokens, &delims, &flags);
25 parser_t *p = config_open(*argv);
28 char **t = xmalloc(sizeof(char *) * ntokens);
29 while ((n = config_read(p, t, ntokens, mintokens, delims, flags)) != 0) {
30 for (int i = 0; i < n; ++i)
47 char *t[3]; // tokens placeholder
48 parser_t *p = config_open(filename);
51 while (config_read(p, t, 3, 0, delimiters, flags)) { // 1..3 tokens
53 bb_error_msg("TOKENS: [%s][%s][%s]", t[0], t[1], t[2]);
63 parser_t* FAST_FUNC config_open2(const char *filename, FILE* FAST_FUNC (*fopen_func)(const char *path))
68 fp = fopen_func(filename);
71 parser = xzalloc(sizeof(*parser));
76 parser_t* FAST_FUNC config_open(const char *filename)
78 return config_open2(filename, fopen_or_warn_stdin);
81 static void config_free_data(parser_t *const parser)
85 if (PARSE_KEEP_COPY) { /* compile-time constant */
91 void FAST_FUNC config_close(parser_t *parser)
94 config_free_data(parser);
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.
106 3. If resulting line is empty goto 1.
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.
118 8. Return the number of seen tokens.
120 mintokens > 0 make config_read() print error message if less than mintokens
121 (but more than 0) are found. Empty lines are always skipped (not warned about).
124 int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const char *delims)
133 ntokens = flags & 0xFF;
134 mintokens = (flags & 0xFF00) >> 8;
137 memset(tokens, 0, sizeof(tokens[0]) * ntokens);
140 config_free_data(parser);
143 //TODO: speed up xmalloc_fgetline by internally using fgets, not fgetc
144 line = xmalloc_fgetline(parser->fp);
149 // handle continuations. Tito's code stolen :)
154 if (line[ii - 1] != '\\')
158 //TODO: add xmalloc_fgetline-like iface but with appending to existing str
159 q = xmalloc_fgetline(parser->fp);
163 line = xasprintf("%s%s", line, q);
168 q = strchrnul(line, comment);
172 // skip leading and trailing delimiters
173 if (flags & PARSE_TRIM) {
175 int n = strspn(line, delims);
178 overlapping_strcpy(line, line + n);
182 while (strchr(delims, line[--ii]))
187 // if something still remains -> return it
195 // non-empty line found, parse and return the number of tokens
198 parser->line = line = xrealloc(line, ii + 1);
199 if (flags & PARSE_KEEP_COPY) {
200 parser->data = xstrdup(line);
203 // split line to tokens
204 ntokens--; // now it's max allowed token no
205 // N.B. non-empty remainder is also a token,
206 // so if ntokens <= 1, we just return the whole line
207 // N.B. if PARSE_GREEDY is set the remainder of the line is stuck to the last token
209 while (*line && ii <= ntokens) {
210 //bb_info_msg("L[%s]", line);
212 // at last token and need greedy token ->
213 if ((flags & PARSE_GREEDY) && (ii == ntokens)) {
214 // skip possible delimiters
215 if (flags & PARSE_COLLAPSE)
216 line += strspn(line, delims);
217 // don't cut the line
218 q = line + strlen(line);
220 // vanilla token. cut the line at the first delim
221 q = line + strcspn(line, delims);
222 if (*q) // watch out: do not step past the line end!
226 if (!(flags & (PARSE_COLLAPSE | PARSE_TRIM)) || *line) {
227 //bb_info_msg("N[%d] T[%s]", ii, line);
229 // process escapes in token
230 #if 0 // unused so far
231 if (flags & PARSE_ESCAPE) {
236 *line++ = bb_process_escape_sequence((const char **)&s);
246 //bb_info_msg("A[%s]", line);
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)