1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include "libbb.h"
30#include "math.h"
31
32#define a_e_h_t arith_eval_hooks_t
33#define lookupvar (math_hooks->lookupvar)
34#define setvar (math_hooks->setvar)
35#define endofname (math_hooks->endofname)
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131#define arith_isspace(arithval) \
132 (arithval == ' ' || arithval == '\n' || arithval == '\t')
133
134typedef unsigned char operator;
135
136
137
138
139
140
141
142#define tok_decl(prec,id) (((id)<<5)|(prec))
143#define PREC(op) ((op) & 0x1F)
144
145#define TOK_LPAREN tok_decl(0,0)
146
147#define TOK_COMMA tok_decl(1,0)
148
149#define TOK_ASSIGN tok_decl(2,0)
150#define TOK_AND_ASSIGN tok_decl(2,1)
151#define TOK_OR_ASSIGN tok_decl(2,2)
152#define TOK_XOR_ASSIGN tok_decl(2,3)
153#define TOK_PLUS_ASSIGN tok_decl(2,4)
154#define TOK_MINUS_ASSIGN tok_decl(2,5)
155#define TOK_LSHIFT_ASSIGN tok_decl(2,6)
156#define TOK_RSHIFT_ASSIGN tok_decl(2,7)
157
158#define TOK_MUL_ASSIGN tok_decl(3,0)
159#define TOK_DIV_ASSIGN tok_decl(3,1)
160#define TOK_REM_ASSIGN tok_decl(3,2)
161
162
163#define convert_prec_is_assing(prec) do { if (prec == 3) prec = 2; } while (0)
164
165
166#define TOK_CONDITIONAL tok_decl(4,0)
167#define TOK_CONDITIONAL_SEP tok_decl(4,1)
168
169#define TOK_OR tok_decl(5,0)
170
171#define TOK_AND tok_decl(6,0)
172
173#define TOK_BOR tok_decl(7,0)
174
175#define TOK_BXOR tok_decl(8,0)
176
177#define TOK_BAND tok_decl(9,0)
178
179#define TOK_EQ tok_decl(10,0)
180#define TOK_NE tok_decl(10,1)
181
182#define TOK_LT tok_decl(11,0)
183#define TOK_GT tok_decl(11,1)
184#define TOK_GE tok_decl(11,2)
185#define TOK_LE tok_decl(11,3)
186
187#define TOK_LSHIFT tok_decl(12,0)
188#define TOK_RSHIFT tok_decl(12,1)
189
190#define TOK_ADD tok_decl(13,0)
191#define TOK_SUB tok_decl(13,1)
192
193#define TOK_MUL tok_decl(14,0)
194#define TOK_DIV tok_decl(14,1)
195#define TOK_REM tok_decl(14,2)
196
197
198#define TOK_EXPONENT tok_decl(15,1)
199
200
201#define UNARYPREC 16
202#define TOK_BNOT tok_decl(UNARYPREC,0)
203#define TOK_NOT tok_decl(UNARYPREC,1)
204
205#define TOK_UMINUS tok_decl(UNARYPREC+1,0)
206#define TOK_UPLUS tok_decl(UNARYPREC+1,1)
207
208#define PREC_PRE (UNARYPREC+2)
209
210#define TOK_PRE_INC tok_decl(PREC_PRE, 0)
211#define TOK_PRE_DEC tok_decl(PREC_PRE, 1)
212
213#define PREC_POST (UNARYPREC+3)
214
215#define TOK_POST_INC tok_decl(PREC_POST, 0)
216#define TOK_POST_DEC tok_decl(PREC_POST, 1)
217
218#define SPEC_PREC (UNARYPREC+4)
219
220#define TOK_NUM tok_decl(SPEC_PREC, 0)
221#define TOK_RPAREN tok_decl(SPEC_PREC, 1)
222
223#define NUMPTR (*numstackptr)
224
225static int
226tok_have_assign(operator op)
227{
228 operator prec = PREC(op);
229
230 convert_prec_is_assing(prec);
231 return (prec == PREC(TOK_ASSIGN) ||
232 prec == PREC_PRE || prec == PREC_POST);
233}
234
235static int
236is_right_associativity(operator prec)
237{
238 return (prec == PREC(TOK_ASSIGN) || prec == PREC(TOK_EXPONENT)
239 || prec == PREC(TOK_CONDITIONAL));
240}
241
242typedef struct {
243 arith_t val;
244 arith_t contidional_second_val;
245 char contidional_second_val_initialized;
246 char *var;
247
248} v_n_t;
249
250typedef struct chk_var_recursive_looped_t {
251 const char *var;
252 struct chk_var_recursive_looped_t *next;
253} chk_var_recursive_looped_t;
254
255static chk_var_recursive_looped_t *prev_chk_var_recursive;
256
257static int
258arith_lookup_val(v_n_t *t, a_e_h_t *math_hooks)
259{
260 if (t->var) {
261 const char * p = lookupvar(t->var);
262
263 if (p) {
264 int errcode;
265
266
267 chk_var_recursive_looped_t *cur;
268 chk_var_recursive_looped_t cur_save;
269
270 for (cur = prev_chk_var_recursive; cur; cur = cur->next) {
271 if (strcmp(cur->var, t->var) == 0) {
272
273 return -5;
274 }
275 }
276
277 cur = prev_chk_var_recursive;
278 cur_save.var = t->var;
279 cur_save.next = cur;
280 prev_chk_var_recursive = &cur_save;
281
282 t->val = arith (p, &errcode, math_hooks);
283
284 prev_chk_var_recursive = cur;
285 return errcode;
286 }
287
288 t->val = 0;
289 }
290 return 0;
291}
292
293
294
295
296static int
297arith_apply(operator op, v_n_t *numstack, v_n_t **numstackptr, a_e_h_t *math_hooks)
298{
299 v_n_t *numptr_m1;
300 arith_t numptr_val, rez;
301 int ret_arith_lookup_val;
302
303
304 if (NUMPTR == numstack) goto err;
305 numptr_m1 = NUMPTR - 1;
306
307
308 ret_arith_lookup_val = arith_lookup_val(numptr_m1, math_hooks);
309 if (ret_arith_lookup_val)
310 return ret_arith_lookup_val;
311
312 rez = numptr_m1->val;
313 if (op == TOK_UMINUS)
314 rez *= -1;
315 else if (op == TOK_NOT)
316 rez = !rez;
317 else if (op == TOK_BNOT)
318 rez = ~rez;
319 else if (op == TOK_POST_INC || op == TOK_PRE_INC)
320 rez++;
321 else if (op == TOK_POST_DEC || op == TOK_PRE_DEC)
322 rez--;
323 else if (op != TOK_UPLUS) {
324
325
326
327 if (numptr_m1 == numstack) goto err;
328
329
330 --NUMPTR;
331 numptr_val = rez;
332 if (op == TOK_CONDITIONAL) {
333 if (!numptr_m1->contidional_second_val_initialized) {
334
335 goto err;
336 }
337 rez = numptr_m1->contidional_second_val;
338 } else if (numptr_m1->contidional_second_val_initialized) {
339
340 goto err;
341 }
342 numptr_m1 = NUMPTR - 1;
343 if (op != TOK_ASSIGN) {
344
345 ret_arith_lookup_val = arith_lookup_val(numptr_m1, math_hooks);
346 if (ret_arith_lookup_val)
347 return ret_arith_lookup_val;
348 }
349 if (op == TOK_CONDITIONAL) {
350 numptr_m1->contidional_second_val = rez;
351 }
352 rez = numptr_m1->val;
353 if (op == TOK_BOR || op == TOK_OR_ASSIGN)
354 rez |= numptr_val;
355 else if (op == TOK_OR)
356 rez = numptr_val || rez;
357 else if (op == TOK_BAND || op == TOK_AND_ASSIGN)
358 rez &= numptr_val;
359 else if (op == TOK_BXOR || op == TOK_XOR_ASSIGN)
360 rez ^= numptr_val;
361 else if (op == TOK_AND)
362 rez = rez && numptr_val;
363 else if (op == TOK_EQ)
364 rez = (rez == numptr_val);
365 else if (op == TOK_NE)
366 rez = (rez != numptr_val);
367 else if (op == TOK_GE)
368 rez = (rez >= numptr_val);
369 else if (op == TOK_RSHIFT || op == TOK_RSHIFT_ASSIGN)
370 rez >>= numptr_val;
371 else if (op == TOK_LSHIFT || op == TOK_LSHIFT_ASSIGN)
372 rez <<= numptr_val;
373 else if (op == TOK_GT)
374 rez = (rez > numptr_val);
375 else if (op == TOK_LT)
376 rez = (rez < numptr_val);
377 else if (op == TOK_LE)
378 rez = (rez <= numptr_val);
379 else if (op == TOK_MUL || op == TOK_MUL_ASSIGN)
380 rez *= numptr_val;
381 else if (op == TOK_ADD || op == TOK_PLUS_ASSIGN)
382 rez += numptr_val;
383 else if (op == TOK_SUB || op == TOK_MINUS_ASSIGN)
384 rez -= numptr_val;
385 else if (op == TOK_ASSIGN || op == TOK_COMMA)
386 rez = numptr_val;
387 else if (op == TOK_CONDITIONAL_SEP) {
388 if (numptr_m1 == numstack) {
389
390 goto err;
391 }
392 numptr_m1->contidional_second_val_initialized = op;
393 numptr_m1->contidional_second_val = numptr_val;
394 } else if (op == TOK_CONDITIONAL) {
395 rez = rez ?
396 numptr_val : numptr_m1->contidional_second_val;
397 } else if (op == TOK_EXPONENT) {
398 if (numptr_val < 0)
399 return -3;
400 else {
401 arith_t c = 1;
402
403 if (numptr_val)
404 while (numptr_val--)
405 c *= rez;
406 rez = c;
407 }
408 } else if (numptr_val==0)
409 return -2;
410 else if (op == TOK_DIV || op == TOK_DIV_ASSIGN)
411 rez /= numptr_val;
412 else if (op == TOK_REM || op == TOK_REM_ASSIGN)
413 rez %= numptr_val;
414 }
415 if (tok_have_assign(op)) {
416 char buf[sizeof(arith_t)*3 + 2];
417
418 if (numptr_m1->var == NULL) {
419
420 goto err;
421 }
422
423 sprintf(buf, arith_t_fmt, rez);
424 setvar(numptr_m1->var, buf, 0);
425
426 if (op == TOK_POST_INC)
427 rez--;
428 else if (op == TOK_POST_DEC)
429 rez++;
430 }
431 numptr_m1->val = rez;
432
433 numptr_m1->var = NULL;
434 return 0;
435 err:
436 return -1;
437}
438
439
440static const char op_tokens[] ALIGN1 = {
441 '<','<','=',0, TOK_LSHIFT_ASSIGN,
442 '>','>','=',0, TOK_RSHIFT_ASSIGN,
443 '<','<', 0, TOK_LSHIFT,
444 '>','>', 0, TOK_RSHIFT,
445 '|','|', 0, TOK_OR,
446 '&','&', 0, TOK_AND,
447 '!','=', 0, TOK_NE,
448 '<','=', 0, TOK_LE,
449 '>','=', 0, TOK_GE,
450 '=','=', 0, TOK_EQ,
451 '|','=', 0, TOK_OR_ASSIGN,
452 '&','=', 0, TOK_AND_ASSIGN,
453 '*','=', 0, TOK_MUL_ASSIGN,
454 '/','=', 0, TOK_DIV_ASSIGN,
455 '%','=', 0, TOK_REM_ASSIGN,
456 '+','=', 0, TOK_PLUS_ASSIGN,
457 '-','=', 0, TOK_MINUS_ASSIGN,
458 '-','-', 0, TOK_POST_DEC,
459 '^','=', 0, TOK_XOR_ASSIGN,
460 '+','+', 0, TOK_POST_INC,
461 '*','*', 0, TOK_EXPONENT,
462 '!', 0, TOK_NOT,
463 '<', 0, TOK_LT,
464 '>', 0, TOK_GT,
465 '=', 0, TOK_ASSIGN,
466 '|', 0, TOK_BOR,
467 '&', 0, TOK_BAND,
468 '*', 0, TOK_MUL,
469 '/', 0, TOK_DIV,
470 '%', 0, TOK_REM,
471 '+', 0, TOK_ADD,
472 '-', 0, TOK_SUB,
473 '^', 0, TOK_BXOR,
474
475 '~', 0, TOK_BNOT,
476 ',', 0, TOK_COMMA,
477 '?', 0, TOK_CONDITIONAL,
478 ':', 0, TOK_CONDITIONAL_SEP,
479 ')', 0, TOK_RPAREN,
480 '(', 0, TOK_LPAREN,
481 0
482};
483
484#define endexpression (&op_tokens[sizeof(op_tokens)-7])
485
486arith_t
487arith(const char *expr, int *perrcode, a_e_h_t *math_hooks)
488{
489 char arithval;
490 operator lasttok, op;
491 operator prec;
492 operator *stack, *stackptr;
493 const char *p = endexpression;
494 int errcode;
495 v_n_t *numstack, *numstackptr;
496 unsigned datasizes = strlen(expr) + 2;
497
498
499
500
501
502 numstackptr = numstack = alloca((datasizes / 2) * sizeof(numstack[0]));
503
504 stackptr = stack = alloca(datasizes * sizeof(stack[0]));
505
506 *stackptr++ = lasttok = TOK_LPAREN;
507 *perrcode = errcode = 0;
508
509 while (1) {
510 arithval = *expr;
511 if (arithval == 0) {
512 if (p == endexpression) {
513
514 return 0;
515 }
516
517
518
519
520
521
522 if (expr != endexpression + 1) {
523
524
525 expr = endexpression;
526
527 continue;
528 }
529
530 if (numstackptr != numstack+1) {
531
532 err:
533 *perrcode = -1;
534 return *perrcode;
535 }
536 if (numstack->var) {
537
538 errcode = arith_lookup_val(numstack, math_hooks);
539 }
540 ret:
541 *perrcode = errcode;
542 return numstack->val;
543 }
544
545
546 if (arith_isspace(arithval)) {
547
548 goto prologue;
549 }
550 p = endofname(expr);
551 if (p != expr) {
552 size_t var_name_size = (p-expr) + 1;
553
554 numstackptr->var = alloca(var_name_size);
555 safe_strncpy(numstackptr->var, expr, var_name_size);
556 expr = p;
557 num:
558 numstackptr->contidional_second_val_initialized = 0;
559 numstackptr++;
560 lasttok = TOK_NUM;
561 continue;
562 }
563 if (isdigit(arithval)) {
564 numstackptr->var = NULL;
565 numstackptr->val = strto_arith_t(expr, (char **) &expr, 0);
566 goto num;
567 }
568 for (p = op_tokens; ; p++) {
569 const char *o;
570
571 if (*p == 0) {
572
573 goto err;
574 }
575 for (o = expr; *p && *o == *p; p++)
576 o++;
577 if (!*p) {
578
579 expr = o - 1;
580 break;
581 }
582
583 while (*p)
584 p++;
585
586 p++;
587 }
588 op = p[1];
589
590
591 if (lasttok == TOK_POST_INC || lasttok == TOK_POST_DEC)
592 lasttok = TOK_NUM;
593
594
595
596
597
598 if (lasttok != TOK_NUM) {
599 switch (op) {
600 case TOK_ADD:
601 op = TOK_UPLUS;
602 break;
603 case TOK_SUB:
604 op = TOK_UMINUS;
605 break;
606 case TOK_POST_INC:
607 op = TOK_PRE_INC;
608 break;
609 case TOK_POST_DEC:
610 op = TOK_PRE_DEC;
611 break;
612 }
613 }
614
615
616
617
618
619
620
621
622
623
624
625 prec = PREC(op);
626 if ((prec > 0 && prec < UNARYPREC) || prec == SPEC_PREC) {
627
628 if (lasttok != TOK_NUM) {
629
630 goto err;
631 }
632 while (stackptr != stack) {
633 if (op == TOK_RPAREN) {
634
635
636
637 if (stackptr[-1] == TOK_LPAREN) {
638 --stackptr;
639
640 lasttok = TOK_NUM;
641
642 goto prologue;
643 }
644 } else {
645 operator prev_prec = PREC(stackptr[-1]);
646
647 convert_prec_is_assing(prec);
648 convert_prec_is_assing(prev_prec);
649 if (prev_prec < prec)
650 break;
651
652 if (prev_prec == prec && is_right_associativity(prec))
653 break;
654 }
655 errcode = arith_apply(*--stackptr, numstack, &numstackptr, math_hooks);
656 if (errcode) goto ret;
657 }
658 if (op == TOK_RPAREN) {
659 goto err;
660 }
661 }
662
663
664 *stackptr++ = lasttok = op;
665 prologue:
666 ++expr;
667 }
668}
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701