/src/haproxy/src/cfgcond.c
Line | Count | Source |
1 | | /* |
2 | | * Configuration condition preprocessor |
3 | | * |
4 | | * Copyright 2000-2021 Willy Tarreau <w@1wt.eu> |
5 | | * |
6 | | * This program is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU General Public License |
8 | | * as published by the Free Software Foundation; either version |
9 | | * 2 of the License, or (at your option) any later version. |
10 | | * |
11 | | */ |
12 | | |
13 | | #include <haproxy/api.h> |
14 | | #include <haproxy/arg.h> |
15 | | #include <haproxy/cfgcond.h> |
16 | | #include <haproxy/proto_tcp.h> |
17 | | #include <haproxy/tools.h> |
18 | | #include <haproxy/version.h> |
19 | | |
20 | | /* supported condition predicates */ |
21 | | const struct cond_pred_kw cond_predicates[] = { |
22 | | { "defined", CFG_PRED_DEFINED, ARG1(1, STR) }, |
23 | | { "feature", CFG_PRED_FEATURE, ARG1(1, STR) }, |
24 | | { "streq", CFG_PRED_STREQ, ARG2(2, STR, STR) }, |
25 | | { "strneq", CFG_PRED_STRNEQ, ARG2(2, STR, STR) }, |
26 | | { "strstr", CFG_PRED_STRSTR, ARG2(2, STR, STR) }, |
27 | | { "version_atleast", CFG_PRED_VERSION_ATLEAST, ARG1(1, STR) }, |
28 | | { "version_before", CFG_PRED_VERSION_BEFORE, ARG1(1, STR) }, |
29 | | { "openssl_version_atleast", CFG_PRED_OSSL_VERSION_ATLEAST, ARG1(1, STR) }, |
30 | | { "openssl_version_before", CFG_PRED_OSSL_VERSION_BEFORE, ARG1(1, STR) }, |
31 | | { "ssllib_name_startswith", CFG_PRED_SSLLIB_NAME_STARTSWITH, ARG1(1, STR) }, |
32 | | { "awslc_api_atleast", CFG_PRED_AWSLC_API_ATLEAST, ARG1(1, STR) }, |
33 | | { "awslc_api_before", CFG_PRED_AWSLC_API_BEFORE, ARG1(1, STR) }, |
34 | | { "enabled", CFG_PRED_ENABLED, ARG1(1, STR) }, |
35 | | { NULL, CFG_PRED_NONE, 0 } |
36 | | }; |
37 | | |
38 | | /* looks up a cond predicate matching the keyword in <str>, possibly followed |
39 | | * by a parenthesis. Returns a pointer to it or NULL if not found. |
40 | | */ |
41 | | const struct cond_pred_kw *cfg_lookup_cond_pred(const char *str) |
42 | 0 | { |
43 | 0 | const struct cond_pred_kw *ret; |
44 | 0 | int len = strcspn(str, " ("); |
45 | |
|
46 | 0 | for (ret = &cond_predicates[0]; ret->word; ret++) { |
47 | 0 | if (len != strlen(ret->word)) |
48 | 0 | continue; |
49 | 0 | if (strncmp(str, ret->word, len) != 0) |
50 | 0 | continue; |
51 | 0 | return ret; |
52 | 0 | } |
53 | 0 | return NULL; |
54 | 0 | } |
55 | | |
56 | | /* Frees <term> and its args. NULL is supported and does nothing. */ |
57 | | void cfg_free_cond_term(struct cfg_cond_term *term) |
58 | 0 | { |
59 | 0 | if (!term) |
60 | 0 | return; |
61 | | |
62 | 0 | if (term->type == CCTT_PAREN) { |
63 | 0 | cfg_free_cond_expr(term->expr); |
64 | 0 | term->expr = NULL; |
65 | 0 | } |
66 | |
|
67 | 0 | free_args(term->args); |
68 | 0 | free(term->args); |
69 | 0 | free(term); |
70 | 0 | } |
71 | | |
72 | | /* Parse an indirect input text as a possible config condition term. |
73 | | * Returns <0 on parsing error, 0 if the parser is desynchronized, or >0 on |
74 | | * success. <term> is allocated and filled with the parsed info, and <text> |
75 | | * is updated on success to point to the first unparsed character, or is left |
76 | | * untouched on failure. On success, the caller must free <term> using |
77 | | * cfg_free_cond_term(). An error will be set in <err> on error, and only |
78 | | * in this case. In this case the first bad character will be reported in |
79 | | * <errptr>. <maxdepth> corresponds to the maximum recursion depth permitted, |
80 | | * it is decremented on each recursive call and the parsing will fail upon |
81 | | * reaching <= 0. |
82 | | */ |
83 | | int cfg_parse_cond_term(const char **text, struct cfg_cond_term **term, char **err, const char **errptr, int maxdepth) |
84 | 0 | { |
85 | 0 | struct cfg_cond_term *t; |
86 | 0 | const char *in = *text; |
87 | 0 | const char *end_ptr; |
88 | 0 | int err_arg; |
89 | 0 | int nbargs; |
90 | 0 | char *end; |
91 | 0 | long val; |
92 | |
|
93 | 0 | while (*in == ' ' || *in == '\t') |
94 | 0 | in++; |
95 | |
|
96 | 0 | if (!*in) /* empty term does not parse */ |
97 | 0 | return 0; |
98 | | |
99 | 0 | *term = NULL; |
100 | 0 | if (maxdepth <= 0) |
101 | 0 | goto fail0; |
102 | | |
103 | 0 | t = *term = calloc(1, sizeof(**term)); |
104 | 0 | if (!t) { |
105 | 0 | memprintf(err, "memory allocation error while parsing conditional expression '%s'", *text); |
106 | 0 | goto fail1; |
107 | 0 | } |
108 | | |
109 | 0 | t->type = CCTT_NONE; |
110 | 0 | t->args = NULL; |
111 | 0 | t->neg = 0; |
112 | | |
113 | | /* !<term> negates the term. White spaces permitted */ |
114 | 0 | while (*in == '!') { |
115 | 0 | t->neg = !t->neg; |
116 | 0 | do { in++; } while (*in == ' ' || *in == '\t'); |
117 | 0 | } |
118 | |
|
119 | 0 | val = strtol(in, &end, 0); |
120 | 0 | if (end != in) { |
121 | 0 | t->type = val ? CCTT_TRUE : CCTT_FALSE; |
122 | 0 | *text = end; |
123 | 0 | return 1; |
124 | 0 | } |
125 | | |
126 | | /* Try to parse '(' EXPR ')' */ |
127 | 0 | if (*in == '(') { |
128 | 0 | int ret; |
129 | |
|
130 | 0 | t->type = CCTT_PAREN; |
131 | 0 | t->args = NULL; |
132 | |
|
133 | 0 | do { in++; } while (*in == ' ' || *in == '\t'); |
134 | 0 | ret = cfg_parse_cond_expr(&in, &t->expr, err, errptr, maxdepth - 1); |
135 | 0 | if (ret == -1) |
136 | 0 | goto fail2; |
137 | 0 | if (ret == 0) |
138 | 0 | goto fail0; |
139 | | |
140 | | /* find the closing ')' */ |
141 | 0 | while (*in == ' ' || *in == '\t') |
142 | 0 | in++; |
143 | 0 | if (*in != ')') { |
144 | 0 | memprintf(err, "expected ')' after conditional expression '%s'", *text); |
145 | 0 | goto fail1; |
146 | 0 | } |
147 | 0 | do { in++; } while (*in == ' ' || *in == '\t'); |
148 | 0 | *text = in; |
149 | 0 | return 1; |
150 | 0 | } |
151 | | |
152 | | /* below we'll likely all make_arg_list() so we must return only via |
153 | | * the <done> label which frees the arg list. |
154 | | */ |
155 | 0 | t->pred = cfg_lookup_cond_pred(in); |
156 | 0 | if (t->pred) { |
157 | 0 | t->type = CCTT_PRED; |
158 | 0 | nbargs = make_arg_list(in + strlen(t->pred->word), -1, |
159 | 0 | t->pred->arg_mask, &t->args, err, |
160 | 0 | &end_ptr, &err_arg, NULL); |
161 | 0 | if (nbargs < 0) { |
162 | 0 | memprintf(err, "%s in argument %d of predicate '%s' used in conditional expression", *err, err_arg, t->pred->word); |
163 | 0 | if (errptr) |
164 | 0 | *errptr = end_ptr; |
165 | 0 | goto fail2; |
166 | 0 | } |
167 | 0 | *text = end_ptr; |
168 | 0 | return 1; |
169 | 0 | } |
170 | | |
171 | 0 | fail0: |
172 | 0 | memprintf(err, "unparsable conditional expression '%s'", *text); |
173 | 0 | fail1: |
174 | 0 | if (errptr) |
175 | 0 | *errptr = *text; |
176 | 0 | fail2: |
177 | 0 | cfg_free_cond_term(*term); |
178 | 0 | *term = NULL; |
179 | 0 | return -1; |
180 | 0 | } |
181 | | |
182 | | /* evaluate a "enabled" expression. Only a subset of options are matched. It |
183 | | * returns 1 if the option is enabled. 0 is returned is the option is not |
184 | | * enabled or if it is not recognized. |
185 | | */ |
186 | | static int cfg_eval_cond_enabled(const char *str) |
187 | 0 | { |
188 | 0 | if (strcmp(str, "POLL") == 0) |
189 | 0 | return !!(global.tune.options & GTUNE_USE_POLL); |
190 | 0 | else if (strcmp(str, "EPOLL") == 0) |
191 | 0 | return !!(global.tune.options & GTUNE_USE_EPOLL); |
192 | 0 | else if (strcmp(str, "KQUEUE") == 0) |
193 | 0 | return !!(global.tune.options & GTUNE_USE_KQUEUE); |
194 | 0 | else if (strcmp(str, "EVPORTS") == 0) |
195 | 0 | return !!(global.tune.options & GTUNE_USE_EVPORTS); |
196 | 0 | else if (strcmp(str, "SPLICE") == 0) |
197 | 0 | return !!(global.tune.options & GTUNE_USE_SPLICE); |
198 | 0 | else if (strcmp(str, "GETADDRINFO") == 0) |
199 | 0 | return !!(global.tune.options & GTUNE_USE_GAI); |
200 | 0 | else if (strcmp(str, "REUSEPORT") == 0) |
201 | 0 | return !!(proto_tcpv4.flags & PROTO_F_REUSEPORT_SUPPORTED); |
202 | 0 | else if (strcmp(str, "FAST-FORWARD") == 0) |
203 | 0 | return !!(global.tune.options & GTUNE_USE_FAST_FWD); |
204 | 0 | else if (strcmp(str, "SERVER-SSL-VERIFY-NONE") == 0) |
205 | 0 | return !!(global.ssl_server_verify == SSL_SERVER_VERIFY_NONE); |
206 | 0 | return 0; |
207 | 0 | } |
208 | | |
209 | | /* evaluate a condition term on a .if/.elif line. The condition was already |
210 | | * parsed in <term>. Returns -1 on error (in which case err is filled with a |
211 | | * message, and only in this case), 0 if the condition is false, 1 if it's |
212 | | * true. |
213 | | */ |
214 | | int cfg_eval_cond_term(const struct cfg_cond_term *term, char **err) |
215 | 0 | { |
216 | 0 | int ret = -1; |
217 | |
|
218 | 0 | if (term->type == CCTT_FALSE) |
219 | 0 | ret = 0; |
220 | 0 | else if (term->type == CCTT_TRUE) |
221 | 0 | ret = 1; |
222 | 0 | else if (term->type == CCTT_PRED) { |
223 | | /* here we know we have a valid predicate with valid arguments |
224 | | * placed in term->args (which the caller will free). |
225 | | */ |
226 | 0 | switch (term->pred->prd) { |
227 | 0 | case CFG_PRED_DEFINED: // checks if arg exists as an environment variable |
228 | 0 | ret = getenv(term->args[0].data.str.area) != NULL; |
229 | 0 | break; |
230 | | |
231 | 0 | case CFG_PRED_FEATURE: { // checks if the arg matches an enabled feature |
232 | 0 | const char *p; |
233 | |
|
234 | 0 | ret = 0; // assume feature not found |
235 | 0 | for (p = build_features; *p && (p = strstr(p, term->args[0].data.str.area)); p++) { |
236 | 0 | if (p > build_features && |
237 | 0 | (p[term->args[0].data.str.data] == ' ' || |
238 | 0 | p[term->args[0].data.str.data] == 0)) { |
239 | 0 | if (*(p-1) == '+') { // e.g. "+OPENSSL" |
240 | 0 | ret = 1; |
241 | 0 | break; |
242 | 0 | } |
243 | 0 | else if (*(p-1) == '-') { // e.g. "-OPENSSL" |
244 | 0 | ret = 0; |
245 | 0 | break; |
246 | 0 | } |
247 | | /* it was a sub-word, let's restart from next place */ |
248 | 0 | } |
249 | 0 | } |
250 | 0 | break; |
251 | 0 | } |
252 | 0 | case CFG_PRED_STREQ: // checks if the two arg are equal |
253 | 0 | ret = strcmp(term->args[0].data.str.area, term->args[1].data.str.area) == 0; |
254 | 0 | break; |
255 | | |
256 | 0 | case CFG_PRED_STRNEQ: // checks if the two arg are different |
257 | 0 | ret = strcmp(term->args[0].data.str.area, term->args[1].data.str.area) != 0; |
258 | 0 | break; |
259 | | |
260 | 0 | case CFG_PRED_STRSTR: // checks if the 2nd arg is found in the first one |
261 | 0 | ret = strstr(term->args[0].data.str.area, term->args[1].data.str.area) != NULL; |
262 | 0 | break; |
263 | | |
264 | 0 | case CFG_PRED_VERSION_ATLEAST: // checks if the current version is at least this one |
265 | 0 | ret = compare_current_version(term->args[0].data.str.area) <= 0; |
266 | 0 | break; |
267 | | |
268 | 0 | case CFG_PRED_VERSION_BEFORE: // checks if the current version is older than this one |
269 | 0 | ret = compare_current_version(term->args[0].data.str.area) > 0; |
270 | 0 | break; |
271 | | |
272 | 0 | case CFG_PRED_OSSL_VERSION_ATLEAST: { // checks if the current openssl version is at least this one |
273 | 0 | int opensslret = openssl_compare_current_version(term->args[0].data.str.area); |
274 | |
|
275 | 0 | if (opensslret < -1) { /* can't parse the string or no openssl available */ |
276 | 0 | memprintf(err, "invalid argument to conditional expression predicate '%s': '%s'", term->pred->word, term->args[0].data.str.area); |
277 | 0 | ret = -1; |
278 | 0 | } |
279 | 0 | else |
280 | 0 | ret = opensslret <= 0; |
281 | 0 | break; |
282 | 0 | } |
283 | 0 | case CFG_PRED_OSSL_VERSION_BEFORE: { // checks if the current openssl version is older than this one |
284 | 0 | int opensslret = openssl_compare_current_version(term->args[0].data.str.area); |
285 | |
|
286 | 0 | if (opensslret < -1) { /* can't parse the string or no openssl available */ |
287 | 0 | memprintf(err, "invalid argument to conditional expression predicate '%s': '%s'", term->pred->word, term->args[0].data.str.area); |
288 | 0 | ret = -1; |
289 | 0 | } |
290 | 0 | else |
291 | 0 | ret = opensslret > 0; |
292 | 0 | break; |
293 | 0 | } |
294 | 0 | case CFG_PRED_AWSLC_API_ATLEAST: { // checks if the current AWSLC API is at least this one |
295 | 0 | int awslcret = awslc_compare_current_api(term->args[0].data.str.area); |
296 | |
|
297 | 0 | if (awslcret < -1) { /* can't parse the string or no AWS-LC available */ |
298 | 0 | memprintf(err, "invalid argument to conditional expression predicate '%s': '%s'", term->pred->word, term->args[0].data.str.area); |
299 | 0 | ret = -1; |
300 | 0 | } |
301 | 0 | else |
302 | 0 | ret = awslcret <= 0; |
303 | 0 | break; |
304 | 0 | } |
305 | 0 | case CFG_PRED_AWSLC_API_BEFORE: { // checks if the current AWSLC API is older than this one |
306 | 0 | int awslcret = awslc_compare_current_api(term->args[0].data.str.area); |
307 | |
|
308 | 0 | if (awslcret < -1) { /* can't parse the string or no AWS-LC available */ |
309 | 0 | memprintf(err, "invalid argument to conditional expression predicate '%s': '%s'", term->pred->word, term->args[0].data.str.area); |
310 | 0 | ret = -1; |
311 | 0 | } |
312 | 0 | else |
313 | 0 | ret = awslcret > 0; |
314 | 0 | break; |
315 | 0 | } |
316 | 0 | case CFG_PRED_SSLLIB_NAME_STARTSWITH: { // checks if the current SSL library's name starts with a specified string (can be used to distinguish OpenSSL from LibreSSL or BoringSSL) |
317 | 0 | ret = openssl_compare_current_name(term->args[0].data.str.area) == 0; |
318 | 0 | break; |
319 | 0 | } |
320 | 0 | case CFG_PRED_ENABLED: { // checks if the arg matches on a subset of enabled options |
321 | 0 | ret = cfg_eval_cond_enabled(term->args[0].data.str.area) != 0; |
322 | 0 | break; |
323 | 0 | } |
324 | 0 | default: |
325 | 0 | memprintf(err, "internal error: unhandled conditional expression predicate '%s'", term->pred->word); |
326 | 0 | break; |
327 | 0 | } |
328 | 0 | } |
329 | 0 | else if (term->type == CCTT_PAREN) { |
330 | 0 | ret = cfg_eval_cond_expr(term->expr, err); |
331 | 0 | } |
332 | 0 | else { |
333 | 0 | memprintf(err, "internal error: unhandled condition term type %d", (int)term->type); |
334 | 0 | } |
335 | | |
336 | 0 | if (ret >= 0 && term->neg) |
337 | 0 | ret = !ret; |
338 | 0 | return ret; |
339 | 0 | } |
340 | | |
341 | | |
342 | | /* Frees <expr> and its terms and args. NULL is supported and does nothing. */ |
343 | | void cfg_free_cond_and(struct cfg_cond_and *expr) |
344 | 0 | { |
345 | 0 | struct cfg_cond_and *prev; |
346 | |
|
347 | 0 | while (expr) { |
348 | 0 | cfg_free_cond_term(expr->left); |
349 | 0 | prev = expr; |
350 | 0 | expr = expr->right; |
351 | 0 | free(prev); |
352 | 0 | } |
353 | 0 | } |
354 | | |
355 | | /* Frees <expr> and its terms and args. NULL is supported and does nothing. */ |
356 | | void cfg_free_cond_expr(struct cfg_cond_expr *expr) |
357 | 0 | { |
358 | 0 | struct cfg_cond_expr *prev; |
359 | |
|
360 | 0 | while (expr) { |
361 | 0 | cfg_free_cond_and(expr->left); |
362 | 0 | prev = expr; |
363 | 0 | expr = expr->right; |
364 | 0 | free(prev); |
365 | 0 | } |
366 | 0 | } |
367 | | |
368 | | /* Parse an indirect input text as a possible config condition sub-expr. |
369 | | * Returns <0 on parsing error, 0 if the parser is desynchronized, or >0 on |
370 | | * success. <expr> is filled with the parsed info, and <text> is updated on |
371 | | * success to point to the first unparsed character, or is left untouched |
372 | | * on failure. On success, the caller will have to free all lower-level |
373 | | * allocated structs using cfg_free_cond_and(). An error will be set in |
374 | | * <err> on error, and only in this case. In this case the first bad |
375 | | * character will be reported in <errptr>. <maxdepth> corresponds to the |
376 | | * maximum recursion depth permitted, it is decremented on each recursive |
377 | | * call and the parsing will fail upon reaching <= 0. |
378 | | */ |
379 | | int cfg_parse_cond_and(const char **text, struct cfg_cond_and **expr, char **err, const char **errptr, int maxdepth) |
380 | 0 | { |
381 | 0 | struct cfg_cond_and *e; |
382 | 0 | const char *in = *text; |
383 | 0 | int ret = -1; |
384 | |
|
385 | 0 | if (!*in) /* empty expr does not parse */ |
386 | 0 | return 0; |
387 | | |
388 | 0 | *expr = NULL; |
389 | 0 | if (maxdepth <= 0) { |
390 | 0 | memprintf(err, "unparsable conditional sub-expression '%s'", in); |
391 | 0 | if (errptr) |
392 | 0 | *errptr = in; |
393 | 0 | goto done; |
394 | 0 | } |
395 | | |
396 | 0 | e = *expr = calloc(1, sizeof(**expr)); |
397 | 0 | if (!e) { |
398 | 0 | memprintf(err, "memory allocation error while parsing conditional expression '%s'", *text); |
399 | 0 | goto done; |
400 | 0 | } |
401 | | |
402 | 0 | ret = cfg_parse_cond_term(&in, &e->left, err, errptr, maxdepth - 1); |
403 | 0 | if (ret == -1) // parse error, error already reported |
404 | 0 | goto done; |
405 | | |
406 | 0 | if (ret == 0) { |
407 | | /* ret == 0, no other way to parse this */ |
408 | 0 | memprintf(err, "unparsable conditional sub-expression '%s'", in); |
409 | 0 | if (errptr) |
410 | 0 | *errptr = in; |
411 | 0 | ret = -1; |
412 | 0 | goto done; |
413 | 0 | } |
414 | | |
415 | | /* ret=1, we have a term in the left hand set */ |
416 | | |
417 | | /* find an optional '&&' */ |
418 | 0 | while (*in == ' ' || *in == '\t') |
419 | 0 | in++; |
420 | |
|
421 | 0 | *text = in; |
422 | 0 | if (in[0] != '&' || in[1] != '&') |
423 | 0 | goto done; |
424 | | |
425 | | /* we have a '&&', let's parse the right handset's subexp */ |
426 | 0 | in += 2; |
427 | 0 | while (*in == ' ' || *in == '\t') |
428 | 0 | in++; |
429 | |
|
430 | 0 | ret = cfg_parse_cond_and(&in, &e->right, err, errptr, maxdepth - 1); |
431 | 0 | if (ret > 0) |
432 | 0 | *text = in; |
433 | 0 | done: |
434 | 0 | if (ret < 0) { |
435 | 0 | cfg_free_cond_and(*expr); |
436 | 0 | *expr = NULL; |
437 | 0 | } |
438 | 0 | return ret; |
439 | 0 | } |
440 | | |
441 | | /* Parse an indirect input text as a possible config condition expression. |
442 | | * Returns <0 on parsing error, 0 if the parser is desynchronized, or >0 on |
443 | | * success. <expr> is filled with the parsed info, and <text> is updated on |
444 | | * success to point to the first unparsed character, or is left untouched |
445 | | * on failure. On success, the caller will have to free all lower-level |
446 | | * allocated structs using cfg_free_cond_expr(). An error will be set in |
447 | | * <err> on error, and only in this case. In this case the first bad |
448 | | * character will be reported in <errptr>. <maxdepth> corresponds to the |
449 | | * maximum recursion depth permitted, it is decremented on each recursive call |
450 | | * and the parsing will fail upon reaching <= 0. |
451 | | */ |
452 | | int cfg_parse_cond_expr(const char **text, struct cfg_cond_expr **expr, char **err, const char **errptr, int maxdepth) |
453 | 0 | { |
454 | 0 | struct cfg_cond_expr *e; |
455 | 0 | const char *in = *text; |
456 | 0 | int ret = -1; |
457 | |
|
458 | 0 | if (!*in) /* empty expr does not parse */ |
459 | 0 | return 0; |
460 | | |
461 | 0 | *expr = NULL; |
462 | 0 | if (maxdepth <= 0) { |
463 | 0 | memprintf(err, "unparsable conditional expression '%s'", in); |
464 | 0 | if (errptr) |
465 | 0 | *errptr = in; |
466 | 0 | goto done; |
467 | 0 | } |
468 | | |
469 | 0 | e = *expr = calloc(1, sizeof(**expr)); |
470 | 0 | if (!e) { |
471 | 0 | memprintf(err, "memory allocation error while parsing conditional expression '%s'", *text); |
472 | 0 | goto done; |
473 | 0 | } |
474 | | |
475 | 0 | ret = cfg_parse_cond_and(&in, &e->left, err, errptr, maxdepth - 1); |
476 | 0 | if (ret == -1) // parse error, error already reported |
477 | 0 | goto done; |
478 | | |
479 | 0 | if (ret == 0) { |
480 | | /* ret == 0, no other way to parse this */ |
481 | 0 | memprintf(err, "unparsable conditional expression '%s'", in); |
482 | 0 | if (errptr) |
483 | 0 | *errptr = in; |
484 | 0 | ret = -1; |
485 | 0 | goto done; |
486 | 0 | } |
487 | | |
488 | | /* ret=1, we have a sub-expr in the left hand set */ |
489 | | |
490 | | /* find an optional '||' */ |
491 | 0 | while (*in == ' ' || *in == '\t') |
492 | 0 | in++; |
493 | |
|
494 | 0 | *text = in; |
495 | 0 | if (in[0] != '|' || in[1] != '|') |
496 | 0 | goto done; |
497 | | |
498 | | /* we have a '||', let's parse the right handset's subexp */ |
499 | 0 | in += 2; |
500 | 0 | while (*in == ' ' || *in == '\t') |
501 | 0 | in++; |
502 | |
|
503 | 0 | ret = cfg_parse_cond_expr(&in, &e->right, err, errptr, maxdepth - 1); |
504 | 0 | if (ret > 0) |
505 | 0 | *text = in; |
506 | 0 | done: |
507 | 0 | if (ret < 0) { |
508 | 0 | cfg_free_cond_expr(*expr); |
509 | 0 | *expr = NULL; |
510 | 0 | } |
511 | 0 | return ret; |
512 | 0 | } |
513 | | |
514 | | /* evaluate a sub-expression on a .if/.elif line. The expression is valid and |
515 | | * was already parsed in <expr>. Returns -1 on error (in which case err is |
516 | | * filled with a message, and only in this case), 0 if the condition is false, |
517 | | * 1 if it's true. |
518 | | */ |
519 | | int cfg_eval_cond_and(struct cfg_cond_and *expr, char **err) |
520 | 0 | { |
521 | 0 | int ret; |
522 | | |
523 | | /* AND: loop on terms and sub-exp's terms as long as they're TRUE |
524 | | * (stop on FALSE and ERROR). |
525 | | */ |
526 | 0 | while ((ret = cfg_eval_cond_term(expr->left, err)) > 0 && expr->right) |
527 | 0 | expr = expr->right; |
528 | 0 | return ret; |
529 | 0 | } |
530 | | |
531 | | /* evaluate an expression on a .if/.elif line. The expression is valid and was |
532 | | * already parsed in <expr>. Returns -1 on error (in which case err is filled |
533 | | * with a message, and only in this case), 0 if the condition is false, 1 if |
534 | | * it's true. |
535 | | */ |
536 | | int cfg_eval_cond_expr(struct cfg_cond_expr *expr, char **err) |
537 | 0 | { |
538 | 0 | int ret; |
539 | | |
540 | | /* OR: loop on sub-exps as long as they're FALSE (stop on TRUE and ERROR) */ |
541 | 0 | while ((ret = cfg_eval_cond_and(expr->left, err)) == 0 && expr->right) |
542 | 0 | expr = expr->right; |
543 | 0 | return ret; |
544 | 0 | } |
545 | | |
546 | | /* evaluate a condition on a .if/.elif line. The condition is already tokenized |
547 | | * in <args>. Returns -1 on error (in which case err is filled with a message, |
548 | | * and only in this case), 0 if the condition is false, 1 if it's true. If |
549 | | * <errptr> is not NULL, it's set to the first invalid character on error. |
550 | | */ |
551 | | int cfg_eval_condition(char **args, char **err, const char **errptr) |
552 | 0 | { |
553 | 0 | struct cfg_cond_expr *expr = NULL; |
554 | 0 | const char *text = args[0]; |
555 | 0 | int ret = -1; |
556 | |
|
557 | 0 | if (!*text) /* note: empty = false */ |
558 | 0 | return 0; |
559 | | |
560 | 0 | ret = cfg_parse_cond_expr(&text, &expr, err, errptr, MAX_CFG_RECURSION); |
561 | 0 | if (ret != 0) { |
562 | 0 | if (ret == -1) // parse error, error already reported |
563 | 0 | goto done; |
564 | 0 | while (*text == ' ' || *text == '\t') |
565 | 0 | text++; |
566 | |
|
567 | 0 | if (*text) { |
568 | 0 | ret = -1; |
569 | 0 | memprintf(err, "unexpected character '%c' at the end of conditional expression '%s'", |
570 | 0 | *text, args[0]); |
571 | 0 | goto fail; |
572 | 0 | } |
573 | | |
574 | 0 | ret = cfg_eval_cond_expr(expr, err); |
575 | 0 | if (ret < 0) |
576 | 0 | goto fail; |
577 | 0 | goto done; |
578 | 0 | } |
579 | | |
580 | | /* ret == 0, no other way to parse this */ |
581 | 0 | ret = -1; |
582 | 0 | memprintf(err, "unparsable conditional expression '%s'", args[0]); |
583 | 0 | fail: |
584 | 0 | if (errptr) |
585 | 0 | *errptr = text; |
586 | 0 | done: |
587 | 0 | cfg_free_cond_expr(expr); |
588 | 0 | return ret; |
589 | 0 | } |