ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/staticperl/perl/perl.c
Revision: 1.1
Committed: Thu Jun 30 14:26:42 2005 UTC (21 years, 2 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: PERL-5-8-7, HEAD
Branch point for: PERL
Log Message:
*** empty log message ***

File Contents

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