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