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