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