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