expr.c 9.4 KB

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