-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.c
1580 lines (1334 loc) · 40.3 KB
/
main.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
/*
* main.c -- Code generator and main program for gawk.
*/
/*
* Copyright (C) 1986, 1988, 1989, 1991-2012 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
*/
/* FIX THIS BEFORE EVERY RELEASE: */
#define UPDATE_YEAR 2012
#include "awk.h"
#include "getopt.h"
#ifdef HAVE_MCHECK_H
#include <mcheck.h>
#endif
#define DEFAULT_PROFILE "awkprof.out" /* where to put profile */
#define DEFAULT_VARFILE "awkvars.out" /* where to put vars */
#define DEFAULT_PREC 53
#define DEFAULT_ROUNDMODE "N" /* round to nearest */
static const char *varfile = DEFAULT_VARFILE;
const char *command_file = NULL; /* debugger commands */
static void usage(int exitval, FILE *fp) ATTRIBUTE_NORETURN;
static void copyleft(void) ATTRIBUTE_NORETURN;
static void cmdline_fs(char *str);
static void init_args(int argc0, int argc, const char *argv0, char **argv);
static void init_vars(void);
static NODE *load_environ(void);
static NODE *load_procinfo(void);
static RETSIGTYPE catchsig(int sig);
#ifdef HAVE_LIBSIGSEGV
static int catchsegv(void *fault_address, int serious);
static void catchstackoverflow(int emergency, stackoverflow_context_t scp);
#endif
static void nostalgia(void) ATTRIBUTE_NORETURN;
static void version(void) ATTRIBUTE_NORETURN;
static void init_fds(void);
static void init_groupset(void);
static void save_argv(int, char **);
extern int debug_prog(INSTRUCTION *pc); /* debug.c */
extern int init_debug(); /* debug.c */
/* These nodes store all the special variables AWK uses */
NODE *ARGC_node, *ARGIND_node, *ARGV_node, *BINMODE_node, *CONVFMT_node;
NODE *ENVIRON_node, *ERRNO_node, *FIELDWIDTHS_node, *FILENAME_node;
NODE *FNR_node, *FPAT_node, *FS_node, *IGNORECASE_node, *LINT_node;
NODE *NF_node, *NR_node, *OFMT_node, *OFS_node, *ORS_node, *PROCINFO_node;
NODE *RLENGTH_node, *RSTART_node, *RS_node, *RT_node, *SUBSEP_node;
NODE *PREC_node, *ROUNDMODE_node;
NODE *TEXTDOMAIN_node;
NODE *_r; /* used as temporary in stack macros */
long NF;
long NR;
long FNR;
int BINMODE;
int IGNORECASE;
char *OFS;
char *ORS;
char *OFMT;
char *TEXTDOMAIN;
/*
* CONVFMT is a convenience pointer for the current number to string format.
* We must supply an initial value to avoid recursion problems of
* set_CONVFMT -> fmt_index -> r_force_string: gets NULL CONVFMT
* Fun, fun, fun, fun.
*/
char *CONVFMT = "%.6g";
NODE *Nnull_string; /* The global null string */
#if defined(HAVE_LOCALE_H)
struct lconv loc; /* current locale */
static void init_locale(struct lconv *l);
#endif /* defined(HAVE_LOCALE_H) */
/* The name the program was invoked under, for error messages */
const char *myname;
/* A block of AWK code to be run */
INSTRUCTION *code_block = NULL;
char **d_argv; /* saved argv for debugger restarting */
/*
* List of rules and functions with first and last instruction (source_line)
* information; used for profiling and debugging.
*/
INSTRUCTION *rule_list;
int exit_val = EXIT_SUCCESS; /* exit value */
#if defined(YYDEBUG) || defined(GAWKDEBUG)
extern int yydebug;
#endif
SRCFILE *srcfiles; /* source files */
/*
* structure to remember variable pre-assignments
*/
struct pre_assign {
enum assign_type { PRE_ASSIGN = 1, PRE_ASSIGN_FS } type;
char *val;
};
static struct pre_assign *preassigns = NULL; /* requested via -v or -F */
static long numassigns = -1; /* how many of them */
static bool disallow_var_assigns = false; /* true for --exec */
static void add_preassign(enum assign_type type, char *val);
int do_flags = false;
bool do_optimize = true; /* apply default optimizations */
static int do_nostalgia = false; /* provide a blast from the past */
static int do_binary = false; /* hands off my data! */
static int do_version = false; /* print version info */
int use_lc_numeric = false; /* obey locale for decimal point */
#if MBS_SUPPORT
int gawk_mb_cur_max; /* MB_CUR_MAX value, see comment in main() */
#endif
FILE *output_fp; /* default gawk output, can be redirected in the debugger */
bool output_is_tty = false; /* control flushing of output */
/* default format for strftime(), available via PROCINFO */
const char def_strftime_format[] = "%a %b %e %H:%M:%S %Z %Y";
extern const char *version_string;
#if defined (HAVE_GETGROUPS) && defined(NGROUPS_MAX) && NGROUPS_MAX > 0
GETGROUPS_T *groupset; /* current group set */
int ngroups; /* size of said set */
#endif
void (*lintfunc)(const char *mesg, ...) = warning;
static const struct option optab[] = {
{ "traditional", no_argument, NULL, 'c' },
{ "lint", optional_argument, NULL, 'L' },
{ "lint-old", no_argument, NULL, 't' },
{ "optimize", no_argument, NULL, 'O' },
{ "posix", no_argument, NULL, 'P' },
{ "nostalgia", no_argument, & do_nostalgia, 1 },
{ "gen-pot", no_argument, NULL, 'g' },
{ "non-decimal-data", no_argument, NULL, 'n' },
{ "pretty-print", optional_argument, NULL, 'o' },
{ "profile", optional_argument, NULL, 'p' },
{ "debug", optional_argument, NULL, 'D' },
{ "copyright", no_argument, NULL, 'C' },
{ "field-separator", required_argument, NULL, 'F' },
{ "file", required_argument, NULL, 'f' },
{ "re-interval", no_argument, NULL, 'r' },
{ "source", required_argument, NULL, 'e' },
{ "load", required_argument, NULL, 'l' },
{ "dump-variables", optional_argument, NULL, 'd' },
{ "assign", required_argument, NULL, 'v' },
{ "version", no_argument, & do_version, 'V' },
{ "help", no_argument, NULL, 'h' },
{ "exec", required_argument, NULL, 'E' },
{ "use-lc-numeric", no_argument, & use_lc_numeric, 1 },
{ "characters-as-bytes", no_argument, & do_binary, 'b' },
{ "sandbox", no_argument, NULL, 'S' },
{ "bignum", no_argument, NULL, 'M' },
#if defined(YYDEBUG) || defined(GAWKDEBUG)
{ "parsedebug", no_argument, NULL, 'Y' },
#endif
{ NULL, 0, NULL, '\0' }
};
/* main --- process args, parse program, run it, clean up */
int
main(int argc, char **argv)
{
/*
* The + on the front tells GNU getopt not to rearrange argv.
*/
const char *optlist = "+F:f:v:W;m:bcCd::D::e:E:gh:i:l:L:nNo::Op::MPrStVY";
bool stopped_early = false;
int old_optind;
int i;
int c;
char *scan, *src;
char *extra_stack;
int have_srcfile = 0;
SRCFILE *s;
/* do these checks early */
if (getenv("TIDYMEM") != NULL)
do_flags |= DO_TIDY_MEM;
#ifdef HAVE_MCHECK_H
#ifdef HAVE_MTRACE
if (do_tidy_mem)
mtrace();
#endif /* HAVE_MTRACE */
#endif /* HAVE_MCHECK_H */
#if defined(LC_CTYPE)
setlocale(LC_CTYPE, "");
#endif
#if defined(LC_COLLATE)
setlocale(LC_COLLATE, "");
#endif
#if defined(LC_MESSAGES)
setlocale(LC_MESSAGES, "");
#endif
#if defined(LC_NUMERIC) && defined(HAVE_LOCALE_H)
/*
* Force the issue here. According to POSIX 2001, decimal
* point is used for parsing source code and for command-line
* assignments and the locale value for processing input,
* number to string conversion, and printing output.
*
* 10/2005 --- see below also; we now only use the locale's
* decimal point if do_posix in effect.
*
* 9/2007:
* This is a mess. We need to get the locale's numeric info for
* the thousands separator for the %'d flag.
*/
setlocale(LC_NUMERIC, "");
init_locale(& loc);
setlocale(LC_NUMERIC, "C");
#endif
#if defined(LC_TIME)
setlocale(LC_TIME, "");
#endif
#if MBS_SUPPORT
/*
* In glibc, MB_CUR_MAX is actually a function. This value is
* tested *a lot* in many speed-critical places in gawk. Caching
* this value once makes a speed difference.
*/
gawk_mb_cur_max = MB_CUR_MAX;
/* Without MBS_SUPPORT, gawk_mb_cur_max is 1. */
/* init the cache for checking bytes if they're characters */
init_btowc_cache();
#endif
(void) bindtextdomain(PACKAGE, LOCALEDIR);
(void) textdomain(PACKAGE);
(void) signal(SIGFPE, catchsig);
#ifdef SIGBUS
(void) signal(SIGBUS, catchsig);
#endif
(void) sigsegv_install_handler(catchsegv);
#define STACK_SIZE (16*1024)
emalloc(extra_stack, char *, STACK_SIZE, "main");
(void) stackoverflow_install_handler(catchstackoverflow, extra_stack, STACK_SIZE);
#undef STACK_SIZE
myname = gawk_name(argv[0]);
os_arg_fixup(&argc, &argv); /* emulate redirection, expand wildcards */
if (argc < 2)
usage(EXIT_FAILURE, stderr);
/* initialize the null string */
Nnull_string = make_string("", 0);
/* Robustness: check that file descriptors 0, 1, 2 are open */
init_fds();
/* init array handling. */
array_init();
/* init the symbol tables */
init_symbol_table();
output_fp = stdout;
/* we do error messages ourselves on invalid options */
opterr = false;
/* copy argv before getopt gets to it; used to restart the debugger */
save_argv(argc, argv);
/* initialize global (main) execution context */
push_context(new_context());
/* option processing. ready, set, go! */
for (optopt = 0, old_optind = 1;
(c = getopt_long(argc, argv, optlist, optab, NULL)) != EOF;
optopt = 0, old_optind = optind) {
if (do_posix)
opterr = true;
switch (c) {
case 'F':
add_preassign(PRE_ASSIGN_FS, optarg);
break;
case 'E':
disallow_var_assigns = true;
/* fall through */
case 'f':
/*
* Allow multiple -f options.
* This makes function libraries real easy.
* Most of the magic is in the scanner.
*
* The following is to allow for whitespace at the end
* of a #! /bin/gawk line in an executable file
*/
scan = optarg;
if (argv[optind-1] != optarg)
while (isspace((unsigned char) *scan))
scan++;
src = (*scan == '\0' ? argv[optind++] : optarg);
(void) add_srcfile((src && src[0] == '-' && src[1] == '\0') ?
SRC_STDIN : SRC_FILE,
src, srcfiles, NULL, NULL);
break;
case 'v':
add_preassign(PRE_ASSIGN, optarg);
break;
case 'm':
/*
* BWK awk extension.
* -mf nnn set # fields, gawk ignores
* -mr nnn set record length, ditto
*
* As of at least 10/2007, BWK awk also ignores it.
*/
if (do_lint)
lintwarn(_("`-m[fr]' option irrelevant in gawk"));
if (optarg[0] != 'r' && optarg[0] != 'f')
warning(_("-m option usage: `-m[fr] nnn'"));
break;
case 'b':
do_binary = true;
break;
case 'c':
do_flags |= DO_TRADITIONAL;
break;
case 'C':
copyleft();
break;
case 'd':
do_flags |= DO_DUMP_VARS;
if (optarg != NULL && optarg[0] != '\0')
varfile = optarg;
break;
case 'D':
do_flags |= DO_DEBUG;
if (optarg != NULL && optarg[0] != '\0')
command_file = optarg;
break;
case 'e':
if (optarg[0] == '\0')
warning(_("empty argument to `-e/--source' ignored"));
else
(void) add_srcfile(SRC_CMDLINE, optarg, srcfiles, NULL, NULL);
break;
case 'g':
do_flags |= DO_INTL;
break;
case 'h':
/* write usage to stdout, per GNU coding stds */
usage(EXIT_SUCCESS, stdout);
break;
case 'i':
(void) add_srcfile(SRC_INC, optarg, srcfiles, NULL, NULL);
break;
case 'l':
(void) add_srcfile(SRC_EXTLIB, optarg, srcfiles, NULL, NULL);
break;
case 'L':
#ifndef NO_LINT
do_flags |= DO_LINT_ALL;
if (optarg != NULL) {
if (strcmp(optarg, "fatal") == 0)
lintfunc = r_fatal;
else if (strcmp(optarg, "invalid") == 0) {
do_flags &= ~DO_LINT_ALL;
do_flags |= DO_LINT_INVALID;
}
}
break;
case 't':
do_flags |= DO_LINT_OLD;
break;
#else
case 'L':
case 't':
break;
#endif
case 'n':
do_flags |= DO_NON_DEC_DATA;
break;
case 'N':
use_lc_numeric = true;
break;
case 'O':
do_optimize++;
break;
case 'p':
do_flags |= DO_PROFILE;
/* fall through */
case 'o':
do_flags |= DO_PRETTY_PRINT;
if (optarg != NULL)
set_prof_file(optarg);
else
set_prof_file(DEFAULT_PROFILE);
break;
case 'M':
#ifdef HAVE_MPFR
do_flags |= DO_MPFR;
#endif
break;
case 'P':
do_flags |= DO_POSIX;
break;
case 'r':
do_flags |= DO_INTERVALS;
break;
case 'S':
do_flags |= DO_SANDBOX;
break;
case 'V':
do_version = true;
break;
case 'W': /* gawk specific options - now in getopt_long */
fprintf(stderr, _("%s: option `-W %s' unrecognized, ignored\n"),
argv[0], optarg);
break;
case 0:
/*
* getopt_long found an option that sets a variable
* instead of returning a letter. Do nothing, just
* cycle around for the next one.
*/
break;
case 'Y':
#if defined(YYDEBUG) || defined(GAWKDEBUG)
if (c == 'Y') {
yydebug = 2;
break;
}
#endif
/* if not debugging, fall through */
case '?':
default:
/*
* If not posix, an unrecognized option stops argument
* processing so that it can go into ARGV for the awk
* program to see. This makes use of ``#! /bin/gawk -f''
* easier.
*
* However, it's never simple. If optopt is set,
* an option that requires an argument didn't get the
* argument. We care because if opterr is 0, then
* getopt_long won't print the error message for us.
*/
if (! do_posix
&& (optopt == '\0' || strchr(optlist, optopt) == NULL)) {
/*
* can't just do optind--. In case of an
* option with >= 2 letters, getopt_long
* won't have incremented optind.
*/
optind = old_optind;
stopped_early = true;
goto out;
} else if (optopt != '\0') {
/* Use POSIX required message format */
fprintf(stderr,
_("%s: option requires an argument -- %c\n"),
myname, optopt);
usage(EXIT_FAILURE, stderr);
}
/* else
let getopt print error message for us */
break;
}
if (c == 'E') /* --exec ends option processing */
break;
}
out:
if (do_nostalgia)
nostalgia();
/* check for POSIXLY_CORRECT environment variable */
if (! do_posix && getenv("POSIXLY_CORRECT") != NULL) {
do_flags |= DO_POSIX;
if (do_lint)
lintwarn(
_("environment variable `POSIXLY_CORRECT' set: turning on `--posix'"));
}
if (do_posix) {
use_lc_numeric = true;
if (do_traditional) /* both on command line */
warning(_("`--posix' overrides `--traditional'"));
else
do_flags |= DO_TRADITIONAL;
/*
* POSIX compliance also implies
* no GNU extensions either.
*/
}
if (do_traditional && do_non_decimal_data) {
do_flags &= ~DO_NON_DEC_DATA;
warning(_("`--posix'/`--traditional' overrides `--non-decimal-data'"));
}
if (do_lint && os_is_setuid())
warning(_("running %s setuid root may be a security problem"), myname);
#if MBS_SUPPORT
if (do_binary) {
if (do_posix)
warning(_("`--posix' overrides `--characters-as-bytes'"));
else
gawk_mb_cur_max = 1; /* hands off my data! */
#if defined(LC_ALL)
setlocale(LC_ALL, "C");
#endif
}
#endif
if (do_debug) /* Need to register the debugger pre-exec hook before any other */
init_debug();
#ifdef HAVE_MPFR
/* Set up MPFR defaults, and register pre-exec hook to process arithmetic opcodes */
if (do_mpfr)
init_mpfr(DEFAULT_PREC, DEFAULT_ROUNDMODE);
#endif
/* load group set */
init_groupset();
#ifdef HAVE_MPFR
if (do_mpfr) {
mpz_init(Nnull_string->mpg_i);
Nnull_string->flags = (MALLOC|STRCUR|STRING|MPZN|NUMCUR|NUMBER);
} else
#endif
{
Nnull_string->numbr = 0.0;
Nnull_string->flags = (MALLOC|STRCUR|STRING|NUMCUR|NUMBER);
}
/*
* Tell the regex routines how they should work.
* Do this before initializing variables, since
* they could want to do a regexp compile.
*/
resetup();
/* Set up the special variables */
init_vars();
/* Set up the field variables */
init_fields();
/* Now process the pre-assignments */
for (i = 0; i <= numassigns; i++) {
if (preassigns[i].type == PRE_ASSIGN)
(void) arg_assign(preassigns[i].val, true);
else /* PRE_ASSIGN_FS */
cmdline_fs(preassigns[i].val);
efree(preassigns[i].val);
}
if (preassigns != NULL)
efree(preassigns);
if ((BINMODE & 1) != 0)
if (os_setbinmode(fileno(stdin), O_BINARY) == -1)
fatal(_("can't set binary mode on stdin (%s)"), strerror(errno));
if ((BINMODE & 2) != 0) {
if (os_setbinmode(fileno(stdout), O_BINARY) == -1)
fatal(_("can't set binary mode on stdout (%s)"), strerror(errno));
if (os_setbinmode(fileno(stderr), O_BINARY) == -1)
fatal(_("can't set binary mode on stderr (%s)"), strerror(errno));
}
#ifdef GAWKDEBUG
setbuf(stdout, (char *) NULL); /* make debugging easier */
#endif
if (os_isatty(fileno(stdout)))
output_is_tty = true;
/* initialize API before loading extension libraries */
init_ext_api();
/* load extension libs */
for (s = srcfiles->next; s != srcfiles; s = s->next) {
if (s->stype == SRC_EXTLIB)
load_ext(s->fullpath);
else if (s->stype != SRC_INC)
have_srcfile++;
}
/* do version check after extensions are loaded to get extension info */
if (do_version)
version();
/* No -f or --source options, use next arg */
if (! have_srcfile) {
if (optind > argc - 1 || stopped_early) /* no args left or no program */
usage(EXIT_FAILURE, stderr);
(void) add_srcfile(SRC_CMDLINE, argv[optind], srcfiles, NULL, NULL);
optind++;
}
/* Select the interpreter routine */
init_interpret();
init_args(optind, argc,
do_posix ? argv[0] : myname,
argv);
#if defined(LC_NUMERIC)
/*
* FRAGILE! CAREFUL!
* Pre-initing the variables with arg_assign() can change the
* locale. Force it to C before parsing the program.
*/
setlocale(LC_NUMERIC, "C");
#endif
/* Read in the program */
if (parse_program(& code_block) != 0)
exit(EXIT_FAILURE);
if (do_intl)
exit(EXIT_SUCCESS);
if (do_lint)
shadow_funcs();
if (do_lint && code_block->nexti->opcode == Op_atexit)
lintwarn(_("no program text at all!"));
load_symbols();
if (do_profile)
init_profiling_signals();
#if defined(LC_NUMERIC)
/*
* See comment above about using locale's decimal point.
*
* 10/2005:
* Bitter experience teaches us that most people the world over
* use period as the decimal point, not whatever their locale
* uses. Thus, only use the locale's decimal point if being
* posixly anal-retentive.
*
* 7/2007:
* Be a little bit kinder. Allow the --use-lc-numeric option
* to also use the local decimal point. This avoids the draconian
* strictness of POSIX mode if someone just wants to parse their
* data using the local decimal point.
*/
if (use_lc_numeric)
setlocale(LC_NUMERIC, "");
#endif
init_io();
output_fp = stdout;
if (do_debug)
debug_prog(code_block);
else
interpret(code_block);
if (do_pretty_print) {
dump_prog(code_block);
dump_funcs();
}
if (do_dump_vars)
dump_vars(varfile);
if (do_tidy_mem)
release_all_vars();
/* keep valgrind happier */
if (extra_stack)
efree(extra_stack);
final_exit(exit_val);
return exit_val; /* to suppress warnings */
}
/* add_preassign --- add one element to preassigns */
static void
add_preassign(enum assign_type type, char *val)
{
static long alloc_assigns; /* for how many are allocated */
#define INIT_SRC 4
++numassigns;
if (preassigns == NULL) {
emalloc(preassigns, struct pre_assign *,
INIT_SRC * sizeof(struct pre_assign), "add_preassign");
alloc_assigns = INIT_SRC;
} else if (numassigns >= alloc_assigns) {
alloc_assigns *= 2;
erealloc(preassigns, struct pre_assign *,
alloc_assigns * sizeof(struct pre_assign), "add_preassigns");
}
preassigns[numassigns].type = type;
preassigns[numassigns].val = estrdup(val, strlen(val));
#undef INIT_SRC
}
/* usage --- print usage information and exit */
static void
usage(int exitval, FILE *fp)
{
/* Not factoring out common stuff makes it easier to translate. */
fprintf(fp, _("Usage: %s [POSIX or GNU style options] -f progfile [--] file ...\n"),
myname);
fprintf(fp, _("Usage: %s [POSIX or GNU style options] [--] %cprogram%c file ...\n"),
myname, quote, quote);
/* GNU long options info. This is too many options. */
fputs(_("POSIX options:\t\tGNU long options: (standard)\n"), fp);
fputs(_("\t-f progfile\t\t--file=progfile\n"), fp);
fputs(_("\t-F fs\t\t\t--field-separator=fs\n"), fp);
fputs(_("\t-v var=val\t\t--assign=var=val\n"), fp);
fputs(_("Short options:\t\tGNU long options: (extensions)\n"), fp);
fputs(_("\t-b\t\t\t--characters-as-bytes\n"), fp);
fputs(_("\t-c\t\t\t--traditional\n"), fp);
fputs(_("\t-C\t\t\t--copyright\n"), fp);
fputs(_("\t-d[file]\t\t--dump-variables[=file]\n"), fp);
fputs(_("\t-D[file]\t\t--debug[=file]\n"), fp);
fputs(_("\t-e 'program-text'\t--source='program-text'\n"), fp);
fputs(_("\t-E file\t\t\t--exec=file\n"), fp);
fputs(_("\t-g\t\t\t--gen-pot\n"), fp);
fputs(_("\t-h\t\t\t--help\n"), fp);
fputs(_("\t-i includefile\t\t--include=includefile\n"), fp);
fputs(_("\t-l library\t\t--load=library\n"), fp);
fputs(_("\t-L [fatal]\t\t--lint[=fatal]\n"), fp);
fputs(_("\t-n\t\t\t--non-decimal-data\n"), fp);
fputs(_("\t-M\t\t\t--bignum\n"), fp);
fputs(_("\t-N\t\t\t--use-lc-numeric\n"), fp);
fputs(_("\t-o[file]\t\t--pretty-print[=file]\n"), fp);
fputs(_("\t-O\t\t\t--optimize\n"), fp);
fputs(_("\t-p[file]\t\t--profile[=file]\n"), fp);
fputs(_("\t-P\t\t\t--posix\n"), fp);
fputs(_("\t-r\t\t\t--re-interval\n"), fp);
fputs(_("\t-S\t\t\t--sandbox\n"), fp);
fputs(_("\t-t\t\t\t--lint-old\n"), fp);
fputs(_("\t-V\t\t\t--version\n"), fp);
#ifdef NOSTALGIA
fputs(_("\t-W nostalgia\t\t--nostalgia\n"), fp);
#endif
#ifdef GAWKDEBUG
fputs(_("\t-Y\t\t--parsedebug\n"), fp);
#endif
/* This is one string to make things easier on translators. */
/* TRANSLATORS: --help output 5 (end)
TRANSLATORS: the placeholder indicates the bug-reporting address
for this application. Please add _another line_ with the
address for translation bugs.
no-wrap */
fputs(_("\nTo report bugs, see node `Bugs' in `gawk.info', which is\n\
section `Reporting Problems and Bugs' in the printed version.\n\n"), fp);
/* ditto */
fputs(_("gawk is a pattern scanning and processing language.\n\
By default it reads standard input and writes standard output.\n\n"), fp);
/* ditto */
fputs(_("Examples:\n\tgawk '{ sum += $1 }; END { print sum }' file\n\
\tgawk -F: '{ print $1 }' /etc/passwd\n"), fp);
fflush(fp);
if (ferror(fp)) {
if (fp == stdout)
warning(_("error writing standard output (%s)"), strerror(errno));
exit(EXIT_FAILURE);
}
exit(exitval);
}
/* copyleft --- print out the short GNU copyright information */
static void
copyleft()
{
static const char blurb_part1[] =
N_("Copyright (C) 1989, 1991-%d Free Software Foundation.\n\
\n\
This program is free software; you can redistribute it and/or modify\n\
it under the terms of the GNU General Public License as published by\n\
the Free Software Foundation; either version 3 of the License, or\n\
(at your option) any later version.\n\
\n");
static const char blurb_part2[] =
N_("This program is distributed in the hope that it will be useful,\n\
but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
GNU General Public License for more details.\n\
\n");
static const char blurb_part3[] =
N_("You should have received a copy of the GNU General Public License\n\
along with this program. If not, see http://www.gnu.org/licenses/.\n");
/* multiple blurbs are needed for some brain dead compilers. */
printf(_(blurb_part1), UPDATE_YEAR); /* Last update year */
fputs(_(blurb_part2), stdout);
fputs(_(blurb_part3), stdout);
fflush(stdout);
if (ferror(stdout)) {
warning(_("error writing standard output (%s)"), strerror(errno));
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
/* cmdline_fs --- set FS from the command line */
static void
cmdline_fs(char *str)
{
NODE **tmp;
tmp = &FS_node->var_value;
unref(*tmp);
/*
* Only if in full compatibility mode check for the stupid special
* case so -F\t works as documented in awk book even though the shell
* hands us -Ft. Bleah!
*
* Thankfully, POSIX didn't propagate this "feature".
*/
if (str[0] == 't' && str[1] == '\0') {
if (do_lint)
lintwarn(_("-Ft does not set FS to tab in POSIX awk"));
if (do_traditional && ! do_posix)
str[0] = '\t';
}
*tmp = make_str_node(str, strlen(str), SCAN); /* do process escapes */
set_FS();
}
/* init_args --- set up ARGV from stuff on the command line */
static void
init_args(int argc0, int argc, const char *argv0, char **argv)
{
int i, j;
NODE **aptr;
NODE *tmp;
ARGV_node = install_symbol(estrdup("ARGV", 4), Node_var_array);
tmp = make_number(0.0);
aptr = assoc_lookup(ARGV_node, tmp);
unref(tmp);
unref(*aptr);
*aptr = make_string(argv0, strlen(argv0));
(*aptr)->flags |= MAYBE_NUM;
for (i = argc0, j = 1; i < argc; i++, j++) {
tmp = make_number((AWKNUM) j);
aptr = assoc_lookup(ARGV_node, tmp);
unref(tmp);
unref(*aptr);
*aptr = make_string(argv[i], strlen(argv[i]));
(*aptr)->flags |= MAYBE_NUM;
}
ARGC_node = install_symbol(estrdup("ARGC", 4), Node_var);
ARGC_node->var_value = make_number((AWKNUM) j);
}
/*
* Set all the special variables to their initial values.
* Note that some of the variables that have set_FOO routines should
* *N*O*T* have those routines called upon initialization, and thus
* they have NULL entries in that field. This is notably true of FS
* and IGNORECASE.
*/
struct varinit {
NODE **spec;
const char *name;
const char *strval;
AWKNUM numval;
Func_ptr update;
Func_ptr assign;
bool do_assign;
int flags;
#define NO_INSTALL 0x01
#define NON_STANDARD 0x02
#define NOT_OFF_LIMITS 0x04 /* may be accessed by extension function */
};
static const struct varinit varinit[] = {
{NULL, "ARGC", NULL, 0, NULL, NULL, false, NO_INSTALL },
{&ARGIND_node, "ARGIND", NULL, 0, NULL, NULL, false, NON_STANDARD },
{NULL, "ARGV", NULL, 0, NULL, NULL, false, NO_INSTALL },
{&BINMODE_node, "BINMODE", NULL, 0, NULL, set_BINMODE, false, NON_STANDARD },
{&CONVFMT_node, "CONVFMT", "%.6g", 0, NULL, set_CONVFMT,true, 0 },
{NULL, "ENVIRON", NULL, 0, NULL, NULL, false, NO_INSTALL },
{&ERRNO_node, "ERRNO", "", 0, NULL, NULL, false, NON_STANDARD },
{&FIELDWIDTHS_node, "FIELDWIDTHS", "", 0, NULL, set_FIELDWIDTHS, false, NON_STANDARD },
{&FILENAME_node, "FILENAME", "", 0, NULL, NULL, false, 0 },
{&FNR_node, "FNR", NULL, 0, update_FNR, set_FNR, true, 0 },
{&FS_node, "FS", " ", 0, NULL, set_FS, false, 0 },
{&FPAT_node, "FPAT", "[^[:space:]]+", 0, NULL, set_FPAT, false, NON_STANDARD },
{&IGNORECASE_node, "IGNORECASE", NULL, 0, NULL, set_IGNORECASE, false, NON_STANDARD },
{&LINT_node, "LINT", NULL, 0, NULL, set_LINT, false, NON_STANDARD },
{&PREC_node, "PREC", NULL, DEFAULT_PREC, NULL, set_PREC, false, NON_STANDARD},
{&NF_node, "NF", NULL, -1, update_NF, set_NF, false, 0 },
{&NR_node, "NR", NULL, 0, update_NR, set_NR, true, 0 },
{&OFMT_node, "OFMT", "%.6g", 0, NULL, set_OFMT, true, 0 },
{&OFS_node, "OFS", " ", 0, NULL, set_OFS, true, 0 },
{&ORS_node, "ORS", "\n", 0, NULL, set_ORS, true, 0 },
{NULL, "PROCINFO", NULL, 0, NULL, NULL, false, NO_INSTALL | NON_STANDARD | NOT_OFF_LIMITS },
{&RLENGTH_node, "RLENGTH", NULL, 0, NULL, NULL, false, 0 },
{&ROUNDMODE_node, "ROUNDMODE", DEFAULT_ROUNDMODE, 0, NULL, set_ROUNDMODE, false, NON_STANDARD },
{&RS_node, "RS", "\n", 0, NULL, set_RS, true, 0 },
{&RSTART_node, "RSTART", NULL, 0, NULL, NULL, false, 0 },
{&RT_node, "RT", "", 0, NULL, NULL, false, NON_STANDARD },
{&SUBSEP_node, "SUBSEP", "\034", 0, NULL, set_SUBSEP, true, 0 },
{&TEXTDOMAIN_node, "TEXTDOMAIN", "messages", 0, NULL, set_TEXTDOMAIN, true, NON_STANDARD },
{0, NULL, NULL, 0, NULL, NULL, false, 0 },
};