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