expr.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 tarball for details.
  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. /* no getopt needed */
  25. #include "libbb.h"
  26. #include "xregex.h"
  27. #if ENABLE_EXPR_MATH_SUPPORT_64
  28. typedef int64_t arith_t;
  29. #define PF_REZ "ll"
  30. #define PF_REZ_TYPE (long long)
  31. #define STRTOL(s, e, b) strtoll(s, e, b)
  32. #else
  33. typedef long arith_t;
  34. #define PF_REZ "l"
  35. #define PF_REZ_TYPE (long)
  36. #define STRTOL(s, e, b) strtol(s, e, b)
  37. #endif
  38. /* TODO: use bb_strtol[l]? It's easier to check for errors... */
  39. /* The kinds of value we can have. */
  40. enum {
  41. INTEGER,
  42. STRING
  43. };
  44. /* A value is.... */
  45. struct valinfo {
  46. smallint type; /* Which kind. */
  47. union { /* The value itself. */
  48. arith_t i;
  49. char *s;
  50. } u;
  51. };
  52. typedef struct valinfo VALUE;
  53. /* The arguments given to the program, minus the program name. */
  54. struct globals {
  55. char **args;
  56. };
  57. #define G (*(struct globals*)&bb_common_bufsiz1)
  58. /* forward declarations */
  59. static VALUE *eval(void);
  60. /* Return a VALUE for I. */
  61. static VALUE *int_value(arith_t i)
  62. {
  63. VALUE *v;
  64. v = xzalloc(sizeof(VALUE));
  65. if (INTEGER) /* otherwise xzaaloc did it already */
  66. v->type = INTEGER;
  67. v->u.i = i;
  68. return v;
  69. }
  70. /* Return a VALUE for S. */
  71. static VALUE *str_value(const char *s)
  72. {
  73. VALUE *v;
  74. v = xzalloc(sizeof(VALUE));
  75. if (STRING) /* otherwise xzaaloc did it already */
  76. v->type = STRING;
  77. v->u.s = xstrdup(s);
  78. return v;
  79. }
  80. /* Free VALUE V, including structure components. */
  81. static void freev(VALUE *v)
  82. {
  83. if (v->type == STRING)
  84. free(v->u.s);
  85. free(v);
  86. }
  87. /* Return nonzero if V is a null-string or zero-number. */
  88. static int null(VALUE *v)
  89. {
  90. if (v->type == INTEGER)
  91. return v->u.i == 0;
  92. /* STRING: */
  93. return v->u.s[0] == '\0' || LONE_CHAR(v->u.s, '0');
  94. }
  95. /* Coerce V to a STRING value (can't fail). */
  96. static void tostring(VALUE *v)
  97. {
  98. if (v->type == INTEGER) {
  99. v->u.s = xasprintf("%" PF_REZ "d", PF_REZ_TYPE v->u.i);
  100. v->type = STRING;
  101. }
  102. }
  103. /* Coerce V to an INTEGER value. Return 1 on success, 0 on failure. */
  104. static bool toarith(VALUE *v)
  105. {
  106. if (v->type == STRING) {
  107. arith_t i;
  108. char *e;
  109. /* Don't interpret the empty string as an integer. */
  110. /* Currently does not worry about overflow or int/long differences. */
  111. i = STRTOL(v->u.s, &e, 10);
  112. if ((v->u.s == e) || *e)
  113. return 0;
  114. free(v->u.s);
  115. v->u.i = i;
  116. v->type = INTEGER;
  117. }
  118. return 1;
  119. }
  120. /* Return str[0]+str[1] if the next token matches STR exactly.
  121. STR must not be NULL. */
  122. static int nextarg(const char *str)
  123. {
  124. if (*G.args == NULL || strcmp(*G.args, str) != 0)
  125. return 0;
  126. return (unsigned char)str[0] + (unsigned char)str[1];
  127. }
  128. /* The comparison operator handling functions. */
  129. static int cmp_common(VALUE *l, VALUE *r, int op)
  130. {
  131. arith_t ll, rr;
  132. ll = l->u.i;
  133. rr = r->u.i;
  134. if (l->type == STRING || r->type == STRING) {
  135. tostring(l);
  136. tostring(r);
  137. ll = strcmp(l->u.s, r->u.s);
  138. rr = 0;
  139. }
  140. /* calculating ll - rr and checking the result is prone to overflows.
  141. * We'll do it differently: */
  142. if (op == '<')
  143. return ll < rr;
  144. if (op == ('<' + '='))
  145. return ll <= rr;
  146. if (op == '=' || (op == '=' + '='))
  147. return ll == rr;
  148. if (op == '!' + '=')
  149. return ll != rr;
  150. if (op == '>')
  151. return ll > rr;
  152. /* >= */
  153. return ll >= rr;
  154. }
  155. /* The arithmetic operator handling functions. */
  156. static arith_t arithmetic_common(VALUE *l, VALUE *r, int op)
  157. {
  158. arith_t li, ri;
  159. if (!toarith(l) || !toarith(r))
  160. bb_error_msg_and_die("non-numeric argument");
  161. li = l->u.i;
  162. ri = r->u.i;
  163. if (op == '+')
  164. return li + ri;
  165. if (op == '-')
  166. return li - ri;
  167. if (op == '*')
  168. return li * ri;
  169. if (ri == 0)
  170. bb_error_msg_and_die("division by zero");
  171. if (op == '/')
  172. return li / ri;
  173. return li % ri;
  174. }
  175. /* Do the : operator.
  176. SV is the VALUE for the lhs (the string),
  177. PV is the VALUE for the rhs (the pattern). */
  178. static VALUE *docolon(VALUE *sv, VALUE *pv)
  179. {
  180. VALUE *v;
  181. regex_t re_buffer;
  182. const int NMATCH = 2;
  183. regmatch_t re_regs[NMATCH];
  184. tostring(sv);
  185. tostring(pv);
  186. if (pv->u.s[0] == '^') {
  187. bb_error_msg(
  188. "warning: '%s': using '^' as the first character\n"
  189. "of a basic regular expression is not portable; it is ignored", pv->u.s);
  190. }
  191. memset(&re_buffer, 0, sizeof(re_buffer));
  192. memset(re_regs, 0, sizeof(re_regs));
  193. xregcomp(&re_buffer, pv->u.s, 0);
  194. /* expr uses an anchored pattern match, so check that there was a
  195. * match and that the match starts at offset 0. */
  196. if (regexec(&re_buffer, sv->u.s, NMATCH, re_regs, 0) != REG_NOMATCH
  197. && re_regs[0].rm_so == 0
  198. ) {
  199. /* Were \(...\) used? */
  200. if (re_buffer.re_nsub > 0 && re_regs[1].rm_so >= 0) {
  201. sv->u.s[re_regs[1].rm_eo] = '\0';
  202. v = str_value(sv->u.s + re_regs[1].rm_so);
  203. } else {
  204. v = int_value(re_regs[0].rm_eo);
  205. }
  206. } else {
  207. /* Match failed -- return the right kind of null. */
  208. if (re_buffer.re_nsub > 0)
  209. v = str_value("");
  210. else
  211. v = int_value(0);
  212. }
  213. regfree(&re_buffer);
  214. return v;
  215. }
  216. /* Handle bare operands and ( expr ) syntax. */
  217. static VALUE *eval7(void)
  218. {
  219. VALUE *v;
  220. if (!*G.args)
  221. bb_error_msg_and_die("syntax error");
  222. if (nextarg("(")) {
  223. G.args++;
  224. v = eval();
  225. if (!nextarg(")"))
  226. bb_error_msg_and_die("syntax error");
  227. G.args++;
  228. return v;
  229. }
  230. if (nextarg(")"))
  231. bb_error_msg_and_die("syntax error");
  232. return str_value(*G.args++);
  233. }
  234. /* Handle match, substr, index, length, and quote keywords. */
  235. static VALUE *eval6(void)
  236. {
  237. static const char keywords[] ALIGN1 =
  238. "quote\0""length\0""match\0""index\0""substr\0";
  239. VALUE *r, *i1, *i2;
  240. VALUE *l = l; /* silence gcc */
  241. VALUE *v = v; /* silence gcc */
  242. int key = *G.args ? index_in_strings(keywords, *G.args) + 1 : 0;
  243. if (key == 0) /* not a keyword */
  244. return eval7();
  245. G.args++; /* We have a valid token, so get the next argument. */
  246. if (key == 1) { /* quote */
  247. if (!*G.args)
  248. bb_error_msg_and_die("syntax error");
  249. return str_value(*G.args++);
  250. }
  251. if (key == 2) { /* length */
  252. r = eval6();
  253. tostring(r);
  254. v = int_value(strlen(r->u.s));
  255. freev(r);
  256. } else
  257. l = eval6();
  258. if (key == 3) { /* match */
  259. r = eval6();
  260. v = docolon(l, r);
  261. freev(l);
  262. freev(r);
  263. }
  264. if (key == 4) { /* index */
  265. r = eval6();
  266. tostring(l);
  267. tostring(r);
  268. v = int_value(strcspn(l->u.s, r->u.s) + 1);
  269. if (v->u.i == (arith_t) strlen(l->u.s) + 1)
  270. v->u.i = 0;
  271. freev(l);
  272. freev(r);
  273. }
  274. if (key == 5) { /* substr */
  275. i1 = eval6();
  276. i2 = eval6();
  277. tostring(l);
  278. if (!toarith(i1) || !toarith(i2)
  279. || i1->u.i > (arith_t) strlen(l->u.s)
  280. || i1->u.i <= 0 || i2->u.i <= 0)
  281. v = str_value("");
  282. else {
  283. v = xmalloc(sizeof(VALUE));
  284. v->type = STRING;
  285. v->u.s = xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
  286. }
  287. freev(l);
  288. freev(i1);
  289. freev(i2);
  290. }
  291. return v;
  292. }
  293. /* Handle : operator (pattern matching).
  294. Calls docolon to do the real work. */
  295. static VALUE *eval5(void)
  296. {
  297. VALUE *l, *r, *v;
  298. l = eval6();
  299. while (nextarg(":")) {
  300. G.args++;
  301. r = eval6();
  302. v = docolon(l, r);
  303. freev(l);
  304. freev(r);
  305. l = v;
  306. }
  307. return l;
  308. }
  309. /* Handle *, /, % operators. */
  310. static VALUE *eval4(void)
  311. {
  312. VALUE *l, *r;
  313. int op;
  314. arith_t val;
  315. l = eval5();
  316. while (1) {
  317. op = nextarg("*");
  318. if (!op) { op = nextarg("/");
  319. if (!op) { op = nextarg("%");
  320. if (!op) return l;
  321. }}
  322. G.args++;
  323. r = eval5();
  324. val = arithmetic_common(l, r, op);
  325. freev(l);
  326. freev(r);
  327. l = int_value(val);
  328. }
  329. }
  330. /* Handle +, - operators. */
  331. static VALUE *eval3(void)
  332. {
  333. VALUE *l, *r;
  334. int op;
  335. arith_t val;
  336. l = eval4();
  337. while (1) {
  338. op = nextarg("+");
  339. if (!op) {
  340. op = nextarg("-");
  341. if (!op) return l;
  342. }
  343. G.args++;
  344. r = eval4();
  345. val = arithmetic_common(l, r, op);
  346. freev(l);
  347. freev(r);
  348. l = int_value(val);
  349. }
  350. }
  351. /* Handle comparisons. */
  352. static VALUE *eval2(void)
  353. {
  354. VALUE *l, *r;
  355. int op;
  356. arith_t val;
  357. l = eval3();
  358. while (1) {
  359. op = nextarg("<");
  360. if (!op) { op = nextarg("<=");
  361. if (!op) { op = nextarg("=");
  362. if (!op) { op = nextarg("==");
  363. if (!op) { op = nextarg("!=");
  364. if (!op) { op = nextarg(">=");
  365. if (!op) { op = nextarg(">");
  366. if (!op) return l;
  367. }}}}}}
  368. G.args++;
  369. r = eval3();
  370. toarith(l);
  371. toarith(r);
  372. val = cmp_common(l, r, op);
  373. freev(l);
  374. freev(r);
  375. l = int_value(val);
  376. }
  377. }
  378. /* Handle &. */
  379. static VALUE *eval1(void)
  380. {
  381. VALUE *l, *r;
  382. l = eval2();
  383. while (nextarg("&")) {
  384. G.args++;
  385. r = eval2();
  386. if (null(l) || null(r)) {
  387. freev(l);
  388. freev(r);
  389. l = int_value(0);
  390. } else
  391. freev(r);
  392. }
  393. return l;
  394. }
  395. /* Handle |. */
  396. static VALUE *eval(void)
  397. {
  398. VALUE *l, *r;
  399. l = eval1();
  400. while (nextarg("|")) {
  401. G.args++;
  402. r = eval1();
  403. if (null(l)) {
  404. freev(l);
  405. l = r;
  406. } else
  407. freev(r);
  408. }
  409. return l;
  410. }
  411. int expr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  412. int expr_main(int argc UNUSED_PARAM, char **argv)
  413. {
  414. VALUE *v;
  415. xfunc_error_retval = 2; /* coreutils compat */
  416. G.args = argv + 1;
  417. if (*G.args == NULL) {
  418. bb_error_msg_and_die("too few arguments");
  419. }
  420. v = eval();
  421. if (*G.args)
  422. bb_error_msg_and_die("syntax error");
  423. if (v->type == INTEGER)
  424. printf("%" PF_REZ "d\n", PF_REZ_TYPE v->u.i);
  425. else
  426. puts(v->u.s);
  427. fflush_stdout_and_exit(null(v));
  428. }