This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
$0 doc tweakage.
[perl5.git] / perl.c
CommitLineData
a0d0e21e
LW
1/* perl.c
2 *
4bb101f2
JH
3 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, by Larry Wall and others
a687059c 5 *
352d5a3a
LW
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
a687059c 8 *
8d063cd8
LW
9 */
10
a0d0e21e
LW
11/*
12 * "A ship then new they built for him/of mithril and of elven glass" --Bilbo
13 */
45d8adaa 14
378cc40b 15#include "EXTERN.h"
864dbfa3 16#define PERL_IN_PERL_C
378cc40b 17#include "perl.h"
e3321bb0 18#include "patchlevel.h" /* for local_patches */
378cc40b 19
011f1a1a
JH
20#ifdef NETWARE
21#include "nwutil.h"
22char *nw_get_sitelib(const char *pl);
23#endif
24
df5cef82 25/* XXX If this causes problems, set i_unistd=undef in the hint file. */
a0d0e21e
LW
26#ifdef I_UNISTD
27#include <unistd.h>
28#endif
a0d0e21e 29
5311654c
JH
30#ifdef __BEOS__
31# define HZ 1000000
32#endif
33
34#ifndef HZ
35# ifdef CLK_TCK
36# define HZ CLK_TCK
37# else
38# define HZ 60
39# endif
40#endif
41
7114a2d2 42#if !defined(STANDARD_C) && !defined(HAS_GETENV_PROTOTYPE) && !defined(PERL_MICRO)
20ce7b12 43char *getenv (char *); /* Usually in <stdlib.h> */
54310121
PP
44#endif
45
acfe0abc 46static I32 read_e_script(pTHX_ int idx, SV *buf_sv, int maxlen);
0cb96387 47
a687059c
LW
48#ifdef IAMSUID
49#ifndef DOSUID
50#define DOSUID
51#endif
52#endif
378cc40b 53
a687059c
LW
54#ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
55#ifdef DOSUID
56#undef DOSUID
57#endif
58#endif
8d063cd8 59
3db8f154 60#if defined(USE_ITHREADS)
06d86050
GS
61# define INIT_TLS_AND_INTERP \
62 STMT_START { \
63 if (!PL_curinterp) { \
64 PERL_SET_INTERP(my_perl); \
65 INIT_THREADS; \
66 ALLOC_THREAD_KEY; \
534825c4
GS
67 PERL_SET_THX(my_perl); \
68 OP_REFCNT_INIT; \
7d4724a4 69 MUTEX_INIT(&PL_dollarzero_mutex); \
534825c4
GS
70 } \
71 else { \
72 PERL_SET_THX(my_perl); \
06d86050 73 } \
06d86050 74 } STMT_END
3db8f154 75#else
06d86050
GS
76# define INIT_TLS_AND_INTERP \
77 STMT_START { \
78 if (!PL_curinterp) { \
79 PERL_SET_INTERP(my_perl); \
80 } \
81 PERL_SET_THX(my_perl); \
82 } STMT_END
83# endif
06d86050 84
32e30700
GS
85#ifdef PERL_IMPLICIT_SYS
86PerlInterpreter *
7766f137
GS
87perl_alloc_using(struct IPerlMem* ipM, struct IPerlMem* ipMS,
88 struct IPerlMem* ipMP, struct IPerlEnv* ipE,
32e30700
GS
89 struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
90 struct IPerlDir* ipD, struct IPerlSock* ipS,
91 struct IPerlProc* ipP)
92{
93 PerlInterpreter *my_perl;
32e30700
GS
94 /* New() needs interpreter, so call malloc() instead */
95 my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
06d86050 96 INIT_TLS_AND_INTERP;
32e30700
GS
97 Zero(my_perl, 1, PerlInterpreter);
98 PL_Mem = ipM;
7766f137
GS
99 PL_MemShared = ipMS;
100 PL_MemParse = ipMP;
32e30700
GS
101 PL_Env = ipE;
102 PL_StdIO = ipStd;
103 PL_LIO = ipLIO;
104 PL_Dir = ipD;
105 PL_Sock = ipS;
106 PL_Proc = ipP;
7766f137 107
32e30700
GS
108 return my_perl;
109}
110#else
954c1994
GS
111
112/*
ccfc67b7
JH
113=head1 Embedding Functions
114
954c1994
GS
115=for apidoc perl_alloc
116
117Allocates a new Perl interpreter. See L<perlembed>.
118
119=cut
120*/
121
93a17b20 122PerlInterpreter *
cea2e8a9 123perl_alloc(void)
79072805 124{
cea2e8a9 125 PerlInterpreter *my_perl;
35d7cf2c
JH
126#ifdef USE_5005THREADS
127 dTHX;
128#endif
79072805 129
54aff467 130 /* New() needs interpreter, so call malloc() instead */
e8ee3774 131 my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
ba869deb 132
06d86050 133 INIT_TLS_AND_INTERP;
dedcbb81 134 Zero(my_perl, 1, PerlInterpreter);
cea2e8a9 135 return my_perl;
79072805 136}
32e30700 137#endif /* PERL_IMPLICIT_SYS */
79072805 138
954c1994
GS
139/*
140=for apidoc perl_construct
141
142Initializes a new Perl interpreter. See L<perlembed>.
143
144=cut
145*/
146
79072805 147void
0cb96387 148perl_construct(pTHXx)
79072805 149{
8990e307 150#ifdef MULTIPLICITY
54aff467 151 init_interp();
ac27b0f5 152 PL_perl_destruct_level = 1;
54aff467
GS
153#else
154 if (PL_perl_destruct_level > 0)
155 init_interp();
156#endif
33f46ff6 157 /* Init the real globals (and main thread)? */
3280af22 158 if (!PL_linestr) {
14dd3ad8 159#ifdef PERL_FLEXIBLE_EXCEPTIONS
0b94c7bb 160 PL_protect = MEMBER_TO_FPTR(Perl_default_protect); /* for exceptions */
14dd3ad8 161#endif
312caa8e 162
2aea9f8a
GS
163 PL_curcop = &PL_compiling; /* needed by ckWARN, right away */
164
3280af22
NIS
165 PL_linestr = NEWSV(65,79);
166 sv_upgrade(PL_linestr,SVt_PVIV);
79072805 167
3280af22 168 if (!SvREADONLY(&PL_sv_undef)) {
d689ffdd
JP
169 /* set read-only and try to insure than we wont see REFCNT==0
170 very often */
171
3280af22
NIS
172 SvREADONLY_on(&PL_sv_undef);
173 SvREFCNT(&PL_sv_undef) = (~(U32)0)/2;
79072805 174
3280af22
NIS
175 sv_setpv(&PL_sv_no,PL_No);
176 SvNV(&PL_sv_no);
177 SvREADONLY_on(&PL_sv_no);
178 SvREFCNT(&PL_sv_no) = (~(U32)0)/2;
79072805 179
3280af22
NIS
180 sv_setpv(&PL_sv_yes,PL_Yes);
181 SvNV(&PL_sv_yes);
182 SvREADONLY_on(&PL_sv_yes);
183 SvREFCNT(&PL_sv_yes) = (~(U32)0)/2;
6e72f9df 184 }
79072805 185
cea2e8a9 186 PL_sighandlerp = Perl_sighandler;
3280af22 187 PL_pidstatus = newHV();
79072805
LW
188 }
189
8bfdd7d9 190 PL_rs = newSVpvn("\n", 1);
dc92893f 191
cea2e8a9 192 init_stacks();
79072805 193
748a9306 194 init_ids();
3280af22 195 PL_lex_state = LEX_NOTPARSING;
a5f75d66 196
312caa8e 197 JMPENV_BOOTSTRAP;
f86702cc
PP
198 STATUS_ALL_SUCCESS;
199
0672f40e 200 init_i18nl10n(1);
36477c24 201 SET_NUMERIC_STANDARD();
0b5b802d 202
a7cb1f99
GS
203 {
204 U8 *s;
205 PL_patchlevel = NEWSV(0,4);
155aba94 206 (void)SvUPGRADE(PL_patchlevel, SVt_PVNV);
a7cb1f99 207 if (PERL_REVISION > 127 || PERL_VERSION > 127 || PERL_SUBVERSION > 127)
806e7201 208 SvGROW(PL_patchlevel, UTF8_MAXLEN*3+1);
a7cb1f99 209 s = (U8*)SvPVX(PL_patchlevel);
9041c2e3
NIS
210 /* Build version strings using "native" characters */
211 s = uvchr_to_utf8(s, (UV)PERL_REVISION);
212 s = uvchr_to_utf8(s, (UV)PERL_VERSION);
213 s = uvchr_to_utf8(s, (UV)PERL_SUBVERSION);
a7cb1f99
GS
214 *s = '\0';
215 SvCUR_set(PL_patchlevel, s - (U8*)SvPVX(PL_patchlevel));
216 SvPOK_on(PL_patchlevel);
00d6e121
MB
217 SvNVX(PL_patchlevel) = (NV)PERL_REVISION +
218 ((NV)PERL_VERSION / (NV)1000) +
219 ((NV)PERL_SUBVERSION / (NV)1000000);
a7cb1f99
GS
220 SvNOK_on(PL_patchlevel); /* dual valued */
221 SvUTF8_on(PL_patchlevel);
222 SvREADONLY_on(PL_patchlevel);
223 }
79072805 224
ab821d7f 225#if defined(LOCAL_PATCH_COUNT)
3280af22 226 PL_localpatches = local_patches; /* For possible -v */
ab821d7f
PP
227#endif
228
52853b95
GS
229#ifdef HAVE_INTERP_INTERN
230 sys_intern_init();
231#endif
232
3a1ee7e8 233 PerlIO_init(aTHX); /* Hook to IO system */
760ac839 234
3280af22
NIS
235 PL_fdpid = newAV(); /* for remembering popen pids by fd */
236 PL_modglobal = newHV(); /* pointers to per-interpreter module globals */
24944567 237 PL_errors = newSVpvn("",0);
48c6b404 238 sv_setpvn(PERL_DEBUG_PAD(0), "", 0); /* For regex debugging. */
1f483ca1
JH
239 sv_setpvn(PERL_DEBUG_PAD(1), "", 0); /* ext/re needs these */
240 sv_setpvn(PERL_DEBUG_PAD(2), "", 0); /* even without DEBUGGING. */
1fcf4c12 241#ifdef USE_ITHREADS
13137afc
AB
242 PL_regex_padav = newAV();
243 av_push(PL_regex_padav,(SV*)newAV()); /* First entry is an array of empty elements */
244 PL_regex_pad = AvARRAY(PL_regex_padav);
1fcf4c12 245#endif
e5dd39fc 246#ifdef USE_REENTRANT_API
59bd0823 247 Perl_reentrant_init(aTHX);
e5dd39fc 248#endif
3d47000e
AB
249
250 /* Note that strtab is a rather special HV. Assumptions are made
251 about not iterating on it, and not adding tie magic to it.
252 It is properly deallocated in perl_destruct() */
253 PL_strtab = newHV();
254
3d47000e
AB
255 HvSHAREKEYS_off(PL_strtab); /* mandatory */
256 hv_ksplit(PL_strtab, 512);
257
0631ea03
AB
258#if defined(__DYNAMIC__) && (defined(NeXT) || defined(__NeXT__))
259 _dyld_lookup_and_bind
260 ("__environ", (unsigned long *) &environ_pointer, NULL);
261#endif /* environ */
262
263#ifdef USE_ENVIRON_ARRAY
264 PL_origenviron = environ;
265#endif
266
5311654c
JH
267 /* Use sysconf(_SC_CLK_TCK) if available, if not
268 * available or if the sysconf() fails, use the HZ. */
269#if defined(HAS_SYSCONF) && defined(_SC_CLK_TCK)
270 PL_clocktick = sysconf(_SC_CLK_TCK);
271 if (PL_clocktick <= 0)
272#endif
273 PL_clocktick = HZ;
274
081fc587
AB
275 PL_stashcache = newHV();
276
504f80c1
JH
277#if defined(USE_HASH_SEED) || defined(USE_HASH_SEED_EXPLICIT)
278 /* [perl #22371] Algorimic Complexity Attack on Perl 5.6.1, 5.8.0 */
279 {
d0d2ba8f
JH
280 char *s = NULL;
281
ed085813 282 if (!PL_earlytaint)
d0d2ba8f 283 s = PerlEnv_getenv("PERL_HASH_SEED");
504f80c1
JH
284 if (s)
285 while (isSPACE(*s)) s++;
286 if (s && isDIGIT(*s))
bf1e0190 287 PL_hash_seed = (UV)Atoul(s);
504f80c1
JH
288#ifndef USE_HASH_SEED_EXPLICIT
289 else {
290 /* Compute a random seed */
291 (void)seedDrand01((Rand_seed_t)seed());
292 PL_srand_called = TRUE;
293 PL_hash_seed = (UV)(Drand01() * (NV)UV_MAX);
294#if RANDBITS < (UVSIZE * 8)
295 {
296 int skip = (UVSIZE * 8) - RANDBITS;
297 PL_hash_seed >>= skip;
298 /* The low bits might need extra help. */
299 PL_hash_seed += (UV)(Drand01() * ((1 << skip) - 1));
300 }
301#endif /* RANDBITS < (UVSIZE * 8) */
302 }
303#endif /* USE_HASH_SEED_EXPLICIT */
ed085813 304 if (!PL_earlytaint && (s = PerlEnv_getenv("PERL_HASH_SEED_DEBUG")))
2191697e
JH
305 PerlIO_printf(Perl_debug_log, "HASH_SEED = %"UVuf"\n",
306 PL_hash_seed);
504f80c1
JH
307 }
308#endif /* #if defined(USE_HASH_SEED) || defined(USE_HASH_SEED_EXPLICIT) */
309
8990e307 310 ENTER;
79072805
LW
311}
312
954c1994 313/*
62375a60
NIS
314=for apidoc nothreadhook
315
316Stub that provides thread hook for perl_destruct when there are
317no threads.
318
319=cut
320*/
321
322int
4e9e3734 323Perl_nothreadhook(pTHX)
62375a60
NIS
324{
325 return 0;
326}
327
328/*
954c1994
GS
329=for apidoc perl_destruct
330
331Shuts down a Perl interpreter. See L<perlembed>.
332
333=cut
334*/
335
31d77e54 336int
0cb96387 337perl_destruct(pTHXx)
79072805 338{
7c474504 339 volatile int destruct_level; /* 0=none, 1=full, 2=full with checks */
a0d0e21e 340 HV *hv;
4d1ff10f 341#ifdef USE_5005THREADS
cea2e8a9 342 dTHX;
4d1ff10f 343#endif /* USE_5005THREADS */
8990e307 344
7766f137
GS
345 /* wait for all pseudo-forked children to finish */
346 PERL_WAIT_FOR_CHILDREN;
347
3280af22 348 destruct_level = PL_perl_destruct_level;
4633a7c4
LW
349#ifdef DEBUGGING
350 {
351 char *s;
155aba94 352 if ((s = PerlEnv_getenv("PERL_DESTRUCT_LEVEL"))) {
5f05dabc
PP
353 int i = atoi(s);
354 if (destruct_level < i)
355 destruct_level = i;
356 }
4633a7c4
LW
357 }
358#endif
359
31d77e54
AB
360
361 if(PL_exit_flags & PERL_EXIT_DESTRUCT_END) {
f3faeb53
AB
362 dJMPENV;
363 int x = 0;
364
365 JMPENV_PUSH(x);
366 if (PL_endav && !PL_minus_c)
367 call_list(PL_scopestack_ix, PL_endav);
368 JMPENV_POP;
26f423df 369 }
f3faeb53 370 LEAVE;
a0d0e21e
LW
371 FREETMPS;
372
e00b64d4 373 /* Need to flush since END blocks can produce output */
f13a2bc0 374 my_fflush_all();
e00b64d4 375
62375a60
NIS
376 if (CALL_FPTR(PL_threadhook)(aTHX)) {
377 /* Threads hook has vetoed further cleanup */
b47cad08 378 return STATUS_NATIVE_EXPORT;
62375a60
NIS
379 }
380
ff0cee69
PP
381 /* We must account for everything. */
382
383 /* Destroy the main CV and syntax tree */
3280af22 384 if (PL_main_root) {
3280af22
NIS
385 op_free(PL_main_root);
386 PL_main_root = Nullop;
a0d0e21e 387 }
3280af22
NIS
388 PL_curcop = &PL_compiling;
389 PL_main_start = Nullop;
390 SvREFCNT_dec(PL_main_cv);
391 PL_main_cv = Nullcv;
24d3c518 392 PL_dirty = TRUE;
ff0cee69 393
13621cfb
NIS
394 /* Tell PerlIO we are about to tear things apart in case
395 we have layers which are using resources that should
396 be cleaned up now.
397 */
398
399 PerlIO_destruct(aTHX);
400
3280af22 401 if (PL_sv_objcount) {
a0d0e21e
LW
402 /*
403 * Try to destruct global references. We do this first so that the
404 * destructors and destructees still exist. Some sv's might remain.
405 * Non-referenced objects are on their own.
406 */
a0d0e21e 407 sv_clean_objs();
8990e307
LW
408 }
409
5cd24f17 410 /* unhook hooks which will soon be, or use, destroyed data */
3280af22
NIS
411 SvREFCNT_dec(PL_warnhook);
412 PL_warnhook = Nullsv;
413 SvREFCNT_dec(PL_diehook);
414 PL_diehook = Nullsv;
5cd24f17 415
4b556e6c 416 /* call exit list functions */
3280af22 417 while (PL_exitlistlen-- > 0)
acfe0abc 418 PL_exitlist[PL_exitlistlen].fn(aTHX_ PL_exitlist[PL_exitlistlen].ptr);
4b556e6c 419
3280af22 420 Safefree(PL_exitlist);
4b556e6c 421
1c4916e5
CB
422 PL_exitlist = NULL;
423 PL_exitlistlen = 0;
424
a0d0e21e 425 if (destruct_level == 0){
8990e307 426
a0d0e21e 427 DEBUG_P(debprofdump());
ac27b0f5 428
56a2bab7
NIS
429#if defined(PERLIO_LAYERS)
430 /* No more IO - including error messages ! */
431 PerlIO_cleanup(aTHX);
432#endif
433
a0d0e21e 434 /* The exit() function will do everything that needs doing. */
b47cad08 435 return STATUS_NATIVE_EXPORT;
a0d0e21e 436 }
5dd60ef7 437
551a8b83 438 /* jettison our possibly duplicated environment */
4b647fb0
DM
439 /* if PERL_USE_SAFE_PUTENV is defined environ will not have been copied
440 * so we certainly shouldn't free it here
441 */
442#if defined(USE_ENVIRON_ARRAY) && !defined(PERL_USE_SAFE_PUTENV)
4efc5df6
GS
443 if (environ != PL_origenviron
444#ifdef USE_ITHREADS
445 /* only main thread can free environ[0] contents */
446 && PL_curinterp == aTHX
447#endif
448 )
449 {
551a8b83
JH
450 I32 i;
451
452 for (i = 0; environ[i]; i++)
4b420006 453 safesysfree(environ[i]);
0631ea03 454
4b420006
JH
455 /* Must use safesysfree() when working with environ. */
456 safesysfree(environ);
551a8b83
JH
457
458 environ = PL_origenviron;
459 }
460#endif
461
5f8cb046
DM
462#ifdef USE_ITHREADS
463 /* the syntax tree is shared between clones
464 * so op_free(PL_main_root) only ReREFCNT_dec's
465 * REGEXPs in the parent interpreter
466 * we need to manually ReREFCNT_dec for the clones
467 */
468 {
469 I32 i = AvFILLp(PL_regex_padav) + 1;
470 SV **ary = AvARRAY(PL_regex_padav);
471
472 while (i) {
35061a7e 473 SV *resv = ary[--i];
ba89bb6e 474 REGEXP *re = INT2PTR(REGEXP *,SvIVX(resv));
35061a7e
DM
475
476 if (SvFLAGS(resv) & SVf_BREAK) {
577e12cc 477 /* this is PL_reg_curpm, already freed
35061a7e
DM
478 * flag is set in regexec.c:S_regtry
479 */
480 SvFLAGS(resv) &= ~SVf_BREAK;
3a1ee7e8 481 }
1cc8b4c5
AB
482 else if(SvREPADTMP(resv)) {
483 SvREPADTMP_off(resv);
484 }
35061a7e 485 else {
5f8cb046
DM
486 ReREFCNT_dec(re);
487 }
488 }
489 }
490 SvREFCNT_dec(PL_regex_padav);
491 PL_regex_padav = Nullav;
492 PL_regex_pad = NULL;
493#endif
494
081fc587
AB
495 SvREFCNT_dec((SV*) PL_stashcache);
496 PL_stashcache = NULL;
497
5f05dabc
PP
498 /* loosen bonds of global variables */
499
3280af22
NIS
500 if(PL_rsfp) {
501 (void)PerlIO_close(PL_rsfp);
502 PL_rsfp = Nullfp;
8ebc5c01
PP
503 }
504
505 /* Filters for program text */
3280af22
NIS
506 SvREFCNT_dec(PL_rsfp_filters);
507 PL_rsfp_filters = Nullav;
8ebc5c01
PP
508
509 /* switches */
3280af22
NIS
510 PL_preprocess = FALSE;
511 PL_minus_n = FALSE;
512 PL_minus_p = FALSE;
513 PL_minus_l = FALSE;
514 PL_minus_a = FALSE;
515 PL_minus_F = FALSE;
516 PL_doswitches = FALSE;
599cee73 517 PL_dowarn = G_WARN_OFF;
3280af22
NIS
518 PL_doextract = FALSE;
519 PL_sawampersand = FALSE; /* must save all match strings */
3280af22
NIS
520 PL_unsafe = FALSE;
521
522 Safefree(PL_inplace);
523 PL_inplace = Nullch;
a7cb1f99 524 SvREFCNT_dec(PL_patchlevel);
3280af22
NIS
525
526 if (PL_e_script) {
527 SvREFCNT_dec(PL_e_script);
528 PL_e_script = Nullsv;
8ebc5c01
PP
529 }
530
531 /* magical thingies */
532
7889fe52
NIS
533 SvREFCNT_dec(PL_ofs_sv); /* $, */
534 PL_ofs_sv = Nullsv;
5f05dabc 535
7889fe52
NIS
536 SvREFCNT_dec(PL_ors_sv); /* $\ */
537 PL_ors_sv = Nullsv;
8ebc5c01 538
3280af22
NIS
539 SvREFCNT_dec(PL_rs); /* $/ */
540 PL_rs = Nullsv;
dc92893f 541
d33b2eba
GS
542 PL_multiline = 0; /* $* */
543 Safefree(PL_osname); /* $^O */
544 PL_osname = Nullch;
5f05dabc 545
3280af22
NIS
546 SvREFCNT_dec(PL_statname);
547 PL_statname = Nullsv;
548 PL_statgv = Nullgv;
5f05dabc 549
8ebc5c01
PP
550 /* defgv, aka *_ should be taken care of elsewhere */
551
8ebc5c01 552 /* clean up after study() */
3280af22
NIS
553 SvREFCNT_dec(PL_lastscream);
554 PL_lastscream = Nullsv;
555 Safefree(PL_screamfirst);
556 PL_screamfirst = 0;
557 Safefree(PL_screamnext);
558 PL_screamnext = 0;
8ebc5c01 559
7d5ea4e7
GS
560 /* float buffer */
561 Safefree(PL_efloatbuf);
562 PL_efloatbuf = Nullch;
563 PL_efloatsize = 0;
564
8ebc5c01 565 /* startup and shutdown function lists */
3280af22 566 SvREFCNT_dec(PL_beginav);
5a837c8f 567 SvREFCNT_dec(PL_beginav_save);
3280af22 568 SvREFCNT_dec(PL_endav);
7d30b5c4 569 SvREFCNT_dec(PL_checkav);
ece599bd 570 SvREFCNT_dec(PL_checkav_save);
3280af22
NIS
571 SvREFCNT_dec(PL_initav);
572 PL_beginav = Nullav;
5a837c8f 573 PL_beginav_save = Nullav;
3280af22 574 PL_endav = Nullav;
7d30b5c4 575 PL_checkav = Nullav;
ece599bd 576 PL_checkav_save = Nullav;
3280af22 577 PL_initav = Nullav;
5618dfe8 578
8ebc5c01 579 /* shortcuts just get cleared */
3280af22 580 PL_envgv = Nullgv;
3280af22
NIS
581 PL_incgv = Nullgv;
582 PL_hintgv = Nullgv;
583 PL_errgv = Nullgv;
584 PL_argvgv = Nullgv;
585 PL_argvoutgv = Nullgv;
586 PL_stdingv = Nullgv;
bf49b057 587 PL_stderrgv = Nullgv;
3280af22
NIS
588 PL_last_in_gv = Nullgv;
589 PL_replgv = Nullgv;
5c831c24 590 PL_debstash = Nullhv;
8ebc5c01
PP
591
592 /* reset so print() ends up where we expect */
593 setdefout(Nullgv);
5c831c24 594
7a1c5554
GS
595 SvREFCNT_dec(PL_argvout_stack);
596 PL_argvout_stack = Nullav;
8ebc5c01 597
5c831c24
GS
598 SvREFCNT_dec(PL_modglobal);
599 PL_modglobal = Nullhv;
600 SvREFCNT_dec(PL_preambleav);
601 PL_preambleav = Nullav;
602 SvREFCNT_dec(PL_subname);
603 PL_subname = Nullsv;
604 SvREFCNT_dec(PL_linestr);
605 PL_linestr = Nullsv;
606 SvREFCNT_dec(PL_pidstatus);
607 PL_pidstatus = Nullhv;
608 SvREFCNT_dec(PL_toptarget);
609 PL_toptarget = Nullsv;
610 SvREFCNT_dec(PL_bodytarget);
611 PL_bodytarget = Nullsv;
612 PL_formtarget = Nullsv;
613
d33b2eba 614 /* free locale stuff */
b9582b6a 615#ifdef USE_LOCALE_COLLATE
d33b2eba
GS
616 Safefree(PL_collation_name);
617 PL_collation_name = Nullch;
b9582b6a 618#endif
d33b2eba 619
b9582b6a 620#ifdef USE_LOCALE_NUMERIC
d33b2eba
GS
621 Safefree(PL_numeric_name);
622 PL_numeric_name = Nullch;
a453c169 623 SvREFCNT_dec(PL_numeric_radix_sv);
b9582b6a 624#endif
d33b2eba 625
5c831c24
GS
626 /* clear utf8 character classes */
627 SvREFCNT_dec(PL_utf8_alnum);
628 SvREFCNT_dec(PL_utf8_alnumc);
629 SvREFCNT_dec(PL_utf8_ascii);
630 SvREFCNT_dec(PL_utf8_alpha);
631 SvREFCNT_dec(PL_utf8_space);
632 SvREFCNT_dec(PL_utf8_cntrl);
633 SvREFCNT_dec(PL_utf8_graph);
634 SvREFCNT_dec(PL_utf8_digit);
635 SvREFCNT_dec(PL_utf8_upper);
636 SvREFCNT_dec(PL_utf8_lower);
637 SvREFCNT_dec(PL_utf8_print);
638 SvREFCNT_dec(PL_utf8_punct);
639 SvREFCNT_dec(PL_utf8_xdigit);
640 SvREFCNT_dec(PL_utf8_mark);
641 SvREFCNT_dec(PL_utf8_toupper);
4dbdbdc2 642 SvREFCNT_dec(PL_utf8_totitle);
5c831c24 643 SvREFCNT_dec(PL_utf8_tolower);
b4e400f9 644 SvREFCNT_dec(PL_utf8_tofold);
82686b01
JH
645 SvREFCNT_dec(PL_utf8_idstart);
646 SvREFCNT_dec(PL_utf8_idcont);
5c831c24
GS
647 PL_utf8_alnum = Nullsv;
648 PL_utf8_alnumc = Nullsv;
649 PL_utf8_ascii = Nullsv;
650 PL_utf8_alpha = Nullsv;
651 PL_utf8_space = Nullsv;
652 PL_utf8_cntrl = Nullsv;
653 PL_utf8_graph = Nullsv;
654 PL_utf8_digit = Nullsv;
655 PL_utf8_upper = Nullsv;
656 PL_utf8_lower = Nullsv;
657 PL_utf8_print = Nullsv;
658 PL_utf8_punct = Nullsv;
659 PL_utf8_xdigit = Nullsv;
660 PL_utf8_mark = Nullsv;
661 PL_utf8_toupper = Nullsv;
662 PL_utf8_totitle = Nullsv;
663 PL_utf8_tolower = Nullsv;
b4e400f9 664 PL_utf8_tofold = Nullsv;
82686b01
JH
665 PL_utf8_idstart = Nullsv;
666 PL_utf8_idcont = Nullsv;
5c831c24 667
971a9dd3
GS
668 if (!specialWARN(PL_compiling.cop_warnings))
669 SvREFCNT_dec(PL_compiling.cop_warnings);
5c831c24 670 PL_compiling.cop_warnings = Nullsv;
ac27b0f5
NIS
671 if (!specialCopIO(PL_compiling.cop_io))
672 SvREFCNT_dec(PL_compiling.cop_io);
673 PL_compiling.cop_io = Nullsv;
05ec9bb3
NIS
674 CopFILE_free(&PL_compiling);
675 CopSTASH_free(&PL_compiling);
5c831c24 676
a0d0e21e 677 /* Prepare to destruct main symbol table. */
5f05dabc 678
3280af22
NIS
679 hv = PL_defstash;
680 PL_defstash = 0;
a0d0e21e 681 SvREFCNT_dec(hv);
5c831c24
GS
682 SvREFCNT_dec(PL_curstname);
683 PL_curstname = Nullsv;
a0d0e21e 684
5a844595
GS
685 /* clear queued errors */
686 SvREFCNT_dec(PL_errors);
687 PL_errors = Nullsv;
688
a0d0e21e 689 FREETMPS;
0453d815 690 if (destruct_level >= 2 && ckWARN_d(WARN_INTERNAL)) {
3280af22 691 if (PL_scopestack_ix != 0)
9014280d 692 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
0453d815 693 "Unbalanced scopes: %ld more ENTERs than LEAVEs\n",
3280af22
NIS
694 (long)PL_scopestack_ix);
695 if (PL_savestack_ix != 0)
9014280d 696 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
0453d815 697 "Unbalanced saves: %ld more saves than restores\n",
3280af22
NIS
698 (long)PL_savestack_ix);
699 if (PL_tmps_floor != -1)
9014280d 700 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),"Unbalanced tmps: %ld more allocs than frees\n",
3280af22 701 (long)PL_tmps_floor + 1);
a0d0e21e 702 if (cxstack_ix != -1)
9014280d 703 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),"Unbalanced context: %ld more PUSHes than POPs\n",
ff0cee69 704 (long)cxstack_ix + 1);
a0d0e21e 705 }
8990e307
LW
706
707 /* Now absolutely destruct everything, somehow or other, loops or no. */
d33b2eba 708 SvFLAGS(PL_fdpid) |= SVTYPEMASK; /* don't clean out pid table now */
3280af22 709 SvFLAGS(PL_strtab) |= SVTYPEMASK; /* don't clean out strtab now */
5226ed68
JH
710
711 /* the 2 is for PL_fdpid and PL_strtab */
712 while (PL_sv_count > 2 && sv_clean_all())
713 ;
714
d33b2eba
GS
715 SvFLAGS(PL_fdpid) &= ~SVTYPEMASK;
716 SvFLAGS(PL_fdpid) |= SVt_PVAV;
3280af22
NIS
717 SvFLAGS(PL_strtab) &= ~SVTYPEMASK;
718 SvFLAGS(PL_strtab) |= SVt_PVHV;
d33b2eba 719
d4777f27
GS
720 AvREAL_off(PL_fdpid); /* no surviving entries */
721 SvREFCNT_dec(PL_fdpid); /* needed in io_close() */
d33b2eba
GS
722 PL_fdpid = Nullav;
723
6c644e78
GS
724#ifdef HAVE_INTERP_INTERN
725 sys_intern_clear();
726#endif
727
6e72f9df
PP
728 /* Destruct the global string table. */
729 {
730 /* Yell and reset the HeVAL() slots that are still holding refcounts,
731 * so that sv_free() won't fail on them.
732 */
733 I32 riter;
734 I32 max;
735 HE *hent;
736 HE **array;
737
738 riter = 0;
3280af22
NIS
739 max = HvMAX(PL_strtab);
740 array = HvARRAY(PL_strtab);
6e72f9df
PP
741 hent = array[0];
742 for (;;) {
0453d815 743 if (hent && ckWARN_d(WARN_INTERNAL)) {
9014280d 744 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
0453d815 745 "Unbalanced string table refcount: (%d) for \"%s\"",
6e72f9df
PP
746 HeVAL(hent) - Nullsv, HeKEY(hent));
747 HeVAL(hent) = Nullsv;
748 hent = HeNEXT(hent);
749 }
750 if (!hent) {
751 if (++riter > max)
752 break;
753 hent = array[riter];
754 }
755 }
756 }
3280af22 757 SvREFCNT_dec(PL_strtab);
6e72f9df 758
e652bb2f 759#ifdef USE_ITHREADS
a0739874
DM
760 /* free the pointer table used for cloning */
761 ptr_table_free(PL_ptr_table);
53186e96 762#endif
a0739874 763
d33b2eba
GS
764 /* free special SVs */
765
766 SvREFCNT(&PL_sv_yes) = 0;
767 sv_clear(&PL_sv_yes);
768 SvANY(&PL_sv_yes) = NULL;
4c5e2b0d 769 SvFLAGS(&PL_sv_yes) = 0;
d33b2eba
GS
770
771 SvREFCNT(&PL_sv_no) = 0;
772 sv_clear(&PL_sv_no);
773 SvANY(&PL_sv_no) = NULL;
4c5e2b0d 774 SvFLAGS(&PL_sv_no) = 0;
01724ea0 775
9f375a43
DM
776 {
777 int i;
778 for (i=0; i<=2; i++) {
779 SvREFCNT(PERL_DEBUG_PAD(i)) = 0;
780 sv_clear(PERL_DEBUG_PAD(i));
781 SvANY(PERL_DEBUG_PAD(i)) = NULL;
782 SvFLAGS(PERL_DEBUG_PAD(i)) = 0;
783 }
784 }
785
0453d815 786 if (PL_sv_count != 0 && ckWARN_d(WARN_INTERNAL))
9014280d 787 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),"Scalars leaked: %ld\n", (long)PL_sv_count);
6e72f9df 788
eba0f806
DM
789#ifdef DEBUG_LEAKING_SCALARS
790 if (PL_sv_count != 0) {
791 SV* sva;
792 SV* sv;
793 register SV* svend;
794
795 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
796 svend = &sva[SvREFCNT(sva)];
797 for (sv = sva + 1; sv < svend; ++sv) {
798 if (SvTYPE(sv) != SVTYPEMASK) {
799 PerlIO_printf(Perl_debug_log, "leaked: 0x%p\n", sv);
800 }
801 }
802 }
803 }
804#endif
805
806
56a2bab7 807#if defined(PERLIO_LAYERS)
3a1ee7e8
NIS
808 /* No more IO - including error messages ! */
809 PerlIO_cleanup(aTHX);
810#endif
811
9f4bd222
NIS
812 /* sv_undef needs to stay immortal until after PerlIO_cleanup
813 as currently layers use it rather than Nullsv as a marker
814 for no arg - and will try and SvREFCNT_dec it.
815 */
816 SvREFCNT(&PL_sv_undef) = 0;
817 SvREADONLY_off(&PL_sv_undef);
818
3280af22 819 Safefree(PL_origfilename);
3280af22 820 Safefree(PL_reg_start_tmp);
5c5e4c24
IZ
821 if (PL_reg_curpm)
822 Safefree(PL_reg_curpm);
82ba1be6 823 Safefree(PL_reg_poscache);
dd28f7bb 824 free_tied_hv_pool();
3280af22 825 Safefree(PL_op_mask);
cf36064f
GS
826 Safefree(PL_psig_ptr);
827 Safefree(PL_psig_name);
2c2666fc 828 Safefree(PL_bitcount);
ce08f86c 829 Safefree(PL_psig_pend);
6e72f9df 830 nuke_stacks();
3280af22 831 PL_hints = 0; /* Reset hints. Should hints be per-interpreter ? */
ac27b0f5 832
a0d0e21e 833 DEBUG_P(debprofdump());
d33b2eba 834
e5dd39fc 835#ifdef USE_REENTRANT_API
10bc17b6 836 Perl_reentrant_free(aTHX);
e5dd39fc
AB
837#endif
838
612f20c3
GS
839 sv_free_arenas();
840
fc36a67e
PP
841 /* As the absolutely last thing, free the non-arena SV for mess() */
842
3280af22 843 if (PL_mess_sv) {
9c63abab
GS
844 /* it could have accumulated taint magic */
845 if (SvTYPE(PL_mess_sv) >= SVt_PVMG) {
846 MAGIC* mg;
847 MAGIC* moremagic;
848 for (mg = SvMAGIC(PL_mess_sv); mg; mg = moremagic) {
849 moremagic = mg->mg_moremagic;
14befaf4
DM
850 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global
851 && mg->mg_len >= 0)
9c63abab
GS
852 Safefree(mg->mg_ptr);
853 Safefree(mg);
854 }
855 }
fc36a67e 856 /* we know that type >= SVt_PV */
155aba94 857 (void)SvOOK_off(PL_mess_sv);
3280af22
NIS
858 Safefree(SvPVX(PL_mess_sv));
859 Safefree(SvANY(PL_mess_sv));
860 Safefree(PL_mess_sv);
861 PL_mess_sv = Nullsv;
fc36a67e 862 }
31d77e54 863 return STATUS_NATIVE_EXPORT;
79072805
LW
864}
865
954c1994
GS
866/*
867=for apidoc perl_free
868
869Releases a Perl interpreter. See L<perlembed>.
870
871=cut
872*/
873
79072805 874void
0cb96387 875perl_free(pTHXx)
79072805 876{
acfe0abc 877#if defined(WIN32) || defined(NETWARE)
ce3e5b80 878# if defined(PERL_IMPLICIT_SYS)
acfe0abc
GS
879# ifdef NETWARE
880 void *host = nw_internal_host;
881# else
882 void *host = w32_internal_host;
883# endif
ce3e5b80 884 PerlMem_free(aTHXx);
acfe0abc 885# ifdef NETWARE
011f1a1a 886 nw_delete_internal_host(host);
acfe0abc
GS
887# else
888 win32_delete_internal_host(host);
889# endif
1c0ca838
GS
890# else
891 PerlMem_free(aTHXx);
892# endif
acfe0abc
GS
893#else
894 PerlMem_free(aTHXx);
76e3520e 895#endif
79072805
LW
896}
897
4b556e6c 898void
864dbfa3 899Perl_call_atexit(pTHX_ ATEXIT_t fn, void *ptr)
4b556e6c 900{
3280af22
NIS
901 Renew(PL_exitlist, PL_exitlistlen+1, PerlExitListEntry);
902 PL_exitlist[PL_exitlistlen].fn = fn;
903 PL_exitlist[PL_exitlistlen].ptr = ptr;
904 ++PL_exitlistlen;
4b556e6c
JD
905}
906
954c1994
GS
907/*
908=for apidoc perl_parse
909
910Tells a Perl interpreter to parse a Perl script. See L<perlembed>.
911
912=cut
913*/
914
79072805 915int
0cb96387 916perl_parse(pTHXx_ XSINIT_t xsinit, int argc, char **argv, char **env)
8d063cd8 917{
6224f72b 918 I32 oldscope;
6224f72b 919 int ret;
db36c5a1 920 dJMPENV;
4d1ff10f 921#ifdef USE_5005THREADS
cea2e8a9
GS
922 dTHX;
923#endif
8d063cd8 924
a687059c
LW
925#ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
926#ifdef IAMSUID
927#undef IAMSUID
cea2e8a9 928 Perl_croak(aTHX_ "suidperl is no longer needed since the kernel can now execute\n\
a687059c
LW
929setuid perl scripts securely.\n");
930#endif
931#endif
932
3280af22 933 PL_origargc = argc;
e2975953 934 PL_origargv = argv;
a0d0e21e 935
54bfe034
JH
936 {
937 char *s = PL_origargv[0];
938 int i;
939
940 s += strlen(s);
941 /* See if all the arguments are contiguous in memory */
942 for (i = 1; i < PL_origargc; i++) {
943 if (PL_origargv[i] == s + 1
944#ifdef OS2
945 || PL_origargv[i] == s + 2
946#endif
947 )
948 {
949 ++s;
950 s += strlen(s); /* this one is ok too */
951 }
952 else
953 break;
954 }
955 /* Can we grab env area too to be used as the area for $0
956 * (in case we later modify it)? */
957 if (PL_origenviron
958 && (PL_origenviron[0] == s + 1))
959 {
960 my_setenv("NoNe SuCh", Nullch);
961 /* force copy of environment */
962 for (i = 0; PL_origenviron[i]; i++)
963 if (PL_origenviron[i] == s + 1) {
964 ++s;
965 s += strlen(s);
966 }
967 else
968 break;
969 }
970 PL_origalen = s - PL_origargv[0];
971 }
972
3280af22 973 if (PL_do_undump) {
a0d0e21e
LW
974
975 /* Come here if running an undumped a.out. */
976
3280af22
NIS
977 PL_origfilename = savepv(argv[0]);
978 PL_do_undump = FALSE;
a0d0e21e 979 cxstack_ix = -1; /* start label stack again */
748a9306 980 init_ids();
a0d0e21e
LW
981 init_postdump_symbols(argc,argv,env);
982 return 0;
983 }
984
3280af22 985 if (PL_main_root) {
3280af22
NIS
986 op_free(PL_main_root);
987 PL_main_root = Nullop;
ff0cee69 988 }
3280af22
NIS
989 PL_main_start = Nullop;
990 SvREFCNT_dec(PL_main_cv);
991 PL_main_cv = Nullcv;
79072805 992
3280af22
NIS
993 time(&PL_basetime);
994 oldscope = PL_scopestack_ix;
599cee73 995 PL_dowarn = G_WARN_OFF;
f86702cc 996
14dd3ad8
GS
997#ifdef PERL_FLEXIBLE_EXCEPTIONS
998 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vparse_body), env, xsinit);
999#else
1000 JMPENV_PUSH(ret);
1001#endif
6224f72b 1002 switch (ret) {
312caa8e 1003 case 0:
14dd3ad8
GS
1004#ifndef PERL_FLEXIBLE_EXCEPTIONS
1005 parse_body(env,xsinit);
1006#endif
7d30b5c4
GS
1007 if (PL_checkav)
1008 call_list(oldscope, PL_checkav);
14dd3ad8
GS
1009 ret = 0;
1010 break;
6224f72b
GS
1011 case 1:
1012 STATUS_ALL_FAILURE;
1013 /* FALL THROUGH */
1014 case 2:
1015 /* my_exit() was called */
3280af22 1016 while (PL_scopestack_ix > oldscope)
6224f72b
GS
1017 LEAVE;
1018 FREETMPS;
3280af22 1019 PL_curstash = PL_defstash;
7d30b5c4
GS
1020 if (PL_checkav)
1021 call_list(oldscope, PL_checkav);
14dd3ad8
GS
1022 ret = STATUS_NATIVE_EXPORT;
1023 break;
6224f72b 1024 case 3:
bf49b057 1025 PerlIO_printf(Perl_error_log, "panic: top_env\n");
14dd3ad8
GS
1026 ret = 1;
1027 break;
6224f72b 1028 }
14dd3ad8
GS
1029 JMPENV_POP;
1030 return ret;
1031}
1032
1033#ifdef PERL_FLEXIBLE_EXCEPTIONS
1034STATIC void *
1035S_vparse_body(pTHX_ va_list args)
1036{
1037 char **env = va_arg(args, char**);
1038 XSINIT_t xsinit = va_arg(args, XSINIT_t);
1039
1040 return parse_body(env, xsinit);
312caa8e 1041}
14dd3ad8 1042#endif
312caa8e
CS
1043
1044STATIC void *
14dd3ad8 1045S_parse_body(pTHX_ char **env, XSINIT_t xsinit)
312caa8e 1046{
312caa8e
CS
1047 int argc = PL_origargc;
1048 char **argv = PL_origargv;
312caa8e
CS
1049 char *scriptname = NULL;
1050 int fdscript = -1;
1051 VOL bool dosearch = FALSE;
1052 char *validarg = "";
312caa8e
CS
1053 register SV *sv;
1054 register char *s;
cf756827 1055 char *cddir = Nullch;
312caa8e 1056
3280af22 1057 sv_setpvn(PL_linestr,"",0);
79cb57f6 1058 sv = newSVpvn("",0); /* first used for -I flags */
6224f72b
GS
1059 SAVEFREESV(sv);
1060 init_main_stash();
54310121 1061
6224f72b
GS
1062 for (argc--,argv++; argc > 0; argc--,argv++) {
1063 if (argv[0][0] != '-' || !argv[0][1])
1064 break;
1065#ifdef DOSUID
1066 if (*validarg)
1067 validarg = " PHOOEY ";
1068 else
1069 validarg = argv[0];
13281fa4 1070#endif
6224f72b
GS
1071 s = argv[0]+1;
1072 reswitch:
1073 switch (*s) {
729a02f2 1074 case 'C':
1d5472a9
GS
1075#ifndef PERL_STRICT_CR
1076 case '\r':
1077#endif
6224f72b
GS
1078 case ' ':
1079 case '0':
1080 case 'F':
1081 case 'a':
1082 case 'c':
1083 case 'd':
1084 case 'D':
1085 case 'h':
1086 case 'i':
1087 case 'l':
1088 case 'M':
1089 case 'm':
1090 case 'n':
1091 case 'p':
1092 case 's':
1093 case 'u':
1094 case 'U':
1095 case 'v':
599cee73
PM
1096 case 'W':
1097 case 'X':
6224f72b 1098 case 'w':
06492da6 1099 case 'A':
155aba94 1100 if ((s = moreswitches(s)))
6224f72b
GS
1101 goto reswitch;
1102 break;
33b78306 1103
1dbad523 1104 case 't':
22f7c9c9 1105 CHECK_MALLOC_TOO_LATE_FOR('t');
317ea90d
MS
1106 if( !PL_tainting ) {
1107 PL_taint_warn = TRUE;
1108 PL_tainting = TRUE;
1109 }
1110 s++;
1111 goto reswitch;
6224f72b 1112 case 'T':
22f7c9c9 1113 CHECK_MALLOC_TOO_LATE_FOR('T');
3280af22 1114 PL_tainting = TRUE;
317ea90d 1115 PL_taint_warn = FALSE;
6224f72b
GS
1116 s++;
1117 goto reswitch;
f86702cc 1118
6224f72b 1119 case 'e':
bf4acbe4
GS
1120#ifdef MACOS_TRADITIONAL
1121 /* ignore -e for Dev:Pseudo argument */
1122 if (argv[1] && !strcmp(argv[1], "Dev:Pseudo"))
e55ac0fa 1123 break;
bf4acbe4 1124#endif
3280af22 1125 if (PL_euid != PL_uid || PL_egid != PL_gid)
cea2e8a9 1126 Perl_croak(aTHX_ "No -e allowed in setuid scripts");
3280af22 1127 if (!PL_e_script) {
79cb57f6 1128 PL_e_script = newSVpvn("",0);
0cb96387 1129 filter_add(read_e_script, NULL);
6224f72b
GS
1130 }
1131 if (*++s)
3280af22 1132 sv_catpv(PL_e_script, s);
6224f72b 1133 else if (argv[1]) {
3280af22 1134 sv_catpv(PL_e_script, argv[1]);
6224f72b
GS
1135 argc--,argv++;
1136 }
1137 else
cea2e8a9 1138 Perl_croak(aTHX_ "No code specified for -e");
3280af22 1139 sv_catpv(PL_e_script, "\n");
6224f72b 1140 break;
afe37c7d 1141
6224f72b
GS
1142 case 'I': /* -I handled both here and in moreswitches() */
1143 forbid_setid("-I");
1144 if (!*++s && (s=argv[1]) != Nullch) {
1145 argc--,argv++;
1146 }
6224f72b 1147 if (s && *s) {
0df16ed7
GS
1148 char *p;
1149 STRLEN len = strlen(s);
1150 p = savepvn(s, len);
574c798a 1151 incpush(p, TRUE, TRUE, FALSE);
0df16ed7
GS
1152 sv_catpvn(sv, "-I", 2);
1153 sv_catpvn(sv, p, len);
1154 sv_catpvn(sv, " ", 1);
6224f72b 1155 Safefree(p);
0df16ed7
GS
1156 }
1157 else
a67e862a 1158 Perl_croak(aTHX_ "No directory specified for -I");
6224f72b
GS
1159 break;
1160 case 'P':
1161 forbid_setid("-P");
3280af22 1162 PL_preprocess = TRUE;
6224f72b
GS
1163 s++;
1164 goto reswitch;
1165 case 'S':
1166 forbid_setid("-S");
1167 dosearch = TRUE;
1168 s++;
1169 goto reswitch;
1170 case 'V':
3280af22
NIS
1171 if (!PL_preambleav)
1172 PL_preambleav = newAV();
1173 av_push(PL_preambleav, newSVpv("use Config qw(myconfig config_vars)",0));
6224f72b 1174 if (*++s != ':') {
3280af22 1175 PL_Sv = newSVpv("print myconfig();",0);
6224f72b 1176#ifdef VMS
6b88bc9c 1177 sv_catpv(PL_Sv,"print \"\\nCharacteristics of this PERLSHR image: \\n\",");
6224f72b 1178#else
3280af22 1179 sv_catpv(PL_Sv,"print \"\\nCharacteristics of this binary (from libperl): \\n\",");
6224f72b 1180#endif
3280af22 1181 sv_catpv(PL_Sv,"\" Compile-time options:");
6224f72b 1182# ifdef DEBUGGING
3280af22 1183 sv_catpv(PL_Sv," DEBUGGING");
6224f72b 1184# endif
6224f72b 1185# ifdef MULTIPLICITY
8f872242 1186 sv_catpv(PL_Sv," MULTIPLICITY");
6224f72b 1187# endif
4d1ff10f
AB
1188# ifdef USE_5005THREADS
1189 sv_catpv(PL_Sv," USE_5005THREADS");
b363f7ed 1190# endif
ac5e8965
JH
1191# ifdef USE_ITHREADS
1192 sv_catpv(PL_Sv," USE_ITHREADS");
1193# endif
10cc9d2a
JH
1194# ifdef USE_64_BIT_INT
1195 sv_catpv(PL_Sv," USE_64_BIT_INT");
1196# endif
1197# ifdef USE_64_BIT_ALL
1198 sv_catpv(PL_Sv," USE_64_BIT_ALL");
ac5e8965
JH
1199# endif
1200# ifdef USE_LONG_DOUBLE
1201 sv_catpv(PL_Sv," USE_LONG_DOUBLE");
1202# endif
53430762
JH
1203# ifdef USE_LARGE_FILES
1204 sv_catpv(PL_Sv," USE_LARGE_FILES");
1205# endif
ac5e8965
JH
1206# ifdef USE_SOCKS
1207 sv_catpv(PL_Sv," USE_SOCKS");
1208# endif
b363f7ed
GS
1209# ifdef PERL_IMPLICIT_CONTEXT
1210 sv_catpv(PL_Sv," PERL_IMPLICIT_CONTEXT");
1211# endif
1212# ifdef PERL_IMPLICIT_SYS
1213 sv_catpv(PL_Sv," PERL_IMPLICIT_SYS");
1214# endif
3280af22 1215 sv_catpv(PL_Sv,"\\n\",");
b363f7ed 1216
6224f72b
GS
1217#if defined(LOCAL_PATCH_COUNT)
1218 if (LOCAL_PATCH_COUNT > 0) {
1219 int i;
3280af22 1220 sv_catpv(PL_Sv,"\" Locally applied patches:\\n\",");
6224f72b 1221 for (i = 1; i <= LOCAL_PATCH_COUNT; i++) {
3280af22 1222 if (PL_localpatches[i])
cea2e8a9 1223 Perl_sv_catpvf(aTHX_ PL_Sv,"q\" \t%s\n\",",PL_localpatches[i]);
6224f72b
GS
1224 }
1225 }
1226#endif
cea2e8a9 1227 Perl_sv_catpvf(aTHX_ PL_Sv,"\" Built under %s\\n\"",OSNAME);
6224f72b
GS
1228#ifdef __DATE__
1229# ifdef __TIME__
cea2e8a9 1230 Perl_sv_catpvf(aTHX_ PL_Sv,",\" Compiled at %s %s\\n\"",__DATE__,__TIME__);
6224f72b 1231# else
cea2e8a9 1232 Perl_sv_catpvf(aTHX_ PL_Sv,",\" Compiled on %s\\n\"",__DATE__);
6224f72b
GS
1233# endif
1234#endif
3280af22 1235 sv_catpv(PL_Sv, "; \
6224f72b 1236$\"=\"\\n \"; \
69fcd688
JH
1237@env = map { \"$_=\\\"$ENV{$_}\\\"\" } sort grep {/^PERL/} keys %ENV; ");
1238#ifdef __CYGWIN__
1239 sv_catpv(PL_Sv,"\
1240push @env, \"CYGWIN=\\\"$ENV{CYGWIN}\\\"\";");
1241#endif
1242 sv_catpv(PL_Sv, "\
6224f72b
GS
1243print \" \\%ENV:\\n @env\\n\" if @env; \
1244print \" \\@INC:\\n @INC\\n\";");
1245 }
1246 else {
3280af22
NIS
1247 PL_Sv = newSVpv("config_vars(qw(",0);
1248 sv_catpv(PL_Sv, ++s);
1249 sv_catpv(PL_Sv, "))");
6224f72b
GS
1250 s += strlen(s);
1251 }
3280af22 1252 av_push(PL_preambleav, PL_Sv);
6224f72b
GS
1253 scriptname = BIT_BUCKET; /* don't look for script or read stdin */
1254 goto reswitch;
1255 case 'x':
3280af22 1256 PL_doextract = TRUE;
6224f72b
GS
1257 s++;
1258 if (*s)
f4c556ac 1259 cddir = s;
6224f72b
GS
1260 break;
1261 case 0:
1262 break;
1263 case '-':
1264 if (!*++s || isSPACE(*s)) {
1265 argc--,argv++;
1266 goto switch_end;
1267 }
1268 /* catch use of gnu style long options */
1269 if (strEQ(s, "version")) {
1270 s = "v";
1271 goto reswitch;
1272 }
1273 if (strEQ(s, "help")) {
1274 s = "h";
1275 goto reswitch;
1276 }
1277 s--;
1278 /* FALL THROUGH */
1279 default:
cea2e8a9 1280 Perl_croak(aTHX_ "Unrecognized switch: -%s (-h will show valid options)",s);
8d063cd8
LW
1281 }
1282 }
6224f72b 1283 switch_end:
7f9e821f 1284 sv_setsv(get_sv("/", TRUE), PL_rs);
54310121 1285
f675dbe5
CB
1286 if (
1287#ifndef SECURE_INTERNAL_GETENV
1288 !PL_tainting &&
1289#endif
cf756827 1290 (s = PerlEnv_getenv("PERL5OPT")))
0df16ed7 1291 {
cf756827 1292 char *popt = s;
74288ac8
GS
1293 while (isSPACE(*s))
1294 s++;
317ea90d 1295 if (*s == '-' && *(s+1) == 'T') {
22f7c9c9 1296 CHECK_MALLOC_TOO_LATE_FOR('T');
74288ac8 1297 PL_tainting = TRUE;
317ea90d
MS
1298 PL_taint_warn = FALSE;
1299 }
74288ac8 1300 else {
cf756827 1301 char *popt_copy = Nullch;
74288ac8 1302 while (s && *s) {
4ea8f8fb 1303 char *d;
74288ac8
GS
1304 while (isSPACE(*s))
1305 s++;
1306 if (*s == '-') {
1307 s++;
1308 if (isSPACE(*s))
1309 continue;
1310 }
4ea8f8fb 1311 d = s;
74288ac8
GS
1312 if (!*s)
1313 break;
06492da6 1314 if (!strchr("DIMUdmtwA", *s))
cea2e8a9 1315 Perl_croak(aTHX_ "Illegal switch in PERL5OPT: -%c", *s);
4ea8f8fb
MS
1316 while (++s && *s) {
1317 if (isSPACE(*s)) {
cf756827
GS
1318 if (!popt_copy) {
1319 popt_copy = SvPVX(sv_2mortal(newSVpv(popt,0)));
1320 s = popt_copy + (s - popt);
1321 d = popt_copy + (d - popt);
1322 }
4ea8f8fb
MS
1323 *s++ = '\0';
1324 break;
1325 }
1326 }
1c4db469 1327 if (*d == 't') {
317ea90d
MS
1328 if( !PL_tainting ) {
1329 PL_taint_warn = TRUE;
1330 PL_tainting = TRUE;
1331 }
1c4db469
RGS
1332 } else {
1333 moreswitches(d);
1334 }
6224f72b 1335 }
6224f72b
GS
1336 }
1337 }
a0d0e21e 1338
317ea90d
MS
1339 if (PL_taint_warn && PL_dowarn != G_WARN_ALL_OFF) {
1340 PL_compiling.cop_warnings = newSVpvn(WARN_TAINTstring, WARNsize);
1341 }
1342
6224f72b
GS
1343 if (!scriptname)
1344 scriptname = argv[0];
3280af22 1345 if (PL_e_script) {
6224f72b
GS
1346 argc++,argv--;
1347 scriptname = BIT_BUCKET; /* don't look for script or read stdin */
1348 }
1349 else if (scriptname == Nullch) {
1350#ifdef MSDOS
1351 if ( PerlLIO_isatty(PerlIO_fileno(PerlIO_stdin())) )
1352 moreswitches("h");
1353#endif
1354 scriptname = "-";
1355 }
1356
1357 init_perllib();
1358
1359 open_script(scriptname,dosearch,sv,&fdscript);
1360
1361 validate_suid(validarg, scriptname,fdscript);
1362
64ca3a65 1363#ifndef PERL_MICRO
0b5b802d
GS
1364#if defined(SIGCHLD) || defined(SIGCLD)
1365 {
1366#ifndef SIGCHLD
1367# define SIGCHLD SIGCLD
1368#endif
1369 Sighandler_t sigstate = rsignal_state(SIGCHLD);
1370 if (sigstate == SIG_IGN) {
1371 if (ckWARN(WARN_SIGNAL))
9014280d 1372 Perl_warner(aTHX_ packWARN(WARN_SIGNAL),
0b5b802d
GS
1373 "Can't ignore signal CHLD, forcing to default");
1374 (void)rsignal(SIGCHLD, (Sighandler_t)SIG_DFL);
1375 }
1376 }
1377#endif
64ca3a65 1378#endif
0b5b802d 1379
bf4acbe4
GS
1380#ifdef MACOS_TRADITIONAL
1381 if (PL_doextract || gMacPerl_AlwaysExtract) {
1382#else
f4c556ac 1383 if (PL_doextract) {
bf4acbe4 1384#endif
6224f72b 1385 find_beginning();
f4c556ac
GS
1386 if (cddir && PerlDir_chdir(cddir) < 0)
1387 Perl_croak(aTHX_ "Can't chdir to %s",cddir);
1388
1389 }
6224f72b 1390
3280af22
NIS
1391 PL_main_cv = PL_compcv = (CV*)NEWSV(1104,0);
1392 sv_upgrade((SV *)PL_compcv, SVt_PVCV);
1393 CvUNIQUE_on(PL_compcv);
1394
dd2155a4 1395 CvPADLIST(PL_compcv) = pad_new(0);
4d1ff10f 1396#ifdef USE_5005THREADS
533c011a
NIS
1397 CvOWNER(PL_compcv) = 0;
1398 New(666, CvMUTEXP(PL_compcv), 1, perl_mutex);
1399 MUTEX_INIT(CvMUTEXP(PL_compcv));
4d1ff10f 1400#endif /* USE_5005THREADS */
6224f72b 1401
0c4f7ff0 1402 boot_core_PerlIO();
6224f72b 1403 boot_core_UNIVERSAL();
9a34ef1d 1404#ifndef PERL_MICRO
09bef843 1405 boot_core_xsutils();
9a34ef1d 1406#endif
6224f72b
GS
1407
1408 if (xsinit)
acfe0abc 1409 (*xsinit)(aTHX); /* in case linked C routines want magical variables */
64ca3a65 1410#ifndef PERL_MICRO
ed79a026 1411#if defined(VMS) || defined(WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(EPOC)
c5be433b 1412 init_os_extras();
6224f72b 1413#endif
64ca3a65 1414#endif
6224f72b 1415
29209bc5 1416#ifdef USE_SOCKS
1b9c9cf5
DH
1417# ifdef HAS_SOCKS5_INIT
1418 socks5_init(argv[0]);
1419# else
29209bc5 1420 SOCKSinit(argv[0]);
1b9c9cf5 1421# endif
ac27b0f5 1422#endif
29209bc5 1423
6224f72b
GS
1424 init_predump_symbols();
1425 /* init_postdump_symbols not currently designed to be called */
1426 /* more than once (ENV isn't cleared first, for example) */
1427 /* But running with -u leaves %ENV & @ARGV undefined! XXX */
3280af22 1428 if (!PL_do_undump)
6224f72b
GS
1429 init_postdump_symbols(argc,argv,env);
1430
a05d7ebb
JH
1431 /* PL_unicode is turned on by -C or by $ENV{PERL_UNICODE}.
1432 * PL_utf8locale is conditionally turned on by
085a54d9 1433 * locale.c:Perl_init_i18nl10n() if the environment
a05d7ebb 1434 * look like the user wants to use UTF-8. */
06e66572
JH
1435 if (PL_unicode) {
1436 /* Requires init_predump_symbols(). */
a05d7ebb 1437 if (!(PL_unicode & PERL_UNICODE_LOCALE_FLAG) || PL_utf8locale) {
06e66572
JH
1438 IO* io;
1439 PerlIO* fp;
1440 SV* sv;
1441
a05d7ebb 1442 /* Turn on UTF-8-ness on STDIN, STDOUT, STDERR
06e66572 1443 * and the default open disciplines. */
a05d7ebb
JH
1444 if ((PL_unicode & PERL_UNICODE_STDIN_FLAG) &&
1445 PL_stdingv && (io = GvIO(PL_stdingv)) &&
1446 (fp = IoIFP(io)))
1447 PerlIO_binmode(aTHX_ fp, IoTYPE(io), 0, ":utf8");
1448 if ((PL_unicode & PERL_UNICODE_STDOUT_FLAG) &&
1449 PL_defoutgv && (io = GvIO(PL_defoutgv)) &&
1450 (fp = IoOFP(io)))
1451 PerlIO_binmode(aTHX_ fp, IoTYPE(io), 0, ":utf8");
1452 if ((PL_unicode & PERL_UNICODE_STDERR_FLAG) &&
1453 PL_stderrgv && (io = GvIO(PL_stderrgv)) &&
1454 (fp = IoOFP(io)))
1455 PerlIO_binmode(aTHX_ fp, IoTYPE(io), 0, ":utf8");
1456 if ((PL_unicode & PERL_UNICODE_INOUT_FLAG) &&
1457 (sv = GvSV(gv_fetchpv("\017PEN", TRUE, SVt_PV)))) {
1458 U32 in = PL_unicode & PERL_UNICODE_IN_FLAG;
1459 U32 out = PL_unicode & PERL_UNICODE_OUT_FLAG;
1460 if (in) {
1461 if (out)
1462 sv_setpvn(sv, ":utf8\0:utf8", 11);
1463 else
1464 sv_setpvn(sv, ":utf8\0", 6);
1465 }
1466 else if (out)
1467 sv_setpvn(sv, "\0:utf8", 6);
1468 SvSETMAGIC(sv);
1469 }
b310b053
JH
1470 }
1471 }
1472
4ffa73a3
JH
1473 if ((s = PerlEnv_getenv("PERL_SIGNALS"))) {
1474 if (strEQ(s, "unsafe"))
1475 PL_signals |= PERL_SIGNALS_UNSAFE_FLAG;
1476 else if (strEQ(s, "safe"))
1477 PL_signals &= ~PERL_SIGNALS_UNSAFE_FLAG;
1478 else
1479 Perl_croak(aTHX_ "PERL_SIGNALS illegal: \"%s\"", s);
1480 }
1481
6224f72b
GS
1482 init_lexer();
1483
1484 /* now parse the script */
1485
93189314 1486 SETERRNO(0,SS_NORMAL);
3280af22 1487 PL_error_count = 0;
bf4acbe4
GS
1488#ifdef MACOS_TRADITIONAL
1489 if (gMacPerl_SyntaxError = (yyparse() || PL_error_count)) {
1490 if (PL_minus_c)
1491 Perl_croak(aTHX_ "%s had compilation errors.\n", MacPerl_MPWFileName(PL_origfilename));
1492 else {
1493 Perl_croak(aTHX_ "Execution of %s aborted due to compilation errors.\n",
1494 MacPerl_MPWFileName(PL_origfilename));
1495 }
1496 }
1497#else
3280af22
NIS
1498 if (yyparse() || PL_error_count) {
1499 if (PL_minus_c)
cea2e8a9 1500 Perl_croak(aTHX_ "%s had compilation errors.\n", PL_origfilename);
6224f72b 1501 else {
cea2e8a9 1502 Perl_croak(aTHX_ "Execution of %s aborted due to compilation errors.\n",
097ee67d 1503 PL_origfilename);
6224f72b
GS
1504 }
1505 }
bf4acbe4 1506#endif
57843af0 1507 CopLINE_set(PL_curcop, 0);
3280af22
NIS
1508 PL_curstash = PL_defstash;
1509 PL_preprocess = FALSE;
1510 if (PL_e_script) {
1511 SvREFCNT_dec(PL_e_script);
1512 PL_e_script = Nullsv;
6224f72b
GS
1513 }
1514
3280af22 1515 if (PL_do_undump)
6224f72b
GS
1516 my_unexec();
1517
57843af0
GS
1518 if (isWARN_ONCE) {
1519 SAVECOPFILE(PL_curcop);
1520 SAVECOPLINE(PL_curcop);
3280af22 1521 gv_check(PL_defstash);
57843af0 1522 }
6224f72b
GS
1523
1524 LEAVE;
1525 FREETMPS;
1526
1527#ifdef MYMALLOC
1528 if ((s=PerlEnv_getenv("PERL_DEBUG_MSTATS")) && atoi(s) >= 2)
1529 dump_mstats("after compilation:");
1530#endif
1531
1532 ENTER;
3280af22 1533 PL_restartop = 0;
312caa8e 1534 return NULL;
6224f72b
GS
1535}
1536
954c1994
GS
1537/*
1538=for apidoc perl_run
1539
1540Tells a Perl interpreter to run. See L<perlembed>.
1541
1542=cut
1543*/
1544
6224f72b 1545int
0cb96387 1546perl_run(pTHXx)
6224f72b 1547{
6224f72b 1548 I32 oldscope;
14dd3ad8 1549 int ret = 0;
db36c5a1 1550 dJMPENV;
4d1ff10f 1551#ifdef USE_5005THREADS
cea2e8a9
GS
1552 dTHX;
1553#endif
6224f72b 1554
3280af22 1555 oldscope = PL_scopestack_ix;
96e176bf
CL
1556#ifdef VMS
1557 VMSISH_HUSHED = 0;
1558#endif
6224f72b 1559
14dd3ad8 1560#ifdef PERL_FLEXIBLE_EXCEPTIONS
312caa8e 1561 redo_body:
14dd3ad8
GS
1562 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vrun_body), oldscope);
1563#else
1564 JMPENV_PUSH(ret);
1565#endif
6224f72b
GS
1566 switch (ret) {
1567 case 1:
1568 cxstack_ix = -1; /* start context stack again */
312caa8e 1569 goto redo_body;
14dd3ad8
GS
1570 case 0: /* normal completion */
1571#ifndef PERL_FLEXIBLE_EXCEPTIONS
1572 redo_body:
1573 run_body(oldscope);
1574#endif
1575 /* FALL THROUGH */
1576 case 2: /* my_exit() */
3280af22 1577 while (PL_scopestack_ix > oldscope)
6224f72b
GS
1578 LEAVE;
1579 FREETMPS;
3280af22 1580 PL_curstash = PL_defstash;
3a1ee7e8 1581 if (!(PL_exit_flags & PERL_EXIT_DESTRUCT_END) &&
31d77e54
AB
1582 PL_endav && !PL_minus_c)
1583 call_list(oldscope, PL_endav);
6224f72b
GS
1584#ifdef MYMALLOC
1585 if (PerlEnv_getenv("PERL_DEBUG_MSTATS"))
1586 dump_mstats("after execution: ");
1587#endif
14dd3ad8
GS
1588 ret = STATUS_NATIVE_EXPORT;
1589 break;
6224f72b 1590 case 3:
312caa8e
CS
1591 if (PL_restartop) {
1592 POPSTACK_TO(PL_mainstack);
1593 goto redo_body;
6224f72b 1594 }
bf49b057 1595 PerlIO_printf(Perl_error_log, "panic: restartop\n");
312caa8e 1596 FREETMPS;
14dd3ad8
GS
1597 ret = 1;
1598 break;
6224f72b
GS
1599 }
1600
14dd3ad8
GS
1601 JMPENV_POP;
1602 return ret;
312caa8e
CS
1603}
1604
14dd3ad8 1605#ifdef PERL_FLEXIBLE_EXCEPTIONS
312caa8e 1606STATIC void *
14dd3ad8 1607S_vrun_body(pTHX_ va_list args)
312caa8e 1608{
312caa8e
CS
1609 I32 oldscope = va_arg(args, I32);
1610
14dd3ad8
GS
1611 return run_body(oldscope);
1612}
1613#endif
1614
1615
1616STATIC void *
1617S_run_body(pTHX_ I32 oldscope)
1618{
6224f72b 1619 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s $` $& $' support.\n",
3280af22 1620 PL_sawampersand ? "Enabling" : "Omitting"));
6224f72b 1621
3280af22 1622 if (!PL_restartop) {
6224f72b
GS
1623 DEBUG_x(dump_all());
1624 DEBUG(PerlIO_printf(Perl_debug_log, "\nEXECUTING...\n\n"));
b900a521
JH
1625 DEBUG_S(PerlIO_printf(Perl_debug_log, "main thread is 0x%"UVxf"\n",
1626 PTR2UV(thr)));
6224f72b 1627
3280af22 1628 if (PL_minus_c) {
bf4acbe4 1629#ifdef MACOS_TRADITIONAL
e69a2255
JH
1630 PerlIO_printf(Perl_error_log, "%s%s syntax OK\n",
1631 (gMacPerl_ErrorFormat ? "# " : ""),
1632 MacPerl_MPWFileName(PL_origfilename));
bf4acbe4 1633#else
bf49b057 1634 PerlIO_printf(Perl_error_log, "%s syntax OK\n", PL_origfilename);
bf4acbe4 1635#endif
6224f72b
GS
1636 my_exit(0);
1637 }
3280af22 1638 if (PERLDB_SINGLE && PL_DBsingle)
ac27b0f5 1639 sv_setiv(PL_DBsingle, 1);
3280af22
NIS
1640 if (PL_initav)
1641 call_list(oldscope, PL_initav);
6224f72b
GS
1642 }
1643
1644 /* do it */
1645
3280af22 1646 if (PL_restartop) {
533c011a 1647 PL_op = PL_restartop;
3280af22 1648 PL_restartop = 0;
cea2e8a9 1649 CALLRUNOPS(aTHX);
6224f72b 1650 }
3280af22
NIS
1651 else if (PL_main_start) {
1652 CvDEPTH(PL_main_cv) = 1;
533c011a 1653 PL_op = PL_main_start;
cea2e8a9 1654 CALLRUNOPS(aTHX);
6224f72b
GS
1655 }
1656
f6b3007c
JH
1657 my_exit(0);
1658 /* NOTREACHED */
312caa8e 1659 return NULL;
6224f72b
GS
1660}
1661
954c1994 1662/*
ccfc67b7
JH
1663=head1 SV Manipulation Functions
1664
954c1994
GS
1665=for apidoc p||get_sv
1666
1667Returns the SV of the specified Perl scalar. If C<create> is set and the
1668Perl variable does not exist then it will be created. If C<create> is not
1669set and the variable does not exist then NULL is returned.
1670
1671=cut
1672*/
1673
6224f72b 1674SV*
864dbfa3 1675Perl_get_sv(pTHX_ const char *name, I32 create)
6224f72b
GS
1676{
1677 GV *gv;
4d1ff10f 1678#ifdef USE_5005THREADS
6224f72b
GS
1679 if (name[1] == '\0' && !isALPHA(name[0])) {
1680 PADOFFSET tmp = find_threadsv(name);
411caa50 1681 if (tmp != NOT_IN_PAD)
6224f72b 1682 return THREADSV(tmp);
6224f72b 1683 }
4d1ff10f 1684#endif /* USE_5005THREADS */
6224f72b
GS
1685 gv = gv_fetchpv(name, create, SVt_PV);
1686 if (gv)
1687 return GvSV(gv);
1688 return Nullsv;
1689}
1690
954c1994 1691/*
ccfc67b7
JH
1692=head1 Array Manipulation Functions
1693
954c1994
GS
1694=for apidoc p||get_av
1695
1696Returns the AV of the specified Perl array. If C<create> is set and the
1697Perl variable does not exist then it will be created. If C<create> is not
1698set and the variable does not exist then NULL is returned.
1699
1700=cut
1701*/
1702
6224f72b 1703AV*
864dbfa3 1704Perl_get_av(pTHX_ const char *name, I32 create)
6224f72b
GS
1705{
1706 GV* gv = gv_fetchpv(name, create, SVt_PVAV);
1707 if (create)
1708 return GvAVn(gv);
1709 if (gv)
1710 return GvAV(gv);
1711 return Nullav;
1712}
1713
954c1994 1714/*
ccfc67b7
JH
1715=head1 Hash Manipulation Functions
1716
954c1994
GS
1717=for apidoc p||get_hv
1718
1719Returns the HV of the specified Perl hash. If C<create> is set and the
1720Perl variable does not exist then it will be created. If C<create> is not
1721set and the variable does not exist then NULL is returned.
1722
1723=cut
1724*/
1725
6224f72b 1726HV*
864dbfa3 1727Perl_get_hv(pTHX_ const char *name, I32 create)
6224f72b 1728{
a0d0e21e
LW
1729 GV* gv = gv_fetchpv(name, create, SVt_PVHV);
1730 if (create)
1731 return GvHVn(gv);
1732 if (gv)
1733 return GvHV(gv);
1734 return Nullhv;
1735}
1736
954c1994 1737/*
ccfc67b7
JH
1738=head1 CV Manipulation Functions
1739
954c1994
GS
1740=for apidoc p||get_cv
1741
1742Returns the CV of the specified Perl subroutine. If C<create> is set and
1743the Perl subroutine does not exist then it will be declared (which has the
1744same effect as saying C<sub name;>). If C<create> is not set and the
1745subroutine does not exist then NULL is returned.
1746
1747=cut
1748*/
1749
a0d0e21e 1750CV*
864dbfa3 1751Perl_get_cv(pTHX_ const char *name, I32 create)
a0d0e21e
LW
1752{
1753 GV* gv = gv_fetchpv(name, create, SVt_PVCV);
b099ddc0 1754 /* XXX unsafe for threads if eval_owner isn't held */
f6ec51f7
GS
1755 /* XXX this is probably not what they think they're getting.
1756 * It has the same effect as "sub name;", i.e. just a forward
1757 * declaration! */
8ebc5c01 1758 if (create && !GvCVu(gv))
774d564b 1759 return newSUB(start_subparse(FALSE, 0),
a0d0e21e 1760 newSVOP(OP_CONST, 0, newSVpv(name,0)),
4633a7c4 1761 Nullop,
a0d0e21e
LW
1762 Nullop);
1763 if (gv)
8ebc5c01 1764 return GvCVu(gv);
a0d0e21e
LW
1765 return Nullcv;
1766}
1767
79072805
LW
1768/* Be sure to refetch the stack pointer after calling these routines. */
1769
954c1994 1770/*
ccfc67b7
JH
1771
1772=head1 Callback Functions
1773
954c1994
GS
1774=for apidoc p||call_argv
1775
1776Performs a callback to the specified Perl sub. See L<perlcall>.
1777
1778=cut
1779*/
1780
a0d0e21e 1781I32
864dbfa3 1782Perl_call_argv(pTHX_ const char *sub_name, I32 flags, register char **argv)
ac27b0f5 1783
8ac85365
NIS
1784 /* See G_* flags in cop.h */
1785 /* null terminated arg list */
8990e307 1786{
a0d0e21e 1787 dSP;
8990e307 1788
924508f0 1789 PUSHMARK(SP);
a0d0e21e 1790 if (argv) {
8990e307 1791 while (*argv) {
a0d0e21e 1792 XPUSHs(sv_2mortal(newSVpv(*argv,0)));
8990e307
LW
1793 argv++;
1794 }
a0d0e21e 1795 PUTBACK;
8990e307 1796 }
864dbfa3 1797 return call_pv(sub_name, flags);
8990e307
LW
1798}
1799
954c1994
GS
1800/*
1801=for apidoc p||call_pv
1802
1803Performs a callback to the specified Perl sub. See L<perlcall>.
1804
1805=cut
1806*/
1807
a0d0e21e 1808I32
864dbfa3 1809Perl_call_pv(pTHX_ const char *sub_name, I32 flags)
8ac85365
NIS
1810 /* name of the subroutine */
1811 /* See G_* flags in cop.h */
a0d0e21e 1812{
864dbfa3 1813 return call_sv((SV*)get_cv(sub_name, TRUE), flags);
a0d0e21e
LW
1814}
1815
954c1994
GS
1816/*
1817=for apidoc p||call_method
1818
1819Performs a callback to the specified Perl method. The blessed object must
1820be on the stack. See L<perlcall>.
1821
1822=cut
1823*/
1824
a0d0e21e 1825I32
864dbfa3 1826Perl_call_method(pTHX_ const char *methname, I32 flags)
8ac85365
NIS
1827 /* name of the subroutine */
1828 /* See G_* flags in cop.h */
a0d0e21e 1829{
968b3946 1830 return call_sv(sv_2mortal(newSVpv(methname,0)), flags | G_METHOD);
a0d0e21e
LW
1831}
1832
1833/* May be called with any of a CV, a GV, or an SV containing the name. */
954c1994
GS
1834/*
1835=for apidoc p||call_sv
1836
1837Performs a callback to the Perl sub whose name is in the SV. See
1838L<perlcall>.
1839
1840=cut
1841*/
1842
a0d0e21e 1843I32
864dbfa3 1844Perl_call_sv(pTHX_ SV *sv, I32 flags)
8ac85365 1845 /* See G_* flags in cop.h */
a0d0e21e 1846{
924508f0 1847 dSP;
a0d0e21e 1848 LOGOP myop; /* fake syntax tree node */
968b3946 1849 UNOP method_op;
aa689395 1850 I32 oldmark;
13689cfe 1851 volatile I32 retval = 0;
a0d0e21e 1852 I32 oldscope;
54310121 1853 bool oldcatch = CATCH_GET;
6224f72b 1854 int ret;
533c011a 1855 OP* oldop = PL_op;
db36c5a1 1856 dJMPENV;
1e422769 1857
a0d0e21e
LW
1858 if (flags & G_DISCARD) {
1859 ENTER;
1860 SAVETMPS;
1861 }
1862
aa689395 1863 Zero(&myop, 1, LOGOP);
54310121 1864 myop.op_next = Nullop;
f51d4af5 1865 if (!(flags & G_NOARGS))
aa689395 1866 myop.op_flags |= OPf_STACKED;
54310121
PP
1867 myop.op_flags |= ((flags & G_VOID) ? OPf_WANT_VOID :
1868 (flags & G_ARRAY) ? OPf_WANT_LIST :
1869 OPf_WANT_SCALAR);
462e5cf6 1870 SAVEOP();
533c011a 1871 PL_op = (OP*)&myop;
aa689395 1872
3280af22
NIS
1873 EXTEND(PL_stack_sp, 1);
1874 *++PL_stack_sp = sv;
aa689395 1875 oldmark = TOPMARK;
3280af22 1876 oldscope = PL_scopestack_ix;
a0d0e21e 1877
3280af22 1878 if (PERLDB_SUB && PL_curstash != PL_debstash
36477c24 1879 /* Handle first BEGIN of -d. */
3280af22 1880 && (PL_DBcv || (PL_DBcv = GvCV(PL_DBsub)))
36477c24
PP
1881 /* Try harder, since this may have been a sighandler, thus
1882 * curstash may be meaningless. */
3280af22 1883 && (SvTYPE(sv) != SVt_PVCV || CvSTASH((CV*)sv) != PL_debstash)
491527d0 1884 && !(flags & G_NODEBUG))
533c011a 1885 PL_op->op_private |= OPpENTERSUB_DB;
a0d0e21e 1886
968b3946
GS
1887 if (flags & G_METHOD) {
1888 Zero(&method_op, 1, UNOP);
1889 method_op.op_next = PL_op;
1890 method_op.op_ppaddr = PL_ppaddr[OP_METHOD];
1891 myop.op_ppaddr = PL_ppaddr[OP_ENTERSUB];
f39d0b86 1892 PL_op = (OP*)&method_op;
968b3946
GS
1893 }
1894
312caa8e 1895 if (!(flags & G_EVAL)) {
0cdb2077 1896 CATCH_SET(TRUE);
14dd3ad8 1897 call_body((OP*)&myop, FALSE);
312caa8e 1898 retval = PL_stack_sp - (PL_stack_base + oldmark);
0253cb41 1899 CATCH_SET(oldcatch);
312caa8e
CS
1900 }
1901 else {
d78bda3d 1902 myop.op_other = (OP*)&myop;
3280af22 1903 PL_markstack_ptr--;
4633a7c4
LW
1904 /* we're trying to emulate pp_entertry() here */
1905 {
c09156bb 1906 register PERL_CONTEXT *cx;
54310121 1907 I32 gimme = GIMME_V;
ac27b0f5 1908
4633a7c4
LW
1909 ENTER;
1910 SAVETMPS;
ac27b0f5 1911
968b3946 1912 push_return(Nullop);
1d76a5c3 1913 PUSHBLOCK(cx, (CXt_EVAL|CXp_TRYBLOCK), PL_stack_sp);
4633a7c4 1914 PUSHEVAL(cx, 0, 0);
533c011a 1915 PL_eval_root = PL_op; /* Only needed so that goto works right. */
ac27b0f5 1916
faef0170 1917 PL_in_eval = EVAL_INEVAL;
4633a7c4 1918 if (flags & G_KEEPERR)
faef0170 1919 PL_in_eval |= EVAL_KEEPERR;
4633a7c4 1920 else
38a03e6e 1921 sv_setpv(ERRSV,"");
4633a7c4 1922 }
3280af22 1923 PL_markstack_ptr++;
a0d0e21e 1924
14dd3ad8
GS
1925#ifdef PERL_FLEXIBLE_EXCEPTIONS
1926 redo_body:
1927 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vcall_body),
db36c5a1 1928 (OP*)&myop, FALSE);
14dd3ad8
GS
1929#else
1930 JMPENV_PUSH(ret);
1931#endif
6224f72b
GS
1932 switch (ret) {
1933 case 0:
14dd3ad8
GS
1934#ifndef PERL_FLEXIBLE_EXCEPTIONS
1935 redo_body:
1936 call_body((OP*)&myop, FALSE);
1937#endif
312caa8e
CS
1938 retval = PL_stack_sp - (PL_stack_base + oldmark);
1939 if (!(flags & G_KEEPERR))
1940 sv_setpv(ERRSV,"");
a0d0e21e 1941 break;
6224f72b 1942 case 1:
f86702cc 1943 STATUS_ALL_FAILURE;
a0d0e21e 1944 /* FALL THROUGH */
6224f72b 1945 case 2:
a0d0e21e 1946 /* my_exit() was called */
3280af22 1947 PL_curstash = PL_defstash;
a0d0e21e 1948 FREETMPS;
14dd3ad8 1949 JMPENV_POP;
cc3604b1 1950 if (PL_statusvalue && !(PL_exit_flags & PERL_EXIT_EXPECTED))
cea2e8a9 1951 Perl_croak(aTHX_ "Callback called exit");
f86702cc 1952 my_exit_jump();
a0d0e21e 1953 /* NOTREACHED */
6224f72b 1954 case 3:
3280af22 1955 if (PL_restartop) {
533c011a 1956 PL_op = PL_restartop;
3280af22 1957 PL_restartop = 0;
312caa8e 1958 goto redo_body;
a0d0e21e 1959 }
3280af22 1960 PL_stack_sp = PL_stack_base + oldmark;
a0d0e21e
LW
1961 if (flags & G_ARRAY)
1962 retval = 0;
1963 else {
1964 retval = 1;
3280af22 1965 *++PL_stack_sp = &PL_sv_undef;
a0d0e21e 1966 }
312caa8e 1967 break;
a0d0e21e 1968 }
a0d0e21e 1969
3280af22 1970 if (PL_scopestack_ix > oldscope) {
a0a2876f
LW
1971 SV **newsp;
1972 PMOP *newpm;
1973 I32 gimme;
c09156bb 1974 register PERL_CONTEXT *cx;
a0a2876f
LW
1975 I32 optype;
1976
1977 POPBLOCK(cx,newpm);
1978 POPEVAL(cx);
1979 pop_return();
3280af22 1980 PL_curpm = newpm;
a0a2876f 1981 LEAVE;
a0d0e21e 1982 }
14dd3ad8 1983 JMPENV_POP;
a0d0e21e 1984 }
1e422769 1985
a0d0e21e 1986 if (flags & G_DISCARD) {
3280af22 1987 PL_stack_sp = PL_stack_base + oldmark;
a0d0e21e
LW
1988 retval = 0;
1989 FREETMPS;
1990 LEAVE;
1991 }
533c011a 1992 PL_op = oldop;
a0d0e21e
LW
1993 return retval;
1994}
1995
14dd3ad8 1996#ifdef PERL_FLEXIBLE_EXCEPTIONS
312caa8e 1997STATIC void *
14dd3ad8 1998S_vcall_body(pTHX_ va_list args)
312caa8e
CS
1999{
2000 OP *myop = va_arg(args, OP*);
2001 int is_eval = va_arg(args, int);
2002
14dd3ad8 2003 call_body(myop, is_eval);
312caa8e
CS
2004 return NULL;
2005}
14dd3ad8 2006#endif
312caa8e
CS
2007
2008STATIC void
14dd3ad8 2009S_call_body(pTHX_ OP *myop, int is_eval)
312caa8e 2010{
312caa8e
CS
2011 if (PL_op == myop) {
2012 if (is_eval)
f807eda9 2013 PL_op = Perl_pp_entereval(aTHX); /* this doesn't do a POPMARK */
312caa8e 2014 else
f807eda9 2015 PL_op = Perl_pp_entersub(aTHX); /* this does */
312caa8e
CS
2016 }
2017 if (PL_op)
cea2e8a9 2018 CALLRUNOPS(aTHX);
312caa8e
CS
2019}
2020
6e72f9df 2021/* Eval a string. The G_EVAL flag is always assumed. */
8990e307 2022
954c1994
GS
2023/*
2024=for apidoc p||eval_sv
2025
2026Tells Perl to C<eval> the string in the SV.
2027
2028=cut
2029*/
2030
a0d0e21e 2031I32
864dbfa3 2032Perl_eval_sv(pTHX_ SV *sv, I32 flags)
ac27b0f5 2033
8ac85365 2034 /* See G_* flags in cop.h */
a0d0e21e 2035{
924508f0 2036 dSP;
a0d0e21e 2037 UNOP myop; /* fake syntax tree node */
8fa7f367 2038 volatile I32 oldmark = SP - PL_stack_base;
13689cfe 2039 volatile I32 retval = 0;
4633a7c4 2040 I32 oldscope;
6224f72b 2041 int ret;
533c011a 2042 OP* oldop = PL_op;
db36c5a1 2043 dJMPENV;
84902520 2044
4633a7c4
LW
2045 if (flags & G_DISCARD) {
2046 ENTER;
2047 SAVETMPS;
2048 }
2049
462e5cf6 2050 SAVEOP();
533c011a
NIS
2051 PL_op = (OP*)&myop;
2052 Zero(PL_op, 1, UNOP);
3280af22
NIS
2053 EXTEND(PL_stack_sp, 1);
2054 *++PL_stack_sp = sv;
2055 oldscope = PL_scopestack_ix;
79072805 2056
4633a7c4
LW
2057 if (!(flags & G_NOARGS))
2058 myop.op_flags = OPf_STACKED;
79072805 2059 myop.op_next = Nullop;
6e72f9df 2060 myop.op_type = OP_ENTEREVAL;
54310121
PP
2061 myop.op_flags |= ((flags & G_VOID) ? OPf_WANT_VOID :
2062 (flags & G_ARRAY) ? OPf_WANT_LIST :
2063 OPf_WANT_SCALAR);
6e72f9df
PP
2064 if (flags & G_KEEPERR)
2065 myop.op_flags |= OPf_SPECIAL;
4633a7c4 2066
14dd3ad8 2067#ifdef PERL_FLEXIBLE_EXCEPTIONS
312caa8e 2068 redo_body:
14dd3ad8 2069 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vcall_body),
db36c5a1 2070 (OP*)&myop, TRUE);
14dd3ad8
GS
2071#else
2072 JMPENV_PUSH(ret);
2073#endif
6224f72b
GS
2074 switch (ret) {
2075 case 0:
14dd3ad8
GS
2076#ifndef PERL_FLEXIBLE_EXCEPTIONS
2077 redo_body:
2078 call_body((OP*)&myop,TRUE);
2079#endif
312caa8e
CS
2080 retval = PL_stack_sp - (PL_stack_base + oldmark);
2081 if (!(flags & G_KEEPERR))
2082 sv_setpv(ERRSV,"");
4633a7c4 2083 break;
6224f72b 2084 case 1:
f86702cc 2085 STATUS_ALL_FAILURE;
4633a7c4 2086 /* FALL THROUGH */
6224f72b 2087 case 2:
4633a7c4 2088 /* my_exit() was called */
3280af22 2089 PL_curstash = PL_defstash;
4633a7c4 2090 FREETMPS;
14dd3ad8 2091 JMPENV_POP;
cc3604b1 2092 if (PL_statusvalue && !(PL_exit_flags & PERL_EXIT_EXPECTED))
cea2e8a9 2093 Perl_croak(aTHX_ "Callback called exit");
f86702cc 2094 my_exit_jump();
4633a7c4 2095 /* NOTREACHED */
6224f72b 2096 case 3:
3280af22 2097 if (PL_restartop) {
533c011a 2098 PL_op = PL_restartop;
3280af22 2099 PL_restartop = 0;
312caa8e 2100 goto redo_body;
4633a7c4 2101 }
3280af22 2102 PL_stack_sp = PL_stack_base + oldmark;
4633a7c4
LW
2103 if (flags & G_ARRAY)
2104 retval = 0;
2105 else {
2106 retval = 1;
3280af22 2107 *++PL_stack_sp = &PL_sv_undef;
4633a7c4 2108 }
312caa8e 2109 break;
4633a7c4
LW
2110 }
2111
14dd3ad8 2112 JMPENV_POP;
4633a7c4 2113 if (flags & G_DISCARD) {
3280af22 2114 PL_stack_sp = PL_stack_base + oldmark;
4633a7c4
LW
2115 retval = 0;
2116 FREETMPS;
2117 LEAVE;
2118 }
533c011a 2119 PL_op = oldop;
4633a7c4
LW
2120 return retval;
2121}
2122
954c1994
GS
2123/*
2124=for apidoc p||eval_pv
2125
2126Tells Perl to C<eval> the given string and return an SV* result.
2127
2128=cut
2129*/
2130
137443ea 2131SV*
864dbfa3 2132Perl_eval_pv(pTHX_ const char *p, I32 croak_on_error)
137443ea
PP
2133{
2134 dSP;
2135 SV* sv = newSVpv(p, 0);
2136
864dbfa3 2137 eval_sv(sv, G_SCALAR);
137443ea
PP
2138 SvREFCNT_dec(sv);
2139
2140 SPAGAIN;
2141 sv = POPs;
2142 PUTBACK;
2143
2d8e6c8d
GS
2144 if (croak_on_error && SvTRUE(ERRSV)) {
2145 STRLEN n_a;
cea2e8a9 2146 Perl_croak(aTHX_ SvPVx(ERRSV, n_a));
2d8e6c8d 2147 }
137443ea
PP
2148
2149 return sv;
2150}
2151
4633a7c4
LW
2152/* Require a module. */
2153
954c1994 2154/*
ccfc67b7
JH
2155=head1 Embedding Functions
2156
954c1994
GS
2157=for apidoc p||require_pv
2158
7d3fb230
BS
2159Tells Perl to C<require> the file named by the string argument. It is
2160analogous to the Perl code C<eval "require '$file'">. It's even
2307c6d0 2161implemented that way; consider using load_module instead.
954c1994 2162
7d3fb230 2163=cut */
954c1994 2164
4633a7c4 2165void
864dbfa3 2166Perl_require_pv(pTHX_ const char *pv)
4633a7c4 2167{
d3acc0f7
JP
2168 SV* sv;
2169 dSP;
e788e7d3 2170 PUSHSTACKi(PERLSI_REQUIRE);
d3acc0f7
JP
2171 PUTBACK;
2172 sv = sv_newmortal();
4633a7c4
LW
2173 sv_setpv(sv, "require '");
2174 sv_catpv(sv, pv);
2175 sv_catpv(sv, "'");
864dbfa3 2176 eval_sv(sv, G_DISCARD);
d3acc0f7
JP
2177 SPAGAIN;
2178 POPSTACK;
79072805
LW
2179}
2180
79072805 2181void
864dbfa3 2182Perl_magicname(pTHX_ char *sym, char *name, I32 namlen)
79072805
LW
2183{
2184 register GV *gv;
2185
155aba94 2186 if ((gv = gv_fetchpv(sym,TRUE, SVt_PV)))
14befaf4 2187 sv_magic(GvSV(gv), (SV*)gv, PERL_MAGIC_sv, name, namlen);
79072805
LW
2188}
2189
76e3520e 2190STATIC void
cea2e8a9 2191S_usage(pTHX_ char *name) /* XXX move this out into a module ? */
4633a7c4 2192{
ab821d7f 2193 /* This message really ought to be max 23 lines.
75c72d73 2194 * Removed -h because the user already knows that option. Others? */
fb73857a 2195
76e3520e 2196 static char *usage_msg[] = {
fb73857a
PP
2197"-0[octal] specify record separator (\\0, if no argument)",
2198"-a autosplit mode with -n or -p (splits $_ into @F)",
46487f74 2199"-C enable native wide character system interfaces",
1950ee41 2200"-c check syntax only (runs BEGIN and CHECK blocks)",
aac3bd0d
GS
2201"-d[:debugger] run program under debugger",
2202"-D[number/list] set debugging flags (argument is a bit mask or alphabets)",
2203"-e 'command' one line of program (several -e's allowed, omit programfile)",
2204"-F/pattern/ split() pattern for -a switch (//'s are optional)",
2205"-i[extension] edit <> files in place (makes backup if extension supplied)",
2206"-Idirectory specify @INC/#include directory (several -I's allowed)",
fb73857a 2207"-l[octal] enable line ending processing, specifies line terminator",
aac3bd0d
GS
2208"-[mM][-]module execute `use/no module...' before executing program",
2209"-n assume 'while (<>) { ... }' loop around program",
2210"-p assume loop like -n but print line also, like sed",
2211"-P run program through C preprocessor before compilation",
2212"-s enable rudimentary parsing for switches after programfile",
2213"-S look for programfile using PATH environment variable",
2214"-T enable tainting checks",
9cbc33e8 2215"-t enable tainting warnings",
aac3bd0d 2216"-u dump core after parsing program",
fb73857a 2217"-U allow unsafe operations",
aac3bd0d
GS
2218"-v print version, subversion (includes VERY IMPORTANT perl info)",
2219"-V[:variable] print configuration summary (or a single Config.pm variable)",
2220"-w enable many useful warnings (RECOMMENDED)",
3c0facb2
GS
2221"-W enable all warnings",
2222"-X disable all warnings",
fb73857a
PP
2223"-x[directory] strip off text before #!perl line and perhaps cd to directory",
2224"\n",
2225NULL
2226};
76e3520e 2227 char **p = usage_msg;
fb73857a 2228
b0e47665
GS
2229 PerlIO_printf(PerlIO_stdout(),
2230 "\nUsage: %s [switches] [--] [programfile] [arguments]",
2231 name);
fb73857a 2232 while (*p)
b0e47665 2233 PerlIO_printf(PerlIO_stdout(), "\n %s", *p++);
4633a7c4
LW
2234}
2235
b4ab917c
DM
2236/* convert a string of -D options (or digits) into an int.
2237 * sets *s to point to the char after the options */
2238
2239#ifdef DEBUGGING
2240int
2241Perl_get_debug_opts(pTHX_ char **s)
2242{
2243 int i = 0;
2244 if (isALPHA(**s)) {
2245 /* if adding extra options, remember to update DEBUG_MASK */
2246 static char debopts[] = "psltocPmfrxu HXDSTRJvC";
2247
2248 for (; isALNUM(**s); (*s)++) {
2249 char *d = strchr(debopts,**s);
2250 if (d)
2251 i |= 1 << (d - debopts);
2252 else if (ckWARN_d(WARN_DEBUGGING))
2253 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING),
2254 "invalid option -D%c\n", **s);
2255 }
2256 }
2257 else {
2258 i = atoi(*s);
2259 for (; isALNUM(**s); (*s)++) ;
2260 }
2261# ifdef EBCDIC
2262 if ((i & DEBUG_p_FLAG) && ckWARN_d(WARN_DEBUGGING))
2263 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING),
2264 "-Dp not implemented on this platform\n");
2265# endif
2266 return i;
2267}
2268#endif
2269
79072805
LW
2270/* This routine handles any switches that can be given during run */
2271
2272char *
864dbfa3 2273Perl_moreswitches(pTHX_ char *s)
79072805 2274{
ba210ebe 2275 STRLEN numlen;
84c133a0 2276 UV rschar;
79072805
LW
2277
2278 switch (*s) {
2279 case '0':
a863c7d1 2280 {
f2095865
JH
2281 I32 flags = 0;
2282
2283 SvREFCNT_dec(PL_rs);
2284 if (s[1] == 'x' && s[2]) {
2285 char *e;
2286 U8 *tmps;
2287
2288 for (s += 2, e = s; *e; e++);
2289 numlen = e - s;
2290 flags = PERL_SCAN_SILENT_ILLDIGIT;
2291 rschar = (U32)grok_hex(s, &numlen, &flags, NULL);
2292 if (s + numlen < e) {
2293 rschar = 0; /* Grandfather -0xFOO as -0 -xFOO. */
2294 numlen = 0;
2295 s--;
2296 }
2297 PL_rs = newSVpvn("", 0);
c5661c80 2298 SvGROW(PL_rs, (STRLEN)(UNISKIP(rschar) + 1));
f2095865
JH
2299 tmps = (U8*)SvPVX(PL_rs);
2300 uvchr_to_utf8(tmps, rschar);
2301 SvCUR_set(PL_rs, UNISKIP(rschar));
2302 SvUTF8_on(PL_rs);
2303 }
2304 else {
2305 numlen = 4;
2306 rschar = (U32)grok_oct(s, &numlen, &flags, NULL);
2307 if (rschar & ~((U8)~0))
2308 PL_rs = &PL_sv_undef;
2309 else if (!rschar && numlen >= 2)
2310 PL_rs = newSVpvn("", 0);
2311 else {
2312 char ch = (char)rschar;
2313 PL_rs = newSVpvn(&ch, 1);
2314 }
2315 }
2316 return s + numlen;
a863c7d1 2317 }
46487f74 2318 case 'C':
a05d7ebb
JH
2319 s++;
2320 PL_unicode = parse_unicode_opts(&s);
46487f74 2321 return s;
2304df62 2322 case 'F':
3280af22 2323 PL_minus_F = TRUE;
ebce5377
RGS
2324 PL_splitstr = ++s;
2325 while (*s && !isSPACE(*s)) ++s;
2326 *s = '\0';
2327 PL_splitstr = savepv(PL_splitstr);
2304df62 2328 return s;
79072805 2329 case 'a':
3280af22 2330 PL_minus_a = TRUE;
79072805
LW
2331 s++;
2332 return s;
2333 case 'c':
3280af22 2334 PL_minus_c = TRUE;
79072805
LW
2335 s++;
2336 return s;
2337 case 'd':
bbce6d69 2338 forbid_setid("-d");
4633a7c4 2339 s++;
70c94a19
RR
2340 /* The following permits -d:Mod to accepts arguments following an =
2341 in the fashion that -MSome::Mod does. */
2342 if (*s == ':' || *s == '=') {
2343 char *start;
2344 SV *sv;
2345 sv = newSVpv("use Devel::", 0);
2346 start = ++s;
2347 /* We now allow -d:Module=Foo,Bar */
2348 while(isALNUM(*s) || *s==':') ++s;
2349 if (*s != '=')
2350 sv_catpv(sv, start);
2351 else {
2352 sv_catpvn(sv, start, s-start);
2353 sv_catpv(sv, " split(/,/,q{");
2354 sv_catpv(sv, ++s);
2355 sv_catpv(sv, "})");
2356 }
4633a7c4 2357 s += strlen(s);
70c94a19 2358 my_setenv("PERL5DB", SvPV(sv, PL_na));
4633a7c4 2359 }
ed094faf 2360 if (!PL_perldb) {
3280af22 2361 PL_perldb = PERLDB_ALL;
a0d0e21e 2362 init_debugger();
ed094faf 2363 }
79072805
LW
2364 return s;
2365 case 'D':
0453d815 2366 {
79072805 2367#ifdef DEBUGGING
bbce6d69 2368 forbid_setid("-D");
b4ab917c
DM
2369 s++;
2370 PL_debug = get_debug_opts(&s) | DEBUG_TOP_FLAG;
12a43e32 2371#else /* !DEBUGGING */
0453d815 2372 if (ckWARN_d(WARN_DEBUGGING))
9014280d 2373 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING),
0453d815 2374 "Recompile perl with -DDEBUGGING to use -D switch\n");
a0d0e21e 2375 for (s++; isALNUM(*s); s++) ;
79072805
LW
2376#endif
2377 /*SUPPRESS 530*/
2378 return s;
0453d815 2379 }
4633a7c4 2380 case 'h':
ac27b0f5 2381 usage(PL_origargv[0]);
7ca617d0 2382 my_exit(0);
79072805 2383 case 'i':
3280af22
NIS
2384 if (PL_inplace)
2385 Safefree(PL_inplace);
c030f24b
GH
2386#if defined(__CYGWIN__) /* do backup extension automagically */
2387 if (*(s+1) == '\0') {
2388 PL_inplace = savepv(".bak");
2389 return s+1;
2390 }
2391#endif /* __CYGWIN__ */
3280af22 2392 PL_inplace = savepv(s+1);
79072805 2393 /*SUPPRESS 530*/
3280af22 2394 for (s = PL_inplace; *s && !isSPACE(*s); s++) ;
7b8d334a 2395 if (*s) {
fb73857a 2396 *s++ = '\0';
7b8d334a
GS
2397 if (*s == '-') /* Additional switches on #! line. */
2398 s++;
2399 }
fb73857a 2400 return s;
4e49a025 2401 case 'I': /* -I handled both here and in parse_body() */
bbce6d69 2402 forbid_setid("-I");
fb73857a
PP
2403 ++s;
2404 while (*s && isSPACE(*s))
2405 ++s;
2406 if (*s) {
774d564b 2407 char *e, *p;
0df16ed7
GS
2408 p = s;
2409 /* ignore trailing spaces (possibly followed by other switches) */
2410 do {
2411 for (e = p; *e && !isSPACE(*e); e++) ;
2412 p = e;
2413 while (isSPACE(*p))
2414 p++;
2415 } while (*p && *p != '-');
2416 e = savepvn(s, e-s);
574c798a 2417 incpush(e, TRUE, TRUE, FALSE);
0df16ed7
GS
2418 Safefree(e);
2419 s = p;
2420 if (*s == '-')
2421 s++;
79072805
LW
2422 }
2423 else
a67e862a 2424 Perl_croak(aTHX_ "No directory specified for -I");
fb73857a 2425 return s;
79072805 2426 case 'l':
3280af22 2427 PL_minus_l = TRUE;
79072805 2428 s++;
7889fe52
NIS
2429 if (PL_ors_sv) {
2430 SvREFCNT_dec(PL_ors_sv);
2431 PL_ors_sv = Nullsv;
2432 }
79072805 2433 if (isDIGIT(*s)) {
53305cf1 2434 I32 flags = 0;
7889fe52 2435 PL_ors_sv = newSVpvn("\n",1);
53305cf1
NC
2436 numlen = 3 + (*s == '0');
2437 *SvPVX(PL_ors_sv) = (char)grok_oct(s, &numlen, &flags, NULL);
79072805
LW
2438 s += numlen;
2439 }
2440 else {
8bfdd7d9 2441 if (RsPARA(PL_rs)) {
7889fe52
NIS
2442 PL_ors_sv = newSVpvn("\n\n",2);
2443 }
2444 else {
8bfdd7d9 2445 PL_ors_sv = newSVsv(PL_rs);
c07a80fd 2446 }
79072805
LW
2447 }
2448 return s;
06492da6
SF
2449 case 'A':
2450 forbid_setid("-A");
930366bd
RGS
2451 if (!PL_preambleav)
2452 PL_preambleav = newAV();
06492da6 2453 if (*++s) {
930366bd 2454 SV *sv = newSVpvn("use assertions::activate split(/,/,q{",37);
06492da6
SF
2455 sv_catpv(sv,s);
2456 sv_catpv(sv,"})");
2457 s+=strlen(s);
06492da6
SF
2458 av_push(PL_preambleav, sv);
2459 }
2460 else
930366bd 2461 av_push(PL_preambleav, newSVpvn("use assertions::activate",24));
06492da6 2462 return s;
1a30305b 2463 case 'M':
bbce6d69 2464 forbid_setid("-M"); /* XXX ? */
1a30305b
PP
2465 /* FALL THROUGH */
2466 case 'm':
bbce6d69 2467 forbid_setid("-m"); /* XXX ? */
1a30305b 2468 if (*++s) {
a5f75d66 2469 char *start;
11343788 2470 SV *sv;
a5f75d66
AD
2471 char *use = "use ";
2472 /* -M-foo == 'no foo' */
2473 if (*s == '-') { use = "no "; ++s; }
11343788 2474 sv = newSVpv(use,0);
a5f75d66 2475 start = s;
1a30305b 2476 /* We allow -M'Module qw(Foo Bar)' */
c07a80fd
PP
2477 while(isALNUM(*s) || *s==':') ++s;
2478 if (*s != '=') {
11343788 2479 sv_catpv(sv, start);
c07a80fd
PP
2480 if (*(start-1) == 'm') {
2481 if (*s != '\0')
cea2e8a9 2482 Perl_croak(aTHX_ "Can't use '%c' after -mname", *s);
11343788 2483 sv_catpv( sv, " ()");
c07a80fd
PP
2484 }
2485 } else {
6df41af2 2486 if (s == start)
be98fb35
GS
2487 Perl_croak(aTHX_ "Module name required with -%c option",
2488 s[-1]);
11343788
MB
2489 sv_catpvn(sv, start, s-start);
2490 sv_catpv(sv, " split(/,/,q{");
2491 sv_catpv(sv, ++s);
2492 sv_catpv(sv, "})");
c07a80fd 2493 }
1a30305b 2494 s += strlen(s);
5c831c24 2495 if (!PL_preambleav)
3280af22
NIS
2496 PL_preambleav = newAV();
2497 av_push(PL_preambleav, sv);
1a30305b
PP
2498 }
2499 else
cea2e8a9 2500 Perl_croak(aTHX_ "No space allowed after -%c", *(s-1));
1a30305b 2501 return s;
79072805 2502 case 'n':
3280af22 2503 PL_minus_n = TRUE;
79072805
LW
2504 s++;
2505 return s;
2506 case 'p':
3280af22 2507 PL_minus_p = TRUE;
79072805
LW
2508 s++;
2509 return s;
2510 case 's':
bbce6d69 2511 forbid_setid("-s");
3280af22 2512 PL_doswitches = TRUE;
79072805
LW
2513 s++;
2514 return s;
6537fe72
MS
2515 case 't':
2516 if (!PL_tainting)
22f7c9c9 2517 TOO_LATE_FOR('t');
6537fe72
MS
2518 s++;
2519 return s;
463ee0b2 2520 case 'T':
3280af22 2521 if (!PL_tainting)
22f7c9c9 2522 TOO_LATE_FOR('T');
463ee0b2
LW
2523 s++;
2524 return s;
79072805 2525 case 'u':
bf4acbe4
GS
2526#ifdef MACOS_TRADITIONAL
2527 Perl_croak(aTHX_ "Believe me, you don't want to use \"-u\" on a Macintosh");
2528#endif
3280af22 2529 PL_do_undump = TRUE;
79072805
LW
2530 s++;
2531 return s;
2532 case 'U':
3280af22 2533 PL_unsafe = TRUE;
79072805
LW
2534 s++;
2535 return s;
2536 case 'v':
8e9464f1 2537#if !defined(DGUX)
b0e47665 2538 PerlIO_printf(PerlIO_stdout(),
d2560b70 2539 Perl_form(aTHX_ "\nThis is perl, v%"VDf" built for %s",
b0e47665 2540 PL_patchlevel, ARCHNAME));
8e9464f1
JH
2541#else /* DGUX */
2542/* Adjust verbose output as in the perl that ships with the DG/UX OS from EMC */
2543 PerlIO_printf(PerlIO_stdout(),
2544 Perl_form(aTHX_ "\nThis is perl, version %vd\n", PL_patchlevel));
2545 PerlIO_printf(PerlIO_stdout(),
2546 Perl_form(aTHX_ " built under %s at %s %s\n",
2547 OSNAME, __DATE__, __TIME__));
2548 PerlIO_printf(PerlIO_stdout(),
2549 Perl_form(aTHX_ " OS Specific Release: %s\n",
40a39f85 2550 OSVERS));
8e9464f1
JH
2551#endif /* !DGUX */
2552
fb73857a
PP
2553#if defined(LOCAL_PATCH_COUNT)
2554 if (LOCAL_PATCH_COUNT > 0)
b0e47665
GS
2555 PerlIO_printf(PerlIO_stdout(),
2556 "\n(with %d registered patch%s, "
2557 "see perl -V for more detail)",
2558 (int)LOCAL_PATCH_COUNT,
2559 (LOCAL_PATCH_COUNT!=1) ? "es" : "");
a5f75d66 2560#endif
1a30305b 2561
b0e47665 2562 PerlIO_printf(PerlIO_stdout(),
4c79ee7a 2563 "\n\nCopyright 1987-2003, Larry Wall\n");
eae9c151
JH
2564#ifdef MACOS_TRADITIONAL
2565 PerlIO_printf(PerlIO_stdout(),
be3c0a43 2566 "\nMac OS port Copyright 1991-2002, Matthias Neeracher;\n"
03765510 2567 "maintained by Chris Nandor\n");
eae9c151 2568#endif
79072805 2569#ifdef MSDOS
b0e47665
GS
2570 PerlIO_printf(PerlIO_stdout(),
2571 "\nMS-DOS port Copyright (c) 1989, 1990, Diomidis Spinellis\n");
55497cff
PP
2572#endif
2573#ifdef DJGPP
b0e47665
GS
2574 PerlIO_printf(PerlIO_stdout(),
2575 "djgpp v2 port (jpl5003c) by Hirofumi Watanabe, 1996\n"
2576 "djgpp v2 port (perl5004+) by Laszlo Molnar, 1997-1999\n");
4633a7c4 2577#endif
79072805 2578#ifdef OS2
b0e47665
GS
2579 PerlIO_printf(PerlIO_stdout(),
2580 "\n\nOS/2 port Copyright (c) 1990, 1991, Raymond Chen, Kai Uwe Rommel\n"
be3c0a43 2581 "Version 5 port Copyright (c) 1994-2002, Andreas Kaiser, Ilya Zakharevich\n");
79072805 2582#endif
79072805 2583#ifdef atarist
b0e47665
GS
2584 PerlIO_printf(PerlIO_stdout(),
2585 "atariST series port, ++jrb bammi@cadence.com\n");
79072805 2586#endif
a3f9223b 2587#ifdef __BEOS__
b0e47665
GS
2588 PerlIO_printf(PerlIO_stdout(),
2589 "BeOS port Copyright Tom Spindler, 1997-1999\n");
a3f9223b 2590#endif
1d84e8df 2591#ifdef MPE
b0e47665 2592 PerlIO_printf(PerlIO_stdout(),
be3c0a43 2593 "MPE/iX port Copyright by Mark Klein and Mark Bixby, 1996-2002\n");
1d84e8df 2594#endif
9d116dd7 2595#ifdef OEMVS
b0e47665
GS
2596 PerlIO_printf(PerlIO_stdout(),
2597 "MVS (OS390) port by Mortice Kern Systems, 1997-1999\n");
9d116dd7 2598#endif
495c5fdc 2599#ifdef __VOS__
b0e47665 2600 PerlIO_printf(PerlIO_stdout(),
94efb9fb 2601 "Stratus VOS port by Paul.Green@stratus.com, 1997-2002\n");
495c5fdc 2602#endif
092bebab 2603#ifdef __OPEN_VM
b0e47665
GS
2604 PerlIO_printf(PerlIO_stdout(),
2605 "VM/ESA port by Neale Ferguson, 1998-1999\n");
092bebab 2606#endif
a1a0e61e 2607#ifdef POSIX_BC
b0e47665
GS
2608 PerlIO_printf(PerlIO_stdout(),
2609 "BS2000 (POSIX) port by Start Amadeus GmbH, 1998-1999\n");
a1a0e61e 2610#endif
61ae2fbf 2611#ifdef __MINT__
b0e47665
GS
2612 PerlIO_printf(PerlIO_stdout(),
2613 "MiNT port by Guido Flohr, 1997-1999\n");
61ae2fbf 2614#endif
f83d2536 2615#ifdef EPOC
b0e47665 2616 PerlIO_printf(PerlIO_stdout(),
be3c0a43 2617 "EPOC port by Olaf Flebbe, 1999-2002\n");
f83d2536 2618#endif
e1caacb4 2619#ifdef UNDER_CE
b475b3e6
JH
2620 PerlIO_printf(PerlIO_stdout(),"WINCE port by Rainer Keuchel, 2001-2002\n");
2621 PerlIO_printf(PerlIO_stdout(),"Built on " __DATE__ " " __TIME__ "\n\n");
e1caacb4
JH
2622 wce_hitreturn();
2623#endif
baed7233
DL
2624#ifdef BINARY_BUILD_NOTICE
2625 BINARY_BUILD_NOTICE;
2626#endif
b0e47665
GS
2627 PerlIO_printf(PerlIO_stdout(),
2628 "\n\
79072805 2629Perl may be copied only under the terms of either the Artistic License or the\n\
3d6f292d 2630GNU General Public License, which may be found in the Perl 5 source kit.\n\n\
95103687
GS
2631Complete documentation for Perl, including FAQ lists, should be found on\n\
2632this system using `man perl' or `perldoc perl'. If you have access to the\n\
2633Internet, point your browser at http://www.perl.com/, the Perl Home Page.\n\n");
7ca617d0 2634 my_exit(0);
79072805 2635 case 'w':
599cee73 2636 if (! (PL_dowarn & G_WARN_ALL_MASK))
ac27b0f5 2637 PL_dowarn |= G_WARN_ON;
599cee73
PM
2638 s++;
2639 return s;
2640 case 'W':
ac27b0f5 2641 PL_dowarn = G_WARN_ALL_ON|G_WARN_ON;
317ea90d
MS
2642 if (!specialWARN(PL_compiling.cop_warnings))
2643 SvREFCNT_dec(PL_compiling.cop_warnings);
d3a7d8c7 2644 PL_compiling.cop_warnings = pWARN_ALL ;
599cee73
PM
2645 s++;
2646 return s;
2647 case 'X':
ac27b0f5 2648 PL_dowarn = G_WARN_ALL_OFF;
317ea90d
MS
2649 if (!specialWARN(PL_compiling.cop_warnings))
2650 SvREFCNT_dec(PL_compiling.cop_warnings);
d3a7d8c7 2651 PL_compiling.cop_warnings = pWARN_NONE ;
79072805
LW
2652 s++;
2653 return s;
a0d0e21e 2654 case '*':
79072805
LW
2655 case ' ':
2656 if (s[1] == '-') /* Additional switches on #! line. */
2657 return s+2;
2658 break;
a0d0e21e 2659 case '-':
79072805 2660 case 0:
51882d45 2661#if defined(WIN32) || !defined(PERL_STRICT_CR)
a868473f
NIS
2662 case '\r':
2663#endif
79072805
LW
2664 case '\n':
2665 case '\t':
2666 break;
aa689395
PP
2667#ifdef ALTERNATE_SHEBANG
2668 case 'S': /* OS/2 needs -S on "extproc" line. */
2669 break;
2670#endif
a0d0e21e 2671 case 'P':
3280af22 2672 if (PL_preprocess)
a0d0e21e
LW
2673 return s+1;
2674 /* FALL THROUGH */
79072805 2675 default:
cea2e8a9 2676 Perl_croak(aTHX_ "Can't emulate -%.1s on #! line",s);
79072805
LW
2677 }
2678 return Nullch;
2679}
2680
2681/* compliments of Tom Christiansen */
2682
2683/* unexec() can be found in the Gnu emacs distribution */
ee580363 2684/* Known to work with -DUNEXEC and using unexelf.c from GNU emacs-20.2 */
79072805
LW
2685
2686void
864dbfa3 2687Perl_my_unexec(pTHX)
79072805
LW
2688{
2689#ifdef UNEXEC
46fc3d4c
PP
2690 SV* prog;
2691 SV* file;
ee580363 2692 int status = 1;
79072805
LW
2693 extern int etext;
2694
ee580363 2695 prog = newSVpv(BIN_EXP, 0);
46fc3d4c 2696 sv_catpv(prog, "/perl");
6b88bc9c 2697 file = newSVpv(PL_origfilename, 0);
46fc3d4c 2698 sv_catpv(file, ".perldump");
79072805 2699
ee580363
GS
2700 unexec(SvPVX(file), SvPVX(prog), &etext, sbrk(0), 0);
2701 /* unexec prints msg to stderr in case of failure */
6ad3d225 2702 PerlProc_exit(status);
79072805 2703#else
a5f75d66
AD
2704# ifdef VMS
2705# include <lib$routines.h>
2706 lib$signal(SS$_DEBUG); /* ssdef.h #included from vmsish.h */
aa689395 2707# else
79072805 2708 ABORT(); /* for use with undump */
aa689395 2709# endif
a5f75d66 2710#endif
79072805
LW
2711}
2712
cb68f92d
GS
2713/* initialize curinterp */
2714STATIC void
cea2e8a9 2715S_init_interp(pTHX)
cb68f92d
GS
2716{
2717
acfe0abc
GS
2718#ifdef MULTIPLICITY
2719# define PERLVAR(var,type)
2720# define PERLVARA(var,n,type)
2721# if defined(PERL_IMPLICIT_CONTEXT)
2722# if defined(USE_5005THREADS)
2723# define PERLVARI(var,type,init) PERL_GET_INTERP->var = init;
c5be433b 2724# define PERLVARIC(var,type,init) PERL_GET_INTERP->var = init;
acfe0abc
GS
2725# else /* !USE_5005THREADS */
2726# define PERLVARI(var,type,init) aTHX->var = init;
2727# define PERLVARIC(var,type,init) aTHX->var = init;
2728# endif /* USE_5005THREADS */
3967c732 2729# else
acfe0abc
GS
2730# define PERLVARI(var,type,init) PERL_GET_INTERP->var = init;
2731# define PERLVARIC(var,type,init) PERL_GET_INTERP->var = init;
066ef5b5 2732# endif
acfe0abc
GS
2733# include "intrpvar.h"
2734# ifndef USE_5005THREADS
2735# include "thrdvar.h"
2736# endif
2737# undef PERLVAR
2738# undef PERLVARA
2739# undef PERLVARI
2740# undef PERLVARIC
2741#else
2742# define PERLVAR(var,type)
2743# define PERLVARA(var,n,type)
2744# define PERLVARI(var,type,init) PL_##var = init;
2745# define PERLVARIC(var,type,init) PL_##var = init;
2746# include "intrpvar.h"
2747# ifndef USE_5005THREADS
2748# include "thrdvar.h"
2749# endif
2750# undef PERLVAR
2751# undef PERLVARA
2752# undef PERLVARI
2753# undef PERLVARIC
cb68f92d
GS
2754#endif
2755
cb68f92d
GS
2756}
2757
76e3520e 2758STATIC void
cea2e8a9 2759S_init_main_stash(pTHX)
79072805 2760{
463ee0b2 2761 GV *gv;
6e72f9df 2762
3280af22 2763 PL_curstash = PL_defstash = newHV();
79cb57f6 2764 PL_curstname = newSVpvn("main",4);
adbc6bb1
LW
2765 gv = gv_fetchpv("main::",TRUE, SVt_PVHV);
2766 SvREFCNT_dec(GvHV(gv));
3280af22 2767 GvHV(gv) = (HV*)SvREFCNT_inc(PL_defstash);
463ee0b2 2768 SvREADONLY_on(gv);
3280af22
NIS
2769 HvNAME(PL_defstash) = savepv("main");
2770 PL_incgv = gv_HVadd(gv_AVadd(gv_fetchpv("INC",TRUE, SVt_PVAV)));
2771 GvMULTI_on(PL_incgv);
2772 PL_hintgv = gv_fetchpv("\010",TRUE, SVt_PV); /* ^H */
2773 GvMULTI_on(PL_hintgv);
2774 PL_defgv = gv_fetchpv("_",TRUE, SVt_PVAV);
2775 PL_errgv = gv_HVadd(gv_fetchpv("@", TRUE, SVt_PV));
2776 GvMULTI_on(PL_errgv);
2777 PL_replgv = gv_fetchpv("\022", TRUE, SVt_PV); /* ^R */
2778 GvMULTI_on(PL_replgv);
cea2e8a9 2779 (void)Perl_form(aTHX_ "%240s",""); /* Preallocate temp - for immediate signals. */
38a03e6e
MB
2780 sv_grow(ERRSV, 240); /* Preallocate - for immediate signals. */
2781 sv_setpvn(ERRSV, "", 0);
3280af22 2782 PL_curstash = PL_defstash;
11faa288 2783 CopSTASH_set(&PL_compiling, PL_defstash);
ed094faf 2784 PL_debstash = GvHV(gv_fetchpv("DB::", GV_ADDMULTI, SVt_PVHV));
3280af22 2785 PL_globalstash = GvHV(gv_fetchpv("CORE::GLOBAL::", GV_ADDMULTI, SVt_PVHV));
4633a7c4 2786 /* We must init $/ before switches are processed. */
864dbfa3 2787 sv_setpvn(get_sv("/", TRUE), "\n", 1);
79072805
LW
2788}
2789
76e3520e 2790STATIC void
cea2e8a9 2791S_open_script(pTHX_ char *scriptname, bool dosearch, SV *sv, int *fdscript)
79072805 2792{
1b24ed4b
MS
2793 char *quote;
2794 char *code;
2795 char *cpp_discard_flag;
2796 char *perl;
2797
6c4ab083 2798 *fdscript = -1;
79072805 2799
3280af22
NIS
2800 if (PL_e_script) {
2801 PL_origfilename = savepv("-e");
96436eeb 2802 }
6c4ab083
GS
2803 else {
2804 /* if find_script() returns, it returns a malloc()-ed value */
3280af22 2805 PL_origfilename = scriptname = find_script(scriptname, dosearch, NULL, 1);
6c4ab083
GS
2806
2807 if (strnEQ(scriptname, "/dev/fd/", 8) && isDIGIT(scriptname[8]) ) {
2808 char *s = scriptname + 8;
2809 *fdscript = atoi(s);
2810 while (isDIGIT(*s))
2811 s++;
2812 if (*s) {
2813 scriptname = savepv(s + 1);
3280af22
NIS
2814 Safefree(PL_origfilename);
2815 PL_origfilename = scriptname;
6c4ab083
GS
2816 }
2817 }
2818 }
2819
05ec9bb3 2820 CopFILE_free(PL_curcop);
57843af0 2821 CopFILE_set(PL_curcop, PL_origfilename);
3280af22 2822 if (strEQ(PL_origfilename,"-"))
79072805 2823 scriptname = "";
01f988be 2824 if (*fdscript >= 0) {
3280af22 2825 PL_rsfp = PerlIO_fdopen(*fdscript,PERL_SCRIPT_MODE);
1b24ed4b
MS
2826# if defined(HAS_FCNTL) && defined(F_SETFD)
2827 if (PL_rsfp)
2828 /* ensure close-on-exec */
2829 fcntl(PerlIO_fileno(PL_rsfp),F_SETFD,1);
2830# endif
96436eeb 2831 }
3280af22 2832 else if (PL_preprocess) {
46fc3d4c 2833 char *cpp_cfg = CPPSTDIN;
79cb57f6 2834 SV *cpp = newSVpvn("",0);
46fc3d4c
PP
2835 SV *cmd = NEWSV(0,0);
2836
2837 if (strEQ(cpp_cfg, "cppstdin"))
cea2e8a9 2838 Perl_sv_catpvf(aTHX_ cpp, "%s/", BIN_EXP);
46fc3d4c 2839 sv_catpv(cpp, cpp_cfg);
79072805 2840
1b24ed4b
MS
2841# ifndef VMS
2842 sv_catpvn(sv, "-I", 2);
2843 sv_catpv(sv,PRIVLIB_EXP);
2844# endif
46fc3d4c 2845
14953ddc
MB
2846 DEBUG_P(PerlIO_printf(Perl_debug_log,
2847 "PL_preprocess: scriptname=\"%s\", cpp=\"%s\", sv=\"%s\", CPPMINUS=\"%s\"\n",
2848 scriptname, SvPVX (cpp), SvPVX (sv), CPPMINUS));
1b24ed4b
MS
2849
2850# if defined(MSDOS) || defined(WIN32) || defined(VMS)
2851 quote = "\"";
2852# else
2853 quote = "'";
2854# endif
2855
2856# ifdef VMS
2857 cpp_discard_flag = "";
2858# else
2859 cpp_discard_flag = "-C";
2860# endif
2861
2862# ifdef OS2
2863 perl = os2_execname(aTHX);
2864# else
2865 perl = PL_origargv[0];
2866# endif
2867
2868
2869 /* This strips off Perl comments which might interfere with
62375a60
NIS
2870 the C pre-processor, including #!. #line directives are
2871 deliberately stripped to avoid confusion with Perl's version
1b24ed4b
MS
2872 of #line. FWP played some golf with it so it will fit
2873 into VMS's 255 character buffer.
2874 */
2875 if( PL_doextract )
2876 code = "(1../^#!.*perl/i)|/^\\s*#(?!\\s*((ifn?|un)def|(el|end)?if|define|include|else|error|pragma)\\b)/||!($|=1)||print";
2877 else
2878 code = "/^\\s*#(?!\\s*((ifn?|un)def|(el|end)?if|define|include|else|error|pragma)\\b)/||!($|=1)||print";
2879
2880 Perl_sv_setpvf(aTHX_ cmd, "\
2881%s -ne%s%s%s %s | %"SVf" %s %"SVf" %s",
62375a60 2882 perl, quote, code, quote, scriptname, cpp,
1b24ed4b
MS
2883 cpp_discard_flag, sv, CPPMINUS);
2884
3280af22 2885 PL_doextract = FALSE;
1b24ed4b
MS
2886# ifdef IAMSUID /* actually, this is caught earlier */
2887 if (PL_euid != PL_uid && !PL_euid) { /* if running suidperl */
2888# ifdef HAS_SETEUID
2889 (void)seteuid(PL_uid); /* musn't stay setuid root */
2890# else
2891# ifdef HAS_SETREUID
2892 (void)setreuid((Uid_t)-1, PL_uid);
2893# else
2894# ifdef HAS_SETRESUID
2895 (void)setresuid((Uid_t)-1, PL_uid, (Uid_t)-1);
2896# else
2897 PerlProc_setuid(PL_uid);
2898# endif
2899# endif
2900# endif
b28d0864 2901 if (PerlProc_geteuid() != PL_uid)
cea2e8a9 2902 Perl_croak(aTHX_ "Can't do seteuid!\n");
79072805 2903 }
1b24ed4b 2904# endif /* IAMSUID */
0a6c758d 2905
62375a60
NIS
2906 DEBUG_P(PerlIO_printf(Perl_debug_log,
2907 "PL_preprocess: cmd=\"%s\"\n",
0a6c758d
MS
2908 SvPVX(cmd)));
2909
3280af22 2910 PL_rsfp = PerlProc_popen(SvPVX(cmd), "r");
46fc3d4c
PP
2911 SvREFCNT_dec(cmd);
2912 SvREFCNT_dec(cpp);
79072805
LW
2913 }
2914 else if (!*scriptname) {
bbce6d69 2915 forbid_setid("program input from stdin");
3280af22 2916 PL_rsfp = PerlIO_stdin();
79072805 2917 }
96436eeb 2918 else {
3280af22 2919 PL_rsfp = PerlIO_open(scriptname,PERL_SCRIPT_MODE);
1b24ed4b
MS
2920# if defined(HAS_FCNTL) && defined(F_SETFD)
2921 if (PL_rsfp)
2922 /* ensure close-on-exec */
2923 fcntl(PerlIO_fileno(PL_rsfp),F_SETFD,1);
2924# endif
96436eeb 2925 }
3280af22 2926 if (!PL_rsfp) {
1b24ed4b
MS
2927# ifdef DOSUID
2928# ifndef IAMSUID /* in case script is not readable before setuid */
2929 if (PL_euid &&
2930 PerlLIO_stat(CopFILE(PL_curcop),&PL_statbuf) >= 0 &&
2931 PL_statbuf.st_mode & (S_ISUID|S_ISGID))
2932 {
2933 /* try again */
62375a60
NIS
2934 PerlProc_execv(Perl_form(aTHX_ "%s/sperl"PERL_FS_VER_FMT,
2935 BIN_EXP, (int)PERL_REVISION,
1b24ed4b
MS
2936 (int)PERL_VERSION,
2937 (int)PERL_SUBVERSION), PL_origargv);
2938 Perl_croak(aTHX_ "Can't do setuid\n");
2939 }
2940# endif
2941# endif
2942# ifdef IAMSUID
2943 errno = EPERM;
2944 Perl_croak(aTHX_ "Can't open perl script: %s\n",
2945 Strerror(errno));
2946# else
2947 Perl_croak(aTHX_ "Can't open perl script \"%s\": %s\n",
2948 CopFILE(PL_curcop), Strerror(errno));
2949# endif
13281fa4 2950 }
79072805 2951}
8d063cd8 2952
7b89560d
JH
2953/* Mention
2954 * I_SYSSTATVFS HAS_FSTATVFS
2955 * I_SYSMOUNT
c890dc6c 2956 * I_STATFS HAS_FSTATFS HAS_GETFSSTAT
7b89560d
JH
2957 * I_MNTENT HAS_GETMNTENT HAS_HASMNTOPT
2958 * here so that metaconfig picks them up. */
2959
104d25b7 2960#ifdef IAMSUID
864dbfa3 2961STATIC int
e688b231 2962S_fd_on_nosuid_fs(pTHX_ int fd)
104d25b7 2963{
0545a864
JH
2964 int check_okay = 0; /* able to do all the required sys/libcalls */
2965 int on_nosuid = 0; /* the fd is on a nosuid fs */
104d25b7 2966/*
ad27e871 2967 * Preferred order: fstatvfs(), fstatfs(), ustat()+getmnt(), getmntent().
e688b231 2968 * fstatvfs() is UNIX98.
0545a864 2969 * fstatfs() is 4.3 BSD.
ad27e871 2970 * ustat()+getmnt() is pre-4.3 BSD.
0545a864
JH
2971 * getmntent() is O(number-of-mounted-filesystems) and can hang on
2972 * an irrelevant filesystem while trying to reach the right one.
104d25b7
JH
2973 */
2974
6439433f
JH
2975#undef FD_ON_NOSUID_CHECK_OKAY /* found the syscalls to do the check? */
2976
2977# if !defined(FD_ON_NOSUID_CHECK_OKAY) && \
2978 defined(HAS_FSTATVFS)
2979# define FD_ON_NOSUID_CHECK_OKAY
104d25b7 2980 struct statvfs stfs;
6439433f 2981
104d25b7
JH
2982 check_okay = fstatvfs(fd, &stfs) == 0;
2983 on_nosuid = check_okay && (stfs.f_flag & ST_NOSUID);
6439433f 2984# endif /* fstatvfs */
ac27b0f5 2985
6439433f
JH
2986# if !defined(FD_ON_NOSUID_CHECK_OKAY) && \
2987 defined(PERL_MOUNT_NOSUID) && \
2988 defined(HAS_FSTATFS) && \
2989 defined(HAS_STRUCT_STATFS) && \
2990 defined(HAS_STRUCT_STATFS_F_FLAGS)
2991# define FD_ON_NOSUID_CHECK_OKAY
e688b231 2992 struct statfs stfs;
6439433f 2993
104d25b7 2994 check_okay = fstatfs(fd, &stfs) == 0;
104d25b7 2995 on_nosuid = check_okay && (stfs.f_flags & PERL_MOUNT_NOSUID);
6439433f
JH
2996# endif /* fstatfs */
2997
2998# if !defined(FD_ON_NOSUID_CHECK_OKAY) && \
2999 defined(PERL_MOUNT_NOSUID) && \
3000 defined(HAS_FSTAT) && \
3001 defined(HAS_USTAT) && \
3002 defined(HAS_GETMNT) && \
3003 defined(HAS_STRUCT_FS_DATA) && \
3004 defined(NOSTAT_ONE)
3005# define FD_ON_NOSUID_CHECK_OKAY
c623ac67 3006 Stat_t fdst;
6439433f 3007
0545a864 3008 if (fstat(fd, &fdst) == 0) {
6439433f
JH
3009 struct ustat us;
3010 if (ustat(fdst.st_dev, &us) == 0) {
3011 struct fs_data fsd;
3012 /* NOSTAT_ONE here because we're not examining fields which
3013 * vary between that case and STAT_ONE. */
ad27e871 3014 if (getmnt((int*)0, &fsd, (int)0, NOSTAT_ONE, us.f_fname) == 0) {
6439433f
JH
3015 size_t cmplen = sizeof(us.f_fname);
3016 if (sizeof(fsd.fd_req.path) < cmplen)
3017 cmplen = sizeof(fsd.fd_req.path);
3018 if (strnEQ(fsd.fd_req.path, us.f_fname, cmplen) &&
3019 fdst.st_dev == fsd.fd_req.dev) {
3020 check_okay = 1;
3021 on_nosuid = fsd.fd_req.flags & PERL_MOUNT_NOSUID;
3022 }
3023 }
3024 }
3025 }
0545a864 3026 }
6439433f
JH
3027# endif /* fstat+ustat+getmnt */
3028
3029# if !defined(FD_ON_NOSUID_CHECK_OKAY) && \
3030 defined(HAS_GETMNTENT) && \
3031 defined(HAS_HASMNTOPT) && \
3032 defined(MNTOPT_NOSUID)
3033# define FD_ON_NOSUID_CHECK_OKAY
3034 FILE *mtab = fopen("/etc/mtab", "r");
3035 struct mntent *entry;
c623ac67 3036 Stat_t stb, fsb;
104d25b7
JH
3037
3038 if (mtab && (fstat(fd, &stb) == 0)) {
6439433f
JH
3039 while (entry = getmntent(mtab)) {
3040 if (stat(entry->mnt_dir, &fsb) == 0
3041 && fsb.st_dev == stb.st_dev)
3042 {
3043 /* found the filesystem */
3044 check_okay = 1;
3045 if (hasmntopt(entry, MNTOPT_NOSUID))
3046 on_nosuid = 1;
3047 break;
3048 } /* A single fs may well fail its stat(). */
3049 }
104d25b7
JH
3050 }
3051 if (mtab)
6439433f
JH
3052 fclose(mtab);
3053# endif /* getmntent+hasmntopt */
0545a864 3054
ac27b0f5 3055 if (!check_okay)
0545a864 3056 Perl_croak(aTHX_ "Can't check filesystem of script \"%s\" for nosuid", PL_origfilename);
104d25b7
JH
3057 return on_nosuid;
3058}
3059#endif /* IAMSUID */
3060
76e3520e 3061STATIC void
cea2e8a9 3062S_validate_suid(pTHX_ char *validarg, char *scriptname, int fdscript)
79072805 3063{
155aba94 3064#ifdef IAMSUID
96436eeb 3065 int which;
155aba94 3066#endif
96436eeb 3067
13281fa4
LW
3068 /* do we need to emulate setuid on scripts? */
3069
3070 /* This code is for those BSD systems that have setuid #! scripts disabled
3071 * in the kernel because of a security problem. Merely defining DOSUID
3072 * in perl will not fix that problem, but if you have disabled setuid
3073 * scripts in the kernel, this will attempt to emulate setuid and setgid
3074 * on scripts that have those now-otherwise-useless bits set. The setuid
27e2fb84
LW
3075 * root version must be called suidperl or sperlN.NNN. If regular perl
3076 * discovers that it has opened a setuid script, it calls suidperl with
3077 * the same argv that it had. If suidperl finds that the script it has
3078 * just opened is NOT setuid root, it sets the effective uid back to the
3079 * uid. We don't just make perl setuid root because that loses the
3080 * effective uid we had before invoking perl, if it was different from the
3081 * uid.
13281fa4
LW
3082 *
3083 * DOSUID must be defined in both perl and suidperl, and IAMSUID must
3084 * be defined in suidperl only. suidperl must be setuid root. The
3085 * Configure script will set this up for you if you want it.
3086 */
a687059c 3087
13281fa4 3088#ifdef DOSUID
6e72f9df 3089 char *s, *s2;
a0d0e21e 3090
b28d0864 3091 if (PerlLIO_fstat(PerlIO_fileno(PL_rsfp),&PL_statbuf) < 0) /* normal stat is insecure */
cea2e8a9 3092 Perl_croak(aTHX_ "Can't stat script \"%s\"",PL_origfilename);
b28d0864 3093 if (fdscript < 0 && PL_statbuf.st_mode & (S_ISUID|S_ISGID)) {
79072805 3094 I32 len;
2d8e6c8d 3095 STRLEN n_a;
13281fa4 3096
a687059c 3097#ifdef IAMSUID
fe14fcc3 3098#ifndef HAS_SETREUID
a687059c
LW
3099 /* On this access check to make sure the directories are readable,
3100 * there is actually a small window that the user could use to make
3101 * filename point to an accessible directory. So there is a faint
3102 * chance that someone could execute a setuid script down in a
3103 * non-accessible directory. I don't know what to do about that.
3104 * But I don't think it's too important. The manual lies when
3105 * it says access() is useful in setuid programs.
3106 */
cc49e20b 3107 if (PerlLIO_access(CopFILE(PL_curcop),1)) /*double check*/
cea2e8a9 3108 Perl_croak(aTHX_ "Permission denied");
a687059c
LW
3109#else
3110 /* If we can swap euid and uid, then we can determine access rights
3111 * with a simple stat of the file, and then compare device and
3112 * inode to make sure we did stat() on the same file we opened.
3113 * Then we just have to make sure he or she can execute it.
3114 */
3115 {
c623ac67 3116 Stat_t tmpstatbuf;
a687059c 3117
85e6fe83
LW
3118 if (
3119#ifdef HAS_SETREUID
b28d0864 3120 setreuid(PL_euid,PL_uid) < 0
a0d0e21e
LW
3121#else
3122# if HAS_SETRESUID
b28d0864 3123 setresuid(PL_euid,PL_uid,(Uid_t)-1) < 0
a0d0e21e 3124# endif
85e6fe83 3125#endif
b28d0864 3126 || PerlProc_getuid() != PL_euid || PerlProc_geteuid() != PL_uid)
cea2e8a9 3127 Perl_croak(aTHX_ "Can't swap uid and euid"); /* really paranoid */
cc49e20b 3128 if (PerlLIO_stat(CopFILE(PL_curcop),&tmpstatbuf) < 0)
cea2e8a9 3129 Perl_croak(aTHX_ "Permission denied"); /* testing full pathname here */
2bb3463c 3130#if defined(IAMSUID) && !defined(NO_NOSUID_CHECK)
e688b231 3131 if (fd_on_nosuid_fs(PerlIO_fileno(PL_rsfp)))
cea2e8a9 3132 Perl_croak(aTHX_ "Permission denied");
104d25b7 3133#endif
b28d0864
NIS
3134 if (tmpstatbuf.st_dev != PL_statbuf.st_dev ||
3135 tmpstatbuf.st_ino != PL_statbuf.st_ino) {
3136 (void)PerlIO_close(PL_rsfp);
cea2e8a9 3137 Perl_croak(aTHX_ "Permission denied\n");
a687059c 3138 }
85e6fe83
LW
3139 if (
3140#ifdef HAS_SETREUID
b28d0864 3141 setreuid(PL_uid,PL_euid) < 0
a0d0e21e
LW
3142#else
3143# if defined(HAS_SETRESUID)
b28d0864 3144 setresuid(PL_uid,PL_euid,(Uid_t)-1) < 0
a0d0e21e 3145# endif
85e6fe83 3146#endif
b28d0864 3147 || PerlProc_getuid() != PL_uid || PerlProc_geteuid() != PL_euid)
cea2e8a9 3148 Perl_croak(aTHX_ "Can't reswap uid and euid");
b28d0864 3149 if (!cando(S_IXUSR,FALSE,&PL_statbuf)) /* can real uid exec? */
cea2e8a9 3150 Perl_croak(aTHX_ "Permission denied\n");
a687059c 3151 }
fe14fcc3 3152#endif /* HAS_SETREUID */
a687059c
LW
3153#endif /* IAMSUID */
3154
b28d0864 3155 if (!S_ISREG(PL_statbuf.st_mode))
cea2e8a9 3156 Perl_croak(aTHX_ "Permission denied");
b28d0864 3157 if (PL_statbuf.st_mode & S_IWOTH)
cea2e8a9 3158 Perl_croak(aTHX_ "Setuid/gid script is writable by world");
6b88bc9c 3159 PL_doswitches = FALSE; /* -s is insecure in suid */
57843af0 3160 CopLINE_inc(PL_curcop);
6b88bc9c 3161 if (sv_gets(PL_linestr, PL_rsfp, 0) == Nullch ||
2d8e6c8d 3162 strnNE(SvPV(PL_linestr,n_a),"#!",2) ) /* required even on Sys V */
cea2e8a9 3163 Perl_croak(aTHX_ "No #! line");
2d8e6c8d 3164 s = SvPV(PL_linestr,n_a)+2;
663a0e37 3165 if (*s == ' ') s++;
45d8adaa 3166 while (!isSPACE(*s)) s++;
2d8e6c8d 3167 for (s2 = s; (s2 > SvPV(PL_linestr,n_a)+2 &&
6e72f9df
PP
3168 (isDIGIT(s2[-1]) || strchr("._-", s2[-1]))); s2--) ;
3169 if (strnNE(s2-4,"perl",4) && strnNE(s-9,"perl",4)) /* sanity check */
cea2e8a9 3170 Perl_croak(aTHX_ "Not a perl script");
a687059c 3171 while (*s == ' ' || *s == '\t') s++;
13281fa4
LW
3172 /*
3173 * #! arg must be what we saw above. They can invoke it by
3174 * mentioning suidperl explicitly, but they may not add any strange
3175 * arguments beyond what #! says if they do invoke suidperl that way.
3176 */
3177 len = strlen(validarg);
3178 if (strEQ(validarg," PHOOEY ") ||
45d8adaa 3179 strnNE(s,validarg,len) || !isSPACE(s[len]))
cea2e8a9 3180 Perl_croak(aTHX_ "Args must match #! line");
a687059c
LW
3181
3182#ifndef IAMSUID
b28d0864
NIS
3183 if (PL_euid != PL_uid && (PL_statbuf.st_mode & S_ISUID) &&
3184 PL_euid == PL_statbuf.st_uid)
3185 if (!PL_do_undump)
cea2e8a9 3186 Perl_croak(aTHX_ "YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
a687059c
LW
3187FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
3188#endif /* IAMSUID */
13281fa4 3189
b28d0864
NIS
3190 if (PL_euid) { /* oops, we're not the setuid root perl */
3191 (void)PerlIO_close(PL_rsfp);
13281fa4 3192#ifndef IAMSUID
46fc3d4c 3193 /* try again */
a7cb1f99 3194 PerlProc_execv(Perl_form(aTHX_ "%s/sperl"PERL_FS_VER_FMT, BIN_EXP,
273cf8d1
GS
3195 (int)PERL_REVISION, (int)PERL_VERSION,
3196 (int)PERL_SUBVERSION), PL_origargv);
13281fa4 3197#endif
cea2e8a9 3198 Perl_croak(aTHX_ "Can't do setuid\n");
13281fa4
LW
3199 }
3200
b28d0864 3201 if (PL_statbuf.st_mode & S_ISGID && PL_statbuf.st_gid != PL_egid) {
fe14fcc3 3202#ifdef HAS_SETEGID
b28d0864 3203 (void)setegid(PL_statbuf.st_gid);
a687059c 3204#else
fe14fcc3 3205#ifdef HAS_SETREGID
b28d0864 3206 (void)setregid((Gid_t)-1,PL_statbuf.st_gid);
85e6fe83
LW
3207#else
3208#ifdef HAS_SETRESGID
b28d0864 3209 (void)setresgid((Gid_t)-1,PL_statbuf.st_gid,(Gid_t)-1);
a687059c 3210#else
b28d0864 3211 PerlProc_setgid(PL_statbuf.st_gid);
a687059c
LW
3212#endif
3213#endif
85e6fe83 3214#endif
b28d0864 3215 if (PerlProc_getegid() != PL_statbuf.st_gid)
cea2e8a9 3216 Perl_croak(aTHX_ "Can't do setegid!\n");
83025b21 3217 }
b28d0864
NIS
3218 if (PL_statbuf.st_mode & S_ISUID) {
3219 if (PL_statbuf.st_uid != PL_euid)
fe14fcc3 3220#ifdef HAS_SETEUID
b28d0864 3221 (void)seteuid(PL_statbuf.st_uid); /* all that for this */
a687059c 3222#else
fe14fcc3 3223#ifdef HAS_SETREUID
b28d0864 3224 (void)setreuid((Uid_t)-1,PL_statbuf.st_uid);
85e6fe83
LW
3225#else
3226#ifdef HAS_SETRESUID
b28d0864 3227 (void)setresuid((Uid_t)-1,PL_statbuf.st_uid,(Uid_t)-1);
a687059c 3228#else
b28d0864 3229 PerlProc_setuid(PL_statbuf.st_uid);
a687059c
LW
3230#endif
3231#endif
85e6fe83 3232#endif
b28d0864 3233 if (PerlProc_geteuid() != PL_statbuf.st_uid)
cea2e8a9 3234 Perl_croak(aTHX_ "Can't do seteuid!\n");
a687059c 3235 }
b28d0864 3236 else if (PL_uid) { /* oops, mustn't run as root */
fe14fcc3 3237#ifdef HAS_SETEUID
b28d0864 3238 (void)seteuid((Uid_t)PL_uid);
a687059c 3239#else
fe14fcc3 3240#ifdef HAS_SETREUID
b28d0864 3241 (void)setreuid((Uid_t)-1,(Uid_t)PL_uid);
a687059c 3242#else
85e6fe83 3243#ifdef HAS_SETRESUID
b28d0864 3244 (void)setresuid((Uid_t)-1,(Uid_t)PL_uid,(Uid_t)-1);
85e6fe83 3245#else
b28d0864 3246 PerlProc_setuid((Uid_t)PL_uid);
85e6fe83 3247#endif
a687059c
LW
3248#endif
3249#endif
b28d0864 3250 if (PerlProc_geteuid() != PL_uid)
cea2e8a9 3251 Perl_croak(aTHX_ "Can't do seteuid!\n");
83025b21 3252 }
748a9306 3253 init_ids();
b28d0864 3254 if (!cando(S_IXUSR,TRUE,&PL_statbuf))
cea2e8a9 3255 Perl_croak(aTHX_ "Permission denied\n"); /* they can't do this */
13281fa4
LW
3256 }
3257#ifdef IAMSUID
6b88bc9c 3258 else if (PL_preprocess)
cea2e8a9 3259 Perl_croak(aTHX_ "-P not allowed for setuid/setgid script\n");
96436eeb 3260 else if (fdscript >= 0)
cea2e8a9 3261 Perl_croak(aTHX_ "fd script not allowed in suidperl\n");
13281fa4 3262 else
cea2e8a9 3263 Perl_croak(aTHX_ "Script is not setuid/setgid in suidperl\n");
96436eeb
PP
3264
3265 /* We absolutely must clear out any saved ids here, so we */
3266 /* exec the real perl, substituting fd script for scriptname. */
3267 /* (We pass script name as "subdir" of fd, which perl will grok.) */
b28d0864
NIS
3268 PerlIO_rewind(PL_rsfp);
3269 PerlLIO_lseek(PerlIO_fileno(PL_rsfp),(Off_t)0,0); /* just in case rewind didn't */
6b88bc9c
GS
3270 for (which = 1; PL_origargv[which] && PL_origargv[which] != scriptname; which++) ;
3271 if (!PL_origargv[which])
cea2e8a9
GS
3272 Perl_croak(aTHX_ "Permission denied");
3273 PL_origargv[which] = savepv(Perl_form(aTHX_ "/dev/fd/%d/%s",
6b88bc9c 3274 PerlIO_fileno(PL_rsfp), PL_origargv[which]));
96436eeb 3275#if defined(HAS_FCNTL) && defined(F_SETFD)
b28d0864 3276 fcntl(PerlIO_fileno(PL_rsfp),F_SETFD,0); /* ensure no close-on-exec */
96436eeb 3277#endif
a7cb1f99 3278 PerlProc_execv(Perl_form(aTHX_ "%s/perl"PERL_FS_VER_FMT, BIN_EXP,
273cf8d1
GS
3279 (int)PERL_REVISION, (int)PERL_VERSION,
3280 (int)PERL_SUBVERSION), PL_origargv);/* try again */
cea2e8a9 3281 Perl_croak(aTHX_ "Can't do setuid\n");
13281fa4 3282#endif /* IAMSUID */
a687059c 3283#else /* !DOSUID */
3280af22 3284 if (PL_euid != PL_uid || PL_egid != PL_gid) { /* (suidperl doesn't exist, in fact) */
a687059c 3285#ifndef SETUID_SCRIPTS_ARE_SECURE_NOW
b28d0864
NIS
3286 PerlLIO_fstat(PerlIO_fileno(PL_rsfp),&PL_statbuf); /* may be either wrapped or real suid */
3287 if ((PL_euid != PL_uid && PL_euid == PL_statbuf.st_uid && PL_statbuf.st_mode & S_ISUID)
a687059c 3288 ||
b28d0864 3289 (PL_egid != PL_gid && PL_egid == PL_statbuf.st_gid && PL_statbuf.st_mode & S_ISGID)
a687059c 3290 )
b28d0864 3291 if (!PL_do_undump)
cea2e8a9 3292 Perl_croak(aTHX_ "YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
a687059c
LW
3293FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
3294#endif /* SETUID_SCRIPTS_ARE_SECURE_NOW */
3295 /* not set-id, must be wrapped */
a687059c 3296 }
13281fa4 3297#endif /* DOSUID */
79072805 3298}
13281fa4 3299
76e3520e 3300STATIC void
cea2e8a9 3301S_find_beginning(pTHX)
79072805 3302{
6e72f9df 3303 register char *s, *s2;
e55ac0fa
HS
3304#ifdef MACOS_TRADITIONAL
3305 int maclines = 0;
3306#endif
33b78306
LW
3307
3308 /* skip forward in input to the real script? */
3309
bbce6d69 3310 forbid_setid("-x");
bf4acbe4 3311#ifdef MACOS_TRADITIONAL
084592ab 3312 /* Since the Mac OS does not honor #! arguments for us, we do it ourselves */
ac27b0f5 3313
bf4acbe4
GS
3314 while (PL_doextract || gMacPerl_AlwaysExtract) {
3315 if ((s = sv_gets(PL_linestr, PL_rsfp, 0)) == Nullch) {
3316 if (!gMacPerl_AlwaysExtract)
3317 Perl_croak(aTHX_ "No Perl script found in input\n");
e55ac0fa 3318
bf4acbe4
GS
3319 if (PL_doextract) /* require explicit override ? */
3320 if (!OverrideExtract(PL_origfilename))
3321 Perl_croak(aTHX_ "User aborted script\n");
3322 else
3323 PL_doextract = FALSE;
e55ac0fa 3324
bf4acbe4
GS
3325 /* Pater peccavi, file does not have #! */
3326 PerlIO_rewind(PL_rsfp);
e55ac0fa 3327
bf4acbe4
GS
3328 break;
3329 }
3330#else
3280af22
NIS
3331 while (PL_doextract) {
3332 if ((s = sv_gets(PL_linestr, PL_rsfp, 0)) == Nullch)
cea2e8a9 3333 Perl_croak(aTHX_ "No Perl script found in input\n");
bf4acbe4 3334#endif
4f0c37ba
IZ
3335 s2 = s;
3336 if (*s == '#' && s[1] == '!' && ((s = instr(s,"perl")) || (s = instr(s2,"PERL")))) {
3280af22
NIS
3337 PerlIO_ungetc(PL_rsfp, '\n'); /* to keep line count right */
3338 PL_doextract = FALSE;
6e72f9df
PP
3339 while (*s && !(isSPACE (*s) || *s == '#')) s++;
3340 s2 = s;
3341 while (*s == ' ' || *s == '\t') s++;
3342 if (*s++ == '-') {
3343 while (isDIGIT(s2[-1]) || strchr("-._", s2[-1])) s2--;
3344 if (strnEQ(s2-4,"perl",4))
3345 /*SUPPRESS 530*/
155aba94
GS
3346 while ((s = moreswitches(s)))
3347 ;
33b78306 3348 }
95e8664e 3349#ifdef MACOS_TRADITIONAL
e55ac0fa
HS
3350 /* We are always searching for the #!perl line in MacPerl,
3351 * so if we find it, still keep the line count correct
3352 * by counting lines we already skipped over
3353 */
3354 for (; maclines > 0 ; maclines--)
3355 PerlIO_ungetc(PL_rsfp, '\n');
3356
95e8664e 3357 break;
e55ac0fa
HS
3358
3359 /* gMacPerl_AlwaysExtract is false in MPW tool */
3360 } else if (gMacPerl_AlwaysExtract) {
3361 ++maclines;
95e8664e 3362#endif
83025b21
LW
3363 }
3364 }
3365}
3366
afe37c7d 3367
76e3520e 3368STATIC void
cea2e8a9 3369S_init_ids(pTHX)
352d5a3a 3370{
d8eceb89
JH
3371 PL_uid = PerlProc_getuid();
3372 PL_euid = PerlProc_geteuid();
3373 PL_gid = PerlProc_getgid();
3374 PL_egid = PerlProc_getegid();
748a9306 3375#ifdef VMS
b28d0864
NIS
3376 PL_uid |= PL_gid << 16;
3377 PL_euid |= PL_egid << 16;
748a9306 3378#endif
22f7c9c9
JH
3379 /* Should not happen: */
3380 CHECK_MALLOC_TAINT(PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
3280af22 3381 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
748a9306 3382}
79072805 3383
a0643315
JH
3384/* This is used very early in the lifetime of the program,
3385 * before even the options are parsed, so PL_tainting has
af419de7
JH
3386 * not been initialized properly. The variable PL_earlytaint
3387 * is set early in main() to the result of this function. */
3388bool
22f7c9c9
JH
3389Perl_doing_taint(int argc, char *argv[], char *envp[])
3390{
af419de7 3391 int uid = PerlProc_getuid();
22f7c9c9 3392 int euid = PerlProc_geteuid();
af419de7 3393 int gid = PerlProc_getgid();
22f7c9c9
JH
3394 int egid = PerlProc_getegid();
3395
3396#ifdef VMS
af419de7 3397 uid |= gid << 16;
22f7c9c9
JH
3398 euid |= egid << 16;
3399#endif
3400 if (uid && (euid != uid || egid != gid))
3401 return 1;
af419de7
JH
3402 /* This is a really primitive check; environment gets ignored only
3403 * if -T are the first chars together; otherwise one gets
3404 * "Too late" message. */
22f7c9c9
JH
3405 if ( argc > 1 && argv[1][0] == '-'
3406 && (argv[1][1] == 't' || argv[1][1] == 'T') )
3407 return 1;
3408 return 0;
3409}
22f7c9c9 3410
76e3520e 3411STATIC void
cea2e8a9 3412S_forbid_setid(pTHX_ char *s)
bbce6d69 3413{
3280af22 3414 if (PL_euid != PL_uid)
cea2e8a9 3415 Perl_croak(aTHX_ "No %s allowed while running setuid", s);
3280af22 3416 if (PL_egid != PL_gid)
cea2e8a9 3417 Perl_croak(aTHX_ "No %s allowed while running setgid", s);
bbce6d69
PP
3418}
3419
1ee4443e
IZ
3420void
3421Perl_init_debugger(pTHX)
748a9306 3422{
1ee4443e
IZ
3423 HV *ostash = PL_curstash;
3424
3280af22
NIS
3425 PL_curstash = PL_debstash;
3426 PL_dbargs = GvAV(gv_AVadd((gv_fetchpv("args", GV_ADDMULTI, SVt_PVAV))));
3427 AvREAL_off(PL_dbargs);
3428 PL_DBgv = gv_fetchpv("DB", GV_ADDMULTI, SVt_PVGV);
3429 PL_DBline = gv_fetchpv("dbline", GV_ADDMULTI, SVt_PVAV);
3430 PL_DBsub = gv_HVadd(gv_fetchpv("sub", GV_ADDMULTI, SVt_PVHV));
1ee4443e 3431 sv_upgrade(GvSV(PL_DBsub), SVt_IV); /* IVX accessed if PERLDB_SUB_NN */
3280af22 3432 PL_DBsingle = GvSV((gv_fetchpv("single", GV_ADDMULTI, SVt_PV)));
ac27b0f5 3433 sv_setiv(PL_DBsingle, 0);
3280af22 3434 PL_DBtrace = GvSV((gv_fetchpv("trace", GV_ADDMULTI, SVt_PV)));
ac27b0f5 3435 sv_setiv(PL_DBtrace, 0);
3280af22 3436 PL_DBsignal = GvSV((gv_fetchpv("signal", GV_ADDMULTI, SVt_PV)));
ac27b0f5 3437 sv_setiv(PL_DBsignal, 0);
06492da6
SF
3438 PL_DBassertion = GvSV((gv_fetchpv("assertion", GV_ADDMULTI, SVt_PV)));
3439 sv_setiv(PL_DBassertion, 0);
1ee4443e 3440 PL_curstash = ostash;
352d5a3a
LW
3441}
3442
2ce36478
SM
3443#ifndef STRESS_REALLOC
3444#define REASONABLE(size) (size)
3445#else
3446#define REASONABLE(size) (1) /* unreasonable */
3447#endif
3448
11343788 3449void
cea2e8a9 3450Perl_init_stacks(pTHX)
79072805 3451{
e336de0d 3452 /* start with 128-item stack and 8K cxstack */
3280af22 3453 PL_curstackinfo = new_stackinfo(REASONABLE(128),
e336de0d 3454 REASONABLE(8192/sizeof(PERL_CONTEXT) - 1));
3280af22
NIS
3455 PL_curstackinfo->si_type = PERLSI_MAIN;
3456 PL_curstack = PL_curstackinfo->si_stack;
3457 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
79072805 3458
3280af22
NIS
3459 PL_stack_base = AvARRAY(PL_curstack);
3460 PL_stack_sp = PL_stack_base;
3461 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
8990e307 3462
3280af22
NIS
3463 New(50,PL_tmps_stack,REASONABLE(128),SV*);
3464 PL_tmps_floor = -1;
3465 PL_tmps_ix = -1;
3466 PL_tmps_max = REASONABLE(128);
8990e307 3467
3280af22
NIS
3468 New(54,PL_markstack,REASONABLE(32),I32);
3469 PL_markstack_ptr = PL_markstack;
3470 PL_markstack_max = PL_markstack + REASONABLE(32);
79072805 3471
ce2f7c3b 3472 SET_MARK_OFFSET;
e336de0d 3473
3280af22
NIS
3474 New(54,PL_scopestack,REASONABLE(32),I32);
3475 PL_scopestack_ix = 0;
3476 PL_scopestack_max = REASONABLE(32);
79072805 3477
3280af22
NIS
3478 New(54,PL_savestack,REASONABLE(128),ANY);
3479 PL_savestack_ix = 0;
3480 PL_savestack_max = REASONABLE(128);
79072805 3481
3280af22
NIS
3482 New(54,PL_retstack,REASONABLE(16),OP*);
3483 PL_retstack_ix = 0;
3484 PL_retstack_max = REASONABLE(16);
378cc40b 3485}
33b78306 3486
2ce36478
SM
3487#undef REASONABLE
3488
76e3520e 3489STATIC void
cea2e8a9 3490S_nuke_stacks(pTHX)
6e72f9df 3491{
3280af22
NIS
3492 while (PL_curstackinfo->si_next)
3493 PL_curstackinfo = PL_curstackinfo->si_next;
3494 while (PL_curstackinfo) {
3495 PERL_SI *p = PL_curstackinfo->si_prev;
bac4b2ad 3496 /* curstackinfo->si_stack got nuked by sv_free_arenas() */
3280af22
NIS
3497 Safefree(PL_curstackinfo->si_cxstack);
3498 Safefree(PL_curstackinfo);
3499 PL_curstackinfo = p;
e336de0d 3500 }
3280af22
NIS
3501 Safefree(PL_tmps_stack);
3502 Safefree(PL_markstack);
3503 Safefree(PL_scopestack);
3504 Safefree(PL_savestack);
3505 Safefree(PL_retstack);
378cc40b 3506}
33b78306 3507
76e3520e 3508STATIC void
cea2e8a9 3509S_init_lexer(pTHX)
8990e307 3510{
06039172 3511 PerlIO *tmpfp;
3280af22
NIS
3512 tmpfp = PL_rsfp;
3513 PL_rsfp = Nullfp;
3514 lex_start(PL_linestr);
3515 PL_rsfp = tmpfp;