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