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