3
0

math.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /*
  2. * Arithmetic code ripped out of ash shell for code sharing.
  3. *
  4. * This code is derived from software contributed to Berkeley by
  5. * Kenneth Almquist.
  6. *
  7. * Original BSD copyright notice is retained at the end of this file.
  8. *
  9. * Copyright (c) 1989, 1991, 1993, 1994
  10. * The Regents of the University of California. All rights reserved.
  11. *
  12. * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au>
  13. * was re-ported from NetBSD and debianized.
  14. *
  15. * rewrite arith.y to micro stack based cryptic algorithm by
  16. * Copyright (c) 2001 Aaron Lehmann <aaronl@vitelus.com>
  17. *
  18. * Modified by Paul Mundt <lethal@linux-sh.org> (c) 2004 to support
  19. * dynamic variables.
  20. *
  21. * Modified by Vladimir Oleynik <dzo@simtreas.ru> (c) 2001-2005 to be
  22. * used in busybox and size optimizations,
  23. * rewrote arith (see notes to this), added locale support,
  24. * rewrote dynamic variables.
  25. *
  26. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  27. */
  28. /* Copyright (c) 2001 Aaron Lehmann <aaronl@vitelus.com>
  29. *
  30. * Permission is hereby granted, free of charge, to any person obtaining
  31. * a copy of this software and associated documentation files (the
  32. * "Software"), to deal in the Software without restriction, including
  33. * without limitation the rights to use, copy, modify, merge, publish,
  34. * distribute, sublicense, and/or sell copies of the Software, and to
  35. * permit persons to whom the Software is furnished to do so, subject to
  36. * the following conditions:
  37. *
  38. * The above copyright notice and this permission notice shall be
  39. * included in all copies or substantial portions of the Software.
  40. *
  41. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  42. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  43. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  44. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  45. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  46. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  47. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  48. */
  49. /* This is my infix parser/evaluator. It is optimized for size, intended
  50. * as a replacement for yacc-based parsers. However, it may well be faster
  51. * than a comparable parser written in yacc. The supported operators are
  52. * listed in #defines below. Parens, order of operations, and error handling
  53. * are supported. This code is thread safe. The exact expression format should
  54. * be that which POSIX specifies for shells.
  55. *
  56. * The code uses a simple two-stack algorithm. See
  57. * http://www.onthenet.com.au/~grahamis/int2008/week02/lect02.html
  58. * for a detailed explanation of the infix-to-postfix algorithm on which
  59. * this is based (this code differs in that it applies operators immediately
  60. * to the stack instead of adding them to a queue to end up with an
  61. * expression).
  62. */
  63. /*
  64. * Aug 24, 2001 Manuel Novoa III
  65. *
  66. * Reduced the generated code size by about 30% (i386) and fixed several bugs.
  67. *
  68. * 1) In arith_apply():
  69. * a) Cached values of *numptr and &(numptr[-1]).
  70. * b) Removed redundant test for zero denominator.
  71. *
  72. * 2) In arith():
  73. * a) Eliminated redundant code for processing operator tokens by moving
  74. * to a table-based implementation. Also folded handling of parens
  75. * into the table.
  76. * b) Combined all 3 loops which called arith_apply to reduce generated
  77. * code size at the cost of speed.
  78. *
  79. * 3) The following expressions were treated as valid by the original code:
  80. * 1() , 0! , 1 ( *3 ) .
  81. * These bugs have been fixed by internally enclosing the expression in
  82. * parens and then checking that all binary ops and right parens are
  83. * preceded by a valid expression (NUM_TOKEN).
  84. *
  85. * Note: It may be desirable to replace Aaron's test for whitespace with
  86. * ctype's isspace() if it is used by another busybox applet or if additional
  87. * whitespace chars should be considered. Look below the "#include"s for a
  88. * precompiler test.
  89. */
  90. /*
  91. * Aug 26, 2001 Manuel Novoa III
  92. *
  93. * Return 0 for null expressions. Pointed out by Vladimir Oleynik.
  94. *
  95. * Merge in Aaron's comments previously posted to the busybox list,
  96. * modified slightly to take account of my changes to the code.
  97. *
  98. */
  99. /*
  100. * (C) 2003 Vladimir Oleynik <dzo@simtreas.ru>
  101. *
  102. * - allow access to variable,
  103. * use recursive value indirection: c="2*2"; a="c"; echo $((a+=2)) produce 6
  104. * - implement assign syntax (VAR=expr, +=, *= etc)
  105. * - implement exponentiation (** operator)
  106. * - implement comma separated - expr, expr
  107. * - implement ++expr --expr expr++ expr--
  108. * - implement expr ? expr : expr (but second expr is always calculated)
  109. * - allow hexadecimal and octal numbers
  110. * - restore lost XOR operator
  111. * - protect $((num num)) as true zero expr (Manuel's error)
  112. * - always use special isspace(), see comment from bash ;-)
  113. */
  114. #include "libbb.h"
  115. #include "math.h"
  116. typedef unsigned char operator;
  117. /* An operator's token id is a bit of a bitfield. The lower 5 bits are the
  118. * precedence, and 3 high bits are an ID unique across operators of that
  119. * precedence. The ID portion is so that multiple operators can have the
  120. * same precedence, ensuring that the leftmost one is evaluated first.
  121. * Consider * and /
  122. */
  123. #define tok_decl(prec,id) (((id)<<5) | (prec))
  124. #define PREC(op) ((op) & 0x1F)
  125. #define TOK_LPAREN tok_decl(0,0)
  126. #define TOK_COMMA tok_decl(1,0)
  127. /* All assignments are right associative and have the same precedence,
  128. * but there are 11 of them, which doesn't fit into 3 bits for unique id.
  129. * Abusing another precedence level:
  130. */
  131. #define TOK_ASSIGN tok_decl(2,0)
  132. #define TOK_AND_ASSIGN tok_decl(2,1)
  133. #define TOK_OR_ASSIGN tok_decl(2,2)
  134. #define TOK_XOR_ASSIGN tok_decl(2,3)
  135. #define TOK_PLUS_ASSIGN tok_decl(2,4)
  136. #define TOK_MINUS_ASSIGN tok_decl(2,5)
  137. #define TOK_LSHIFT_ASSIGN tok_decl(2,6)
  138. #define TOK_RSHIFT_ASSIGN tok_decl(2,7)
  139. #define TOK_MUL_ASSIGN tok_decl(3,0)
  140. #define TOK_DIV_ASSIGN tok_decl(3,1)
  141. #define TOK_REM_ASSIGN tok_decl(3,2)
  142. #define fix_assignment_prec(prec) do { if (prec == 3) prec = 2; } while (0)
  143. /* Ternary conditional operator is right associative too */
  144. #define TOK_CONDITIONAL tok_decl(4,0)
  145. #define TOK_CONDITIONAL_SEP tok_decl(4,1)
  146. #define TOK_OR tok_decl(5,0)
  147. #define TOK_AND tok_decl(6,0)
  148. #define TOK_BOR tok_decl(7,0)
  149. #define TOK_BXOR tok_decl(8,0)
  150. #define TOK_BAND tok_decl(9,0)
  151. #define TOK_EQ tok_decl(10,0)
  152. #define TOK_NE tok_decl(10,1)
  153. #define TOK_LT tok_decl(11,0)
  154. #define TOK_GT tok_decl(11,1)
  155. #define TOK_GE tok_decl(11,2)
  156. #define TOK_LE tok_decl(11,3)
  157. #define TOK_LSHIFT tok_decl(12,0)
  158. #define TOK_RSHIFT tok_decl(12,1)
  159. #define TOK_ADD tok_decl(13,0)
  160. #define TOK_SUB tok_decl(13,1)
  161. #define TOK_MUL tok_decl(14,0)
  162. #define TOK_DIV tok_decl(14,1)
  163. #define TOK_REM tok_decl(14,2)
  164. /* Exponent is right associative */
  165. #define TOK_EXPONENT tok_decl(15,1)
  166. /* Unary operators */
  167. #define UNARYPREC 16
  168. #define TOK_BNOT tok_decl(UNARYPREC,0)
  169. #define TOK_NOT tok_decl(UNARYPREC,1)
  170. #define TOK_UMINUS tok_decl(UNARYPREC+1,0)
  171. #define TOK_UPLUS tok_decl(UNARYPREC+1,1)
  172. #define PREC_PRE (UNARYPREC+2)
  173. #define TOK_PRE_INC tok_decl(PREC_PRE, 0)
  174. #define TOK_PRE_DEC tok_decl(PREC_PRE, 1)
  175. #define PREC_POST (UNARYPREC+3)
  176. #define TOK_POST_INC tok_decl(PREC_POST, 0)
  177. #define TOK_POST_DEC tok_decl(PREC_POST, 1)
  178. #define SPEC_PREC (UNARYPREC+4)
  179. #define TOK_NUM tok_decl(SPEC_PREC, 0)
  180. #define TOK_RPAREN tok_decl(SPEC_PREC, 1)
  181. static int
  182. is_assign_op(operator op)
  183. {
  184. operator prec = PREC(op);
  185. fix_assignment_prec(prec);
  186. return prec == PREC(TOK_ASSIGN)
  187. || prec == PREC_PRE
  188. || prec == PREC_POST;
  189. }
  190. static int
  191. is_right_associative(operator prec)
  192. {
  193. return prec == PREC(TOK_ASSIGN)
  194. || prec == PREC(TOK_EXPONENT)
  195. || prec == PREC(TOK_CONDITIONAL);
  196. }
  197. typedef struct {
  198. arith_t val;
  199. /* We acquire second_val only when "expr1 : expr2" part
  200. * of ternary ?: op is evaluated.
  201. * We treat ?: as two binary ops: (expr ? (expr1 : expr2)).
  202. * ':' produces a new value which has two parts, val and second_val;
  203. * then '?' selects one of them based on its left side.
  204. */
  205. arith_t second_val;
  206. char second_val_present;
  207. /* If NULL then it's just a number, else it's a named variable */
  208. char *var;
  209. } var_or_num_t;
  210. typedef struct remembered_name {
  211. struct remembered_name *next;
  212. const char *var;
  213. } remembered_name;
  214. static arith_t
  215. evaluate_string(arith_state_t *math_state, const char *expr);
  216. static const char*
  217. arith_lookup_val(arith_state_t *math_state, var_or_num_t *t)
  218. {
  219. if (t->var) {
  220. const char *p = math_state->lookupvar(t->var);
  221. if (p) {
  222. remembered_name *cur;
  223. remembered_name cur_save;
  224. /* did we already see this name?
  225. * testcase: a=b; b=a; echo $((a))
  226. */
  227. for (cur = math_state->list_of_recursed_names; cur; cur = cur->next) {
  228. if (strcmp(cur->var, t->var) == 0) {
  229. /* Yes */
  230. return "expression recursion loop detected";
  231. }
  232. }
  233. /* push current var name */
  234. cur = math_state->list_of_recursed_names;
  235. cur_save.var = t->var;
  236. cur_save.next = cur;
  237. math_state->list_of_recursed_names = &cur_save;
  238. /* recursively evaluate p as expression */
  239. t->val = evaluate_string(math_state, p);
  240. /* pop current var name */
  241. math_state->list_of_recursed_names = cur;
  242. return math_state->errmsg;
  243. }
  244. /* treat undefined var as 0 */
  245. t->val = 0;
  246. }
  247. return 0;
  248. }
  249. /* "Applying" a token means performing it on the top elements on the integer
  250. * stack. For an unary operator it will only change the top element, but a
  251. * binary operator will pop two arguments and push the result */
  252. static NOINLINE const char*
  253. arith_apply(arith_state_t *math_state, operator op, var_or_num_t *numstack, var_or_num_t **numstackptr)
  254. {
  255. #define NUMPTR (*numstackptr)
  256. var_or_num_t *top_of_stack;
  257. arith_t rez;
  258. const char *err;
  259. /* There is no operator that can work without arguments */
  260. if (NUMPTR == numstack)
  261. goto err;
  262. top_of_stack = NUMPTR - 1;
  263. /* Resolve name to value, if needed */
  264. err = arith_lookup_val(math_state, top_of_stack);
  265. if (err)
  266. return err;
  267. rez = top_of_stack->val;
  268. if (op == TOK_UMINUS)
  269. rez = -rez;
  270. else if (op == TOK_NOT)
  271. rez = !rez;
  272. else if (op == TOK_BNOT)
  273. rez = ~rez;
  274. else if (op == TOK_POST_INC || op == TOK_PRE_INC)
  275. rez++;
  276. else if (op == TOK_POST_DEC || op == TOK_PRE_DEC)
  277. rez--;
  278. else if (op != TOK_UPLUS) {
  279. /* Binary operators */
  280. arith_t right_side_val;
  281. char bad_second_val;
  282. /* Binary operators need two arguments */
  283. if (top_of_stack == numstack)
  284. goto err;
  285. /* ...and they pop one */
  286. NUMPTR = top_of_stack; /* this decrements NUMPTR */
  287. bad_second_val = top_of_stack->second_val_present;
  288. if (op == TOK_CONDITIONAL) { /* ? operation */
  289. /* Make next if (...) protect against
  290. * $((expr1 ? expr2)) - that is, missing ": expr" */
  291. bad_second_val = !bad_second_val;
  292. }
  293. if (bad_second_val) {
  294. /* Protect against $((expr <not_?_op> expr1 : expr2)) */
  295. return "malformed ?: operator";
  296. }
  297. top_of_stack--; /* now points to left side */
  298. if (op != TOK_ASSIGN) {
  299. /* Resolve left side value (unless the op is '=') */
  300. err = arith_lookup_val(math_state, top_of_stack);
  301. if (err)
  302. return err;
  303. }
  304. right_side_val = rez;
  305. rez = top_of_stack->val;
  306. if (op == TOK_CONDITIONAL) /* ? operation */
  307. rez = (rez ? right_side_val : top_of_stack[1].second_val);
  308. else if (op == TOK_CONDITIONAL_SEP) { /* : operation */
  309. if (top_of_stack == numstack) {
  310. /* Protect against $((expr : expr)) */
  311. return "malformed ?: operator";
  312. }
  313. top_of_stack->second_val_present = op;
  314. top_of_stack->second_val = right_side_val;
  315. }
  316. else if (op == TOK_BOR || op == TOK_OR_ASSIGN)
  317. rez |= right_side_val;
  318. else if (op == TOK_OR)
  319. rez = right_side_val || rez;
  320. else if (op == TOK_BAND || op == TOK_AND_ASSIGN)
  321. rez &= right_side_val;
  322. else if (op == TOK_BXOR || op == TOK_XOR_ASSIGN)
  323. rez ^= right_side_val;
  324. else if (op == TOK_AND)
  325. rez = rez && right_side_val;
  326. else if (op == TOK_EQ)
  327. rez = (rez == right_side_val);
  328. else if (op == TOK_NE)
  329. rez = (rez != right_side_val);
  330. else if (op == TOK_GE)
  331. rez = (rez >= right_side_val);
  332. else if (op == TOK_RSHIFT || op == TOK_RSHIFT_ASSIGN)
  333. rez >>= right_side_val;
  334. else if (op == TOK_LSHIFT || op == TOK_LSHIFT_ASSIGN)
  335. rez <<= right_side_val;
  336. else if (op == TOK_GT)
  337. rez = (rez > right_side_val);
  338. else if (op == TOK_LT)
  339. rez = (rez < right_side_val);
  340. else if (op == TOK_LE)
  341. rez = (rez <= right_side_val);
  342. else if (op == TOK_MUL || op == TOK_MUL_ASSIGN)
  343. rez *= right_side_val;
  344. else if (op == TOK_ADD || op == TOK_PLUS_ASSIGN)
  345. rez += right_side_val;
  346. else if (op == TOK_SUB || op == TOK_MINUS_ASSIGN)
  347. rez -= right_side_val;
  348. else if (op == TOK_ASSIGN || op == TOK_COMMA)
  349. rez = right_side_val;
  350. else if (op == TOK_EXPONENT) {
  351. arith_t c;
  352. if (right_side_val < 0)
  353. return "exponent less than 0";
  354. c = 1;
  355. while (--right_side_val >= 0)
  356. c *= rez;
  357. rez = c;
  358. }
  359. else if (right_side_val == 0)
  360. return "divide by zero";
  361. else if (op == TOK_DIV || op == TOK_DIV_ASSIGN
  362. || op == TOK_REM || op == TOK_REM_ASSIGN) {
  363. /*
  364. * bash 4.2.45 x86 64bit: SEGV on 'echo $((2**63 / -1))'
  365. *
  366. * MAX_NEGATIVE_INT / -1 = MAX_POSITIVE_INT+1
  367. * and thus is not representable.
  368. * Some CPUs segfault trying such op.
  369. * Others overflow MAX_POSITIVE_INT+1 to
  370. * MAX_NEGATIVE_INT (0x7fff+1 = 0x8000).
  371. * Make sure to at least not SEGV here:
  372. */
  373. if (right_side_val == -1
  374. && rez << 1 == 0 /* MAX_NEGATIVE_INT or 0 */
  375. ) {
  376. right_side_val = 1;
  377. }
  378. if (op == TOK_DIV || op == TOK_DIV_ASSIGN)
  379. rez /= right_side_val;
  380. else {
  381. rez %= right_side_val;
  382. }
  383. }
  384. }
  385. if (is_assign_op(op)) {
  386. char buf[sizeof(arith_t)*3 + 2];
  387. if (top_of_stack->var == NULL) {
  388. /* Hmm, 1=2 ? */
  389. goto err;
  390. }
  391. /* Save to shell variable */
  392. sprintf(buf, ARITH_FMT, rez);
  393. math_state->setvar(top_of_stack->var, buf);
  394. /* After saving, make previous value for v++ or v-- */
  395. if (op == TOK_POST_INC)
  396. rez--;
  397. if (op == TOK_POST_DEC)
  398. rez++;
  399. }
  400. top_of_stack->val = rez;
  401. /* Erase var name, it is just a number now */
  402. top_of_stack->var = NULL;
  403. return NULL;
  404. err:
  405. return "arithmetic syntax error";
  406. #undef NUMPTR
  407. }
  408. /* longest must be first */
  409. static const char op_tokens[] ALIGN1 = {
  410. '<','<','=',0, TOK_LSHIFT_ASSIGN,
  411. '>','>','=',0, TOK_RSHIFT_ASSIGN,
  412. '<','<', 0, TOK_LSHIFT,
  413. '>','>', 0, TOK_RSHIFT,
  414. '|','|', 0, TOK_OR,
  415. '&','&', 0, TOK_AND,
  416. '!','=', 0, TOK_NE,
  417. '<','=', 0, TOK_LE,
  418. '>','=', 0, TOK_GE,
  419. '=','=', 0, TOK_EQ,
  420. '|','=', 0, TOK_OR_ASSIGN,
  421. '&','=', 0, TOK_AND_ASSIGN,
  422. '*','=', 0, TOK_MUL_ASSIGN,
  423. '/','=', 0, TOK_DIV_ASSIGN,
  424. '%','=', 0, TOK_REM_ASSIGN,
  425. '+','=', 0, TOK_PLUS_ASSIGN,
  426. '-','=', 0, TOK_MINUS_ASSIGN,
  427. '-','-', 0, TOK_POST_DEC,
  428. '^','=', 0, TOK_XOR_ASSIGN,
  429. '+','+', 0, TOK_POST_INC,
  430. '*','*', 0, TOK_EXPONENT,
  431. '!', 0, TOK_NOT,
  432. '<', 0, TOK_LT,
  433. '>', 0, TOK_GT,
  434. '=', 0, TOK_ASSIGN,
  435. '|', 0, TOK_BOR,
  436. '&', 0, TOK_BAND,
  437. '*', 0, TOK_MUL,
  438. '/', 0, TOK_DIV,
  439. '%', 0, TOK_REM,
  440. '+', 0, TOK_ADD,
  441. '-', 0, TOK_SUB,
  442. '^', 0, TOK_BXOR,
  443. /* uniq */
  444. '~', 0, TOK_BNOT,
  445. ',', 0, TOK_COMMA,
  446. '?', 0, TOK_CONDITIONAL,
  447. ':', 0, TOK_CONDITIONAL_SEP,
  448. ')', 0, TOK_RPAREN,
  449. '(', 0, TOK_LPAREN,
  450. 0
  451. };
  452. #define ptr_to_rparen (&op_tokens[sizeof(op_tokens)-7])
  453. #if ENABLE_FEATURE_SH_MATH_BASE
  454. static arith_t strto_arith_t(const char *nptr, char **endptr)
  455. {
  456. unsigned base;
  457. arith_t n;
  458. # if ENABLE_FEATURE_SH_MATH_64
  459. n = strtoull(nptr, endptr, 0);
  460. # else
  461. n = strtoul(nptr, endptr, 0);
  462. # endif
  463. if (**endptr != '#'
  464. || (*nptr < '1' || *nptr > '9')
  465. || (n < 2 || n > 64)
  466. ) {
  467. return n;
  468. }
  469. /* It's "N#nnnn" or "NN#nnnn" syntax, NN can't start with 0,
  470. * NN is in 2..64 range.
  471. */
  472. base = (unsigned)n;
  473. n = 0;
  474. nptr = *endptr + 1;
  475. for (;;) {
  476. unsigned digit = (unsigned)*nptr - '0';
  477. if (digit >= 10 /* not 0..9 */
  478. && digit <= 'z' - '0' /* needed to reject e.g. $((64#~)) */
  479. ) {
  480. /* in bases up to 36, case does not matter for a-z */
  481. digit = (unsigned)(*nptr | 0x20) - ('a' - 10);
  482. if (base > 36 && *nptr <= '_') {
  483. /* otherwise, A-Z,@,_ are 36-61,62,63 */
  484. if (*nptr == '_')
  485. digit = 63;
  486. else if (*nptr == '@')
  487. digit = 62;
  488. else if (digit < 36) /* A-Z */
  489. digit += 36 - 10;
  490. else
  491. break; /* error: one of [\]^ */
  492. }
  493. //bb_error_msg("ch:'%c'%d digit:%u", *nptr, *nptr, digit);
  494. //if (digit < 10) - example where we need this?
  495. // break;
  496. }
  497. if (digit >= base)
  498. break;
  499. /* bash does not check for overflows */
  500. n = n * base + digit;
  501. nptr++;
  502. }
  503. /* Note: we do not set errno on bad chars, we just set a pointer
  504. * to the first invalid char. For example, this allows
  505. * "N#" (empty "nnnn" part): 64#+1 is a valid expression,
  506. * it means 64# + 1, whereas 64#~... is not, since ~ is not a valid
  507. * operator.
  508. */
  509. *endptr = (char*)nptr;
  510. return n;
  511. }
  512. #else /* !ENABLE_FEATURE_SH_MATH_BASE */
  513. # if ENABLE_FEATURE_SH_MATH_64
  514. # define strto_arith_t(nptr, endptr) strtoull(nptr, endptr, 0)
  515. # else
  516. # define strto_arith_t(nptr, endptr) strtoul(nptr, endptr, 0)
  517. # endif
  518. #endif
  519. static arith_t
  520. evaluate_string(arith_state_t *math_state, const char *expr)
  521. {
  522. operator lasttok;
  523. const char *errmsg;
  524. const char *start_expr = expr = skip_whitespace(expr);
  525. unsigned expr_len = strlen(expr) + 2;
  526. /* Stack of integers */
  527. /* The proof that there can be no more than strlen(startbuf)/2+1
  528. * integers in any given correct or incorrect expression
  529. * is left as an exercise to the reader. */
  530. var_or_num_t *const numstack = alloca((expr_len / 2) * sizeof(numstack[0]));
  531. var_or_num_t *numstackptr = numstack;
  532. /* Stack of operator tokens */
  533. operator *const stack = alloca(expr_len * sizeof(stack[0]));
  534. operator *stackptr = stack;
  535. /* Start with a left paren */
  536. *stackptr++ = lasttok = TOK_LPAREN;
  537. errmsg = NULL;
  538. while (1) {
  539. const char *p;
  540. operator op;
  541. operator prec;
  542. expr = skip_whitespace(expr);
  543. if (*expr == '\0') {
  544. if (expr == start_expr) {
  545. /* Null expression */
  546. numstack->val = 0;
  547. goto ret;
  548. }
  549. /* This is only reached after all tokens have been extracted from the
  550. * input stream. If there are still tokens on the operator stack, they
  551. * are to be applied in order. At the end, there should be a final
  552. * result on the integer stack */
  553. if (expr != ptr_to_rparen + 1) {
  554. /* If we haven't done so already,
  555. * append a closing right paren
  556. * and let the loop process it */
  557. expr = ptr_to_rparen;
  558. //bb_error_msg("expr=')'");
  559. continue;
  560. }
  561. /* At this point, we're done with the expression */
  562. if (numstackptr != numstack + 1) {
  563. /* ...but if there isn't, it's bad */
  564. goto err;
  565. }
  566. goto ret;
  567. }
  568. p = endofname(expr);
  569. if (p != expr) {
  570. /* Name */
  571. size_t var_name_size = (p - expr) + 1; /* +1 for NUL */
  572. numstackptr->var = alloca(var_name_size);
  573. safe_strncpy(numstackptr->var, expr, var_name_size);
  574. //bb_error_msg("var:'%s'", numstackptr->var);
  575. expr = p;
  576. num:
  577. numstackptr->second_val_present = 0;
  578. numstackptr++;
  579. lasttok = TOK_NUM;
  580. continue;
  581. }
  582. if (isdigit(*expr)) {
  583. /* Number */
  584. numstackptr->var = NULL;
  585. errno = 0;
  586. numstackptr->val = strto_arith_t(expr, (char**) &expr);
  587. //bb_error_msg("val:%lld", numstackptr->val);
  588. if (errno)
  589. numstackptr->val = 0; /* bash compat */
  590. goto num;
  591. }
  592. /* Should be an operator */
  593. /* Special case: XYZ--, XYZ++, --XYZ, ++XYZ are recognized
  594. * only if XYZ is a variable name, not a number or EXPR. IOW:
  595. * "a+++v" is a++ + v.
  596. * "(a)+++7" is ( a ) + + + 7.
  597. * "7+++v" is 7 + ++v, not 7++ + v.
  598. * "--7" is - - 7, not --7.
  599. * "++++a" is + + ++a, not ++ ++a.
  600. */
  601. if ((expr[0] == '+' || expr[0] == '-')
  602. && (expr[1] == expr[0])
  603. ) {
  604. if (numstackptr == numstack || !numstackptr[-1].var) { /* not a VAR++ */
  605. char next = skip_whitespace(expr + 2)[0];
  606. if (!(isalpha(next) || next == '_')) { /* not a ++VAR */
  607. //bb_error_msg("special %c%c", expr[0], expr[0]);
  608. op = (expr[0] == '+' ? TOK_ADD : TOK_SUB);
  609. expr++;
  610. goto tok_found1;
  611. }
  612. }
  613. }
  614. p = op_tokens;
  615. while (1) {
  616. /* Compare expr to current op_tokens[] element */
  617. const char *e = expr;
  618. while (1) {
  619. if (*p == '\0') {
  620. /* Match: operator is found */
  621. expr = e;
  622. goto tok_found;
  623. }
  624. if (*p != *e)
  625. break;
  626. p++;
  627. e++;
  628. }
  629. /* No match, go to next element of op_tokens[] */
  630. while (*p)
  631. p++;
  632. p += 2; /* skip NUL and TOK_foo bytes */
  633. if (*p == '\0') {
  634. /* No next element, operator not found */
  635. //math_state->syntax_error_at = expr;
  636. goto err;
  637. }
  638. }
  639. tok_found:
  640. op = p[1]; /* fetch TOK_foo value */
  641. tok_found1:
  642. /* NB: expr now points past the operator */
  643. /* post grammar: a++ reduce to num */
  644. if (lasttok == TOK_POST_INC || lasttok == TOK_POST_DEC)
  645. lasttok = TOK_NUM;
  646. /* Plus and minus are binary (not unary) _only_ if the last
  647. * token was a number, or a right paren (which pretends to be
  648. * a number, since it evaluates to one). Think about it.
  649. * It makes sense. */
  650. if (lasttok != TOK_NUM) {
  651. switch (op) {
  652. case TOK_ADD:
  653. op = TOK_UPLUS;
  654. break;
  655. case TOK_SUB:
  656. op = TOK_UMINUS;
  657. break;
  658. case TOK_POST_INC:
  659. op = TOK_PRE_INC;
  660. break;
  661. case TOK_POST_DEC:
  662. op = TOK_PRE_DEC;
  663. break;
  664. }
  665. }
  666. /* We don't want an unary operator to cause recursive descent on the
  667. * stack, because there can be many in a row and it could cause an
  668. * operator to be evaluated before its argument is pushed onto the
  669. * integer stack.
  670. * But for binary operators, "apply" everything on the operator
  671. * stack until we find an operator with a lesser priority than the
  672. * one we have just extracted. If op is right-associative,
  673. * then stop "applying" on the equal priority too.
  674. * Left paren is given the lowest priority so it will never be
  675. * "applied" in this way.
  676. */
  677. prec = PREC(op);
  678. //bb_error_msg("prec:%02x", prec);
  679. if ((prec > 0 && prec < UNARYPREC) || prec == SPEC_PREC) {
  680. /* not left paren or unary */
  681. if (lasttok != TOK_NUM) {
  682. /* binary op must be preceded by a num */
  683. goto err;
  684. }
  685. /* The algorithm employed here is simple: while we don't
  686. * hit an open paren nor the bottom of the stack, pop
  687. * tokens and apply them */
  688. while (stackptr != stack) {
  689. operator prev_op = *--stackptr;
  690. if (op == TOK_RPAREN) {
  691. //bb_error_msg("op == TOK_RPAREN");
  692. if (prev_op == TOK_LPAREN) {
  693. //bb_error_msg("prev_op == TOK_LPAREN");
  694. //bb_error_msg(" %p %p numstackptr[-1].var:'%s'", numstack, numstackptr-1, numstackptr[-1].var);
  695. if (numstackptr[-1].var) {
  696. /* Expression is (var), lookup now */
  697. errmsg = arith_lookup_val(math_state, &numstackptr[-1]);
  698. if (errmsg)
  699. goto err_with_custom_msg;
  700. /* Erase var name: (var) is just a number, for example, (var) = 1 is not valid */
  701. numstackptr[-1].var = NULL;
  702. }
  703. /* Any operator directly after a
  704. * close paren should consider itself binary */
  705. lasttok = TOK_NUM;
  706. goto next;
  707. }
  708. //bb_error_msg("prev_op != TOK_LPAREN");
  709. } else {
  710. operator prev_prec = PREC(prev_op);
  711. //bb_error_msg("op != TOK_RPAREN");
  712. fix_assignment_prec(prec);
  713. fix_assignment_prec(prev_prec);
  714. if (prev_prec < prec
  715. || (prev_prec == prec && is_right_associative(prec))
  716. ) {
  717. stackptr++;
  718. break;
  719. }
  720. }
  721. //bb_error_msg("arith_apply(prev_op:%02x)", prev_op);
  722. errmsg = arith_apply(math_state, prev_op, numstack, &numstackptr);
  723. if (errmsg)
  724. goto err_with_custom_msg;
  725. }
  726. if (op == TOK_RPAREN)
  727. goto err;
  728. }
  729. /* Push this operator to the stack and remember it */
  730. //bb_error_msg("push op:%02x", op);
  731. *stackptr++ = lasttok = op;
  732. next: ;
  733. } /* while (1) */
  734. err:
  735. errmsg = "arithmetic syntax error";
  736. err_with_custom_msg:
  737. numstack->val = -1;
  738. ret:
  739. math_state->errmsg = errmsg;
  740. return numstack->val;
  741. }
  742. arith_t FAST_FUNC
  743. arith(arith_state_t *math_state, const char *expr)
  744. {
  745. math_state->errmsg = NULL;
  746. math_state->list_of_recursed_names = NULL;
  747. return evaluate_string(math_state, expr);
  748. }
  749. /*
  750. * Copyright (c) 1989, 1991, 1993, 1994
  751. * The Regents of the University of California. All rights reserved.
  752. *
  753. * This code is derived from software contributed to Berkeley by
  754. * Kenneth Almquist.
  755. *
  756. * Redistribution and use in source and binary forms, with or without
  757. * modification, are permitted provided that the following conditions
  758. * are met:
  759. * 1. Redistributions of source code must retain the above copyright
  760. * notice, this list of conditions and the following disclaimer.
  761. * 2. Redistributions in binary form must reproduce the above copyright
  762. * notice, this list of conditions and the following disclaimer in the
  763. * documentation and/or other materials provided with the distribution.
  764. * 3. Neither the name of the University nor the names of its contributors
  765. * may be used to endorse or promote products derived from this software
  766. * without specific prior written permission.
  767. *
  768. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND
  769. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  770. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  771. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  772. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  773. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  774. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  775. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  776. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  777. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  778. * SUCH DAMAGE.
  779. */