forked from redox-os/gawk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpret.h
1485 lines (1270 loc) · 35 KB
/
interpret.h
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
/*
* interpret.h --- run a list of instructions.
*/
/*
* Copyright (C) 1986, 1988, 1989, 1991-2018 the Free Software Foundation, Inc.
*
* This file is part of GAWK, the GNU implementation of the
* AWK Programming Language.
*
* GAWK 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.
*
* GAWK 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/*
* If "r" is a field, valref should normally be > 1, because the field is
* created initially with valref 1, and valref should be bumped when it is
* pushed onto the stack by Op_field_spec. On the other hand, if we are
* assigning to $n, then Op_store_field calls unref(*lhs) before assigning
* the new value, so that decrements valref. So if the RHS is a field with
* valref 1, that effectively means that this is an assignment like "$n = $n",
* so a no-op, other than triggering $0 reconstitution.
*/
// not a macro so we can step into it with a debugger
#ifndef UNFIELD_DEFINED
#define UNFIELD_DEFINED 1
static inline void
unfield(NODE **l, NODE **r)
{
/* if was a field, turn it into a var */
if (((*r)->flags & MALLOC) != 0 || (*r)->valref == 1) {
(*l) = (*r);
} else {
(*l) = dupnode(*r);
DEREF(*r);
}
force_string(*l);
}
#define UNFIELD(l, r) unfield(& (l), & (r))
#endif
int
r_interpret(INSTRUCTION *code)
{
INSTRUCTION *pc; /* current instruction */
OPCODE op; /* current opcode */
NODE *r = NULL;
NODE *m;
INSTRUCTION *ni;
NODE *t1, *t2;
NODE **lhs;
AWKNUM x, x2;
int di;
Regexp *rp;
NODE *set_array = NULL; /* array with a post-assignment routine */
NODE *set_idx = NULL; /* the index of the array element */
/* array subscript */
#define mk_sub(n) (n == 1 ? POP_SCALAR() : concat_exp(n, true))
#ifdef EXEC_HOOK
#define JUMPTO(x) do { if (post_execute) post_execute(pc); pc = (x); goto top; } while (false)
#else
#define JUMPTO(x) do { pc = (x); goto top; } while (false)
#endif
pc = code;
/* N.B.: always use JUMPTO for next instruction, otherwise bad things
* may happen. DO NOT add a real loop (for/while) below to
* replace ' forever {'; this catches failure to use JUMPTO to execute
* next instruction (e.g. continue statement).
*/
/* loop until hit Op_stop instruction */
/* forever { */
top:
if (pc->source_line > 0)
sourceline = pc->source_line;
#ifdef EXEC_HOOK
for (di = 0; di < num_exec_hook; di++) {
if (! pre_execute[di](& pc))
goto top;
}
#endif
switch ((op = pc->opcode)) {
case Op_rule:
currule = pc->in_rule; /* for sole use in Op_K_next, Op_K_nextfile, Op_K_getline */
/* fall through */
case Op_func:
source = pc->source_file;
break;
case Op_atexit:
{
bool stdio_problem = false;
bool got_EPIPE = false;
/* avoid false source indications */
source = NULL;
sourceline = 0;
(void) nextfile(& curfile, true); /* close input data file */
/*
* This used to be:
*
* if (close_io() != 0 && ! exiting && exit_val == 0)
* exit_val = 1;
*
* Other awks don't care about problems closing open files
* and pipes, in that it doesn't affect their exit status.
* So we no longer do either.
*/
(void) close_io(& stdio_problem, & got_EPIPE);
/*
* However, we do want to exit non-zero if there was a problem
* with stdout/stderr, so we reinstate a slightly different
* version of the above:
*/
if (stdio_problem && ! exiting && exit_val == 0)
exit_val = 1;
close_extensions();
if (got_EPIPE)
die_via_sigpipe();
}
break;
case Op_stop:
return 0;
case Op_push_i:
m = pc->memory;
if (! do_traditional && (m->flags & INTLSTR) != 0) {
char *orig, *trans, save;
save = m->stptr[m->stlen];
m->stptr[m->stlen] = '\0';
orig = m->stptr;
trans = dgettext(TEXTDOMAIN, orig);
m->stptr[m->stlen] = save;
m = make_string(trans, strlen(trans));
} else
UPREF(m);
PUSH(m);
break;
case Op_push:
case Op_push_arg:
case Op_push_arg_untyped:
{
NODE *save_symbol;
bool isparam = false;
save_symbol = m = pc->memory;
if (m->type == Node_param_list) {
isparam = true;
save_symbol = m = GET_PARAM(m->param_cnt);
if (m->type == Node_array_ref) {
if (m->orig_array->type == Node_var) {
/* gawk 'func f(x) { a = 10; print x; } BEGIN{ f(a) }' */
goto uninitialized_scalar;
}
m = m->orig_array;
}
}
switch (m->type) {
case Node_var:
if (do_lint && var_uninitialized(m))
lintwarn(isparam ?
_("reference to uninitialized argument `%s'") :
_("reference to uninitialized variable `%s'"),
save_symbol->vname);
m = m->var_value;
UPREF(m);
PUSH(m);
break;
case Node_var_new:
uninitialized_scalar:
if (op != Op_push_arg_untyped) {
/* convert untyped to scalar */
m->type = Node_var;
m->var_value = dupnode(Nnull_string);
}
if (do_lint)
lintwarn(isparam ?
_("reference to uninitialized argument `%s'") :
_("reference to uninitialized variable `%s'"),
save_symbol->vname);
if (op != Op_push_arg_untyped)
m = dupnode(Nnull_string);
PUSH(m);
break;
case Node_var_array:
if (op == Op_push_arg || op == Op_push_arg_untyped)
PUSH(m);
else
fatal(_("attempt to use array `%s' in a scalar context"),
array_vname(save_symbol));
break;
default:
cant_happen();
}
}
break;
case Op_push_param: /* function argument */
m = pc->memory;
if (m->type == Node_param_list)
m = GET_PARAM(m->param_cnt);
if (m->type == Node_var) {
m = m->var_value;
UPREF(m);
PUSH(m);
break;
}
/* else
fall through */
case Op_push_array:
PUSH(pc->memory);
break;
case Op_push_lhs:
lhs = get_lhs(pc->memory, pc->do_reference);
PUSH_ADDRESS(lhs);
break;
case Op_subscript:
t2 = mk_sub(pc->sub_count);
t1 = POP_ARRAY(false);
if (do_lint && in_array(t1, t2) == NULL) {
t2 = force_string(t2);
lintwarn(_("reference to uninitialized element `%s[\"%.*s\"]'"),
array_vname(t1), (int) t2->stlen, t2->stptr);
if (t2->stlen == 0)
lintwarn(_("subscript of array `%s' is null string"), array_vname(t1));
}
/* for FUNCTAB, get the name as the element value */
if (t1 == func_table) {
static bool warned = false;
if (do_lint && ! warned) {
warned = true;
lintwarn(_("FUNCTAB is a gawk extension"));
}
r = t2;
} else {
/* make sure stuff like NF, NR, are up to date */
if (t1 == symbol_table)
update_global_values();
r = *assoc_lookup(t1, t2);
}
DEREF(t2);
/* for SYMTAB, step through to the actual variable */
if (t1 == symbol_table) {
static bool warned = false;
if (do_lint && ! warned) {
warned = true;
lintwarn(_("SYMTAB is a gawk extension"));
}
if (r->type == Node_var)
r = r->var_value;
}
if (r->type == Node_val)
UPREF(r);
PUSH(r);
break;
case Op_sub_array:
t2 = mk_sub(pc->sub_count);
t1 = POP_ARRAY(false);
r = in_array(t1, t2);
if (r == NULL) {
r = make_array();
r->parent_array = t1;
lhs = assoc_lookup(t1, t2);
unref(*lhs);
*lhs = r;
t2 = force_string(t2);
r->vname = estrdup(t2->stptr, t2->stlen); /* the subscript in parent array */
/* execute post-assignment routine if any */
if (t1->astore != NULL)
(*t1->astore)(t1, t2);
} else if (r->type != Node_var_array) {
t2 = force_string(t2);
fatal(_("attempt to use scalar `%s[\"%.*s\"]' as an array"),
array_vname(t1), (int) t2->stlen, t2->stptr);
}
DEREF(t2);
PUSH(r);
break;
case Op_subscript_lhs:
t2 = mk_sub(pc->sub_count);
t1 = POP_ARRAY(false);
if (do_lint && in_array(t1, t2) == NULL) {
t2 = force_string(t2);
if (pc->do_reference)
lintwarn(_("reference to uninitialized element `%s[\"%.*s\"]'"),
array_vname(t1), (int) t2->stlen, t2->stptr);
if (t2->stlen == 0)
lintwarn(_("subscript of array `%s' is null string"), array_vname(t1));
}
lhs = assoc_lookup(t1, t2);
if ((*lhs)->type == Node_var_array) {
t2 = force_string(t2);
fatal(_("attempt to use array `%s[\"%.*s\"]' in a scalar context"),
array_vname(t1), (int) t2->stlen, t2->stptr);
}
/*
* Changing something in FUNCTAB is not allowed.
*
* SYMTAB is a little more messy. Three kinds of values may
* be stored in SYMTAB:
* 1. Variables that don"t yet have a value (Node_var_new)
* 2. Variables that have a value (Node_var)
* 3. Values that awk code stuck into SYMTAB not related to variables (Node_value)
* For 1, since we are giving it a value, we have to change the type to Node_var.
* For 1 and 2, we have to step through the Node_var to get to the value.
* For 3, we just us the value we got from assoc_lookup(), above.
*/
if (t1 == func_table)
fatal(_("cannot assign to elements of FUNCTAB"));
else if ( t1 == symbol_table
&& ( (*lhs)->type == Node_var
|| (*lhs)->type == Node_var_new)) {
update_global_values(); /* make sure stuff like NF, NR, are up to date */
(*lhs)->type = Node_var; /* in case was Node_var_new */
lhs = & ((*lhs)->var_value); /* extra level of indirection */
}
assert(set_idx == NULL);
if (t1->astore) {
/* array has post-assignment routine */
set_array = t1;
set_idx = t2;
} else
DEREF(t2);
PUSH_ADDRESS(lhs);
break;
case Op_field_spec:
t1 = TOP_SCALAR();
lhs = r_get_field(t1, (Func_ptr *) 0, true);
decr_sp();
DEREF(t1);
r = *lhs;
UPREF(r);
PUSH(r);
break;
case Op_field_spec_lhs:
t1 = TOP_SCALAR();
lhs = r_get_field(t1, &pc->target_assign->field_assign, pc->do_reference);
decr_sp();
DEREF(t1);
PUSH_ADDRESS(lhs);
break;
case Op_lint:
if (do_lint) {
switch (pc->lint_type) {
case LINT_assign_in_cond:
lintwarn(_("assignment used in conditional context"));
break;
case LINT_no_effect:
lintwarn(_("statement has no effect"));
break;
default:
cant_happen();
}
}
break;
case Op_K_break:
case Op_K_continue:
case Op_jmp:
assert(pc->target_jmp != NULL);
JUMPTO(pc->target_jmp);
case Op_jmp_false:
r = POP_SCALAR();
di = eval_condition(r);
DEREF(r);
if (! di)
JUMPTO(pc->target_jmp);
break;
case Op_jmp_true:
r = POP_SCALAR();
di = eval_condition(r);
DEREF(r);
if (di)
JUMPTO(pc->target_jmp);
break;
case Op_and:
case Op_or:
t1 = POP_SCALAR();
di = eval_condition(t1);
DEREF(t1);
if ((op == Op_and && di) || (op == Op_or && ! di))
break;
r = node_Boolean[di];
UPREF(r);
PUSH(r);
ni = pc->target_jmp;
JUMPTO(ni->nexti);
case Op_and_final:
case Op_or_final:
t1 = TOP_SCALAR();
r = node_Boolean[eval_condition(t1)];
DEREF(t1);
UPREF(r);
REPLACE(r);
break;
case Op_not:
t1 = TOP_SCALAR();
r = node_Boolean[! eval_condition(t1)];
DEREF(t1);
UPREF(r);
REPLACE(r);
break;
case Op_equal:
r = node_Boolean[cmp_scalars(SCALAR_EQ_NEQ) == 0];
UPREF(r);
REPLACE(r);
break;
case Op_notequal:
r = node_Boolean[cmp_scalars(SCALAR_EQ_NEQ) != 0];
UPREF(r);
REPLACE(r);
break;
case Op_less:
r = node_Boolean[cmp_scalars(SCALAR_RELATIONAL) < 0];
UPREF(r);
REPLACE(r);
break;
case Op_greater:
r = node_Boolean[cmp_scalars(SCALAR_RELATIONAL) > 0];
UPREF(r);
REPLACE(r);
break;
case Op_leq:
r = node_Boolean[cmp_scalars(SCALAR_RELATIONAL) <= 0];
UPREF(r);
REPLACE(r);
break;
case Op_geq:
r = node_Boolean[cmp_scalars(SCALAR_RELATIONAL) >= 0];
UPREF(r);
REPLACE(r);
break;
case Op_plus_i:
x2 = force_number(pc->memory)->numbr;
goto plus;
case Op_plus:
t2 = POP_NUMBER();
x2 = t2->numbr;
DEREF(t2);
plus:
t1 = TOP_NUMBER();
r = make_number(t1->numbr + x2);
DEREF(t1);
REPLACE(r);
break;
case Op_minus_i:
x2 = force_number(pc->memory)->numbr;
goto minus;
case Op_minus:
t2 = POP_NUMBER();
x2 = t2->numbr;
DEREF(t2);
minus:
t1 = TOP_NUMBER();
r = make_number(t1->numbr - x2);
DEREF(t1);
REPLACE(r);
break;
case Op_times_i:
x2 = force_number(pc->memory)->numbr;
goto times;
case Op_times:
t2 = POP_NUMBER();
x2 = t2->numbr;
DEREF(t2);
times:
t1 = TOP_NUMBER();
r = make_number(t1->numbr * x2);
DEREF(t1);
REPLACE(r);
break;
case Op_exp_i:
x2 = force_number(pc->memory)->numbr;
goto exp;
case Op_exp:
t2 = POP_NUMBER();
x2 = t2->numbr;
DEREF(t2);
exp:
t1 = TOP_NUMBER();
r = make_number(calc_exp(t1->numbr, x2));
DEREF(t1);
REPLACE(r);
break;
case Op_quotient_i:
x2 = force_number(pc->memory)->numbr;
goto quotient;
case Op_quotient:
t2 = POP_NUMBER();
x2 = t2->numbr;
DEREF(t2);
quotient:
t1 = TOP_NUMBER();
if (x2 == 0)
fatal(_("division by zero attempted"));
r = make_number(t1->numbr / x2);
DEREF(t1);
REPLACE(r);
break;
case Op_mod_i:
x2 = force_number(pc->memory)->numbr;
goto mod;
case Op_mod:
t2 = POP_NUMBER();
x2 = t2->numbr;
DEREF(t2);
mod:
t1 = TOP_NUMBER();
if (x2 == 0)
fatal(_("division by zero attempted in `%%'"));
#ifdef HAVE_FMOD
x = fmod(t1->numbr, x2);
#else /* ! HAVE_FMOD */
(void) modf(t1->numbr / x2, &x);
x = t1->numbr - x * x2;
#endif /* ! HAVE_FMOD */
r = make_number(x);
DEREF(t1);
REPLACE(r);
break;
case Op_preincrement:
case Op_predecrement:
x = op == Op_preincrement ? 1.0 : -1.0;
lhs = TOP_ADDRESS();
t1 = *lhs;
force_number(t1);
if (t1->valref == 1 && t1->flags == (MALLOC|NUMCUR|NUMBER)) {
/* optimization */
t1->numbr += x;
r = t1;
} else {
r = *lhs = make_number(t1->numbr + x);
unref(t1);
}
UPREF(r);
REPLACE(r);
break;
case Op_postincrement:
case Op_postdecrement:
x = op == Op_postincrement ? 1.0 : -1.0;
lhs = TOP_ADDRESS();
t1 = *lhs;
force_number(t1);
r = make_number(t1->numbr);
if (t1->valref == 1 && t1->flags == (MALLOC|NUMCUR|NUMBER)) {
/* optimization */
t1->numbr += x;
} else {
*lhs = make_number(t1->numbr + x);
unref(t1);
}
REPLACE(r);
break;
case Op_unary_minus:
t1 = TOP_NUMBER();
r = make_number(-t1->numbr);
DEREF(t1);
REPLACE(r);
break;
case Op_unary_plus:
// Force argument to be numeric
t1 = TOP_NUMBER();
r = make_number(t1->numbr);
DEREF(t1);
REPLACE(r);
break;
case Op_store_sub:
/*
* array[sub] assignment optimization,
* see awkgram.y (optimize_assignment)
*/
t1 = force_array(pc->memory, true); /* array */
t2 = mk_sub(pc->expr_count); /* subscript */
lhs = assoc_lookup(t1, t2);
if ((*lhs)->type == Node_var_array) {
t2 = force_string(t2);
fatal(_("attempt to use array `%s[\"%.*s\"]' in a scalar context"),
array_vname(t1), (int) t2->stlen, t2->stptr);
}
DEREF(t2);
/*
* Changing something in FUNCTAB is not allowed.
*
* SYMTAB is a little more messy. Three kinds of values may
* be stored in SYMTAB:
* 1. Variables that don"t yet have a value (Node_var_new)
* 2. Variables that have a value (Node_var)
* 3. Values that awk code stuck into SYMTAB not related to variables (Node_value)
* For 1, since we are giving it a value, we have to change the type to Node_var.
* For 1 and 2, we have to step through the Node_var to get to the value.
* For 3, we just us the value we got from assoc_lookup(), above.
*/
if (t1 == func_table)
fatal(_("cannot assign to elements of FUNCTAB"));
else if ( t1 == symbol_table
&& ( (*lhs)->type == Node_var
|| (*lhs)->type == Node_var_new)) {
(*lhs)->type = Node_var; /* in case was Node_var_new */
lhs = & ((*lhs)->var_value); /* extra level of indirection */
}
unref(*lhs);
r = POP_SCALAR();
UNFIELD(*lhs, r);
/* execute post-assignment routine if any */
if (t1->astore != NULL)
(*t1->astore)(t1, t2);
DEREF(t2);
break;
case Op_store_var:
/*
* simple variable assignment optimization,
* see awkgram.y (optimize_assignment)
*/
lhs = get_lhs(pc->memory, false);
unref(*lhs);
r = pc->initval; /* constant initializer */
if (r != NULL) {
UPREF(r);
*lhs = r;
} else {
r = POP_SCALAR();
UNFIELD(*lhs, r);
}
break;
case Op_store_field:
{
/* field assignment optimization,
* see awkgram.y (optimize_assignment)
*/
Func_ptr assign;
t1 = TOP_SCALAR();
lhs = r_get_field(t1, & assign, false);
decr_sp();
DEREF(t1);
/*
* N.B. We must call assign() before unref, since
* we may need to copy $n values before freeing the
* $0 buffer.
*/
assert(assign != NULL);
assign();
unref(*lhs);
r = POP_SCALAR();
UNFIELD(*lhs, r);
}
break;
case Op_assign_concat:
/* x = x ... string concatenation optimization */
lhs = get_lhs(pc->memory, false);
t1 = force_string(*lhs);
t2 = POP_STRING();
if (t1 != *lhs) {
unref(*lhs);
*lhs = dupnode(t1);
}
if (t1 != t2 && t1->valref == 1 && (t1->flags & (MALLOC|MPFN|MPZN)) == MALLOC) {
size_t nlen = t1->stlen + t2->stlen;
erealloc(t1->stptr, char *, nlen + 1, "r_interpret");
memcpy(t1->stptr + t1->stlen, t2->stptr, t2->stlen);
t1->stlen = nlen;
t1->stptr[nlen] = '\0';
/* clear flags except WSTRCUR (used below) */
t1->flags &= WSTRCUR;
/* configure as a string as in make_str_node */
t1->flags |= (MALLOC|STRING|STRCUR);
t1->stfmt = STFMT_UNUSED;
#ifdef HAVE_MPFR
t1->strndmode = MPFR_round_mode;
#endif
if ((t1->flags & WSTRCUR) != 0 && (t2->flags & WSTRCUR) != 0) {
size_t wlen = t1->wstlen + t2->wstlen;
erealloc(t1->wstptr, wchar_t *,
sizeof(wchar_t) * (wlen + 1), "r_interpret");
memcpy(t1->wstptr + t1->wstlen, t2->wstptr, t2->wstlen * sizeof(wchar_t));
t1->wstlen = wlen;
t1->wstptr[wlen] = L'\0';
} else
free_wstr(*lhs);
} else {
size_t nlen = t1->stlen + t2->stlen;
char *p;
emalloc(p, char *, nlen + 1, "r_interpret");
memcpy(p, t1->stptr, t1->stlen);
memcpy(p + t1->stlen, t2->stptr, t2->stlen);
/* N.B. No NUL-termination required, since make_str_node will do it. */
unref(*lhs);
t1 = *lhs = make_str_node(p, nlen, ALREADY_MALLOCED);
}
DEREF(t2);
break;
case Op_assign:
lhs = POP_ADDRESS();
r = TOP_SCALAR();
unref(*lhs);
UPREF(r);
UNFIELD(*lhs, r);
REPLACE(r);
break;
case Op_subscript_assign:
/* conditionally execute post-assignment routine for an array element */
if (set_idx != NULL) {
di = true;
if (pc->assign_ctxt == Op_sub_builtin
&& (r = TOP())
&& get_number_si(r) == 0 /* no substitution performed */
)
di = false;
else if ((pc->assign_ctxt == Op_K_getline
|| pc->assign_ctxt == Op_K_getline_redir)
&& (r = TOP())
&& get_number_si(r) <= 0 /* EOF or error */
)
di = false;
if (di)
(*set_array->astore)(set_array, set_idx);
unref(set_idx);
set_idx = NULL;
}
break;
/* numeric assignments */
case Op_assign_plus:
case Op_assign_minus:
case Op_assign_times:
case Op_assign_quotient:
case Op_assign_mod:
case Op_assign_exp:
op_assign(op);
break;
case Op_var_update: /* update value of NR, FNR or NF */
pc->update_var();
break;
case Op_var_assign:
case Op_field_assign:
r = TOP();
if (pc->assign_ctxt == Op_sub_builtin
&& get_number_si(r) == 0 /* top of stack has a number == 0 */
) {
/* There wasn't any substitutions. If the target is a FIELD,
* this means no field re-splitting or $0 reconstruction.
* Skip the set_FOO routine if the target is a special variable.
*/
break;
} else if ((pc->assign_ctxt == Op_K_getline
|| pc->assign_ctxt == Op_K_getline_redir)
&& get_number_si(r) <= 0 /* top of stack has a number <= 0 */
) {
/* getline returned EOF or error */
break;
}
if (op == Op_var_assign)
pc->assign_var();
else
pc->field_assign();
break;
case Op_concat:
r = concat_exp(pc->expr_count, pc->concat_flag & CSUBSEP);
PUSH(r);
break;
case Op_K_case:
if ((pc + 1)->match_exp) {
/* match a constant regex against switch expression instead of $0. */
m = POP(); /* regex */
t2 = TOP_SCALAR(); /* switch expression */
t2 = force_string(t2);
rp = re_update(m);
di = (research(rp, t2->stptr, 0, t2->stlen, RE_NO_FLAGS) >= 0);
} else {
t1 = POP_SCALAR(); /* case value */
t2 = TOP_SCALAR(); /* switch expression */
di = (cmp_nodes(t2, t1, true) == 0);
DEREF(t1);
}
if (di) {
/* match found */
t2 = POP_SCALAR();
DEREF(t2);
JUMPTO(pc->target_jmp);
}
break;
case Op_K_delete:
t1 = POP_ARRAY(false);
do_delete(t1, pc->expr_count);
stack_adj(-pc->expr_count);
break;
case Op_K_delete_loop:
t1 = POP_ARRAY(false);
lhs = POP_ADDRESS(); /* item */
do_delete_loop(t1, lhs);
break;
case Op_in_array:
t1 = POP_ARRAY(false);
t2 = mk_sub(pc->expr_count);
r = node_Boolean[(in_array(t1, t2) != NULL)];
DEREF(t2);
UPREF(r);
PUSH(r);
break;
case Op_arrayfor_init:
{
NODE **list = NULL;
NODE *array, *sort_str;
size_t num_elems = 0;
static NODE *sorted_in = NULL;
const char *how_to_sort = "@unsorted";
char save;
bool saved_end = false;
/* get the array */
array = POP_ARRAY(true);
/* sanity: check if empty */
num_elems = assoc_length(array);
if (num_elems == 0)
goto arrayfor;
if (sorted_in == NULL) /* do this once */
sorted_in = make_string("sorted_in", 9);
sort_str = NULL;
/*
* If posix, or if there's no PROCINFO[],
* there's no ["sorted_in"], so no sorting
*/
if (! do_posix && PROCINFO_node != NULL)
sort_str = in_array(PROCINFO_node, sorted_in);
if (sort_str != NULL) {
sort_str = force_string(sort_str);
if (sort_str->stlen > 0) {
how_to_sort = sort_str->stptr;
str_terminate(sort_str, save);
saved_end = true;
}
}
list = assoc_list(array, how_to_sort, SORTED_IN);
if (saved_end)
str_restore(sort_str, save);
arrayfor:
getnode(r);
r->type = Node_arrayfor;
r->for_list = list;
r->for_list_size = num_elems; /* # of elements in list */
r->cur_idx = -1; /* current index */
r->for_array = array; /* array */
PUSH(r);
if (num_elems == 0)
JUMPTO(pc->target_jmp); /* Op_arrayfor_final */
}
break;
case Op_arrayfor_incr:
r = TOP(); /* Node_arrayfor */
if (++r->cur_idx == r->for_list_size) {
NODE *array;
array = r->for_array; /* actual array */
if (do_lint && array->table_size != r->for_list_size)
lintwarn(_("for loop: array `%s' changed size from %ld to %ld during loop execution"),
array_vname(array), (long) r->for_list_size, (long) array->table_size);
JUMPTO(pc->target_jmp); /* Op_arrayfor_final */
}
t1 = r->for_list[r->cur_idx];
lhs = get_lhs(pc->array_var, false);
unref(*lhs);
*lhs = dupnode(t1);
break;
case Op_arrayfor_final:
r = POP();
assert(r->type == Node_arrayfor);
free_arrayfor(r);
break;
case Op_builtin:
r = pc->builtin(pc->expr_count);
PUSH(r);
break;
case Op_ext_builtin:
{
size_t arg_count = pc->expr_count;
awk_ext_func_t *f = pc[1].c_function;
size_t min_req = f->min_required_args;
size_t max_expect = f->max_expected_args;
awk_value_t result;
if (arg_count < min_req)
fatal(_("%s: called with %lu arguments, expecting at least %lu"),