test.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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 token_types {
  207. UNOP,
  208. BINOP,
  209. BUNOP,
  210. BBINOP,
  211. PAREN
  212. };
  213. struct operator_t {
  214. char op_text[4];
  215. unsigned char op_num, op_type;
  216. };
  217. static const struct operator_t ops[] = {
  218. { "-r", FILRD , UNOP },
  219. { "-w", FILWR , UNOP },
  220. { "-x", FILEX , UNOP },
  221. { "-e", FILEXIST, UNOP },
  222. { "-f", FILREG , UNOP },
  223. { "-d", FILDIR , UNOP },
  224. { "-c", FILCDEV , UNOP },
  225. { "-b", FILBDEV , UNOP },
  226. { "-p", FILFIFO , UNOP },
  227. { "-u", FILSUID , UNOP },
  228. { "-g", FILSGID , UNOP },
  229. { "-k", FILSTCK , UNOP },
  230. { "-s", FILGZ , UNOP },
  231. { "-t", FILTT , UNOP },
  232. { "-z", STREZ , UNOP },
  233. { "-n", STRNZ , UNOP },
  234. { "-h", FILSYM , UNOP }, /* for backwards compat */
  235. { "-O" , FILUID , UNOP },
  236. { "-G" , FILGID , UNOP },
  237. { "-L" , FILSYM , UNOP },
  238. { "-S" , FILSOCK, UNOP },
  239. { "=" , STREQ , BINOP },
  240. { "==" , STREQ , BINOP },
  241. { "!=" , STRNE , BINOP },
  242. { "<" , STRLT , BINOP },
  243. { ">" , STRGT , BINOP },
  244. { "-eq", INTEQ , BINOP },
  245. { "-ne", INTNE , BINOP },
  246. { "-ge", INTGE , BINOP },
  247. { "-gt", INTGT , BINOP },
  248. { "-le", INTLE , BINOP },
  249. { "-lt", INTLT , BINOP },
  250. { "-nt", FILNT , BINOP },
  251. { "-ot", FILOT , BINOP },
  252. { "-ef", FILEQ , BINOP },
  253. { "!" , UNOT , BUNOP },
  254. { "-a" , BAND , BBINOP },
  255. { "-o" , BOR , BBINOP },
  256. { "(" , LPAREN , PAREN },
  257. { ")" , RPAREN , PAREN },
  258. };
  259. #if ENABLE_FEATURE_TEST_64
  260. typedef int64_t number_t;
  261. #else
  262. typedef int number_t;
  263. #endif
  264. /* We try to minimize both static and stack usage. */
  265. struct test_statics {
  266. char **args;
  267. /* set only by check_operator(), either to bogus struct
  268. * or points to matching operator_t struct. Never NULL. */
  269. const struct operator_t *last_operator;
  270. gid_t *group_array;
  271. int ngroups;
  272. jmp_buf leaving;
  273. };
  274. /* See test_ptr_hack.c */
  275. extern struct test_statics *const test_ptr_to_statics;
  276. #define S (*test_ptr_to_statics)
  277. #define args (S.args )
  278. #define last_operator (S.last_operator)
  279. #define group_array (S.group_array )
  280. #define ngroups (S.ngroups )
  281. #define leaving (S.leaving )
  282. #define INIT_S() do { \
  283. (*(struct test_statics**)&test_ptr_to_statics) = xzalloc(sizeof(S)); \
  284. barrier(); \
  285. } while (0)
  286. #define DEINIT_S() do { \
  287. free(test_ptr_to_statics); \
  288. } while (0)
  289. static number_t primary(enum token n);
  290. static void syntax(const char *op, const char *msg) NORETURN;
  291. static void syntax(const char *op, const char *msg)
  292. {
  293. if (op && *op) {
  294. bb_error_msg("%s: %s", op, msg);
  295. } else {
  296. bb_error_msg("%s: %s"+4, msg);
  297. }
  298. longjmp(leaving, 2);
  299. }
  300. /* atoi with error detection */
  301. //XXX: FIXME: duplicate of existing libbb function?
  302. static number_t getn(const char *s)
  303. {
  304. char *p;
  305. #if ENABLE_FEATURE_TEST_64
  306. long long r;
  307. #else
  308. long r;
  309. #endif
  310. errno = 0;
  311. #if ENABLE_FEATURE_TEST_64
  312. r = strtoll(s, &p, 10);
  313. #else
  314. r = strtol(s, &p, 10);
  315. #endif
  316. if (errno != 0)
  317. syntax(s, "out of range");
  318. if (*(skip_whitespace(p)))
  319. syntax(s, "bad number");
  320. return r;
  321. }
  322. /* UNUSED
  323. static int newerf(const char *f1, const char *f2)
  324. {
  325. struct stat b1, b2;
  326. return (stat(f1, &b1) == 0 &&
  327. stat(f2, &b2) == 0 && b1.st_mtime > b2.st_mtime);
  328. }
  329. static int olderf(const char *f1, const char *f2)
  330. {
  331. struct stat b1, b2;
  332. return (stat(f1, &b1) == 0 &&
  333. stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime);
  334. }
  335. static int equalf(const char *f1, const char *f2)
  336. {
  337. struct stat b1, b2;
  338. return (stat(f1, &b1) == 0 &&
  339. stat(f2, &b2) == 0 &&
  340. b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino);
  341. }
  342. */
  343. static enum token check_operator(char *s)
  344. {
  345. static const struct operator_t no_op = {
  346. .op_num = -1,
  347. .op_type = -1
  348. };
  349. const struct operator_t *op;
  350. last_operator = &no_op;
  351. if (s == NULL) {
  352. return EOI;
  353. }
  354. op = ops;
  355. do {
  356. if (strcmp(s, op->op_text) == 0) {
  357. last_operator = op;
  358. return op->op_num;
  359. }
  360. op++;
  361. } while (op < ops + ARRAY_SIZE(ops));
  362. return OPERAND;
  363. }
  364. static int binop(void)
  365. {
  366. const char *opnd1, *opnd2;
  367. const struct operator_t *op;
  368. number_t val1, val2;
  369. opnd1 = *args;
  370. check_operator(*++args);
  371. op = last_operator;
  372. opnd2 = *++args;
  373. if (opnd2 == NULL)
  374. syntax(op->op_text, "argument expected");
  375. if (is_int_op(op->op_num)) {
  376. val1 = getn(opnd1);
  377. val2 = getn(opnd2);
  378. if (op->op_num == INTEQ)
  379. return val1 == val2;
  380. if (op->op_num == INTNE)
  381. return val1 != val2;
  382. if (op->op_num == INTGE)
  383. return val1 >= val2;
  384. if (op->op_num == INTGT)
  385. return val1 > val2;
  386. if (op->op_num == INTLE)
  387. return val1 <= val2;
  388. /*if (op->op_num == INTLT)*/
  389. return val1 < val2;
  390. }
  391. if (is_str_op(op->op_num)) {
  392. val1 = strcmp(opnd1, opnd2);
  393. if (op->op_num == STREQ)
  394. return val1 == 0;
  395. if (op->op_num == STRNE)
  396. return val1 != 0;
  397. if (op->op_num == STRLT)
  398. return val1 < 0;
  399. /*if (op->op_num == STRGT)*/
  400. return val1 > 0;
  401. }
  402. /* We are sure that these three are by now the only binops we didn't check
  403. * yet, so we do not check if the class is correct:
  404. */
  405. /* if (is_file_op(op->op_num)) */
  406. {
  407. struct stat b1, b2;
  408. if (stat(opnd1, &b1) || stat(opnd2, &b2))
  409. return 0; /* false, since at least one stat failed */
  410. if (op->op_num == FILNT)
  411. return b1.st_mtime > b2.st_mtime;
  412. if (op->op_num == FILOT)
  413. return b1.st_mtime < b2.st_mtime;
  414. /*if (op->op_num == FILEQ)*/
  415. return b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino;
  416. }
  417. /*return 1; - NOTREACHED */
  418. }
  419. static void initialize_group_array(void)
  420. {
  421. int n;
  422. /* getgroups may be expensive, try to use it only once */
  423. ngroups = 32;
  424. do {
  425. /* FIXME: ash tries so hard to not die on OOM,
  426. * and we spoil it with just one xrealloc here */
  427. /* We realloc, because test_main can be entered repeatedly by shell.
  428. * Testcase (ash): 'while true; do test -x some_file; done'
  429. * and watch top. (some_file must have owner != you) */
  430. n = ngroups;
  431. group_array = xrealloc(group_array, n * sizeof(gid_t));
  432. ngroups = getgroups(n, group_array);
  433. } while (ngroups > n);
  434. }
  435. /* Return non-zero if GID is one that we have in our groups list. */
  436. //XXX: FIXME: duplicate of existing libbb function?
  437. // see toplevel TODO file:
  438. // possible code duplication ingroup() and is_a_group_member()
  439. static int is_a_group_member(gid_t gid)
  440. {
  441. int i;
  442. /* Short-circuit if possible, maybe saving a call to getgroups(). */
  443. if (gid == getgid() || gid == getegid())
  444. return 1;
  445. if (ngroups == 0)
  446. initialize_group_array();
  447. /* Search through the list looking for GID. */
  448. for (i = 0; i < ngroups; i++)
  449. if (gid == group_array[i])
  450. return 1;
  451. return 0;
  452. }
  453. /* Do the same thing access(2) does, but use the effective uid and gid,
  454. and don't make the mistake of telling root that any file is
  455. executable. */
  456. static int test_eaccess(char *path, int mode)
  457. {
  458. struct stat st;
  459. unsigned int euid = geteuid();
  460. if (stat(path, &st) < 0)
  461. return -1;
  462. if (euid == 0) {
  463. /* Root can read or write any file. */
  464. if (mode != X_OK)
  465. return 0;
  466. /* Root can execute any file that has any one of the execute
  467. bits set. */
  468. if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
  469. return 0;
  470. }
  471. if (st.st_uid == euid) /* owner */
  472. mode <<= 6;
  473. else if (is_a_group_member(st.st_gid))
  474. mode <<= 3;
  475. if (st.st_mode & mode)
  476. return 0;
  477. return -1;
  478. }
  479. static int filstat(char *nm, enum token mode)
  480. {
  481. struct stat s;
  482. unsigned i = i; /* gcc 3.x thinks it can be used uninitialized */
  483. if (mode == FILSYM) {
  484. #ifdef S_IFLNK
  485. if (lstat(nm, &s) == 0) {
  486. i = S_IFLNK;
  487. goto filetype;
  488. }
  489. #endif
  490. return 0;
  491. }
  492. if (stat(nm, &s) != 0)
  493. return 0;
  494. if (mode == FILEXIST)
  495. return 1;
  496. if (is_file_access(mode)) {
  497. if (mode == FILRD)
  498. i = R_OK;
  499. if (mode == FILWR)
  500. i = W_OK;
  501. if (mode == FILEX)
  502. i = X_OK;
  503. return test_eaccess(nm, i) == 0;
  504. }
  505. if (is_file_type(mode)) {
  506. if (mode == FILREG)
  507. i = S_IFREG;
  508. if (mode == FILDIR)
  509. i = S_IFDIR;
  510. if (mode == FILCDEV)
  511. i = S_IFCHR;
  512. if (mode == FILBDEV)
  513. i = S_IFBLK;
  514. if (mode == FILFIFO) {
  515. #ifdef S_IFIFO
  516. i = S_IFIFO;
  517. #else
  518. return 0;
  519. #endif
  520. }
  521. if (mode == FILSOCK) {
  522. #ifdef S_IFSOCK
  523. i = S_IFSOCK;
  524. #else
  525. return 0;
  526. #endif
  527. }
  528. filetype:
  529. return ((s.st_mode & S_IFMT) == i);
  530. }
  531. if (is_file_bit(mode)) {
  532. if (mode == FILSUID)
  533. i = S_ISUID;
  534. if (mode == FILSGID)
  535. i = S_ISGID;
  536. if (mode == FILSTCK)
  537. i = S_ISVTX;
  538. return ((s.st_mode & i) != 0);
  539. }
  540. if (mode == FILGZ)
  541. return s.st_size > 0L;
  542. if (mode == FILUID)
  543. return s.st_uid == geteuid();
  544. if (mode == FILGID)
  545. return s.st_gid == getegid();
  546. return 1; /* NOTREACHED */
  547. }
  548. static number_t nexpr(enum token n)
  549. {
  550. number_t res;
  551. nest_msg(">nexpr(%s)\n", TOKSTR[n]);
  552. if (n == UNOT) {
  553. n = check_operator(*++args);
  554. if (n == EOI) {
  555. /* special case: [ ! ], [ a -a ! ] are valid */
  556. /* IOW, "! ARG" may miss ARG */
  557. unnest_msg("<nexpr:1 (!EOI)\n");
  558. return 1;
  559. }
  560. res = !nexpr(n);
  561. unnest_msg("<nexpr:%lld\n", res);
  562. return res;
  563. }
  564. res = primary(n);
  565. unnest_msg("<nexpr:%lld\n", res);
  566. return res;
  567. }
  568. static number_t aexpr(enum token n)
  569. {
  570. number_t res;
  571. nest_msg(">aexpr(%s)\n", TOKSTR[n]);
  572. res = nexpr(n);
  573. dbg_msg("aexpr: nexpr:%lld, next args:%s\n", res, args[1]);
  574. if (check_operator(*++args) == BAND) {
  575. dbg_msg("aexpr: arg is AND, next args:%s\n", args[1]);
  576. res = aexpr(check_operator(*++args)) && res;
  577. unnest_msg("<aexpr:%lld\n", res);
  578. return res;
  579. }
  580. args--;
  581. unnest_msg("<aexpr:%lld, args:%s\n", res, args[0]);
  582. return res;
  583. }
  584. static number_t oexpr(enum token n)
  585. {
  586. number_t res;
  587. nest_msg(">oexpr(%s)\n", TOKSTR[n]);
  588. res = aexpr(n);
  589. dbg_msg("oexpr: aexpr:%lld, next args:%s\n", res, args[1]);
  590. if (check_operator(*++args) == BOR) {
  591. dbg_msg("oexpr: next arg is OR, next args:%s\n", args[1]);
  592. res = oexpr(check_operator(*++args)) || res;
  593. unnest_msg("<oexpr:%lld\n", res);
  594. return res;
  595. }
  596. args--;
  597. unnest_msg("<oexpr:%lld, args:%s\n", res, args[0]);
  598. return res;
  599. }
  600. static number_t primary(enum token n)
  601. {
  602. #if TEST_DEBUG
  603. number_t res = res; /* for compiler */
  604. #else
  605. number_t res;
  606. #endif
  607. const struct operator_t *args0_op;
  608. nest_msg(">primary(%s)\n", TOKSTR[n]);
  609. if (n == EOI) {
  610. syntax(NULL, "argument expected");
  611. }
  612. if (n == LPAREN) {
  613. res = oexpr(check_operator(*++args));
  614. if (check_operator(*++args) != RPAREN)
  615. syntax(NULL, "closing paren expected");
  616. unnest_msg("<primary:%lld\n", res);
  617. return res;
  618. }
  619. /* coreutils 6.9 checks "is args[1] binop and args[2] exist?" first,
  620. * do the same */
  621. args0_op = last_operator;
  622. /* last_operator = operator at args[1] */
  623. if (check_operator(args[1]) != EOI) { /* if args[1] != NULL */
  624. if (args[2]) {
  625. // coreutils also does this:
  626. // if (args[3] && args[0]="-l" && args[2] is BINOP)
  627. // return binop(1 /* prepended by -l */);
  628. if (last_operator->op_type == BINOP)
  629. unnest_msg_and_return(binop(), "<primary: binop:%lld\n");
  630. }
  631. }
  632. /* check "is args[0] unop?" second */
  633. if (args0_op->op_type == UNOP) {
  634. /* unary expression */
  635. if (args[1] == NULL)
  636. // syntax(args0_op->op_text, "argument expected");
  637. goto check_emptiness;
  638. args++;
  639. if (n == STREZ)
  640. unnest_msg_and_return(args[0][0] == '\0', "<primary:%lld\n");
  641. if (n == STRNZ)
  642. unnest_msg_and_return(args[0][0] != '\0', "<primary:%lld\n");
  643. if (n == FILTT)
  644. unnest_msg_and_return(isatty(getn(*args)), "<primary: isatty(%s)%lld\n", *args);
  645. unnest_msg_and_return(filstat(*args, n), "<primary: filstat(%s):%lld\n", *args);
  646. }
  647. /*check_operator(args[1]); - already done */
  648. if (last_operator->op_type == BINOP) {
  649. /* args[2] is known to be NULL, isn't it bound to fail? */
  650. unnest_msg_and_return(binop(), "<primary:%lld\n");
  651. }
  652. check_emptiness:
  653. unnest_msg_and_return(args[0][0] != '\0', "<primary:%lld\n");
  654. }
  655. int test_main(int argc, char **argv)
  656. {
  657. int res;
  658. const char *arg0;
  659. // bool negate = 0;
  660. arg0 = bb_basename(argv[0]);
  661. if (arg0[0] == '[') {
  662. --argc;
  663. if (!arg0[1]) { /* "[" ? */
  664. if (NOT_LONE_CHAR(argv[argc], ']')) {
  665. bb_error_msg("missing ]");
  666. return 2;
  667. }
  668. } else { /* assuming "[[" */
  669. if (strcmp(argv[argc], "]]") != 0) {
  670. bb_error_msg("missing ]]");
  671. return 2;
  672. }
  673. }
  674. argv[argc] = NULL;
  675. }
  676. /* We must do DEINIT_S() prior to returning */
  677. INIT_S();
  678. res = setjmp(leaving);
  679. if (res)
  680. goto ret;
  681. /* resetting ngroups is probably unnecessary. it will
  682. * force a new call to getgroups(), which prevents using
  683. * group data fetched during a previous call. but the
  684. * only way the group data could be stale is if there's
  685. * been an intervening call to setgroups(), and this
  686. * isn't likely in the case of a shell. paranoia
  687. * prevails...
  688. */
  689. /*ngroups = 0; - done by INIT_S() */
  690. //argc--;
  691. argv++;
  692. /* Implement special cases from POSIX.2, section 4.62.4 */
  693. if (!argv[0]) { /* "test" */
  694. res = 1;
  695. goto ret;
  696. }
  697. #if 0
  698. // Now it's fixed in the parser and should not be needed
  699. if (LONE_CHAR(argv[0], '!') && argv[1]) {
  700. negate = 1;
  701. //argc--;
  702. argv++;
  703. }
  704. if (!argv[1]) { /* "test [!] arg" */
  705. res = (*argv[0] == '\0');
  706. goto ret;
  707. }
  708. if (argv[2] && !argv[3]) {
  709. check_operator(argv[1]);
  710. if (last_operator->op_type == BINOP) {
  711. /* "test [!] arg1 <binary_op> arg2" */
  712. args = argv;
  713. res = (binop() == 0);
  714. goto ret;
  715. }
  716. }
  717. /* Some complex expression. Undo '!' removal */
  718. if (negate) {
  719. negate = 0;
  720. //argc++;
  721. argv--;
  722. }
  723. #endif
  724. args = argv;
  725. res = !oexpr(check_operator(*args));
  726. if (*args != NULL && *++args != NULL) {
  727. /* TODO: example when this happens? */
  728. bb_error_msg("%s: unknown operand", *args);
  729. res = 2;
  730. }
  731. ret:
  732. DEINIT_S();
  733. // return negate ? !res : res;
  734. return res;
  735. }