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