loadfont,setfont: make them NOEXEC
[oweals/busybox.git] / libbb / parse_config.c
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 source tree.
8  * Also for use in uClibc (http://uclibc.org/) licensed under LGPLv2.1 or later.
9  */
10
11 /* Uncomment to enable test applet */
12 ////config:config PARSE
13 ////config:     bool "Uniform config file parser debugging applet: parse"
14 ////config:     default n
15 ////config:     help
16 ////config:     Typical usage of parse API:
17 ////config:             char *t[3];
18 ////config:             parser_t *p = config_open(filename);
19 ////config:             while (config_read(p, t, 3, 0, delimiters, flags)) { // 1..3 tokens
20 ////config:                     bb_error_msg("TOKENS: '%s''%s''%s'", t[0], t[1], t[2]);
21 ////config:             }
22 ////config:             config_close(p);
23
24 ////applet:IF_PARSE(APPLET(parse, BB_DIR_USR_BIN, BB_SUID_DROP))
25
26 //kbuild:lib-y += parse_config.o
27
28 //usage:#define parse_trivial_usage
29 //usage:       "[-x] [-n MAXTOKENS] [-m MINTOKENS] [-d DELIMS] [-f FLAGS] FILE..."
30 //usage:#define parse_full_usage "\n\n"
31 //usage:       "        -x      Suppress output (for benchmarking)"
32
33 #include "libbb.h"
34
35 #if defined ENABLE_PARSE && ENABLE_PARSE
36 int parse_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
37 int parse_main(int argc UNUSED_PARAM, char **argv)
38 {
39         const char *delims = "# \t";
40         char **t;
41         unsigned flags = PARSE_NORMAL;
42         int mintokens = 0, ntokens = 128;
43         unsigned noout;
44
45         opt_complementary = "-1";
46         noout = 1 & getopt32(argv, "xn:+m:+d:f:+", &ntokens, &mintokens, &delims, &flags);
47         //argc -= optind;
48         argv += optind;
49
50         t = xmalloc(sizeof(t[0]) * ntokens);
51         while (*argv) {
52                 int n;
53                 parser_t *p = config_open(*argv);
54                 while ((n = config_read(p, t, ntokens, mintokens, delims, flags)) != 0) {
55                         if (!noout) {
56                                 for (int i = 0; i < n; ++i)
57                                         printf("[%s]", t[i]);
58                                 puts("");
59                         }
60                 }
61                 config_close(p);
62                 argv++;
63         }
64         return EXIT_SUCCESS;
65 }
66 #endif
67
68 parser_t* FAST_FUNC config_open2(const char *filename, FILE* FAST_FUNC (*fopen_func)(const char *path))
69 {
70         FILE* fp;
71         parser_t *parser;
72
73         fp = fopen_func(filename);
74         if (!fp)
75                 return NULL;
76         parser = xzalloc(sizeof(*parser));
77         parser->fp = fp;
78         return parser;
79 }
80
81 parser_t* FAST_FUNC config_open(const char *filename)
82 {
83         return config_open2(filename, fopen_or_warn_stdin);
84 }
85
86 void FAST_FUNC config_close(parser_t *parser)
87 {
88         if (parser) {
89                 if (PARSE_KEEP_COPY) /* compile-time constant */
90                         free(parser->data);
91                 fclose(parser->fp);
92                 free(parser->line);
93                 free(parser->nline);
94                 free(parser);
95         }
96 }
97
98 /* This function reads an entire line from a text file,
99  * up to a newline, exclusive.
100  * Trailing '\' is recognized as line continuation.
101  * Returns -1 if EOF/error.
102  */
103 static int get_line_with_continuation(parser_t *parser)
104 {
105         ssize_t len, nlen;
106         char *line;
107
108         len = getline(&parser->line, &parser->line_alloc, parser->fp);
109         if (len <= 0)
110                 return len;
111
112         line = parser->line;
113         for (;;) {
114                 parser->lineno++;
115                 if (line[len - 1] == '\n')
116                         len--;
117                 if (len == 0 || line[len - 1] != '\\')
118                         break;
119                 len--;
120
121                 nlen = getline(&parser->nline, &parser->nline_alloc, parser->fp);
122                 if (nlen <= 0)
123                         break;
124
125                 if (parser->line_alloc < len + nlen + 1) {
126                         parser->line_alloc = len + nlen + 1;
127                         line = parser->line = xrealloc(line, parser->line_alloc);
128                 }
129                 memcpy(&line[len], parser->nline, nlen);
130                 len += nlen;
131         }
132
133         line[len] = '\0';
134         return len;
135 }
136
137
138 /*
139 0. If parser is NULL return 0.
140 1. Read a line from config file. If nothing to read then return 0.
141    Handle continuation character. Advance lineno for each physical line.
142    Discard everything past comment character.
143 2. if PARSE_TRIM is set (default), remove leading and trailing delimiters.
144 3. If resulting line is empty goto 1.
145 4. Look for first delimiter. If !PARSE_COLLAPSE or !PARSE_TRIM is set then
146    remember the token as empty.
147 5. Else (default) if number of seen tokens is equal to max number of tokens
148    (token is the last one) and PARSE_GREEDY is set then the remainder
149    of the line is the last token.
150    Else (token is not last or PARSE_GREEDY is not set) just replace
151    first delimiter with '\0' thus delimiting the token.
152 6. Advance line pointer past the end of token. If number of seen tokens
153    is less than required number of tokens then goto 4.
154 7. Check the number of seen tokens is not less the min number of tokens.
155    Complain or die otherwise depending on PARSE_MIN_DIE.
156 8. Return the number of seen tokens.
157
158 mintokens > 0 make config_read() print error message if less than mintokens
159 (but more than 0) are found. Empty lines are always skipped (not warned about).
160 */
161 #undef config_read
162 int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const char *delims)
163 {
164         char *line, *p;
165         int ntokens, mintokens;
166         int t;
167         char alt_comment_ch;
168
169         if (!parser)
170                 return 0;
171
172         alt_comment_ch = '\0';
173         if (flags & PARSE_ALT_COMMENTS)
174                 alt_comment_ch = *delims++;
175
176         ntokens = (uint8_t)flags;
177         mintokens = (uint8_t)(flags >> 8);
178
179  again:
180         memset(tokens, 0, sizeof(tokens[0]) * ntokens);
181
182         /* Read one line (handling continuations with backslash) */
183         if (get_line_with_continuation(parser) < 0)
184                 return 0;
185
186         line = parser->line;
187
188         /* Skip token in the start of line? */
189         if (flags & PARSE_TRIM)
190                 line += strspn(line, delims + 1);
191
192         p = line;
193         if (flags & PARSE_WS_COMMENTS)
194                 p = skip_whitespace(p);
195         if (p[0] == '\0' || p[0] == delims[0] || p[0] == alt_comment_ch)
196                 goto again;
197
198         if (flags & PARSE_KEEP_COPY) {
199                 free(parser->data);
200                 parser->data = xstrdup(line);
201         }
202
203         /* Tokenize the line */
204         t = 0;
205         do {
206                 /* Pin token */
207                 tokens[t] = line;
208
209                 /* Combine remaining arguments? */
210                 if ((t != (ntokens-1)) || !(flags & PARSE_GREEDY)) {
211                         /* Vanilla token, find next delimiter */
212                         line += strcspn(line, (delims[0] && (flags & PARSE_EOL_COMMENTS)) ? delims : delims + 1);
213                 } else {
214                         /* Combining, find comment char if any */
215                         line = strchrnul(line, (flags & PARSE_EOL_COMMENTS) ? delims[0] : '\0');
216
217                         /* Trim any extra delimiters from the end */
218                         if (flags & PARSE_TRIM) {
219                                 while (strchr(delims + 1, line[-1]) != NULL)
220                                         line--;
221                         }
222                 }
223
224                 /* Token not terminated? */
225                 if ((flags & PARSE_EOL_COMMENTS) && *line == delims[0])
226                         *line = '\0'; /* ends with comment char: this line is done */
227                 else if (*line != '\0')
228                         *line++ = '\0'; /* token is done, continue parsing line */
229
230 #if 0 /* unused so far */
231                 if (flags & PARSE_ESCAPE) {
232                         strcpy_and_process_escape_sequences(tokens[t], tokens[t]);
233                 }
234 #endif
235                 /* Skip possible delimiters */
236                 if (flags & PARSE_COLLAPSE)
237                         line += strspn(line, delims + 1);
238
239                 t++;
240         } while (*line && *line != delims[0] && t < ntokens);
241
242         if (t < mintokens) {
243                 bb_error_msg("bad line %u: %d tokens found, %d needed",
244                                 parser->lineno, t, mintokens);
245                 if (flags & PARSE_MIN_DIE)
246                         xfunc_die();
247                 goto again;
248         }
249
250         return t;
251 }