12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031 |
- #include "libbb.h"
- #include <regex.h>
- #include <fnmatch.h>
- #define TEST_DEBUG 0
- #if ENABLE_TEST2 \
- || (ENABLE_ASH_BASH_COMPAT && ENABLE_ASH_TEST) \
- || (ENABLE_HUSH_BASH_COMPAT && ENABLE_HUSH_TEST)
- # define BASH_TEST2 1
- #else
- # define BASH_TEST2 0
- #endif
- enum token {
- EOI,
- FILRD,
- FILWR,
- FILEX,
- FILEXIST,
- FILREG,
- FILDIR,
- FILCDEV,
- FILBDEV,
- FILFIFO,
- FILSOCK,
- FILSYM,
- FILGZ,
- FILTT,
- FILSUID,
- FILSGID,
- FILSTCK,
- FILNT,
- FILOT,
- FILEQ,
- FILUID,
- FILGID,
- STREZ,
- STRNZ,
- STREQ,
- STRNE,
- STRLT,
- STRGT,
- #if BASH_TEST2
- REGEX,
- #endif
- INTEQ,
- INTNE,
- INTGE,
- INTGT,
- INTLE,
- INTLT,
- UNOT,
- BAND,
- BOR,
- LPAREN,
- RPAREN,
- OPERAND
- };
- #define is_int_op(a) (((unsigned char)((a) - INTEQ)) <= 5)
- #define is_str_op(a) (((unsigned char)((a) - STREZ)) <= 5)
- #define is_file_op(a) (((unsigned char)((a) - FILNT)) <= 2)
- #define is_file_access(a) (((unsigned char)((a) - FILRD)) <= 2)
- #define is_file_type(a) (((unsigned char)((a) - FILREG)) <= 5)
- #define is_file_bit(a) (((unsigned char)((a) - FILSUID)) <= 2)
- #if TEST_DEBUG
- int depth;
- #define nest_msg(...) do { \
- depth++; \
- fprintf(stderr, "%*s", depth*2, ""); \
- fprintf(stderr, __VA_ARGS__); \
- } while (0)
- #define unnest_msg(...) do { \
- fprintf(stderr, "%*s", depth*2, ""); \
- fprintf(stderr, __VA_ARGS__); \
- depth--; \
- } while (0)
- #define dbg_msg(...) do { \
- fprintf(stderr, "%*s", depth*2, ""); \
- fprintf(stderr, __VA_ARGS__); \
- } while (0)
- #define unnest_msg_and_return(expr, ...) do { \
- number_t __res = (expr); \
- fprintf(stderr, "%*s", depth*2, ""); \
- fprintf(stderr, __VA_ARGS__, res); \
- depth--; \
- return __res; \
- } while (0)
- static const char *const TOKSTR[] ALIGN_PTR = {
- "EOI",
- "FILRD",
- "FILWR",
- "FILEX",
- "FILEXIST",
- "FILREG",
- "FILDIR",
- "FILCDEV",
- "FILBDEV",
- "FILFIFO",
- "FILSOCK",
- "FILSYM",
- "FILGZ",
- "FILTT",
- "FILSUID",
- "FILSGID",
- "FILSTCK",
- "FILNT",
- "FILOT",
- "FILEQ",
- "FILUID",
- "FILGID",
- "STREZ",
- "STRNZ",
- "STREQ",
- "STRNE",
- "STRLT",
- "STRGT",
- #if BASH_TEST2
- "REGEX",
- #endif
- "INTEQ",
- "INTNE",
- "INTGE",
- "INTGT",
- "INTLE",
- "INTLT",
- "UNOT",
- "BAND",
- "BOR",
- "LPAREN",
- "RPAREN",
- "OPERAND"
- };
- #else
- #define nest_msg(...) ((void)0)
- #define unnest_msg(...) ((void)0)
- #define dbg_msg(...) ((void)0)
- #define unnest_msg_and_return(expr, ...) return expr
- #endif
- enum {
- UNOP,
- BINOP,
- BUNOP,
- BBINOP,
- PAREN
- };
- struct operator_t {
- unsigned char op_num, op_type;
- };
- static const struct operator_t ops_table[] ALIGN2 = {
- { FILRD , UNOP },
- { FILWR , UNOP },
- { FILEX , UNOP },
- { FILEXIST, UNOP },
- { FILREG , UNOP },
- { FILDIR , UNOP },
- { FILCDEV , UNOP },
- { FILBDEV , UNOP },
- { FILFIFO , UNOP },
- { FILSUID , UNOP },
- { FILSGID , UNOP },
- { FILSTCK , UNOP },
- { FILGZ , UNOP },
- { FILTT , UNOP },
- { STREZ , UNOP },
- { STRNZ , UNOP },
- { FILSYM , UNOP },
- { FILUID , UNOP },
- { FILGID , UNOP },
- { FILSYM , UNOP },
- { FILSOCK , UNOP },
- { STREQ , BINOP },
-
- { STREQ , BINOP },
- { STRNE , BINOP },
- { STRLT , BINOP },
- { STRGT , BINOP },
- #if BASH_TEST2
- { REGEX , BINOP },
- #endif
- { INTEQ , BINOP },
- { INTNE , BINOP },
- { INTGE , BINOP },
- { INTGT , BINOP },
- { INTLE , BINOP },
- { INTLT , BINOP },
- { FILNT , BINOP },
- { FILOT , BINOP },
- { FILEQ , BINOP },
- { UNOT , BUNOP },
- { BAND , BBINOP },
- { BOR , BBINOP },
- #if BASH_TEST2
- { BAND , BBINOP },
- { BOR , BBINOP },
- #endif
- { LPAREN , PAREN },
- { RPAREN , PAREN },
- };
- static const char ops_texts[] ALIGN1 =
- "-r" "\0"
- "-w" "\0"
- "-x" "\0"
- "-e" "\0"
- "-f" "\0"
- "-d" "\0"
- "-c" "\0"
- "-b" "\0"
- "-p" "\0"
- "-u" "\0"
- "-g" "\0"
- "-k" "\0"
- "-s" "\0"
- "-t" "\0"
- "-z" "\0"
- "-n" "\0"
- "-h" "\0"
- "-O" "\0"
- "-G" "\0"
- "-L" "\0"
- "-S" "\0"
- "=" "\0"
-
- "==" "\0"
- "!=" "\0"
- "<" "\0"
- ">" "\0"
- #if BASH_TEST2
- "=~" "\0"
- #endif
- "-eq" "\0"
- "-ne" "\0"
- "-ge" "\0"
- "-gt" "\0"
- "-le" "\0"
- "-lt" "\0"
- "-nt" "\0"
- "-ot" "\0"
- "-ef" "\0"
- "!" "\0"
- "-a" "\0"
- "-o" "\0"
- #if BASH_TEST2
- "&&" "\0"
- "||" "\0"
- #endif
- "(" "\0"
- ")" "\0"
- ;
- #if ENABLE_FEATURE_TEST_64
- typedef int64_t number_t;
- #else
- typedef int number_t;
- #endif
- struct test_statics {
- char **args;
-
- const struct operator_t *last_operator;
- struct cached_groupinfo *groupinfo;
- #if BASH_TEST2
- bool bash_test2;
- #endif
- jmp_buf leaving;
- };
- extern struct test_statics *BB_GLOBAL_CONST test_ptr_to_statics;
- #define S (*test_ptr_to_statics)
- #define args (S.args )
- #define last_operator (S.last_operator)
- #define groupinfo (S.groupinfo )
- #define bash_test2 (S.bash_test2 )
- #define leaving (S.leaving )
- #define INIT_S() do { \
- XZALLOC_CONST_PTR(&test_ptr_to_statics, sizeof(S)); \
- } while (0)
- #define DEINIT_S() do { \
- free(test_ptr_to_statics); \
- } while (0)
- static number_t primary(enum token n);
- static void syntax(const char *op, const char *msg) NORETURN;
- static void syntax(const char *op, const char *msg)
- {
- if (op && *op) {
- bb_error_msg("%s: %s", op, msg);
- } else {
- bb_error_msg("%s: %s"+4, msg);
- }
- longjmp(leaving, 2);
- }
- static number_t getn(const char *s)
- {
- char *p;
- #if ENABLE_FEATURE_TEST_64
- long long r;
- #else
- long r;
- #endif
- errno = 0;
- #if ENABLE_FEATURE_TEST_64
- r = strtoll(s, &p, 10);
- #else
- r = strtol(s, &p, 10);
- #endif
- if (errno != 0)
- syntax(s, "out of range");
- if (p == s || *(skip_whitespace(p)) != '\0')
- syntax(s, "bad number");
- return r;
- }
- static enum token check_operator(const char *s)
- {
- static const struct operator_t no_op = {
- .op_num = -1,
- .op_type = -1
- };
- int n;
- last_operator = &no_op;
- if (s == NULL)
- return EOI;
- n = index_in_strings(ops_texts, s);
- if (n < 0)
- return OPERAND;
- #if BASH_TEST2
- if (ops_table[n].op_num == REGEX && !bash_test2) {
-
- return OPERAND;
- }
- if (ops_table[n].op_num == BAND || ops_table[n].op_num == BOR) {
-
-
- if (bash_test2 == (s[0] == '-'))
- return OPERAND;
- }
- #endif
- last_operator = &ops_table[n];
- return ops_table[n].op_num;
- }
- static int binop(void)
- {
- const char *opnd1, *opnd2;
- const struct operator_t *op;
- number_t val1, val2;
- opnd1 = *args;
- check_operator(*++args);
- op = last_operator;
- opnd2 = *++args;
- if (opnd2 == NULL)
- syntax(args[-1], "argument expected");
- if (is_int_op(op->op_num)) {
- val1 = getn(opnd1);
- val2 = getn(opnd2);
- if (op->op_num == INTEQ)
- return val1 == val2;
- if (op->op_num == INTNE)
- return val1 != val2;
- if (op->op_num == INTGE)
- return val1 >= val2;
- if (op->op_num == INTGT)
- return val1 > val2;
- if (op->op_num == INTLE)
- return val1 <= val2;
-
- return val1 < val2;
- }
- #if BASH_TEST2
- if (bash_test2) {
- if (op->op_num == STREQ) {
- val1 = fnmatch(opnd2, opnd1, 0);
- return val1 == 0;
- }
- if (op->op_num == STRNE) {
- val1 = fnmatch(opnd2, opnd1, 0);
- return val1 != 0;
- }
- if (op->op_num == REGEX) {
- regex_t re_buffer;
- memset(&re_buffer, 0, sizeof(re_buffer));
- if (regcomp(&re_buffer, opnd2, REG_EXTENDED)) {
-
- longjmp(leaving, 2);
- }
- val1 = regexec(&re_buffer, opnd1, 0, NULL, 0);
- regfree(&re_buffer);
- return val1 == 0;
- }
- }
- #endif
- if (is_str_op(op->op_num)) {
- val1 = strcmp(opnd1, opnd2);
- if (op->op_num == STREQ)
- return val1 == 0;
- if (op->op_num == STRNE)
- return val1 != 0;
- if (op->op_num == STRLT)
- return val1 < 0;
-
- return val1 > 0;
- }
-
- {
- struct stat b1, b2;
- if (stat(opnd1, &b1) || stat(opnd2, &b2))
- return 0;
- if (op->op_num == FILNT)
- return b1.st_mtime > b2.st_mtime;
- if (op->op_num == FILOT)
- return b1.st_mtime < b2.st_mtime;
-
- return b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino;
- }
-
- }
- static int is_a_group_member(gid_t gid)
- {
-
- if (gid == get_cached_egid(&groupinfo->egid))
- return 1;
- return is_in_supplementary_groups(groupinfo, gid);
- }
- static int test_st_mode(struct stat *st, int mode)
- {
- enum { ANY_IX = S_IXUSR | S_IXGRP | S_IXOTH };
- unsigned euid;
- if (mode == X_OK) {
-
-
-
-
- if ((st->st_mode & ANY_IX) == 0)
- return 0;
- if ((st->st_mode & ANY_IX) == ANY_IX)
- return 1;
- }
- euid = get_cached_euid(&groupinfo->euid);
- if (euid == 0) {
-
- if (mode != X_OK)
- return 1;
-
- mode = S_IXUSR | S_IXGRP | S_IXOTH;
- } else if (st->st_uid == euid)
- mode <<= 6;
- else if (is_a_group_member(st->st_gid))
- mode <<= 3;
- return st->st_mode & mode;
- }
- static int filstat(char *nm, enum token mode)
- {
- struct stat s;
- unsigned i = i;
- if (mode == FILSYM) {
- #ifdef S_IFLNK
- if (lstat(nm, &s) == 0) {
- i = S_IFLNK;
- goto filetype;
- }
- #endif
- return 0;
- }
- if (stat(nm, &s) != 0)
- return 0;
- if (mode == FILEXIST)
- return 1;
- if (is_file_access(mode)) {
- if (mode == FILRD)
- i = R_OK;
- if (mode == FILWR)
- i = W_OK;
- if (mode == FILEX)
- i = X_OK;
- return test_st_mode(&s, i);
- }
- if (is_file_type(mode)) {
- if (mode == FILREG)
- i = S_IFREG;
- if (mode == FILDIR)
- i = S_IFDIR;
- if (mode == FILCDEV)
- i = S_IFCHR;
- if (mode == FILBDEV)
- i = S_IFBLK;
- if (mode == FILFIFO) {
- #ifdef S_IFIFO
- i = S_IFIFO;
- #else
- return 0;
- #endif
- }
- if (mode == FILSOCK) {
- #ifdef S_IFSOCK
- i = S_IFSOCK;
- #else
- return 0;
- #endif
- }
- filetype:
- return ((s.st_mode & S_IFMT) == i);
- }
- if (is_file_bit(mode)) {
- if (mode == FILSUID)
- i = S_ISUID;
- if (mode == FILSGID)
- i = S_ISGID;
- if (mode == FILSTCK)
- i = S_ISVTX;
- return ((s.st_mode & i) != 0);
- }
- if (mode == FILGZ)
- return s.st_size != 0L;
- if (mode == FILUID)
- return s.st_uid == geteuid();
- if (mode == FILGID)
- return s.st_gid == getegid();
- return 1;
- }
- static number_t nexpr(enum token n)
- {
- number_t res;
- nest_msg(">nexpr(%s)\n", TOKSTR[n]);
- if (n == UNOT) {
- n = check_operator(*++args);
- if (n == EOI) {
-
-
- args--;
- unnest_msg("<nexpr:1 (!EOI), args:%s(%p)\n", args[0], &args[0]);
- return 1;
- }
- res = !nexpr(n);
- unnest_msg("<nexpr:%lld\n", res);
- return res;
- }
- res = primary(n);
- unnest_msg("<nexpr:%lld\n", res);
- return res;
- }
- static number_t aexpr(enum token n)
- {
- number_t res;
- nest_msg(">aexpr(%s)\n", TOKSTR[n]);
- res = nexpr(n);
- dbg_msg("aexpr: nexpr:%lld, next args:%s(%p)\n", res, args[1], &args[1]);
- if (check_operator(*++args) == BAND) {
- dbg_msg("aexpr: arg is AND, next args:%s(%p)\n", args[1], &args[1]);
- res = aexpr(check_operator(*++args)) && res;
- unnest_msg("<aexpr:%lld\n", res);
- return res;
- }
- args--;
- unnest_msg("<aexpr:%lld, args:%s(%p)\n", res, args[0], &args[0]);
- return res;
- }
- static number_t oexpr(enum token n)
- {
- number_t res;
- nest_msg(">oexpr(%s)\n", TOKSTR[n]);
- res = aexpr(n);
- dbg_msg("oexpr: aexpr:%lld, next args:%s(%p)\n", res, args[1], &args[1]);
- if (check_operator(*++args) == BOR) {
- dbg_msg("oexpr: next arg is OR, next args:%s(%p)\n", args[1], &args[1]);
- res = oexpr(check_operator(*++args)) || res;
- unnest_msg("<oexpr:%lld\n", res);
- return res;
- }
- args--;
- unnest_msg("<oexpr:%lld, args:%s(%p)\n", res, args[0], &args[0]);
- return res;
- }
- static number_t primary(enum token n)
- {
- #if TEST_DEBUG
- number_t res = res;
- #else
- number_t res;
- #endif
- const struct operator_t *args0_op;
- nest_msg(">primary(%s)\n", TOKSTR[n]);
- if (n == EOI) {
- syntax(NULL, "argument expected");
- }
- if (n == LPAREN) {
- res = oexpr(check_operator(*++args));
- if (check_operator(*++args) != RPAREN)
- syntax(NULL, "closing paren expected");
- unnest_msg("<primary:%lld\n", res);
- return res;
- }
-
- args0_op = last_operator;
-
- if (check_operator(args[1]) != EOI) {
- if (args[2]) {
-
-
-
- if (last_operator->op_type == BINOP)
- unnest_msg_and_return(binop(), "<primary: binop:%lld\n");
- }
- }
-
- if (args0_op->op_type == UNOP) {
-
- if (args[1] == NULL)
- goto check_emptiness;
- args++;
- if (n == STREZ)
- unnest_msg_and_return(args[0][0] == '\0', "<primary:%lld\n");
- if (n == STRNZ)
- unnest_msg_and_return(args[0][0] != '\0', "<primary:%lld\n");
- if (n == FILTT)
- unnest_msg_and_return(isatty(getn(*args)), "<primary: isatty(%s)%lld\n", *args);
- unnest_msg_and_return(filstat(*args, n), "<primary: filstat(%s):%lld\n", *args);
- }
-
- if (last_operator->op_type == BINOP) {
-
- unnest_msg_and_return(binop(), "<primary:%lld\n");
- }
- check_emptiness:
- unnest_msg_and_return(args[0][0] != '\0', "<primary:%lld\n");
- }
- int FAST_FUNC test_main2(struct cached_groupinfo *pgroupinfo, int argc, char **argv)
- {
- int res;
- const char *arg0;
- #if BASH_TEST2
- bool bt2 = 0;
- #endif
- arg0 = bb_basename(argv[0]);
- if ((ENABLE_TEST1 || ENABLE_TEST2 || ENABLE_ASH_TEST || ENABLE_HUSH_TEST)
- && (arg0[0] == '[')
- ) {
- --argc;
- if (!arg0[1]) {
- if (NOT_LONE_CHAR(argv[argc], ']')) {
- bb_simple_error_msg("missing ]");
- return 2;
- }
- } else {
- if (strcmp(argv[argc], "]]") != 0) {
- bb_simple_error_msg("missing ]]");
- return 2;
- }
- #if BASH_TEST2
- bt2 = 1;
- #endif
- }
- argv[argc] = NULL;
- }
-
-
- INIT_S();
- groupinfo = pgroupinfo;
- #if BASH_TEST2
- bash_test2 = bt2;
- #endif
- res = setjmp(leaving);
- if (res)
- goto ret;
-
-
- argv++;
- args = argv;
-
- if (1) {
- int negate = 0;
- again:
- if (!argv[0]) {
-
- res = 1;
- goto ret_special;
- }
- if (!argv[1]) {
-
- res = (argv[0][0] == '\0');
- goto ret_special;
- }
- if (argv[2]) {
- if (!argv[3]) {
-
- check_operator(argv[1]);
- if (last_operator->op_type == BINOP) {
-
- args = argv;
- res = (binop() == 0);
- ret_special:
-
- res ^= negate;
- goto ret;
- }
-
- goto check_negate;
- }
-
- if (!argv[4]) {
-
- if (LONE_CHAR(argv[0], '(')
- && LONE_CHAR(argv[3], ')')
- ) {
-
- argv[3] = NULL;
- argv++;
- }
- }
- }
- check_negate:
- if (LONE_CHAR(argv[0], '!')) {
- argv++;
- negate ^= 1;
- goto again;
- }
- }
- res = !oexpr(check_operator(*args));
- if (*args != NULL && *++args != NULL) {
-
- bb_error_msg("%s: unknown operand", *args);
- res = 2;
- }
- ret:
- DEINIT_S();
- return res;
- }
- int test_main(int argc, char **argv)
- {
- struct cached_groupinfo info;
- int r;
- info.euid = -1;
- info.egid = -1;
- info.ngroups = 0;
- info.supplementary_array = NULL;
- r = test_main2(&info, argc, argv);
- free(info.supplementary_array);
- return r;
- }
|