3 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, by Larry Wall and others
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.
12 * "A ship then new they built for him/of mithril and of elven glass" --Bilbo
16 #define PERL_IN_PERL_C
18 #include "patchlevel.h" /* for local_patches */
22 char *nw_get_sitelib(const char *pl);
25 /* XXX If this causes problems, set i_unistd=undef in the hint file. */
42 #if !defined(STANDARD_C) && !defined(HAS_GETENV_PROTOTYPE) && !defined(PERL_MICRO)
43 char *getenv (char *); /* Usually in <stdlib.h> */
46 static I32 read_e_script(pTHX_ int idx, SV *buf_sv, int maxlen);
54 #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
60 #if defined(USE_ITHREADS)
61 # define INIT_TLS_AND_INTERP \
63 if (!PL_curinterp) { \
64 PERL_SET_INTERP(my_perl); \
67 PERL_SET_THX(my_perl); \
69 MUTEX_INIT(&PL_dollarzero_mutex); \
72 PERL_SET_THX(my_perl); \
76 # define INIT_TLS_AND_INTERP \
78 if (!PL_curinterp) { \
79 PERL_SET_INTERP(my_perl); \
81 PERL_SET_THX(my_perl); \
85 #ifdef PERL_IMPLICIT_SYS
87 perl_alloc_using(struct IPerlMem* ipM, struct IPerlMem* ipMS,
88 struct IPerlMem* ipMP, struct IPerlEnv* ipE,
89 struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
90 struct IPerlDir* ipD, struct IPerlSock* ipS,
91 struct IPerlProc* ipP)
93 PerlInterpreter *my_perl;
94 /* New() needs interpreter, so call malloc() instead */
95 my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
97 Zero(my_perl, 1, PerlInterpreter);
113 =head1 Embedding Functions
115 =for apidoc perl_alloc
117 Allocates a new Perl interpreter. See L<perlembed>.
125 PerlInterpreter *my_perl;
126 #ifdef USE_5005THREADS
130 /* New() needs interpreter, so call malloc() instead */
131 my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
134 Zero(my_perl, 1, PerlInterpreter);
137 #endif /* PERL_IMPLICIT_SYS */
140 =for apidoc perl_construct
142 Initializes a new Perl interpreter. See L<perlembed>.
148 perl_construct(pTHXx)
152 PL_perl_destruct_level = 1;
154 if (PL_perl_destruct_level > 0)
157 /* Init the real globals (and main thread)? */
159 #ifdef PERL_FLEXIBLE_EXCEPTIONS
160 PL_protect = MEMBER_TO_FPTR(Perl_default_protect); /* for exceptions */
163 PL_curcop = &PL_compiling; /* needed by ckWARN, right away */
165 PL_linestr = NEWSV(65,79);
166 sv_upgrade(PL_linestr,SVt_PVIV);
168 if (!SvREADONLY(&PL_sv_undef)) {
169 /* set read-only and try to insure than we wont see REFCNT==0
172 SvREADONLY_on(&PL_sv_undef);
173 SvREFCNT(&PL_sv_undef) = (~(U32)0)/2;
175 sv_setpv(&PL_sv_no,PL_No);
177 SvREADONLY_on(&PL_sv_no);
178 SvREFCNT(&PL_sv_no) = (~(U32)0)/2;
180 sv_setpv(&PL_sv_yes,PL_Yes);
182 SvREADONLY_on(&PL_sv_yes);
183 SvREFCNT(&PL_sv_yes) = (~(U32)0)/2;
185 SvREADONLY_on(&PL_sv_placeholder);
186 SvREFCNT(&PL_sv_placeholder) = (~(U32)0)/2;
189 PL_sighandlerp = Perl_sighandler;
190 PL_pidstatus = newHV();
193 PL_rs = newSVpvn("\n", 1);
198 PL_lex_state = LEX_NOTPARSING;
204 SET_NUMERIC_STANDARD();
208 PL_patchlevel = NEWSV(0,4);
209 (void)SvUPGRADE(PL_patchlevel, SVt_PVNV);
210 if (PERL_REVISION > 127 || PERL_VERSION > 127 || PERL_SUBVERSION > 127)
211 SvGROW(PL_patchlevel, UTF8_MAXLEN*3+1);
212 s = (U8*)SvPVX(PL_patchlevel);
213 /* Build version strings using "native" characters */
214 s = uvchr_to_utf8(s, (UV)PERL_REVISION);
215 s = uvchr_to_utf8(s, (UV)PERL_VERSION);
216 s = uvchr_to_utf8(s, (UV)PERL_SUBVERSION);
218 SvCUR_set(PL_patchlevel, s - (U8*)SvPVX(PL_patchlevel));
219 SvPOK_on(PL_patchlevel);
220 SvNVX(PL_patchlevel) = (NV)PERL_REVISION +
221 ((NV)PERL_VERSION / (NV)1000) +
222 ((NV)PERL_SUBVERSION / (NV)1000000);
223 SvNOK_on(PL_patchlevel); /* dual valued */
224 SvUTF8_on(PL_patchlevel);
225 SvREADONLY_on(PL_patchlevel);
228 #if defined(LOCAL_PATCH_COUNT)
229 PL_localpatches = local_patches; /* For possible -v */
232 #ifdef HAVE_INTERP_INTERN
236 PerlIO_init(aTHX); /* Hook to IO system */
238 PL_fdpid = newAV(); /* for remembering popen pids by fd */
239 PL_modglobal = newHV(); /* pointers to per-interpreter module globals */
240 PL_errors = newSVpvn("",0);
241 sv_setpvn(PERL_DEBUG_PAD(0), "", 0); /* For regex debugging. */
242 sv_setpvn(PERL_DEBUG_PAD(1), "", 0); /* ext/re needs these */
243 sv_setpvn(PERL_DEBUG_PAD(2), "", 0); /* even without DEBUGGING. */
245 PL_regex_padav = newAV();
246 av_push(PL_regex_padav,(SV*)newAV()); /* First entry is an array of empty elements */
247 PL_regex_pad = AvARRAY(PL_regex_padav);
249 #ifdef USE_REENTRANT_API
250 Perl_reentrant_init(aTHX);
253 /* Note that strtab is a rather special HV. Assumptions are made
254 about not iterating on it, and not adding tie magic to it.
255 It is properly deallocated in perl_destruct() */
258 HvSHAREKEYS_off(PL_strtab); /* mandatory */
259 hv_ksplit(PL_strtab, 512);
261 #if defined(__DYNAMIC__) && (defined(NeXT) || defined(__NeXT__))
262 _dyld_lookup_and_bind
263 ("__environ", (unsigned long *) &environ_pointer, NULL);
267 # ifdef USE_ENVIRON_ARRAY
268 PL_origenviron = environ;
272 /* Use sysconf(_SC_CLK_TCK) if available, if not
273 * available or if the sysconf() fails, use the HZ. */
274 #if defined(HAS_SYSCONF) && defined(_SC_CLK_TCK)
275 PL_clocktick = sysconf(_SC_CLK_TCK);
276 if (PL_clocktick <= 0)
280 PL_stashcache = newHV();
286 =for apidoc nothreadhook
288 Stub that provides thread hook for perl_destruct when there are
295 Perl_nothreadhook(pTHX)
301 =for apidoc perl_destruct
303 Shuts down a Perl interpreter. See L<perlembed>.
311 volatile int destruct_level; /* 0=none, 1=full, 2=full with checks */
313 #ifdef USE_5005THREADS
315 #endif /* USE_5005THREADS */
317 /* wait for all pseudo-forked children to finish */
318 PERL_WAIT_FOR_CHILDREN;
320 destruct_level = PL_perl_destruct_level;
324 if ((s = PerlEnv_getenv("PERL_DESTRUCT_LEVEL"))) {
326 if (destruct_level < i)
333 if(PL_exit_flags & PERL_EXIT_DESTRUCT_END) {
338 if (PL_endav && !PL_minus_c)
339 call_list(PL_scopestack_ix, PL_endav);
345 /* Need to flush since END blocks can produce output */
348 if (CALL_FPTR(PL_threadhook)(aTHX)) {
349 /* Threads hook has vetoed further cleanup */
350 return STATUS_NATIVE_EXPORT;
353 /* We must account for everything. */
355 /* Destroy the main CV and syntax tree */
357 /* ensure comppad/curpad to refer to main's pad */
358 if (CvPADLIST(PL_main_cv)) {
359 PAD_SET_CUR_NOSAVE(CvPADLIST(PL_main_cv), 1);
361 op_free(PL_main_root);
362 PL_main_root = Nullop;
364 PL_curcop = &PL_compiling;
365 PL_main_start = Nullop;
366 SvREFCNT_dec(PL_main_cv);
370 /* Tell PerlIO we are about to tear things apart in case
371 we have layers which are using resources that should
375 PerlIO_destruct(aTHX);
377 if (PL_sv_objcount) {
379 * Try to destruct global references. We do this first so that the
380 * destructors and destructees still exist. Some sv's might remain.
381 * Non-referenced objects are on their own.
386 /* unhook hooks which will soon be, or use, destroyed data */
387 SvREFCNT_dec(PL_warnhook);
388 PL_warnhook = Nullsv;
389 SvREFCNT_dec(PL_diehook);
392 /* call exit list functions */
393 while (PL_exitlistlen-- > 0)
394 PL_exitlist[PL_exitlistlen].fn(aTHX_ PL_exitlist[PL_exitlistlen].ptr);
396 Safefree(PL_exitlist);
401 if (destruct_level == 0){
403 DEBUG_P(debprofdump());
405 #if defined(PERLIO_LAYERS)
406 /* No more IO - including error messages ! */
407 PerlIO_cleanup(aTHX);
410 /* The exit() function will do everything that needs doing. */
411 return STATUS_NATIVE_EXPORT;
414 /* jettison our possibly duplicated environment */
415 /* if PERL_USE_SAFE_PUTENV is defined environ will not have been copied
416 * so we certainly shouldn't free it here
419 #if defined(USE_ENVIRON_ARRAY) && !defined(PERL_USE_SAFE_PUTENV)
420 if (environ != PL_origenviron
422 /* only main thread can free environ[0] contents */
423 && PL_curinterp == aTHX
429 for (i = 0; environ[i]; i++)
430 safesysfree(environ[i]);
432 /* Must use safesysfree() when working with environ. */
433 safesysfree(environ);
435 environ = PL_origenviron;
438 #endif /* !PERL_MICRO */
441 /* the syntax tree is shared between clones
442 * so op_free(PL_main_root) only ReREFCNT_dec's
443 * REGEXPs in the parent interpreter
444 * we need to manually ReREFCNT_dec for the clones
447 I32 i = AvFILLp(PL_regex_padav) + 1;
448 SV **ary = AvARRAY(PL_regex_padav);
452 REGEXP *re = INT2PTR(REGEXP *,SvIVX(resv));
454 if (SvFLAGS(resv) & SVf_BREAK) {
455 /* this is PL_reg_curpm, already freed
456 * flag is set in regexec.c:S_regtry
458 SvFLAGS(resv) &= ~SVf_BREAK;
460 else if(SvREPADTMP(resv)) {
461 SvREPADTMP_off(resv);
468 SvREFCNT_dec(PL_regex_padav);
469 PL_regex_padav = Nullav;
473 SvREFCNT_dec((SV*) PL_stashcache);
474 PL_stashcache = NULL;
476 /* loosen bonds of global variables */
479 (void)PerlIO_close(PL_rsfp);
483 /* Filters for program text */
484 SvREFCNT_dec(PL_rsfp_filters);
485 PL_rsfp_filters = Nullav;
488 PL_preprocess = FALSE;
494 PL_doswitches = FALSE;
495 PL_dowarn = G_WARN_OFF;
496 PL_doextract = FALSE;
497 PL_sawampersand = FALSE; /* must save all match strings */
500 Safefree(PL_inplace);
502 SvREFCNT_dec(PL_patchlevel);
505 SvREFCNT_dec(PL_e_script);
506 PL_e_script = Nullsv;
509 /* magical thingies */
511 SvREFCNT_dec(PL_ofs_sv); /* $, */
514 SvREFCNT_dec(PL_ors_sv); /* $\ */
517 SvREFCNT_dec(PL_rs); /* $/ */
520 PL_multiline = 0; /* $* */
521 Safefree(PL_osname); /* $^O */
524 SvREFCNT_dec(PL_statname);
525 PL_statname = Nullsv;
528 /* defgv, aka *_ should be taken care of elsewhere */
530 /* clean up after study() */
531 SvREFCNT_dec(PL_lastscream);
532 PL_lastscream = Nullsv;
533 Safefree(PL_screamfirst);
535 Safefree(PL_screamnext);
539 Safefree(PL_efloatbuf);
540 PL_efloatbuf = Nullch;
543 /* startup and shutdown function lists */
544 SvREFCNT_dec(PL_beginav);
545 SvREFCNT_dec(PL_beginav_save);
546 SvREFCNT_dec(PL_endav);
547 SvREFCNT_dec(PL_checkav);
548 SvREFCNT_dec(PL_checkav_save);
549 SvREFCNT_dec(PL_initav);
551 PL_beginav_save = Nullav;
554 PL_checkav_save = Nullav;
557 /* shortcuts just get cleared */
563 PL_argvoutgv = Nullgv;
565 PL_stderrgv = Nullgv;
566 PL_last_in_gv = Nullgv;
568 PL_debstash = Nullhv;
570 /* reset so print() ends up where we expect */
573 SvREFCNT_dec(PL_argvout_stack);
574 PL_argvout_stack = Nullav;
576 SvREFCNT_dec(PL_modglobal);
577 PL_modglobal = Nullhv;
578 SvREFCNT_dec(PL_preambleav);
579 PL_preambleav = Nullav;
580 SvREFCNT_dec(PL_subname);
582 SvREFCNT_dec(PL_linestr);
584 SvREFCNT_dec(PL_pidstatus);
585 PL_pidstatus = Nullhv;
586 SvREFCNT_dec(PL_toptarget);
587 PL_toptarget = Nullsv;
588 SvREFCNT_dec(PL_bodytarget);
589 PL_bodytarget = Nullsv;
590 PL_formtarget = Nullsv;
592 /* free locale stuff */
593 #ifdef USE_LOCALE_COLLATE
594 Safefree(PL_collation_name);
595 PL_collation_name = Nullch;
598 #ifdef USE_LOCALE_NUMERIC
599 Safefree(PL_numeric_name);
600 PL_numeric_name = Nullch;
601 SvREFCNT_dec(PL_numeric_radix_sv);
604 /* clear utf8 character classes */
605 SvREFCNT_dec(PL_utf8_alnum);
606 SvREFCNT_dec(PL_utf8_alnumc);
607 SvREFCNT_dec(PL_utf8_ascii);
608 SvREFCNT_dec(PL_utf8_alpha);
609 SvREFCNT_dec(PL_utf8_space);
610 SvREFCNT_dec(PL_utf8_cntrl);
611 SvREFCNT_dec(PL_utf8_graph);
612 SvREFCNT_dec(PL_utf8_digit);
613 SvREFCNT_dec(PL_utf8_upper);
614 SvREFCNT_dec(PL_utf8_lower);
615 SvREFCNT_dec(PL_utf8_print);
616 SvREFCNT_dec(PL_utf8_punct);
617 SvREFCNT_dec(PL_utf8_xdigit);
618 SvREFCNT_dec(PL_utf8_mark);
619 SvREFCNT_dec(PL_utf8_toupper);
620 SvREFCNT_dec(PL_utf8_totitle);
621 SvREFCNT_dec(PL_utf8_tolower);
622 SvREFCNT_dec(PL_utf8_tofold);
623 SvREFCNT_dec(PL_utf8_idstart);
624 SvREFCNT_dec(PL_utf8_idcont);
625 PL_utf8_alnum = Nullsv;
626 PL_utf8_alnumc = Nullsv;
627 PL_utf8_ascii = Nullsv;
628 PL_utf8_alpha = Nullsv;
629 PL_utf8_space = Nullsv;
630 PL_utf8_cntrl = Nullsv;
631 PL_utf8_graph = Nullsv;
632 PL_utf8_digit = Nullsv;
633 PL_utf8_upper = Nullsv;
634 PL_utf8_lower = Nullsv;
635 PL_utf8_print = Nullsv;
636 PL_utf8_punct = Nullsv;
637 PL_utf8_xdigit = Nullsv;
638 PL_utf8_mark = Nullsv;
639 PL_utf8_toupper = Nullsv;
640 PL_utf8_totitle = Nullsv;
641 PL_utf8_tolower = Nullsv;
642 PL_utf8_tofold = Nullsv;
643 PL_utf8_idstart = Nullsv;
644 PL_utf8_idcont = Nullsv;
646 if (!specialWARN(PL_compiling.cop_warnings))
647 SvREFCNT_dec(PL_compiling.cop_warnings);
648 PL_compiling.cop_warnings = Nullsv;
649 if (!specialCopIO(PL_compiling.cop_io))
650 SvREFCNT_dec(PL_compiling.cop_io);
651 PL_compiling.cop_io = Nullsv;
652 CopFILE_free(&PL_compiling);
653 CopSTASH_free(&PL_compiling);
655 /* Prepare to destruct main symbol table. */
660 SvREFCNT_dec(PL_curstname);
661 PL_curstname = Nullsv;
663 /* clear queued errors */
664 SvREFCNT_dec(PL_errors);
668 if (destruct_level >= 2 && ckWARN_d(WARN_INTERNAL)) {
669 if (PL_scopestack_ix != 0)
670 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
671 "Unbalanced scopes: %ld more ENTERs than LEAVEs\n",
672 (long)PL_scopestack_ix);
673 if (PL_savestack_ix != 0)
674 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
675 "Unbalanced saves: %ld more saves than restores\n",
676 (long)PL_savestack_ix);
677 if (PL_tmps_floor != -1)
678 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),"Unbalanced tmps: %ld more allocs than frees\n",
679 (long)PL_tmps_floor + 1);
680 if (cxstack_ix != -1)
681 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),"Unbalanced context: %ld more PUSHes than POPs\n",
682 (long)cxstack_ix + 1);
685 /* Now absolutely destruct everything, somehow or other, loops or no. */
686 SvFLAGS(PL_fdpid) |= SVTYPEMASK; /* don't clean out pid table now */
687 SvFLAGS(PL_strtab) |= SVTYPEMASK; /* don't clean out strtab now */
689 /* the 2 is for PL_fdpid and PL_strtab */
690 while (PL_sv_count > 2 && sv_clean_all())
693 SvFLAGS(PL_fdpid) &= ~SVTYPEMASK;
694 SvFLAGS(PL_fdpid) |= SVt_PVAV;
695 SvFLAGS(PL_strtab) &= ~SVTYPEMASK;
696 SvFLAGS(PL_strtab) |= SVt_PVHV;
698 AvREAL_off(PL_fdpid); /* no surviving entries */
699 SvREFCNT_dec(PL_fdpid); /* needed in io_close() */
702 #ifdef HAVE_INTERP_INTERN
706 /* Destruct the global string table. */
708 /* Yell and reset the HeVAL() slots that are still holding refcounts,
709 * so that sv_free() won't fail on them.
717 max = HvMAX(PL_strtab);
718 array = HvARRAY(PL_strtab);
721 if (hent && ckWARN_d(WARN_INTERNAL)) {
722 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
723 "Unbalanced string table refcount: (%d) for \"%s\"",
724 HeVAL(hent) - Nullsv, HeKEY(hent));
725 HeVAL(hent) = Nullsv;
735 SvREFCNT_dec(PL_strtab);
738 /* free the pointer table used for cloning */
739 ptr_table_free(PL_ptr_table);
742 /* free special SVs */
744 SvREFCNT(&PL_sv_yes) = 0;
745 sv_clear(&PL_sv_yes);
746 SvANY(&PL_sv_yes) = NULL;
747 SvFLAGS(&PL_sv_yes) = 0;
749 SvREFCNT(&PL_sv_no) = 0;
751 SvANY(&PL_sv_no) = NULL;
752 SvFLAGS(&PL_sv_no) = 0;
756 for (i=0; i<=2; i++) {
757 SvREFCNT(PERL_DEBUG_PAD(i)) = 0;
758 sv_clear(PERL_DEBUG_PAD(i));
759 SvANY(PERL_DEBUG_PAD(i)) = NULL;
760 SvFLAGS(PERL_DEBUG_PAD(i)) = 0;
764 if (PL_sv_count != 0 && ckWARN_d(WARN_INTERNAL))
765 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),"Scalars leaked: %ld\n", (long)PL_sv_count);
767 #ifdef DEBUG_LEAKING_SCALARS
768 if (PL_sv_count != 0) {
773 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
774 svend = &sva[SvREFCNT(sva)];
775 for (sv = sva + 1; sv < svend; ++sv) {
776 if (SvTYPE(sv) != SVTYPEMASK) {
777 PerlIO_printf(Perl_debug_log, "leaked: 0x%p\n", sv);
785 #if defined(PERLIO_LAYERS)
786 /* No more IO - including error messages ! */
787 PerlIO_cleanup(aTHX);
790 /* sv_undef needs to stay immortal until after PerlIO_cleanup
791 as currently layers use it rather than Nullsv as a marker
792 for no arg - and will try and SvREFCNT_dec it.
794 SvREFCNT(&PL_sv_undef) = 0;
795 SvREADONLY_off(&PL_sv_undef);
797 SvREFCNT(&PL_sv_placeholder) = 0;
798 SvREADONLY_off(&PL_sv_placeholder);
800 Safefree(PL_origfilename);
801 Safefree(PL_reg_start_tmp);
803 Safefree(PL_reg_curpm);
804 Safefree(PL_reg_poscache);
806 Safefree(PL_op_mask);
807 Safefree(PL_psig_ptr);
808 Safefree(PL_psig_name);
809 Safefree(PL_bitcount);
810 Safefree(PL_psig_pend);
812 PL_hints = 0; /* Reset hints. Should hints be per-interpreter ? */
814 DEBUG_P(debprofdump());
816 #ifdef USE_REENTRANT_API
817 Perl_reentrant_free(aTHX);
822 /* As the absolutely last thing, free the non-arena SV for mess() */
825 /* it could have accumulated taint magic */
826 if (SvTYPE(PL_mess_sv) >= SVt_PVMG) {
829 for (mg = SvMAGIC(PL_mess_sv); mg; mg = moremagic) {
830 moremagic = mg->mg_moremagic;
831 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global
833 Safefree(mg->mg_ptr);
837 /* we know that type >= SVt_PV */
838 (void)SvOOK_off(PL_mess_sv);
839 Safefree(SvPVX(PL_mess_sv));
840 Safefree(SvANY(PL_mess_sv));
841 Safefree(PL_mess_sv);
844 return STATUS_NATIVE_EXPORT;
848 =for apidoc perl_free
850 Releases a Perl interpreter. See L<perlembed>.
858 #if defined(WIN32) || defined(NETWARE)
859 # if defined(PERL_IMPLICIT_SYS)
861 void *host = nw_internal_host;
863 void *host = w32_internal_host;
867 nw_delete_internal_host(host);
869 win32_delete_internal_host(host);
880 Perl_call_atexit(pTHX_ ATEXIT_t fn, void *ptr)
882 Renew(PL_exitlist, PL_exitlistlen+1, PerlExitListEntry);
883 PL_exitlist[PL_exitlistlen].fn = fn;
884 PL_exitlist[PL_exitlistlen].ptr = ptr;
889 =for apidoc perl_parse
891 Tells a Perl interpreter to parse a Perl script. See L<perlembed>.
897 perl_parse(pTHXx_ XSINIT_t xsinit, int argc, char **argv, char **env)
902 #ifdef USE_5005THREADS
906 #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
909 Perl_croak(aTHX_ "suidperl is no longer needed since the kernel can now execute\n\
910 setuid perl scripts securely.\n");
914 #if defined(USE_HASH_SEED) || defined(USE_HASH_SEED_EXPLICIT)
915 /* [perl #22371] Algorimic Complexity Attack on Perl 5.6.1, 5.8.0
916 * This MUST be done before any hash stores or fetches take place.
917 * If you set PL_hash_seed (and assumedly also PL_hash_seed_set) yourself,
918 * it is your responsibility to provide a good random seed!
919 * You can also define PERL_HASH_SEED in compile time, see hv.h. */
920 if (!PL_hash_seed_set)
921 PL_new_hash_seed = get_hash_seed();
923 char *s = PerlEnv_getenv("PERL_HASH_SEED_DEBUG");
929 PerlIO_printf(Perl_debug_log, "HASH_SEED = %"UVuf"\n",
933 #endif /* #if defined(USE_HASH_SEED) || defined(USE_HASH_SEED_EXPLICIT) */
939 /* Set PL_origalen be the sum of the contiguous argv[]
940 * elements plus the size of the env in case that it is
941 * contiguous with the argv[]. This is used in mg.c:mg_set()
942 * as the maximum modifiable length of $0. In the worst case
943 * the area we are able to modify is limited to the size of
944 * the original argv[0]. (See below for 'contiguous', though.)
949 ~(UV)(PTRSIZE == 4 ? 3 : PTRSIZE == 8 ? 7 : PTRSIZE == 16 ? 15 : 0);
950 /* Do the mask check only if the args seem like aligned. */
952 (mask < ~(UV)0) && ((PTR2UV(argv[0]) & mask) == PTR2UV(argv[0]));
954 /* See if all the arguments are contiguous in memory. Note
955 * that 'contiguous' is a loose term because some platforms
956 * align the argv[] and the envp[]. If the arguments look
957 * like non-aligned, assume that they are 'strictly' or
958 * 'traditionally' contiguous. If the arguments look like
959 * aligned, we just check that they are within aligned
960 * PTRSIZE bytes. As long as no system has something bizarre
961 * like the argv[] interleaved with some other data, we are
962 * fine. (Did I just evoke Murphy's Law?) --jhi */
963 if (PL_origargv && PL_origargc >= 1 && (s = PL_origargv[0])) {
965 for (i = 1; i < PL_origargc; i++) {
966 if ((PL_origargv[i] == s + 1
968 || PL_origargv[i] == s + 2
973 (PL_origargv[i] > s &&
975 INT2PTR(char *, PTR2UV(s + PTRSIZE) & mask)))
985 /* Can we grab env area too to be used as the area for $0? */
986 if (PL_origenviron) {
987 if ((PL_origenviron[0] == s + 1
989 || (PL_origenviron[0] == s + 9 && (s += 8))
994 (PL_origenviron[0] > s &&
996 INT2PTR(char *, PTR2UV(s + PTRSIZE) & mask)))
1000 s = PL_origenviron[0];
1003 my_setenv("NoNe SuCh", Nullch);
1004 /* Force copy of environment. */
1005 for (i = 1; PL_origenviron[i]; i++) {
1006 if (PL_origenviron[i] == s + 1
1009 (PL_origenviron[i] > s &&
1010 PL_origenviron[i] <=
1011 INT2PTR(char *, PTR2UV(s + PTRSIZE) & mask)))
1014 s = PL_origenviron[i];
1022 PL_origalen = s - PL_origargv[0];
1027 /* Come here if running an undumped a.out. */
1029 PL_origfilename = savepv(argv[0]);
1030 PL_do_undump = FALSE;
1031 cxstack_ix = -1; /* start label stack again */
1033 init_postdump_symbols(argc,argv,env);
1038 op_free(PL_main_root);
1039 PL_main_root = Nullop;
1041 PL_main_start = Nullop;
1042 SvREFCNT_dec(PL_main_cv);
1043 PL_main_cv = Nullcv;
1046 oldscope = PL_scopestack_ix;
1047 PL_dowarn = G_WARN_OFF;
1049 #ifdef PERL_FLEXIBLE_EXCEPTIONS
1050 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vparse_body), env, xsinit);
1056 #ifndef PERL_FLEXIBLE_EXCEPTIONS
1057 parse_body(env,xsinit);
1060 call_list(oldscope, PL_checkav);
1067 /* my_exit() was called */
1068 while (PL_scopestack_ix > oldscope)
1071 PL_curstash = PL_defstash;
1073 call_list(oldscope, PL_checkav);
1074 ret = STATUS_NATIVE_EXPORT;
1077 PerlIO_printf(Perl_error_log, "panic: top_env\n");
1085 #ifdef PERL_FLEXIBLE_EXCEPTIONS
1087 S_vparse_body(pTHX_ va_list args)
1089 char **env = va_arg(args, char**);
1090 XSINIT_t xsinit = va_arg(args, XSINIT_t);
1092 return parse_body(env, xsinit);
1097 S_parse_body(pTHX_ char **env, XSINIT_t xsinit)
1099 int argc = PL_origargc;
1100 char **argv = PL_origargv;
1101 char *scriptname = NULL;
1103 VOL bool dosearch = FALSE;
1104 char *validarg = "";
1107 char *cddir = Nullch;
1109 sv_setpvn(PL_linestr,"",0);
1110 sv = newSVpvn("",0); /* first used for -I flags */
1114 for (argc--,argv++; argc > 0; argc--,argv++) {
1115 if (argv[0][0] != '-' || !argv[0][1])
1119 validarg = " PHOOEY ";
1127 #ifndef PERL_STRICT_CR
1152 if ((s = moreswitches(s)))
1157 CHECK_MALLOC_TOO_LATE_FOR('t');
1158 if( !PL_tainting ) {
1159 PL_taint_warn = TRUE;
1165 CHECK_MALLOC_TOO_LATE_FOR('T');
1167 PL_taint_warn = FALSE;
1172 #ifdef MACOS_TRADITIONAL
1173 /* ignore -e for Dev:Pseudo argument */
1174 if (argv[1] && !strcmp(argv[1], "Dev:Pseudo"))
1177 if (PL_euid != PL_uid || PL_egid != PL_gid)
1178 Perl_croak(aTHX_ "No -e allowed in setuid scripts");
1180 PL_e_script = newSVpvn("",0);
1181 filter_add(read_e_script, NULL);
1184 sv_catpv(PL_e_script, s);
1186 sv_catpv(PL_e_script, argv[1]);
1190 Perl_croak(aTHX_ "No code specified for -e");
1191 sv_catpv(PL_e_script, "\n");
1194 case 'I': /* -I handled both here and in moreswitches() */
1196 if (!*++s && (s=argv[1]) != Nullch) {
1201 STRLEN len = strlen(s);
1202 p = savepvn(s, len);
1203 incpush(p, TRUE, TRUE, FALSE);
1204 sv_catpvn(sv, "-I", 2);
1205 sv_catpvn(sv, p, len);
1206 sv_catpvn(sv, " ", 1);
1210 Perl_croak(aTHX_ "No directory specified for -I");
1214 PL_preprocess = TRUE;
1224 PL_preambleav = newAV();
1225 av_push(PL_preambleav, newSVpv("use Config qw(myconfig config_vars)",0));
1227 PL_Sv = newSVpv("print myconfig();",0);
1229 sv_catpv(PL_Sv,"print \"\\nCharacteristics of this PERLSHR image: \\n\",");
1231 sv_catpv(PL_Sv,"print \"\\nCharacteristics of this binary (from libperl): \\n\",");
1233 sv_catpv(PL_Sv,"\" Compile-time options:");
1235 sv_catpv(PL_Sv," DEBUGGING");
1237 # ifdef MULTIPLICITY
1238 sv_catpv(PL_Sv," MULTIPLICITY");
1240 # ifdef USE_5005THREADS
1241 sv_catpv(PL_Sv," USE_5005THREADS");
1243 # ifdef USE_ITHREADS
1244 sv_catpv(PL_Sv," USE_ITHREADS");
1246 # ifdef USE_64_BIT_INT
1247 sv_catpv(PL_Sv," USE_64_BIT_INT");
1249 # ifdef USE_64_BIT_ALL
1250 sv_catpv(PL_Sv," USE_64_BIT_ALL");
1252 # ifdef USE_LONG_DOUBLE
1253 sv_catpv(PL_Sv," USE_LONG_DOUBLE");
1255 # ifdef USE_LARGE_FILES
1256 sv_catpv(PL_Sv," USE_LARGE_FILES");
1259 sv_catpv(PL_Sv," USE_SOCKS");
1261 # ifdef PERL_IMPLICIT_CONTEXT
1262 sv_catpv(PL_Sv," PERL_IMPLICIT_CONTEXT");
1264 # ifdef PERL_IMPLICIT_SYS
1265 sv_catpv(PL_Sv," PERL_IMPLICIT_SYS");
1267 sv_catpv(PL_Sv,"\\n\",");
1269 #if defined(LOCAL_PATCH_COUNT)
1270 if (LOCAL_PATCH_COUNT > 0) {
1272 sv_catpv(PL_Sv,"\" Locally applied patches:\\n\",");
1273 for (i = 1; i <= LOCAL_PATCH_COUNT; i++) {
1274 if (PL_localpatches[i])
1275 Perl_sv_catpvf(aTHX_ PL_Sv,"q\" \t%s\n\",",PL_localpatches[i]);
1279 Perl_sv_catpvf(aTHX_ PL_Sv,"\" Built under %s\\n\"",OSNAME);
1282 Perl_sv_catpvf(aTHX_ PL_Sv,",\" Compiled at %s %s\\n\"",__DATE__,__TIME__);
1284 Perl_sv_catpvf(aTHX_ PL_Sv,",\" Compiled on %s\\n\"",__DATE__);
1287 sv_catpv(PL_Sv, "; \
1289 @env = map { \"$_=\\\"$ENV{$_}\\\"\" } sort grep {/^PERL/} keys %ENV; ");
1292 push @env, \"CYGWIN=\\\"$ENV{CYGWIN}\\\"\";");
1295 print \" \\%ENV:\\n @env\\n\" if @env; \
1296 print \" \\@INC:\\n @INC\\n\";");
1299 PL_Sv = newSVpv("config_vars(qw(",0);
1300 sv_catpv(PL_Sv, ++s);
1301 sv_catpv(PL_Sv, "))");
1304 av_push(PL_preambleav, PL_Sv);
1305 scriptname = BIT_BUCKET; /* don't look for script or read stdin */
1308 PL_doextract = TRUE;
1316 if (!*++s || isSPACE(*s)) {
1320 /* catch use of gnu style long options */
1321 if (strEQ(s, "version")) {
1325 if (strEQ(s, "help")) {
1332 Perl_croak(aTHX_ "Unrecognized switch: -%s (-h will show valid options)",s);
1336 sv_setsv(get_sv("/", TRUE), PL_rs);
1339 #ifndef SECURE_INTERNAL_GETENV
1342 (s = PerlEnv_getenv("PERL5OPT")))
1347 if (*s == '-' && *(s+1) == 'T') {
1348 CHECK_MALLOC_TOO_LATE_FOR('T');
1350 PL_taint_warn = FALSE;
1353 char *popt_copy = Nullch;
1366 if (!strchr("DIMUdmtwA", *s))
1367 Perl_croak(aTHX_ "Illegal switch in PERL5OPT: -%c", *s);
1371 popt_copy = SvPVX(sv_2mortal(newSVpv(popt,0)));
1372 s = popt_copy + (s - popt);
1373 d = popt_copy + (d - popt);
1380 if( !PL_tainting ) {
1381 PL_taint_warn = TRUE;
1391 if (PL_taint_warn && PL_dowarn != G_WARN_ALL_OFF) {
1392 PL_compiling.cop_warnings = newSVpvn(WARN_TAINTstring, WARNsize);
1396 scriptname = argv[0];
1399 scriptname = BIT_BUCKET; /* don't look for script or read stdin */
1401 else if (scriptname == Nullch) {
1403 if ( PerlLIO_isatty(PerlIO_fileno(PerlIO_stdin())) )
1411 open_script(scriptname,dosearch,sv,&fdscript);
1413 validate_suid(validarg, scriptname,fdscript);
1416 #if defined(SIGCHLD) || defined(SIGCLD)
1419 # define SIGCHLD SIGCLD
1421 Sighandler_t sigstate = rsignal_state(SIGCHLD);
1422 if (sigstate == SIG_IGN) {
1423 if (ckWARN(WARN_SIGNAL))
1424 Perl_warner(aTHX_ packWARN(WARN_SIGNAL),
1425 "Can't ignore signal CHLD, forcing to default");
1426 (void)rsignal(SIGCHLD, (Sighandler_t)SIG_DFL);
1432 #ifdef MACOS_TRADITIONAL
1433 if (PL_doextract || gMacPerl_AlwaysExtract) {
1438 if (cddir && PerlDir_chdir(cddir) < 0)
1439 Perl_croak(aTHX_ "Can't chdir to %s",cddir);
1443 PL_main_cv = PL_compcv = (CV*)NEWSV(1104,0);
1444 sv_upgrade((SV *)PL_compcv, SVt_PVCV);
1445 CvUNIQUE_on(PL_compcv);
1447 CvPADLIST(PL_compcv) = pad_new(0);
1448 #ifdef USE_5005THREADS
1449 CvOWNER(PL_compcv) = 0;
1450 New(666, CvMUTEXP(PL_compcv), 1, perl_mutex);
1451 MUTEX_INIT(CvMUTEXP(PL_compcv));
1452 #endif /* USE_5005THREADS */
1455 boot_core_UNIVERSAL();
1456 boot_core_xsutils();
1459 (*xsinit)(aTHX); /* in case linked C routines want magical variables */
1461 #if defined(VMS) || defined(WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(EPOC)
1467 # ifdef HAS_SOCKS5_INIT
1468 socks5_init(argv[0]);
1474 init_predump_symbols();
1475 /* init_postdump_symbols not currently designed to be called */
1476 /* more than once (ENV isn't cleared first, for example) */
1477 /* But running with -u leaves %ENV & @ARGV undefined! XXX */
1479 init_postdump_symbols(argc,argv,env);
1481 /* PL_unicode is turned on by -C or by $ENV{PERL_UNICODE}.
1482 * PL_utf8locale is conditionally turned on by
1483 * locale.c:Perl_init_i18nl10n() if the environment
1484 * look like the user wants to use UTF-8. */
1486 /* Requires init_predump_symbols(). */
1487 if (!(PL_unicode & PERL_UNICODE_LOCALE_FLAG) || PL_utf8locale) {
1492 /* Turn on UTF-8-ness on STDIN, STDOUT, STDERR
1493 * and the default open disciplines. */
1494 if ((PL_unicode & PERL_UNICODE_STDIN_FLAG) &&
1495 PL_stdingv && (io = GvIO(PL_stdingv)) &&
1497 PerlIO_binmode(aTHX_ fp, IoTYPE(io), 0, ":utf8");
1498 if ((PL_unicode & PERL_UNICODE_STDOUT_FLAG) &&
1499 PL_defoutgv && (io = GvIO(PL_defoutgv)) &&
1501 PerlIO_binmode(aTHX_ fp, IoTYPE(io), 0, ":utf8");
1502 if ((PL_unicode & PERL_UNICODE_STDERR_FLAG) &&
1503 PL_stderrgv && (io = GvIO(PL_stderrgv)) &&
1505 PerlIO_binmode(aTHX_ fp, IoTYPE(io), 0, ":utf8");
1506 if ((PL_unicode & PERL_UNICODE_INOUT_FLAG) &&
1507 (sv = GvSV(gv_fetchpv("\017PEN", TRUE, SVt_PV)))) {
1508 U32 in = PL_unicode & PERL_UNICODE_IN_FLAG;
1509 U32 out = PL_unicode & PERL_UNICODE_OUT_FLAG;
1512 sv_setpvn(sv, ":utf8\0:utf8", 11);
1514 sv_setpvn(sv, ":utf8\0", 6);
1517 sv_setpvn(sv, "\0:utf8", 6);
1523 if ((s = PerlEnv_getenv("PERL_SIGNALS"))) {
1524 if (strEQ(s, "unsafe"))
1525 PL_signals |= PERL_SIGNALS_UNSAFE_FLAG;
1526 else if (strEQ(s, "safe"))
1527 PL_signals &= ~PERL_SIGNALS_UNSAFE_FLAG;
1529 Perl_croak(aTHX_ "PERL_SIGNALS illegal: \"%s\"", s);
1534 /* now parse the script */
1536 SETERRNO(0,SS_NORMAL);
1538 #ifdef MACOS_TRADITIONAL
1539 if (gMacPerl_SyntaxError = (yyparse() || PL_error_count)) {
1541 Perl_croak(aTHX_ "%s had compilation errors.\n", MacPerl_MPWFileName(PL_origfilename));
1543 Perl_croak(aTHX_ "Execution of %s aborted due to compilation errors.\n",
1544 MacPerl_MPWFileName(PL_origfilename));
1548 if (yyparse() || PL_error_count) {
1550 Perl_croak(aTHX_ "%s had compilation errors.\n", PL_origfilename);
1552 Perl_croak(aTHX_ "Execution of %s aborted due to compilation errors.\n",
1557 CopLINE_set(PL_curcop, 0);
1558 PL_curstash = PL_defstash;
1559 PL_preprocess = FALSE;
1561 SvREFCNT_dec(PL_e_script);
1562 PL_e_script = Nullsv;
1569 SAVECOPFILE(PL_curcop);
1570 SAVECOPLINE(PL_curcop);
1571 gv_check(PL_defstash);
1578 if ((s=PerlEnv_getenv("PERL_DEBUG_MSTATS")) && atoi(s) >= 2)
1579 dump_mstats("after compilation:");
1588 =for apidoc perl_run
1590 Tells a Perl interpreter to run. See L<perlembed>.
1601 #ifdef USE_5005THREADS
1605 oldscope = PL_scopestack_ix;
1610 #ifdef PERL_FLEXIBLE_EXCEPTIONS
1612 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vrun_body), oldscope);
1618 cxstack_ix = -1; /* start context stack again */
1620 case 0: /* normal completion */
1621 #ifndef PERL_FLEXIBLE_EXCEPTIONS
1626 case 2: /* my_exit() */
1627 while (PL_scopestack_ix > oldscope)
1630 PL_curstash = PL_defstash;
1631 if (!(PL_exit_flags & PERL_EXIT_DESTRUCT_END) &&
1632 PL_endav && !PL_minus_c)
1633 call_list(oldscope, PL_endav);
1635 if (PerlEnv_getenv("PERL_DEBUG_MSTATS"))
1636 dump_mstats("after execution: ");
1638 ret = STATUS_NATIVE_EXPORT;
1642 POPSTACK_TO(PL_mainstack);
1645 PerlIO_printf(Perl_error_log, "panic: restartop\n");
1655 #ifdef PERL_FLEXIBLE_EXCEPTIONS
1657 S_vrun_body(pTHX_ va_list args)
1659 I32 oldscope = va_arg(args, I32);
1661 return run_body(oldscope);
1667 S_run_body(pTHX_ I32 oldscope)
1669 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s $` $& $' support.\n",
1670 PL_sawampersand ? "Enabling" : "Omitting"));
1672 if (!PL_restartop) {
1673 DEBUG_x(dump_all());
1674 PERL_DEBUG(PerlIO_printf(Perl_debug_log, "\nEXECUTING...\n\n"));
1675 DEBUG_S(PerlIO_printf(Perl_debug_log, "main thread is 0x%"UVxf"\n",
1679 #ifdef MACOS_TRADITIONAL
1680 PerlIO_printf(Perl_error_log, "%s%s syntax OK\n",
1681 (gMacPerl_ErrorFormat ? "# " : ""),
1682 MacPerl_MPWFileName(PL_origfilename));
1684 PerlIO_printf(Perl_error_log, "%s syntax OK\n", PL_origfilename);
1688 if (PERLDB_SINGLE && PL_DBsingle)
1689 sv_setiv(PL_DBsingle, 1);
1691 call_list(oldscope, PL_initav);
1697 PL_op = PL_restartop;
1701 else if (PL_main_start) {
1702 CvDEPTH(PL_main_cv) = 1;
1703 PL_op = PL_main_start;
1713 =head1 SV Manipulation Functions
1715 =for apidoc p||get_sv
1717 Returns the SV of the specified Perl scalar. If C<create> is set and the
1718 Perl variable does not exist then it will be created. If C<create> is not
1719 set and the variable does not exist then NULL is returned.
1725 Perl_get_sv(pTHX_ const char *name, I32 create)
1728 #ifdef USE_5005THREADS
1729 if (name[1] == '\0' && !isALPHA(name[0])) {
1730 PADOFFSET tmp = find_threadsv(name);
1731 if (tmp != NOT_IN_PAD)
1732 return THREADSV(tmp);
1734 #endif /* USE_5005THREADS */
1735 gv = gv_fetchpv(name, create, SVt_PV);
1742 =head1 Array Manipulation Functions
1744 =for apidoc p||get_av
1746 Returns the AV of the specified Perl array. If C<create> is set and the
1747 Perl variable does not exist then it will be created. If C<create> is not
1748 set and the variable does not exist then NULL is returned.
1754 Perl_get_av(pTHX_ const char *name, I32 create)
1756 GV* gv = gv_fetchpv(name, create, SVt_PVAV);
1765 =head1 Hash Manipulation Functions
1767 =for apidoc p||get_hv
1769 Returns the HV of the specified Perl hash. If C<create> is set and the
1770 Perl variable does not exist then it will be created. If C<create> is not
1771 set and the variable does not exist then NULL is returned.
1777 Perl_get_hv(pTHX_ const char *name, I32 create)
1779 GV* gv = gv_fetchpv(name, create, SVt_PVHV);
1788 =head1 CV Manipulation Functions
1790 =for apidoc p||get_cv
1792 Returns the CV of the specified Perl subroutine. If C<create> is set and
1793 the Perl subroutine does not exist then it will be declared (which has the
1794 same effect as saying C<sub name;>). If C<create> is not set and the
1795 subroutine does not exist then NULL is returned.
1801 Perl_get_cv(pTHX_ const char *name, I32 create)
1803 GV* gv = gv_fetchpv(name, create, SVt_PVCV);
1804 /* XXX unsafe for threads if eval_owner isn't held */
1805 /* XXX this is probably not what they think they're getting.
1806 * It has the same effect as "sub name;", i.e. just a forward
1808 if (create && !GvCVu(gv))
1809 return newSUB(start_subparse(FALSE, 0),
1810 newSVOP(OP_CONST, 0, newSVpv(name,0)),
1818 /* Be sure to refetch the stack pointer after calling these routines. */
1822 =head1 Callback Functions
1824 =for apidoc p||call_argv
1826 Performs a callback to the specified Perl sub. See L<perlcall>.
1832 Perl_call_argv(pTHX_ const char *sub_name, I32 flags, register char **argv)
1834 /* See G_* flags in cop.h */
1835 /* null terminated arg list */
1842 XPUSHs(sv_2mortal(newSVpv(*argv,0)));
1847 return call_pv(sub_name, flags);
1851 =for apidoc p||call_pv
1853 Performs a callback to the specified Perl sub. See L<perlcall>.
1859 Perl_call_pv(pTHX_ const char *sub_name, I32 flags)
1860 /* name of the subroutine */
1861 /* See G_* flags in cop.h */
1863 return call_sv((SV*)get_cv(sub_name, TRUE), flags);
1867 =for apidoc p||call_method
1869 Performs a callback to the specified Perl method. The blessed object must
1870 be on the stack. See L<perlcall>.
1876 Perl_call_method(pTHX_ const char *methname, I32 flags)
1877 /* name of the subroutine */
1878 /* See G_* flags in cop.h */
1880 return call_sv(sv_2mortal(newSVpv(methname,0)), flags | G_METHOD);
1883 /* May be called with any of a CV, a GV, or an SV containing the name. */
1885 =for apidoc p||call_sv
1887 Performs a callback to the Perl sub whose name is in the SV. See
1894 Perl_call_sv(pTHX_ SV *sv, I32 flags)
1895 /* See G_* flags in cop.h */
1898 LOGOP myop; /* fake syntax tree node */
1901 volatile I32 retval = 0;
1903 bool oldcatch = CATCH_GET;
1908 if (flags & G_DISCARD) {
1913 Zero(&myop, 1, LOGOP);
1914 myop.op_next = Nullop;
1915 if (!(flags & G_NOARGS))
1916 myop.op_flags |= OPf_STACKED;
1917 myop.op_flags |= ((flags & G_VOID) ? OPf_WANT_VOID :
1918 (flags & G_ARRAY) ? OPf_WANT_LIST :
1923 EXTEND(PL_stack_sp, 1);
1924 *++PL_stack_sp = sv;
1926 oldscope = PL_scopestack_ix;
1928 if (PERLDB_SUB && PL_curstash != PL_debstash
1929 /* Handle first BEGIN of -d. */
1930 && (PL_DBcv || (PL_DBcv = GvCV(PL_DBsub)))
1931 /* Try harder, since this may have been a sighandler, thus
1932 * curstash may be meaningless. */
1933 && (SvTYPE(sv) != SVt_PVCV || CvSTASH((CV*)sv) != PL_debstash)
1934 && !(flags & G_NODEBUG))
1935 PL_op->op_private |= OPpENTERSUB_DB;
1937 if (flags & G_METHOD) {
1938 Zero(&method_op, 1, UNOP);
1939 method_op.op_next = PL_op;
1940 method_op.op_ppaddr = PL_ppaddr[OP_METHOD];
1941 myop.op_ppaddr = PL_ppaddr[OP_ENTERSUB];
1942 PL_op = (OP*)&method_op;
1945 if (!(flags & G_EVAL)) {
1947 call_body((OP*)&myop, FALSE);
1948 retval = PL_stack_sp - (PL_stack_base + oldmark);
1949 CATCH_SET(oldcatch);
1952 myop.op_other = (OP*)&myop;
1954 /* we're trying to emulate pp_entertry() here */
1956 register PERL_CONTEXT *cx;
1957 I32 gimme = GIMME_V;
1962 push_return(Nullop);
1963 PUSHBLOCK(cx, (CXt_EVAL|CXp_TRYBLOCK), PL_stack_sp);
1965 PL_eval_root = PL_op; /* Only needed so that goto works right. */
1967 PL_in_eval = EVAL_INEVAL;
1968 if (flags & G_KEEPERR)
1969 PL_in_eval |= EVAL_KEEPERR;
1975 #ifdef PERL_FLEXIBLE_EXCEPTIONS
1977 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vcall_body),
1984 #ifndef PERL_FLEXIBLE_EXCEPTIONS
1986 call_body((OP*)&myop, FALSE);
1988 retval = PL_stack_sp - (PL_stack_base + oldmark);
1989 if (!(flags & G_KEEPERR))
1996 /* my_exit() was called */
1997 PL_curstash = PL_defstash;
2000 if (PL_statusvalue && !(PL_exit_flags & PERL_EXIT_EXPECTED))
2001 Perl_croak(aTHX_ "Callback called exit");
2006 PL_op = PL_restartop;
2010 PL_stack_sp = PL_stack_base + oldmark;
2011 if (flags & G_ARRAY)
2015 *++PL_stack_sp = &PL_sv_undef;
2020 if (PL_scopestack_ix > oldscope) {
2024 register PERL_CONTEXT *cx;
2036 if (flags & G_DISCARD) {
2037 PL_stack_sp = PL_stack_base + oldmark;
2046 #ifdef PERL_FLEXIBLE_EXCEPTIONS
2048 S_vcall_body(pTHX_ va_list args)
2050 OP *myop = va_arg(args, OP*);
2051 int is_eval = va_arg(args, int);
2053 call_body(myop, is_eval);
2059 S_call_body(pTHX_ OP *myop, int is_eval)
2061 if (PL_op == myop) {
2063 PL_op = Perl_pp_entereval(aTHX); /* this doesn't do a POPMARK */
2065 PL_op = Perl_pp_entersub(aTHX); /* this does */
2071 /* Eval a string. The G_EVAL flag is always assumed. */
2074 =for apidoc p||eval_sv
2076 Tells Perl to C<eval> the string in the SV.
2082 Perl_eval_sv(pTHX_ SV *sv, I32 flags)
2084 /* See G_* flags in cop.h */
2087 UNOP myop; /* fake syntax tree node */
2088 volatile I32 oldmark = SP - PL_stack_base;
2089 volatile I32 retval = 0;
2095 if (flags & G_DISCARD) {
2102 Zero(PL_op, 1, UNOP);
2103 EXTEND(PL_stack_sp, 1);
2104 *++PL_stack_sp = sv;
2105 oldscope = PL_scopestack_ix;
2107 if (!(flags & G_NOARGS))
2108 myop.op_flags = OPf_STACKED;
2109 myop.op_next = Nullop;
2110 myop.op_type = OP_ENTEREVAL;
2111 myop.op_flags |= ((flags & G_VOID) ? OPf_WANT_VOID :
2112 (flags & G_ARRAY) ? OPf_WANT_LIST :
2114 if (flags & G_KEEPERR)
2115 myop.op_flags |= OPf_SPECIAL;
2117 #ifdef PERL_FLEXIBLE_EXCEPTIONS
2119 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vcall_body),
2126 #ifndef PERL_FLEXIBLE_EXCEPTIONS
2128 call_body((OP*)&myop,TRUE);
2130 retval = PL_stack_sp - (PL_stack_base + oldmark);
2131 if (!(flags & G_KEEPERR))
2138 /* my_exit() was called */
2139 PL_curstash = PL_defstash;
2142 if (PL_statusvalue && !(PL_exit_flags & PERL_EXIT_EXPECTED))
2143 Perl_croak(aTHX_ "Callback called exit");
2148 PL_op = PL_restartop;
2152 PL_stack_sp = PL_stack_base + oldmark;
2153 if (flags & G_ARRAY)
2157 *++PL_stack_sp = &PL_sv_undef;
2163 if (flags & G_DISCARD) {
2164 PL_stack_sp = PL_stack_base + oldmark;
2174 =for apidoc p||eval_pv
2176 Tells Perl to C<eval> the given string and return an SV* result.
2182 Perl_eval_pv(pTHX_ const char *p, I32 croak_on_error)
2185 SV* sv = newSVpv(p, 0);
2187 eval_sv(sv, G_SCALAR);
2194 if (croak_on_error && SvTRUE(ERRSV)) {
2196 Perl_croak(aTHX_ SvPVx(ERRSV, n_a));
2202 /* Require a module. */
2205 =head1 Embedding Functions
2207 =for apidoc p||require_pv
2209 Tells Perl to C<require> the file named by the string argument. It is
2210 analogous to the Perl code C<eval "require '$file'">. It's even
2211 implemented that way; consider using load_module instead.
2216 Perl_require_pv(pTHX_ const char *pv)
2220 PUSHSTACKi(PERLSI_REQUIRE);
2222 sv = sv_newmortal();
2223 sv_setpv(sv, "require '");
2226 eval_sv(sv, G_DISCARD);
2232 Perl_magicname(pTHX_ char *sym, char *name, I32 namlen)
2236 if ((gv = gv_fetchpv(sym,TRUE, SVt_PV)))
2237 sv_magic(GvSV(gv), (SV*)gv, PERL_MAGIC_sv, name, namlen);
2241 S_usage(pTHX_ char *name) /* XXX move this out into a module ? */
2243 /* This message really ought to be max 23 lines.
2244 * Removed -h because the user already knows that option. Others? */
2246 static char *usage_msg[] = {
2247 "-0[octal] specify record separator (\\0, if no argument)",
2248 "-a autosplit mode with -n or -p (splits $_ into @F)",
2249 "-C[number/list] enables the listed Unicode features",
2250 "-c check syntax only (runs BEGIN and CHECK blocks)",
2251 "-d[:debugger] run program under debugger",
2252 "-D[number/list] set debugging flags (argument is a bit mask or alphabets)",
2253 "-e program one line of program (several -e's allowed, omit programfile)",
2254 "-F/pattern/ split() pattern for -a switch (//'s are optional)",
2255 "-i[extension] edit <> files in place (makes backup if extension supplied)",
2256 "-Idirectory specify @INC/#include directory (several -I's allowed)",
2257 "-l[octal] enable line ending processing, specifies line terminator",
2258 "-[mM][-]module execute `use/no module...' before executing program",
2259 "-n assume 'while (<>) { ... }' loop around program",
2260 "-p assume loop like -n but print line also, like sed",
2261 "-P run program through C preprocessor before compilation",
2262 "-s enable rudimentary parsing for switches after programfile",
2263 "-S look for programfile using PATH environment variable",
2264 "-t enable tainting warnings",
2265 "-T enable tainting checks",
2266 "-u dump core after parsing program",
2267 "-U allow unsafe operations",
2268 "-v print version, subversion (includes VERY IMPORTANT perl info)",
2269 "-V[:variable] print configuration summary (or a single Config.pm variable)",
2270 "-w enable many useful warnings (RECOMMENDED)",
2271 "-W enable all warnings",
2272 "-x[directory] strip off text before #!perl line and perhaps cd to directory",
2273 "-X disable all warnings",
2277 char **p = usage_msg;
2279 PerlIO_printf(PerlIO_stdout(),
2280 "\nUsage: %s [switches] [--] [programfile] [arguments]",
2283 PerlIO_printf(PerlIO_stdout(), "\n %s", *p++);
2286 /* convert a string of -D options (or digits) into an int.
2287 * sets *s to point to the char after the options */
2291 Perl_get_debug_opts(pTHX_ char **s)
2295 /* if adding extra options, remember to update DEBUG_MASK */
2296 static char debopts[] = "psltocPmfrxu HXDSTRJvC";
2298 for (; isALNUM(**s); (*s)++) {
2299 char *d = strchr(debopts,**s);
2301 i |= 1 << (d - debopts);
2302 else if (ckWARN_d(WARN_DEBUGGING))
2303 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING),
2304 "invalid option -D%c\n", **s);
2309 for (; isALNUM(**s); (*s)++) ;
2312 if ((i & DEBUG_p_FLAG) && ckWARN_d(WARN_DEBUGGING))
2313 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING),
2314 "-Dp not implemented on this platform\n");
2320 /* This routine handles any switches that can be given during run */
2323 Perl_moreswitches(pTHX_ char *s)
2333 SvREFCNT_dec(PL_rs);
2334 if (s[1] == 'x' && s[2]) {
2338 for (s += 2, e = s; *e; e++);
2340 flags = PERL_SCAN_SILENT_ILLDIGIT;
2341 rschar = (U32)grok_hex(s, &numlen, &flags, NULL);
2342 if (s + numlen < e) {
2343 rschar = 0; /* Grandfather -0xFOO as -0 -xFOO. */
2347 PL_rs = newSVpvn("", 0);
2348 SvGROW(PL_rs, (STRLEN)(UNISKIP(rschar) + 1));
2349 tmps = (U8*)SvPVX(PL_rs);
2350 uvchr_to_utf8(tmps, rschar);
2351 SvCUR_set(PL_rs, UNISKIP(rschar));
2356 rschar = (U32)grok_oct(s, &numlen, &flags, NULL);
2357 if (rschar & ~((U8)~0))
2358 PL_rs = &PL_sv_undef;
2359 else if (!rschar && numlen >= 2)
2360 PL_rs = newSVpvn("", 0);
2362 char ch = (char)rschar;
2363 PL_rs = newSVpvn(&ch, 1);
2370 PL_unicode = parse_unicode_opts(&s);
2375 while (*s && !isSPACE(*s)) ++s;
2377 PL_splitstr = savepv(PL_splitstr);
2390 /* The following permits -d:Mod to accepts arguments following an =
2391 in the fashion that -MSome::Mod does. */
2392 if (*s == ':' || *s == '=') {
2395 sv = newSVpv("use Devel::", 0);
2397 /* We now allow -d:Module=Foo,Bar */
2398 while(isALNUM(*s) || *s==':') ++s;
2400 sv_catpv(sv, start);
2402 sv_catpvn(sv, start, s-start);
2403 sv_catpv(sv, " split(/,/,q{");
2408 my_setenv("PERL5DB", SvPV(sv, PL_na));
2411 PL_perldb = PERLDB_ALL;
2420 PL_debug = get_debug_opts(&s) | DEBUG_TOP_FLAG;
2421 #else /* !DEBUGGING */
2422 if (ckWARN_d(WARN_DEBUGGING))
2423 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING),
2424 "Recompile perl with -DDEBUGGING to use -D switch\n");
2425 for (s++; isALNUM(*s); s++) ;
2431 usage(PL_origargv[0]);
2435 Safefree(PL_inplace);
2436 #if defined(__CYGWIN__) /* do backup extension automagically */
2437 if (*(s+1) == '\0') {
2438 PL_inplace = savepv(".bak");
2441 #endif /* __CYGWIN__ */
2442 PL_inplace = savepv(s+1);
2444 for (s = PL_inplace; *s && !isSPACE(*s); s++) ;
2447 if (*s == '-') /* Additional switches on #! line. */
2451 case 'I': /* -I handled both here and in parse_body() */
2454 while (*s && isSPACE(*s))
2459 /* ignore trailing spaces (possibly followed by other switches) */
2461 for (e = p; *e && !isSPACE(*e); e++) ;
2465 } while (*p && *p != '-');
2466 e = savepvn(s, e-s);
2467 incpush(e, TRUE, TRUE, FALSE);
2474 Perl_croak(aTHX_ "No directory specified for -I");
2480 SvREFCNT_dec(PL_ors_sv);
2485 PL_ors_sv = newSVpvn("\n",1);
2486 numlen = 3 + (*s == '0');
2487 *SvPVX(PL_ors_sv) = (char)grok_oct(s, &numlen, &flags, NULL);
2491 if (RsPARA(PL_rs)) {
2492 PL_ors_sv = newSVpvn("\n\n",2);
2495 PL_ors_sv = newSVsv(PL_rs);
2502 PL_preambleav = newAV();
2504 SV *sv = newSVpv("use assertions::activate split(/,/,q", 0);
2505 sv_catpvn(sv, "\0", 1); /* Use NUL as q//-delimiter. */
2507 sv_catpvn(sv, "\0)", 2);
2509 av_push(PL_preambleav, sv);
2512 av_push(PL_preambleav, newSVpvn("use assertions::activate",24));
2515 forbid_setid("-M"); /* XXX ? */
2518 forbid_setid("-m"); /* XXX ? */
2523 /* -M-foo == 'no foo' */
2524 if (*s == '-') { use = "no "; ++s; }
2525 sv = newSVpv(use,0);
2527 /* We allow -M'Module qw(Foo Bar)' */
2528 while(isALNUM(*s) || *s==':') ++s;
2530 sv_catpv(sv, start);
2531 if (*(start-1) == 'm') {
2533 Perl_croak(aTHX_ "Can't use '%c' after -mname", *s);
2534 sv_catpv( sv, " ()");
2538 Perl_croak(aTHX_ "Module name required with -%c option",
2540 sv_catpvn(sv, start, s-start);
2541 sv_catpv(sv, " split(/,/,q");
2542 sv_catpvn(sv, "\0)", 1); /* Use NUL as q//-delimiter. */
2544 sv_catpvn(sv, "\0)", 2);
2548 PL_preambleav = newAV();
2549 av_push(PL_preambleav, sv);
2552 Perl_croak(aTHX_ "No space allowed after -%c", *(s-1));
2564 PL_doswitches = TRUE;
2578 #ifdef MACOS_TRADITIONAL
2579 Perl_croak(aTHX_ "Believe me, you don't want to use \"-u\" on a Macintosh");
2581 PL_do_undump = TRUE;
2590 PerlIO_printf(PerlIO_stdout(),
2591 Perl_form(aTHX_ "\nThis is perl, v%"VDf" built for %s",
2592 PL_patchlevel, ARCHNAME));
2594 /* Adjust verbose output as in the perl that ships with the DG/UX OS from EMC */
2595 PerlIO_printf(PerlIO_stdout(),
2596 Perl_form(aTHX_ "\nThis is perl, version %vd\n", PL_patchlevel));
2597 PerlIO_printf(PerlIO_stdout(),
2598 Perl_form(aTHX_ " built under %s at %s %s\n",
2599 OSNAME, __DATE__, __TIME__));
2600 PerlIO_printf(PerlIO_stdout(),
2601 Perl_form(aTHX_ " OS Specific Release: %s\n",
2605 #if defined(LOCAL_PATCH_COUNT)
2606 if (LOCAL_PATCH_COUNT > 0)
2607 PerlIO_printf(PerlIO_stdout(),
2608 "\n(with %d registered patch%s, "
2609 "see perl -V for more detail)",
2610 (int)LOCAL_PATCH_COUNT,
2611 (LOCAL_PATCH_COUNT!=1) ? "es" : "");
2614 PerlIO_printf(PerlIO_stdout(),
2615 "\n\nCopyright 1987-2003, Larry Wall\n");
2616 #ifdef MACOS_TRADITIONAL
2617 PerlIO_printf(PerlIO_stdout(),
2618 "\nMac OS port Copyright 1991-2002, Matthias Neeracher;\n"
2619 "maintained by Chris Nandor\n");
2622 PerlIO_printf(PerlIO_stdout(),
2623 "\nMS-DOS port Copyright (c) 1989, 1990, Diomidis Spinellis\n");
2626 PerlIO_printf(PerlIO_stdout(),
2627 "djgpp v2 port (jpl5003c) by Hirofumi Watanabe, 1996\n"
2628 "djgpp v2 port (perl5004+) by Laszlo Molnar, 1997-1999\n");
2631 PerlIO_printf(PerlIO_stdout(),
2632 "\n\nOS/2 port Copyright (c) 1990, 1991, Raymond Chen, Kai Uwe Rommel\n"
2633 "Version 5 port Copyright (c) 1994-2002, Andreas Kaiser, Ilya Zakharevich\n");
2636 PerlIO_printf(PerlIO_stdout(),
2637 "atariST series port, ++jrb bammi@cadence.com\n");
2640 PerlIO_printf(PerlIO_stdout(),
2641 "BeOS port Copyright Tom Spindler, 1997-1999\n");
2644 PerlIO_printf(PerlIO_stdout(),
2645 "MPE/iX port Copyright by Mark Klein and Mark Bixby, 1996-2003\n");
2648 PerlIO_printf(PerlIO_stdout(),
2649 "MVS (OS390) port by Mortice Kern Systems, 1997-1999\n");
2652 PerlIO_printf(PerlIO_stdout(),
2653 "Stratus VOS port by Paul.Green@stratus.com, 1997-2002\n");
2656 PerlIO_printf(PerlIO_stdout(),
2657 "VM/ESA port by Neale Ferguson, 1998-1999\n");
2660 PerlIO_printf(PerlIO_stdout(),
2661 "BS2000 (POSIX) port by Start Amadeus GmbH, 1998-1999\n");
2664 PerlIO_printf(PerlIO_stdout(),
2665 "MiNT port by Guido Flohr, 1997-1999\n");
2668 PerlIO_printf(PerlIO_stdout(),
2669 "EPOC port by Olaf Flebbe, 1999-2002\n");
2672 PerlIO_printf(PerlIO_stdout(),"WINCE port by Rainer Keuchel, 2001-2002\n");
2673 PerlIO_printf(PerlIO_stdout(),"Built on " __DATE__ " " __TIME__ "\n\n");
2676 #ifdef BINARY_BUILD_NOTICE
2677 BINARY_BUILD_NOTICE;
2679 PerlIO_printf(PerlIO_stdout(),
2681 Perl may be copied only under the terms of either the Artistic License or the\n\
2682 GNU General Public License, which may be found in the Perl 5 source kit.\n\n\
2683 Complete documentation for Perl, including FAQ lists, should be found on\n\
2684 this system using `man perl' or `perldoc perl'. If you have access to the\n\
2685 Internet, point your browser at http://www.perl.com/, the Perl Home Page.\n\n");
2688 if (! (PL_dowarn & G_WARN_ALL_MASK))
2689 PL_dowarn |= G_WARN_ON;
2693 PL_dowarn = G_WARN_ALL_ON|G_WARN_ON;
2694 if (!specialWARN(PL_compiling.cop_warnings))
2695 SvREFCNT_dec(PL_compiling.cop_warnings);
2696 PL_compiling.cop_warnings = pWARN_ALL ;
2700 PL_dowarn = G_WARN_ALL_OFF;
2701 if (!specialWARN(PL_compiling.cop_warnings))
2702 SvREFCNT_dec(PL_compiling.cop_warnings);
2703 PL_compiling.cop_warnings = pWARN_NONE ;
2708 if (s[1] == '-') /* Additional switches on #! line. */
2713 #if defined(WIN32) || !defined(PERL_STRICT_CR)
2719 #ifdef ALTERNATE_SHEBANG
2720 case 'S': /* OS/2 needs -S on "extproc" line. */
2728 Perl_croak(aTHX_ "Can't emulate -%.1s on #! line",s);
2733 /* compliments of Tom Christiansen */
2735 /* unexec() can be found in the Gnu emacs distribution */
2736 /* Known to work with -DUNEXEC and using unexelf.c from GNU emacs-20.2 */
2739 Perl_my_unexec(pTHX)
2747 prog = newSVpv(BIN_EXP, 0);
2748 sv_catpv(prog, "/perl");
2749 file = newSVpv(PL_origfilename, 0);
2750 sv_catpv(file, ".perldump");
2752 unexec(SvPVX(file), SvPVX(prog), &etext, sbrk(0), 0);
2753 /* unexec prints msg to stderr in case of failure */
2754 PerlProc_exit(status);
2757 # include <lib$routines.h>
2758 lib$signal(SS$_DEBUG); /* ssdef.h #included from vmsish.h */
2760 ABORT(); /* for use with undump */
2765 /* initialize curinterp */
2771 # define PERLVAR(var,type)
2772 # define PERLVARA(var,n,type)
2773 # if defined(PERL_IMPLICIT_CONTEXT)
2774 # if defined(USE_5005THREADS)
2775 # define PERLVARI(var,type,init) PERL_GET_INTERP->var = init;
2776 # define PERLVARIC(var,type,init) PERL_GET_INTERP->var = init;
2777 # else /* !USE_5005THREADS */
2778 # define PERLVARI(var,type,init) aTHX->var = init;
2779 # define PERLVARIC(var,type,init) aTHX->var = init;
2780 # endif /* USE_5005THREADS */
2782 # define PERLVARI(var,type,init) PERL_GET_INTERP->var = init;
2783 # define PERLVARIC(var,type,init) PERL_GET_INTERP->var = init;
2785 # include "intrpvar.h"
2786 # ifndef USE_5005THREADS
2787 # include "thrdvar.h"
2794 # define PERLVAR(var,type)
2795 # define PERLVARA(var,n,type)
2796 # define PERLVARI(var,type,init) PL_##var = init;
2797 # define PERLVARIC(var,type,init) PL_##var = init;
2798 # include "intrpvar.h"
2799 # ifndef USE_5005THREADS
2800 # include "thrdvar.h"
2811 S_init_main_stash(pTHX)
2815 PL_curstash = PL_defstash = newHV();
2816 PL_curstname = newSVpvn("main",4);
2817 gv = gv_fetchpv("main::",TRUE, SVt_PVHV);
2818 SvREFCNT_dec(GvHV(gv));
2819 GvHV(gv) = (HV*)SvREFCNT_inc(PL_defstash);
2821 HvNAME(PL_defstash) = savepv("main");
2822 PL_incgv = gv_HVadd(gv_AVadd(gv_fetchpv("INC",TRUE, SVt_PVAV)));
2823 GvMULTI_on(PL_incgv);
2824 PL_hintgv = gv_fetchpv("\010",TRUE, SVt_PV); /* ^H */
2825 GvMULTI_on(PL_hintgv);
2826 PL_defgv = gv_fetchpv("_",TRUE, SVt_PVAV);
2827 PL_errgv = gv_HVadd(gv_fetchpv("@", TRUE, SVt_PV));
2828 GvMULTI_on(PL_errgv);
2829 PL_replgv = gv_fetchpv("\022", TRUE, SVt_PV); /* ^R */
2830 GvMULTI_on(PL_replgv);
2831 (void)Perl_form(aTHX_ "%240s",""); /* Preallocate temp - for immediate signals. */
2832 sv_grow(ERRSV, 240); /* Preallocate - for immediate signals. */
2833 sv_setpvn(ERRSV, "", 0);
2834 PL_curstash = PL_defstash;
2835 CopSTASH_set(&PL_compiling, PL_defstash);
2836 PL_debstash = GvHV(gv_fetchpv("DB::", GV_ADDMULTI, SVt_PVHV));
2837 PL_globalstash = GvHV(gv_fetchpv("CORE::GLOBAL::", GV_ADDMULTI, SVt_PVHV));
2838 /* We must init $/ before switches are processed. */
2839 sv_setpvn(get_sv("/", TRUE), "\n", 1);
2843 S_open_script(pTHX_ char *scriptname, bool dosearch, SV *sv, int *fdscript)
2847 char *cpp_discard_flag;
2853 PL_origfilename = savepv("-e");
2856 /* if find_script() returns, it returns a malloc()-ed value */
2857 PL_origfilename = scriptname = find_script(scriptname, dosearch, NULL, 1);
2859 if (strnEQ(scriptname, "/dev/fd/", 8) && isDIGIT(scriptname[8]) ) {
2860 char *s = scriptname + 8;
2861 *fdscript = atoi(s);
2865 scriptname = savepv(s + 1);
2866 Safefree(PL_origfilename);
2867 PL_origfilename = scriptname;
2872 CopFILE_free(PL_curcop);
2873 CopFILE_set(PL_curcop, PL_origfilename);
2874 if (strEQ(PL_origfilename,"-"))
2876 if (*fdscript >= 0) {
2877 PL_rsfp = PerlIO_fdopen(*fdscript,PERL_SCRIPT_MODE);
2878 # if defined(HAS_FCNTL) && defined(F_SETFD)
2880 /* ensure close-on-exec */
2881 fcntl(PerlIO_fileno(PL_rsfp),F_SETFD,1);
2884 else if (PL_preprocess) {
2885 char *cpp_cfg = CPPSTDIN;
2886 SV *cpp = newSVpvn("",0);
2887 SV *cmd = NEWSV(0,0);
2889 if (cpp_cfg[0] == 0) /* PERL_MICRO? */
2890 Perl_croak(aTHX_ "Can't run with cpp -P with CPPSTDIN undefined");
2891 if (strEQ(cpp_cfg, "cppstdin"))
2892 Perl_sv_catpvf(aTHX_ cpp, "%s/", BIN_EXP);
2893 sv_catpv(cpp, cpp_cfg);
2896 sv_catpvn(sv, "-I", 2);
2897 sv_catpv(sv,PRIVLIB_EXP);
2900 DEBUG_P(PerlIO_printf(Perl_debug_log,
2901 "PL_preprocess: scriptname=\"%s\", cpp=\"%s\", sv=\"%s\", CPPMINUS=\"%s\"\n",
2902 scriptname, SvPVX (cpp), SvPVX (sv), CPPMINUS));
2904 # if defined(MSDOS) || defined(WIN32) || defined(VMS)
2911 cpp_discard_flag = "";
2913 cpp_discard_flag = "-C";
2917 perl = os2_execname(aTHX);
2919 perl = PL_origargv[0];
2923 /* This strips off Perl comments which might interfere with
2924 the C pre-processor, including #!. #line directives are
2925 deliberately stripped to avoid confusion with Perl's version
2926 of #line. FWP played some golf with it so it will fit
2927 into VMS's 255 character buffer.
2930 code = "(1../^#!.*perl/i)|/^\\s*#(?!\\s*((ifn?|un)def|(el|end)?if|define|include|else|error|pragma)\\b)/||!($|=1)||print";
2932 code = "/^\\s*#(?!\\s*((ifn?|un)def|(el|end)?if|define|include|else|error|pragma)\\b)/||!($|=1)||print";
2934 Perl_sv_setpvf(aTHX_ cmd, "\
2935 %s -ne%s%s%s %s | %"SVf" %s %"SVf" %s",
2936 perl, quote, code, quote, scriptname, cpp,
2937 cpp_discard_flag, sv, CPPMINUS);
2939 PL_doextract = FALSE;
2940 # ifdef IAMSUID /* actually, this is caught earlier */
2941 if (PL_euid != PL_uid && !PL_euid) { /* if running suidperl */
2943 (void)seteuid(PL_uid); /* musn't stay setuid root */
2945 # ifdef HAS_SETREUID
2946 (void)setreuid((Uid_t)-1, PL_uid);
2948 # ifdef HAS_SETRESUID
2949 (void)setresuid((Uid_t)-1, PL_uid, (Uid_t)-1);
2951 PerlProc_setuid(PL_uid);
2955 if (PerlProc_geteuid() != PL_uid)
2956 Perl_croak(aTHX_ "Can't do seteuid!\n");
2958 # endif /* IAMSUID */
2960 DEBUG_P(PerlIO_printf(Perl_debug_log,
2961 "PL_preprocess: cmd=\"%s\"\n",
2964 PL_rsfp = PerlProc_popen(SvPVX(cmd), "r");
2968 else if (!*scriptname) {
2969 forbid_setid("program input from stdin");
2970 PL_rsfp = PerlIO_stdin();
2973 PL_rsfp = PerlIO_open(scriptname,PERL_SCRIPT_MODE);
2974 # if defined(HAS_FCNTL) && defined(F_SETFD)
2976 /* ensure close-on-exec */
2977 fcntl(PerlIO_fileno(PL_rsfp),F_SETFD,1);
2982 # ifndef IAMSUID /* in case script is not readable before setuid */
2984 PerlLIO_stat(CopFILE(PL_curcop),&PL_statbuf) >= 0 &&
2985 PL_statbuf.st_mode & (S_ISUID|S_ISGID))
2989 PerlProc_execv(Perl_form(aTHX_ "%s/sperl"PERL_FS_VER_FMT,
2990 BIN_EXP, (int)PERL_REVISION,
2992 (int)PERL_SUBVERSION), PL_origargv);
2994 Perl_croak(aTHX_ "Can't do setuid\n");
3000 Perl_croak(aTHX_ "Permission denied\n");
3002 Perl_croak(aTHX_ "Can't open perl script \"%s\": %s\n",
3003 CopFILE(PL_curcop), Strerror(errno));
3009 * I_SYSSTATVFS HAS_FSTATVFS
3011 * I_STATFS HAS_FSTATFS HAS_GETFSSTAT
3012 * I_MNTENT HAS_GETMNTENT HAS_HASMNTOPT
3013 * here so that metaconfig picks them up. */
3017 S_fd_on_nosuid_fs(pTHX_ int fd)
3019 int check_okay = 0; /* able to do all the required sys/libcalls */
3020 int on_nosuid = 0; /* the fd is on a nosuid fs */
3022 * Preferred order: fstatvfs(), fstatfs(), ustat()+getmnt(), getmntent().
3023 * fstatvfs() is UNIX98.
3024 * fstatfs() is 4.3 BSD.
3025 * ustat()+getmnt() is pre-4.3 BSD.
3026 * getmntent() is O(number-of-mounted-filesystems) and can hang on
3027 * an irrelevant filesystem while trying to reach the right one.
3030 #undef FD_ON_NOSUID_CHECK_OKAY /* found the syscalls to do the check? */
3032 # if !defined(FD_ON_NOSUID_CHECK_OKAY) && \
3033 defined(HAS_FSTATVFS)
3034 # define FD_ON_NOSUID_CHECK_OKAY
3035 struct statvfs stfs;
3037 check_okay = fstatvfs(fd, &stfs) == 0;
3038 on_nosuid = check_okay && (stfs.f_flag & ST_NOSUID);
3039 # endif /* fstatvfs */
3041 # if !defined(FD_ON_NOSUID_CHECK_OKAY) && \
3042 defined(PERL_MOUNT_NOSUID) && \
3043 defined(HAS_FSTATFS) && \
3044 defined(HAS_STRUCT_STATFS) && \
3045 defined(HAS_STRUCT_STATFS_F_FLAGS)
3046 # define FD_ON_NOSUID_CHECK_OKAY
3049 check_okay = fstatfs(fd, &stfs) == 0;
3050 on_nosuid = check_okay && (stfs.f_flags & PERL_MOUNT_NOSUID);
3051 # endif /* fstatfs */
3053 # if !defined(FD_ON_NOSUID_CHECK_OKAY) && \
3054 defined(PERL_MOUNT_NOSUID) && \
3055 defined(HAS_FSTAT) && \
3056 defined(HAS_USTAT) && \
3057 defined(HAS_GETMNT) && \
3058 defined(HAS_STRUCT_FS_DATA) && \
3060 # define FD_ON_NOSUID_CHECK_OKAY
3063 if (fstat(fd, &fdst) == 0) {
3065 if (ustat(fdst.st_dev, &us) == 0) {
3067 /* NOSTAT_ONE here because we're not examining fields which
3068 * vary between that case and STAT_ONE. */
3069 if (getmnt((int*)0, &fsd, (int)0, NOSTAT_ONE, us.f_fname) == 0) {
3070 size_t cmplen = sizeof(us.f_fname);
3071 if (sizeof(fsd.fd_req.path) < cmplen)
3072 cmplen = sizeof(fsd.fd_req.path);
3073 if (strnEQ(fsd.fd_req.path, us.f_fname, cmplen) &&
3074 fdst.st_dev == fsd.fd_req.dev) {
3076 on_nosuid = fsd.fd_req.flags & PERL_MOUNT_NOSUID;
3082 # endif /* fstat+ustat+getmnt */
3084 # if !defined(FD_ON_NOSUID_CHECK_OKAY) && \
3085 defined(HAS_GETMNTENT) && \
3086 defined(HAS_HASMNTOPT) && \
3087 defined(MNTOPT_NOSUID)
3088 # define FD_ON_NOSUID_CHECK_OKAY
3089 FILE *mtab = fopen("/etc/mtab", "r");
3090 struct mntent *entry;
3093 if (mtab && (fstat(fd, &stb) == 0)) {
3094 while (entry = getmntent(mtab)) {
3095 if (stat(entry->mnt_dir, &fsb) == 0
3096 && fsb.st_dev == stb.st_dev)
3098 /* found the filesystem */
3100 if (hasmntopt(entry, MNTOPT_NOSUID))
3103 } /* A single fs may well fail its stat(). */
3108 # endif /* getmntent+hasmntopt */
3111 Perl_croak(aTHX_ "Can't check filesystem of script \"%s\" for nosuid", PL_origfilename);
3114 #endif /* IAMSUID */
3117 S_validate_suid(pTHX_ char *validarg, char *scriptname, int fdscript)
3123 /* do we need to emulate setuid on scripts? */
3125 /* This code is for those BSD systems that have setuid #! scripts disabled
3126 * in the kernel because of a security problem. Merely defining DOSUID
3127 * in perl will not fix that problem, but if you have disabled setuid
3128 * scripts in the kernel, this will attempt to emulate setuid and setgid
3129 * on scripts that have those now-otherwise-useless bits set. The setuid
3130 * root version must be called suidperl or sperlN.NNN. If regular perl
3131 * discovers that it has opened a setuid script, it calls suidperl with
3132 * the same argv that it had. If suidperl finds that the script it has
3133 * just opened is NOT setuid root, it sets the effective uid back to the
3134 * uid. We don't just make perl setuid root because that loses the
3135 * effective uid we had before invoking perl, if it was different from the
3138 * DOSUID must be defined in both perl and suidperl, and IAMSUID must
3139 * be defined in suidperl only. suidperl must be setuid root. The
3140 * Configure script will set this up for you if you want it.
3146 if (PerlLIO_fstat(PerlIO_fileno(PL_rsfp),&PL_statbuf) < 0) /* normal stat is insecure */
3147 Perl_croak(aTHX_ "Can't stat script \"%s\"",PL_origfilename);
3148 if (fdscript < 0 && PL_statbuf.st_mode & (S_ISUID|S_ISGID)) {
3153 #ifndef HAS_SETREUID
3154 /* On this access check to make sure the directories are readable,
3155 * there is actually a small window that the user could use to make
3156 * filename point to an accessible directory. So there is a faint
3157 * chance that someone could execute a setuid script down in a
3158 * non-accessible directory. I don't know what to do about that.
3159 * But I don't think it's too important. The manual lies when
3160 * it says access() is useful in setuid programs.
3162 if (PerlLIO_access(CopFILE(PL_curcop),1)) { /*double check*/
3164 Perl_croak(aTHX_ "Permission denied\n");
3167 /* If we can swap euid and uid, then we can determine access rights
3168 * with a simple stat of the file, and then compare device and
3169 * inode to make sure we did stat() on the same file we opened.
3170 * Then we just have to make sure he or she can execute it.
3177 setreuid(PL_euid,PL_uid) < 0
3180 setresuid(PL_euid,PL_uid,(Uid_t)-1) < 0
3183 || PerlProc_getuid() != PL_euid || PerlProc_geteuid() != PL_uid)
3184 Perl_croak(aTHX_ "Can't swap uid and euid"); /* really paranoid */
3185 if (PerlLIO_stat(CopFILE(PL_curcop),&tmpstatbuf) < 0) {
3187 Perl_croak(aTHX_ "Permission denied\n"); /* testing full pathname here */
3189 #if defined(IAMSUID) && !defined(NO_NOSUID_CHECK)
3190 if (fd_on_nosuid_fs(PerlIO_fileno(PL_rsfp))) {
3192 Perl_croak(aTHX_ "Permission denied\n");
3195 if (tmpstatbuf.st_dev != PL_statbuf.st_dev ||
3196 tmpstatbuf.st_ino != PL_statbuf.st_ino) {
3197 (void)PerlIO_close(PL_rsfp);
3199 Perl_croak(aTHX_ "Permission denied\n");
3203 setreuid(PL_uid,PL_euid) < 0
3205 # if defined(HAS_SETRESUID)
3206 setresuid(PL_uid,PL_euid,(Uid_t)-1) < 0
3209 || PerlProc_getuid() != PL_uid || PerlProc_geteuid() != PL_euid)
3210 Perl_croak(aTHX_ "Can't reswap uid and euid");
3211 if (!cando(S_IXUSR,FALSE,&PL_statbuf)) /* can real uid exec? */
3212 Perl_croak(aTHX_ "Permission denied\n");
3214 #endif /* HAS_SETREUID */
3215 #endif /* IAMSUID */
3217 if (!S_ISREG(PL_statbuf.st_mode)) {
3219 Perl_croak(aTHX_ "Permission denied\n");
3221 if (PL_statbuf.st_mode & S_IWOTH)
3222 Perl_croak(aTHX_ "Setuid/gid script is writable by world");
3223 PL_doswitches = FALSE; /* -s is insecure in suid */
3224 CopLINE_inc(PL_curcop);
3225 if (sv_gets(PL_linestr, PL_rsfp, 0) == Nullch ||
3226 strnNE(SvPV(PL_linestr,n_a),"#!",2) ) /* required even on Sys V */
3227 Perl_croak(aTHX_ "No #! line");
3228 s = SvPV(PL_linestr,n_a)+2;
3230 while (!isSPACE(*s)) s++;
3231 for (s2 = s; (s2 > SvPV(PL_linestr,n_a)+2 &&
3232 (isDIGIT(s2[-1]) || strchr("._-", s2[-1]))); s2--) ;
3233 if (strnNE(s2-4,"perl",4) && strnNE(s-9,"perl",4)) /* sanity check */
3234 Perl_croak(aTHX_ "Not a perl script");
3235 while (*s == ' ' || *s == '\t') s++;
3237 * #! arg must be what we saw above. They can invoke it by
3238 * mentioning suidperl explicitly, but they may not add any strange
3239 * arguments beyond what #! says if they do invoke suidperl that way.
3241 len = strlen(validarg);
3242 if (strEQ(validarg," PHOOEY ") ||
3243 strnNE(s,validarg,len) || !isSPACE(s[len]))
3244 Perl_croak(aTHX_ "Args must match #! line");
3247 if (PL_euid != PL_uid && (PL_statbuf.st_mode & S_ISUID) &&
3248 PL_euid == PL_statbuf.st_uid)
3250 Perl_croak(aTHX_ "YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
3251 FIX YOUR KERNEL, OR PUT A C WRAPPER AROUND THIS SCRIPT!\n");
3252 #endif /* IAMSUID */
3254 if (PL_euid) { /* oops, we're not the setuid root perl */
3255 (void)PerlIO_close(PL_rsfp);
3259 PerlProc_execv(Perl_form(aTHX_ "%s/sperl"PERL_FS_VER_FMT, BIN_EXP,
3260 (int)PERL_REVISION, (int)PERL_VERSION,
3261 (int)PERL_SUBVERSION), PL_origargv);
3264 Perl_croak(aTHX_ "Can't do setuid\n");
3267 if (PL_statbuf.st_mode & S_ISGID && PL_statbuf.st_gid != PL_egid) {
3269 (void)setegid(PL_statbuf.st_gid);
3272 (void)setregid((Gid_t)-1,PL_statbuf.st_gid);
3274 #ifdef HAS_SETRESGID
3275 (void)setresgid((Gid_t)-1,PL_statbuf.st_gid,(Gid_t)-1);
3277 PerlProc_setgid(PL_statbuf.st_gid);
3281 if (PerlProc_getegid() != PL_statbuf.st_gid)
3282 Perl_croak(aTHX_ "Can't do setegid!\n");
3284 if (PL_statbuf.st_mode & S_ISUID) {
3285 if (PL_statbuf.st_uid != PL_euid)
3287 (void)seteuid(PL_statbuf.st_uid); /* all that for this */
3290 (void)setreuid((Uid_t)-1,PL_statbuf.st_uid);
3292 #ifdef HAS_SETRESUID
3293 (void)setresuid((Uid_t)-1,PL_statbuf.st_uid,(Uid_t)-1);
3295 PerlProc_setuid(PL_statbuf.st_uid);
3299 if (PerlProc_geteuid() != PL_statbuf.st_uid)
3300 Perl_croak(aTHX_ "Can't do seteuid!\n");
3302 else if (PL_uid) { /* oops, mustn't run as root */
3304 (void)seteuid((Uid_t)PL_uid);
3307 (void)setreuid((Uid_t)-1,(Uid_t)PL_uid);
3309 #ifdef HAS_SETRESUID
3310 (void)setresuid((Uid_t)-1,(Uid_t)PL_uid,(Uid_t)-1);
3312 PerlProc_setuid((Uid_t)PL_uid);
3316 if (PerlProc_geteuid() != PL_uid)
3317 Perl_croak(aTHX_ "Can't do seteuid!\n");
3320 if (!cando(S_IXUSR,TRUE,&PL_statbuf))
3321 Perl_croak(aTHX_ "Permission denied\n"); /* they can't do this */
3324 else if (PL_preprocess)
3325 Perl_croak(aTHX_ "-P not allowed for setuid/setgid script\n");
3326 else if (fdscript >= 0)
3327 Perl_croak(aTHX_ "fd script not allowed in suidperl\n");
3330 Perl_croak(aTHX_ "Permission denied\n");
3333 /* We absolutely must clear out any saved ids here, so we */
3334 /* exec the real perl, substituting fd script for scriptname. */
3335 /* (We pass script name as "subdir" of fd, which perl will grok.) */
3336 PerlIO_rewind(PL_rsfp);
3337 PerlLIO_lseek(PerlIO_fileno(PL_rsfp),(Off_t)0,0); /* just in case rewind didn't */
3338 for (which = 1; PL_origargv[which] && PL_origargv[which] != scriptname; which++) ;
3339 if (!PL_origargv[which]) {
3341 Perl_croak(aTHX_ "Permission denied\n");
3343 PL_origargv[which] = savepv(Perl_form(aTHX_ "/dev/fd/%d/%s",
3344 PerlIO_fileno(PL_rsfp), PL_origargv[which]));
3345 #if defined(HAS_FCNTL) && defined(F_SETFD)
3346 fcntl(PerlIO_fileno(PL_rsfp),F_SETFD,0); /* ensure no close-on-exec */
3349 PerlProc_execv(Perl_form(aTHX_ "%s/perl"PERL_FS_VER_FMT, BIN_EXP,
3350 (int)PERL_REVISION, (int)PERL_VERSION,
3351 (int)PERL_SUBVERSION), PL_origargv);/* try again */
3353 Perl_croak(aTHX_ "Can't do setuid\n");
3354 #endif /* IAMSUID */
3356 if (PL_euid != PL_uid || PL_egid != PL_gid) { /* (suidperl doesn't exist, in fact) */
3357 #ifndef SETUID_SCRIPTS_ARE_SECURE_NOW
3358 PerlLIO_fstat(PerlIO_fileno(PL_rsfp),&PL_statbuf); /* may be either wrapped or real suid */
3359 if ((PL_euid != PL_uid && PL_euid == PL_statbuf.st_uid && PL_statbuf.st_mode & S_ISUID)
3361 (PL_egid != PL_gid && PL_egid == PL_statbuf.st_gid && PL_statbuf.st_mode & S_ISGID)
3364 Perl_croak(aTHX_ "YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
3365 FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
3366 #endif /* SETUID_SCRIPTS_ARE_SECURE_NOW */
3367 /* not set-id, must be wrapped */
3373 S_find_beginning(pTHX)
3375 register char *s, *s2;
3376 #ifdef MACOS_TRADITIONAL
3380 /* skip forward in input to the real script? */
3383 #ifdef MACOS_TRADITIONAL
3384 /* Since the Mac OS does not honor #! arguments for us, we do it ourselves */
3386 while (PL_doextract || gMacPerl_AlwaysExtract) {
3387 if ((s = sv_gets(PL_linestr, PL_rsfp, 0)) == Nullch) {
3388 if (!gMacPerl_AlwaysExtract)
3389 Perl_croak(aTHX_ "No Perl script found in input\n");
3391 if (PL_doextract) /* require explicit override ? */
3392 if (!OverrideExtract(PL_origfilename))
3393 Perl_croak(aTHX_ "User aborted script\n");
3395 PL_doextract = FALSE;
3397 /* Pater peccavi, file does not have #! */
3398 PerlIO_rewind(PL_rsfp);
3403 while (PL_doextract) {
3404 if ((s = sv_gets(PL_linestr, PL_rsfp, 0)) == Nullch)
3405 Perl_croak(aTHX_ "No Perl script found in input\n");
3408 if (*s == '#' && s[1] == '!' && ((s = instr(s,"perl")) || (s = instr(s2,"PERL")))) {
3409 PerlIO_ungetc(PL_rsfp, '\n'); /* to keep line count right */
3410 PL_doextract = FALSE;
3411 while (*s && !(isSPACE (*s) || *s == '#')) s++;
3413 while (*s == ' ' || *s == '\t') s++;
3415 while (isDIGIT(s2[-1]) || strchr("-._", s2[-1])) s2--;
3416 if (strnEQ(s2-4,"perl",4))
3418 while ((s = moreswitches(s)))
3421 #ifdef MACOS_TRADITIONAL
3422 /* We are always searching for the #!perl line in MacPerl,
3423 * so if we find it, still keep the line count correct
3424 * by counting lines we already skipped over
3426 for (; maclines > 0 ; maclines--)
3427 PerlIO_ungetc(PL_rsfp, '\n');
3431 /* gMacPerl_AlwaysExtract is false in MPW tool */
3432 } else if (gMacPerl_AlwaysExtract) {
3443 PL_uid = PerlProc_getuid();
3444 PL_euid = PerlProc_geteuid();
3445 PL_gid = PerlProc_getgid();
3446 PL_egid = PerlProc_getegid();
3448 PL_uid |= PL_gid << 16;
3449 PL_euid |= PL_egid << 16;
3451 /* Should not happen: */
3452 CHECK_MALLOC_TAINT(PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
3453 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
3456 /* This is used very early in the lifetime of the program,
3457 * before even the options are parsed, so PL_tainting has
3458 * not been initialized properly. */
3460 Perl_doing_taint(int argc, char *argv[], char *envp[])
3462 #ifndef PERL_IMPLICIT_SYS
3463 /* If we have PERL_IMPLICIT_SYS we can't call getuid() et alia
3464 * before we have an interpreter-- and the whole point of this
3465 * function is to be called at such an early stage. If you are on
3466 * a system with PERL_IMPLICIT_SYS but you do have a concept of
3467 * "tainted because running with altered effective ids', you'll
3468 * have to add your own checks somewhere in here. The two most
3469 * known samples of 'implicitness' are Win32 and NetWare, neither
3470 * of which has much of concept of 'uids'. */
3471 int uid = PerlProc_getuid();
3472 int euid = PerlProc_geteuid();
3473 int gid = PerlProc_getgid();
3474 int egid = PerlProc_getegid();
3480 if (uid && (euid != uid || egid != gid))
3482 #endif /* !PERL_IMPLICIT_SYS */
3483 /* This is a really primitive check; environment gets ignored only
3484 * if -T are the first chars together; otherwise one gets
3485 * "Too late" message. */
3486 if ( argc > 1 && argv[1][0] == '-'
3487 && (argv[1][1] == 't' || argv[1][1] == 'T') )
3493 S_forbid_setid(pTHX_ char *s)
3495 if (PL_euid != PL_uid)
3496 Perl_croak(aTHX_ "No %s allowed while running setuid", s);
3497 if (PL_egid != PL_gid)
3498 Perl_croak(aTHX_ "No %s allowed while running setgid", s);
3502 Perl_init_debugger(pTHX)
3504 HV *ostash = PL_curstash;
3506 PL_curstash = PL_debstash;
3507 PL_dbargs = GvAV(gv_AVadd((gv_fetchpv("DB::args", GV_ADDMULTI, SVt_PVAV))));
3508 AvREAL_off(PL_dbargs);
3509 PL_DBgv = gv_fetchpv("DB::DB", GV_ADDMULTI, SVt_PVGV);
3510 PL_DBline = gv_fetchpv("DB::dbline", GV_ADDMULTI, SVt_PVAV);
3511 PL_DBsub = gv_HVadd(gv_fetchpv("DB::sub", GV_ADDMULTI, SVt_PVHV));
3512 sv_upgrade(GvSV(PL_DBsub), SVt_IV); /* IVX accessed if PERLDB_SUB_NN */
3513 PL_DBsingle = GvSV((gv_fetchpv("DB::single", GV_ADDMULTI, SVt_PV)));
3514 sv_setiv(PL_DBsingle, 0);
3515 PL_DBtrace = GvSV((gv_fetchpv("DB::trace", GV_ADDMULTI, SVt_PV)));
3516 sv_setiv(PL_DBtrace, 0);
3517 PL_DBsignal = GvSV((gv_fetchpv("DB::signal", GV_ADDMULTI, SVt_PV)));
3518 sv_setiv(PL_DBsignal, 0);
3519 PL_DBassertion = GvSV((gv_fetchpv("assertion", GV_ADDMULTI, SVt_PV)));
3520 sv_setiv(PL_DBassertion, 0);
3521 PL_curstash = ostash;
3524 #ifndef STRESS_REALLOC
3525 #define REASONABLE(size) (size)
3527 #define REASONABLE(size) (1) /* unreasonable */
3531 Perl_init_stacks(pTHX)
3533 /* start with 128-item stack and 8K cxstack */
3534 PL_curstackinfo = new_stackinfo(REASONABLE(128),
3535 REASONABLE(8192/sizeof(PERL_CONTEXT) - 1));
3536 PL_curstackinfo->si_type = PERLSI_MAIN;
3537 PL_curstack = PL_curstackinfo->si_stack;
3538 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
3540 PL_stack_base = AvARRAY(PL_curstack);
3541 PL_stack_sp = PL_stack_base;
3542 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
3544 New(50,PL_tmps_stack,REASONABLE(128),SV*);
3547 PL_tmps_max = REASONABLE(128);
3549 New(54,PL_markstack,REASONABLE(32),I32);
3550 PL_markstack_ptr = PL_markstack;
3551 PL_markstack_max = PL_markstack + REASONABLE(32);
3555 New(54,PL_scopestack,REASONABLE(32),I32);
3556 PL_scopestack_ix = 0;
3557 PL_scopestack_max = REASONABLE(32);
3559 New(54,PL_savestack,REASONABLE(128),ANY);
3560 PL_savestack_ix = 0;
3561 PL_savestack_max = REASONABLE(128);
3563 New(54,PL_retstack,REASONABLE(16),OP*);
3565 PL_retstack_max = REASONABLE(16);
3573 while (PL_curstackinfo->si_next)
3574 PL_curstackinfo = PL_curstackinfo->si_next;
3575 while (PL_curstackinfo) {
3576 PERL_SI *p = PL_curstackinfo->si_prev;
3577 /* curstackinfo->si_stack got nuked by sv_free_arenas() */
3578 Safefree(PL_curstackinfo->si_cxstack);
3579 Safefree(PL_curstackinfo);
3580 PL_curstackinfo = p;
3582 Safefree(PL_tmps_stack);
3583 Safefree(PL_markstack);
3584 Safefree(PL_scopestack);
3585 Safefree(PL_savestack);
3586 Safefree(PL_retstack);
3595 lex_start(PL_linestr);
3597 PL_subname = newSVpvn("main",4);
3601 S_init_predump_symbols(pTHX)
3606 sv_setpvn(get_sv("\"", TRUE), " ", 1);
3607 PL_stdingv = gv_fetchpv("STDIN",TRUE, SVt_PVIO);
3608 GvMULTI_on(PL_stdingv);
3609 io = GvIOp(PL_stdingv);
3610 IoTYPE(io) = IoTYPE_RDONLY;
3611 IoIFP(io) = PerlIO_stdin();
3612 tmpgv = gv_fetchpv("stdin",TRUE, SVt_PV);
3614 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(io);
3616 tmpgv = gv_fetchpv("STDOUT",TRUE, SVt_PVIO);
3619 IoTYPE(io) = IoTYPE_WRONLY;
3620 IoOFP(io) = IoIFP(io) = PerlIO_stdout();
3622 tmpgv = gv_fetchpv("stdout",TRUE, SVt_PV);
3624 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(io);
3626 PL_stderrgv = gv_fetchpv("STDERR",TRUE, SVt_PVIO);
3627 GvMULTI_on(PL_stderrgv);
3628 io = GvIOp(PL_stderrgv);
3629 IoTYPE(io) = IoTYPE_WRONLY;
3630 IoOFP(io) = IoIFP(io) = PerlIO_stderr();
3631 tmpgv = gv_fetchpv("stderr",TRUE, SVt_PV);
3633 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(io);
3635 PL_statname = NEWSV(66,0); /* last filename we did stat on */
3638 Safefree(PL_osname);
3639 PL_osname = savepv(OSNAME);
3643 Perl_init_argv_symbols(pTHX_ register int argc, register char **argv)
3646 argc--,argv++; /* skip name of script */
3647 if (PL_doswitches) {
3648 for (; argc > 0 && **argv == '-'; argc--,argv++) {
3651 if (argv[0][1] == '-' && !argv[0][2]) {
3655 if ((s = strchr(argv[0], '='))) {
3657 sv_setpv(GvSV(gv_fetchpv(argv[0]+1,TRUE, SVt_PV)),s);
3660 sv_setiv(GvSV(gv_fetchpv(argv[0]+1,TRUE, SVt_PV)),1);
3663 if ((PL_argvgv = gv_fetchpv("ARGV",TRUE, SVt_PVAV))) {
3664 GvMULTI_on(PL_argvgv);
3665 (void)gv_AVadd(PL_argvgv);
3666 av_clear(GvAVn(PL_argvgv));
3667 for (; argc > 0; argc--,argv++) {
3668 SV *sv = newSVpv(argv[0],0);
3669 av_push(GvAVn(PL_argvgv),sv);
3670 if (!(PL_unicode & PERL_UNICODE_LOCALE_FLAG) || PL_utf8locale) {
3671 if (PL_unicode & PERL_UNICODE_ARGV_FLAG)
3674 if (PL_unicode & PERL_UNICODE_WIDESYSCALLS_FLAG) /* Sarathy? */
3675 (void)sv_utf8_decode(sv);
3680 #ifdef HAS_PROCSELFEXE
3681 /* This is a function so that we don't hold on to MAXPATHLEN
3682 bytes of stack longer than necessary
3685 S_procself_val(pTHX_ SV *sv, char *arg0)
3687 char buf[MAXPATHLEN];
3688 int len = readlink(PROCSELFEXE_PATH, buf, sizeof(buf) - 1);
3690 /* On Playstation2 Linux V1.0 (kernel 2.2.1) readlink(/proc/self/exe)
3691 includes a spurious NUL which will cause $^X to fail in system
3692 or backticks (this will prevent extensions from being built and
3693 many tests from working). readlink is not meant to add a NUL.
3694 Normal readlink works fine.
3696 if (len > 0 && buf[len-1] == '\0') {
3700 /* FreeBSD's implementation is acknowledged to be imperfect, sometimes
3701 returning the text "unknown" from the readlink rather than the path
3702 to the executable (or returning an error from the readlink). Any valid
3703 path has a '/' in it somewhere, so use that to validate the result.
3704 See http://www.freebsd.org/cgi/query-pr.cgi?pr=35703
3706 if (len > 0 && memchr(buf, '/', len)) {
3707 sv_setpvn(sv,buf,len);
3713 #endif /* HAS_PROCSELFEXE */
3716 S_init_postdump_symbols(pTHX_ register int argc, register char **argv, register char **env)
3722 PL_toptarget = NEWSV(0,0);
3723 sv_upgrade(PL_toptarget, SVt_PVFM);
3724 sv_setpvn(PL_toptarget, "", 0);
3725 PL_bodytarget = NEWSV(0,0);
3726 sv_upgrade(PL_bodytarget, SVt_PVFM);
3727 sv_setpvn(PL_bodytarget, "", 0);
3728 PL_formtarget = PL_bodytarget;
3732 init_argv_symbols(argc,argv);
3734 if ((tmpgv = gv_fetchpv("0",TRUE, SVt_PV))) {
3735 #ifdef MACOS_TRADITIONAL
3736 /* $0 is not majick on a Mac */
3737 sv_setpv(GvSV(tmpgv),MacPerl_MPWFileName(PL_origfilename));
3739 sv_setpv(GvSV(tmpgv),PL_origfilename);
3740 magicname("0", "0", 1);
3743 if ((tmpgv = gv_fetchpv("\030",TRUE, SVt_PV))) {/* $^X */
3744 #ifdef HAS_PROCSELFEXE
3745 S_procself_val(aTHX_ GvSV(tmpgv), PL_origargv[0]);
3748 sv_setpv(GvSV(tmpgv), os2_execname(aTHX));
3750 sv_setpv(GvSV(tmpgv),PL_origargv[0]);
3754 if ((PL_envgv = gv_fetchpv("ENV",TRUE, SVt_PVHV))) {
3756 GvMULTI_on(PL_envgv);
3757 hv = GvHVn(PL_envgv);
3758 hv_magic(hv, Nullgv, PERL_MAGIC_env);
3760 #ifdef USE_ENVIRON_ARRAY
3761 /* Note that if the supplied env parameter is actually a copy
3762 of the global environ then it may now point to free'd memory
3763 if the environment has been modified since. To avoid this
3764 problem we treat env==NULL as meaning 'use the default'
3769 # ifdef USE_ITHREADS
3770 && PL_curinterp == aTHX
3774 environ[0] = Nullch;
3777 for (; *env; env++) {
3778 if (!(s = strchr(*env,'=')))
3780 #if defined(MSDOS) && !defined(DJGPP)
3785 sv = newSVpv(s+1, 0);
3786 (void)hv_store(hv, *env, s - *env, sv, 0);
3790 #endif /* USE_ENVIRON_ARRAY */
3791 #endif /* !PERL_MICRO */
3794 if ((tmpgv = gv_fetchpv("$",TRUE, SVt_PV))) {
3795 SvREADONLY_off(GvSV(tmpgv));
3796 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3797 SvREADONLY_on(GvSV(tmpgv));
3799 #ifdef THREADS_HAVE_PIDS
3800 PL_ppid = (IV)getppid();
3803 /* touch @F array to prevent spurious warnings 20020415 MJD */
3805 (void) get_av("main::F", TRUE | GV_ADDMULTI);
3807 /* touch @- and @+ arrays to prevent spurious warnings 20020415 MJD */
3808 (void) get_av("main::-", TRUE | GV_ADDMULTI);
3809 (void) get_av("main::+", TRUE | GV_ADDMULTI);
3813 S_init_perllib(pTHX)
3818 s = PerlEnv_getenv("PERL5LIB");
3820 incpush(s, TRUE, TRUE, TRUE);
3822 incpush(PerlEnv_getenv("PERLLIB"), FALSE, FALSE, TRUE);
3824 /* Treat PERL5?LIB as a possible search list logical name -- the
3825 * "natural" VMS idiom for a Unix path string. We allow each
3826 * element to be a set of |-separated directories for compatibility.
3830 if (my_trnlnm("PERL5LIB",buf,0))
3831 do { incpush(buf,TRUE,TRUE,TRUE); } while (my_trnlnm("PERL5LIB",buf,++idx));
3833 while (my_trnlnm("PERLLIB",buf,idx++)) incpush(buf,FALSE,FALSE,TRUE);
3837 /* Use the ~-expanded versions of APPLLIB (undocumented),
3838 ARCHLIB PRIVLIB SITEARCH SITELIB VENDORARCH and VENDORLIB
3841 incpush(APPLLIB_EXP, TRUE, TRUE, TRUE);
3845 incpush(ARCHLIB_EXP, FALSE, FALSE, TRUE);
3847 #ifdef MACOS_TRADITIONAL
3850 SV * privdir = NEWSV(55, 0);
3851 char * macperl = PerlEnv_getenv("MACPERL");
3856 Perl_sv_setpvf(aTHX_ privdir, "%slib:", macperl);
3857 if (PerlLIO_stat(SvPVX(privdir), &tmpstatbuf) >= 0 && S_ISDIR(tmpstatbuf.st_mode))
3858 incpush(SvPVX(privdir), TRUE, FALSE, TRUE);
3859 Perl_sv_setpvf(aTHX_ privdir, "%ssite_perl:", macperl);
3860 if (PerlLIO_stat(SvPVX(privdir), &tmpstatbuf) >= 0 && S_ISDIR(tmpstatbuf.st_mode))
3861 incpush(SvPVX(privdir), TRUE, FALSE, TRUE);
3863 SvREFCNT_dec(privdir);
3866 incpush(":", FALSE, FALSE, TRUE);
3869 # define PRIVLIB_EXP "/usr/local/lib/perl5:/usr/local/lib/perl"
3872 incpush(PRIVLIB_EXP, TRUE, FALSE, TRUE);
3874 incpush(PRIVLIB_EXP, FALSE, FALSE, TRUE);
3878 /* sitearch is always relative to sitelib on Windows for
3879 * DLL-based path intuition to work correctly */
3880 # if !defined(WIN32)
3881 incpush(SITEARCH_EXP, FALSE, FALSE, TRUE);
3887 /* this picks up sitearch as well */
3888 incpush(SITELIB_EXP, TRUE, FALSE, TRUE);
3890 incpush(SITELIB_EXP, FALSE, FALSE, TRUE);
3894 #ifdef SITELIB_STEM /* Search for version-specific dirs below here */
3895 incpush(SITELIB_STEM, FALSE, TRUE, TRUE);
3898 #ifdef PERL_VENDORARCH_EXP
3899 /* vendorarch is always relative to vendorlib on Windows for
3900 * DLL-based path intuition to work correctly */
3901 # if !defined(WIN32)
3902 incpush(PERL_VENDORARCH_EXP, FALSE, FALSE, TRUE);
3906 #ifdef PERL_VENDORLIB_EXP
3908 incpush(PERL_VENDORLIB_EXP, TRUE, FALSE, TRUE); /* this picks up vendorarch as well */
3910 incpush(PERL_VENDORLIB_EXP, FALSE, FALSE, TRUE);
3914 #ifdef PERL_VENDORLIB_STEM /* Search for version-specific dirs below here */
3915 incpush(PERL_VENDORLIB_STEM, FALSE, TRUE, TRUE);
3918 #ifdef PERL_OTHERLIBDIRS
3919 incpush(PERL_OTHERLIBDIRS, TRUE, TRUE, TRUE);
3923 incpush(".", FALSE, FALSE, TRUE);
3924 #endif /* MACOS_TRADITIONAL */
3927 #if defined(DOSISH) || defined(EPOC)
3928 # define PERLLIB_SEP ';'
3931 # define PERLLIB_SEP '|'
3933 # if defined(MACOS_TRADITIONAL)
3934 # define PERLLIB_SEP ','
3936 # define PERLLIB_SEP ':'
3940 #ifndef PERLLIB_MANGLE
3941 # define PERLLIB_MANGLE(s,n) (s)
3945 S_incpush(pTHX_ char *p, int addsubdirs, int addoldvers, int usesep)
3947 SV *subdir = Nullsv;
3952 if (addsubdirs || addoldvers) {
3953 subdir = sv_newmortal();
3956 /* Break at all separators */
3958 SV *libdir = NEWSV(55,0);
3961 /* skip any consecutive separators */
3963 while ( *p == PERLLIB_SEP ) {
3964 /* Uncomment the next line for PATH semantics */
3965 /* av_push(GvAVn(PL_incgv), newSVpvn(".", 1)); */
3970 if ( usesep && (s = strchr(p, PERLLIB_SEP)) != Nullch ) {
3971 sv_setpvn(libdir, PERLLIB_MANGLE(p, (STRLEN)(s - p)),
3976 sv_setpv(libdir, PERLLIB_MANGLE(p, 0));
3977 p = Nullch; /* break out */
3979 #ifdef MACOS_TRADITIONAL
3980 if (!strchr(SvPVX(libdir), ':')) {
3983 sv_setpv(libdir, MacPerl_CanonDir(SvPVX(libdir), buf, 0));
3985 if (SvPVX(libdir)[SvCUR(libdir)-1] != ':')
3986 sv_catpv(libdir, ":");
3990 * BEFORE pushing libdir onto @INC we may first push version- and
3991 * archname-specific sub-directories.
3993 if (addsubdirs || addoldvers) {
3994 #ifdef PERL_INC_VERSION_LIST
3995 /* Configure terminates PERL_INC_VERSION_LIST with a NULL */
3996 const char *incverlist[] = { PERL_INC_VERSION_LIST };
3997 const char **incver;
4004 if ((unix = tounixspec_ts(SvPV(libdir,len),Nullch)) != Nullch) {
4006 while (unix[len-1] == '/') len--; /* Cosmetic */
4007 sv_usepvn(libdir,unix,len);
4010 PerlIO_printf(Perl_error_log,
4011 "Failed to unixify @INC element \"%s\"\n",
4015 #ifdef MACOS_TRADITIONAL
4016 #define PERL_AV_SUFFIX_FMT ""
4017 #define PERL_ARCH_FMT "%s:"
4018 #define PERL_ARCH_FMT_PATH PERL_FS_VER_FMT PERL_AV_SUFFIX_FMT
4020 #define PERL_AV_SUFFIX_FMT "/"
4021 #define PERL_ARCH_FMT "/%s"
4022 #define PERL_ARCH_FMT_PATH PERL_AV_SUFFIX_FMT PERL_FS_VER_FMT
4024 /* .../version/archname if -d .../version/archname */
4025 Perl_sv_setpvf(aTHX_ subdir, "%"SVf PERL_ARCH_FMT_PATH PERL_ARCH_FMT,
4027 (int)PERL_REVISION, (int)PERL_VERSION,
4028 (int)PERL_SUBVERSION, ARCHNAME);
4029 if (PerlLIO_stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
4030 S_ISDIR(tmpstatbuf.st_mode))
4031 av_push(GvAVn(PL_incgv), newSVsv(subdir));
4033 /* .../version if -d .../version */
4034 Perl_sv_setpvf(aTHX_ subdir, "%"SVf PERL_ARCH_FMT_PATH, libdir,
4035 (int)PERL_REVISION, (int)PERL_VERSION,
4036 (int)PERL_SUBVERSION);
4037 if (PerlLIO_stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
4038 S_ISDIR(tmpstatbuf.st_mode))
4039 av_push(GvAVn(PL_incgv), newSVsv(subdir));
4041 /* .../archname if -d .../archname */
4042 Perl_sv_setpvf(aTHX_ subdir, "%"SVf PERL_ARCH_FMT, libdir, ARCHNAME);
4043 if (PerlLIO_stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
4044 S_ISDIR(tmpstatbuf.st_mode))
4045 av_push(GvAVn(PL_incgv), newSVsv(subdir));
4048 #ifdef PERL_INC_VERSION_LIST
4050 for (incver = incverlist; *incver; incver++) {
4051 /* .../xxx if -d .../xxx */
4052 Perl_sv_setpvf(aTHX_ subdir, "%"SVf PERL_ARCH_FMT, libdir, *incver);
4053 if (PerlLIO_stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
4054 S_ISDIR(tmpstatbuf.st_mode))
4055 av_push(GvAVn(PL_incgv), newSVsv(subdir));
4061 /* finally push this lib directory on the end of @INC */
4062 av_push(GvAVn(PL_incgv), libdir);
4066 #ifdef USE_5005THREADS
4067 STATIC struct perl_thread *
4068 S_init_main_thread(pTHX)
4070 #if !defined(PERL_IMPLICIT_CONTEXT)
4071 struct perl_thread *thr;
4075 Newz(53, thr, 1, struct perl_thread);
4076 PL_curcop = &PL_compiling;
4077 thr->interp = PERL_GET_INTERP;
4078 thr->cvcache = newHV();
4079 thr->threadsv = newAV();
4080 /* thr->threadsvp is set when find_threadsv is called */
4081 thr->specific = newAV();
4082 thr->flags = THRf_R_JOINABLE;
4083 MUTEX_INIT(&thr->mutex);
4084 /* Handcraft thrsv similarly to mess_sv */
4085 New(53, PL_thrsv, 1, SV);
4086 Newz(53, xpv, 1, XPV);
4087 SvFLAGS(PL_thrsv) = SVt_PV;
4088 SvANY(PL_thrsv) = (void*)xpv;
4089 SvREFCNT(PL_thrsv) = 1 << 30; /* practically infinite */
4090 SvPVX(PL_thrsv) = (char*)thr;
4091 SvCUR_set(PL_thrsv, sizeof(thr));
4092 SvLEN_set(PL_thrsv, sizeof(thr));
4093 *SvEND(PL_thrsv) = '\0'; /* in the trailing_nul field */
4094 thr->oursv = PL_thrsv;
4095 PL_chopset = " \n-";
4098 MUTEX_LOCK(&PL_threads_mutex);
4104 MUTEX_UNLOCK(&PL_threads_mutex);
4106 #ifdef HAVE_THREAD_INTERN
4107 Perl_init_thread_intern(thr);
4110 #ifdef SET_THREAD_SELF
4111 SET_THREAD_SELF(thr);
4113 thr->self = pthread_self();
4114 #endif /* SET_THREAD_SELF */
4118 * These must come after the thread self setting
4119 * because sv_setpvn does SvTAINT and the taint
4120 * fields thread selfness being set.
4122 PL_toptarget = NEWSV(0,0);
4123 sv_upgrade(PL_toptarget, SVt_PVFM);
4124 sv_setpvn(PL_toptarget, "", 0);
4125 PL_bodytarget = NEWSV(0,0);
4126 sv_upgrade(PL_bodytarget, SVt_PVFM);
4127 sv_setpvn(PL_bodytarget, "", 0);
4128 PL_formtarget = PL_bodytarget;
4129 thr->errsv = newSVpvn("", 0);
4130 (void) find_threadsv("@"); /* Ensure $@ is initialised early */
4133 PL_peepp = MEMBER_TO_FPTR(Perl_peep);
4134 PL_regcompp = MEMBER_TO_FPTR(Perl_pregcomp);
4135 PL_regexecp = MEMBER_TO_FPTR(Perl_regexec_flags);
4136 PL_regint_start = MEMBER_TO_FPTR(Perl_re_intuit_start);
4137 PL_regint_string = MEMBER_TO_FPTR(Perl_re_intuit_string);
4138 PL_regfree = MEMBER_TO_FPTR(Perl_pregfree);
4140 PL_reginterp_cnt = 0;
4144 #endif /* USE_5005THREADS */
4147 Perl_call_list(pTHX_ I32 oldscope, AV *paramList)
4150 line_t oldline = CopLINE(PL_curcop);
4156 while (AvFILL(paramList) >= 0) {
4157 cv = (CV*)av_shift(paramList);
4159 if (paramList == PL_beginav) {
4160 /* save PL_beginav for compiler */
4161 if (! PL_beginav_save)
4162 PL_beginav_save = newAV();
4163 av_push(PL_beginav_save, (SV*)cv);
4165 else if (paramList == PL_checkav) {
4166 /* save PL_checkav for compiler */
4167 if (! PL_checkav_save)
4168 PL_checkav_save = newAV();
4169 av_push(PL_checkav_save, (SV*)cv);
4174 #ifdef PERL_FLEXIBLE_EXCEPTIONS
4175 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vcall_list_body), cv);
4181 #ifndef PERL_FLEXIBLE_EXCEPTIONS
4185 (void)SvPV(atsv, len);
4187 PL_curcop = &PL_compiling;
4188 CopLINE_set(PL_curcop, oldline);
4189 if (paramList == PL_beginav)
4190 sv_catpv(atsv, "BEGIN failed--compilation aborted");
4192 Perl_sv_catpvf(aTHX_ atsv,
4193 "%s failed--call queue aborted",
4194 paramList == PL_checkav ? "CHECK"
4195 : paramList == PL_initav ? "INIT"
4197 while (PL_scopestack_ix > oldscope)
4200 Perl_croak(aTHX_ "%"SVf"", atsv);
4207 /* my_exit() was called */
4208 while (PL_scopestack_ix > oldscope)
4211 PL_curstash = PL_defstash;
4212 PL_curcop = &PL_compiling;
4213 CopLINE_set(PL_curcop, oldline);
4215 if (PL_statusvalue && !(PL_exit_flags & PERL_EXIT_EXPECTED)) {
4216 if (paramList == PL_beginav)
4217 Perl_croak(aTHX_ "BEGIN failed--compilation aborted");
4219 Perl_croak(aTHX_ "%s failed--call queue aborted",
4220 paramList == PL_checkav ? "CHECK"
4221 : paramList == PL_initav ? "INIT"
4228 PL_curcop = &PL_compiling;
4229 CopLINE_set(PL_curcop, oldline);
4232 PerlIO_printf(Perl_error_log, "panic: restartop\n");
4240 #ifdef PERL_FLEXIBLE_EXCEPTIONS
4242 S_vcall_list_body(pTHX_ va_list args)
4244 CV *cv = va_arg(args, CV*);
4245 return call_list_body(cv);
4250 S_call_list_body(pTHX_ CV *cv)
4252 PUSHMARK(PL_stack_sp);
4253 call_sv((SV*)cv, G_EVAL|G_DISCARD);
4258 Perl_my_exit(pTHX_ U32 status)
4260 DEBUG_S(PerlIO_printf(Perl_debug_log, "my_exit: thread %p, status %lu\n",
4261 thr, (unsigned long) status));
4270 STATUS_NATIVE_SET(status);
4277 Perl_my_failure_exit(pTHX)
4280 if (vaxc$errno & 1) {
4281 if (STATUS_NATIVE & 1) /* fortuitiously includes "-1" */
4282 STATUS_NATIVE_SET(44);
4285 if (!vaxc$errno && errno) /* unlikely */
4286 STATUS_NATIVE_SET(44);
4288 STATUS_NATIVE_SET(vaxc$errno);
4293 STATUS_POSIX_SET(errno);
4295 exitstatus = STATUS_POSIX >> 8;
4296 if (exitstatus & 255)
4297 STATUS_POSIX_SET(exitstatus);
4299 STATUS_POSIX_SET(255);
4306 S_my_exit_jump(pTHX)
4308 register PERL_CONTEXT *cx;
4313 SvREFCNT_dec(PL_e_script);
4314 PL_e_script = Nullsv;
4317 POPSTACK_TO(PL_mainstack);
4318 if (cxstack_ix >= 0) {
4321 POPBLOCK(cx,PL_curpm);
4329 read_e_script(pTHX_ int idx, SV *buf_sv, int maxlen)
4332 p = SvPVX(PL_e_script);
4333 nl = strchr(p, '\n');
4334 nl = (nl) ? nl+1 : SvEND(PL_e_script);
4336 filter_del(read_e_script);
4339 sv_catpvn(buf_sv, p, nl-p);
4340 sv_chop(PL_e_script, nl);