-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.c
543 lines (450 loc) · 13.8 KB
/
init.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
/********************************************
init.c
copyright 1991,2014-2016 Michael D. Brennan
This is a source file for mawk, an implementation of
the AWK programming language.
Mawk is distributed without warranty under the terms of
the GNU General Public License, version 3, 2007.
If you import elements of this code into another product,
you agree to not name that product mawk.
********************************************/
/* init.c */
#include <signal.h>
#include <stdbool.h>
#include "mawk.h"
#include "code.h"
#include "types_string.h"
#include "table.h"
#include "init.h"
#include "bi_vars.h"
#include "field.h"
static void process_cmdline( int, char ** );
static void set_ARGV( int, char **, int );
static void exit_bad_option( const char * );
static void exit_no_program( void );
static void print_help( void );
static void catch_fpe( int );
extern void print_version( void );
extern int is_cmdline_assign( char * );
#if MSDOS
void stdout_init( void );
#if HAVE_REARGV
void reargv( int *, char *** );
#endif
#endif
const char * progname;
int interactive_flag = 0;
#ifndef SET_PROGNAME
#define SET_PROGNAME() \
{ \
char * p = strrchr( argv[0], '/' ); \
progname = p ? p + 1 : argv[0]; \
}
#endif
static void
buffer_init( void )
{
string_buff = (char *)emalloc( SPRINTF_LIMIT );
string_buff_end = string_buff + SPRINTF_LIMIT;
}
void
initialize( int argc, char ** argv )
{
SET_PROGNAME();
buffer_init();
bi_vars_init(); /* load the builtin variables */
bi_funct_init(); /* load the builtin functions */
kw_init(); /* load the keywords */
field_init();
#if MSDOS
{
char * p = getenv( "MAWKBINMODE" );
if ( p )
set_binmode( atoi( p ) );
}
#endif
process_cmdline( argc, argv );
code_init();
signal( SIGFPE, catch_fpe );
set_stdoutput();
#if MSDOS
stdout_init();
#endif
}
int dump_code_flag; /* if on dump internal code */
int posix_space_flag;
#ifdef DEBUG
int dump_RE = 1; /* if on dump compiled REs */
#endif
// #define optarg optarg_ /* remove conflict with optarg in <getopt.h> */
#define MAWKCLI_REQ_ARG_VAL( ARGKEY ) \
do { \
if ( ++i == argc ) { \
errmsg( 0, "arg key missing val: %s", ( ARGKEY ) ); \
mawk_exit( 2 ); \
} \
} while ( 0 )
// typedef struct mm_args {
// unsigned int const argc,
// char const * const argv,
// }
// typedef struct mm_arg {
// mm_arg_handler * handle,
// mm_arg_setter * set
// } mm_arg;
// typedef struct mawk_opts {
// unsigned int is_interactive:1,
// unsigned int is_posix:1,
// unsigned int has_e:1,
// unsigned int has_f:1,
// //unsigned int is_:1,
// char * pfile_name,
// PFILE * pfile_list,
// } mawk_opts;
// typedef bool (*mawk_arg_handler)(const mawk_opts *);
// typedef bool (*mawk_arg_setter)(const mawk_opts *, const char *);
// mawk_arg_i_handler(const mawk_opts * opts) {
// }
// mm_arg[] mawk_args = {
// {.handle = &mawk_arg_i_handler},
// }
// #MM_
// struct props {
// unsigned int is_interactive:1,
// unsigned int is_posix:1,
// unsigned int has_e:1,
// unsigned int has_f:1,
// //unsigned int is_:1,
// char * pfile_name,
// PFILE * pfile_list,
// }
// begin() {
// }
// end() {
// }
// %%
// "-i" "--interactive" (*opts) {
// opts->is_interactive = 1;
// setbuf( stdout, (char *)0 );
// return true;
// }
// "--" (*opts) {
// }
// "--FS" (*opts) {
// // recognize escape sequences
// const size_t len = rm_escape( argv[i] );
// cell_destroy( FS );
// FS->type = C_STRING;
// FS->ptr = new_STRING2( argv[i], len );
// cast_for_split(
// cellcpy( &fs_shadow, FS )
// );
// }
// #
// typedef struct mawk_arg_def {
// mm_arg[] defs,
// } mawk_opt;a
static void
process_cmdline( int argc, char ** argv )
{
// unsigned char a = 'a';
// unsigned char A = 'A';
// unsigned char z = 'z';
// unsigned char Z = 'Z';
// unsigned char dash = '-';
// printf("\na=%u A=%u z=%u Z=%u -=%u\n",a,A,z,Z,dash);
// exit_no_program();
int i;
PFILE dummy; /* starts linked list of filenames */
PFILE * tail = &dummy;
for ( i = 1; i < argc && argv[i][0] == '-'; ) {
const char c1 = argv[i][1];
if ( c1 == 0 ) { // - alone
if ( !pfile_name )
exit_no_program();
break;
}
// safe to look at argv[i][2]
const char c2 = argv[i][2];
const bool isdubdash = c1 == '-';
const bool ismultichar = c2 != 0;
const char c = isdubdash && ismultichar
? c2
: c1;
switch ( c ) {
case 'i':
interactive_flag = 1;
setbuf( stdout, (char *)0 );
i++;
continue;
case '-':
i++;
goto no_more_opts;
case 'F':
MAWKCLI_REQ_ARG_VAL( "-F --FS field_separator" );
{
// recognize escape sequences
const size_t len = rm_escape( argv[i] );
cell_destroy( FS );
FS->type = C_STRING;
FS->ptr = new_STRING2( argv[i], len );
cast_for_split(
cellcpy( &fs_shadow, FS )
);
}
i++;
continue;
case 'f':
// TODO: ASSERT -e option not set
// first file goes in pfile_name ; any more go on a list
MAWKCLI_REQ_ARG_VAL( "-f file" );
if ( !pfile_name )
pfile_name = argv[i];
else {
tail = tail->link = ZMALLOC( PFILE );
tail->fname = argv[i];
}
i++;
continue;
case 'e':
// -e --exec file
MAWKCLI_REQ_ARG_VAL( "-e --exec file" );
if ( pfile_name ) {
errmsg( 0, "--exec is incompatible with -f" );
mawk_exit( 2 );
}
pfile_name = argv[i];
i++;
goto no_more_opts;
case 'v':
// --version
if (ismultichar) {
print_version();
i++;
}
// -v var=val
else {
MAWKCLI_REQ_ARG_VAL( "-v var=val" );
if ( !is_cmdline_assign( argv[i] ) ) {
errmsg( 0, "improper assignment: -v %s", argv[i] );
mawk_exit( 2 );
}
i++;
}
continue;
case 'h':
// -h --help
print_help();
i++;
continue;
case 'd':
// -d --dump
dump_code_flag = 1;
i++;
continue;
case 'p':
posix_space_flag = 1;
posix_repl_scan_flag = 1;
i++;
continue;
#if MSDOS
case 'b':
// --bin-mode=...?
if (ismultichar) {
char * p = strchr( argv[i], '=' );
int x = p ? atoi( p + 1 ) : 0;
set_binmode( x );
i++;
}
// -b ...?
// -bin-mode ...?
else {
MAWKCLI_REQ_ARG_VAL( "-b bin-mode" );
}
continue;
#endif
case 's': // -s !!OBSELETE!!
i++;
continue;
default:
exit_bad_option( argv[i] );
}
// TODO:
// else { // argument glued to option
// optarg = &argv[i][2];
// nextarg = i + 1;
// }
}
no_more_opts:
tail->link = (PFILE *)0;
pfile_list = dummy.link;
if ( pfile_name ) {
set_ARGV( argc, argv, i );
scan_init( (char *)0 );
}
else /* program on command line */
{
if ( i == argc )
exit_no_program();
set_ARGV( argc, argv, i + 1 );
#if MSDOS && !HAVE_REARGV /* reversed quotes */
{
char * p;
for ( p = argv[i]; *p; p++ )
if ( *p == '\'' )
*p = '\"';
}
#endif
scan_init( argv[i] );
/* #endif */
}
}
static void
set_ARGV( int argc, char ** argv, int i )
/* argv[i] = ARGV[i] */
{
SYMTAB * st_p;
CELL argi;
register CELL * cp;
st_p = insert( "ARGV" );
st_p->type = ST_ARRAY;
Argv = st_p->stval.array = new_ARRAY();
argi.type = C_DOUBLE;
argi.dval = 0.0;
cp = array_find( st_p->stval.array, &argi, CREATE );
cp->type = C_STRING;
cp->ptr = (void *)new_STRING( argv[0] );
/* ARGV[0] is set, do the rest
The type of ARGV[1] ... should be C_MBSTRN
because the user might enter numbers from the command line */
for ( argi.dval = 1.0; i < argc; i++, argi.dval += 1.0 ) {
cp = array_find( st_p->stval.array, &argi, CREATE );
cp->type = C_MBSTRN;
cp->ptr = (void *)new_STRING( argv[i] );
}
ARGC->type = C_DOUBLE;
ARGC->dval = argi.dval;
}
/*----- ENVIRON ----------*/
void
load_environ( ARRAY ENV )
{
CELL c;
extern char ** environ;
register char ** p = environ; /* walks environ */
char * s; /* looks for the '=' */
CELL * cp; /* pts at ENV[&c] */
c.type = C_STRING;
while ( *p ) {
if ( ( s = strchr( *p, '=' ) ) ) /* shouldn't fail */
{
int len = s - *p;
c.ptr = (void *)new_STRING0( len );
memcpy( string( &c )->str, *p, len );
s++;
cp = array_find( ENV, &c, CREATE );
cp->type = C_MBSTRN;
cp->ptr = (void *)new_STRING( s );
free_STRING( string( &c ) );
}
p++;
}
}
/* FPE exceptions
mawk 1.x.x in 199x tried hard to catch fpe when necessary,
have optimal settings and
maximize information passed to user
What to do in 201x?
Odds of fpe in an awk program is verrrrrrrrrry small
probablity it generates an INF or NAN is at least .9
probably higher
Since an fpe is unlikely, mawk 2.x.x will deal with
it very simply.
*/
static void
catch_fpe( int x )
{
if ( x == SIGFPE ) {
rt_error( "floating point arithmetic exception" );
}
else
bozo( "catch_fpe" );
mawk_exit( 2 );
}
static void
exit_bad_option( const char * s )
{
errmsg( 0, "not an option: %s", s );
mawk_exit( 2 );
}
static void
exit_no_program( void )
{
mawk_exit( 0 );
}
/* HELP */
/* to change this
edit help.txt
run $ mawk -f help.awk help.txt > out
with an editor put out into help[]
*/
static const char * const help[] = {
"Usage:",
"\tmawk [--help]",
"\tmawk [--version]",
"\tmawk [-W option] [-F value] [-v var=value] [--] 'program text' file(s)",
"\tmawk [-W option] [-F value] [-v var=value] [-f program-file] [--] file(s)",
"",
"Generic options:",
"",
"\t--help displays this help message and exits 0.",
"",
"\t--version displays mawk version and exits 0.",
"",
"Normal awk-implementation options:",
"",
"\t-F value sets the field separator, FS, to value.",
"",
"\t-f file program text is read from file instead of from the",
"\t command line. Multiple -f options are allowed.",
"",
"\t-v var=value assigns value to program variable var.",
"",
"\t-- indicates the unambiguous end of options.",
"",
"The above options are available with any IEEE 1003 POSIX-compatible",
"implementation of AWK. mawk-specific options are prefaced with -W.",
"",
"\t-W dump writes an assembler-like listing of the internal",
"\t representation of the program to stdout and exits 0 (on",
"\t successful compilation).",
"",
"\t-W exec file program text is read from file and this is the last",
"\t option. It is useful on systems that support the",
"\t #! \"magic number\" convention for executable scripts.",
"",
"\t-W help displays this help message and exits 0.",
"",
"\t-W interactive sets unbuffered writes to stdout and line buffered reads",
"\t from stdin. Records from stdin are lines regardless of",
"\t the value of RS.",
"",
"\t-W posix forces mawk not to consider '\\n' to be space and \\\\",
"\t is always \\ on the second scan of a replacement string.",
"",
"\t-W version displays mawk version and exits 0.",
"",
"Just the first letter for each option is enough. For example, -Wv, -W v ",
"and --v are equivalent to -W version or --version.",
0
};
static void
print_help()
{
int i = 0;
while ( help[i] ) {
printf( "%s\n", help[i] );
i++;
}
mawk_exit( 0 );
}