Jiri Olsa | 576a65b | 2020-02-28 10:36:12 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jiri Olsa | 26226a9 | 2020-02-28 10:36:13 +0100 | [diff] [blame] | 2 | #include <stdbool.h> |
Jiri Olsa | 576a65b | 2020-02-28 10:36:12 +0100 | [diff] [blame] | 3 | #include <assert.h> |
| 4 | #include "expr.h" |
Jiri Olsa | 26226a9 | 2020-02-28 10:36:13 +0100 | [diff] [blame] | 5 | #include "expr-bison.h" |
Jiri Olsa | 26226a9 | 2020-02-28 10:36:13 +0100 | [diff] [blame] | 6 | #include "expr-flex.h" |
| 7 | |
| 8 | #ifdef PARSER_DEBUG |
| 9 | extern int expr_debug; |
| 10 | #endif |
Jiri Olsa | 576a65b | 2020-02-28 10:36:12 +0100 | [diff] [blame] | 11 | |
| 12 | /* Caller must make sure id is allocated */ |
Jiri Olsa | aecce63 | 2020-04-02 02:03:34 +0530 | [diff] [blame] | 13 | void expr__add_id(struct expr_parse_ctx *ctx, const char *name, double val) |
Jiri Olsa | 576a65b | 2020-02-28 10:36:12 +0100 | [diff] [blame] | 14 | { |
| 15 | int idx; |
| 16 | |
| 17 | assert(ctx->num_ids < MAX_PARSE_ID); |
| 18 | idx = ctx->num_ids++; |
| 19 | ctx->ids[idx].name = name; |
| 20 | ctx->ids[idx].val = val; |
| 21 | } |
| 22 | |
Jiri Olsa | aecce63 | 2020-04-02 02:03:34 +0530 | [diff] [blame] | 23 | void expr__ctx_init(struct expr_parse_ctx *ctx) |
Jiri Olsa | 576a65b | 2020-02-28 10:36:12 +0100 | [diff] [blame] | 24 | { |
| 25 | ctx->num_ids = 0; |
| 26 | } |
Jiri Olsa | 26226a9 | 2020-02-28 10:36:13 +0100 | [diff] [blame] | 27 | |
| 28 | static int |
Jiri Olsa | aecce63 | 2020-04-02 02:03:34 +0530 | [diff] [blame] | 29 | __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr, |
Kajol Jain | 1e1a873 | 2020-04-02 02:03:37 +0530 | [diff] [blame] | 30 | int start, int runtime) |
Jiri Olsa | 26226a9 | 2020-02-28 10:36:13 +0100 | [diff] [blame] | 31 | { |
Jiri Olsa | 871f9f5 | 2020-04-02 02:03:35 +0530 | [diff] [blame] | 32 | struct expr_scanner_ctx scanner_ctx = { |
| 33 | .start_token = start, |
Kajol Jain | 1e1a873 | 2020-04-02 02:03:37 +0530 | [diff] [blame] | 34 | .runtime = runtime, |
Jiri Olsa | 871f9f5 | 2020-04-02 02:03:35 +0530 | [diff] [blame] | 35 | }; |
Jiri Olsa | 26226a9 | 2020-02-28 10:36:13 +0100 | [diff] [blame] | 36 | YY_BUFFER_STATE buffer; |
| 37 | void *scanner; |
| 38 | int ret; |
| 39 | |
Jiri Olsa | 871f9f5 | 2020-04-02 02:03:35 +0530 | [diff] [blame] | 40 | ret = expr_lex_init_extra(&scanner_ctx, &scanner); |
Jiri Olsa | 26226a9 | 2020-02-28 10:36:13 +0100 | [diff] [blame] | 41 | if (ret) |
| 42 | return ret; |
| 43 | |
| 44 | buffer = expr__scan_string(expr, scanner); |
| 45 | |
| 46 | #ifdef PARSER_DEBUG |
| 47 | expr_debug = 1; |
Ian Rogers | e5e0e63 | 2020-05-01 10:33:28 -0700 | [diff] [blame^] | 48 | expr_set_debug(1, scanner); |
Jiri Olsa | 26226a9 | 2020-02-28 10:36:13 +0100 | [diff] [blame] | 49 | #endif |
| 50 | |
| 51 | ret = expr_parse(val, ctx, scanner); |
| 52 | |
| 53 | expr__flush_buffer(buffer, scanner); |
| 54 | expr__delete_buffer(buffer, scanner); |
| 55 | expr_lex_destroy(scanner); |
| 56 | return ret; |
| 57 | } |
| 58 | |
Kajol Jain | 1e1a873 | 2020-04-02 02:03:37 +0530 | [diff] [blame] | 59 | int expr__parse(double *final_val, struct expr_parse_ctx *ctx, const char *expr, int runtime) |
Jiri Olsa | 26226a9 | 2020-02-28 10:36:13 +0100 | [diff] [blame] | 60 | { |
Kajol Jain | 1e1a873 | 2020-04-02 02:03:37 +0530 | [diff] [blame] | 61 | return __expr__parse(final_val, ctx, expr, EXPR_PARSE, runtime) ? -1 : 0; |
Jiri Olsa | 26226a9 | 2020-02-28 10:36:13 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static bool |
| 65 | already_seen(const char *val, const char *one, const char **other, |
| 66 | int num_other) |
| 67 | { |
| 68 | int i; |
| 69 | |
| 70 | if (one && !strcasecmp(one, val)) |
| 71 | return true; |
| 72 | for (i = 0; i < num_other; i++) |
| 73 | if (!strcasecmp(other[i], val)) |
| 74 | return true; |
| 75 | return false; |
| 76 | } |
| 77 | |
Jiri Olsa | 0f9b1e1 | 2020-02-28 10:36:15 +0100 | [diff] [blame] | 78 | int expr__find_other(const char *expr, const char *one, const char ***other, |
Kajol Jain | 1e1a873 | 2020-04-02 02:03:37 +0530 | [diff] [blame] | 79 | int *num_other, int runtime) |
Jiri Olsa | 26226a9 | 2020-02-28 10:36:13 +0100 | [diff] [blame] | 80 | { |
| 81 | int err, i = 0, j = 0; |
Jiri Olsa | aecce63 | 2020-04-02 02:03:34 +0530 | [diff] [blame] | 82 | struct expr_parse_ctx ctx; |
Jiri Olsa | 26226a9 | 2020-02-28 10:36:13 +0100 | [diff] [blame] | 83 | |
| 84 | expr__ctx_init(&ctx); |
Kajol Jain | 1e1a873 | 2020-04-02 02:03:37 +0530 | [diff] [blame] | 85 | err = __expr__parse(NULL, &ctx, expr, EXPR_OTHER, runtime); |
Jiri Olsa | 26226a9 | 2020-02-28 10:36:13 +0100 | [diff] [blame] | 86 | if (err) |
| 87 | return -1; |
| 88 | |
| 89 | *other = malloc((ctx.num_ids + 1) * sizeof(char *)); |
| 90 | if (!*other) |
| 91 | return -ENOMEM; |
| 92 | |
| 93 | for (i = 0, j = 0; i < ctx.num_ids; i++) { |
| 94 | const char *str = ctx.ids[i].name; |
| 95 | |
| 96 | if (already_seen(str, one, *other, j)) |
| 97 | continue; |
| 98 | |
| 99 | str = strdup(str); |
| 100 | if (!str) |
| 101 | goto out; |
| 102 | (*other)[j++] = str; |
| 103 | } |
| 104 | (*other)[j] = NULL; |
| 105 | |
| 106 | out: |
| 107 | if (i != ctx.num_ids) { |
| 108 | while (--j) |
| 109 | free((char *) (*other)[i]); |
| 110 | free(*other); |
| 111 | err = -1; |
| 112 | } |
| 113 | |
| 114 | *num_other = j; |
| 115 | return err; |
| 116 | } |