/** * @file cond.c * @brief Evaluating `[[ expression ]]`, the Korn shell conditional * command. * * The expression was parsed into a tree (see parse_cond_command()), so * evaluating it needs no argument vector, no builtin lookup and no * re-parsing. Operands are expanded only when they are reached, which * makes `&&` and `||` short-circuit expansions and command substitutions * too, and they are never split into fields or globbed. * * | Expression | True when | * |-------------------|---------------------------------------------------| * | `s` | s is not empty | * | `-n s`, `-z s` | s is not empty / is empty | * | `-f file` ... | the file tests of `test`. `-a file` is `-e` | * | `-o option` | the shell option is on (`-o errexit`) | * | `-v name` | the variable is set | * | `s == p`, `s = p` | s matches the pattern p (quote p to compare text) | * | `s != p` | s does not match p | * | `s =~ re` | s matches the extended regular expression re | * | `s < t`, `s > t` | s sorts before / after t (byte order) | * | `x -eq y` ... | arithmetic comparison. X and y are expressions | * | `f -nt g` ... | file comparisons, as in `test` | * * The status is 0 for true, 1 for false and 2 for a bad regular * expression. An invalid arithmetic operand is an error, as in `$(( ))`. */ #include #include #include "exec.h" #include "expand.h" /** * @brief Expand an operand: parameters, command substitution, arithmetic, * tilde, quote removal. No splitting or globbing. */ static const char *operand(const word *w) { if (w->plain) return w->plain; return expand_string(w->parts, XS_TILDE); } /** * @brief Is this a character with a meaning in an extended regular * expression? */ static bool is_ere_special(char c) { return strchr("\\.[]()*+?{}|^$", c) != nullptr; } /** * @brief Expand the right-hand side of `=~` into a regular expression. * * Unquoted parts keep their meaning as regular-expression text. Quoted * parts are escaped so that they match literally, as in bash and ksh93: * `[[ $x =~ "a.b" ]]` looks for the three characters `a.b`. * * @param w The operand. * @return The expression, in scratch memory. */ static const char *regex_text(const word *w) { strbuf sb; sb_init(&sb, &scratch); for (const wpart *p = w->parts; p; p = p->next) { wpart one = *p; one.next = nullptr; const char *s = expand_string(&one, 0); if (!p->quoted) { sb_puts(&sb, s); continue; } for (; *s; s++) { if (is_ere_special(*s)) sb_putc(&sb, '\\'); sb_putc(&sb, *s); } } return sb_cstr(&sb); } /** * @brief Evaluate `s =~ re`. * @return 0 for a match, 1 for none, 2 if `re` is not a valid * expression. */ static int regex_match(const char *s, const char *re) { regex_t rx; int rc = regcomp(&rx, re, REG_EXTENDED | REG_NOSUB); if (rc != 0) { char msg[128]; regerror(rc, &rx, msg, sizeof msg); sh_warn("[[: %s: %s", re, msg); return 2; } rc = regexec(&rx, s, 0, nullptr, 0); regfree(&rx); return rc == 0 ? 0 : 1; } /** * @brief Evaluate a unary test. * @param op The operator's letter. * @param arg The expanded operand. */ static bool unary(char op, const char *arg) { switch (op) { case 'a': return test_unary('e', arg); case 'o': return option_is_set(arg) == 1; case 'v': return var_get(arg) != nullptr; default: return test_unary(op, arg); } } /** * @brief Evaluate a binary test. * @param c The node. * @return 0, 1, or 2 for a bad regular expression. */ static int binary(const cond *c) { const char *a = operand(c->a); switch (c->op) { case CO_MATCH: case CO_NOMATCH: { /* A quoted literal is plain too, but must match only itself. */ const char *pat = c->b->plain && !c->b->parts->quoted ? c->b->plain : expand_pattern(c->b->parts); bool m = pat_match(pat, a, a + strlen(a)); return (c->op == CO_MATCH) == m ? 0 : 1; } case CO_REGEX: return regex_match(a, regex_text(c->b)); case CO_LESS: return strcmp(a, operand(c->b)) < 0 ? 0 : 1; case CO_GREATER: return strcmp(a, operand(c->b)) > 0 ? 0 : 1; case CO_NEWER: return test_files(a, 'n', operand(c->b)) ? 0 : 1; case CO_OLDER: return test_files(a, 'o', operand(c->b)) ? 0 : 1; case CO_SAMEFILE: return test_files(a, 'e', operand(c->b)) ? 0 : 1; default: break; } /* -eq and friends: both sides are arithmetic expressions. */ intmax_t x = arith_eval(a); intmax_t y = arith_eval(operand(c->b)); bool r; switch (c->op) { case CO_EQ: r = x == y; break; case CO_NE: r = x != y; break; case CO_LT: r = x < y; break; case CO_LE: r = x <= y; break; case CO_GT: r = x > y; break; default: r = x >= y; break; } return r ? 0 : 1; } /** * @brief Evaluate a `[[ ]]` expression. * @param c The expression. * @return 0 if it is true, 1 if false, 2 on a bad regular expression. */ int eval_cond(const cond *c) { int r; switch (c->kind) { case CN_AND: r = eval_cond(c->left); return r ? r : eval_cond(c->right); case CN_OR: r = eval_cond(c->left); return r == 1 ? eval_cond(c->right) : r; case CN_NOT: r = eval_cond(c->left); return r == 2 ? 2 : !r; case CN_UNARY: return unary(c->uop, operand(c->a)) ? 0 : 1; case CN_BINARY: return binary(c); case CN_STRING: return *operand(c->a) ? 0 : 1; } UNREACHABLE(); }