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