/** * @file test.c * @brief The `test` and `[` builtins. */ #include #include #include #include #include #include #include #include "exec.h" /** * @brief State of one `test` evaluation: the arguments and the read position. */ typedef struct test_state { char **argv; int pos; int argc; bool error; } test_state; static bool parse_or(test_state *t); /** * @brief Number of arguments not yet consumed. */ static int remaining(const test_state *t) { return t->argc - t->pos; } /** * @brief Is this a binary operator of `test` (`=`, `-eq`, `-nt` ...)? */ static bool is_binary_op(const char *s) { static const char *const ops[] = { "=", "==", "!=", "<", ">", "-eq", "-ne", "-lt", "-le", "-gt", "-ge", "-nt", "-ot", "-ef", }; for (size_t i = 0; i < ARRAY_LEN(ops); i++) { if (!strcmp(s, ops[i])) return true; } return false; } /** * @brief Is this a unary operator of `test` (`-f`, `-z` ...)? */ static bool is_unary_op(const char *s) { return s[0] == '-' && s[1] && !s[2] && strchr("bcdefghkLnprsStuwxzOG", s[1]); } /** * @brief Convert an operand of a numeric comparison. * @param t State. `error` is set if `s` is not an integer. * @param s The operand. * @return Its value. */ static intmax_t to_int(test_state *t, const char *s) { char *end; errno = 0; intmax_t v = strtoimax(s, &end, 10); while (*end == ' ' || *end == '\t') end++; if (end == s || *end || errno) { sh_warn("test: %s: integer expected", s); t->error = true; } return v; } /** * @brief Evaluate a unary test such as `-f file` or `-n string`. * @param op The operator's letter. * @param arg Its operand. */ bool test_unary(char op, const char *arg) { struct stat st; switch (op) { case 'n': return *arg != '\0'; case 'z': return *arg == '\0'; case 't': { long fd; return parse_long(arg, &fd) && fd >= 0 && fd <= INT_MAX && isatty((int)fd) != 0; } case 'r': return access(arg, R_OK) == 0; case 'w': return access(arg, W_OK) == 0; case 'x': return access(arg, X_OK) == 0; case 'L': case 'h': return lstat(arg, &st) == 0 && S_ISLNK(st.st_mode); default: break; } if (stat(arg, &st) != 0) return false; switch (op) { case 'e': return true; case 'f': return S_ISREG(st.st_mode); case 'd': return S_ISDIR(st.st_mode); case 'b': return S_ISBLK(st.st_mode); case 'c': return S_ISCHR(st.st_mode); case 'p': return S_ISFIFO(st.st_mode); case 'S': return S_ISSOCK(st.st_mode); case 's': return st.st_size > 0; case 'g': return (st.st_mode & S_ISGID) != 0; case 'u': return (st.st_mode & S_ISUID) != 0; case 'k': return (st.st_mode & S_ISVTX) != 0; case 'O': return st.st_uid == geteuid(); case 'G': return st.st_gid == getegid(); default: return false; } } /** * @brief Compare two files: `-nt`, `-ot` or `-ef`. * * As in POSIX.1-2024, an existing file is newer than a missing one. * * @param a First file. * @param op `n` (newer than), `o` (older than) or `e` (same file). * @param b Second file. */ bool test_files(const char *a, char op, const char *b) { struct stat sa; struct stat sb; bool ha = stat(a, &sa) == 0; bool hb = stat(b, &sb) == 0; if (op == 'e') return ha && hb && sa.st_dev == sb.st_dev && sa.st_ino == sb.st_ino; if (op == 'n') return ha && (!hb || sa.st_mtime > sb.st_mtime); return hb && (!ha || sa.st_mtime < sb.st_mtime); } /** * @brief Evaluate a binary test. * @param t State, for error reporting. * @param a Left operand. * @param op The operator. * @param b Right operand. */ static bool eval_binary(test_state *t, const char *a, const char *op, const char *b) { if (op[0] != '-') { int c = strcmp(a, b); if (op[0] == '=') return c == 0; if (op[0] == '!') return c != 0; return op[0] == '<' ? c < 0 : c > 0; } if (!strcmp(op, "-nt") || !strcmp(op, "-ot") || !strcmp(op, "-ef")) return test_files(a, op[1], b); intmax_t x = to_int(t, a); intmax_t y = to_int(t, b); if (!strcmp(op, "-eq")) return x == y; if (!strcmp(op, "-ne")) return x != y; if (!strcmp(op, "-lt")) return x < y; if (!strcmp(op, "-le")) return x <= y; if (!strcmp(op, "-gt")) return x > y; return x >= y; } /** * @brief Is the argument after the current one a binary operator that has both * of its operands? */ static bool binary_next(const test_state *t) { return remaining(t) >= 3 && is_binary_op(t->argv[t->pos + 1]); } /** * @brief Parse one primary: `( expr )`, a unary or binary test, or a bare * string (true if not empty). */ static bool parse_primary(test_state *t) { if (remaining(t) == 0) { sh_warn("test: argument expected"); t->error = true; return false; } const char *a = t->argv[t->pos]; if (binary_next(t)) { const char *op = t->argv[t->pos + 1]; const char *b = t->argv[t->pos + 2]; t->pos += 3; return eval_binary(t, a, op, b); } if (!strcmp(a, "(") && remaining(t) >= 3) { t->pos++; bool v = parse_or(t); if (remaining(t) == 0 || strcmp(t->argv[t->pos], ")")) { sh_warn("test: missing )"); t->error = true; return false; } t->pos++; return v; } if (is_unary_op(a) && remaining(t) >= 2) { t->pos += 2; return test_unary(a[1], t->argv[t->pos - 1]); } t->pos++; return *a != '\0'; } /** * @brief Parse `! expr`. */ static bool parse_not(test_state *t) { if (remaining(t) >= 2 && !strcmp(t->argv[t->pos], "!") && !binary_next(t)) { t->pos++; return !parse_not(t); } return parse_primary(t); } /** * @brief Parse `expr -a expr`. */ static bool parse_and(test_state *t) { bool v = parse_not(t); while (remaining(t) > 0 && !strcmp(t->argv[t->pos], "-a")) { t->pos++; bool rhs = parse_not(t); v = v && rhs; } return v; } /** * @brief Parse `expr -o expr`. */ static bool parse_or(test_state *t) { bool v = parse_and(t); while (remaining(t) > 0 && !strcmp(t->argv[t->pos], "-o")) { t->pos++; bool rhs = parse_and(t); v = v || rhs; } return v; } /** * @brief Evaluate the POSIX two-argument `test` rule. */ static bool eval_2(test_state *t, const char *a, const char *b) { if (!strcmp(a, "!")) return *b == '\0'; if (is_unary_op(a)) return test_unary(a[1], b); sh_warn("test: %s: unary operator expected", a); t->error = true; return false; } /** * @brief Evaluate the POSIX three-argument `test` rule (plus XSI `-a`/`-o`). */ static bool eval_3(test_state *t, const char *a, const char *b, const char *c) { if (is_binary_op(b)) return eval_binary(t, a, b, c); if (strcmp(a, "!") == 0) return !eval_2(t, b, c); if (strcmp(a, "(") == 0 && strcmp(c, ")") == 0) return *b != '\0'; if (strcmp(b, "-a") == 0) return (*a != '\0') && (*c != '\0'); if (strcmp(b, "-o") == 0) return (*a != '\0') || (*c != '\0'); sh_warn("test: %s: unexpected operator", b); t->error = true; return false; } /** * @brief The `test` and `[` builtins. * * With up to four arguments the POSIX rules by argument count are * followed exactly, so that operands that look like operators are not * misread. Longer expressions use the recursive parser. * * @return 0 if the expression is true, 1 if false, 2 on a syntax error. */ int builtin_test(int argc, char **argv) { if (!strcmp(argv[0], "[")) { if (argc < 2 || strcmp(argv[argc - 1], "]")) { sh_warn("[: missing ]"); return 2; } argc--; } int n = argc - 1; if (n == 0) return 1; if (n == 1) return argv[1][0] != '\0' ? 0 : 1; test_state t = {.argv = argv, .pos = 1, .argc = argc}; bool v; if (n == 2) { v = eval_2(&t, argv[1], argv[2]); } else if (n == 3) { v = eval_3(&t, argv[1], argv[2], argv[3]); } else if (n == 4 && strcmp(argv[1], "!") == 0) { v = !eval_3(&t, argv[2], argv[3], argv[4]); } else if (n == 4 && strcmp(argv[1], "(") == 0 && strcmp(argv[4], ")") == 0) { v = eval_2(&t, argv[2], argv[3]); } else { v = parse_or(&t); if (!t.error && remaining(&t) > 0) { sh_warn("test: %s: unexpected operator", t.argv[t.pos]); t.error = true; } } if (t.error) return 2; return v ? 0 : 1; }