3
0

expr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini expr implementation for busybox
  4. *
  5. * based on GNU expr Mike Parker.
  6. * Copyright (C) 86, 1991-1997, 1999 Free Software Foundation, Inc.
  7. *
  8. * Busybox modifications
  9. * Copyright (c) 2000 Edward Betts <edward@debian.org>.
  10. * Copyright (C) 2003-2005 Vladimir Oleynik <dzo@simtreas.ru>
  11. * - reduced 464 bytes.
  12. * - 64 math support
  13. *
  14. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  15. */
  16. /* This program evaluates expressions. Each token (operator, operand,
  17. * parenthesis) of the expression must be a separate argument. The
  18. * parser used is a reasonably general one, though any incarnation of
  19. * it is language-specific. It is especially nice for expressions.
  20. *
  21. * No parse tree is needed; a new node is evaluated immediately.
  22. * One function can handle multiple operators all of equal precedence,
  23. * provided they all associate ((x op x) op x).
  24. */
  25. //config:config EXPR
  26. //config: bool "expr (6.1 kb)"
  27. //config: default y
  28. //config: help
  29. //config: expr is used to calculate numbers and print the result
  30. //config: to standard output.
  31. //config:
  32. //config:config EXPR_MATH_SUPPORT_64
  33. //config: bool "Extend Posix numbers support to 64 bit"
  34. //config: default y
  35. //config: depends on EXPR
  36. //config: help
  37. //config: Enable 64-bit math support in the expr applet. This will make
  38. //config: the applet slightly larger, but will allow computation with very
  39. //config: large numbers.
  40. //applet:IF_EXPR(APPLET_NOEXEC(expr, expr, BB_DIR_USR_BIN, BB_SUID_DROP, expr))
  41. //kbuild:lib-$(CONFIG_EXPR) += expr.o
  42. //usage:#define expr_trivial_usage
  43. //usage: "EXPRESSION"
  44. //usage:#define expr_full_usage "\n\n"
  45. //usage: "Print the value of EXPRESSION to stdout\n"
  46. //usage: "\n"
  47. //usage: "EXPRESSION may be:\n"
  48. //usage: " ARG1 | ARG2 ARG1 if it is neither null nor 0, otherwise ARG2\n"
  49. //usage: " ARG1 & ARG2 ARG1 if neither argument is null or 0, otherwise 0\n"
  50. //usage: " ARG1 < ARG2 1 if ARG1 is less than ARG2, else 0. Similarly:\n"
  51. //usage: " ARG1 <= ARG2\n"
  52. //usage: " ARG1 = ARG2\n"
  53. //usage: " ARG1 != ARG2\n"
  54. //usage: " ARG1 >= ARG2\n"
  55. //usage: " ARG1 > ARG2\n"
  56. //usage: " ARG1 + ARG2 Sum of ARG1 and ARG2. Similarly:\n"
  57. //usage: " ARG1 - ARG2\n"
  58. //usage: " ARG1 * ARG2\n"
  59. //usage: " ARG1 / ARG2\n"
  60. //usage: " ARG1 % ARG2\n"
  61. //usage: " STRING : REGEXP Anchored pattern match of REGEXP in STRING\n"
  62. //usage: " match STRING REGEXP Same as STRING : REGEXP\n"
  63. //usage: " substr STRING POS LENGTH Substring of STRING, POS counted from 1\n"
  64. //usage: " index STRING CHARS Index in STRING where any CHARS is found, or 0\n"
  65. //usage: " length STRING Length of STRING\n"
  66. //usage: " quote TOKEN Interpret TOKEN as a string, even if\n"
  67. //usage: " it is a keyword like 'match' or an\n"
  68. //usage: " operator like '/'\n"
  69. //usage: " (EXPRESSION) Value of EXPRESSION\n"
  70. //usage: "\n"
  71. //usage: "Beware that many operators need to be escaped or quoted for shells.\n"
  72. //usage: "Comparisons are arithmetic if both ARGs are numbers, else\n"
  73. //usage: "lexicographical. Pattern matches return the string matched between\n"
  74. //usage: "\\( and \\) or null; if \\( and \\) are not used, they return the number\n"
  75. //usage: "of characters matched or 0."
  76. #include "libbb.h"
  77. #include "common_bufsiz.h"
  78. #include "xregex.h"
  79. #if ENABLE_EXPR_MATH_SUPPORT_64
  80. typedef int64_t arith_t;
  81. #define PF_REZ "ll"
  82. #define PF_REZ_TYPE (long long)
  83. #define STRTOL(s, e, b) strtoll(s, e, b)
  84. #else
  85. typedef long arith_t;
  86. #define PF_REZ "l"
  87. #define PF_REZ_TYPE (long)
  88. #define STRTOL(s, e, b) strtol(s, e, b)
  89. #endif
  90. /* TODO: use bb_strtol[l]? It's easier to check for errors... */
  91. /* The kinds of value we can have. */
  92. enum {
  93. INTEGER,
  94. STRING
  95. };
  96. /* A value is.... */
  97. struct valinfo {
  98. smallint type; /* Which kind. */
  99. union { /* The value itself. */
  100. arith_t i;
  101. char *s;
  102. } u;
  103. };
  104. typedef struct valinfo VALUE;
  105. /* The arguments given to the program, minus the program name. */
  106. struct globals {
  107. char **args;
  108. } FIX_ALIASING;
  109. #define G (*(struct globals*)bb_common_bufsiz1)
  110. #define INIT_G() do { \
  111. setup_common_bufsiz(); \
  112. /* NB: noexec applet - globals not zeroed */ \
  113. } while (0)
  114. /* forward declarations */
  115. static VALUE *eval(void);
  116. /* Return a VALUE for I. */
  117. static VALUE *int_value(arith_t i)
  118. {
  119. VALUE *v;
  120. v = xzalloc(sizeof(VALUE));
  121. if (INTEGER) /* otherwise xzalloc did it already */
  122. v->type = INTEGER;
  123. v->u.i = i;
  124. return v;
  125. }
  126. /* Return a VALUE for S. */
  127. static VALUE *str_value(const char *s)
  128. {
  129. VALUE *v;
  130. v = xzalloc(sizeof(VALUE));
  131. if (STRING) /* otherwise xzalloc did it already */
  132. v->type = STRING;
  133. v->u.s = xstrdup(s);
  134. return v;
  135. }
  136. /* Free VALUE V, including structure components. */
  137. static void freev(VALUE *v)
  138. {
  139. if (v->type == STRING)
  140. free(v->u.s);
  141. free(v);
  142. }
  143. /* Return nonzero if V is a null-string or zero-number. */
  144. static int null(VALUE *v)
  145. {
  146. if (v->type == INTEGER)
  147. return v->u.i == 0;
  148. /* STRING: */
  149. return v->u.s[0] == '\0' || LONE_CHAR(v->u.s, '0');
  150. }
  151. /* Coerce V to a STRING value (can't fail). */
  152. static void tostring(VALUE *v)
  153. {
  154. if (v->type == INTEGER) {
  155. v->u.s = xasprintf("%" PF_REZ "d", PF_REZ_TYPE v->u.i);
  156. v->type = STRING;
  157. }
  158. }
  159. /* Coerce V to an INTEGER value. Return 1 on success, 0 on failure. */
  160. static bool toarith(VALUE *v)
  161. {
  162. if (v->type == STRING) {
  163. arith_t i;
  164. char *e;
  165. /* Don't interpret the empty string as an integer. */
  166. /* Currently does not worry about overflow or int/long differences. */
  167. i = STRTOL(v->u.s, &e, 10);
  168. if ((v->u.s == e) || *e)
  169. return 0;
  170. free(v->u.s);
  171. v->u.i = i;
  172. v->type = INTEGER;
  173. }
  174. return 1;
  175. }
  176. /* Return str[0]+str[1] if the next token matches STR exactly.
  177. STR must not be NULL. */
  178. static int nextarg(const char *str)
  179. {
  180. if (*G.args == NULL || strcmp(*G.args, str) != 0)
  181. return 0;
  182. return (unsigned char)str[0] + (unsigned char)str[1];
  183. }
  184. /* The comparison operator handling functions. */
  185. static int cmp_common(VALUE *l, VALUE *r, int op)
  186. {
  187. arith_t ll, rr;
  188. ll = l->u.i;
  189. rr = r->u.i;
  190. if (l->type == STRING || r->type == STRING) {
  191. tostring(l);
  192. tostring(r);
  193. ll = strcmp(l->u.s, r->u.s);
  194. rr = 0;
  195. }
  196. /* calculating ll - rr and checking the result is prone to overflows.
  197. * We'll do it differently: */
  198. if (op == '<')
  199. return ll < rr;
  200. if (op == ('<' + '='))
  201. return ll <= rr;
  202. if (op == '=' || (op == '=' + '='))
  203. return ll == rr;
  204. if (op == '!' + '=')
  205. return ll != rr;
  206. if (op == '>')
  207. return ll > rr;
  208. /* >= */
  209. return ll >= rr;
  210. }
  211. /* The arithmetic operator handling functions. */
  212. static arith_t arithmetic_common(VALUE *l, VALUE *r, int op)
  213. {
  214. arith_t li, ri;
  215. if (!toarith(l) || !toarith(r))
  216. bb_error_msg_and_die("non-numeric argument");
  217. li = l->u.i;
  218. ri = r->u.i;
  219. if (op == '+')
  220. return li + ri;
  221. if (op == '-')
  222. return li - ri;
  223. if (op == '*')
  224. return li * ri;
  225. if (ri == 0)
  226. bb_error_msg_and_die("division by zero");
  227. if (op == '/')
  228. return li / ri;
  229. return li % ri;
  230. }
  231. /* Do the : operator.
  232. SV is the VALUE for the lhs (the string),
  233. PV is the VALUE for the rhs (the pattern). */
  234. static VALUE *docolon(VALUE *sv, VALUE *pv)
  235. {
  236. enum { NMATCH = 2 };
  237. VALUE *v;
  238. regex_t re_buffer;
  239. regmatch_t re_regs[NMATCH];
  240. tostring(sv);
  241. tostring(pv);
  242. if (pv->u.s[0] == '^') {
  243. bb_error_msg(
  244. "warning: '%s': using '^' as the first character\n"
  245. "of a basic regular expression is not portable; it is ignored", pv->u.s);
  246. }
  247. memset(&re_buffer, 0, sizeof(re_buffer));
  248. memset(re_regs, 0, sizeof(re_regs));
  249. xregcomp(&re_buffer, pv->u.s, 0);
  250. /* expr uses an anchored pattern match, so check that there was a
  251. * match and that the match starts at offset 0. */
  252. if (regexec(&re_buffer, sv->u.s, NMATCH, re_regs, 0) != REG_NOMATCH
  253. && re_regs[0].rm_so == 0
  254. ) {
  255. /* Were \(...\) used? */
  256. if (re_buffer.re_nsub > 0 && re_regs[1].rm_so >= 0) {
  257. sv->u.s[re_regs[1].rm_eo] = '\0';
  258. v = str_value(sv->u.s + re_regs[1].rm_so);
  259. } else {
  260. v = int_value(re_regs[0].rm_eo);
  261. }
  262. } else {
  263. /* Match failed -- return the right kind of null. */
  264. if (re_buffer.re_nsub > 0)
  265. v = str_value("");
  266. else
  267. v = int_value(0);
  268. }
  269. regfree(&re_buffer);
  270. return v;
  271. }
  272. /* Handle bare operands and ( expr ) syntax. */
  273. static VALUE *eval7(void)
  274. {
  275. VALUE *v;
  276. if (!*G.args)
  277. bb_error_msg_and_die("syntax error");
  278. if (nextarg("(")) {
  279. G.args++;
  280. v = eval();
  281. if (!nextarg(")"))
  282. bb_error_msg_and_die("syntax error");
  283. G.args++;
  284. return v;
  285. }
  286. if (nextarg(")"))
  287. bb_error_msg_and_die("syntax error");
  288. return str_value(*G.args++);
  289. }
  290. /* Handle match, substr, index, length, and quote keywords. */
  291. static VALUE *eval6(void)
  292. {
  293. static const char keywords[] ALIGN1 =
  294. "quote\0""length\0""match\0""index\0""substr\0";
  295. VALUE *r, *i1, *i2;
  296. VALUE *l = l; /* silence gcc */
  297. VALUE *v = v; /* silence gcc */
  298. int key = *G.args ? index_in_strings(keywords, *G.args) + 1 : 0;
  299. if (key == 0) /* not a keyword */
  300. return eval7();
  301. G.args++; /* We have a valid token, so get the next argument. */
  302. if (key == 1) { /* quote */
  303. if (!*G.args)
  304. bb_error_msg_and_die("syntax error");
  305. return str_value(*G.args++);
  306. }
  307. if (key == 2) { /* length */
  308. r = eval6();
  309. tostring(r);
  310. v = int_value(strlen(r->u.s));
  311. freev(r);
  312. } else
  313. l = eval6();
  314. if (key == 3) { /* match */
  315. r = eval6();
  316. v = docolon(l, r);
  317. freev(l);
  318. freev(r);
  319. }
  320. if (key == 4) { /* index */
  321. r = eval6();
  322. tostring(l);
  323. tostring(r);
  324. v = int_value(strcspn(l->u.s, r->u.s) + 1);
  325. if (v->u.i == (arith_t) strlen(l->u.s) + 1)
  326. v->u.i = 0;
  327. freev(l);
  328. freev(r);
  329. }
  330. if (key == 5) { /* substr */
  331. i1 = eval6();
  332. i2 = eval6();
  333. tostring(l);
  334. if (!toarith(i1) || !toarith(i2)
  335. || i1->u.i > (arith_t) strlen(l->u.s)
  336. || i1->u.i <= 0 || i2->u.i <= 0)
  337. v = str_value("");
  338. else {
  339. v = xmalloc(sizeof(VALUE));
  340. v->type = STRING;
  341. v->u.s = xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
  342. }
  343. freev(l);
  344. freev(i1);
  345. freev(i2);
  346. }
  347. return v;
  348. }
  349. /* Handle : operator (pattern matching).
  350. Calls docolon to do the real work. */
  351. static VALUE *eval5(void)
  352. {
  353. VALUE *l, *r, *v;
  354. l = eval6();
  355. while (nextarg(":")) {
  356. G.args++;
  357. r = eval6();
  358. v = docolon(l, r);
  359. freev(l);
  360. freev(r);
  361. l = v;
  362. }
  363. return l;
  364. }
  365. /* Handle *, /, % operators. */
  366. static VALUE *eval4(void)
  367. {
  368. VALUE *l, *r;
  369. int op;
  370. arith_t val;
  371. l = eval5();
  372. while (1) {
  373. op = nextarg("*");
  374. if (!op) { op = nextarg("/");
  375. if (!op) { op = nextarg("%");
  376. if (!op) return l;
  377. }}
  378. G.args++;
  379. r = eval5();
  380. val = arithmetic_common(l, r, op);
  381. freev(l);
  382. freev(r);
  383. l = int_value(val);
  384. }
  385. }
  386. /* Handle +, - operators. */
  387. static VALUE *eval3(void)
  388. {
  389. VALUE *l, *r;
  390. int op;
  391. arith_t val;
  392. l = eval4();
  393. while (1) {
  394. op = nextarg("+");
  395. if (!op) {
  396. op = nextarg("-");
  397. if (!op) return l;
  398. }
  399. G.args++;
  400. r = eval4();
  401. val = arithmetic_common(l, r, op);
  402. freev(l);
  403. freev(r);
  404. l = int_value(val);
  405. }
  406. }
  407. /* Handle comparisons. */
  408. static VALUE *eval2(void)
  409. {
  410. VALUE *l, *r;
  411. int op;
  412. arith_t val;
  413. l = eval3();
  414. while (1) {
  415. op = nextarg("<");
  416. if (!op) { op = nextarg("<=");
  417. if (!op) { op = nextarg("=");
  418. if (!op) { op = nextarg("==");
  419. if (!op) { op = nextarg("!=");
  420. if (!op) { op = nextarg(">=");
  421. if (!op) { op = nextarg(">");
  422. if (!op) return l;
  423. }}}}}}
  424. G.args++;
  425. r = eval3();
  426. toarith(l);
  427. toarith(r);
  428. val = cmp_common(l, r, op);
  429. freev(l);
  430. freev(r);
  431. l = int_value(val);
  432. }
  433. }
  434. /* Handle &. */
  435. static VALUE *eval1(void)
  436. {
  437. VALUE *l, *r;
  438. l = eval2();
  439. while (nextarg("&")) {
  440. G.args++;
  441. r = eval2();
  442. if (null(l) || null(r)) {
  443. freev(l);
  444. freev(r);
  445. l = int_value(0);
  446. } else
  447. freev(r);
  448. }
  449. return l;
  450. }
  451. /* Handle |. */
  452. static VALUE *eval(void)
  453. {
  454. VALUE *l, *r;
  455. l = eval1();
  456. while (nextarg("|")) {
  457. G.args++;
  458. r = eval1();
  459. if (null(l)) {
  460. freev(l);
  461. l = r;
  462. } else
  463. freev(r);
  464. }
  465. return l;
  466. }
  467. int expr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  468. int expr_main(int argc UNUSED_PARAM, char **argv)
  469. {
  470. VALUE *v;
  471. INIT_G();
  472. xfunc_error_retval = 2; /* coreutils compat */
  473. G.args = argv + 1;
  474. if (*G.args == NULL) {
  475. bb_error_msg_and_die("too few arguments");
  476. }
  477. v = eval();
  478. if (*G.args)
  479. bb_error_msg_and_die("syntax error");
  480. if (v->type == INTEGER)
  481. printf("%" PF_REZ "d\n", PF_REZ_TYPE v->u.i);
  482. else
  483. puts(v->u.s);
  484. fflush_stdout_and_exit(null(v));
  485. }