test.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * test implementation for busybox
  4. *
  5. * Copyright (c) by a whole pile of folks:
  6. *
  7. * test(1); version 7-like -- author Erik Baalbergen
  8. * modified by Eric Gisin to be used as built-in.
  9. * modified by Arnold Robbins to add SVR3 compatibility
  10. * (-x -c -b -p -u -g -k) plus Korn's -L -nt -ot -ef and new -S (socket).
  11. * modified by J.T. Conklin for NetBSD.
  12. * modified by Herbert Xu to be used as built-in in ash.
  13. * modified by Erik Andersen <andersen@codepoet.org> to be used
  14. * in busybox.
  15. * modified by Bernhard Reutner-Fischer to be useable (i.e. a bit less bloaty).
  16. *
  17. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  18. *
  19. * Original copyright notice states:
  20. * "This program is in the Public Domain."
  21. */
  22. #include "libbb.h"
  23. #include <setjmp.h>
  24. /* This is a NOFORK applet. Be very careful! */
  25. /* test_main() is called from shells, and we need to be extra careful here.
  26. * This is true regardless of PREFER_APPLETS and STANDALONE_SHELL
  27. * state. */
  28. /* test(1) accepts the following grammar:
  29. oexpr ::= aexpr | aexpr "-o" oexpr ;
  30. aexpr ::= nexpr | nexpr "-a" aexpr ;
  31. nexpr ::= primary | "!" primary
  32. primary ::= unary-operator operand
  33. | operand binary-operator operand
  34. | operand
  35. | "(" oexpr ")"
  36. ;
  37. unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
  38. "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
  39. binary-operator ::= "="|"=="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
  40. "-nt"|"-ot"|"-ef";
  41. operand ::= <any legal UNIX file name>
  42. */
  43. /* TODO: handle [[ expr ]] bashism bash-compatibly.
  44. * [[ ]] is meant to be a "better [ ]", with less weird syntax
  45. * and without the risk of variables and quoted strings misinterpreted
  46. * as operators.
  47. * This will require support from shells - we need to know quote status
  48. * of each parameter (see below).
  49. *
  50. * Word splitting and pathname expansion should NOT be performed:
  51. * # a="a b"; [[ $a = "a b" ]] && echo YES
  52. * YES
  53. * # [[ /bin/m* ]] && echo YES
  54. * YES
  55. *
  56. * =~ should do regexp match
  57. * = and == should do pattern match against right side:
  58. * # [[ *a* == bab ]] && echo YES
  59. * # [[ bab == *a* ]] && echo YES
  60. * YES
  61. * != does the negated == (i.e., also with pattern matching).
  62. * Pattern matching is quotation-sensitive:
  63. * # [[ bab == "b"a* ]] && echo YES
  64. * YES
  65. * # [[ bab == b"a*" ]] && echo YES
  66. *
  67. * Conditional operators such as -f must be unquoted literals to be recognized:
  68. * # [[ -e /bin ]] && echo YES
  69. * YES
  70. * # [[ '-e' /bin ]] && echo YES
  71. * bash: conditional binary operator expected...
  72. * # A='-e'; [[ $A /bin ]] && echo YES
  73. * bash: conditional binary operator expected...
  74. *
  75. * || and && should work as -o and -a work in [ ]
  76. * -a and -o aren't recognized (&& and || are to be used instead)
  77. * ( and ) do not need to be quoted unlike in [ ]:
  78. * # [[ ( abc ) && '' ]] && echo YES
  79. * # [[ ( abc ) || '' ]] && echo YES
  80. * YES
  81. * # [[ ( abc ) -o '' ]] && echo YES
  82. * bash: syntax error in conditional expression...
  83. *
  84. * Apart from the above, [[ expr ]] should work as [ expr ]
  85. */
  86. #define TEST_DEBUG 0
  87. enum token {
  88. EOI,
  89. FILRD, /* file access */
  90. FILWR,
  91. FILEX,
  92. FILEXIST,
  93. FILREG, /* file type */
  94. FILDIR,
  95. FILCDEV,
  96. FILBDEV,
  97. FILFIFO,
  98. FILSOCK,
  99. FILSYM,
  100. FILGZ,
  101. FILTT,
  102. FILSUID, /* file bit */
  103. FILSGID,
  104. FILSTCK,
  105. FILNT, /* file ops */
  106. FILOT,
  107. FILEQ,
  108. FILUID,
  109. FILGID,
  110. STREZ, /* str ops */
  111. STRNZ,
  112. STREQ,
  113. STRNE,
  114. STRLT,
  115. STRGT,
  116. INTEQ, /* int ops */
  117. INTNE,
  118. INTGE,
  119. INTGT,
  120. INTLE,
  121. INTLT,
  122. UNOT,
  123. BAND,
  124. BOR,
  125. LPAREN,
  126. RPAREN,
  127. OPERAND
  128. };
  129. #define is_int_op(a) (((unsigned char)((a) - INTEQ)) <= 5)
  130. #define is_str_op(a) (((unsigned char)((a) - STREZ)) <= 5)
  131. #define is_file_op(a) (((unsigned char)((a) - FILNT)) <= 2)
  132. #define is_file_access(a) (((unsigned char)((a) - FILRD)) <= 2)
  133. #define is_file_type(a) (((unsigned char)((a) - FILREG)) <= 5)
  134. #define is_file_bit(a) (((unsigned char)((a) - FILSUID)) <= 2)
  135. #if TEST_DEBUG
  136. int depth;
  137. #define nest_msg(...) do { \
  138. depth++; \
  139. fprintf(stderr, "%*s", depth*2, ""); \
  140. fprintf(stderr, __VA_ARGS__); \
  141. } while (0)
  142. #define unnest_msg(...) do { \
  143. fprintf(stderr, "%*s", depth*2, ""); \
  144. fprintf(stderr, __VA_ARGS__); \
  145. depth--; \
  146. } while (0)
  147. #define dbg_msg(...) do { \
  148. fprintf(stderr, "%*s", depth*2, ""); \
  149. fprintf(stderr, __VA_ARGS__); \
  150. } while (0)
  151. #define unnest_msg_and_return(expr, ...) do { \
  152. number_t __res = (expr); \
  153. fprintf(stderr, "%*s", depth*2, ""); \
  154. fprintf(stderr, __VA_ARGS__, res); \
  155. depth--; \
  156. return __res; \
  157. } while (0)
  158. static const char *const TOKSTR[] = {
  159. "EOI",
  160. "FILRD",
  161. "FILWR",
  162. "FILEX",
  163. "FILEXIST",
  164. "FILREG",
  165. "FILDIR",
  166. "FILCDEV",
  167. "FILBDEV",
  168. "FILFIFO",
  169. "FILSOCK",
  170. "FILSYM",
  171. "FILGZ",
  172. "FILTT",
  173. "FILSUID",
  174. "FILSGID",
  175. "FILSTCK",
  176. "FILNT",
  177. "FILOT",
  178. "FILEQ",
  179. "FILUID",
  180. "FILGID",
  181. "STREZ",
  182. "STRNZ",
  183. "STREQ",
  184. "STRNE",
  185. "STRLT",
  186. "STRGT",
  187. "INTEQ",
  188. "INTNE",
  189. "INTGE",
  190. "INTGT",
  191. "INTLE",
  192. "INTLT",
  193. "UNOT",
  194. "BAND",
  195. "BOR",
  196. "LPAREN",
  197. "RPAREN",
  198. "OPERAND"
  199. };
  200. #else
  201. #define nest_msg(...) ((void)0)
  202. #define unnest_msg(...) ((void)0)
  203. #define dbg_msg(...) ((void)0)
  204. #define unnest_msg_and_return(expr, ...) return expr
  205. #endif
  206. enum {
  207. UNOP,
  208. BINOP,
  209. BUNOP,
  210. BBINOP,
  211. PAREN
  212. };
  213. struct operator_t {
  214. unsigned char op_num, op_type;
  215. };
  216. static const struct operator_t ops_table[] = {
  217. { /* "-r" */ FILRD , UNOP },
  218. { /* "-w" */ FILWR , UNOP },
  219. { /* "-x" */ FILEX , UNOP },
  220. { /* "-e" */ FILEXIST, UNOP },
  221. { /* "-f" */ FILREG , UNOP },
  222. { /* "-d" */ FILDIR , UNOP },
  223. { /* "-c" */ FILCDEV , UNOP },
  224. { /* "-b" */ FILBDEV , UNOP },
  225. { /* "-p" */ FILFIFO , UNOP },
  226. { /* "-u" */ FILSUID , UNOP },
  227. { /* "-g" */ FILSGID , UNOP },
  228. { /* "-k" */ FILSTCK , UNOP },
  229. { /* "-s" */ FILGZ , UNOP },
  230. { /* "-t" */ FILTT , UNOP },
  231. { /* "-z" */ STREZ , UNOP },
  232. { /* "-n" */ STRNZ , UNOP },
  233. { /* "-h" */ FILSYM , UNOP }, /* for backwards compat */
  234. { /* "-O" */ FILUID , UNOP },
  235. { /* "-G" */ FILGID , UNOP },
  236. { /* "-L" */ FILSYM , UNOP },
  237. { /* "-S" */ FILSOCK , UNOP },
  238. { /* "=" */ STREQ , BINOP },
  239. { /* "==" */ STREQ , BINOP },
  240. { /* "!=" */ STRNE , BINOP },
  241. { /* "<" */ STRLT , BINOP },
  242. { /* ">" */ STRGT , BINOP },
  243. { /* "-eq"*/ INTEQ , BINOP },
  244. { /* "-ne"*/ INTNE , BINOP },
  245. { /* "-ge"*/ INTGE , BINOP },
  246. { /* "-gt"*/ INTGT , BINOP },
  247. { /* "-le"*/ INTLE , BINOP },
  248. { /* "-lt"*/ INTLT , BINOP },
  249. { /* "-nt"*/ FILNT , BINOP },
  250. { /* "-ot"*/ FILOT , BINOP },
  251. { /* "-ef"*/ FILEQ , BINOP },
  252. { /* "!" */ UNOT , BUNOP },
  253. { /* "-a" */ BAND , BBINOP },
  254. { /* "-o" */ BOR , BBINOP },
  255. { /* "(" */ LPAREN , PAREN },
  256. { /* ")" */ RPAREN , PAREN },
  257. };
  258. /* Please keep these two tables in sync */
  259. static const char ops_texts[] ALIGN1 =
  260. "-r" "\0"
  261. "-w" "\0"
  262. "-x" "\0"
  263. "-e" "\0"
  264. "-f" "\0"
  265. "-d" "\0"
  266. "-c" "\0"
  267. "-b" "\0"
  268. "-p" "\0"
  269. "-u" "\0"
  270. "-g" "\0"
  271. "-k" "\0"
  272. "-s" "\0"
  273. "-t" "\0"
  274. "-z" "\0"
  275. "-n" "\0"
  276. "-h" "\0"
  277. "-O" "\0"
  278. "-G" "\0"
  279. "-L" "\0"
  280. "-S" "\0"
  281. "=" "\0"
  282. "==" "\0"
  283. "!=" "\0"
  284. "<" "\0"
  285. ">" "\0"
  286. "-eq" "\0"
  287. "-ne" "\0"
  288. "-ge" "\0"
  289. "-gt" "\0"
  290. "-le" "\0"
  291. "-lt" "\0"
  292. "-nt" "\0"
  293. "-ot" "\0"
  294. "-ef" "\0"
  295. "!" "\0"
  296. "-a" "\0"
  297. "-o" "\0"
  298. "(" "\0"
  299. ")" "\0"
  300. ;
  301. #if ENABLE_FEATURE_TEST_64
  302. typedef int64_t number_t;
  303. #else
  304. typedef int number_t;
  305. #endif
  306. /* We try to minimize both static and stack usage. */
  307. struct test_statics {
  308. char **args;
  309. /* set only by check_operator(), either to bogus struct
  310. * or points to matching operator_t struct. Never NULL. */
  311. const struct operator_t *last_operator;
  312. gid_t *group_array;
  313. int ngroups;
  314. jmp_buf leaving;
  315. };
  316. /* See test_ptr_hack.c */
  317. extern struct test_statics *const test_ptr_to_statics;
  318. #define S (*test_ptr_to_statics)
  319. #define args (S.args )
  320. #define last_operator (S.last_operator)
  321. #define group_array (S.group_array )
  322. #define ngroups (S.ngroups )
  323. #define leaving (S.leaving )
  324. #define INIT_S() do { \
  325. (*(struct test_statics**)&test_ptr_to_statics) = xzalloc(sizeof(S)); \
  326. barrier(); \
  327. } while (0)
  328. #define DEINIT_S() do { \
  329. free(test_ptr_to_statics); \
  330. } while (0)
  331. static number_t primary(enum token n);
  332. static void syntax(const char *op, const char *msg) NORETURN;
  333. static void syntax(const char *op, const char *msg)
  334. {
  335. if (op && *op) {
  336. bb_error_msg("%s: %s", op, msg);
  337. } else {
  338. bb_error_msg("%s: %s"+4, msg);
  339. }
  340. longjmp(leaving, 2);
  341. }
  342. /* atoi with error detection */
  343. //XXX: FIXME: duplicate of existing libbb function?
  344. static number_t getn(const char *s)
  345. {
  346. char *p;
  347. #if ENABLE_FEATURE_TEST_64
  348. long long r;
  349. #else
  350. long r;
  351. #endif
  352. errno = 0;
  353. #if ENABLE_FEATURE_TEST_64
  354. r = strtoll(s, &p, 10);
  355. #else
  356. r = strtol(s, &p, 10);
  357. #endif
  358. if (errno != 0)
  359. syntax(s, "out of range");
  360. if (*(skip_whitespace(p)))
  361. syntax(s, "bad number");
  362. return r;
  363. }
  364. /* UNUSED
  365. static int newerf(const char *f1, const char *f2)
  366. {
  367. struct stat b1, b2;
  368. return (stat(f1, &b1) == 0 &&
  369. stat(f2, &b2) == 0 && b1.st_mtime > b2.st_mtime);
  370. }
  371. static int olderf(const char *f1, const char *f2)
  372. {
  373. struct stat b1, b2;
  374. return (stat(f1, &b1) == 0 &&
  375. stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime);
  376. }
  377. static int equalf(const char *f1, const char *f2)
  378. {
  379. struct stat b1, b2;
  380. return (stat(f1, &b1) == 0 &&
  381. stat(f2, &b2) == 0 &&
  382. b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino);
  383. }
  384. */
  385. static enum token check_operator(const char *s)
  386. {
  387. static const struct operator_t no_op = {
  388. .op_num = -1,
  389. .op_type = -1
  390. };
  391. int n;
  392. last_operator = &no_op;
  393. if (s == NULL)
  394. return EOI;
  395. n = index_in_strings(ops_texts, s);
  396. if (n < 0)
  397. return OPERAND;
  398. last_operator = &ops_table[n];
  399. return ops_table[n].op_num;
  400. }
  401. static int binop(void)
  402. {
  403. const char *opnd1, *opnd2;
  404. const struct operator_t *op;
  405. number_t val1, val2;
  406. opnd1 = *args;
  407. check_operator(*++args);
  408. op = last_operator;
  409. opnd2 = *++args;
  410. if (opnd2 == NULL)
  411. syntax(args[-1], "argument expected");
  412. if (is_int_op(op->op_num)) {
  413. val1 = getn(opnd1);
  414. val2 = getn(opnd2);
  415. if (op->op_num == INTEQ)
  416. return val1 == val2;
  417. if (op->op_num == INTNE)
  418. return val1 != val2;
  419. if (op->op_num == INTGE)
  420. return val1 >= val2;
  421. if (op->op_num == INTGT)
  422. return val1 > val2;
  423. if (op->op_num == INTLE)
  424. return val1 <= val2;
  425. /*if (op->op_num == INTLT)*/
  426. return val1 < val2;
  427. }
  428. if (is_str_op(op->op_num)) {
  429. val1 = strcmp(opnd1, opnd2);
  430. if (op->op_num == STREQ)
  431. return val1 == 0;
  432. if (op->op_num == STRNE)
  433. return val1 != 0;
  434. if (op->op_num == STRLT)
  435. return val1 < 0;
  436. /*if (op->op_num == STRGT)*/
  437. return val1 > 0;
  438. }
  439. /* We are sure that these three are by now the only binops we didn't check
  440. * yet, so we do not check if the class is correct:
  441. */
  442. /* if (is_file_op(op->op_num)) */
  443. {
  444. struct stat b1, b2;
  445. if (stat(opnd1, &b1) || stat(opnd2, &b2))
  446. return 0; /* false, since at least one stat failed */
  447. if (op->op_num == FILNT)
  448. return b1.st_mtime > b2.st_mtime;
  449. if (op->op_num == FILOT)
  450. return b1.st_mtime < b2.st_mtime;
  451. /*if (op->op_num == FILEQ)*/
  452. return b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino;
  453. }
  454. /*return 1; - NOTREACHED */
  455. }
  456. static void initialize_group_array(void)
  457. {
  458. int n;
  459. /* getgroups may be expensive, try to use it only once */
  460. ngroups = 32;
  461. do {
  462. /* FIXME: ash tries so hard to not die on OOM,
  463. * and we spoil it with just one xrealloc here */
  464. /* We realloc, because test_main can be entered repeatedly by shell.
  465. * Testcase (ash): 'while true; do test -x some_file; done'
  466. * and watch top. (some_file must have owner != you) */
  467. n = ngroups;
  468. group_array = xrealloc(group_array, n * sizeof(gid_t));
  469. ngroups = getgroups(n, group_array);
  470. } while (ngroups > n);
  471. }
  472. /* Return non-zero if GID is one that we have in our groups list. */
  473. //XXX: FIXME: duplicate of existing libbb function?
  474. // see toplevel TODO file:
  475. // possible code duplication ingroup() and is_a_group_member()
  476. static int is_a_group_member(gid_t gid)
  477. {
  478. int i;
  479. /* Short-circuit if possible, maybe saving a call to getgroups(). */
  480. if (gid == getgid() || gid == getegid())
  481. return 1;
  482. if (ngroups == 0)
  483. initialize_group_array();
  484. /* Search through the list looking for GID. */
  485. for (i = 0; i < ngroups; i++)
  486. if (gid == group_array[i])
  487. return 1;
  488. return 0;
  489. }
  490. /* Do the same thing access(2) does, but use the effective uid and gid,
  491. and don't make the mistake of telling root that any file is
  492. executable. */
  493. static int test_eaccess(char *path, int mode)
  494. {
  495. struct stat st;
  496. unsigned int euid = geteuid();
  497. if (stat(path, &st) < 0)
  498. return -1;
  499. if (euid == 0) {
  500. /* Root can read or write any file. */
  501. if (mode != X_OK)
  502. return 0;
  503. /* Root can execute any file that has any one of the execute
  504. bits set. */
  505. if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
  506. return 0;
  507. }
  508. if (st.st_uid == euid) /* owner */
  509. mode <<= 6;
  510. else if (is_a_group_member(st.st_gid))
  511. mode <<= 3;
  512. if (st.st_mode & mode)
  513. return 0;
  514. return -1;
  515. }
  516. static int filstat(char *nm, enum token mode)
  517. {
  518. struct stat s;
  519. unsigned i = i; /* gcc 3.x thinks it can be used uninitialized */
  520. if (mode == FILSYM) {
  521. #ifdef S_IFLNK
  522. if (lstat(nm, &s) == 0) {
  523. i = S_IFLNK;
  524. goto filetype;
  525. }
  526. #endif
  527. return 0;
  528. }
  529. if (stat(nm, &s) != 0)
  530. return 0;
  531. if (mode == FILEXIST)
  532. return 1;
  533. if (is_file_access(mode)) {
  534. if (mode == FILRD)
  535. i = R_OK;
  536. if (mode == FILWR)
  537. i = W_OK;
  538. if (mode == FILEX)
  539. i = X_OK;
  540. return test_eaccess(nm, i) == 0;
  541. }
  542. if (is_file_type(mode)) {
  543. if (mode == FILREG)
  544. i = S_IFREG;
  545. if (mode == FILDIR)
  546. i = S_IFDIR;
  547. if (mode == FILCDEV)
  548. i = S_IFCHR;
  549. if (mode == FILBDEV)
  550. i = S_IFBLK;
  551. if (mode == FILFIFO) {
  552. #ifdef S_IFIFO
  553. i = S_IFIFO;
  554. #else
  555. return 0;
  556. #endif
  557. }
  558. if (mode == FILSOCK) {
  559. #ifdef S_IFSOCK
  560. i = S_IFSOCK;
  561. #else
  562. return 0;
  563. #endif
  564. }
  565. filetype:
  566. return ((s.st_mode & S_IFMT) == i);
  567. }
  568. if (is_file_bit(mode)) {
  569. if (mode == FILSUID)
  570. i = S_ISUID;
  571. if (mode == FILSGID)
  572. i = S_ISGID;
  573. if (mode == FILSTCK)
  574. i = S_ISVTX;
  575. return ((s.st_mode & i) != 0);
  576. }
  577. if (mode == FILGZ)
  578. return s.st_size > 0L;
  579. if (mode == FILUID)
  580. return s.st_uid == geteuid();
  581. if (mode == FILGID)
  582. return s.st_gid == getegid();
  583. return 1; /* NOTREACHED */
  584. }
  585. static number_t nexpr(enum token n)
  586. {
  587. number_t res;
  588. nest_msg(">nexpr(%s)\n", TOKSTR[n]);
  589. if (n == UNOT) {
  590. n = check_operator(*++args);
  591. if (n == EOI) {
  592. /* special case: [ ! ], [ a -a ! ] are valid */
  593. /* IOW, "! ARG" may miss ARG */
  594. unnest_msg("<nexpr:1 (!EOI)\n");
  595. return 1;
  596. }
  597. res = !nexpr(n);
  598. unnest_msg("<nexpr:%lld\n", res);
  599. return res;
  600. }
  601. res = primary(n);
  602. unnest_msg("<nexpr:%lld\n", res);
  603. return res;
  604. }
  605. static number_t aexpr(enum token n)
  606. {
  607. number_t res;
  608. nest_msg(">aexpr(%s)\n", TOKSTR[n]);
  609. res = nexpr(n);
  610. dbg_msg("aexpr: nexpr:%lld, next args:%s\n", res, args[1]);
  611. if (check_operator(*++args) == BAND) {
  612. dbg_msg("aexpr: arg is AND, next args:%s\n", args[1]);
  613. res = aexpr(check_operator(*++args)) && res;
  614. unnest_msg("<aexpr:%lld\n", res);
  615. return res;
  616. }
  617. args--;
  618. unnest_msg("<aexpr:%lld, args:%s\n", res, args[0]);
  619. return res;
  620. }
  621. static number_t oexpr(enum token n)
  622. {
  623. number_t res;
  624. nest_msg(">oexpr(%s)\n", TOKSTR[n]);
  625. res = aexpr(n);
  626. dbg_msg("oexpr: aexpr:%lld, next args:%s\n", res, args[1]);
  627. if (check_operator(*++args) == BOR) {
  628. dbg_msg("oexpr: next arg is OR, next args:%s\n", args[1]);
  629. res = oexpr(check_operator(*++args)) || res;
  630. unnest_msg("<oexpr:%lld\n", res);
  631. return res;
  632. }
  633. args--;
  634. unnest_msg("<oexpr:%lld, args:%s\n", res, args[0]);
  635. return res;
  636. }
  637. static number_t primary(enum token n)
  638. {
  639. #if TEST_DEBUG
  640. number_t res = res; /* for compiler */
  641. #else
  642. number_t res;
  643. #endif
  644. const struct operator_t *args0_op;
  645. nest_msg(">primary(%s)\n", TOKSTR[n]);
  646. if (n == EOI) {
  647. syntax(NULL, "argument expected");
  648. }
  649. if (n == LPAREN) {
  650. res = oexpr(check_operator(*++args));
  651. if (check_operator(*++args) != RPAREN)
  652. syntax(NULL, "closing paren expected");
  653. unnest_msg("<primary:%lld\n", res);
  654. return res;
  655. }
  656. /* coreutils 6.9 checks "is args[1] binop and args[2] exist?" first,
  657. * do the same */
  658. args0_op = last_operator;
  659. /* last_operator = operator at args[1] */
  660. if (check_operator(args[1]) != EOI) { /* if args[1] != NULL */
  661. if (args[2]) {
  662. // coreutils also does this:
  663. // if (args[3] && args[0]="-l" && args[2] is BINOP)
  664. // return binop(1 /* prepended by -l */);
  665. if (last_operator->op_type == BINOP)
  666. unnest_msg_and_return(binop(), "<primary: binop:%lld\n");
  667. }
  668. }
  669. /* check "is args[0] unop?" second */
  670. if (args0_op->op_type == UNOP) {
  671. /* unary expression */
  672. if (args[1] == NULL)
  673. // syntax(args0_op->op_text, "argument expected");
  674. goto check_emptiness;
  675. args++;
  676. if (n == STREZ)
  677. unnest_msg_and_return(args[0][0] == '\0', "<primary:%lld\n");
  678. if (n == STRNZ)
  679. unnest_msg_and_return(args[0][0] != '\0', "<primary:%lld\n");
  680. if (n == FILTT)
  681. unnest_msg_and_return(isatty(getn(*args)), "<primary: isatty(%s)%lld\n", *args);
  682. unnest_msg_and_return(filstat(*args, n), "<primary: filstat(%s):%lld\n", *args);
  683. }
  684. /*check_operator(args[1]); - already done */
  685. if (last_operator->op_type == BINOP) {
  686. /* args[2] is known to be NULL, isn't it bound to fail? */
  687. unnest_msg_and_return(binop(), "<primary:%lld\n");
  688. }
  689. check_emptiness:
  690. unnest_msg_and_return(args[0][0] != '\0', "<primary:%lld\n");
  691. }
  692. int test_main(int argc, char **argv)
  693. {
  694. int res;
  695. const char *arg0;
  696. // bool negate = 0;
  697. arg0 = bb_basename(argv[0]);
  698. if (arg0[0] == '[') {
  699. --argc;
  700. if (!arg0[1]) { /* "[" ? */
  701. if (NOT_LONE_CHAR(argv[argc], ']')) {
  702. bb_error_msg("missing ]");
  703. return 2;
  704. }
  705. } else { /* assuming "[[" */
  706. if (strcmp(argv[argc], "]]") != 0) {
  707. bb_error_msg("missing ]]");
  708. return 2;
  709. }
  710. }
  711. argv[argc] = NULL;
  712. }
  713. /* We must do DEINIT_S() prior to returning */
  714. INIT_S();
  715. res = setjmp(leaving);
  716. if (res)
  717. goto ret;
  718. /* resetting ngroups is probably unnecessary. it will
  719. * force a new call to getgroups(), which prevents using
  720. * group data fetched during a previous call. but the
  721. * only way the group data could be stale is if there's
  722. * been an intervening call to setgroups(), and this
  723. * isn't likely in the case of a shell. paranoia
  724. * prevails...
  725. */
  726. /*ngroups = 0; - done by INIT_S() */
  727. //argc--;
  728. argv++;
  729. /* Implement special cases from POSIX.2, section 4.62.4 */
  730. if (!argv[0]) { /* "test" */
  731. res = 1;
  732. goto ret;
  733. }
  734. #if 0
  735. // Now it's fixed in the parser and should not be needed
  736. if (LONE_CHAR(argv[0], '!') && argv[1]) {
  737. negate = 1;
  738. //argc--;
  739. argv++;
  740. }
  741. if (!argv[1]) { /* "test [!] arg" */
  742. res = (*argv[0] == '\0');
  743. goto ret;
  744. }
  745. if (argv[2] && !argv[3]) {
  746. check_operator(argv[1]);
  747. if (last_operator->op_type == BINOP) {
  748. /* "test [!] arg1 <binary_op> arg2" */
  749. args = argv;
  750. res = (binop() == 0);
  751. goto ret;
  752. }
  753. }
  754. /* Some complex expression. Undo '!' removal */
  755. if (negate) {
  756. negate = 0;
  757. //argc++;
  758. argv--;
  759. }
  760. #endif
  761. args = argv;
  762. res = !oexpr(check_operator(*args));
  763. if (*args != NULL && *++args != NULL) {
  764. /* TODO: example when this happens? */
  765. bb_error_msg("%s: unknown operand", *args);
  766. res = 2;
  767. }
  768. ret:
  769. DEINIT_S();
  770. // return negate ? !res : res;
  771. return res;
  772. }