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
|
|
|
|
|
2016-11-28 11:05:37 +08:00
|
|
|
#include <errno.h>
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <stdarg.h>
|
2016-04-20 10:49:15 +08:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <wchar.h>
|
2017-02-14 12:37:27 +08:00
|
|
|
|
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-21 14:00:54 +08:00
|
|
|
#include "builtin.h"
|
2016-04-20 10:49:15 +08:00
|
|
|
#include "common.h"
|
2016-04-21 14:00:54 +08:00
|
|
|
#include "io.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::unique_ptr;
|
|
|
|
using std::move;
|
|
|
|
|
2015-09-22 02:24:49 +08:00
|
|
|
int builtin_test(parser_t &parser, io_streams_t &streams, wchar_t **argv);
|
2012-03-07 16:54:01 +08:00
|
|
|
|
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
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
static const struct token_info_t {
|
2012-11-19 08:30:30 +08:00
|
|
|
token_t tok;
|
|
|
|
const wchar_t *string;
|
|
|
|
unsigned int flags;
|
2016-04-20 10:49:15 +08:00
|
|
|
} token_infos[] = {{test_unknown, L"", 0},
|
|
|
|
{test_bang, L"!", 0},
|
|
|
|
{test_filetype_b, L"-b", UNARY_PRIMARY},
|
|
|
|
{test_filetype_c, L"-c", UNARY_PRIMARY},
|
|
|
|
{test_filetype_d, L"-d", UNARY_PRIMARY},
|
|
|
|
{test_filetype_e, L"-e", UNARY_PRIMARY},
|
|
|
|
{test_filetype_f, L"-f", UNARY_PRIMARY},
|
|
|
|
{test_filetype_G, L"-G", UNARY_PRIMARY},
|
|
|
|
{test_filetype_g, L"-g", UNARY_PRIMARY},
|
|
|
|
{test_filetype_h, L"-h", UNARY_PRIMARY},
|
2017-03-15 12:43:15 +08:00
|
|
|
{test_filetype_k, L"-k", UNARY_PRIMARY},
|
2016-04-20 10:49:15 +08:00
|
|
|
{test_filetype_L, L"-L", UNARY_PRIMARY},
|
|
|
|
{test_filetype_O, L"-O", UNARY_PRIMARY},
|
|
|
|
{test_filetype_p, L"-p", UNARY_PRIMARY},
|
|
|
|
{test_filetype_S, L"-S", UNARY_PRIMARY},
|
|
|
|
{test_filesize_s, L"-s", UNARY_PRIMARY},
|
|
|
|
{test_filedesc_t, L"-t", UNARY_PRIMARY},
|
|
|
|
{test_fileperm_r, L"-r", UNARY_PRIMARY},
|
|
|
|
{test_fileperm_u, L"-u", UNARY_PRIMARY},
|
|
|
|
{test_fileperm_w, L"-w", UNARY_PRIMARY},
|
|
|
|
{test_fileperm_x, L"-x", UNARY_PRIMARY},
|
|
|
|
{test_string_n, L"-n", UNARY_PRIMARY},
|
|
|
|
{test_string_z, L"-z", UNARY_PRIMARY},
|
|
|
|
{test_string_equal, L"=", BINARY_PRIMARY},
|
|
|
|
{test_string_not_equal, L"!=", BINARY_PRIMARY},
|
|
|
|
{test_number_equal, L"-eq", BINARY_PRIMARY},
|
|
|
|
{test_number_not_equal, L"-ne", BINARY_PRIMARY},
|
|
|
|
{test_number_greater, L"-gt", BINARY_PRIMARY},
|
|
|
|
{test_number_greater_equal, L"-ge", BINARY_PRIMARY},
|
|
|
|
{test_number_lesser, L"-lt", BINARY_PRIMARY},
|
|
|
|
{test_number_lesser_equal, L"-le", BINARY_PRIMARY},
|
|
|
|
{test_combine_and, L"-a", 0},
|
|
|
|
{test_combine_or, L"-o", 0},
|
|
|
|
{test_paren_open, L"(", 0},
|
|
|
|
{test_paren_close, L")", 0}};
|
|
|
|
|
|
|
|
const token_info_t *token_for_string(const wcstring &str) {
|
|
|
|
for (size_t i = 0; i < sizeof token_infos / sizeof *token_infos; i++) {
|
|
|
|
if (str == token_infos[i].string) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return &token_infos[i];
|
2012-03-07 16:54:01 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
return &token_infos[0]; // unknown
|
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:
|
2018-02-19 10:39:03 +08:00
|
|
|
expression(token_t what, range_t where) : token(what), range(std::move(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;
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
virtual ~expression() {}
|
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)) {}
|
2012-11-19 08:30:30 +08:00
|
|
|
bool evaluate(wcstring_list_t &errors);
|
|
|
|
};
|
|
|
|
|
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)) {}
|
2012-11-19 08:30:30 +08:00
|
|
|
bool evaluate(wcstring_list_t &errors);
|
|
|
|
};
|
|
|
|
|
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)) {}
|
2012-11-19 08:30:30 +08:00
|
|
|
bool evaluate(wcstring_list_t &errors);
|
|
|
|
};
|
|
|
|
|
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,
|
2017-05-05 13:42:42 +08:00
|
|
|
const 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
|
|
|
|
2017-01-22 08:08:53 +08:00
|
|
|
virtual ~combining_expression() {}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
bool evaluate(wcstring_list_t &errors);
|
|
|
|
};
|
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
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
virtual bool evaluate(wcstring_list_t &errors);
|
|
|
|
};
|
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, ...) {
|
2012-11-19 08:30:30 +08:00
|
|
|
assert(fmt != NULL);
|
|
|
|
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, ...) {
|
2012-11-19 08:30:30 +08:00
|
|
|
assert(fmt != NULL);
|
|
|
|
va_list va;
|
|
|
|
va_start(va, fmt);
|
|
|
|
this->errors.push_back(vformat_string(fmt, va));
|
|
|
|
va_end(va);
|
|
|
|
return NULL;
|
|
|
|
}
|
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));
|
2016-04-20 10:49:15 +08:00
|
|
|
if (subject.get()) {
|
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
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
return NULL;
|
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) {
|
2016-04-20 10:49:15 +08:00
|
|
|
if (start >= end) return NULL;
|
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()) {
|
|
|
|
return NULL; // 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));
|
2016-04-20 10:49:15 +08:00
|
|
|
if (!(info->flags & UNARY_PRIMARY)) return NULL;
|
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));
|
2016-04-20 10:49:15 +08:00
|
|
|
if (!(info->flags & BINARY_PRIMARY)) return NULL;
|
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.
|
|
|
|
if (start + 3 >= end) return NULL;
|
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));
|
2016-04-20 10:49:15 +08:00
|
|
|
if (open_paren->tok != test_paren_open) return NULL;
|
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);
|
|
|
|
if (!subexpr) return NULL;
|
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
|
|
|
}
|
|
|
|
|
2017-01-22 08:08:53 +08:00
|
|
|
unique_ptr<expression> expr = NULL;
|
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);
|
2017-01-22 08:08:53 +08:00
|
|
|
unique_ptr<expression> result = NULL;
|
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);
|
2017-01-22 08:08:53 +08:00
|
|
|
unique_ptr<expression> result = NULL;
|
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));
|
2016-04-20 10:49:15 +08:00
|
|
|
if (subject.get()) {
|
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-11-02 13:36:30 +08:00
|
|
|
break;
|
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);
|
|
|
|
}
|
2016-04-20 10:49:15 +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);
|
2017-01-22 08:08:53 +08:00
|
|
|
unique_ptr<expression> result = parser.parse_expression(0, (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()) {
|
2016-11-28 12:28:38 +08:00
|
|
|
append_format(err, L"%ls: unexpected argument at index %lu: '%ls'\n", program_name,
|
2016-04-20 10:49:15 +08:00
|
|
|
(unsigned long)result->range.end, args.at(result->range.end).c_str());
|
2013-01-05 17:30:03 +08:00
|
|
|
}
|
2017-01-22 08:08:53 +08:00
|
|
|
result.reset(NULL);
|
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__));
|
2017-05-05 12:35:41 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
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
|
|
|
|
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.
|
|
|
|
static bool parse_number(const wcstring &arg, long long *out, wcstring_list_t &errors) {
|
2016-11-25 10:43:50 +08:00
|
|
|
*out = fish_wcstoll(arg.c_str());
|
2016-11-28 11:05:37 +08:00
|
|
|
if (errno) {
|
|
|
|
errors.push_back(format_string(_(L"invalid integer '%ls'"), arg.c_str()));
|
|
|
|
}
|
2016-11-25 10:43:50 +08:00
|
|
|
return !errno;
|
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;
|
|
|
|
long long left_num, right_num;
|
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: {
|
2016-11-28 11:05:37 +08:00
|
|
|
return parse_number(left, &left_num, errors) &&
|
|
|
|
parse_number(right, &right_num, errors) && left_num == right_num;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_number_not_equal: {
|
2016-11-28 11:05:37 +08:00
|
|
|
return parse_number(left, &left_num, errors) &&
|
|
|
|
parse_number(right, &right_num, errors) && left_num != right_num;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_number_greater: {
|
2016-11-28 11:05:37 +08:00
|
|
|
return parse_number(left, &left_num, errors) &&
|
|
|
|
parse_number(right, &right_num, errors) && left_num > right_num;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_number_greater_equal: {
|
2016-11-28 11:05:37 +08:00
|
|
|
return parse_number(left, &left_num, errors) &&
|
|
|
|
parse_number(right, &right_num, errors) && left_num >= right_num;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_number_lesser: {
|
2016-11-28 11:05:37 +08:00
|
|
|
return parse_number(left, &left_num, errors) &&
|
|
|
|
parse_number(right, &right_num, errors) && left_num < right_num;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case test_number_lesser_equal: {
|
2016-11-28 11:05:37 +08:00
|
|
|
return parse_number(left, &left_num, errors) &&
|
|
|
|
parse_number(right, &right_num, errors) && left_num <= right_num;
|
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;
|
|
|
|
long long num;
|
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
|
2016-11-28 11:05:37 +08:00
|
|
|
return parse_number(arg, &num, errors) && num == (int)num && isatty((int)num);
|
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
|
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];
|
|
|
|
const bool is_bracket = !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) {
|
|
|
|
if (!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");
|
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) {
|
2017-05-04 15:18:02 +08:00
|
|
|
return STATUS_CMD_ERROR; // 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) {
|
2013-03-04 05:22:00 +08:00
|
|
|
#if 0
|
2017-01-21 19:35:38 +08:00
|
|
|
streams.err.append(L"Oops! test was given args:\n");
|
2016-10-23 11:32:25 +08:00
|
|
|
for (size_t i=0; i < argc; i++) {
|
2017-01-21 19:35:38 +08:00
|
|
|
streams.err.append_format(L"\t%ls\n", args.at(i).c_str());
|
2016-10-23 11:32:25 +08:00
|
|
|
}
|
2017-01-21 19:35:38 +08:00
|
|
|
streams.err.append_format(L"and returned parse error: %ls\n", err.c_str());
|
2013-03-04 05:22:00 +08:00
|
|
|
#endif
|
2016-10-23 11:32:25 +08:00
|
|
|
streams.err.append(err);
|
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);
|
2016-12-04 12:12:53 +08:00
|
|
|
if (!eval_errors.empty() && !should_suppress_stderr_for_tests()) {
|
2017-01-21 19:35:38 +08:00
|
|
|
streams.err.append(L"test returned eval errors:\n");
|
2016-10-23 11:32:25 +08:00
|
|
|
for (size_t i = 0; i < eval_errors.size(); i++) {
|
2017-01-21 19:35:38 +08:00
|
|
|
streams.err.append_format(L"\t%ls\n", eval_errors.at(i).c_str());
|
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
|
|
|
}
|