3
0

expr.c 9.3 KB

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