This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Note the GDBM_File changes in perldelta.
[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
010d7370 6 * 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
166f8a29
DM
25 */
26
c44493f1 27#if defined(PERL_IS_MINIPERL) && !defined(USE_SITECUSTOMIZE)
43c0c913
NC
28# define USE_SITECUSTOMIZE
29#endif
30
378cc40b 31#include "EXTERN.h"
864dbfa3 32#define PERL_IN_PERL_C
378cc40b 33#include "perl.h"
e3321bb0 34#include "patchlevel.h" /* for local_patches */
4a5df386 35#include "XSUB.h"
378cc40b 36
011f1a1a
JH
37#ifdef NETWARE
38#include "nwutil.h"
011f1a1a
JH
39#endif
40
2982a345
NC
41#ifdef USE_KERN_PROC_PATHNAME
42# include <sys/sysctl.h>
43#endif
44
ae60cb46
NC
45#ifdef USE_NSGETEXECUTABLEPATH
46# include <mach-o/dyld.h>
47#endif
48
2aa47728 49#ifdef DEBUG_LEAKING_SCALARS_FORK_DUMP
bf357333
NC
50# ifdef I_SYSUIO
51# include <sys/uio.h>
52# endif
53
54union control_un {
55 struct cmsghdr cm;
56 char control[CMSG_SPACE(sizeof(int))];
57};
58
2aa47728
NC
59#endif
60
5311654c
JH
61#ifdef __BEOS__
62# define HZ 1000000
63#endif
64
65#ifndef HZ
66# ifdef CLK_TCK
67# define HZ CLK_TCK
68# else
69# define HZ 60
70# endif
71#endif
72
7114a2d2 73#if !defined(STANDARD_C) && !defined(HAS_GETENV_PROTOTYPE) && !defined(PERL_MICRO)
20ce7b12 74char *getenv (char *); /* Usually in <stdlib.h> */
54310121
PP
75#endif
76
acfe0abc 77static I32 read_e_script(pTHX_ int idx, SV *buf_sv, int maxlen);
0cb96387 78
cc69b689 79#ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
b24bc095 80# define validate_suid(rsfp) NOOP
cc69b689 81#else
b24bc095 82# define validate_suid(rsfp) S_validate_suid(aTHX_ rsfp)
a687059c 83#endif
8d063cd8 84
d6f07c05
AL
85#define CALL_BODY_SUB(myop) \
86 if (PL_op == (myop)) \
139d0ce6 87 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); \
d6f07c05
AL
88 if (PL_op) \
89 CALLRUNOPS(aTHX);
90
91#define CALL_LIST_BODY(cv) \
92 PUSHMARK(PL_stack_sp); \
9a8aa25b 93 call_sv(MUTABLE_SV((cv)), G_EVAL|G_DISCARD|G_VOID);
d6f07c05 94
e6827a76 95static void
daa7d858 96S_init_tls_and_interp(PerlInterpreter *my_perl)
e6827a76 97{
27da23d5 98 dVAR;
e6827a76
NC
99 if (!PL_curinterp) {
100 PERL_SET_INTERP(my_perl);
3db8f154 101#if defined(USE_ITHREADS)
e6827a76
NC
102 INIT_THREADS;
103 ALLOC_THREAD_KEY;
104 PERL_SET_THX(my_perl);
105 OP_REFCNT_INIT;
e8570548 106 OP_CHECK_MUTEX_INIT;
71ad1b0c 107 HINTS_REFCNT_INIT;
e6827a76 108 MUTEX_INIT(&PL_dollarzero_mutex);
016af4f1
DM
109 MUTEX_INIT(&PL_my_ctx_mutex);
110# endif
e6827a76 111 }
c0bce9aa
NC
112#if defined(USE_ITHREADS)
113 else
114#else
115 /* This always happens for non-ithreads */
116#endif
117 {
e6827a76
NC
118 PERL_SET_THX(my_perl);
119 }
120}
06d86050 121
cbec8ebe
DM
122
123/* these implement the PERL_SYS_INIT, PERL_SYS_INIT3, PERL_SYS_TERM macros */
124
125void
126Perl_sys_init(int* argc, char*** argv)
127{
4fc0badb 128 dVAR;
7918f24d
NC
129
130 PERL_ARGS_ASSERT_SYS_INIT;
131
cbec8ebe
DM
132 PERL_UNUSED_ARG(argc); /* may not be used depending on _BODY macro */
133 PERL_UNUSED_ARG(argv);
134 PERL_SYS_INIT_BODY(argc, argv);
135}
136
137void
138Perl_sys_init3(int* argc, char*** argv, char*** env)
139{
4fc0badb 140 dVAR;
7918f24d
NC
141
142 PERL_ARGS_ASSERT_SYS_INIT3;
143
cbec8ebe
DM
144 PERL_UNUSED_ARG(argc); /* may not be used depending on _BODY macro */
145 PERL_UNUSED_ARG(argv);
146 PERL_UNUSED_ARG(env);
147 PERL_SYS_INIT3_BODY(argc, argv, env);
148}
149
150void
d0820ef1 151Perl_sys_term()
cbec8ebe 152{
4fc0badb 153 dVAR;
bf81751b
DM
154 if (!PL_veto_cleanup) {
155 PERL_SYS_TERM_BODY();
156 }
cbec8ebe
DM
157}
158
159
32e30700
GS
160#ifdef PERL_IMPLICIT_SYS
161PerlInterpreter *
7766f137
GS
162perl_alloc_using(struct IPerlMem* ipM, struct IPerlMem* ipMS,
163 struct IPerlMem* ipMP, struct IPerlEnv* ipE,
32e30700
GS
164 struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
165 struct IPerlDir* ipD, struct IPerlSock* ipS,
166 struct IPerlProc* ipP)
167{
168 PerlInterpreter *my_perl;
7918f24d
NC
169
170 PERL_ARGS_ASSERT_PERL_ALLOC_USING;
171
9f653bb5 172 /* Newx() needs interpreter, so call malloc() instead */
32e30700 173 my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
e6827a76 174 S_init_tls_and_interp(my_perl);
32e30700
GS
175 Zero(my_perl, 1, PerlInterpreter);
176 PL_Mem = ipM;
7766f137
GS
177 PL_MemShared = ipMS;
178 PL_MemParse = ipMP;
32e30700
GS
179 PL_Env = ipE;
180 PL_StdIO = ipStd;
181 PL_LIO = ipLIO;
182 PL_Dir = ipD;
183 PL_Sock = ipS;
184 PL_Proc = ipP;
7cb608b5 185 INIT_TRACK_MEMPOOL(PL_memory_debug_header, my_perl);
7766f137 186
32e30700
GS
187 return my_perl;
188}
189#else
954c1994
GS
190
191/*
ccfc67b7
JH
192=head1 Embedding Functions
193
954c1994
GS
194=for apidoc perl_alloc
195
196Allocates a new Perl interpreter. See L<perlembed>.
197
198=cut
199*/
200
93a17b20 201PerlInterpreter *
cea2e8a9 202perl_alloc(void)
79072805 203{
cea2e8a9 204 PerlInterpreter *my_perl;
79072805 205
9f653bb5 206 /* Newx() needs interpreter, so call malloc() instead */
e8ee3774 207 my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
ba869deb 208
e6827a76 209 S_init_tls_and_interp(my_perl);
7cb608b5 210#ifndef PERL_TRACK_MEMPOOL
07409e01 211 return (PerlInterpreter *) ZeroD(my_perl, 1, PerlInterpreter);
7cb608b5
NC
212#else
213 Zero(my_perl, 1, PerlInterpreter);
214 INIT_TRACK_MEMPOOL(PL_memory_debug_header, my_perl);
215 return my_perl;
216#endif
79072805 217}
32e30700 218#endif /* PERL_IMPLICIT_SYS */
79072805 219
954c1994
GS
220/*
221=for apidoc perl_construct
222
223Initializes a new Perl interpreter. See L<perlembed>.
224
225=cut
226*/
227
79072805 228void
0cb96387 229perl_construct(pTHXx)
79072805 230{
27da23d5 231 dVAR;
7918f24d
NC
232
233 PERL_ARGS_ASSERT_PERL_CONSTRUCT;
234
8990e307 235#ifdef MULTIPLICITY
54aff467 236 init_interp();
ac27b0f5 237 PL_perl_destruct_level = 1;
54aff467 238#else
7918f24d 239 PERL_UNUSED_ARG(my_perl);
54aff467
GS
240 if (PL_perl_destruct_level > 0)
241 init_interp();
242#endif
34caed6d
DM
243 PL_curcop = &PL_compiling; /* needed by ckWARN, right away */
244
0d96b528 245 init_constants();
34caed6d
DM
246
247 SvREADONLY_on(&PL_sv_placeholder);
248 SvREFCNT(&PL_sv_placeholder) = (~(U32)0)/2;
249
250 PL_sighandlerp = (Sighandler_t) Perl_sighandler;
ca0c25f6 251#ifdef PERL_USES_PL_PIDSTATUS
34caed6d 252 PL_pidstatus = newHV();
ca0c25f6 253#endif
79072805 254
396482e1 255 PL_rs = newSVpvs("\n");
dc92893f 256
cea2e8a9 257 init_stacks();
79072805 258
748a9306 259 init_ids();
a5f75d66 260
312caa8e 261 JMPENV_BOOTSTRAP;
f86702cc
PP
262 STATUS_ALL_SUCCESS;
263
0672f40e 264 init_i18nl10n(1);
36477c24 265 SET_NUMERIC_STANDARD();
0b5b802d 266
ab821d7f 267#if defined(LOCAL_PATCH_COUNT)
3280af22 268 PL_localpatches = local_patches; /* For possible -v */
ab821d7f
PP
269#endif
270
52853b95
GS
271#ifdef HAVE_INTERP_INTERN
272 sys_intern_init();
273#endif
274
3a1ee7e8 275 PerlIO_init(aTHX); /* Hook to IO system */
760ac839 276
3280af22
NIS
277 PL_fdpid = newAV(); /* for remembering popen pids by fd */
278 PL_modglobal = newHV(); /* pointers to per-interpreter module globals */
396482e1 279 PL_errors = newSVpvs("");
76f68e9b
MHM
280 sv_setpvs(PERL_DEBUG_PAD(0), ""); /* For regex debugging. */
281 sv_setpvs(PERL_DEBUG_PAD(1), ""); /* ext/re needs these */
282 sv_setpvs(PERL_DEBUG_PAD(2), ""); /* even without DEBUGGING. */
1fcf4c12 283#ifdef USE_ITHREADS
402d2eb1
NC
284 /* First entry is a list of empty elements. It needs to be initialised
285 else all hell breaks loose in S_find_uninit_var(). */
286 Perl_av_create_and_push(aTHX_ &PL_regex_padav, newSVpvs(""));
13137afc 287 PL_regex_pad = AvARRAY(PL_regex_padav);
d4d03940 288 Newxz(PL_stashpad, PL_stashpadmax, HV *);
1fcf4c12 289#endif
e5dd39fc 290#ifdef USE_REENTRANT_API
59bd0823 291 Perl_reentrant_init(aTHX);
e5dd39fc 292#endif
7dc86639
YO
293#if defined(USE_HASH_SEED) || defined(USE_HASH_SEED_EXPLICIT)
294 /* [perl #22371] Algorimic Complexity Attack on Perl 5.6.1, 5.8.0
295 * This MUST be done before any hash stores or fetches take place.
296 * If you set PL_hash_seed (and presumably also PL_hash_seed_set)
297 * yourself, it is your responsibility to provide a good random seed!
298 * You can also define PERL_HASH_SEED in compile time, see hv.h.
299 *
300 * XXX: fix this comment */
301 if (PL_hash_seed_set == FALSE) {
302 Perl_get_hash_seed(aTHX_ PL_hash_seed);
303 PL_hash_seed_set= TRUE;
304 }
305#endif /* #if defined(USE_HASH_SEED) || defined(USE_HASH_SEED_EXPLICIT) */
3d47000e
AB
306
307 /* Note that strtab is a rather special HV. Assumptions are made
308 about not iterating on it, and not adding tie magic to it.
309 It is properly deallocated in perl_destruct() */
310 PL_strtab = newHV();
311
3d47000e
AB
312 HvSHAREKEYS_off(PL_strtab); /* mandatory */
313 hv_ksplit(PL_strtab, 512);
314
0631ea03
AB
315#if defined(__DYNAMIC__) && (defined(NeXT) || defined(__NeXT__))
316 _dyld_lookup_and_bind
317 ("__environ", (unsigned long *) &environ_pointer, NULL);
318#endif /* environ */
319
2f42fcb0
JH
320#ifndef PERL_MICRO
321# ifdef USE_ENVIRON_ARRAY
0631ea03 322 PL_origenviron = environ;
2f42fcb0 323# endif
0631ea03
AB
324#endif
325
5311654c 326 /* Use sysconf(_SC_CLK_TCK) if available, if not
dbc1d986 327 * available or if the sysconf() fails, use the HZ.
27da23d5
JH
328 * BeOS has those, but returns the wrong value.
329 * The HZ if not originally defined has been by now
330 * been defined as CLK_TCK, if available. */
dbc1d986 331#if defined(HAS_SYSCONF) && defined(_SC_CLK_TCK) && !defined(__BEOS__)
5311654c
JH
332 PL_clocktick = sysconf(_SC_CLK_TCK);
333 if (PL_clocktick <= 0)
334#endif
335 PL_clocktick = HZ;
336
081fc587
AB
337 PL_stashcache = newHV();
338
e8e3635e 339 PL_patchlevel = newSVpvs("v" PERL_VERSION_STRING);
1e8125c6 340 PL_apiversion = newSVpvs("v" PERL_API_VERSION_STRING);
d7aa5382 341
27da23d5
JH
342#ifdef HAS_MMAP
343 if (!PL_mmap_page_size) {
344#if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_MMAP_PAGE_SIZE))
345 {
346 SETERRNO(0, SS_NORMAL);
347# ifdef _SC_PAGESIZE
348 PL_mmap_page_size = sysconf(_SC_PAGESIZE);
349# else
350 PL_mmap_page_size = sysconf(_SC_MMAP_PAGE_SIZE);
351# endif
352 if ((long) PL_mmap_page_size < 0) {
353 if (errno) {
44f8325f 354 SV * const error = ERRSV;
d4c19fe8 355 SvUPGRADE(error, SVt_PV);
0510663f 356 Perl_croak(aTHX_ "panic: sysconf: %s", SvPV_nolen_const(error));
27da23d5
JH
357 }
358 else
359 Perl_croak(aTHX_ "panic: sysconf: pagesize unknown");
360 }
361 }
362#else
363# ifdef HAS_GETPAGESIZE
364 PL_mmap_page_size = getpagesize();
365# else
366# if defined(I_SYS_PARAM) && defined(PAGESIZE)
367 PL_mmap_page_size = PAGESIZE; /* compiletime, bad */
368# endif
369# endif
370#endif
371 if (PL_mmap_page_size <= 0)
372 Perl_croak(aTHX_ "panic: bad pagesize %" IVdf,
373 (IV) PL_mmap_page_size);
374 }
375#endif /* HAS_MMAP */
376
377#if defined(HAS_TIMES) && defined(PERL_NEED_TIMESBASE)
378 PL_timesbase.tms_utime = 0;
379 PL_timesbase.tms_stime = 0;
380 PL_timesbase.tms_cutime = 0;
381 PL_timesbase.tms_cstime = 0;
382#endif
383
7d113631
NC
384 PL_osname = Perl_savepvn(aTHX_ STR_WITH_LEN(OSNAME));
385
a3e6e81e 386 PL_registered_mros = newHV();
9e169432
NC
387 /* Start with 1 bucket, for DFS. It's unlikely we'll need more. */
388 HvMAX(PL_registered_mros) = 0;
a3e6e81e 389
8990e307 390 ENTER;
79072805
LW
391}
392
954c1994 393/*
62375a60
NIS
394=for apidoc nothreadhook
395
396Stub that provides thread hook for perl_destruct when there are
397no threads.
398
399=cut
400*/
401
402int
4e9e3734 403Perl_nothreadhook(pTHX)
62375a60 404{
96a5add6 405 PERL_UNUSED_CONTEXT;
62375a60
NIS
406 return 0;
407}
408
41e4abd8
NC
409#ifdef DEBUG_LEAKING_SCALARS_FORK_DUMP
410void
411Perl_dump_sv_child(pTHX_ SV *sv)
412{
413 ssize_t got;
bf357333
NC
414 const int sock = PL_dumper_fd;
415 const int debug_fd = PerlIO_fileno(Perl_debug_log);
bf357333
NC
416 union control_un control;
417 struct msghdr msg;
808ad2d0 418 struct iovec vec[2];
bf357333 419 struct cmsghdr *cmptr;
808ad2d0
NC
420 int returned_errno;
421 unsigned char buffer[256];
41e4abd8 422
7918f24d
NC
423 PERL_ARGS_ASSERT_DUMP_SV_CHILD;
424
bf357333 425 if(sock == -1 || debug_fd == -1)
41e4abd8
NC
426 return;
427
428 PerlIO_flush(Perl_debug_log);
429
bf357333
NC
430 /* All these shenanigans are to pass a file descriptor over to our child for
431 it to dump out to. We can't let it hold open the file descriptor when it
432 forks, as the file descriptor it will dump to can turn out to be one end
433 of pipe that some other process will wait on for EOF. (So as it would
b293a5f8 434 be open, the wait would be forever.) */
bf357333
NC
435
436 msg.msg_control = control.control;
437 msg.msg_controllen = sizeof(control.control);
438 /* We're a connected socket so we don't need a destination */
439 msg.msg_name = NULL;
440 msg.msg_namelen = 0;
441 msg.msg_iov = vec;
808ad2d0 442 msg.msg_iovlen = 1;
bf357333
NC
443
444 cmptr = CMSG_FIRSTHDR(&msg);
445 cmptr->cmsg_len = CMSG_LEN(sizeof(int));
446 cmptr->cmsg_level = SOL_SOCKET;
447 cmptr->cmsg_type = SCM_RIGHTS;
448 *((int *)CMSG_DATA(cmptr)) = 1;
449
450 vec[0].iov_base = (void*)&sv;
451 vec[0].iov_len = sizeof(sv);
452 got = sendmsg(sock, &msg, 0);
41e4abd8
NC
453
454 if(got < 0) {
bf357333 455 perror("Debug leaking scalars parent sendmsg failed");
41e4abd8
NC
456 abort();
457 }
bf357333
NC
458 if(got < sizeof(sv)) {
459 perror("Debug leaking scalars parent short sendmsg");
41e4abd8
NC
460 abort();
461 }
462
808ad2d0
NC
463 /* Return protocol is
464 int: errno value
465 unsigned char: length of location string (0 for empty)
466 unsigned char*: string (not terminated)
467 */
468 vec[0].iov_base = (void*)&returned_errno;
469 vec[0].iov_len = sizeof(returned_errno);
470 vec[1].iov_base = buffer;
471 vec[1].iov_len = 1;
472
473 got = readv(sock, vec, 2);
41e4abd8
NC
474
475 if(got < 0) {
476 perror("Debug leaking scalars parent read failed");
808ad2d0 477 PerlIO_flush(PerlIO_stderr());
41e4abd8
NC
478 abort();
479 }
808ad2d0 480 if(got < sizeof(returned_errno) + 1) {
41e4abd8 481 perror("Debug leaking scalars parent short read");
808ad2d0 482 PerlIO_flush(PerlIO_stderr());
41e4abd8
NC
483 abort();
484 }
485
808ad2d0
NC
486 if (*buffer) {
487 got = read(sock, buffer + 1, *buffer);
488 if(got < 0) {
489 perror("Debug leaking scalars parent read 2 failed");
490 PerlIO_flush(PerlIO_stderr());
491 abort();
492 }
493
494 if(got < *buffer) {
495 perror("Debug leaking scalars parent short read 2");
496 PerlIO_flush(PerlIO_stderr());
497 abort();
498 }
499 }
500
501 if (returned_errno || *buffer) {
502 Perl_warn(aTHX_ "Debug leaking scalars child failed%s%.*s with errno"
503 " %d: %s", (*buffer ? " at " : ""), (int) *buffer, buffer + 1,
504 returned_errno, strerror(returned_errno));
41e4abd8
NC
505 }
506}
507#endif
508
62375a60 509/*
954c1994
GS
510=for apidoc perl_destruct
511
512Shuts down a Perl interpreter. See L<perlembed>.
513
514=cut
515*/
516
31d77e54 517int
0cb96387 518perl_destruct(pTHXx)
79072805 519{
27da23d5 520 dVAR;
be2ea8ed 521 VOL signed char destruct_level; /* see possible values in intrpvar.h */
a0d0e21e 522 HV *hv;
2aa47728 523#ifdef DEBUG_LEAKING_SCALARS_FORK_DUMP
2aa47728
NC
524 pid_t child;
525#endif
8990e307 526
7918f24d
NC
527 PERL_ARGS_ASSERT_PERL_DESTRUCT;
528#ifndef MULTIPLICITY
ed6c66dd 529 PERL_UNUSED_ARG(my_perl);
7918f24d 530#endif
9d4ba2ae 531
3d22c4f0
GG
532 assert(PL_scopestack_ix == 1);
533
7766f137
GS
534 /* wait for all pseudo-forked children to finish */
535 PERL_WAIT_FOR_CHILDREN;
536
3280af22 537 destruct_level = PL_perl_destruct_level;
36e77d41 538#if defined(DEBUGGING) || defined(PERL_TRACK_MEMPOOL)
4633a7c4 539 {
9d4ba2ae
AL
540 const char * const s = PerlEnv_getenv("PERL_DESTRUCT_LEVEL");
541 if (s) {
36e77d41
DD
542 const int i = atoi(s);
543#ifdef DEBUGGING
544 if (destruct_level < i) destruct_level = i;
545#endif
546#ifdef PERL_TRACK_MEMPOOL
547 /* RT #114496, for perl_free */
548 PL_perl_destruct_level = i;
549#endif
5f05dabc 550 }
4633a7c4
LW
551 }
552#endif
553
27da23d5 554 if (PL_exit_flags & PERL_EXIT_DESTRUCT_END) {
f3faeb53
AB
555 dJMPENV;
556 int x = 0;
557
558 JMPENV_PUSH(x);
1b6737cc 559 PERL_UNUSED_VAR(x);
9ebf26ad 560 if (PL_endav && !PL_minus_c) {
ca7b837b 561 PERL_SET_PHASE(PERL_PHASE_END);
f3faeb53 562 call_list(PL_scopestack_ix, PL_endav);
9ebf26ad 563 }
f3faeb53 564 JMPENV_POP;
26f423df 565 }
f3faeb53 566 LEAVE;
a0d0e21e 567 FREETMPS;
3d22c4f0 568 assert(PL_scopestack_ix == 0);
a0d0e21e 569
e00b64d4 570 /* Need to flush since END blocks can produce output */
f13a2bc0 571 my_fflush_all();
e00b64d4 572
16c91539 573 if (PL_threadhook(aTHX)) {
62375a60 574 /* Threads hook has vetoed further cleanup */
c301d606 575 PL_veto_cleanup = TRUE;
37038d91 576 return STATUS_EXIT;
62375a60
NIS
577 }
578
2aa47728
NC
579#ifdef DEBUG_LEAKING_SCALARS_FORK_DUMP
580 if (destruct_level != 0) {
581 /* Fork here to create a child. Our child's job is to preserve the
582 state of scalars prior to destruction, so that we can instruct it
583 to dump any scalars that we later find have leaked.
584 There's no subtlety in this code - it assumes POSIX, and it doesn't
585 fail gracefully */
586 int fd[2];
587
588 if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) {
589 perror("Debug leaking scalars socketpair failed");
590 abort();
591 }
592
593 child = fork();
594 if(child == -1) {
595 perror("Debug leaking scalars fork failed");
596 abort();
597 }
598 if (!child) {
599 /* We are the child */
3125a5a4
NC
600 const int sock = fd[1];
601 const int debug_fd = PerlIO_fileno(Perl_debug_log);
602 int f;
808ad2d0
NC
603 const char *where;
604 /* Our success message is an integer 0, and a char 0 */
b61433a9 605 static const char success[sizeof(int) + 1] = {0};
3125a5a4 606
2aa47728 607 close(fd[0]);
2aa47728 608
3125a5a4
NC
609 /* We need to close all other file descriptors otherwise we end up
610 with interesting hangs, where the parent closes its end of a
611 pipe, and sits waiting for (another) child to terminate. Only
612 that child never terminates, because it never gets EOF, because
bf357333
NC
613 we also have the far end of the pipe open. We even need to
614 close the debugging fd, because sometimes it happens to be one
615 end of a pipe, and a process is waiting on the other end for
616 EOF. Normally it would be closed at some point earlier in
617 destruction, but if we happen to cause the pipe to remain open,
618 EOF never occurs, and we get an infinite hang. Hence all the
619 games to pass in a file descriptor if it's actually needed. */
3125a5a4
NC
620
621 f = sysconf(_SC_OPEN_MAX);
622 if(f < 0) {
808ad2d0
NC
623 where = "sysconf failed";
624 goto abort;
3125a5a4
NC
625 }
626 while (f--) {
627 if (f == sock)
628 continue;
3125a5a4
NC
629 close(f);
630 }
631
2aa47728
NC
632 while (1) {
633 SV *target;
bf357333
NC
634 union control_un control;
635 struct msghdr msg;
636 struct iovec vec[1];
637 struct cmsghdr *cmptr;
638 ssize_t got;
639 int got_fd;
640
641 msg.msg_control = control.control;
642 msg.msg_controllen = sizeof(control.control);
643 /* We're a connected socket so we don't need a source */
644 msg.msg_name = NULL;
645 msg.msg_namelen = 0;
646 msg.msg_iov = vec;
647 msg.msg_iovlen = sizeof(vec)/sizeof(vec[0]);
648
649 vec[0].iov_base = (void*)&target;
650 vec[0].iov_len = sizeof(target);
651
652 got = recvmsg(sock, &msg, 0);
2aa47728
NC
653
654 if(got == 0)
655 break;
656 if(got < 0) {
808ad2d0
NC
657 where = "recv failed";
658 goto abort;
2aa47728
NC
659 }
660 if(got < sizeof(target)) {
808ad2d0
NC
661 where = "short recv";
662 goto abort;
2aa47728 663 }
bf357333 664
808ad2d0
NC
665 if(!(cmptr = CMSG_FIRSTHDR(&msg))) {
666 where = "no cmsg";
667 goto abort;
668 }
669 if(cmptr->cmsg_len != CMSG_LEN(sizeof(int))) {
670 where = "wrong cmsg_len";
671 goto abort;
672 }
673 if(cmptr->cmsg_level != SOL_SOCKET) {
674 where = "wrong cmsg_level";
675 goto abort;
676 }
677 if(cmptr->cmsg_type != SCM_RIGHTS) {
678 where = "wrong cmsg_type";
679 goto abort;
680 }
bf357333
NC
681
682 got_fd = *(int*)CMSG_DATA(cmptr);
683 /* For our last little bit of trickery, put the file descriptor
684 back into Perl_debug_log, as if we never actually closed it
685 */
808ad2d0
NC
686 if(got_fd != debug_fd) {
687 if (dup2(got_fd, debug_fd) == -1) {
688 where = "dup2";
689 goto abort;
690 }
691 }
2aa47728 692 sv_dump(target);
bf357333 693
2aa47728
NC
694 PerlIO_flush(Perl_debug_log);
695
808ad2d0 696 got = write(sock, &success, sizeof(success));
2aa47728
NC
697
698 if(got < 0) {
808ad2d0
NC
699 where = "write failed";
700 goto abort;
2aa47728 701 }
808ad2d0
NC
702 if(got < sizeof(success)) {
703 where = "short write";
704 goto abort;
2aa47728
NC
705 }
706 }
707 _exit(0);
808ad2d0
NC
708 abort:
709 {
710 int send_errno = errno;
711 unsigned char length = (unsigned char) strlen(where);
712 struct iovec failure[3] = {
713 {(void*)&send_errno, sizeof(send_errno)},
714 {&length, 1},
715 {(void*)where, length}
716 };
717 int got = writev(sock, failure, 3);
718 /* Bad news travels fast. Faster than data. We'll get a SIGPIPE
719 in the parent if we try to read from the socketpair after the
720 child has exited, even if there was data to read.
721 So sleep a bit to give the parent a fighting chance of
722 reading the data. */
723 sleep(2);
724 _exit((got == -1) ? errno : 0);
725 }
bf357333 726 /* End of child. */
2aa47728 727 }
41e4abd8 728 PL_dumper_fd = fd[0];
2aa47728
NC
729 close(fd[1]);
730 }
731#endif
732
ff0cee69
PP
733 /* We must account for everything. */
734
735 /* Destroy the main CV and syntax tree */
37e77c23
FC
736 /* Set PL_curcop now, because destroying ops can cause new SVs
737 to be generated in Perl_pad_swipe, and when running with
738 -DDEBUG_LEAKING_SCALARS they expect PL_curcop to point to a valid
739 op from which the filename structure member is copied. */
17fbfdf6 740 PL_curcop = &PL_compiling;
3280af22 741 if (PL_main_root) {
4e380990
DM
742 /* ensure comppad/curpad to refer to main's pad */
743 if (CvPADLIST(PL_main_cv)) {
744 PAD_SET_CUR_NOSAVE(CvPADLIST(PL_main_cv), 1);
745 }
3280af22 746 op_free(PL_main_root);
5f66b61c 747 PL_main_root = NULL;
a0d0e21e 748 }
5f66b61c 749 PL_main_start = NULL;
aac9d523
DM
750 /* note that PL_main_cv isn't usually actually freed at this point,
751 * due to the CvOUTSIDE refs from subs compiled within it. It will
752 * get freed once all the subs are freed in sv_clean_all(), for
753 * destruct_level > 0 */
3280af22 754 SvREFCNT_dec(PL_main_cv);
601f1833 755 PL_main_cv = NULL;
ca7b837b 756 PERL_SET_PHASE(PERL_PHASE_DESTRUCT);
ff0cee69 757
13621cfb
NIS
758 /* Tell PerlIO we are about to tear things apart in case
759 we have layers which are using resources that should
760 be cleaned up now.
761 */
762
763 PerlIO_destruct(aTHX);
764
3280af22 765 if (PL_sv_objcount) {
a0d0e21e
LW
766 /*
767 * Try to destruct global references. We do this first so that the
768 * destructors and destructees still exist. Some sv's might remain.
769 * Non-referenced objects are on their own.
770 */
a0d0e21e 771 sv_clean_objs();
bf9cdc68 772 PL_sv_objcount = 0;
8990e307
LW
773 }
774
5cd24f17 775 /* unhook hooks which will soon be, or use, destroyed data */
3280af22 776 SvREFCNT_dec(PL_warnhook);
a0714e2c 777 PL_warnhook = NULL;
3280af22 778 SvREFCNT_dec(PL_diehook);
a0714e2c 779 PL_diehook = NULL;
5cd24f17 780
4b556e6c 781 /* call exit list functions */
3280af22 782 while (PL_exitlistlen-- > 0)
acfe0abc 783 PL_exitlist[PL_exitlistlen].fn(aTHX_ PL_exitlist[PL_exitlistlen].ptr);
4b556e6c 784
3280af22 785 Safefree(PL_exitlist);
4b556e6c 786
1c4916e5
CB
787 PL_exitlist = NULL;
788 PL_exitlistlen = 0;
789
a3e6e81e
NC
790 SvREFCNT_dec(PL_registered_mros);
791
551a8b83 792 /* jettison our possibly duplicated environment */
4b647fb0
DM
793 /* if PERL_USE_SAFE_PUTENV is defined environ will not have been copied
794 * so we certainly shouldn't free it here
795 */
2f42fcb0 796#ifndef PERL_MICRO
4b647fb0 797#if defined(USE_ENVIRON_ARRAY) && !defined(PERL_USE_SAFE_PUTENV)
50acdf95 798 if (environ != PL_origenviron && !PL_use_safe_putenv
4efc5df6
GS
799#ifdef USE_ITHREADS
800 /* only main thread can free environ[0] contents */
801 && PL_curinterp == aTHX
802#endif
803 )
804 {
551a8b83
JH
805 I32 i;
806
807 for (i = 0; environ[i]; i++)
4b420006 808 safesysfree(environ[i]);
0631ea03 809
4b420006
JH
810 /* Must use safesysfree() when working with environ. */
811 safesysfree(environ);
551a8b83
JH
812
813 environ = PL_origenviron;
814 }
815#endif
2f42fcb0 816#endif /* !PERL_MICRO */
551a8b83 817
30985c42
JH
818 if (destruct_level == 0) {
819
820 DEBUG_P(debprofdump());
821
822#if defined(PERLIO_LAYERS)
823 /* No more IO - including error messages ! */
824 PerlIO_cleanup(aTHX);
825#endif
826
827 CopFILE_free(&PL_compiling);
30985c42
JH
828
829 /* The exit() function will do everything that needs doing. */
830 return STATUS_EXIT;
831 }
832
5f8cb046
DM
833#ifdef USE_ITHREADS
834 /* the syntax tree is shared between clones
835 * so op_free(PL_main_root) only ReREFCNT_dec's
836 * REGEXPs in the parent interpreter
837 * we need to manually ReREFCNT_dec for the clones
838 */
0547a729
DM
839 {
840 I32 i = AvFILLp(PL_regex_padav);
841 SV **ary = AvARRAY(PL_regex_padav);
842
843 for (; i; i--) {
844 SvREFCNT_dec(ary[i]);
845 ary[i] = &PL_sv_undef;
846 }
847 }
5f8cb046
DM
848#endif
849
0547a729 850
ad64d0ec 851 SvREFCNT_dec(MUTABLE_SV(PL_stashcache));
081fc587
AB
852 PL_stashcache = NULL;
853
5f05dabc
PP
854 /* loosen bonds of global variables */
855
2f9285f8
DM
856 /* XXX can PL_parser still be non-null here? */
857 if(PL_parser && PL_parser->rsfp) {
858 (void)PerlIO_close(PL_parser->rsfp);
859 PL_parser->rsfp = NULL;
8ebc5c01
PP
860 }
861
84386e14
RGS
862 if (PL_minus_F) {
863 Safefree(PL_splitstr);
864 PL_splitstr = NULL;
865 }
866
8ebc5c01 867 /* switches */
3280af22
NIS
868 PL_minus_n = FALSE;
869 PL_minus_p = FALSE;
870 PL_minus_l = FALSE;
871 PL_minus_a = FALSE;
872 PL_minus_F = FALSE;
873 PL_doswitches = FALSE;
599cee73 874 PL_dowarn = G_WARN_OFF;
1a904fc8 875#ifdef PERL_SAWAMPERSAND
d3b97530 876 PL_sawampersand = 0; /* must save all match strings */
1a904fc8 877#endif
3280af22
NIS
878 PL_unsafe = FALSE;
879
880 Safefree(PL_inplace);
bd61b366 881 PL_inplace = NULL;
a7cb1f99 882 SvREFCNT_dec(PL_patchlevel);
1e8125c6 883 SvREFCNT_dec(PL_apiversion);
3280af22
NIS
884
885 if (PL_e_script) {
886 SvREFCNT_dec(PL_e_script);
a0714e2c 887 PL_e_script = NULL;
8ebc5c01
PP
888 }
889
bf9cdc68
RG
890 PL_perldb = 0;
891
8ebc5c01
PP
892 /* magical thingies */
893
e23d9e2f
CS
894 SvREFCNT_dec(PL_ofsgv); /* *, */
895 PL_ofsgv = NULL;
5f05dabc 896
7889fe52 897 SvREFCNT_dec(PL_ors_sv); /* $\ */
a0714e2c 898 PL_ors_sv = NULL;
8ebc5c01 899
3280af22 900 SvREFCNT_dec(PL_rs); /* $/ */
a0714e2c 901 PL_rs = NULL;
dc92893f 902
d33b2eba 903 Safefree(PL_osname); /* $^O */
bd61b366 904 PL_osname = NULL;
5f05dabc 905
3280af22 906 SvREFCNT_dec(PL_statname);
a0714e2c
SS
907 PL_statname = NULL;
908 PL_statgv = NULL;
5f05dabc 909
8ebc5c01
PP
910 /* defgv, aka *_ should be taken care of elsewhere */
911
7d5ea4e7
GS
912 /* float buffer */
913 Safefree(PL_efloatbuf);
bd61b366 914 PL_efloatbuf = NULL;
7d5ea4e7
GS
915 PL_efloatsize = 0;
916
8ebc5c01 917 /* startup and shutdown function lists */
3280af22 918 SvREFCNT_dec(PL_beginav);
5a837c8f 919 SvREFCNT_dec(PL_beginav_save);
3280af22 920 SvREFCNT_dec(PL_endav);
7d30b5c4 921 SvREFCNT_dec(PL_checkav);
ece599bd 922 SvREFCNT_dec(PL_checkav_save);
3c10abe3
AG
923 SvREFCNT_dec(PL_unitcheckav);
924 SvREFCNT_dec(PL_unitcheckav_save);
3280af22 925 SvREFCNT_dec(PL_initav);
7d49f689
NC
926 PL_beginav = NULL;
927 PL_beginav_save = NULL;
928 PL_endav = NULL;
929 PL_checkav = NULL;
930 PL_checkav_save = NULL;
3c10abe3
AG
931 PL_unitcheckav = NULL;
932 PL_unitcheckav_save = NULL;
7d49f689 933 PL_initav = NULL;
5618dfe8 934
8ebc5c01 935 /* shortcuts just get cleared */
a0714e2c
SS
936 PL_envgv = NULL;
937 PL_incgv = NULL;
938 PL_hintgv = NULL;
939 PL_errgv = NULL;
940 PL_argvgv = NULL;
941 PL_argvoutgv = NULL;
942 PL_stdingv = NULL;
943 PL_stderrgv = NULL;
944 PL_last_in_gv = NULL;
945 PL_replgv = NULL;
946 PL_DBgv = NULL;
947 PL_DBline = NULL;
948 PL_DBsub = NULL;
949 PL_DBsingle = NULL;
950 PL_DBtrace = NULL;
951 PL_DBsignal = NULL;
601f1833 952 PL_DBcv = NULL;
7d49f689 953 PL_dbargs = NULL;
5c284bb0 954 PL_debstash = NULL;
8ebc5c01 955
7a1c5554 956 SvREFCNT_dec(PL_argvout_stack);
7d49f689 957 PL_argvout_stack = NULL;
8ebc5c01 958
5c831c24 959 SvREFCNT_dec(PL_modglobal);
5c284bb0 960 PL_modglobal = NULL;
5c831c24 961 SvREFCNT_dec(PL_preambleav);
7d49f689 962 PL_preambleav = NULL;
5c831c24 963 SvREFCNT_dec(PL_subname);
a0714e2c 964 PL_subname = NULL;
ca0c25f6 965#ifdef PERL_USES_PL_PIDSTATUS
5c831c24 966 SvREFCNT_dec(PL_pidstatus);
5c284bb0 967 PL_pidstatus = NULL;
ca0c25f6 968#endif
5c831c24 969 SvREFCNT_dec(PL_toptarget);
a0714e2c 970 PL_toptarget = NULL;
5c831c24 971 SvREFCNT_dec(PL_bodytarget);
a0714e2c
SS
972 PL_bodytarget = NULL;
973 PL_formtarget = NULL;
5c831c24 974
d33b2eba 975 /* free locale stuff */
b9582b6a 976#ifdef USE_LOCALE_COLLATE
d33b2eba 977 Safefree(PL_collation_name);
bd61b366 978 PL_collation_name = NULL;
b9582b6a 979#endif
d33b2eba 980
b9582b6a 981#ifdef USE_LOCALE_NUMERIC
d33b2eba 982 Safefree(PL_numeric_name);
bd61b366 983 PL_numeric_name = NULL;
a453c169 984 SvREFCNT_dec(PL_numeric_radix_sv);
a0714e2c 985 PL_numeric_radix_sv = NULL;
b9582b6a 986#endif
d33b2eba 987
5c831c24
GS
988 /* clear utf8 character classes */
989 SvREFCNT_dec(PL_utf8_alnum);
5c831c24 990 SvREFCNT_dec(PL_utf8_alpha);
5c831c24
GS
991 SvREFCNT_dec(PL_utf8_graph);
992 SvREFCNT_dec(PL_utf8_digit);
993 SvREFCNT_dec(PL_utf8_upper);
994 SvREFCNT_dec(PL_utf8_lower);
995 SvREFCNT_dec(PL_utf8_print);
996 SvREFCNT_dec(PL_utf8_punct);
5c831c24
GS
997 SvREFCNT_dec(PL_utf8_mark);
998 SvREFCNT_dec(PL_utf8_toupper);
4dbdbdc2 999 SvREFCNT_dec(PL_utf8_totitle);
5c831c24 1000 SvREFCNT_dec(PL_utf8_tolower);
b4e400f9 1001 SvREFCNT_dec(PL_utf8_tofold);
82686b01
JH
1002 SvREFCNT_dec(PL_utf8_idstart);
1003 SvREFCNT_dec(PL_utf8_idcont);
2726813d 1004 SvREFCNT_dec(PL_utf8_foldclosures);
a0714e2c 1005 PL_utf8_alnum = NULL;
a0714e2c 1006 PL_utf8_alpha = NULL;
a0714e2c
SS
1007 PL_utf8_graph = NULL;
1008 PL_utf8_digit = NULL;
1009 PL_utf8_upper = NULL;
1010 PL_utf8_lower = NULL;
1011 PL_utf8_print = NULL;
1012 PL_utf8_punct = NULL;
a0714e2c
SS
1013 PL_utf8_mark = NULL;
1014 PL_utf8_toupper = NULL;
1015 PL_utf8_totitle = NULL;
1016 PL_utf8_tolower = NULL;
1017 PL_utf8_tofold = NULL;
1018 PL_utf8_idstart = NULL;
1019 PL_utf8_idcont = NULL;
2726813d 1020 PL_utf8_foldclosures = NULL;
5c831c24 1021
971a9dd3 1022 if (!specialWARN(PL_compiling.cop_warnings))
72dc9ed5 1023 PerlMemShared_free(PL_compiling.cop_warnings);
a0714e2c 1024 PL_compiling.cop_warnings = NULL;
20439bc7
Z
1025 cophh_free(CopHINTHASH_get(&PL_compiling));
1026 CopHINTHASH_set(&PL_compiling, cophh_new_empty());
05ec9bb3 1027 CopFILE_free(&PL_compiling);
5c831c24 1028
a0d0e21e 1029 /* Prepare to destruct main symbol table. */
5f05dabc 1030
3280af22 1031 hv = PL_defstash;
ca556bcd
DM
1032 /* break ref loop *:: <=> %:: */
1033 (void)hv_delete(hv, "main::", 6, G_DISCARD);
3280af22 1034 PL_defstash = 0;
a0d0e21e 1035 SvREFCNT_dec(hv);
5c831c24 1036 SvREFCNT_dec(PL_curstname);
a0714e2c 1037 PL_curstname = NULL;
a0d0e21e 1038
5a844595
GS
1039 /* clear queued errors */
1040 SvREFCNT_dec(PL_errors);
a0714e2c 1041 PL_errors = NULL;
5a844595 1042
dd69841b
BB
1043 SvREFCNT_dec(PL_isarev);
1044
a0d0e21e 1045 FREETMPS;
9b387841 1046 if (destruct_level >= 2) {
3280af22 1047 if (PL_scopestack_ix != 0)
9b387841
NC
1048 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
1049 "Unbalanced scopes: %ld more ENTERs than LEAVEs\n",
1050 (long)PL_scopestack_ix);
3280af22 1051 if (PL_savestack_ix != 0)
9b387841
NC
1052 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
1053 "Unbalanced saves: %ld more saves than restores\n",
1054 (long)PL_savestack_ix);
3280af22 1055 if (PL_tmps_floor != -1)
9b387841
NC
1056 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),"Unbalanced tmps: %ld more allocs than frees\n",
1057 (long)PL_tmps_floor + 1);
a0d0e21e 1058 if (cxstack_ix != -1)
9b387841
NC
1059 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),"Unbalanced context: %ld more PUSHes than POPs\n",
1060 (long)cxstack_ix + 1);
a0d0e21e 1061 }
8990e307 1062
0547a729
DM
1063#ifdef USE_ITHREADS
1064 SvREFCNT_dec(PL_regex_padav);
1065 PL_regex_padav = NULL;
1066 PL_regex_pad = NULL;
1067#endif
1068
776df701 1069#ifdef PERL_IMPLICIT_CONTEXT
57bb2458
JH
1070 /* the entries in this list are allocated via SV PVX's, so get freed
1071 * in sv_clean_all */
1072 Safefree(PL_my_cxt_list);
776df701 1073#endif
57bb2458 1074
8990e307 1075 /* Now absolutely destruct everything, somehow or other, loops or no. */
5226ed68
JH
1076
1077 /* the 2 is for PL_fdpid and PL_strtab */
d17ea597 1078 while (sv_clean_all() > 2)
5226ed68
JH
1079 ;
1080
23083432
FC
1081#ifdef USE_ITHREADS
1082 Safefree(PL_stashpad); /* must come after sv_clean_all */
1083#endif
1084
d4777f27
GS
1085 AvREAL_off(PL_fdpid); /* no surviving entries */
1086 SvREFCNT_dec(PL_fdpid); /* needed in io_close() */
7d49f689 1087 PL_fdpid = NULL;
d33b2eba 1088
6c644e78
GS
1089#ifdef HAVE_INTERP_INTERN
1090 sys_intern_clear();
1091#endif
1092
6e72f9df
PP
1093 /* Destruct the global string table. */
1094 {
1095 /* Yell and reset the HeVAL() slots that are still holding refcounts,
1096 * so that sv_free() won't fail on them.
80459961
NC
1097 * Now that the global string table is using a single hunk of memory
1098 * for both HE and HEK, we either need to explicitly unshare it the
1099 * correct way, or actually free things here.
6e72f9df 1100 */
80459961
NC
1101 I32 riter = 0;
1102 const I32 max = HvMAX(PL_strtab);
c4420975 1103 HE * const * const array = HvARRAY(PL_strtab);
80459961
NC
1104 HE *hent = array[0];
1105
6e72f9df 1106 for (;;) {
0453d815 1107 if (hent && ckWARN_d(WARN_INTERNAL)) {
44f8325f 1108 HE * const next = HeNEXT(hent);
9014280d 1109 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
44f8325f 1110 "Unbalanced string table refcount: (%ld) for \"%s\"",
de616631 1111 (long)hent->he_valu.hent_refcount, HeKEY(hent));
80459961
NC
1112 Safefree(hent);
1113 hent = next;
6e72f9df
PP
1114 }
1115 if (!hent) {
1116 if (++riter > max)
1117 break;
1118 hent = array[riter];
1119 }
1120 }
80459961
NC
1121
1122 Safefree(array);
1123 HvARRAY(PL_strtab) = 0;
1124 HvTOTALKEYS(PL_strtab) = 0;
6e72f9df 1125 }
3280af22 1126 SvREFCNT_dec(PL_strtab);
6e72f9df 1127
e652bb2f 1128#ifdef USE_ITHREADS
c21d1a0f 1129 /* free the pointer tables used for cloning */
a0739874 1130 ptr_table_free(PL_ptr_table);
bf9cdc68 1131 PL_ptr_table = (PTR_TBL_t*)NULL;
53186e96 1132#endif
a0739874 1133
d33b2eba
GS
1134 /* free special SVs */
1135
1136 SvREFCNT(&PL_sv_yes) = 0;
1137 sv_clear(&PL_sv_yes);
1138 SvANY(&PL_sv_yes) = NULL;
4c5e2b0d 1139 SvFLAGS(&PL_sv_yes) = 0;
d33b2eba
GS
1140
1141 SvREFCNT(&PL_sv_no) = 0;
1142 sv_clear(&PL_sv_no);
1143 SvANY(&PL_sv_no) = NULL;
4c5e2b0d 1144 SvFLAGS(&PL_sv_no) = 0;
01724ea0 1145
9f375a43
DM
1146 {
1147 int i;
1148 for (i=0; i<=2; i++) {
1149 SvREFCNT(PERL_DEBUG_PAD(i)) = 0;
1150 sv_clear(PERL_DEBUG_PAD(i));
1151 SvANY(PERL_DEBUG_PAD(i)) = NULL;
1152 SvFLAGS(PERL_DEBUG_PAD(i)) = 0;
1153 }
1154 }
1155
0453d815 1156 if (PL_sv_count != 0 && ckWARN_d(WARN_INTERNAL))
9014280d 1157 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),"Scalars leaked: %ld\n", (long)PL_sv_count);
6e72f9df 1158
eba0f806
DM
1159#ifdef DEBUG_LEAKING_SCALARS
1160 if (PL_sv_count != 0) {
1161 SV* sva;
1162 SV* sv;
eb578fdb 1163 SV* svend;
eba0f806 1164
ad64d0ec 1165 for (sva = PL_sv_arenaroot; sva; sva = MUTABLE_SV(SvANY(sva))) {
eba0f806
DM
1166 svend = &sva[SvREFCNT(sva)];
1167 for (sv = sva + 1; sv < svend; ++sv) {
e4787c0c 1168 if (SvTYPE(sv) != (svtype)SVTYPEMASK) {
a548cda8 1169 PerlIO_printf(Perl_debug_log, "leaked: sv=0x%p"
61b61456 1170 " flags=0x%"UVxf
fd0854ff 1171 " refcnt=%"UVuf pTHX__FORMAT "\n"
cd676548
DM
1172 "\tallocated at %s:%d %s %s (parent 0x%"UVxf");"
1173 "serial %"UVuf"\n",
574b8821
NC
1174 (void*)sv, (UV)sv->sv_flags, (UV)sv->sv_refcnt
1175 pTHX__VALUE,
fd0854ff
DM
1176 sv->sv_debug_file ? sv->sv_debug_file : "(unknown)",
1177 sv->sv_debug_line,
1178 sv->sv_debug_inpad ? "for" : "by",
1179 sv->sv_debug_optype ?
1180 PL_op_name[sv->sv_debug_optype]: "(none)",
cd676548 1181 PTR2UV(sv->sv_debug_parent),
cbe56f1d 1182 sv->sv_debug_serial
fd0854ff 1183 );
2aa47728 1184#ifdef DEBUG_LEAKING_SCALARS_FORK_DUMP
41e4abd8 1185 Perl_dump_sv_child(aTHX_ sv);
2aa47728 1186#endif
eba0f806
DM
1187 }
1188 }
1189 }
1190 }
2aa47728
NC
1191#ifdef DEBUG_LEAKING_SCALARS_FORK_DUMP
1192 {
1193 int status;
1194 fd_set rset;
1195 /* Wait for up to 4 seconds for child to terminate.
1196 This seems to be the least effort way of timing out on reaping
1197 its exit status. */
1198 struct timeval waitfor = {4, 0};
41e4abd8 1199 int sock = PL_dumper_fd;
2aa47728
NC
1200
1201 shutdown(sock, 1);
1202 FD_ZERO(&rset);
1203 FD_SET(sock, &rset);
1204 select(sock + 1, &rset, NULL, NULL, &waitfor);
1205 waitpid(child, &status, WNOHANG);
1206 close(sock);
1207 }
1208#endif
eba0f806 1209#endif
77abb4c6
NC
1210#ifdef DEBUG_LEAKING_SCALARS_ABORT
1211 if (PL_sv_count)
1212 abort();
1213#endif
bf9cdc68 1214 PL_sv_count = 0;
eba0f806 1215
56a2bab7 1216#if defined(PERLIO_LAYERS)
3a1ee7e8
NIS
1217 /* No more IO - including error messages ! */
1218 PerlIO_cleanup(aTHX);
1219#endif
1220
9f4bd222 1221 /* sv_undef needs to stay immortal until after PerlIO_cleanup
a0714e2c 1222 as currently layers use it rather than NULL as a marker
9f4bd222
NIS
1223 for no arg - and will try and SvREFCNT_dec it.
1224 */
1225 SvREFCNT(&PL_sv_undef) = 0;
1226 SvREADONLY_off(&PL_sv_undef);
1227
3280af22 1228 Safefree(PL_origfilename);
bd61b366 1229 PL_origfilename = NULL;
43c5f42d 1230 Safefree(PL_reg_curpm);
82ba1be6 1231 Safefree(PL_reg_poscache);
dd28f7bb 1232 free_tied_hv_pool();
3280af22 1233 Safefree(PL_op_mask);
cf36064f 1234 Safefree(PL_psig_name);
bf9cdc68 1235 PL_psig_name = (SV**)NULL;
d525a7b2 1236 PL_psig_ptr = (SV**)NULL;
31c91b43
LR
1237 {
1238 /* We need to NULL PL_psig_pend first, so that
1239 signal handlers know not to use it */
1240 int *psig_save = PL_psig_pend;
1241 PL_psig_pend = (int*)NULL;
1242 Safefree(psig_save);
1243 }
6e72f9df 1244 nuke_stacks();
284167a5
SM
1245 TAINTING_set(FALSE);
1246 TAINT_WARN_set(FALSE);
3280af22 1247 PL_hints = 0; /* Reset hints. Should hints be per-interpreter ? */
bf9cdc68 1248 PL_debug = 0;
ac27b0f5 1249
a0d0e21e 1250 DEBUG_P(debprofdump());
d33b2eba 1251
e5dd39fc 1252#ifdef USE_REENTRANT_API
10bc17b6 1253 Perl_reentrant_free(aTHX);
e5dd39fc
AB
1254#endif
1255
612f20c3
GS
1256 sv_free_arenas();
1257
5d9a96ca
DM
1258 while (PL_regmatch_slab) {
1259 regmatch_slab *s = PL_regmatch_slab;
1260 PL_regmatch_slab = PL_regmatch_slab->next;
1261 Safefree(s);
1262 }
1263
fc36a67e
PP
1264 /* As the absolutely last thing, free the non-arena SV for mess() */
1265
3280af22 1266 if (PL_mess_sv) {
f350b448
NC
1267 /* we know that type == SVt_PVMG */
1268
9c63abab 1269 /* it could have accumulated taint magic */
f350b448
NC
1270 MAGIC* mg;
1271 MAGIC* moremagic;
1272 for (mg = SvMAGIC(PL_mess_sv); mg; mg = moremagic) {
1273 moremagic = mg->mg_moremagic;
1274 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global
1275 && mg->mg_len >= 0)
1276 Safefree(mg->mg_ptr);
1277 Safefree(mg);
9c63abab 1278 }
f350b448 1279
fc36a67e 1280 /* we know that type >= SVt_PV */
8bd4d4c5 1281 SvPV_free(PL_mess_sv);
3280af22
NIS
1282 Safefree(SvANY(PL_mess_sv));
1283 Safefree(PL_mess_sv);
a0714e2c 1284 PL_mess_sv = NULL;
fc36a67e 1285 }
37038d91 1286 return STATUS_EXIT;
79072805
LW
1287}
1288
954c1994
GS
1289/*
1290=for apidoc perl_free
1291
1292Releases a Perl interpreter. See L<perlembed>.
1293
1294=cut
1295*/
1296
79072805 1297void
0cb96387 1298perl_free(pTHXx)
79072805 1299{
5174512c
NC
1300 dVAR;
1301
7918f24d
NC
1302 PERL_ARGS_ASSERT_PERL_FREE;
1303
c301d606
DM
1304 if (PL_veto_cleanup)
1305 return;
1306
7cb608b5 1307#ifdef PERL_TRACK_MEMPOOL
55ef9aae
MHM
1308 {
1309 /*
1310 * Don't free thread memory if PERL_DESTRUCT_LEVEL is set to a non-zero
1311 * value as we're probably hunting memory leaks then
1312 */
36e77d41 1313 if (PL_perl_destruct_level == 0) {
4fd0a9b8 1314 const U32 old_debug = PL_debug;
55ef9aae
MHM
1315 /* Emulate the PerlHost behaviour of free()ing all memory allocated in this
1316 thread at thread exit. */
4fd0a9b8
NC
1317 if (DEBUG_m_TEST) {
1318 PerlIO_puts(Perl_debug_log, "Disabling memory debugging as we "
1319 "free this thread's memory\n");
1320 PL_debug &= ~ DEBUG_m_FLAG;
1321 }
55ef9aae
MHM
1322 while(aTHXx->Imemory_debug_header.next != &(aTHXx->Imemory_debug_header))
1323 safesysfree(sTHX + (char *)(aTHXx->Imemory_debug_header.next));
4fd0a9b8 1324 PL_debug = old_debug;
55ef9aae
MHM
1325 }
1326 }
7cb608b5
NC
1327#endif
1328
acfe0abc 1329#if defined(WIN32) || defined(NETWARE)
ce3e5b80 1330# if defined(PERL_IMPLICIT_SYS)
b36c9a52 1331 {
acfe0abc 1332# ifdef NETWARE
7af12a34 1333 void *host = nw_internal_host;
acfe0abc 1334# else
7af12a34 1335 void *host = w32_internal_host;
acfe0abc 1336# endif
7af12a34 1337 PerlMem_free(aTHXx);
acfe0abc 1338# ifdef NETWARE
7af12a34 1339 nw_delete_internal_host(host);
acfe0abc 1340# else
7af12a34 1341 win32_delete_internal_host(host);
acfe0abc 1342# endif
7af12a34 1343 }
1c0ca838
GS
1344# else
1345 PerlMem_free(aTHXx);
1346# endif
acfe0abc
GS
1347#else
1348 PerlMem_free(aTHXx);
76e3520e 1349#endif
79072805
LW
1350}
1351
b7f7fff6 1352#if defined(USE_ITHREADS)
aebd1ac7
GA
1353/* provide destructors to clean up the thread key when libperl is unloaded */
1354#ifndef WIN32 /* handled during DLL_PROCESS_DETACH in win32/perllib.c */
1355
826955bd 1356#if defined(__hpux) && !(defined(__ux_version) && __ux_version <= 1020) && !defined(__GNUC__)
aebd1ac7 1357#pragma fini "perl_fini"
666ad1ec
GA
1358#elif defined(__sun) && !defined(__GNUC__)
1359#pragma fini (perl_fini)
aebd1ac7
GA
1360#endif
1361
0dbb1585
AL
1362static void
1363#if defined(__GNUC__)
1364__attribute__((destructor))
aebd1ac7 1365#endif
de009b76 1366perl_fini(void)
aebd1ac7 1367{
27da23d5 1368 dVAR;
c301d606 1369 if (PL_curinterp && !PL_veto_cleanup)
aebd1ac7
GA
1370 FREE_THREAD_KEY;
1371}
1372
1373#endif /* WIN32 */
1374#endif /* THREADS */
1375
4b556e6c 1376void
864dbfa3 1377Perl_call_atexit(pTHX_ ATEXIT_t fn, void *ptr)
4b556e6c 1378{
97aff369 1379 dVAR;
3280af22
NIS
1380 Renew(PL_exitlist, PL_exitlistlen+1, PerlExitListEntry);
1381 PL_exitlist[PL_exitlistlen].fn = fn;
1382 PL_exitlist[PL_exitlistlen].ptr = ptr;
1383 ++PL_exitlistlen;
4b556e6c
JD
1384}
1385
b7975bdd
NC
1386STATIC void
1387S_set_caret_X(pTHX) {
97aff369 1388 dVAR;
fafc274c 1389 GV* tmpgv = gv_fetchpvs("\030", GV_ADD|GV_NOTQUAL, SVt_PV); /* $^X */
b7975bdd 1390 if (tmpgv) {
0f1723e1 1391 SV *const caret_x = GvSV(tmpgv);
82552a95
NC
1392#if defined(OS2)
1393 sv_setpv(caret_x, os2_execname(aTHX));
1394#else
2982a345
NC
1395# ifdef USE_KERN_PROC_PATHNAME
1396 size_t size = 0;
1397 int mib[4];
1398 mib[0] = CTL_KERN;
1399 mib[1] = KERN_PROC;
1400 mib[2] = KERN_PROC_PATHNAME;
1401 mib[3] = -1;
1402
1403 if (sysctl(mib, 4, NULL, &size, NULL, 0) == 0
1404 && size > 0 && size < MAXPATHLEN * MAXPATHLEN) {
1405 sv_grow(caret_x, size);
1406
1407 if (sysctl(mib, 4, SvPVX(caret_x), &size, NULL, 0) == 0
1408 && size > 2) {
1409 SvPOK_only(caret_x);
1410 SvCUR_set(caret_x, size - 1);
1411 SvTAINT(caret_x);
1412 return;
1413 }
1414 }
ae60cb46
NC
1415# elif defined(USE_NSGETEXECUTABLEPATH)
1416 char buf[1];
1417 uint32_t size = sizeof(buf);
ae60cb46
NC
1418
1419 _NSGetExecutablePath(buf, &size);
1420 if (size < MAXPATHLEN * MAXPATHLEN) {
1421 sv_grow(caret_x, size);
1422 if (_NSGetExecutablePath(SvPVX(caret_x), &size) == 0) {
1423 char *const tidied = realpath(SvPVX(caret_x), NULL);
1424 if (tidied) {
1425 sv_setpv(caret_x, tidied);
1426 free(tidied);
1427 } else {
1428 SvPOK_only(caret_x);
1429 SvCUR_set(caret_x, size);
1430 }
1431 return;
1432 }
1433 }
2982a345 1434# elif defined(HAS_PROCSELFEXE)
700dd4f8
NC
1435 char buf[MAXPATHLEN];
1436 int len = readlink(PROCSELFEXE_PATH, buf, sizeof(buf) - 1);
1437
1438 /* On Playstation2 Linux V1.0 (kernel 2.2.1) readlink(/proc/self/exe)
1439 includes a spurious NUL which will cause $^X to fail in system
1440 or backticks (this will prevent extensions from being built and
1441 many tests from working). readlink is not meant to add a NUL.
1442 Normal readlink works fine.
1443 */
1444 if (len > 0 && buf[len-1] == '\0') {
1445 len--;
1446 }
1447
1448 /* FreeBSD's implementation is acknowledged to be imperfect, sometimes
1449 returning the text "unknown" from the readlink rather than the path
1450 to the executable (or returning an error from the readlink). Any
1451 valid path has a '/' in it somewhere, so use that to validate the
1452 result. See http://www.freebsd.org/cgi/query-pr.cgi?pr=35703
1453 */
1454 if (len > 0 && memchr(buf, '/', len)) {
1455 sv_setpvn(caret_x, buf, len);
82552a95 1456 return;
700dd4f8 1457 }
82552a95
NC
1458# endif
1459 /* Fallback to this: */
0f1723e1 1460 sv_setpv(caret_x, PL_origargv[0]);
b7975bdd 1461#endif
b7975bdd
NC
1462 }
1463}
1464
954c1994
GS
1465/*
1466=for apidoc perl_parse
1467
1468Tells a Perl interpreter to parse a Perl script. See L<perlembed>.
1469
1470=cut
1471*/
1472
03d9f026
FC
1473#define SET_CURSTASH(newstash) \
1474 if (PL_curstash != newstash) { \
1475 SvREFCNT_dec(PL_curstash); \
1476 PL_curstash = (HV *)SvREFCNT_inc(newstash); \
1477 }
1478
79072805 1479int
0cb96387 1480perl_parse(pTHXx_ XSINIT_t xsinit, int argc, char **argv, char **env)
8d063cd8 1481{
27da23d5 1482 dVAR;
6224f72b 1483 I32 oldscope;
6224f72b 1484 int ret;
db36c5a1 1485 dJMPENV;
8d063cd8 1486
7918f24d
NC
1487 PERL_ARGS_ASSERT_PERL_PARSE;
1488#ifndef MULTIPLICITY
ed6c66dd 1489 PERL_UNUSED_ARG(my_perl);
7918f24d 1490#endif
7dc86639 1491#if defined(USE_HASH_SEED) || defined(USE_HASH_SEED_EXPLICIT) || defined(USE_HASH_SEED_DEBUG)
b0891165 1492 {
7dc86639
YO
1493 const char * const s = PerlEnv_getenv("PERL_HASH_SEED_DEBUG");
1494
1495 if (s && (atoi(s) == 1)) {
1496 unsigned char *seed= PERL_HASH_SEED;
1497 unsigned char *seed_end= PERL_HASH_SEED + PERL_HASH_SEED_BYTES;
1498 PerlIO_printf(Perl_debug_log, "HASH_FUNCTION = %s HASH_SEED = 0x", PERL_HASH_FUNC);
1499 while (seed < seed_end) {
1500 PerlIO_printf(Perl_debug_log, "%02x", *seed++);
1501 }
1502 PerlIO_printf(Perl_debug_log, "\n");
1503 }
b0891165
JH
1504 }
1505#endif /* #if defined(USE_HASH_SEED) || defined(USE_HASH_SEED_EXPLICIT) */
3280af22 1506 PL_origargc = argc;
e2975953 1507 PL_origargv = argv;
a0d0e21e 1508
a2722ac9
GA
1509 if (PL_origalen != 0) {
1510 PL_origalen = 1; /* don't use old PL_origalen if perl_parse() is called again */
1511 }
1512 else {
3cb9023d
JH
1513 /* Set PL_origalen be the sum of the contiguous argv[]
1514 * elements plus the size of the env in case that it is
e9137a8e 1515 * contiguous with the argv[]. This is used in mg.c:Perl_magic_set()
3cb9023d
JH
1516 * as the maximum modifiable length of $0. In the worst case
1517 * the area we are able to modify is limited to the size of
43c32782 1518 * the original argv[0]. (See below for 'contiguous', though.)
3cb9023d 1519 * --jhi */
e1ec3a88 1520 const char *s = NULL;
54bfe034 1521 int i;
1b6737cc 1522 const UV mask =
7d8e7db3 1523 ~(UV)(PTRSIZE == 4 ? 3 : PTRSIZE == 8 ? 7 : PTRSIZE == 16 ? 15 : 0);
43c32782 1524 /* Do the mask check only if the args seem like aligned. */
1b6737cc 1525 const UV aligned =
43c32782
JH
1526 (mask < ~(UV)0) && ((PTR2UV(argv[0]) & mask) == PTR2UV(argv[0]));
1527
1528 /* See if all the arguments are contiguous in memory. Note
1529 * that 'contiguous' is a loose term because some platforms
1530 * align the argv[] and the envp[]. If the arguments look
1531 * like non-aligned, assume that they are 'strictly' or
1532 * 'traditionally' contiguous. If the arguments look like
1533 * aligned, we just check that they are within aligned
1534 * PTRSIZE bytes. As long as no system has something bizarre
1535 * like the argv[] interleaved with some other data, we are
1536 * fine. (Did I just evoke Murphy's Law?) --jhi */
c8941eeb
JH
1537 if (PL_origargv && PL_origargc >= 1 && (s = PL_origargv[0])) {
1538 while (*s) s++;
1539 for (i = 1; i < PL_origargc; i++) {
1540 if ((PL_origargv[i] == s + 1
43c32782 1541#ifdef OS2
c8941eeb 1542 || PL_origargv[i] == s + 2
43c32782 1543#endif
c8941eeb
JH
1544 )
1545 ||
1546 (aligned &&
1547 (PL_origargv[i] > s &&
1548 PL_origargv[i] <=
1549 INT2PTR(char *, PTR2UV(s + PTRSIZE) & mask)))
1550 )
1551 {
1552 s = PL_origargv[i];
1553 while (*s) s++;
1554 }
1555 else
1556 break;
54bfe034 1557 }
54bfe034 1558 }
a4a109c2
JD
1559
1560#ifndef PERL_USE_SAFE_PUTENV
3cb9023d 1561 /* Can we grab env area too to be used as the area for $0? */
a4a109c2 1562 if (s && PL_origenviron && !PL_use_safe_putenv) {
9d419b5f 1563 if ((PL_origenviron[0] == s + 1)
43c32782
JH
1564 ||
1565 (aligned &&
1566 (PL_origenviron[0] > s &&
1567 PL_origenviron[0] <=
1568 INT2PTR(char *, PTR2UV(s + PTRSIZE) & mask)))
1569 )
1570 {
9d419b5f 1571#ifndef OS2 /* ENVIRON is read by the kernel too. */
43c32782
JH
1572 s = PL_origenviron[0];
1573 while (*s) s++;
1574#endif
bd61b366 1575 my_setenv("NoNe SuCh", NULL);
43c32782
JH
1576 /* Force copy of environment. */
1577 for (i = 1; PL_origenviron[i]; i++) {
1578 if (PL_origenviron[i] == s + 1
1579 ||
1580 (aligned &&
1581 (PL_origenviron[i] > s &&
1582 PL_origenviron[i] <=
1583 INT2PTR(char *, PTR2UV(s + PTRSIZE) & mask)))
1584 )
1585 {
1586 s = PL_origenviron[i];
1587 while (*s) s++;
1588 }
1589 else
1590 break;
54bfe034 1591 }
43c32782 1592 }
54bfe034 1593 }
a4a109c2
JD
1594#endif /* !defined(PERL_USE_SAFE_PUTENV) */
1595
2d2af554 1596 PL_origalen = s ? s - PL_origargv[0] + 1 : 0;
54bfe034
JH
1597 }
1598
3280af22 1599 if (PL_do_undump) {
a0d0e21e
LW
1600
1601 /* Come here if running an undumped a.out. */
1602
3280af22
NIS
1603 PL_origfilename = savepv(argv[0]);
1604 PL_do_undump = FALSE;
a0d0e21e 1605 cxstack_ix = -1; /* start label stack again */
748a9306 1606 init_ids();
284167a5 1607 assert (!TAINT_get);
b7975bdd
NC
1608 TAINT;
1609 S_set_caret_X(aTHX);
1610 TAINT_NOT;
a0d0e21e
LW
1611 init_postdump_symbols(argc,argv,env);
1612 return 0;
1613 }
1614
3280af22 1615 if (PL_main_root) {
3280af22 1616 op_free(PL_main_root);
5f66b61c 1617 PL_main_root = NULL;
ff0cee69 1618 }
5f66b61c 1619 PL_main_start = NULL;
3280af22 1620 SvREFCNT_dec(PL_main_cv);
601f1833 1621 PL_main_cv = NULL;
79072805 1622
3280af22
NIS
1623 time(&PL_basetime);
1624 oldscope = PL_scopestack_ix;
599cee73 1625 PL_dowarn = G_WARN_OFF;
f86702cc 1626
14dd3ad8 1627 JMPENV_PUSH(ret);
6224f72b 1628 switch (ret) {
312caa8e 1629 case 0:
14dd3ad8 1630 parse_body(env,xsinit);
9ebf26ad 1631 if (PL_unitcheckav) {
3c10abe3 1632 call_list(oldscope, PL_unitcheckav);
9ebf26ad
FR
1633 }
1634 if (PL_checkav) {
ca7b837b 1635 PERL_SET_PHASE(PERL_PHASE_CHECK);
7d30b5c4 1636 call_list(oldscope, PL_checkav);
9ebf26ad 1637 }
14dd3ad8
GS
1638 ret = 0;
1639 break;
6224f72b
GS
1640 case 1:
1641 STATUS_ALL_FAILURE;
1642 /* FALL THROUGH */
1643 case 2:
1644 /* my_exit() was called */
3280af22 1645 while (PL_scopestack_ix > oldscope)
6224f72b
GS
1646 LEAVE;
1647 FREETMPS;
03d9f026 1648 SET_CURSTASH(PL_defstash);
9ebf26ad 1649 if (PL_unitcheckav) {
3c10abe3 1650 call_list(oldscope, PL_unitcheckav);
9ebf26ad
FR
1651 }
1652 if (PL_checkav) {
ca7b837b 1653 PERL_SET_PHASE(PERL_PHASE_CHECK);
7d30b5c4 1654 call_list(oldscope, PL_checkav);
9ebf26ad 1655 }
37038d91 1656 ret = STATUS_EXIT;
14dd3ad8 1657 break;
6224f72b 1658 case 3:
bf49b057 1659 PerlIO_printf(Perl_error_log, "panic: top_env\n");
14dd3ad8
GS
1660 ret = 1;
1661 break;
6224f72b 1662 }
14dd3ad8
GS
1663 JMPENV_POP;
1664 return ret;
1665}
1666
4a5df386
NC
1667/* This needs to stay in perl.c, as perl.c is compiled with different flags for
1668 miniperl, and we need to see those flags reflected in the values here. */
1669
1670/* What this returns is subject to change. Use the public interface in Config.
1671 */
1672static void
1673S_Internals_V(pTHX_ CV *cv)
1674{
1675 dXSARGS;
1676#ifdef LOCAL_PATCH_COUNT
1677 const int local_patch_count = LOCAL_PATCH_COUNT;
1678#else
1679 const int local_patch_count = 0;
1680#endif
2dc296d2 1681 const int entries = 3 + local_patch_count;
4a5df386 1682 int i;
fe1c5936 1683 static const char non_bincompat_options[] =
4a5df386
NC
1684# ifdef DEBUGGING
1685 " DEBUGGING"
1686# endif
1687# ifdef NO_MATHOMS
0d311fbe 1688 " NO_MATHOMS"
4a5df386
NC
1689# endif
1690# ifdef PERL_DISABLE_PMC
1691 " PERL_DISABLE_PMC"
1692# endif
1693# ifdef PERL_DONT_CREATE_GVSV
1694 " PERL_DONT_CREATE_GVSV"
1695# endif
9a044a43
NC
1696# ifdef PERL_EXTERNAL_GLOB
1697 " PERL_EXTERNAL_GLOB"
1698# endif
4a5df386
NC
1699# ifdef PERL_IS_MINIPERL
1700 " PERL_IS_MINIPERL"
1701# endif
1702# ifdef PERL_MALLOC_WRAP
1703 " PERL_MALLOC_WRAP"
1704# endif
1705# ifdef PERL_MEM_LOG
1706 " PERL_MEM_LOG"
1707# endif
1708# ifdef PERL_MEM_LOG_NOIMPL
1709 " PERL_MEM_LOG_NOIMPL"
1710# endif
c3cf41ec
NC
1711# ifdef PERL_PRESERVE_IVUV
1712 " PERL_PRESERVE_IVUV"
1713# endif
c051e30b
NC
1714# ifdef PERL_RELOCATABLE_INCPUSH
1715 " PERL_RELOCATABLE_INCPUSH"
1716# endif
4a5df386
NC
1717# ifdef PERL_USE_DEVEL
1718 " PERL_USE_DEVEL"
1719# endif
1720# ifdef PERL_USE_SAFE_PUTENV
1721 " PERL_USE_SAFE_PUTENV"
1722# endif
a3749cf3
NC
1723# ifdef UNLINK_ALL_VERSIONS
1724 " UNLINK_ALL_VERSIONS"
1725# endif
de618ee4
NC
1726# ifdef USE_ATTRIBUTES_FOR_PERLIO
1727 " USE_ATTRIBUTES_FOR_PERLIO"
1728# endif
4a5df386
NC
1729# ifdef USE_FAST_STDIO
1730 " USE_FAST_STDIO"
1731# endif
98548bdf
NC
1732# ifdef USE_LOCALE
1733 " USE_LOCALE"
1734# endif
98548bdf
NC
1735# ifdef USE_LOCALE_CTYPE
1736 " USE_LOCALE_CTYPE"
1737# endif
5a8d8935
NC
1738# ifdef USE_PERL_ATOF
1739 " USE_PERL_ATOF"
1740# endif
0d311fbe
NC
1741# ifdef USE_SITECUSTOMIZE
1742 " USE_SITECUSTOMIZE"
1743# endif
4a5df386
NC
1744 ;
1745 PERL_UNUSED_ARG(cv);
1746 PERL_UNUSED_ARG(items);
1747
1748 EXTEND(SP, entries);
1749
1750 PUSHs(sv_2mortal(newSVpv(PL_bincompat_options, 0)));
1751 PUSHs(Perl_newSVpvn_flags(aTHX_ non_bincompat_options,
1752 sizeof(non_bincompat_options) - 1, SVs_TEMP));
1753
1754#ifdef __DATE__
1755# ifdef __TIME__
1756 PUSHs(Perl_newSVpvn_flags(aTHX_
1757 STR_WITH_LEN("Compiled at " __DATE__ " " __TIME__),
1758 SVs_TEMP));
1759# else
1760 PUSHs(Perl_newSVpvn_flags(aTHX_ STR_WITH_LEN("Compiled on " __DATE__),
1761 SVs_TEMP));
1762# endif
1763#else
1764 PUSHs(&PL_sv_undef);
1765#endif
1766
4a5df386
NC
1767 for (i = 1; i <= local_patch_count; i++) {
1768 /* This will be an undef, if PL_localpatches[i] is NULL. */
1769 PUSHs(sv_2mortal(newSVpv(PL_localpatches[i], 0)));
1770 }
1771
1772 XSRETURN(entries);
1773}
1774
be71fc8f
NC
1775#define INCPUSH_UNSHIFT 0x01
1776#define INCPUSH_ADD_OLD_VERS 0x02
1777#define INCPUSH_ADD_VERSIONED_SUB_DIRS 0x04
1778#define INCPUSH_ADD_ARCHONLY_SUB_DIRS 0x08
1779#define INCPUSH_NOT_BASEDIR 0x10
1780#define INCPUSH_CAN_RELOCATE 0x20
1e3208d8
NC
1781#define INCPUSH_ADD_SUB_DIRS \
1782 (INCPUSH_ADD_VERSIONED_SUB_DIRS|INCPUSH_ADD_ARCHONLY_SUB_DIRS)
e28f3139 1783
312caa8e 1784STATIC void *
14dd3ad8 1785S_parse_body(pTHX_ char **env, XSINIT_t xsinit)
312caa8e 1786{
27da23d5 1787 dVAR;
2f9285f8 1788 PerlIO *rsfp;
312caa8e 1789 int argc = PL_origargc;
8f42b153 1790 char **argv = PL_origargv;
e1ec3a88 1791 const char *scriptname = NULL;
312caa8e 1792 VOL bool dosearch = FALSE;
eb578fdb 1793 char c;
737c24fc 1794 bool doextract = FALSE;
bd61b366 1795 const char *cddir = NULL;
ab019eaa 1796#ifdef USE_SITECUSTOMIZE
20ef40cf 1797 bool minus_f = FALSE;
ab019eaa 1798#endif
95670bde 1799 SV *linestr_sv = NULL;
5486870f 1800 bool add_read_e_script = FALSE;
87606032 1801 U32 lex_start_flags = 0;
009d90df 1802
ca7b837b 1803 PERL_SET_PHASE(PERL_PHASE_START);
9ebf26ad 1804
6224f72b 1805 init_main_stash();
54310121 1806
c7030b81
NC
1807 {
1808 const char *s;
6224f72b
GS
1809 for (argc--,argv++; argc > 0; argc--,argv++) {
1810 if (argv[0][0] != '-' || !argv[0][1])
1811 break;
6224f72b
GS
1812 s = argv[0]+1;
1813 reswitch:
47f56822 1814 switch ((c = *s)) {
729a02f2 1815 case 'C':
1d5472a9
GS
1816#ifndef PERL_STRICT_CR
1817 case '\r':
1818#endif
6224f72b
GS
1819 case ' ':
1820 case '0':
1821 case 'F':
1822 case 'a':
1823 case 'c':
1824 case 'd':
1825 case 'D':
1826 case 'h':
1827 case 'i':
1828 case 'l':
1829 case 'M':
1830 case 'm':
1831 case 'n':
1832 case 'p':
1833 case 's':
1834 case 'u':
1835 case 'U':
1836 case 'v':
599cee73
PM
1837 case 'W':
1838 case 'X':
6224f72b 1839 case 'w':
97bd5664 1840 if ((s = moreswitches(s)))
6224f72b
GS
1841 goto reswitch;
1842 break;
33b78306 1843
1dbad523 1844 case 't':
284167a5
SM
1845#if SILENT_NO_TAINT_SUPPORT
1846 /* silently ignore */
1847#elif NO_TAINT_SUPPORT
1848 Perl_croak("This perl was compiled without taint support. "
1849 "Cowardly refusing to run with -t or -T flags");
1850#else
22f7c9c9 1851 CHECK_MALLOC_TOO_LATE_FOR('t');
284167a5
SM
1852 if( !TAINTING_get ) {
1853 TAINT_WARN_set(TRUE);
1854 TAINTING_set(TRUE);
317ea90d 1855 }
284167a5 1856#endif
317ea90d
MS
1857 s++;
1858 goto reswitch;
6224f72b 1859 case 'T':
284167a5
SM
1860#if SILENT_NO_TAINT_SUPPORT
1861 /* silently ignore */
1862#elif NO_TAINT_SUPPORT
1863 Perl_croak("This perl was compiled without taint support. "
1864 "Cowardly refusing to run with -t or -T flags");
1865#else
22f7c9c9 1866 CHECK_MALLOC_TOO_LATE_FOR('T');
284167a5
SM
1867 TAINTING_set(TRUE);
1868 TAINT_WARN_set(FALSE);
1869#endif
6224f72b
GS
1870 s++;
1871 goto reswitch;
f86702cc 1872
bc9b29db
RH
1873 case 'E':
1874 PL_minus_E = TRUE;
1875 /* FALL THROUGH */
6224f72b 1876 case 'e':
f20b2998 1877 forbid_setid('e', FALSE);
3280af22 1878 if (!PL_e_script) {
396482e1 1879 PL_e_script = newSVpvs("");
5486870f 1880 add_read_e_script = TRUE;
6224f72b
GS
1881 }
1882 if (*++s)
3280af22 1883 sv_catpv(PL_e_script, s);
6224f72b 1884 else if (argv[1]) {
3280af22 1885 sv_catpv(PL_e_script, argv[1]);
6224f72b
GS
1886 argc--,argv++;
1887 }
1888 else
47f56822 1889 Perl_croak(aTHX_ "No code specified for -%c", c);
396482e1 1890 sv_catpvs(PL_e_script, "\n");
6224f72b 1891 break;
afe37c7d 1892
20ef40cf 1893 case 'f':
f5542d3a 1894#ifdef USE_SITECUSTOMIZE
20ef40cf 1895 minus_f = TRUE;
f5542d3a 1896#endif
20ef40cf
GA
1897 s++;
1898 goto reswitch;
1899
6224f72b 1900 case 'I': /* -I handled both here and in moreswitches() */
f20b2998 1901 forbid_setid('I', FALSE);
bd61b366 1902 if (!*++s && (s=argv[1]) != NULL) {
6224f72b
GS
1903 argc--,argv++;
1904 }
6224f72b 1905 if (s && *s) {
0df16ed7 1906 STRLEN len = strlen(s);
55b4bc1c 1907 incpush(s, len, INCPUSH_ADD_SUB_DIRS|INCPUSH_ADD_OLD_VERS);
0df16ed7
GS
1908 }
1909 else
a67e862a 1910 Perl_croak(aTHX_ "No directory specified for -I");
6224f72b 1911 break;
6224f72b 1912 case 'S':
f20b2998 1913 forbid_setid('S', FALSE);
6224f72b
GS
1914 dosearch = TRUE;
1915 s++;
1916 goto reswitch;
1917 case 'V':
7edfd0ef
NC
1918 {
1919 SV *opts_prog;
1920
7edfd0ef 1921 if (*++s != ':') {
37ca4a5b 1922 opts_prog = newSVpvs("use Config; Config::_V()");
7edfd0ef
NC
1923 }
1924 else {
1925 ++s;
1926 opts_prog = Perl_newSVpvf(aTHX_
37ca4a5b 1927 "use Config; Config::config_vars(qw%c%s%c)",
7edfd0ef
NC
1928 0, s, 0);
1929 s += strlen(s);
1930 }
37ca4a5b 1931 Perl_av_create_and_push(aTHX_ &PL_preambleav, opts_prog);
7edfd0ef
NC
1932 /* don't look for script or read stdin */
1933 scriptname = BIT_BUCKET;
1934 goto reswitch;
6224f72b 1935 }
6224f72b 1936 case 'x':
737c24fc 1937 doextract = TRUE;
6224f72b 1938 s++;
304334da 1939 if (*s)
f4c556ac 1940 cddir = s;
6224f72b
GS
1941 break;
1942 case 0:
1943 break;
1944 case '-':
1945 if (!*++s || isSPACE(*s)) {
1946 argc--,argv++;
1947 goto switch_end;
1948 }
ee8bc8b7
NC
1949 /* catch use of gnu style long options.
1950 Both of these exit immediately. */
1951 if (strEQ(s, "version"))
1952 minus_v();
1953 if (strEQ(s, "help"))
1954 usage();
6224f72b
GS
1955 s--;
1956 /* FALL THROUGH */
1957 default:
cea2e8a9 1958 Perl_croak(aTHX_ "Unrecognized switch: -%s (-h will show valid options)",s);
8d063cd8
LW
1959 }
1960 }
c7030b81
NC
1961 }
1962
6224f72b 1963 switch_end:
54310121 1964
c7030b81
NC
1965 {
1966 char *s;
1967
f675dbe5
CB
1968 if (
1969#ifndef SECURE_INTERNAL_GETENV
284167a5 1970 !TAINTING_get &&
f675dbe5 1971#endif
cf756827 1972 (s = PerlEnv_getenv("PERL5OPT")))
0df16ed7 1973 {
74288ac8
GS
1974 while (isSPACE(*s))
1975 s++;
317ea90d 1976 if (*s == '-' && *(s+1) == 'T') {
284167a5
SM
1977#if SILENT_NO_TAINT_SUPPORT
1978 /* silently ignore */
1979#elif NO_TAINT_SUPPORT
1980 Perl_croak("This perl was compiled without taint support. "
1981 "Cowardly refusing to run with -t or -T flags");
1982#else
22f7c9c9 1983 CHECK_MALLOC_TOO_LATE_FOR('T');
284167a5
SM
1984 TAINTING_set(TRUE);
1985 TAINT_WARN_set(FALSE);
1986#endif
317ea90d 1987 }
74288ac8 1988 else {
bd61b366 1989 char *popt_copy = NULL;
74288ac8 1990 while (s && *s) {
54913509 1991 const char *d;
74288ac8
GS
1992 while (isSPACE(*s))
1993 s++;
1994 if (*s == '-') {
1995 s++;
1996 if (isSPACE(*s))
1997 continue;
1998 }
4ea8f8fb 1999 d = s;
74288ac8
GS
2000 if (!*s)
2001 break;
2b622f1a 2002 if (!strchr("CDIMUdmtwW", *s))
cea2e8a9 2003 Perl_croak(aTHX_ "Illegal switch in PERL5OPT: -%c", *s);
4ea8f8fb
MS
2004 while (++s && *s) {
2005 if (isSPACE(*s)) {
cf756827 2006 if (!popt_copy) {
bfa6c418
NC
2007 popt_copy = SvPVX(sv_2mortal(newSVpv(d,0)));
2008 s = popt_copy + (s - d);
2009 d = popt_copy;
cf756827 2010 }
4ea8f8fb
MS
2011 *s++ = '\0';
2012 break;
2013 }
2014 }
1c4db469 2015 if (*d == 't') {
284167a5
SM
2016#if SILENT_NO_TAINT_SUPPORT
2017 /* silently ignore */
2018#elif NO_TAINT_SUPPORT
2019 Perl_croak("This perl was compiled without taint support. "
2020 "Cowardly refusing to run with -t or -T flags");
2021#else
2022 if( !TAINTING_get) {
2023 TAINT_WARN_set(TRUE);
2024 TAINTING_set(TRUE);
317ea90d 2025 }
284167a5 2026#endif
1c4db469 2027 } else {
97bd5664 2028 moreswitches(d);
1c4db469 2029 }
6224f72b 2030 }
6224f72b
GS
2031 }
2032 }
c7030b81 2033 }
a0d0e21e 2034
c29067d7
CH
2035 /* Set $^X early so that it can be used for relocatable paths in @INC */
2036 /* and for SITELIB_EXP in USE_SITECUSTOMIZE */
284167a5 2037 assert (!TAINT_get);
c29067d7
CH
2038 TAINT;
2039 S_set_caret_X(aTHX);
2040 TAINT_NOT;
2041
43c0c913 2042#if defined(USE_SITECUSTOMIZE)
20ef40cf 2043 if (!minus_f) {
43c0c913 2044 /* The games with local $! are to avoid setting errno if there is no
fc81b718
NC
2045 sitecustomize script. "q%c...%c", 0, ..., 0 becomes "q\0...\0",
2046 ie a q() operator with a NUL byte as a the delimiter. This avoids
2047 problems with pathnames containing (say) ' */
43c0c913
NC
2048# ifdef PERL_IS_MINIPERL
2049 AV *const inc = GvAV(PL_incgv);
2050 SV **const inc0 = inc ? av_fetch(inc, 0, FALSE) : NULL;
2051
2052 if (inc0) {
2053 (void)Perl_av_create_and_unshift_one(aTHX_ &PL_preambleav,
2054 Perl_newSVpvf(aTHX_
fc81b718
NC
2055 "BEGIN { do {local $!; -f q%c%"SVf"/buildcustomize.pl%c} && do q%c%"SVf"/buildcustomize.pl%c }",
2056 0, *inc0, 0,
2057 0, *inc0, 0));
43c0c913
NC
2058 }
2059# else
2060 /* SITELIB_EXP is a function call on Win32. */
c29067d7 2061 const char *const raw_sitelib = SITELIB_EXP;
bac5c4fc
JD
2062 if (raw_sitelib) {
2063 /* process .../.. if PERL_RELOCATABLE_INC is defined */
2064 SV *sitelib_sv = mayberelocate(raw_sitelib, strlen(raw_sitelib),
2065 INCPUSH_CAN_RELOCATE);
2066 const char *const sitelib = SvPVX(sitelib_sv);
2067 (void)Perl_av_create_and_unshift_one(aTHX_ &PL_preambleav,
2068 Perl_newSVpvf(aTHX_
2069 "BEGIN { do {local $!; -f q%c%s/sitecustomize.pl%c} && do q%c%s/sitecustomize.pl%c }",
2070 0, sitelib, 0,
2071 0, sitelib, 0));
2072 assert (SvREFCNT(sitelib_sv) == 1);
2073 SvREFCNT_dec(sitelib_sv);
2074 }
43c0c913 2075# endif
20ef40cf
GA
2076 }
2077#endif
2078
6224f72b
GS
2079 if (!scriptname)
2080 scriptname = argv[0];
3280af22 2081 if (PL_e_script) {
6224f72b
GS
2082 argc++,argv--;
2083 scriptname = BIT_BUCKET; /* don't look for script or read stdin */
2084 }
bd61b366 2085 else if (scriptname == NULL) {
6224f72b
GS
2086#ifdef MSDOS
2087 if ( PerlLIO_isatty(PerlIO_fileno(PerlIO_stdin())) )
97bd5664 2088 moreswitches("h");
6224f72b
GS
2089#endif
2090 scriptname = "-";
2091 }
2092
284167a5 2093 assert (!TAINT_get);
2cace6ac 2094 init_perllib();
6224f72b 2095
a52eba0e 2096 {
f20b2998 2097 bool suidscript = FALSE;
829372d3 2098
8d113837 2099 rsfp = open_script(scriptname, dosearch, &suidscript);
c0b3891a
NC
2100 if (!rsfp) {
2101 rsfp = PerlIO_stdin();
87606032 2102 lex_start_flags = LEX_DONT_CLOSE_RSFP;
c0b3891a 2103 }
6224f72b 2104
b24bc095 2105 validate_suid(rsfp);
6224f72b 2106
64ca3a65 2107#ifndef PERL_MICRO
a52eba0e
NC
2108# if defined(SIGCHLD) || defined(SIGCLD)
2109 {
2110# ifndef SIGCHLD
2111# define SIGCHLD SIGCLD
2112# endif
2113 Sighandler_t sigstate = rsignal_state(SIGCHLD);
2114 if (sigstate == (Sighandler_t) SIG_IGN) {
a2a5de95
NC
2115 Perl_ck_warner(aTHX_ packWARN(WARN_SIGNAL),
2116 "Can't ignore signal CHLD, forcing to default");
a52eba0e
NC
2117 (void)rsignal(SIGCHLD, (Sighandler_t)SIG_DFL);
2118 }
0b5b802d 2119 }
a52eba0e 2120# endif
64ca3a65 2121#endif
0b5b802d 2122
737c24fc 2123 if (doextract) {
faef540c 2124
f20b2998 2125 /* This will croak if suidscript is true, as -x cannot be used with
faef540c
NC
2126 setuid scripts. */
2127 forbid_setid('x', suidscript);
f20b2998 2128 /* Hence you can't get here if suidscript is true */
faef540c 2129
95670bde
NC
2130 linestr_sv = newSV_type(SVt_PV);
2131 lex_start_flags |= LEX_START_COPIED;
2f9285f8 2132 find_beginning(linestr_sv, rsfp);
a52eba0e
NC
2133 if (cddir && PerlDir_chdir( (char *)cddir ) < 0)
2134 Perl_croak(aTHX_ "Can't chdir to %s",cddir);
2135 }
f4c556ac 2136 }
6224f72b 2137
ea726b52 2138 PL_main_cv = PL_compcv = MUTABLE_CV(newSV_type(SVt_PVCV));
3280af22
NIS
2139 CvUNIQUE_on(PL_compcv);
2140
dd2155a4 2141 CvPADLIST(PL_compcv) = pad_new(0);
6224f72b 2142
dd69841b
BB
2143 PL_isarev = newHV();
2144
0c4f7ff0 2145 boot_core_PerlIO();
6224f72b 2146 boot_core_UNIVERSAL();
e1a479c5 2147 boot_core_mro();
4a5df386 2148 newXS("Internals::V", S_Internals_V, __FILE__);
6224f72b
GS
2149
2150 if (xsinit)
acfe0abc 2151 (*xsinit)(aTHX); /* in case linked C routines want magical variables */
64ca3a65 2152#ifndef PERL_MICRO
739a0b84 2153#if defined(VMS) || defined(WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(SYMBIAN)
c5be433b 2154 init_os_extras();
6224f72b 2155#endif
64ca3a65 2156#endif
6224f72b 2157
29209bc5 2158#ifdef USE_SOCKS
1b9c9cf5
DH
2159# ifdef HAS_SOCKS5_INIT
2160 socks5_init(argv[0]);
2161# else
29209bc5 2162 SOCKSinit(argv[0]);
1b9c9cf5 2163# endif
ac27b0f5 2164#endif
29209bc5 2165
6224f72b
GS
2166 init_predump_symbols();
2167 /* init_postdump_symbols not currently designed to be called */
2168 /* more than once (ENV isn't cleared first, for example) */
2169 /* But running with -u leaves %ENV & @ARGV undefined! XXX */
3280af22 2170 if (!PL_do_undump)
6224f72b
GS
2171 init_postdump_symbols(argc,argv,env);
2172
27da23d5
JH
2173 /* PL_unicode is turned on by -C, or by $ENV{PERL_UNICODE},
2174 * or explicitly in some platforms.
085a54d9 2175 * locale.c:Perl_init_i18nl10n() if the environment
a05d7ebb 2176 * look like the user wants to use UTF-8. */
a0fd4948 2177#if defined(__SYMBIAN32__)
27da23d5
JH
2178 PL_unicode = PERL_UNICODE_STD_FLAG; /* See PERL_SYMBIAN_CONSOLE_UTF8. */
2179#endif
e27b5b51 2180# ifndef PERL_IS_MINIPERL
06e66572
JH
2181 if (PL_unicode) {
2182 /* Requires init_predump_symbols(). */
a05d7ebb 2183 if (!(PL_unicode & PERL_UNICODE_LOCALE_FLAG) || PL_utf8locale) {
06e66572
JH
2184 IO* io;
2185 PerlIO* fp;
2186 SV* sv;
2187
a05d7ebb 2188 /* Turn on UTF-8-ness on STDIN, STDOUT, STDERR
06e66572 2189 * and the default open disciplines. */
a05d7ebb
JH
2190 if ((PL_unicode & PERL_UNICODE_STDIN_FLAG) &&
2191 PL_stdingv && (io = GvIO(PL_stdingv)) &&
2192 (fp = IoIFP(io)))
2193 PerlIO_binmode(aTHX_ fp, IoTYPE(io), 0, ":utf8");
2194 if ((PL_unicode & PERL_UNICODE_STDOUT_FLAG) &&
2195 PL_defoutgv && (io = GvIO(PL_defoutgv)) &&
2196 (fp = IoOFP(io)))
2197 PerlIO_binmode(aTHX_ fp, IoTYPE(io), 0, ":utf8");
2198 if ((PL_unicode & PERL_UNICODE_STDERR_FLAG) &&
2199 PL_stderrgv && (io = GvIO(PL_stderrgv)) &&
2200 (fp = IoOFP(io)))
2201 PerlIO_binmode(aTHX_ fp, IoTYPE(io), 0, ":utf8");
2202 if ((PL_unicode & PERL_UNICODE_INOUT_FLAG) &&
fafc274c
NC
2203 (sv = GvSV(gv_fetchpvs("\017PEN", GV_ADD|GV_NOTQUAL,
2204 SVt_PV)))) {
a05d7ebb
JH
2205 U32 in = PL_unicode & PERL_UNICODE_IN_FLAG;
2206 U32 out = PL_unicode & PERL_UNICODE_OUT_FLAG;
2207 if (in) {
2208 if (out)
76f68e9b 2209 sv_setpvs(sv, ":utf8\0:utf8");
a05d7ebb 2210 else
76f68e9b 2211 sv_setpvs(sv, ":utf8\0");
a05d7ebb
JH
2212 }
2213 else if (out)
76f68e9b 2214 sv_setpvs(sv, "\0:utf8");
a05d7ebb
JH
2215 SvSETMAGIC(sv);
2216 }
b310b053
JH
2217 }
2218 }
e27b5b51 2219#endif
b310b053 2220
c7030b81
NC
2221 {
2222 const char *s;
4ffa73a3
JH
2223 if ((s = PerlEnv_getenv("PERL_SIGNALS"))) {
2224 if (strEQ(s, "unsafe"))
2225 PL_signals |= PERL_SIGNALS_UNSAFE_FLAG;
2226 else if (strEQ(s, "safe"))
2227 PL_signals &= ~PERL_SIGNALS_UNSAFE_FLAG;
2228 else
2229 Perl_croak(aTHX_ "PERL_SIGNALS illegal: \"%s\"", s);
2230 }
c7030b81 2231 }
4ffa73a3 2232
81d86705 2233#ifdef PERL_MAD
c7030b81
NC
2234 {
2235 const char *s;
284167a5 2236 if (!TAINTING_get &&
2cc12391 2237 (s = PerlEnv_getenv("PERL_XMLDUMP"))) {
81d86705
NC
2238 PL_madskills = 1;
2239 PL_minus_c = 1;
2240 if (!s || !s[0])
2241 PL_xmlfp = PerlIO_stdout();
2242 else {
2243 PL_xmlfp = PerlIO_open(s, "w");
2244 if (!PL_xmlfp)
2245 Perl_croak(aTHX_ "Can't open %s", s);
2246 }
1a9a51d4 2247 my_setenv("PERL_XMLDUMP", NULL); /* hide from subprocs */
81d86705 2248 }
c7030b81
NC
2249 }
2250
2251 {
2252 const char *s;
81d86705
NC
2253 if ((s = PerlEnv_getenv("PERL_MADSKILLS"))) {
2254 PL_madskills = atoi(s);
1a9a51d4 2255 my_setenv("PERL_MADSKILLS", NULL); /* hide from subprocs */
81d86705 2256 }
c7030b81 2257 }
81d86705
NC
2258#endif
2259
87606032 2260 lex_start(linestr_sv, rsfp, lex_start_flags);
95670bde
NC
2261 if(linestr_sv)
2262 SvREFCNT_dec(linestr_sv);
2263
219f7226 2264 PL_subname = newSVpvs("main");
6224f72b 2265
5486870f
DM
2266 if (add_read_e_script)
2267 filter_add(read_e_script, NULL);
2268
6224f72b
GS
2269 /* now parse the script */
2270
93189314 2271 SETERRNO(0,SS_NORMAL);
28ac2b49 2272 if (yyparse(GRAMPROG) || PL_parser->error_count) {
3280af22 2273 if (PL_minus_c)
cea2e8a9 2274 Perl_croak(aTHX_ "%s had compilation errors.\n", PL_origfilename);
6224f72b 2275 else {
cea2e8a9 2276 Perl_croak(aTHX_ "Execution of %s aborted due to compilation errors.\n",
097ee67d 2277 PL_origfilename);
6224f72b
GS
2278 }
2279 }
57843af0 2280 CopLINE_set(PL_curcop, 0);
03d9f026 2281 SET_CURSTASH(PL_defstash);
3280af22
NIS
2282 if (PL_e_script) {
2283 SvREFCNT_dec(PL_e_script);
a0714e2c 2284 PL_e_script = NULL;
6224f72b
GS
2285 }
2286
3280af22 2287 if (PL_do_undump)
6224f72b
GS
2288 my_unexec();
2289
57843af0
GS
2290 if (isWARN_ONCE) {
2291 SAVECOPFILE(PL_curcop);
2292 SAVECOPLINE(PL_curcop);
3280af22 2293 gv_check(PL_defstash);
57843af0 2294 }
6224f72b
GS
2295
2296 LEAVE;
2297 FREETMPS;
2298
2299#ifdef MYMALLOC
f6a607bc
RGS
2300 {
2301 const char *s;
6224f72b
GS
2302 if ((s=PerlEnv_getenv("PERL_DEBUG_MSTATS")) && atoi(s) >= 2)
2303 dump_mstats("after compilation:");
f6a607bc 2304 }
6224f72b
GS
2305#endif
2306
2307 ENTER;
febb3a6d 2308 PL_restartjmpenv = NULL;
3280af22 2309 PL_restartop = 0;
312caa8e 2310 return NULL;
6224f72b
GS
2311}
2312
954c1994
GS
2313/*
2314=for apidoc perl_run
2315
2316Tells a Perl interpreter to run. See L<perlembed>.
2317
2318=cut
2319*/
2320
6224f72b 2321int
0cb96387 2322perl_run(pTHXx)
6224f72b 2323{
97aff369 2324 dVAR;
6224f72b 2325 I32 oldscope;
14dd3ad8 2326 int ret = 0;
db36c5a1 2327 dJMPENV;
6224f72b 2328
7918f24d
NC
2329 PERL_ARGS_ASSERT_PERL_RUN;
2330#ifndef MULTIPLICITY
ed6c66dd 2331 PERL_UNUSED_ARG(my_perl);
7918f24d 2332#endif
9d4ba2ae 2333
3280af22 2334 oldscope = PL_scopestack_ix;
96e176bf
CL
2335#ifdef VMS
2336 VMSISH_HUSHED = 0;
2337#endif
6224f72b 2338
14dd3ad8 2339 JMPENV_PUSH(ret);
6224f72b
GS
2340 switch (ret) {
2341 case 1:
2342 cxstack_ix = -1; /* start context stack again */
312caa8e 2343 goto redo_body;
14dd3ad8 2344 case 0: /* normal completion */
14dd3ad8
GS
2345 redo_body:
2346 run_body(oldscope);
14dd3ad8
GS
2347 /* FALL THROUGH */
2348 case 2: /* my_exit() */
3280af22 2349 while (PL_scopestack_ix > oldscope)
6224f72b
GS
2350 LEAVE;
2351 FREETMPS;
03d9f026 2352 SET_CURSTASH(PL_defstash);
3a1ee7e8 2353 if (!(PL_exit_flags & PERL_EXIT_DESTRUCT_END) &&
9ebf26ad 2354 PL_endav && !PL_minus_c) {
ca7b837b 2355 PERL_SET_PHASE(PERL_PHASE_END);
31d77e54 2356 call_list(oldscope, PL_endav);
9ebf26ad 2357 }
6224f72b
GS
2358#ifdef MYMALLOC
2359 if (PerlEnv_getenv("PERL_DEBUG_MSTATS"))
2360 dump_mstats("after execution: ");
2361#endif
37038d91 2362 ret = STATUS_EXIT;
14dd3ad8 2363 break;
6224f72b 2364 case 3:
312caa8e
CS
2365 if (PL_restartop) {
2366 POPSTACK_TO(PL_mainstack);
2367 goto redo_body;
6224f72b 2368 }
5637ef5b 2369 PerlIO_printf(Perl_error_log, "panic: restartop in perl_run\n");
312caa8e 2370 FREETMPS;
14dd3ad8
GS
2371 ret = 1;
2372 break;
6224f72b
GS
2373 }
2374
14dd3ad8
GS
2375 JMPENV_POP;
2376 return ret;
312caa8e
CS
2377}
2378
dd374669 2379STATIC void
14dd3ad8
GS
2380S_run_body(pTHX_ I32 oldscope)
2381{
97aff369 2382 dVAR;
d3b97530
DM
2383 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s $` $& $' support (0x%x).\n",
2384 PL_sawampersand ? "Enabling" : "Omitting",
2385 (unsigned int)(PL_sawampersand)));
6224f72b 2386
3280af22 2387 if (!PL_restartop) {
81d86705
NC
2388#ifdef PERL_MAD
2389 if (PL_xmlfp) {
2390 xmldump_all();
2391 exit(0); /* less likely to core dump than my_exit(0) */
2392 }
2393#endif
cf2782cd 2394#ifdef DEBUGGING
f0e3f042
CS
2395 if (DEBUG_x_TEST || DEBUG_B_TEST)
2396 dump_all_perl(!DEBUG_B_TEST);
ecae49c0
NC
2397 if (!DEBUG_q_TEST)
2398 PERL_DEBUG(PerlIO_printf(Perl_debug_log, "\nEXECUTING...\n\n"));
cf2782cd 2399#endif
6224f72b 2400
3280af22 2401 if (PL_minus_c) {
bf49b057 2402 PerlIO_printf(Perl_error_log, "%s syntax OK\n", PL_origfilename);
6224f72b
GS
2403 my_exit(0);
2404 }
3280af22 2405 if (PERLDB_SINGLE && PL_DBsingle)
ac27b0f5 2406 sv_setiv(PL_DBsingle, 1);
9ebf26ad 2407 if (PL_initav) {
ca7b837b 2408 PERL_SET_PHASE(PERL_PHASE_INIT);
3280af22 2409 call_list(oldscope, PL_initav);
9ebf26ad 2410 }
f1fac472 2411#ifdef PERL_DEBUG_READONLY_OPS
3107b51f
FC
2412 if (PL_main_root && PL_main_root->op_slabbed)
2413 Slab_to_ro(OpSLAB(PL_main_root));
f1fac472 2414#endif
6224f72b
GS
2415 }
2416
2417 /* do it */
2418
ca7b837b 2419 PERL_SET_PHASE(PERL_PHASE_RUN);
9ebf26ad 2420
3280af22 2421 if (PL_restartop) {
febb3a6d 2422 PL_restartjmpenv = NULL;
533c011a 2423 PL_op = PL_restartop;
3280af22 2424 PL_restartop = 0;
cea2e8a9 2425 CALLRUNOPS(aTHX);
6224f72b 2426 }
3280af22
NIS
2427 else if (PL_main_start) {
2428 CvDEPTH(PL_main_cv) = 1;
533c011a 2429 PL_op = PL_main_start;
cea2e8a9 2430 CALLRUNOPS(aTHX);
6224f72b 2431 }
f6b3007c 2432 my_exit(0);
118e2215 2433 assert(0); /* NOTREACHED */
6224f72b
GS
2434}
2435
954c1994 2436/*
ccfc67b7
JH
2437=head1 SV Manipulation Functions
2438
954c1994
GS
2439=for apidoc p||get_sv
2440
64ace3f8
NC
2441Returns the SV of the specified Perl scalar. C<flags> are passed to
2442C<gv_fetchpv>. If C<GV_ADD> is set and the
2443Perl variable does not exist then it will be created. If C<flags> is zero
2444and the variable does not exist then NULL is returned.
954c1994
GS
2445
2446=cut
2447*/
2448
6224f72b 2449SV*
64ace3f8 2450Perl_get_sv(pTHX_ const char *name, I32 flags)
6224f72b
GS
2451{
2452 GV *gv;
7918f24d
NC
2453
2454 PERL_ARGS_ASSERT_GET_SV;
2455
64ace3f8 2456 gv = gv_fetchpv(name, flags, SVt_PV);
6224f72b
GS
2457 if (gv)
2458 return GvSV(gv);
a0714e2c 2459 return NULL;
6224f72b
GS
2460}
2461
954c1994 2462/*
ccfc67b7
JH
2463=head1 Array Manipulation Functions
2464
954c1994
GS
2465=for apidoc p||get_av
2466
f0b90de1
SF
2467Returns the AV of the specified Perl global or package array with the given
2468name (so it won't work on lexical variables). C<flags> are passed
2469to C<gv_fetchpv>. If C<GV_ADD> is set and the
cbfd0a87
NC
2470Perl variable does not exist then it will be created. If C<flags> is zero
2471and the variable does not exist then NULL is returned.
954c1994 2472
f0b90de1
SF
2473Perl equivalent: C<@{"$name"}>.
2474
954c1994
GS
2475=cut
2476*/
2477
6224f72b 2478AV*
cbfd0a87 2479Perl_get_av(pTHX_ const char *name, I32 flags)
6224f72b 2480{
cbfd0a87 2481 GV* const gv = gv_fetchpv(name, flags, SVt_PVAV);
7918f24d
NC
2482
2483 PERL_ARGS_ASSERT_GET_AV;
2484
cbfd0a87 2485 if (flags)
6224f72b
GS
2486 return GvAVn(gv);
2487 if (gv)
2488 return GvAV(gv);
7d49f689 2489 return NULL;
6224f72b
GS
2490}
2491
954c1994 2492/*
ccfc67b7
JH
2493=head1 Hash Manipulation Functions
2494
954c1994
GS
2495=for apidoc p||get_hv
2496
6673a63c
NC
2497Returns the HV of the specified Perl hash. C<flags> are passed to
2498C<gv_fetchpv>. If C<GV_ADD> is set and the
2499Perl variable does not exist then it will be created. If C<flags> is zero
2500and the variable does not exist then NULL is returned.
954c1994
GS
2501
2502=cut
2503*/
2504
6224f72b 2505HV*
6673a63c 2506Perl_get_hv(pTHX_ const char *name, I32 flags)
6224f72b 2507{
6673a63c 2508 GV* const gv = gv_fetchpv(name, flags, SVt_PVHV);
7918f24d
NC
2509
2510 PERL_ARGS_ASSERT_GET_HV;
2511
6673a63c 2512 if (flags)
a0d0e21e
LW
2513 return GvHVn(gv);
2514 if (gv)
2515 return GvHV(gv);
5c284bb0 2516 return NULL;
a0d0e21e
LW
2517}
2518
954c1994 2519/*
ccfc67b7
JH
2520=head1 CV Manipulation Functions
2521
780a5241
NC
2522=for apidoc p||get_cvn_flags
2523
2524Returns the CV of the specified Perl subroutine. C<flags> are passed to
2525C<gv_fetchpvn_flags>. If C<GV_ADD> is set and the Perl subroutine does not
2526exist then it will be declared (which has the same effect as saying
2527C<sub name;>). If C<GV_ADD> is not set and the subroutine does not exist
2528then NULL is returned.
2529
954c1994
GS
2530=for apidoc p||get_cv
2531
780a5241 2532Uses C<strlen> to get the length of C<name>, then calls C<get_cvn_flags>.
954c1994
GS
2533
2534=cut
2535*/
2536
a0d0e21e 2537CV*
780a5241 2538Perl_get_cvn_flags(pTHX_ const char *name, STRLEN len, I32 flags)
a0d0e21e 2539{
780a5241 2540 GV* const gv = gv_fetchpvn_flags(name, len, flags, SVt_PVCV);
7918f24d
NC
2541
2542 PERL_ARGS_ASSERT_GET_CVN_FLAGS;
2543
334dda80
FC
2544 /* XXX this is probably not what they think they're getting.
2545 * It has the same effect as "sub name;", i.e. just a forward
2546 * declaration! */
780a5241 2547 if ((flags & ~GV_NOADD_MASK) && !GvCVu(gv)) {
186a5ba8 2548 return newSTUB(gv,0);
780a5241 2549 }
a0d0e21e 2550 if (gv)
8ebc5c01 2551 return GvCVu(gv);
601f1833 2552 return NULL;
a0d0e21e
LW
2553}
2554
2c67934f
NC
2555/* Nothing in core calls this now, but we can't replace it with a macro and
2556 move it to mathoms.c as a macro would evaluate name twice. */
780a5241
NC
2557CV*
2558Perl_get_cv(pTHX_ const char *name, I32 flags)
2559{
7918f24d
NC
2560 PERL_ARGS_ASSERT_GET_CV;
2561
780a5241
NC
2562 return get_cvn_flags(name, strlen(name), flags);
2563}
2564
79072805
LW
2565/* Be sure to refetch the stack pointer after calling these routines. */
2566
954c1994 2567/*
ccfc67b7
JH
2568
2569=head1 Callback Functions
2570
954c1994
GS
2571=for apidoc p||call_argv
2572
f0b90de1
SF
2573Performs a callback to the specified named and package-scoped Perl subroutine
2574with C<argv> (a NULL-terminated array of strings) as arguments. See L<perlcall>.
2575
2576Approximate Perl equivalent: C<&{"$sub_name"}(@$argv)>.
954c1994
GS
2577
2578=cut
2579*/
2580
a0d0e21e 2581I32
5aaab254 2582Perl_call_argv(pTHX_ const char *sub_name, I32 flags, char **argv)
ac27b0f5 2583
8ac85365
NIS
2584 /* See G_* flags in cop.h */
2585 /* null terminated arg list */
8990e307 2586{
97aff369 2587 dVAR;
a0d0e21e 2588 dSP;
8990e307 2589
7918f24d
NC
2590 PERL_ARGS_ASSERT_CALL_ARGV;
2591
924508f0 2592 PUSHMARK(SP);
a0d0e21e 2593 if (argv) {
8990e307 2594 while (*argv) {
6e449a3a 2595 mXPUSHs(newSVpv(*argv,0));
8990e307
LW
2596 argv++;
2597 }
a0d0e21e 2598 PUTBACK;
8990e307 2599 }
864dbfa3 2600 return call_pv(sub_name, flags);
8990e307
LW
2601}
2602
954c1994
GS
2603/*
2604=for apidoc p||call_pv
2605
2606Performs a callback to the specified Perl sub. See L<perlcall>.
2607
2608=cut
2609*/
2610
a0d0e21e 2611I32
864dbfa3 2612Perl_call_pv(pTHX_ const char *sub_name, I32 flags)
8ac85365
NIS
2613 /* name of the subroutine */
2614 /* See G_* flags in cop.h */
a0d0e21e 2615{
7918f24d
NC
2616 PERL_ARGS_ASSERT_CALL_PV;
2617
0da0e728 2618 return call_sv(MUTABLE_SV(get_cv(sub_name, GV_ADD)), flags);
a0d0e21e
LW
2619}
2620
954c1994
GS
2621/*
2622=for apidoc p||call_method
2623
2624Performs a callback to the specified Perl method. The blessed object must
2625be on the stack. See L<perlcall>.
2626
2627=cut
2628*/
2629
a0d0e21e 2630I32
864dbfa3 2631Perl_call_method(pTHX_ const char *methname, I32 flags)
8ac85365
NIS
2632 /* name of the subroutine */
2633 /* See G_* flags in cop.h */
a0d0e21e 2634{
46ca9bac 2635 STRLEN len;
7918f24d
NC
2636 PERL_ARGS_ASSERT_CALL_METHOD;
2637
46ca9bac 2638 len = strlen(methname);
2639
2640 /* XXX: sv_2mortal(newSVpvn_share(methname, len)) can be faster */
2641 return call_sv(newSVpvn_flags(methname, len, SVs_TEMP), flags | G_METHOD);
a0d0e21e
LW
2642}
2643
2644/* May be called with any of a CV, a GV, or an SV containing the name. */
954c1994
GS
2645/*
2646=for apidoc p||call_sv
2647
2648Performs a callback to the Perl sub whose name is in the SV. See
2649L<perlcall>.
2650
2651=cut
2652*/
2653
a0d0e21e 2654I32
001d637e 2655Perl_call_sv(pTHX_ SV *sv, VOL I32 flags)
8ac85365 2656 /* See G_* flags in cop.h */
a0d0e21e 2657{
27da23d5 2658 dVAR; dSP;
a0d0e21e 2659 LOGOP myop; /* fake syntax tree node */
968b3946 2660 UNOP method_op;
aa689395 2661 I32 oldmark;
8ea43dc8 2662 VOL I32 retval = 0;
a0d0e21e 2663 I32 oldscope;
54310121 2664 bool oldcatch = CATCH_GET;
6224f72b 2665 int ret;
c4420975 2666 OP* const oldop = PL_op;
db36c5a1 2667 dJMPENV;
1e422769 2668
7918f24d
NC
2669 PERL_ARGS_ASSERT_CALL_SV;
2670
a0d0e21e
LW
2671 if (flags & G_DISCARD) {
2672 ENTER;
2673 SAVETMPS;
2674 }
2f8edad0
NC
2675 if (!(flags & G_WANT)) {
2676 /* Backwards compatibility - as G_SCALAR was 0, it could be omitted.
2677 */
2678 flags |= G_SCALAR;
2679 }
a0d0e21e 2680
aa689395 2681 Zero(&myop, 1, LOGOP);
f51d4af5 2682 if (!(flags & G_NOARGS))
aa689395 2683 myop.op_flags |= OPf_STACKED;
4f911530 2684 myop.op_flags |= OP_GIMME_REVERSE(flags);
462e5cf6 2685 SAVEOP();
533c011a 2686 PL_op = (OP*)&myop;
aa689395 2687
3280af22
NIS
2688 EXTEND(PL_stack_sp, 1);
2689 *++PL_stack_sp = sv;
aa689395 2690 oldmark = TOPMARK;
3280af22 2691 oldscope = PL_scopestack_ix;
a0d0e21e 2692
3280af22 2693 if (PERLDB_SUB && PL_curstash != PL_debstash
36477c24 2694 /* Handle first BEGIN of -d. */
3280af22 2695 && (PL_DBcv || (PL_DBcv = GvCV(PL_DBsub)))
36477c24
PP
2696 /* Try harder, since this may have been a sighandler, thus
2697 * curstash may be meaningless. */
ea726b52 2698 && (SvTYPE(sv) != SVt_PVCV || CvSTASH((const CV *)sv) != PL_debstash)
491527d0 2699 && !(flags & G_NODEBUG))
5ff48db8 2700 myop.op_private |= OPpENTERSUB_DB;
a0d0e21e 2701
968b3946
GS
2702 if (flags & G_METHOD) {
2703 Zero(&method_op, 1, UNOP);
5ff48db8 2704 method_op.op_next = (OP*)&myop;
968b3946 2705 method_op.op_ppaddr = PL_ppaddr[OP_METHOD];
377b1098 2706 method_op.op_type = OP_METHOD;
968b3946 2707 myop.op_ppaddr = PL_ppaddr[OP_ENTERSUB];
377b1098 2708 myop.op_type = OP_ENTERSUB;
f39d0b86 2709 PL_op = (OP*)&method_op;
968b3946
GS
2710 }
2711
312caa8e 2712 if (!(flags & G_EVAL)) {
0cdb2077 2713 CATCH_SET(TRUE);
d6f07c05 2714 CALL_BODY_SUB((OP*)&myop);
312caa8e 2715 retval = PL_stack_sp - (PL_stack_base + oldmark);
0253cb41 2716 CATCH_SET(oldcatch);
312caa8e
CS
2717 }
2718 else {
d78bda3d 2719 myop.op_other = (OP*)&myop;
3280af22 2720 PL_markstack_ptr--;
edb2152a 2721 create_eval_scope(flags|G_FAKINGEVAL);
3280af22 2722 PL_markstack_ptr++;
a0d0e21e 2723
14dd3ad8 2724 JMPENV_PUSH(ret);
edb2152a 2725
6224f72b
GS
2726 switch (ret) {
2727 case 0:
14dd3ad8 2728 redo_body:
d6f07c05 2729 CALL_BODY_SUB((OP*)&myop);
312caa8e 2730 retval = PL_stack_sp - (PL_stack_base + oldmark);
8433848b 2731 if (!(flags & G_KEEPERR)) {
ab69dbc2 2732 CLEAR_ERRSV();
8433848b 2733 }
a0d0e21e 2734 break;
6224f72b 2735 case 1:
f86702cc 2736 STATUS_ALL_FAILURE;
a0d0e21e 2737 /* FALL THROUGH */
6224f72b 2738 case 2:
a0d0e21e 2739 /* my_exit() was called */
03d9f026 2740 SET_CURSTASH(PL_defstash);
a0d0e21e 2741 FREETMPS;
14dd3ad8 2742 JMPENV_POP;
f86702cc 2743 my_exit_jump();
118e2215 2744 assert(0); /* NOTREACHED */
6224f72b 2745 case 3:
3280af22 2746 if (PL_restartop) {
febb3a6d 2747 PL_restartjmpenv = NULL;
533c011a 2748 PL_op = PL_restartop;
3280af22 2749 PL_restartop = 0;
312caa8e 2750 goto redo_body;
a0d0e21e 2751 }
3280af22 2752 PL_stack_sp = PL_stack_base + oldmark;
51ce5529 2753 if ((flags & G_WANT) == G_ARRAY)
a0d0e21e
LW
2754 retval = 0;
2755 else {
2756 retval = 1;
3280af22 2757 *++PL_stack_sp = &PL_sv_undef;
a0d0e21e 2758 }
312caa8e 2759 break;
a0d0e21e 2760 }
a0d0e21e 2761
edb2152a
NC
2762 if (PL_scopestack_ix > oldscope)
2763 delete_eval_scope();
14dd3ad8 2764 JMPENV_POP;
a0d0e21e 2765 }
1e422769 2766
a0d0e21e 2767 if (flags & G_DISCARD) {
3280af22 2768 PL_stack_sp = PL_stack_base + oldmark;
a0d0e21e
LW
2769 retval = 0;
2770 FREETMPS;
2771 LEAVE;
2772 }
533c011a 2773 PL_op = oldop;
a0d0e21e
LW
2774 return retval;
2775}
2776
6e72f9df 2777/* Eval a string. The G_EVAL flag is always assumed. */
8990e307 2778
954c1994
GS
2779/*
2780=for apidoc p||eval_sv
2781
be064c4a
DM
2782Tells Perl to C<eval> the string in the SV. It supports the same flags
2783as C<call_sv>, with the obvious exception of G_EVAL. See L<perlcall>.
954c1994
GS
2784
2785=cut
2786*/
2787
a0d0e21e 2788I32
864dbfa3 2789Perl_eval_sv(pTHX_ SV *sv, I32 flags)
ac27b0f5 2790
8ac85365 2791 /* See G_* flags in cop.h */
a0d0e21e 2792{
97aff369 2793 dVAR;
924508f0 2794 dSP;
a0d0e21e 2795 UNOP myop; /* fake syntax tree node */
8ea43dc8
SP
2796 VOL I32 oldmark = SP - PL_stack_base;
2797 VOL I32 retval = 0;
6224f72b 2798 int ret;
c4420975 2799 OP* const oldop = PL_op;
db36c5a1 2800 dJMPENV;
84902520 2801
7918f24d
NC
2802 PERL_ARGS_ASSERT_EVAL_SV;
2803
4633a7c4
LW
2804 if (flags & G_DISCARD) {
2805 ENTER;
2806 SAVETMPS;
2807 }
2808
462e5cf6 2809 SAVEOP();
533c011a 2810 PL_op = (OP*)&myop;
5ff48db8 2811 Zero(&myop, 1, UNOP);
3280af22
NIS
2812 EXTEND(PL_stack_sp, 1);
2813 *++PL_stack_sp = sv;
79072805 2814
4633a7c4
LW
2815 if (!(flags & G_NOARGS))
2816 myop.op_flags = OPf_STACKED;
6e72f9df 2817 myop.op_type = OP_ENTEREVAL;
4f911530 2818 myop.op_flags |= OP_GIMME_REVERSE(flags);
6e72f9df
PP
2819 if (flags & G_KEEPERR)
2820 myop.op_flags |= OPf_SPECIAL;
d24ca0c5
DM
2821 if (PL_reg_state.re_reparsing)
2822 myop.op_private = OPpEVAL_COPHH;
4633a7c4 2823
dedbcade
DM
2824 /* fail now; otherwise we could fail after the JMPENV_PUSH but
2825 * before a PUSHEVAL, which corrupts the stack after a croak */
2826 TAINT_PROPER("eval_sv()");
2827
14dd3ad8 2828 JMPENV_PUSH(ret);
6224f72b
GS
2829 switch (ret) {
2830 case 0:
14dd3ad8 2831 redo_body:
2ba65d5f
DM
2832 if (PL_op == (OP*)(&myop)) {
2833 PL_op = PL_ppaddr[OP_ENTEREVAL](aTHX);
2834 if (!PL_op)
2835 goto fail; /* failed in compilation */
2836 }
4aca2f62 2837 CALLRUNOPS(aTHX);
312caa8e 2838 retval = PL_stack_sp - (PL_stack_base + oldmark);
8433848b 2839 if (!(flags & G_KEEPERR)) {
ab69dbc2 2840 CLEAR_ERRSV();
8433848b 2841 }
4633a7c4 2842 break;
6224f72b 2843 case 1:
f86702cc 2844 STATUS_ALL_FAILURE;
4633a7c4 2845 /* FALL THROUGH */
6224f72b 2846 case 2:
4633a7c4 2847 /* my_exit() was called */
03d9f026 2848 SET_CURSTASH(PL_defstash);
4633a7c4 2849 FREETMPS;
14dd3ad8 2850 JMPENV_POP;
f86702cc 2851 my_exit_jump();
118e2215 2852 assert(0); /* NOTREACHED */
6224f72b 2853 case 3:
3280af22 2854 if (PL_restartop) {
febb3a6d 2855 PL_restartjmpenv = NULL;
533c011a 2856 PL_op = PL_restartop;
3280af22 2857 PL_restartop = 0;
312caa8e 2858 goto redo_body;
4633a7c4 2859 }
4aca2f62 2860 fail:
3280af22 2861 PL_stack_sp = PL_stack_base + oldmark;
51ce5529 2862 if ((flags & G_WANT) == G_ARRAY)
4633a7c4
LW
2863 retval = 0;
2864 else {
2865 retval = 1;
3280af22 2866 *++PL_stack_sp = &PL_sv_undef;
4633a7c4 2867 }
312caa8e 2868 break;
4633a7c4
LW
2869 }
2870
14dd3ad8 2871 JMPENV_POP;
4633a7c4 2872 if (flags & G_DISCARD) {
3280af22 2873 PL_stack_sp = PL_stack_base + oldmark;
4633a7c4
LW
2874 retval = 0;
2875 FREETMPS;
2876 LEAVE;
2877 }
533c011a 2878 PL_op = oldop;
4633a7c4
LW
2879 return retval;
2880}
2881
954c1994
GS
2882/*
2883=for apidoc p||eval_pv
2884
2885Tells Perl to C<eval> the given string and return an SV* result.
2886
2887=cut
2888*/
2889
137443ea 2890SV*
864dbfa3 2891Perl_eval_pv(pTHX_ const char *p, I32 croak_on_error)
137443ea 2892{
97aff369 2893 dVAR;
137443ea
PP
2894 SV* sv = newSVpv(p, 0);
2895
7918f24d
NC
2896 PERL_ARGS_ASSERT_EVAL_PV;
2897
864dbfa3 2898 eval_sv(sv, G_SCALAR);
137443ea
PP
2899 SvREFCNT_dec(sv);
2900
ed1786ad
DD
2901 {
2902 dSP;
2903 sv = POPs;
2904 PUTBACK;
2905 }
137443ea 2906
eed484f9
DD
2907 /* just check empty string or undef? */
2908 if (croak_on_error) {
2909 SV * const errsv = ERRSV;
2910 if(SvTRUE_NN(errsv))
2911 /* replace with croak_sv? */
2912 Perl_croak_nocontext("%s", SvPV_nolen_const(errsv));
2d8e6c8d 2913 }
137443ea
PP
2914
2915 return sv;
2916}
2917
4633a7c4
LW
2918/* Require a module. */
2919
954c1994 2920/*
ccfc67b7
JH
2921=head1 Embedding Functions
2922
954c1994
GS
2923=for apidoc p||require_pv
2924
7d3fb230
BS
2925Tells Perl to C<require> the file named by the string argument. It is
2926analogous to the Perl code C<eval "require '$file'">. It's even
2307c6d0 2927implemented that way; consider using load_module instead.
954c1994 2928
7d3fb230 2929=cut */
954c1994 2930
4633a7c4 2931void
864dbfa3 2932Perl_require_pv(pTHX_ const char *pv)
4633a7c4 2933{
97aff369 2934 dVAR;
d3acc0f7 2935 dSP;
97aff369 2936 SV* sv;
7918f24d
NC
2937
2938 PERL_ARGS_ASSERT_REQUIRE_PV;
2939
e788e7d3 2940 PUSHSTACKi(PERLSI_REQUIRE);
d3acc0f7 2941 PUTBACK;
be41e5d9
NC
2942 sv = Perl_newSVpvf(aTHX_ "require q%c%s%c", 0, pv, 0);
2943 eval_sv(sv_2mortal(sv), G_DISCARD);
d3acc0f7
JP
2944 SPAGAIN;
2945 POPSTACK;
79072805
LW
2946}
2947
76e3520e 2948STATIC void
b6f82619 2949S_usage(pTHX) /* XXX move this out into a module ? */
4633a7c4 2950{
ab821d7f 2951 /* This message really ought to be max 23 lines.
75c72d73 2952 * Removed -h because the user already knows that option. Others? */
fb73857a 2953
1566c39d
NC
2954 /* Grouped as 6 lines per C string literal, to keep under the ANSI C 89
2955 minimum of 509 character string literals. */
27da23d5 2956 static const char * const usage_msg[] = {
1566c39d
NC
2957" -0[octal] specify record separator (\\0, if no argument)\n"
2958" -a autosplit mode with -n or -p (splits $_ into @F)\n"
2959" -C[number/list] enables the listed Unicode features\n"
2960" -c check syntax only (runs BEGIN and CHECK blocks)\n"
2961" -d[:debugger] run program under debugger\n"
2962" -D[number/list] set debugging flags (argument is a bit mask or alphabets)\n",
2963" -e program one line of program (several -e's allowed, omit programfile)\n"
2964" -E program like -e, but enables all optional features\n"
2965" -f don't do $sitelib/sitecustomize.pl at startup\n"
2966" -F/pattern/ split() pattern for -a switch (//'s are optional)\n"
2967" -i[extension] edit <> files in place (makes backup if extension supplied)\n"
2968" -Idirectory specify @INC/#include directory (several -I's allowed)\n",
2969" -l[octal] enable line ending processing, specifies line terminator\n"
2970" -[mM][-]module execute \"use/no module...\" before executing program\n"
2971" -n assume \"while (<>) { ... }\" loop around program\n"
2972" -p assume loop like -n but print line also, like sed\n"
2973" -s enable rudimentary parsing for switches after programfile\n"
2974" -S look for programfile using PATH environment variable\n",
2975" -t enable tainting warnings\n"
2976" -T enable tainting checks\n"
2977" -u dump core after parsing program\n"
2978" -U allow unsafe operations\n"
2979" -v print version, patchlevel and license\n"
2980" -V[:variable] print configuration summary (or a single Config.pm variable)\n",
60eaec42 2981" -w enable many useful warnings\n"
1566c39d
NC
2982" -W enable all warnings\n"
2983" -x[directory] ignore text before #!perl line (optionally cd to directory)\n"
2984" -X disable all warnings\n"
2985" \n"
2986"Run 'perldoc perl' for more help with Perl.\n\n",
fb73857a
PP
2987NULL
2988};
27da23d5 2989 const char * const *p = usage_msg;
1566c39d 2990 PerlIO *out = PerlIO_stdout();
fb73857a 2991
1566c39d
NC
2992 PerlIO_printf(out,
2993 "\nUsage: %s [switches] [--] [programfile] [arguments]\n",
b6f82619 2994 PL_origargv[0]);
fb73857a 2995 while (*p)
1566c39d 2996 PerlIO_puts(out, *p++);
b6f82619 2997 my_exit(0);
4633a7c4
LW
2998}
2999
b4ab917c
DM
3000/* convert a string of -D options (or digits) into an int.
3001 * sets *s to point to the char after the options */
3002
3003#ifdef DEBUGGING
3004int
e1ec3a88 3005Perl_get_debug_opts(pTHX_ const char **s, bool givehelp)
b4ab917c 3006{
27da23d5 3007 static const char * const usage_msgd[] = {
651b8f1a
NC
3008 " Debugging flag values: (see also -d)\n"
3009 " p Tokenizing and parsing (with v, displays parse stack)\n"
3010 " s Stack snapshots (with v, displays all stacks)\n"
3011 " l Context (loop) stack processing\n"
3012 " t Trace execution\n"
3013 " o Method and overloading resolution\n",
3014 " c String/numeric conversions\n"
3015 " P Print profiling info, source file input state\n"
3016 " m Memory and SV allocation\n"
3017 " f Format processing\n"
3018 " r Regular expression parsing and execution\n"
3019 " x Syntax tree dump\n",
3020 " u Tainting checks\n"
3021 " H Hash dump -- usurps values()\n"
3022 " X Scratchpad allocation\n"
3023 " D Cleaning up\n"
56967202 3024 " S Op slab allocation\n"
651b8f1a
NC
3025 " T Tokenising\n"
3026 " R Include reference counts of dumped variables (eg when using -Ds)\n",
3027 " J Do not s,t,P-debug (Jump over) opcodes within package DB\n"
3028 " v Verbose: use in conjunction with other flags\n"
3029 " C Copy On Write\n"
3030 " A Consistency checks on internal structures\n"
3031 " q quiet - currently only suppresses the 'EXECUTING' message\n"
3032 " M trace smart match resolution\n"
3033 " B dump suBroutine definitions, including special Blocks like BEGIN\n",
e6e64d9b
JC
3034 NULL
3035 };
b4ab917c 3036 int i = 0;
7918f24d
NC
3037
3038 PERL_ARGS_ASSERT_GET_DEBUG_OPTS;
3039
b4ab917c
DM
3040 if (isALPHA(**s)) {
3041 /* if adding extra options, remember to update DEBUG_MASK */
cc8773c0 3042 static const char debopts[] = "psltocPmfrxuUHXDSTRJvCAqMB";
b4ab917c
DM
3043
3044 for (; isALNUM(**s); (*s)++) {
c4420975 3045 const char * const d = strchr(debopts,**s);
b4ab917c
DM
3046 if (d)
3047 i |= 1 << (d - debopts);
3048 else if (ckWARN_d(WARN_DEBUGGING))
e6e64d9b
JC
3049 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING),
3050 "invalid option -D%c, use -D'' to see choices\n", **s);
b4ab917c
DM
3051 }
3052 }
e6e64d9b 3053 else if (isDIGIT(**s)) {
b4ab917c
DM
3054 i = atoi(*s);
3055 for (; isALNUM(**s); (*s)++) ;
3056 }
ddcf8bc1 3057 else if (givehelp) {
06e869a4 3058 const char *const *p = usage_msgd;
651b8f1a 3059 while (*p) PerlIO_puts(PerlIO_stdout(), *p++);
e6e64d9b 3060 }
b4ab917c
DM
3061# ifdef EBCDIC
3062 if ((i & DEBUG_p_FLAG) && ckWARN_d(WARN_DEBUGGING))
3063 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING),
3064 "-Dp not implemented on this platform\n");
3065# endif
3066 return i;
3067}
3068#endif
3069
79072805
LW
3070/* This routine handles any switches that can be given during run */
3071
c7030b81
NC
3072const char *
3073Perl_moreswitches(pTHX_ const char *s)
79072805 3074{
27da23d5 3075 dVAR;
84c133a0 3076 UV rschar;
0544e6df 3077 const char option = *s; /* used to remember option in -m/-M code */
79072805 3078
7918f24d
NC
3079 PERL_ARGS_ASSERT_MORESWITCHES;
3080
79072805
LW
3081 switch (*s) {
3082 case '0':
a863c7d1 3083 {
f2095865 3084 I32 flags = 0;
a3b680e6 3085 STRLEN numlen;
f2095865
JH
3086
3087 SvREFCNT_dec(PL_rs);
3088 if (s[1] == 'x' && s[2]) {
a3b680e6 3089 const char *e = s+=2;
f2095865
JH
3090 U8 *tmps;
3091
a3b680e6
AL
3092 while (*e)
3093 e++;
f2095865
JH
3094 numlen = e - s;
3095 flags = PERL_SCAN_SILENT_ILLDIGIT;
3096 rschar = (U32)grok_hex(s, &numlen, &flags, NULL);
3097 if (s + numlen < e) {
3098 rschar = 0; /* Grandfather -0xFOO as -0 -xFOO. */
3099 numlen = 0;
3100 s--;
3101 }
396482e1 3102 PL_rs = newSVpvs("");
c5661c80 3103 SvGROW(PL_rs, (STRLEN)(UNISKIP(rschar) + 1));
f2095865
JH
3104 tmps = (U8*)SvPVX(PL_rs);
3105 uvchr_to_utf8(tmps, rschar);
3106 SvCUR_set(PL_rs, UNISKIP(rschar));
3107 SvUTF8_on(PL_rs);
3108 }
3109 else {
3110 numlen = 4;
3111 rschar = (U32)grok_oct(s, &numlen, &flags, NULL);
3112 if (rschar & ~((U8)~0))
3113 PL_rs = &PL_sv_undef;
3114 else if (!rschar && numlen >= 2)
396482e1 3115 PL_rs = newSVpvs("");
f2095865
JH
3116 else {
3117 char ch = (char)rschar;
3118 PL_rs = newSVpvn(&ch, 1);
3119 }
3120 }
64ace3f8 3121 sv_setsv(get_sv("/", GV_ADD), PL_rs);
f2095865 3122 return s + numlen;
a863c7d1 3123 }
46487f74 3124 case 'C':
a05d7ebb 3125 s++;
dd374669 3126 PL_unicode = parse_unicode_opts( (const char **)&s );
5a22a2bb
NC
3127 if (PL_unicode & PERL_UNICODE_UTF8CACHEASSERT_FLAG)
3128 PL_utf8cache = -1;
46487f74 3129 return s;
2304df62 3130 case 'F':
3280af22 3131 PL_minus_F = TRUE;
ebce5377
RGS
3132 PL_splitstr = ++s;
3133 while (*s && !isSPACE(*s)) ++s;
e49e380e 3134 PL_splitstr = savepvn(PL_splitstr, s - PL_splitstr);
2304df62 3135 return s;
79072805 3136 case 'a':
3280af22 3137 PL_minus_a = TRUE;
79072805
LW
3138 s++;
3139 return s;
3140 case 'c':
3280af22 3141 PL_minus_c = TRUE;
79072805
LW
3142 s++;
3143 return s;
3144 case 'd':
f20b2998 3145 forbid_setid('d', FALSE);
4633a7c4 3146 s++;
2cbb2ee1
RGS
3147
3148 /* -dt indicates to the debugger that threads will be used */
3149 if (*s == 't' && !isALNUM(s[1])) {
3150 ++s;
3151 my_setenv("PERL5DB_THREADED", "1");
3152 }
3153
70c94a19
RR
3154 /* The following permits -d:Mod to accepts arguments following an =
3155 in the fashion that -MSome::Mod does. */
3156 if (*s == ':' || *s == '=') {
b19934fb
NC
3157 const char *start;
3158 const char *end;
3159 SV *sv;
3160
3161 if (*++s == '-') {
3162 ++s;
3163 sv = newSVpvs("no Devel::");
3164 } else {
3165 sv = newSVpvs("use Devel::");
3166 }
3167
3168 start = s;
3169 end = s + strlen(s);
f85893a1 3170
b19934fb 3171 /* We now allow -d:Module=Foo,Bar and -d:-Module */
70c94a19
RR
3172 while(isALNUM(*s) || *s==':') ++s;
3173 if (*s != '=')
f85893a1 3174 sv_catpvn(sv, start, end - start);
70c94a19
RR
3175 else {
3176 sv_catpvn(sv, start, s-start);
95a2b409
RGS
3177 /* Don't use NUL as q// delimiter here, this string goes in the
3178 * environment. */
3179 Perl_sv_catpvf(aTHX_ sv, " split(/,/,q{%s});", ++s);
70c94a19 3180 }
f85893a1 3181 s = end;
184f32ec 3182 my_setenv("PERL5DB", SvPV_nolen_const(sv));
c4db126b 3183 SvREFCNT_dec(sv);
4633a7c4 3184 }
ed094faf 3185 if (!PL_perldb) {
3280af22 3186 PL_perldb = PERLDB_ALL;
a0d0e21e 3187 init_debugger();
ed094faf 3188 }
79072805
LW
3189 return s;
3190 case 'D':
0453d815 3191 {
79072805 3192#ifdef DEBUGGING
f20b2998 3193 forbid_setid('D', FALSE);
b4ab917c 3194 s++;
dd374669 3195 PL_debug = get_debug_opts( (const char **)&s, 1) | DEBUG_TOP_FLAG;
12a43e32 3196#else /* !DEBUGGING */
0453d815 3197 if (ckWARN_d(WARN_DEBUGGING))
9014280d 3198 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING),
e6e64d9b 3199 "Recompile perl with -DDEBUGGING to use -D switch (did you mean -d ?)\n");
a0d0e21e 3200 for (s++; isALNUM(*s); s++) ;
79072805 3201#endif
79072805 3202 return s;
0453d815 3203 }
4633a7c4 3204 case 'h':
b6f82619 3205 usage();
79072805 3206 case 'i':
43c5f42d 3207 Safefree(PL_inplace);
c030f24b
GH
3208#if defined(__CYGWIN__) /* do backup extension automagically */
3209 if (*(s+1) == '\0') {
c86a4f2e 3210 PL_inplace = savepvs(".bak");
c030f24b
GH
3211 return s+1;
3212 }
3213#endif /* __CYGWIN__ */
5ef5d758 3214 {
d4c19fe8 3215 const char * const start = ++s;
5ef5d758
NC
3216 while (*s && !isSPACE(*s))
3217 ++s;
3218
3219 PL_inplace = savepvn(start, s - start);
3220 }
7b8d334a 3221 if (*s) {
5ef5d758 3222 ++s;
7b8d334a 3223 if (*s == '-') /* Additional switches on #! line. */
5ef5d758 3224 s++;
7b8d334a 3225 }
fb73857a 3226 return s;
4e49a025 3227 case 'I': /* -I handled both here and in parse_body() */
f20b2998 3228 forbid_setid('I', FALSE);
fb73857a
PP
3229 ++s;
3230 while (*s && isSPACE(*s))
3231 ++s;
3232 if (*s) {
c7030b81 3233 const char *e, *p;
0df16ed7
GS
3234 p = s;
3235 /* ignore trailing spaces (possibly followed by other switches) */
3236 do {
3237 for (e = p; *e && !isSPACE(*e); e++) ;
3238 p = e;
3239 while (isSPACE(*p))
3240 p++;
3241 } while (*p && *p != '-');
55b4bc1c 3242 incpush(s, e-s,
e28f3139 3243 INCPUSH_ADD_SUB_DIRS|INCPUSH_ADD_OLD_VERS|INCPUSH_UNSHIFT);
0df16ed7
GS
3244 s = p;
3245 if (*s == '-')
3246 s++;
79072805
LW
3247 }
3248 else
a67e862a 3249 Perl_croak(aTHX_ "No directory specified for -I");
fb73857a 3250 return s;
79072805 3251 case 'l':
3280af22 3252 PL_minus_l = TRUE;
79072805 3253 s++;
7889fe52
NIS
3254 if (PL_ors_sv) {
3255 SvREFCNT_dec(PL_ors_sv);
a0714e2c 3256 PL_ors_sv = NULL;
7889fe52 3257 }
79072805 3258 if (isDIGIT(*s)) {
53305cf1 3259 I32 flags = 0;
a3b680e6 3260 STRLEN numlen;
396482e1 3261 PL_ors_sv = newSVpvs("\n");
53305cf1
NC
3262 numlen = 3 + (*s == '0');
3263 *SvPVX(PL_ors_sv) = (char)grok_oct(s, &numlen, &flags, NULL);
79072805
LW
3264 s += numlen;
3265 }
3266 else {
8bfdd7d9 3267 if (RsPARA(PL_rs)) {
396482e1 3268 PL_ors_sv = newSVpvs("\n\n");
7889fe52
NIS
3269 }
3270 else {
8bfdd7d9 3271 PL_ors_sv = newSVsv(PL_rs);
c07a80fd 3272 }
79072805
LW
3273 }
3274 return s;
1a30305b 3275 case 'M':
f20b2998 3276 forbid_setid('M', FALSE); /* XXX ? */
1a30305b
PP
3277 /* FALL THROUGH */
3278 case 'm':
f20b2998 3279 forbid_setid('m', FALSE); /* XXX ? */
1a30305b 3280 if (*++s) {
c7030b81 3281 const char *start;
b64cb68c 3282 const char *end;
11343788 3283 SV *sv;
e1ec3a88 3284 const char *use = "use ";
0544e6df 3285 bool colon = FALSE;
a5f75d66 3286 /* -M-foo == 'no foo' */
d0043bd1
NC
3287 /* Leading space on " no " is deliberate, to make both
3288 possibilities the same length. */
3289 if (*s == '-') { use = " no "; ++s; }
3290 sv = newSVpvn(use,4);
a5f75d66 3291 start = s;
1a30305b 3292 /* We allow -M'Module qw(Foo Bar)' */
0544e6df
RB
3293 while(isALNUM(*s) || *s==':') {
3294 if( *s++ == ':' ) {
3295 if( *s == ':' )
3296 s++;
3297 else
3298 colon = TRUE;
3299 }
3300 }
3301 if (s == start)
3302 Perl_croak(aTHX_ "Module name required with -%c option",
3303 option);
3304 if (colon)
3305 Perl_croak(aTHX_ "Invalid module name %.*s with -%c option: "
3306 "contains single ':'",
63da6837 3307 (int)(s - start), start, option);
b64cb68c 3308 end = s + strlen(s);
c07a80fd 3309 if (*s != '=') {
b64cb68c 3310 sv_catpvn(sv, start, end - start);
0544e6df 3311 if (option == 'm') {
c07a80fd 3312 if (*s != '\0')
cea2e8a9 3313 Perl_croak(aTHX_ "Can't use '%c' after -mname", *s);
396482e1 3314 sv_catpvs( sv, " ()");
c07a80fd
PP
3315 }
3316 } else {
11343788 3317 sv_catpvn(sv, start, s-start);
b64cb68c
NC
3318 /* Use NUL as q''-delimiter. */
3319 sv_catpvs(sv, " split(/,/,q\0");
3320 ++s;
3321 sv_catpvn(sv, s, end - s);
396482e1 3322 sv_catpvs(sv, "\0)");
c07a80fd 3323 }
b64cb68c 3324 s = end;
29a861e7 3325 Perl_av_create_and_push(aTHX_ &PL_preambleav, sv);
1a30305b
PP
3326 }
3327 else
0544e6df 3328 Perl_croak(aTHX_ "Missing argument to -%c", option);
1a30305b 3329 return s;
79072805 3330 case 'n':
3280af22 3331 PL_minus_n = TRUE;
79072805
LW
3332 s++;
3333 return s;
3334 case 'p':
3280af22 3335 PL_minus_p = TRUE;
79072805
LW
3336 s++;
3337 return s;
3338 case 's':
f20b2998 3339 forbid_setid('s', FALSE);
3280af22 3340 PL_doswitches = TRUE;
79072805
LW
3341 s++;
3342 return s;
6537fe72 3343 case 't':
27a6968b 3344 case 'T':
284167a5
SM
3345#if SILENT_NO_TAINT_SUPPORT
3346 /* silently ignore */
3347#elif NO_TAINT_SUPPORT
3348 Perl_croak("This perl was compiled without taint support. "
3349 "Cowardly refusing to run with -t or -T flags");
3350#else
3351 if (!TAINTING_get)
27a6968b 3352 TOO_LATE_FOR(*s);
284167a5 3353#endif
6537fe72 3354 s++;
463ee0b2 3355 return s;
79072805 3356 case 'u':
3280af22 3357 PL_do_undump = TRUE;
79072805
LW
3358 s++;
3359 return s;
3360 case 'U':
3280af22 3361 PL_unsafe = TRUE;
79072805
LW
3362 s++;
3363 return s;
3364 case 'v':
c4bc78d9
NC
3365 minus_v();
3366 case 'w':
3367 if (! (PL_dowarn & G_WARN_ALL_MASK)) {
3368 PL_dowarn |= G_WARN_ON;
3369 }
3370 s++;
3371 return s;
3372 case 'W':
3373 PL_dowarn = G_WARN_ALL_ON|G_WARN_ON;
3374 if (!specialWARN(PL_compiling.cop_warnings))
3375 PerlMemShared_free(PL_compiling.cop_warnings);
3376 PL_compiling.cop_warnings = pWARN_ALL ;
3377 s++;
3378 return s;
3379 case 'X':
3380 PL_dowarn = G_WARN_ALL_OFF;
3381 if (!specialWARN(PL_compiling.cop_warnings))
3382 PerlMemShared_free(PL_compiling.cop_warnings);
3383 PL_compiling.cop_warnings = pWARN_NONE ;
3384 s++;
3385 return s;
3386 case '*':
3387 case ' ':
3388 while( *s == ' ' )
3389 ++s;
3390 if (s[0] == '-') /* Additional switches on #! line. */
3391 return s+1;
3392 break;
3393 case '-':
3394 case 0:
3395#if defined(WIN32) || !defined(PERL_STRICT_CR)
3396 case '\r':
3397#endif
3398 case '\n':
3399 case '\t':
3400 break;
3401#ifdef ALTERNATE_SHEBANG
3402 case 'S': /* OS/2 needs -S on "extproc" line. */
3403 break;
3404#endif
4bb78d63
CB
3405 case 'e': case 'f': case 'x': case 'E':
3406#ifndef ALTERNATE_SHEBANG
3407 case 'S':
3408#endif
3409 case 'V':
c4bc78d9 3410 Perl_croak(aTHX_ "Can't emulate -%.1s on #! line",s);
b7e077d0
FC
3411 default:
3412 Perl_croak(aTHX_
3413 "Unrecognized switch: -%.1s (-h will show valid options)",s
3414 );
c4bc78d9
NC
3415 }
3416 return NULL;
3417}
3418
3419
3420STATIC void
3421S_minus_v(pTHX)
3422{
fc3381af 3423 PerlIO * PIO_stdout;
d7aa5382 3424 if (!sv_derived_from(PL_patchlevel, "version"))
ac0e6a2f 3425 upg_version(PL_patchlevel, TRUE);
8e9464f1 3426#if !defined(DGUX)
46807d8e
YO
3427 {
3428 SV* level= vstringify(PL_patchlevel);
3429#ifdef PERL_PATCHNUM
23d483e2
NC
3430# ifdef PERL_GIT_UNCOMMITTED_CHANGES
3431 SV *num = newSVpvs(PERL_PATCHNUM "*");
3432# else
3433 SV *num = newSVpvs(PERL_PATCHNUM);
3434# endif
fc3381af
DD
3435 {
3436 STRLEN level_len, num_len;
3437 char * level_str, * num_str;
3438 num_str = SvPV(num, num_len);
3439 level_str = SvPV(level, level_len);
3440 if (num_len>=level_len && strnEQ(num_str,level_str,level_len)) {
3441 SvREFCNT_dec(level);
3442 level= num;
3443 } else {
3444 Perl_sv_catpvf(aTHX_ level, " (%"SVf")", num);
3445 SvREFCNT_dec(num);
3446 }
46807d8e
YO
3447 }
3448 #endif
fc3381af
DD
3449 PIO_stdout = PerlIO_stdout();
3450 PerlIO_printf(PIO_stdout,
ded326e4
DG
3451 "\nThis is perl " STRINGIFY(PERL_REVISION)
3452 ", version " STRINGIFY(PERL_VERSION)
3453 ", subversion " STRINGIFY(PERL_SUBVERSION)
3454 " (%"SVf") built for " ARCHNAME, level
3455 );
46807d8e
YO
3456 SvREFCNT_dec(level);
3457 }
8e9464f1 3458#else /* DGUX */
fc3381af 3459 PIO_stdout = PerlIO_stdout();
8e9464f1 3460/* Adjust verbose output as in the perl that ships with the DG/UX OS from EMC */
fc3381af 3461 PerlIO_printf(PIO_stdout,
52ea0aec 3462 Perl_form(aTHX_ "\nThis is perl, %"SVf"\n",
be2597df 3463 SVfARG(vstringify(PL_patchlevel))));
fc3381af 3464 PerlIO_printf(PIO_stdout,
8e9464f1
JH
3465 Perl_form(aTHX_ " built under %s at %s %s\n",
3466 OSNAME, __DATE__, __TIME__));
fc3381af 3467 PerlIO_printf(PIO_stdout,
8e9464f1 3468 Perl_form(aTHX_ " OS Specific Release: %s\n",
40a39f85 3469 OSVERS));
8e9464f1 3470#endif /* !DGUX */
fb73857a
PP
3471#if defined(LOCAL_PATCH_COUNT)
3472 if (LOCAL_PATCH_COUNT > 0)
fc3381af 3473 PerlIO_printf(PIO_stdout,
b0e47665
GS
3474 "\n(with %d registered patch%s, "
3475 "see perl -V for more detail)",
bb7a0f54 3476 LOCAL_PATCH_COUNT,
b0e47665 3477 (LOCAL_PATCH_COUNT!=1) ? "es" : "");
a5f75d66 3478#endif
1a30305b 3479
fc3381af 3480 PerlIO_printf(PIO_stdout,
fdfb76e4 3481 "\n\nCopyright 1987-2012, Larry Wall\n");
79072805 3482#ifdef MSDOS
fc3381af 3483 PerlIO_printf(PIO_stdout,
b0e47665 3484 "\nMS-DOS port Copyright (c) 1989, 1990, Diomidis Spinellis\n");
55497cff
PP
3485#endif
3486#ifdef DJGPP
fc3381af 3487 PerlIO_printf(PIO_stdout,
b0e47665
GS
3488 "djgpp v2 port (jpl5003c) by Hirofumi Watanabe, 1996\n"
3489 "djgpp v2 port (perl5004+) by Laszlo Molnar, 1997-1999\n");
4633a7c4 3490#endif
79072805 3491#ifdef OS2
fc3381af 3492 PerlIO_printf(PIO_stdout,
b0e47665 3493 "\n\nOS/2 port Copyright (c) 1990, 1991, Raymond Chen, Kai Uwe Rommel\n"
be3c0a43 3494 "Version 5 port Copyright (c) 1994-2002, Andreas Kaiser, Ilya Zakharevich\n");
79072805 3495#endif
a3f9223b 3496#ifdef __BEOS__
fc3381af 3497 PerlIO_printf(PIO_stdout,
b0e47665 3498 "BeOS port Copyright Tom Spindler, 1997-1999\n");
a3f9223b 3499#endif
9d116dd7 3500#ifdef OEMVS
fc3381af 3501 PerlIO_printf(PIO_stdout,
b0e47665 3502 "MVS (OS390) port by Mortice Kern Systems, 1997-1999\n");
9d116dd7 3503#endif
495c5fdc 3504#ifdef __VOS__
fc3381af 3505 PerlIO_printf(PIO_stdout,
94efb9fb 3506 "Stratus VOS port by Paul.Green@stratus.com, 1997-2002\n");
495c5fdc 3507#endif
a1a0e61e 3508#ifdef POSIX_BC
fc3381af 3509 PerlIO_printf(PIO_stdout,
b0e47665 3510 "BS2000 (POSIX) port by Start Amadeus GmbH, 1998-1999\n");
a1a0e61e 3511#endif
e1caacb4 3512#ifdef UNDER_CE
fc3381af
DD
3513 PerlIO_printf(PIO_stdout,
3514 "WINCE port by Rainer Keuchel, 2001-2002\n"
3515 "Built on " __DATE__ " " __TIME__ "\n\n");
e1caacb4
JH
3516 wce_hitreturn();
3517#endif
a0fd4948 3518#ifdef __SYMBIAN32__
fc3381af 3519 PerlIO_printf(PIO_stdout,
27da23d5
JH
3520 "Symbian port by Nokia, 2004-2005\n");
3521#endif
baed7233
DL
3522#ifdef BINARY_BUILD_NOTICE
3523 BINARY_BUILD_NOTICE;
3524#endif
fc3381af 3525