forked from illumos/illumos-gate
/
awk3.c
2223 lines (2042 loc) · 45.4 KB
/
awk3.c
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
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* awk -- executor
*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Copyright 1985, 1994 by Mortice Kern Systems Inc. All rights reserved.
*
* Based on MKS awk(1) ported to be /usr/xpg4/bin/awk with POSIX/XCU4 changes
*/
#include "awk.h"
#include "y.tab.h"
static int dohash(wchar_t *name);
static NODE *arithmetic(NODE *np);
static NODE *comparison(NODE *np);
static int type_of(NODE *np);
static NODE *lfield(INT fieldno, NODE *value);
static NODE *rfield(INT fieldno);
static NODE *userfunc(NODE *np);
static wchar_t *lltoa(long long l);
static NODE *exprconcat(NODE *np, int len);
static int s_if(NODE *np);
static int s_while(NODE *np);
static int s_for(NODE *np);
static int s_forin(NODE *np);
static void setrefield(NODE *value);
static void freetemps(void);
static int action(NODE *np);
static wchar_t *makeindex(NODE *np, wchar_t *array, int tag);
static int exprtest(NODE *np);
#define regmatch(rp, s) REGWEXEC(rp, s, 0, (REGWMATCH_T*)NULL, 0)
/*
* This code allows for integers to be stored in longs (type INT) and
* only promoted to double precision floating point numbers (type REAL)
* when overflow occurs during +, -, or * operations. This is very
* non-portable if you desire such a speed optimisation. You may wish
* to put something here for your system. This "something" would likely
* include either an assembler "jump on overflow" instruction or a
* method to get traps on overflows from the hardware.
*
* This portable method works for ones and twos complement integer
* representations (which is, realistically) almost all machines.
*/
#if __TURBOC__
#define addoverflow() asm jo overflow
#define suboverflow() asm jo overflow
#else
/*
* These are portable to two's complement integer machines
*/
#define addoverflow() if ((i1^i2) >= 0 && (iresult^i1) < 0) goto overflow
#define suboverflow() if ((i1^i2) < 0 && (iresult^i2) >= 0) goto overflow
#endif
#define muloverflow() if (((short)i1 != i1 || (short)i2 != i2) && \
((i2 != 0 && iresult/i2 != i1) || \
(i1 == LONG_MIN && i2 == -1))) goto overflow
static char notarray[] = "scalar \"%s\" cannot be used as array";
static char badarray[] = "array \"%s\" cannot be used as a scalar";
static char varnotfunc[] = "variable \"%s\" cannot be used as a function";
static char tmfld[] = "Too many fields (LIMIT: %d)";
static char toolong[] = "Record too long (LIMIT: %d bytes)";
static char divzero[] = "division (/ or %%) by zero";
static char toodeep[] = "too deeply nested for in loop (LIMIT: %d)";
static wchar_t numbuf[NUMSIZE]; /* Used to convert INTs to strings */
static wchar_t *fields[NFIELD]; /* Cache of pointers into fieldbuf */
static wchar_t *fieldbuf; /* '\0' separated copy of linebuf */
static NODE nodes[NSNODE]; /* Cache of quick access nodes */
static NODE *fnodep = &nodes[0];
#define NINDEXBUF 50
static wchar_t indexbuf[NINDEXBUF]; /* Used for simple array indices */
static int concflag; /* In CONCAT operation (no frees) */
static NODE *retval; /* Last return value of a function */
/*
* The following stack is used to store the next pointers for all nested
* for-in loops. This needs to be global so that delete can check to see
* if it is deleting the next node to be used by a loop.
*/
#define NFORINLOOP 10
static NODE* forindex[NFORINLOOP];
static NODE** next_forin = forindex;
/*
* Assign a string directly to a NODE without creating an intermediate
* NODE. This can handle either FALLOC, FSTATIC, FNOALLOC or FSENSE for
* "flags" argument. Also the NODE "np" must be reduced to an lvalue
* (PARM nodes are not acceptable).
*/
void
strassign(NODE *np, STRING string, int flags, size_t length)
{
if (np->n_type == FUNC)
awkerr(gettext("attempt to redefine builtin function"));
else if (np->n_type == GETLINE || np->n_type == KEYWORD)
awkerr(gettext("inadmissible use of reserved keyword"));
if (np->n_flags & FSPECIAL) {
(void) nassign(np, stringnode(string, flags, length));
return;
}
if (isastring(np->n_flags))
free((wchar_t *)np->n_string);
np->n_strlen = length++;
if (flags & FALLOC) {
length *= sizeof (wchar_t);
np->n_string = (STRING) emalloc(length);
(void) memcpy((void *)np->n_string, string, length);
} else {
np->n_string = string;
if (flags & FNOALLOC) {
flags &= ~FNOALLOC;
flags |= FALLOC;
}
}
np->n_flags &= FSAVE;
if (flags & FSENSE) {
flags &= ~FSENSE;
flags |= type_of(np);
} else
flags |= FSTRING;
np->n_flags |= flags;
}
/*
* Assign to a variable node.
* LHS must be a VAR type and RHS must be reduced by now.
* To speed certain operations up, check for
* certain things here and do special assignments.
*/
NODE *
nassign(NODE *np, NODE *value)
{
register wchar_t *cp;
register int len;
/* short circuit assignment of a node to itself */
if (np == value)
return (np);
if (np->n_flags & FSPECIAL) {
if (np == varRS || np == varFS) {
if (isastring(np->n_flags))
free((void *)np->n_string);
len = sizeof (wchar_t) * ((np->n_strlen =
wcslen(cp = exprstring(value)))+1);
np->n_string = emalloc(len);
(void) memcpy((wchar_t *)np->n_string, cp, len);
np->n_flags = FALLOC|FSTRING|FSPECIAL;
if (np == varRS) {
if (np->n_string[0] == '\n')
awkrecord = defrecord;
else if (np->n_string[0] == '\0')
awkrecord = multirecord;
else
awkrecord = charrecord;
} else if (np == varFS) {
if (resep != (REGEXP)NULL) {
REGWFREE(resep);
resep = (REGEXP)NULL;
}
if (wcslen((wchar_t *)np->n_string) > 1)
setrefield(np);
else if (np->n_string[0] == ' ')
awkfield = whitefield;
else
awkfield = blackfield;
}
return (np);
}
}
if (isastring(np->n_flags))
free((wchar_t *)np->n_string);
if (isstring(value->n_flags)) {
np->n_strlen = value->n_strlen;
if (value->n_flags&FALLOC || value->n_string != _null) {
len = (np->n_strlen+1) * sizeof (wchar_t);
np->n_string = emalloc(len);
(void) memcpy(np->n_string, value->n_string, len);
np->n_flags &= FSAVE;
np->n_flags |= value->n_flags & ~FSAVE;
np->n_flags |= FALLOC;
return (np);
} else
np->n_string = value->n_string;
} else if (value->n_flags & FINT)
np->n_int = value->n_int;
else
np->n_real = value->n_real;
np->n_flags &= FSAVE;
np->n_flags |= value->n_flags & ~FSAVE;
return (np);
}
/*
* Set regular expression FS value.
*/
static void
setrefield(NODE *np)
{
static REGEXP re;
int n;
if ((n = REGWCOMP(&re, np->n_string)) != REG_OK) {
REGWERROR(n, &re, (char *)linebuf, sizeof (linebuf));
awkerr(gettext("syntax error \"%s\" in /%s/\n"),
(char *)linebuf, np->n_string);
}
resep = re;
awkfield = refield;
}
/*
* Assign to an l-value node.
*/
NODE *
assign(NODE *left, NODE *right)
{
if (isleaf(right->n_flags)) {
if (right->n_type == PARM)
right = right->n_next;
} else
right = exprreduce(right);
top:
switch (left->n_type) {
case INDEX:
left = exprreduce(left);
/* FALLTHROUGH */
case VAR:
return (nassign(left, right));
case PARM:
/*
* If it's a parameter then link to the actual value node and
* do the checks again.
*/
left = left->n_next;
goto top;
case FIELD:
return (lfield(exprint(left->n_left), right));
case CALLUFUNC:
case UFUNC:
awkerr(gettext("cannot assign to function \"%s\""),
left->n_name);
default:
awkerr(gettext("lvalue required in assignment"));
}
/* NOTREACHED */
return (0);
}
/*
* Compiled tree non-terminal node.
*/
NODE *
node(int type, NODE *left, NODE *right)
{
register NODE *np;
np = emptynode(type, 0);
np->n_left = left;
np->n_right = right;
np->n_lineno = lineno;
return (np);
}
/*
* Create an integer node.
*/
NODE *
intnode(INT i)
{
register NODE *np;
np = emptynode(CONSTANT, 0);
np->n_flags = FINT|FVINT;
np->n_int = i;
return (np);
}
/*
* Create a real number node.
*/
NODE *
realnode(REAL real)
{
register NODE *np;
np = emptynode(CONSTANT, 0);
np->n_flags = FREAL|FVREAL;
np->n_real = real;
return (np);
}
/*
* Make a node for a string.
*/
NODE *
stringnode(STRING s, int how, size_t length)
{
register NODE *np;
np = emptynode(CONSTANT, 0);
np->n_strlen = length;
if (how & FALLOC) {
np->n_string = emalloc(length = (length+1) * sizeof (wchar_t));
(void) memcpy(np->n_string, s, length);
} else {
np->n_string = s;
if (how & FNOALLOC) {
how &= ~FNOALLOC;
how |= FALLOC;
}
}
if (how & FSENSE) {
np->n_flags = type_of(np);
how &= ~FSENSE;
} else
np->n_flags = FSTRING;
np->n_flags |= how;
return (np);
}
/*
* Save a copy of a string.
*/
STRING
strsave(wchar_t *old)
{
STRING new;
register size_t len;
new = (STRING)emalloc(len = (wcslen(old)+1) * sizeof (wchar_t));
(void) memcpy(new, old, len);
return (new);
}
/*
* Allocate an empty node of given type.
* String space for the node is given by `length'.
*/
NODE *
emptynode(int type, size_t length)
{
register NODE *np;
if (length == 0 && running && fnodep < &nodes[NSNODE]) {
np = fnodep++;
} else {
np = (NODE *)emalloc(sizeof (NODE) +
(length * sizeof (wchar_t)));
if (running && type != VAR && type != ARRAY) {
np->n_next = freelist;
freelist = np;
}
}
np->n_flags = FNONTOK;
np->n_type = type;
np->n_alink = NNULL;
return (np);
}
/*
* Free a node.
*/
void
freenode(NODE *np)
{
if (isastring(np->n_flags))
free((wchar_t *)np->n_string);
else if (np->n_type == RE) {
REGWFREE(np->n_regexp);
}
free((wchar_t *)np);
}
/*
* Install a keyword of given `type'.
*/
void
kinstall(LOCCHARP name, int type)
{
register NODE *np;
register size_t l;
l = wcslen(name);
np = emptynode(KEYWORD, l);
np->n_keywtype = type;
(void) memcpy(np->n_name, name, (l+1) * sizeof (wchar_t));
addsymtab(np);
}
/*
* Install built-in function.
*/
NODE *
finstall(LOCCHARP name, FUNCTION func, int type)
{
register NODE *np;
register size_t l;
l = wcslen(name);
np = emptynode(type, l);
np->n_function = func;
(void) memcpy(np->n_name, name, (l+1) * sizeof (wchar_t));
addsymtab(np);
return (np);
}
/*
* Lookup an identifier.
* nocreate contains the following flag values:
* 1 if no creation of a new NODE,
* 0 if ok to create new NODE
*/
NODE *
vlookup(wchar_t *name, int nocreate)
{
register ushort_t hash;
register NODE *np;
np = symtab[hashbuck(hash = dohash((wchar_t *)name))];
while (np != NNULL) {
if (np->n_hash == hash && wcscmp(name, np->n_name) == 0)
return (np);
np = np->n_next;
}
if (nocreate) {
np = NNULL;
} else {
np = emptynode(VAR, hash = wcslen(name));
np->n_flags = FSTRING|FVINT;
np->n_strlen = 0;
np->n_string = _null;
(void) memcpy(np->n_name, name,
(hash+1) * sizeof (wchar_t));
addsymtab(np);
}
return (np);
}
/*
* Add a symbol to the table.
*/
void
addsymtab(NODE *np)
{
register NODE **spp;
np->n_hash = dohash((wchar_t *)np->n_name);
spp = &symtab[hashbuck(np->n_hash)];
np->n_next = *spp;
*spp = np;
}
/*
* Delete the given node from the symbol table.
* If fflag is non-zero, also free the node space.
* This routine must also check the stack of forin loop pointers. If
* we are deleting the next item to be used, then the pointer must be
* advanced.
*/
void
delsymtab(NODE *np, int fflag)
{
register NODE *rnp;
register NODE *prevp;
register NODE **sptr;
register ushort_t h;
h = hashbuck(np->n_hash);
prevp = NNULL;
for (rnp = symtab[h]; rnp != NNULL; rnp = rnp->n_next) {
if (rnp == np) {
/*
* check all of the for-in loop pointers
* to see if any need to be advanced because
* this element is being deleted.
*/
if (next_forin != forindex) {
sptr = next_forin;
do {
if (*--sptr == rnp) {
*sptr = rnp->n_next;
break;
}
} while (sptr != forindex);
}
if (prevp == NNULL)
symtab[h] = rnp->n_next; else
prevp->n_next = rnp->n_next;
if (fflag)
freenode(rnp);
break;
}
prevp = rnp;
}
}
/*
* Hashing function.
*/
static int
dohash(wchar_t *name)
{
register int hash = 0;
while (*name != '\0')
hash += *name++;
return (hash);
}
/*
* Top level executor for an awk programme.
* This will be passed: pattern, action or a list of these.
* The former function to evaluate a pattern has been
* subsumed into this function for speed.
* Patterns are:
* BEGIN,
* END,
* other expressions (including regular expressions)
*/
void
execute(NODE *wp)
{
register NODE *np;
register int type;
register NODE *tnp;
curnode = wp;
if (phase != 0) {
linebuf[0] = '\0';
lbuflen = 0;
}
while (wp != NNULL) {
if (wp->n_type == COMMA) {
np = wp->n_left;
wp = wp->n_right;
} else {
np = wp;
wp = NNULL;
}
if (np->n_type != PACT)
awkerr(interr, "PACT");
/*
* Save the parent node and evaluate the pattern.
* If it evaluates to false (0) just continue
* to the next pattern/action (PACT) pair.
*/
tnp = np;
np = np->n_left;
if (np == NNULL) {
if (phase != 0)
continue;
} else if (phase != 0) {
if (np->n_type != phase)
continue;
} else if ((type = np->n_type) == BEGIN || type == END) {
continue;
} else if (type == COMMA) {
/*
* The grammar only allows expressions
* to be separated by the ',' operator
* for range patterns.
*/
if (np->n_flags & FMATCH) {
if (exprint(np->n_right) != 0)
np->n_flags &= ~FMATCH;
} else if (exprint(np->n_left) != 0) {
if (exprint(np->n_right) == 0)
np->n_flags |= FMATCH;
} else
continue;
} else if (exprint(np) == 0)
continue;
np = tnp;
if (action(np->n_right)) {
loopexit = 0;
break;
}
}
if (freelist != NNULL)
freetemps();
}
/*
* Free all temporary nodes.
*/
static void
freetemps()
{
register NODE *np, *nnp;
if (concflag)
return;
for (np = &nodes[0]; np < fnodep; np++) {
if (isastring(np->n_flags)) {
free((wchar_t *)np->n_string);
} else if (np->n_type == RE) {
REGWFREE(np->n_regexp);
}
}
fnodep = &nodes[0];
for (np = freelist; np != NNULL; np = nnp) {
nnp = np->n_next;
freenode(np);
}
freelist = NNULL;
}
/*
* Do the given action.
* Actions are statements or expressions.
*/
static int
action(NODE *wp)
{
register NODE *np;
register int act = 0;
register NODE *l;
while (wp != NNULL) {
if (wp->n_type == COMMA) {
np = wp->n_left;
wp = wp->n_right;
} else {
np = wp;
wp = NNULL;
}
if (freelist != NNULL)
freetemps();
curnode = np;
/*
* Don't change order of these cases without
* changing order in awk.y declarations.
* The order is optimised.
*/
switch (np->n_type) {
case ASG:
(void) assign(np->n_left, np->n_right);
continue;
case PRINT:
s_print(np);
continue;
case PRINTF:
s_prf(np);
continue;
case EXIT:
if (np->n_left != NNULL)
act = (int)exprint(np->n_left); else
act = 0;
doend(act);
/* NOTREACHED */
case RETURN:
if (slevel == 0)
awkerr(gettext("return outside of a function"));
np = np->n_left != NNULL
? exprreduce(np->n_left)
: const0;
retval = emptynode(CONSTANT, 0);
retval->n_flags = FINT;
(void) nassign(retval, np);
return (RETURN);
case NEXT:
loopexit = NEXT;
/* FALLTHROUGH */
case BREAK:
case CONTINUE:
return (np->n_type);
case DELETE:
if ((l = np->n_left)->n_type == PARM) {
l = l->n_next;
if (!(l->n_flags & FLARRAY))
l = l->n_alink;
}
switch (l->n_type) {
case ARRAY:
delarray(l);
break;
case INDEX:
if ((np = l->n_left)->n_type == PARM) {
np = np->n_next;
if (!(np->n_flags & FLARRAY))
np = np->n_alink;
}
/*
* get pointer to the node for this array
* element using the hash key.
*/
l = exprreduce(l);
/*
* now search linearly from the beginning of
* the list to find the element before the
* one being deleted. This must be done
* because arrays are singley-linked.
*/
while (np != NNULL) {
if (np->n_alink == l) {
np->n_alink = l->n_alink;
break;
}
np = np->n_alink;
}
delsymtab(l, 1);
break;
case VAR:
if (isstring(l->n_flags) &&
l->n_string == _null)
break;
/* FALLTHROUGH */
default:
awkerr(gettext(
"may delete only array element or array"));
break;
}
continue;
case WHILE:
case DO:
if ((act = s_while(np)) != 0)
break;
continue;
case FOR:
if ((act = s_for(np)) != 0)
break;
continue;
case FORIN:
if ((act = s_forin(np)) != 0)
break;
continue;
case IF:
if ((act = s_if(np)) != 0)
break;
continue;
default:
(void) exprreduce(np);
if (loopexit != 0) {
act = loopexit;
break;
}
continue;
}
return (act);
}
return (0);
}
/*
* Delete an entire array
*/
void
delarray(NODE *np)
{
register NODE *nnp;
nnp = np->n_alink;
np->n_alink = NNULL;
while (nnp != NNULL) {
np = nnp->n_alink;
delsymtab(nnp, 1);
nnp = np;
}
}
/*
* Return the INT value of an expression.
*/
INT
exprint(NODE *np)
{
if (isleaf(np->n_flags)) {
if (np->n_type == PARM)
np = np->n_next;
goto leaf;
}
np = exprreduce(np);
switch (np->n_type) {
case CONSTANT:
case VAR:
leaf:
if (np->n_flags & FINT)
return (np->n_int);
if (np->n_flags & FREAL)
return ((INT)np->n_real);
return ((INT)wcstoll(np->n_string, NULL, 10));
default:
awkerr(interr, "exprint");
}
/* NOTREACHED */
return (0);
}
/*
* Return a real number from an expression tree.
*/
REAL
exprreal(NODE *np)
{
if (loopexit)
return ((REAL)loopexit);
if (isleaf(np->n_flags)) {
if (np->n_type == PARM)
np = np->n_next;
goto leaf;
}
np = exprreduce(np);
switch (np->n_type) {
case CONSTANT:
case VAR:
leaf:
if (np->n_flags & FREAL)
return (np->n_real);
if (np->n_flags & FINT)
return ((REAL)np->n_int);
return ((REAL)wcstod((wchar_t *)np->n_string, (wchar_t **)0));
default:
awkerr(interr, "exprreal");
}
/* NOTREACHED */
return ((REAL)0);
}
/*
* Return a string from an expression tree.
*/
STRING
exprstring(NODE *np)
{
if (isleaf(np->n_flags)) {
if (np->n_type == PARM)
np = np->n_next;
goto leaf;
}
np = exprreduce(np);
switch (np->n_type) {
case CONSTANT:
case VAR:
leaf:
if (isstring(np->n_flags))
return (np->n_string);
if (np->n_flags & FINT)
return (STRING)lltoa((long long)np->n_int);
{
char *tmp;
(void) wsprintf(numbuf,
(const char *) (tmp = wcstombsdup(exprstring(varCONVFMT))),
(double)np->n_real);
if (tmp != NULL)
free(tmp);
}
return ((STRING)numbuf);
default:
awkerr(interr, "exprstring");
}
/* NOTREACHED */
return (0);
}
/*
* Convert number to string.
*/
static wchar_t *
lltoa(long long l)
{
register wchar_t *p = &numbuf[NUMSIZE];
register int s;
register int neg;
static wchar_t zero[] = M_MB_L("0");
if (l == 0)
return (zero);
*--p = '\0';
if (l < 0)
neg = 1, l = -l; else
neg = 0;
if ((s = (short)l) == l) {
while (s != 0) {
*--p = s%10 + '0';
s /= 10;
}
} else {
while (l != 0) {
*--p = l%10 + '0';
l /= 10;
}
}
if (neg)
*--p = '-';
return (wcscpy(numbuf, p));
}
/*
* Return pointer to node with concatenation of operands of CONCAT node.
* In the interest of speed, a left recursive tree of CONCAT nodes
* is handled with a single malloc. The accumulated lengths of the
* right operands are passed down recursive invocations of this
* routine, which allocates a large enough string when the left
* operand is not a CONCAT node.
*/
static NODE *
exprconcat(NODE *np, int len)
{
/* we KNOW (np->n_type==CONCAT) */
register NODE *lnp = np->n_left;
register NODE *rnp = np->n_right;
register STRING rsp;
int rlen;
size_t llen;
wchar_t *cp;
wchar_t rnumbuf[NUMSIZE];
if (isleaf(rnp->n_flags) && rnp->n_type == PARM)
rnp = rnp->n_next;
if (isstring(rnp->n_flags)) {
rsp = rnp->n_string;
rlen = rnp->n_strlen;
} else
rlen = wcslen((wchar_t *)(rsp = exprstring(rnp)));
if (rsp == numbuf) { /* static, so save a copy */
(void) memcpy(rnumbuf, (wchar_t *)rsp,
(rlen+1) * sizeof (wchar_t));
rsp = rnumbuf;
}
len += rlen;
if (lnp->n_type == CONCAT) {
lnp = exprconcat(lnp, len);
cp = lnp->n_string;
llen = lnp->n_strlen;
} else {
register STRING lsp;
if (isleaf(lnp->n_flags) && lnp->n_type == PARM)
lnp = lnp->n_next;
if (isstring(lnp->n_flags)) {
lsp = lnp->n_string;
llen = lnp->n_strlen;
} else
llen = wcslen((wchar_t *)(lsp = exprstring(lnp)));
cp = emalloc((llen+len+1) * sizeof (wchar_t));
(void) memcpy(cp, (wchar_t *)lsp, llen * sizeof (wchar_t));
lnp = stringnode(cp, FNOALLOC, llen);
}
(void) memcpy(cp+llen, (wchar_t *)rsp, (rlen+1) * sizeof (wchar_t));
lnp->n_strlen += rlen;
return (lnp);
}
/*
* Reduce an expression to a terminal node.
*/
NODE *
exprreduce(NODE *np)
{
register wchar_t *cp;