forked from bminor/bash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpr.cc
1355 lines (1165 loc) · 32.9 KB
/
expr.cc
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
30
31
32
33
34
35
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* expr.cc -- arithmetic expression evaluation. */
/* Copyright (C) 1990-2021 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
Bash is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Bash is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Bash. If not, see <http://www.gnu.org/licenses/>.
*/
/*
All arithmetic is done as int64_t integers with no checking for overflow
(though division by 0 is caught and flagged as an error).
The following operators are handled, grouped into a set of levels in
order of decreasing precedence.
"id++", "id--" [post-increment and post-decrement]
"-", "+" [(unary operators)]
"++id", "--id" [pre-increment and pre-decrement]
"!", "~"
"**" [(exponentiation)]
"*", "/", "%"
"+", "-"
"<<", ">>"
"<=", ">=", "<", ">"
"==", "!="
"&"
"^"
"|"
"&&"
"||"
"expr ? expr : expr"
"=", "*=", "/=", "%=", "+=", "-=", "<<=", ">>=", "&=", "^=", "|="
, [comma]
(Note that most of these operators have special meaning to bash, and an
entire expression should be quoted, e.g. "a=$a+1" or "a=a+1" to ensure
that it is passed intact to the evaluator when using `let'. When using
the $[] or $(( )) forms, the text between the `[' and `]' or `((' and `))'
is treated as if in double quotes.)
Sub-expressions within parentheses have a precedence level greater than
all of the above levels and are evaluated first. Within a single prece-
dence group, evaluation is left-to-right, except for the arithmetic
assignment operator (`='), which is evaluated right-to-left (as in C).
The expression evaluator returns the value of the expression (assignment
statements have as a value what is returned by the RHS). The `let'
builtin, on the other hand, returns 0 if the last expression evaluates to
a non-zero, and 1 otherwise.
Implementation is a recursive-descent parser.
Chet Ramey
*/
#include "config.h"
#include "shell.hh"
namespace bash
{
/* Because of the $((...)) construct, expressions may include newlines.
Here is a macro which accepts newlines, tabs and spaces as whitespace. */
#define cr_whitespace(c) (whitespace (c) || ((c) == '\n'))
/* Maximum amount of recursion allowed. This prevents a non-integer
variable such as "num=num+2" from infinitely adding to itself when
"let num=num+2" is given. */
#define MAX_EXPR_RECURSION_LEVEL 1024
/* This should be the function corresponding to the operator with the
lowest precedence. */
#define EXP_LOWEST expcomma
// Standard error message to use when encountering an invalid array subscript
extern const char *bash_badsub_errmsg;
static int64_t ipow (int64_t base, int64_t exp);
/* Push and save away the contents of the globals describing the
current expression context. */
void
Shell::pushexp ()
{
if (expr_stack.size () >= MAX_EXPR_RECURSION_LEVEL)
evalerror (_ ("expression recursion level exceeded"));
EXPR_CONTEXT *context = new EXPR_CONTEXT (this_expr);
expr_stack.push_back (context);
}
/* Pop the the contents of the expression context stack into the
globals describing the current expression context. */
void
Shell::popexp ()
{
if (expr_stack.empty ())
{
/* See the comment at the top of evalexp() for an explanation of why
this is done. */
this_expr.expression = nullptr;
this_expr.lasttp = nullptr;
evalerror (_ ("recursion stack underflow"));
}
this_expr = *(expr_stack.back ());
delete expr_stack.back ();
expr_stack.pop_back ();
}
void
Shell::expr_unwind ()
{
while (!expr_stack.empty ())
{
EXPR_CONTEXT *context = expr_stack.back ();
delete context;
}
this_expr.noeval = false; /* XXX */
}
void
Shell::expr_bind_variable (const string &lhs, const string &rhs)
{
SHELL_VAR *v;
assign_flags aflags;
if (lhs.empty ())
return; /* XXX */
#if defined(ARRAY_VARS)
aflags
= (assoc_expand_once && already_expanded) ? ASS_NOEXPAND : ASS_NOFLAGS;
aflags |= ASS_ALLOWALLSUB; // allow assoc[@]=value
#else
aflags = ASS_NOFLAGS;
#endif
v = bind_int_variable (lhs, rhs, aflags);
if (v && (v->readonly () || v->noassign ()))
throw bash_exception (FORCE_EOF); /* variable assignment error */
stupidly_hack_special_variables (lhs);
}
#if defined(ARRAY_VARS)
/* This is similar to the logic in arrayfunc.cc:valid_array_reference when
you pass VA_NOEXPAND. */
size_t
Shell::expr_skipsubscript (const char *vp, char *cp)
{
bool isassoc = false;
SHELL_VAR *entry = nullptr;
if (assoc_expand_once & already_expanded)
{
*cp = '\0';
isassoc = legal_identifier (vp) && (entry = find_variable (vp))
&& entry->assoc ();
*cp = '[';
}
int flags
= (isassoc && assoc_expand_once && already_expanded) ? VA_NOEXPAND : 0;
return skipsubscript (cp, 0, flags);
}
/* Rewrite tok, which is of the form vname[expression], to vname[ind], where
IND is the already-calculated value of expression. */
void
Shell::expr_bind_array_element (const string &tok, arrayind_t ind,
const string &rhs)
{
string istr = fmtumax (static_cast<uint64_t> (ind), 10);
string *vname = array_variable_name (tok, 0, nullptr);
if (vname == nullptr)
return;
string lhs (*vname);
delete vname;
lhs.push_back ('[');
lhs += istr;
lhs.push_back (']');
/*itrace("expr_bind_array_element: %s=%s", lhs, rhs);*/
expr_bind_variable (lhs, rhs);
}
#endif /* ARRAY_VARS */
/* Evaluate EXPR, and return the arithmetic result. If VALIDP is
non-null, a zero is stored into the location to which it points
if the expression is invalid, non-zero otherwise. If a non-zero
value is returned in *VALIDP, the return value of evalexp() may
be used.
The `while' loop after the exception is caught relies on the above
implementation of pushexp and popexp leaving in expr_stack[0] the
values that the variables had when the program started. That is,
the first things saved are the initial values of the variables that
were assigned at program startup or by the compiler. Therefore, it is
safe to let the loop terminate when expr_depth == 0, without freeing up
any of the expr_depth[0] stuff. */
int64_t
Shell::evalexp (const string &expr, eval_flags flags, bool *validp)
{
try
{
this_expr.noeval = 0;
already_expanded = (flags & EXP_EXPANDED);
int64_t val = subexpr (expr);
if (validp)
*validp = true;
return val;
}
catch (const eval_exception &)
{
delete[] this_expr.tokstr;
delete[] this_expr.expression;
this_expr.tokstr = nullptr;
this_expr.expression = nullptr;
expr_unwind ();
if (validp)
*validp = false;
return 0;
}
}
int64_t
Shell::subexpr (const string &expr)
{
string::const_iterator it;
for (it = expr.begin (); it != expr.end () && cr_whitespace (*it); ++it)
;
if (it == expr.end ())
return 0;
pushexp ();
this_expr.expression = savestring (expr);
this_expr.tp = this_expr.expression;
this_expr.curtok = this_expr.lasttok = NONE;
this_expr.tokstr = nullptr;
this_expr.tokval = 0;
this_expr.lval.init ();
lastlval = this_expr.lval;
readtok ();
int64_t val = EXP_LOWEST ();
/*TAG:bash-5.3 make it clear that these are arithmetic syntax errors */
if (this_expr.curtok != NONE)
evalerror (_ ("syntax error in expression"));
delete[] this_expr.tokstr;
delete[] this_expr.expression;
popexp ();
return val;
}
int64_t
Shell::expcomma ()
{
int64_t value;
value = expassign ();
while (this_expr.curtok == COMMA)
{
readtok ();
value = expassign ();
}
return value;
}
int64_t
Shell::expassign ()
{
int64_t value;
arrayind_t lind;
value = expcond ();
if (this_expr.curtok == EQ || this_expr.curtok == OP_ASSIGN)
{
token_t op = NONE;
int64_t lvalue = 0;
bool special = (this_expr.curtok == OP_ASSIGN);
if (this_expr.lasttok != STR)
evalerror (_ ("attempted assignment to non-variable"));
if (special)
{
op = assigntok; /* a OP= b */
lvalue = value;
}
if (this_expr.tokstr == nullptr)
evalerror (_ ("syntax error in variable assignment"));
string lhs = this_expr.tokstr;
/* save ind in case rhs is string var and evaluation overwrites it */
lind = this_expr.lval.ind;
readtok ();
value = expassign ();
if (special)
{
if ((op == DIV || op == MOD) && value == 0)
{
if (this_expr.noeval == 0)
evalerror (_ ("division by 0"));
else
value = 1;
}
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wswitch-enum"
#endif
switch (op)
{
case MUL:
/* Handle INTMAX_MIN and INTMAX_MAX * -1 specially here? */
lvalue *= value;
break;
case DIV:
case MOD:
if (lvalue == INTMAX_MIN && value == -1)
lvalue = (op == DIV) ? INTMAX_MIN : 0;
else
lvalue = (op == DIV) ? lvalue / value : lvalue % value;
break;
case PLUS:
lvalue += value;
break;
case MINUS:
lvalue -= value;
break;
case LSH:
lvalue <<= value;
break;
case RSH:
lvalue >>= value;
break;
case BAND:
lvalue &= value;
break;
case BOR:
lvalue |= value;
break;
case BXOR:
lvalue ^= value;
break;
default:
evalerror (_ ("bug: bad expassign token"));
/* NOTREACHED */
}
value = lvalue;
}
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
string rhs = itos (value);
if (this_expr.noeval == 0)
{
#if defined(ARRAY_VARS)
if (lind != -1)
expr_bind_array_element (lhs, lind, rhs);
else
#endif
expr_bind_variable (lhs, rhs);
}
if (this_expr.lval.tokstr == this_expr.tokstr)
this_expr.lval.init ();
delete[] this_expr.tokstr;
this_expr.tokstr = nullptr; /* For freeing on errors. */
}
return value;
}
/* Conditional expression (expr?expr:expr) */
int64_t
Shell::expcond ()
{
int64_t cval, val1, val2, rval;
bool set_noeval = false;
rval = cval = explor ();
if (this_expr.curtok == QUES) /* found conditional expr */
{
if (cval == 0)
{
set_noeval = true;
this_expr.noeval++;
}
readtok ();
if (this_expr.curtok == NONE || this_expr.curtok == COL)
evalerror (_ ("expression expected"));
val1 = EXP_LOWEST ();
if (set_noeval)
this_expr.noeval--;
if (this_expr.curtok != COL)
evalerror (_ ("`:' expected for conditional expression"));
set_noeval = false;
if (cval)
{
set_noeval = true;
this_expr.noeval++;
}
readtok ();
if (this_expr.curtok == 0)
evalerror (_ ("expression expected"));
val2 = expcond ();
if (set_noeval)
this_expr.noeval--;
rval = cval ? val1 : val2;
this_expr.lasttok = COND;
}
return rval;
}
/* Logical OR. */
int64_t
Shell::explor ()
{
int64_t val1 = expland ();
while (this_expr.curtok == LOR)
{
bool set_noeval = false;
if (val1 != 0)
{
this_expr.noeval++;
set_noeval = true;
}
readtok ();
int64_t val2 = expland ();
if (set_noeval)
this_expr.noeval--;
val1 = val1 || val2;
this_expr.lasttok = LOR;
}
return val1;
}
/* Logical AND. */
int64_t
Shell::expland ()
{
int64_t val1 = expbor ();
while (this_expr.curtok == LAND)
{
bool set_noeval = false;
if (val1 == 0)
{
set_noeval = true;
this_expr.noeval++;
}
readtok ();
int64_t val2 = expbor ();
if (set_noeval)
this_expr.noeval--;
val1 = val1 && val2;
this_expr.lasttok = LAND;
}
return val1;
}
/* Bitwise OR. */
int64_t
Shell::expbor ()
{
int64_t val1 = expbxor ();
while (this_expr.curtok == BOR)
{
readtok ();
int64_t val2 = expbxor ();
val1 = val1 | val2;
this_expr.lasttok = NUM;
}
return val1;
}
/* Bitwise XOR. */
int64_t
Shell::expbxor ()
{
int64_t val1 = expband ();
while (this_expr.curtok == BXOR)
{
readtok ();
int64_t val2 = expband ();
val1 = val1 ^ val2;
this_expr.lasttok = NUM;
}
return val1;
}
/* Bitwise AND. */
int64_t
Shell::expband ()
{
int64_t val1 = exp5 ();
while (this_expr.curtok == BAND)
{
readtok ();
int64_t val2 = exp5 ();
val1 = val1 & val2;
this_expr.lasttok = NUM;
}
return val1;
}
int64_t
Shell::exp5 ()
{
int64_t val1 = exp4 ();
while ((this_expr.curtok == EQEQ) || (this_expr.curtok == NEQ))
{
token_t op = this_expr.curtok;
readtok ();
int64_t val2 = exp4 ();
if (op == EQEQ)
val1 = (val1 == val2);
else if (op == NEQ)
val1 = (val1 != val2);
this_expr.lasttok = NUM;
}
return val1;
}
int64_t
Shell::exp4 ()
{
int64_t val1 = expshift ();
while ((this_expr.curtok == LEQ) || (this_expr.curtok == GEQ)
|| (this_expr.curtok == LT) || (this_expr.curtok == GT))
{
token_t op = this_expr.curtok;
readtok ();
int64_t val2 = expshift ();
if (op == LEQ)
val1 = val1 <= val2;
else if (op == GEQ)
val1 = val1 >= val2;
else if (op == LT)
val1 = val1 < val2;
else /* (op == GT) */
val1 = val1 > val2;
this_expr.lasttok = NUM;
}
return val1;
}
/* Left and right shifts. */
int64_t
Shell::expshift ()
{
int64_t val1 = exp3 ();
while ((this_expr.curtok == LSH) || (this_expr.curtok == RSH))
{
token_t op = this_expr.curtok;
readtok ();
int64_t val2 = exp3 ();
if (op == LSH)
val1 = val1 << val2;
else
val1 = val1 >> val2;
this_expr.lasttok = NUM;
}
return val1;
}
int64_t
Shell::exp3 ()
{
int64_t val1 = expmuldiv ();
while ((this_expr.curtok == PLUS) || (this_expr.curtok == MINUS))
{
token_t op = this_expr.curtok;
readtok ();
int64_t val2 = expmuldiv ();
if (op == PLUS)
val1 += val2;
else if (op == MINUS)
val1 -= val2;
this_expr.lasttok = NUM;
}
return val1;
}
int64_t
Shell::expmuldiv ()
{
int64_t val1 = exppower ();
while ((this_expr.curtok == MUL) || (this_expr.curtok == DIV)
|| (this_expr.curtok == MOD))
{
token_t op = this_expr.curtok;
char *stp = this_expr.tp;
readtok ();
int64_t val2 = exppower ();
/* Handle division by 0 and twos-complement arithmetic overflow */
if (((op == DIV) || (op == MOD)) && (val2 == 0))
{
if (this_expr.noeval == 0)
{
this_expr.lasttp = stp;
if (stp)
while (*this_expr.lasttp && whitespace (*this_expr.lasttp))
this_expr.lasttp++;
evalerror (_ ("division by 0"));
}
else
val2 = 1;
}
else if (op == MOD && val1 == INTMAX_MIN && val2 == -1)
{
val1 = 0;
continue;
}
else if (op == DIV && val1 == INTMAX_MIN && val2 == -1)
val2 = 1;
if (op == MUL)
val1 *= val2;
else if (op == DIV || op == MOD)
val1 = (op == DIV) ? val1 / val2 : val1 % val2;
this_expr.lasttok = NUM;
}
return val1;
}
static inline int64_t
ipow (int64_t base, int64_t exp)
{
int64_t result = 1;
while (exp)
{
if (exp & 1)
result *= base;
exp >>= 1;
base *= base;
}
return result;
}
int64_t
Shell::exppower ()
{
int64_t val1 = exp1 ();
while (this_expr.curtok == POWER)
{
readtok ();
int64_t val2 = exppower (); /* exponentiation is right-associative */
this_expr.lasttok = NUM;
if (val2 == 0)
return 1;
if (val2 < 0)
evalerror (_ ("exponent less than 0"));
val1 = ipow (val1, val2);
}
return val1;
}
int64_t
Shell::exp1 ()
{
int64_t val;
if (this_expr.curtok == NOT)
{
readtok ();
val = !exp1 ();
this_expr.lasttok = NUM;
}
else if (this_expr.curtok == BNOT)
{
readtok ();
val = ~exp1 ();
this_expr.lasttok = NUM;
}
else if (this_expr.curtok == MINUS)
{
readtok ();
val = -exp1 ();
this_expr.lasttok = NUM;
}
else if (this_expr.curtok == PLUS)
{
readtok ();
val = exp1 ();
this_expr.lasttok = NUM;
}
else
val = exp0 ();
return val;
}
int64_t
Shell::exp0 ()
{
int64_t val = 0, v2;
EXPR_CONTEXT ec;
/* XXX - might need additional logic here to decide whether or not
pre-increment or pre-decrement is legal at this point. */
if (this_expr.curtok == PREINC || this_expr.curtok == PREDEC)
{
token_t stok = this_expr.lasttok = this_expr.curtok;
readtok ();
if (this_expr.curtok != STR)
/* readtok() catches this */
evalerror (
_ ("identifier expected after pre-increment or pre-decrement"));
v2 = this_expr.tokval + ((stok == PREINC) ? 1 : -1);
string vincdec = itos (v2);
if (this_expr.noeval == 0)
{
#if defined(ARRAY_VARS)
if (this_expr.lval.ind != -1)
expr_bind_array_element (this_expr.lval.tokstr, this_expr.lval.ind,
vincdec);
else
#endif
if (this_expr.tokstr)
expr_bind_variable (this_expr.tokstr, vincdec);
}
val = v2;
this_expr.curtok = NUM; /* make sure --x=7 is flagged as an error */
readtok ();
}
else if (this_expr.curtok == LPAR)
{
/* XXX - save curlval here? Or entire expression context? */
readtok ();
val = EXP_LOWEST ();
if (this_expr.curtok != RPAR)
evalerror (_ ("missing `)'"));
/* Skip over closing paren. */
readtok ();
}
else if ((this_expr.curtok == NUM) || (this_expr.curtok == STR))
{
val = this_expr.tokval;
if (this_expr.curtok == STR)
{
ec = this_expr;
this_expr.tokstr = nullptr; /* keep it from being freed */
this_expr.noeval = 1;
readtok ();
token_t stok = this_expr.curtok;
/* post-increment or post-decrement */
if (stok == POSTINC || stok == POSTDEC)
{
/* restore certain portions of EC */
this_expr.tokstr = ec.tokstr;
this_expr.noeval = ec.noeval;
this_expr.lval = ec.lval;
this_expr.lasttok = STR; /* ec.curtok */
v2 = val + ((stok == POSTINC) ? 1 : -1);
string vincdec = itos (v2);
if (this_expr.noeval == 0)
{
#if defined(ARRAY_VARS)
if (this_expr.lval.ind != -1)
expr_bind_array_element (this_expr.lval.tokstr,
this_expr.lval.ind, vincdec);
else
#endif
expr_bind_variable (this_expr.tokstr, vincdec);
}
this_expr.curtok
= NUM; /* make sure x++=7 is flagged as an error */
}
else
{
/* XXX - watch out for pointer aliasing issues here */
if (stok == STR) /* free new tokstr before old one is restored */
delete[] this_expr.tokstr;
this_expr = ec;
}
}
readtok ();
}
else
evalerror (_ ("syntax error: operand expected"));
return val;
}
int64_t
Shell::expr_streval (char *tok, int e, token_lvalue *lvalue)
{
SHELL_VAR *v;
// itrace("expr_streval: %s: noeval = %d expanded=%d", tok.c_str (),
// this_expr.noeval, already_expanded);
/* If we are suppressing evaluation, just short-circuit here instead of
going through the rest of the evaluator. */
if (this_expr.noeval)
return 0;
size_t initial_depth = expr_stack.size ();
#if defined(ARRAY_VARS)
bool tflag = assoc_expand_once && already_expanded; /* for a start */
#endif
/* [[[[[ */
#if defined(ARRAY_VARS)
av_flags aflag = (tflag) ? AV_NOEXPAND : AV_NOFLAGS;
v = (e == ']') ? array_variable_part (tok, tflag, nullptr, nullptr)
: find_variable (tok);
#else
v = find_variable (tok);
#endif
if (v == nullptr && e != ']')
v = find_variable_last_nameref (tok, 0);
if ((v == nullptr || v->invisible ()) && unbound_vars_is_error)
{
const char *value;
#if defined(ARRAY_VARS)
string *str_value = nullptr;
if (e == ']')
{
str_value = array_variable_name (tok, tflag, nullptr);
value = str_value ? str_value->c_str () : tok;
}
else
#endif
value = tok;
set_exit_status (EXECUTION_FAILURE);
err_unboundvar (value);
#if defined(ARRAY_VARS)
if (e == ']')
delete str_value; /* array_variable_name returns new memory */
#endif
if (no_throw_on_fatal_error && interactive_shell)
throw eval_exception ();
if (interactive_shell)
{
expr_unwind ();
top_level_cleanup ();
throw bash_exception (DISCARD);
}
else
throw bash_exception (FORCE_EOF);
}
string *value;
#if defined(ARRAY_VARS)
array_eltstate_t es (-1);
/* If the second argument to get_array_value doesn't include AV_ALLOWALL,
we don't allow references like array[@]. In this case, get_array_value
is just like get_variable_value in that it does not return newly-allocated
memory or quote the results. AFLAG is set above and is either AV_NOEXPAND
or AV_NOFLAGS. */
value = (e == ']') ? get_array_value (tok, aflag, &es)
: get_variable_value (v);
arrayind_t ind = es.ind;
#else
value = get_variable_value (v);
#endif
if (expr_stack.size () < initial_depth)
{
if (no_throw_on_fatal_error && interactive_shell)
throw eval_exception ();
return 0;
}
int64_t tval = (value && !value->empty ()) ? subexpr (*value) : 0;
if (lvalue)
{
lvalue->tokstr = tok; /* XXX */
lvalue->tokval = tval;
lvalue->tokvar = v; /* XXX */
#if defined(ARRAY_VARS)
lvalue->ind = ind;
#else
lvalue->ind = -1;
#endif
}
return tval;
}
static inline bool
_is_multiop (char c)
{
switch (c)
{
case EQEQ:
case NEQ:
case LEQ:
case GEQ:
case LAND:
case LOR:
case LSH:
case RSH:
case OP_ASSIGN:
case COND:
case POWER:
case PREINC:
case PREDEC:
case POSTINC:
case POSTDEC:
return true;
default:
return false;
}
}
static inline bool
_is_arithop (char c)
{
switch (c)
{
case EQ: