| 909 | } |
| 910 | |
| 911 | static const char *sh_eval(sh_t *s, TSNode n) { |
| 912 | if (ts_node_is_null(n)) { |
| 913 | return NULL; |
| 914 | } |
| 915 | const char *t = ts_node_type(n); |
| 916 | uint32_t b = ts_node_start_byte(n); |
| 917 | uint32_t e = ts_node_end_byte(n); |
| 918 | if (strcmp(t, "word") == 0 || strcmp(t, "number") == 0 || strcmp(t, "extglob_pattern") == 0) { |
| 919 | if (s->src[b] == '~') { |
| 920 | return NULL; |
| 921 | } |
| 922 | sh_str_t out = {0}; |
| 923 | sh_add_unescaped(s->a, &out, s->src + b, e - b, false); |
| 924 | return sh_str_done(&out); |
| 925 | } |
| 926 | if (strcmp(t, "raw_string") == 0) { |
| 927 | return e - b >= PAIR_LEN ? cbm_arena_strndup(s->a, s->src + b + SKIP_ONE, e - b - PAIR_LEN) |
| 928 | : NULL; |
| 929 | } |
| 930 | if (strcmp(t, "string") == 0 || strcmp(t, "concatenation") == 0) { |
| 931 | sh_str_t out = {0}; |
| 932 | bool dq = strcmp(t, "string") == 0; |
| 933 | uint32_t from = dq ? b + SKIP_ONE : b; |
| 934 | uint32_t to = dq && e > b + SKIP_ONE ? e - SKIP_ONE : e; |
| 935 | sh_concat(s, &out, n, from, to, dq); |
| 936 | return sh_str_done(&out); |
| 937 | } |
| 938 | if (strcmp(t, "simple_expansion") == 0) { |
| 939 | TSNode v = ts_node_named_child(n, 0); |
| 940 | const char *nm = ts_node_is_null(v) ? NULL : sh_text(s, v); |
| 941 | if (!nm) { |
| 942 | return NULL; |
| 943 | } |
| 944 | bool found = false; |
| 945 | const sh_val_t *val = sh_lookup(s, nm, &found); |
| 946 | return found ? val->s : NULL; |
| 947 | } |
| 948 | if (strcmp(t, "expansion") == 0) { |
| 949 | return sh_eval_expansion(s, n); |
| 950 | } |
| 951 | if (strcmp(t, "command_substitution") == 0) { |
| 952 | return sh_eval_subst(s, n); |
| 953 | } |
| 954 | return NULL; |
| 955 | } |
| 956 | |
| 957 | /* A command word: its value; an option whose value is unknown is still that |
| 958 | * option (--context="${ctx}" -> "--context="). */ |
no test coverage detected