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)
158 /* Init the real globals (and main thread)? */
160 #ifdef PERL_FLEXIBLE_EXCEPTIONS
161 PL_protect = MEMBER_TO_FPTR(Perl_default_protect); /* for exceptions */
164 PL_curcop = &PL_compiling; /* needed by ckWARN, right away */
166 PL_linestr = NEWSV(65,79);
167 sv_upgrade(PL_linestr,SVt_PVIV);
169 if (!SvREADONLY(&PL_sv_undef)) {
170 /* set read-only and try to insure than we wont see REFCNT==0
173 SvREADONLY_on(&PL_sv_undef);
174 SvREFCNT(&PL_sv_undef) = (~(U32)0)/2;
176 sv_setpv(&PL_sv_no,PL_No);
178 SvREADONLY_on(&PL_sv_no);
179 SvREFCNT(&PL_sv_no) = (~(U32)0)/2;
181 sv_setpv(&PL_sv_yes,PL_Yes);
183 SvREADONLY_on(&PL_sv_yes);
184 SvREFCNT(&PL_sv_yes) = (~(U32)0)/2;
187 PL_sighandlerp = Perl_sighandler;
188 PL_pidstatus = newHV();
191 PL_rs = newSVpvn("\n", 1);
196 PL_lex_state = LEX_NOTPARSING;
202 SET_NUMERIC_STANDARD();
206 PL_patchlevel = NEWSV(0,4);
207 (void)SvUPGRADE(PL_patchlevel, SVt_PVNV);
208 if (PERL_REVISION > 127 || PERL_VERSION > 127 || PERL_SUBVERSION > 127)
209 SvGROW(PL_patchlevel, UTF8_MAXLEN*3+1);
210 s = (U8*)SvPVX(PL_patchlevel);
211 /* Build version strings using "native" characters */
212 s = uvchr_to_utf8(s, (UV)PERL_REVISION);
213 s = uvchr_to_utf8(s, (UV)PERL_VERSION);
214 s = uvchr_to_utf8(s, (UV)PERL_SUBVERSION);
216 SvCUR_set(PL_patchlevel, s - (U8*)SvPVX(PL_patchlevel));
217 SvPOK_on(PL_patchlevel);
218 SvNVX(PL_patchlevel) = (NV)PERL_REVISION +
219 ((NV)PERL_VERSION / (NV)1000) +
220 ((NV)PERL_SUBVERSION / (NV)1000000);
221 SvNOK_on(PL_patchlevel); /* dual valued */
222 SvUTF8_on(PL_patchlevel);
223 SvREADONLY_on(PL_patchlevel);
226 #if defined(LOCAL_PATCH_COUNT)
227 PL_localpatches = local_patches; /* For possible -v */
230 #ifdef HAVE_INTERP_INTERN
234 PerlIO_init(aTHX); /* Hook to IO system */
236 PL_fdpid = newAV(); /* for remembering popen pids by fd */
237 PL_modglobal = newHV(); /* pointers to per-interpreter module globals */
238 PL_errors = newSVpvn("",0);
239 sv_setpvn(PERL_DEBUG_PAD(0), "", 0); /* For regex debugging. */
240 sv_setpvn(PERL_DEBUG_PAD(1), "", 0); /* ext/re needs these */
241 sv_setpvn(PERL_DEBUG_PAD(2), "", 0); /* even without DEBUGGING. */
243 PL_regex_padav = newAV();
244 av_push(PL_regex_padav,(SV*)newAV()); /* First entry is an array of empty elements */
245 PL_regex_pad = AvARRAY(PL_regex_padav);
247 #ifdef USE_REENTRANT_API
248 Perl_reentrant_init(aTHX);
251 /* Note that strtab is a rather special HV. Assumptions are made
252 about not iterating on it, and not adding tie magic to it.
253 It is properly deallocated in perl_destruct() */
256 HvSHAREKEYS_off(PL_strtab); /* mandatory */
257 hv_ksplit(PL_strtab, 512);
259 #if defined(__DYNAMIC__) && (defined(NeXT) || defined(__NeXT__))
260 _dyld_lookup_and_bind
261 ("__environ", (unsigned long *) &environ_pointer, NULL);
264 #ifdef USE_ENVIRON_ARRAY
265 PL_origenviron = environ;
268 /* Use sysconf(_SC_CLK_TCK) if available, if not
269 * available or if the sysconf() fails, use the HZ. */
270 #if defined(HAS_SYSCONF) && defined(_SC_CLK_TCK)
271 PL_clocktick = sysconf(_SC_CLK_TCK);
272 if (PL_clocktick <= 0)
276 PL_stashcache = newHV();
282 =for apidoc nothreadhook
284 Stub that provides thread hook for perl_destruct when there are
291 Perl_nothreadhook(pTHX)
297 =for apidoc perl_destruct
299 Shuts down a Perl interpreter. See L<perlembed>.
307 volatile int destruct_level; /* 0=none, 1=full, 2=full with checks */
309 #ifdef USE_5005THREADS
311 #endif /* USE_5005THREADS */
313 /* wait for all pseudo-forked children to finish */
314 PERL_WAIT_FOR_CHILDREN;
316 destruct_level = PL_perl_destruct_level;
320 if ((s = PerlEnv_getenv("PERL_DESTRUCT_LEVEL"))) {
322 if (destruct_level < i)
329 if(PL_exit_flags & PERL_EXIT_DESTRUCT_END) {
334 if (PL_endav && !PL_minus_c)
335 call_list(PL_scopestack_ix, PL_endav);
341 /* Need to flush since END blocks can produce output */
344 if (CALL_FPTR(PL_threadhook)(aTHX)) {
345 /* Threads hook has vetoed further cleanup */
346 return STATUS_NATIVE_EXPORT;
349 /* We must account for everything. */
351 /* Destroy the main CV and syntax tree */
353 op_free(PL_main_root);
354 PL_main_root = Nullop;
356 PL_curcop = &PL_compiling;
357 PL_main_start = Nullop;
358 SvREFCNT_dec(PL_main_cv);
362 /* Tell PerlIO we are about to tear things apart in case
363 we have layers which are using resources that should
367 PerlIO_destruct(aTHX);
369 if (PL_sv_objcount) {
371 * Try to destruct global references. We do this first so that the
372 * destructors and destructees still exist. Some sv's might remain.
373 * Non-referenced objects are on their own.
378 /* unhook hooks which will soon be, or use, destroyed data */
379 SvREFCNT_dec(PL_warnhook);
380 PL_warnhook = Nullsv;
381 SvREFCNT_dec(PL_diehook);
384 /* call exit list functions */
385 while (PL_exitlistlen-- > 0)
386 PL_exitlist[PL_exitlistlen].fn(aTHX_ PL_exitlist[PL_exitlistlen].ptr);
388 Safefree(PL_exitlist);
393 if (destruct_level == 0){
395 DEBUG_P(debprofdump());
397 #if defined(PERLIO_LAYERS)
398 /* No more IO - including error messages ! */
399 PerlIO_cleanup(aTHX);
402 /* The exit() function will do everything that needs doing. */
403 return STATUS_NATIVE_EXPORT;
406 /* jettison our possibly duplicated environment */
407 /* if PERL_USE_SAFE_PUTENV is defined environ will not have been copied
408 * so we certainly shouldn't free it here
410 #if defined(USE_ENVIRON_ARRAY) && !defined(PERL_USE_SAFE_PUTENV)
411 if (environ != PL_origenviron
413 /* only main thread can free environ[0] contents */
414 && PL_curinterp == aTHX
420 for (i = 0; environ[i]; i++)
421 safesysfree(environ[i]);
423 /* Must use safesysfree() when working with environ. */
424 safesysfree(environ);
426 environ = PL_origenviron;
431 /* the syntax tree is shared between clones
432 * so op_free(PL_main_root) only ReREFCNT_dec's
433 * REGEXPs in the parent interpreter
434 * we need to manually ReREFCNT_dec for the clones
437 I32 i = AvFILLp(PL_regex_padav) + 1;
438 SV **ary = AvARRAY(PL_regex_padav);
442 REGEXP *re = INT2PTR(REGEXP *,SvIVX(resv));
444 if (SvFLAGS(resv) & SVf_BREAK) {
445 /* this is PL_reg_curpm, already freed
446 * flag is set in regexec.c:S_regtry
448 SvFLAGS(resv) &= ~SVf_BREAK;
450 else if(SvREPADTMP(resv)) {
451 SvREPADTMP_off(resv);
458 SvREFCNT_dec(PL_regex_padav);
459 PL_regex_padav = Nullav;
463 SvREFCNT_dec((SV*) PL_stashcache);
464 PL_stashcache = NULL;
466 /* loosen bonds of global variables */
469 (void)PerlIO_close(PL_rsfp);
473 /* Filters for program text */
474 SvREFCNT_dec(PL_rsfp_filters);
475 PL_rsfp_filters = Nullav;
478 PL_preprocess = FALSE;
484 PL_doswitches = FALSE;
485 PL_dowarn = G_WARN_OFF;
486 PL_doextract = FALSE;
487 PL_sawampersand = FALSE; /* must save all match strings */
490 Safefree(PL_inplace);
492 SvREFCNT_dec(PL_patchlevel);
495 SvREFCNT_dec(PL_e_script);
496 PL_e_script = Nullsv;
499 /* magical thingies */
501 SvREFCNT_dec(PL_ofs_sv); /* $, */
504 SvREFCNT_dec(PL_ors_sv); /* $\ */
507 SvREFCNT_dec(PL_rs); /* $/ */
510 PL_multiline = 0; /* $* */
511 Safefree(PL_osname); /* $^O */
514 SvREFCNT_dec(PL_statname);
515 PL_statname = Nullsv;
518 /* defgv, aka *_ should be taken care of elsewhere */
520 /* clean up after study() */
521 SvREFCNT_dec(PL_lastscream);
522 PL_lastscream = Nullsv;
523 Safefree(PL_screamfirst);
525 Safefree(PL_screamnext);
529 Safefree(PL_efloatbuf);
530 PL_efloatbuf = Nullch;
533 /* startup and shutdown function lists */
534 SvREFCNT_dec(PL_beginav);
535 SvREFCNT_dec(PL_beginav_save);
536 SvREFCNT_dec(PL_endav);
537 SvREFCNT_dec(PL_checkav);
538 SvREFCNT_dec(PL_checkav_save);
539 SvREFCNT_dec(PL_initav);
541 PL_beginav_save = Nullav;
544 PL_checkav_save = Nullav;
547 /* shortcuts just get cleared */
553 PL_argvoutgv = Nullgv;
555 PL_stderrgv = Nullgv;
556 PL_last_in_gv = Nullgv;
558 PL_debstash = Nullhv;
560 /* reset so print() ends up where we expect */
563 SvREFCNT_dec(PL_argvout_stack);
564 PL_argvout_stack = Nullav;
566 SvREFCNT_dec(PL_modglobal);
567 PL_modglobal = Nullhv;
568 SvREFCNT_dec(PL_preambleav);
569 PL_preambleav = Nullav;
570 SvREFCNT_dec(PL_subname);
572 SvREFCNT_dec(PL_linestr);
574 SvREFCNT_dec(PL_pidstatus);
575 PL_pidstatus = Nullhv;
576 SvREFCNT_dec(PL_toptarget);
577 PL_toptarget = Nullsv;
578 SvREFCNT_dec(PL_bodytarget);
579 PL_bodytarget = Nullsv;
580 PL_formtarget = Nullsv;
582 /* free locale stuff */
583 #ifdef USE_LOCALE_COLLATE
584 Safefree(PL_collation_name);
585 PL_collation_name = Nullch;
588 #ifdef USE_LOCALE_NUMERIC
589 Safefree(PL_numeric_name);
590 PL_numeric_name = Nullch;
591 SvREFCNT_dec(PL_numeric_radix_sv);
594 /* clear utf8 character classes */
595 SvREFCNT_dec(PL_utf8_alnum);
596 SvREFCNT_dec(PL_utf8_alnumc);
597 SvREFCNT_dec(PL_utf8_ascii);
598 SvREFCNT_dec(PL_utf8_alpha);
599 SvREFCNT_dec(PL_utf8_space);
600 SvREFCNT_dec(PL_utf8_cntrl);
601 SvREFCNT_dec(PL_utf8_graph);
602 SvREFCNT_dec(PL_utf8_digit);
603 SvREFCNT_dec(PL_utf8_upper);
604 SvREFCNT_dec(PL_utf8_lower);
605 SvREFCNT_dec(PL_utf8_print);
606 SvREFCNT_dec(PL_utf8_punct);
607 SvREFCNT_dec(PL_utf8_xdigit);
608 SvREFCNT_dec(PL_utf8_mark);
609 SvREFCNT_dec(PL_utf8_toupper);
610 SvREFCNT_dec(PL_utf8_totitle);
611 SvREFCNT_dec(PL_utf8_tolower);
612 SvREFCNT_dec(PL_utf8_tofold);
613 SvREFCNT_dec(PL_utf8_idstart);
614 SvREFCNT_dec(PL_utf8_idcont);
615 PL_utf8_alnum = Nullsv;
616 PL_utf8_alnumc = Nullsv;
617 PL_utf8_ascii = Nullsv;
618 PL_utf8_alpha = Nullsv;
619 PL_utf8_space = Nullsv;
620 PL_utf8_cntrl = Nullsv;
621 PL_utf8_graph = Nullsv;
622 PL_utf8_digit = Nullsv;
623 PL_utf8_upper = Nullsv;
624 PL_utf8_lower = Nullsv;
625 PL_utf8_print = Nullsv;
626 PL_utf8_punct = Nullsv;
627 PL_utf8_xdigit = Nullsv;
628 PL_utf8_mark = Nullsv;
629 PL_utf8_toupper = Nullsv;
630 PL_utf8_totitle = Nullsv;
631 PL_utf8_tolower = Nullsv;
632 PL_utf8_tofold = Nullsv;
633 PL_utf8_idstart = Nullsv;
634 PL_utf8_idcont = Nullsv;
636 if (!specialWARN(PL_compiling.cop_warnings))
637 SvREFCNT_dec(PL_compiling.cop_warnings);
638 PL_compiling.cop_warnings = Nullsv;
639 if (!specialCopIO(PL_compiling.cop_io))
640 SvREFCNT_dec(PL_compiling.cop_io);
641 PL_compiling.cop_io = Nullsv;
642 CopFILE_free(&PL_compiling);
643 CopSTASH_free(&PL_compiling);
645 /* Prepare to destruct main symbol table. */
650 SvREFCNT_dec(PL_curstname);
651 PL_curstname = Nullsv;
653 /* clear queued errors */
654 SvREFCNT_dec(PL_errors);
658 if (destruct_level >= 2 && ckWARN_d(WARN_INTERNAL)) {
659 if (PL_scopestack_ix != 0)
660 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
661 "Unbalanced scopes: %ld more ENTERs than LEAVEs\n",
662 (long)PL_scopestack_ix);
663 if (PL_savestack_ix != 0)
664 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
665 "Unbalanced saves: %ld more saves than restores\n",
666 (long)PL_savestack_ix);
667 if (PL_tmps_floor != -1)
668 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),"Unbalanced tmps: %ld more allocs than frees\n",
669 (long)PL_tmps_floor + 1);
670 if (cxstack_ix != -1)
671 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),"Unbalanced context: %ld more PUSHes than POPs\n",
672 (long)cxstack_ix + 1);
675 /* Now absolutely destruct everything, somehow or other, loops or no. */
676 SvFLAGS(PL_fdpid) |= SVTYPEMASK; /* don't clean out pid table now */
677 SvFLAGS(PL_strtab) |= SVTYPEMASK; /* don't clean out strtab now */
679 /* the 2 is for PL_fdpid and PL_strtab */
680 while (PL_sv_count > 2 && sv_clean_all())
683 SvFLAGS(PL_fdpid) &= ~SVTYPEMASK;
684 SvFLAGS(PL_fdpid) |= SVt_PVAV;
685 SvFLAGS(PL_strtab) &= ~SVTYPEMASK;
686 SvFLAGS(PL_strtab) |= SVt_PVHV;
688 AvREAL_off(PL_fdpid); /* no surviving entries */
689 SvREFCNT_dec(PL_fdpid); /* needed in io_close() */
692 #ifdef HAVE_INTERP_INTERN
696 /* Destruct the global string table. */
698 /* Yell and reset the HeVAL() slots that are still holding refcounts,
699 * so that sv_free() won't fail on them.
707 max = HvMAX(PL_strtab);
708 array = HvARRAY(PL_strtab);
711 if (hent && ckWARN_d(WARN_INTERNAL)) {
712 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
713 "Unbalanced string table refcount: (%d) for \"%s\"",
714 HeVAL(hent) - Nullsv, HeKEY(hent));
715 HeVAL(hent) = Nullsv;
725 SvREFCNT_dec(PL_strtab);
728 /* free the pointer table used for cloning */
729 ptr_table_free(PL_ptr_table);
732 /* free special SVs */
734 SvREFCNT(&PL_sv_yes) = 0;
735 sv_clear(&PL_sv_yes);
736 SvANY(&PL_sv_yes) = NULL;
737 SvFLAGS(&PL_sv_yes) = 0;
739 SvREFCNT(&PL_sv_no) = 0;
741 SvANY(&PL_sv_no) = NULL;
742 SvFLAGS(&PL_sv_no) = 0;
746 for (i=0; i<=2; i++) {
747 SvREFCNT(PERL_DEBUG_PAD(i)) = 0;
748 sv_clear(PERL_DEBUG_PAD(i));
749 SvANY(PERL_DEBUG_PAD(i)) = NULL;
750 SvFLAGS(PERL_DEBUG_PAD(i)) = 0;
754 if (PL_sv_count != 0 && ckWARN_d(WARN_INTERNAL))
755 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),"Scalars leaked: %ld\n", (long)PL_sv_count);
757 #ifdef DEBUG_LEAKING_SCALARS
758 if (PL_sv_count != 0) {
763 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
764 svend = &sva[SvREFCNT(sva)];
765 for (sv = sva + 1; sv < svend; ++sv) {
766 if (SvTYPE(sv) != SVTYPEMASK) {
767 PerlIO_printf(Perl_debug_log, "leaked: 0x%p\n", sv);
775 #if defined(PERLIO_LAYERS)
776 /* No more IO - including error messages ! */
777 PerlIO_cleanup(aTHX);
780 /* sv_undef needs to stay immortal until after PerlIO_cleanup
781 as currently layers use it rather than Nullsv as a marker
782 for no arg - and will try and SvREFCNT_dec it.
784 SvREFCNT(&PL_sv_undef) = 0;
785 SvREADONLY_off(&PL_sv_undef);
787 Safefree(PL_origfilename);
788 Safefree(PL_reg_start_tmp);
790 Safefree(PL_reg_curpm);
791 Safefree(PL_reg_poscache);
793 Safefree(PL_op_mask);
794 Safefree(PL_psig_ptr);
795 Safefree(PL_psig_name);
796 Safefree(PL_bitcount);
797 Safefree(PL_psig_pend);
799 PL_hints = 0; /* Reset hints. Should hints be per-interpreter ? */
801 DEBUG_P(debprofdump());
803 #ifdef USE_REENTRANT_API
804 Perl_reentrant_free(aTHX);
809 /* As the absolutely last thing, free the non-arena SV for mess() */
812 /* it could have accumulated taint magic */
813 if (SvTYPE(PL_mess_sv) >= SVt_PVMG) {
816 for (mg = SvMAGIC(PL_mess_sv); mg; mg = moremagic) {
817 moremagic = mg->mg_moremagic;
818 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global
820 Safefree(mg->mg_ptr);
824 /* we know that type >= SVt_PV */
825 (void)SvOOK_off(PL_mess_sv);
826 Safefree(SvPVX(PL_mess_sv));
827 Safefree(SvANY(PL_mess_sv));
828 Safefree(PL_mess_sv);
831 return STATUS_NATIVE_EXPORT;
835 =for apidoc perl_free
837 Releases a Perl interpreter. See L<perlembed>.
845 #if defined(WIN32) || defined(NETWARE)
846 # if defined(PERL_IMPLICIT_SYS)
848 void *host = nw_internal_host;
850 void *host = w32_internal_host;
854 nw_delete_internal_host(host);
856 win32_delete_internal_host(host);
867 Perl_call_atexit(pTHX_ ATEXIT_t fn, void *ptr)
869 Renew(PL_exitlist, PL_exitlistlen+1, PerlExitListEntry);
870 PL_exitlist[PL_exitlistlen].fn = fn;
871 PL_exitlist[PL_exitlistlen].ptr = ptr;
876 =for apidoc perl_parse
878 Tells a Perl interpreter to parse a Perl script. See L<perlembed>.
884 perl_parse(pTHXx_ XSINIT_t xsinit, int argc, char **argv, char **env)
889 #ifdef USE_5005THREADS
893 #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
896 Perl_croak(aTHX_ "suidperl is no longer needed since the kernel can now execute\n\
897 setuid perl scripts securely.\n");
906 /* Come here if running an undumped a.out. */
908 PL_origfilename = savepv(argv[0]);
909 PL_do_undump = FALSE;
910 cxstack_ix = -1; /* start label stack again */
912 init_postdump_symbols(argc,argv,env);
917 op_free(PL_main_root);
918 PL_main_root = Nullop;
920 PL_main_start = Nullop;
921 SvREFCNT_dec(PL_main_cv);
925 oldscope = PL_scopestack_ix;
926 PL_dowarn = G_WARN_OFF;
928 #ifdef PERL_FLEXIBLE_EXCEPTIONS
929 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vparse_body), env, xsinit);
935 #ifndef PERL_FLEXIBLE_EXCEPTIONS
936 parse_body(env,xsinit);
939 call_list(oldscope, PL_checkav);
946 /* my_exit() was called */
947 while (PL_scopestack_ix > oldscope)
950 PL_curstash = PL_defstash;
952 call_list(oldscope, PL_checkav);
953 ret = STATUS_NATIVE_EXPORT;
956 PerlIO_printf(Perl_error_log, "panic: top_env\n");
964 #ifdef PERL_FLEXIBLE_EXCEPTIONS
966 S_vparse_body(pTHX_ va_list args)
968 char **env = va_arg(args, char**);
969 XSINIT_t xsinit = va_arg(args, XSINIT_t);
971 return parse_body(env, xsinit);
976 S_parse_body(pTHX_ char **env, XSINIT_t xsinit)
978 int argc = PL_origargc;
979 char **argv = PL_origargv;
980 char *scriptname = NULL;
982 VOL bool dosearch = FALSE;
986 char *cddir = Nullch;
988 sv_setpvn(PL_linestr,"",0);
989 sv = newSVpvn("",0); /* first used for -I flags */
993 for (argc--,argv++; argc > 0; argc--,argv++) {
994 if (argv[0][0] != '-' || !argv[0][1])
998 validarg = " PHOOEY ";
1006 #ifndef PERL_STRICT_CR
1031 if ((s = moreswitches(s)))
1036 CHECK_MALLOC_TOO_LATE_FOR('t');
1037 if( !PL_tainting ) {
1038 PL_taint_warn = TRUE;
1044 CHECK_MALLOC_TOO_LATE_FOR('T');
1046 PL_taint_warn = FALSE;
1051 #ifdef MACOS_TRADITIONAL
1052 /* ignore -e for Dev:Pseudo argument */
1053 if (argv[1] && !strcmp(argv[1], "Dev:Pseudo"))
1056 if (PL_euid != PL_uid || PL_egid != PL_gid)
1057 Perl_croak(aTHX_ "No -e allowed in setuid scripts");
1059 PL_e_script = newSVpvn("",0);
1060 filter_add(read_e_script, NULL);
1063 sv_catpv(PL_e_script, s);
1065 sv_catpv(PL_e_script, argv[1]);
1069 Perl_croak(aTHX_ "No code specified for -e");
1070 sv_catpv(PL_e_script, "\n");
1073 case 'I': /* -I handled both here and in moreswitches() */
1075 if (!*++s && (s=argv[1]) != Nullch) {
1080 STRLEN len = strlen(s);
1081 p = savepvn(s, len);
1082 incpush(p, TRUE, TRUE, FALSE);
1083 sv_catpvn(sv, "-I", 2);
1084 sv_catpvn(sv, p, len);
1085 sv_catpvn(sv, " ", 1);
1089 Perl_croak(aTHX_ "No directory specified for -I");
1093 PL_preprocess = TRUE;
1103 PL_preambleav = newAV();
1104 av_push(PL_preambleav, newSVpv("use Config qw(myconfig config_vars)",0));
1106 PL_Sv = newSVpv("print myconfig();",0);
1108 sv_catpv(PL_Sv,"print \"\\nCharacteristics of this PERLSHR image: \\n\",");
1110 sv_catpv(PL_Sv,"print \"\\nCharacteristics of this binary (from libperl): \\n\",");
1112 sv_catpv(PL_Sv,"\" Compile-time options:");
1114 sv_catpv(PL_Sv," DEBUGGING");
1116 # ifdef MULTIPLICITY
1117 sv_catpv(PL_Sv," MULTIPLICITY");
1119 # ifdef USE_5005THREADS
1120 sv_catpv(PL_Sv," USE_5005THREADS");
1122 # ifdef USE_ITHREADS
1123 sv_catpv(PL_Sv," USE_ITHREADS");
1125 # ifdef USE_64_BIT_INT
1126 sv_catpv(PL_Sv," USE_64_BIT_INT");
1128 # ifdef USE_64_BIT_ALL
1129 sv_catpv(PL_Sv," USE_64_BIT_ALL");
1131 # ifdef USE_LONG_DOUBLE
1132 sv_catpv(PL_Sv," USE_LONG_DOUBLE");
1134 # ifdef USE_LARGE_FILES
1135 sv_catpv(PL_Sv," USE_LARGE_FILES");
1138 sv_catpv(PL_Sv," USE_SOCKS");
1140 # ifdef PERL_IMPLICIT_CONTEXT
1141 sv_catpv(PL_Sv," PERL_IMPLICIT_CONTEXT");
1143 # ifdef PERL_IMPLICIT_SYS
1144 sv_catpv(PL_Sv," PERL_IMPLICIT_SYS");
1146 sv_catpv(PL_Sv,"\\n\",");
1148 #if defined(LOCAL_PATCH_COUNT)
1149 if (LOCAL_PATCH_COUNT > 0) {
1151 sv_catpv(PL_Sv,"\" Locally applied patches:\\n\",");
1152 for (i = 1; i <= LOCAL_PATCH_COUNT; i++) {
1153 if (PL_localpatches[i])
1154 Perl_sv_catpvf(aTHX_ PL_Sv,"q\" \t%s\n\",",PL_localpatches[i]);
1158 Perl_sv_catpvf(aTHX_ PL_Sv,"\" Built under %s\\n\"",OSNAME);
1161 Perl_sv_catpvf(aTHX_ PL_Sv,",\" Compiled at %s %s\\n\"",__DATE__,__TIME__);
1163 Perl_sv_catpvf(aTHX_ PL_Sv,",\" Compiled on %s\\n\"",__DATE__);
1166 sv_catpv(PL_Sv, "; \
1168 @env = map { \"$_=\\\"$ENV{$_}\\\"\" } sort grep {/^PERL/} keys %ENV; ");
1171 push @env, \"CYGWIN=\\\"$ENV{CYGWIN}\\\"\";");
1174 print \" \\%ENV:\\n @env\\n\" if @env; \
1175 print \" \\@INC:\\n @INC\\n\";");
1178 PL_Sv = newSVpv("config_vars(qw(",0);
1179 sv_catpv(PL_Sv, ++s);
1180 sv_catpv(PL_Sv, "))");
1183 av_push(PL_preambleav, PL_Sv);
1184 scriptname = BIT_BUCKET; /* don't look for script or read stdin */
1187 PL_doextract = TRUE;
1195 if (!*++s || isSPACE(*s)) {
1199 /* catch use of gnu style long options */
1200 if (strEQ(s, "version")) {
1204 if (strEQ(s, "help")) {
1211 Perl_croak(aTHX_ "Unrecognized switch: -%s (-h will show valid options)",s);
1215 sv_setsv(get_sv("/", TRUE), PL_rs);
1218 #ifndef SECURE_INTERNAL_GETENV
1221 (s = PerlEnv_getenv("PERL5OPT")))
1226 if (*s == '-' && *(s+1) == 'T') {
1227 CHECK_MALLOC_TOO_LATE_FOR('T');
1229 PL_taint_warn = FALSE;
1232 char *popt_copy = Nullch;
1245 if (!strchr("DIMUdmtwA", *s))
1246 Perl_croak(aTHX_ "Illegal switch in PERL5OPT: -%c", *s);
1250 popt_copy = SvPVX(sv_2mortal(newSVpv(popt,0)));
1251 s = popt_copy + (s - popt);
1252 d = popt_copy + (d - popt);
1259 if( !PL_tainting ) {
1260 PL_taint_warn = TRUE;
1270 if (PL_taint_warn && PL_dowarn != G_WARN_ALL_OFF) {
1271 PL_compiling.cop_warnings = newSVpvn(WARN_TAINTstring, WARNsize);
1275 scriptname = argv[0];
1278 scriptname = BIT_BUCKET; /* don't look for script or read stdin */
1280 else if (scriptname == Nullch) {
1282 if ( PerlLIO_isatty(PerlIO_fileno(PerlIO_stdin())) )
1290 open_script(scriptname,dosearch,sv,&fdscript);
1292 validate_suid(validarg, scriptname,fdscript);
1295 #if defined(SIGCHLD) || defined(SIGCLD)
1298 # define SIGCHLD SIGCLD
1300 Sighandler_t sigstate = rsignal_state(SIGCHLD);
1301 if (sigstate == SIG_IGN) {
1302 if (ckWARN(WARN_SIGNAL))
1303 Perl_warner(aTHX_ packWARN(WARN_SIGNAL),
1304 "Can't ignore signal CHLD, forcing to default");
1305 (void)rsignal(SIGCHLD, (Sighandler_t)SIG_DFL);
1311 #ifdef MACOS_TRADITIONAL
1312 if (PL_doextract || gMacPerl_AlwaysExtract) {
1317 if (cddir && PerlDir_chdir(cddir) < 0)
1318 Perl_croak(aTHX_ "Can't chdir to %s",cddir);
1322 PL_main_cv = PL_compcv = (CV*)NEWSV(1104,0);
1323 sv_upgrade((SV *)PL_compcv, SVt_PVCV);
1324 CvUNIQUE_on(PL_compcv);
1326 CvPADLIST(PL_compcv) = pad_new(0);
1327 #ifdef USE_5005THREADS
1328 CvOWNER(PL_compcv) = 0;
1329 New(666, CvMUTEXP(PL_compcv), 1, perl_mutex);
1330 MUTEX_INIT(CvMUTEXP(PL_compcv));
1331 #endif /* USE_5005THREADS */
1334 boot_core_UNIVERSAL();
1336 boot_core_xsutils();
1340 (*xsinit)(aTHX); /* in case linked C routines want magical variables */
1342 #if defined(VMS) || defined(WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(EPOC)
1348 # ifdef HAS_SOCKS5_INIT
1349 socks5_init(argv[0]);
1355 init_predump_symbols();
1356 /* init_postdump_symbols not currently designed to be called */
1357 /* more than once (ENV isn't cleared first, for example) */
1358 /* But running with -u leaves %ENV & @ARGV undefined! XXX */
1360 init_postdump_symbols(argc,argv,env);
1362 /* PL_unicode is turned on by -C or by $ENV{PERL_UNICODE}.
1363 * PL_utf8locale is conditionally turned on by
1364 * locale.c:Perl_init_i18nl10n() if the environment
1365 * look like the user wants to use UTF-8. */
1367 /* Requires init_predump_symbols(). */
1368 if (!(PL_unicode & PERL_UNICODE_LOCALE_FLAG) || PL_utf8locale) {
1373 /* Turn on UTF-8-ness on STDIN, STDOUT, STDERR
1374 * and the default open disciplines. */
1375 if ((PL_unicode & PERL_UNICODE_STDIN_FLAG) &&
1376 PL_stdingv && (io = GvIO(PL_stdingv)) &&
1378 PerlIO_binmode(aTHX_ fp, IoTYPE(io), 0, ":utf8");
1379 if ((PL_unicode & PERL_UNICODE_STDOUT_FLAG) &&
1380 PL_defoutgv && (io = GvIO(PL_defoutgv)) &&
1382 PerlIO_binmode(aTHX_ fp, IoTYPE(io), 0, ":utf8");
1383 if ((PL_unicode & PERL_UNICODE_STDERR_FLAG) &&
1384 PL_stderrgv && (io = GvIO(PL_stderrgv)) &&
1386 PerlIO_binmode(aTHX_ fp, IoTYPE(io), 0, ":utf8");
1387 if ((PL_unicode & PERL_UNICODE_INOUT_FLAG) &&
1388 (sv = GvSV(gv_fetchpv("\017PEN", TRUE, SVt_PV)))) {
1389 U32 in = PL_unicode & PERL_UNICODE_IN_FLAG;
1390 U32 out = PL_unicode & PERL_UNICODE_OUT_FLAG;
1393 sv_setpvn(sv, ":utf8\0:utf8", 11);
1395 sv_setpvn(sv, ":utf8\0", 6);
1398 sv_setpvn(sv, "\0:utf8", 6);
1404 if ((s = PerlEnv_getenv("PERL_SIGNALS"))) {
1405 if (strEQ(s, "unsafe"))
1406 PL_signals |= PERL_SIGNALS_UNSAFE_FLAG;
1407 else if (strEQ(s, "safe"))
1408 PL_signals &= ~PERL_SIGNALS_UNSAFE_FLAG;
1410 Perl_croak(aTHX_ "PERL_SIGNALS illegal: \"%s\"", s);
1415 /* now parse the script */
1417 SETERRNO(0,SS_NORMAL);
1419 #ifdef MACOS_TRADITIONAL
1420 if (gMacPerl_SyntaxError = (yyparse() || PL_error_count)) {
1422 Perl_croak(aTHX_ "%s had compilation errors.\n", MacPerl_MPWFileName(PL_origfilename));
1424 Perl_croak(aTHX_ "Execution of %s aborted due to compilation errors.\n",
1425 MacPerl_MPWFileName(PL_origfilename));
1429 if (yyparse() || PL_error_count) {
1431 Perl_croak(aTHX_ "%s had compilation errors.\n", PL_origfilename);
1433 Perl_croak(aTHX_ "Execution of %s aborted due to compilation errors.\n",
1438 CopLINE_set(PL_curcop, 0);
1439 PL_curstash = PL_defstash;
1440 PL_preprocess = FALSE;
1442 SvREFCNT_dec(PL_e_script);
1443 PL_e_script = Nullsv;
1450 SAVECOPFILE(PL_curcop);
1451 SAVECOPLINE(PL_curcop);
1452 gv_check(PL_defstash);
1459 if ((s=PerlEnv_getenv("PERL_DEBUG_MSTATS")) && atoi(s) >= 2)
1460 dump_mstats("after compilation:");
1469 =for apidoc perl_run
1471 Tells a Perl interpreter to run. See L<perlembed>.
1482 #ifdef USE_5005THREADS
1486 oldscope = PL_scopestack_ix;
1491 #ifdef PERL_FLEXIBLE_EXCEPTIONS
1493 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vrun_body), oldscope);
1499 cxstack_ix = -1; /* start context stack again */
1501 case 0: /* normal completion */
1502 #ifndef PERL_FLEXIBLE_EXCEPTIONS
1507 case 2: /* my_exit() */
1508 while (PL_scopestack_ix > oldscope)
1511 PL_curstash = PL_defstash;
1512 if (!(PL_exit_flags & PERL_EXIT_DESTRUCT_END) &&
1513 PL_endav && !PL_minus_c)
1514 call_list(oldscope, PL_endav);
1516 if (PerlEnv_getenv("PERL_DEBUG_MSTATS"))
1517 dump_mstats("after execution: ");
1519 ret = STATUS_NATIVE_EXPORT;
1523 POPSTACK_TO(PL_mainstack);
1526 PerlIO_printf(Perl_error_log, "panic: restartop\n");
1536 #ifdef PERL_FLEXIBLE_EXCEPTIONS
1538 S_vrun_body(pTHX_ va_list args)
1540 I32 oldscope = va_arg(args, I32);
1542 return run_body(oldscope);
1548 S_run_body(pTHX_ I32 oldscope)
1550 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s $` $& $' support.\n",
1551 PL_sawampersand ? "Enabling" : "Omitting"));
1553 if (!PL_restartop) {
1554 DEBUG_x(dump_all());
1555 DEBUG(PerlIO_printf(Perl_debug_log, "\nEXECUTING...\n\n"));
1556 DEBUG_S(PerlIO_printf(Perl_debug_log, "main thread is 0x%"UVxf"\n",
1560 #ifdef MACOS_TRADITIONAL
1561 PerlIO_printf(Perl_error_log, "%s%s syntax OK\n",
1562 (gMacPerl_ErrorFormat ? "# " : ""),
1563 MacPerl_MPWFileName(PL_origfilename));
1565 PerlIO_printf(Perl_error_log, "%s syntax OK\n", PL_origfilename);
1569 if (PERLDB_SINGLE && PL_DBsingle)
1570 sv_setiv(PL_DBsingle, 1);
1572 call_list(oldscope, PL_initav);
1578 PL_op = PL_restartop;
1582 else if (PL_main_start) {
1583 CvDEPTH(PL_main_cv) = 1;
1584 PL_op = PL_main_start;
1594 =head1 SV Manipulation Functions
1596 =for apidoc p||get_sv
1598 Returns the SV of the specified Perl scalar. If C<create> is set and the
1599 Perl variable does not exist then it will be created. If C<create> is not
1600 set and the variable does not exist then NULL is returned.
1606 Perl_get_sv(pTHX_ const char *name, I32 create)
1609 #ifdef USE_5005THREADS
1610 if (name[1] == '\0' && !isALPHA(name[0])) {
1611 PADOFFSET tmp = find_threadsv(name);
1612 if (tmp != NOT_IN_PAD)
1613 return THREADSV(tmp);
1615 #endif /* USE_5005THREADS */
1616 gv = gv_fetchpv(name, create, SVt_PV);
1623 =head1 Array Manipulation Functions
1625 =for apidoc p||get_av
1627 Returns the AV of the specified Perl array. If C<create> is set and the
1628 Perl variable does not exist then it will be created. If C<create> is not
1629 set and the variable does not exist then NULL is returned.
1635 Perl_get_av(pTHX_ const char *name, I32 create)
1637 GV* gv = gv_fetchpv(name, create, SVt_PVAV);
1646 =head1 Hash Manipulation Functions
1648 =for apidoc p||get_hv
1650 Returns the HV of the specified Perl hash. If C<create> is set and the
1651 Perl variable does not exist then it will be created. If C<create> is not
1652 set and the variable does not exist then NULL is returned.
1658 Perl_get_hv(pTHX_ const char *name, I32 create)
1660 GV* gv = gv_fetchpv(name, create, SVt_PVHV);
1669 =head1 CV Manipulation Functions
1671 =for apidoc p||get_cv
1673 Returns the CV of the specified Perl subroutine. If C<create> is set and
1674 the Perl subroutine does not exist then it will be declared (which has the
1675 same effect as saying C<sub name;>). If C<create> is not set and the
1676 subroutine does not exist then NULL is returned.
1682 Perl_get_cv(pTHX_ const char *name, I32 create)
1684 GV* gv = gv_fetchpv(name, create, SVt_PVCV);
1685 /* XXX unsafe for threads if eval_owner isn't held */
1686 /* XXX this is probably not what they think they're getting.
1687 * It has the same effect as "sub name;", i.e. just a forward
1689 if (create && !GvCVu(gv))
1690 return newSUB(start_subparse(FALSE, 0),
1691 newSVOP(OP_CONST, 0, newSVpv(name,0)),
1699 /* Be sure to refetch the stack pointer after calling these routines. */
1703 =head1 Callback Functions
1705 =for apidoc p||call_argv
1707 Performs a callback to the specified Perl sub. See L<perlcall>.
1713 Perl_call_argv(pTHX_ const char *sub_name, I32 flags, register char **argv)
1715 /* See G_* flags in cop.h */
1716 /* null terminated arg list */
1723 XPUSHs(sv_2mortal(newSVpv(*argv,0)));
1728 return call_pv(sub_name, flags);
1732 =for apidoc p||call_pv
1734 Performs a callback to the specified Perl sub. See L<perlcall>.
1740 Perl_call_pv(pTHX_ const char *sub_name, I32 flags)
1741 /* name of the subroutine */
1742 /* See G_* flags in cop.h */
1744 return call_sv((SV*)get_cv(sub_name, TRUE), flags);
1748 =for apidoc p||call_method
1750 Performs a callback to the specified Perl method. The blessed object must
1751 be on the stack. See L<perlcall>.
1757 Perl_call_method(pTHX_ const char *methname, I32 flags)
1758 /* name of the subroutine */
1759 /* See G_* flags in cop.h */
1761 return call_sv(sv_2mortal(newSVpv(methname,0)), flags | G_METHOD);
1764 /* May be called with any of a CV, a GV, or an SV containing the name. */
1766 =for apidoc p||call_sv
1768 Performs a callback to the Perl sub whose name is in the SV. See
1775 Perl_call_sv(pTHX_ SV *sv, I32 flags)
1776 /* See G_* flags in cop.h */
1779 LOGOP myop; /* fake syntax tree node */
1782 volatile I32 retval = 0;
1784 bool oldcatch = CATCH_GET;
1789 if (flags & G_DISCARD) {
1794 Zero(&myop, 1, LOGOP);
1795 myop.op_next = Nullop;
1796 if (!(flags & G_NOARGS))
1797 myop.op_flags |= OPf_STACKED;
1798 myop.op_flags |= ((flags & G_VOID) ? OPf_WANT_VOID :
1799 (flags & G_ARRAY) ? OPf_WANT_LIST :
1804 EXTEND(PL_stack_sp, 1);
1805 *++PL_stack_sp = sv;
1807 oldscope = PL_scopestack_ix;
1809 if (PERLDB_SUB && PL_curstash != PL_debstash
1810 /* Handle first BEGIN of -d. */
1811 && (PL_DBcv || (PL_DBcv = GvCV(PL_DBsub)))
1812 /* Try harder, since this may have been a sighandler, thus
1813 * curstash may be meaningless. */
1814 && (SvTYPE(sv) != SVt_PVCV || CvSTASH((CV*)sv) != PL_debstash)
1815 && !(flags & G_NODEBUG))
1816 PL_op->op_private |= OPpENTERSUB_DB;
1818 if (flags & G_METHOD) {
1819 Zero(&method_op, 1, UNOP);
1820 method_op.op_next = PL_op;
1821 method_op.op_ppaddr = PL_ppaddr[OP_METHOD];
1822 myop.op_ppaddr = PL_ppaddr[OP_ENTERSUB];
1823 PL_op = (OP*)&method_op;
1826 if (!(flags & G_EVAL)) {
1828 call_body((OP*)&myop, FALSE);
1829 retval = PL_stack_sp - (PL_stack_base + oldmark);
1830 CATCH_SET(oldcatch);
1833 myop.op_other = (OP*)&myop;
1835 /* we're trying to emulate pp_entertry() here */
1837 register PERL_CONTEXT *cx;
1838 I32 gimme = GIMME_V;
1843 push_return(Nullop);
1844 PUSHBLOCK(cx, (CXt_EVAL|CXp_TRYBLOCK), PL_stack_sp);
1846 PL_eval_root = PL_op; /* Only needed so that goto works right. */
1848 PL_in_eval = EVAL_INEVAL;
1849 if (flags & G_KEEPERR)
1850 PL_in_eval |= EVAL_KEEPERR;
1856 #ifdef PERL_FLEXIBLE_EXCEPTIONS
1858 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vcall_body),
1865 #ifndef PERL_FLEXIBLE_EXCEPTIONS
1867 call_body((OP*)&myop, FALSE);
1869 retval = PL_stack_sp - (PL_stack_base + oldmark);
1870 if (!(flags & G_KEEPERR))
1877 /* my_exit() was called */
1878 PL_curstash = PL_defstash;
1881 if (PL_statusvalue && !(PL_exit_flags & PERL_EXIT_EXPECTED))
1882 Perl_croak(aTHX_ "Callback called exit");
1887 PL_op = PL_restartop;
1891 PL_stack_sp = PL_stack_base + oldmark;
1892 if (flags & G_ARRAY)
1896 *++PL_stack_sp = &PL_sv_undef;
1901 if (PL_scopestack_ix > oldscope) {
1905 register PERL_CONTEXT *cx;
1917 if (flags & G_DISCARD) {
1918 PL_stack_sp = PL_stack_base + oldmark;
1927 #ifdef PERL_FLEXIBLE_EXCEPTIONS
1929 S_vcall_body(pTHX_ va_list args)
1931 OP *myop = va_arg(args, OP*);
1932 int is_eval = va_arg(args, int);
1934 call_body(myop, is_eval);
1940 S_call_body(pTHX_ OP *myop, int is_eval)
1942 if (PL_op == myop) {
1944 PL_op = Perl_pp_entereval(aTHX); /* this doesn't do a POPMARK */
1946 PL_op = Perl_pp_entersub(aTHX); /* this does */
1952 /* Eval a string. The G_EVAL flag is always assumed. */
1955 =for apidoc p||eval_sv
1957 Tells Perl to C<eval> the string in the SV.
1963 Perl_eval_sv(pTHX_ SV *sv, I32 flags)
1965 /* See G_* flags in cop.h */
1968 UNOP myop; /* fake syntax tree node */
1969 volatile I32 oldmark = SP - PL_stack_base;
1970 volatile I32 retval = 0;
1976 if (flags & G_DISCARD) {
1983 Zero(PL_op, 1, UNOP);
1984 EXTEND(PL_stack_sp, 1);
1985 *++PL_stack_sp = sv;
1986 oldscope = PL_scopestack_ix;
1988 if (!(flags & G_NOARGS))
1989 myop.op_flags = OPf_STACKED;
1990 myop.op_next = Nullop;
1991 myop.op_type = OP_ENTEREVAL;
1992 myop.op_flags |= ((flags & G_VOID) ? OPf_WANT_VOID :
1993 (flags & G_ARRAY) ? OPf_WANT_LIST :
1995 if (flags & G_KEEPERR)
1996 myop.op_flags |= OPf_SPECIAL;
1998 #ifdef PERL_FLEXIBLE_EXCEPTIONS
2000 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vcall_body),
2007 #ifndef PERL_FLEXIBLE_EXCEPTIONS
2009 call_body((OP*)&myop,TRUE);
2011 retval = PL_stack_sp - (PL_stack_base + oldmark);
2012 if (!(flags & G_KEEPERR))
2019 /* my_exit() was called */
2020 PL_curstash = PL_defstash;
2023 if (PL_statusvalue && !(PL_exit_flags & PERL_EXIT_EXPECTED))
2024 Perl_croak(aTHX_ "Callback called exit");
2029 PL_op = PL_restartop;
2033 PL_stack_sp = PL_stack_base + oldmark;
2034 if (flags & G_ARRAY)
2038 *++PL_stack_sp = &PL_sv_undef;
2044 if (flags & G_DISCARD) {
2045 PL_stack_sp = PL_stack_base + oldmark;
2055 =for apidoc p||eval_pv
2057 Tells Perl to C<eval> the given string and return an SV* result.
2063 Perl_eval_pv(pTHX_ const char *p, I32 croak_on_error)
2066 SV* sv = newSVpv(p, 0);
2068 eval_sv(sv, G_SCALAR);
2075 if (croak_on_error && SvTRUE(ERRSV)) {
2077 Perl_croak(aTHX_ SvPVx(ERRSV, n_a));
2083 /* Require a module. */
2086 =head1 Embedding Functions
2088 =for apidoc p||require_pv
2090 Tells Perl to C<require> the file named by the string argument. It is
2091 analogous to the Perl code C<eval "require '$file'">. It's even
2092 implemented that way; consider using load_module instead.
2097 Perl_require_pv(pTHX_ const char *pv)
2101 PUSHSTACKi(PERLSI_REQUIRE);
2103 sv = sv_newmortal();
2104 sv_setpv(sv, "require '");
2107 eval_sv(sv, G_DISCARD);
2113 Perl_magicname(pTHX_ char *sym, char *name, I32 namlen)
2117 if ((gv = gv_fetchpv(sym,TRUE, SVt_PV)))
2118 sv_magic(GvSV(gv), (SV*)gv, PERL_MAGIC_sv, name, namlen);
2122 S_usage(pTHX_ char *name) /* XXX move this out into a module ? */
2124 /* This message really ought to be max 23 lines.
2125 * Removed -h because the user already knows that option. Others? */
2127 static char *usage_msg[] = {
2128 "-0[octal] specify record separator (\\0, if no argument)",
2129 "-a autosplit mode with -n or -p (splits $_ into @F)",
2130 "-C enable native wide character system interfaces",
2131 "-c check syntax only (runs BEGIN and CHECK blocks)",
2132 "-d[:debugger] run program under debugger",
2133 "-D[number/list] set debugging flags (argument is a bit mask or alphabets)",
2134 "-e 'command' one line of program (several -e's allowed, omit programfile)",
2135 "-F/pattern/ split() pattern for -a switch (//'s are optional)",
2136 "-i[extension] edit <> files in place (makes backup if extension supplied)",
2137 "-Idirectory specify @INC/#include directory (several -I's allowed)",
2138 "-l[octal] enable line ending processing, specifies line terminator",
2139 "-[mM][-]module execute `use/no module...' before executing program",
2140 "-n assume 'while (<>) { ... }' loop around program",
2141 "-p assume loop like -n but print line also, like sed",
2142 "-P run program through C preprocessor before compilation",
2143 "-s enable rudimentary parsing for switches after programfile",
2144 "-S look for programfile using PATH environment variable",
2145 "-T enable tainting checks",
2146 "-t enable tainting warnings",
2147 "-u dump core after parsing program",
2148 "-U allow unsafe operations",
2149 "-v print version, subversion (includes VERY IMPORTANT perl info)",
2150 "-V[:variable] print configuration summary (or a single Config.pm variable)",
2151 "-w enable many useful warnings (RECOMMENDED)",
2152 "-W enable all warnings",
2153 "-X disable all warnings",
2154 "-x[directory] strip off text before #!perl line and perhaps cd to directory",
2158 char **p = usage_msg;
2160 PerlIO_printf(PerlIO_stdout(),
2161 "\nUsage: %s [switches] [--] [programfile] [arguments]",
2164 PerlIO_printf(PerlIO_stdout(), "\n %s", *p++);
2167 /* This routine handles any switches that can be given during run */
2170 Perl_moreswitches(pTHX_ char *s)
2180 SvREFCNT_dec(PL_rs);
2181 if (s[1] == 'x' && s[2]) {
2185 for (s += 2, e = s; *e; e++);
2187 flags = PERL_SCAN_SILENT_ILLDIGIT;
2188 rschar = (U32)grok_hex(s, &numlen, &flags, NULL);
2189 if (s + numlen < e) {
2190 rschar = 0; /* Grandfather -0xFOO as -0 -xFOO. */
2194 PL_rs = newSVpvn("", 0);
2195 SvGROW(PL_rs, (STRLEN)(UNISKIP(rschar) + 1));
2196 tmps = (U8*)SvPVX(PL_rs);
2197 uvchr_to_utf8(tmps, rschar);
2198 SvCUR_set(PL_rs, UNISKIP(rschar));
2203 rschar = (U32)grok_oct(s, &numlen, &flags, NULL);
2204 if (rschar & ~((U8)~0))
2205 PL_rs = &PL_sv_undef;
2206 else if (!rschar && numlen >= 2)
2207 PL_rs = newSVpvn("", 0);
2209 char ch = (char)rschar;
2210 PL_rs = newSVpvn(&ch, 1);
2217 PL_unicode = parse_unicode_opts(&s);
2222 while (*s && !isSPACE(*s)) ++s;
2224 PL_splitstr = savepv(PL_splitstr);
2237 /* The following permits -d:Mod to accepts arguments following an =
2238 in the fashion that -MSome::Mod does. */
2239 if (*s == ':' || *s == '=') {
2242 sv = newSVpv("use Devel::", 0);
2244 /* We now allow -d:Module=Foo,Bar */
2245 while(isALNUM(*s) || *s==':') ++s;
2247 sv_catpv(sv, start);
2249 sv_catpvn(sv, start, s-start);
2250 sv_catpv(sv, " split(/,/,q{");
2255 my_setenv("PERL5DB", SvPV(sv, PL_na));
2258 PL_perldb = PERLDB_ALL;
2266 if (isALPHA(s[1])) {
2267 /* if adding extra options, remember to update DEBUG_MASK */
2268 static char debopts[] = "psltocPmfrxu HXDSTRJvC";
2271 for (s++; *s && (d = strchr(debopts,*s)); s++)
2272 PL_debug |= 1 << (d - debopts);
2275 PL_debug = atoi(s+1);
2276 for (s++; isDIGIT(*s); s++) ;
2279 if (DEBUG_p_TEST_ && ckWARN_d(WARN_DEBUGGING))
2280 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING),
2281 "-Dp not implemented on this platform\n");
2283 PL_debug |= DEBUG_TOP_FLAG;
2284 #else /* !DEBUGGING */
2285 if (ckWARN_d(WARN_DEBUGGING))
2286 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING),
2287 "Recompile perl with -DDEBUGGING to use -D switch\n");
2288 for (s++; isALNUM(*s); s++) ;
2294 usage(PL_origargv[0]);
2298 Safefree(PL_inplace);
2299 #if defined(__CYGWIN__) /* do backup extension automagically */
2300 if (*(s+1) == '\0') {
2301 PL_inplace = savepv(".bak");
2304 #endif /* __CYGWIN__ */
2305 PL_inplace = savepv(s+1);
2307 for (s = PL_inplace; *s && !isSPACE(*s); s++) ;
2310 if (*s == '-') /* Additional switches on #! line. */
2314 case 'I': /* -I handled both here and in parse_body() */
2317 while (*s && isSPACE(*s))
2322 /* ignore trailing spaces (possibly followed by other switches) */
2324 for (e = p; *e && !isSPACE(*e); e++) ;
2328 } while (*p && *p != '-');
2329 e = savepvn(s, e-s);
2330 incpush(e, TRUE, TRUE, FALSE);
2337 Perl_croak(aTHX_ "No directory specified for -I");
2343 SvREFCNT_dec(PL_ors_sv);
2348 PL_ors_sv = newSVpvn("\n",1);
2349 numlen = 3 + (*s == '0');
2350 *SvPVX(PL_ors_sv) = (char)grok_oct(s, &numlen, &flags, NULL);
2354 if (RsPARA(PL_rs)) {
2355 PL_ors_sv = newSVpvn("\n\n",2);
2358 PL_ors_sv = newSVsv(PL_rs);
2365 PL_preambleav = newAV();
2367 SV *sv = newSVpvn("use assertions::activate split(/,/,q{",37);
2371 av_push(PL_preambleav, sv);
2374 av_push(PL_preambleav, newSVpvn("use assertions::activate",24));
2377 forbid_setid("-M"); /* XXX ? */
2380 forbid_setid("-m"); /* XXX ? */
2385 /* -M-foo == 'no foo' */
2386 if (*s == '-') { use = "no "; ++s; }
2387 sv = newSVpv(use,0);
2389 /* We allow -M'Module qw(Foo Bar)' */
2390 while(isALNUM(*s) || *s==':') ++s;
2392 sv_catpv(sv, start);
2393 if (*(start-1) == 'm') {
2395 Perl_croak(aTHX_ "Can't use '%c' after -mname", *s);
2396 sv_catpv( sv, " ()");
2400 Perl_croak(aTHX_ "Module name required with -%c option",
2402 sv_catpvn(sv, start, s-start);
2403 sv_catpv(sv, " split(/,/,q{");
2409 PL_preambleav = newAV();
2410 av_push(PL_preambleav, sv);
2413 Perl_croak(aTHX_ "No space allowed after -%c", *(s-1));
2425 PL_doswitches = TRUE;
2439 #ifdef MACOS_TRADITIONAL
2440 Perl_croak(aTHX_ "Believe me, you don't want to use \"-u\" on a Macintosh");
2442 PL_do_undump = TRUE;
2451 PerlIO_printf(PerlIO_stdout(),
2452 Perl_form(aTHX_ "\nThis is perl, v%"VDf" built for %s",
2453 PL_patchlevel, ARCHNAME));
2455 /* Adjust verbose output as in the perl that ships with the DG/UX OS from EMC */
2456 PerlIO_printf(PerlIO_stdout(),
2457 Perl_form(aTHX_ "\nThis is perl, version %vd\n", PL_patchlevel));
2458 PerlIO_printf(PerlIO_stdout(),
2459 Perl_form(aTHX_ " built under %s at %s %s\n",
2460 OSNAME, __DATE__, __TIME__));
2461 PerlIO_printf(PerlIO_stdout(),
2462 Perl_form(aTHX_ " OS Specific Release: %s\n",
2466 #if defined(LOCAL_PATCH_COUNT)
2467 if (LOCAL_PATCH_COUNT > 0)
2468 PerlIO_printf(PerlIO_stdout(),
2469 "\n(with %d registered patch%s, "
2470 "see perl -V for more detail)",
2471 (int)LOCAL_PATCH_COUNT,
2472 (LOCAL_PATCH_COUNT!=1) ? "es" : "");
2475 PerlIO_printf(PerlIO_stdout(),
2476 "\n\nCopyright 1987-2003, Larry Wall\n");
2477 #ifdef MACOS_TRADITIONAL
2478 PerlIO_printf(PerlIO_stdout(),
2479 "\nMac OS port Copyright 1991-2002, Matthias Neeracher;\n"
2480 "maintained by Chris Nandor\n");
2483 PerlIO_printf(PerlIO_stdout(),
2484 "\nMS-DOS port Copyright (c) 1989, 1990, Diomidis Spinellis\n");
2487 PerlIO_printf(PerlIO_stdout(),
2488 "djgpp v2 port (jpl5003c) by Hirofumi Watanabe, 1996\n"
2489 "djgpp v2 port (perl5004+) by Laszlo Molnar, 1997-1999\n");
2492 PerlIO_printf(PerlIO_stdout(),
2493 "\n\nOS/2 port Copyright (c) 1990, 1991, Raymond Chen, Kai Uwe Rommel\n"
2494 "Version 5 port Copyright (c) 1994-2002, Andreas Kaiser, Ilya Zakharevich\n");
2497 PerlIO_printf(PerlIO_stdout(),
2498 "atariST series port, ++jrb bammi@cadence.com\n");
2501 PerlIO_printf(PerlIO_stdout(),
2502 "BeOS port Copyright Tom Spindler, 1997-1999\n");
2505 PerlIO_printf(PerlIO_stdout(),
2506 "MPE/iX port Copyright by Mark Klein and Mark Bixby, 1996-2002\n");
2509 PerlIO_printf(PerlIO_stdout(),
2510 "MVS (OS390) port by Mortice Kern Systems, 1997-1999\n");
2513 PerlIO_printf(PerlIO_stdout(),
2514 "Stratus VOS port by Paul.Green@stratus.com, 1997-2002\n");
2517 PerlIO_printf(PerlIO_stdout(),
2518 "VM/ESA port by Neale Ferguson, 1998-1999\n");
2521 PerlIO_printf(PerlIO_stdout(),
2522 "BS2000 (POSIX) port by Start Amadeus GmbH, 1998-1999\n");
2525 PerlIO_printf(PerlIO_stdout(),
2526 "MiNT port by Guido Flohr, 1997-1999\n");
2529 PerlIO_printf(PerlIO_stdout(),
2530 "EPOC port by Olaf Flebbe, 1999-2002\n");
2533 PerlIO_printf(PerlIO_stdout(),"WINCE port by Rainer Keuchel, 2001-2002\n");
2534 PerlIO_printf(PerlIO_stdout(),"Built on " __DATE__ " " __TIME__ "\n\n");
2537 #ifdef BINARY_BUILD_NOTICE
2538 BINARY_BUILD_NOTICE;
2540 PerlIO_printf(PerlIO_stdout(),
2542 Perl may be copied only under the terms of either the Artistic License or the\n\
2543 GNU General Public License, which may be found in the Perl 5 source kit.\n\n\
2544 Complete documentation for Perl, including FAQ lists, should be found on\n\
2545 this system using `man perl' or `perldoc perl'. If you have access to the\n\
2546 Internet, point your browser at http://www.perl.com/, the Perl Home Page.\n\n");
2549 if (! (PL_dowarn & G_WARN_ALL_MASK))
2550 PL_dowarn |= G_WARN_ON;
2554 PL_dowarn = G_WARN_ALL_ON|G_WARN_ON;
2555 if (!specialWARN(PL_compiling.cop_warnings))
2556 SvREFCNT_dec(PL_compiling.cop_warnings);
2557 PL_compiling.cop_warnings = pWARN_ALL ;
2561 PL_dowarn = G_WARN_ALL_OFF;
2562 if (!specialWARN(PL_compiling.cop_warnings))
2563 SvREFCNT_dec(PL_compiling.cop_warnings);
2564 PL_compiling.cop_warnings = pWARN_NONE ;
2569 if (s[1] == '-') /* Additional switches on #! line. */
2574 #if defined(WIN32) || !defined(PERL_STRICT_CR)
2580 #ifdef ALTERNATE_SHEBANG
2581 case 'S': /* OS/2 needs -S on "extproc" line. */
2589 Perl_croak(aTHX_ "Can't emulate -%.1s on #! line",s);
2594 /* compliments of Tom Christiansen */
2596 /* unexec() can be found in the Gnu emacs distribution */
2597 /* Known to work with -DUNEXEC and using unexelf.c from GNU emacs-20.2 */
2600 Perl_my_unexec(pTHX)
2608 prog = newSVpv(BIN_EXP, 0);
2609 sv_catpv(prog, "/perl");
2610 file = newSVpv(PL_origfilename, 0);
2611 sv_catpv(file, ".perldump");
2613 unexec(SvPVX(file), SvPVX(prog), &etext, sbrk(0), 0);
2614 /* unexec prints msg to stderr in case of failure */
2615 PerlProc_exit(status);
2618 # include <lib$routines.h>
2619 lib$signal(SS$_DEBUG); /* ssdef.h #included from vmsish.h */
2621 ABORT(); /* for use with undump */
2626 /* initialize curinterp */
2632 # define PERLVAR(var,type)
2633 # define PERLVARA(var,n,type)
2634 # if defined(PERL_IMPLICIT_CONTEXT)
2635 # if defined(USE_5005THREADS)
2636 # define PERLVARI(var,type,init) PERL_GET_INTERP->var = init;
2637 # define PERLVARIC(var,type,init) PERL_GET_INTERP->var = init;
2638 # else /* !USE_5005THREADS */
2639 # define PERLVARI(var,type,init) aTHX->var = init;
2640 # define PERLVARIC(var,type,init) aTHX->var = init;
2641 # endif /* USE_5005THREADS */
2643 # define PERLVARI(var,type,init) PERL_GET_INTERP->var = init;
2644 # define PERLVARIC(var,type,init) PERL_GET_INTERP->var = init;
2646 # include "intrpvar.h"
2647 # ifndef USE_5005THREADS
2648 # include "thrdvar.h"
2655 # define PERLVAR(var,type)
2656 # define PERLVARA(var,n,type)
2657 # define PERLVARI(var,type,init) PL_##var = init;
2658 # define PERLVARIC(var,type,init) PL_##var = init;
2659 # include "intrpvar.h"
2660 # ifndef USE_5005THREADS
2661 # include "thrdvar.h"
2672 S_init_main_stash(pTHX)
2676 PL_curstash = PL_defstash = newHV();
2677 PL_curstname = newSVpvn("main",4);
2678 gv = gv_fetchpv("main::",TRUE, SVt_PVHV);
2679 SvREFCNT_dec(GvHV(gv));
2680 GvHV(gv) = (HV*)SvREFCNT_inc(PL_defstash);
2682 HvNAME(PL_defstash) = savepv("main");
2683 PL_incgv = gv_HVadd(gv_AVadd(gv_fetchpv("INC",TRUE, SVt_PVAV)));
2684 GvMULTI_on(PL_incgv);
2685 PL_hintgv = gv_fetchpv("\010",TRUE, SVt_PV); /* ^H */
2686 GvMULTI_on(PL_hintgv);
2687 PL_defgv = gv_fetchpv("_",TRUE, SVt_PVAV);
2688 PL_errgv = gv_HVadd(gv_fetchpv("@", TRUE, SVt_PV));
2689 GvMULTI_on(PL_errgv);
2690 PL_replgv = gv_fetchpv("\022", TRUE, SVt_PV); /* ^R */
2691 GvMULTI_on(PL_replgv);
2692 (void)Perl_form(aTHX_ "%240s",""); /* Preallocate temp - for immediate signals. */
2693 sv_grow(ERRSV, 240); /* Preallocate - for immediate signals. */
2694 sv_setpvn(ERRSV, "", 0);
2695 PL_curstash = PL_defstash;
2696 CopSTASH_set(&PL_compiling, PL_defstash);
2697 PL_debstash = GvHV(gv_fetchpv("DB::", GV_ADDMULTI, SVt_PVHV));
2698 PL_globalstash = GvHV(gv_fetchpv("CORE::GLOBAL::", GV_ADDMULTI, SVt_PVHV));
2699 /* We must init $/ before switches are processed. */
2700 sv_setpvn(get_sv("/", TRUE), "\n", 1);
2704 S_open_script(pTHX_ char *scriptname, bool dosearch, SV *sv, int *fdscript)
2708 char *cpp_discard_flag;
2714 PL_origfilename = savepv("-e");
2717 /* if find_script() returns, it returns a malloc()-ed value */
2718 PL_origfilename = scriptname = find_script(scriptname, dosearch, NULL, 1);
2720 if (strnEQ(scriptname, "/dev/fd/", 8) && isDIGIT(scriptname[8]) ) {
2721 char *s = scriptname + 8;
2722 *fdscript = atoi(s);
2726 scriptname = savepv(s + 1);
2727 Safefree(PL_origfilename);
2728 PL_origfilename = scriptname;
2733 CopFILE_free(PL_curcop);
2734 CopFILE_set(PL_curcop, PL_origfilename);
2735 if (strEQ(PL_origfilename,"-"))
2737 if (*fdscript >= 0) {
2738 PL_rsfp = PerlIO_fdopen(*fdscript,PERL_SCRIPT_MODE);
2739 # if defined(HAS_FCNTL) && defined(F_SETFD)
2741 /* ensure close-on-exec */
2742 fcntl(PerlIO_fileno(PL_rsfp),F_SETFD,1);
2745 else if (PL_preprocess) {
2746 char *cpp_cfg = CPPSTDIN;
2747 SV *cpp = newSVpvn("",0);
2748 SV *cmd = NEWSV(0,0);
2750 if (strEQ(cpp_cfg, "cppstdin"))
2751 Perl_sv_catpvf(aTHX_ cpp, "%s/", BIN_EXP);
2752 sv_catpv(cpp, cpp_cfg);
2755 sv_catpvn(sv, "-I", 2);
2756 sv_catpv(sv,PRIVLIB_EXP);
2759 DEBUG_P(PerlIO_printf(Perl_debug_log,
2760 "PL_preprocess: scriptname=\"%s\", cpp=\"%s\", sv=\"%s\", CPPMINUS=\"%s\"\n",
2761 scriptname, SvPVX (cpp), SvPVX (sv), CPPMINUS));
2763 # if defined(MSDOS) || defined(WIN32) || defined(VMS)
2770 cpp_discard_flag = "";
2772 cpp_discard_flag = "-C";
2776 perl = os2_execname(aTHX);
2778 perl = PL_origargv[0];
2782 /* This strips off Perl comments which might interfere with
2783 the C pre-processor, including #!. #line directives are
2784 deliberately stripped to avoid confusion with Perl's version
2785 of #line. FWP played some golf with it so it will fit
2786 into VMS's 255 character buffer.
2789 code = "(1../^#!.*perl/i)|/^\\s*#(?!\\s*((ifn?|un)def|(el|end)?if|define|include|else|error|pragma)\\b)/||!($|=1)||print";
2791 code = "/^\\s*#(?!\\s*((ifn?|un)def|(el|end)?if|define|include|else|error|pragma)\\b)/||!($|=1)||print";
2793 Perl_sv_setpvf(aTHX_ cmd, "\
2794 %s -ne%s%s%s %s | %"SVf" %s %"SVf" %s",
2795 perl, quote, code, quote, scriptname, cpp,
2796 cpp_discard_flag, sv, CPPMINUS);
2798 PL_doextract = FALSE;
2799 # ifdef IAMSUID /* actually, this is caught earlier */
2800 if (PL_euid != PL_uid && !PL_euid) { /* if running suidperl */
2802 (void)seteuid(PL_uid); /* musn't stay setuid root */
2804 # ifdef HAS_SETREUID
2805 (void)setreuid((Uid_t)-1, PL_uid);
2807 # ifdef HAS_SETRESUID
2808 (void)setresuid((Uid_t)-1, PL_uid, (Uid_t)-1);
2810 PerlProc_setuid(PL_uid);
2814 if (PerlProc_geteuid() != PL_uid)
2815 Perl_croak(aTHX_ "Can't do seteuid!\n");
2817 # endif /* IAMSUID */
2819 DEBUG_P(PerlIO_printf(Perl_debug_log,
2820 "PL_preprocess: cmd=\"%s\"\n",
2823 PL_rsfp = PerlProc_popen(SvPVX(cmd), "r");
2827 else if (!*scriptname) {
2828 forbid_setid("program input from stdin");
2829 PL_rsfp = PerlIO_stdin();
2832 PL_rsfp = PerlIO_open(scriptname,PERL_SCRIPT_MODE);
2833 # if defined(HAS_FCNTL) && defined(F_SETFD)
2835 /* ensure close-on-exec */
2836 fcntl(PerlIO_fileno(PL_rsfp),F_SETFD,1);
2841 # ifndef IAMSUID /* in case script is not readable before setuid */
2843 PerlLIO_stat(CopFILE(PL_curcop),&PL_statbuf) >= 0 &&
2844 PL_statbuf.st_mode & (S_ISUID|S_ISGID))
2847 PerlProc_execv(Perl_form(aTHX_ "%s/sperl"PERL_FS_VER_FMT,
2848 BIN_EXP, (int)PERL_REVISION,
2850 (int)PERL_SUBVERSION), PL_origargv);
2851 Perl_croak(aTHX_ "Can't do setuid\n");
2857 Perl_croak(aTHX_ "Can't open perl script: %s\n",
2860 Perl_croak(aTHX_ "Can't open perl script \"%s\": %s\n",
2861 CopFILE(PL_curcop), Strerror(errno));
2867 * I_SYSSTATVFS HAS_FSTATVFS
2869 * I_STATFS HAS_FSTATFS HAS_GETFSSTAT
2870 * I_MNTENT HAS_GETMNTENT HAS_HASMNTOPT
2871 * here so that metaconfig picks them up. */
2875 S_fd_on_nosuid_fs(pTHX_ int fd)
2877 int check_okay = 0; /* able to do all the required sys/libcalls */
2878 int on_nosuid = 0; /* the fd is on a nosuid fs */
2880 * Preferred order: fstatvfs(), fstatfs(), ustat()+getmnt(), getmntent().
2881 * fstatvfs() is UNIX98.
2882 * fstatfs() is 4.3 BSD.
2883 * ustat()+getmnt() is pre-4.3 BSD.
2884 * getmntent() is O(number-of-mounted-filesystems) and can hang on
2885 * an irrelevant filesystem while trying to reach the right one.
2888 #undef FD_ON_NOSUID_CHECK_OKAY /* found the syscalls to do the check? */
2890 # if !defined(FD_ON_NOSUID_CHECK_OKAY) && \
2891 defined(HAS_FSTATVFS)
2892 # define FD_ON_NOSUID_CHECK_OKAY
2893 struct statvfs stfs;
2895 check_okay = fstatvfs(fd, &stfs) == 0;
2896 on_nosuid = check_okay && (stfs.f_flag & ST_NOSUID);
2897 # endif /* fstatvfs */
2899 # if !defined(FD_ON_NOSUID_CHECK_OKAY) && \
2900 defined(PERL_MOUNT_NOSUID) && \
2901 defined(HAS_FSTATFS) && \
2902 defined(HAS_STRUCT_STATFS) && \
2903 defined(HAS_STRUCT_STATFS_F_FLAGS)
2904 # define FD_ON_NOSUID_CHECK_OKAY
2907 check_okay = fstatfs(fd, &stfs) == 0;
2908 on_nosuid = check_okay && (stfs.f_flags & PERL_MOUNT_NOSUID);
2909 # endif /* fstatfs */
2911 # if !defined(FD_ON_NOSUID_CHECK_OKAY) && \
2912 defined(PERL_MOUNT_NOSUID) && \
2913 defined(HAS_FSTAT) && \
2914 defined(HAS_USTAT) && \
2915 defined(HAS_GETMNT) && \
2916 defined(HAS_STRUCT_FS_DATA) && \
2918 # define FD_ON_NOSUID_CHECK_OKAY
2921 if (fstat(fd, &fdst) == 0) {
2923 if (ustat(fdst.st_dev, &us) == 0) {
2925 /* NOSTAT_ONE here because we're not examining fields which
2926 * vary between that case and STAT_ONE. */
2927 if (getmnt((int*)0, &fsd, (int)0, NOSTAT_ONE, us.f_fname) == 0) {
2928 size_t cmplen = sizeof(us.f_fname);
2929 if (sizeof(fsd.fd_req.path) < cmplen)
2930 cmplen = sizeof(fsd.fd_req.path);
2931 if (strnEQ(fsd.fd_req.path, us.f_fname, cmplen) &&
2932 fdst.st_dev == fsd.fd_req.dev) {
2934 on_nosuid = fsd.fd_req.flags & PERL_MOUNT_NOSUID;
2940 # endif /* fstat+ustat+getmnt */
2942 # if !defined(FD_ON_NOSUID_CHECK_OKAY) && \
2943 defined(HAS_GETMNTENT) && \
2944 defined(HAS_HASMNTOPT) && \
2945 defined(MNTOPT_NOSUID)
2946 # define FD_ON_NOSUID_CHECK_OKAY
2947 FILE *mtab = fopen("/etc/mtab", "r");
2948 struct mntent *entry;
2951 if (mtab && (fstat(fd, &stb) == 0)) {
2952 while (entry = getmntent(mtab)) {
2953 if (stat(entry->mnt_dir, &fsb) == 0
2954 && fsb.st_dev == stb.st_dev)
2956 /* found the filesystem */
2958 if (hasmntopt(entry, MNTOPT_NOSUID))
2961 } /* A single fs may well fail its stat(). */
2966 # endif /* getmntent+hasmntopt */
2969 Perl_croak(aTHX_ "Can't check filesystem of script \"%s\" for nosuid", PL_origfilename);
2972 #endif /* IAMSUID */
2975 S_validate_suid(pTHX_ char *validarg, char *scriptname, int fdscript)
2981 /* do we need to emulate setuid on scripts? */
2983 /* This code is for those BSD systems that have setuid #! scripts disabled
2984 * in the kernel because of a security problem. Merely defining DOSUID
2985 * in perl will not fix that problem, but if you have disabled setuid
2986 * scripts in the kernel, this will attempt to emulate setuid and setgid
2987 * on scripts that have those now-otherwise-useless bits set. The setuid
2988 * root version must be called suidperl or sperlN.NNN. If regular perl
2989 * discovers that it has opened a setuid script, it calls suidperl with
2990 * the same argv that it had. If suidperl finds that the script it has
2991 * just opened is NOT setuid root, it sets the effective uid back to the
2992 * uid. We don't just make perl setuid root because that loses the
2993 * effective uid we had before invoking perl, if it was different from the
2996 * DOSUID must be defined in both perl and suidperl, and IAMSUID must
2997 * be defined in suidperl only. suidperl must be setuid root. The
2998 * Configure script will set this up for you if you want it.
3004 if (PerlLIO_fstat(PerlIO_fileno(PL_rsfp),&PL_statbuf) < 0) /* normal stat is insecure */
3005 Perl_croak(aTHX_ "Can't stat script \"%s\"",PL_origfilename);
3006 if (fdscript < 0 && PL_statbuf.st_mode & (S_ISUID|S_ISGID)) {
3011 #ifndef HAS_SETREUID
3012 /* On this access check to make sure the directories are readable,
3013 * there is actually a small window that the user could use to make
3014 * filename point to an accessible directory. So there is a faint
3015 * chance that someone could execute a setuid script down in a
3016 * non-accessible directory. I don't know what to do about that.
3017 * But I don't think it's too important. The manual lies when
3018 * it says access() is useful in setuid programs.
3020 if (PerlLIO_access(CopFILE(PL_curcop),1)) /*double check*/
3021 Perl_croak(aTHX_ "Permission denied");
3023 /* If we can swap euid and uid, then we can determine access rights
3024 * with a simple stat of the file, and then compare device and
3025 * inode to make sure we did stat() on the same file we opened.
3026 * Then we just have to make sure he or she can execute it.
3033 setreuid(PL_euid,PL_uid) < 0
3036 setresuid(PL_euid,PL_uid,(Uid_t)-1) < 0
3039 || PerlProc_getuid() != PL_euid || PerlProc_geteuid() != PL_uid)
3040 Perl_croak(aTHX_ "Can't swap uid and euid"); /* really paranoid */
3041 if (PerlLIO_stat(CopFILE(PL_curcop),&tmpstatbuf) < 0)
3042 Perl_croak(aTHX_ "Permission denied"); /* testing full pathname here */
3043 #if defined(IAMSUID) && !defined(NO_NOSUID_CHECK)
3044 if (fd_on_nosuid_fs(PerlIO_fileno(PL_rsfp)))
3045 Perl_croak(aTHX_ "Permission denied");
3047 if (tmpstatbuf.st_dev != PL_statbuf.st_dev ||
3048 tmpstatbuf.st_ino != PL_statbuf.st_ino) {
3049 (void)PerlIO_close(PL_rsfp);
3050 Perl_croak(aTHX_ "Permission denied\n");
3054 setreuid(PL_uid,PL_euid) < 0
3056 # if defined(HAS_SETRESUID)
3057 setresuid(PL_uid,PL_euid,(Uid_t)-1) < 0
3060 || PerlProc_getuid() != PL_uid || PerlProc_geteuid() != PL_euid)
3061 Perl_croak(aTHX_ "Can't reswap uid and euid");
3062 if (!cando(S_IXUSR,FALSE,&PL_statbuf)) /* can real uid exec? */
3063 Perl_croak(aTHX_ "Permission denied\n");
3065 #endif /* HAS_SETREUID */
3066 #endif /* IAMSUID */
3068 if (!S_ISREG(PL_statbuf.st_mode))
3069 Perl_croak(aTHX_ "Permission denied");
3070 if (PL_statbuf.st_mode & S_IWOTH)
3071 Perl_croak(aTHX_ "Setuid/gid script is writable by world");
3072 PL_doswitches = FALSE; /* -s is insecure in suid */
3073 CopLINE_inc(PL_curcop);
3074 if (sv_gets(PL_linestr, PL_rsfp, 0) == Nullch ||
3075 strnNE(SvPV(PL_linestr,n_a),"#!",2) ) /* required even on Sys V */
3076 Perl_croak(aTHX_ "No #! line");
3077 s = SvPV(PL_linestr,n_a)+2;
3079 while (!isSPACE(*s)) s++;
3080 for (s2 = s; (s2 > SvPV(PL_linestr,n_a)+2 &&
3081 (isDIGIT(s2[-1]) || strchr("._-", s2[-1]))); s2--) ;
3082 if (strnNE(s2-4,"perl",4) && strnNE(s-9,"perl",4)) /* sanity check */
3083 Perl_croak(aTHX_ "Not a perl script");
3084 while (*s == ' ' || *s == '\t') s++;
3086 * #! arg must be what we saw above. They can invoke it by
3087 * mentioning suidperl explicitly, but they may not add any strange
3088 * arguments beyond what #! says if they do invoke suidperl that way.
3090 len = strlen(validarg);
3091 if (strEQ(validarg," PHOOEY ") ||
3092 strnNE(s,validarg,len) || !isSPACE(s[len]))
3093 Perl_croak(aTHX_ "Args must match #! line");
3096 if (PL_euid != PL_uid && (PL_statbuf.st_mode & S_ISUID) &&
3097 PL_euid == PL_statbuf.st_uid)
3099 Perl_croak(aTHX_ "YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
3100 FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
3101 #endif /* IAMSUID */
3103 if (PL_euid) { /* oops, we're not the setuid root perl */
3104 (void)PerlIO_close(PL_rsfp);
3107 PerlProc_execv(Perl_form(aTHX_ "%s/sperl"PERL_FS_VER_FMT, BIN_EXP,
3108 (int)PERL_REVISION, (int)PERL_VERSION,
3109 (int)PERL_SUBVERSION), PL_origargv);
3111 Perl_croak(aTHX_ "Can't do setuid\n");
3114 if (PL_statbuf.st_mode & S_ISGID && PL_statbuf.st_gid != PL_egid) {
3116 (void)setegid(PL_statbuf.st_gid);
3119 (void)setregid((Gid_t)-1,PL_statbuf.st_gid);
3121 #ifdef HAS_SETRESGID
3122 (void)setresgid((Gid_t)-1,PL_statbuf.st_gid,(Gid_t)-1);
3124 PerlProc_setgid(PL_statbuf.st_gid);
3128 if (PerlProc_getegid() != PL_statbuf.st_gid)
3129 Perl_croak(aTHX_ "Can't do setegid!\n");
3131 if (PL_statbuf.st_mode & S_ISUID) {
3132 if (PL_statbuf.st_uid != PL_euid)
3134 (void)seteuid(PL_statbuf.st_uid); /* all that for this */
3137 (void)setreuid((Uid_t)-1,PL_statbuf.st_uid);
3139 #ifdef HAS_SETRESUID
3140 (void)setresuid((Uid_t)-1,PL_statbuf.st_uid,(Uid_t)-1);
3142 PerlProc_setuid(PL_statbuf.st_uid);
3146 if (PerlProc_geteuid() != PL_statbuf.st_uid)
3147 Perl_croak(aTHX_ "Can't do seteuid!\n");
3149 else if (PL_uid) { /* oops, mustn't run as root */
3151 (void)seteuid((Uid_t)PL_uid);
3154 (void)setreuid((Uid_t)-1,(Uid_t)PL_uid);
3156 #ifdef HAS_SETRESUID
3157 (void)setresuid((Uid_t)-1,(Uid_t)PL_uid,(Uid_t)-1);
3159 PerlProc_setuid((Uid_t)PL_uid);
3163 if (PerlProc_geteuid() != PL_uid)
3164 Perl_croak(aTHX_ "Can't do seteuid!\n");
3167 if (!cando(S_IXUSR,TRUE,&PL_statbuf))
3168 Perl_croak(aTHX_ "Permission denied\n"); /* they can't do this */
3171 else if (PL_preprocess)
3172 Perl_croak(aTHX_ "-P not allowed for setuid/setgid script\n");
3173 else if (fdscript >= 0)
3174 Perl_croak(aTHX_ "fd script not allowed in suidperl\n");
3176 Perl_croak(aTHX_ "Script is not setuid/setgid in suidperl\n");
3178 /* We absolutely must clear out any saved ids here, so we */
3179 /* exec the real perl, substituting fd script for scriptname. */
3180 /* (We pass script name as "subdir" of fd, which perl will grok.) */
3181 PerlIO_rewind(PL_rsfp);
3182 PerlLIO_lseek(PerlIO_fileno(PL_rsfp),(Off_t)0,0); /* just in case rewind didn't */
3183 for (which = 1; PL_origargv[which] && PL_origargv[which] != scriptname; which++) ;
3184 if (!PL_origargv[which])
3185 Perl_croak(aTHX_ "Permission denied");
3186 PL_origargv[which] = savepv(Perl_form(aTHX_ "/dev/fd/%d/%s",
3187 PerlIO_fileno(PL_rsfp), PL_origargv[which]));
3188 #if defined(HAS_FCNTL) && defined(F_SETFD)
3189 fcntl(PerlIO_fileno(PL_rsfp),F_SETFD,0); /* ensure no close-on-exec */
3191 PerlProc_execv(Perl_form(aTHX_ "%s/perl"PERL_FS_VER_FMT, BIN_EXP,
3192 (int)PERL_REVISION, (int)PERL_VERSION,
3193 (int)PERL_SUBVERSION), PL_origargv);/* try again */
3194 Perl_croak(aTHX_ "Can't do setuid\n");
3195 #endif /* IAMSUID */
3197 if (PL_euid != PL_uid || PL_egid != PL_gid) { /* (suidperl doesn't exist, in fact) */
3198 #ifndef SETUID_SCRIPTS_ARE_SECURE_NOW
3199 PerlLIO_fstat(PerlIO_fileno(PL_rsfp),&PL_statbuf); /* may be either wrapped or real suid */
3200 if ((PL_euid != PL_uid && PL_euid == PL_statbuf.st_uid && PL_statbuf.st_mode & S_ISUID)
3202 (PL_egid != PL_gid && PL_egid == PL_statbuf.st_gid && PL_statbuf.st_mode & S_ISGID)
3205 Perl_croak(aTHX_ "YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
3206 FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
3207 #endif /* SETUID_SCRIPTS_ARE_SECURE_NOW */
3208 /* not set-id, must be wrapped */
3214 S_find_beginning(pTHX)
3216 register char *s, *s2;
3217 #ifdef MACOS_TRADITIONAL
3221 /* skip forward in input to the real script? */
3224 #ifdef MACOS_TRADITIONAL
3225 /* Since the Mac OS does not honor #! arguments for us, we do it ourselves */
3227 while (PL_doextract || gMacPerl_AlwaysExtract) {
3228 if ((s = sv_gets(PL_linestr, PL_rsfp, 0)) == Nullch) {
3229 if (!gMacPerl_AlwaysExtract)
3230 Perl_croak(aTHX_ "No Perl script found in input\n");
3232 if (PL_doextract) /* require explicit override ? */
3233 if (!OverrideExtract(PL_origfilename))
3234 Perl_croak(aTHX_ "User aborted script\n");
3236 PL_doextract = FALSE;
3238 /* Pater peccavi, file does not have #! */
3239 PerlIO_rewind(PL_rsfp);
3244 while (PL_doextract) {
3245 if ((s = sv_gets(PL_linestr, PL_rsfp, 0)) == Nullch)
3246 Perl_croak(aTHX_ "No Perl script found in input\n");
3249 if (*s == '#' && s[1] == '!' && ((s = instr(s,"perl")) || (s = instr(s2,"PERL")))) {
3250 PerlIO_ungetc(PL_rsfp, '\n'); /* to keep line count right */
3251 PL_doextract = FALSE;
3252 while (*s && !(isSPACE (*s) || *s == '#')) s++;
3254 while (*s == ' ' || *s == '\t') s++;
3256 while (isDIGIT(s2[-1]) || strchr("-._", s2[-1])) s2--;
3257 if (strnEQ(s2-4,"perl",4))
3259 while ((s = moreswitches(s)))
3262 #ifdef MACOS_TRADITIONAL
3263 /* We are always searching for the #!perl line in MacPerl,
3264 * so if we find it, still keep the line count correct
3265 * by counting lines we already skipped over
3267 for (; maclines > 0 ; maclines--)
3268 PerlIO_ungetc(PL_rsfp, '\n');
3272 /* gMacPerl_AlwaysExtract is false in MPW tool */
3273 } else if (gMacPerl_AlwaysExtract) {
3284 PL_uid = PerlProc_getuid();
3285 PL_euid = PerlProc_geteuid();
3286 PL_gid = PerlProc_getgid();
3287 PL_egid = PerlProc_getegid();
3289 PL_uid |= PL_gid << 16;
3290 PL_euid |= PL_egid << 16;
3292 /* Should not happen: */
3293 CHECK_MALLOC_TAINT(PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
3294 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
3298 /* This is used very early in the lifetime of the program. */
3300 Perl_doing_taint(int argc, char *argv[], char *envp[])
3302 int uid = PerlProc_getuid();
3303 int euid = PerlProc_geteuid();
3304 int gid = PerlProc_getgid();
3305 int egid = PerlProc_getegid();
3311 if (uid && (euid != uid || egid != gid))
3313 /* This is a really primitive check; $ENV{PERL_MALLOC_OPT} is
3314 ignored only if -T are the first chars together; otherwise one
3315 gets "Too late" message. */
3316 if ( argc > 1 && argv[1][0] == '-'
3317 && (argv[1][1] == 't' || argv[1][1] == 'T') )
3324 S_forbid_setid(pTHX_ char *s)
3326 if (PL_euid != PL_uid)
3327 Perl_croak(aTHX_ "No %s allowed while running setuid", s);
3328 if (PL_egid != PL_gid)
3329 Perl_croak(aTHX_ "No %s allowed while running setgid", s);
3333 Perl_init_debugger(pTHX)
3335 HV *ostash = PL_curstash;
3337 PL_curstash = PL_debstash;
3338 PL_dbargs = GvAV(gv_AVadd((gv_fetchpv("args", GV_ADDMULTI, SVt_PVAV))));
3339 AvREAL_off(PL_dbargs);
3340 PL_DBgv = gv_fetchpv("DB", GV_ADDMULTI, SVt_PVGV);
3341 PL_DBline = gv_fetchpv("dbline", GV_ADDMULTI, SVt_PVAV);
3342 PL_DBsub = gv_HVadd(gv_fetchpv("sub", GV_ADDMULTI, SVt_PVHV));
3343 sv_upgrade(GvSV(PL_DBsub), SVt_IV); /* IVX accessed if PERLDB_SUB_NN */
3344 PL_DBsingle = GvSV((gv_fetchpv("single", GV_ADDMULTI, SVt_PV)));
3345 sv_setiv(PL_DBsingle, 0);
3346 PL_DBtrace = GvSV((gv_fetchpv("trace", GV_ADDMULTI, SVt_PV)));
3347 sv_setiv(PL_DBtrace, 0);
3348 PL_DBsignal = GvSV((gv_fetchpv("signal", GV_ADDMULTI, SVt_PV)));
3349 sv_setiv(PL_DBsignal, 0);
3350 PL_DBassertion = GvSV((gv_fetchpv("assertion", GV_ADDMULTI, SVt_PV)));
3351 sv_setiv(PL_DBassertion, 0);
3352 PL_curstash = ostash;
3355 #ifndef STRESS_REALLOC
3356 #define REASONABLE(size) (size)
3358 #define REASONABLE(size) (1) /* unreasonable */
3362 Perl_init_stacks(pTHX)
3364 /* start with 128-item stack and 8K cxstack */
3365 PL_curstackinfo = new_stackinfo(REASONABLE(128),
3366 REASONABLE(8192/sizeof(PERL_CONTEXT) - 1));
3367 PL_curstackinfo->si_type = PERLSI_MAIN;
3368 PL_curstack = PL_curstackinfo->si_stack;
3369 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
3371 PL_stack_base = AvARRAY(PL_curstack);
3372 PL_stack_sp = PL_stack_base;
3373 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
3375 New(50,PL_tmps_stack,REASONABLE(128),SV*);
3378 PL_tmps_max = REASONABLE(128);
3380 New(54,PL_markstack,REASONABLE(32),I32);
3381 PL_markstack_ptr = PL_markstack;
3382 PL_markstack_max = PL_markstack + REASONABLE(32);
3386 New(54,PL_scopestack,REASONABLE(32),I32);
3387 PL_scopestack_ix = 0;
3388 PL_scopestack_max = REASONABLE(32);
3390 New(54,PL_savestack,REASONABLE(128),ANY);
3391 PL_savestack_ix = 0;
3392 PL_savestack_max = REASONABLE(128);
3394 New(54,PL_retstack,REASONABLE(16),OP*);
3396 PL_retstack_max = REASONABLE(16);
3404 while (PL_curstackinfo->si_next)
3405 PL_curstackinfo = PL_curstackinfo->si_next;
3406 while (PL_curstackinfo) {
3407 PERL_SI *p = PL_curstackinfo->si_prev;
3408 /* curstackinfo->si_stack got nuked by sv_free_arenas() */
3409 Safefree(PL_curstackinfo->si_cxstack);
3410 Safefree(PL_curstackinfo);
3411 PL_curstackinfo = p;
3413 Safefree(PL_tmps_stack);
3414 Safefree(PL_markstack);
3415 Safefree(PL_scopestack);
3416 Safefree(PL_savestack);
3417 Safefree(PL_retstack);
3426 lex_start(PL_linestr);
3428 PL_subname = newSVpvn("main",4);
3432 S_init_predump_symbols(pTHX)
3437 sv_setpvn(get_sv("\"", TRUE), " ", 1);
3438 PL_stdingv = gv_fetchpv("STDIN",TRUE, SVt_PVIO);
3439 GvMULTI_on(PL_stdingv);
3440 io = GvIOp(PL_stdingv);
3441 IoTYPE(io) = IoTYPE_RDONLY;
3442 IoIFP(io) = PerlIO_stdin();
3443 tmpgv = gv_fetchpv("stdin",TRUE, SVt_PV);
3445 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(io);
3447 tmpgv = gv_fetchpv("STDOUT",TRUE, SVt_PVIO);
3450 IoTYPE(io) = IoTYPE_WRONLY;
3451 IoOFP(io) = IoIFP(io) = PerlIO_stdout();
3453 tmpgv = gv_fetchpv("stdout",TRUE, SVt_PV);
3455 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(io);
3457 PL_stderrgv = gv_fetchpv("STDERR",TRUE, SVt_PVIO);
3458 GvMULTI_on(PL_stderrgv);
3459 io = GvIOp(PL_stderrgv);
3460 IoTYPE(io) = IoTYPE_WRONLY;
3461 IoOFP(io) = IoIFP(io) = PerlIO_stderr();
3462 tmpgv = gv_fetchpv("stderr",TRUE, SVt_PV);
3464 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(io);
3466 PL_statname = NEWSV(66,0); /* last filename we did stat on */
3469 Safefree(PL_osname);
3470 PL_osname = savepv(OSNAME);
3474 Perl_init_argv_symbols(pTHX_ register int argc, register char **argv)
3477 argc--,argv++; /* skip name of script */
3478 if (PL_doswitches) {
3479 for (; argc > 0 && **argv == '-'; argc--,argv++) {
3482 if (argv[0][1] == '-' && !argv[0][2]) {
3486 if ((s = strchr(argv[0], '='))) {
3488 sv_setpv(GvSV(gv_fetchpv(argv[0]+1,TRUE, SVt_PV)),s);
3491 sv_setiv(GvSV(gv_fetchpv(argv[0]+1,TRUE, SVt_PV)),1);
3494 if ((PL_argvgv = gv_fetchpv("ARGV",TRUE, SVt_PVAV))) {
3495 GvMULTI_on(PL_argvgv);
3496 (void)gv_AVadd(PL_argvgv);
3497 av_clear(GvAVn(PL_argvgv));
3498 for (; argc > 0; argc--,argv++) {
3499 SV *sv = newSVpv(argv[0],0);
3500 av_push(GvAVn(PL_argvgv),sv);
3501 if (!(PL_unicode & PERL_UNICODE_LOCALE_FLAG) || PL_utf8locale) {
3502 if (PL_unicode & PERL_UNICODE_ARGV_FLAG)
3505 if (PL_unicode & PERL_UNICODE_WIDESYSCALLS_FLAG) /* Sarathy? */
3506 (void)sv_utf8_decode(sv);
3511 #ifdef HAS_PROCSELFEXE
3512 /* This is a function so that we don't hold on to MAXPATHLEN
3513 bytes of stack longer than necessary
3516 S_procself_val(pTHX_ SV *sv, char *arg0)
3518 char buf[MAXPATHLEN];
3519 int len = readlink(PROCSELFEXE_PATH, buf, sizeof(buf) - 1);
3521 /* On Playstation2 Linux V1.0 (kernel 2.2.1) readlink(/proc/self/exe)
3522 includes a spurious NUL which will cause $^X to fail in system
3523 or backticks (this will prevent extensions from being built and
3524 many tests from working). readlink is not meant to add a NUL.
3525 Normal readlink works fine.
3527 if (len > 0 && buf[len-1] == '\0') {
3531 /* FreeBSD's implementation is acknowledged to be imperfect, sometimes
3532 returning the text "unknown" from the readlink rather than the path
3533 to the executable (or returning an error from the readlink). Any valid
3534 path has a '/' in it somewhere, so use that to validate the result.
3535 See http://www.freebsd.org/cgi/query-pr.cgi?pr=35703
3537 if (len > 0 && memchr(buf, '/', len)) {
3538 sv_setpvn(sv,buf,len);
3544 #endif /* HAS_PROCSELFEXE */
3547 S_init_postdump_symbols(pTHX_ register int argc, register char **argv, register char **env)
3553 PL_toptarget = NEWSV(0,0);
3554 sv_upgrade(PL_toptarget, SVt_PVFM);
3555 sv_setpvn(PL_toptarget, "", 0);
3556 PL_bodytarget = NEWSV(0,0);
3557 sv_upgrade(PL_bodytarget, SVt_PVFM);
3558 sv_setpvn(PL_bodytarget, "", 0);
3559 PL_formtarget = PL_bodytarget;
3563 init_argv_symbols(argc,argv);
3565 if ((tmpgv = gv_fetchpv("0",TRUE, SVt_PV))) {
3566 #ifdef MACOS_TRADITIONAL
3567 /* $0 is not majick on a Mac */
3568 sv_setpv(GvSV(tmpgv),MacPerl_MPWFileName(PL_origfilename));
3570 sv_setpv(GvSV(tmpgv),PL_origfilename);
3571 magicname("0", "0", 1);
3574 if ((tmpgv = gv_fetchpv("\030",TRUE, SVt_PV))) {/* $^X */
3575 #ifdef HAS_PROCSELFEXE
3576 S_procself_val(aTHX_ GvSV(tmpgv), PL_origargv[0]);
3579 sv_setpv(GvSV(tmpgv), os2_execname(aTHX));
3581 sv_setpv(GvSV(tmpgv),PL_origargv[0]);
3585 if ((PL_envgv = gv_fetchpv("ENV",TRUE, SVt_PVHV))) {
3587 GvMULTI_on(PL_envgv);
3588 hv = GvHVn(PL_envgv);
3589 hv_magic(hv, Nullgv, PERL_MAGIC_env);
3590 #ifdef USE_ENVIRON_ARRAY
3591 /* Note that if the supplied env parameter is actually a copy
3592 of the global environ then it may now point to free'd memory
3593 if the environment has been modified since. To avoid this
3594 problem we treat env==NULL as meaning 'use the default'
3599 # ifdef USE_ITHREADS
3600 && PL_curinterp == aTHX
3604 environ[0] = Nullch;
3607 for (; *env; env++) {
3608 if (!(s = strchr(*env,'=')))
3615 sv = newSVpv(s+1, 0);
3616 (void)hv_store(hv, *env, s - *env, sv, 0);
3620 #endif /* USE_ENVIRON_ARRAY */
3623 if ((tmpgv = gv_fetchpv("$",TRUE, SVt_PV))) {
3624 SvREADONLY_off(GvSV(tmpgv));
3625 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3626 SvREADONLY_on(GvSV(tmpgv));
3628 #ifdef THREADS_HAVE_PIDS
3629 PL_ppid = (IV)getppid();
3632 /* touch @F array to prevent spurious warnings 20020415 MJD */
3634 (void) get_av("main::F", TRUE | GV_ADDMULTI);
3636 /* touch @- and @+ arrays to prevent spurious warnings 20020415 MJD */
3637 (void) get_av("main::-", TRUE | GV_ADDMULTI);
3638 (void) get_av("main::+", TRUE | GV_ADDMULTI);
3642 S_init_perllib(pTHX)
3647 s = PerlEnv_getenv("PERL5LIB");
3649 incpush(s, TRUE, TRUE, TRUE);
3651 incpush(PerlEnv_getenv("PERLLIB"), FALSE, FALSE, TRUE);
3653 /* Treat PERL5?LIB as a possible search list logical name -- the
3654 * "natural" VMS idiom for a Unix path string. We allow each
3655 * element to be a set of |-separated directories for compatibility.
3659 if (my_trnlnm("PERL5LIB",buf,0))
3660 do { incpush(buf,TRUE,TRUE,TRUE); } while (my_trnlnm("PERL5LIB",buf,++idx));
3662 while (my_trnlnm("PERLLIB",buf,idx++)) incpush(buf,FALSE,FALSE,TRUE);
3666 /* Use the ~-expanded versions of APPLLIB (undocumented),
3667 ARCHLIB PRIVLIB SITEARCH SITELIB VENDORARCH and VENDORLIB
3670 incpush(APPLLIB_EXP, TRUE, TRUE, TRUE);
3674 incpush(ARCHLIB_EXP, FALSE, FALSE, TRUE);
3676 #ifdef MACOS_TRADITIONAL
3679 SV * privdir = NEWSV(55, 0);
3680 char * macperl = PerlEnv_getenv("MACPERL");
3685 Perl_sv_setpvf(aTHX_ privdir, "%slib:", macperl);
3686 if (PerlLIO_stat(SvPVX(privdir), &tmpstatbuf) >= 0 && S_ISDIR(tmpstatbuf.st_mode))
3687 incpush(SvPVX(privdir), TRUE, FALSE, TRUE);
3688 Perl_sv_setpvf(aTHX_ privdir, "%ssite_perl:", macperl);
3689 if (PerlLIO_stat(SvPVX(privdir), &tmpstatbuf) >= 0 && S_ISDIR(tmpstatbuf.st_mode))
3690 incpush(SvPVX(privdir), TRUE, FALSE, TRUE);
3692 SvREFCNT_dec(privdir);
3695 incpush(":", FALSE, FALSE, TRUE);
3698 # define PRIVLIB_EXP "/usr/local/lib/perl5:/usr/local/lib/perl"
3701 incpush(PRIVLIB_EXP, TRUE, FALSE, TRUE);
3703 incpush(PRIVLIB_EXP, FALSE, FALSE, TRUE);
3707 /* sitearch is always relative to sitelib on Windows for
3708 * DLL-based path intuition to work correctly */
3709 # if !defined(WIN32)
3710 incpush(SITEARCH_EXP, FALSE, FALSE, TRUE);
3716 /* this picks up sitearch as well */
3717 incpush(SITELIB_EXP, TRUE, FALSE, TRUE);
3719 incpush(SITELIB_EXP, FALSE, FALSE, TRUE);
3723 #ifdef SITELIB_STEM /* Search for version-specific dirs below here */
3724 incpush(SITELIB_STEM, FALSE, TRUE, TRUE);
3727 #ifdef PERL_VENDORARCH_EXP
3728 /* vendorarch is always relative to vendorlib on Windows for
3729 * DLL-based path intuition to work correctly */
3730 # if !defined(WIN32)
3731 incpush(PERL_VENDORARCH_EXP, FALSE, FALSE, TRUE);
3735 #ifdef PERL_VENDORLIB_EXP
3737 incpush(PERL_VENDORLIB_EXP, TRUE, FALSE, TRUE); /* this picks up vendorarch as well */
3739 incpush(PERL_VENDORLIB_EXP, FALSE, FALSE, TRUE);
3743 #ifdef PERL_VENDORLIB_STEM /* Search for version-specific dirs below here */
3744 incpush(PERL_VENDORLIB_STEM, FALSE, TRUE, TRUE);
3747 #ifdef PERL_OTHERLIBDIRS
3748 incpush(PERL_OTHERLIBDIRS, TRUE, TRUE, TRUE);
3752 incpush(".", FALSE, FALSE, TRUE);
3753 #endif /* MACOS_TRADITIONAL */
3756 #if defined(DOSISH) || defined(EPOC)
3757 # define PERLLIB_SEP ';'
3760 # define PERLLIB_SEP '|'
3762 # if defined(MACOS_TRADITIONAL)
3763 # define PERLLIB_SEP ','
3765 # define PERLLIB_SEP ':'
3769 #ifndef PERLLIB_MANGLE
3770 # define PERLLIB_MANGLE(s,n) (s)
3774 S_incpush(pTHX_ char *p, int addsubdirs, int addoldvers, int usesep)
3776 SV *subdir = Nullsv;
3781 if (addsubdirs || addoldvers) {
3782 subdir = sv_newmortal();
3785 /* Break at all separators */
3787 SV *libdir = NEWSV(55,0);
3790 /* skip any consecutive separators */
3792 while ( *p == PERLLIB_SEP ) {
3793 /* Uncomment the next line for PATH semantics */
3794 /* av_push(GvAVn(PL_incgv), newSVpvn(".", 1)); */
3799 if ( usesep && (s = strchr(p, PERLLIB_SEP)) != Nullch ) {
3800 sv_setpvn(libdir, PERLLIB_MANGLE(p, (STRLEN)(s - p)),
3805 sv_setpv(libdir, PERLLIB_MANGLE(p, 0));
3806 p = Nullch; /* break out */
3808 #ifdef MACOS_TRADITIONAL
3809 if (!strchr(SvPVX(libdir), ':')) {
3812 sv_setpv(libdir, MacPerl_CanonDir(SvPVX(libdir), buf, 0));
3814 if (SvPVX(libdir)[SvCUR(libdir)-1] != ':')
3815 sv_catpv(libdir, ":");
3819 * BEFORE pushing libdir onto @INC we may first push version- and
3820 * archname-specific sub-directories.
3822 if (addsubdirs || addoldvers) {
3823 #ifdef PERL_INC_VERSION_LIST
3824 /* Configure terminates PERL_INC_VERSION_LIST with a NULL */
3825 const char *incverlist[] = { PERL_INC_VERSION_LIST };
3826 const char **incver;
3833 if ((unix = tounixspec_ts(SvPV(libdir,len),Nullch)) != Nullch) {
3835 while (unix[len-1] == '/') len--; /* Cosmetic */
3836 sv_usepvn(libdir,unix,len);
3839 PerlIO_printf(Perl_error_log,
3840 "Failed to unixify @INC element \"%s\"\n",
3844 #ifdef MACOS_TRADITIONAL
3845 #define PERL_AV_SUFFIX_FMT ""
3846 #define PERL_ARCH_FMT "%s:"
3847 #define PERL_ARCH_FMT_PATH PERL_FS_VER_FMT PERL_AV_SUFFIX_FMT
3849 #define PERL_AV_SUFFIX_FMT "/"
3850 #define PERL_ARCH_FMT "/%s"
3851 #define PERL_ARCH_FMT_PATH PERL_AV_SUFFIX_FMT PERL_FS_VER_FMT
3853 /* .../version/archname if -d .../version/archname */
3854 Perl_sv_setpvf(aTHX_ subdir, "%"SVf PERL_ARCH_FMT_PATH PERL_ARCH_FMT,
3856 (int)PERL_REVISION, (int)PERL_VERSION,
3857 (int)PERL_SUBVERSION, ARCHNAME);
3858 if (PerlLIO_stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
3859 S_ISDIR(tmpstatbuf.st_mode))
3860 av_push(GvAVn(PL_incgv), newSVsv(subdir));
3862 /* .../version if -d .../version */
3863 Perl_sv_setpvf(aTHX_ subdir, "%"SVf PERL_ARCH_FMT_PATH, libdir,
3864 (int)PERL_REVISION, (int)PERL_VERSION,
3865 (int)PERL_SUBVERSION);
3866 if (PerlLIO_stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
3867 S_ISDIR(tmpstatbuf.st_mode))
3868 av_push(GvAVn(PL_incgv), newSVsv(subdir));
3870 /* .../archname if -d .../archname */
3871 Perl_sv_setpvf(aTHX_ subdir, "%"SVf PERL_ARCH_FMT, libdir, ARCHNAME);
3872 if (PerlLIO_stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
3873 S_ISDIR(tmpstatbuf.st_mode))
3874 av_push(GvAVn(PL_incgv), newSVsv(subdir));
3877 #ifdef PERL_INC_VERSION_LIST
3879 for (incver = incverlist; *incver; incver++) {
3880 /* .../xxx if -d .../xxx */
3881 Perl_sv_setpvf(aTHX_ subdir, "%"SVf PERL_ARCH_FMT, libdir, *incver);
3882 if (PerlLIO_stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
3883 S_ISDIR(tmpstatbuf.st_mode))
3884 av_push(GvAVn(PL_incgv), newSVsv(subdir));
3890 /* finally push this lib directory on the end of @INC */
3891 av_push(GvAVn(PL_incgv), libdir);
3895 #ifdef USE_5005THREADS
3896 STATIC struct perl_thread *
3897 S_init_main_thread(pTHX)
3899 #if !defined(PERL_IMPLICIT_CONTEXT)
3900 struct perl_thread *thr;
3904 Newz(53, thr, 1, struct perl_thread);
3905 PL_curcop = &PL_compiling;
3906 thr->interp = PERL_GET_INTERP;
3907 thr->cvcache = newHV();
3908 thr->threadsv = newAV();
3909 /* thr->threadsvp is set when find_threadsv is called */
3910 thr->specific = newAV();
3911 thr->flags = THRf_R_JOINABLE;
3912 MUTEX_INIT(&thr->mutex);
3913 /* Handcraft thrsv similarly to mess_sv */
3914 New(53, PL_thrsv, 1, SV);
3915 Newz(53, xpv, 1, XPV);
3916 SvFLAGS(PL_thrsv) = SVt_PV;
3917 SvANY(PL_thrsv) = (void*)xpv;
3918 SvREFCNT(PL_thrsv) = 1 << 30; /* practically infinite */
3919 SvPVX(PL_thrsv) = (char*)thr;
3920 SvCUR_set(PL_thrsv, sizeof(thr));
3921 SvLEN_set(PL_thrsv, sizeof(thr));
3922 *SvEND(PL_thrsv) = '\0'; /* in the trailing_nul field */
3923 thr->oursv = PL_thrsv;
3924 PL_chopset = " \n-";
3927 MUTEX_LOCK(&PL_threads_mutex);
3933 MUTEX_UNLOCK(&PL_threads_mutex);
3935 #ifdef HAVE_THREAD_INTERN
3936 Perl_init_thread_intern(thr);
3939 #ifdef SET_THREAD_SELF
3940 SET_THREAD_SELF(thr);
3942 thr->self = pthread_self();
3943 #endif /* SET_THREAD_SELF */
3947 * These must come after the thread self setting
3948 * because sv_setpvn does SvTAINT and the taint
3949 * fields thread selfness being set.
3951 PL_toptarget = NEWSV(0,0);
3952 sv_upgrade(PL_toptarget, SVt_PVFM);
3953 sv_setpvn(PL_toptarget, "", 0);
3954 PL_bodytarget = NEWSV(0,0);
3955 sv_upgrade(PL_bodytarget, SVt_PVFM);
3956 sv_setpvn(PL_bodytarget, "", 0);
3957 PL_formtarget = PL_bodytarget;
3958 thr->errsv = newSVpvn("", 0);
3959 (void) find_threadsv("@"); /* Ensure $@ is initialised early */
3962 PL_peepp = MEMBER_TO_FPTR(Perl_peep);
3963 PL_regcompp = MEMBER_TO_FPTR(Perl_pregcomp);
3964 PL_regexecp = MEMBER_TO_FPTR(Perl_regexec_flags);
3965 PL_regint_start = MEMBER_TO_FPTR(Perl_re_intuit_start);
3966 PL_regint_string = MEMBER_TO_FPTR(Perl_re_intuit_string);
3967 PL_regfree = MEMBER_TO_FPTR(Perl_pregfree);
3969 PL_reginterp_cnt = 0;
3973 #endif /* USE_5005THREADS */
3976 Perl_call_list(pTHX_ I32 oldscope, AV *paramList)
3979 line_t oldline = CopLINE(PL_curcop);
3985 while (AvFILL(paramList) >= 0) {
3986 cv = (CV*)av_shift(paramList);
3988 if (paramList == PL_beginav) {
3989 /* save PL_beginav for compiler */
3990 if (! PL_beginav_save)
3991 PL_beginav_save = newAV();
3992 av_push(PL_beginav_save, (SV*)cv);
3994 else if (paramList == PL_checkav) {
3995 /* save PL_checkav for compiler */
3996 if (! PL_checkav_save)
3997 PL_checkav_save = newAV();
3998 av_push(PL_checkav_save, (SV*)cv);
4003 #ifdef PERL_FLEXIBLE_EXCEPTIONS
4004 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vcall_list_body), cv);
4010 #ifndef PERL_FLEXIBLE_EXCEPTIONS
4014 (void)SvPV(atsv, len);
4016 PL_curcop = &PL_compiling;
4017 CopLINE_set(PL_curcop, oldline);
4018 if (paramList == PL_beginav)
4019 sv_catpv(atsv, "BEGIN failed--compilation aborted");
4021 Perl_sv_catpvf(aTHX_ atsv,
4022 "%s failed--call queue aborted",
4023 paramList == PL_checkav ? "CHECK"
4024 : paramList == PL_initav ? "INIT"
4026 while (PL_scopestack_ix > oldscope)
4029 Perl_croak(aTHX_ "%"SVf"", atsv);
4036 /* my_exit() was called */
4037 while (PL_scopestack_ix > oldscope)
4040 PL_curstash = PL_defstash;
4041 PL_curcop = &PL_compiling;
4042 CopLINE_set(PL_curcop, oldline);
4044 if (PL_statusvalue && !(PL_exit_flags & PERL_EXIT_EXPECTED)) {
4045 if (paramList == PL_beginav)
4046 Perl_croak(aTHX_ "BEGIN failed--compilation aborted");
4048 Perl_croak(aTHX_ "%s failed--call queue aborted",
4049 paramList == PL_checkav ? "CHECK"
4050 : paramList == PL_initav ? "INIT"
4057 PL_curcop = &PL_compiling;
4058 CopLINE_set(PL_curcop, oldline);
4061 PerlIO_printf(Perl_error_log, "panic: restartop\n");
4069 #ifdef PERL_FLEXIBLE_EXCEPTIONS
4071 S_vcall_list_body(pTHX_ va_list args)
4073 CV *cv = va_arg(args, CV*);
4074 return call_list_body(cv);
4079 S_call_list_body(pTHX_ CV *cv)
4081 PUSHMARK(PL_stack_sp);
4082 call_sv((SV*)cv, G_EVAL|G_DISCARD);
4087 Perl_my_exit(pTHX_ U32 status)
4089 DEBUG_S(PerlIO_printf(Perl_debug_log, "my_exit: thread %p, status %lu\n",
4090 thr, (unsigned long) status));
4099 STATUS_NATIVE_SET(status);
4106 Perl_my_failure_exit(pTHX)
4109 if (vaxc$errno & 1) {
4110 if (STATUS_NATIVE & 1) /* fortuitiously includes "-1" */
4111 STATUS_NATIVE_SET(44);
4114 if (!vaxc$errno && errno) /* unlikely */
4115 STATUS_NATIVE_SET(44);
4117 STATUS_NATIVE_SET(vaxc$errno);
4122 STATUS_POSIX_SET(errno);
4124 exitstatus = STATUS_POSIX >> 8;
4125 if (exitstatus & 255)
4126 STATUS_POSIX_SET(exitstatus);
4128 STATUS_POSIX_SET(255);
4135 S_my_exit_jump(pTHX)
4137 register PERL_CONTEXT *cx;
4142 SvREFCNT_dec(PL_e_script);
4143 PL_e_script = Nullsv;
4146 POPSTACK_TO(PL_mainstack);
4147 if (cxstack_ix >= 0) {
4150 POPBLOCK(cx,PL_curpm);
4158 read_e_script(pTHX_ int idx, SV *buf_sv, int maxlen)
4161 p = SvPVX(PL_e_script);
4162 nl = strchr(p, '\n');
4163 nl = (nl) ? nl+1 : SvEND(PL_e_script);
4165 filter_del(read_e_script);
4168 sv_catpvn(buf_sv, p, nl-p);
4169 sv_chop(PL_e_script, nl);