blob: b7c3a00e0670f43d240c8a4d337b2669bcb92f0d [file] [log] [blame]
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +00001/* 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.
Bernhard Reutner-Fischerf3b39a22009-02-23 16:21:53 +00008 * Also for use in uClibc (http://uclibc.org/) licensed under LGPLv2.1 or later.
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +00009 */
10
11#include "libbb.h"
12
Denis Vlasenko2d5bd802008-10-24 10:49:49 +000013#if defined ENABLE_PARSE && ENABLE_PARSE
Denis Vlasenko2e157dd2008-07-19 09:27:19 +000014int parse_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
15int parse_main(int argc UNUSED_PARAM, char **argv)
16{
17 const char *delims = "# \t";
Denis Vlasenko084266e2008-07-26 23:08:31 +000018 unsigned flags = PARSE_NORMAL;
Denis Vlasenko2e157dd2008-07-19 09:27:19 +000019 int mintokens = 0, ntokens = 128;
Denis Vlasenko084266e2008-07-26 23:08:31 +000020
Denis Vlasenko2e157dd2008-07-19 09:27:19 +000021 opt_complementary = "-1:n+:m+:f+";
22 getopt32(argv, "n:m:d:f:", &ntokens, &mintokens, &delims, &flags);
23 //argc -= optind;
24 argv += optind;
25 while (*argv) {
26 parser_t *p = config_open(*argv);
27 if (p) {
28 int n;
29 char **t = xmalloc(sizeof(char *) * ntokens);
Denis Vlasenko4a717e02008-07-20 13:01:56 +000030 while ((n = config_read(p, t, ntokens, mintokens, delims, flags)) != 0) {
Denis Vlasenko2e157dd2008-07-19 09:27:19 +000031 for (int i = 0; i < n; ++i)
32 printf("[%s]", t[i]);
33 puts("");
34 }
35 config_close(p);
36 }
37 argv++;
38 }
39 return EXIT_SUCCESS;
40}
41#endif
42
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +000043/*
44
45Typical usage:
46
47----- CUT -----
48 char *t[3]; // tokens placeholder
Denis Vlasenko2e157dd2008-07-19 09:27:19 +000049 parser_t *p = config_open(filename);
50 if (p) {
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +000051 // parse line-by-line
Denis Vlasenko2e157dd2008-07-19 09:27:19 +000052 while (config_read(p, t, 3, 0, delimiters, flags)) { // 1..3 tokens
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +000053 // use tokens
54 bb_error_msg("TOKENS: [%s][%s][%s]", t[0], t[1], t[2]);
55 }
56 ...
57 // free parser
Denis Vlasenko2e157dd2008-07-19 09:27:19 +000058 config_close(p);
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +000059 }
60----- CUT -----
61
62*/
63
Denis Vlasenko5415c852008-07-21 23:05:26 +000064parser_t* FAST_FUNC config_open2(const char *filename, FILE* FAST_FUNC (*fopen_func)(const char *path))
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +000065{
Denis Vlasenko084266e2008-07-26 23:08:31 +000066 FILE* fp;
67 parser_t *parser;
68
69 fp = fopen_func(filename);
70 if (!fp)
71 return NULL;
72 parser = xzalloc(sizeof(*parser));
73 parser->fp = fp;
74 return parser;
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +000075}
76
Denis Vlasenko5415c852008-07-21 23:05:26 +000077parser_t* FAST_FUNC config_open(const char *filename)
78{
79 return config_open2(filename, fopen_or_warn_stdin);
80}
81
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +000082static void config_free_data(parser_t *parser)
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +000083{
Denis Vlasenkoc01340f2008-07-16 22:12:18 +000084 free(parser->line);
Denis Vlasenko2e157dd2008-07-19 09:27:19 +000085 parser->line = NULL;
Denis Vlasenkodcb3fcb2008-07-19 22:57:00 +000086 if (PARSE_KEEP_COPY) { /* compile-time constant */
Denis Vlasenko2e157dd2008-07-19 09:27:19 +000087 free(parser->data);
88 parser->data = NULL;
Denis Vlasenkodcb3fcb2008-07-19 22:57:00 +000089 }
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +000090}
Denis Vlasenko2e157dd2008-07-19 09:27:19 +000091
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +000092void FAST_FUNC config_close(parser_t *parser)
93{
Denis Vlasenko084266e2008-07-26 23:08:31 +000094 if (parser) {
95 config_free_data(parser);
96 fclose(parser->fp);
97 free(parser);
98 }
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +000099}
100
Denis Vlasenko2e157dd2008-07-19 09:27:19 +0000101/*
Denis Vlasenko084266e2008-07-26 23:08:31 +00001020. If parser is NULL return 0.
1031. Read a line from config file. If nothing to read then return 0.
104 Handle continuation character. Advance lineno for each physical line.
Denys Vlasenko5370bfb2009-09-06 02:58:59 +0200105 Discard everything past comment character.
Denis Vlasenko084266e2008-07-26 23:08:31 +00001062. if PARSE_TRIM is set (default), remove leading and trailing delimiters.
Denis Vlasenko2e157dd2008-07-19 09:27:19 +00001073. If resulting line is empty goto 1.
Denis Vlasenko084266e2008-07-26 23:08:31 +00001084. Look for first delimiter. If !PARSE_COLLAPSE or !PARSE_TRIM is set then
109 remember the token as empty.
1105. 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.
1156. Advance line pointer past the end of token. If number of seen tokens
116 is less than required number of tokens then goto 4.
1177. Check the number of seen tokens is not less the min number of tokens.
118 Complain or die otherwise depending on PARSE_MIN_DIE.
Denis Vlasenko2e157dd2008-07-19 09:27:19 +00001198. Return the number of seen tokens.
120
Denis Vlasenko084266e2008-07-26 23:08:31 +0000121mintokens > 0 make config_read() print error message if less than mintokens
Denis Vlasenko2e157dd2008-07-19 09:27:19 +0000122(but more than 0) are found. Empty lines are always skipped (not warned about).
123*/
124#undef config_read
125int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const char *delims)
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000126{
Denis Vlasenko69f4f9a2008-08-09 17:16:40 +0000127 char *line;
128 int ntokens, mintokens;
129 int t, len;
Denis Vlasenko084266e2008-07-26 23:08:31 +0000130
Denys Vlasenko63144be2010-06-26 04:00:52 +0200131 ntokens = (uint8_t)flags;
132 mintokens = (uint8_t)(flags >> 8);
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000133
Denis Vlasenko69f4f9a2008-08-09 17:16:40 +0000134 if (parser == NULL)
Denis Vlasenko084266e2008-07-26 23:08:31 +0000135 return 0;
Denis Vlasenko69f4f9a2008-08-09 17:16:40 +0000136
137again:
138 memset(tokens, 0, sizeof(tokens[0]) * ntokens);
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000139 config_free_data(parser);
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000140
Denis Vlasenko69f4f9a2008-08-09 17:16:40 +0000141 /* Read one line (handling continuations with backslash) */
142 line = bb_get_chunk_with_continuation(parser->fp, &len, &parser->lineno);
143 if (line == NULL)
144 return 0;
145 parser->line = line;
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000146
Denis Vlasenko69f4f9a2008-08-09 17:16:40 +0000147 /* Strip trailing line-feed if any */
148 if (len && line[len-1] == '\n')
149 line[len-1] = '\0';
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000150
Denis Vlasenko69f4f9a2008-08-09 17:16:40 +0000151 /* Skip token in the start of line? */
152 if (flags & PARSE_TRIM)
153 line += strspn(line, delims + 1);
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000154
Denis Vlasenko69f4f9a2008-08-09 17:16:40 +0000155 if (line[0] == '\0' || line[0] == delims[0])
156 goto again;
157
158 if (flags & PARSE_KEEP_COPY)
Denis Vlasenko2e157dd2008-07-19 09:27:19 +0000159 parser->data = xstrdup(line);
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000160
Denis Vlasenko69f4f9a2008-08-09 17:16:40 +0000161 /* Tokenize the line */
Denys Vlasenko63144be2010-06-26 04:00:52 +0200162 t = 0;
163 do {
Denis Vlasenko69f4f9a2008-08-09 17:16:40 +0000164 /* Pin token */
165 tokens[t] = line;
166
167 /* Combine remaining arguments? */
168 if ((t != (ntokens-1)) || !(flags & PARSE_GREEDY)) {
169 /* Vanilla token, find next delimiter */
170 line += strcspn(line, delims[0] ? delims : delims + 1);
Denis Vlasenko2e157dd2008-07-19 09:27:19 +0000171 } else {
Denis Vlasenko69f4f9a2008-08-09 17:16:40 +0000172 /* Combining, find comment char if any */
173 line = strchrnul(line, delims[0]);
174
175 /* Trim any extra delimiters from the end */
176 if (flags & PARSE_TRIM) {
177 while (strchr(delims + 1, line[-1]) != NULL)
178 line--;
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000179 }
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000180 }
Denis Vlasenko69f4f9a2008-08-09 17:16:40 +0000181
182 /* Token not terminated? */
Denys Vlasenko63144be2010-06-26 04:00:52 +0200183 if (*line == delims[0])
Denis Vlasenko69f4f9a2008-08-09 17:16:40 +0000184 *line = '\0';
Denys Vlasenko63144be2010-06-26 04:00:52 +0200185 else if (*line != '\0')
186 *line++ = '\0';
Denis Vlasenko69f4f9a2008-08-09 17:16:40 +0000187
188#if 0 /* unused so far */
189 if (flags & PARSE_ESCAPE) {
190 const char *from;
191 char *to;
192
193 from = to = tokens[t];
194 while (*from) {
195 if (*from == '\\') {
196 from++;
197 *to++ = bb_process_escape_sequence(&from);
198 } else {
199 *to++ = *from++;
200 }
201 }
202 *to = '\0';
203 }
204#endif
Denis Vlasenko69f4f9a2008-08-09 17:16:40 +0000205 /* Skip possible delimiters */
206 if (flags & PARSE_COLLAPSE)
207 line += strspn(line, delims + 1);
Denys Vlasenko63144be2010-06-26 04:00:52 +0200208
209 t++;
210 } while (*line && *line != delims[0] && t < ntokens);
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000211
Denis Vlasenko69f4f9a2008-08-09 17:16:40 +0000212 if (t < mintokens) {
Denis Vlasenko5415c852008-07-21 23:05:26 +0000213 bb_error_msg("bad line %u: %d tokens found, %d needed",
Denis Vlasenko69f4f9a2008-08-09 17:16:40 +0000214 parser->lineno, t, mintokens);
Denis Vlasenko5415c852008-07-21 23:05:26 +0000215 if (flags & PARSE_MIN_DIE)
216 xfunc_die();
Denys Vlasenko63144be2010-06-26 04:00:52 +0200217 if (flags & PARSE_KEEP_COPY)
218 free(parser->data);
Denis Vlasenko5415c852008-07-21 23:05:26 +0000219 goto again;
220 }
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000221
Denis Vlasenko69f4f9a2008-08-09 17:16:40 +0000222 return t;
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000223}