1 /* expr.c -- arithmetic expression evaluation. */
3 /* Copyright (C) 1990-2015 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
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.
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.
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/>.
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).
25 The following operators are handled, grouped into a set of levels in
26 order of decreasing precedence.
28 "id++", "id--" [post-increment and post-decrement]
29 "-", "+" [(unary operators)]
30 "++id", "--id" [pre-increment and pre-decrement]
32 "**" [(exponentiation)]
44 "=", "*=", "/=", "%=", "+=", "-=", "<<=", ">>=", "&=", "^=", "|="
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.)
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).
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.
63 Implementation is a recursive-descent parser.
74 #if defined (HAVE_UNISTD_H)
76 # include <sys/types.h>
81 #include "chartypes.h"
85 #include "arrayfunc.h"
86 #include "execute_cmd.h"
89 #include "typemax.h" /* INTMAX_MAX, INTMAX_MIN */
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'))
95 /* Size be which the expression stack grows when necessary. */
96 #define EXPR_STACK_GROW_SIZE 10
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
103 /* The Tokens. Singing "The Lion Sleeps Tonight". */
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-- */
133 #define BAND '&' /* Bitwise AND */
134 #define BOR '|' /* Bitwise OR. */
135 #define BXOR '^' /* Bitwise eXclusive OR. */
136 #define BNOT '~' /* Bitwise NOT; Two's complement. */
141 /* This should be the function corresponding to the operator with the
142 highest precedence. */
143 #define EXP_HIGHEST expcomma
146 # define MAX_INT_LEN 32
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 */
157 /* A structure defining a single expression context. */
160 char *expression
, *tp
, *lasttp
;
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
;
178 /* set to 1 if the expression has already been run through word expansion */
179 static int already_expanded
;
181 static struct lvalue curlval
= {0, 0, 0, -1};
182 static struct lvalue lastlval
= {0, 0, 0, -1};
184 static int _is_arithop
__P((int));
185 static void readtok
__P((void)); /* lexical analyzer */
187 static void init_lvalue
__P((struct lvalue
*));
188 static struct lvalue
*alloc_lvalue
__P((void));
189 static void free_lvalue
__P((struct lvalue
*));
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 *));
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 *));
203 static intmax_t subexpr
__P((char *));
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));
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. */
227 #if defined (ARRAY_VARS)
228 extern const char * const bash_badsub_errmsg
;
233 (X)->curtok = curtok; \
234 (X)->lasttok = lasttok; \
236 (X)->lasttp = lasttp; \
237 (X)->tokval = tokval; \
238 (X)->tokstr = tokstr; \
239 (X)->noeval = noeval; \
240 (X)->lval = curlval; \
243 #define RESTORETOK(X) \
245 curtok = (X)->curtok; \
246 lasttok = (X)->lasttok; \
248 lasttp = (X)->lasttp; \
249 tokval = (X)->tokval; \
250 tokstr = (X)->tokstr; \
251 noeval = (X)->noeval; \
252 curlval = (X)->lval; \
255 /* Push and save away the contents of the globals describing the
256 current expression context. */
260 EXPR_CONTEXT
*context
;
262 if (expr_depth
>= MAX_EXPR_RECURSION_LEVEL
)
263 evalerror (_("expression recursion level exceeded"));
265 if (expr_depth
>= expr_stack_size
)
267 expr_stack_size
+= EXPR_STACK_GROW_SIZE
;
268 expr_stack
= (EXPR_CONTEXT
**)xrealloc (expr_stack
, expr_stack_size
* sizeof (EXPR_CONTEXT
*));
271 context
= (EXPR_CONTEXT
*)xmalloc (sizeof (EXPR_CONTEXT
));
273 context
->expression
= expression
;
276 expr_stack
[expr_depth
++] = context
;
279 /* Pop the the contents of the expression context stack into the
280 globals describing the current expression context. */
284 EXPR_CONTEXT
*context
;
288 /* See the comment at the top of evalexp() for an explanation of why
290 expression
= lasttp
= 0;
291 evalerror (_("recursion stack underflow"));
294 context
= expr_stack
[--expr_depth
];
296 expression
= context
->expression
;
297 RESTORETOK (context
);
305 while (--expr_depth
> 0)
307 if (expr_stack
[expr_depth
]->tokstr
)
308 free (expr_stack
[expr_depth
]->tokstr
);
310 if (expr_stack
[expr_depth
]->expression
)
311 free (expr_stack
[expr_depth
]->expression
);
313 free (expr_stack
[expr_depth
]);
316 free (expr_stack
[expr_depth
]); /* free the allocated EXPR_CONTEXT */
318 noeval
= 0; /* XXX */
322 expr_bind_variable (lhs
, rhs
)
328 if (lhs
== 0 || *lhs
== 0)
331 #if defined (ARRAY_VARS)
332 aflags
= (assoc_expand_once
&& already_expanded
) ? ASS_NOEXPAND
: 0;
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
);
342 #if defined (ARRAY_VARS)
343 /* This is similar to the logic in arrayfunc.c:valid_array_subscript when
344 you pass VA_NOEXPAND. */
346 expr_skipsubscript (vp
, cp
)
354 if (assoc_expand_once
& already_expanded
)
357 isassoc
= legal_identifier (vp
) && (entry
= find_variable (vp
)) && assoc_p (entry
);
360 flags
= (isassoc
&& assoc_expand_once
&& already_expanded
) ? VA_NOEXPAND
: 0;
361 return (skipsubscript (cp
, 0, flags
));
364 /* Rewrite tok, which is of the form vname[expression], to vname[ind], where
365 IND is the already-calculated value of expression. */
367 expr_bind_array_element (tok
, ind
, rhs
)
374 char ibuf
[INT_STRLEN_BOUND (arrayind_t
) + 1], *istr
;
376 istr
= fmtumax (ind
, 10, ibuf
, sizeof (ibuf
), 0);
377 vname
= array_variable_name (tok
, 0, (char **)NULL
, (int *)NULL
);
379 llen
= strlen (vname
) + sizeof (ibuf
) + 3;
380 lhs
= xmalloc (llen
);
382 sprintf (lhs
, "%s[%s]", vname
, istr
); /* XXX */
384 /*itrace("expr_bind_array_element: %s=%s", lhs, rhs);*/
385 expr_bind_variable (lhs
, rhs
);
389 #endif /* ARRAY_VARS */
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
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. */
405 evalexp (expr
, flags
, validp
)
416 already_expanded
= (flags
&EXP_EXPANDED
);
418 FASTCOPY (evalbuf
, oevalbuf
, sizeof (evalbuf
));
420 c
= setjmp_nosigs (evalbuf
);
426 tokstr
= expression
= (char *)NULL
;
429 expr_depth
= 0; /* XXX - make sure */
431 /* We copy in case we've called evalexp recursively */
432 FASTCOPY (oevalbuf
, evalbuf
, sizeof (evalbuf
));
439 val
= subexpr (expr
);
444 FASTCOPY (oevalbuf
, evalbuf
, sizeof (evalbuf
));
456 for (p
= expr
; p
&& *p
&& cr_whitespace (*p
); p
++)
459 if (p
== NULL
|| *p
== '\0')
463 expression
= savestring (expr
);
466 curtok
= lasttok
= 0;
467 tokstr
= (char *)NULL
;
469 init_lvalue (&curlval
);
474 val
= EXP_HIGHEST ();
477 evalerror (_("syntax error in expression"));
490 register intmax_t value
;
492 value
= expassign ();
493 while (curtok
== COMMA
)
496 value
= expassign ();
505 register intmax_t value
;
508 #if defined (HAVE_IMAXDIV)
513 if (curtok
== EQ
|| curtok
== OP_ASSIGN
)
518 special
= curtok
== OP_ASSIGN
;
521 evalerror (_("attempted assignment to non-variable"));
525 op
= assigntok
; /* a OP= b */
530 evalerror (_("syntax error in variable assignment"));
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 */
537 value
= expassign ();
541 if ((op
== DIV
|| op
== MOD
) && value
== 0)
544 evalerror (_("division by 0"));
556 if (lvalue
== INTMAX_MIN
&& value
== -1)
557 lvalue
= (op
== DIV
) ? INTMAX_MIN
: 0;
561 idiv
= imaxdiv (lvalue
, value
);
562 lvalue
= (op
== DIV
) ? idiv
.quot
: idiv
.rem
;
565 lvalue
= (op
== DIV
) ? lvalue
/ value
: lvalue
% value
;
591 evalerror (_("bug: bad expassign token"));
600 #if defined (ARRAY_VARS)
602 expr_bind_array_element (lhs
, lind
, rhs
);
605 expr_bind_variable (lhs
, rhs
);
607 if (curlval
.tokstr
&& curlval
.tokstr
== tokstr
)
608 init_lvalue (&curlval
);
613 tokstr
= (char *)NULL
; /* For freeing on errors. */
619 /* Conditional expression (expr?expr:expr) */
623 intmax_t cval
, val1
, val2
, rval
;
627 rval
= cval
= explor ();
628 if (curtok
== QUES
) /* found conditional expr */
637 if (curtok
== 0 || curtok
== COL
)
638 evalerror (_("expression expected"));
640 val1
= EXP_HIGHEST ();
645 evalerror (_("`:' expected for conditional expression"));
656 evalerror (_("expression expected"));
661 rval
= cval
? val1
: val2
;
671 register intmax_t val1
, val2
;
676 while (curtok
== LOR
)
699 register intmax_t val1
, val2
;
704 while (curtok
== LAND
)
727 register intmax_t val1
, val2
;
731 while (curtok
== BOR
)
746 register intmax_t val1
, val2
;
750 while (curtok
== BXOR
)
765 register intmax_t val1
, val2
;
769 while (curtok
== BAND
)
783 register intmax_t val1
, val2
;
787 while ((curtok
== EQEQ
) || (curtok
== NEQ
))
794 val1
= (val1
== val2
);
796 val1
= (val1
!= val2
);
805 register intmax_t val1
, val2
;
808 while ((curtok
== LEQ
) ||
824 else /* (op == GT) */
831 /* Left and right shifts. */
835 register intmax_t val1
, val2
;
839 while ((curtok
== LSH
) || (curtok
== RSH
))
859 register intmax_t val1
, val2
;
863 while ((curtok
== PLUS
) || (curtok
== MINUS
))
872 else if (op
== MINUS
)
882 register intmax_t val1
, val2
;
883 #if defined (HAVE_IMAXDIV)
889 while ((curtok
== MUL
) ||
901 /* Handle division by 0 and twos-complement arithmetic overflow */
902 if (((op
== DIV
) || (op
== MOD
)) && (val2
== 0))
908 while (lasttp
&& *lasttp
&& whitespace (*lasttp
))
910 evalerror (_("division by 0"));
916 else if (op
== MOD
&& val1
== INTMAX_MIN
&& val2
== -1)
921 else if (op
== DIV
&& val1
== INTMAX_MIN
&& val2
== -1)
926 else if (op
== DIV
|| op
== MOD
)
927 #if defined (HAVE_IMAXDIV)
929 idiv
= imaxdiv (val1
, val2
);
930 val1
= (op
== DIV
) ? idiv
.quot
: idiv
.rem
;
933 val1
= (op
== DIV
) ? val1
/ val2
: val1
% val2
;
960 register intmax_t val1
, val2
, c
;
963 while (curtok
== POWER
)
966 val2
= exppower (); /* exponentiation is right-associative */
971 evalerror (_("exponent less than 0"));
972 val1
= ipow (val1
, val2
);
980 register intmax_t val
;
988 else if (curtok
== BNOT
)
994 else if (curtok
== MINUS
)
1000 else if (curtok
== PLUS
)
1015 register intmax_t val
= 0, v2
;
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
)
1024 stok
= lasttok
= curtok
;
1027 /* readtok() catches this */
1028 evalerror (_("identifier expected after pre-increment or pre-decrement"));
1030 v2
= tokval
+ ((stok
== PREINC
) ? 1 : -1);
1031 vincdec
= itos (v2
);
1034 #if defined (ARRAY_VARS)
1035 if (curlval
.ind
!= -1)
1036 expr_bind_array_element (curlval
.tokstr
, curlval
.ind
, vincdec
);
1040 expr_bind_variable (tokstr
, vincdec
);
1045 curtok
= NUM
; /* make sure --x=7 is flagged as an error */
1048 else if (curtok
== LPAR
)
1050 /* XXX - save curlval here? Or entire expression context? */
1052 val
= EXP_HIGHEST ();
1054 if (curtok
!= RPAR
) /* ( */
1055 evalerror (_("missing `)'"));
1057 /* Skip over closing paren. */
1060 else if ((curtok
== NUM
) || (curtok
== STR
))
1066 tokstr
= (char *)NULL
; /* keep it from being freed */
1071 /* post-increment or post-decrement */
1072 if (stok
== POSTINC
|| stok
== POSTDEC
)
1074 /* restore certain portions of EC */
1078 lasttok
= STR
; /* ec.curtok */
1080 v2
= val
+ ((stok
== POSTINC
) ? 1 : -1);
1081 vincdec
= itos (v2
);
1084 #if defined (ARRAY_VARS)
1085 if (curlval
.ind
!= -1)
1086 expr_bind_array_element (curlval
.tokstr
, curlval
.ind
, vincdec
);
1089 expr_bind_variable (tokstr
, vincdec
);
1092 curtok
= NUM
; /* make sure x++=7 is flagged as an error */
1096 /* XXX - watch out for pointer aliasing issues here */
1097 if (stok
== STR
) /* free new tokstr before old one is restored */
1106 evalerror (_("syntax error: operand expected"));
1117 lv
->tokval
= lv
->ind
= -1;
1120 static struct lvalue
*
1125 lv
= xmalloc (sizeof (struct lvalue
));
1134 free (lv
); /* should be inlined */
1138 expr_streval (tok
, e
, lvalue
)
1141 struct lvalue
*lvalue
;
1147 #if defined (ARRAY_VARS)
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. */
1158 initial_depth
= expr_depth
;
1160 #if defined (ARRAY_VARS)
1161 tflag
= assoc_expand_once
&& already_expanded
; /* for a start */
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
);
1169 v
= find_variable (tok
);
1172 if ((v
== 0 || invisible_p (v
)) && unbound_vars_is_error
)
1174 #if defined (ARRAY_VARS)
1175 value
= (e
== ']') ? array_variable_name (tok
, tflag
, (char **)0, (int *)0) : tok
;
1180 last_command_exit_value
= EXECUTION_FAILURE
;
1181 err_unboundvar (value
);
1183 #if defined (ARRAY_VARS)
1185 FREE (value
); /* array_variable_name returns new memory */
1188 if (no_longjmp_on_fatal_error
&& interactive_shell
)
1189 sh_longjmp (evalbuf
, 1);
1191 if (interactive_shell
)
1194 top_level_cleanup ();
1195 jump_to_top_level (DISCARD
);
1198 jump_to_top_level (FORCE_EOF
);
1201 #if defined (ARRAY_VARS)
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
1208 value
= (e
== ']') ? get_array_value (tok
, aflag
, (int *)NULL
, &ind
) : get_variable_value (v
);
1210 value
= get_variable_value (v
);
1213 if (expr_depth
< initial_depth
)
1215 if (no_longjmp_on_fatal_error
&& interactive_shell
)
1216 sh_longjmp (evalbuf
, 1);
1220 tval
= (value
&& *value
) ? subexpr (value
) : 0;
1224 lvalue
->tokstr
= tok
; /* XXX */
1225 lvalue
->tokval
= tval
;
1226 lvalue
->tokvar
= v
; /* XXX */
1227 #if defined (ARRAY_VARS)
1285 return 1; /* operator tokens */
1289 return 1; /* questionable */
1291 return 0; /* anything else is invalid */
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
1302 register char *cp
, *xp
;
1303 register unsigned char c
, c1
;
1307 /* Skip leading whitespace. */
1310 while (cp
&& (c
= *cp
) && (cr_whitespace (c
)))
1323 lasttp
= tp
= cp
- 1;
1325 if (legal_variable_starter (c
))
1327 /* variable names not preceded with a dollar sign are shell variables. */
1332 while (legal_variable_char (c
))
1337 #if defined (ARRAY_VARS)
1340 e
= expr_skipsubscript (tp
, cp
); /* XXX - was skipsubscript */
1348 evalerror (bash_badsub_errmsg
);
1350 #endif /* ARRAY_VARS */
1353 /* XXX - watch out for pointer aliasing issues here */
1354 if (curlval
.tokstr
&& curlval
.tokstr
== tokstr
)
1355 init_lvalue (&curlval
);
1358 tokstr
= savestring (tp
);
1361 /* XXX - make peektok part of saved token state? */
1363 tokstr
= (char *)NULL
; /* keep it from being freed */
1369 if (peektok
== STR
) /* free new tokstr before old one is restored */
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
)
1379 tokval
= expr_streval (tokstr
, e
, &curlval
);
1389 while (ISALNUM (c
) || c
== '#' || c
== '@' || c
== '_')
1395 tokval
= strlong (tp
);
1403 if ((c
== EQ
) && (c1
== EQ
))
1405 else if ((c
== NOT
) && (c1
== EQ
))
1407 else if ((c
== GT
) && (c1
== EQ
))
1409 else if ((c
== LT
) && (c1
== EQ
))
1411 else if ((c
== LT
) && (c1
== LT
))
1413 if (*cp
== '=') /* a <<= b */
1422 else if ((c
== GT
) && (c1
== GT
))
1426 assigntok
= RSH
; /* a >>= b */
1433 else if ((c
== BAND
) && (c1
== BAND
))
1435 else if ((c
== BOR
) && (c1
== BOR
))
1437 else if ((c
== '*') && (c1
== '*'))
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
))
1443 /* This catches something like --FOO++ */
1445 evalerror ("--: assignment requires lvalue");
1447 evalerror ("++: assignment requires lvalue");
1449 else if ((c
== '-' || c
== '+') && c1
== c
)
1451 /* Quickly scan forward to see if this is followed by optional
1452 whitespace and an identifier. */
1454 while (xp
&& *xp
&& cr_whitespace (*xp
))
1456 if (legal_variable_starter ((unsigned char)*xp
))
1457 c
= (c
== '-') ? PREDEC
: PREINC
;
1459 /* Could force parsing as preinc or predec and throw an error */
1462 /* Posix says unary plus and minus have higher priority than
1463 preinc and predec. */
1464 /* This catches something like --4++ */
1466 evalerror ("--: assignment requires lvalue");
1468 evalerror ("++: assignment requires lvalue");
1471 cp
--; /* not preinc or predec, so unget the character */
1474 else if (c1
== EQ
&& member (c
, "*/%+-&^|"))
1476 assigntok
= c
; /* a OP= b */
1479 else if (_is_arithop (c
) == 0)
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"));
1486 evalerror (_("syntax error: invalid arithmetic operator"));
1489 cp
--; /* `unget' the character */
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
1507 name
= this_command_name
;
1508 for (t
= expression
; t
&& whitespace (*t
); t
++)
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);
1516 /* Convert a string to an intmax_t integer, with an arbitrary base.
1519 Anything else: [base#]number (this is implemented to match ksh93)
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). */
1532 register unsigned char c
;
1533 int base
, foundbase
;
1548 if (*s
== 'x' || *s
== 'X')
1559 for (c
= *s
++; c
; c
= *s
++)
1564 evalerror (_("invalid number"));
1566 /* Illegal base specifications raise an evaluation error. */
1567 if (val
< 2 || val
> 64)
1568 evalerror (_("invalid arithmetic base"));
1574 else if (ISALNUM(c
) || (c
== '_') || (c
== '@'))
1578 else if (c
>= 'a' && c
<= 'z')
1580 else if (c
>= 'A' && c
<= 'Z')
1581 c
-= 'A' - ((base
<= 36) ? 10 : 36);
1588 evalerror (_("value too great for base"));
1590 val
= (val
* base
) + c
;
1599 #if defined (EXPR_TEST)
1604 return (malloc (n
));
1612 return (realloc (s
, n
));
1615 SHELL_VAR
*find_variable () { return 0;}
1616 SHELL_VAR
*bind_variable () { return 0; }
1618 char *get_string_value () { return 0; }
1620 procenv_t top_level
;
1630 if (setjmp (top_level
))
1633 for (i
= 1; i
< argc
; i
++)
1635 v
= evalexp (argv
[i
], 0, &expok
);
1637 fprintf (stderr
, _("%s: expression error\n"), argv
[i
]);
1639 printf ("'%s' -> %ld\n", argv
[i
], v
);
1645 builtin_error (format
, arg1
, arg2
, arg3
, arg4
, arg5
)
1648 fprintf (stderr
, "expr: ");
1649 fprintf (stderr
, format
, arg1
, arg2
, arg3
, arg4
, arg5
);
1650 fprintf (stderr
, "\n");
1661 #endif /* EXPR_TEST */