-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.c
2013 lines (1878 loc) · 44.9 KB
/
run.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
/****************************************************************
Copyright (C) Lucent Technologies 1997
All Rights Reserved
Permission to use, copy, modify, and distribute this software and
its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name Lucent Technologies or any of
its entities not be used in advertising or publicity pertaining
to distribution of the software without specific, written prior
permission.
LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
****************************************************************/
#define DEBUG
#include <stdio.h>
#include <ctype.h>
#include <setjmp.h>
#include <limits.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "awk.h"
#include "ytab.h"
#define tempfree(x) if (istemp(x)) tfree(x); else
/*
#undef tempfree
void tempfree(Cell *p) {
if (p->ctype == OCELL && (p->csub < CUNK || p->csub > CFREE)) {
WARNING("bad csub %d in Cell %d %s",
p->csub, p->ctype, p->sval);
}
if (istemp(p))
tfree(p);
}
*/
/* do we really need these? */
/* #ifdef _NFILE */
/* #ifndef FOPEN_MAX */
/* #define FOPEN_MAX _NFILE */
/* #endif */
/* #endif */
/* */
/* #ifndef FOPEN_MAX */
/* #define FOPEN_MAX 40 */ /* max number of open files */
/* #endif */
/* */
/* #ifndef RAND_MAX */
/* #define RAND_MAX 32767 */ /* all that ansi guarantees */
/* #endif */
jmp_buf env;
extern int pairstack[];
extern Awkfloat srand_seed;
Node *winner = NULL; /* root of parse tree */
Cell *tmps; /* free temporary cells for execution */
static Cell truecell ={ OBOOL, BTRUE, 0, 0, 1.0, NUM, NULL };
Cell *True = &truecell;
static Cell falsecell ={ OBOOL, BFALSE, 0, 0, 0.0, NUM, NULL };
Cell *False = &falsecell;
static Cell breakcell ={ OJUMP, JBREAK, 0, 0, 0.0, NUM, NULL };
Cell *jbreak = &breakcell;
static Cell contcell ={ OJUMP, JCONT, 0, 0, 0.0, NUM, NULL };
Cell *jcont = &contcell;
static Cell nextcell ={ OJUMP, JNEXT, 0, 0, 0.0, NUM, NULL };
Cell *jnext = &nextcell;
static Cell nextfilecell ={ OJUMP, JNEXTFILE, 0, 0, 0.0, NUM, NULL };
Cell *jnextfile = &nextfilecell;
static Cell exitcell ={ OJUMP, JEXIT, 0, 0, 0.0, NUM, NULL };
Cell *jexit = &exitcell;
static Cell retcell ={ OJUMP, JRET, 0, 0, 0.0, NUM, NULL };
Cell *jret = &retcell;
static Cell tempcell ={ OCELL, CTEMP, 0, "", 0.0, NUM|STR|DONTFREE, NULL };
Node *curnode = NULL; /* the node being executed, for debugging */
/* buffer memory management */
int adjbuf(char **pbuf, int *psiz, int minlen, int quantum, char **pbptr,
const char *whatrtn)
/* pbuf: address of pointer to buffer being managed
* psiz: address of buffer size variable
* minlen: minimum length of buffer needed
* quantum: buffer size quantum
* pbptr: address of movable pointer into buffer, or 0 if none
* whatrtn: name of the calling routine if failure should cause fatal error
*
* return 0 for realloc failure, !=0 for success
*/
{
if (minlen > *psiz) {
char *tbuf;
int rminlen = quantum ? minlen % quantum : 0;
int boff = pbptr ? *pbptr - *pbuf : 0;
/* round up to next multiple of quantum */
if (rminlen)
minlen += quantum - rminlen;
tbuf = (char *) realloc(*pbuf, minlen);
dprintf( ("adjbuf %s: %d %d (pbuf=%p, tbuf=%p)\n", whatrtn, *psiz, minlen, (void *) *pbuf, (void *) tbuf) );
if (tbuf == NULL) {
if (whatrtn)
FATAL("out of memory in %s", whatrtn);
return 0;
}
*pbuf = tbuf;
*psiz = minlen;
if (pbptr)
*pbptr = tbuf + boff;
}
return 1;
}
void run(Node *a) /* execution of parse tree starts here */
{
extern void stdinit(void);
stdinit();
execute(a);
closeall();
}
Cell *execute(Node *u) /* execute a node of the parse tree */
{
Cell *(*proc)(Node **, int);
Cell *x;
Node *a;
if (u == NULL)
return(True);
for (a = u; ; a = a->nnext) {
curnode = a;
if (isvalue(a)) {
x = (Cell *) (a->narg[0]);
if (isfld(x) && !donefld)
fldbld();
else if (isrec(x) && !donerec)
recbld();
return(x);
}
if (notlegal(a->nobj)) /* probably a Cell* but too risky to print */
FATAL("illegal statement");
proc = proctab[a->nobj-FIRSTTOKEN];
x = (*proc)(a->narg, a->nobj);
if (isfld(x) && !donefld)
fldbld();
else if (isrec(x) && !donerec)
recbld();
if (isexpr(a))
return(x);
if (isjump(x))
return(x);
if (a->nnext == NULL)
return(x);
tempfree(x);
}
}
Cell *program(Node **a, int n) /* execute an awk program */
{ /* a[0] = BEGIN, a[1] = body, a[2] = END */
Cell *x;
if (setjmp(env) != 0)
goto ex;
if (a[0]) { /* BEGIN */
x = execute(a[0]);
if (isexit(x))
return(True);
if (isjump(x))
FATAL("illegal break, continue, next or nextfile from BEGIN");
tempfree(x);
}
if (a[1] || a[2])
while (getrec(&record, &recsize, 1) > 0) {
x = execute(a[1]);
if (isexit(x))
break;
tempfree(x);
}
ex:
if (setjmp(env) != 0) /* handles exit within END */
goto ex1;
if (a[2]) { /* END */
x = execute(a[2]);
if (isbreak(x) || isnext(x) || iscont(x))
FATAL("illegal break, continue, next or nextfile from END");
tempfree(x);
}
ex1:
return(True);
}
struct Frame { /* stack frame for awk function calls */
int nargs; /* number of arguments in this call */
Cell *fcncell; /* pointer to Cell for function */
Cell **args; /* pointer to array of arguments after execute */
Cell *retval; /* return value */
};
#define NARGS 50 /* max args in a call */
struct Frame *frame = NULL; /* base of stack frames; dynamically allocated */
int nframe = 0; /* number of frames allocated */
struct Frame *fp = NULL; /* frame pointer. bottom level unused */
Cell *call(Node **a, int n) /* function call. very kludgy and fragile */
{
static Cell newcopycell = { OCELL, CCOPY, 0, "", 0.0, NUM|STR|DONTFREE, NULL };
int i, ncall, ndef;
int freed = 0; /* handles potential double freeing when fcn & param share a tempcell */
Node *x;
Cell *args[NARGS], *oargs[NARGS]; /* BUG: fixed size arrays */
Cell *y, *z, *fcn;
char *s;
fcn = execute(a[0]); /* the function itself */
s = fcn->nval;
if (!isfcn(fcn))
FATAL("calling undefined function %s", s);
if (frame == NULL) {
fp = frame = (struct Frame *) calloc(nframe += 100, sizeof(struct Frame));
if (frame == NULL)
FATAL("out of space for stack frames calling %s", s);
}
for (ncall = 0, x = a[1]; x != NULL; x = x->nnext) /* args in call */
ncall++;
ndef = (int) fcn->fval; /* args in defn */
dprintf( ("calling %s, %d args (%d in defn), fp=%d\n", s, ncall, ndef, (int) (fp-frame)) );
if (ncall > ndef)
WARNING("function %s called with %d args, uses only %d",
s, ncall, ndef);
if (ncall + ndef > NARGS)
FATAL("function %s has %d arguments, limit %d", s, ncall+ndef, NARGS);
for (i = 0, x = a[1]; x != NULL; i++, x = x->nnext) { /* get call args */
dprintf( ("evaluate args[%d], fp=%d:\n", i, (int) (fp-frame)) );
y = execute(x);
oargs[i] = y;
dprintf( ("args[%d]: %s %f <%s>, t=%o\n",
i, NN(y->nval), y->fval, isarr(y) ? "(array)" : NN(y->sval), y->tval) );
if (isfcn(y))
FATAL("can't use function %s as argument in %s", y->nval, s);
if (isarr(y))
args[i] = y; /* arrays by ref */
else
args[i] = copycell(y);
tempfree(y);
}
for ( ; i < ndef; i++) { /* add null args for ones not provided */
args[i] = gettemp();
*args[i] = newcopycell;
}
fp++; /* now ok to up frame */
if (fp >= frame + nframe) {
int dfp = fp - frame; /* old index */
frame = (struct Frame *)
realloc((char *) frame, (nframe += 100) * sizeof(struct Frame));
if (frame == NULL)
FATAL("out of space for stack frames in %s", s);
fp = frame + dfp;
}
fp->fcncell = fcn;
fp->args = args;
fp->nargs = ndef; /* number defined with (excess are locals) */
fp->retval = gettemp();
dprintf( ("start exec of %s, fp=%d\n", s, (int) (fp-frame)) );
y = execute((Node *)(fcn->sval)); /* execute body */
dprintf( ("finished exec of %s, fp=%d\n", s, (int) (fp-frame)) );
for (i = 0; i < ndef; i++) {
Cell *t = fp->args[i];
if (isarr(t)) {
if (t->csub == CCOPY) {
if (i >= ncall) {
freesymtab(t);
t->csub = CTEMP;
tempfree(t);
} else {
oargs[i]->tval = t->tval;
oargs[i]->tval &= ~(STR|NUM|DONTFREE);
oargs[i]->sval = t->sval;
tempfree(t);
}
}
} else if (t != y) { /* kludge to prevent freeing twice */
t->csub = CTEMP;
tempfree(t);
} else if (t == y && t->csub == CCOPY) {
t->csub = CTEMP;
tempfree(t);
freed = 1;
}
}
tempfree(fcn);
if (isexit(y) || isnext(y))
return y;
if (freed == 0) {
tempfree(y); /* don't free twice! */
}
z = fp->retval; /* return value */
dprintf( ("%s returns %g |%s| %o\n", s, getfval(z), getsval(z), z->tval) );
fp--;
return(z);
}
Cell *copycell(Cell *x) /* make a copy of a cell in a temp */
{
Cell *y;
/* copy is not constant or field */
y = gettemp();
y->tval = x->tval & ~(CON|FLD|REC);
y->csub = CCOPY; /* prevents freeing until call is over */
y->nval = x->nval; /* BUG? */
if (isstr(x) /* || x->ctype == OCELL */) {
y->sval = tostring(x->sval);
y->tval &= ~DONTFREE;
} else
y->tval |= DONTFREE;
y->fval = x->fval;
return y;
}
Cell *arg(Node **a, int n) /* nth argument of a function */
{
n = ptoi(a[0]); /* argument number, counting from 0 */
dprintf( ("arg(%d), fp->nargs=%d\n", n, fp->nargs) );
if (n+1 > fp->nargs)
FATAL("argument #%d of function %s was not supplied",
n+1, fp->fcncell->nval);
return fp->args[n];
}
Cell *jump(Node **a, int n) /* break, continue, next, nextfile, return */
{
Cell *y;
switch (n) {
case EXIT:
if (a[0] != NULL) {
y = execute(a[0]);
errorflag = (int) getfval(y);
tempfree(y);
}
longjmp(env, 1);
case RETURN:
if (a[0] != NULL) {
y = execute(a[0]);
if ((y->tval & (STR|NUM)) == (STR|NUM)) {
setsval(fp->retval, getsval(y));
fp->retval->fval = getfval(y);
fp->retval->tval |= NUM;
}
else if (y->tval & STR)
setsval(fp->retval, getsval(y));
else if (y->tval & NUM)
setfval(fp->retval, getfval(y));
else /* can't happen */
FATAL("bad type variable %d", y->tval);
tempfree(y);
}
return(jret);
case NEXT:
return(jnext);
case NEXTFILE:
nextfile();
return(jnextfile);
case BREAK:
return(jbreak);
case CONTINUE:
return(jcont);
default: /* can't happen */
FATAL("illegal jump type %d", n);
}
return 0; /* not reached */
}
Cell *awkgetline(Node **a, int n) /* get next line from specific input */
{ /* a[0] is variable, a[1] is operator, a[2] is filename */
Cell *r, *x;
extern Cell **fldtab;
FILE *fp;
char *buf;
int bufsize = recsize;
int mode;
if ((buf = (char *) malloc(bufsize)) == NULL)
FATAL("out of memory in getline");
fflush(stdout); /* in case someone is waiting for a prompt */
r = gettemp();
if (a[1] != NULL) { /* getline < file */
x = execute(a[2]); /* filename */
mode = ptoi(a[1]);
if (mode == '|') /* input pipe */
mode = LE; /* arbitrary flag */
fp = openfile(mode, getsval(x));
tempfree(x);
if (fp == NULL)
n = -1;
else
n = readrec(&buf, &bufsize, fp);
if (n <= 0) {
;
} else if (a[0] != NULL) { /* getline var <file */
x = execute(a[0]);
setsval(x, buf);
if (is_number(x->sval)) {
x->fval = atof(x->sval);
x->tval |= NUM;
}
tempfree(x);
} else { /* getline <file */
setsval(fldtab[0], buf);
if (is_number(fldtab[0]->sval)) {
fldtab[0]->fval = atof(fldtab[0]->sval);
fldtab[0]->tval |= NUM;
}
}
} else { /* bare getline; use current input */
if (a[0] == NULL) /* getline */
n = getrec(&record, &recsize, 1);
else { /* getline var */
n = getrec(&buf, &bufsize, 0);
x = execute(a[0]);
setsval(x, buf);
if (is_number(x->sval)) {
x->fval = atof(x->sval);
x->tval |= NUM;
}
tempfree(x);
}
}
setfval(r, (Awkfloat) n);
free(buf);
return r;
}
Cell *getnf(Node **a, int n) /* get NF */
{
if (donefld == 0)
fldbld();
return (Cell *) a[0];
}
Cell *array(Node **a, int n) /* a[0] is symtab, a[1] is list of subscripts */
{
Cell *x, *y, *z;
char *s;
Node *np;
char *buf;
int bufsz = recsize;
int nsub;
if ((buf = (char *) malloc(bufsz)) == NULL)
FATAL("out of memory in array");
x = execute(a[0]); /* Cell* for symbol table */
buf[0] = 0;
for (np = a[1]; np; np = np->nnext) {
y = execute(np); /* subscript */
s = getsval(y);
nsub = strlen(getsval(subseploc));
if (!adjbuf(&buf, &bufsz, strlen(buf)+strlen(s)+nsub+1, recsize, 0, "array"))
FATAL("out of memory for %s[%s...]", x->nval, buf);
strcat(buf, s);
if (np->nnext)
strcat(buf, *SUBSEP);
tempfree(y);
}
if (!isarr(x)) {
dprintf( ("making %s into an array\n", NN(x->nval)) );
if (freeable(x))
xfree(x->sval);
x->tval &= ~(STR|NUM|DONTFREE);
x->tval |= ARR;
x->sval = (char *) makesymtab(NSYMTAB);
}
z = setsymtab(buf, "", 0.0, STR|NUM, (Array *) x->sval);
z->ctype = OCELL;
z->csub = CVAR;
tempfree(x);
free(buf);
return(z);
}
Cell *awkdelete(Node **a, int n) /* a[0] is symtab, a[1] is list of subscripts */
{
Cell *x, *y;
Node *np;
char *s;
int nsub;
x = execute(a[0]); /* Cell* for symbol table */
if (!isarr(x))
return True;
if (a[1] == 0) { /* delete the elements, not the table */
freesymtab(x);
x->tval &= ~STR;
x->tval |= ARR;
x->sval = (char *) makesymtab(NSYMTAB);
} else {
int bufsz = recsize;
char *buf;
if ((buf = (char *) malloc(bufsz)) == NULL)
FATAL("out of memory in adelete");
buf[0] = 0;
for (np = a[1]; np; np = np->nnext) {
y = execute(np); /* subscript */
s = getsval(y);
nsub = strlen(getsval(subseploc));
if (!adjbuf(&buf, &bufsz, strlen(buf)+strlen(s)+nsub+1, recsize, 0, "awkdelete"))
FATAL("out of memory deleting %s[%s...]", x->nval, buf);
strcat(buf, s);
if (np->nnext)
strcat(buf, *SUBSEP);
tempfree(y);
}
freeelem(x, buf);
free(buf);
}
tempfree(x);
return True;
}
Cell *intest(Node **a, int n) /* a[0] is index (list), a[1] is symtab */
{
Cell *x, *ap, *k;
Node *p;
char *buf;
char *s;
int bufsz = recsize;
int nsub;
ap = execute(a[1]); /* array name */
if (!isarr(ap)) {
dprintf( ("making %s into an array\n", ap->nval) );
if (freeable(ap))
xfree(ap->sval);
ap->tval &= ~(STR|NUM|DONTFREE);
ap->tval |= ARR;
ap->sval = (char *) makesymtab(NSYMTAB);
}
if ((buf = (char *) malloc(bufsz)) == NULL) {
FATAL("out of memory in intest");
}
buf[0] = 0;
for (p = a[0]; p; p = p->nnext) {
x = execute(p); /* expr */
s = getsval(x);
nsub = strlen(getsval(subseploc));
if (!adjbuf(&buf, &bufsz, strlen(buf)+strlen(s)+nsub+1, recsize, 0, "intest"))
FATAL("out of memory deleting %s[%s...]", x->nval, buf);
strcat(buf, s);
tempfree(x);
if (p->nnext)
strcat(buf, *SUBSEP);
}
k = lookup(buf, (Array *) ap->sval);
tempfree(ap);
free(buf);
if (k == NULL)
return(False);
else
return(True);
}
Cell *matchop(Node **a, int n) /* ~ and match() */
{
Cell *x, *y;
char *s, *t;
int i;
fa *pfa;
int (*mf)(fa *, const char *) = match, mode = 0;
if (n == MATCHFCN) {
mf = pmatch;
mode = 1;
}
x = execute(a[1]); /* a[1] = target text */
s = getsval(x);
if (a[0] == 0) /* a[1] == 0: already-compiled reg expr */
i = (*mf)((fa *) a[2], s);
else {
y = execute(a[2]); /* a[2] = regular expr */
t = getsval(y);
pfa = makedfa(t, mode);
i = (*mf)(pfa, s);
tempfree(y);
}
tempfree(x);
if (n == MATCHFCN) {
int start = patbeg - s + 1;
if (patlen < 0)
start = 0;
setfval(rstartloc, (Awkfloat) start);
setfval(rlengthloc, (Awkfloat) patlen);
x = gettemp();
x->tval = NUM;
x->fval = start;
return x;
} else if ((n == MATCH && i == 1) || (n == NOTMATCH && i == 0))
return(True);
else
return(False);
}
Cell *boolop(Node **a, int n) /* a[0] || a[1], a[0] && a[1], !a[0] */
{
Cell *x, *y;
int i;
x = execute(a[0]);
i = istrue(x);
tempfree(x);
switch (n) {
case BOR:
if (i) return(True);
y = execute(a[1]);
i = istrue(y);
tempfree(y);
if (i) return(True);
else return(False);
case AND:
if ( !i ) return(False);
y = execute(a[1]);
i = istrue(y);
tempfree(y);
if (i) return(True);
else return(False);
case NOT:
if (i) return(False);
else return(True);
default: /* can't happen */
FATAL("unknown boolean operator %d", n);
}
return 0; /*NOTREACHED*/
}
Cell *relop(Node **a, int n) /* a[0 < a[1], etc. */
{
int i;
Cell *x, *y;
Awkfloat j;
x = execute(a[0]);
y = execute(a[1]);
if (x->tval&NUM && y->tval&NUM) {
j = x->fval - y->fval;
i = j<0? -1: (j>0? 1: 0);
} else {
i = strcmp(getsval(x), getsval(y));
}
tempfree(x);
tempfree(y);
switch (n) {
case LT: if (i<0) return(True);
else return(False);
case LE: if (i<=0) return(True);
else return(False);
case NE: if (i!=0) return(True);
else return(False);
case EQ: if (i == 0) return(True);
else return(False);
case GE: if (i>=0) return(True);
else return(False);
case GT: if (i>0) return(True);
else return(False);
default: /* can't happen */
FATAL("unknown relational operator %d", n);
}
return 0; /*NOTREACHED*/
}
void tfree(Cell *a) /* free a tempcell */
{
if (freeable(a)) {
dprintf( ("freeing %s %s %o\n", NN(a->nval), NN(a->sval), a->tval) );
xfree(a->sval);
}
if (a == tmps)
FATAL("tempcell list is curdled");
a->cnext = tmps;
tmps = a;
}
Cell *gettemp(void) /* get a tempcell */
{ int i;
Cell *x;
if (!tmps) {
tmps = (Cell *) calloc(100, sizeof(Cell));
if (!tmps)
FATAL("out of space for temporaries");
for(i = 1; i < 100; i++)
tmps[i-1].cnext = &tmps[i];
tmps[i-1].cnext = 0;
}
x = tmps;
tmps = x->cnext;
*x = tempcell;
return(x);
}
Cell *indirect(Node **a, int n) /* $( a[0] ) */
{
Awkfloat val;
Cell *x;
int m;
char *s;
x = execute(a[0]);
val = getfval(x); /* freebsd: defend against super large field numbers */
if ((Awkfloat)INT_MAX < val)
FATAL("trying to access out of range field %s", x->nval);
m = (int) val;
if (m == 0 && !is_number(s = getsval(x))) /* suspicion! */
FATAL("illegal field $(%s), name \"%s\"", s, x->nval);
/* BUG: can x->nval ever be null??? */
tempfree(x);
x = fieldadr(m);
x->ctype = OCELL; /* BUG? why are these needed? */
x->csub = CFLD;
return(x);
}
Cell *substr(Node **a, int nnn) /* substr(a[0], a[1], a[2]) */
{
int k, m, n;
char *s;
int temp;
Cell *x, *y, *z = 0;
x = execute(a[0]);
y = execute(a[1]);
if (a[2] != 0)
z = execute(a[2]);
s = getsval(x);
k = strlen(s) + 1;
if (k <= 1) {
tempfree(x);
tempfree(y);
if (a[2] != 0) {
tempfree(z);
}
x = gettemp();
setsval(x, "");
return(x);
}
m = (int) getfval(y);
if (m <= 0)
m = 1;
else if (m > k)
m = k;
tempfree(y);
if (a[2] != 0) {
n = (int) getfval(z);
tempfree(z);
} else
n = k - 1;
if (n < 0)
n = 0;
else if (n > k - m)
n = k - m;
dprintf( ("substr: m=%d, n=%d, s=%s\n", m, n, s) );
y = gettemp();
temp = s[n+m-1]; /* with thanks to John Linderman */
s[n+m-1] = '\0';
setsval(y, s + m - 1);
s[n+m-1] = temp;
tempfree(x);
return(y);
}
Cell *sindex(Node **a, int nnn) /* index(a[0], a[1]) */
{
Cell *x, *y, *z;
char *s1, *s2, *p1, *p2, *q;
Awkfloat v = 0.0;
x = execute(a[0]);
s1 = getsval(x);
y = execute(a[1]);
s2 = getsval(y);
z = gettemp();
for (p1 = s1; *p1 != '\0'; p1++) {
for (q=p1, p2=s2; *p2 != '\0' && *q == *p2; q++, p2++)
;
if (*p2 == '\0') {
v = (Awkfloat) (p1 - s1 + 1); /* origin 1 */
break;
}
}
tempfree(x);
tempfree(y);
setfval(z, v);
return(z);
}
#define MAXNUMSIZE 50
int format(char **pbuf, int *pbufsize, const char *s, Node *a) /* printf-like conversions */
{
char *fmt;
char *p, *t;
const char *os;
Cell *x;
int flag = 0, n;
int fmtwd; /* format width */
int fmtsz = recsize;
char *buf = *pbuf;
int bufsize = *pbufsize;
static int first = 1;
static int have_a_format = 0;
if (first) {
char buf[100];
sprintf(buf, "%a", 42.0);
have_a_format = (strcmp(buf, "0x1.5p+5") == 0);
first = 0;
}
os = s;
p = buf;
if ((fmt = (char *) malloc(fmtsz)) == NULL)
FATAL("out of memory in format()");
while (*s) {
adjbuf(&buf, &bufsize, MAXNUMSIZE+1+p-buf, recsize, &p, "format1");
if (*s != '%') {
*p++ = *s++;
continue;
}
if (*(s+1) == '%') {
*p++ = '%';
s += 2;
continue;
}
/* have to be real careful in case this is a huge number, eg, %100000d */
fmtwd = atoi(s+1);
if (fmtwd < 0)
fmtwd = -fmtwd;
adjbuf(&buf, &bufsize, fmtwd+1+p-buf, recsize, &p, "format2");
for (t = fmt; (*t++ = *s) != '\0'; s++) {
if (!adjbuf(&fmt, &fmtsz, MAXNUMSIZE+1+t-fmt, recsize, &t, "format3"))
FATAL("format item %.30s... ran format() out of memory", os);
if (isalpha((uschar)*s) && *s != 'l' && *s != 'h' && *s != 'L')
break; /* the ansi panoply */
if (*s == '$') {
FATAL("'$' not permitted in awk formats");
}
if (*s == '*') {
if (a == NULL) {
FATAL("not enough args in printf(%s)", os);
}
x = execute(a);
a = a->nnext;
sprintf(t-1, "%d", fmtwd=(int) getfval(x));
if (fmtwd < 0)
fmtwd = -fmtwd;
adjbuf(&buf, &bufsize, fmtwd+1+p-buf, recsize, &p, "format");
t = fmt + strlen(fmt);
tempfree(x);
}
}
*t = '\0';
if (fmtwd < 0)
fmtwd = -fmtwd;
adjbuf(&buf, &bufsize, fmtwd+1+p-buf, recsize, &p, "format4");
switch (*s) {
case 'a': case 'A':
if (have_a_format)
flag = *s;
else
flag = 'f';
break;
case 'f': case 'e': case 'g': case 'E': case 'G':
flag = 'f';
break;
case 'd': case 'i':
flag = 'd';
if(*(s-1) == 'l') break;
*(t-1) = 'l';
*t = 'd';
*++t = '\0';
break;
case 'o': case 'x': case 'X': case 'u':
flag = *(s-1) == 'l' ? 'd' : 'u';
break;
case 's':
flag = 's';
break;
case 'c':
flag = 'c';
break;
default:
WARNING("weird printf conversion %s", fmt);
flag = '?';
break;
}
if (a == NULL)
FATAL("not enough args in printf(%s)", os);
x = execute(a);
a = a->nnext;
n = MAXNUMSIZE;
if (fmtwd > n)
n = fmtwd;
adjbuf(&buf, &bufsize, 1+n+p-buf, recsize, &p, "format5");
switch (flag) {
case '?': sprintf(p, "%s", fmt); /* unknown, so dump it too */
t = getsval(x);
n = strlen(t);
if (fmtwd > n)
n = fmtwd;
adjbuf(&buf, &bufsize, 1+strlen(p)+n+p-buf, recsize, &p, "format6");
p += strlen(p);
sprintf(p, "%s", t);
break;
case 'a':
case 'A':
case 'f': sprintf(p, fmt, getfval(x)); break;
case 'd': sprintf(p, fmt, (long) getfval(x)); break;
case 'u': sprintf(p, fmt, (int) getfval(x)); break;
case 's':
t = getsval(x);
n = strlen(t);
if (fmtwd > n)
n = fmtwd;
if (!adjbuf(&buf, &bufsize, 1+n+p-buf, recsize, &p, "format7"))
FATAL("huge string/format (%d chars) in printf %.30s... ran format() out of memory", n, t);
sprintf(p, fmt, t);
break;
case 'c':
if (isnum(x)) {
if (getfval(x))
sprintf(p, fmt, (int) getfval(x));
else {
*p++ = '\0'; /* explicit null byte */
*p = '\0'; /* next output will start here */
}
} else
sprintf(p, fmt, getsval(x)[0]);
break;
default:
FATAL("can't happen: bad conversion %c in format()", flag);
}
tempfree(x);
p += strlen(p);
s++;
}
*p = '\0';
free(fmt);
for ( ; a; a = a->nnext) /* evaluate any remaining args */
execute(a);
*pbuf = buf;
*pbufsize = bufsize;
return p - buf;
}
Cell *awksprintf(Node **a, int n) /* sprintf(a[0]) */
{
Cell *x;
Node *y;
char *buf;
int bufsz=3*recsize;
if ((buf = (char *) malloc(bufsz)) == NULL)
FATAL("out of memory in awksprintf");
y = a[0]->nnext;
x = execute(a[0]);
if (format(&buf, &bufsz, getsval(x), y) == -1)
FATAL("sprintf string %.30s... too long. can't happen.", buf);
tempfree(x);
x = gettemp();
x->sval = buf;
x->tval = STR;