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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
|
/*
* awk3 -- Builtin functions and various utility procedures
*
* Copyright (C) 1986,1987 Free Software Foundation Written by Jay Fenlason,
* December 1986
*
* $Log: awk3.c,v $
* Revision 1.34 88/12/13 22:28:10 david
* temporarily #ifdef out flush_io in redirect(); adjust atan2() for
* force_number as a macro
*
* Revision 1.32 88/12/01 15:03:21 david
* renamed hack_print_node to do_print (at last!)
* moved force_string() up out of print_simple for simplicity
*
* Revision 1.31 88/11/30 15:17:27 david
* free previous value in set_fs
*
* Revision 1.30 88/11/29 16:24:47 david
* fix bug in previous change
*
* Revision 1.29 88/11/29 15:14:52 david
* dynamically manage open files/pipes to allow an arbitrary number of open files
* (i.e. when out of file descriptors, close the least recently used file,
* saving the current offset; if it is reused, reopen and seek to saved offset)
*
* Revision 1.28 88/11/28 20:12:53 david
* correct previous error in cleanup of do_substr
*
* Revision 1.27 88/11/23 21:42:13 david
* Arnold: change ENV to ENVIRON nad a further bug fix for -Ft
* ..
*
* Revision 1.26 88/11/22 13:50:33 david
* Arnold: added ENV array and bug fix to -Ft
*
* Revision 1.25 88/11/15 10:24:08 david
* Arnold: cleanup of comments, #include's and obsolete code
*
* Revision 1.24 88/11/14 21:57:03 david
* Arnold: init. FILENAME to "-" and cleanup in do_substr()
*
* Revision 1.23 88/11/01 12:17:45 david
* cleanu and code movement; changes to reflect change to parse_fields()
*
* Revision 1.22 88/10/19 21:58:43 david
* replace malloc and realloc with error checking versions
*
* Revision 1.21 88/10/17 20:55:31 david
* SYSV --> USG
*
* Revision 1.20 88/10/13 21:59:55 david
* purge FAST and cleanup error messages
*
* Revision 1.19 88/10/06 21:54:28 david
* cleaned up I/O handling
*
* Revision 1.18 88/10/06 15:49:01 david
* changes from Arnold: be careful about flushing I/O; warn about error on close;
* return seed from srand
*
* Revision 1.17 88/09/19 20:39:11 david
* minor cleanup
*
* Revision 1.16 88/08/09 14:55:16 david
* getline now gets next file properly
* stupid bug in do_split() fixed
* substr() now works if second arg. is negative (truncated to 0)
*
* Revision 1.15 88/06/13 18:07:12 david
* delete -R option
* cleanup of redirection code [from Arnold]
*
* Revision 1.14 88/06/07 23:41:00 david
* some paranoid typecasting plus one bug fix:
* in do_getline(), use stdin if input_file is NULL and ther is no redirection
*
* Revision 1.13 88/06/06 21:40:49 david
* oops! got a little overenthusiastic on that last merge
*
* Revision 1.12 88/06/06 11:27:57 david
* get rid of some obsolete code
* merge parsing of fields for record input and split()
*
* Revision 1.11 88/06/05 21:00:35 david
* flush I/O buffers before calling system (fix from Arnold)
*
* Revision 1.10 88/06/05 20:59:26 david
* local vars. now come off a stack
*
* Revision 1.9 88/06/01 22:08:24 david
* in split(), ensure that if second arg. is a local var. that the value is
* looked up
*
* Revision 1.8 88/05/31 09:30:16 david
* Arnold's portability fixes to last change in random() stuff
*
* Revision 1.7 88/05/30 09:53:49 david
* clean up some fatal() calls
* de-lint the random number code
*
* Revision 1.6 88/05/27 11:06:21 david
* input_file wasn't getting properly reset after getline
*
* Revision 1.5 88/05/26 22:49:55 david
* fixed error message for redirection
*
* Revision 1.4 88/05/18 18:20:02 david
* fixed case where RS==""; record was including a trailing newline
*
* Revision 1.3 88/04/13 17:39:26 david
* fixed bug in handling of NR and FNR
*
* Revision 1.2 88/04/12 16:04:02 david
* fixed bug: NF at end of record generated one less field than it should have
*
* Revision 1.1 88/04/08 15:15:07 david
* Initial revision
* Revision 1.7 88/04/08 15:08:48 david bug fix for file
* descriptor handlin
*
* Revision 1.6 88/04/08 14:48:36 david changes from Arnold Robbins
*
* Revision 1.5 88/03/28 14:13:54 david *** empty log message ***
*
* Revision 1.4 88/03/23 22:17:41 david mostly delinting -- a couple of bug
* fixes
*
* Revision 1.3 88/03/18 21:00:13 david Baseline -- hoefully all the
* functionality of the new awk added. Just debugging and tuning to do.
*
* Revision 1.2 87/11/19 14:42:31 david expanded functionality for getline
* broke out get_a_record() from inrec() so that the former can be used from
* do_getline add system() builtin and skeletons for many other new builtins
*
* Revision 1.1 87/10/27 15:23:33 david Initial revision
*
*/
/*
* GAWK is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY. No author or distributor accepts responsibility to anyone for
* the consequences of using it or for whether it serves any particular
* purpose or works at all, unless he says so in writing. Refer to the GAWK
* General Public License for full details.
*
* Everyone is granted permission to copy, modify and redistribute GAWK, but
* only under the conditions described in the GAWK General Public License. A
* copy of this license is supposed to have been given to you along with GAWK
* so you can know your rights and responsibilities. It should be in a file
* named COPYING. Among other things, the copyright notice and this notice
* must be preserved on all copies.
*
* In other words, go ahead and share GAWK, but don't try to stop anyone else
* from sharing it farther. Help stamp out software hoarding!
*/
#include "awk.h"
/* These nodes store all the special variables AWK uses */
NODE *FS_node, *NF_node, *RS_node, *NR_node;
NODE *FILENAME_node, *OFS_node, *ORS_node, *OFMT_node;
NODE *FNR_node, *RLENGTH_node, *RSTART_node, *SUBSEP_node;
NODE *ENVIRON_node;
FILE *redirect();
/*
* structure used to dynamically maintain a linked-list of open files/pipes
*/
struct redirect {
int flag;
# define RED_FILE 1
# define RED_PIPE 2
# define RED_READ 4
# define RED_WRITE 8
# define RED_APPEND 16
char *value;
FILE *fp;
long offset; /* used for dynamic management of open files */
struct redirect *prev;
struct redirect *next;
};
struct redirect *red_head = NULL;
/*
* Set all the special variables to their initial values.
*/
init_vars()
{
NODE *spc_var();
NODE *do_sprintf();
extern char **environ;
char *var, *val;
NODE **aptr;
int i;
extern NODE **assoc_lookup();
extern NODE *tmp_string();
FS_node = spc_var("FS", make_string(" ", 1));
NF_node = spc_var("NF", make_number(-1.0));
RS_node = spc_var("RS", make_string("\n", 1));
NR_node = spc_var("NR", make_number(0.0));
FNR_node = spc_var("FNR", make_number(0.0));
FILENAME_node = spc_var("FILENAME", make_string("-", 1));
OFS_node = spc_var("OFS", make_string(" ", 1));
ORS_node = spc_var("ORS", make_string("\n", 1));
OFMT_node = spc_var("OFMT", make_string("%.6g", 4));
RLENGTH_node = spc_var("RLENGTH", make_number(0.0));
RSTART_node = spc_var("RSTART", make_number(0.0));
SUBSEP_node = spc_var("SUBSEP", make_string("\034", 1));
ENVIRON_node = spc_var("ENVIRON", Nnull_string);
for (i = 0; environ[i]; i++) {
var = environ[i];
val = index(var, '=');
if (val)
*val++ = '\0';
else
val = "";
aptr = assoc_lookup(ENVIRON_node, tmp_string(var, strlen (var)));
*aptr = make_string(val, strlen (val));
}
}
/*
* OFMT is special because we don't dare use force_string on it for fear of
* infinite loops. Thus, if it isn't a string, we return the default "%.6g"
* This may or may not be the right thing to do, but its the easiest
*/
/* This routine isn't used! It should be. */
#ifdef notdef
char *
get_ofmt()
{
register NODE *tmp;
tmp = *get_lhs(OFMT_node);
if ((tmp->type != Node_string && tmp->type != Node_str_num) || tmp->stlen == 0)
return "%.6g";
return tmp->stptr;
}
#endif
char *
get_fs()
{
register NODE *tmp;
tmp = force_string(FS_node->var_value);
if (tmp->stlen == 0)
return 0;
return tmp->stptr;
}
set_fs(str)
char *str;
{
register NODE **tmp;
tmp = get_lhs(FS_node);
do_deref();
/* stupid special case so -F\t works as documented in awk */
/* even though the shell hands us -Ft. Bleah! */
if (str[0] == 't' && str[1] == '\0')
str[0] = '\t';
*tmp = make_string(str, 1);
do_deref();
}
int
get_rs()
{
register NODE *tmp;
tmp = force_string(RS_node->var_value);
if (tmp->stlen == 0)
return 0;
return *(tmp->stptr);
}
/* Builtin functions */
NODE *
do_exp(tree)
NODE *tree;
{
NODE *tmp;
double exp();
get_one(tree, &tmp);
return tmp_number((AWKNUM)exp((double)force_number(tmp)));
}
NODE *
do_index(tree)
NODE *tree;
{
NODE *s1, *s2;
register char *p1, *p2;
register int l1, l2;
get_two(tree, &s1, &s2);
p1 = s1->stptr;
p2 = s2->stptr;
l1 = s1->stlen;
l2 = s2->stlen;
while (l1) {
if (!strncmp(p1, p2, l2))
return tmp_number((AWKNUM) (1 + s1->stlen - l1));
l1--;
p1++;
}
return tmp_number((AWKNUM) 0.0);
}
NODE *
do_int(tree)
NODE *tree;
{
NODE *tmp;
double floor();
get_one(tree, &tmp);
return tmp_number((AWKNUM)floor((double)force_number(tmp)));
}
NODE *
do_length(tree)
NODE *tree;
{
NODE *tmp;
get_one(tree, &tmp);
return tmp_number((AWKNUM) (force_string(tmp)->stlen));
}
NODE *
do_log(tree)
NODE *tree;
{
NODE *tmp;
double log();
get_one(tree, &tmp);
return tmp_number((AWKNUM)log((double)force_number(tmp)));
}
NODE *
do_printf(tree)
NODE *tree;
{
register FILE *fp;
NODE *do_sprintf();
fp = redirect(tree->rnode);
print_simple(do_sprintf(tree->lnode), fp);
return Nnull_string;
}
set_element(num, s, len, n)
int num;
char *s;
int len;
NODE *n;
{
extern NODE **assoc_lookup();
*assoc_lookup(n, tmp_number((AWKNUM) (num))) = make_string(s, len);
}
NODE *
do_split(tree)
NODE *tree;
{
NODE *t1, *t2, *t3;
register char *splitc;
char *s;
NODE *n;
if (a_get_three(tree, &t1, &t2, &t3) < 3)
splitc = get_fs();
else
splitc = force_string(t3)->stptr;
n = t2;
if (t2->type == Node_param_list)
n = stack_ptr[t2->param_cnt];
if (n->type != Node_var && n->type != Node_var_array)
fatal("second argument of split is not a variable");
assoc_clear(n);
tree = force_string(t1);
s = tree->stptr;
return tmp_number((AWKNUM)
parse_fields(HUGE, &s, tree->stlen, splitc, set_element, n));
}
/*
* Note that the output buffer cannot be static because sprintf may get
* called recursively by force_string. Hence the wasteful alloca calls
*/
/* %e and %f formats are not properly implemented. Someone should fix them */
NODE *
do_sprintf(tree)
NODE *tree;
{
#define bchunk(s,l) if(l) {\
if((l)>ofre) {\
char *tmp;\
tmp=(char *)alloca(osiz*2);\
bcopy(obuf,tmp,olen);\
obuf=tmp;\
ofre+=osiz;\
osiz*=2;\
}\
bcopy(s,obuf+olen,(l));\
olen+=(l);\
ofre-=(l);\
}
/* Is there space for something L big in the buffer? */
#define chksize(l) if((l)>ofre) {\
char *tmp;\
tmp=(char *)alloca(osiz*2);\
bcopy(obuf,tmp,olen);\
obuf=tmp;\
ofre+=osiz;\
osiz*=2;\
}
/*
* Get the next arg to be formatted. If we've run out of args,
* return "" (Null string)
*/
#define parse_next_arg() {\
if(!carg) arg= Nnull_string;\
else {\
get_one(carg,&arg);\
carg=carg->rnode;\
}\
}
char *obuf;
int osiz, ofre, olen;
static char chbuf[] = "0123456789abcdef";
static char sp[] = " ";
char *s0, *s1;
int n0;
NODE *sfmt, *arg;
register NODE *carg;
long fw, prec, lj, alt, big;
long *cur;
long val;
unsigned long uval;
int sgn;
int base;
char cpbuf[30]; /* if we have numbers bigger than 30 */
char *cend = &cpbuf[30];/* chars, we lose, but seems unlikely */
char *cp;
char *fill;
double tmpval;
char *pr_str;
extern char *gcvt();
obuf = (char *) alloca(120);
osiz = 120;
ofre = osiz;
olen = 0;
get_one(tree, &sfmt);
sfmt = force_string(sfmt);
carg = tree->rnode;
for (s0 = s1 = sfmt->stptr, n0 = sfmt->stlen; n0-- > 0;) {
if (*s1 != '%') {
s1++;
continue;
}
bchunk(s0, s1 - s0);
s0 = s1;
cur = &fw;
fw = 0;
prec = 0;
lj = alt = big = 0;
fill = sp;
cp = cend;
s1++;
retry:
--n0;
switch (*s1++) {
case '%':
bchunk("%", 1);
s0 = s1;
break;
case '0':
if (fill != sp || lj)
goto lose;
fill = "0"; /* FALL through */
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (cur == 0)
goto lose;
*cur = s1[-1] - '0';
while (n0 > 0 && *s1 >= '0' && *s1 <= '9') {
--n0;
*cur = *cur * 10 + *s1++ - '0';
}
goto retry;
case '-':
if (lj || fill != sp)
goto lose;
lj++;
goto retry;
case '.':
if (cur != &fw)
goto lose;
cur = ≺
goto retry;
case '#':
if (alt)
goto lose;
alt++;
goto retry;
case 'l':
if (big)
goto lose;
big++;
goto retry;
case 'c':
parse_next_arg();
if (arg->flags & NUM) {
uval = (unsigned long) arg->numbr;
cpbuf[0] = uval;
prec = 1;
pr_str = cpbuf;
goto dopr_string;
}
if (!prec || prec > arg->stlen)
prec = arg->stlen;
pr_str = cpbuf;
goto dopr_string;
case 's':
parse_next_arg();
arg = force_string(arg);
if (!prec || prec > arg->stlen)
prec = arg->stlen;
pr_str = arg->stptr;
dopr_string:
if (fw > prec && !lj) {
while (fw > prec) {
bchunk(sp, 1);
fw--;
}
}
bchunk(pr_str, (int) prec);
if (fw > prec) {
while (fw > prec) {
bchunk(sp, 1);
fw--;
}
}
s0 = s1;
break;
case 'd':
parse_next_arg();
val = (long) force_number(arg);
if (val < 0) {
sgn = 1;
val = -val;
} else
sgn = 0;
do {
*--cp = '0' + val % 10;
val /= 10;
} while (val);
if (sgn)
*--cp = '-';
prec = cend - cp;
if (fw > prec && !lj) {
if (fill != sp && *cp == '-') {
bchunk(cp, 1);
cp++;
prec--;
fw--;
}
while (fw > prec) {
bchunk(fill, 1);
fw--;
}
}
bchunk(cp, (int) prec);
if (fw > prec) {
while (fw > prec) {
bchunk(fill, 1);
fw--;
}
}
s0 = s1;
break;
case 'u':
base = 10;
goto pr_unsigned;
case 'o':
base = 8;
goto pr_unsigned;
case 'x':
base = 16;
goto pr_unsigned;
pr_unsigned:
parse_next_arg();
uval = (unsigned long) force_number(arg);
do {
*--cp = chbuf[uval % base];
uval /= base;
} while (uval);
prec = cend - cp;
if (fw > prec && !lj) {
while (fw > prec) {
bchunk(fill, 1);
fw--;
}
}
bchunk(cp, (int) prec);
if (fw > prec) {
while (fw > prec) {
bchunk(fill, 1);
fw--;
}
}
s0 = s1;
break;
case 'g':
parse_next_arg();
tmpval = force_number(arg);
if (prec == 0)
prec = 13;
(void) gcvt(tmpval, (int) prec, cpbuf);
prec = strlen(cpbuf);
cp = cpbuf;
if (fw > prec && !lj) {
if (fill != sp && *cp == '-') {
bchunk(cp, 1);
cp++;
prec--;
} /* Deal with .5 as 0.5 */
if (fill == sp && *cp == '.') {
--fw;
while (--fw >= prec) {
bchunk(fill, 1);
}
bchunk("0", 1);
} else
while (fw-- > prec)
bchunk(fill, 1);
} else {/* Turn .5 into 0.5 */
/* FOO */
if (*cp == '.' && fill == sp) {
bchunk("0", 1);
--fw;
}
}
bchunk(cp, (int) prec);
if (fw > prec)
while (fw-- > prec)
bchunk(fill, 1);
s0 = s1;
break;
case 'f':
parse_next_arg();
tmpval = force_number(arg);
chksize(fw + prec + 5); /* 5==slop */
cp = cpbuf;
*cp++ = '%';
if (lj)
*cp++ = '-';
if (fill != sp)
*cp++ = '0';
if (prec != 0) {
(void) strcpy(cp, "*.*f");
(void) sprintf(obuf + olen, cpbuf, fw, prec, (double) tmpval);
} else {
(void) strcpy(cp, "*f");
(void) sprintf(obuf + olen, cpbuf, fw, (double) tmpval);
}
cp = obuf + olen;
ofre -= strlen(obuf + olen);
olen += strlen(obuf + olen); /* There may be nulls */
s0 = s1;
break;
case 'e':
parse_next_arg();
tmpval = force_number(arg);
chksize(fw + prec + 5); /* 5==slop */
cp = cpbuf;
*cp++ = '%';
if (lj)
*cp++ = '-';
if (fill != sp)
*cp++ = '0';
if (prec != 0) {
(void) strcpy(cp, "*.*e");
(void) sprintf(obuf + olen, cpbuf, fw, prec, (double) tmpval);
} else {
(void) strcpy(cp, "*e");
(void) sprintf(obuf + olen, cpbuf, fw, (double) tmpval);
}
cp = obuf + olen;
ofre -= strlen(obuf + olen);
olen += strlen(obuf + olen); /* There may be nulls */
s0 = s1;
break;
default:
lose:
break;
}
}
bchunk(s0, s1 - s0);
return tmp_string(obuf, olen);
}
NODE *
do_sqrt(tree)
NODE *tree;
{
NODE *tmp;
double sqrt();
get_one(tree, &tmp);
return tmp_number((AWKNUM)sqrt((double)force_number(tmp)));
}
NODE *
do_substr(tree)
NODE *tree;
{
NODE *t1, *t2, *t3;
register int index, length;
length = -1;
if (get_three(tree, &t1, &t2, &t3) == 3)
length = (int) force_number(t3);
index = (int) force_number(t2) - 1;
tree = force_string(t1);
if (length == -1)
length = tree->stlen;
if (index < 0)
index = 0;
if (index >= tree->stlen || length <= 0)
return Nnull_string;
if (index + length > tree->stlen)
length = tree->stlen - index;
return tmp_string(tree->stptr + index, length);
}
NODE *
do_system(tree)
NODE *tree;
{
NODE *tmp;
int ret;
extern int flush_io ();
(void) flush_io (); /* so output is syncrhonous with gawk's */
get_one(tree, &tmp);
ret = system(force_string(tmp)->stptr);
ret = (ret >> 8) & 0xff;
return tmp_number((AWKNUM) ret);
}
/* The print command. Its name is historical */
do_print(tree)
NODE *tree;
{
register FILE *fp;
fp = redirect(tree->rnode);
tree = tree->lnode;
if (!tree)
tree = WHOLELINE;
if (tree->type != Node_expression_list) {
if (!(tree->flags & STR))
cant_happen();
print_simple(tree, fp);
} else {
while (tree) {
print_simple(force_string(tree_eval(tree->lnode)), fp);
tree = tree->rnode;
if (tree)
print_simple(OFS_node->var_value, fp);
}
}
print_simple(ORS_node->var_value, fp);
}
/*
* Get the arguments to functions. No function cares if you give it too many
* args (they're ignored). Only a few fuctions complain about being given
* too few args. The rest have defaults
*/
get_one(tree, res)
NODE *tree, **res;
{
if (!tree) {
*res = WHOLELINE;
return;
}
*res = tree_eval(tree->lnode);
}
get_two(tree, res1, res2)
NODE *tree, **res1, **res2;
{
if (!tree) {
*res1 = WHOLELINE;
return;
}
*res1 = tree_eval(tree->lnode);
if (!tree->rnode)
return;
tree = tree->rnode;
*res2 = tree_eval(tree->lnode);
}
get_three(tree, res1, res2, res3)
NODE *tree, **res1, **res2, **res3;
{
if (!tree) {
*res1 = WHOLELINE;
return 0;
}
*res1 = tree_eval(tree->lnode);
if (!tree->rnode)
return 1;
tree = tree->rnode;
*res2 = tree_eval(tree->lnode);
if (!tree->rnode)
return 2;
tree = tree->rnode;
*res3 = tree_eval(tree->lnode);
return 3;
}
a_get_three(tree, res1, res2, res3)
NODE *tree, **res1, **res2, **res3;
{
if (!tree) {
*res1 = WHOLELINE;
return 0;
}
*res1 = tree_eval(tree->lnode);
if (!tree->rnode)
return 1;
tree = tree->rnode;
*res2 = tree->lnode;
if (!tree->rnode)
return 2;
tree = tree->rnode;
*res3 = tree_eval(tree->lnode);
return 3;
}
/* Redirection for printf and print commands */
FILE *
redirect(tree)
NODE *tree;
{
register NODE *tmp;
register struct redirect *rp;
register char *str;
register FILE *fp;
FILE *popen();
FILE *fopen();
int tflag;
char *direction = "to";
if (!tree)
return stdout;
tflag = 0;
switch (tree->type) {
case Node_redirect_append:
tflag = RED_APPEND;
case Node_redirect_output:
tflag |= (RED_FILE|RED_WRITE);
break;
case Node_redirect_pipe:
tflag = (RED_PIPE|RED_WRITE);
break;
case Node_redirect_pipein:
tflag = (RED_PIPE|RED_READ);
break;
case Node_redirect_input:
tflag = (RED_FILE|RED_READ);
break;
default:
fatal ("invalid tree type %d in redirect()\n", tree->type);
break;
}
tmp = force_string(tree_eval(tree->subnode));
str = tmp->stptr;
for (rp = red_head; rp != NULL; rp = rp->next)
if (rp->flag == tflag && strcmp(rp->value, str) == 0)
break;
if (rp == NULL) {
emalloc(rp, struct redirect *, sizeof(struct redirect),
"redirect");
emalloc(str, char *, strlen(tmp->stptr)+1, "redirect");
(void) strcpy(str, tmp->stptr);
rp->value = str;
rp->flag = tflag;
rp->offset = 0;
rp->fp = NULL;
/* maintain list in most-recently-used first order */
if (red_head)
red_head->prev = rp;
rp->prev = NULL;
rp->next = red_head;
red_head = rp;
}
while (rp->fp == NULL) {
errno = 0;
switch (tree->type) {
case Node_redirect_output:
fp = rp->fp = fopen(str, "w");
break;
case Node_redirect_append:
fp = rp->fp = fopen(str, "a");
break;
case Node_redirect_pipe:
fp = rp->fp = popen(str, "w");
break;
case Node_redirect_pipein:
direction = "from";
fp = rp->fp = popen(str, "r");
break;
case Node_redirect_input:
direction = "from";
fp = rp->fp = fopen(str, "r");
break;
}
if (fp == NULL) {
/* too many files open -- close one and try again */
if (errno == ENFILE || errno == EMFILE)
close_one();
else /* some other reason for failure */
fatal("can't redirect %s `%s'\n", direction,
str);
}
}
if (rp->offset != 0) { /* this file was previously open */
if (fseek(fp, rp->offset, 0) == -1)
fatal("can't seek to %ld on `%s'\n", rp->offset, str);
}
#ifdef notdef
(void) flush_io(); /* a la SVR4 awk */
#endif
free_temp(tmp);
return rp->fp;
}
close_one()
{
register struct redirect *rp;
register struct redirect *rplast;
/* go to end of list first, to pick up least recently used entry */
for (rp = red_head; rp != NULL; rp = rp->next)
rplast = rp;
/* now work back up through the list */
for (rp = rplast; rp != NULL; rp = rp->prev)
if (rp->fp && (rp->flag & RED_FILE)) {
rp->offset = ftell(rp->fp);
if (fclose(rp->fp))
warning("close of \"%s\" failed.",
rp->value);
rp->fp = NULL;
break;
}
if (rp == NULL)
/* surely this is the only reason ??? */
fatal("too many pipes open");
}
NODE *
do_close(tree)
NODE *tree;
{
NODE *tmp;
register struct redirect *rp;
tmp = force_string(tree_eval(tree->subnode));
for (rp = red_head; rp != NULL; rp = rp->next) {
if (strcmp(rp->value, tmp->stptr) == 0)
break;
}
free_temp(tmp);
if (rp == NULL) /* no match */
return tmp_number((AWKNUM) 0.0);
return tmp_number((AWKNUM)close_fp(rp));
}
int
close_fp(rp)
register struct redirect *rp;
{
int status;
if (rp->flag & RED_PIPE)
status = pclose(rp->fp);
else
status = fclose(rp->fp);
/* SVR4 awk checks and warns about status of close */
if (status)
warning("%s close of \"%s\" failed.",
(rp->flag & RED_PIPE) ? "pipe" : "file", rp->value);
if (rp->prev)
rp->prev->next = rp->next;
else
red_head = rp->next;
free(rp->value);
free(rp);
return status;
}
int
flush_io ()
{
register struct redirect *rp;
int status = 0;
if (fflush(stdout)) {
warning("error writing standard output.");
status++;
}
if (fflush(stderr)) {
warning("error writing standard error.");
status++;
}
for (rp = red_head; rp != NULL; rp = rp->next)
/* flush both files and pipes, what the heck */
if ((rp->flag & RED_WRITE) && rp->fp != NULL)
if (fflush(rp->fp)) {
warning( "%s flush of \"%s\" failed.",
(rp->flag & RED_PIPE) ? "pipe" : "file",
rp->value);
status++;
}
return status;
}
int
close_io ()
{
register struct redirect *rp;
int status = 0;
for (rp = red_head; rp != NULL; rp = rp->next)
if (rp->fp && close_fp(rp))
status++;
return status;
}
print_simple(tree, fp)
NODE *tree;
FILE *fp;
{
if (fwrite(tree->stptr, sizeof(char), tree->stlen, fp) != tree->stlen)
warning("fwrite: %s", sys_errlist[errno]);
free_temp(tree);
}
NODE *
do_atan2(tree)
NODE *tree;
{
NODE *t1, *t2;
extern double atan2();
get_two(tree, &t1, &t2);
(void) force_number(t1);
return tmp_number((AWKNUM) atan2((double) t1->numbr,
(double) force_number(t2)));
}
NODE *
do_sin(tree)
NODE *tree;
{
NODE *tmp;
extern double sin();
get_one(tree, &tmp);
return tmp_number((AWKNUM) sin((double)force_number(tmp)));
}
NODE *
do_cos(tree)
NODE *tree;
{
NODE *tmp;
extern double cos();
get_one(tree, &tmp);
return tmp_number((AWKNUM) cos((double)force_number(tmp)));
}
static int firstrand = 1;
#ifndef USG
static char state[256];
extern char *initstate();
#endif
#define MAXLONG 2147483647 /* maximum value for long int */
/* ARGSUSED */
NODE *
do_rand(tree)
NODE *tree;
{
#ifdef USG
extern long lrand48();
return tmp_number((AWKNUM) lrand48() / MAXLONG);
#else
extern long random();
if (firstrand) {
(void) initstate((unsigned) 1, state, sizeof state);
srandom(1);
firstrand = 0;
}
return tmp_number((AWKNUM) random() / MAXLONG);
#endif
}
NODE *
do_srand(tree)
NODE *tree;
{
NODE *tmp;
extern long time();
static long save_seed = 1;
long ret = save_seed; /* SVR4 awk srand returns previous seed */
#ifdef USG
extern void srand48();
if (tree == NULL)
srand48(save_seed = time((long *) 0));
else {
get_one(tree, &tmp);
srand48(save_seed = (long) force_number(tmp));
}
#else
extern srandom();
extern char *setstate();
if (firstrand)
(void) initstate((unsigned) 1, state, sizeof state);
else
(void) setstate(state);
if (!tree)
srandom((int) (save_seed = time((long *) 0)));
else {
get_one(tree, &tmp);
srandom((int) (save_seed = (long) force_number(tmp)));
}
#endif
firstrand = 0;
return tmp_number((AWKNUM) ret);
}
|