This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Configure: add defs summarizing doublekind/longdblkind
[perl5.git] / perl.c
CommitLineData
4b88f280 1#line 2 "perl.c"
a0d0e21e
LW
2/* perl.c
3 *
737f4459 4 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
2eee27d7 5 * 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
7a2bbcbf 6 * 2013, 2014, 2015, 2016 by Larry Wall and others
a687059c 7 *
352d5a3a
LW
8 * You may distribute under the terms of either the GNU General Public
9 * License or the Artistic License, as specified in the README file.
a687059c 10 *
8d063cd8
LW
11 */
12
a0d0e21e 13/*
4ac71550
TC
14 * A ship then new they built for him
15 * of mithril and of elven-glass
cdad3b53 16 * --from Bilbo's song of EƤrendil
4ac71550
TC
17 *
18 * [p.236 of _The Lord of the Rings_, II/i: "Many Meetings"]
a0d0e21e 19 */
45d8adaa 20
166f8a29
DM
21/* This file contains the top-level functions that are used to create, use
22 * and destroy a perl interpreter, plus the functions used by XS code to
23 * call back into perl. Note that it does not contain the actual main()
ddfa107c 24 * function of the interpreter; that can be found in perlmain.c
a1b69980
DM
25 *
26 * Note that at build time this file is also linked to as perlmini.c,
27 * and perlmini.o is then built with PERL_IS_MINIPERL defined, which is
28 * then used to create the miniperl executable, rather than perl.o.
166f8a29
DM
29 */
30
c44493f1 31#if defined(PERL_IS_MINIPERL) && !defined(USE_SITECUSTOMIZE)
43c0c913
NC
32# define USE_SITECUSTOMIZE
33#endif
34
378cc40b 35#include "EXTERN.h"
864dbfa3 36#define PERL_IN_PERL_C
378cc40b 37#include "perl.h"
e3321bb0 38#include "patchlevel.h" /* for local_patches */
4a5df386 39#include "XSUB.h"
378cc40b 40
011f1a1a
JH
41#ifdef NETWARE
42#include "nwutil.h"
011f1a1a
JH
43#endif
44
2aa47728 45#ifdef DEBUG_LEAKING_SCALARS_FORK_DUMP
bf357333
NC
46# ifdef I_SYSUIO
47# include <sys/uio.h>
48# endif
49
50union control_un {
51 struct cmsghdr cm;
52 char control[CMSG_SPACE(sizeof(int))];
53};
54
2aa47728
NC
55#endif
56
5311654c
JH
57#ifndef HZ
58# ifdef CLK_TCK
59# define HZ CLK_TCK
60# else
61# define HZ 60
62# endif
63#endif
64
7114a2d2 65#if !defined(STANDARD_C) && !defined(HAS_GETENV_PROTOTYPE) && !defined(PERL_MICRO)
20ce7b12 66char *getenv (char *); /* Usually in <stdlib.h> */
54310121 67#endif
68
acfe0abc 69static I32 read_e_script(pTHX_ int idx, SV *buf_sv, int maxlen);
0cb96387 70
cc69b689 71#ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
b24bc095 72# define validate_suid(rsfp) NOOP
cc69b689 73#else
b24bc095 74# define validate_suid(rsfp) S_validate_suid(aTHX_ rsfp)
a687059c 75#endif
8d063cd8 76
d6f07c05
AL
77#define CALL_BODY_SUB(myop) \
78 if (PL_op == (myop)) \
139d0ce6 79 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); \
d6f07c05
AL
80 if (PL_op) \
81 CALLRUNOPS(aTHX);
82
83#define CALL_LIST_BODY(cv) \
84 PUSHMARK(PL_stack_sp); \
9a8aa25b 85 call_sv(MUTABLE_SV((cv)), G_EVAL|G_DISCARD|G_VOID);
d6f07c05 86
e6827a76 87static void
daa7d858 88S_init_tls_and_interp(PerlInterpreter *my_perl)
e6827a76 89{
27da23d5 90 dVAR;
e6827a76
NC
91 if (!PL_curinterp) {
92 PERL_SET_INTERP(my_perl);
3db8f154 93#if defined(USE_ITHREADS)
e6827a76
NC
94 INIT_THREADS;
95 ALLOC_THREAD_KEY;
96 PERL_SET_THX(my_perl);
97 OP_REFCNT_INIT;
e8570548 98 OP_CHECK_MUTEX_INIT;
71ad1b0c 99 HINTS_REFCNT_INIT;
929e1213 100 LOCALE_INIT;
e6827a76 101 MUTEX_INIT(&PL_dollarzero_mutex);
016af4f1
DM
102 MUTEX_INIT(&PL_my_ctx_mutex);
103# endif
e6827a76 104 }
c0bce9aa
NC
105#if defined(USE_ITHREADS)
106 else
107#else
108 /* This always happens for non-ithreads */
109#endif
110 {
e6827a76
NC
111 PERL_SET_THX(my_perl);
112 }
113}
06d86050 114
cbec8ebe
DM
115
116/* these implement the PERL_SYS_INIT, PERL_SYS_INIT3, PERL_SYS_TERM macros */
117
118void
119Perl_sys_init(int* argc, char*** argv)
120{
4fc0badb 121 dVAR;
7918f24d
NC
122
123 PERL_ARGS_ASSERT_SYS_INIT;
124
cbec8ebe
DM
125 PERL_UNUSED_ARG(argc); /* may not be used depending on _BODY macro */
126 PERL_UNUSED_ARG(argv);
127 PERL_SYS_INIT_BODY(argc, argv);
128}
129
130void
131Perl_sys_init3(int* argc, char*** argv, char*** env)
132{
4fc0badb 133 dVAR;
7918f24d
NC
134
135 PERL_ARGS_ASSERT_SYS_INIT3;
136
cbec8ebe
DM
137 PERL_UNUSED_ARG(argc); /* may not be used depending on _BODY macro */
138 PERL_UNUSED_ARG(argv);
139 PERL_UNUSED_ARG(env);
140 PERL_SYS_INIT3_BODY(argc, argv, env);
141}
142
143void
88772978 144Perl_sys_term(void)
cbec8ebe 145{
4fc0badb 146 dVAR;
bf81751b
DM
147 if (!PL_veto_cleanup) {
148 PERL_SYS_TERM_BODY();
149 }
cbec8ebe
DM
150}
151
152
32e30700
GS
153#ifdef PERL_IMPLICIT_SYS
154PerlInterpreter *
7766f137
GS
155perl_alloc_using(struct IPerlMem* ipM, struct IPerlMem* ipMS,
156 struct IPerlMem* ipMP, struct IPerlEnv* ipE,
32e30700
GS
157 struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
158 struct IPerlDir* ipD, struct IPerlSock* ipS,
159 struct IPerlProc* ipP)
160{
161 PerlInterpreter *my_perl;
7918f24d
NC
162
163 PERL_ARGS_ASSERT_PERL_ALLOC_USING;
164
9f653bb5 165 /* Newx() needs interpreter, so call malloc() instead */
32e30700 166 my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
e6827a76 167 S_init_tls_and_interp(my_perl);
32e30700
GS
168 Zero(my_perl, 1, PerlInterpreter);
169 PL_Mem = ipM;
7766f137
GS
170 PL_MemShared = ipMS;
171 PL_MemParse = ipMP;
32e30700
GS
172 PL_Env = ipE;
173 PL_StdIO = ipStd;
174 PL_LIO = ipLIO;
175 PL_Dir = ipD;
176 PL_Sock = ipS;
177 PL_Proc = ipP;
7cb608b5 178 INIT_TRACK_MEMPOOL(PL_memory_debug_header, my_perl);
7766f137 179
32e30700
GS
180 return my_perl;
181}
182#else
954c1994
GS
183
184/*
ccfc67b7
JH
185=head1 Embedding Functions
186
954c1994
GS
187=for apidoc perl_alloc
188
189Allocates a new Perl interpreter. See L<perlembed>.
190
191=cut
192*/
193
93a17b20 194PerlInterpreter *
cea2e8a9 195perl_alloc(void)
79072805 196{
cea2e8a9 197 PerlInterpreter *my_perl;
79072805 198
9f653bb5 199 /* Newx() needs interpreter, so call malloc() instead */
e8ee3774 200 my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
ba869deb 201
e6827a76 202 S_init_tls_and_interp(my_perl);
7cb608b5 203#ifndef PERL_TRACK_MEMPOOL
07409e01 204 return (PerlInterpreter *) ZeroD(my_perl, 1, PerlInterpreter);
7cb608b5
NC
205#else
206 Zero(my_perl, 1, PerlInterpreter);
207 INIT_TRACK_MEMPOOL(PL_memory_debug_header, my_perl);
208 return my_perl;
209#endif
79072805 210}
32e30700 211#endif /* PERL_IMPLICIT_SYS */
79072805 212
954c1994
GS
213/*
214=for apidoc perl_construct
215
216Initializes a new Perl interpreter. See L<perlembed>.
217
218=cut
219*/
220
0927ade0
JC
221static void
222S_fixup_platform_bugs(void)
223{
224#if defined(__GLIBC__) && IVSIZE == 8 \
225 && ( __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 8))
226 {
227 IV l = 3;
228 IV r = -10;
229 /* Cannot do this check with inlined IV constants since
230 * that seems to work correctly even with the buggy glibc. */
231 if (l % r == -3) {
232 dTHX;
233 /* Yikes, we have the bug.
234 * Patch in the workaround version. */
235 PL_ppaddr[OP_I_MODULO] = &Perl_pp_i_modulo_glibc_bugfix;
236 }
237 }
238#endif
239}
240
79072805 241void
0cb96387 242perl_construct(pTHXx)
79072805 243{
27da23d5 244 dVAR;
7918f24d
NC
245
246 PERL_ARGS_ASSERT_PERL_CONSTRUCT;
247
8990e307 248#ifdef MULTIPLICITY
54aff467 249 init_interp();
ac27b0f5 250 PL_perl_destruct_level = 1;
54aff467 251#else
7918f24d 252 PERL_UNUSED_ARG(my_perl);
54aff467
GS
253 if (PL_perl_destruct_level > 0)
254 init_interp();
255#endif
34caed6d
DM
256 PL_curcop = &PL_compiling; /* needed by ckWARN, right away */
257
75d476e2
S
258#ifdef PERL_TRACE_OPS
259 Zero(PL_op_exec_cnt, OP_max+2, UV);
260#endif
261
0d96b528 262 init_constants();
34caed6d
DM
263
264 SvREADONLY_on(&PL_sv_placeholder);
fe54beba 265 SvREFCNT(&PL_sv_placeholder) = SvREFCNT_IMMORTAL;
34caed6d
DM
266
267 PL_sighandlerp = (Sighandler_t) Perl_sighandler;
ca0c25f6 268#ifdef PERL_USES_PL_PIDSTATUS
34caed6d 269 PL_pidstatus = newHV();
ca0c25f6 270#endif
79072805 271
396482e1 272 PL_rs = newSVpvs("\n");
dc92893f 273
cea2e8a9 274 init_stacks();
79072805 275
748a9306 276 init_ids();
a5f75d66 277
0927ade0
JC
278 S_fixup_platform_bugs();
279
312caa8e 280 JMPENV_BOOTSTRAP;
f86702cc 281 STATUS_ALL_SUCCESS;
282
0672f40e 283 init_i18nl10n(1);
0b5b802d 284
ab821d7f 285#if defined(LOCAL_PATCH_COUNT)
3280af22 286 PL_localpatches = local_patches; /* For possible -v */
ab821d7f 287#endif
288
52853b95
GS
289#ifdef HAVE_INTERP_INTERN
290 sys_intern_init();
291#endif
292
3a1ee7e8 293 PerlIO_init(aTHX); /* Hook to IO system */
760ac839 294
3280af22
NIS
295 PL_fdpid = newAV(); /* for remembering popen pids by fd */
296 PL_modglobal = newHV(); /* pointers to per-interpreter module globals */
396482e1 297 PL_errors = newSVpvs("");
854da30f
YO
298 SvPVCLEAR(PERL_DEBUG_PAD(0)); /* For regex debugging. */
299 SvPVCLEAR(PERL_DEBUG_PAD(1)); /* ext/re needs these */
300 SvPVCLEAR(PERL_DEBUG_PAD(2)); /* even without DEBUGGING. */
1fcf4c12 301#ifdef USE_ITHREADS
402d2eb1
NC
302 /* First entry is a list of empty elements. It needs to be initialised
303 else all hell breaks loose in S_find_uninit_var(). */
304 Perl_av_create_and_push(aTHX_ &PL_regex_padav, newSVpvs(""));
13137afc 305 PL_regex_pad = AvARRAY(PL_regex_padav);
d4d03940 306 Newxz(PL_stashpad, PL_stashpadmax, HV *);
1fcf4c12 307#endif
e5dd39fc 308#ifdef USE_REENTRANT_API
59bd0823 309 Perl_reentrant_init(aTHX);
e5dd39fc 310#endif
7dc86639
YO
311#if defined(USE_HASH_SEED) || defined(USE_HASH_SEED_EXPLICIT)
312 /* [perl #22371] Algorimic Complexity Attack on Perl 5.6.1, 5.8.0
313 * This MUST be done before any hash stores or fetches take place.
314 * If you set PL_hash_seed (and presumably also PL_hash_seed_set)
315 * yourself, it is your responsibility to provide a good random seed!
316 * You can also define PERL_HASH_SEED in compile time, see hv.h.
317 *
318 * XXX: fix this comment */
319 if (PL_hash_seed_set == FALSE) {
320 Perl_get_hash_seed(aTHX_ PL_hash_seed);
321 PL_hash_seed_set= TRUE;
322 }
323#endif /* #if defined(USE_HASH_SEED) || defined(USE_HASH_SEED_EXPLICIT) */
3d47000e 324
d3148f75
TR
325 /* Afaik, this is good as anywhere else to assert
326 * that our HEK structure is properly sized.
327 * We expect that the char used for the flags is
328 * not padded and the bytes for the key continue right
329 * after. (For now, perhaps we should just make the flags
330 * a U32, and leave the beginning of key buffer aligned)
331 * - Yves
332 */
333 STATIC_ASSERT_STMT( sizeof(((struct hek *)0)->hek_flags) ==
334 (offsetof(struct hek, hek_key)
335 - offsetof(struct hek, hek_flags)));
336
3d47000e
AB
337 /* Note that strtab is a rather special HV. Assumptions are made
338 about not iterating on it, and not adding tie magic to it.
339 It is properly deallocated in perl_destruct() */
340 PL_strtab = newHV();
341
3d47000e
AB
342 HvSHAREKEYS_off(PL_strtab); /* mandatory */
343 hv_ksplit(PL_strtab, 512);
344
a38ab475
RZ
345 Zero(PL_sv_consts, SV_CONSTS_COUNT, SV*);
346
2f42fcb0
JH
347#ifndef PERL_MICRO
348# ifdef USE_ENVIRON_ARRAY
0631ea03 349 PL_origenviron = environ;
2f42fcb0 350# endif
0631ea03
AB
351#endif
352
5311654c 353 /* Use sysconf(_SC_CLK_TCK) if available, if not
dbc1d986 354 * available or if the sysconf() fails, use the HZ.
27da23d5
JH
355 * The HZ if not originally defined has been by now
356 * been defined as CLK_TCK, if available. */
b6c36746 357#if defined(HAS_SYSCONF) && defined(_SC_CLK_TCK)
5311654c
JH
358 PL_clocktick = sysconf(_SC_CLK_TCK);
359 if (PL_clocktick <= 0)
360#endif
361 PL_clocktick = HZ;
362
081fc587
AB
363 PL_stashcache = newHV();
364
e8e3635e 365 PL_patchlevel = newSVpvs("v" PERL_VERSION_STRING);
d7aa5382 366
27da23d5
JH
367#ifdef HAS_MMAP
368 if (!PL_mmap_page_size) {
369#if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_MMAP_PAGE_SIZE))
370 {
371 SETERRNO(0, SS_NORMAL);
372# ifdef _SC_PAGESIZE
373 PL_mmap_page_size = sysconf(_SC_PAGESIZE);
374# else
375 PL_mmap_page_size = sysconf(_SC_MMAP_PAGE_SIZE);
376# endif
377 if ((long) PL_mmap_page_size < 0) {
378 if (errno) {
44f8325f 379 SV * const error = ERRSV;
d4c19fe8 380 SvUPGRADE(error, SVt_PV);
0510663f 381 Perl_croak(aTHX_ "panic: sysconf: %s", SvPV_nolen_const(error));
27da23d5
JH
382 }
383 else
384 Perl_croak(aTHX_ "panic: sysconf: pagesize unknown");
385 }
386 }
387#else
388# ifdef HAS_GETPAGESIZE
389 PL_mmap_page_size = getpagesize();
390# else
391# if defined(I_SYS_PARAM) && defined(PAGESIZE)
392 PL_mmap_page_size = PAGESIZE; /* compiletime, bad */
393# endif
394# endif
395#endif
396 if (PL_mmap_page_size <= 0)
397 Perl_croak(aTHX_ "panic: bad pagesize %" IVdf,
398 (IV) PL_mmap_page_size);
399 }
400#endif /* HAS_MMAP */
401
402#if defined(HAS_TIMES) && defined(PERL_NEED_TIMESBASE)
403 PL_timesbase.tms_utime = 0;
404 PL_timesbase.tms_stime = 0;
405 PL_timesbase.tms_cutime = 0;
406 PL_timesbase.tms_cstime = 0;
407#endif
408
7d113631
NC
409 PL_osname = Perl_savepvn(aTHX_ STR_WITH_LEN(OSNAME));
410
a3e6e81e 411 PL_registered_mros = newHV();
9e169432
NC
412 /* Start with 1 bucket, for DFS. It's unlikely we'll need more. */
413 HvMAX(PL_registered_mros) = 0;
a3e6e81e 414
7b8dd5f4
KW
415 PL_XPosix_ptrs[_CC_ASCII] = _new_invlist_C_array(ASCII_invlist);
416 PL_XPosix_ptrs[_CC_ALPHANUMERIC] = _new_invlist_C_array(XPosixAlnum_invlist);
417 PL_XPosix_ptrs[_CC_ALPHA] = _new_invlist_C_array(XPosixAlpha_invlist);
418 PL_XPosix_ptrs[_CC_BLANK] = _new_invlist_C_array(XPosixBlank_invlist);
419 PL_XPosix_ptrs[_CC_CASED] = _new_invlist_C_array(Cased_invlist);
420 PL_XPosix_ptrs[_CC_CNTRL] = _new_invlist_C_array(XPosixCntrl_invlist);
421 PL_XPosix_ptrs[_CC_DIGIT] = _new_invlist_C_array(XPosixDigit_invlist);
422 PL_XPosix_ptrs[_CC_GRAPH] = _new_invlist_C_array(XPosixGraph_invlist);
423 PL_XPosix_ptrs[_CC_LOWER] = _new_invlist_C_array(XPosixLower_invlist);
424 PL_XPosix_ptrs[_CC_PRINT] = _new_invlist_C_array(XPosixPrint_invlist);
425 PL_XPosix_ptrs[_CC_PUNCT] = _new_invlist_C_array(XPosixPunct_invlist);
426 PL_XPosix_ptrs[_CC_SPACE] = _new_invlist_C_array(XPerlSpace_invlist);
7b8dd5f4
KW
427 PL_XPosix_ptrs[_CC_UPPER] = _new_invlist_C_array(XPosixUpper_invlist);
428 PL_XPosix_ptrs[_CC_VERTSPACE] = _new_invlist_C_array(VertSpace_invlist);
429 PL_XPosix_ptrs[_CC_WORDCHAR] = _new_invlist_C_array(XPosixWord_invlist);
430 PL_XPosix_ptrs[_CC_XDIGIT] = _new_invlist_C_array(XPosixXDigit_invlist);
02f811dd 431 PL_GCB_invlist = _new_invlist_C_array(_Perl_GCB_invlist);
bf4268fa
KW
432 PL_SB_invlist = _new_invlist_C_array(_Perl_SB_invlist);
433 PL_WB_invlist = _new_invlist_C_array(_Perl_WB_invlist);
6b659339 434 PL_LB_invlist = _new_invlist_C_array(_Perl_LB_invlist);
6ebbc862
KW
435#ifdef USE_THREAD_SAFE_LOCALE
436 PL_C_locale_obj = newlocale(LC_ALL_MASK, "C", NULL);
437#endif
7b8dd5f4 438
8990e307 439 ENTER;
79072805
LW
440}
441
954c1994 442/*
62375a60
NIS
443=for apidoc nothreadhook
444
445Stub that provides thread hook for perl_destruct when there are
446no threads.
447
448=cut
449*/
450
451int
4e9e3734 452Perl_nothreadhook(pTHX)
62375a60 453{
96a5add6 454 PERL_UNUSED_CONTEXT;
62375a60
NIS
455 return 0;
456}
457
41e4abd8
NC
458#ifdef DEBUG_LEAKING_SCALARS_FORK_DUMP
459void
460Perl_dump_sv_child(pTHX_ SV *sv)
461{
462 ssize_t got;
bf357333
NC
463 const int sock = PL_dumper_fd;
464 const int debug_fd = PerlIO_fileno(Perl_debug_log);
bf357333
NC
465 union control_un control;
466 struct msghdr msg;
808ad2d0 467 struct iovec vec[2];
bf357333 468 struct cmsghdr *cmptr;
808ad2d0
NC
469 int returned_errno;
470 unsigned char buffer[256];
41e4abd8 471
7918f24d
NC
472 PERL_ARGS_ASSERT_DUMP_SV_CHILD;
473
bf357333 474 if(sock == -1 || debug_fd == -1)
41e4abd8
NC
475 return;
476
477 PerlIO_flush(Perl_debug_log);
478
bf357333
NC
479 /* All these shenanigans are to pass a file descriptor over to our child for
480 it to dump out to. We can't let it hold open the file descriptor when it
481 forks, as the file descriptor it will dump to can turn out to be one end
482 of pipe that some other process will wait on for EOF. (So as it would
b293a5f8 483 be open, the wait would be forever.) */
bf357333
NC
484
485 msg.msg_control = control.control;
486 msg.msg_controllen = sizeof(control.control);
487 /* We're a connected socket so we don't need a destination */
488 msg.msg_name = NULL;
489 msg.msg_namelen = 0;
490 msg.msg_iov = vec;
808ad2d0 491 msg.msg_iovlen = 1;
bf357333
NC
492
493 cmptr = CMSG_FIRSTHDR(&msg);
494 cmptr->cmsg_len = CMSG_LEN(sizeof(int));
495 cmptr->cmsg_level = SOL_SOCKET;
496 cmptr->cmsg_type = SCM_RIGHTS;
497 *((int *)CMSG_DATA(cmptr)) = 1;
498
499 vec[0].iov_base = (void*)&sv;
500 vec[0].iov_len = sizeof(sv);
501 got = sendmsg(sock, &msg, 0);
41e4abd8
NC
502
503 if(got < 0) {
bf357333 504 perror("Debug leaking scalars parent sendmsg failed");
41e4abd8
NC
505 abort();
506 }
bf357333
NC
507 if(got < sizeof(sv)) {
508 perror("Debug leaking scalars parent short sendmsg");
41e4abd8
NC
509 abort();
510 }
511
808ad2d0
NC
512 /* Return protocol is
513 int: errno value
514 unsigned char: length of location string (0 for empty)
515 unsigned char*: string (not terminated)
516 */
517 vec[0].iov_base = (void*)&returned_errno;
518 vec[0].iov_len = sizeof(returned_errno);
519 vec[1].iov_base = buffer;
520 vec[1].iov_len = 1;
521
522 got = readv(sock, vec, 2);
41e4abd8
NC
523
524 if(got < 0) {
525 perror("Debug leaking scalars parent read failed");
808ad2d0 526 PerlIO_flush(PerlIO_stderr());
41e4abd8
NC
527 abort();
528 }
808ad2d0 529 if(got < sizeof(returned_errno) + 1) {
41e4abd8 530 perror("Debug leaking scalars parent short read");
808ad2d0 531 PerlIO_flush(PerlIO_stderr());
41e4abd8
NC
532 abort();
533 }
534
808ad2d0
NC
535 if (*buffer) {
536 got = read(sock, buffer + 1, *buffer);
537 if(got < 0) {
538 perror("Debug leaking scalars parent read 2 failed");
539 PerlIO_flush(PerlIO_stderr());
540 abort();
541 }
542
543 if(got < *buffer) {
544 perror("Debug leaking scalars parent short read 2");
545 PerlIO_flush(PerlIO_stderr());
546 abort();
547 }
548 }
549
550 if (returned_errno || *buffer) {
551 Perl_warn(aTHX_ "Debug leaking scalars child failed%s%.*s with errno"
552 " %d: %s", (*buffer ? " at " : ""), (int) *buffer, buffer + 1,
0c0d42ff 553 returned_errno, Strerror(returned_errno));
41e4abd8
NC
554 }
555}
556#endif
557
62375a60 558/*
954c1994
GS
559=for apidoc perl_destruct
560
561Shuts down a Perl interpreter. See L<perlembed>.
562
563=cut
564*/
565
31d77e54 566int
0cb96387 567perl_destruct(pTHXx)
79072805 568{
27da23d5 569 dVAR;
be2ea8ed 570 VOL signed char destruct_level; /* see possible values in intrpvar.h */
a0d0e21e 571 HV *hv;
2aa47728 572#ifdef DEBUG_LEAKING_SCALARS_FORK_DUMP
2aa47728
NC
573 pid_t child;
574#endif
9c0b6888 575 int i;
8990e307 576
7918f24d
NC
577 PERL_ARGS_ASSERT_PERL_DESTRUCT;
578#ifndef MULTIPLICITY
ed6c66dd 579 PERL_UNUSED_ARG(my_perl);
7918f24d 580#endif
9d4ba2ae 581
3d22c4f0
GG
582 assert(PL_scopestack_ix == 1);
583
7766f137
GS
584 /* wait for all pseudo-forked children to finish */
585 PERL_WAIT_FOR_CHILDREN;
586
3280af22 587 destruct_level = PL_perl_destruct_level;
36e77d41 588#if defined(DEBUGGING) || defined(PERL_TRACK_MEMPOOL)
4633a7c4 589 {
9d4ba2ae
AL
590 const char * const s = PerlEnv_getenv("PERL_DESTRUCT_LEVEL");
591 if (s) {
96e440d2
JH
592 int i;
593 if (strEQ(s, "-1")) { /* Special case: modperl folklore. */
594 i = -1;
595 } else {
22ff3130
HS
596 UV uv;
597 if (grok_atoUV(s, &uv, NULL) && uv <= INT_MAX)
598 i = (int)uv;
599 else
600 i = 0;
96e440d2 601 }
36e77d41
DD
602#ifdef DEBUGGING
603 if (destruct_level < i) destruct_level = i;
604#endif
605#ifdef PERL_TRACK_MEMPOOL
f5199772
KW
606 /* RT #114496, for perl_free */
607 PL_perl_destruct_level = i;
36e77d41 608#endif
5f05dabc 609 }
4633a7c4
LW
610 }
611#endif
612
27da23d5 613 if (PL_exit_flags & PERL_EXIT_DESTRUCT_END) {
f3faeb53
AB
614 dJMPENV;
615 int x = 0;
616
617 JMPENV_PUSH(x);
1b6737cc 618 PERL_UNUSED_VAR(x);
9ebf26ad 619 if (PL_endav && !PL_minus_c) {
ca7b837b 620 PERL_SET_PHASE(PERL_PHASE_END);
f3faeb53 621 call_list(PL_scopestack_ix, PL_endav);
9ebf26ad 622 }
f3faeb53 623 JMPENV_POP;
26f423df 624 }
f3faeb53 625 LEAVE;
a0d0e21e 626 FREETMPS;
3d22c4f0 627 assert(PL_scopestack_ix == 0);
a0d0e21e 628
e00b64d4 629 /* Need to flush since END blocks can produce output */
8abddda3
TC
630 /* flush stdout separately, since we can identify it */
631#ifdef USE_PERLIO
632 {
633 PerlIO *stdo = PerlIO_stdout();
634 if (*stdo && PerlIO_flush(stdo)) {
635 PerlIO_restore_errno(stdo);
675c73ca
MB
636 if (errno)
637 PerlIO_printf(PerlIO_stderr(), "Unable to flush stdout: %s",
638 Strerror(errno));
8abddda3
TC
639 if (!STATUS_UNIX)
640 STATUS_ALL_FAILURE;
641 }
642 }
643#endif
f13a2bc0 644 my_fflush_all();
e00b64d4 645
75d476e2 646#ifdef PERL_TRACE_OPS
e71f25b3
JC
647 /* dump OP-counts if $ENV{PERL_TRACE_OPS} > 0 */
648 {
649 const char * const ptoenv = PerlEnv_getenv("PERL_TRACE_OPS");
650 UV uv;
651
652 if (!ptoenv || !Perl_grok_atoUV(ptoenv, &uv, NULL)
653 || !(uv > 0))
654 goto no_trace_out;
655 }
75d476e2
S
656 PerlIO_printf(Perl_debug_log, "Trace of all OPs executed:\n");
657 for (i = 0; i <= OP_max; ++i) {
e71f25b3
JC
658 if (PL_op_exec_cnt[i])
659 PerlIO_printf(Perl_debug_log, " %s: %"UVuf"\n", PL_op_name[i], PL_op_exec_cnt[i]);
75d476e2
S
660 }
661 /* Utility slot for easily doing little tracing experiments in the runloop: */
662 if (PL_op_exec_cnt[OP_max+1] != 0)
663 PerlIO_printf(Perl_debug_log, " SPECIAL: %"UVuf"\n", PL_op_exec_cnt[OP_max+1]);
664 PerlIO_printf(Perl_debug_log, "\n");
e71f25b3 665 no_trace_out:
75d476e2
S
666#endif
667
668
16c91539 669 if (PL_threadhook(aTHX)) {
62375a60 670 /* Threads hook has vetoed further cleanup */
c301d606 671 PL_veto_cleanup = TRUE;
37038d91 672 return STATUS_EXIT;
62375a60
NIS
673 }
674
2aa47728
NC
675#ifdef DEBUG_LEAKING_SCALARS_FORK_DUMP
676 if (destruct_level != 0) {
677 /* Fork here to create a child. Our child's job is to preserve the
678 state of scalars prior to destruction, so that we can instruct it
679 to dump any scalars that we later find have leaked.
680 There's no subtlety in this code - it assumes POSIX, and it doesn't
681 fail gracefully */
682 int fd[2];
683
684 if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) {
685 perror("Debug leaking scalars socketpair failed");
686 abort();
687 }
688
689 child = fork();
690 if(child == -1) {
691 perror("Debug leaking scalars fork failed");
692 abort();
693 }
694 if (!child) {
695 /* We are the child */
3125a5a4
NC
696 const int sock = fd[1];
697 const int debug_fd = PerlIO_fileno(Perl_debug_log);
698 int f;
808ad2d0
NC
699 const char *where;
700 /* Our success message is an integer 0, and a char 0 */
b61433a9 701 static const char success[sizeof(int) + 1] = {0};
3125a5a4 702
2aa47728 703 close(fd[0]);
2aa47728 704
3125a5a4
NC
705 /* We need to close all other file descriptors otherwise we end up
706 with interesting hangs, where the parent closes its end of a
707 pipe, and sits waiting for (another) child to terminate. Only
708 that child never terminates, because it never gets EOF, because
bf357333
NC
709 we also have the far end of the pipe open. We even need to
710 close the debugging fd, because sometimes it happens to be one
711 end of a pipe, and a process is waiting on the other end for
712 EOF. Normally it would be closed at some point earlier in
713 destruction, but if we happen to cause the pipe to remain open,
714 EOF never occurs, and we get an infinite hang. Hence all the
715 games to pass in a file descriptor if it's actually needed. */
3125a5a4
NC
716
717 f = sysconf(_SC_OPEN_MAX);
718 if(f < 0) {
808ad2d0
NC
719 where = "sysconf failed";
720 goto abort;
3125a5a4
NC
721 }
722 while (f--) {
723 if (f == sock)
724 continue;
3125a5a4
NC
725 close(f);
726 }
727
2aa47728
NC
728 while (1) {
729 SV *target;
bf357333
NC
730 union control_un control;
731 struct msghdr msg;
732 struct iovec vec[1];
733 struct cmsghdr *cmptr;
734 ssize_t got;
735 int got_fd;
736
737 msg.msg_control = control.control;
738 msg.msg_controllen = sizeof(control.control);
739 /* We're a connected socket so we don't need a source */
740 msg.msg_name = NULL;
741 msg.msg_namelen = 0;
742 msg.msg_iov = vec;
c3caa5c3 743 msg.msg_iovlen = C_ARRAY_LENGTH(vec);
bf357333
NC
744
745 vec[0].iov_base = (void*)&target;
746 vec[0].iov_len = sizeof(target);
747
748 got = recvmsg(sock, &msg, 0);
2aa47728
NC
749
750 if(got == 0)
751 break;
752 if(got < 0) {
808ad2d0
NC
753 where = "recv failed";
754 goto abort;
2aa47728
NC
755 }
756 if(got < sizeof(target)) {
808ad2d0
NC
757 where = "short recv";
758 goto abort;
2aa47728 759 }
bf357333 760
808ad2d0
NC
761 if(!(cmptr = CMSG_FIRSTHDR(&msg))) {
762 where = "no cmsg";
763 goto abort;
764 }
765 if(cmptr->cmsg_len != CMSG_LEN(sizeof(int))) {
766 where = "wrong cmsg_len";
767 goto abort;
768 }
769 if(cmptr->cmsg_level != SOL_SOCKET) {
770 where = "wrong cmsg_level";
771 goto abort;
772 }
773 if(cmptr->cmsg_type != SCM_RIGHTS) {
774 where = "wrong cmsg_type";
775 goto abort;
776 }
bf357333
NC
777
778 got_fd = *(int*)CMSG_DATA(cmptr);
779 /* For our last little bit of trickery, put the file descriptor
780 back into Perl_debug_log, as if we never actually closed it
781 */
808ad2d0
NC
782 if(got_fd != debug_fd) {
783 if (dup2(got_fd, debug_fd) == -1) {
784 where = "dup2";
785 goto abort;
786 }
787 }
2aa47728 788 sv_dump(target);
bf357333 789
2aa47728
NC
790 PerlIO_flush(Perl_debug_log);
791
808ad2d0 792 got = write(sock, &success, sizeof(success));
2aa47728
NC
793
794 if(got < 0) {
808ad2d0
NC
795 where = "write failed";
796 goto abort;
2aa47728 797 }
808ad2d0
NC
798 if(got < sizeof(success)) {
799 where = "short write";
800 goto abort;
2aa47728
NC
801 }
802 }
803 _exit(0);
808ad2d0
NC
804 abort:
805 {
806 int send_errno = errno;
807 unsigned char length = (unsigned char) strlen(where);
808 struct iovec failure[3] = {
809 {(void*)&send_errno, sizeof(send_errno)},
810 {&length, 1},
811 {(void*)where, length}
812 };
813 int got = writev(sock, failure, 3);
814 /* Bad news travels fast. Faster than data. We'll get a SIGPIPE
815 in the parent if we try to read from the socketpair after the
816 child has exited, even if there was data to read.
817 So sleep a bit to give the parent a fighting chance of
818 reading the data. */
819 sleep(2);
820 _exit((got == -1) ? errno : 0);
821 }
bf357333 822 /* End of child. */
2aa47728 823 }
41e4abd8 824 PL_dumper_fd = fd[0];
2aa47728
NC
825 close(fd[1]);
826 }
827#endif
828
ff0cee69 829 /* We must account for everything. */
830
831 /* Destroy the main CV and syntax tree */
37e77c23
FC
832 /* Set PL_curcop now, because destroying ops can cause new SVs
833 to be generated in Perl_pad_swipe, and when running with
834 -DDEBUG_LEAKING_SCALARS they expect PL_curcop to point to a valid
835 op from which the filename structure member is copied. */
17fbfdf6 836 PL_curcop = &PL_compiling;
3280af22 837 if (PL_main_root) {
4e380990
DM
838 /* ensure comppad/curpad to refer to main's pad */
839 if (CvPADLIST(PL_main_cv)) {
840 PAD_SET_CUR_NOSAVE(CvPADLIST(PL_main_cv), 1);
325e1816 841 PL_comppad_name = PadlistNAMES(CvPADLIST(PL_main_cv));
4e380990 842 }
3280af22 843 op_free(PL_main_root);
5f66b61c 844 PL_main_root = NULL;
a0d0e21e 845 }
5f66b61c 846 PL_main_start = NULL;
aac9d523
DM
847 /* note that PL_main_cv isn't usually actually freed at this point,
848 * due to the CvOUTSIDE refs from subs compiled within it. It will
849 * get freed once all the subs are freed in sv_clean_all(), for
850 * destruct_level > 0 */
3280af22 851 SvREFCNT_dec(PL_main_cv);
601f1833 852 PL_main_cv = NULL;
ca7b837b 853 PERL_SET_PHASE(PERL_PHASE_DESTRUCT);
ff0cee69 854
13621cfb
NIS
855 /* Tell PerlIO we are about to tear things apart in case
856 we have layers which are using resources that should
857 be cleaned up now.
858 */
859
860 PerlIO_destruct(aTHX);
861
ddf23d4a
S
862 /*
863 * Try to destruct global references. We do this first so that the
864 * destructors and destructees still exist. Some sv's might remain.
865 * Non-referenced objects are on their own.
866 */
867 sv_clean_objs();
8990e307 868
5cd24f17 869 /* unhook hooks which will soon be, or use, destroyed data */
3280af22 870 SvREFCNT_dec(PL_warnhook);
a0714e2c 871 PL_warnhook = NULL;
3280af22 872 SvREFCNT_dec(PL_diehook);
a0714e2c 873 PL_diehook = NULL;
5cd24f17 874
4b556e6c 875 /* call exit list functions */
3280af22 876 while (PL_exitlistlen-- > 0)
acfe0abc 877 PL_exitlist[PL_exitlistlen].fn(aTHX_ PL_exitlist[PL_exitlistlen].ptr);
4b556e6c 878
3280af22 879 Safefree(PL_exitlist);
4b556e6c 880
1c4916e5
CB
881 PL_exitlist = NULL;
882 PL_exitlistlen = 0;
883
a3e6e81e
NC
884 SvREFCNT_dec(PL_registered_mros);
885
551a8b83 886 /* jettison our possibly duplicated environment */
4b647fb0
DM
887 /* if PERL_USE_SAFE_PUTENV is defined environ will not have been copied
888 * so we certainly shouldn't free it here
889 */
2f42fcb0 890#ifndef PERL_MICRO
4b647fb0 891#if defined(USE_ENVIRON_ARRAY) && !defined(PERL_USE_SAFE_PUTENV)
50acdf95 892 if (environ != PL_origenviron && !PL_use_safe_putenv
4efc5df6
GS
893#ifdef USE_ITHREADS
894 /* only main thread can free environ[0] contents */
895 && PL_curinterp == aTHX
896#endif
897 )
898 {
551a8b83
JH
899 I32 i;
900
901 for (i = 0; environ[i]; i++)
4b420006 902 safesysfree(environ[i]);
0631ea03 903
4b420006
JH
904 /* Must use safesysfree() when working with environ. */
905 safesysfree(environ);
551a8b83
JH
906
907 environ = PL_origenviron;
908 }
909#endif
2f42fcb0 910#endif /* !PERL_MICRO */
551a8b83 911
30985c42
JH
912 if (destruct_level == 0) {
913
914 DEBUG_P(debprofdump());
915
916#if defined(PERLIO_LAYERS)
917 /* No more IO - including error messages ! */
918 PerlIO_cleanup(aTHX);
919#endif
920
921 CopFILE_free(&PL_compiling);
30985c42
JH
922
923 /* The exit() function will do everything that needs doing. */
924 return STATUS_EXIT;
925 }
926
9fa9f06b
KW
927 /* Below, do clean up for when PERL_DESTRUCT_LEVEL is not 0 */
928
5f8cb046
DM
929#ifdef USE_ITHREADS
930 /* the syntax tree is shared between clones
931 * so op_free(PL_main_root) only ReREFCNT_dec's
932 * REGEXPs in the parent interpreter
933 * we need to manually ReREFCNT_dec for the clones
934 */
0547a729
DM
935 {
936 I32 i = AvFILLp(PL_regex_padav);
937 SV **ary = AvARRAY(PL_regex_padav);
938
939 for (; i; i--) {
940 SvREFCNT_dec(ary[i]);
941 ary[i] = &PL_sv_undef;
942 }
943 }
5f8cb046
DM
944#endif
945
0547a729 946
ad64d0ec 947 SvREFCNT_dec(MUTABLE_SV(PL_stashcache));
081fc587
AB
948 PL_stashcache = NULL;
949
5f05dabc 950 /* loosen bonds of global variables */
951
2f9285f8
DM
952 /* XXX can PL_parser still be non-null here? */
953 if(PL_parser && PL_parser->rsfp) {
954 (void)PerlIO_close(PL_parser->rsfp);
955 PL_parser->rsfp = NULL;
8ebc5c01 956 }
957
84386e14
RGS
958 if (PL_minus_F) {
959 Safefree(PL_splitstr);
960 PL_splitstr = NULL;
961 }
962
8ebc5c01 963 /* switches */
3280af22
NIS
964 PL_minus_n = FALSE;
965 PL_minus_p = FALSE;
966 PL_minus_l = FALSE;
967 PL_minus_a = FALSE;
968 PL_minus_F = FALSE;
969 PL_doswitches = FALSE;
599cee73 970 PL_dowarn = G_WARN_OFF;
1a904fc8 971#ifdef PERL_SAWAMPERSAND
d3b97530 972 PL_sawampersand = 0; /* must save all match strings */
1a904fc8 973#endif
3280af22
NIS
974 PL_unsafe = FALSE;
975
976 Safefree(PL_inplace);
bd61b366 977 PL_inplace = NULL;
a7cb1f99 978 SvREFCNT_dec(PL_patchlevel);
3280af22
NIS
979
980 if (PL_e_script) {
981 SvREFCNT_dec(PL_e_script);
a0714e2c 982 PL_e_script = NULL;
8ebc5c01 983 }
984
bf9cdc68
RG
985 PL_perldb = 0;
986
8ebc5c01 987 /* magical thingies */
988
e23d9e2f
CS
989 SvREFCNT_dec(PL_ofsgv); /* *, */
990 PL_ofsgv = NULL;
5f05dabc 991
7889fe52 992 SvREFCNT_dec(PL_ors_sv); /* $\ */
a0714e2c 993 PL_ors_sv = NULL;
8ebc5c01 994
3280af22 995 SvREFCNT_dec(PL_rs); /* $/ */
a0714e2c 996 PL_rs = NULL;
dc92893f 997
d33b2eba 998 Safefree(PL_osname); /* $^O */
bd61b366 999 PL_osname = NULL;
5f05dabc 1000
3280af22 1001 SvREFCNT_dec(PL_statname);
a0714e2c
SS
1002 PL_statname = NULL;
1003 PL_statgv = NULL;
5f05dabc 1004
8ebc5c01 1005 /* defgv, aka *_ should be taken care of elsewhere */
1006
7d5ea4e7
GS
1007 /* float buffer */
1008 Safefree(PL_efloatbuf);
bd61b366 1009 PL_efloatbuf = NULL;
7d5ea4e7
GS
1010 PL_efloatsize = 0;
1011
8ebc5c01 1012 /* startup and shutdown function lists */
3280af22 1013 SvREFCNT_dec(PL_beginav);
5a837c8f 1014 SvREFCNT_dec(PL_beginav_save);
3280af22 1015 SvREFCNT_dec(PL_endav);
7d30b5c4 1016 SvREFCNT_dec(PL_checkav);
ece599bd 1017 SvREFCNT_dec(PL_checkav_save);
3c10abe3
AG
1018 SvREFCNT_dec(PL_unitcheckav);
1019 SvREFCNT_dec(PL_unitcheckav_save);
3280af22 1020 SvREFCNT_dec(PL_initav);
7d49f689
NC
1021 PL_beginav = NULL;
1022 PL_beginav_save = NULL;
1023 PL_endav = NULL;
1024 PL_checkav = NULL;
1025 PL_checkav_save = NULL;
3c10abe3
AG
1026 PL_unitcheckav = NULL;
1027 PL_unitcheckav_save = NULL;
7d49f689 1028 PL_initav = NULL;
5618dfe8 1029
8ebc5c01 1030 /* shortcuts just get cleared */
a0714e2c
SS
1031 PL_hintgv = NULL;
1032 PL_errgv = NULL;
a0714e2c
SS
1033 PL_argvoutgv = NULL;
1034 PL_stdingv = NULL;
1035 PL_stderrgv = NULL;
1036 PL_last_in_gv = NULL;
a0714e2c
SS
1037 PL_DBsingle = NULL;
1038 PL_DBtrace = NULL;
1039 PL_DBsignal = NULL;
a6d69523
TC
1040 PL_DBsingle_iv = 0;
1041 PL_DBtrace_iv = 0;
1042 PL_DBsignal_iv = 0;
601f1833 1043 PL_DBcv = NULL;
7d49f689 1044 PL_dbargs = NULL;
5c284bb0 1045 PL_debstash = NULL;
8ebc5c01 1046
cf93a474 1047 SvREFCNT_dec(PL_envgv);
f03015cd 1048 SvREFCNT_dec(PL_incgv);
722fa0e9 1049 SvREFCNT_dec(PL_argvgv);
475b1e90 1050 SvREFCNT_dec(PL_replgv);
8cece913
FC
1051 SvREFCNT_dec(PL_DBgv);
1052 SvREFCNT_dec(PL_DBline);
1053 SvREFCNT_dec(PL_DBsub);
cf93a474 1054 PL_envgv = NULL;
f03015cd 1055 PL_incgv = NULL;
722fa0e9 1056 PL_argvgv = NULL;
475b1e90 1057 PL_replgv = NULL;
8cece913
FC
1058 PL_DBgv = NULL;
1059 PL_DBline = NULL;
1060 PL_DBsub = NULL;
1061
7a1c5554 1062 SvREFCNT_dec(PL_argvout_stack);
7d49f689 1063 PL_argvout_stack = NULL;
8ebc5c01 1064
5c831c24 1065 SvREFCNT_dec(PL_modglobal);
5c284bb0 1066 PL_modglobal = NULL;
5c831c24 1067 SvREFCNT_dec(PL_preambleav);
7d49f689 1068 PL_preambleav = NULL;
5c831c24 1069 SvREFCNT_dec(PL_subname);
a0714e2c 1070 PL_subname = NULL;
ca0c25f6 1071#ifdef PERL_USES_PL_PIDSTATUS
5c831c24 1072 SvREFCNT_dec(PL_pidstatus);
5c284bb0 1073 PL_pidstatus = NULL;
ca0c25f6 1074#endif
5c831c24 1075 SvREFCNT_dec(PL_toptarget);
a0714e2c 1076 PL_toptarget = NULL;
5c831c24 1077 SvREFCNT_dec(PL_bodytarget);
a0714e2c
SS
1078 PL_bodytarget = NULL;
1079 PL_formtarget = NULL;
5c831c24 1080
d33b2eba 1081 /* free locale stuff */
b9582b6a 1082#ifdef USE_LOCALE_COLLATE
d33b2eba 1083 Safefree(PL_collation_name);
bd61b366 1084 PL_collation_name = NULL;
b9582b6a 1085#endif
d33b2eba 1086
b9582b6a 1087#ifdef USE_LOCALE_NUMERIC
d33b2eba 1088 Safefree(PL_numeric_name);
bd61b366 1089 PL_numeric_name = NULL;
a453c169 1090 SvREFCNT_dec(PL_numeric_radix_sv);
a0714e2c 1091 PL_numeric_radix_sv = NULL;
b9582b6a 1092#endif
d33b2eba 1093
9c0b6888
KW
1094 /* clear character classes */
1095 for (i = 0; i < POSIX_SWASH_COUNT; i++) {
1096 SvREFCNT_dec(PL_utf8_swash_ptrs[i]);
1097 PL_utf8_swash_ptrs[i] = NULL;
1098 }
5c831c24
GS
1099 SvREFCNT_dec(PL_utf8_mark);
1100 SvREFCNT_dec(PL_utf8_toupper);
4dbdbdc2 1101 SvREFCNT_dec(PL_utf8_totitle);
5c831c24 1102 SvREFCNT_dec(PL_utf8_tolower);
b4e400f9 1103 SvREFCNT_dec(PL_utf8_tofold);
82686b01
JH
1104 SvREFCNT_dec(PL_utf8_idstart);
1105 SvREFCNT_dec(PL_utf8_idcont);
c60f4405 1106 SvREFCNT_dec(PL_utf8_foldable);
2726813d 1107 SvREFCNT_dec(PL_utf8_foldclosures);
9fa9f06b 1108 SvREFCNT_dec(PL_AboveLatin1);
e0a1ff7a 1109 SvREFCNT_dec(PL_InBitmap);
9fa9f06b
KW
1110 SvREFCNT_dec(PL_UpperLatin1);
1111 SvREFCNT_dec(PL_Latin1);
1112 SvREFCNT_dec(PL_NonL1NonFinalFold);
1113 SvREFCNT_dec(PL_HasMultiCharFold);
5b7de470 1114#ifdef USE_LOCALE_CTYPE
780fcc9f 1115 SvREFCNT_dec(PL_warn_locale);
5b7de470 1116#endif
a0714e2c
SS
1117 PL_utf8_mark = NULL;
1118 PL_utf8_toupper = NULL;
1119 PL_utf8_totitle = NULL;
1120 PL_utf8_tolower = NULL;
1121 PL_utf8_tofold = NULL;
1122 PL_utf8_idstart = NULL;
1123 PL_utf8_idcont = NULL;
2726813d 1124 PL_utf8_foldclosures = NULL;
9fa9f06b 1125 PL_AboveLatin1 = NULL;
e0a1ff7a 1126 PL_InBitmap = NULL;
9fa9f06b 1127 PL_HasMultiCharFold = NULL;
5b7de470 1128#ifdef USE_LOCALE_CTYPE
780fcc9f 1129 PL_warn_locale = NULL;
5b7de470 1130#endif
9fa9f06b
KW
1131 PL_Latin1 = NULL;
1132 PL_NonL1NonFinalFold = NULL;
1133 PL_UpperLatin1 = NULL;
86f72d56 1134 for (i = 0; i < POSIX_CC_COUNT; i++) {
cac6e0ca
KW
1135 SvREFCNT_dec(PL_XPosix_ptrs[i]);
1136 PL_XPosix_ptrs[i] = NULL;
86f72d56 1137 }
64935bc6 1138 PL_GCB_invlist = NULL;
6b659339 1139 PL_LB_invlist = NULL;
06ae2722 1140 PL_SB_invlist = NULL;
ae3bb8ea 1141 PL_WB_invlist = NULL;
5c831c24 1142
971a9dd3 1143 if (!specialWARN(PL_compiling.cop_warnings))
72dc9ed5 1144 PerlMemShared_free(PL_compiling.cop_warnings);
a0714e2c 1145 PL_compiling.cop_warnings = NULL;
20439bc7
Z
1146 cophh_free(CopHINTHASH_get(&PL_compiling));
1147 CopHINTHASH_set(&PL_compiling, cophh_new_empty());
05ec9bb3 1148 CopFILE_free(&PL_compiling);
5c831c24 1149
a0d0e21e 1150 /* Prepare to destruct main symbol table. */
5f05dabc 1151
3280af22 1152 hv = PL_defstash;
ca556bcd 1153 /* break ref loop *:: <=> %:: */
854da30f 1154 (void)hv_deletes(hv, "main::", G_DISCARD);
3280af22 1155 PL_defstash = 0;
a0d0e21e 1156 SvREFCNT_dec(hv);
5c831c24 1157 SvREFCNT_dec(PL_curstname);
a0714e2c 1158 PL_curstname = NULL;
a0d0e21e 1159
5a844595
GS
1160 /* clear queued errors */
1161 SvREFCNT_dec(PL_errors);
a0714e2c 1162 PL_errors = NULL;
5a844595 1163
dd69841b
BB
1164 SvREFCNT_dec(PL_isarev);
1165
a0d0e21e 1166 FREETMPS;
9b387841 1167 if (destruct_level >= 2) {
3280af22 1168 if (PL_scopestack_ix != 0)
9b387841
NC
1169 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
1170 "Unbalanced scopes: %ld more ENTERs than LEAVEs\n",
1171 (long)PL_scopestack_ix);
3280af22 1172 if (PL_savestack_ix != 0)
9b387841
NC
1173 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
1174 "Unbalanced saves: %ld more saves than restores\n",
1175 (long)PL_savestack_ix);
3280af22 1176 if (PL_tmps_floor != -1)
9b387841
NC
1177 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),"Unbalanced tmps: %ld more allocs than frees\n",
1178 (long)PL_tmps_floor + 1);
a0d0e21e 1179 if (cxstack_ix != -1)
9b387841
NC
1180 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),"Unbalanced context: %ld more PUSHes than POPs\n",
1181 (long)cxstack_ix + 1);
a0d0e21e 1182 }
8990e307 1183
0547a729
DM
1184#ifdef USE_ITHREADS
1185 SvREFCNT_dec(PL_regex_padav);
1186 PL_regex_padav = NULL;
1187 PL_regex_pad = NULL;
1188#endif
1189
776df701 1190#ifdef PERL_IMPLICIT_CONTEXT
57bb2458
JH
1191 /* the entries in this list are allocated via SV PVX's, so get freed
1192 * in sv_clean_all */
1193 Safefree(PL_my_cxt_list);
776df701 1194#endif
57bb2458 1195
8990e307 1196 /* Now absolutely destruct everything, somehow or other, loops or no. */
5226ed68
JH
1197
1198 /* the 2 is for PL_fdpid and PL_strtab */
d17ea597 1199 while (sv_clean_all() > 2)
5226ed68
JH
1200 ;
1201
23083432
FC
1202#ifdef USE_ITHREADS
1203 Safefree(PL_stashpad); /* must come after sv_clean_all */
1204#endif
1205
d4777f27
GS
1206 AvREAL_off(PL_fdpid); /* no surviving entries */
1207 SvREFCNT_dec(PL_fdpid); /* needed in io_close() */
7d49f689 1208 PL_fdpid = NULL;
d33b2eba 1209
6c644e78
GS
1210#ifdef HAVE_INTERP_INTERN
1211 sys_intern_clear();
1212#endif
1213
a38ab475
RZ
1214 /* constant strings */
1215 for (i = 0; i < SV_CONSTS_COUNT; i++) {
1216 SvREFCNT_dec(PL_sv_consts[i]);
1217 PL_sv_consts[i] = NULL;
1218 }
1219
6e72f9df 1220 /* Destruct the global string table. */
1221 {
1222 /* Yell and reset the HeVAL() slots that are still holding refcounts,
1223 * so that sv_free() won't fail on them.
80459961
NC
1224 * Now that the global string table is using a single hunk of memory
1225 * for both HE and HEK, we either need to explicitly unshare it the
1226 * correct way, or actually free things here.
6e72f9df 1227 */
80459961
NC
1228 I32 riter = 0;
1229 const I32 max = HvMAX(PL_strtab);
c4420975 1230 HE * const * const array = HvARRAY(PL_strtab);
80459961
NC
1231 HE *hent = array[0];
1232
6e72f9df 1233 for (;;) {
0453d815 1234 if (hent && ckWARN_d(WARN_INTERNAL)) {
44f8325f 1235 HE * const next = HeNEXT(hent);
9014280d 1236 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
44f8325f 1237 "Unbalanced string table refcount: (%ld) for \"%s\"",
de616631 1238 (long)hent->he_valu.hent_refcount, HeKEY(hent));
80459961
NC
1239 Safefree(hent);
1240 hent = next;
6e72f9df 1241 }
1242 if (!hent) {
1243 if (++riter > max)
1244 break;
1245 hent = array[riter];
1246 }
1247 }
80459961
NC
1248
1249 Safefree(array);
1250 HvARRAY(PL_strtab) = 0;
1251 HvTOTALKEYS(PL_strtab) = 0;
6e72f9df 1252 }
3280af22 1253 SvREFCNT_dec(PL_strtab);
6e72f9df 1254
e652bb2f 1255#ifdef USE_ITHREADS
c21d1a0f 1256 /* free the pointer tables used for cloning */
a0739874 1257 ptr_table_free(PL_ptr_table);
bf9cdc68 1258 PL_ptr_table = (PTR_TBL_t*)NULL;
53186e96 1259#endif
a0739874 1260
d33b2eba
GS
1261 /* free special SVs */
1262
1263 SvREFCNT(&PL_sv_yes) = 0;
1264 sv_clear(&PL_sv_yes);
1265 SvANY(&PL_sv_yes) = NULL;
4c5e2b0d 1266 SvFLAGS(&PL_sv_yes) = 0;
d33b2eba
GS
1267
1268 SvREFCNT(&PL_sv_no) = 0;
1269 sv_clear(&PL_sv_no);
1270 SvANY(&PL_sv_no) = NULL;
4c5e2b0d 1271 SvFLAGS(&PL_sv_no) = 0;
01724ea0 1272
9f375a43
DM
1273 {
1274 int i;
1275 for (i=0; i<=2; i++) {
1276 SvREFCNT(PERL_DEBUG_PAD(i)) = 0;
1277 sv_clear(PERL_DEBUG_PAD(i));
1278 SvANY(PERL_DEBUG_PAD(i)) = NULL;
1279 SvFLAGS(PERL_DEBUG_PAD(i)) = 0;
1280 }
1281 }
1282
0453d815 1283 if (PL_sv_count != 0 && ckWARN_d(WARN_INTERNAL))
9014280d 1284 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),"Scalars leaked: %ld\n", (long)PL_sv_count);
6e72f9df 1285
eba0f806
DM
1286#ifdef DEBUG_LEAKING_SCALARS
1287 if (PL_sv_count != 0) {
1288 SV* sva;
1289 SV* sv;
eb578fdb 1290 SV* svend;
eba0f806 1291
ad64d0ec 1292 for (sva = PL_sv_arenaroot; sva; sva = MUTABLE_SV(SvANY(sva))) {
eba0f806
DM
1293 svend = &sva[SvREFCNT(sva)];
1294 for (sv = sva + 1; sv < svend; ++sv) {
e4787c0c 1295 if (SvTYPE(sv) != (svtype)SVTYPEMASK) {
a548cda8 1296 PerlIO_printf(Perl_debug_log, "leaked: sv=0x%p"
61b61456 1297 " flags=0x%"UVxf
fd0854ff 1298 " refcnt=%"UVuf pTHX__FORMAT "\n"
cd676548
DM
1299 "\tallocated at %s:%d %s %s (parent 0x%"UVxf");"
1300 "serial %"UVuf"\n",
574b8821
NC
1301 (void*)sv, (UV)sv->sv_flags, (UV)sv->sv_refcnt
1302 pTHX__VALUE,
fd0854ff
DM
1303 sv->sv_debug_file ? sv->sv_debug_file : "(unknown)",
1304 sv->sv_debug_line,
1305 sv->sv_debug_inpad ? "for" : "by",
1306 sv->sv_debug_optype ?
1307 PL_op_name[sv->sv_debug_optype]: "(none)",
cd676548 1308 PTR2UV(sv->sv_debug_parent),
cbe56f1d 1309 sv->sv_debug_serial
fd0854ff 1310 );
2aa47728 1311#ifdef DEBUG_LEAKING_SCALARS_FORK_DUMP
41e4abd8 1312 Perl_dump_sv_child(aTHX_ sv);
2aa47728 1313#endif
eba0f806
DM
1314 }
1315 }
1316 }
1317 }
2aa47728
NC
1318#ifdef DEBUG_LEAKING_SCALARS_FORK_DUMP
1319 {
1320 int status;
1321 fd_set rset;
1322 /* Wait for up to 4 seconds for child to terminate.
1323 This seems to be the least effort way of timing out on reaping
1324 its exit status. */
1325 struct timeval waitfor = {4, 0};
41e4abd8 1326 int sock = PL_dumper_fd;
2aa47728
NC
1327
1328 shutdown(sock, 1);
1329 FD_ZERO(&rset);
1330 FD_SET(sock, &rset);
1331 select(sock + 1, &rset, NULL, NULL, &waitfor);
1332 waitpid(child, &status, WNOHANG);
1333 close(sock);
1334 }
1335#endif
eba0f806 1336#endif
77abb4c6
NC
1337#ifdef DEBUG_LEAKING_SCALARS_ABORT
1338 if (PL_sv_count)
1339 abort();
1340#endif
bf9cdc68 1341 PL_sv_count = 0;
eba0f806 1342
56a2bab7 1343#if defined(PERLIO_LAYERS)
3a1ee7e8
NIS
1344 /* No more IO - including error messages ! */
1345 PerlIO_cleanup(aTHX);
1346#endif
1347
9f4bd222 1348 /* sv_undef needs to stay immortal until after PerlIO_cleanup
a0714e2c 1349 as currently layers use it rather than NULL as a marker
9f4bd222
NIS
1350 for no arg - and will try and SvREFCNT_dec it.
1351 */
1352 SvREFCNT(&PL_sv_undef) = 0;
1353 SvREADONLY_off(&PL_sv_undef);
1354
3280af22 1355 Safefree(PL_origfilename);
bd61b366 1356 PL_origfilename = NULL;
43c5f42d 1357 Safefree(PL_reg_curpm);
dd28f7bb 1358 free_tied_hv_pool();
3280af22 1359 Safefree(PL_op_mask);
cf36064f 1360 Safefree(PL_psig_name);
bf9cdc68 1361 PL_psig_name = (SV**)NULL;
d525a7b2 1362 PL_psig_ptr = (SV**)NULL;
31c91b43
LR
1363 {
1364 /* We need to NULL PL_psig_pend first, so that
1365 signal handlers know not to use it */
1366 int *psig_save = PL_psig_pend;
1367 PL_psig_pend = (int*)NULL;
1368 Safefree(psig_save);
1369 }
6e72f9df 1370 nuke_stacks();
284167a5
S
1371 TAINTING_set(FALSE);
1372 TAINT_WARN_set(FALSE);
3280af22 1373 PL_hints = 0; /* Reset hints. Should hints be per-interpreter ? */
ac27b0f5 1374
a0d0e21e 1375 DEBUG_P(debprofdump());
d33b2eba 1376
b173165c
FC
1377 PL_debug = 0;
1378
e5dd39fc 1379#ifdef USE_REENTRANT_API
10bc17b6 1380 Perl_reentrant_free(aTHX);
e5dd39fc
AB
1381#endif
1382
a24da70b
NC
1383 /* These all point to HVs that are about to be blown away.
1384 Code in core and on CPAN assumes that if the interpreter is re-started
1385 that they will be cleanly NULL or pointing to a valid HV. */
1386 PL_custom_op_names = NULL;
1387 PL_custom_op_descs = NULL;
1388 PL_custom_ops = NULL;
1389
612f20c3
GS
1390 sv_free_arenas();
1391
5d9a96ca
DM
1392 while (PL_regmatch_slab) {
1393 regmatch_slab *s = PL_regmatch_slab;
1394 PL_regmatch_slab = PL_regmatch_slab->next;
1395 Safefree(s);
1396 }
1397
fc36a67e 1398 /* As the absolutely last thing, free the non-arena SV for mess() */
1399
3280af22 1400 if (PL_mess_sv) {
f350b448
NC
1401 /* we know that type == SVt_PVMG */
1402
9c63abab 1403 /* it could have accumulated taint magic */
f350b448
NC
1404 MAGIC* mg;
1405 MAGIC* moremagic;
1406 for (mg = SvMAGIC(PL_mess_sv); mg; mg = moremagic) {
1407 moremagic = mg->mg_moremagic;
1408 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global
1409 && mg->mg_len >= 0)
1410 Safefree(mg->mg_ptr);
1411 Safefree(mg);
9c63abab 1412 }
f350b448 1413
fc36a67e 1414 /* we know that type >= SVt_PV */
8bd4d4c5 1415 SvPV_free(PL_mess_sv);
3280af22
NIS
1416 Safefree(SvANY(PL_mess_sv));
1417 Safefree(PL_mess_sv);
a0714e2c 1418 PL_mess_sv = NULL;
fc36a67e 1419 }
37038d91 1420 return STATUS_EXIT;
79072805
LW
1421}
1422
954c1994
GS
1423/*
1424=for apidoc perl_free
1425
1426Releases a Perl interpreter. See L<perlembed>.
1427
1428=cut
1429*/
1430
79072805 1431void
0cb96387 1432perl_free(pTHXx)
79072805 1433{
5174512c
NC
1434 dVAR;
1435
7918f24d
NC
1436 PERL_ARGS_ASSERT_PERL_FREE;
1437
c301d606
DM
1438 if (PL_veto_cleanup)
1439 return;
1440
7cb608b5 1441#ifdef PERL_TRACK_MEMPOOL
55ef9aae
MHM
1442 {
1443 /*
1444 * Don't free thread memory if PERL_DESTRUCT_LEVEL is set to a non-zero
1445 * value as we're probably hunting memory leaks then
1446 */
36e77d41 1447 if (PL_perl_destruct_level == 0) {
4fd0a9b8 1448 const U32 old_debug = PL_debug;
55ef9aae
MHM
1449 /* Emulate the PerlHost behaviour of free()ing all memory allocated in this
1450 thread at thread exit. */
4fd0a9b8
NC
1451 if (DEBUG_m_TEST) {
1452 PerlIO_puts(Perl_debug_log, "Disabling memory debugging as we "
1453 "free this thread's memory\n");
1454 PL_debug &= ~ DEBUG_m_FLAG;
1455 }
6edcbed6
DD
1456 while(aTHXx->Imemory_debug_header.next != &(aTHXx->Imemory_debug_header)){
1457 char * next = (char *)(aTHXx->Imemory_debug_header.next);
1458 Malloc_t ptr = PERL_MEMORY_DEBUG_HEADER_SIZE + next;
1459 safesysfree(ptr);
1460 }
4fd0a9b8 1461 PL_debug = old_debug;
55ef9aae
MHM
1462 }
1463 }
7cb608b5
NC
1464#endif
1465
acfe0abc 1466#if defined(WIN32) || defined(NETWARE)
ce3e5b80 1467# if defined(PERL_IMPLICIT_SYS)
b36c9a52 1468 {
acfe0abc 1469# ifdef NETWARE
7af12a34 1470 void *host = nw_internal_host;
7af12a34 1471 PerlMem_free(aTHXx);
7af12a34 1472 nw_delete_internal_host(host);
acfe0abc 1473# else
bdb50480
NC
1474 void *host = w32_internal_host;
1475 PerlMem_free(aTHXx);
7af12a34 1476 win32_delete_internal_host(host);
acfe0abc 1477# endif
7af12a34 1478 }
1c0ca838
GS
1479# else
1480 PerlMem_free(aTHXx);
1481# endif
acfe0abc
GS
1482#else
1483 PerlMem_free(aTHXx);
76e3520e 1484#endif
79072805
LW
1485}
1486
b7f7fff6 1487#if defined(USE_ITHREADS)
aebd1ac7
GA
1488/* provide destructors to clean up the thread key when libperl is unloaded */
1489#ifndef WIN32 /* handled during DLL_PROCESS_DETACH in win32/perllib.c */
1490
826955bd 1491#if defined(__hpux) && !(defined(__ux_version) && __ux_version <= 1020) && !defined(__GNUC__)
aebd1ac7 1492#pragma fini "perl_fini"
666ad1ec
GA
1493#elif defined(__sun) && !defined(__GNUC__)
1494#pragma fini (perl_fini)
aebd1ac7
GA
1495#endif
1496
0dbb1585
AL
1497static void
1498#if defined(__GNUC__)
1499__attribute__((destructor))
aebd1ac7 1500#endif
de009b76 1501perl_fini(void)
aebd1ac7 1502{
27da23d5 1503 dVAR;
5c64bffd
NC
1504 if (
1505#ifdef PERL_GLOBAL_STRUCT_PRIVATE
1506 my_vars &&
1507#endif
1508 PL_curinterp && !PL_veto_cleanup)
aebd1ac7
GA
1509 FREE_THREAD_KEY;
1510}
1511
1512#endif /* WIN32 */
1513#endif /* THREADS */
1514
4b556e6c 1515void
864dbfa3 1516Perl_call_atexit(pTHX_ ATEXIT_t fn, void *ptr)
4b556e6c 1517{
3280af22
NIS
1518 Renew(PL_exitlist, PL_exitlistlen+1, PerlExitListEntry);
1519 PL_exitlist[PL_exitlistlen].fn = fn;
1520 PL_exitlist[PL_exitlistlen].ptr = ptr;
1521 ++PL_exitlistlen;
4b556e6c
JD
1522}
1523
954c1994
GS
1524/*
1525=for apidoc perl_parse
1526
1527Tells a Perl interpreter to parse a Perl script. See L<perlembed>.
1528
1529=cut
1530*/
1531
03d9f026
FC
1532#define SET_CURSTASH(newstash) \
1533 if (PL_curstash != newstash) { \
1534 SvREFCNT_dec(PL_curstash); \
1535 PL_curstash = (HV *)SvREFCNT_inc(newstash); \
1536 }
1537
79072805 1538int
0cb96387 1539perl_parse(pTHXx_ XSINIT_t xsinit, int argc, char **argv, char **env)
8d063cd8 1540{
27da23d5 1541 dVAR;
6224f72b 1542 I32 oldscope;
6224f72b 1543 int ret;
db36c5a1 1544 dJMPENV;
8d063cd8 1545
7918f24d
NC
1546 PERL_ARGS_ASSERT_PERL_PARSE;
1547#ifndef MULTIPLICITY
ed6c66dd 1548 PERL_UNUSED_ARG(my_perl);
7918f24d 1549#endif
7dc86639 1550#if defined(USE_HASH_SEED) || defined(USE_HASH_SEED_EXPLICIT) || defined(USE_HASH_SEED_DEBUG)
b0891165 1551 {
7dc86639
YO
1552 const char * const s = PerlEnv_getenv("PERL_HASH_SEED_DEBUG");
1553
22ff3130 1554 if (s && strEQ(s, "1")) {
25c1b134
TC
1555 const unsigned char *seed= PERL_HASH_SEED;
1556 const unsigned char *seed_end= PERL_HASH_SEED + PERL_HASH_SEED_BYTES;
7dc86639
YO
1557 PerlIO_printf(Perl_debug_log, "HASH_FUNCTION = %s HASH_SEED = 0x", PERL_HASH_FUNC);
1558 while (seed < seed_end) {
1559 PerlIO_printf(Perl_debug_log, "%02x", *seed++);
1560 }
6a5b4183
YO
1561#ifdef PERL_HASH_RANDOMIZE_KEYS
1562 PerlIO_printf(Perl_debug_log, " PERTURB_KEYS = %d (%s)",
1563 PL_HASH_RAND_BITS_ENABLED,
1564 PL_HASH_RAND_BITS_ENABLED == 0 ? "NO" : PL_HASH_RAND_BITS_ENABLED == 1 ? "RANDOM" : "DETERMINISTIC");
1565#endif
7dc86639
YO
1566 PerlIO_printf(Perl_debug_log, "\n");
1567 }
b0891165
JH
1568 }
1569#endif /* #if defined(USE_HASH_SEED) || defined(USE_HASH_SEED_EXPLICIT) */
43238333 1570
ea34f6bd 1571#ifdef __amigaos4__
43238333
AB
1572 {
1573 struct NameTranslationInfo nti;
1574 __translate_amiga_to_unix_path_name(&argv[0],&nti);
1575 }
1576#endif
1577
3280af22 1578 PL_origargc = argc;
e2975953 1579 PL_origargv = argv;
a0d0e21e 1580
a2722ac9
GA
1581 if (PL_origalen != 0) {
1582 PL_origalen = 1; /* don't use old PL_origalen if perl_parse() is called again */
1583 }
1584 else {
3cb9023d
JH
1585 /* Set PL_origalen be the sum of the contiguous argv[]
1586 * elements plus the size of the env in case that it is
e9137a8e 1587 * contiguous with the argv[]. This is used in mg.c:Perl_magic_set()
3cb9023d
JH
1588 * as the maximum modifiable length of $0. In the worst case
1589 * the area we are able to modify is limited to the size of
43c32782 1590 * the original argv[0]. (See below for 'contiguous', though.)
3cb9023d 1591 * --jhi */
e1ec3a88 1592 const char *s = NULL;
54bfe034 1593 int i;
b7249aaf 1594 const UV mask = ~(UV)(PTRSIZE-1);
43c32782 1595 /* Do the mask check only if the args seem like aligned. */
1b6737cc 1596 const UV aligned =
43c32782
JH
1597 (mask < ~(UV)0) && ((PTR2UV(argv[0]) & mask) == PTR2UV(argv[0]));
1598
1599 /* See if all the arguments are contiguous in memory. Note
1600 * that 'contiguous' is a loose term because some platforms
1601 * align the argv[] and the envp[]. If the arguments look
1602 * like non-aligned, assume that they are 'strictly' or
1603 * 'traditionally' contiguous. If the arguments look like
1604 * aligned, we just check that they are within aligned
1605 * PTRSIZE bytes. As long as no system has something bizarre
1606 * like the argv[] interleaved with some other data, we are
1607 * fine. (Did I just evoke Murphy's Law?) --jhi */
c8941eeb
JH
1608 if (PL_origargv && PL_origargc >= 1 && (s = PL_origargv[0])) {
1609 while (*s) s++;
1610 for (i = 1; i < PL_origargc; i++) {
1611 if ((PL_origargv[i] == s + 1
43c32782 1612#ifdef OS2
c8941eeb 1613 || PL_origargv[i] == s + 2
43c32782 1614#endif
c8941eeb
JH
1615 )
1616 ||
1617 (aligned &&
1618 (PL_origargv[i] > s &&
1619 PL_origargv[i] <=
1620 INT2PTR(char *, PTR2UV(s + PTRSIZE) & mask)))
1621 )
1622 {
1623 s = PL_origargv[i];
1624 while (*s) s++;
1625 }
1626 else
1627 break;
54bfe034 1628 }
54bfe034 1629 }
a4a109c2
JD
1630
1631#ifndef PERL_USE_SAFE_PUTENV
3cb9023d 1632 /* Can we grab env area too to be used as the area for $0? */
a4a109c2 1633 if (s && PL_origenviron && !PL_use_safe_putenv) {
9d419b5f 1634 if ((PL_origenviron[0] == s + 1)
43c32782
JH
1635 ||
1636 (aligned &&
1637 (PL_origenviron[0] > s &&
1638 PL_origenviron[0] <=
1639 INT2PTR(char *, PTR2UV(s + PTRSIZE) & mask)))
1640 )
1641 {
9d419b5f 1642#ifndef OS2 /* ENVIRON is read by the kernel too. */
43c32782
JH
1643 s = PL_origenviron[0];
1644 while (*s) s++;
1645#endif
bd61b366 1646 my_setenv("NoNe SuCh", NULL);
43c32782
JH
1647 /* Force copy of environment. */
1648 for (i = 1; PL_origenviron[i]; i++) {
1649 if (PL_origenviron[i] == s + 1
1650 ||
1651 (aligned &&
1652 (PL_origenviron[i] > s &&
1653 PL_origenviron[i] <=
1654 INT2PTR(char *, PTR2UV(s + PTRSIZE) & mask)))
1655 )
1656 {
1657 s = PL_origenviron[i];
1658 while (*s) s++;
1659 }
1660 else
1661 break;
54bfe034 1662 }
43c32782 1663 }
54bfe034 1664 }
a4a109c2
JD
1665#endif /* !defined(PERL_USE_SAFE_PUTENV) */
1666
2d2af554 1667 PL_origalen = s ? s - PL_origargv[0] + 1 : 0;
54bfe034
JH
1668 }
1669
3280af22 1670 if (PL_do_undump) {
a0d0e21e
LW
1671
1672 /* Come here if running an undumped a.out. */
1673
3280af22
NIS
1674 PL_origfilename = savepv(argv[0]);
1675 PL_do_undump = FALSE;
a0d0e21e 1676 cxstack_ix = -1; /* start label stack again */
748a9306 1677 init_ids();
284167a5 1678 assert (!TAINT_get);
b7975bdd 1679 TAINT;
e2051532 1680 set_caret_X();
b7975bdd 1681 TAINT_NOT;
a0d0e21e
LW
1682 init_postdump_symbols(argc,argv,env);
1683 return 0;
1684 }
1685
3280af22 1686 if (PL_main_root) {
3280af22 1687 op_free(PL_main_root);
5f66b61c 1688 PL_main_root = NULL;
ff0cee69 1689 }
5f66b61c 1690 PL_main_start = NULL;
3280af22 1691 SvREFCNT_dec(PL_main_cv);
601f1833 1692 PL_main_cv = NULL;
79072805 1693
3280af22
NIS
1694 time(&PL_basetime);
1695 oldscope = PL_scopestack_ix;
599cee73 1696 PL_dowarn = G_WARN_OFF;
f86702cc 1697
14dd3ad8 1698 JMPENV_PUSH(ret);
6224f72b 1699 switch (ret) {
312caa8e 1700 case 0:
14dd3ad8 1701 parse_body(env,xsinit);
9ebf26ad 1702 if (PL_unitcheckav) {
3c10abe3 1703 call_list(oldscope, PL_unitcheckav);
9ebf26ad
FR
1704 }
1705 if (PL_checkav) {
ca7b837b 1706 PERL_SET_PHASE(PERL_PHASE_CHECK);
7d30b5c4 1707 call_list(oldscope, PL_checkav);
9ebf26ad 1708 }
14dd3ad8
GS
1709 ret = 0;
1710 break;
6224f72b
GS
1711 case 1:
1712 STATUS_ALL_FAILURE;
924ba076 1713 /* FALLTHROUGH */
6224f72b
GS
1714 case 2:
1715 /* my_exit() was called */
3280af22 1716 while (PL_scopestack_ix > oldscope)
6224f72b
GS
1717 LEAVE;
1718 FREETMPS;
03d9f026 1719 SET_CURSTASH(PL_defstash);
9ebf26ad 1720 if (PL_unitcheckav) {
3c10abe3 1721 call_list(oldscope, PL_unitcheckav);
9ebf26ad
FR
1722 }
1723 if (PL_checkav) {
ca7b837b 1724 PERL_SET_PHASE(PERL_PHASE_CHECK);
7d30b5c4 1725 call_list(oldscope, PL_checkav);
9ebf26ad 1726 }
37038d91 1727 ret = STATUS_EXIT;
14dd3ad8 1728 break;
6224f72b 1729 case 3:
bf49b057 1730 PerlIO_printf(Perl_error_log, "panic: top_env\n");
14dd3ad8
GS
1731 ret = 1;
1732 break;
6224f72b 1733 }
14dd3ad8
GS
1734 JMPENV_POP;
1735 return ret;
1736}
1737
4a5df386
NC
1738/* This needs to stay in perl.c, as perl.c is compiled with different flags for
1739 miniperl, and we need to see those flags reflected in the values here. */
1740
1741/* What this returns is subject to change. Use the public interface in Config.
1742 */
1743static void
1744S_Internals_V(pTHX_ CV *cv)
1745{
1746 dXSARGS;
1747#ifdef LOCAL_PATCH_COUNT
1748 const int local_patch_count = LOCAL_PATCH_COUNT;
1749#else
1750 const int local_patch_count = 0;
1751#endif
2dc296d2 1752 const int entries = 3 + local_patch_count;
4a5df386 1753 int i;
fe1c5936 1754 static const char non_bincompat_options[] =
4a5df386
NC
1755# ifdef DEBUGGING
1756 " DEBUGGING"
1757# endif
1758# ifdef NO_MATHOMS
0d311fbe 1759 " NO_MATHOMS"
4a5df386 1760# endif
59b86f4b
DM
1761# ifdef NO_HASH_SEED
1762 " NO_HASH_SEED"
1763# endif
3b0e4ee2
MB
1764# ifdef NO_TAINT_SUPPORT
1765 " NO_TAINT_SUPPORT"
1766# endif
cb26ef7a
MB
1767# ifdef PERL_BOOL_AS_CHAR
1768 " PERL_BOOL_AS_CHAR"
1769# endif
93c10d60
FC
1770# ifdef PERL_COPY_ON_WRITE
1771 " PERL_COPY_ON_WRITE"
1772# endif
4a5df386
NC
1773# ifdef PERL_DISABLE_PMC
1774 " PERL_DISABLE_PMC"
1775# endif
1776# ifdef PERL_DONT_CREATE_GVSV
1777 " PERL_DONT_CREATE_GVSV"
1778# endif
9a044a43
NC
1779# ifdef PERL_EXTERNAL_GLOB
1780 " PERL_EXTERNAL_GLOB"
1781# endif
59b86f4b
DM
1782# ifdef PERL_HASH_FUNC_SIPHASH
1783 " PERL_HASH_FUNC_SIPHASH"
1784# endif
1785# ifdef PERL_HASH_FUNC_SDBM
1786 " PERL_HASH_FUNC_SDBM"
1787# endif
1788# ifdef PERL_HASH_FUNC_DJB2
1789 " PERL_HASH_FUNC_DJB2"
1790# endif
1791# ifdef PERL_HASH_FUNC_SUPERFAST
1792 " PERL_HASH_FUNC_SUPERFAST"
1793# endif
1794# ifdef PERL_HASH_FUNC_MURMUR3
1795 " PERL_HASH_FUNC_MURMUR3"
1796# endif
1797# ifdef PERL_HASH_FUNC_ONE_AT_A_TIME
1798 " PERL_HASH_FUNC_ONE_AT_A_TIME"
1799# endif
1800# ifdef PERL_HASH_FUNC_ONE_AT_A_TIME_HARD
1801 " PERL_HASH_FUNC_ONE_AT_A_TIME_HARD"
1802# endif
1803# ifdef PERL_HASH_FUNC_ONE_AT_A_TIME_OLD
1804 " PERL_HASH_FUNC_ONE_AT_A_TIME_OLD"
1805# endif
4a5df386
NC
1806# ifdef PERL_IS_MINIPERL
1807 " PERL_IS_MINIPERL"
1808# endif
1809# ifdef PERL_MALLOC_WRAP
1810 " PERL_MALLOC_WRAP"
1811# endif
1812# ifdef PERL_MEM_LOG
1813 " PERL_MEM_LOG"
1814# endif
1815# ifdef PERL_MEM_LOG_NOIMPL
1816 " PERL_MEM_LOG_NOIMPL"
1817# endif
4e499636
DM
1818# ifdef PERL_OP_PARENT
1819 " PERL_OP_PARENT"
1820# endif
59b86f4b
DM
1821# ifdef PERL_PERTURB_KEYS_DETERMINISTIC
1822 " PERL_PERTURB_KEYS_DETERMINISTIC"
1823# endif
1824# ifdef PERL_PERTURB_KEYS_DISABLED
1825 " PERL_PERTURB_KEYS_DISABLED"
1826# endif
1827# ifdef PERL_PERTURB_KEYS_RANDOM
1828 " PERL_PERTURB_KEYS_RANDOM"
1829# endif
c3cf41ec
NC
1830# ifdef PERL_PRESERVE_IVUV
1831 " PERL_PRESERVE_IVUV"
1832# endif
c051e30b
NC
1833# ifdef PERL_RELOCATABLE_INCPUSH
1834 " PERL_RELOCATABLE_INCPUSH"
1835# endif
4a5df386
NC
1836# ifdef PERL_USE_DEVEL
1837 " PERL_USE_DEVEL"
1838# endif
1839# ifdef PERL_USE_SAFE_PUTENV
1840 " PERL_USE_SAFE_PUTENV"
1841# endif
102b7877
YO
1842# ifdef SILENT_NO_TAINT_SUPPORT
1843 " SILENT_NO_TAINT_SUPPORT"
1844# endif
a3749cf3
NC
1845# ifdef UNLINK_ALL_VERSIONS
1846 " UNLINK_ALL_VERSIONS"
1847# endif
de618ee4
NC
1848# ifdef USE_ATTRIBUTES_FOR_PERLIO
1849 " USE_ATTRIBUTES_FOR_PERLIO"
1850# endif
4a5df386
NC
1851# ifdef USE_FAST_STDIO
1852 " USE_FAST_STDIO"
1853# endif
59b86f4b
DM
1854# ifdef USE_HASH_SEED_EXPLICIT
1855 " USE_HASH_SEED_EXPLICIT"
1856# endif
98548bdf
NC
1857# ifdef USE_LOCALE
1858 " USE_LOCALE"
1859# endif
98548bdf
NC
1860# ifdef USE_LOCALE_CTYPE
1861 " USE_LOCALE_CTYPE"
1862# endif
6937817d
DD
1863# ifdef WIN32_NO_REGISTRY
1864 " USE_NO_REGISTRY"
1865# endif
5a8d8935
NC
1866# ifdef USE_PERL_ATOF
1867 " USE_PERL_ATOF"
1868# endif
0d311fbe
NC
1869# ifdef USE_SITECUSTOMIZE
1870 " USE_SITECUSTOMIZE"
1871# endif
4a5df386
NC
1872 ;
1873 PERL_UNUSED_ARG(cv);
d3db1514 1874 PERL_UNUSED_VAR(items);
4a5df386
NC
1875
1876 EXTEND(SP, entries);
1877
1878 PUSHs(sv_2mortal(newSVpv(PL_bincompat_options, 0)));
1879 PUSHs(Perl_newSVpvn_flags(aTHX_ non_bincompat_options,
1880 sizeof(non_bincompat_options) - 1, SVs_TEMP));
1881
6baa8dbd
NT
1882#ifndef PERL_BUILD_DATE
1883# ifdef __DATE__
1884# ifdef __TIME__
1885# define PERL_BUILD_DATE __DATE__ " " __TIME__
1886# else
1887# define PERL_BUILD_DATE __DATE__
1888# endif
1889# endif
1890#endif
1891
1892#ifdef PERL_BUILD_DATE
4a5df386 1893 PUSHs(Perl_newSVpvn_flags(aTHX_
6baa8dbd 1894 STR_WITH_LEN("Compiled at " PERL_BUILD_DATE),
4a5df386 1895 SVs_TEMP));
4a5df386
NC
1896#else
1897 PUSHs(&PL_sv_undef);
1898#endif
1899
4a5df386
NC
1900 for (i = 1; i <= local_patch_count; i++) {
1901 /* This will be an undef, if PL_localpatches[i] is NULL. */
1902 PUSHs(sv_2mortal(newSVpv(PL_localpatches[i], 0)));
1903 }
1904
1905 XSRETURN(entries);
1906}
1907
be71fc8f
NC
1908#define INCPUSH_UNSHIFT 0x01
1909#define INCPUSH_ADD_OLD_VERS 0x02
1910#define INCPUSH_ADD_VERSIONED_SUB_DIRS 0x04
1911#define INCPUSH_ADD_ARCHONLY_SUB_DIRS 0x08
1912#define INCPUSH_NOT_BASEDIR 0x10
1913#define INCPUSH_CAN_RELOCATE 0x20
1e3208d8
NC
1914#define INCPUSH_ADD_SUB_DIRS \
1915 (INCPUSH_ADD_VERSIONED_SUB_DIRS|INCPUSH_ADD_ARCHONLY_SUB_DIRS)
e28f3139 1916
312caa8e 1917STATIC void *
14dd3ad8 1918S_parse_body(pTHX_ char **env, XSINIT_t xsinit)
312caa8e 1919{
27da23d5 1920 dVAR;
2f9285f8 1921 PerlIO *rsfp;
312caa8e 1922 int argc = PL_origargc;
8f42b153 1923 char **argv = PL_origargv;
e1ec3a88 1924 const char *scriptname = NULL;
402582ca 1925 bool dosearch = FALSE;
eb578fdb 1926 char c;
737c24fc 1927 bool doextract = FALSE;
bd61b366 1928 const char *cddir = NULL;
ab019eaa 1929#ifdef USE_SITECUSTOMIZE
20ef40cf 1930 bool minus_f = FALSE;
ab019eaa 1931#endif
95670bde 1932 SV *linestr_sv = NULL;
5486870f 1933 bool add_read_e_script = FALSE;
87606032 1934 U32 lex_start_flags = 0;
009d90df 1935
ca7b837b 1936 PERL_SET_PHASE(PERL_PHASE_START);
9ebf26ad 1937
6224f72b 1938 init_main_stash();
54310121 1939
c7030b81
NC
1940 {
1941 const char *s;
6224f72b
GS
1942 for (argc--,argv++; argc > 0; argc--,argv++) {
1943 if (argv[0][0] != '-' || !argv[0][1])
1944 break;
6224f72b
GS
1945 s = argv[0]+1;
1946 reswitch:
47f56822 1947 switch ((c = *s)) {
729a02f2 1948 case 'C':
1d5472a9
GS
1949#ifndef PERL_STRICT_CR
1950 case '\r':
1951#endif
6224f72b
GS
1952 case ' ':
1953 case '0':
1954 case 'F':
1955 case 'a':
1956 case 'c':
1957 case 'd':
1958 case 'D':
1959 case 'h':
1960 case 'i':
1961 case 'l':
1962 case 'M':
1963 case 'm':
1964 case 'n':
1965 case 'p':
1966 case 's':
1967 case 'u':
1968 case 'U':
1969 case 'v':
599cee73
PM
1970 case 'W':
1971 case 'X':
6224f72b 1972 case 'w':
97bd5664 1973 if ((s = moreswitches(s)))
6224f72b
GS
1974 goto reswitch;
1975 break;
33b78306 1976
1dbad523 1977 case 't':
dc6d7f5c 1978#if defined(SILENT_NO_TAINT_SUPPORT)
284167a5 1979 /* silently ignore */
dc6d7f5c 1980#elif defined(NO_TAINT_SUPPORT)
3231f579 1981 Perl_croak_nocontext("This perl was compiled without taint support. "
284167a5
S
1982 "Cowardly refusing to run with -t or -T flags");
1983#else
22f7c9c9 1984 CHECK_MALLOC_TOO_LATE_FOR('t');
284167a5
S
1985 if( !TAINTING_get ) {
1986 TAINT_WARN_set(TRUE);
1987 TAINTING_set(TRUE);
317ea90d 1988 }
284167a5 1989#endif
317ea90d
MS
1990 s++;
1991 goto reswitch;
6224f72b 1992 case 'T':
dc6d7f5c 1993#if defined(SILENT_NO_TAINT_SUPPORT)
284167a5 1994 /* silently ignore */
dc6d7f5c 1995#elif defined(NO_TAINT_SUPPORT)
3231f579 1996 Perl_croak_nocontext("This perl was compiled without taint support. "
284167a5
S
1997 "Cowardly refusing to run with -t or -T flags");
1998#else
22f7c9c9 1999 CHECK_MALLOC_TOO_LATE_FOR('T');
284167a5
S
2000 TAINTING_set(TRUE);
2001 TAINT_WARN_set(FALSE);
2002#endif
6224f72b
GS
2003 s++;
2004 goto reswitch;
f86702cc 2005
bc9b29db
RH
2006 case 'E':
2007 PL_minus_E = TRUE;
924ba076 2008 /* FALLTHROUGH */
6224f72b 2009 case 'e':
f20b2998 2010 forbid_setid('e', FALSE);
3280af22 2011 if (!PL_e_script) {
396482e1 2012 PL_e_script = newSVpvs("");
5486870f 2013 add_read_e_script = TRUE;
6224f72b
GS
2014 }
2015 if (*++s)
3280af22 2016 sv_catpv(PL_e_script, s);
6224f72b 2017 else if (argv[1]) {
3280af22 2018 sv_catpv(PL_e_script, argv[1]);
6224f72b
GS
2019 argc--,argv++;
2020 }
2021 else
47f56822 2022 Perl_croak(aTHX_ "No code specified for -%c", c);
396482e1 2023 sv_catpvs(PL_e_script, "\n");
6224f72b 2024 break;
afe37c7d 2025
20ef40cf 2026 case 'f':
f5542d3a 2027#ifdef USE_SITECUSTOMIZE
20ef40cf 2028 minus_f = TRUE;
f5542d3a 2029#endif
20ef40cf
GA
2030 s++;
2031 goto reswitch;
2032
6224f72b 2033 case 'I': /* -I handled both here and in moreswitches() */
f20b2998 2034 forbid_setid('I', FALSE);
bd61b366 2035 if (!*++s && (s=argv[1]) != NULL) {
6224f72b
GS
2036 argc--,argv++;
2037 }
6224f72b 2038 if (s && *s) {
0df16ed7 2039 STRLEN len = strlen(s);
55b4bc1c 2040 incpush(s, len, INCPUSH_ADD_SUB_DIRS|INCPUSH_ADD_OLD_VERS);
0df16ed7
GS
2041 }
2042 else
a67e862a 2043 Perl_croak(aTHX_ "No directory specified for -I");
6224f72b 2044 break;
6224f72b 2045 case 'S':
f20b2998 2046 forbid_setid('S', FALSE);
6224f72b
GS
2047 dosearch = TRUE;
2048 s++;
2049 goto reswitch;
2050 case 'V':
7edfd0ef
NC
2051 {
2052 SV *opts_prog;
2053
7edfd0ef 2054 if (*++s != ':') {
37ca4a5b 2055 opts_prog = newSVpvs("use Config; Config::_V()");
7edfd0ef
NC
2056 }
2057 else {
2058 ++s;
2059 opts_prog = Perl_newSVpvf(aTHX_
37ca4a5b 2060 "use Config; Config::config_vars(qw%c%s%c)",
7edfd0ef
NC
2061 0, s, 0);
2062 s += strlen(s);
2063 }
37ca4a5b 2064 Perl_av_create_and_push(aTHX_ &PL_preambleav, opts_prog);
7edfd0ef
NC
2065 /* don't look for script or read stdin */
2066 scriptname = BIT_BUCKET;
2067 goto reswitch;
6224f72b 2068 }
6224f72b 2069 case 'x':
737c24fc 2070 doextract = TRUE;
6224f72b 2071 s++;
304334da 2072 if (*s)
f4c556ac 2073 cddir = s;
6224f72b
GS
2074 break;
2075 case 0:
2076 break;
2077 case '-':
2078 if (!*++s || isSPACE(*s)) {
2079 argc--,argv++;
2080 goto switch_end;
2081 }
ee8bc8b7
NC
2082 /* catch use of gnu style long options.
2083 Both of these exit immediately. */
2084 if (strEQ(s, "version"))
2085 minus_v();
2086 if (strEQ(s, "help"))
2087 usage();
6224f72b 2088 s--;
924ba076 2089 /* FALLTHROUGH */
6224f72b 2090 default:
cea2e8a9 2091 Perl_croak(aTHX_ "Unrecognized switch: -%s (-h will show valid options)",s);
8d063cd8
LW
2092 }
2093 }
c7030b81
NC
2094 }
2095
6224f72b 2096 switch_end:
54310121 2097
c7030b81
NC
2098 {
2099 char *s;
2100
f675dbe5
CB
2101 if (
2102#ifndef SECURE_INTERNAL_GETENV
284167a5 2103 !TAINTING_get &&
f675dbe5 2104#endif
cf756827 2105 (s = PerlEnv_getenv("PERL5OPT")))
0df16ed7 2106 {
9e0b0d62
KW
2107 /* s points to static memory in getenv(), which may be overwritten at
2108 * any time; use a mortal copy instead */
2109 s = SvPVX(sv_2mortal(newSVpv(s, 0)));
2110
74288ac8
GS
2111 while (isSPACE(*s))
2112 s++;
317ea90d 2113 if (*s == '-' && *(s+1) == 'T') {
dc6d7f5c 2114#if defined(SILENT_NO_TAINT_SUPPORT)
284167a5 2115 /* silently ignore */
dc6d7f5c 2116#elif defined(NO_TAINT_SUPPORT)
3231f579 2117 Perl_croak_nocontext("This perl was compiled without taint support. "
284167a5
S
2118 "Cowardly refusing to run with -t or -T flags");
2119#else
22f7c9c9 2120 CHECK_MALLOC_TOO_LATE_FOR('T');
284167a5
S
2121 TAINTING_set(TRUE);
2122 TAINT_WARN_set(FALSE);
2123#endif
317ea90d 2124 }
74288ac8 2125 else {
bd61b366 2126 char *popt_copy = NULL;
74288ac8 2127 while (s && *s) {
54913509 2128 const char *d;
74288ac8
GS
2129 while (isSPACE(*s))
2130 s++;
2131 if (*s == '-') {
2132 s++;
2133 if (isSPACE(*s))
2134 continue;
2135 }
4ea8f8fb 2136 d = s;
74288ac8
GS
2137 if (!*s)
2138 break;
2b622f1a 2139 if (!strchr("CDIMUdmtwW", *s))
cea2e8a9 2140 Perl_croak(aTHX_ "Illegal switch in PERL5OPT: -%c", *s);
4ea8f8fb
MS
2141 while (++s && *s) {
2142 if (isSPACE(*s)) {
cf756827 2143 if (!popt_copy) {
bfa6c418
NC
2144 popt_copy = SvPVX(sv_2mortal(newSVpv(d,0)));
2145 s = popt_copy + (s - d);
2146 d = popt_copy;
cf756827 2147 }
4ea8f8fb
MS
2148 *s++ = '\0';
2149 break;
2150 }
2151 }
1c4db469 2152 if (*d == 't') {
dc6d7f5c 2153#if defined(SILENT_NO_TAINT_SUPPORT)
284167a5 2154 /* silently ignore */
dc6d7f5c 2155#elif defined(NO_TAINT_SUPPORT)
3231f579 2156 Perl_croak_nocontext("This perl was compiled without taint support. "
284167a5
S
2157 "Cowardly refusing to run with -t or -T flags");
2158#else
2159 if( !TAINTING_get) {
2160 TAINT_WARN_set(TRUE);
2161 TAINTING_set(TRUE);
317ea90d 2162 }
284167a5 2163#endif
1c4db469 2164 } else {
97bd5664 2165 moreswitches(d);
1c4db469 2166 }
6224f72b 2167 }
6224f72b
GS
2168 }
2169 }
c7030b81 2170 }
a0d0e21e 2171
c29067d7
CH
2172 /* Set $^X early so that it can be used for relocatable paths in @INC */
2173 /* and for SITELIB_EXP in USE_SITECUSTOMIZE */
284167a5 2174 assert (!TAINT_get);
c29067d7 2175 TAINT;
e2051532 2176 set_caret_X();
c29067d7
CH
2177 TAINT_NOT;
2178
43c0c913 2179#if defined(USE_SITECUSTOMIZE)
20ef40cf 2180 if (!minus_f) {
43c0c913 2181 /* The games with local $! are to avoid setting errno if there is no
fc81b718
NC
2182 sitecustomize script. "q%c...%c", 0, ..., 0 becomes "q\0...\0",
2183 ie a q() operator with a NUL byte as a the delimiter. This avoids
2184 problems with pathnames containing (say) ' */
43c0c913
NC
2185# ifdef PERL_IS_MINIPERL
2186 AV *const inc = GvAV(PL_incgv);
2187 SV **const inc0 = inc ? av_fetch(inc, 0, FALSE) : NULL;
2188
2189 if (inc0) {
15870c5c
NC
2190 /* if lib/buildcustomize.pl exists, it should not fail. If it does,
2191 it should be reported immediately as a build failure. */
43c0c913
NC
2192 (void)Perl_av_create_and_unshift_one(aTHX_ &PL_preambleav,
2193 Perl_newSVpvf(aTHX_
5de87db5 2194 "BEGIN { my $f = q%c%s%"SVf"/buildcustomize.pl%c; "
af26e4f2
FC
2195 "do {local $!; -f $f }"
2196 " and do $f || die $@ || qq '$f: $!' }",
5de87db5 2197 0, (TAINTING_get ? "./" : ""), SVfARG(*inc0), 0));
43c0c913
NC
2198 }
2199# else
2200 /* SITELIB_EXP is a function call on Win32. */
c29067d7 2201 const char *const raw_sitelib = SITELIB_EXP;
bac5c4fc
JD
2202 if (raw_sitelib) {
2203 /* process .../.. if PERL_RELOCATABLE_INC is defined */
2204 SV *sitelib_sv = mayberelocate(raw_sitelib, strlen(raw_sitelib),
2205 INCPUSH_CAN_RELOCATE);
2206 const char *const sitelib = SvPVX(sitelib_sv);
2207 (void)Perl_av_create_and_unshift_one(aTHX_ &PL_preambleav,
2208 Perl_newSVpvf(aTHX_
2209 "BEGIN { do {local $!; -f q%c%s/sitecustomize.pl%c} && do q%c%s/sitecustomize.pl%c }",
c1f6cd39
BF
2210 0, SVfARG(sitelib), 0,
2211 0, SVfARG(sitelib), 0));
bac5c4fc
JD
2212 assert (SvREFCNT(sitelib_sv) == 1);
2213 SvREFCNT_dec(sitelib_sv);
2214 }
43c0c913 2215# endif
20ef40cf
GA
2216 }
2217#endif
2218
6224f72b
GS
2219 if (!scriptname)
2220 scriptname = argv[0];
3280af22 2221 if (PL_e_script) {
6224f72b
GS
2222 argc++,argv--;
2223 scriptname = BIT_BUCKET; /* don't look for script or read stdin */
2224 }
bd61b366 2225 else if (scriptname == NULL) {
6224f72b
GS
2226#ifdef MSDOS
2227 if ( PerlLIO_isatty(PerlIO_fileno(PerlIO_stdin())) )
97bd5664 2228 moreswitches("h");
6224f72b
GS
2229#endif
2230 scriptname = "-";
2231 }
2232
284167a5 2233 assert (!TAINT_get);
2cace6ac 2234 init_perllib();
6224f72b 2235
a52eba0e 2236 {
f20b2998 2237 bool suidscript = FALSE;
829372d3 2238
8d113837 2239 rsfp = open_script(scriptname, dosearch, &suidscript);
c0b3891a
NC
2240 if (!rsfp) {
2241 rsfp = PerlIO_stdin();
87606032 2242 lex_start_flags = LEX_DONT_CLOSE_RSFP;
c0b3891a 2243 }
6224f72b 2244
b24bc095 2245 validate_suid(rsfp);
6224f72b 2246
64ca3a65 2247#ifndef PERL_MICRO
a52eba0e
NC
2248# if defined(SIGCHLD) || defined(SIGCLD)
2249 {
2250# ifndef SIGCHLD
2251# define SIGCHLD SIGCLD
2252# endif
2253 Sighandler_t sigstate = rsignal_state(SIGCHLD);
2254 if (sigstate == (Sighandler_t) SIG_IGN) {
a2a5de95
NC
2255 Perl_ck_warner(aTHX_ packWARN(WARN_SIGNAL),
2256 "Can't ignore signal CHLD, forcing to default");
a52eba0e
NC
2257 (void)rsignal(SIGCHLD, (Sighandler_t)SIG_DFL);
2258 }
0b5b802d 2259 }
a52eba0e 2260# endif
64ca3a65 2261#endif
0b5b802d 2262
737c24fc 2263 if (doextract) {
faef540c 2264
f20b2998 2265 /* This will croak if suidscript is true, as -x cannot be used with
faef540c
NC
2266 setuid scripts. */
2267 forbid_setid('x', suidscript);
f20b2998 2268 /* Hence you can't get here if suidscript is true */
faef540c 2269
95670bde
NC
2270 linestr_sv = newSV_type(SVt_PV);
2271 lex_start_flags |= LEX_START_COPIED;
2f9285f8 2272 find_beginning(linestr_sv, rsfp);
a52eba0e
NC
2273 if (cddir && PerlDir_chdir( (char *)cddir ) < 0)
2274 Perl_croak(aTHX_ "Can't chdir to %s",cddir);
2275 }
f4c556ac 2276 }
6224f72b 2277
ea726b52 2278 PL_main_cv = PL_compcv = MUTABLE_CV(newSV_type(SVt_PVCV));
3280af22
NIS
2279 CvUNIQUE_on(PL_compcv);
2280
eacbb379 2281 CvPADLIST_set(PL_compcv, pad_new(0));
6224f72b 2282
dd69841b
BB
2283 PL_isarev = newHV();
2284
0c4f7ff0 2285 boot_core_PerlIO();
6224f72b 2286 boot_core_UNIVERSAL();
e1a479c5 2287 boot_core_mro();
4a5df386 2288 newXS("Internals::V", S_Internals_V, __FILE__);
6224f72b
GS
2289
2290 if (xsinit)
acfe0abc 2291 (*xsinit)(aTHX); /* in case linked C routines want magical variables */
64ca3a65 2292#ifndef PERL_MICRO
739a0b84 2293#if defined(VMS) || defined(WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(SYMBIAN)
c5be433b 2294 init_os_extras();
6224f72b 2295#endif
64ca3a65 2296#endif
6224f72b 2297
29209bc5 2298#ifdef USE_SOCKS
1b9c9cf5
DH
2299# ifdef HAS_SOCKS5_INIT
2300 socks5_init(argv[0]);
2301# else
29209bc5 2302 SOCKSinit(argv[0]);
1b9c9cf5 2303# endif
ac27b0f5 2304#endif
29209bc5 2305
6224f72b
GS
2306 init_predump_symbols();
2307 /* init_postdump_symbols not currently designed to be called */
2308 /* more than once (ENV isn't cleared first, for example) */
2309 /* But running with -u leaves %ENV & @ARGV undefined! XXX */
3280af22 2310 if (!PL_do_undump)
6224f72b
GS
2311 init_postdump_symbols(argc,argv,env);
2312
27da23d5
JH
2313 /* PL_unicode is turned on by -C, or by $ENV{PERL_UNICODE},
2314 * or explicitly in some platforms.
73e1bd1a 2315 * PL_utf8locale is conditionally turned on by
085a54d9 2316 * locale.c:Perl_init_i18nl10n() if the environment
a05d7ebb 2317 * look like the user wants to use UTF-8. */
a0fd4948 2318#if defined(__SYMBIAN32__)
27da23d5
JH
2319 PL_unicode = PERL_UNICODE_STD_FLAG; /* See PERL_SYMBIAN_CONSOLE_UTF8. */
2320#endif
e27b5b51 2321# ifndef PERL_IS_MINIPERL
06e66572
JH
2322 if (PL_unicode) {
2323 /* Requires init_predump_symbols(). */
a05d7ebb 2324 if (!(PL_unicode & PERL_UNICODE_LOCALE_FLAG) || PL_utf8locale) {
06e66572
JH
2325 IO* io;
2326 PerlIO* fp;
2327 SV* sv;
2328
a05d7ebb 2329 /* Turn on UTF-8-ness on STDIN, STDOUT, STDERR
06e66572 2330 * and the default open disciplines. */
a05d7ebb
JH
2331 if ((PL_unicode & PERL_UNICODE_STDIN_FLAG) &&
2332 PL_stdingv && (io = GvIO(PL_stdingv)) &&
2333 (fp = IoIFP(io)))
2334 PerlIO_binmode(aTHX_ fp, IoTYPE(io), 0, ":utf8");
2335 if ((PL_unicode & PERL_UNICODE_STDOUT_FLAG) &&
2336 PL_defoutgv && (io = GvIO(PL_defoutgv)) &&
2337 (fp = IoOFP(io)))
2338 PerlIO_binmode(aTHX_ fp, IoTYPE(io), 0, ":utf8");
2339 if ((PL_unicode & PERL_UNICODE_STDERR_FLAG) &&
2340 PL_stderrgv && (io = GvIO(PL_stderrgv)) &&
2341 (fp = IoOFP(io)))
2342 PerlIO_binmode(aTHX_ fp, IoTYPE(io), 0, ":utf8");
2343 if ((PL_unicode & PERL_UNICODE_INOUT_FLAG) &&
fafc274c
NC
2344 (sv = GvSV(gv_fetchpvs("\017PEN", GV_ADD|GV_NOTQUAL,
2345 SVt_PV)))) {
a05d7ebb
JH
2346 U32 in = PL_unicode & PERL_UNICODE_IN_FLAG;
2347 U32 out = PL_unicode & PERL_UNICODE_OUT_FLAG;
2348 if (in) {
2349 if (out)
76f68e9b 2350 sv_setpvs(sv, ":utf8\0:utf8");
a05d7ebb 2351 else
76f68e9b 2352 sv_setpvs(sv, ":utf8\0");
a05d7ebb
JH
2353 }
2354 else if (out)
76f68e9b 2355 sv_setpvs(sv, "\0:utf8");
a05d7ebb
JH
2356 SvSETMAGIC(sv);
2357 }
b310b053
JH
2358 }
2359 }
e27b5b51 2360#endif
b310b053 2361
c7030b81
NC
2362 {
2363 const char *s;
4ffa73a3
JH
2364 if ((s = PerlEnv_getenv("PERL_SIGNALS"))) {
2365 if (strEQ(s, "unsafe"))
2366 PL_signals |= PERL_SIGNALS_UNSAFE_FLAG;
2367 else if (strEQ(s, "safe"))
2368 PL_signals &= ~PERL_SIGNALS_UNSAFE_FLAG;
2369 else
2370 Perl_croak(aTHX_ "PERL_SIGNALS illegal: \"%s\"", s);
2371 }
c7030b81 2372 }
4ffa73a3 2373
81d86705 2374
87606032 2375 lex_start(linestr_sv, rsfp, lex_start_flags);
d2687c98 2376 SvREFCNT_dec(linestr_sv);
95670bde 2377
219f7226 2378 PL_subname = newSVpvs("main");
6224f72b 2379
5486870f
DM
2380 if (add_read_e_script)
2381 filter_add(read_e_script, NULL);
2382
6224f72b
GS
2383 /* now parse the script */
2384
93189314 2385 SETERRNO(0,SS_NORMAL);
28ac2b49 2386 if (yyparse(GRAMPROG) || PL_parser->error_count) {
3280af22 2387 if (PL_minus_c)
cea2e8a9 2388 Perl_croak(aTHX_ "%s had compilation errors.\n", PL_origfilename);
6224f72b 2389 else {
cea2e8a9 2390 Perl_croak(aTHX_ "Execution of %s aborted due to compilation errors.\n",
097ee67d 2391 PL_origfilename);
6224f72b
GS
2392 }
2393 }
57843af0 2394 CopLINE_set(PL_curcop, 0);
03d9f026 2395 SET_CURSTASH(PL_defstash);
3280af22
NIS
2396 if (PL_e_script) {
2397 SvREFCNT_dec(PL_e_script);
a0714e2c 2398 PL_e_script = NULL;
6224f72b
GS
2399 }
2400
3280af22 2401 if (PL_do_undump)
6224f72b
GS
2402 my_unexec();
2403
57843af0
GS
2404 if (isWARN_ONCE) {
2405 SAVECOPFILE(PL_curcop);
2406 SAVECOPLINE(PL_curcop);
3280af22 2407 gv_check(PL_defstash);
57843af0 2408 }
6224f72b
GS
2409
2410 LEAVE;
2411 FREETMPS;
2412
2413#ifdef MYMALLOC
f6a607bc
RGS
2414 {
2415 const char *s;
22ff3130
HS
2416 UV uv;
2417 s = PerlEnv_getenv("PERL_DEBUG_MSTATS");
2418 if (s && grok_atoUV(s, &uv, NULL) && uv >= 2)
96e440d2 2419 dump_mstats("after compilation:");
f6a607bc 2420 }
6224f72b
GS
2421#endif
2422
2423 ENTER;
febb3a6d 2424 PL_restartjmpenv = NULL;
3280af22 2425 PL_restartop = 0;
312caa8e 2426 return NULL;
6224f72b
GS
2427}
2428
954c1994
GS
2429/*
2430=for apidoc perl_run
2431
2432Tells a Perl interpreter to run. See L<perlembed>.
2433
2434=cut
2435*/
2436
6224f72b 2437int
0cb96387 2438perl_run(pTHXx)
6224f72b 2439{
6224f72b 2440 I32 oldscope;
14dd3ad8 2441 int ret = 0;
db36c5a1 2442 dJMPENV;
6224f72b 2443
7918f24d
NC
2444 PERL_ARGS_ASSERT_PERL_RUN;
2445#ifndef MULTIPLICITY
ed6c66dd 2446 PERL_UNUSED_ARG(my_perl);
7918f24d 2447#endif
9d4ba2ae 2448
3280af22 2449 oldscope = PL_scopestack_ix;
96e176bf
CL
2450#ifdef VMS
2451 VMSISH_HUSHED = 0;
2452#endif
6224f72b 2453
14dd3ad8 2454 JMPENV_PUSH(ret);
6224f72b
GS
2455 switch (ret) {
2456 case 1:
2457 cxstack_ix = -1; /* start context stack again */
312caa8e 2458 goto redo_body;
14dd3ad8 2459 case 0: /* normal completion */
14dd3ad8
GS
2460 redo_body:
2461 run_body(oldscope);
924ba076 2462 /* FALLTHROUGH */
14dd3ad8 2463 case 2: /* my_exit() */
3280af22 2464 while (PL_scopestack_ix > oldscope)
6224f72b
GS
2465 LEAVE;
2466 FREETMPS;
03d9f026 2467 SET_CURSTASH(PL_defstash);
3a1ee7e8 2468 if (!(PL_exit_flags & PERL_EXIT_DESTRUCT_END) &&
9ebf26ad 2469 PL_endav && !PL_minus_c) {
ca7b837b 2470 PERL_SET_PHASE(PERL_PHASE_END);
31d77e54 2471 call_list(oldscope, PL_endav);
9ebf26ad 2472 }
6224f72b
GS
2473#ifdef MYMALLOC
2474 if (PerlEnv_getenv("PERL_DEBUG_MSTATS"))
2475 dump_mstats("after execution: ");
2476#endif
37038d91 2477 ret = STATUS_EXIT;
14dd3ad8 2478 break;
6224f72b 2479 case 3:
312caa8e
CS
2480 if (PL_restartop) {
2481 POPSTACK_TO(PL_mainstack);
2482 goto redo_body;
6224f72b 2483 }
5637ef5b 2484 PerlIO_printf(Perl_error_log, "panic: restartop in perl_run\n");
312caa8e 2485 FREETMPS;
14dd3ad8
GS
2486 ret = 1;
2487 break;
6224f72b
GS
2488 }
2489
14dd3ad8
GS
2490 JMPENV_POP;
2491 return ret;
312caa8e
CS
2492}
2493
dd374669 2494STATIC void
14dd3ad8
GS
2495S_run_body(pTHX_ I32 oldscope)
2496{
d3b97530
DM
2497 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s $` $& $' support (0x%x).\n",
2498 PL_sawampersand ? "Enabling" : "Omitting",
2499 (unsigned int)(PL_sawampersand)));
6224f72b 2500
3280af22 2501 if (!PL_restartop) {
cf2782cd 2502#ifdef DEBUGGING
f0e3f042
CS
2503 if (DEBUG_x_TEST || DEBUG_B_TEST)
2504 dump_all_perl(!DEBUG_B_TEST);
ecae49c0
NC
2505 if (!DEBUG_q_TEST)
2506 PERL_DEBUG(PerlIO_printf(Perl_debug_log, "\nEXECUTING...\n\n"));
cf2782cd 2507#endif
6224f72b 2508
3280af22 2509 if (PL_minus_c) {
bf49b057 2510 PerlIO_printf(Perl_error_log, "%s syntax OK\n", PL_origfilename);
6224f72b
GS
2511 my_exit(0);
2512 }
3280af22 2513 if (PERLDB_SINGLE && PL_DBsingle)
a6d69523 2514 PL_DBsingle_iv = 1;
9ebf26ad 2515 if (PL_initav) {
ca7b837b 2516 PERL_SET_PHASE(PERL_PHASE_INIT);
3280af22 2517 call_list(oldscope, PL_initav);
9ebf26ad 2518 }
f1fac472 2519#ifdef PERL_DEBUG_READONLY_OPS
3107b51f
FC
2520 if (PL_main_root && PL_main_root->op_slabbed)
2521 Slab_to_ro(OpSLAB(PL_main_root));
f1fac472 2522#endif
6224f72b
GS
2523 }
2524
2525 /* do it */
2526
ca7b837b 2527 PERL_SET_PHASE(PERL_PHASE_RUN);
9ebf26ad 2528
3280af22 2529 if (PL_restartop) {
febb3a6d 2530 PL_restartjmpenv = NULL;
533c011a 2531 PL_op = PL_restartop;
3280af22 2532 PL_restartop = 0;
cea2e8a9 2533 CALLRUNOPS(aTHX);
6224f72b 2534 }
3280af22
NIS
2535 else if (PL_main_start) {
2536 CvDEPTH(PL_main_cv) = 1;
533c011a 2537 PL_op = PL_main_start;
cea2e8a9 2538 CALLRUNOPS(aTHX);
6224f72b 2539 }
f6b3007c 2540 my_exit(0);
e5964223 2541 NOT_REACHED; /* NOTREACHED */
6224f72b
GS
2542}
2543
954c1994 2544/*
ccfc67b7
JH
2545=head1 SV Manipulation Functions
2546
954c1994
GS
2547=for apidoc p||get_sv
2548
64ace3f8 2549Returns the SV of the specified Perl scalar. C<flags> are passed to
72d33970 2550C<gv_fetchpv>. If C<GV_ADD> is set and the
64ace3f8
NC
2551Perl variable does not exist then it will be created. If C<flags> is zero
2552and the variable does not exist then NULL is returned.
954c1994
GS
2553
2554=cut
2555*/
2556
6224f72b 2557SV*
64ace3f8 2558Perl_get_sv(pTHX_ const char *name, I32 flags)
6224f72b
GS
2559{
2560 GV *gv;
7918f24d
NC
2561
2562 PERL_ARGS_ASSERT_GET_SV;
2563
64ace3f8 2564 gv = gv_fetchpv(name, flags, SVt_PV);
6224f72b
GS
2565 if (gv)
2566 return GvSV(gv);
a0714e2c 2567 return NULL;
6224f72b
GS
2568}
2569
954c1994 2570/*
ccfc67b7
JH
2571=head1 Array Manipulation Functions
2572
954c1994
GS
2573=for apidoc p||get_av
2574
f0b90de1
SF
2575Returns the AV of the specified Perl global or package array with the given
2576name (so it won't work on lexical variables). C<flags> are passed
72d33970 2577to C<gv_fetchpv>. If C<GV_ADD> is set and the
cbfd0a87
NC
2578Perl variable does not exist then it will be created. If C<flags> is zero
2579and the variable does not exist then NULL is returned.
954c1994 2580
f0b90de1
SF
2581Perl equivalent: C<@{"$name"}>.
2582
954c1994
GS
2583=cut
2584*/
2585
6224f72b 2586AV*
cbfd0a87 2587Perl_get_av(pTHX_ const char *name, I32 flags)
6224f72b 2588{
cbfd0a87 2589 GV* const gv = gv_fetchpv(name, flags, SVt_PVAV);
7918f24d
NC
2590
2591 PERL_ARGS_ASSERT_GET_AV;
2592
cbfd0a87 2593 if (flags)
6224f72b
GS
2594 return GvAVn(gv);
2595 if (gv)
2596 return GvAV(gv);
7d49f689 2597 return NULL;
6224f72b
GS
2598}
2599
954c1994 2600/*
ccfc67b7
JH
2601=head1 Hash Manipulation Functions
2602
954c1994
GS
2603=for apidoc p||get_hv
2604
6673a63c 2605Returns the HV of the specified Perl hash. C<flags> are passed to
72d33970 2606C<gv_fetchpv>. If C<GV_ADD> is set and the
6673a63c 2607Perl variable does not exist then it will be created. If C<flags> is zero
796b6530 2608and the variable does not exist then C<NULL> is returned.
954c1994
GS
2609
2610=cut
2611*/
2612
6224f72b 2613HV*
6673a63c 2614Perl_get_hv(pTHX_ const char *name, I32 flags)
6224f72b 2615{
6673a63c 2616 GV* const gv = gv_fetchpv(name, flags, SVt_PVHV);
7918f24d
NC
2617
2618 PERL_ARGS_ASSERT_GET_HV;
2619
6673a63c 2620 if (flags)
a0d0e21e
LW
2621 return GvHVn(gv);
2622 if (gv)
2623 return GvHV(gv);
5c284bb0 2624 return NULL;
a0d0e21e
LW
2625}
2626
954c1994 2627/*
ccfc67b7
JH
2628=head1 CV Manipulation Functions
2629
780a5241
NC
2630=for apidoc p||get_cvn_flags
2631
2632Returns the CV of the specified Perl subroutine. C<flags> are passed to
72d33970 2633C<gv_fetchpvn_flags>. If C<GV_ADD> is set and the Perl subroutine does not
780a5241
NC
2634exist then it will be declared (which has the same effect as saying
2635C<sub name;>). If C<GV_ADD> is not set and the subroutine does not exist
2636then NULL is returned.
2637
954c1994
GS
2638=for apidoc p||get_cv
2639
780a5241 2640Uses C<strlen> to get the length of C<name>, then calls C<get_cvn_flags>.
954c1994
GS
2641
2642=cut
2643*/
2644
a0d0e21e 2645CV*
780a5241 2646Perl_get_cvn_flags(pTHX_ const char *name, STRLEN len, I32 flags)
a0d0e21e 2647{
780a5241 2648 GV* const gv = gv_fetchpvn_flags(name, len, flags, SVt_PVCV);
7918f24d
NC
2649
2650 PERL_ARGS_ASSERT_GET_CVN_FLAGS;
2651
334dda80
FC
2652 /* XXX this is probably not what they think they're getting.
2653 * It has the same effect as "sub name;", i.e. just a forward
2654 * declaration! */
780a5241 2655 if ((flags & ~GV_NOADD_MASK) && !GvCVu(gv)) {
186a5ba8 2656 return newSTUB(gv,0);
780a5241 2657 }
a0d0e21e 2658 if (gv)
8ebc5c01 2659 return GvCVu(gv);
601f1833 2660 return NULL;
a0d0e21e
LW
2661}
2662
2c67934f
NC
2663/* Nothing in core calls this now, but we can't replace it with a macro and
2664 move it to mathoms.c as a macro would evaluate name twice. */
780a5241
NC
2665CV*
2666Perl_get_cv(pTHX_ const char *name, I32 flags)
2667{
7918f24d
NC
2668 PERL_ARGS_ASSERT_GET_CV;
2669
780a5241
NC
2670 return get_cvn_flags(name, strlen(name), flags);
2671}
2672
79072805
LW
2673/* Be sure to refetch the stack pointer after calling these routines. */
2674
954c1994 2675/*
ccfc67b7
JH
2676
2677=head1 Callback Functions
2678
954c1994
GS
2679=for apidoc p||call_argv
2680
f0b90de1 2681Performs a callback to the specified named and package-scoped Perl subroutine
796b6530 2682with C<argv> (a C<NULL>-terminated array of strings) as arguments. See
72d33970 2683L<perlcall>.
f0b90de1
SF
2684
2685Approximate Perl equivalent: C<&{"$sub_name"}(@$argv)>.
954c1994
GS
2686
2687=cut
2688*/
2689
a0d0e21e 2690I32
5aaab254 2691Perl_call_argv(pTHX_ const char *sub_name, I32 flags, char **argv)
ac27b0f5 2692
8ac85365
NIS
2693 /* See G_* flags in cop.h */
2694 /* null terminated arg list */
8990e307 2695{
a0d0e21e 2696 dSP;
8990e307 2697
7918f24d
NC
2698 PERL_ARGS_ASSERT_CALL_ARGV;
2699
924508f0 2700 PUSHMARK(SP);
3dc78631
DM
2701 while (*argv) {
2702 mXPUSHs(newSVpv(*argv,0));
2703 argv++;
8990e307 2704 }
3dc78631 2705 PUTBACK;
864dbfa3 2706 return call_pv(sub_name, flags);
8990e307
LW
2707}
2708
954c1994
GS
2709/*
2710=for apidoc p||call_pv
2711
2712Performs a callback to the specified Perl sub. See L<perlcall>.
2713
2714=cut
2715*/
2716
a0d0e21e 2717I32
864dbfa3 2718Perl_call_pv(pTHX_ const char *sub_name, I32 flags)
8ac85365
NIS
2719 /* name of the subroutine */
2720 /* See G_* flags in cop.h */
a0d0e21e 2721{
7918f24d
NC
2722 PERL_ARGS_ASSERT_CALL_PV;
2723
0da0e728 2724 return call_sv(MUTABLE_SV(get_cv(sub_name, GV_ADD)), flags);
a0d0e21e
LW
2725}
2726
954c1994
GS
2727/*
2728=for apidoc p||call_method
2729
2730Performs a callback to the specified Perl method. The blessed object must
2731be on the stack. See L<perlcall>.
2732
2733=cut
2734*/
2735
a0d0e21e 2736I32
864dbfa3 2737Perl_call_method(pTHX_ const char *methname, I32 flags)
8ac85365
NIS
2738 /* name of the subroutine */
2739 /* See G_* flags in cop.h */
a0d0e21e 2740{
46ca9bac 2741 STRLEN len;
c106c2be 2742 SV* sv;
7918f24d
NC
2743 PERL_ARGS_ASSERT_CALL_METHOD;
2744
46ca9bac 2745 len = strlen(methname);
c106c2be
RZ
2746 sv = flags & G_METHOD_NAMED
2747 ? sv_2mortal(newSVpvn_share(methname, len,0))
2748 : newSVpvn_flags(methname, len, SVs_TEMP);
46ca9bac 2749
c106c2be 2750 return call_sv(sv, flags | G_METHOD);
a0d0e21e
LW
2751}
2752
2753/* May be called with any of a CV, a GV, or an SV containing the name. */
954c1994
GS
2754/*
2755=for apidoc p||call_sv
2756
078e2213
TC
2757Performs a callback to the Perl sub specified by the SV.
2758
7c0c544c 2759If neither the C<G_METHOD> nor C<G_METHOD_NAMED> flag is supplied, the
078e2213
TC
2760SV may be any of a CV, a GV, a reference to a CV, a reference to a GV
2761or C<SvPV(sv)> will be used as the name of the sub to call.
2762
2763If the C<G_METHOD> flag is supplied, the SV may be a reference to a CV or
2764C<SvPV(sv)> will be used as the name of the method to call.
2765
2766If the C<G_METHOD_NAMED> flag is supplied, C<SvPV(sv)> will be used as
2767the name of the method to call.
2768
2769Some other values are treated specially for internal use and should
2770not be depended on.
2771
2772See L<perlcall>.
954c1994
GS
2773
2774=cut
2775*/
2776
a0d0e21e 2777I32
001d637e 2778Perl_call_sv(pTHX_ SV *sv, VOL I32 flags)
8ac85365 2779 /* See G_* flags in cop.h */
a0d0e21e 2780{
5b434c73 2781 dVAR;
a0d0e21e 2782 LOGOP myop; /* fake syntax tree node */
b46e009d 2783 METHOP method_op;
aa689395 2784 I32 oldmark;
8ea43dc8 2785 VOL I32 retval = 0;
54310121 2786 bool oldcatch = CATCH_GET;
6224f72b 2787 int ret;
c4420975 2788 OP* const oldop = PL_op;
db36c5a1 2789 dJMPENV;
1e422769 2790
7918f24d
NC
2791 PERL_ARGS_ASSERT_CALL_SV;
2792
a0d0e21e
LW
2793 if (flags & G_DISCARD) {
2794 ENTER;
2795 SAVETMPS;
2796 }
2f8edad0
NC
2797 if (!(flags & G_WANT)) {
2798 /* Backwards compatibility - as G_SCALAR was 0, it could be omitted.
2799 */
2800 flags |= G_SCALAR;
2801 }
a0d0e21e 2802
aa689395 2803 Zero(&myop, 1, LOGOP);
f51d4af5 2804 if (!(flags & G_NOARGS))
aa689395 2805 myop.op_flags |= OPf_STACKED;
4f911530 2806 myop.op_flags |= OP_GIMME_REVERSE(flags);
462e5cf6 2807 SAVEOP();
533c011a 2808 PL_op = (OP*)&myop;
aa689395 2809
8c9009ad 2810 if (!(flags & G_METHOD_NAMED)) {
5b434c73
DD
2811 dSP;
2812 EXTEND(SP, 1);
8c9009ad
DD
2813 PUSHs(sv);
2814 PUTBACK;
5b434c73 2815 }
aa689395 2816 oldmark = TOPMARK;
a0d0e21e 2817
3280af22 2818 if (PERLDB_SUB && PL_curstash != PL_debstash
36477c24 2819 /* Handle first BEGIN of -d. */
3280af22 2820 && (PL_DBcv || (PL_DBcv = GvCV(PL_DBsub)))
36477c24 2821 /* Try harder, since this may have been a sighandler, thus
2822 * curstash may be meaningless. */
ea726b52 2823 && (SvTYPE(sv) != SVt_PVCV || CvSTASH((const CV *)sv) != PL_debstash)
491527d0 2824 && !(flags & G_NODEBUG))
5ff48db8 2825 myop.op_private |= OPpENTERSUB_DB;
a0d0e21e 2826
c106c2be 2827 if (flags & (G_METHOD|G_METHOD_NAMED)) {
b46e009d 2828 Zero(&method_op, 1, METHOP);
2829 method_op.op_next = (OP*)&myop;
2830 PL_op = (OP*)&method_op;
c106c2be 2831 if ( flags & G_METHOD_NAMED ) {
b46e009d 2832 method_op.op_ppaddr = PL_ppaddr[OP_METHOD_NAMED];
2833 method_op.op_type = OP_METHOD_NAMED;
2834 method_op.op_u.op_meth_sv = sv;
c106c2be 2835 } else {
b46e009d 2836 method_op.op_ppaddr = PL_ppaddr[OP_METHOD];
2837 method_op.op_type = OP_METHOD;
c106c2be
RZ
2838 }
2839 myop.op_ppaddr = PL_ppaddr[OP_ENTERSUB];
2840 myop.op_type = OP_ENTERSUB;
968b3946
GS
2841 }
2842
312caa8e 2843 if (!(flags & G_EVAL)) {
0cdb2077 2844 CATCH_SET(TRUE);
d6f07c05 2845 CALL_BODY_SUB((OP*)&myop);
312caa8e 2846 retval = PL_stack_sp - (PL_stack_base + oldmark);
0253cb41 2847 CATCH_SET(oldcatch);
312caa8e
CS
2848 }
2849 else {
8e90e786 2850 I32 old_cxix;
d78bda3d 2851 myop.op_other = (OP*)&myop;
101d6365 2852 (void)POPMARK;
8e90e786 2853 old_cxix = cxstack_ix;
274ed8ae 2854 create_eval_scope(NULL, flags|G_FAKINGEVAL);
c318a6ee 2855 INCMARK;
a0d0e21e 2856
14dd3ad8 2857 JMPENV_PUSH(ret);
edb2152a 2858
6224f72b
GS
2859 switch (ret) {
2860 case 0:
14dd3ad8 2861 redo_body:
d6f07c05 2862 CALL_BODY_SUB((OP*)&myop);
312caa8e 2863 retval = PL_stack_sp - (PL_stack_base + oldmark);
8433848b 2864 if (!(flags & G_KEEPERR)) {
ab69dbc2 2865 CLEAR_ERRSV();
8433848b 2866 }
a0d0e21e 2867 break;
6224f72b 2868 case 1:
f86702cc 2869 STATUS_ALL_FAILURE;
924ba076 2870 /* FALLTHROUGH */
6224f72b 2871 case 2:
a0d0e21e 2872 /* my_exit() was called */
03d9f026 2873 SET_CURSTASH(PL_defstash);
a0d0e21e 2874 FREETMPS;
14dd3ad8 2875 JMPENV_POP;
f86702cc 2876 my_exit_jump();
e5964223 2877 NOT_REACHED; /* NOTREACHED */
6224f72b 2878 case 3:
3280af22 2879 if (PL_restartop) {
febb3a6d 2880 PL_restartjmpenv = NULL;
533c011a 2881 PL_op = PL_restartop;
3280af22 2882 PL_restartop = 0;
312caa8e 2883 goto redo_body;
a0d0e21e 2884 }
3280af22 2885 PL_stack_sp = PL_stack_base + oldmark;
51ce5529 2886 if ((flags & G_WANT) == G_ARRAY)
a0d0e21e
LW
2887 retval = 0;
2888 else {
2889 retval = 1;
3280af22 2890 *++PL_stack_sp = &PL_sv_undef;
a0d0e21e 2891 }
312caa8e 2892 break;
a0d0e21e 2893 }
a0d0e21e 2894
8e90e786
DM
2895 /* if we croaked, depending on how we croaked the eval scope
2896 * may or may not have already been popped */
2897 if (cxstack_ix > old_cxix) {
2898 assert(cxstack_ix == old_cxix + 1);
4ebe6e95 2899 assert(CxTYPE(CX_CUR()) == CXt_EVAL);
edb2152a 2900 delete_eval_scope();
8e90e786 2901 }
14dd3ad8 2902 JMPENV_POP;
a0d0e21e 2903 }
1e422769 2904
a0d0e21e 2905 if (flags & G_DISCARD) {
3280af22 2906 PL_stack_sp = PL_stack_base + oldmark;
a0d0e21e
LW
2907 retval = 0;
2908 FREETMPS;
2909 LEAVE;
2910 }
533c011a 2911 PL_op = oldop;
a0d0e21e
LW
2912 return retval;
2913}
2914
6e72f9df 2915/* Eval a string. The G_EVAL flag is always assumed. */
8990e307 2916
954c1994
GS
2917/*
2918=for apidoc p||eval_sv
2919
72d33970 2920Tells Perl to C<eval> the string in the SV. It supports the same flags
796b6530 2921as C<call_sv>, with the obvious exception of C<G_EVAL>. See L<perlcall>.
954c1994
GS
2922
2923=cut
2924*/
2925
a0d0e21e 2926I32
864dbfa3 2927Perl_eval_sv(pTHX_ SV *sv, I32 flags)
ac27b0f5 2928
8ac85365 2929 /* See G_* flags in cop.h */
a0d0e21e 2930{
97aff369 2931 dVAR;
a0d0e21e 2932 UNOP myop; /* fake syntax tree node */
5b434c73 2933 VOL I32 oldmark;
8ea43dc8 2934 VOL I32 retval = 0;
6224f72b 2935 int ret;
c4420975 2936 OP* const oldop = PL_op;
db36c5a1 2937 dJMPENV;
84902520 2938
7918f24d
NC
2939 PERL_ARGS_ASSERT_EVAL_SV;
2940
4633a7c4
LW
2941 if (flags & G_DISCARD) {
2942 ENTER;
2943 SAVETMPS;
2944 }
2945
462e5cf6 2946 SAVEOP();
533c011a 2947 PL_op = (OP*)&myop;
5ff48db8 2948 Zero(&myop, 1, UNOP);
5b434c73
DD
2949 {
2950 dSP;
2951 oldmark = SP - PL_stack_base;
2952 EXTEND(SP, 1);
2953 PUSHs(sv);
2954 PUTBACK;
2955 }
79072805 2956
4633a7c4
LW
2957 if (!(flags & G_NOARGS))
2958 myop.op_flags = OPf_STACKED;
6e72f9df 2959 myop.op_type = OP_ENTEREVAL;
4f911530 2960 myop.op_flags |= OP_GIMME_REVERSE(flags);
6e72f9df 2961 if (flags & G_KEEPERR)
2962 myop.op_flags |= OPf_SPECIAL;
a1941760
DM
2963
2964 if (flags & G_RE_REPARSING)
2965 myop.op_private = (OPpEVAL_COPHH | OPpEVAL_RE_REPARSING);
4633a7c4 2966
dedbcade 2967 /* fail now; otherwise we could fail after the JMPENV_PUSH but
13febba5 2968 * before a cx_pusheval(), which corrupts the stack after a croak */
dedbcade
DM
2969 TAINT_PROPER("eval_sv()");
2970
14dd3ad8 2971 JMPENV_PUSH(ret);
6224f72b
GS
2972 switch (ret) {
2973 case 0:
14dd3ad8 2974 redo_body:
2ba65d5f
DM
2975 if (PL_op == (OP*)(&myop)) {
2976 PL_op = PL_ppaddr[OP_ENTEREVAL](aTHX);
2977 if (!PL_op)
2978 goto fail; /* failed in compilation */
2979 }
4aca2f62 2980 CALLRUNOPS(aTHX);
312caa8e 2981 retval = PL_stack_sp - (PL_stack_base + oldmark);
8433848b 2982 if (!(flags & G_KEEPERR)) {
ab69dbc2 2983 CLEAR_ERRSV();
8433848b 2984 }
4633a7c4 2985 break;
6224f72b 2986 case 1:
f86702cc 2987 STATUS_ALL_FAILURE;
924ba076 2988 /* FALLTHROUGH */
6224f72b 2989 case 2:
4633a7c4 2990 /* my_exit() was called */
03d9f026 2991 SET_CURSTASH(PL_defstash);
4633a7c4 2992 FREETMPS;
14dd3ad8 2993 JMPENV_POP;
f86702cc 2994 my_exit_jump();
e5964223 2995 NOT_REACHED; /* NOTREACHED */
6224f72b 2996 case 3:
3280af22 2997 if (PL_restartop) {
febb3a6d 2998 PL_restartjmpenv = NULL;
533c011a 2999 PL_op = PL_restartop;
3280af22 3000 PL_restartop = 0;
312caa8e 3001 goto redo_body;
4633a7c4 3002 }
4aca2f62 3003 fail:
3280af22 3004 PL_stack_sp = PL_stack_base + oldmark;
51ce5529 3005 if ((flags & G_WANT) == G_ARRAY)
4633a7c4
LW
3006 retval = 0;
3007 else {
3008 retval = 1;
3280af22 3009 *++PL_stack_sp = &PL_sv_undef;
4633a7c4 3010 }
312caa8e 3011 break;
4633a7c4
LW
3012 }
3013
14dd3ad8 3014 JMPENV_POP;
4633a7c4 3015 if (flags & G_DISCARD) {
3280af22 3016 PL_stack_sp = PL_stack_base + oldmark;
4633a7c4
LW
3017 retval = 0;
3018 FREETMPS;
3019 LEAVE;
3020 }
533c011a 3021 PL_op = oldop;
4633a7c4
LW
3022 return retval;
3023}
3024
954c1994
GS
3025/*
3026=for apidoc p||eval_pv
3027
422791e4 3028Tells Perl to C<eval> the given string in scalar context and return an SV* result.
954c1994
GS
3029
3030=cut
3031*/
3032
137443ea 3033SV*
864dbfa3 3034Perl_eval_pv(pTHX_ const char *p, I32 croak_on_error)
137443ea 3035{
137443ea 3036 SV* sv = newSVpv(p, 0);
3037
7918f24d
NC
3038 PERL_ARGS_ASSERT_EVAL_PV;
3039
864dbfa3 3040 eval_sv(sv, G_SCALAR);
137443ea 3041 SvREFCNT_dec(sv);
3042
ed1786ad
DD
3043 {
3044 dSP;
3045 sv = POPs;
3046 PUTBACK;
3047 }
137443ea 3048
eed484f9
DD
3049 /* just check empty string or undef? */
3050 if (croak_on_error) {
3051 SV * const errsv = ERRSV;
3052 if(SvTRUE_NN(errsv))
3053 /* replace with croak_sv? */
3054 Perl_croak_nocontext("%s", SvPV_nolen_const(errsv));
2d8e6c8d 3055 }
137443ea 3056
3057 return sv;
3058}
3059
4633a7c4
LW
3060/* Require a module. */
3061
954c1994 3062/*
ccfc67b7
JH
3063=head1 Embedding Functions
3064
954c1994
GS
3065=for apidoc p||require_pv
3066
7d3fb230
BS
3067Tells Perl to C<require> the file named by the string argument. It is
3068analogous to the Perl code C<eval "require '$file'">. It's even
2307c6d0 3069implemented that way; consider using load_module instead.
954c1994 3070
7d3fb230 3071=cut */
954c1994 3072
4633a7c4 3073void
864dbfa3 3074Perl_require_pv(pTHX_ const char *pv)
4633a7c4 3075{
d3acc0f7 3076 dSP;
97aff369 3077 SV* sv;
7918f24d
NC
3078
3079 PERL_ARGS_ASSERT_REQUIRE_PV;
3080
e788e7d3 3081 PUSHSTACKi(PERLSI_REQUIRE);
be41e5d9
NC
3082 sv = Perl_newSVpvf(aTHX_ "require q%c%s%c", 0, pv, 0);
3083 eval_sv(sv_2mortal(sv), G_DISCARD);
d3acc0f7 3084 POPSTACK;
79072805
LW
3085}
3086
76e3520e 3087STATIC void
b6f82619 3088S_usage(pTHX) /* XXX move this out into a module ? */
4633a7c4 3089{
ab821d7f 3090 /* This message really ought to be max 23 lines.
75c72d73 3091 * Removed -h because the user already knows that option. Others? */
fb73857a 3092
1566c39d
NC
3093 /* Grouped as 6 lines per C string literal, to keep under the ANSI C 89
3094 minimum of 509 character string literals. */
27da23d5 3095 static const char * const usage_msg[] = {
1566c39d
NC
3096" -0[octal] specify record separator (\\0, if no argument)\n"
3097" -a autosplit mode with -n or -p (splits $_ into @F)\n"
3098" -C[number/list] enables the listed Unicode features\n"
3099" -c check syntax only (runs BEGIN and CHECK blocks)\n"
3100" -d[:debugger] run program under debugger\n"
3101" -D[number/list] set debugging flags (argument is a bit mask or alphabets)\n",
3102" -e program one line of program (several -e's allowed, omit programfile)\n"
3103" -E program like -e, but enables all optional features\n"
3104" -f don't do $sitelib/sitecustomize.pl at startup\n"
3105" -F/pattern/ split() pattern for -a switch (//'s are optional)\n"
3106" -i[extension] edit <> files in place (makes backup if extension supplied)\n"
3107" -Idirectory specify @INC/#include directory (several -I's allowed)\n",
3108" -l[octal] enable line ending processing, specifies line terminator\n"
3109" -[mM][-]module execute \"use/no module...\" before executing program\n"
3110" -n assume \"while (<>) { ... }\" loop around program\n"
3111" -p assume loop like -n but print line also, like sed\n"
3112" -s enable rudimentary parsing for switches after programfile\n"
3113" -S look for programfile using PATH environment variable\n",
3114" -t enable tainting warnings\n"
3115" -T enable tainting checks\n"
3116" -u dump core after parsing program\n"
3117" -U allow unsafe operations\n"
3118" -v print version, patchlevel and license\n"
3119" -V[:variable] print configuration summary (or a single Config.pm variable)\n",
60eaec42 3120" -w enable many useful warnings\n"
1566c39d
NC
3121" -W enable all warnings\n"
3122" -x[directory] ignore text before #!perl line (optionally cd to directory)\n"
3123" -X disable all warnings\n"
3124" \n"
3125"Run 'perldoc perl' for more help with Perl.\n\n",
fb73857a 3126NULL
3127};
27da23d5 3128 const char * const *p = usage_msg;
1566c39d 3129 PerlIO *out = PerlIO_stdout();
fb73857a 3130
1566c39d
NC
3131 PerlIO_printf(out,
3132 "\nUsage: %s [switches] [--] [programfile] [arguments]\n",
b6f82619 3133 PL_origargv[0]);
fb73857a 3134 while (*p)
1566c39d 3135 PerlIO_puts(out, *p++);
b6f82619 3136 my_exit(0);
4633a7c4
LW
3137}
3138
b4ab917c
DM
3139/* convert a string of -D options (or digits) into an int.
3140 * sets *s to point to the char after the options */
3141
3142#ifdef DEBUGGING
3143int
e1ec3a88 3144Perl_get_debug_opts(pTHX_ const char **s, bool givehelp)
b4ab917c 3145{
27da23d5 3146 static const char * const usage_msgd[] = {
651b8f1a
NC
3147 " Debugging flag values: (see also -d)\n"
3148 " p Tokenizing and parsing (with v, displays parse stack)\n"
3149 " s Stack snapshots (with v, displays all stacks)\n"
3150 " l Context (loop) stack processing\n"
3151 " t Trace execution\n"
3152 " o Method and overloading resolution\n",
3153 " c String/numeric conversions\n"
3154 " P Print profiling info, source file input state\n"
3155 " m Memory and SV allocation\n"
3156 " f Format processing\n"
3157 " r Regular expression parsing and execution\n"
3158 " x Syntax tree dump\n",
3159 " u Tainting checks\n"
3160 " H Hash dump -- usurps values()\n"
3161 " X Scratchpad allocation\n"
3162 " D Cleaning up\n"
56967202 3163 " S Op slab allocation\n"
651b8f1a
NC
3164 " T Tokenising\n"
3165 " R Include reference counts of dumped variables (eg when using -Ds)\n",
3166 " J Do not s,t,P-debug (Jump over) opcodes within package DB\n"
3167 " v Verbose: use in conjunction with other flags\n"
3168 " C Copy On Write\n"
3169 " A Consistency checks on internal structures\n"
3170 " q quiet - currently only suppresses the 'EXECUTING' message\n"
3171 " M trace smart match resolution\n"
3172 " B dump suBroutine definitions, including special Blocks like BEGIN\n",
69014004 3173 " L trace some locale setting information--for Perl core development\n",
e17bc05a 3174 " i trace PerlIO layer processing\n",
e6e64d9b
JC
3175 NULL
3176 };
22ff3130 3177 UV uv = 0;
7918f24d
NC
3178
3179 PERL_ARGS_ASSERT_GET_DEBUG_OPTS;
3180
b4ab917c
DM
3181 if (isALPHA(**s)) {
3182 /* if adding extra options, remember to update DEBUG_MASK */
e17bc05a 3183 static const char debopts[] = "psltocPmfrxuUHXDSTRJvCAqMBLi";
b4ab917c 3184
0eb30aeb 3185 for (; isWORDCHAR(**s); (*s)++) {
c4420975 3186 const char * const d = strchr(debopts,**s);
b4ab917c 3187 if (d)
22ff3130 3188 uv |= 1 << (d - debopts);
b4ab917c 3189 else if (ckWARN_d(WARN_DEBUGGING))
e6e64d9b
JC
3190 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING),
3191 "invalid option -D%c, use -D'' to see choices\n", **s);
b4ab917c
DM
3192 }
3193 }
e6e64d9b 3194 else if (isDIGIT(**s)) {
96e440d2 3195 const char* e;
22ff3130 3196 if (grok_atoUV(*s, &uv, &e))
96e440d2 3197 *s = e;
0eb30aeb 3198 for (; isWORDCHAR(**s); (*s)++) ;
b4ab917c 3199 }
ddcf8bc1 3200 else if (givehelp) {
06e869a4 3201 const char *const *p = usage_msgd;
651b8f1a 3202 while (*p) PerlIO_puts(PerlIO_stdout(), *p++);
e6e64d9b 3203 }
22ff3130 3204 return (int)uv; /* ignore any UV->int conversion loss */
b4ab917c
DM
3205}
3206#endif
3207
79072805
LW
3208/* This routine handles any switches that can be given during run */
3209
c7030b81
NC
3210const char *
3211Perl_moreswitches(pTHX_ const char *s)
79072805 3212{
27da23d5 3213 dVAR;
84c133a0 3214 UV rschar;
0544e6df 3215 const char option = *s; /* used to remember option in -m/-M code */
79072805 3216
7918f24d
NC
3217 PERL_ARGS_ASSERT_MORESWITCHES;
3218
79072805
LW
3219 switch (*s) {
3220 case '0':
a863c7d1 3221 {
f2095865 3222 I32 flags = 0;
a3b680e6 3223 STRLEN numlen;
f2095865
JH
3224
3225 SvREFCNT_dec(PL_rs);
3226 if (s[1] == 'x' && s[2]) {
a3b680e6 3227 const char *e = s+=2;
f2095865
JH
3228 U8 *tmps;
3229
a3b680e6
AL
3230 while (*e)
3231 e++;
f2095865
JH
3232 numlen = e - s;
3233 flags = PERL_SCAN_SILENT_ILLDIGIT;
3234 rschar = (U32)grok_hex(s, &numlen, &flags, NULL);
3235 if (s + numlen < e) {
3236 rschar = 0; /* Grandfather -0xFOO as -0 -xFOO. */
3237 numlen = 0;
3238 s--;
3239 }
396482e1 3240 PL_rs = newSVpvs("");
10656159 3241 tmps = (U8*) SvGROW(PL_rs, (STRLEN)(UVCHR_SKIP(rschar) + 1));
f2095865 3242 uvchr_to_utf8(tmps, rschar);
5f560d8a 3243 SvCUR_set(PL_rs, UVCHR_SKIP(rschar));
f2095865
JH
3244 SvUTF8_on(PL_rs);
3245 }
3246 else {
3247 numlen = 4;
3248 rschar = (U32)grok_oct(s, &numlen, &flags, NULL);
3249 if (rschar & ~((U8)~0))
3250 PL_rs = &PL_sv_undef;
3251 else if (!rschar && numlen >= 2)
396482e1 3252 PL_rs = newSVpvs("");
f2095865
JH
3253 else {
3254 char ch = (char)rschar;
3255 PL_rs = newSVpvn(&ch, 1);
3256 }
3257 }
64ace3f8 3258 sv_setsv(get_sv("/", GV_ADD), PL_rs);
f2095865 3259 return s + numlen;
a863c7d1 3260 }
46487f74 3261 case 'C':
a05d7ebb 3262 s++;
dd374669 3263 PL_unicode = parse_unicode_opts( (const char **)&s );
5a22a2bb
NC
3264 if (PL_unicode & PERL_UNICODE_UTF8CACHEASSERT_FLAG)
3265 PL_utf8cache = -1;
46487f74 3266 return s;
2304df62 3267 case 'F':
5fc691f1 3268 PL_minus_a = TRUE;
3280af22 3269 PL_minus_F = TRUE;
24ffa309 3270 PL_minus_n = TRUE;
ebce5377
RGS
3271 PL_splitstr = ++s;
3272 while (*s && !isSPACE(*s)) ++s;
e49e380e 3273 PL_splitstr = savepvn(PL_splitstr, s - PL_splitstr);
2304df62 3274 return s;
79072805 3275 case 'a':
3280af22 3276 PL_minus_a = TRUE;
24ffa309 3277 PL_minus_n = TRUE;
79072805
LW
3278 s++;
3279 return s;
3280 case 'c':
3280af22 3281 PL_minus_c = TRUE;
79072805
LW
3282 s++;
3283 return s;
3284 case 'd':
f20b2998 3285 forbid_setid('d', FALSE);
4633a7c4 3286 s++;
2cbb2ee1
RGS
3287
3288 /* -dt indicates to the debugger that threads will be used */
0eb30aeb 3289 if (*s == 't' && !isWORDCHAR(s[1])) {
2cbb2ee1
RGS
3290 ++s;
3291 my_setenv("PERL5DB_THREADED", "1");
3292 }
3293
70c94a19
RR
3294 /* The following permits -d:Mod to accepts arguments following an =
3295 in the fashion that -MSome::Mod does. */
3296 if (*s == ':' || *s == '=') {
b19934fb
NC
3297 const char *start;
3298 const char *end;
3299 SV *sv;
3300
3301 if (*++s == '-') {
3302 ++s;
3303 sv = newSVpvs("no Devel::");
3304 } else {
3305 sv = newSVpvs("use Devel::");
3306 }
3307
3308 start = s;
3309 end = s + strlen(s);
f85893a1 3310
b19934fb 3311 /* We now allow -d:Module=Foo,Bar and -d:-Module */
0eb30aeb 3312 while(isWORDCHAR(*s) || *s==':') ++s;
70c94a19 3313 if (*s != '=')
f85893a1 3314 sv_catpvn(sv, start, end - start);
70c94a19
RR
3315 else {
3316 sv_catpvn(sv, start, s-start);
95a2b409
RGS
3317 /* Don't use NUL as q// delimiter here, this string goes in the
3318 * environment. */
3319 Perl_sv_catpvf(aTHX_ sv, " split(/,/,q{%s});", ++s);
70c94a19 3320 }
f85893a1 3321 s = end;
184f32ec 3322 my_setenv("PERL5DB", SvPV_nolen_const(sv));
c4db126b 3323 SvREFCNT_dec(sv);
4633a7c4 3324 }
ed094faf 3325 if (!PL_perldb) {
3280af22 3326 PL_perldb = PERLDB_ALL;
a0d0e21e 3327 init_debugger();
ed094faf 3328 }
79072805
LW
3329 return s;
3330 case 'D':
0453d815 3331 {
79072805 3332#ifdef DEBUGGING
f20b2998 3333 forbid_setid('D', FALSE);
b4ab917c 3334 s++;
dd374669 3335 PL_debug = get_debug_opts( (const char **)&s, 1) | DEBUG_TOP_FLAG;
12a43e32 3336#else /* !DEBUGGING */
0453d815 3337 if (ckWARN_d(WARN_DEBUGGING))
9014280d 3338 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING),
e6e64d9b 3339 "Recompile perl with -DDEBUGGING to use -D switch (did you mean -d ?)\n");
0eb30aeb 3340 for (s++; isWORDCHAR(*s); s++) ;
79072805 3341#endif
79072805 3342 return s;
2b5060ae 3343 NOT_REACHED; /* NOTREACHED */
0453d815 3344 }
4633a7c4 3345 case 'h':
b6f82619 3346 usage();
2b5060ae
DM
3347 NOT_REACHED; /* NOTREACHED */
3348
79072805 3349 case 'i':
43c5f42d 3350 Safefree(PL_inplace);
c030f24b
GH
3351#if defined(__CYGWIN__) /* do backup extension automagically */
3352 if (*(s+1) == '\0') {
c86a4f2e 3353 PL_inplace = savepvs(".bak");
c030f24b
GH
3354 return s+1;
3355 }
3356#endif /* __CYGWIN__ */
5ef5d758 3357 {
d4c19fe8 3358 const char * const start = ++s;
5ef5d758
NC
3359 while (*s && !isSPACE(*s))
3360 ++s;
3361
3362 PL_inplace = savepvn(start, s - start);
3363 }
fb73857a 3364 return s;
4e49a025 3365 case 'I': /* -I handled both here and in parse_body() */
f20b2998 3366 forbid_setid('I', FALSE);
fb73857a 3367 ++s;
3368 while (*s && isSPACE(*s))
3369 ++s;
3370 if (*s) {
c7030b81 3371 const char *e, *p;
0df16ed7
GS
3372 p = s;
3373 /* ignore trailing spaces (possibly followed by other switches) */
3374 do {
3375 for (e = p; *e && !isSPACE(*e); e++) ;
3376 p = e;
3377 while (isSPACE(*p))
3378 p++;
3379 } while (*p && *p != '-');
55b4bc1c 3380 incpush(s, e-s,
e28f3139 3381 INCPUSH_ADD_SUB_DIRS|INCPUSH_ADD_OLD_VERS|INCPUSH_UNSHIFT);
0df16ed7
GS
3382 s = p;
3383 if (*s == '-')
3384 s++;
79072805
LW
3385 }
3386 else
a67e862a 3387 Perl_croak(aTHX_ "No directory specified for -I");
fb73857a 3388 return s;
79072805 3389 case 'l':
3280af22 3390 PL_minus_l = TRUE;
79072805 3391 s++;
7889fe52
NIS
3392 if (PL_ors_sv) {
3393 SvREFCNT_dec(PL_ors_sv);
a0714e2c 3394 PL_ors_sv = NULL;
7889fe52 3395 }
79072805 3396 if (isDIGIT(*s)) {
53305cf1 3397 I32 flags = 0;
a3b680e6 3398 STRLEN numlen;
396482e1 3399 PL_ors_sv = newSVpvs("\n");
53305cf1
NC
3400 numlen = 3 + (*s == '0');
3401 *SvPVX(PL_ors_sv) = (char)grok_oct(s, &numlen, &flags, NULL);
79072805
LW
3402 s += numlen;
3403 }
3404 else {
8bfdd7d9 3405 if (RsPARA(PL_rs)) {
396482e1 3406 PL_ors_sv = newSVpvs("\n\n");
7889fe52
NIS
3407 }
3408 else {
8bfdd7d9 3409 PL_ors_sv = newSVsv(PL_rs);
c07a80fd 3410 }
79072805
LW
3411 }
3412 return s;
1a30305b 3413 case 'M':
f20b2998 3414 forbid_setid('M', FALSE); /* XXX ? */
924ba076 3415 /* FALLTHROUGH */
1a30305b 3416 case 'm':
f20b2998 3417 forbid_setid('m', FALSE); /* XXX ? */
1a30305b 3418 if (*++s) {
c7030b81 3419 const char *start;
b64cb68c 3420 const char *end;
11343788 3421 SV *sv;
e1ec3a88 3422 const char *use = "use ";
0544e6df 3423 bool colon = FALSE;
a5f75d66 3424 /* -M-foo == 'no foo' */
d0043bd1
NC
3425 /* Leading space on " no " is deliberate, to make both
3426 possibilities the same length. */
3427 if (*s == '-') { use = " no "; ++s; }
3428 sv = newSVpvn(use,4);
a5f75d66 3429 start = s;
1a30305b 3430 /* We allow -M'Module qw(Foo Bar)' */
0eb30aeb 3431 while(isWORDCHAR(*s) || *s==':') {
0544e6df
RB
3432 if( *s++ == ':' ) {
3433 if( *s == ':' )
3434 s++;
3435 else
3436 colon = TRUE;
3437 }
3438 }
3439 if (s == start)
3440 Perl_croak(aTHX_ "Module name required with -%c option",
3441 option);
3442 if (colon)
3443 Perl_croak(aTHX_ "Invalid module name %.*s with -%c option: "
3444 "contains single ':'",
63da6837 3445 (int)(s - start), start, option);
b64cb68c 3446 end = s + strlen(s);
c07a80fd 3447 if (*s != '=') {
b64cb68c 3448 sv_catpvn(sv, start, end - start);
0544e6df 3449 if (option == 'm') {
c07a80fd 3450 if (*s != '\0')
cea2e8a9 3451 Perl_croak(aTHX_ "Can't use '%c' after -mname", *s);
396482e1 3452 sv_catpvs( sv, " ()");
c07a80fd 3453 }
3454 } else {
11343788 3455 sv_catpvn(sv, start, s-start);
b64cb68c
NC
3456 /* Use NUL as q''-delimiter. */
3457 sv_catpvs(sv, " split(/,/,q\0");
3458 ++s;
3459 sv_catpvn(sv, s, end - s);
396482e1 3460 sv_catpvs(sv, "\0)");
c07a80fd 3461 }
b64cb68c 3462 s = end;
29a861e7 3463 Perl_av_create_and_push(aTHX_ &PL_preambleav, sv);
1a30305b 3464 }
3465 else
0544e6df 3466 Perl_croak(aTHX_ "Missing argument to -%c", option);
1a30305b 3467 return s;
79072805 3468 case 'n':
3280af22 3469 PL_minus_n = TRUE;
79072805
LW
3470 s++;
3471 return s;
3472 case 'p':
3280af22 3473 PL_minus_p = TRUE;
79072805
LW
3474 s++;
3475 return s;
3476 case 's':
f20b2998 3477 forbid_setid('s', FALSE);
3280af22 3478 PL_doswitches = TRUE;
79072805
LW
3479 s++;
3480 return s;
6537fe72 3481 case 't':
27a6968b 3482 case 'T':
dc6d7f5c 3483#if defined(SILENT_NO_TAINT_SUPPORT)
284167a5 3484 /* silently ignore */
dc6d7f5c 3485#elif defined(NO_TAINT_SUPPORT)
3231f579 3486 Perl_croak_nocontext("This perl was compiled without taint support. "
284167a5
S
3487 "Cowardly refusing to run with -t or -T flags");
3488#else
3489 if (!TAINTING_get)
27a6968b 3490 TOO_LATE_FOR(*s);
284167a5 3491#endif
6537fe72 3492 s++;
463ee0b2 3493 return s;
79072805 3494 case 'u':
3280af22 3495 PL_do_undump = TRUE;
79072805
LW
3496 s++;
3497 return s;
3498 case 'U':
3280af22 3499 PL_unsafe = TRUE;
79072805
LW
3500 s++;
3501 return s;
3502 case 'v':
c4bc78d9
NC
3503 minus_v();
3504 case 'w':
3505 if (! (PL_dowarn & G_WARN_ALL_MASK)) {
3506 PL_dowarn |= G_WARN_ON;
3507 }
3508 s++;
3509 return s;
3510 case 'W':
3511 PL_dowarn = G_WARN_ALL_ON|G_WARN_ON;
3512 if (!specialWARN(PL_compiling.cop_warnings))
3513 PerlMemShared_free(PL_compiling.cop_warnings);
3514 PL_compiling.cop_warnings = pWARN_ALL ;
3515 s++;
3516 return s;
3517 case 'X':
3518 PL_dowarn = G_WARN_ALL_OFF;
3519 if (!specialWARN(PL_compiling.cop_warnings))
3520 PerlMemShared_free(PL_compiling.cop_warnings);
3521 PL_compiling.cop_warnings = pWARN_NONE ;
3522 s++;
3523 return s;
3524 case '*':
3525 case ' ':
3526 while( *s == ' ' )
3527 ++s;
3528 if (s[0] == '-') /* Additional switches on #! line. */
3529 return s+1;
3530 break;
3531 case '-':
3532 case 0:
3533#if defined(WIN32) || !defined(PERL_STRICT_CR)
3534 case '\r':
3535#endif
3536 case '\n':
3537 case '\t':
3538 break;
3539#ifdef ALTERNATE_SHEBANG
3540 case 'S': /* OS/2 needs -S on "extproc" line. */
3541 break;
3542#endif
4bb78d63
CB
3543 case 'e': case 'f': case 'x': case 'E':
3544#ifndef ALTERNATE_SHEBANG
3545 case 'S':
3546#endif
3547 case 'V':
c4bc78d9 3548 Perl_croak(aTHX_ "Can't emulate -%.1s on #! line",s);
b7e077d0
FC
3549 default:
3550 Perl_croak(aTHX_
3551 "Unrecognized switch: -%.1s (-h will show valid options)",s
3552 );
c4bc78d9
NC
3553 }
3554 return NULL;
3555}
3556
3557
3558STATIC void
3559S_minus_v(pTHX)
3560{
fc3381af 3561 PerlIO * PIO_stdout;
46807d8e 3562 {
709aee94
DD
3563 const char * const level_str = "v" PERL_VERSION_STRING;
3564 const STRLEN level_len = sizeof("v" PERL_VERSION_STRING)-1;
46807d8e 3565#ifdef PERL_PATCHNUM
709aee94 3566 SV* level;
23d483e2 3567# ifdef PERL_GIT_UNCOMMITTED_CHANGES
709aee94 3568 static const char num [] = PERL_PATCHNUM "*";
23d483e2 3569# else
709aee94 3570 static const char num [] = PERL_PATCHNUM;
23d483e2 3571# endif
fc3381af 3572 {
709aee94
DD
3573 const STRLEN num_len = sizeof(num)-1;
3574 /* A very advanced compiler would fold away the strnEQ
3575 and this whole conditional, but most (all?) won't do it.
3576 SV level could also be replaced by with preprocessor
3577 catenation.
3578 */
3579 if (num_len >= level_len && strnEQ(num,level_str,level_len)) {
3580 /* per 46807d8e80, PERL_PATCHNUM is outside of the control
3581 of the interp so it might contain format characters
3582 */
3583 level = newSVpvn(num, num_len);
fc3381af 3584 } else {
709aee94 3585 level = Perl_newSVpvf_nocontext("%s (%s)", level_str, num);
fc3381af 3586 }
46807d8e 3587 }
709aee94
DD
3588#else
3589 SV* level = newSVpvn(level_str, level_len);
3590#endif /* #ifdef PERL_PATCHNUM */
fc3381af
DD
3591 PIO_stdout = PerlIO_stdout();
3592 PerlIO_printf(PIO_stdout,
ded326e4
DG
3593 "\nThis is perl " STRINGIFY(PERL_REVISION)
3594 ", version " STRINGIFY(PERL_VERSION)
3595 ", subversion " STRINGIFY(PERL_SUBVERSION)
c1f6cd39 3596 " (%"SVf") built for " ARCHNAME, SVfARG(level)
ded326e4 3597 );
709aee94 3598 SvREFCNT_dec_NN(level);
46807d8e 3599 }
fb73857a 3600#if defined(LOCAL_PATCH_COUNT)
3601 if (LOCAL_PATCH_COUNT > 0)
fc3381af 3602 PerlIO_printf(PIO_stdout,
b0e47665
GS
3603 "\n(with %d registered patch%s, "
3604 "see perl -V for more detail)",
bb7a0f54 3605 LOCAL_PATCH_COUNT,
b0e47665 3606 (LOCAL_PATCH_COUNT!=1) ? "es" : "");
a5f75d66 3607#endif
1a30305b 3608
fc3381af 3609 PerlIO_printf(PIO_stdout,
7a2bbcbf 3610 "\n\nCopyright 1987-2016, Larry Wall\n");
79072805 3611#ifdef MSDOS
fc3381af 3612 PerlIO_printf(PIO_stdout,
b0e47665 3613 "\nMS-DOS port Copyright (c) 1989, 1990, Diomidis Spinellis\n");
55497cff 3614#endif
3615#ifdef DJGPP
fc3381af 3616 PerlIO_printf(PIO_stdout,
b0e47665
GS
3617 "djgpp v2 port (jpl5003c) by Hirofumi Watanabe, 1996\n"
3618 "djgpp v2 port (perl5004+) by Laszlo Molnar, 1997-1999\n");
4633a7c4 3619#endif
79072805 3620#ifdef OS2
fc3381af 3621 PerlIO_printf(PIO_stdout,
b0e47665 3622 "\n\nOS/2 port Copyright (c) 1990, 1991, Raymond Chen, Kai Uwe Rommel\n"
be3c0a43 3623 "Version 5 port Copyright (c) 1994-2002, Andreas Kaiser, Ilya Zakharevich\n");
79072805 3624#endif
9d116dd7 3625#ifdef OEMVS
fc3381af 3626 PerlIO_printf(PIO_stdout,
b0e47665 3627 "MVS (OS390) port by Mortice Kern Systems, 1997-1999\n");
9d116dd7 3628#endif
495c5fdc 3629#ifdef __VOS__
fc3381af 3630 PerlIO_printf(PIO_stdout,
c0fcb8c5 3631 "Stratus OpenVOS port by Paul.Green@stratus.com, 1997-2013\n");
495c5fdc 3632#endif
a1a0e61e 3633#ifdef POSIX_BC
fc3381af 3634 PerlIO_printf(PIO_stdout,
b0e47665 3635 "BS2000 (POSIX) port by Start Amadeus GmbH, 1998-1999\n");
a1a0e61e 3636#endif
e1caacb4 3637#ifdef UNDER_CE
fc3381af
DD
3638 PerlIO_printf(PIO_stdout,
3639 "WINCE port by Rainer Keuchel, 2001-2002\n"
3640 "Built on " __DATE__ " " __TIME__ "\n\n");
e1caacb4
JH
3641 wce_hitreturn();
3642#endif
a0fd4948 3643#ifdef __SYMBIAN32__
fc3381af 3644 PerlIO_printf(PIO_stdout,
27da23d5
JH
3645 "Symbian port by Nokia, 2004-2005\n");
3646#endif
baed7233
DL
3647#ifdef BINARY_BUILD_NOTICE
3648 BINARY_BUILD_NOTICE;
3649#endif
fc3381af 3650 PerlIO_printf(PIO_stdout,
b0e47665 3651 "\n\
79072805 3652Perl may be copied only under the terms of either the Artistic License or the\n\
3d6f292d 3653GNU General Public License, which may be found in the Perl 5 source kit.\n\n\
95103687 3654Complete documentation for Perl, including FAQ lists, should be found on\n\
a0288114 3655this system using \"man perl\" or \"perldoc perl\". If you have access to the\n\
c9e30dd8 3656Internet, point your browser at http://www.perl.org/, the Perl Home Page.\n\n");
7ca617d0 3657 my_exit(0);
79072805
LW
3658}
3659
3660/* compliments of Tom Christiansen */
3661
3662/* unexec() can be found in the Gnu emacs distribution */
ee580363 3663/* Known to work with -DUNEXEC and using unexelf.c from GNU emacs-20.2 */
79072805 3664
25bbd826
CB
3665#ifdef VMS
3666#include <lib$routines.h>
3667#endif
3668
79072805 3669void
864dbfa3 3670Perl_my_unexec(pTHX)
79072805
LW
3671{
3672#ifdef UNEXEC
b37c2d43
AL
3673 SV * prog = newSVpv(BIN_EXP, 0);
3674 SV * file = newSVpv(PL_origfilename, 0);
ee580363 3675 int status = 1;
79072805
LW
3676 extern int etext;
3677
396482e1 3678 sv_catpvs(prog, "/perl");
396482e1 3679 sv_catpvs(file, ".perldump");
79072805 3680
ee580363
GS
3681 unexec(SvPVX(file), SvPVX(prog), &etext, sbrk(0), 0);
3682 /* unexec prints msg to stderr in case of failure */
6ad3d225 3683 PerlProc_exit(status);
79072805 3684#else
ddeaf645 3685 PERL_UNUSED_CONTEXT;
a5f75d66 3686# ifdef VMS
a5f75d66 3687 lib$signal(SS$_DEBUG); /* ssdef.h #included from vmsish.h */
84d78eb7 3688# elif defined(WIN32) || defined(__CYGWIN__)
ddeaf645 3689 Perl_croak_nocontext("dump is not supported");
aa689395 3690# else
79072805 3691 ABORT(); /* for use with undump */
aa689395 3692# endif
a5f75d66 3693#endif
79072805
LW
3694}
3695
cb68f92d
GS
3696/* initialize curinterp */
3697STATIC void
cea2e8a9 3698S_init_interp(pTHX)
cb68f92d 3699{
acfe0abc 3700#ifdef MULTIPLICITY
115ff745
NC
3701# define PERLVAR(prefix,var,type)
3702# define PERLVARA(prefix,var,n,type)
acfe0abc 3703# if defined(PERL_IMPLICIT_CONTEXT)
115ff745
NC
3704# define PERLVARI(prefix,var,type,init) aTHX->prefix##var = init;
3705# define PERLVARIC(prefix,var,type,init) aTHX->prefix##var = init;
3967c732 3706# else
115ff745
NC
3707# define PERLVARI(prefix,var,type,init) PERL_GET_INTERP->var = init;
3708# define PERLVARIC(prefix,var,type,init) PERL_GET_INTERP->var = init;
066ef5b5 3709# endif
acfe0abc 3710# include "intrpvar.h"
acfe0abc
GS
3711# undef PERLVAR
3712# undef PERLVARA
3713# undef PERLVARI
3714# undef PERLVARIC
3715#else
115ff745
NC
3716# define PERLVAR(prefix,var,type)
3717# define PERLVARA(prefix,var,n,type)
3718# define PERLVARI(prefix,var,type,init) PL_##var = init;
3719# define PERLVARIC(prefix,var,type,init) PL_##var = init;
acfe0abc 3720# include "intrpvar.h"
acfe0abc
GS
3721# undef PERLVAR
3722# undef PERLVARA
3723# undef PERLVARI
3724# undef PERLVARIC
cb68f92d
GS
3725#endif
3726
cb68f92d
GS
3727}
3728
76e3520e 3729STATIC void
cea2e8a9 3730S_init_main_stash(pTHX)
79072805 3731{
463ee0b2 3732 GV *gv;
6e72f9df 3733
03d9f026 3734 PL_curstash = PL_defstash = (HV *)SvREFCNT_inc_simple_NN(newHV());
23579a14
NC
3735 /* We know that the string "main" will be in the global shared string
3736 table, so it's a small saving to use it rather than allocate another
3737 8 bytes. */
18916d0d 3738 PL_curstname = newSVpvs_share("main");
fafc274c 3739 gv = gv_fetchpvs("main::", GV_ADD|GV_NOTQUAL, SVt_PVHV);
23579a14
NC
3740 /* If we hadn't caused another reference to "main" to be in the shared
3741 string table above, then it would be worth reordering these two,
3742 because otherwise all we do is delete "main" from it as a consequence
3743 of the SvREFCNT_dec, only to add it again with hv_name_set */
adbc6bb1 3744 SvREFCNT_dec(GvHV(gv));
854da30f 3745 hv_name_sets(PL_defstash, "main", 0);
85fbaab2 3746 GvHV(gv) = MUTABLE_HV(SvREFCNT_inc_simple(PL_defstash));
463ee0b2 3747 SvREADONLY_on(gv);
fafc274c
NC
3748 PL_incgv = gv_HVadd(gv_AVadd(gv_fetchpvs("INC", GV_ADD|GV_NOTQUAL,
3749 SVt_PVAV)));
5a5094bd 3750 SvREFCNT_inc_simple_void(PL_incgv); /* Don't allow it to be freed */
3280af22 3751 GvMULTI_on(PL_incgv);
fafc274c 3752 PL_hintgv = gv_fetchpvs("\010", GV_ADD|GV_NOTQUAL, SVt_PV); /* ^H */
4639d557 3753 SvREFCNT_inc_simple_void(PL_hintgv);
3280af22 3754 GvMULTI_on(PL_hintgv);
fafc274c 3755 PL_defgv = gv_fetchpvs("_", GV_ADD|GV_NOTQUAL, SVt_PVAV);
5a5094bd 3756 SvREFCNT_inc_simple_void(PL_defgv);
d456e3f4 3757 PL_errgv = gv_fetchpvs("@", GV_ADD|GV_NOTQUAL, SVt_PV);
5a5094bd 3758 SvREFCNT_inc_simple_void(PL_errgv);
3280af22 3759 GvMULTI_on(PL_errgv);
fafc274c 3760 PL_replgv = gv_fetchpvs("\022", GV_ADD|GV_NOTQUAL, SVt_PV); /* ^R */
475b1e90 3761 SvREFCNT_inc_simple_void(PL_replgv);
3280af22 3762 GvMULTI_on(PL_replgv);
cea2e8a9 3763 (void)Perl_form(aTHX_ "%240s",""); /* Preallocate temp - for immediate signals. */
c69033f2 3764#ifdef PERL_DONT_CREATE_GVSV
689fbe18 3765 (void)gv_SVadd(PL_errgv);
c69033f2 3766#endif
38a03e6e 3767 sv_grow(ERRSV, 240); /* Preallocate - for immediate signals. */
ab69dbc2 3768 CLEAR_ERRSV();
03d9f026 3769 SET_CURSTASH(PL_defstash);
11faa288 3770 CopSTASH_set(&PL_compiling, PL_defstash);
5c1737d1
NC
3771 PL_debstash = GvHV(gv_fetchpvs("DB::", GV_ADDMULTI, SVt_PVHV));
3772 PL_globalstash = GvHV(gv_fetchpvs("CORE::GLOBAL::", GV_ADDMULTI,
3773 SVt_PVHV));
4633a7c4 3774 /* We must init $/ before switches are processed. */
64ace3f8 3775 sv_setpvs(get_sv("/", GV_ADD), "\n");
79072805
LW
3776}
3777
8d113837
NC
3778STATIC PerlIO *
3779S_open_script(pTHX_ const char *scriptname, bool dosearch, bool *suidscript)
79072805 3780{
fdf5d70d 3781 int fdscript = -1;
8d113837 3782 PerlIO *rsfp = NULL;
1dfef69b 3783 Stat_t tmpstatbuf;
375ed12a 3784 int fd;
1b24ed4b 3785
7918f24d
NC
3786 PERL_ARGS_ASSERT_OPEN_SCRIPT;
3787
3280af22 3788 if (PL_e_script) {
8afc33d6 3789 PL_origfilename = savepvs("-e");
96436eeb 3790 }
6c4ab083 3791 else {
22ff3130
HS
3792 const char *s;
3793 UV uv;
6c4ab083 3794 /* if find_script() returns, it returns a malloc()-ed value */
dd374669 3795 scriptname = PL_origfilename = find_script(scriptname, dosearch, NULL, 1);
6c4ab083 3796
854da30f 3797 if (strEQs(scriptname, "/dev/fd/")
22ff3130
HS
3798 && isDIGIT(scriptname[8])
3799 && grok_atoUV(scriptname + 8, &uv, &s)
3800 && uv <= PERL_INT_MAX
3801 ) {
3802 fdscript = (int)uv;
6c4ab083 3803 if (*s) {
ae3f3efd
PS
3804 /* PSz 18 Feb 04
3805 * Tell apart "normal" usage of fdscript, e.g.
3806 * with bash on FreeBSD:
3807 * perl <( echo '#!perl -DA'; echo 'print "$0\n"')
3808 * from usage in suidperl.
3809 * Does any "normal" usage leave garbage after the number???
3810 * Is it a mistake to use a similar /dev/fd/ construct for
3811 * suidperl?
3812 */
f20b2998 3813 *suidscript = TRUE;
ae3f3efd
PS
3814 /* PSz 20 Feb 04
3815 * Be supersafe and do some sanity-checks.
3816 * Still, can we be sure we got the right thing?
3817 */
3818 if (*s != '/') {
3819 Perl_croak(aTHX_ "Wrong syntax (suid) fd script name \"%s\"\n", s);
3820 }
3821 if (! *(s+1)) {
3822 Perl_croak(aTHX_ "Missing (suid) fd script name\n");
3823 }
6c4ab083 3824 scriptname = savepv(s + 1);
3280af22 3825 Safefree(PL_origfilename);
dd374669 3826 PL_origfilename = (char *)scriptname;
6c4ab083
GS
3827 }
3828 }
3829 }
3830
05ec9bb3 3831 CopFILE_free(PL_curcop);
57843af0 3832 CopFILE_set(PL_curcop, PL_origfilename);
770526c1 3833 if (*PL_origfilename == '-' && PL_origfilename[1] == '\0')
dd374669 3834 scriptname = (char *)"";
fdf5d70d 3835 if (fdscript >= 0) {
8d113837 3836 rsfp = PerlIO_fdopen(fdscript,PERL_SCRIPT_MODE);
96436eeb 3837 }
79072805 3838 else if (!*scriptname) {
cdd8118e 3839 forbid_setid(0, *suidscript);
c0b3891a 3840 return NULL;
79072805 3841 }
96436eeb 3842 else {
9c12f1e5
RGS
3843#ifdef FAKE_BIT_BUCKET
3844 /* This hack allows one not to have /dev/null (or BIT_BUCKET as it
3845 * is called) and still have the "-e" work. (Believe it or not,
3846 * a /dev/null is required for the "-e" to work because source
3847 * filter magic is used to implement it. ) This is *not* a general
3848 * replacement for a /dev/null. What we do here is create a temp
3849 * file (an empty file), open up that as the script, and then
3850 * immediately close and unlink it. Close enough for jazz. */
3851#define FAKE_BIT_BUCKET_PREFIX "/tmp/perlnull-"
3852#define FAKE_BIT_BUCKET_SUFFIX "XXXXXXXX"
3853#define FAKE_BIT_BUCKET_TEMPLATE FAKE_BIT_BUCKET_PREFIX FAKE_BIT_BUCKET_SUFFIX
3854 char tmpname[sizeof(FAKE_BIT_BUCKET_TEMPLATE)] = {
3855 FAKE_BIT_BUCKET_TEMPLATE
3856 };
3857 const char * const err = "Failed to create a fake bit bucket";
3858 if (strEQ(scriptname, BIT_BUCKET)) {
3859#ifdef HAS_MKSTEMP /* Hopefully mkstemp() is safe here. */
e57270be 3860 int old_umask = umask(0177);
9c12f1e5 3861 int tmpfd = mkstemp(tmpname);
60f7fc1e 3862 umask(old_umask);
9c12f1e5
RGS
3863 if (tmpfd > -1) {
3864 scriptname = tmpname;
3865 close(tmpfd);
3866 } else
3867 Perl_croak(aTHX_ err);
3868#else
3869# ifdef HAS_MKTEMP
3870 scriptname = mktemp(tmpname);
3871 if (!scriptname)
3872 Perl_croak(aTHX_ err);
3873# endif
3874#endif
3875 }
3876#endif
8d113837 3877 rsfp = PerlIO_open(scriptname,PERL_SCRIPT_MODE);
9c12f1e5
RGS
3878#ifdef FAKE_BIT_BUCKET
3879 if (memEQ(scriptname, FAKE_BIT_BUCKET_PREFIX,
3880 sizeof(FAKE_BIT_BUCKET_PREFIX) - 1)
3881 && strlen(scriptname) == sizeof(tmpname) - 1) {
3882 unlink(scriptname);
3883 }
3884 scriptname = BIT_BUCKET;
3885#endif
96436eeb 3886 }
8d113837 3887 if (!rsfp) {
447218f8 3888 /* PSz 16 Sep 03 Keep neat error message */
b1681ed3
RGS
3889 if (PL_e_script)
3890 Perl_croak(aTHX_ "Can't open "BIT_BUCKET": %s\n", Strerror(errno));
3891 else
3892 Perl_croak(aTHX_ "Can't open perl script \"%s\": %s\n",
3893 CopFILE(PL_curcop), Strerror(errno));
13281fa4 3894 }
375ed12a 3895 fd = PerlIO_fileno(rsfp);
131d45a9 3896#if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC)
375ed12a
JH
3897 if (fd >= 0) {
3898 /* ensure close-on-exec */
131d45a9 3899 if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) {
375ed12a
JH
3900 Perl_croak(aTHX_ "Can't open perl script \"%s\": %s\n",
3901 CopFILE(PL_curcop), Strerror(errno));
3902 }
3903 }
a7c81930 3904#endif
1dfef69b 3905
375ed12a
JH
3906 if (fd < 0 ||
3907 (PerlLIO_fstat(fd, &tmpstatbuf) >= 0
3908 && S_ISDIR(tmpstatbuf.st_mode)))
1dfef69b
RS
3909 Perl_croak(aTHX_ "Can't open perl script \"%s\": %s\n",
3910 CopFILE(PL_curcop),
0c0d42ff 3911 Strerror(EISDIR));
1dfef69b 3912
8d113837 3913 return rsfp;
79072805 3914}
8d063cd8 3915
ea442100
JH
3916/* Mention
3917 * I_SYSSTATVFS HAS_FSTATVFS
3918 * I_SYSMOUNT
3919 * I_STATFS HAS_FSTATFS HAS_GETFSSTAT
3920 * I_MNTENT HAS_GETMNTENT HAS_HASMNTOPT
3921 * here so that metaconfig picks them up. */
3922
3923
cc69b689 3924#ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
ec2019ad 3925/* Don't even need this function. */
cc69b689 3926#else
ec2019ad
NC
3927STATIC void
3928S_validate_suid(pTHX_ PerlIO *rsfp)
3929{
dfff4baf
BF
3930 const Uid_t my_uid = PerlProc_getuid();
3931 const Uid_t my_euid = PerlProc_geteuid();
3932 const Gid_t my_gid = PerlProc_getgid();
3933 const Gid_t my_egid = PerlProc_getegid();
985213f2 3934
ac076a5c
NC
3935 PERL_ARGS_ASSERT_VALIDATE_SUID;
3936
985213f2 3937 if (my_euid != my_uid || my_egid != my_gid) { /* (suidperl doesn't exist, in fact) */
a2e578da 3938 dVAR;
375ed12a 3939 int fd = PerlIO_fileno(rsfp);
45a23732
DD
3940 Stat_t statbuf;
3941 if (fd < 0 || PerlLIO_fstat(fd, &statbuf) < 0) { /* may be either wrapped or real suid */
3942 Perl_croak_nocontext( "Illegal suidscript");
375ed12a 3943 }
45a23732 3944 if ((my_euid != my_uid && my_euid == statbuf.st_uid && statbuf.st_mode & S_ISUID)
375ed12a 3945 ||
45a23732 3946 (my_egid != my_gid && my_egid == statbuf.st_gid && statbuf.st_mode & S_ISGID)
375ed12a 3947 )
b28d0864 3948 if (!PL_do_undump)
cea2e8a9 3949 Perl_croak(aTHX_ "YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
a687059c 3950FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
a687059c 3951 /* not set-id, must be wrapped */
a687059c 3952 }
79072805 3953}
cc69b689 3954#endif /* SETUID_SCRIPTS_ARE_SECURE_NOW */
13281fa4 3955
76e3520e 3956STATIC void
2f9285f8 3957S_find_beginning(pTHX_ SV* linestr_sv, PerlIO *rsfp)
79072805 3958{
c7030b81 3959 const char *s;
eb578fdb 3960 const char *s2;
33b78306 3961
7918f24d
NC
3962 PERL_ARGS_ASSERT_FIND_BEGINNING;
3963
33b78306
LW
3964 /* skip forward in input to the real script? */
3965
737c24fc 3966 do {
2f9285f8 3967 if ((s = sv_gets(linestr_sv, rsfp, 0)) == NULL)
cea2e8a9 3968 Perl_croak(aTHX_ "No Perl script found in input\n");
4f0c37ba 3969 s2 = s;
737c24fc
Z
3970 } while (!(*s == '#' && s[1] == '!' && ((s = instr(s,"perl")) || (s = instr(s2,"PERL")))));
3971 PerlIO_ungetc(rsfp, '\n'); /* to keep line count right */
3972 while (*s && !(isSPACE (*s) || *s == '#')) s++;
3973 s2 = s;
3974 while (*s == ' ' || *s == '\t') s++;
3975 if (*s++ == '-') {
3976 while (isDIGIT(s2[-1]) || s2[-1] == '-' || s2[-1] == '.'
3977 || s2[-1] == '_') s2--;
854da30f 3978 if (strEQs(s2-4,"perl"))
737c24fc
Z
3979 while ((s = moreswitches(s)))
3980 ;
83025b21
LW
3981 }
3982}
3983
afe37c7d 3984
76e3520e 3985STATIC void
cea2e8a9 3986S_init_ids(pTHX)
352d5a3a 3987{
284167a5
S
3988 /* no need to do anything here any more if we don't
3989 * do tainting. */
dc6d7f5c 3990#ifndef NO_TAINT_SUPPORT
dfff4baf
BF
3991 const Uid_t my_uid = PerlProc_getuid();
3992 const Uid_t my_euid = PerlProc_geteuid();
3993 const Gid_t my_gid = PerlProc_getgid();
3994 const Gid_t my_egid = PerlProc_getegid();
985213f2 3995
20b7effb
JH
3996 PERL_UNUSED_CONTEXT;
3997
22f7c9c9 3998 /* Should not happen: */
985213f2 3999 CHECK_MALLOC_TAINT(my_uid && (my_euid != my_uid || my_egid != my_gid));
284167a5
S
4000 TAINTING_set( TAINTING_get | (my_uid && (my_euid != my_uid || my_egid != my_gid)) );
4001#endif
ae3f3efd
PS
4002 /* BUG */
4003 /* PSz 27 Feb 04
4004 * Should go by suidscript, not uid!=euid: why disallow
4005 * system("ls") in scripts run from setuid things?
4006 * Or, is this run before we check arguments and set suidscript?
4007 * What about SETUID_SCRIPTS_ARE_SECURE_NOW: could we use fdscript then?
4008 * (We never have suidscript, can we be sure to have fdscript?)
4009 * Or must then go by UID checks? See comments in forbid_setid also.
4010 */
748a9306 4011}
79072805 4012
a0643315
JH
4013/* This is used very early in the lifetime of the program,
4014 * before even the options are parsed, so PL_tainting has
b0891165 4015 * not been initialized properly. */
af419de7 4016bool
8f42b153 4017Perl_doing_taint(int argc, char *argv[], char *envp[])
22f7c9c9 4018{
c3446a78
JH
4019#ifndef PERL_IMPLICIT_SYS
4020 /* If we have PERL_IMPLICIT_SYS we can't call getuid() et alia
4021 * before we have an interpreter-- and the whole point of this
4022 * function is to be called at such an early stage. If you are on
4023 * a system with PERL_IMPLICIT_SYS but you do have a concept of
4024 * "tainted because running with altered effective ids', you'll
4025 * have to add your own checks somewhere in here. The two most
4026 * known samples of 'implicitness' are Win32 and NetWare, neither
4027 * of which has much of concept of 'uids'. */
dfff4baf
BF
4028 Uid_t uid = PerlProc_getuid();
4029 Uid_t euid = PerlProc_geteuid();
4030 Gid_t gid = PerlProc_getgid();
4031 Gid_t egid = PerlProc_getegid();
6867be6d 4032 (void)envp;
22f7c9c9
JH
4033
4034#ifdef VMS
af419de7 4035 uid |= gid << 16;
22f7c9c9
JH
4036 euid |= egid << 16;
4037#endif
4038 if (uid && (euid != uid || egid != gid))
4039 return 1;
c3446a78 4040#endif /* !PERL_IMPLICIT_SYS */
af419de7
JH
4041 /* This is a really primitive check; environment gets ignored only
4042 * if -T are the first chars together; otherwise one gets
4043 * "Too late" message. */
22f7c9c9 4044 if ( argc > 1 && argv[1][0] == '-'
305b8651 4045 && isALPHA_FOLD_EQ(argv[1][1], 't'))
22f7c9c9
JH
4046 return 1;
4047 return 0;
4048}
22f7c9c9 4049
d0bafe7e
NC
4050/* Passing the flag as a single char rather than a string is a slight space
4051 optimisation. The only message that isn't /^-.$/ is
4052 "program input from stdin", which is substituted in place of '\0', which
4053 could never be a command line flag. */
76e3520e 4054STATIC void
f20b2998 4055S_forbid_setid(pTHX_ const char flag, const bool suidscript) /* g */
bbce6d69 4056{
d0bafe7e
NC
4057 char string[3] = "-x";
4058 const char *message = "program input from stdin";
4059
20b7effb 4060 PERL_UNUSED_CONTEXT;
d0bafe7e
NC
4061 if (flag) {
4062 string[1] = flag;
4063 message = string;
4064 }
4065
ae3f3efd 4066#ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
985213f2 4067 if (PerlProc_getuid() != PerlProc_geteuid())
d0bafe7e 4068 Perl_croak(aTHX_ "No %s allowed while running setuid", message);
985213f2 4069 if (PerlProc_getgid() != PerlProc_getegid())
d0bafe7e 4070 Perl_croak(aTHX_ "No %s allowed while running setgid", message);
ae3f3efd 4071#endif /* SETUID_SCRIPTS_ARE_SECURE_NOW */
f20b2998 4072 if (suidscript)
d0bafe7e 4073 Perl_croak(aTHX_ "No %s allowed with (suid) fdscript", message);
bbce6d69 4074}
4075
1ee4443e 4076void
5b235299
NC
4077Perl_init_dbargs(pTHX)
4078{
4079 AV *const args = PL_dbargs = GvAV(gv_AVadd((gv_fetchpvs("DB::args",
4080 GV_ADDMULTI,
4081 SVt_PVAV))));
4082
4083 if (AvREAL(args)) {
4084 /* Someone has already created it.
4085 It might have entries, and if we just turn off AvREAL(), they will
4086 "leak" until global destruction. */
4087 av_clear(args);
3df49e2a 4088 if (SvTIED_mg((const SV *)args, PERL_MAGIC_tied))
7355df7e 4089 Perl_croak(aTHX_ "Cannot set tied @DB::args");
5b235299 4090 }
af80dd86 4091 AvREIFY_only(PL_dbargs);
5b235299
NC
4092}
4093
4094void
1ee4443e 4095Perl_init_debugger(pTHX)
748a9306 4096{
c4420975 4097 HV * const ostash = PL_curstash;
a6d69523 4098 MAGIC *mg;
1ee4443e 4099
03d9f026 4100 PL_curstash = (HV *)SvREFCNT_inc_simple(PL_debstash);
5b235299
NC
4101
4102 Perl_init_dbargs(aTHX);
8cece913
FC
4103 PL_DBgv = MUTABLE_GV(
4104 SvREFCNT_inc(gv_fetchpvs("DB::DB", GV_ADDMULTI, SVt_PVGV))
4105 );
4106 PL_DBline = MUTABLE_GV(
4107 SvREFCNT_inc(gv_fetchpvs("DB::dbline", GV_ADDMULTI, SVt_PVAV))
4108 );
4109 PL_DBsub = MUTABLE_GV(SvREFCNT_inc(
4110 gv_HVadd(gv_fetchpvs("DB::sub", GV_ADDMULTI, SVt_PVHV))
4111 ));
5c1737d1 4112 PL_DBsingle = GvSV((gv_fetchpvs("DB::single", GV_ADDMULTI, SVt_PV)));
4c0f30d6
NC
4113 if (!SvIOK(PL_DBsingle))
4114 sv_setiv(PL_DBsingle, 0);
a6d69523
TC
4115 mg = sv_magicext(PL_DBsingle, NULL, PERL_MAGIC_debugvar, &PL_vtbl_debugvar, 0, 0);
4116 mg->mg_private = DBVARMG_SINGLE;
4117 SvSETMAGIC(PL_DBsingle);
4118
5c1737d1 4119 PL_DBtrace = GvSV((gv_fetchpvs("DB::trace", GV_ADDMULTI, SVt_PV)));
4c0f30d6
NC
4120 if (!SvIOK(PL_DBtrace))
4121 sv_setiv(PL_DBtrace, 0);
a6d69523
TC
4122 mg = sv_magicext(PL_DBtrace, NULL, PERL_MAGIC_debugvar, &PL_vtbl_debugvar, 0, 0);
4123 mg->mg_private = DBVARMG_TRACE;
4124 SvSETMAGIC(PL_DBtrace);
4125
5c1737d1 4126 PL_DBsignal = GvSV((gv_fetchpvs("DB::signal", GV_ADDMULTI, SVt_PV)));
4c0f30d6
NC
4127 if (!SvIOK(PL_DBsignal))
4128 sv_setiv(PL_DBsignal, 0);
a6d69523
TC
4129 mg = sv_magicext(PL_DBsignal, NULL, PERL_MAGIC_debugvar, &PL_vtbl_debugvar, 0, 0);
4130 mg->mg_private = DBVARMG_SIGNAL;
4131 SvSETMAGIC(PL_DBsignal);
4132
03d9f026 4133 SvREFCNT_dec(PL_curstash);
1ee4443e 4134 PL_curstash = ostash;
352d5a3a
LW
4135}
4136
2ce36478
SM
4137#ifndef STRESS_REALLOC
4138#define REASONABLE(size) (size)
0ff72558 4139#define REASONABLE_but_at_least(size,min) (size)
2ce36478
SM
4140#else
4141#define REASONABLE(size) (1) /* unreasonable */
0ff72558 4142#define REASONABLE_but_at_least(size,min) (min)
2ce36478
SM
4143#endif
4144
11343788 4145void
cea2e8a9 4146Perl_init_stacks(pTHX)
79072805 4147{
3caf0269
DM
4148 SSize_t size;
4149
e336de0d 4150 /* start with 128-item stack and 8K cxstack */
3280af22 4151 PL_curstackinfo = new_stackinfo(REASONABLE(128),
e336de0d 4152 REASONABLE(8192/sizeof(PERL_CONTEXT) - 1));
3280af22
NIS
4153 PL_curstackinfo->si_type = PERLSI_MAIN;
4154 PL_curstack = PL_curstackinfo->si_stack;
4155 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
79072805 4156
3280af22
NIS
4157 PL_stack_base = AvARRAY(PL_curstack);
4158 PL_stack_sp = PL_stack_base;
4159 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
8990e307 4160
a02a5408 4161 Newx(PL_tmps_stack,REASONABLE(128),SV*);
3280af22
NIS
4162 PL_tmps_floor = -1;
4163 PL_tmps_ix = -1;
4164 PL_tmps_max = REASONABLE(128);
8990e307 4165
a02a5408 4166 Newx(PL_markstack,REASONABLE(32),I32);
3280af22
NIS
4167 PL_markstack_ptr = PL_markstack;
4168 PL_markstack_max = PL_markstack + REASONABLE(32);
79072805 4169
ce2f7c3b 4170 SET_MARK_OFFSET;
e336de0d 4171
a02a5408 4172 Newx(PL_scopestack,REASONABLE(32),I32);
d343c3ef
GG
4173#ifdef DEBUGGING
4174 Newx(PL_scopestack_name,REASONABLE(32),const char*);
4175#endif
3280af22
NIS
4176 PL_scopestack_ix = 0;
4177 PL_scopestack_max = REASONABLE(32);
79072805 4178
3caf0269
DM
4179 size = REASONABLE_but_at_least(128,SS_MAXPUSH);
4180 Newx(PL_savestack, size, ANY);
3280af22 4181 PL_savestack_ix = 0;
3caf0269
DM
4182 /*PL_savestack_max lies: it always has SS_MAXPUSH more than it claims */
4183 PL_savestack_max = size - SS_MAXPUSH;
378cc40b 4184}
33b78306 4185
2ce36478
SM
4186#undef REASONABLE
4187
76e3520e 4188STATIC void
cea2e8a9 4189S_nuke_stacks(pTHX)
6e72f9df 4190{
3280af22
NIS
4191 while (PL_curstackinfo->si_next)
4192 PL_curstackinfo = PL_curstackinfo->si_next;
4193 while (PL_curstackinfo) {
4194 PERL_SI *p = PL_curstackinfo->si_prev;
bac4b2ad 4195 /* curstackinfo->si_stack got nuked by sv_free_arenas() */
3280af22
NIS
4196 Safefree(PL_curstackinfo->si_cxstack);
4197 Safefree(PL_curstackinfo);
4198 PL_curstackinfo = p;
e336de0d 4199 }
3280af22
NIS
4200 Safefree(PL_tmps_stack);
4201 Safefree(PL_markstack);
4202 Safefree(PL_scopestack);
58780814
GG
4203#ifdef DEBUGGING
4204 Safefree(PL_scopestack_name);
4205#endif
3280af22 4206 Safefree(PL_savestack);
378cc40b 4207}
33b78306 4208
74e8ce34
NC
4209void
4210Perl_populate_isa(pTHX_ const char *name, STRLEN len, ...)
4211{
4212 GV *const gv = gv_fetchpvn(name, len, GV_ADD | GV_ADDMULTI, SVt_PVAV);
4213 AV *const isa = GvAVn(gv);
4214 va_list args;
4215
4216 PERL_ARGS_ASSERT_POPULATE_ISA;
4217
4218 if(AvFILLp(isa) != -1)
4219 return;
4220
4221 /* NOTE: No support for tied ISA */
4222
4223 va_start(args, len);
4224 do {
4225 const char *const parent = va_arg(args, const char*);
4226 size_t parent_len;
4227
4228 if (!parent)
4229 break;
4230 parent_len = va_arg(args, size_t);
4231
4232 /* Arguments are supplied with a trailing :: */
4233 assert(parent_len > 2);
4234 assert(parent[parent_len - 1] == ':');
4235 assert(parent[parent_len - 2] == ':');
4236 av_push(isa, newSVpvn(parent, parent_len - 2));
4237 (void) gv_fetchpvn(parent, parent_len, GV_ADD, SVt_PVGV);
4238 } while (1);
4239 va_end(args);
4240}
4241
8990e307 4242
76e3520e 4243STATIC void
cea2e8a9 4244S_init_predump_symbols(pTHX)
45d8adaa 4245{
93a17b20 4246 GV *tmpgv;
af8c498a 4247 IO *io;
79072805 4248
64ace3f8 4249 sv_setpvs(get_sv("\"", GV_ADD), " ");
e23d9e2f
CS
4250 PL_ofsgv = (GV*)SvREFCNT_inc(gv_fetchpvs(",", GV_ADD|GV_NOTQUAL, SVt_PV));
4251
d963bf01
NC
4252
4253 /* Historically, PVIOs were blessed into IO::Handle, unless
4254 FileHandle was loaded, in which case they were blessed into
4255 that. Action at a distance.
4256 However, if we simply bless into IO::Handle, we break code
4257 that assumes that PVIOs will have (among others) a seek
4258 method. IO::File inherits from IO::Handle and IO::Seekable,
4259 and provides the needed methods. But if we simply bless into
4260 it, then we break code that assumed that by loading
4261 IO::Handle, *it* would work.
4262 So a compromise is to set up the correct @IO::File::ISA,
4263 so that code that does C<use IO::Handle>; will still work.
4264 */
4265
74e8ce34
NC
4266 Perl_populate_isa(aTHX_ STR_WITH_LEN("IO::File::ISA"),
4267 STR_WITH_LEN("IO::Handle::"),
4268 STR_WITH_LEN("IO::Seekable::"),
4269 STR_WITH_LEN("Exporter::"),
4270 NULL);
d963bf01 4271
fafc274c 4272 PL_stdingv = gv_fetchpvs("STDIN", GV_ADD|GV_NOTQUAL, SVt_PVIO);
3280af22 4273 GvMULTI_on(PL_stdingv);
af8c498a 4274 io = GvIOp(PL_stdingv);
a04651f4 4275 IoTYPE(io) = IoTYPE_RDONLY;
af8c498a 4276 IoIFP(io) = PerlIO_stdin();
fafc274c 4277 tmpgv = gv_fetchpvs("stdin", GV_ADD|GV_NOTQUAL, SVt_PV);
a5f75d66 4278 GvMULTI_on(tmpgv);
a45c7426 4279 GvIOp(tmpgv) = MUTABLE_IO(SvREFCNT_inc_simple(io));
79072805 4280
fafc274c 4281 tmpgv = gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
a5f75d66 4282 GvMULTI_on(tmpgv);
af8c498a 4283 io = GvIOp(tmpgv);
a04651f4 4284 IoTYPE(io) = IoTYPE_WRONLY;
af8c498a 4285 IoOFP(io) = IoIFP(io) = PerlIO_stdout();
4633a7c4 4286 setdefout(tmpgv);
fafc274c 4287 tmpgv = gv_fetchpvs("stdout", GV_ADD|GV_NOTQUAL, SVt_PV);
a5f75d66 4288 GvMULTI_on(tmpgv);
a45c7426 4289 GvIOp(tmpgv) = MUTABLE_IO(SvREFCNT_inc_simple(io));
79072805 4290
fafc274c 4291 PL_stderrgv = gv_fetchpvs("STDERR", GV_ADD|GV_NOTQUAL, SVt_PVIO);
bf49b057
GS
4292 GvMULTI_on(PL_stderrgv);
4293 io = GvIOp(PL_stderrgv);
a04651f4 4294 IoTYPE(io) = IoTYPE_WRONLY;
af8c498a 4295 IoOFP(io) = IoIFP(io) = PerlIO_stderr();
fafc274c 4296 tmpgv = gv_fetchpvs("stderr", GV_ADD|GV_NOTQUAL, SVt_PV);
a5f75d66 4297 GvMULTI_on(tmpgv);
a45c7426 4298 GvIOp(tmpgv) = MUTABLE_IO(SvREFCNT_inc_simple(io));
79072805 4299
de61bf2a 4300 PL_statname = newSVpvs(""); /* last filename we did stat on */
79072805 4301}
33b78306 4302
a11ec5a9 4303void
5aaab254 4304Perl_init_argv_symbols(pTHX_ int argc, char **argv)
33b78306 4305{
7918f24d
NC
4306 PERL_ARGS_ASSERT_INIT_ARGV_SYMBOLS;
4307
79072805 4308 argc--,argv++; /* skip name of script */
3280af22 4309 if (PL_doswitches) {
79072805 4310 for (; argc > 0 && **argv == '-'; argc--,argv++) {
aec46f14 4311 char *s;
79072805
LW
4312 if (!argv[0][1])
4313 break;
379d538a 4314 if (argv[0][1] == '-' && !argv[0][2]) {
79072805
LW
4315 argc--,argv++;
4316 break;
4317 }
155aba94 4318 if ((s = strchr(argv[0], '='))) {
b3d904f3
NC
4319 const char *const start_name = argv[0] + 1;
4320 sv_setpv(GvSV(gv_fetchpvn_flags(start_name, s - start_name,
4321 TRUE, SVt_PV)), s + 1);
79072805
LW
4322 }
4323 else
71315bf2 4324 sv_setiv(GvSV(gv_fetchpv(argv[0]+1, GV_ADD, SVt_PV)),1);
fe14fcc3 4325 }
79072805 4326 }
fafc274c 4327 if ((PL_argvgv = gv_fetchpvs("ARGV", GV_ADD|GV_NOTQUAL, SVt_PVAV))) {
722fa0e9 4328 SvREFCNT_inc_simple_void_NN(PL_argvgv);
a11ec5a9 4329 GvMULTI_on(PL_argvgv);
a11ec5a9
RGS
4330 av_clear(GvAVn(PL_argvgv));
4331 for (; argc > 0; argc--,argv++) {
aec46f14 4332 SV * const sv = newSVpv(argv[0],0);
b188953e 4333 av_push(GvAV(PL_argvgv),sv);
ce81ff12
JH
4334 if (!(PL_unicode & PERL_UNICODE_LOCALE_FLAG) || PL_utf8locale) {
4335 if (PL_unicode & PERL_UNICODE_ARGV_FLAG)
4336 SvUTF8_on(sv);
4337 }
a05d7ebb
JH
4338 if (PL_unicode & PERL_UNICODE_WIDESYSCALLS_FLAG) /* Sarathy? */
4339 (void)sv_utf8_decode(sv);
a11ec5a9
RGS
4340 }
4341 }
82f96200
JL
4342
4343 if (PL_inplace && (!PL_argvgv || AvFILL(GvAV(PL_argvgv)) == -1))
4344 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
4345 "-i used with no filenames on the command line, "
4346 "reading from STDIN");
a11ec5a9
RGS
4347}
4348
4349STATIC void
5aaab254 4350S_init_postdump_symbols(pTHX_ int argc, char **argv, char **env)
a11ec5a9 4351{
20b7effb 4352#ifdef USE_ITHREADS
27da23d5 4353 dVAR;
20b7effb 4354#endif
a11ec5a9 4355 GV* tmpgv;
a11ec5a9 4356
7918f24d
NC
4357 PERL_ARGS_ASSERT_INIT_POSTDUMP_SYMBOLS;
4358
f2da823f 4359 PL_toptarget = newSV_type(SVt_PVIV);
854da30f 4360 SvPVCLEAR(PL_toptarget);
f2da823f 4361 PL_bodytarget = newSV_type(SVt_PVIV);
854da30f 4362 SvPVCLEAR(PL_bodytarget);
3280af22 4363 PL_formtarget = PL_bodytarget;
79072805 4364
bbce6d69 4365 TAINT;
a11ec5a9
RGS
4366
4367 init_argv_symbols(argc,argv);
4368
fafc274c 4369 if ((tmpgv = gv_fetchpvs("0", GV_ADD|GV_NOTQUAL, SVt_PV))) {
3280af22 4370 sv_setpv(GvSV(tmpgv),PL_origfilename);
79072805 4371 }
fafc274c 4372 if ((PL_envgv = gv_fetchpvs("ENV", GV_ADD|GV_NOTQUAL, SVt_PVHV))) {
79072805 4373 HV *hv;
e17132c1 4374 bool env_is_not_environ;
cf93a474 4375 SvREFCNT_inc_simple_void_NN(PL_envgv);
3280af22
NIS
4376 GvMULTI_on(PL_envgv);
4377 hv = GvHVn(PL_envgv);
a0714e2c 4378 hv_magic(hv, NULL, PERL_MAGIC_env);
2f42fcb0 4379#ifndef PERL_MICRO
fa6a1c44 4380#ifdef USE_ENVIRON_ARRAY
4633a7c4
LW
4381 /* Note that if the supplied env parameter is actually a copy
4382 of the global environ then it may now point to free'd memory
4383 if the environment has been modified since. To avoid this
4384 problem we treat env==NULL as meaning 'use the default'
4385 */
4386 if (!env)
4387 env = environ;
e17132c1
JD
4388 env_is_not_environ = env != environ;
4389 if (env_is_not_environ
4efc5df6
GS
4390# ifdef USE_ITHREADS
4391 && PL_curinterp == aTHX
4392# endif
4393 )
4394 {
bd61b366 4395 environ[0] = NULL;
4efc5df6 4396 }
9b4eeda5 4397 if (env) {
9d27dca9 4398 char *s, *old_var;
ae37b791 4399 STRLEN nlen;
27da23d5 4400 SV *sv;
ae37b791
TC
4401 HV *dups = newHV();
4402
764df951 4403 for (; *env; env++) {
9d27dca9
MT
4404 old_var = *env;
4405
4406 if (!(s = strchr(old_var,'=')) || s == old_var)
79072805 4407 continue;
ae37b791 4408 nlen = s - old_var;
9d27dca9 4409
7da0e383 4410#if defined(MSDOS) && !defined(DJGPP)
61968511 4411 *s = '\0';
9d27dca9 4412 (void)strupr(old_var);
61968511 4413 *s = '=';
137443ea 4414#endif
ae37b791
TC
4415 if (hv_exists(hv, old_var, nlen)) {
4416 const char *name = savepvn(old_var, nlen);
4417
4418 /* make sure we use the same value as getenv(), otherwise code that
4419 uses getenv() (like setlocale()) might see a different value to %ENV
4420 */
4421 sv = newSVpv(PerlEnv_getenv(name), 0);
4422
4423 /* keep a count of the dups of this name so we can de-dup environ later */
4424 if (hv_exists(dups, name, nlen))
4425 ++SvIVX(*hv_fetch(dups, name, nlen, 0));
4426 else
4427 (void)hv_store(dups, name, nlen, newSViv(1), 0);
4428
4429 Safefree(name);
4430 }
4431 else {
4432 sv = newSVpv(s+1, 0);
4433 }
4434 (void)hv_store(hv, old_var, nlen, sv, 0);
e17132c1 4435 if (env_is_not_environ)
61968511 4436 mg_set(sv);
764df951 4437 }
ae37b791
TC
4438 if (HvKEYS(dups)) {
4439 /* environ has some duplicate definitions, remove them */
4440 HE *entry;
4441 hv_iterinit(dups);
4442 while ((entry = hv_iternext_flags(dups, 0))) {
4443 STRLEN nlen;
4444 const char *name = HePV(entry, nlen);
4445 IV count = SvIV(HeVAL(entry));
4446 IV i;
4447 SV **valp = hv_fetch(hv, name, nlen, 0);
4448
4449 assert(valp);
4450
4451 /* try to remove any duplicate names, depending on the
4452 * implementation used in my_setenv() the iteration might
4453 * not be necessary, but let's be safe.
4454 */
4455 for (i = 0; i < count; ++i)
4456 my_setenv(name, 0);
4457
4458 /* and set it back to the value we set $ENV{name} to */
4459 my_setenv(name, SvPV_nolen(*valp));
4460 }
4461 }
4462 SvREFCNT_dec_NN(dups);
9b4eeda5 4463 }
103a7189 4464#endif /* USE_ENVIRON_ARRAY */
2f42fcb0 4465#endif /* !PERL_MICRO */
79072805 4466 }
bbce6d69 4467 TAINT_NOT;
2710853f
MJD
4468
4469 /* touch @F array to prevent spurious warnings 20020415 MJD */
4470 if (PL_minus_a) {
cbfd0a87 4471 (void) get_av("main::F", GV_ADD | GV_ADDMULTI);
2710853f 4472 }
33b78306 4473}
34de22dd 4474
76e3520e 4475STATIC void
2cace6ac 4476S_init_perllib(pTHX)
34de22dd 4477{
32910c7a 4478#ifndef VMS
929e5b34 4479 const char *perl5lib = NULL;
32910c7a 4480#endif
35ba5ce9 4481 const char *s;
a7560424 4482#if defined(WIN32) && !defined(PERL_IS_MINIPERL)
e6a0bbf8
NC
4483 STRLEN len;
4484#endif
4485
284167a5 4486 if (!TAINTING_get) {
552a7a9b 4487#ifndef VMS
32910c7a 4488 perl5lib = PerlEnv_getenv("PERL5LIB");
88f5bc07
AB
4489/*
4490 * It isn't possible to delete an environment variable with
42a3dd3a
RGS
4491 * PERL_USE_SAFE_PUTENV set unless unsetenv() is also available, so in that
4492 * case we treat PERL5LIB as undefined if it has a zero-length value.
88f5bc07
AB
4493 */
4494#if defined(PERL_USE_SAFE_PUTENV) && ! defined(HAS_UNSETENV)
32910c7a 4495 if (perl5lib && *perl5lib != '\0')
88f5bc07 4496#else
32910c7a 4497 if (perl5lib)
88f5bc07 4498#endif
32910c7a 4499 incpush_use_sep(perl5lib, 0, INCPUSH_ADD_SUB_DIRS);
2cace6ac 4500 else {
4705144d
NC
4501 s = PerlEnv_getenv("PERLLIB");
4502 if (s)
50d61629 4503 incpush_use_sep(s, 0, 0);
4705144d 4504 }
552a7a9b 4505#else /* VMS */
4506 /* Treat PERL5?LIB as a possible search list logical name -- the
4507 * "natural" VMS idiom for a Unix path string. We allow each
4508 * element to be a set of |-separated directories for compatibility.
4509 */
4510 char buf[256];
4511 int idx = 0;
88467a4b 4512 if (vmstrnenv("PERL5LIB",buf,0,NULL,0))
e28f3139 4513 do {
2cace6ac 4514 incpush_use_sep(buf, 0, INCPUSH_ADD_SUB_DIRS);
88467a4b 4515 } while (vmstrnenv("PERL5LIB",buf,++idx,NULL,0));
f05b5874 4516 else {
88467a4b 4517 while (vmstrnenv("PERLLIB",buf,idx++,NULL,0))
50d61629 4518 incpush_use_sep(buf, 0, 0);
f05b5874 4519 }
552a7a9b 4520#endif /* VMS */
85e6fe83 4521 }
34de22dd 4522
b0e687f7
NC
4523#ifndef PERL_IS_MINIPERL
4524 /* miniperl gets just -I..., the split of $ENV{PERL5LIB}, and "." in @INC
4525 (and not the architecture specific directories from $ENV{PERL5LIB}) */
4526
c90c0ff4 4527/* Use the ~-expanded versions of APPLLIB (undocumented),
826e305c 4528 SITEARCH, SITELIB, VENDORARCH, VENDORLIB, ARCHLIB and PRIVLIB
df5cef82 4529*/
4633a7c4 4530#ifdef APPLLIB_EXP
be71fc8f
NC
4531 S_incpush_use_sep(aTHX_ STR_WITH_LEN(APPLLIB_EXP),
4532 INCPUSH_ADD_SUB_DIRS|INCPUSH_CAN_RELOCATE);
16d20bd9 4533#endif
4633a7c4 4534
65f19062 4535#ifdef SITEARCH_EXP
3b290362
GS
4536 /* sitearch is always relative to sitelib on Windows for
4537 * DLL-based path intuition to work correctly */
4538# if !defined(WIN32)
be71fc8f
NC
4539 S_incpush_use_sep(aTHX_ STR_WITH_LEN(SITEARCH_EXP),
4540 INCPUSH_CAN_RELOCATE);
65f19062
GS
4541# endif
4542#endif
4543
4633a7c4 4544#ifdef SITELIB_EXP
65f19062 4545# if defined(WIN32)
574c798a 4546 /* this picks up sitearch as well */
1c68cbf7 4547 s = PerlEnv_sitelib_path(PERL_FS_VERSION, &len);
1fa74d9f 4548 if (s)
e6a0bbf8 4549 incpush_use_sep(s, len, INCPUSH_ADD_SUB_DIRS|INCPUSH_CAN_RELOCATE);
65f19062 4550# else
50d61629 4551 S_incpush_use_sep(aTHX_ STR_WITH_LEN(SITELIB_EXP), INCPUSH_CAN_RELOCATE);
65f19062
GS
4552# endif
4553#endif
189d1e8d 4554
65f19062 4555#ifdef PERL_VENDORARCH_EXP
4ea817c6 4556 /* vendorarch is always relative to vendorlib on Windows for
3b290362
GS
4557 * DLL-based path intuition to work correctly */
4558# if !defined(WIN32)
be71fc8f
NC
4559 S_incpush_use_sep(aTHX_ STR_WITH_LEN(PERL_VENDORARCH_EXP),
4560 INCPUSH_CAN_RELOCATE);
65f19062 4561# endif
4b03c463 4562#endif
65f19062
GS
4563
4564#ifdef PERL_VENDORLIB_EXP
4565# if defined(WIN32)
e28f3139 4566 /* this picks up vendorarch as well */
1c68cbf7 4567 s = PerlEnv_vendorlib_path(PERL_FS_VERSION, &len);
1fa74d9f 4568 if (s)
e6a0bbf8 4569 incpush_use_sep(s, len, INCPUSH_ADD_SUB_DIRS|INCPUSH_CAN_RELOCATE);
65f19062 4570# else
be71fc8f
NC
4571 S_incpush_use_sep(aTHX_ STR_WITH_LEN(PERL_VENDORLIB_EXP),
4572 INCPUSH_CAN_RELOCATE);
65f19062 4573# endif
a3635516 4574#endif
65f19062 4575
b9ba2fad 4576#ifdef ARCHLIB_EXP
2cace6ac 4577 S_incpush_use_sep(aTHX_ STR_WITH_LEN(ARCHLIB_EXP), INCPUSH_CAN_RELOCATE);
b9ba2fad
NC
4578#endif
4579
4580#ifndef PRIVLIB_EXP
4581# define PRIVLIB_EXP "/usr/local/lib/perl5:/usr/local/lib/perl"
4582#endif
4583
4584#if defined(WIN32)
1c68cbf7 4585 s = PerlEnv_lib_path(PERL_FS_VERSION, &len);
2cace6ac
NC
4586 if (s)
4587 incpush_use_sep(s, len, INCPUSH_ADD_SUB_DIRS|INCPUSH_CAN_RELOCATE);
b9ba2fad 4588#else
04c9eecc 4589# ifdef NETWARE
2cace6ac 4590 S_incpush_use_sep(aTHX_ PRIVLIB_EXP, 0, INCPUSH_CAN_RELOCATE);
04c9eecc 4591# else
2cace6ac 4592 S_incpush_use_sep(aTHX_ STR_WITH_LEN(PRIVLIB_EXP), INCPUSH_CAN_RELOCATE);
04c9eecc 4593# endif
b9ba2fad
NC
4594#endif
4595
3b777bb4 4596#ifdef PERL_OTHERLIBDIRS
1e3208d8
NC
4597 S_incpush_use_sep(aTHX_ STR_WITH_LEN(PERL_OTHERLIBDIRS),
4598 INCPUSH_ADD_VERSIONED_SUB_DIRS|INCPUSH_NOT_BASEDIR
2cace6ac
NC
4599 |INCPUSH_CAN_RELOCATE);
4600#endif
2cace6ac 4601
284167a5 4602 if (!TAINTING_get) {
2cace6ac 4603#ifndef VMS
2cace6ac
NC
4604/*
4605 * It isn't possible to delete an environment variable with
4606 * PERL_USE_SAFE_PUTENV set unless unsetenv() is also available, so in that
4607 * case we treat PERL5LIB as undefined if it has a zero-length value.
4608 */
4609#if defined(PERL_USE_SAFE_PUTENV) && ! defined(HAS_UNSETENV)
32910c7a 4610 if (perl5lib && *perl5lib != '\0')
2cace6ac 4611#else
32910c7a 4612 if (perl5lib)
2cace6ac 4613#endif
32910c7a
NC
4614 incpush_use_sep(perl5lib, 0,
4615 INCPUSH_ADD_OLD_VERS|INCPUSH_NOT_BASEDIR);
2cace6ac
NC
4616#else /* VMS */
4617 /* Treat PERL5?LIB as a possible search list logical name -- the
4618 * "natural" VMS idiom for a Unix path string. We allow each
4619 * element to be a set of |-separated directories for compatibility.
4620 */
4621 char buf[256];
4622 int idx = 0;
88467a4b 4623 if (vmstrnenv("PERL5LIB",buf,0,NULL,0))
2cace6ac 4624 do {
be71fc8f
NC
4625 incpush_use_sep(buf, 0,
4626 INCPUSH_ADD_OLD_VERS|INCPUSH_NOT_BASEDIR);
88467a4b 4627 } while (vmstrnenv("PERL5LIB",buf,++idx,NULL,0));
2cace6ac 4628#endif /* VMS */
a26c0e28 4629 }
2cace6ac
NC
4630
4631/* Use the ~-expanded versions of APPLLIB (undocumented),
826e305c 4632 SITELIB and VENDORLIB for older versions
2cace6ac
NC
4633*/
4634#ifdef APPLLIB_EXP
be71fc8f
NC
4635 S_incpush_use_sep(aTHX_ STR_WITH_LEN(APPLLIB_EXP), INCPUSH_ADD_OLD_VERS
4636 |INCPUSH_NOT_BASEDIR|INCPUSH_CAN_RELOCATE);
2cace6ac
NC
4637#endif
4638
2cace6ac
NC
4639#if defined(SITELIB_STEM) && defined(PERL_INC_VERSION_LIST)
4640 /* Search for version-specific dirs below here */
be71fc8f
NC
4641 S_incpush_use_sep(aTHX_ STR_WITH_LEN(SITELIB_STEM),
4642 INCPUSH_ADD_OLD_VERS|INCPUSH_CAN_RELOCATE);
2cace6ac
NC
4643#endif
4644
4645
4646#if defined(PERL_VENDORLIB_STEM) && defined(PERL_INC_VERSION_LIST)
4647 /* Search for version-specific dirs below here */
be71fc8f
NC
4648 S_incpush_use_sep(aTHX_ STR_WITH_LEN(PERL_VENDORLIB_STEM),
4649 INCPUSH_ADD_OLD_VERS|INCPUSH_CAN_RELOCATE);
2cace6ac
NC
4650#endif
4651
4652#ifdef PERL_OTHERLIBDIRS
1e3208d8
NC
4653 S_incpush_use_sep(aTHX_ STR_WITH_LEN(PERL_OTHERLIBDIRS),
4654 INCPUSH_ADD_OLD_VERS|INCPUSH_ADD_ARCHONLY_SUB_DIRS
4655 |INCPUSH_CAN_RELOCATE);
3b777bb4 4656#endif
b0e687f7 4657#endif /* !PERL_IS_MINIPERL */
3b777bb4 4658
284167a5 4659 if (!TAINTING_get)
55b4bc1c 4660 S_incpush(aTHX_ STR_WITH_LEN("."), 0);
774d564b 4661}
4662
739a0b84 4663#if defined(DOSISH) || defined(__SYMBIAN32__)
774d564b 4664# define PERLLIB_SEP ';'
4665#else
483efd0a
CB
4666# if defined(__VMS)
4667# define PERLLIB_SEP PL_perllib_sep
774d564b 4668# else
e37778c2 4669# define PERLLIB_SEP ':'
774d564b 4670# endif
4671#endif
4672#ifndef PERLLIB_MANGLE
4673# define PERLLIB_MANGLE(s,n) (s)
ac27b0f5 4674#endif
774d564b 4675
59d6f6a4 4676#ifndef PERL_IS_MINIPERL
ad17a1ae
NC
4677/* Push a directory onto @INC if it exists.
4678 Generate a new SV if we do this, to save needing to copy the SV we push
4679 onto @INC */
4680STATIC SV *
7ffdaae6 4681S_incpush_if_exists(pTHX_ AV *const av, SV *dir, SV *const stem)
ad17a1ae
NC
4682{
4683 Stat_t tmpstatbuf;
7918f24d
NC
4684
4685 PERL_ARGS_ASSERT_INCPUSH_IF_EXISTS;
4686
848ef955 4687 if (PerlLIO_stat(SvPVX_const(dir), &tmpstatbuf) >= 0 &&
ad17a1ae 4688 S_ISDIR(tmpstatbuf.st_mode)) {
3a9a9ba7 4689 av_push(av, dir);
7ffdaae6
NC
4690 dir = newSVsv(stem);
4691 } else {
4692 /* Truncate dir back to stem. */
4693 SvCUR_set(dir, SvCUR(stem));
ad17a1ae
NC
4694 }
4695 return dir;
4696}
59d6f6a4 4697#endif
ad17a1ae 4698
c29067d7
CH
4699STATIC SV *
4700S_mayberelocate(pTHX_ const char *const dir, STRLEN len, U32 flags)
774d564b 4701{
6434436b 4702 const U8 canrelocate = (U8)flags & INCPUSH_CAN_RELOCATE;
c29067d7 4703 SV *libdir;
774d564b 4704
c29067d7 4705 PERL_ARGS_ASSERT_MAYBERELOCATE;
08d0d8ab 4706 assert(len > 0);
3a9a9ba7 4707
d2898d73
EB
4708 /* I am not convinced that this is valid when PERLLIB_MANGLE is
4709 defined to so something (in os2/os2.c), but the code has been
4710 this way, ignoring any possible changed of length, since
4711 760ac839baf413929cd31cc32ffd6dba6b781a81 (5.003_02) so I'll leave
4712 it be. */
4713 libdir = newSVpvn(PERLLIB_MANGLE(dir, len), len);
774d564b 4714
81600524 4715#ifdef VMS
db12e2d3 4716 {
81600524 4717 char *unix;
81600524
CB
4718
4719 if ((unix = tounixspec_ts(SvPV(libdir,len),NULL)) != NULL) {
4720 len = strlen(unix);
f420cce1 4721 while (len > 1 && unix[len-1] == '/') len--; /* Cosmetic */
81600524
CB
4722 sv_usepvn(libdir,unix,len);
4723 }
4724 else
4725 PerlIO_printf(Perl_error_log,
4726 "Failed to unixify @INC element \"%s\"\n",
9dfa9235 4727 SvPV_nolen_const(libdir));
db12e2d3 4728 }
81600524
CB
4729#endif
4730
dd374669
AL
4731 /* Do the if() outside the #ifdef to avoid warnings about an unused
4732 parameter. */
4733 if (canrelocate) {
88fe16b2
NC
4734#ifdef PERL_RELOCATABLE_INC
4735 /*
4736 * Relocatable include entries are marked with a leading .../
4737 *
4738 * The algorithm is
4739 * 0: Remove that leading ".../"
4740 * 1: Remove trailing executable name (anything after the last '/')
4741 * from the perl path to give a perl prefix
4742 * Then
4743 * While the @INC element starts "../" and the prefix ends with a real
4744 * directory (ie not . or ..) chop that real directory off the prefix
4745 * and the leading "../" from the @INC element. ie a logical "../"
4746 * cleanup
4747 * Finally concatenate the prefix and the remainder of the @INC element
4748 * The intent is that /usr/local/bin/perl and .../../lib/perl5
4749 * generates /usr/local/lib/perl5
4750 */
890ce7af 4751 const char *libpath = SvPVX(libdir);
88fe16b2
NC
4752 STRLEN libpath_len = SvCUR(libdir);
4753 if (libpath_len >= 4 && memEQ (libpath, ".../", 4)) {
4754 /* Game on! */
890ce7af 4755 SV * const caret_X = get_sv("\030", 0);
88fe16b2
NC
4756 /* Going to use the SV just as a scratch buffer holding a C
4757 string: */
4758 SV *prefix_sv;
4759 char *prefix;
4760 char *lastslash;
4761
4762 /* $^X is *the* source of taint if tainting is on, hence
4763 SvPOK() won't be true. */
4764 assert(caret_X);
4765 assert(SvPOKp(caret_X));
a663657d
NC
4766 prefix_sv = newSVpvn_flags(SvPVX(caret_X), SvCUR(caret_X),
4767 SvUTF8(caret_X));
88fe16b2
NC
4768 /* Firstly take off the leading .../
4769 If all else fail we'll do the paths relative to the current
4770 directory. */
4771 sv_chop(libdir, libpath + 4);
4772 /* Don't use SvPV as we're intentionally bypassing taining,
4773 mortal copies that the mg_get of tainting creates, and
4774 corruption that seems to come via the save stack.
4775 I guess that the save stack isn't correctly set up yet. */
4776 libpath = SvPVX(libdir);
4777 libpath_len = SvCUR(libdir);
4778
4779 /* This would work more efficiently with memrchr, but as it's
4780 only a GNU extension we'd need to probe for it and
4781 implement our own. Not hard, but maybe not worth it? */
4782
4783 prefix = SvPVX(prefix_sv);
4784 lastslash = strrchr(prefix, '/');
4785
4786 /* First time in with the *lastslash = '\0' we just wipe off
4787 the trailing /perl from (say) /usr/foo/bin/perl
4788 */
4789 if (lastslash) {
4790 SV *tempsv;
4791 while ((*lastslash = '\0'), /* Do that, come what may. */
854da30f 4792 (libpath_len >= 3 && _memEQs(libpath, "../")
88fe16b2
NC
4793 && (lastslash = strrchr(prefix, '/')))) {
4794 if (lastslash[1] == '\0'
4795 || (lastslash[1] == '.'
4796 && (lastslash[2] == '/' /* ends "/." */
4797 || (lastslash[2] == '/'
4798 && lastslash[3] == '/' /* or "/.." */
4799 )))) {
4800 /* Prefix ends "/" or "/." or "/..", any of which
4801 are fishy, so don't do any more logical cleanup.
4802 */
4803 break;
4804 }
4805 /* Remove leading "../" from path */
4806 libpath += 3;
4807 libpath_len -= 3;
4808 /* Next iteration round the loop removes the last
4809 directory name from prefix by writing a '\0' in
4810 the while clause. */
4811 }
4812 /* prefix has been terminated with a '\0' to the correct
4813 length. libpath points somewhere into the libdir SV.
4814 We need to join the 2 with '/' and drop the result into
4815 libdir. */
4816 tempsv = Perl_newSVpvf(aTHX_ "%s/%s", prefix, libpath);
4817 SvREFCNT_dec(libdir);
4818 /* And this is the new libdir. */
4819 libdir = tempsv;
284167a5 4820 if (TAINTING_get &&
985213f2
AB
4821 (PerlProc_getuid() != PerlProc_geteuid() ||
4822 PerlProc_getgid() != PerlProc_getegid())) {
486ec47a 4823 /* Need to taint relocated paths if running set ID */
88fe16b2
NC
4824 SvTAINTED_on(libdir);
4825 }
4826 }
4827 SvREFCNT_dec(prefix_sv);
4828 }
88fe16b2 4829#endif
dd374669 4830 }
c29067d7 4831 return libdir;
c29067d7
CH
4832}
4833
4834STATIC void
4835S_incpush(pTHX_ const char *const dir, STRLEN len, U32 flags)
4836{
c29067d7
CH
4837#ifndef PERL_IS_MINIPERL
4838 const U8 using_sub_dirs
4839 = (U8)flags & (INCPUSH_ADD_VERSIONED_SUB_DIRS
4840 |INCPUSH_ADD_ARCHONLY_SUB_DIRS|INCPUSH_ADD_OLD_VERS);
4841 const U8 add_versioned_sub_dirs
4842 = (U8)flags & INCPUSH_ADD_VERSIONED_SUB_DIRS;
4843 const U8 add_archonly_sub_dirs
4844 = (U8)flags & INCPUSH_ADD_ARCHONLY_SUB_DIRS;
4845#ifdef PERL_INC_VERSION_LIST
4846 const U8 addoldvers = (U8)flags & INCPUSH_ADD_OLD_VERS;
4847#endif
4848#endif
4849 const U8 unshift = (U8)flags & INCPUSH_UNSHIFT;
4850 const U8 push_basedir = (flags & INCPUSH_NOT_BASEDIR) ? 0 : 1;
4851 AV *const inc = GvAVn(PL_incgv);
4852
4853 PERL_ARGS_ASSERT_INCPUSH;
4854 assert(len > 0);
4855
4856 /* Could remove this vestigial extra block, if we don't mind a lot of
4857 re-indenting diff noise. */
4858 {
5a702b9a 4859 SV *const libdir = mayberelocate(dir, len, flags);
c29067d7
CH
4860 /* Change 20189146be79a0596543441fa369c6bf7f85103f, to fix RT#6665,
4861 arranged to unshift #! line -I onto the front of @INC. However,
4862 -I can add version and architecture specific libraries, and they
4863 need to go first. The old code assumed that it was always
4864 pushing. Hence to make it work, need to push the architecture
4865 (etc) libraries onto a temporary array, then "unshift" that onto
4866 the front of @INC. */
4867#ifndef PERL_IS_MINIPERL
4868 AV *const av = (using_sub_dirs) ? (unshift ? newAV() : inc) : NULL;
c29067d7 4869
774d564b 4870 /*
4871 * BEFORE pushing libdir onto @INC we may first push version- and
4872 * archname-specific sub-directories.
4873 */
ee80e7be 4874 if (using_sub_dirs) {
5a702b9a 4875 SV *subdir = newSVsv(libdir);
29d82f8d 4876#ifdef PERL_INC_VERSION_LIST
8353b874 4877 /* Configure terminates PERL_INC_VERSION_LIST with a NULL */
c4420975
AL
4878 const char * const incverlist[] = { PERL_INC_VERSION_LIST };
4879 const char * const *incver;
29d82f8d 4880#endif
7ffdaae6 4881
1e3208d8 4882 if (add_versioned_sub_dirs) {
9c8a64f0 4883 /* .../version/archname if -d .../version/archname */
e51b748d 4884 sv_catpvs(subdir, "/" PERL_FS_VERSION "/" ARCHNAME);
7ffdaae6 4885 subdir = S_incpush_if_exists(aTHX_ av, subdir, libdir);
4b03c463 4886
9c8a64f0 4887 /* .../version if -d .../version */
e51b748d 4888 sv_catpvs(subdir, "/" PERL_FS_VERSION);
7ffdaae6 4889 subdir = S_incpush_if_exists(aTHX_ av, subdir, libdir);
29d82f8d 4890 }
9c8a64f0 4891
9c8a64f0 4892#ifdef PERL_INC_VERSION_LIST
ccc2aad8 4893 if (addoldvers) {
9c8a64f0
GS
4894 for (incver = incverlist; *incver; incver++) {
4895 /* .../xxx if -d .../xxx */
e51b748d 4896 Perl_sv_catpvf(aTHX_ subdir, "/%s", *incver);
7ffdaae6 4897 subdir = S_incpush_if_exists(aTHX_ av, subdir, libdir);
9c8a64f0
GS
4898 }
4899 }
29d82f8d 4900#endif
c992324b 4901
1e3208d8 4902 if (add_archonly_sub_dirs) {
c992324b 4903 /* .../archname if -d .../archname */
e51b748d 4904 sv_catpvs(subdir, "/" ARCHNAME);
7ffdaae6 4905 subdir = S_incpush_if_exists(aTHX_ av, subdir, libdir);
c992324b
NC
4906
4907 }
10cc20f6
NC
4908
4909 assert (SvREFCNT(subdir) == 1);
4910 SvREFCNT_dec(subdir);
774d564b 4911 }
59d6f6a4 4912#endif /* !PERL_IS_MINIPERL */
20189146
RGS
4913 /* finally add this lib directory at the end of @INC */
4914 if (unshift) {
76895e89 4915#ifdef PERL_IS_MINIPERL
c70927a6 4916 const Size_t extra = 0;
76895e89 4917#else
b9f2b683 4918 Size_t extra = av_tindex(av) + 1;
76895e89 4919#endif
a26c0e28
NC
4920 av_unshift(inc, extra + push_basedir);
4921 if (push_basedir)
4922 av_store(inc, extra, libdir);
76895e89 4923#ifndef PERL_IS_MINIPERL
3a9a9ba7
NC
4924 while (extra--) {
4925 /* av owns a reference, av_store() expects to be donated a
4926 reference, and av expects to be sane when it's cleared.
4927 If I wanted to be naughty and wrong, I could peek inside the
4928 implementation of av_clear(), realise that it uses
4929 SvREFCNT_dec() too, so av's array could be a run of NULLs,
4930 and so directly steal from it (with a memcpy() to inc, and
4931 then memset() to NULL them out. But people copy code from the
4932 core expecting it to be best practise, so let's use the API.
4933 Although studious readers will note that I'm not checking any
4934 return codes. */
4935 av_store(inc, extra, SvREFCNT_inc(*av_fetch(av, extra, FALSE)));
4936 }
4937 SvREFCNT_dec(av);
59d6f6a4 4938#endif
20189146 4939 }
a26c0e28 4940 else if (push_basedir) {
3a9a9ba7 4941 av_push(inc, libdir);
20189146 4942 }
a26c0e28
NC
4943
4944 if (!push_basedir) {
4945 assert (SvREFCNT(libdir) == 1);
4946 SvREFCNT_dec(libdir);
4947 }
774d564b 4948 }
34de22dd 4949}
93a17b20 4950
55b4bc1c 4951STATIC void
50d61629 4952S_incpush_use_sep(pTHX_ const char *p, STRLEN len, U32 flags)
55b4bc1c 4953{
50d61629
NC
4954 const char *s;
4955 const char *end;
55b4bc1c
NC
4956 /* This logic has been broken out from S_incpush(). It may be possible to
4957 simplify it. */
4958
4705144d
NC
4959 PERL_ARGS_ASSERT_INCPUSH_USE_SEP;
4960
f31c6eed
JD
4961 /* perl compiled with -DPERL_RELOCATABLE_INCPUSH will ignore the len
4962 * argument to incpush_use_sep. This allows creation of relocatable
4963 * Perl distributions that patch the binary at install time. Those
4964 * distributions will have to provide their own relocation tools; this
4965 * is not a feature otherwise supported by core Perl.
4966 */
4967#ifndef PERL_RELOCATABLE_INCPUSH
50d61629 4968 if (!len)
f31c6eed 4969#endif
50d61629
NC
4970 len = strlen(p);
4971
4972 end = p + len;
4973
55b4bc1c 4974 /* Break at all separators */
e42f52dd 4975 while ((s = (const char*)memchr(p, PERLLIB_SEP, end - p))) {
50d61629
NC
4976 if (s == p) {
4977 /* skip any consecutive separators */
55b4bc1c 4978
55b4bc1c 4979 /* Uncomment the next line for PATH semantics */
50d61629 4980 /* But you'll need to write tests */
55b4bc1c 4981 /* av_push(GvAVn(PL_incgv), newSVpvs(".")); */
50d61629 4982 } else {
55b4bc1c 4983 incpush(p, (STRLEN)(s - p), flags);
55b4bc1c 4984 }
50d61629 4985 p = s + 1;
55b4bc1c 4986 }
50d61629
NC
4987 if (p != end)
4988 incpush(p, (STRLEN)(end - p), flags);
4989
55b4bc1c 4990}
199100c8 4991
93a17b20 4992void
864dbfa3 4993Perl_call_list(pTHX_ I32 oldscope, AV *paramList)
93a17b20 4994{
971a9dd3 4995 SV *atsv;
b084bc87 4996 VOL const line_t oldline = PL_curcop ? CopLINE(PL_curcop) : 0;
312caa8e 4997 CV *cv;
22921e25 4998 STRLEN len;
6224f72b 4999 int ret;
db36c5a1 5000 dJMPENV;
93a17b20 5001
7918f24d
NC
5002 PERL_ARGS_ASSERT_CALL_LIST;
5003
b9f2b683 5004 while (av_tindex(paramList) >= 0) {
ea726b52 5005 cv = MUTABLE_CV(av_shift(paramList));
ece599bd
RGS
5006 if (PL_savebegin) {
5007 if (paramList == PL_beginav) {
059a8bb7 5008 /* save PL_beginav for compiler */
ad64d0ec 5009 Perl_av_create_and_push(aTHX_ &PL_beginav_save, MUTABLE_SV(cv));
ece599bd
RGS
5010 }
5011 else if (paramList == PL_checkav) {
5012 /* save PL_checkav for compiler */
ad64d0ec 5013 Perl_av_create_and_push(aTHX_ &PL_checkav_save, MUTABLE_SV(cv));
ece599bd 5014 }
3c10abe3
AG
5015 else if (paramList == PL_unitcheckav) {
5016 /* save PL_unitcheckav for compiler */
ad64d0ec 5017 Perl_av_create_and_push(aTHX_ &PL_unitcheckav_save, MUTABLE_SV(cv));
3c10abe3 5018 }
059a8bb7 5019 } else {
b5bbe64a 5020 SAVEFREESV(cv);
059a8bb7 5021 }
14dd3ad8 5022 JMPENV_PUSH(ret);
6224f72b 5023 switch (ret) {
312caa8e 5024 case 0:
d6f07c05 5025 CALL_LIST_BODY(cv);
971a9dd3 5026 atsv = ERRSV;
10516c54 5027 (void)SvPV_const(atsv, len);
312caa8e
CS
5028 if (len) {
5029 PL_curcop = &PL_compiling;
57843af0 5030 CopLINE_set(PL_curcop, oldline);
312caa8e 5031 if (paramList == PL_beginav)
396482e1 5032 sv_catpvs(atsv, "BEGIN failed--compilation aborted");
312caa8e 5033 else
4f25aa18
GS
5034 Perl_sv_catpvf(aTHX_ atsv,
5035 "%s failed--call queue aborted",
7d30b5c4 5036 paramList == PL_checkav ? "CHECK"
4f25aa18 5037 : paramList == PL_initav ? "INIT"
3c10abe3 5038 : paramList == PL_unitcheckav ? "UNITCHECK"
4f25aa18 5039 : "END");
312caa8e
CS
5040 while (PL_scopestack_ix > oldscope)
5041 LEAVE;
14dd3ad8 5042 JMPENV_POP;
be2597df 5043 Perl_croak(aTHX_ "%"SVf"", SVfARG(atsv));
a0d0e21e 5044 }
85e6fe83 5045 break;
6224f72b 5046 case 1:
f86702cc 5047 STATUS_ALL_FAILURE;
924ba076 5048 /* FALLTHROUGH */
6224f72b 5049 case 2:
85e6fe83 5050 /* my_exit() was called */
3280af22 5051 while (PL_scopestack_ix > oldscope)
2ae324a7 5052 LEAVE;
84902520 5053 FREETMPS;
03d9f026 5054 SET_CURSTASH(PL_defstash);
3280af22 5055 PL_curcop = &PL_compiling;
57843af0 5056 CopLINE_set(PL_curcop, oldline);
14dd3ad8 5057 JMPENV_POP;
f86702cc 5058 my_exit_jump();
e5964223 5059 NOT_REACHED; /* NOTREACHED */
6224f72b 5060 case 3:
312caa8e
CS
5061 if (PL_restartop) {
5062 PL_curcop = &PL_compiling;
57843af0 5063 CopLINE_set(PL_curcop, oldline);
312caa8e 5064 JMPENV_JUMP(3);
85e6fe83 5065 }
5637ef5b 5066 PerlIO_printf(Perl_error_log, "panic: restartop in call_list\n");
312caa8e
CS
5067 FREETMPS;
5068 break;
8990e307 5069 }
14dd3ad8 5070 JMPENV_POP;
93a17b20 5071 }
93a17b20 5072}
93a17b20 5073
f86702cc 5074void
864dbfa3 5075Perl_my_exit(pTHX_ U32 status)
f86702cc 5076{
6136213b
JGM
5077 if (PL_exit_flags & PERL_EXIT_ABORT) {
5078 abort();
5079 }
5080 if (PL_exit_flags & PERL_EXIT_WARN) {
5081 PL_exit_flags |= PERL_EXIT_ABORT; /* Protect against reentrant calls */
7b0eb0b8 5082 Perl_warn(aTHX_ "Unexpected exit %lu", (unsigned long)status);
6136213b
JGM
5083 PL_exit_flags &= ~PERL_EXIT_ABORT;
5084 }
f86702cc 5085 switch (status) {
5086 case 0:
5087 STATUS_ALL_SUCCESS;
5088 break;
5089 case 1:
5090 STATUS_ALL_FAILURE;
5091 break;
5092 default:
6ac6a52b 5093 STATUS_EXIT_SET(status);
f86702cc 5094 break;
5095 }
5096 my_exit_jump();
5097}
5098
5099void
864dbfa3 5100Perl_my_failure_exit(pTHX)
f86702cc 5101{
5102#ifdef VMS
fb38d079
JM
5103 /* We have been called to fall on our sword. The desired exit code
5104 * should be already set in STATUS_UNIX, but could be shifted over
0968cdad
JM
5105 * by 8 bits. STATUS_UNIX_EXIT_SET will handle the cases where a
5106 * that code is set.
fb38d079
JM
5107 *
5108 * If an error code has not been set, then force the issue.
5109 */
0968cdad
JM
5110 if (MY_POSIX_EXIT) {
5111
e08e1e1d
JM
5112 /* According to the die_exit.t tests, if errno is non-zero */
5113 /* It should be used for the error status. */
0968cdad 5114
e08e1e1d
JM
5115 if (errno == EVMSERR) {
5116 STATUS_NATIVE = vaxc$errno;
5117 } else {
0968cdad 5118
e08e1e1d
JM
5119 /* According to die_exit.t tests, if the child_exit code is */
5120 /* also zero, then we need to exit with a code of 255 */
5121 if ((errno != 0) && (errno < 256))
5122 STATUS_UNIX_EXIT_SET(errno);
5123 else if (STATUS_UNIX < 255) {
0968cdad 5124 STATUS_UNIX_EXIT_SET(255);
e08e1e1d
JM
5125 }
5126
0968cdad 5127 }
e08e1e1d
JM
5128
5129 /* The exit code could have been set by $? or vmsish which
5130 * means that it may not have fatal set. So convert
5131 * success/warning codes to fatal with out changing
5132 * the POSIX status code. The severity makes VMS native
5133 * status handling work, while UNIX mode programs use the
5134 * the POSIX exit codes.
5135 */
5136 if ((STATUS_NATIVE & (STS$K_SEVERE|STS$K_ERROR)) == 0) {
5137 STATUS_NATIVE &= STS$M_COND_ID;
5138 STATUS_NATIVE |= STS$K_ERROR | STS$M_INHIB_MSG;
5139 }
0968cdad
JM
5140 }
5141 else {
5142 /* Traditionally Perl on VMS always expects a Fatal Error. */
5143 if (vaxc$errno & 1) {
5144
5145 /* So force success status to failure */
5146 if (STATUS_NATIVE & 1)
5147 STATUS_ALL_FAILURE;
5148 }
5149 else {
5150 if (!vaxc$errno) {
5151 STATUS_UNIX = EINTR; /* In case something cares */
5152 STATUS_ALL_FAILURE;
5153 }
5154 else {
5155 int severity;
5156 STATUS_NATIVE = vaxc$errno; /* Should already be this */
5157
5158 /* Encode the severity code */
5159 severity = STATUS_NATIVE & STS$M_SEVERITY;
5160 STATUS_UNIX = (severity ? severity : 1) << 8;
5161
5162 /* Perl expects this to be a fatal error */
5163 if (severity != STS$K_SEVERE)
5164 STATUS_ALL_FAILURE;
5165 }
5166 }
5167 }
fb38d079 5168
f86702cc 5169#else
9b599b2a 5170 int exitstatus;
f86702cc 5171 if (errno & 255)
e5218da5 5172 STATUS_UNIX_SET(errno);
9b599b2a 5173 else {
e5218da5 5174 exitstatus = STATUS_UNIX >> 8;
9b599b2a 5175 if (exitstatus & 255)
e5218da5 5176 STATUS_UNIX_SET(exitstatus);
9b599b2a 5177 else
e5218da5 5178 STATUS_UNIX_SET(255);
9b599b2a 5179 }
f86702cc 5180#endif
6136213b
JGM
5181 if (PL_exit_flags & PERL_EXIT_ABORT) {
5182 abort();
5183 }
5184 if (PL_exit_flags & PERL_EXIT_WARN) {
5185 PL_exit_flags |= PERL_EXIT_ABORT; /* Protect against reentrant calls */
7b0eb0b8 5186 Perl_warn(aTHX_ "Unexpected exit failure %ld", (long)PL_statusvalue);
6136213b
JGM
5187 PL_exit_flags &= ~PERL_EXIT_ABORT;
5188 }
f86702cc 5189 my_exit_jump();
93a17b20
LW
5190}
5191
76e3520e 5192STATIC void
cea2e8a9 5193S_my_exit_jump(pTHX)
f86702cc 5194{
3280af22
NIS
5195 if (PL_e_script) {
5196 SvREFCNT_dec(PL_e_script);
a0714e2c 5197 PL_e_script = NULL;
f86702cc 5198 }
5199
3280af22 5200 POPSTACK_TO(PL_mainstack);
3706fcea
DM
5201 if (cxstack_ix >= 0) {
5202 dounwind(-1);
ed8ff0f3 5203 cx_popblock(cxstack);
3706fcea 5204 }
f97a0ef2 5205 LEAVE_SCOPE(0);
ff0cee69 5206
6224f72b 5207 JMPENV_JUMP(2);
f86702cc 5208}
873ef191 5209
0cb96387 5210static I32
acfe0abc 5211read_e_script(pTHX_ int idx, SV *buf_sv, int maxlen)
873ef191 5212{
9d4ba2ae
AL
5213 const char * const p = SvPVX_const(PL_e_script);
5214 const char *nl = strchr(p, '\n');
5215
5216 PERL_UNUSED_ARG(idx);
5217 PERL_UNUSED_ARG(maxlen);
dd374669 5218
3280af22 5219 nl = (nl) ? nl+1 : SvEND(PL_e_script);
7dfe3f66 5220 if (nl-p == 0) {
0cb96387 5221 filter_del(read_e_script);
873ef191 5222 return 0;
7dfe3f66 5223 }
873ef191 5224 sv_catpvn(buf_sv, p, nl-p);
3280af22 5225 sv_chop(PL_e_script, nl);
873ef191
GS
5226 return 1;
5227}
66610fdd 5228
db6e00bd
DD
5229/* removes boilerplate code at the end of each boot_Module xsub */
5230void
b01a1eea 5231Perl_xs_boot_epilog(pTHX_ const I32 ax)
db6e00bd
DD
5232{
5233 if (PL_unitcheckav)
5234 call_list(PL_scopestack_ix, PL_unitcheckav);
5235 XSRETURN_YES;
5236}
5237
66610fdd 5238/*
14d04a33 5239 * ex: set ts=8 sts=4 sw=4 et:
37442d52 5240 */