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.
8 * Also for use in uClibc (http://uclibc.org/) licensed under LGPLv2.1 or later.
13 #if defined ENABLE_PARSE && ENABLE_PARSE
14 int parse_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
15 int parse_main(int argc UNUSED_PARAM, char **argv)
17 const char *delims = "# \t";
18 unsigned flags = PARSE_NORMAL;
19 int mintokens = 0, ntokens = 128;
21 opt_complementary = "-1:n+:m+:f+";
22 getopt32(argv, "n:m:d:f:", &ntokens, &mintokens, &delims, &flags);
26 parser_t *p = config_open(*argv);
29 char **t = xmalloc(sizeof(char *) * ntokens);
30 while ((n = config_read(p, t, ntokens, mintokens, delims, flags)) != 0) {
31 for (int i = 0; i < n; ++i)
48 char *t[3]; // tokens placeholder
49 parser_t *p = config_open(filename);
52 while (config_read(p, t, 3, 0, delimiters, flags)) { // 1..3 tokens
54 bb_error_msg("TOKENS: [%s][%s][%s]", t[0], t[1], t[2]);
64 parser_t* FAST_FUNC config_open2(const char *filename, FILE* FAST_FUNC (*fopen_func)(const char *path))
69 fp = fopen_func(filename);
72 parser = xzalloc(sizeof(*parser));
77 parser_t* FAST_FUNC config_open(const char *filename)
79 return config_open2(filename, fopen_or_warn_stdin);
82 static void config_free_data(parser_t *const parser)
86 if (PARSE_KEEP_COPY) { /* compile-time constant */
92 void FAST_FUNC config_close(parser_t *parser)
95 config_free_data(parser);
102 0. If parser is NULL return 0.
103 1. Read a line from config file. If nothing to read then return 0.
104 Handle continuation character. Advance lineno for each physical line.
105 Discard everything past comment characher.
106 2. if PARSE_TRIM is set (default), remove leading and trailing delimiters.
107 3. If resulting line is empty goto 1.
108 4. Look for first delimiter. If !PARSE_COLLAPSE or !PARSE_TRIM is set then
109 remember the token as empty.
110 5. Else (default) if number of seen tokens is equal to max number of tokens
111 (token is the last one) and PARSE_GREEDY is set then the remainder
112 of the line is the last token.
113 Else (token is not last or PARSE_GREEDY is not set) just replace
114 first delimiter with '\0' thus delimiting the token.
115 6. Advance line pointer past the end of token. If number of seen tokens
116 is less than required number of tokens then goto 4.
117 7. Check the number of seen tokens is not less the min number of tokens.
118 Complain or die otherwise depending on PARSE_MIN_DIE.
119 8. Return the number of seen tokens.
121 mintokens > 0 make config_read() print error message if less than mintokens
122 (but more than 0) are found. Empty lines are always skipped (not warned about).
125 int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const char *delims)
128 int ntokens, mintokens;
131 ntokens = flags & 0xFF;
132 mintokens = (flags & 0xFF00) >> 8;
138 memset(tokens, 0, sizeof(tokens[0]) * ntokens);
139 config_free_data(parser);
141 /* Read one line (handling continuations with backslash) */
142 line = bb_get_chunk_with_continuation(parser->fp, &len, &parser->lineno);
147 /* Strip trailing line-feed if any */
148 if (len && line[len-1] == '\n')
151 /* Skip token in the start of line? */
152 if (flags & PARSE_TRIM)
153 line += strspn(line, delims + 1);
155 if (line[0] == '\0' || line[0] == delims[0])
158 if (flags & PARSE_KEEP_COPY)
159 parser->data = xstrdup(line);
161 /* Tokenize the line */
162 for (t = 0; *line && *line != delims[0] && t < ntokens; t++) {
166 /* Combine remaining arguments? */
167 if ((t != (ntokens-1)) || !(flags & PARSE_GREEDY)) {
168 /* Vanilla token, find next delimiter */
169 line += strcspn(line, delims[0] ? delims : delims + 1);
171 /* Combining, find comment char if any */
172 line = strchrnul(line, delims[0]);
174 /* Trim any extra delimiters from the end */
175 if (flags & PARSE_TRIM) {
176 while (strchr(delims + 1, line[-1]) != NULL)
181 /* Token not terminated? */
182 if (line[0] == delims[0])
184 else if (line[0] != '\0')
187 #if 0 /* unused so far */
188 if (flags & PARSE_ESCAPE) {
192 from = to = tokens[t];
196 *to++ = bb_process_escape_sequence(&from);
205 /* Skip possible delimiters */
206 if (flags & PARSE_COLLAPSE)
207 line += strspn(line, delims + 1);
211 bb_error_msg("bad line %u: %d tokens found, %d needed",
212 parser->lineno, t, mintokens);
213 if (flags & PARSE_MIN_DIE)