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