2016-04-20 02:49:15 +00:00
|
|
|
// Functions used for implementing the test builtin.
|
|
|
|
//
|
|
|
|
// Implemented from scratch (yes, really) by way of IEEE 1003.1 as reference.
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdarg.h>
|
2016-04-20 02:49:15 +00:00
|
|
|
#include <stdbool.h>
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <stdio.h>
|
2016-04-20 02:49:15 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <wchar.h>
|
2016-04-20 02:49:15 +00:00
|
|
|
#include <memory>
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <string>
|
2012-03-07 08:54:01 +00:00
|
|
|
|
2016-04-21 06:00:54 +00:00
|
|
|
#include "builtin.h"
|
2016-04-20 02:49:15 +00:00
|
|
|
#include "common.h"
|
2016-04-21 06:00:54 +00:00
|
|
|
#include "io.h"
|
2016-04-20 02:49:15 +00:00
|
|
|
#include "proc.h"
|
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2012-03-07 08:54:01 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
enum { BUILTIN_TEST_SUCCESS = STATUS_BUILTIN_OK, BUILTIN_TEST_FAIL = STATUS_BUILTIN_ERROR };
|
2012-03-07 08:54:01 +00:00
|
|
|
|
2015-09-21 18:24:49 +00:00
|
|
|
int builtin_test(parser_t &parser, io_streams_t &streams, wchar_t **argv);
|
2012-03-07 08:54:01 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
namespace test_expressions {
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
enum token_t {
|
|
|
|
test_unknown, // arbitrary string
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
test_bang, // "!", inverts sense
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-04-20 02:49:15 +00: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
|
|
|
|
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 00:30:30 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
test_filesize_s, // "-s", size greater than zero
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
test_filedesc_t, // "-t", whether the fd is associated with a terminal
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-04-20 02:49:15 +00: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 00:30:30 +00:00
|
|
|
|
2016-04-20 02:49:15 +00: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 00:30:30 +00: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 02:49:15 +00: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 00:30:30 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
test_paren_open, // "(", open paren
|
|
|
|
test_paren_close, // ")", close paren
|
2012-11-19 00:30:30 +00:00
|
|
|
};
|
|
|
|
|
2016-04-20 02:49:15 +00: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 00:30:30 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
enum { UNARY_PRIMARY = 1 << 0, BINARY_PRIMARY = 1 << 1 };
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
static const struct token_info_t {
|
2012-11-19 00:30:30 +00:00
|
|
|
token_t tok;
|
|
|
|
const wchar_t *string;
|
|
|
|
unsigned int flags;
|
2016-04-20 02:49:15 +00: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},
|
|
|
|
{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 00:30:30 +00:00
|
|
|
return &token_infos[i];
|
2012-03-07 08:54:01 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-20 02:49:15 +00:00
|
|
|
return &token_infos[0]; // unknown
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-03-07 08:54:01 +00:00
|
|
|
|
2016-04-20 02:49:15 +00: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 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
class expression;
|
2016-04-20 02:49:15 +00:00
|
|
|
class test_parser {
|
|
|
|
private:
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring_list_t strings;
|
|
|
|
wcstring_list_t errors;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
expression *error(const wchar_t *fmt, ...);
|
|
|
|
void add_error(const wchar_t *fmt, ...);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
const wcstring &arg(unsigned int idx) { return strings.at(idx); }
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
public:
|
|
|
|
explicit test_parser(const wcstring_list_t &val) : strings(val) {}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
expression *parse_expression(unsigned int start, unsigned int end);
|
2013-03-03 21:22:00 +00:00
|
|
|
expression *parse_3_arg_expression(unsigned int start, unsigned int end);
|
|
|
|
expression *parse_4_arg_expression(unsigned int start, unsigned int end);
|
2012-11-19 00:30:30 +00:00
|
|
|
expression *parse_combining_expression(unsigned int start, unsigned int end);
|
|
|
|
expression *parse_unary_expression(unsigned int start, unsigned int end);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
expression *parse_primary(unsigned int start, unsigned int end);
|
|
|
|
expression *parse_parenthentical(unsigned int start, unsigned int end);
|
|
|
|
expression *parse_unary_primary(unsigned int start, unsigned int end);
|
|
|
|
expression *parse_binary_primary(unsigned int start, unsigned int end);
|
|
|
|
expression *parse_just_a_string(unsigned int start, unsigned int end);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
static expression *parse_args(const wcstring_list_t &args, wcstring &err);
|
|
|
|
};
|
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
struct range_t {
|
2012-11-19 00:30:30 +00:00
|
|
|
unsigned int start;
|
|
|
|
unsigned int end;
|
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
range_t(unsigned s, unsigned e) : start(s), end(e) {}
|
2012-11-19 00:30:30 +00:00
|
|
|
};
|
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
/// Base class for expressions.
|
|
|
|
class expression {
|
|
|
|
protected:
|
|
|
|
expression(token_t what, range_t where) : token(what), range(where) {}
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
public:
|
2012-11-19 00:30:30 +00:00
|
|
|
const token_t token;
|
|
|
|
range_t range;
|
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
virtual ~expression() {}
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
/// Evaluate returns true if the expression is true (i.e. BUILTIN_TEST_SUCCESS).
|
2012-11-19 00:30:30 +00:00
|
|
|
virtual bool evaluate(wcstring_list_t &errors) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::auto_ptr<expression> expr_ref_t;
|
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
/// Single argument like -n foo or "just a string".
|
|
|
|
class unary_primary : public expression {
|
|
|
|
public:
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring arg;
|
2016-04-20 02:49:15 +00:00
|
|
|
unary_primary(token_t tok, range_t where, const wcstring &what)
|
|
|
|
: expression(tok, where), arg(what) {}
|
2012-11-19 00:30:30 +00:00
|
|
|
bool evaluate(wcstring_list_t &errors);
|
|
|
|
};
|
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
/// Two argument primary like foo != bar.
|
|
|
|
class binary_primary : public expression {
|
|
|
|
public:
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring arg_left;
|
|
|
|
wcstring arg_right;
|
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
binary_primary(token_t tok, range_t where, const wcstring &left, const wcstring &right)
|
|
|
|
: expression(tok, where), arg_left(left), arg_right(right) {}
|
2012-11-19 00:30:30 +00:00
|
|
|
bool evaluate(wcstring_list_t &errors);
|
|
|
|
};
|
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
/// Unary operator like bang.
|
|
|
|
class unary_operator : public expression {
|
|
|
|
public:
|
2012-11-19 00:30:30 +00:00
|
|
|
expr_ref_t subject;
|
2016-04-20 02:49:15 +00:00
|
|
|
unary_operator(token_t tok, range_t where, expr_ref_t &exp)
|
|
|
|
: expression(tok, where), subject(exp) {}
|
2012-11-19 00:30:30 +00:00
|
|
|
bool evaluate(wcstring_list_t &errors);
|
|
|
|
};
|
|
|
|
|
2016-04-20 02:49:15 +00: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:
|
2012-11-19 00:30:30 +00:00
|
|
|
const std::vector<expression *> subjects;
|
|
|
|
const std::vector<token_t> combiners;
|
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
combining_expression(token_t tok, range_t where, const std::vector<expression *> &exprs,
|
|
|
|
const std::vector<token_t> &combs)
|
|
|
|
: expression(tok, where), subjects(exprs), combiners(combs) {
|
|
|
|
// We should have one more subject than combiner.
|
2012-11-19 00:30:30 +00:00
|
|
|
assert(subjects.size() == combiners.size() + 1);
|
2012-03-07 08:54:01 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
// We are responsible for destroying our expressions.
|
|
|
|
virtual ~combining_expression() {
|
|
|
|
for (size_t i = 0; i < subjects.size(); i++) {
|
2012-11-19 00:30:30 +00:00
|
|
|
delete subjects[i];
|
2012-03-07 08:54:01 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
bool evaluate(wcstring_list_t &errors);
|
|
|
|
};
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
/// Parenthetical expression.
|
|
|
|
class parenthetical_expression : public expression {
|
|
|
|
public:
|
2012-11-19 00:30:30 +00:00
|
|
|
expr_ref_t contents;
|
2016-04-20 02:49:15 +00:00
|
|
|
parenthetical_expression(token_t tok, range_t where, expr_ref_t &expr)
|
|
|
|
: expression(tok, where), contents(expr) {}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
virtual bool evaluate(wcstring_list_t &errors);
|
|
|
|
};
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
void test_parser::add_error(const wchar_t *fmt, ...) {
|
2012-11-19 00:30:30 +00: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 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
expression *test_parser::error(const wchar_t *fmt, ...) {
|
2012-11-19 00:30:30 +00: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 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
expression *test_parser::parse_unary_expression(unsigned int start, unsigned int end) {
|
|
|
|
if (start >= end) {
|
2012-11-19 00:30:30 +00:00
|
|
|
return error(L"Missing argument at index %u", start);
|
|
|
|
}
|
|
|
|
token_t tok = token_for_string(arg(start))->tok;
|
2016-04-20 02:49:15 +00:00
|
|
|
if (tok == test_bang) {
|
2012-11-19 00:30:30 +00:00
|
|
|
expr_ref_t subject(parse_unary_expression(start + 1, end));
|
2016-04-20 02:49:15 +00:00
|
|
|
if (subject.get()) {
|
2012-11-19 00:30:30 +00:00
|
|
|
return new unary_operator(tok, range_t(start, subject->range.end), subject);
|
2012-03-07 08:54:01 +00:00
|
|
|
}
|
2016-05-04 22:19:47 +00:00
|
|
|
return NULL;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-05-04 22:19:47 +00:00
|
|
|
return parse_primary(start, end);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
/// Parse a combining expression (AND, OR).
|
|
|
|
expression *test_parser::parse_combining_expression(unsigned int start, unsigned int end) {
|
|
|
|
if (start >= end) return NULL;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
std::vector<expression *> subjects;
|
|
|
|
std::vector<token_t> combiners;
|
|
|
|
unsigned int idx = start;
|
2013-01-06 22:48:46 +00:00
|
|
|
bool first = true;
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
while (idx < end) {
|
|
|
|
if (!first) {
|
|
|
|
// This is not the first expression, so we expect a combiner.
|
2012-11-19 00:30:30 +00:00
|
|
|
token_t combiner = token_for_string(arg(idx))->tok;
|
2016-04-20 02:49:15 +00:00
|
|
|
if (combiner != test_combine_and && combiner != test_combine_or) {
|
2012-11-19 00:30:30 +00:00
|
|
|
/* Not a combiner, we're done */
|
2016-04-20 02:49:15 +00:00
|
|
|
this->errors.insert(
|
|
|
|
this->errors.begin(),
|
|
|
|
format_string(L"Expected a combining operator like '-a' at index %u", idx));
|
2012-11-19 00:30:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
combiners.push_back(combiner);
|
|
|
|
idx++;
|
2012-03-07 08:54:01 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
// Parse another expression.
|
2012-11-19 00:30:30 +00:00
|
|
|
expression *expr = parse_unary_expression(idx, end);
|
2016-04-20 02:49:15 +00:00
|
|
|
if (!expr) {
|
2012-11-19 00:30:30 +00:00
|
|
|
add_error(L"Missing argument at index %u", idx);
|
2016-04-20 02:49:15 +00:00
|
|
|
if (!first) {
|
|
|
|
// Clean up the dangling combiner, since it never got its right hand expression.
|
2013-01-06 22:48:46 +00:00
|
|
|
combiners.pop_back();
|
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
break;
|
2012-03-07 08:54:01 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
// Go to the end of this expression.
|
2012-11-19 00:30:30 +00:00
|
|
|
idx = expr->range.end;
|
|
|
|
subjects.push_back(expr);
|
2013-01-06 22:48:46 +00:00
|
|
|
first = false;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-04 22:19:47 +00:00
|
|
|
if (subjects.empty()) {
|
|
|
|
return NULL; // no subjects
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-05-04 22:19:47 +00:00
|
|
|
// Our new expression takes ownership of all expressions we created. The token we pass is
|
|
|
|
// irrelevant.
|
|
|
|
return new combining_expression(test_combine_and, range_t(start, idx), subjects, combiners);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
expression *test_parser::parse_unary_primary(unsigned int start, unsigned int end) {
|
|
|
|
// We need two arguments.
|
|
|
|
if (start >= end) {
|
2012-11-19 00:30:30 +00:00
|
|
|
return error(L"Missing argument at index %u", start);
|
|
|
|
}
|
2016-04-20 02:49:15 +00:00
|
|
|
if (start + 1 >= end) {
|
2012-11-19 00:30:30 +00:00
|
|
|
return error(L"Missing argument at index %u", start + 1);
|
2012-03-07 08:54:01 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
// All our unary primaries are prefix, so the operator is at start.
|
2012-11-19 00:30:30 +00:00
|
|
|
const token_info_t *info = token_for_string(arg(start));
|
2016-04-20 02:49:15 +00:00
|
|
|
if (!(info->flags & UNARY_PRIMARY)) return NULL;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
return new unary_primary(info->tok, range_t(start, start + 2), arg(start + 1));
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
expression *test_parser::parse_just_a_string(unsigned int start, unsigned int end) {
|
|
|
|
// 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 00:30:30 +00:00
|
|
|
return error(L"Missing argument at index %u", start);
|
2012-05-20 19:58:03 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
const token_info_t *info = token_for_string(arg(start));
|
2016-04-20 02:49:15 +00:00
|
|
|
if (info->tok != test_unknown) {
|
2012-11-19 00:30:30 +00:00
|
|
|
return error(L"Unexpected argument type at index %u", start);
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
// This is hackish; a nicer way to implement this would be with a "just a string" expression
|
|
|
|
// type.
|
2012-11-19 00:30:30 +00:00
|
|
|
return new unary_primary(test_string_n, range_t(start, start + 1), arg(start));
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
#if 0
|
|
|
|
expression *test_parser::parse_unary_primary(unsigned int start, unsigned int end)
|
|
|
|
{
|
2016-04-20 02:49:15 +00:00
|
|
|
// We need either one or two arguments.
|
|
|
|
if (start >= end) {
|
2012-11-19 00:30:30 +00:00
|
|
|
return error(L"Missing argument at index %u", start);
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
// The index of the argument to the unary primary.
|
2012-11-19 00:30:30 +00:00
|
|
|
unsigned int arg_idx;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
// All our unary primaries are prefix, so any operator is at start. But it also may just be a
|
|
|
|
// string, with no operator.
|
2012-11-19 00:30:30 +00:00
|
|
|
const token_info_t *info = token_for_string(arg(start));
|
2016-04-20 02:49:15 +00:00
|
|
|
if (info->flags & UNARY_PRIMARY) {
|
2012-11-19 00:30:30 +00:00
|
|
|
/* We have an operator. Skip the operator argument */
|
|
|
|
arg_idx = start + 1;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
// We have some freedom here...do we allow other tokens for the argument to operate on? For
|
|
|
|
// example, should 'test -n =' work? I say yes. So no typechecking on the next token.
|
|
|
|
} else if (info->tok == test_unknown) {
|
|
|
|
// "Just a string.
|
2012-11-19 00:30:30 +00:00
|
|
|
arg_idx = start;
|
2016-04-20 02:49:15 +00:00
|
|
|
} else {
|
|
|
|
// Here we don't allow arbitrary tokens as "just a string." I.e. 'test = -a =' should have a
|
|
|
|
// parse error. We could relax this at some point.
|
2012-11-19 00:30:30 +00:00
|
|
|
return error(L"Parse error at argument index %u", start);
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
// Verify we have the argument we want, i.e. test -n should fail to parse.
|
|
|
|
if (arg_idx >= end) {
|
2012-11-19 00:30:30 +00:00
|
|
|
return error(L"Missing argument at index %u", arg_idx);
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
return new unary_primary(info->tok, range_t(start, arg_idx + 1), arg(arg_idx));
|
|
|
|
}
|
|
|
|
#endif
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
expression *test_parser::parse_binary_primary(unsigned int start, unsigned int end) {
|
|
|
|
// We need three arguments.
|
|
|
|
for (unsigned int idx = start; idx < start + 3; idx++) {
|
|
|
|
if (idx >= end) {
|
2012-11-19 00:30:30 +00:00
|
|
|
return error(L"Missing argument at index %u", idx);
|
|
|
|
}
|
2012-03-07 08:54:01 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
// All our binary primaries are infix, so the operator is at start + 1.
|
2012-11-19 00:30:30 +00:00
|
|
|
const token_info_t *info = token_for_string(arg(start + 1));
|
2016-04-20 02:49:15 +00:00
|
|
|
if (!(info->flags & BINARY_PRIMARY)) return NULL;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
return new binary_primary(info->tok, range_t(start, start + 3), arg(start), arg(start + 2));
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
expression *test_parser::parse_parenthentical(unsigned int start, unsigned int end) {
|
|
|
|
// We need at least three arguments: open paren, argument, close paren.
|
|
|
|
if (start + 3 >= end) return NULL;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
// Must start with an open expression.
|
2012-11-19 00:30:30 +00:00
|
|
|
const token_info_t *open_paren = token_for_string(arg(start));
|
2016-04-20 02:49:15 +00:00
|
|
|
if (open_paren->tok != test_paren_open) return NULL;
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
// Parse a subexpression.
|
2012-11-19 00:30:30 +00:00
|
|
|
expression *subexr_ptr = parse_expression(start + 1, end);
|
2016-04-20 02:49:15 +00:00
|
|
|
if (!subexr_ptr) return NULL;
|
2012-11-19 00:30:30 +00:00
|
|
|
expr_ref_t subexpr(subexr_ptr);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
// Parse a close paren.
|
2012-11-19 00:30:30 +00:00
|
|
|
unsigned close_index = subexpr->range.end;
|
|
|
|
assert(close_index <= end);
|
2016-04-20 02:49:15 +00:00
|
|
|
if (close_index == end) {
|
2012-11-19 00:30:30 +00: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 02:49:15 +00:00
|
|
|
if (close_paren->tok != test_paren_close) {
|
2012-11-19 00:30:30 +00:00
|
|
|
return error(L"Expected close paren at index %u", close_index);
|
2012-03-16 03:40:31 +00:00
|
|
|
}
|
2012-03-07 08:54:01 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
// Success.
|
|
|
|
return new parenthetical_expression(test_paren_open, range_t(start, close_index + 1), subexpr);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
expression *test_parser::parse_primary(unsigned int start, unsigned int end) {
|
|
|
|
if (start >= end) {
|
2012-11-19 00:30:30 +00:00
|
|
|
return error(L"Missing argument at index %u", start);
|
2012-03-07 08:54:01 +00:00
|
|
|
}
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
expression *expr = NULL;
|
2016-04-20 02:49:15 +00: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 00:30:30 +00:00
|
|
|
return expr;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
// See IEEE 1003.1 breakdown of the behavior for different parameter counts.
|
|
|
|
expression *test_parser::parse_3_arg_expression(unsigned int start, unsigned int end) {
|
2013-03-03 21:22:00 +00:00
|
|
|
assert(end - start == 3);
|
|
|
|
expression *result = NULL;
|
2013-03-22 00:44:51 +00:00
|
|
|
|
2013-03-03 21:22:00 +00:00
|
|
|
const token_info_t *center_token = token_for_string(arg(start + 1));
|
2016-04-20 02:49:15 +00:00
|
|
|
if (center_token->flags & BINARY_PRIMARY) {
|
2013-03-03 21:22:00 +00:00
|
|
|
result = parse_binary_primary(start, end);
|
2016-04-20 02:49:15 +00:00
|
|
|
} else if (center_token->tok == test_combine_and || center_token->tok == test_combine_or) {
|
2013-03-03 21:22:00 +00:00
|
|
|
expr_ref_t left(parse_unary_expression(start, start + 1));
|
|
|
|
expr_ref_t right(parse_unary_expression(start + 2, start + 3));
|
2016-04-20 02:49:15 +00:00
|
|
|
if (left.get() && right.get()) {
|
|
|
|
// Transfer ownership to the vector of subjects.
|
2013-03-03 21:22:00 +00:00
|
|
|
std::vector<token_t> combiners(1, center_token->tok);
|
|
|
|
std::vector<expression *> subjects;
|
|
|
|
subjects.push_back(left.release());
|
|
|
|
subjects.push_back(right.release());
|
2016-04-20 02:49:15 +00:00
|
|
|
result = new combining_expression(center_token->tok, range_t(start, end), subjects,
|
|
|
|
combiners);
|
2013-03-03 21:22:00 +00:00
|
|
|
}
|
2016-04-20 02:49:15 +00:00
|
|
|
} else {
|
2013-03-03 21:22:00 +00:00
|
|
|
result = parse_unary_expression(start, end);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
expression *test_parser::parse_4_arg_expression(unsigned int start, unsigned int end) {
|
2013-03-03 21:22:00 +00:00
|
|
|
assert(end - start == 4);
|
|
|
|
expression *result = NULL;
|
2013-03-22 00:44:51 +00:00
|
|
|
|
2013-03-03 21:22:00 +00:00
|
|
|
token_t first_token = token_for_string(arg(start))->tok;
|
2016-04-20 02:49:15 +00:00
|
|
|
if (first_token == test_bang) {
|
2013-03-03 21:22:00 +00:00
|
|
|
expr_ref_t subject(parse_3_arg_expression(start + 1, end));
|
2016-04-20 02:49:15 +00:00
|
|
|
if (subject.get()) {
|
2013-03-03 21:22:00 +00:00
|
|
|
result = new unary_operator(first_token, range_t(start, subject->range.end), subject);
|
|
|
|
}
|
2016-04-20 02:49:15 +00:00
|
|
|
} else if (first_token == test_paren_open) {
|
2013-03-03 21:22:00 +00:00
|
|
|
result = parse_parenthentical(start, end);
|
2016-04-20 02:49:15 +00:00
|
|
|
} else {
|
2013-03-03 21:22:00 +00:00
|
|
|
result = parse_combining_expression(start, end);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
expression *test_parser::parse_expression(unsigned int start, unsigned int end) {
|
|
|
|
if (start >= end) {
|
2012-11-19 00:30:30 +00:00
|
|
|
return error(L"Missing argument at index %u", start);
|
2012-03-07 08:54:01 +00:00
|
|
|
}
|
2013-03-22 00:44:51 +00:00
|
|
|
|
2013-03-03 21:22:00 +00:00
|
|
|
unsigned int argc = end - start;
|
2016-04-20 02:49:15 +00:00
|
|
|
switch (argc) {
|
|
|
|
case 0: {
|
|
|
|
assert(0); // should have been caught by the above test
|
2013-03-03 21:22:00 +00:00
|
|
|
return NULL;
|
2016-04-20 02:49:15 +00:00
|
|
|
}
|
|
|
|
case 1: {
|
2013-03-03 21:22:00 +00:00
|
|
|
return error(L"Missing argument at index %u", start + 1);
|
|
|
|
}
|
2016-04-20 02:49:15 +00:00
|
|
|
case 2: {
|
2013-03-03 21:22:00 +00:00
|
|
|
return parse_unary_expression(start, end);
|
|
|
|
}
|
2016-04-20 02:49:15 +00:00
|
|
|
case 3: {
|
2013-03-03 21:22:00 +00:00
|
|
|
return parse_3_arg_expression(start, end);
|
|
|
|
}
|
2016-04-20 02:49:15 +00:00
|
|
|
case 4: {
|
2013-03-03 21:22:00 +00:00
|
|
|
return parse_4_arg_expression(start, end);
|
|
|
|
}
|
2016-04-20 02:49:15 +00:00
|
|
|
default: { return parse_combining_expression(start, end); }
|
2013-03-03 21:22:00 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
expression *test_parser::parse_args(const wcstring_list_t &args, wcstring &err) {
|
|
|
|
// Empty list and one-arg list should be handled by caller.
|
2012-11-19 00:30:30 +00:00
|
|
|
assert(args.size() > 1);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
test_parser parser(args);
|
|
|
|
expression *result = parser.parse_expression(0, (unsigned int)args.size());
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
// Handle errors.
|
|
|
|
for (size_t i = 0; i < parser.errors.size(); i++) {
|
2012-11-19 00:30:30 +00:00
|
|
|
err.append(L"test: ");
|
|
|
|
err.append(parser.errors.at(i));
|
|
|
|
err.push_back(L'\n');
|
2016-04-20 02:49:15 +00:00
|
|
|
// For now we only show the first error.
|
2012-11-19 00:30:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
if (result) {
|
|
|
|
// It's also an error if there are any unused arguments. This is not detected by
|
|
|
|
// parse_expression().
|
2012-11-19 00:30:30 +00:00
|
|
|
assert(result->range.end <= args.size());
|
2016-04-20 02:49:15 +00:00
|
|
|
if (result->range.end < args.size()) {
|
|
|
|
if (err.empty()) {
|
|
|
|
append_format(err, L"test: unexpected argument at index %lu: '%ls'\n",
|
|
|
|
(unsigned long)result->range.end, args.at(result->range.end).c_str());
|
2013-01-05 09:30:03 +00:00
|
|
|
}
|
2013-01-08 10:39:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
delete result;
|
|
|
|
result = NULL;
|
2012-03-07 08:54:01 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
return result;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
bool unary_primary::evaluate(wcstring_list_t &errors) {
|
2012-11-19 00:30:30 +00:00
|
|
|
return unary_primary_evaluate(token, arg, errors);
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
bool binary_primary::evaluate(wcstring_list_t &errors) {
|
2012-11-19 00:30:30 +00:00
|
|
|
return binary_primary_evaluate(token, arg_left, arg_right, errors);
|
|
|
|
}
|
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
bool unary_operator::evaluate(wcstring_list_t &errors) {
|
|
|
|
switch (token) {
|
|
|
|
case test_bang: {
|
2012-11-19 08:31:03 +00:00
|
|
|
assert(subject.get());
|
2016-04-20 02:49:15 +00:00
|
|
|
return !subject->evaluate(errors);
|
|
|
|
}
|
|
|
|
default: {
|
2012-11-19 08:31:03 +00:00
|
|
|
errors.push_back(format_string(L"Unknown token type in %s", __func__));
|
|
|
|
return false;
|
2016-04-20 02:49:15 +00:00
|
|
|
}
|
2012-03-07 08:54:01 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
bool combining_expression::evaluate(wcstring_list_t &errors) {
|
|
|
|
switch (token) {
|
2012-11-19 08:31:03 +00:00
|
|
|
case test_combine_and:
|
2016-04-20 02:49:15 +00:00
|
|
|
case test_combine_or: {
|
|
|
|
// One-element case.
|
|
|
|
if (subjects.size() == 1) return subjects.at(0)->evaluate(errors);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
// Evaluate our lists, remembering that AND has higher precedence than OR. We can
|
|
|
|
// visualize this as a sequence of OR expressions of AND expressions.
|
2012-11-19 08:31:03 +00:00
|
|
|
assert(combiners.size() + 1 == subjects.size());
|
2016-04-20 02:49:15 +00:00
|
|
|
assert(!subjects.empty());
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2012-11-19 08:31:03 +00:00
|
|
|
size_t idx = 0, max = subjects.size();
|
|
|
|
bool or_result = false;
|
2016-04-20 02:49:15 +00:00
|
|
|
while (idx < max) {
|
|
|
|
if (or_result) { // short circuit
|
2012-11-19 00:30:30 +00:00
|
|
|
break;
|
2012-03-07 08:54:01 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
// Evaluate a stream of AND starting at given subject index. It may only have one
|
|
|
|
// element.
|
2012-11-19 08:31:03 +00:00
|
|
|
bool and_result = true;
|
2016-04-20 02:49:15 +00:00
|
|
|
for (; idx < max; idx++) {
|
|
|
|
// Evaluate it, short-circuiting.
|
2012-11-19 08:31:03 +00:00
|
|
|
and_result = and_result && subjects.at(idx)->evaluate(errors);
|
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
// 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) {
|
2012-11-19 08:31:03 +00:00
|
|
|
idx++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
// OR it in.
|
2012-11-19 08:31:03 +00:00
|
|
|
or_result = or_result || and_result;
|
|
|
|
}
|
|
|
|
return or_result;
|
2012-03-07 08:54:01 +00:00
|
|
|
}
|
2016-04-20 02:49:15 +00:00
|
|
|
default: {
|
2012-11-19 08:31:03 +00:00
|
|
|
errors.push_back(format_string(L"Unknown token type in %s", __func__));
|
|
|
|
return BUILTIN_TEST_FAIL;
|
2016-04-20 02:49:15 +00:00
|
|
|
}
|
2012-03-07 08:54:01 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-03-07 08:54:01 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
bool parenthetical_expression::evaluate(wcstring_list_t &errors) {
|
2012-11-19 00:30:30 +00:00
|
|
|
return contents->evaluate(errors);
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00: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,
|
|
|
|
// which allows for leading + and -, and leading whitespace. This matches bash.
|
|
|
|
static bool parse_number(const wcstring &arg, long long *out) {
|
2012-11-19 00:30:30 +00:00
|
|
|
const wchar_t *str = arg.c_str();
|
|
|
|
wchar_t *endptr = NULL;
|
|
|
|
*out = wcstoll(str, &endptr, 10);
|
|
|
|
return endptr && *endptr == L'\0';
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
static bool binary_primary_evaluate(test_expressions::token_t token, const wcstring &left,
|
|
|
|
const wcstring &right, wcstring_list_t &errors) {
|
2012-11-19 00:30:30 +00:00
|
|
|
using namespace test_expressions;
|
|
|
|
long long left_num, right_num;
|
2016-04-20 02:49:15 +00:00
|
|
|
switch (token) {
|
|
|
|
case test_string_equal: {
|
2012-11-19 08:31:03 +00:00
|
|
|
return left == right;
|
2016-04-20 02:49:15 +00:00
|
|
|
}
|
|
|
|
case test_string_not_equal: {
|
2012-11-19 08:31:03 +00:00
|
|
|
return left != right;
|
2016-04-20 02:49:15 +00:00
|
|
|
}
|
|
|
|
case test_number_equal: {
|
|
|
|
return parse_number(left, &left_num) && parse_number(right, &right_num) &&
|
|
|
|
left_num == right_num;
|
|
|
|
}
|
|
|
|
case test_number_not_equal: {
|
|
|
|
return parse_number(left, &left_num) && parse_number(right, &right_num) &&
|
|
|
|
left_num != right_num;
|
|
|
|
}
|
|
|
|
case test_number_greater: {
|
|
|
|
return parse_number(left, &left_num) && parse_number(right, &right_num) &&
|
|
|
|
left_num > right_num;
|
|
|
|
}
|
|
|
|
case test_number_greater_equal: {
|
|
|
|
return parse_number(left, &left_num) && parse_number(right, &right_num) &&
|
|
|
|
left_num >= right_num;
|
|
|
|
}
|
|
|
|
case test_number_lesser: {
|
|
|
|
return parse_number(left, &left_num) && parse_number(right, &right_num) &&
|
|
|
|
left_num < right_num;
|
|
|
|
}
|
|
|
|
case test_number_lesser_equal: {
|
|
|
|
return parse_number(left, &left_num) && parse_number(right, &right_num) &&
|
|
|
|
left_num <= right_num;
|
|
|
|
}
|
|
|
|
default: {
|
2012-11-19 08:31:03 +00:00
|
|
|
errors.push_back(format_string(L"Unknown token type in %s", __func__));
|
|
|
|
return false;
|
2016-04-20 02:49:15 +00:00
|
|
|
}
|
2012-03-07 08:54:01 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-03-07 08:54:01 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
static bool unary_primary_evaluate(test_expressions::token_t token, const wcstring &arg,
|
|
|
|
wcstring_list_t &errors) {
|
2012-11-19 00:30:30 +00:00
|
|
|
using namespace test_expressions;
|
|
|
|
struct stat buf;
|
|
|
|
long long num;
|
2016-04-20 02:49:15 +00:00
|
|
|
switch (token) {
|
|
|
|
case test_filetype_b: { // "-b", for block special files
|
2012-11-19 08:31:03 +00:00
|
|
|
return !wstat(arg, &buf) && S_ISBLK(buf.st_mode);
|
2016-04-20 02:49:15 +00:00
|
|
|
}
|
|
|
|
case test_filetype_c: { // "-c", for character special files
|
2012-11-19 08:31:03 +00:00
|
|
|
return !wstat(arg, &buf) && S_ISCHR(buf.st_mode);
|
2016-04-20 02:49:15 +00:00
|
|
|
}
|
|
|
|
case test_filetype_d: { // "-d", for directories
|
2012-11-19 08:31:03 +00:00
|
|
|
return !wstat(arg, &buf) && S_ISDIR(buf.st_mode);
|
2016-04-20 02:49:15 +00:00
|
|
|
}
|
|
|
|
case test_filetype_e: { // "-e", for files that exist
|
2012-11-19 08:31:03 +00:00
|
|
|
return !wstat(arg, &buf);
|
2016-04-20 02:49:15 +00:00
|
|
|
}
|
|
|
|
case test_filetype_f: { // "-f", for for regular files
|
2012-11-19 08:31:03 +00:00
|
|
|
return !wstat(arg, &buf) && S_ISREG(buf.st_mode);
|
2016-04-20 02:49:15 +00:00
|
|
|
}
|
|
|
|
case test_filetype_G: { // "-G", for check effective group id
|
2013-08-25 01:10:19 +00:00
|
|
|
return !wstat(arg, &buf) && getegid() == buf.st_gid;
|
2016-04-20 02:49:15 +00:00
|
|
|
}
|
|
|
|
case test_filetype_g: { // "-g", for set-group-id
|
2012-11-19 08:31:03 +00:00
|
|
|
return !wstat(arg, &buf) && (S_ISGID & buf.st_mode);
|
2016-04-20 02:49:15 +00:00
|
|
|
}
|
|
|
|
case test_filetype_h: // "-h", for symbolic links
|
|
|
|
case test_filetype_L: { // "-L", same as -h
|
2012-11-19 08:31:03 +00:00
|
|
|
return !lwstat(arg, &buf) && S_ISLNK(buf.st_mode);
|
2016-04-20 02:49:15 +00:00
|
|
|
}
|
|
|
|
case test_filetype_O: { // "-O", for check effective user id
|
2013-08-25 01:10:19 +00:00
|
|
|
return !wstat(arg, &buf) && geteuid() == buf.st_uid;
|
2016-04-20 02:49:15 +00:00
|
|
|
}
|
|
|
|
case test_filetype_p: { // "-p", for FIFO
|
2012-11-19 08:31:03 +00:00
|
|
|
return !wstat(arg, &buf) && S_ISFIFO(buf.st_mode);
|
2016-04-20 02:49:15 +00:00
|
|
|
}
|
|
|
|
case test_filetype_S: { // "-S", socket
|
2012-11-19 08:31:03 +00:00
|
|
|
return !wstat(arg, &buf) && S_ISSOCK(buf.st_mode);
|
2016-04-20 02:49:15 +00:00
|
|
|
}
|
|
|
|
case test_filesize_s: { // "-s", size greater than zero
|
2012-11-19 08:31:03 +00:00
|
|
|
return !wstat(arg, &buf) && buf.st_size > 0;
|
2016-04-20 02:49:15 +00:00
|
|
|
}
|
|
|
|
case test_filedesc_t: { // "-t", whether the fd is associated with a terminal
|
2012-11-19 08:31:03 +00:00
|
|
|
return parse_number(arg, &num) && num == (int)num && isatty((int)num);
|
2016-04-20 02:49:15 +00:00
|
|
|
}
|
|
|
|
case test_fileperm_r: { // "-r", read permission
|
2012-11-19 08:31:03 +00:00
|
|
|
return !waccess(arg, R_OK);
|
2016-04-20 02:49:15 +00:00
|
|
|
}
|
|
|
|
case test_fileperm_u: { // "-u", whether file is setuid
|
2012-11-19 08:31:03 +00:00
|
|
|
return !wstat(arg, &buf) && (S_ISUID & buf.st_mode);
|
2016-04-20 02:49:15 +00:00
|
|
|
}
|
|
|
|
case test_fileperm_w: { // "-w", whether file write permission is allowed
|
2012-11-19 08:31:03 +00:00
|
|
|
return !waccess(arg, W_OK);
|
2016-04-20 02:49:15 +00:00
|
|
|
}
|
|
|
|
case test_fileperm_x: { // "-x", whether file execute/search is allowed
|
2012-11-19 08:31:03 +00:00
|
|
|
return !waccess(arg, X_OK);
|
2016-04-20 02:49:15 +00: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 08:31:03 +00:00
|
|
|
return arg.empty();
|
2016-04-20 02:49:15 +00:00
|
|
|
}
|
|
|
|
default: {
|
2012-11-19 08:31:03 +00:00
|
|
|
errors.push_back(format_string(L"Unknown token type in %s", __func__));
|
|
|
|
return false;
|
2016-04-20 02:49:15 +00:00
|
|
|
}
|
2012-03-07 08:54:01 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-03-07 08:54:01 +00:00
|
|
|
};
|
|
|
|
|
2016-04-20 02:49:15 +00: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) {
|
2012-03-07 08:54:01 +00:00
|
|
|
using namespace test_expressions;
|
2013-03-22 00:44:51 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
// The first argument should be the name of the command ('test').
|
|
|
|
if (!argv[0]) return BUILTIN_TEST_FAIL;
|
2013-03-22 00:44:51 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
// Whether we are invoked with bracket '[' or not.
|
|
|
|
const bool is_bracket = !wcscmp(argv[0], L"[");
|
2013-03-22 00:44:51 +00:00
|
|
|
|
2012-03-07 08:54:01 +00:00
|
|
|
size_t argc = 0;
|
2016-04-20 02:49:15 +00: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"]")) {
|
|
|
|
// Ignore the closing bracketp.
|
2013-01-05 09:30:03 +00:00
|
|
|
argc--;
|
2016-04-20 02:49:15 +00:00
|
|
|
} else {
|
2015-09-21 18:24:49 +00:00
|
|
|
streams.err.append(L"[: the last argument must be ']'\n");
|
2013-01-05 09:30:03 +00:00
|
|
|
return BUILTIN_TEST_FAIL;
|
|
|
|
}
|
|
|
|
}
|
2013-03-22 00:44:51 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
// Collect the arguments into a list.
|
2012-03-07 08:54:01 +00:00
|
|
|
const wcstring_list_t args(argv + 1, argv + 1 + argc);
|
2013-03-22 00:44:51 +00:00
|
|
|
|
2016-04-20 02:49:15 +00:00
|
|
|
switch (argc) {
|
|
|
|
case 0: {
|
|
|
|
// Per 1003.1, exit false.
|
2012-03-07 08:54:01 +00:00
|
|
|
return BUILTIN_TEST_FAIL;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-04-20 02:49:15 +00:00
|
|
|
case 1: {
|
|
|
|
// Per 1003.1, exit true if the arg is non-empty.
|
2013-03-03 21:22:00 +00:00
|
|
|
return args.at(0).empty() ? BUILTIN_TEST_FAIL : BUILTIN_TEST_SUCCESS;
|
|
|
|
}
|
2016-04-20 02:49:15 +00:00
|
|
|
default: {
|
2013-03-03 21:22:00 +00:00
|
|
|
// Try parsing. If expr is not nil, we are responsible for deleting it.
|
|
|
|
wcstring err;
|
|
|
|
expression *expr = test_parser::parse_args(args, err);
|
2016-04-20 02:49:15 +00:00
|
|
|
if (!expr) {
|
2013-03-03 21:22:00 +00:00
|
|
|
#if 0
|
|
|
|
printf("Oops! test was given args:\n");
|
2016-04-20 02:49:15 +00:00
|
|
|
for (size_t i=0; i < argc; i++) {
|
2013-03-03 21:22:00 +00:00
|
|
|
printf("\t%ls\n", args.at(i).c_str());
|
|
|
|
}
|
|
|
|
printf("and returned parse error: %ls\n", err.c_str());
|
|
|
|
#endif
|
2015-09-21 18:24:49 +00:00
|
|
|
streams.err.append(err);
|
2013-03-03 21:22:00 +00:00
|
|
|
return BUILTIN_TEST_FAIL;
|
2016-05-04 22:19:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wcstring_list_t eval_errors;
|
|
|
|
bool result = expr->evaluate(eval_errors);
|
|
|
|
if (!eval_errors.empty()) {
|
|
|
|
printf("test returned eval errors:\n");
|
|
|
|
for (size_t i = 0; i < eval_errors.size(); i++) {
|
|
|
|
printf("\t%ls\n", eval_errors.at(i).c_str());
|
2012-03-07 08:54:01 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-04 22:19:47 +00:00
|
|
|
delete expr;
|
|
|
|
return result ? BUILTIN_TEST_SUCCESS : BUILTIN_TEST_FAIL;
|
2012-03-07 08:54:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|