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 * Also for use in uClibc (http://uclibc.org/) licensed under LGPLv2.1 or later.
9 */
10
11 #if !defined _LIBC
12 #include "libbb.h"
13
14 #if defined ENABLE_PARSE && ENABLE_PARSE
15 int parse_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
parse_main(int argc UNUSED_PARAM,char ** argv)16 int parse_main(int argc UNUSED_PARAM, char **argv)
17 {
18 const char *delims = "# \t";
19 unsigned flags = PARSE_NORMAL;
20 int mintokens = 0, ntokens = 128;
21
22 opt_complementary = "-1:n+:m+:f+";
23 getopt32(argv, "n:m:d:f:", &ntokens, &mintokens, &delims, &flags);
24 //argc -= optind;
25 argv += optind;
26 while (*argv) {
27 parser_t *p = config_open(*argv);
28 if (p) {
29 int n;
30 char **t = xmalloc(sizeof(char *) * ntokens);
31 while ((n = config_read(p, t, ntokens, mintokens, delims, flags)) != 0) {
32 for (int i = 0; i < n; ++i)
33 printf("[%s]", t[i]);
34 puts("");
35 }
36 config_close(p);
37 }
38 argv++;
39 }
40 return EXIT_SUCCESS;
41 }
42 #endif
43 #else
44 # include <unistd.h>
45 # include <string.h>
46 # include <malloc.h>
47 # include <bits/uClibc_page.h>
48 # include "internal/parse_config.h"
49 # ifndef FAST_FUNC
50 # define FAST_FUNC
51 # endif
52 # define fopen_or_warn_stdin fopen
53 # define bb_error_msg(...)
54 # define xstrdup strdup
55 # define xfunc_die() return 0
56 /* Read up to EOF or EOL, treat line-continuations as extending the line.
57 Return number of bytes read into .line, -1 otherwise */
bb_get_chunk_with_continuation(parser_t * parsr)58 static off_t bb_get_chunk_with_continuation(parser_t* parsr)
59 {
60 off_t pos = 0;
61 char *chp;
62
63 while (1) {
64 if (fgets(parsr->line + pos, parsr->line_len - pos, parsr->fp) == NULL) {
65 memset(parsr->line, 0, parsr->line_len);
66 pos = -1;
67 break;
68 }
69 pos += strlen(parsr->line + pos);
70 chp = strchr(parsr->line, '\n');
71 if (chp) {
72 --pos;
73 if (--*chp == '\\')
74 --pos;
75 else
76 break;
77 } else if (parsr->allocated) {
78 parsr->line_len += PAGE_SIZE;
79 parsr->data = realloc(parsr->data,
80 parsr->data_len + parsr->line_len);
81 parsr->line = parsr->data + parsr->data_len;
82 } else {
83 /* discard rest of line if not enough space in buffer */
84 int c;
85 do {
86 c = fgetc(parsr->fp);
87 } while (c != EOF && c != '\n');
88 break;
89 }
90 }
91 return pos;
92 }
93 #endif
94
95 /*
96
97 Typical usage:
98
99 ----- CUT -----
100 char *t[3]; // tokens placeholder
101 parser_t *p = config_open(filename);
102 if (p) {
103 // parse line-by-line
104 while (config_read(p, t, 3, 0, delimiters, flags)) { // 1..3 tokens
105 // use tokens
106 bb_error_msg("TOKENS: [%s][%s][%s]", t[0], t[1], t[2]);
107 }
108 ...
109 // free parser
110 config_close(p);
111 }
112 ----- CUT -----
113
114 */
115
config_open2(const char * filename,FILE * FAST_FUNC (* fopen_func)(const char * path,const char * mode))116 static __always_inline parser_t * FAST_FUNC config_open2(const char *filename,
117 FILE* FAST_FUNC (*fopen_func)(const char *path, const char *mode))
118 {
119 parser_t *parser;
120 FILE* fp;
121
122 fp = fopen_func(filename, "r");
123 if (!fp)
124 return NULL;
125 parser = calloc(1, sizeof(*parser));
126 if (parser) {
127 parser->fp = fp;
128 }
129 return parser;
130 }
131
config_open(const char * filename)132 parser_t * FAST_FUNC config_open(const char *filename)
133 {
134 return config_open2(filename, fopen_or_warn_stdin);
135 }
136
137 #ifdef UNUSED
config_free_data(parser_t * parser)138 static void config_free_data(parser_t *parser)
139 {
140 free(parser->data);
141 parser->data = parser->line = NULL;
142 }
143 #endif
144
config_close(parser_t * parser)145 void FAST_FUNC config_close(parser_t *parser)
146 {
147 if (parser) {
148 fclose(parser->fp);
149 if (parser->allocated)
150 free(parser->data);
151 free(parser);
152 }
153 }
154
155 /*
156 0. If parser is NULL return 0.
157 1. Read a line from config file. If nothing to read then return 0.
158 Handle continuation character. Advance lineno for each physical line.
159 Discard everything past comment character.
160 2. if PARSE_TRIM is set (default), remove leading and trailing delimiters.
161 3. If resulting line is empty goto 1.
162 4. Look for first delimiter. If !PARSE_COLLAPSE or !PARSE_TRIM is set then
163 remember the token as empty.
164 5. Else (default) if number of seen tokens is equal to max number of tokens
165 (token is the last one) and PARSE_GREEDY is set then the remainder
166 of the line is the last token.
167 Else (token is not last or PARSE_GREEDY is not set) just replace
168 first delimiter with '\0' thus delimiting the token.
169 6. Advance line pointer past the end of token. If number of seen tokens
170 is less than required number of tokens then goto 4.
171 7. Check the number of seen tokens is not less the min number of tokens.
172 Complain or die otherwise depending on PARSE_MIN_DIE.
173 8. Return the number of seen tokens.
174
175 mintokens > 0 make config_read() print error message if less than mintokens
176 (but more than 0) are found. Empty lines are always skipped (not warned about).
177 */
178 #undef config_read
config_read(parser_t * parser,char *** tokens,unsigned flags,const char * delims)179 int FAST_FUNC config_read(parser_t *parser, char ***tokens,
180 unsigned flags, const char *delims)
181 {
182 char *line;
183 int ntokens, mintokens;
184 off_t len;
185 int t;
186
187 if (parser == NULL)
188 return 0;
189 ntokens = flags & 0xFF;
190 mintokens = (flags & 0xFF00) >> 8;
191 again:
192 if (parser->data == NULL) {
193 if (parser->line_len == 0)
194 parser->line_len = 81;
195 if (parser->data_len == 0)
196 parser->data_len += 1 + ntokens * sizeof(char *);
197 parser->data = malloc(parser->data_len + parser->line_len);
198 if (parser->data == NULL)
199 return 0;
200 parser->allocated |= 1;
201 } /* else { assert(parser->data_len > 0); } */
202 parser->line = parser->data + parser->data_len;
203 /*config_free_data(parser);*/
204
205 /* Read one line (handling continuations with backslash) */
206 len = bb_get_chunk_with_continuation(parser);
207 if (len == -1)
208 return 0;
209 line = parser->line;
210
211 /* Skip multiple token-delimiters in the start of line? */
212 if (flags & PARSE_TRIM)
213 line += strspn(line, delims + 1);
214
215 if (line[0] == '\0' || line[0] == delims[0])
216 goto again;
217
218 *tokens = (char **) parser->data;
219 memset(*tokens, 0, sizeof(*tokens[0]) * ntokens);
220
221 /* Tokenize the line */
222 for (t = 0; *line && *line != delims[0] && t < ntokens; t++) {
223 /* Pin token */
224 *(*tokens + t) = line;
225
226 /* Combine remaining arguments? */
227 if ((t != ntokens-1) || !(flags & PARSE_GREEDY)) {
228 /* Vanilla token, find next delimiter */
229 line += strcspn(line, delims[0] ? delims : delims + 1);
230 } else {
231 /* Combining, find comment char if any */
232 line = strchrnul(line, delims[0]);
233
234 /* Trim any extra delimiters from the end */
235 if (flags & PARSE_TRIM) {
236 while (strchr(delims + 1, line[-1]) != NULL)
237 line--;
238 }
239 }
240
241 /* Token not terminated? */
242 if (line[0] == delims[0])
243 *line = '\0';
244 else if (line[0] != '\0')
245 *(line++) = '\0';
246
247 #if 0 /* unused so far */
248 if (flags & PARSE_ESCAPE) {
249 const char *from;
250 char *to;
251
252 from = to = tokens[t];
253 while (*from) {
254 if (*from == '\\') {
255 from++;
256 *to++ = bb_process_escape_sequence(&from);
257 } else {
258 *to++ = *from++;
259 }
260 }
261 *to = '\0';
262 }
263 #endif
264
265 /* Skip possible delimiters */
266 if (flags & PARSE_COLLAPSE)
267 line += strspn(line, delims + 1);
268 }
269
270 if (t < mintokens) {
271 bb_error_msg(/*"bad line %u: "*/"%d tokens found, %d needed",
272 /*parser->lineno, */t, mintokens);
273 if (flags & PARSE_MIN_DIE)
274 xfunc_die();
275 goto again;
276 }
277 return t;
278 }
279