test.c 20 KB

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