2016-04-20 10:49:15 +08:00
|
|
|
// Functions used for implementing the test builtin.
|
|
|
|
//
|
|
|
|
// Implemented from scratch (yes, really) by way of IEEE 1003.1 as reference.
|
2016-05-19 06:30:21 +08:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2019-10-14 06:50:48 +08:00
|
|
|
#include "builtin.h"
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2017-02-14 12:37:27 +08:00
|
|
|
|
2019-11-19 09:11:16 +08:00
|
|
|
#include <cerrno>
|
2018-07-29 15:41:03 +08:00
|
|
|
#include <cmath>
|
2019-11-19 09:11:16 +08:00
|
|
|
#include <cstdarg>
|
2019-10-14 06:50:48 +08:00
|
|
|
#include <cstring>
|
|
|
|
#include <cwchar>
|
2019-11-19 09:11:16 +08:00
|
|
|
#include <cwctype>
|
2016-04-20 10:49:15 +08:00
|
|
|
#include <memory>
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <string>
|
2017-02-11 10:47:02 +08:00
|
|
|
#include <type_traits>
|
2018-02-19 10:39:03 +08:00
|
|
|
#include <utility>
|
2012-03-07 16:54:01 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
#include "common.h"
|
2016-04-21 14:00:54 +08:00
|
|
|
#include "io.h"
|
2019-03-27 04:40:10 +08:00
|
|
|
#include "parser.h"
|
2016-04-20 10:49:15 +08:00
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2012-03-07 16:54:01 +08:00
|
|
|
|
2017-01-22 08:08:53 +08:00
|
|
|
using std::move;
|
2019-05-05 18:09:25 +08:00
|
|
|
using std::unique_ptr;
|
2017-01-22 08:08:53 +08:00
|
|
|
|
2018-08-05 06:46:04 +08:00
|
|
|
namespace {
|
2016-04-20 10:49:15 +08:00
|
|
|
namespace test_expressions {
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
enum token_t {
|
|
|
|
test_unknown, // arbitrary string
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
test_bang, // "!", inverts sense
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
test_filetype_b, // "-b", for block special files
|
|
|
|
test_filetype_c, // "-c", for character special files
|
|
|
|
test_filetype_d, // "-d", for directories
|
|
|
|
test_filetype_e, // "-e", for files that exist
|
|
|
|
test_filetype_f, // "-f", for for regular files
|
|
|
|
test_filetype_G, // "-G", for check effective group id
|
|
|
|
test_filetype_g, // "-g", for set-group-id
|
|
|
|
test_filetype_h, // "-h", for symbolic links
|
2017-03-15 12:43:15 +08:00
|
|
|
test_filetype_k, // "-k", for sticky bit
|
2016-04-20 10:49:15 +08:00
|
|
|
test_filetype_L, // "-L", same as -h
|
|
|
|
test_filetype_O, // "-O", for check effective user id
|
|
|
|
test_filetype_p, // "-p", for FIFO
|
|
|
|
test_filetype_S, // "-S", socket
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
test_filesize_s, // "-s", size greater than zero
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
test_filedesc_t, // "-t", whether the fd is associated with a terminal
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
test_fileperm_r, // "-r", read permission
|
|
|
|
test_fileperm_u, // "-u", whether file is setuid
|
|
|
|
test_fileperm_w, // "-w", whether file write permission is allowed
|
|
|
|
test_fileperm_x, // "-x", whether file execute/search is allowed
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
test_string_n, // "-n", non-empty string
|
|
|
|
test_string_z, // "-z", true if length of string is 0
|
|
|
|
test_string_equal, // "=", true if strings are identical
|
|
|
|
test_string_not_equal, // "!=", true if strings are not identical
|
2012-11-19 08:30:30 +08:00
|
|
|
|
|
|
|
test_number_equal, // "-eq", true if numbers are equal
|
|
|
|
test_number_not_equal, // "-ne", true if numbers are not equal
|
|
|
|
test_number_greater, // "-gt", true if first number is larger than second
|
|
|
|
test_number_greater_equal, // "-ge", true if first number is at least second
|
|
|
|
test_number_lesser, // "-lt", true if first number is smaller than second
|
|
|
|
test_number_lesser_equal, // "-le", true if first number is at most second
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
test_combine_and, // "-a", true if left and right are both true
|
|
|
|
test_combine_or, // "-o", true if either left or right is true
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
test_paren_open, // "(", open paren
|
|
|
|
test_paren_close, // ")", close paren
|
2012-11-19 08:30:30 +08:00
|
|
|
};
|
|
|
|
|
2018-07-29 15:41:03 +08:00
|
|
|
/// Our number type. We support both doubles and long longs. We have to support these separately
|
|
|
|
/// because some integers are not representable as doubles; these may come up in practice (e.g.
|
|
|
|
/// inodes).
|
|
|
|
class number_t {
|
|
|
|
// A number has an integral base and a floating point delta.
|
|
|
|
// Conceptually the number is base + delta.
|
|
|
|
// We enforce the property that 0 <= delta < 1.
|
|
|
|
long long base;
|
|
|
|
double delta;
|
|
|
|
|
|
|
|
public:
|
|
|
|
number_t(long long base, double delta) : base(base), delta(delta) {
|
|
|
|
assert(0.0 <= delta && delta < 1.0 && "Invalid delta");
|
|
|
|
}
|
|
|
|
number_t() : number_t(0, 0.0) {}
|
|
|
|
|
|
|
|
// Compare two numbers. Returns an integer -1, 0, 1 corresponding to whether we are less than,
|
|
|
|
// equal to, or greater than the rhs.
|
|
|
|
int compare(number_t rhs) const {
|
|
|
|
if (this->base != rhs.base) return (this->base > rhs.base) - (this->base < rhs.base);
|
|
|
|
return (this->delta > rhs.delta) - (this->delta < rhs.delta);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if the number is a tty()/
|
|
|
|
bool isatty() const {
|
|
|
|
if (delta != 0.0 || base > INT_MAX || base < INT_MIN) return false;
|
|
|
|
return ::isatty(static_cast<int>(base));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
static bool binary_primary_evaluate(test_expressions::token_t token, const wcstring &left,
|
|
|
|
const wcstring &right, wcstring_list_t &errors);
|
|
|
|
static bool unary_primary_evaluate(test_expressions::token_t token, const wcstring &arg,
|
|
|
|
wcstring_list_t &errors);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
enum { UNARY_PRIMARY = 1 << 0, BINARY_PRIMARY = 1 << 1 };
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2018-11-04 00:27:01 +08:00
|
|
|
struct token_info_t {
|
2012-11-19 08:30:30 +08:00
|
|
|
token_t tok;
|
|
|
|
unsigned int flags;
|
2018-11-04 00:27:01 +08:00
|
|
|
};
|
|
|
|
|
2019-01-14 07:03:19 +08:00
|
|
|
const token_info_t *token_for_string(const wcstring &str) {
|
2018-11-04 03:16:56 +08:00
|
|
|
static const std::map<wcstring, const token_info_t> token_infos = {
|
|
|
|
{L"", {test_unknown, 0}},
|
|
|
|
{L"!", {test_bang, 0}},
|
|
|
|
{L"-b", {test_filetype_b, UNARY_PRIMARY}},
|
|
|
|
{L"-c", {test_filetype_c, UNARY_PRIMARY}},
|
|
|
|
{L"-d", {test_filetype_d, UNARY_PRIMARY}},
|
|
|
|
{L"-e", {test_filetype_e, UNARY_PRIMARY}},
|
|
|
|
{L"-f", {test_filetype_f, UNARY_PRIMARY}},
|
|
|
|
{L"-G", {test_filetype_G, UNARY_PRIMARY}},
|
|
|
|
{L"-g", {test_filetype_g, UNARY_PRIMARY}},
|
|
|
|
{L"-h", {test_filetype_h, UNARY_PRIMARY}},
|
|
|
|
{L"-k", {test_filetype_k, UNARY_PRIMARY}},
|
|
|
|
{L"-L", {test_filetype_L, UNARY_PRIMARY}},
|
|
|
|
{L"-O", {test_filetype_O, UNARY_PRIMARY}},
|
|
|
|
{L"-p", {test_filetype_p, UNARY_PRIMARY}},
|
|
|
|
{L"-S", {test_filetype_S, UNARY_PRIMARY}},
|
|
|
|
{L"-s", {test_filesize_s, UNARY_PRIMARY}},
|
|
|
|
{L"-t", {test_filedesc_t, UNARY_PRIMARY}},
|
|
|
|
{L"-r", {test_fileperm_r, UNARY_PRIMARY}},
|
|
|
|
{L"-u", {test_fileperm_u, UNARY_PRIMARY}},
|
|
|
|
{L"-w", {test_fileperm_w, UNARY_PRIMARY}},
|
|
|
|
{L"-x", {test_fileperm_x, UNARY_PRIMARY}},
|
|
|
|
{L"-n", {test_string_n, UNARY_PRIMARY}},
|
|
|
|
{L"-z", {test_string_z, UNARY_PRIMARY}},
|
|
|
|
{L"=", {test_string_equal, BINARY_PRIMARY}},
|
|
|
|
{L"!=", {test_string_not_equal, BINARY_PRIMARY}},
|
|
|
|
{L"-eq", {test_number_equal, BINARY_PRIMARY}},
|
|
|
|
{L"-ne", {test_number_not_equal, BINARY_PRIMARY}},
|
|
|
|
{L"-gt", {test_number_greater, BINARY_PRIMARY}},
|
|
|
|
{L"-ge", {test_number_greater_equal, BINARY_PRIMARY}},
|
|
|
|
{L"-lt", {test_number_lesser, BINARY_PRIMARY}},
|
|
|
|
{L"-le", {test_number_lesser_equal, BINARY_PRIMARY}},
|
|
|
|
{L"-a", {test_combine_and, 0}},
|
|
|
|
{L"-o", {test_combine_or, 0}},
|
|
|
|
{L"(", {test_paren_open, 0}},
|
|
|
|
{L")", {test_paren_close, 0}}};
|
|
|
|
|
2018-11-04 00:27:01 +08:00
|
|
|
auto t = token_infos.find(str);
|
|
|
|
if (t != token_infos.end()) return &t->second;
|
|
|
|
return &token_infos.find(L"")->second;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-03-07 16:54:01 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Grammar.
|
|
|
|
//
|
|
|
|
// <expr> = <combining_expr>
|
|
|
|
//
|
|
|
|
// <combining_expr> = <unary_expr> and/or <combining_expr> |
|
|
|
|
// <unary_expr>
|
|
|
|
//
|
|
|
|
// <unary_expr> = bang <unary_expr> |
|
|
|
|
// <primary>
|
|
|
|
//
|
|
|
|
// <primary> = <unary_primary> arg |
|
|
|
|
// arg <binary_primary> arg |
|
|
|
|
// '(' <expr> ')'
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
class expression;
|
2016-04-20 10:49:15 +08:00
|
|
|
class test_parser {
|
|
|
|
private:
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring_list_t strings;
|
|
|
|
wcstring_list_t errors;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-01-22 08:08:53 +08:00
|
|
|
unique_ptr<expression> error(const wchar_t *fmt, ...);
|
2012-11-19 08:30:30 +08:00
|
|
|
void add_error(const wchar_t *fmt, ...);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
const wcstring &arg(unsigned int idx) { return strings.at(idx); }
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
public:
|
2018-02-19 10:39:03 +08:00
|
|
|
explicit test_parser(wcstring_list_t val) : strings(std::move(val)) {}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-01-22 08:08:53 +08:00
|
|
|
unique_ptr<expression> parse_expression(unsigned int start, unsigned int end);
|
|
|
|
unique_ptr<expression> parse_3_arg_expression(unsigned int start, unsigned int end);
|
|
|
|
unique_ptr<expression> parse_4_arg_expression(unsigned int start, unsigned int end);
|
|
|
|
unique_ptr<expression> parse_combining_expression(unsigned int start, unsigned int end);
|
|
|
|
unique_ptr<expression> parse_unary_expression(unsigned int start, unsigned int end);
|
|
|
|
|
|
|
|
unique_ptr<expression> parse_primary(unsigned int start, unsigned int end);
|
|
|
|
unique_ptr<expression> parse_parenthentical(unsigned int start, unsigned int end);
|
|
|
|
unique_ptr<expression> parse_unary_primary(unsigned int start, unsigned int end);
|
|
|
|
unique_ptr<expression> parse_binary_primary(unsigned int start, unsigned int end);
|
|
|
|
unique_ptr<expression> parse_just_a_string(unsigned int start, unsigned int end);
|
|
|
|
|
2017-01-27 12:00:43 +08:00
|
|
|
static unique_ptr<expression> parse_args(const wcstring_list_t &args, wcstring &err,
|
2017-01-22 08:08:53 +08:00
|
|
|
wchar_t *program_name);
|
2012-11-19 08:30:30 +08:00
|
|
|
};
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
struct range_t {
|
2012-11-19 08:30:30 +08:00
|
|
|
unsigned int start;
|
|
|
|
unsigned int end;
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
range_t(unsigned s, unsigned e) : start(s), end(e) {}
|
2012-11-19 08:30:30 +08:00
|
|
|
};
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
/// Base class for expressions.
|
|
|
|
class expression {
|
|
|
|
protected:
|
2019-12-22 05:53:48 +08:00
|
|
|
expression(token_t what, range_t where) : token(what), range(where) {}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
public:
|
2012-11-19 08:30:30 +08:00
|
|
|
const token_t token;
|
|
|
|
range_t range;
|
|
|
|
|
2018-02-19 10:44:58 +08:00
|
|
|
virtual ~expression() = default;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2017-05-04 15:18:02 +08:00
|
|
|
/// Evaluate returns true if the expression is true (i.e. STATUS_CMD_OK).
|
2012-11-19 08:30:30 +08:00
|
|
|
virtual bool evaluate(wcstring_list_t &errors) = 0;
|
|
|
|
};
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
/// Single argument like -n foo or "just a string".
|
|
|
|
class unary_primary : public expression {
|
|
|
|
public:
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring arg;
|
2018-02-19 10:39:03 +08:00
|
|
|
unary_primary(token_t tok, range_t where, wcstring what)
|
|
|
|
: expression(tok, where), arg(std::move(what)) {}
|
2018-02-19 10:50:35 +08:00
|
|
|
bool evaluate(wcstring_list_t &errors) override;
|
2012-11-19 08:30:30 +08:00
|
|
|
};
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
/// Two argument primary like foo != bar.
|
|
|
|
class binary_primary : public expression {
|
|
|
|
public:
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring arg_left;
|
|
|
|
wcstring arg_right;
|
|
|
|
|
2018-02-19 10:39:03 +08:00
|
|
|
binary_primary(token_t tok, range_t where, wcstring left, wcstring right)
|
|
|
|
: expression(tok, where), arg_left(std::move(left)), arg_right(std::move(right)) {}
|
2018-02-19 10:50:35 +08:00
|
|
|
bool evaluate(wcstring_list_t &errors) override;
|
2012-11-19 08:30:30 +08:00
|
|
|
};
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
/// Unary operator like bang.
|
|
|
|
class unary_operator : public expression {
|
|
|
|
public:
|
2017-01-22 08:08:53 +08:00
|
|
|
unique_ptr<expression> subject;
|
|
|
|
unary_operator(token_t tok, range_t where, unique_ptr<expression> exp)
|
|
|
|
: expression(tok, where), subject(move(exp)) {}
|
2018-02-19 10:50:35 +08:00
|
|
|
bool evaluate(wcstring_list_t &errors) override;
|
2012-11-19 08:30:30 +08:00
|
|
|
};
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
/// Combining expression. Contains a list of AND or OR expressions. It takes more than two so that
|
|
|
|
/// we don't have to worry about precedence in the parser.
|
|
|
|
class combining_expression : public expression {
|
|
|
|
public:
|
2017-01-22 08:08:53 +08:00
|
|
|
const std::vector<unique_ptr<expression>> subjects;
|
2012-11-19 08:30:30 +08:00
|
|
|
const std::vector<token_t> combiners;
|
|
|
|
|
2017-01-22 08:08:53 +08:00
|
|
|
combining_expression(token_t tok, range_t where, std::vector<unique_ptr<expression>> exprs,
|
2019-12-22 05:50:58 +08:00
|
|
|
std::vector<token_t> combs)
|
2017-02-11 10:47:02 +08:00
|
|
|
: expression(tok, where), subjects(std::move(exprs)), combiners(std::move(combs)) {
|
2016-04-20 10:49:15 +08:00
|
|
|
// We should have one more subject than combiner.
|
2012-11-19 08:30:30 +08:00
|
|
|
assert(subjects.size() == combiners.size() + 1);
|
2012-03-07 16:54:01 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2018-02-19 10:50:35 +08:00
|
|
|
~combining_expression() override = default;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2018-02-19 10:50:35 +08:00
|
|
|
bool evaluate(wcstring_list_t &errors) override;
|
2012-11-19 08:30:30 +08:00
|
|
|
};
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
/// Parenthetical expression.
|
|
|
|
class parenthetical_expression : public expression {
|
|
|
|
public:
|
2017-01-22 08:08:53 +08:00
|
|
|
unique_ptr<expression> contents;
|
|
|
|
parenthetical_expression(token_t tok, range_t where, unique_ptr<expression> expr)
|
|
|
|
: expression(tok, where), contents(move(expr)) {}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2018-02-19 10:50:35 +08:00
|
|
|
bool evaluate(wcstring_list_t &errors) override;
|
2012-11-19 08:30:30 +08:00
|
|
|
};
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
void test_parser::add_error(const wchar_t *fmt, ...) {
|
2019-11-19 10:34:50 +08:00
|
|
|
assert(fmt != nullptr);
|
2012-11-19 08:30:30 +08:00
|
|
|
va_list va;
|
|
|
|
va_start(va, fmt);
|
|
|
|
this->errors.push_back(vformat_string(fmt, va));
|
|
|
|
va_end(va);
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-01-22 08:08:53 +08:00
|
|
|
unique_ptr<expression> test_parser::error(const wchar_t *fmt, ...) {
|
2019-11-19 10:34:50 +08:00
|
|
|
assert(fmt != nullptr);
|
2012-11-19 08:30:30 +08:00
|
|
|
va_list va;
|
|
|
|
va_start(va, fmt);
|
|
|
|
this->errors.push_back(vformat_string(fmt, va));
|
|
|
|
va_end(va);
|
2019-11-19 10:34:50 +08:00
|
|
|
return nullptr;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-01-22 08:08:53 +08:00
|
|
|
unique_ptr<expression> test_parser::parse_unary_expression(unsigned int start, unsigned int end) {
|
2016-04-20 10:49:15 +08:00
|
|
|
if (start >= end) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return error(L"Missing argument at index %u", start);
|
|
|
|
}
|
|
|
|
token_t tok = token_for_string(arg(start))->tok;
|
2016-04-20 10:49:15 +08:00
|
|
|
if (tok == test_bang) {
|
2017-01-22 08:08:53 +08:00
|
|
|
unique_ptr<expression> subject(parse_unary_expression(start + 1, end));
|
2019-12-22 04:42:12 +08:00
|
|
|
if (subject) {
|
2017-01-27 12:00:43 +08:00
|
|
|
return make_unique<unary_operator>(tok, range_t(start, subject->range.end),
|
|
|
|
move(subject));
|
2012-03-07 16:54:01 +08:00
|
|
|
}
|
2019-11-19 10:34:50 +08:00
|
|
|
return nullptr;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
return parse_primary(start, end);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
/// Parse a combining expression (AND, OR).
|
2017-01-27 12:00:43 +08:00
|
|
|
unique_ptr<expression> test_parser::parse_combining_expression(unsigned int start,
|
|
|
|
unsigned int end) {
|
2019-11-19 10:34:50 +08:00
|
|
|
if (start >= end) return nullptr;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-01-22 08:08:53 +08:00
|
|
|
std::vector<unique_ptr<expression>> subjects;
|
2012-11-19 08:30:30 +08:00
|
|
|
std::vector<token_t> combiners;
|
|
|
|
unsigned int idx = start;
|
2013-01-07 06:48:46 +08:00
|
|
|
bool first = true;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
while (idx < end) {
|
|
|
|
if (!first) {
|
|
|
|
// This is not the first expression, so we expect a combiner.
|
2012-11-19 08:30:30 +08:00
|
|
|
token_t combiner = token_for_string(arg(idx))->tok;
|
2016-04-20 10:49:15 +08:00
|
|
|
if (combiner != test_combine_and && combiner != test_combine_or) {
|
2012-11-19 08:30:30 +08:00
|
|
|
/* Not a combiner, we're done */
|
2016-04-20 10:49:15 +08:00
|
|
|
this->errors.insert(
|
|
|
|
this->errors.begin(),
|
|
|
|
format_string(L"Expected a combining operator like '-a' at index %u", idx));
|
2012-11-19 08:30:30 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
combiners.push_back(combiner);
|
|
|
|
idx++;
|
2012-03-07 16:54:01 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Parse another expression.
|
2017-01-22 08:08:53 +08:00
|
|
|
unique_ptr<expression> expr = parse_unary_expression(idx, end);
|
2016-04-20 10:49:15 +08:00
|
|
|
if (!expr) {
|
2012-11-19 08:30:30 +08:00
|
|
|
add_error(L"Missing argument at index %u", idx);
|
2016-04-20 10:49:15 +08:00
|
|
|
if (!first) {
|
|
|
|
// Clean up the dangling combiner, since it never got its right hand expression.
|
2013-01-07 06:48:46 +08:00
|
|
|
combiners.pop_back();
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
break;
|
2012-03-07 16:54:01 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Go to the end of this expression.
|
2012-11-19 08:30:30 +08:00
|
|
|
idx = expr->range.end;
|
2017-01-22 08:08:53 +08:00
|
|
|
subjects.push_back(move(expr));
|
2013-01-07 06:48:46 +08:00
|
|
|
first = false;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-05 06:19:47 +08:00
|
|
|
if (subjects.empty()) {
|
2019-11-19 10:34:50 +08:00
|
|
|
return nullptr; // no subjects
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
// Our new expression takes ownership of all expressions we created. The token we pass is
|
|
|
|
// irrelevant.
|
2017-01-27 12:00:43 +08:00
|
|
|
return make_unique<combining_expression>(test_combine_and, range_t(start, idx), move(subjects),
|
|
|
|
move(combiners));
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2017-01-22 08:08:53 +08:00
|
|
|
unique_ptr<expression> test_parser::parse_unary_primary(unsigned int start, unsigned int end) {
|
2016-04-20 10:49:15 +08:00
|
|
|
// We need two arguments.
|
|
|
|
if (start >= end) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return error(L"Missing argument at index %u", start);
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
if (start + 1 >= end) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return error(L"Missing argument at index %u", start + 1);
|
2012-03-07 16:54:01 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// All our unary primaries are prefix, so the operator is at start.
|
2012-11-19 08:30:30 +08:00
|
|
|
const token_info_t *info = token_for_string(arg(start));
|
2019-11-19 10:34:50 +08:00
|
|
|
if (!(info->flags & UNARY_PRIMARY)) return nullptr;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-01-22 08:08:53 +08:00
|
|
|
return make_unique<unary_primary>(info->tok, range_t(start, start + 2), arg(start + 1));
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-01-22 08:08:53 +08:00
|
|
|
unique_ptr<expression> test_parser::parse_just_a_string(unsigned int start, unsigned int end) {
|
2016-04-20 10:49:15 +08:00
|
|
|
// Handle a string as a unary primary that is not a token of any other type. e.g. 'test foo -a
|
|
|
|
// bar' should evaluate to true We handle this with a unary primary of test_string_n.
|
|
|
|
|
|
|
|
// We need one argument.
|
|
|
|
if (start >= end) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return error(L"Missing argument at index %u", start);
|
2012-05-21 03:58:03 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
const token_info_t *info = token_for_string(arg(start));
|
2016-04-20 10:49:15 +08:00
|
|
|
if (info->tok != test_unknown) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return error(L"Unexpected argument type at index %u", start);
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// This is hackish; a nicer way to implement this would be with a "just a string" expression
|
|
|
|
// type.
|
2017-01-22 08:08:53 +08:00
|
|
|
return make_unique<unary_primary>(test_string_n, range_t(start, start + 1), arg(start));
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-01-22 08:08:53 +08:00
|
|
|
unique_ptr<expression> test_parser::parse_binary_primary(unsigned int start, unsigned int end) {
|
2016-04-20 10:49:15 +08:00
|
|
|
// We need three arguments.
|
|
|
|
for (unsigned int idx = start; idx < start + 3; idx++) {
|
|
|
|
if (idx >= end) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return error(L"Missing argument at index %u", idx);
|
|
|
|
}
|
2012-03-07 16:54:01 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// All our binary primaries are infix, so the operator is at start + 1.
|
2012-11-19 08:30:30 +08:00
|
|
|
const token_info_t *info = token_for_string(arg(start + 1));
|
2019-11-19 10:34:50 +08:00
|
|
|
if (!(info->flags & BINARY_PRIMARY)) return nullptr;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-01-27 12:00:43 +08:00
|
|
|
return make_unique<binary_primary>(info->tok, range_t(start, start + 3), arg(start),
|
|
|
|
arg(start + 2));
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-01-22 08:08:53 +08:00
|
|
|
unique_ptr<expression> test_parser::parse_parenthentical(unsigned int start, unsigned int end) {
|
2016-04-20 10:49:15 +08:00
|
|
|
// We need at least three arguments: open paren, argument, close paren.
|
2019-11-19 10:34:50 +08:00
|
|
|
if (start + 3 >= end) return nullptr;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Must start with an open expression.
|
2012-11-19 08:30:30 +08:00
|
|
|
const token_info_t *open_paren = token_for_string(arg(start));
|
2019-11-19 10:34:50 +08:00
|
|
|
if (open_paren->tok != test_paren_open) return nullptr;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Parse a subexpression.
|
2017-01-22 08:08:53 +08:00
|
|
|
unique_ptr<expression> subexpr = parse_expression(start + 1, end);
|
2019-11-19 10:34:50 +08:00
|
|
|
if (!subexpr) return nullptr;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Parse a close paren.
|
2012-11-19 08:30:30 +08:00
|
|
|
unsigned close_index = subexpr->range.end;
|
|
|
|
assert(close_index <= end);
|
2016-04-20 10:49:15 +08:00
|
|
|
if (close_index == end) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return error(L"Missing close paren at index %u", close_index);
|
|
|
|
}
|
|
|
|
const token_info_t *close_paren = token_for_string(arg(close_index));
|
2016-04-20 10:49:15 +08:00
|
|
|
if (close_paren->tok != test_paren_close) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return error(L"Expected close paren at index %u", close_index);
|
2012-03-16 11:40:31 +08:00
|
|
|
}
|
2012-03-07 16:54:01 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Success.
|
2017-01-27 12:00:43 +08:00
|
|
|
return make_unique<parenthetical_expression>(test_paren_open, range_t(start, close_index + 1),
|
|
|
|
move(subexpr));
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-01-22 08:08:53 +08:00
|
|
|
unique_ptr<expression> test_parser::parse_primary(unsigned int start, unsigned int end) {
|
2016-04-20 10:49:15 +08:00
|
|
|
if (start >= end) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return error(L"Missing argument at index %u", start);
|
2012-03-07 16:54:01 +08:00
|
|
|
}
|
|
|
|
|
2019-11-19 10:34:50 +08:00
|
|
|
unique_ptr<expression> expr = nullptr;
|
2016-04-20 10:49:15 +08:00
|
|
|
if (!expr) expr = parse_parenthentical(start, end);
|
|
|
|
if (!expr) expr = parse_unary_primary(start, end);
|
|
|
|
if (!expr) expr = parse_binary_primary(start, end);
|
|
|
|
if (!expr) expr = parse_just_a_string(start, end);
|
2012-11-19 08:30:30 +08:00
|
|
|
return expr;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// See IEEE 1003.1 breakdown of the behavior for different parameter counts.
|
2017-01-22 08:08:53 +08:00
|
|
|
unique_ptr<expression> test_parser::parse_3_arg_expression(unsigned int start, unsigned int end) {
|
2013-03-04 05:22:00 +08:00
|
|
|
assert(end - start == 3);
|
2019-11-19 10:34:50 +08:00
|
|
|
unique_ptr<expression> result = nullptr;
|
2013-03-22 08:44:51 +08:00
|
|
|
|
2013-03-04 05:22:00 +08:00
|
|
|
const token_info_t *center_token = token_for_string(arg(start + 1));
|
2016-04-20 10:49:15 +08:00
|
|
|
if (center_token->flags & BINARY_PRIMARY) {
|
2013-03-04 05:22:00 +08:00
|
|
|
result = parse_binary_primary(start, end);
|
2016-04-20 10:49:15 +08:00
|
|
|
} else if (center_token->tok == test_combine_and || center_token->tok == test_combine_or) {
|
2017-01-22 08:08:53 +08:00
|
|
|
unique_ptr<expression> left(parse_unary_expression(start, start + 1));
|
|
|
|
unique_ptr<expression> right(parse_unary_expression(start + 2, start + 3));
|
2016-04-20 10:49:15 +08:00
|
|
|
if (left.get() && right.get()) {
|
|
|
|
// Transfer ownership to the vector of subjects.
|
2017-01-22 08:08:53 +08:00
|
|
|
std::vector<token_t> combiners = {center_token->tok};
|
|
|
|
std::vector<unique_ptr<expression>> subjects;
|
|
|
|
subjects.push_back(move(left));
|
|
|
|
subjects.push_back(move(right));
|
|
|
|
result = make_unique<combining_expression>(center_token->tok, range_t(start, end),
|
2017-01-27 12:00:43 +08:00
|
|
|
move(subjects), move(combiners));
|
2013-03-04 05:22:00 +08:00
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
} else {
|
2013-03-04 05:22:00 +08:00
|
|
|
result = parse_unary_expression(start, end);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-01-22 08:08:53 +08:00
|
|
|
unique_ptr<expression> test_parser::parse_4_arg_expression(unsigned int start, unsigned int end) {
|
2013-03-04 05:22:00 +08:00
|
|
|
assert(end - start == 4);
|
2019-11-19 10:34:50 +08:00
|
|
|
unique_ptr<expression> result = nullptr;
|
2013-03-22 08:44:51 +08:00
|
|
|
|
2013-03-04 05:22:00 +08:00
|
|
|
token_t first_token = token_for_string(arg(start))->tok;
|
2016-04-20 10:49:15 +08:00
|
|
|
if (first_token == test_bang) {
|
2017-01-22 08:08:53 +08:00
|
|
|
unique_ptr<expression> subject(parse_3_arg_expression(start + 1, end));
|
2019-12-22 04:42:12 +08:00
|
|
|
if (subject) {
|
2017-01-27 12:00:43 +08:00
|
|
|
result = make_unique<unary_operator>(first_token, range_t(start, subject->range.end),
|
|
|
|
move(subject));
|
2013-03-04 05:22:00 +08:00
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
} else if (first_token == test_paren_open) {
|
2013-03-04 05:22:00 +08:00
|
|
|
result = parse_parenthentical(start, end);
|
2016-04-20 10:49:15 +08:00
|
|
|
} else {
|
2013-03-04 05:22:00 +08:00
|
|
|
result = parse_combining_expression(start, end);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-01-22 08:08:53 +08:00
|
|
|
unique_ptr<expression> test_parser::parse_expression(unsigned int start, unsigned int end) {
|
2016-04-20 10:49:15 +08:00
|
|
|
if (start >= end) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return error(L"Missing argument at index %u", start);
|
2012-03-07 16:54:01 +08:00
|
|
|
}
|
2013-03-22 08:44:51 +08:00
|
|
|
|
2013-03-04 05:22:00 +08:00
|
|
|
unsigned int argc = end - start;
|
2016-04-20 10:49:15 +08:00
|
|
|
switch (argc) {
|
|
|
|
case 0: {
|
2016-11-02 12:19:34 +08:00
|
|
|
DIE("argc should not be zero"); // should have been caught by the above test
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 1: {
|
2013-03-04 05:22:00 +08:00
|
|
|
return error(L"Missing argument at index %u", start + 1);
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
case 2: {
|
2013-03-04 05:22:00 +08:00
|
|
|
return parse_unary_expression(start, end);
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
case 3: {
|
2013-03-04 05:22:00 +08:00
|
|
|
return parse_3_arg_expression(start, end);
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
case 4: {
|
2013-03-04 05:22:00 +08:00
|
|
|
return parse_4_arg_expression(start, end);
|
|
|
|
}
|
2019-05-05 18:09:25 +08:00
|
|
|
default: {
|
|
|
|
return parse_combining_expression(start, end);
|
|
|
|
}
|
2013-03-04 05:22:00 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2017-01-22 08:08:53 +08:00
|
|
|
unique_ptr<expression> test_parser::parse_args(const wcstring_list_t &args, wcstring &err,
|
2017-01-27 12:00:43 +08:00
|
|
|
wchar_t *program_name) {
|
2016-04-20 10:49:15 +08:00
|
|
|
// Empty list and one-arg list should be handled by caller.
|
2012-11-19 08:30:30 +08:00
|
|
|
assert(args.size() > 1);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
test_parser parser(args);
|
2019-11-19 09:08:16 +08:00
|
|
|
unique_ptr<expression> result =
|
|
|
|
parser.parse_expression(0, static_cast<unsigned int>(args.size()));
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Handle errors.
|
2016-10-02 09:03:44 +08:00
|
|
|
// For now we only show the first error.
|
2016-10-05 11:06:14 +08:00
|
|
|
if (!parser.errors.empty()) {
|
2016-11-28 12:28:38 +08:00
|
|
|
err.append(program_name);
|
|
|
|
err.append(L": ");
|
2016-10-02 09:03:44 +08:00
|
|
|
err.append(parser.errors.at(0));
|
2012-11-19 08:30:30 +08:00
|
|
|
err.push_back(L'\n');
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (result) {
|
|
|
|
// It's also an error if there are any unused arguments. This is not detected by
|
|
|
|
// parse_expression().
|
2012-11-19 08:30:30 +08:00
|
|
|
assert(result->range.end <= args.size());
|
2016-04-20 10:49:15 +08:00
|
|
|
if (result->range.end < args.size()) {
|
|
|
|
if (err.empty()) {
|
2018-11-28 22:08:24 +08:00
|
|
|
append_format(err, L"%ls: unexpected argument at index %lu: '%ls'\n", program_name,
|
2019-11-19 09:08:16 +08:00
|
|
|
static_cast<unsigned long>(result->range.end),
|
|
|
|
args.at(result->range.end).c_str());
|
2013-01-05 17:30:03 +08:00
|
|
|
}
|
2019-11-19 10:34:50 +08:00
|
|
|
result.reset(nullptr);
|
2012-03-07 16:54:01 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
return result;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
bool unary_primary::evaluate(wcstring_list_t &errors) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return unary_primary_evaluate(token, arg, errors);
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
bool binary_primary::evaluate(wcstring_list_t &errors) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return binary_primary_evaluate(token, arg_left, arg_right, errors);
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
bool unary_operator::evaluate(wcstring_list_t &errors) {
|
2016-10-23 11:32:25 +08:00
|
|
|
if (token == test_bang) {
|
|
|
|
assert(subject.get());
|
|
|
|
return !subject->evaluate(errors);
|
2012-03-07 16:54:01 +08:00
|
|
|
}
|
2016-10-23 11:32:25 +08:00
|
|
|
|
|
|
|
errors.push_back(format_string(L"Unknown token type in %s", __func__));
|
|
|
|
return false;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
bool combining_expression::evaluate(wcstring_list_t &errors) {
|
2016-10-23 11:32:25 +08:00
|
|
|
if (token == test_combine_and || token == test_combine_or) {
|
2016-10-24 04:58:12 +08:00
|
|
|
assert(!subjects.empty()); //!OCLINT(multiple unary operator)
|
2016-10-23 11:32:25 +08:00
|
|
|
assert(combiners.size() + 1 == subjects.size());
|
|
|
|
|
|
|
|
// One-element case.
|
|
|
|
if (subjects.size() == 1) return subjects.at(0)->evaluate(errors);
|
|
|
|
|
|
|
|
// Evaluate our lists, remembering that AND has higher precedence than OR. We can
|
|
|
|
// visualize this as a sequence of OR expressions of AND expressions.
|
|
|
|
size_t idx = 0, max = subjects.size();
|
|
|
|
bool or_result = false;
|
|
|
|
while (idx < max) {
|
|
|
|
if (or_result) { // short circuit
|
|
|
|
break;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-10-23 11:32:25 +08:00
|
|
|
// Evaluate a stream of AND starting at given subject index. It may only have one
|
|
|
|
// element.
|
|
|
|
bool and_result = true;
|
|
|
|
for (; idx < max; idx++) {
|
|
|
|
// Evaluate it, short-circuiting.
|
|
|
|
and_result = and_result && subjects.at(idx)->evaluate(errors);
|
|
|
|
|
|
|
|
// If the combiner at this index (which corresponding to how we combine with the
|
|
|
|
// next subject) is not AND, then exit the loop.
|
|
|
|
if (idx + 1 < max && combiners.at(idx) != test_combine_and) {
|
|
|
|
idx++;
|
|
|
|
break;
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
|
|
|
}
|
2016-10-23 11:32:25 +08:00
|
|
|
|
|
|
|
// OR it in.
|
|
|
|
or_result = or_result || and_result;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
2016-10-23 11:32:25 +08:00
|
|
|
return or_result;
|
2012-03-07 16:54:01 +08:00
|
|
|
}
|
2016-10-23 11:32:25 +08:00
|
|
|
|
|
|
|
errors.push_back(format_string(L"Unknown token type in %s", __func__));
|
2019-05-30 02:26:02 +08:00
|
|
|
return false;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-03-07 16:54:01 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
bool parenthetical_expression::evaluate(wcstring_list_t &errors) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return contents->evaluate(errors);
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2018-07-29 15:41:03 +08:00
|
|
|
// Parse a double from arg. Return true on success, false on failure.
|
|
|
|
static bool parse_double(const wchar_t *arg, double *out_res) {
|
|
|
|
// Consume leading spaces.
|
|
|
|
while (arg && *arg != L'\0' && iswspace(*arg)) arg++;
|
|
|
|
errno = 0;
|
2019-11-19 10:34:50 +08:00
|
|
|
wchar_t *end = nullptr;
|
2018-11-05 07:53:31 +08:00
|
|
|
*out_res = fish_wcstod(arg, &end);
|
2018-07-29 15:41:03 +08:00
|
|
|
// Consume trailing spaces.
|
|
|
|
while (end && *end != L'\0' && iswspace(*end)) end++;
|
|
|
|
return errno == 0 && end > arg && *end == L'\0';
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// IEEE 1003.1 says nothing about what it means for two strings to be "algebraically equal". For
|
|
|
|
// example, should we interpret 0x10 as 0, 10, or 16? Here we use only base 10 and use wcstoll,
|
2016-11-28 11:05:37 +08:00
|
|
|
// which allows for leading + and -, and whitespace. This is consistent, albeit a bit more lenient
|
|
|
|
// since we allow trailing whitespace, with other implementations such as bash.
|
2018-07-29 15:41:03 +08:00
|
|
|
static bool parse_number(const wcstring &arg, number_t *number, wcstring_list_t &errors) {
|
|
|
|
const wchar_t *argcs = arg.c_str();
|
|
|
|
double floating = 0;
|
|
|
|
bool got_float = parse_double(argcs, &floating);
|
|
|
|
errno = 0;
|
|
|
|
long long integral = fish_wcstoll(argcs);
|
|
|
|
bool got_int = (errno == 0);
|
|
|
|
if (got_int) {
|
|
|
|
// Here the value is just an integer; ignore the floating point parse because it may be
|
|
|
|
// invalid (e.g. not a representable integer).
|
|
|
|
*number = number_t{integral, 0.0};
|
2018-12-16 14:05:19 +08:00
|
|
|
|
2018-07-29 15:41:03 +08:00
|
|
|
return true;
|
2020-02-26 23:42:20 +08:00
|
|
|
} else if (got_float && errno != ERANGE && std::isfinite(floating)) {
|
2018-07-29 15:41:03 +08:00
|
|
|
// Here we parsed an (in range) floating point value that could not be parsed as an integer.
|
|
|
|
// Break the floating point value into base and delta. Ensure that base is <= the floating
|
|
|
|
// point value.
|
2020-02-26 23:42:20 +08:00
|
|
|
//
|
2020-03-27 03:45:40 +08:00
|
|
|
// Note that a non-finite number like infinity or NaN doesn't work for us, so we checked
|
|
|
|
// above.
|
2018-07-29 15:41:03 +08:00
|
|
|
double intpart = std::floor(floating);
|
|
|
|
double delta = floating - intpart;
|
|
|
|
*number = number_t{static_cast<long long>(intpart), delta};
|
2018-12-16 14:05:19 +08:00
|
|
|
|
2018-07-29 15:41:03 +08:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// We could not parse a float or an int.
|
2018-12-15 03:42:23 +08:00
|
|
|
// Check for special fish_wcsto* value or show standard EINVAL/ERANGE error.
|
2018-12-15 04:43:18 +08:00
|
|
|
if (errno == -1) {
|
2019-05-05 18:09:25 +08:00
|
|
|
errors.push_back(
|
|
|
|
format_string(_(L"Integer %lld in '%ls' followed by non-digit"), integral, argcs));
|
2020-02-26 23:42:20 +08:00
|
|
|
} else if (std::isnan(floating)) {
|
|
|
|
// NaN is an error as far as we're concerned.
|
|
|
|
errors.push_back(_(L"Not a number"));
|
|
|
|
} else if (std::isinf(floating)) {
|
|
|
|
errors.push_back(_(L"Number is infinite"));
|
2018-12-15 04:43:18 +08:00
|
|
|
} else {
|
2019-03-13 06:07:07 +08:00
|
|
|
errors.push_back(format_string(L"%s: '%ls'", std::strerror(errno), argcs));
|
2018-12-15 04:43:18 +08:00
|
|
|
}
|
2018-07-29 15:41:03 +08:00
|
|
|
return false;
|
2016-11-28 11:05:37 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
static bool binary_primary_evaluate(test_expressions::token_t token, const wcstring &left,
|
|
|
|
const wcstring &right, wcstring_list_t &errors) {
|
2012-11-19 08:30:30 +08:00
|
|
|
using namespace test_expressions;
|
2018-07-29 15:41:03 +08:00
|
|
|
number_t ln, rn;
|
2016-04-20 10:49:15 +08:00
|
|
|
switch (token) {
|
|
|
|
case test_string_equal: {
|
2012-11-19 16:31:03 +08:00
|
|
|
return left == right;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_string_not_equal: {
|
2012-11-19 16:31:03 +08:00
|
|
|
return left != right;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_number_equal: {
|
2018-07-29 15:41:03 +08:00
|
|
|
return parse_number(left, &ln, errors) && parse_number(right, &rn, errors) &&
|
|
|
|
ln.compare(rn) == 0;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_number_not_equal: {
|
2018-07-29 15:41:03 +08:00
|
|
|
return parse_number(left, &ln, errors) && parse_number(right, &rn, errors) &&
|
|
|
|
ln.compare(rn) != 0;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_number_greater: {
|
2018-07-29 15:41:03 +08:00
|
|
|
return parse_number(left, &ln, errors) && parse_number(right, &rn, errors) &&
|
|
|
|
ln.compare(rn) > 0;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_number_greater_equal: {
|
2018-07-29 15:41:03 +08:00
|
|
|
return parse_number(left, &ln, errors) && parse_number(right, &rn, errors) &&
|
|
|
|
ln.compare(rn) >= 0;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_number_lesser: {
|
2018-07-29 15:41:03 +08:00
|
|
|
return parse_number(left, &ln, errors) && parse_number(right, &rn, errors) &&
|
|
|
|
ln.compare(rn) < 0;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_number_lesser_equal: {
|
2018-07-29 15:41:03 +08:00
|
|
|
return parse_number(left, &ln, errors) && parse_number(right, &rn, errors) &&
|
|
|
|
ln.compare(rn) <= 0;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
default: {
|
2012-11-19 16:31:03 +08:00
|
|
|
errors.push_back(format_string(L"Unknown token type in %s", __func__));
|
|
|
|
return false;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
2012-03-07 16:54:01 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-03-07 16:54:01 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
static bool unary_primary_evaluate(test_expressions::token_t token, const wcstring &arg,
|
|
|
|
wcstring_list_t &errors) {
|
2012-11-19 08:30:30 +08:00
|
|
|
using namespace test_expressions;
|
|
|
|
struct stat buf;
|
2016-04-20 10:49:15 +08:00
|
|
|
switch (token) {
|
|
|
|
case test_filetype_b: { // "-b", for block special files
|
2012-11-19 16:31:03 +08:00
|
|
|
return !wstat(arg, &buf) && S_ISBLK(buf.st_mode);
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_filetype_c: { // "-c", for character special files
|
2012-11-19 16:31:03 +08:00
|
|
|
return !wstat(arg, &buf) && S_ISCHR(buf.st_mode);
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_filetype_d: { // "-d", for directories
|
2012-11-19 16:31:03 +08:00
|
|
|
return !wstat(arg, &buf) && S_ISDIR(buf.st_mode);
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_filetype_e: { // "-e", for files that exist
|
2012-11-19 16:31:03 +08:00
|
|
|
return !wstat(arg, &buf);
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_filetype_f: { // "-f", for for regular files
|
2012-11-19 16:31:03 +08:00
|
|
|
return !wstat(arg, &buf) && S_ISREG(buf.st_mode);
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_filetype_G: { // "-G", for check effective group id
|
2013-08-25 09:10:19 +08:00
|
|
|
return !wstat(arg, &buf) && getegid() == buf.st_gid;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_filetype_g: { // "-g", for set-group-id
|
2012-11-19 16:31:03 +08:00
|
|
|
return !wstat(arg, &buf) && (S_ISGID & buf.st_mode);
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_filetype_h: // "-h", for symbolic links
|
|
|
|
case test_filetype_L: { // "-L", same as -h
|
2012-11-19 16:31:03 +08:00
|
|
|
return !lwstat(arg, &buf) && S_ISLNK(buf.st_mode);
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
2017-03-15 12:43:15 +08:00
|
|
|
case test_filetype_k: { // "-k", for sticky bit
|
|
|
|
#ifdef S_ISVTX
|
|
|
|
return !lwstat(arg, &buf) && buf.st_mode & S_ISVTX;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
case test_filetype_O: { // "-O", for check effective user id
|
2013-08-25 09:10:19 +08:00
|
|
|
return !wstat(arg, &buf) && geteuid() == buf.st_uid;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_filetype_p: { // "-p", for FIFO
|
2012-11-19 16:31:03 +08:00
|
|
|
return !wstat(arg, &buf) && S_ISFIFO(buf.st_mode);
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_filetype_S: { // "-S", socket
|
2012-11-19 16:31:03 +08:00
|
|
|
return !wstat(arg, &buf) && S_ISSOCK(buf.st_mode);
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_filesize_s: { // "-s", size greater than zero
|
2012-11-19 16:31:03 +08:00
|
|
|
return !wstat(arg, &buf) && buf.st_size > 0;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_filedesc_t: { // "-t", whether the fd is associated with a terminal
|
2018-07-29 15:41:03 +08:00
|
|
|
number_t num;
|
|
|
|
return parse_number(arg, &num, errors) && num.isatty();
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_fileperm_r: { // "-r", read permission
|
2012-11-19 16:31:03 +08:00
|
|
|
return !waccess(arg, R_OK);
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_fileperm_u: { // "-u", whether file is setuid
|
2012-11-19 16:31:03 +08:00
|
|
|
return !wstat(arg, &buf) && (S_ISUID & buf.st_mode);
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_fileperm_w: { // "-w", whether file write permission is allowed
|
2012-11-19 16:31:03 +08:00
|
|
|
return !waccess(arg, W_OK);
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_fileperm_x: { // "-x", whether file execute/search is allowed
|
2012-11-19 16:31:03 +08:00
|
|
|
return !waccess(arg, X_OK);
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_string_n: { // "-n", non-empty string
|
|
|
|
return !arg.empty();
|
|
|
|
}
|
|
|
|
case test_string_z: { // "-z", true if length of string is 0
|
2012-11-19 16:31:03 +08:00
|
|
|
return arg.empty();
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
default: {
|
2012-11-19 16:31:03 +08:00
|
|
|
errors.push_back(format_string(L"Unknown token type in %s", __func__));
|
|
|
|
return false;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
2012-03-07 16:54:01 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2017-05-02 12:44:30 +08:00
|
|
|
}; // namespace test_expressions
|
2018-08-05 06:46:04 +08:00
|
|
|
}; // anonymous namespace
|
2012-03-07 16:54:01 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
/// Evaluate a conditional expression given the arguments. If fromtest is set, the caller is the
|
|
|
|
/// test or [ builtin; with the pointer giving the name of the command. for POSIX conformance this
|
|
|
|
/// supports a more limited range of functionality.
|
|
|
|
///
|
|
|
|
/// Return status is the final shell status, i.e. 0 for true, 1 for false and 2 for error.
|
|
|
|
int builtin_test(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2016-10-10 05:38:26 +08:00
|
|
|
UNUSED(parser);
|
2012-03-07 16:54:01 +08:00
|
|
|
using namespace test_expressions;
|
2013-03-22 08:44:51 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// The first argument should be the name of the command ('test').
|
2017-05-05 12:35:41 +08:00
|
|
|
if (!argv[0]) return STATUS_INVALID_ARGS;
|
2013-03-22 08:44:51 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Whether we are invoked with bracket '[' or not.
|
2016-11-28 12:28:38 +08:00
|
|
|
wchar_t *program_name = argv[0];
|
2019-03-13 05:06:01 +08:00
|
|
|
const bool is_bracket = !std::wcscmp(program_name, L"[");
|
2013-03-22 08:44:51 +08:00
|
|
|
|
2012-03-07 16:54:01 +08:00
|
|
|
size_t argc = 0;
|
2016-04-20 10:49:15 +08:00
|
|
|
while (argv[argc + 1]) argc++;
|
|
|
|
|
|
|
|
// If we're bracket, the last argument ought to be ]; we ignore it. Note that argc is the number
|
|
|
|
// of arguments after the command name; thus argv[argc] is the last argument.
|
|
|
|
if (is_bracket) {
|
2019-03-13 05:06:01 +08:00
|
|
|
if (!std::wcscmp(argv[argc], L"]")) {
|
2016-11-28 12:28:38 +08:00
|
|
|
// Ignore the closing bracket from now on.
|
2013-01-05 17:30:03 +08:00
|
|
|
argc--;
|
2016-04-20 10:49:15 +08:00
|
|
|
} else {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.err.append(L"[: the last argument must be ']'\n");
|
2019-03-27 04:40:10 +08:00
|
|
|
builtin_print_error_trailer(parser, streams.err, program_name);
|
2017-05-05 12:35:41 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
2013-01-05 17:30:03 +08:00
|
|
|
}
|
|
|
|
}
|
2013-03-22 08:44:51 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Collect the arguments into a list.
|
2012-03-07 16:54:01 +08:00
|
|
|
const wcstring_list_t args(argv + 1, argv + 1 + argc);
|
2013-03-22 08:44:51 +08:00
|
|
|
|
2016-10-23 11:32:25 +08:00
|
|
|
if (argc == 0) {
|
2018-12-16 14:05:19 +08:00
|
|
|
return STATUS_INVALID_ARGS; // Per 1003.1, exit false.
|
2016-10-23 11:32:25 +08:00
|
|
|
} else if (argc == 1) {
|
|
|
|
// Per 1003.1, exit true if the arg is non-empty.
|
2017-05-04 15:18:02 +08:00
|
|
|
return args.at(0).empty() ? STATUS_CMD_ERROR : STATUS_CMD_OK;
|
2016-10-23 11:32:25 +08:00
|
|
|
}
|
|
|
|
|
2017-01-22 08:08:53 +08:00
|
|
|
// Try parsing
|
2016-10-23 11:32:25 +08:00
|
|
|
wcstring err;
|
2017-01-22 08:08:53 +08:00
|
|
|
unique_ptr<expression> expr = test_parser::parse_args(args, err, program_name);
|
2016-10-23 11:32:25 +08:00
|
|
|
if (!expr) {
|
|
|
|
streams.err.append(err);
|
2019-03-27 04:40:10 +08:00
|
|
|
builtin_print_error_trailer(parser, streams.err, program_name);
|
2017-05-04 15:18:02 +08:00
|
|
|
return STATUS_CMD_ERROR;
|
2016-10-23 11:32:25 +08:00
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
|
2016-10-23 11:32:25 +08:00
|
|
|
wcstring_list_t eval_errors;
|
|
|
|
bool result = expr->evaluate(eval_errors);
|
2018-12-16 14:05:19 +08:00
|
|
|
if (!eval_errors.empty()) {
|
|
|
|
if (!should_suppress_stderr_for_tests()) {
|
2019-11-20 05:46:47 +08:00
|
|
|
for (const auto &eval_error : eval_errors) {
|
|
|
|
streams.err.append_format(L"%ls\n", eval_error.c_str());
|
2018-12-16 14:05:19 +08:00
|
|
|
}
|
2019-03-27 04:40:10 +08:00
|
|
|
// Add a backtrace but not the "see help" message
|
|
|
|
// because this isn't about passing the wrong options.
|
|
|
|
streams.err.append(parser.current_line());
|
2012-03-07 16:54:01 +08:00
|
|
|
}
|
2018-12-16 14:05:19 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
2012-03-07 16:54:01 +08:00
|
|
|
}
|
2017-05-04 15:18:02 +08:00
|
|
|
return result ? STATUS_CMD_OK : STATUS_CMD_ERROR;
|
2012-03-07 16:54:01 +08:00
|
|
|
}
|