1 /********************************************
2 main.c
3 copyright 2009-2014,2017 Thomas E. Dickey
4 copyright 1991-1995,2014, Michael D. Brennan
5
6 This is a source file for mawk, an implementation of
7 the AWK programming language.
8
9 Mawk is distributed without warranty under the terms of
10 the GNU General Public License, version 2, 1991.
11 ********************************************/
12
13 /*
14 * $MawkId: main.c,v 1.31 2017/10/17 00:41:48 tom Exp $
15 */
16
17 /* main.c */
18
19 #include "mawk.h"
20 #include "bi_vars.h"
21 #include "init.h"
22 #include "code.h"
23 #include "files.h"
24
25 #ifdef LOCALE
26 #include <locale.h>
27 #endif
28
29 short mawk_state; /* 0 is compiling */
30 int exit_code;
31
32 #if defined(__GNUC__) && defined(_FORTIFY_SOURCE)
33 int ignore_unused;
34 #endif
35
36 #ifdef LOCALE
37 char decimal_dot;
38 #endif
39
40 int
main(int argc,char ** argv)41 main(int argc, char **argv)
42 {
43 #ifdef LOCALE
44 setlocale(LC_CTYPE, "");
45 setlocale(LC_NUMERIC, "C");
46 #endif
47 initialize(argc, argv);
48 #ifdef LOCALE
49 {
50 struct lconv *data;
51
52 decimal_dot = '\0'; /* only set to nonzero if not POSIX '.' */
53 setlocale(LC_NUMERIC, "");
54 data = localeconv();
55 if (data != 0
56 && data->decimal_point != 0
57 && strlen(data->decimal_point) == 1) {
58 decimal_dot = data->decimal_point[0];
59 } else {
60 /* back out of this if we cannot handle it */
61 setlocale(LC_NUMERIC, "C");
62 }
63 if (decimal_dot == '.')
64 decimal_dot = 0;
65 }
66 #endif
67
68 parse();
69
70 mawk_state = EXECUTION;
71 execute(execution_start, eval_stack - 1, 0);
72 /* never returns */
73 return 0;
74 }
75
76 void
mawk_exit(int x)77 mawk_exit(int x)
78 {
79 #ifdef HAVE_REAL_PIPES
80 close_out_pipes(); /* actually closes all output */
81 #else
82 #ifdef HAVE_FAKE_PIPES
83 close_fake_pipes();
84 #endif
85 #endif
86
87 #ifdef NO_LEAKS
88 code_leaks();
89 scan_leaks();
90 cell_leaks();
91 re_leaks();
92 rexp_leaks();
93 bi_vars_leaks();
94 hash_leaks();
95 array_leaks();
96 files_leaks();
97 fin_leaks();
98 field_leaks();
99 zmalloc_leaks();
100 #if OPT_TRACE > 0
101 trace_leaks();
102 #endif
103 #endif
104
105 exit(x);
106 }
107