]> git.ipfire.org Git - thirdparty/bash.git/blob - expr.c
Bash-5.2 patch 26: fix typo when specifying readline's custom color prefix
[thirdparty/bash.git] / expr.c
1 /* expr.c -- arithmetic expression evaluation. */
2
3 /* Copyright (C) 1990-2015 Free Software Foundation, Inc.
4
5 This file is part of GNU Bash, the Bourne Again SHell.
6
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /*
22 All arithmetic is done as intmax_t integers with no checking for overflow
23 (though division by 0 is caught and flagged as an error).
24
25 The following operators are handled, grouped into a set of levels in
26 order of decreasing precedence.
27
28 "id++", "id--" [post-increment and post-decrement]
29 "-", "+" [(unary operators)]
30 "++id", "--id" [pre-increment and pre-decrement]
31 "!", "~"
32 "**" [(exponentiation)]
33 "*", "/", "%"
34 "+", "-"
35 "<<", ">>"
36 "<=", ">=", "<", ">"
37 "==", "!="
38 "&"
39 "^"
40 "|"
41 "&&"
42 "||"
43 "expr ? expr : expr"
44 "=", "*=", "/=", "%=", "+=", "-=", "<<=", ">>=", "&=", "^=", "|="
45 , [comma]
46
47 (Note that most of these operators have special meaning to bash, and an
48 entire expression should be quoted, e.g. "a=$a+1" or "a=a+1" to ensure
49 that it is passed intact to the evaluator when using `let'. When using
50 the $[] or $(( )) forms, the text between the `[' and `]' or `((' and `))'
51 is treated as if in double quotes.)
52
53 Sub-expressions within parentheses have a precedence level greater than
54 all of the above levels and are evaluated first. Within a single prece-
55 dence group, evaluation is left-to-right, except for the arithmetic
56 assignment operator (`='), which is evaluated right-to-left (as in C).
57
58 The expression evaluator returns the value of the expression (assignment
59 statements have as a value what is returned by the RHS). The `let'
60 builtin, on the other hand, returns 0 if the last expression evaluates to
61 a non-zero, and 1 otherwise.
62
63 Implementation is a recursive-descent parser.
64
65 Chet Ramey
66 chet@po.cwru.edu
67 */
68
69 #include "config.h"
70
71 #include <stdio.h>
72 #include "bashansi.h"
73
74 #if defined (HAVE_UNISTD_H)
75 # ifdef _MINIX
76 # include <sys/types.h>
77 # endif
78 # include <unistd.h>
79 #endif
80
81 #include "chartypes.h"
82 #include "bashintl.h"
83
84 #include "shell.h"
85 #include "arrayfunc.h"
86 #include "execute_cmd.h"
87 #include "flags.h"
88 #include "subst.h"
89 #include "typemax.h" /* INTMAX_MAX, INTMAX_MIN */
90
91 /* Because of the $((...)) construct, expressions may include newlines.
92 Here is a macro which accepts newlines, tabs and spaces as whitespace. */
93 #define cr_whitespace(c) (whitespace(c) || ((c) == '\n'))
94
95 /* Size be which the expression stack grows when necessary. */
96 #define EXPR_STACK_GROW_SIZE 10
97
98 /* Maximum amount of recursion allowed. This prevents a non-integer
99 variable such as "num=num+2" from infinitely adding to itself when
100 "let num=num+2" is given. */
101 #define MAX_EXPR_RECURSION_LEVEL 1024
102
103 /* The Tokens. Singing "The Lion Sleeps Tonight". */
104
105 #define EQEQ 1 /* "==" */
106 #define NEQ 2 /* "!=" */
107 #define LEQ 3 /* "<=" */
108 #define GEQ 4 /* ">=" */
109 #define STR 5 /* string */
110 #define NUM 6 /* number */
111 #define LAND 7 /* "&&" Logical AND */
112 #define LOR 8 /* "||" Logical OR */
113 #define LSH 9 /* "<<" Left SHift */
114 #define RSH 10 /* ">>" Right SHift */
115 #define OP_ASSIGN 11 /* op= expassign as in Posix.2 */
116 #define COND 12 /* exp1 ? exp2 : exp3 */
117 #define POWER 13 /* exp1**exp2 */
118 #define PREINC 14 /* ++var */
119 #define PREDEC 15 /* --var */
120 #define POSTINC 16 /* var++ */
121 #define POSTDEC 17 /* var-- */
122 #define EQ '='
123 #define GT '>'
124 #define LT '<'
125 #define PLUS '+'
126 #define MINUS '-'
127 #define MUL '*'
128 #define DIV '/'
129 #define MOD '%'
130 #define NOT '!'
131 #define LPAR '('
132 #define RPAR ')'
133 #define BAND '&' /* Bitwise AND */
134 #define BOR '|' /* Bitwise OR. */
135 #define BXOR '^' /* Bitwise eXclusive OR. */
136 #define BNOT '~' /* Bitwise NOT; Two's complement. */
137 #define QUES '?'
138 #define COL ':'
139 #define COMMA ','
140
141 /* This should be the function corresponding to the operator with the
142 highest precedence. */
143 #define EXP_HIGHEST expcomma
144
145 #ifndef MAX_INT_LEN
146 # define MAX_INT_LEN 32
147 #endif
148
149 struct lvalue
150 {
151 char *tokstr; /* possibly-rewritten lvalue if not NULL */
152 intmax_t tokval; /* expression evaluated value */
153 SHELL_VAR *tokvar; /* variable described by array or var reference */
154 intmax_t ind; /* array index if not -1 */
155 };
156
157 /* A structure defining a single expression context. */
158 typedef struct {
159 int curtok, lasttok;
160 char *expression, *tp, *lasttp;
161 intmax_t tokval;
162 char *tokstr;
163 int noeval;
164 struct lvalue lval;
165 } EXPR_CONTEXT;
166
167 static char *expression; /* The current expression */
168 static char *tp; /* token lexical position */
169 static char *lasttp; /* pointer to last token position */
170 static int curtok; /* the current token */
171 static int lasttok; /* the previous token */
172 static int assigntok; /* the OP in OP= */
173 static char *tokstr; /* current token string */
174 static intmax_t tokval; /* current token value */
175 static int noeval; /* set to 1 if no assignment to be done */
176 static procenv_t evalbuf;
177
178 /* set to 1 if the expression has already been run through word expansion */
179 static int already_expanded;
180
181 static struct lvalue curlval = {0, 0, 0, -1};
182 static struct lvalue lastlval = {0, 0, 0, -1};
183
184 static int _is_arithop __P((int));
185 static void readtok __P((void)); /* lexical analyzer */
186
187 static void init_lvalue __P((struct lvalue *));
188 static struct lvalue *alloc_lvalue __P((void));
189 static void free_lvalue __P((struct lvalue *));
190
191 static intmax_t expr_streval __P((char *, int, struct lvalue *));
192 static intmax_t strlong __P((char *));
193 static void evalerror __P((const char *));
194
195 static void pushexp __P((void));
196 static void popexp __P((void));
197 static void expr_unwind __P((void));
198 static void expr_bind_variable __P((char *, char *));
199 #if defined (ARRAY_VARS)
200 static void expr_bind_array_element __P((char *, arrayind_t, char *));
201 #endif
202
203 static intmax_t subexpr __P((char *));
204
205 static intmax_t expcomma __P((void));
206 static intmax_t expassign __P((void));
207 static intmax_t expcond __P((void));
208 static intmax_t explor __P((void));
209 static intmax_t expland __P((void));
210 static intmax_t expbor __P((void));
211 static intmax_t expbxor __P((void));
212 static intmax_t expband __P((void));
213 static intmax_t exp5 __P((void));
214 static intmax_t exp4 __P((void));
215 static intmax_t expshift __P((void));
216 static intmax_t exp3 __P((void));
217 static intmax_t expmuldiv __P((void));
218 static intmax_t exppower __P((void));
219 static intmax_t exp1 __P((void));
220 static intmax_t exp0 __P((void));
221
222 /* Global var which contains the stack of expression contexts. */
223 static EXPR_CONTEXT **expr_stack;
224 static int expr_depth; /* Location in the stack. */
225 static int expr_stack_size; /* Number of slots already allocated. */
226
227 #if defined (ARRAY_VARS)
228 extern const char * const bash_badsub_errmsg;
229 #endif
230
231 #define SAVETOK(X) \
232 do { \
233 (X)->curtok = curtok; \
234 (X)->lasttok = lasttok; \
235 (X)->tp = tp; \
236 (X)->lasttp = lasttp; \
237 (X)->tokval = tokval; \
238 (X)->tokstr = tokstr; \
239 (X)->noeval = noeval; \
240 (X)->lval = curlval; \
241 } while (0)
242
243 #define RESTORETOK(X) \
244 do { \
245 curtok = (X)->curtok; \
246 lasttok = (X)->lasttok; \
247 tp = (X)->tp; \
248 lasttp = (X)->lasttp; \
249 tokval = (X)->tokval; \
250 tokstr = (X)->tokstr; \
251 noeval = (X)->noeval; \
252 curlval = (X)->lval; \
253 } while (0)
254
255 /* Push and save away the contents of the globals describing the
256 current expression context. */
257 static void
258 pushexp ()
259 {
260 EXPR_CONTEXT *context;
261
262 if (expr_depth >= MAX_EXPR_RECURSION_LEVEL)
263 evalerror (_("expression recursion level exceeded"));
264
265 if (expr_depth >= expr_stack_size)
266 {
267 expr_stack_size += EXPR_STACK_GROW_SIZE;
268 expr_stack = (EXPR_CONTEXT **)xrealloc (expr_stack, expr_stack_size * sizeof (EXPR_CONTEXT *));
269 }
270
271 context = (EXPR_CONTEXT *)xmalloc (sizeof (EXPR_CONTEXT));
272
273 context->expression = expression;
274 SAVETOK(context);
275
276 expr_stack[expr_depth++] = context;
277 }
278
279 /* Pop the the contents of the expression context stack into the
280 globals describing the current expression context. */
281 static void
282 popexp ()
283 {
284 EXPR_CONTEXT *context;
285
286 if (expr_depth <= 0)
287 {
288 /* See the comment at the top of evalexp() for an explanation of why
289 this is done. */
290 expression = lasttp = 0;
291 evalerror (_("recursion stack underflow"));
292 }
293
294 context = expr_stack[--expr_depth];
295
296 expression = context->expression;
297 RESTORETOK (context);
298
299 free (context);
300 }
301
302 static void
303 expr_unwind ()
304 {
305 while (--expr_depth > 0)
306 {
307 if (expr_stack[expr_depth]->tokstr)
308 free (expr_stack[expr_depth]->tokstr);
309
310 if (expr_stack[expr_depth]->expression)
311 free (expr_stack[expr_depth]->expression);
312
313 free (expr_stack[expr_depth]);
314 }
315 if (expr_depth == 0)
316 free (expr_stack[expr_depth]); /* free the allocated EXPR_CONTEXT */
317
318 noeval = 0; /* XXX */
319 }
320
321 static void
322 expr_bind_variable (lhs, rhs)
323 char *lhs, *rhs;
324 {
325 SHELL_VAR *v;
326 int aflags;
327
328 if (lhs == 0 || *lhs == 0)
329 return; /* XXX */
330
331 #if defined (ARRAY_VARS)
332 aflags = (assoc_expand_once && already_expanded) ? ASS_NOEXPAND : 0;
333 #else
334 aflags = 0;
335 #endif
336 v = bind_int_variable (lhs, rhs, aflags);
337 if (v && (readonly_p (v) || noassign_p (v)))
338 sh_longjmp (evalbuf, 1); /* variable assignment error */
339 stupidly_hack_special_variables (lhs);
340 }
341
342 #if defined (ARRAY_VARS)
343 /* This is similar to the logic in arrayfunc.c:valid_array_subscript when
344 you pass VA_NOEXPAND. */
345 static int
346 expr_skipsubscript (vp, cp)
347 char *vp, *cp;
348 {
349 int flags, isassoc;
350 SHELL_VAR *entry;
351
352 isassoc = 0;
353 entry = 0;
354 if (assoc_expand_once & already_expanded)
355 {
356 *cp = '\0';
357 isassoc = legal_identifier (vp) && (entry = find_variable (vp)) && assoc_p (entry);
358 *cp = '['; /* ] */
359 }
360 flags = (isassoc && assoc_expand_once && already_expanded) ? VA_NOEXPAND : 0;
361 return (skipsubscript (cp, 0, flags));
362 }
363
364 /* Rewrite tok, which is of the form vname[expression], to vname[ind], where
365 IND is the already-calculated value of expression. */
366 static void
367 expr_bind_array_element (tok, ind, rhs)
368 char *tok;
369 arrayind_t ind;
370 char *rhs;
371 {
372 char *lhs, *vname;
373 size_t llen;
374 char ibuf[INT_STRLEN_BOUND (arrayind_t) + 1], *istr;
375
376 istr = fmtumax (ind, 10, ibuf, sizeof (ibuf), 0);
377 vname = array_variable_name (tok, 0, (char **)NULL, (int *)NULL);
378
379 llen = strlen (vname) + sizeof (ibuf) + 3;
380 lhs = xmalloc (llen);
381
382 sprintf (lhs, "%s[%s]", vname, istr); /* XXX */
383
384 /*itrace("expr_bind_array_element: %s=%s", lhs, rhs);*/
385 expr_bind_variable (lhs, rhs);
386 free (vname);
387 free (lhs);
388 }
389 #endif /* ARRAY_VARS */
390
391 /* Evaluate EXPR, and return the arithmetic result. If VALIDP is
392 non-null, a zero is stored into the location to which it points
393 if the expression is invalid, non-zero otherwise. If a non-zero
394 value is returned in *VALIDP, the return value of evalexp() may
395 be used.
396
397 The `while' loop after the longjmp is caught relies on the above
398 implementation of pushexp and popexp leaving in expr_stack[0] the
399 values that the variables had when the program started. That is,
400 the first things saved are the initial values of the variables that
401 were assigned at program startup or by the compiler. Therefore, it is
402 safe to let the loop terminate when expr_depth == 0, without freeing up
403 any of the expr_depth[0] stuff. */
404 intmax_t
405 evalexp (expr, flags, validp)
406 char *expr;
407 int flags;
408 int *validp;
409 {
410 intmax_t val;
411 int c;
412 procenv_t oevalbuf;
413
414 val = 0;
415 noeval = 0;
416 already_expanded = (flags&EXP_EXPANDED);
417
418 FASTCOPY (evalbuf, oevalbuf, sizeof (evalbuf));
419
420 c = setjmp_nosigs (evalbuf);
421
422 if (c)
423 {
424 FREE (tokstr);
425 FREE (expression);
426 tokstr = expression = (char *)NULL;
427
428 expr_unwind ();
429 expr_depth = 0; /* XXX - make sure */
430
431 /* We copy in case we've called evalexp recursively */
432 FASTCOPY (oevalbuf, evalbuf, sizeof (evalbuf));
433
434 if (validp)
435 *validp = 0;
436 return (0);
437 }
438
439 val = subexpr (expr);
440
441 if (validp)
442 *validp = 1;
443
444 FASTCOPY (oevalbuf, evalbuf, sizeof (evalbuf));
445
446 return (val);
447 }
448
449 static intmax_t
450 subexpr (expr)
451 char *expr;
452 {
453 intmax_t val;
454 char *p;
455
456 for (p = expr; p && *p && cr_whitespace (*p); p++)
457 ;
458
459 if (p == NULL || *p == '\0')
460 return (0);
461
462 pushexp ();
463 expression = savestring (expr);
464 tp = expression;
465
466 curtok = lasttok = 0;
467 tokstr = (char *)NULL;
468 tokval = 0;
469 init_lvalue (&curlval);
470 lastlval = curlval;
471
472 readtok ();
473
474 val = EXP_HIGHEST ();
475
476 if (curtok != 0)
477 evalerror (_("syntax error in expression"));
478
479 FREE (tokstr);
480 FREE (expression);
481
482 popexp ();
483
484 return val;
485 }
486
487 static intmax_t
488 expcomma ()
489 {
490 register intmax_t value;
491
492 value = expassign ();
493 while (curtok == COMMA)
494 {
495 readtok ();
496 value = expassign ();
497 }
498
499 return value;
500 }
501
502 static intmax_t
503 expassign ()
504 {
505 register intmax_t value;
506 char *lhs, *rhs;
507 arrayind_t lind;
508 #if defined (HAVE_IMAXDIV)
509 imaxdiv_t idiv;
510 #endif
511
512 value = expcond ();
513 if (curtok == EQ || curtok == OP_ASSIGN)
514 {
515 int special, op;
516 intmax_t lvalue;
517
518 special = curtok == OP_ASSIGN;
519
520 if (lasttok != STR)
521 evalerror (_("attempted assignment to non-variable"));
522
523 if (special)
524 {
525 op = assigntok; /* a OP= b */
526 lvalue = value;
527 }
528
529 if (tokstr == 0)
530 evalerror (_("syntax error in variable assignment"));
531
532 /* XXX - watch out for pointer aliasing issues here */
533 lhs = savestring (tokstr);
534 /* save ind in case rhs is string var and evaluation overwrites it */
535 lind = curlval.ind;
536 readtok ();
537 value = expassign ();
538
539 if (special)
540 {
541 if ((op == DIV || op == MOD) && value == 0)
542 {
543 if (noeval == 0)
544 evalerror (_("division by 0"));
545 else
546 value = 1;
547 }
548
549 switch (op)
550 {
551 case MUL:
552 lvalue *= value;
553 break;
554 case DIV:
555 case MOD:
556 if (lvalue == INTMAX_MIN && value == -1)
557 lvalue = (op == DIV) ? INTMAX_MIN : 0;
558 else
559 #if HAVE_IMAXDIV
560 {
561 idiv = imaxdiv (lvalue, value);
562 lvalue = (op == DIV) ? idiv.quot : idiv.rem;
563 }
564 #else
565 lvalue = (op == DIV) ? lvalue / value : lvalue % value;
566 #endif
567 break;
568 case PLUS:
569 lvalue += value;
570 break;
571 case MINUS:
572 lvalue -= value;
573 break;
574 case LSH:
575 lvalue <<= value;
576 break;
577 case RSH:
578 lvalue >>= value;
579 break;
580 case BAND:
581 lvalue &= value;
582 break;
583 case BOR:
584 lvalue |= value;
585 break;
586 case BXOR:
587 lvalue ^= value;
588 break;
589 default:
590 free (lhs);
591 evalerror (_("bug: bad expassign token"));
592 break;
593 }
594 value = lvalue;
595 }
596
597 rhs = itos (value);
598 if (noeval == 0)
599 {
600 #if defined (ARRAY_VARS)
601 if (lind != -1)
602 expr_bind_array_element (lhs, lind, rhs);
603 else
604 #endif
605 expr_bind_variable (lhs, rhs);
606 }
607 if (curlval.tokstr && curlval.tokstr == tokstr)
608 init_lvalue (&curlval);
609
610 free (rhs);
611 free (lhs);
612 FREE (tokstr);
613 tokstr = (char *)NULL; /* For freeing on errors. */
614 }
615
616 return (value);
617 }
618
619 /* Conditional expression (expr?expr:expr) */
620 static intmax_t
621 expcond ()
622 {
623 intmax_t cval, val1, val2, rval;
624 int set_noeval;
625
626 set_noeval = 0;
627 rval = cval = explor ();
628 if (curtok == QUES) /* found conditional expr */
629 {
630 if (cval == 0)
631 {
632 set_noeval = 1;
633 noeval++;
634 }
635
636 readtok ();
637 if (curtok == 0 || curtok == COL)
638 evalerror (_("expression expected"));
639
640 val1 = EXP_HIGHEST ();
641
642 if (set_noeval)
643 noeval--;
644 if (curtok != COL)
645 evalerror (_("`:' expected for conditional expression"));
646
647 set_noeval = 0;
648 if (cval)
649 {
650 set_noeval = 1;
651 noeval++;
652 }
653
654 readtok ();
655 if (curtok == 0)
656 evalerror (_("expression expected"));
657 val2 = expcond ();
658
659 if (set_noeval)
660 noeval--;
661 rval = cval ? val1 : val2;
662 lasttok = COND;
663 }
664 return rval;
665 }
666
667 /* Logical OR. */
668 static intmax_t
669 explor ()
670 {
671 register intmax_t val1, val2;
672 int set_noeval;
673
674 val1 = expland ();
675
676 while (curtok == LOR)
677 {
678 set_noeval = 0;
679 if (val1 != 0)
680 {
681 noeval++;
682 set_noeval = 1;
683 }
684 readtok ();
685 val2 = expland ();
686 if (set_noeval)
687 noeval--;
688 val1 = val1 || val2;
689 lasttok = LOR;
690 }
691
692 return (val1);
693 }
694
695 /* Logical AND. */
696 static intmax_t
697 expland ()
698 {
699 register intmax_t val1, val2;
700 int set_noeval;
701
702 val1 = expbor ();
703
704 while (curtok == LAND)
705 {
706 set_noeval = 0;
707 if (val1 == 0)
708 {
709 set_noeval = 1;
710 noeval++;
711 }
712 readtok ();
713 val2 = expbor ();
714 if (set_noeval)
715 noeval--;
716 val1 = val1 && val2;
717 lasttok = LAND;
718 }
719
720 return (val1);
721 }
722
723 /* Bitwise OR. */
724 static intmax_t
725 expbor ()
726 {
727 register intmax_t val1, val2;
728
729 val1 = expbxor ();
730
731 while (curtok == BOR)
732 {
733 readtok ();
734 val2 = expbxor ();
735 val1 = val1 | val2;
736 lasttok = NUM;
737 }
738
739 return (val1);
740 }
741
742 /* Bitwise XOR. */
743 static intmax_t
744 expbxor ()
745 {
746 register intmax_t val1, val2;
747
748 val1 = expband ();
749
750 while (curtok == BXOR)
751 {
752 readtok ();
753 val2 = expband ();
754 val1 = val1 ^ val2;
755 lasttok = NUM;
756 }
757
758 return (val1);
759 }
760
761 /* Bitwise AND. */
762 static intmax_t
763 expband ()
764 {
765 register intmax_t val1, val2;
766
767 val1 = exp5 ();
768
769 while (curtok == BAND)
770 {
771 readtok ();
772 val2 = exp5 ();
773 val1 = val1 & val2;
774 lasttok = NUM;
775 }
776
777 return (val1);
778 }
779
780 static intmax_t
781 exp5 ()
782 {
783 register intmax_t val1, val2;
784
785 val1 = exp4 ();
786
787 while ((curtok == EQEQ) || (curtok == NEQ))
788 {
789 int op = curtok;
790
791 readtok ();
792 val2 = exp4 ();
793 if (op == EQEQ)
794 val1 = (val1 == val2);
795 else if (op == NEQ)
796 val1 = (val1 != val2);
797 lasttok = NUM;
798 }
799 return (val1);
800 }
801
802 static intmax_t
803 exp4 ()
804 {
805 register intmax_t val1, val2;
806
807 val1 = expshift ();
808 while ((curtok == LEQ) ||
809 (curtok == GEQ) ||
810 (curtok == LT) ||
811 (curtok == GT))
812 {
813 int op = curtok;
814
815 readtok ();
816 val2 = expshift ();
817
818 if (op == LEQ)
819 val1 = val1 <= val2;
820 else if (op == GEQ)
821 val1 = val1 >= val2;
822 else if (op == LT)
823 val1 = val1 < val2;
824 else /* (op == GT) */
825 val1 = val1 > val2;
826 lasttok = NUM;
827 }
828 return (val1);
829 }
830
831 /* Left and right shifts. */
832 static intmax_t
833 expshift ()
834 {
835 register intmax_t val1, val2;
836
837 val1 = exp3 ();
838
839 while ((curtok == LSH) || (curtok == RSH))
840 {
841 int op = curtok;
842
843 readtok ();
844 val2 = exp3 ();
845
846 if (op == LSH)
847 val1 = val1 << val2;
848 else
849 val1 = val1 >> val2;
850 lasttok = NUM;
851 }
852
853 return (val1);
854 }
855
856 static intmax_t
857 exp3 ()
858 {
859 register intmax_t val1, val2;
860
861 val1 = expmuldiv ();
862
863 while ((curtok == PLUS) || (curtok == MINUS))
864 {
865 int op = curtok;
866
867 readtok ();
868 val2 = expmuldiv ();
869
870 if (op == PLUS)
871 val1 += val2;
872 else if (op == MINUS)
873 val1 -= val2;
874 lasttok = NUM;
875 }
876 return (val1);
877 }
878
879 static intmax_t
880 expmuldiv ()
881 {
882 register intmax_t val1, val2;
883 #if defined (HAVE_IMAXDIV)
884 imaxdiv_t idiv;
885 #endif
886
887 val1 = exppower ();
888
889 while ((curtok == MUL) ||
890 (curtok == DIV) ||
891 (curtok == MOD))
892 {
893 int op = curtok;
894 char *stp, *sltp;
895
896 stp = tp;
897 readtok ();
898
899 val2 = exppower ();
900
901 /* Handle division by 0 and twos-complement arithmetic overflow */
902 if (((op == DIV) || (op == MOD)) && (val2 == 0))
903 {
904 if (noeval == 0)
905 {
906 sltp = lasttp;
907 lasttp = stp;
908 while (lasttp && *lasttp && whitespace (*lasttp))
909 lasttp++;
910 evalerror (_("division by 0"));
911 lasttp = sltp;
912 }
913 else
914 val2 = 1;
915 }
916 else if (op == MOD && val1 == INTMAX_MIN && val2 == -1)
917 {
918 val1 = 0;
919 continue;
920 }
921 else if (op == DIV && val1 == INTMAX_MIN && val2 == -1)
922 val2 = 1;
923
924 if (op == MUL)
925 val1 *= val2;
926 else if (op == DIV || op == MOD)
927 #if defined (HAVE_IMAXDIV)
928 {
929 idiv = imaxdiv (val1, val2);
930 val1 = (op == DIV) ? idiv.quot : idiv.rem;
931 }
932 #else
933 val1 = (op == DIV) ? val1 / val2 : val1 % val2;
934 #endif
935 lasttok = NUM;
936 }
937 return (val1);
938 }
939
940 static intmax_t
941 ipow (base, exp)
942 intmax_t base, exp;
943 {
944 intmax_t result;
945
946 result = 1;
947 while (exp)
948 {
949 if (exp & 1)
950 result *= base;
951 exp >>= 1;
952 base *= base;
953 }
954 return result;
955 }
956
957 static intmax_t
958 exppower ()
959 {
960 register intmax_t val1, val2, c;
961
962 val1 = exp1 ();
963 while (curtok == POWER)
964 {
965 readtok ();
966 val2 = exppower (); /* exponentiation is right-associative */
967 lasttok = NUM;
968 if (val2 == 0)
969 return (1);
970 if (val2 < 0)
971 evalerror (_("exponent less than 0"));
972 val1 = ipow (val1, val2);
973 }
974 return (val1);
975 }
976
977 static intmax_t
978 exp1 ()
979 {
980 register intmax_t val;
981
982 if (curtok == NOT)
983 {
984 readtok ();
985 val = !exp1 ();
986 lasttok = NUM;
987 }
988 else if (curtok == BNOT)
989 {
990 readtok ();
991 val = ~exp1 ();
992 lasttok = NUM;
993 }
994 else if (curtok == MINUS)
995 {
996 readtok ();
997 val = - exp1 ();
998 lasttok = NUM;
999 }
1000 else if (curtok == PLUS)
1001 {
1002 readtok ();
1003 val = exp1 ();
1004 lasttok = NUM;
1005 }
1006 else
1007 val = exp0 ();
1008
1009 return (val);
1010 }
1011
1012 static intmax_t
1013 exp0 ()
1014 {
1015 register intmax_t val = 0, v2;
1016 char *vincdec;
1017 int stok;
1018 EXPR_CONTEXT ec;
1019
1020 /* XXX - might need additional logic here to decide whether or not
1021 pre-increment or pre-decrement is legal at this point. */
1022 if (curtok == PREINC || curtok == PREDEC)
1023 {
1024 stok = lasttok = curtok;
1025 readtok ();
1026 if (curtok != STR)
1027 /* readtok() catches this */
1028 evalerror (_("identifier expected after pre-increment or pre-decrement"));
1029
1030 v2 = tokval + ((stok == PREINC) ? 1 : -1);
1031 vincdec = itos (v2);
1032 if (noeval == 0)
1033 {
1034 #if defined (ARRAY_VARS)
1035 if (curlval.ind != -1)
1036 expr_bind_array_element (curlval.tokstr, curlval.ind, vincdec);
1037 else
1038 #endif
1039 if (tokstr)
1040 expr_bind_variable (tokstr, vincdec);
1041 }
1042 free (vincdec);
1043 val = v2;
1044
1045 curtok = NUM; /* make sure --x=7 is flagged as an error */
1046 readtok ();
1047 }
1048 else if (curtok == LPAR)
1049 {
1050 /* XXX - save curlval here? Or entire expression context? */
1051 readtok ();
1052 val = EXP_HIGHEST ();
1053
1054 if (curtok != RPAR) /* ( */
1055 evalerror (_("missing `)'"));
1056
1057 /* Skip over closing paren. */
1058 readtok ();
1059 }
1060 else if ((curtok == NUM) || (curtok == STR))
1061 {
1062 val = tokval;
1063 if (curtok == STR)
1064 {
1065 SAVETOK (&ec);
1066 tokstr = (char *)NULL; /* keep it from being freed */
1067 noeval = 1;
1068 readtok ();
1069 stok = curtok;
1070
1071 /* post-increment or post-decrement */
1072 if (stok == POSTINC || stok == POSTDEC)
1073 {
1074 /* restore certain portions of EC */
1075 tokstr = ec.tokstr;
1076 noeval = ec.noeval;
1077 curlval = ec.lval;
1078 lasttok = STR; /* ec.curtok */
1079
1080 v2 = val + ((stok == POSTINC) ? 1 : -1);
1081 vincdec = itos (v2);
1082 if (noeval == 0)
1083 {
1084 #if defined (ARRAY_VARS)
1085 if (curlval.ind != -1)
1086 expr_bind_array_element (curlval.tokstr, curlval.ind, vincdec);
1087 else
1088 #endif
1089 expr_bind_variable (tokstr, vincdec);
1090 }
1091 free (vincdec);
1092 curtok = NUM; /* make sure x++=7 is flagged as an error */
1093 }
1094 else
1095 {
1096 /* XXX - watch out for pointer aliasing issues here */
1097 if (stok == STR) /* free new tokstr before old one is restored */
1098 FREE (tokstr);
1099 RESTORETOK (&ec);
1100 }
1101 }
1102
1103 readtok ();
1104 }
1105 else
1106 evalerror (_("syntax error: operand expected"));
1107
1108 return (val);
1109 }
1110
1111 static void
1112 init_lvalue (lv)
1113 struct lvalue *lv;
1114 {
1115 lv->tokstr = 0;
1116 lv->tokvar = 0;
1117 lv->tokval = lv->ind = -1;
1118 }
1119
1120 static struct lvalue *
1121 alloc_lvalue ()
1122 {
1123 struct lvalue *lv;
1124
1125 lv = xmalloc (sizeof (struct lvalue));
1126 init_lvalue (lv);
1127 return (lv);
1128 }
1129
1130 static void
1131 free_lvalue (lv)
1132 struct lvalue *lv;
1133 {
1134 free (lv); /* should be inlined */
1135 }
1136
1137 static intmax_t
1138 expr_streval (tok, e, lvalue)
1139 char *tok;
1140 int e;
1141 struct lvalue *lvalue;
1142 {
1143 SHELL_VAR *v;
1144 char *value;
1145 intmax_t tval;
1146 int initial_depth;
1147 #if defined (ARRAY_VARS)
1148 arrayind_t ind;
1149 int tflag, aflag;
1150 #endif
1151
1152 /*itrace("expr_streval: %s: noeval = %d expanded=%d", tok, noeval, already_expanded);*/
1153 /* If we are suppressing evaluation, just short-circuit here instead of
1154 going through the rest of the evaluator. */
1155 if (noeval)
1156 return (0);
1157
1158 initial_depth = expr_depth;
1159
1160 #if defined (ARRAY_VARS)
1161 tflag = assoc_expand_once && already_expanded; /* for a start */
1162 #endif
1163
1164 /* [[[[[ */
1165 #if defined (ARRAY_VARS)
1166 aflag = (tflag) ? AV_NOEXPAND : 0;
1167 v = (e == ']') ? array_variable_part (tok, tflag, (char **)0, (int *)0) : find_variable (tok);
1168 #else
1169 v = find_variable (tok);
1170 #endif
1171
1172 if ((v == 0 || invisible_p (v)) && unbound_vars_is_error)
1173 {
1174 #if defined (ARRAY_VARS)
1175 value = (e == ']') ? array_variable_name (tok, tflag, (char **)0, (int *)0) : tok;
1176 #else
1177 value = tok;
1178 #endif
1179
1180 last_command_exit_value = EXECUTION_FAILURE;
1181 err_unboundvar (value);
1182
1183 #if defined (ARRAY_VARS)
1184 if (e == ']')
1185 FREE (value); /* array_variable_name returns new memory */
1186 #endif
1187
1188 if (no_longjmp_on_fatal_error && interactive_shell)
1189 sh_longjmp (evalbuf, 1);
1190
1191 if (interactive_shell)
1192 {
1193 expr_unwind ();
1194 top_level_cleanup ();
1195 jump_to_top_level (DISCARD);
1196 }
1197 else
1198 jump_to_top_level (FORCE_EOF);
1199 }
1200
1201 #if defined (ARRAY_VARS)
1202 ind = -1;
1203 /* If the second argument to get_array_value doesn't include AV_ALLOWALL,
1204 we don't allow references like array[@]. In this case, get_array_value
1205 is just like get_variable_value in that it does not return newly-allocated
1206 memory or quote the results. AFLAG is set above and is either AV_NOEXPAND
1207 or 0. */
1208 value = (e == ']') ? get_array_value (tok, aflag, (int *)NULL, &ind) : get_variable_value (v);
1209 #else
1210 value = get_variable_value (v);
1211 #endif
1212
1213 if (expr_depth < initial_depth)
1214 {
1215 if (no_longjmp_on_fatal_error && interactive_shell)
1216 sh_longjmp (evalbuf, 1);
1217 return (0);
1218 }
1219
1220 tval = (value && *value) ? subexpr (value) : 0;
1221
1222 if (lvalue)
1223 {
1224 lvalue->tokstr = tok; /* XXX */
1225 lvalue->tokval = tval;
1226 lvalue->tokvar = v; /* XXX */
1227 #if defined (ARRAY_VARS)
1228 lvalue->ind = ind;
1229 #else
1230 lvalue->ind = -1;
1231 #endif
1232 }
1233
1234 return (tval);
1235 }
1236
1237 static int
1238 _is_multiop (c)
1239 int c;
1240 {
1241 switch (c)
1242 {
1243 case EQEQ:
1244 case NEQ:
1245 case LEQ:
1246 case GEQ:
1247 case LAND:
1248 case LOR:
1249 case LSH:
1250 case RSH:
1251 case OP_ASSIGN:
1252 case COND:
1253 case POWER:
1254 case PREINC:
1255 case PREDEC:
1256 case POSTINC:
1257 case POSTDEC:
1258 return 1;
1259 default:
1260 return 0;
1261 }
1262 }
1263
1264 static int
1265 _is_arithop (c)
1266 int c;
1267 {
1268 switch (c)
1269 {
1270 case EQ:
1271 case GT:
1272 case LT:
1273 case PLUS:
1274 case MINUS:
1275 case MUL:
1276 case DIV:
1277 case MOD:
1278 case NOT:
1279 case LPAR:
1280 case RPAR:
1281 case BAND:
1282 case BOR:
1283 case BXOR:
1284 case BNOT:
1285 return 1; /* operator tokens */
1286 case QUES:
1287 case COL:
1288 case COMMA:
1289 return 1; /* questionable */
1290 default:
1291 return 0; /* anything else is invalid */
1292 }
1293 }
1294
1295 /* Lexical analyzer/token reader for the expression evaluator. Reads the
1296 next token and puts its value into curtok, while advancing past it.
1297 Updates value of tp. May also set tokval (for number) or tokstr (for
1298 string). */
1299 static void
1300 readtok ()
1301 {
1302 register char *cp, *xp;
1303 register unsigned char c, c1;
1304 register int e;
1305 struct lvalue lval;
1306
1307 /* Skip leading whitespace. */
1308 cp = tp;
1309 c = e = 0;
1310 while (cp && (c = *cp) && (cr_whitespace (c)))
1311 cp++;
1312
1313 if (c)
1314 cp++;
1315
1316 if (c == '\0')
1317 {
1318 lasttok = curtok;
1319 curtok = 0;
1320 tp = cp;
1321 return;
1322 }
1323 lasttp = tp = cp - 1;
1324
1325 if (legal_variable_starter (c))
1326 {
1327 /* variable names not preceded with a dollar sign are shell variables. */
1328 char *savecp;
1329 EXPR_CONTEXT ec;
1330 int peektok;
1331
1332 while (legal_variable_char (c))
1333 c = *cp++;
1334
1335 c = *--cp;
1336
1337 #if defined (ARRAY_VARS)
1338 if (c == '[')
1339 {
1340 e = expr_skipsubscript (tp, cp); /* XXX - was skipsubscript */
1341 if (cp[e] == ']')
1342 {
1343 cp += e + 1;
1344 c = *cp;
1345 e = ']';
1346 }
1347 else
1348 evalerror (bash_badsub_errmsg);
1349 }
1350 #endif /* ARRAY_VARS */
1351
1352 *cp = '\0';
1353 /* XXX - watch out for pointer aliasing issues here */
1354 if (curlval.tokstr && curlval.tokstr == tokstr)
1355 init_lvalue (&curlval);
1356
1357 FREE (tokstr);
1358 tokstr = savestring (tp);
1359 *cp = c;
1360
1361 /* XXX - make peektok part of saved token state? */
1362 SAVETOK (&ec);
1363 tokstr = (char *)NULL; /* keep it from being freed */
1364 tp = savecp = cp;
1365 noeval = 1;
1366 curtok = STR;
1367 readtok ();
1368 peektok = curtok;
1369 if (peektok == STR) /* free new tokstr before old one is restored */
1370 FREE (tokstr);
1371 RESTORETOK (&ec);
1372 cp = savecp;
1373
1374 /* The tests for PREINC and PREDEC aren't strictly correct, but they
1375 preserve old behavior if a construct like --x=9 is given. */
1376 if (lasttok == PREINC || lasttok == PREDEC || peektok != EQ)
1377 {
1378 lastlval = curlval;
1379 tokval = expr_streval (tokstr, e, &curlval);
1380 }
1381 else
1382 tokval = 0;
1383
1384 lasttok = curtok;
1385 curtok = STR;
1386 }
1387 else if (DIGIT(c))
1388 {
1389 while (ISALNUM (c) || c == '#' || c == '@' || c == '_')
1390 c = *cp++;
1391
1392 c = *--cp;
1393 *cp = '\0';
1394
1395 tokval = strlong (tp);
1396 *cp = c;
1397 lasttok = curtok;
1398 curtok = NUM;
1399 }
1400 else
1401 {
1402 c1 = *cp++;
1403 if ((c == EQ) && (c1 == EQ))
1404 c = EQEQ;
1405 else if ((c == NOT) && (c1 == EQ))
1406 c = NEQ;
1407 else if ((c == GT) && (c1 == EQ))
1408 c = GEQ;
1409 else if ((c == LT) && (c1 == EQ))
1410 c = LEQ;
1411 else if ((c == LT) && (c1 == LT))
1412 {
1413 if (*cp == '=') /* a <<= b */
1414 {
1415 assigntok = LSH;
1416 c = OP_ASSIGN;
1417 cp++;
1418 }
1419 else
1420 c = LSH;
1421 }
1422 else if ((c == GT) && (c1 == GT))
1423 {
1424 if (*cp == '=')
1425 {
1426 assigntok = RSH; /* a >>= b */
1427 c = OP_ASSIGN;
1428 cp++;
1429 }
1430 else
1431 c = RSH;
1432 }
1433 else if ((c == BAND) && (c1 == BAND))
1434 c = LAND;
1435 else if ((c == BOR) && (c1 == BOR))
1436 c = LOR;
1437 else if ((c == '*') && (c1 == '*'))
1438 c = POWER;
1439 else if ((c == '-' || c == '+') && c1 == c && curtok == STR)
1440 c = (c == '-') ? POSTDEC : POSTINC;
1441 else if ((c == '-' || c == '+') && c1 == c && curtok == NUM && (lasttok == PREINC || lasttok == PREDEC))
1442 {
1443 /* This catches something like --FOO++ */
1444 if (c == '-')
1445 evalerror ("--: assignment requires lvalue");
1446 else
1447 evalerror ("++: assignment requires lvalue");
1448 }
1449 else if ((c == '-' || c == '+') && c1 == c)
1450 {
1451 /* Quickly scan forward to see if this is followed by optional
1452 whitespace and an identifier. */
1453 xp = cp;
1454 while (xp && *xp && cr_whitespace (*xp))
1455 xp++;
1456 if (legal_variable_starter ((unsigned char)*xp))
1457 c = (c == '-') ? PREDEC : PREINC;
1458 else
1459 /* Could force parsing as preinc or predec and throw an error */
1460 #if 0
1461 {
1462 /* Posix says unary plus and minus have higher priority than
1463 preinc and predec. */
1464 /* This catches something like --4++ */
1465 if (c == '-')
1466 evalerror ("--: assignment requires lvalue");
1467 else
1468 evalerror ("++: assignment requires lvalue");
1469 }
1470 #else
1471 cp--; /* not preinc or predec, so unget the character */
1472 #endif
1473 }
1474 else if (c1 == EQ && member (c, "*/%+-&^|"))
1475 {
1476 assigntok = c; /* a OP= b */
1477 c = OP_ASSIGN;
1478 }
1479 else if (_is_arithop (c) == 0)
1480 {
1481 cp--;
1482 /* use curtok, since it hasn't been copied to lasttok yet */
1483 if (curtok == 0 || _is_arithop (curtok) || _is_multiop (curtok))
1484 evalerror (_("syntax error: operand expected"));
1485 else
1486 evalerror (_("syntax error: invalid arithmetic operator"));
1487 }
1488 else
1489 cp--; /* `unget' the character */
1490
1491 /* Should check here to make sure that the current character is one
1492 of the recognized operators and flag an error if not. Could create
1493 a character map the first time through and check it on subsequent
1494 calls. */
1495 lasttok = curtok;
1496 curtok = c;
1497 }
1498 tp = cp;
1499 }
1500
1501 static void
1502 evalerror (msg)
1503 const char *msg;
1504 {
1505 char *name, *t;
1506
1507 name = this_command_name;
1508 for (t = expression; t && whitespace (*t); t++)
1509 ;
1510 internal_error (_("%s%s%s: %s (error token is \"%s\")"),
1511 name ? name : "", name ? ": " : "",
1512 t ? t : "", msg, (lasttp && *lasttp) ? lasttp : "");
1513 sh_longjmp (evalbuf, 1);
1514 }
1515
1516 /* Convert a string to an intmax_t integer, with an arbitrary base.
1517 0nnn -> base 8
1518 0[Xx]nn -> base 16
1519 Anything else: [base#]number (this is implemented to match ksh93)
1520
1521 Base may be >=2 and <=64. If base is <= 36, the numbers are drawn
1522 from [0-9][a-zA-Z], and lowercase and uppercase letters may be used
1523 interchangably. If base is > 36 and <= 64, the numbers are drawn
1524 from [0-9][a-z][A-Z]_@ (a = 10, z = 35, A = 36, Z = 61, @ = 62, _ = 63 --
1525 you get the picture). */
1526
1527 static intmax_t
1528 strlong (num)
1529 char *num;
1530 {
1531 register char *s;
1532 register unsigned char c;
1533 int base, foundbase;
1534 intmax_t val;
1535
1536 s = num;
1537
1538 base = 10;
1539 foundbase = 0;
1540 if (*s == '0')
1541 {
1542 s++;
1543
1544 if (*s == '\0')
1545 return 0;
1546
1547 /* Base 16? */
1548 if (*s == 'x' || *s == 'X')
1549 {
1550 base = 16;
1551 s++;
1552 }
1553 else
1554 base = 8;
1555 foundbase++;
1556 }
1557
1558 val = 0;
1559 for (c = *s++; c; c = *s++)
1560 {
1561 if (c == '#')
1562 {
1563 if (foundbase)
1564 evalerror (_("invalid number"));
1565
1566 /* Illegal base specifications raise an evaluation error. */
1567 if (val < 2 || val > 64)
1568 evalerror (_("invalid arithmetic base"));
1569
1570 base = val;
1571 val = 0;
1572 foundbase++;
1573 }
1574 else if (ISALNUM(c) || (c == '_') || (c == '@'))
1575 {
1576 if (DIGIT(c))
1577 c = TODIGIT(c);
1578 else if (c >= 'a' && c <= 'z')
1579 c -= 'a' - 10;
1580 else if (c >= 'A' && c <= 'Z')
1581 c -= 'A' - ((base <= 36) ? 10 : 36);
1582 else if (c == '@')
1583 c = 62;
1584 else if (c == '_')
1585 c = 63;
1586
1587 if (c >= base)
1588 evalerror (_("value too great for base"));
1589
1590 val = (val * base) + c;
1591 }
1592 else
1593 break;
1594 }
1595
1596 return (val);
1597 }
1598
1599 #if defined (EXPR_TEST)
1600 void *
1601 xmalloc (n)
1602 int n;
1603 {
1604 return (malloc (n));
1605 }
1606
1607 void *
1608 xrealloc (s, n)
1609 char *s;
1610 int n;
1611 {
1612 return (realloc (s, n));
1613 }
1614
1615 SHELL_VAR *find_variable () { return 0;}
1616 SHELL_VAR *bind_variable () { return 0; }
1617
1618 char *get_string_value () { return 0; }
1619
1620 procenv_t top_level;
1621
1622 main (argc, argv)
1623 int argc;
1624 char **argv;
1625 {
1626 register int i;
1627 intmax_t v;
1628 int expok;
1629
1630 if (setjmp (top_level))
1631 exit (0);
1632
1633 for (i = 1; i < argc; i++)
1634 {
1635 v = evalexp (argv[i], 0, &expok);
1636 if (expok == 0)
1637 fprintf (stderr, _("%s: expression error\n"), argv[i]);
1638 else
1639 printf ("'%s' -> %ld\n", argv[i], v);
1640 }
1641 exit (0);
1642 }
1643
1644 int
1645 builtin_error (format, arg1, arg2, arg3, arg4, arg5)
1646 char *format;
1647 {
1648 fprintf (stderr, "expr: ");
1649 fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
1650 fprintf (stderr, "\n");
1651 return 0;
1652 }
1653
1654 char *
1655 itos (n)
1656 intmax_t n;
1657 {
1658 return ("42");
1659 }
1660
1661 #endif /* EXPR_TEST */