Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
694 lines (602 loc) · 14.8 KB
/
Copy pathmain.c
File metadata and controls
694 lines (602 loc) · 14.8 KB
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
/*
|| This file is part of Pike. For copyright information see COPYRIGHT.
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING
|| for more information.
*/
#include "global.h"
#include "fdlib.h"
#include "backend.h"
#include "object.h"
#include "lex.h"
#include "pike_types.h"
#include "threads.h"
#include "builtin_functions.h"
#include "array.h"
#include "stralloc.h"
#include "interpret.h"
#include "pike_error.h"
#include "pike_macros.h"
#include "callback.h"
#include "signal_handler.h"
#include "dynamic_load.h"
#include "gc.h"
#include "multiset.h"
#include "mapping.h"
#include "cpp.h"
#include "main.h"
#include "operators.h"
#include "rbtree.h"
#include "constants.h"
#include "version.h"
#include "program.h"
#include "pike_rusage.h"
#include "module_support.h"
#include "opcodes.h"
#include "buffer.h"
#include "pike_embed.h"
#ifdef LIBPIKE
#if defined(HAVE_DLOPEN) && defined(HAVE_DLFCN_H)
#include <dlfcn.h>
#undef LIBPIKE
#endif
#endif
#include "las.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <errno.h>
#include <locale.h>
#include "time_stuff.h"
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
/* Define this to trace the execution of main(). */
/* #define TRACE_MAIN */
#ifdef PIKE_EXTRA_DEBUG
#define TRACE_MAIN
#endif /* PIKE_EXTRA_DEBUG */
#ifdef TRACE_MAIN
#define TRACE(...) fprintf(stderr, __VA_ARGS__)
#else /* !TRACE_MAIN */
#define TRACE(...)
#endif /* TRACE_MAIN */
/*
* Code searching for master & libpike.
*/
#define MASTER_COOKIE1 "(#*&)@(*&$"
#define MASTER_COOKIE2 "Master Cookie:"
#define MASTER_COOKIE MASTER_COOKIE1 MASTER_COOKIE2
#ifndef MAXPATHLEN
#define MAXPATHLEN 32768
#endif
static const char _master_location[MAXPATHLEN+CONSTANT_STRLEN(MASTER_COOKIE)] = MASTER_COOKIE "\0";
static const char *master_file_location = _master_location + CONSTANT_STRLEN(MASTER_COOKIE);
static void set_master(const char *file)
{
if( master_file_location != _master_location+CONSTANT_STRLEN(MASTER_COOKIE))
free((void*)master_file_location);
#ifdef DEBUG_MALLOC
#undef strdup /* We can't use dmalloc strdup before pike_memory is initialized. */
#endif /* DEBUG_MALLOC */
#if defined(__NT__) && !defined(strdup)
/* strdup() is broken on WIN32, but _strdup() isn't... */
#define strdup _strdup
#endif /* __NT__ */
master_file_location = strdup( file );
}
#ifdef __NT__
static void get_master_key(HKEY cat)
{
HKEY k;
char buffer[4096];
DWORD len=sizeof(buffer)-1,type=REG_SZ;
if(RegOpenKeyEx(cat,
(LPCTSTR)("SOFTWARE\\Pike\\"
DEFINETOSTR(PIKE_MAJOR_VERSION)
"."
DEFINETOSTR(PIKE_MINOR_VERSION)
"."
DEFINETOSTR(PIKE_BUILD_VERSION)),
0,KEY_READ,&k)==ERROR_SUCCESS)
{
if(RegQueryValueEx(k,
"PIKE_MASTER",
0,
&type,
buffer,
&len)==ERROR_SUCCESS)
{
/* FIXME: Look at len? UNICODE? */
set_master(buffer);
}
RegCloseKey(k);
}
}
#endif /* __NT__ */
static void set_default_master(const char *bin_name)
{
#define mp master_file_location
if(getenv("PIKE_MASTER")) {
set_master(getenv("PIKE_MASTER"));
}
#ifdef __NT__
if(!*mp) get_master_key(HKEY_CURRENT_USER);
if(!*mp) get_master_key(HKEY_LOCAL_MACHINE);
#endif
if(!*mp && strncmp(DEFAULT_MASTER, "NONE/", 5))
{
char tmp[CONSTANT_STRLEN( DEFAULT_MASTER ) + 10 + 10 + 10];
snprintf (tmp, sizeof(tmp)-1,
DEFAULT_MASTER,
PIKE_MAJOR_VERSION,
PIKE_MINOR_VERSION,
PIKE_BUILD_VERSION);
set_master( tmp );
}
#ifdef __NT__
if (!(*mp == '/' || *mp == '\\' || (isalpha (*mp) && mp[1] == ':'))) {
char exepath[MAXPATHLEN];
if (!*mp) set_master("master.pike");
if (!GetModuleFileName (NULL, exepath, _MAX_PATH))
fprintf (stderr, "Failed to get path to exe file: %d\n",
GetLastError());
else {
char tmp[MAXPATHLEN * 2];
char *p = strrchr (exepath, '\\');
if (p) *p = 0;
snprintf (tmp, sizeof (tmp), "%s/%s", exepath, mp);
set_master( tmp );
}
}
#else
if (!*mp) {
/* Attempt to find a master via the path to the binary. */
/* Note: We assume that MAXPATHLEN is > 18 characters. */
if (strlen(bin_name) + CONSTANT_STRLEN("master.pike") < MAXPATHLEN) {
char tmp[MAXPATHLEN];
char *p;
strcpy(tmp, bin_name);
p = strrchr(tmp, '/');
if (!p) p = tmp;
else p++;
strcpy(p, "master.pike");
set_master( tmp );
}
}
#endif
TRACE("Default master at \"%s\"...\n", mp);
#undef mp
}
#ifdef LIBPIKE
static char libpike_file[MAXPATHLEN * 2];
static void *libpike;
typedef void (*modfun)(void);
#ifdef NO_CAST_TO_FUN
/* Function pointers can't be casted to scalar pointers according to
* ISO-C (probably to support true Harward achitecture machines).
*/
static modfun CAST_TO_FUN(void *ptr)
{
union {
void *ptr;
modfun fun;
} u;
u.ptr = ptr;
return u.fun;
}
#else /* !NO_CAST_TO_FUN */
#define CAST_TO_FUN(X) ((modfun)X)
#endif /* NO_CAST_TO_FUN */
static void (*init_pike_var)(const char **argv, const char *file);
static void (*init_pike_runtime_var)(void (*exit_cb)(int));
static void (*add_predefine_var)(char *s);
#endif /* LIBPIKE */
static void find_lib_dir(int argc, char **argv)
{
int e;
TRACE("find_lib_dir...\n");
set_default_master(argv[0]);
for(e=1; e<argc; e++)
{
TRACE("Parse argument %d:\"%s\"...\n", e, argv[e]);
if(argv[e][0] != '-') break;
switch(argv[e][1])
{
default:
break;
case 'm':
if(argv[e][2])
{
set_master(argv[e]+2);
}else{
e++;
if(e >= argc)
{
fprintf(stderr,"Missing argument to -m\n");
exit(1);
}
set_master(argv[e]);
}
break;
case 's':
if((!argv[e][2]) ||
((argv[e][2] == 's') && !argv[e][3])) {
e++;
}
break;
case 'q':
if(!argv[e][2]) e++;
break;
}
}
#ifdef LIBPIKE
{
char *p;
char *dir;
strcpy(libpike_file, master_file_location);
for (p = dir = libpike_file; *p; p++) {
if ((*p == '/')
#ifdef __NT__
|| (*p == '\\')
#endif /* __NT__ */
)
dir = p+1;
}
if ((dir + CONSTANT_STRLEN("pike.so")) >= libpike_file + 2*MAXPATHLEN) {
/* Not likely to happen as long as MASTER_COOKIE is longer than "pike.so".
*/
fprintf(stderr, "Too long path to pike.so.\n");
exit(1);
}
/* Don't forget the NUL! */
memcpy(dir, "pike.so", CONSTANT_STRLEN("pike.so") + 1);
}
#endif /* LIBPIKE */
}
#ifdef __amigaos__
static const char version_tag[] __attribute__((used)) =
"\0$VER: pike " DEFINETOSTR(PIKE_MAJOR_VERSION)
"." DEFINETOSTR(PIKE_MINOR_VERSION)
" (" DEFINETOSTR(PIKE_BUILD_DATE)
") build " DEFINETOSTR(PIKE_BUILD_VERSION) "\0";
#endif
int main(int argc, char **argv)
{
JMP_BUF back;
int e, num;
char *p;
#ifdef PIKE_EXTRA_DEBUG
if (sizeof(void *) == 8) {
/* 64-bit Solaris 10 in Xenofarm fails with SIGPIPE.
* Force a core dump.
*/
signal(SIGPIPE, abort);
}
#endif
#ifdef HAVE_MALLOPT
TRACE("Init malloc...\n");
/* The malloc implementation in recent glibc (eg Linux)
* defaults to using one arena / thread. This means that
* memory once allocated from one thread can't later be
* allocated by another thread.
*
* cf [bug 6045] and http://sourceware.org/bugzilla/show_bug.cgi?id=11261
*
* We try to alleviate the problem by reducing the number
* of arenas as much as possible.
*/
#ifdef M_ARENA_TEST
/* NB: Some versions of glibc don't support setting M_ARENA_TEST to 0. */
/* Note also that the test is inverted since mallopt returns 0 on success. */
mallopt(M_ARENA_TEST, 0) && mallopt(M_ARENA_TEST, 1);
#endif
#ifdef M_ARENA_MAX
mallopt(M_ARENA_MAX, 1);
#endif
#endif /* HAVE_MALLOPT */
TRACE("Init master...\n");
find_lib_dir(argc, argv);
#ifdef LIBPIKE
#ifdef HAVE_DLINIT
if (!dlinit()) {
fprintf(stderr, "dlinit failed.\n");
exit(1);
}
#endif /* HAVE_DLINIT */
if (!(libpike = dlopen(libpike_file, RTLD_NOW))) {
const char *err = dlerror();
if (!err) err = "Unknown reason.";
fprintf(stderr, "Failed to open %s: %s\n", libpike_file, err);
exit(1);
}
#define LOOKUP(symbol) do { \
if (!(PIKE_CONCAT(symbol, _var) = \
CAST_TO_FUN(dlsym(libpike, TOSTR(symbol)))) && \
!(PIKE_CONCAT(symbol, _var) = \
CAST_TO_FUN(dlsym(libpike, "_" TOSTR(symbol))))) { \
fprintf(stderr, "Missing symbol in %s: " TOSTR(symbol) "\n", \
libpike_file); \
dlclose(libpike); \
exit(1); \
} \
} while(0)
LOOKUP(init_pike);
#define init_pike init_pike_var
LOOKUP(init_pike_runtime);
#define init_pike_runtime init_pike_runtime_var
LOOKUP(add_predefine);
#define add_predefine add_predefine_var
#endif /* LIBPIKE */
TRACE("init_pike()\n");
init_pike(argv, master_file_location);
for(e=1; e<argc; e++)
{
TRACE("Parse argument %d:\"%s\"...\n", e, argv[e]);
if(argv[e][0]=='-')
{
for(p=argv[e]+1; *p;)
{
switch(*p)
{
case 'D':
add_predefine(p+1);
p+=strlen(p);
break;
case 'm':
if(p[1])
{
p+=strlen(p);
}else{
e++;
if(e >= argc)
{
fprintf(stderr,"Missing argument to -m\n");
exit(1);
}
p+=strlen(p);
}
break;
case 's':
if(!p[1])
{
e++;
if(e >= argc)
{
fprintf(stderr,"Missing argument to -s\n");
exit(1);
}
p=argv[e];
}else{
p++;
if(*p=='s')
{
if(!p[1])
{
e++;
if(e >= argc)
{
fprintf(stderr,"Missing argument to -ss\n");
exit(1);
}
p=argv[e];
}else{
p++;
}
#ifdef _REENTRANT
thread_stack_size=strtol(p,&p,0);
#endif
p+=strlen(p);
break;
}
}
Pike_stack_size=strtol(p,&p,0);
p+=strlen(p);
if(Pike_stack_size < 256)
{
fprintf(stderr,"Stack size must at least be 256.\n");
exit(1);
}
break;
case 'q':
if(!p[1])
{
e++;
if(e >= argc)
{
fprintf(stderr,"Missing argument to -q\n");
exit(1);
}
p=argv[e];
}else{
p++;
}
set_pike_evaluator_limit(strtol(p, &p, 0));
p+=strlen(p);
break;
case 'd':
more_d_flags:
switch(p[1])
{
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
d_flag+=strtol(p+1,&p,10);
break;
case 'c':
p++;
#if (defined(YYDEBUG) && (YYDEBUG==1)) && defined(PIKE_DEBUG)
yydebug++;
#endif /* YYDEBUG && PIKE_DEBUG */
break;
case 's':
set_pike_debug_options(DEBUG_SIGNALS, DEBUG_SIGNALS);
p++;
goto more_d_flags;
case 't':
set_pike_debug_options(NO_TAILRECURSION, NO_TAILRECURSION);
p++;
goto more_d_flags;
case 'g':
set_pike_debug_options(GC_RESET_DMALLOC, GC_RESET_DMALLOC);
p++;
goto more_d_flags;
case 'p':
set_pike_debug_options(NO_PEEP_OPTIMIZING, NO_PEEP_OPTIMIZING);
p++;
goto more_d_flags;
case 'T':
set_pike_debug_options(ERRORCHECK_MUTEXES, ERRORCHECK_MUTEXES);
p++;
goto more_d_flags;
case 'L':
set_pike_debug_options (WINDOWS_ERROR_DIALOGS,
WINDOWS_ERROR_DIALOGS);
p++;
goto more_d_flags;
default:
d_flag += (p[0] == 'd');
p++;
}
break;
case 'r':
more_r_flags:
switch(p[1])
{
case 't':
set_pike_runtime_options(RUNTIME_CHECK_TYPES, RUNTIME_CHECK_TYPES);
p++;
goto more_r_flags;
case 'T':
set_pike_runtime_options(RUNTIME_STRICT_TYPES,
RUNTIME_STRICT_TYPES);
p++;
goto more_r_flags;
default:
p++;
break;
}
break;
case 'a':
if(p[1]>='0' && p[1]<='9')
a_flag+=strtol(p+1,&p,10);
else
a_flag++,p++;
break;
case 't':
more_t_flags:
switch (p[1]) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
Pike_interpreter.trace_level+=strtol(p+1,&p,10);
break;
case 'g':
gc_trace++;
p++;
goto more_t_flags;
default:
if (p[0] == 't')
Pike_interpreter.trace_level++;
p++;
}
default_t_flag = Pike_interpreter.trace_level;
break;
case 'p':
if(p[1]=='s')
{
pike_enable_stack_profiling();
p+=strlen(p);
}else{
if(p[1]>='0' && p[1]<='9')
p_flag+=strtol(p+1,&p,10);
else
p_flag++,p++;
}
break;
case 'l':
if(p[1]>='0' && p[1]<='9')
l_flag+=strtol(p+1,&p,10);
else
l_flag++,p++;
break;
default:
p+=strlen(p);
}
}
}else{
break;
}
}
#ifndef PIKE_MUTEX_ERRORCHECK
if (set_pike_debug_options(0,0) & ERRORCHECK_MUTEXES)
fputs ("Warning: -dT (error checking mutexes) not supported on this system.\n",
stderr);
#endif
if (d_flag)
set_pike_debug_options(ERRORCHECK_MUTEXES, ERRORCHECK_MUTEXES);
#ifdef HAVE_SETERRORMODE
if (!(debug_options & WINDOWS_ERROR_DIALOGS)) {
/* This avoids popups when LoadLibrary fails to find a dll.
*
* Note that the popup is the _only_ way to see which dll (loaded
* indirectly by dependencies from the one in the LoadLibrary
* call) that Windows didn't find. :( Hence the -rl runtime option
* to turn it on.
*
* Note: This setting is process global. */
SetErrorMode (SEM_FAILCRITICALERRORS |
/* Maybe set this too? Don't know exactly when it
* has effect. /mast */
/*SEM_NOOPENFILEERRORBOX | */
SetErrorMode (0));
}
#endif
init_pike_runtime(exit);
/* NOTE: Reuse master_location here to avoid duplicates of
* the MASTER_COOKIE string in the binary.
*/
add_pike_string_constant("__master_cookie",
_master_location, CONSTANT_STRLEN(MASTER_COOKIE));
if(SETJMP(back))
{
if(throw_severity == THROW_EXIT || throw_severity == THROW_THREAD_EXIT)
{
num=throw_value.u.integer;
}else{
if (TYPEOF(throw_value) == T_OBJECT &&
throw_value.u.object->prog == master_load_error_program &&
!get_master()) {
/* Report this specific error in a nice way. Since there's no
* master it'd be reported with a raw error dump otherwise. */
struct generic_error_struct *err;
struct byte_buffer buf = BUFFER_INIT();
struct svalue t;
move_svalue (Pike_sp++, &throw_value);
mark_free_svalue (&throw_value);
err =
get_storage (Pike_sp[-1].u.object, generic_error_program);
SET_SVAL(t, PIKE_T_STRING, 0, string, err->error_message);
describe_svalue(&buf, &t,0,0);
fputs(buffer_get_string(&buf), stderr);
buffer_free(&buf);
}
else
call_handle_error();
num=10;
}
}else{
struct object *m;
back.severity=THROW_EXIT;
if ((m = load_pike_master())) {
TRACE("Call master->_main()...\n");
pike_push_argv(argc, argv);
apply(m, "_main", 1);
pop_stack();
num=0;
} else {
num = -1;
}
}
UNSETJMP(back);
TRACE("Exit %d...\n", num);
pike_do_exit(num);
return num; /* avoid warning */
}