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();
278 #if defined(USE_HASH_SEED) || defined(USE_HASH_SEED_EXPLICIT)
279 /* [perl #22371] Algorimic Complexity Attack on Perl 5.6.1, 5.8.0 */
281 char *s = PerlEnv_getenv("PERL_HASH_SEED");
283 while (isSPACE(*s)) s++;
284 if (s && isDIGIT(*s))
285 PL_hash_seed = (UV)atoi(s);
286 #ifndef USE_HASH_SEED_EXPLICIT
288 /* Compute a random seed */
289 (void)seedDrand01((Rand_seed_t)seed());
290 PL_srand_called = TRUE;
291 PL_hash_seed = (UV)(Drand01() * (NV)UV_MAX);
292 #if RANDBITS < (UVSIZE * 8)
294 int skip = (UVSIZE * 8) - RANDBITS;
295 PL_hash_seed >>= skip;
296 /* The low bits might need extra help. */
297 PL_hash_seed += (UV)(Drand01() * ((1 << skip) - 1));
299 #endif /* RANDBITS < (UVSIZE * 8) */
301 #endif /* USE_HASH_SEED_EXPLICIT */
303 #endif /* #if defined(USE_HASH_SEED) || defined(USE_HASH_SEED_EXPLICIT) */
309 =for apidoc nothreadhook
311 Stub that provides thread hook for perl_destruct when there are
318 Perl_nothreadhook(pTHX)
324 =for apidoc perl_destruct
326 Shuts down a Perl interpreter. See L<perlembed>.
334 volatile int destruct_level; /* 0=none, 1=full, 2=full with checks */
336 #ifdef USE_5005THREADS
338 #endif /* USE_5005THREADS */
340 /* wait for all pseudo-forked children to finish */
341 PERL_WAIT_FOR_CHILDREN;
343 destruct_level = PL_perl_destruct_level;
347 if ((s = PerlEnv_getenv("PERL_DESTRUCT_LEVEL"))) {
349 if (destruct_level < i)
356 if(PL_exit_flags & PERL_EXIT_DESTRUCT_END) {
361 if (PL_endav && !PL_minus_c)
362 call_list(PL_scopestack_ix, PL_endav);
368 /* Need to flush since END blocks can produce output */
371 if (CALL_FPTR(PL_threadhook)(aTHX)) {
372 /* Threads hook has vetoed further cleanup */
373 return STATUS_NATIVE_EXPORT;
376 /* We must account for everything. */
378 /* Destroy the main CV and syntax tree */
380 op_free(PL_main_root);
381 PL_main_root = Nullop;
383 PL_curcop = &PL_compiling;
384 PL_main_start = Nullop;
385 SvREFCNT_dec(PL_main_cv);
389 /* Tell PerlIO we are about to tear things apart in case
390 we have layers which are using resources that should
394 PerlIO_destruct(aTHX);
396 if (PL_sv_objcount) {
398 * Try to destruct global references. We do this first so that the
399 * destructors and destructees still exist. Some sv's might remain.
400 * Non-referenced objects are on their own.
405 /* unhook hooks which will soon be, or use, destroyed data */
406 SvREFCNT_dec(PL_warnhook);
407 PL_warnhook = Nullsv;
408 SvREFCNT_dec(PL_diehook);
411 /* call exit list functions */
412 while (PL_exitlistlen-- > 0)
413 PL_exitlist[PL_exitlistlen].fn(aTHX_ PL_exitlist[PL_exitlistlen].ptr);
415 Safefree(PL_exitlist);
420 if (destruct_level == 0){
422 DEBUG_P(debprofdump());
424 #if defined(PERLIO_LAYERS)
425 /* No more IO - including error messages ! */
426 PerlIO_cleanup(aTHX);
429 /* The exit() function will do everything that needs doing. */
430 return STATUS_NATIVE_EXPORT;
433 /* jettison our possibly duplicated environment */
434 /* if PERL_USE_SAFE_PUTENV is defined environ will not have been copied
435 * so we certainly shouldn't free it here
437 #if defined(USE_ENVIRON_ARRAY) && !defined(PERL_USE_SAFE_PUTENV)
438 if (environ != PL_origenviron
440 /* only main thread can free environ[0] contents */
441 && PL_curinterp == aTHX
447 for (i = 0; environ[i]; i++)
448 safesysfree(environ[i]);
450 /* Must use safesysfree() when working with environ. */
451 safesysfree(environ);
453 environ = PL_origenviron;
458 /* the syntax tree is shared between clones
459 * so op_free(PL_main_root) only ReREFCNT_dec's
460 * REGEXPs in the parent interpreter
461 * we need to manually ReREFCNT_dec for the clones
464 I32 i = AvFILLp(PL_regex_padav) + 1;
465 SV **ary = AvARRAY(PL_regex_padav);
469 REGEXP *re = INT2PTR(REGEXP *,SvIVX(resv));
471 if (SvFLAGS(resv) & SVf_BREAK) {
472 /* this is PL_reg_curpm, already freed
473 * flag is set in regexec.c:S_regtry
475 SvFLAGS(resv) &= ~SVf_BREAK;
477 else if(SvREPADTMP(resv)) {
478 SvREPADTMP_off(resv);
485 SvREFCNT_dec(PL_regex_padav);
486 PL_regex_padav = Nullav;
490 SvREFCNT_dec((SV*) PL_stashcache);
491 PL_stashcache = NULL;
493 /* loosen bonds of global variables */
496 (void)PerlIO_close(PL_rsfp);
500 /* Filters for program text */
501 SvREFCNT_dec(PL_rsfp_filters);
502 PL_rsfp_filters = Nullav;
505 PL_preprocess = FALSE;
511 PL_doswitches = FALSE;
512 PL_dowarn = G_WARN_OFF;
513 PL_doextract = FALSE;
514 PL_sawampersand = FALSE; /* must save all match strings */
517 Safefree(PL_inplace);
519 SvREFCNT_dec(PL_patchlevel);
522 SvREFCNT_dec(PL_e_script);
523 PL_e_script = Nullsv;
526 /* magical thingies */
528 SvREFCNT_dec(PL_ofs_sv); /* $, */
531 SvREFCNT_dec(PL_ors_sv); /* $\ */
534 SvREFCNT_dec(PL_rs); /* $/ */
537 PL_multiline = 0; /* $* */
538 Safefree(PL_osname); /* $^O */
541 SvREFCNT_dec(PL_statname);
542 PL_statname = Nullsv;
545 /* defgv, aka *_ should be taken care of elsewhere */
547 /* clean up after study() */
548 SvREFCNT_dec(PL_lastscream);
549 PL_lastscream = Nullsv;
550 Safefree(PL_screamfirst);
552 Safefree(PL_screamnext);
556 Safefree(PL_efloatbuf);
557 PL_efloatbuf = Nullch;
560 /* startup and shutdown function lists */
561 SvREFCNT_dec(PL_beginav);
562 SvREFCNT_dec(PL_beginav_save);
563 SvREFCNT_dec(PL_endav);
564 SvREFCNT_dec(PL_checkav);
565 SvREFCNT_dec(PL_checkav_save);
566 SvREFCNT_dec(PL_initav);
568 PL_beginav_save = Nullav;
571 PL_checkav_save = Nullav;
574 /* shortcuts just get cleared */
580 PL_argvoutgv = Nullgv;
582 PL_stderrgv = Nullgv;
583 PL_last_in_gv = Nullgv;
585 PL_debstash = Nullhv;
587 /* reset so print() ends up where we expect */
590 SvREFCNT_dec(PL_argvout_stack);
591 PL_argvout_stack = Nullav;
593 SvREFCNT_dec(PL_modglobal);
594 PL_modglobal = Nullhv;
595 SvREFCNT_dec(PL_preambleav);
596 PL_preambleav = Nullav;
597 SvREFCNT_dec(PL_subname);
599 SvREFCNT_dec(PL_linestr);
601 SvREFCNT_dec(PL_pidstatus);
602 PL_pidstatus = Nullhv;
603 SvREFCNT_dec(PL_toptarget);
604 PL_toptarget = Nullsv;
605 SvREFCNT_dec(PL_bodytarget);
606 PL_bodytarget = Nullsv;
607 PL_formtarget = Nullsv;
609 /* free locale stuff */
610 #ifdef USE_LOCALE_COLLATE
611 Safefree(PL_collation_name);
612 PL_collation_name = Nullch;
615 #ifdef USE_LOCALE_NUMERIC
616 Safefree(PL_numeric_name);
617 PL_numeric_name = Nullch;
618 SvREFCNT_dec(PL_numeric_radix_sv);
621 /* clear utf8 character classes */
622 SvREFCNT_dec(PL_utf8_alnum);
623 SvREFCNT_dec(PL_utf8_alnumc);
624 SvREFCNT_dec(PL_utf8_ascii);
625 SvREFCNT_dec(PL_utf8_alpha);
626 SvREFCNT_dec(PL_utf8_space);
627 SvREFCNT_dec(PL_utf8_cntrl);
628 SvREFCNT_dec(PL_utf8_graph);
629 SvREFCNT_dec(PL_utf8_digit);
630 SvREFCNT_dec(PL_utf8_upper);
631 SvREFCNT_dec(PL_utf8_lower);
632 SvREFCNT_dec(PL_utf8_print);
633 SvREFCNT_dec(PL_utf8_punct);
634 SvREFCNT_dec(PL_utf8_xdigit);
635 SvREFCNT_dec(PL_utf8_mark);
636 SvREFCNT_dec(PL_utf8_toupper);
637 SvREFCNT_dec(PL_utf8_totitle);
638 SvREFCNT_dec(PL_utf8_tolower);
639 SvREFCNT_dec(PL_utf8_tofold);
640 SvREFCNT_dec(PL_utf8_idstart);
641 SvREFCNT_dec(PL_utf8_idcont);
642 PL_utf8_alnum = Nullsv;
643 PL_utf8_alnumc = Nullsv;
644 PL_utf8_ascii = Nullsv;
645 PL_utf8_alpha = Nullsv;
646 PL_utf8_space = Nullsv;
647 PL_utf8_cntrl = Nullsv;
648 PL_utf8_graph = Nullsv;
649 PL_utf8_digit = Nullsv;
650 PL_utf8_upper = Nullsv;
651 PL_utf8_lower = Nullsv;
652 PL_utf8_print = Nullsv;
653 PL_utf8_punct = Nullsv;
654 PL_utf8_xdigit = Nullsv;
655 PL_utf8_mark = Nullsv;
656 PL_utf8_toupper = Nullsv;
657 PL_utf8_totitle = Nullsv;
658 PL_utf8_tolower = Nullsv;
659 PL_utf8_tofold = Nullsv;
660 PL_utf8_idstart = Nullsv;
661 PL_utf8_idcont = Nullsv;
663 if (!specialWARN(PL_compiling.cop_warnings))
664 SvREFCNT_dec(PL_compiling.cop_warnings);
665 PL_compiling.cop_warnings = Nullsv;
666 if (!specialCopIO(PL_compiling.cop_io))
667 SvREFCNT_dec(PL_compiling.cop_io);
668 PL_compiling.cop_io = Nullsv;
669 CopFILE_free(&PL_compiling);
670 CopSTASH_free(&PL_compiling);
672 /* Prepare to destruct main symbol table. */
677 SvREFCNT_dec(PL_curstname);
678 PL_curstname = Nullsv;
680 /* clear queued errors */
681 SvREFCNT_dec(PL_errors);
685 if (destruct_level >= 2 && ckWARN_d(WARN_INTERNAL)) {
686 if (PL_scopestack_ix != 0)
687 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
688 "Unbalanced scopes: %ld more ENTERs than LEAVEs\n",
689 (long)PL_scopestack_ix);
690 if (PL_savestack_ix != 0)
691 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
692 "Unbalanced saves: %ld more saves than restores\n",
693 (long)PL_savestack_ix);
694 if (PL_tmps_floor != -1)
695 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),"Unbalanced tmps: %ld more allocs than frees\n",
696 (long)PL_tmps_floor + 1);
697 if (cxstack_ix != -1)
698 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),"Unbalanced context: %ld more PUSHes than POPs\n",
699 (long)cxstack_ix + 1);
702 /* Now absolutely destruct everything, somehow or other, loops or no. */
703 SvFLAGS(PL_fdpid) |= SVTYPEMASK; /* don't clean out pid table now */
704 SvFLAGS(PL_strtab) |= SVTYPEMASK; /* don't clean out strtab now */
706 /* the 2 is for PL_fdpid and PL_strtab */
707 while (PL_sv_count > 2 && sv_clean_all())
710 SvFLAGS(PL_fdpid) &= ~SVTYPEMASK;
711 SvFLAGS(PL_fdpid) |= SVt_PVAV;
712 SvFLAGS(PL_strtab) &= ~SVTYPEMASK;
713 SvFLAGS(PL_strtab) |= SVt_PVHV;
715 AvREAL_off(PL_fdpid); /* no surviving entries */
716 SvREFCNT_dec(PL_fdpid); /* needed in io_close() */
719 #ifdef HAVE_INTERP_INTERN
723 /* Destruct the global string table. */
725 /* Yell and reset the HeVAL() slots that are still holding refcounts,
726 * so that sv_free() won't fail on them.
734 max = HvMAX(PL_strtab);
735 array = HvARRAY(PL_strtab);
738 if (hent && ckWARN_d(WARN_INTERNAL)) {
739 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
740 "Unbalanced string table refcount: (%d) for \"%s\"",
741 HeVAL(hent) - Nullsv, HeKEY(hent));
742 HeVAL(hent) = Nullsv;
752 SvREFCNT_dec(PL_strtab);
755 /* free the pointer table used for cloning */
756 ptr_table_free(PL_ptr_table);
759 /* free special SVs */
761 SvREFCNT(&PL_sv_yes) = 0;
762 sv_clear(&PL_sv_yes);
763 SvANY(&PL_sv_yes) = NULL;
764 SvFLAGS(&PL_sv_yes) = 0;
766 SvREFCNT(&PL_sv_no) = 0;
768 SvANY(&PL_sv_no) = NULL;
769 SvFLAGS(&PL_sv_no) = 0;
773 for (i=0; i<=2; i++) {
774 SvREFCNT(PERL_DEBUG_PAD(i)) = 0;
775 sv_clear(PERL_DEBUG_PAD(i));
776 SvANY(PERL_DEBUG_PAD(i)) = NULL;
777 SvFLAGS(PERL_DEBUG_PAD(i)) = 0;
781 if (PL_sv_count != 0 && ckWARN_d(WARN_INTERNAL))
782 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),"Scalars leaked: %ld\n", (long)PL_sv_count);
784 #ifdef DEBUG_LEAKING_SCALARS
785 if (PL_sv_count != 0) {
790 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
791 svend = &sva[SvREFCNT(sva)];
792 for (sv = sva + 1; sv < svend; ++sv) {
793 if (SvTYPE(sv) != SVTYPEMASK) {
794 PerlIO_printf(Perl_debug_log, "leaked: 0x%p\n", sv);
802 #if defined(PERLIO_LAYERS)
803 /* No more IO - including error messages ! */
804 PerlIO_cleanup(aTHX);
807 /* sv_undef needs to stay immortal until after PerlIO_cleanup
808 as currently layers use it rather than Nullsv as a marker
809 for no arg - and will try and SvREFCNT_dec it.
811 SvREFCNT(&PL_sv_undef) = 0;
812 SvREADONLY_off(&PL_sv_undef);
814 Safefree(PL_origfilename);
815 Safefree(PL_reg_start_tmp);
817 Safefree(PL_reg_curpm);
818 Safefree(PL_reg_poscache);
820 Safefree(PL_op_mask);
821 Safefree(PL_psig_ptr);
822 Safefree(PL_psig_name);
823 Safefree(PL_bitcount);
824 Safefree(PL_psig_pend);
826 PL_hints = 0; /* Reset hints. Should hints be per-interpreter ? */
828 DEBUG_P(debprofdump());
830 #ifdef USE_REENTRANT_API
831 Perl_reentrant_free(aTHX);
836 /* As the absolutely last thing, free the non-arena SV for mess() */
839 /* it could have accumulated taint magic */
840 if (SvTYPE(PL_mess_sv) >= SVt_PVMG) {
843 for (mg = SvMAGIC(PL_mess_sv); mg; mg = moremagic) {
844 moremagic = mg->mg_moremagic;
845 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global
847 Safefree(mg->mg_ptr);
851 /* we know that type >= SVt_PV */
852 (void)SvOOK_off(PL_mess_sv);
853 Safefree(SvPVX(PL_mess_sv));
854 Safefree(SvANY(PL_mess_sv));
855 Safefree(PL_mess_sv);
858 return STATUS_NATIVE_EXPORT;
862 =for apidoc perl_free
864 Releases a Perl interpreter. See L<perlembed>.
872 #if defined(WIN32) || defined(NETWARE)
873 # if defined(PERL_IMPLICIT_SYS)
875 void *host = nw_internal_host;
877 void *host = w32_internal_host;
881 nw_delete_internal_host(host);
883 win32_delete_internal_host(host);
894 Perl_call_atexit(pTHX_ ATEXIT_t fn, void *ptr)
896 Renew(PL_exitlist, PL_exitlistlen+1, PerlExitListEntry);
897 PL_exitlist[PL_exitlistlen].fn = fn;
898 PL_exitlist[PL_exitlistlen].ptr = ptr;
903 =for apidoc perl_parse
905 Tells a Perl interpreter to parse a Perl script. See L<perlembed>.
911 perl_parse(pTHXx_ XSINIT_t xsinit, int argc, char **argv, char **env)
916 #ifdef USE_5005THREADS
920 #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
923 Perl_croak(aTHX_ "suidperl is no longer needed since the kernel can now execute\n\
924 setuid perl scripts securely.\n");
933 /* Come here if running an undumped a.out. */
935 PL_origfilename = savepv(argv[0]);
936 PL_do_undump = FALSE;
937 cxstack_ix = -1; /* start label stack again */
939 init_postdump_symbols(argc,argv,env);
944 op_free(PL_main_root);
945 PL_main_root = Nullop;
947 PL_main_start = Nullop;
948 SvREFCNT_dec(PL_main_cv);
952 oldscope = PL_scopestack_ix;
953 PL_dowarn = G_WARN_OFF;
955 #ifdef PERL_FLEXIBLE_EXCEPTIONS
956 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vparse_body), env, xsinit);
962 #ifndef PERL_FLEXIBLE_EXCEPTIONS
963 parse_body(env,xsinit);
966 call_list(oldscope, PL_checkav);
973 /* my_exit() was called */
974 while (PL_scopestack_ix > oldscope)
977 PL_curstash = PL_defstash;
979 call_list(oldscope, PL_checkav);
980 ret = STATUS_NATIVE_EXPORT;
983 PerlIO_printf(Perl_error_log, "panic: top_env\n");
991 #ifdef PERL_FLEXIBLE_EXCEPTIONS
993 S_vparse_body(pTHX_ va_list args)
995 char **env = va_arg(args, char**);
996 XSINIT_t xsinit = va_arg(args, XSINIT_t);
998 return parse_body(env, xsinit);
1003 S_parse_body(pTHX_ char **env, XSINIT_t xsinit)
1005 int argc = PL_origargc;
1006 char **argv = PL_origargv;
1007 char *scriptname = NULL;
1009 VOL bool dosearch = FALSE;
1010 char *validarg = "";
1013 char *cddir = Nullch;
1015 sv_setpvn(PL_linestr,"",0);
1016 sv = newSVpvn("",0); /* first used for -I flags */
1020 for (argc--,argv++; argc > 0; argc--,argv++) {
1021 if (argv[0][0] != '-' || !argv[0][1])
1025 validarg = " PHOOEY ";
1033 #ifndef PERL_STRICT_CR
1058 if ((s = moreswitches(s)))
1063 CHECK_MALLOC_TOO_LATE_FOR('t');
1064 if( !PL_tainting ) {
1065 PL_taint_warn = TRUE;
1071 CHECK_MALLOC_TOO_LATE_FOR('T');
1073 PL_taint_warn = FALSE;
1078 #ifdef MACOS_TRADITIONAL
1079 /* ignore -e for Dev:Pseudo argument */
1080 if (argv[1] && !strcmp(argv[1], "Dev:Pseudo"))
1083 if (PL_euid != PL_uid || PL_egid != PL_gid)
1084 Perl_croak(aTHX_ "No -e allowed in setuid scripts");
1086 PL_e_script = newSVpvn("",0);
1087 filter_add(read_e_script, NULL);
1090 sv_catpv(PL_e_script, s);
1092 sv_catpv(PL_e_script, argv[1]);
1096 Perl_croak(aTHX_ "No code specified for -e");
1097 sv_catpv(PL_e_script, "\n");
1100 case 'I': /* -I handled both here and in moreswitches() */
1102 if (!*++s && (s=argv[1]) != Nullch) {
1107 STRLEN len = strlen(s);
1108 p = savepvn(s, len);
1109 incpush(p, TRUE, TRUE, FALSE);
1110 sv_catpvn(sv, "-I", 2);
1111 sv_catpvn(sv, p, len);
1112 sv_catpvn(sv, " ", 1);
1116 Perl_croak(aTHX_ "No directory specified for -I");
1120 PL_preprocess = TRUE;
1130 PL_preambleav = newAV();
1131 av_push(PL_preambleav, newSVpv("use Config qw(myconfig config_vars)",0));
1133 PL_Sv = newSVpv("print myconfig();",0);
1135 sv_catpv(PL_Sv,"print \"\\nCharacteristics of this PERLSHR image: \\n\",");
1137 sv_catpv(PL_Sv,"print \"\\nCharacteristics of this binary (from libperl): \\n\",");
1139 sv_catpv(PL_Sv,"\" Compile-time options:");
1141 sv_catpv(PL_Sv," DEBUGGING");
1143 # ifdef MULTIPLICITY
1144 sv_catpv(PL_Sv," MULTIPLICITY");
1146 # ifdef USE_5005THREADS
1147 sv_catpv(PL_Sv," USE_5005THREADS");
1149 # ifdef USE_ITHREADS
1150 sv_catpv(PL_Sv," USE_ITHREADS");
1152 # ifdef USE_64_BIT_INT
1153 sv_catpv(PL_Sv," USE_64_BIT_INT");
1155 # ifdef USE_64_BIT_ALL
1156 sv_catpv(PL_Sv," USE_64_BIT_ALL");
1158 # ifdef USE_LONG_DOUBLE
1159 sv_catpv(PL_Sv," USE_LONG_DOUBLE");
1161 # ifdef USE_LARGE_FILES
1162 sv_catpv(PL_Sv," USE_LARGE_FILES");
1165 sv_catpv(PL_Sv," USE_SOCKS");
1167 # ifdef PERL_IMPLICIT_CONTEXT
1168 sv_catpv(PL_Sv," PERL_IMPLICIT_CONTEXT");
1170 # ifdef PERL_IMPLICIT_SYS
1171 sv_catpv(PL_Sv," PERL_IMPLICIT_SYS");
1173 sv_catpv(PL_Sv,"\\n\",");
1175 #if defined(LOCAL_PATCH_COUNT)
1176 if (LOCAL_PATCH_COUNT > 0) {
1178 sv_catpv(PL_Sv,"\" Locally applied patches:\\n\",");
1179 for (i = 1; i <= LOCAL_PATCH_COUNT; i++) {
1180 if (PL_localpatches[i])
1181 Perl_sv_catpvf(aTHX_ PL_Sv,"q\" \t%s\n\",",PL_localpatches[i]);
1185 Perl_sv_catpvf(aTHX_ PL_Sv,"\" Built under %s\\n\"",OSNAME);
1188 Perl_sv_catpvf(aTHX_ PL_Sv,",\" Compiled at %s %s\\n\"",__DATE__,__TIME__);
1190 Perl_sv_catpvf(aTHX_ PL_Sv,",\" Compiled on %s\\n\"",__DATE__);
1193 sv_catpv(PL_Sv, "; \
1195 @env = map { \"$_=\\\"$ENV{$_}\\\"\" } sort grep {/^PERL/} keys %ENV; ");
1198 push @env, \"CYGWIN=\\\"$ENV{CYGWIN}\\\"\";");
1201 print \" \\%ENV:\\n @env\\n\" if @env; \
1202 print \" \\@INC:\\n @INC\\n\";");
1205 PL_Sv = newSVpv("config_vars(qw(",0);
1206 sv_catpv(PL_Sv, ++s);
1207 sv_catpv(PL_Sv, "))");
1210 av_push(PL_preambleav, PL_Sv);
1211 scriptname = BIT_BUCKET; /* don't look for script or read stdin */
1214 PL_doextract = TRUE;
1222 if (!*++s || isSPACE(*s)) {
1226 /* catch use of gnu style long options */
1227 if (strEQ(s, "version")) {
1231 if (strEQ(s, "help")) {
1238 Perl_croak(aTHX_ "Unrecognized switch: -%s (-h will show valid options)",s);
1242 sv_setsv(get_sv("/", TRUE), PL_rs);
1245 #ifndef SECURE_INTERNAL_GETENV
1248 (s = PerlEnv_getenv("PERL5OPT")))
1253 if (*s == '-' && *(s+1) == 'T') {
1254 CHECK_MALLOC_TOO_LATE_FOR('T');
1256 PL_taint_warn = FALSE;
1259 char *popt_copy = Nullch;
1272 if (!strchr("DIMUdmtwA", *s))
1273 Perl_croak(aTHX_ "Illegal switch in PERL5OPT: -%c", *s);
1277 popt_copy = SvPVX(sv_2mortal(newSVpv(popt,0)));
1278 s = popt_copy + (s - popt);
1279 d = popt_copy + (d - popt);
1286 if( !PL_tainting ) {
1287 PL_taint_warn = TRUE;
1297 if (PL_taint_warn && PL_dowarn != G_WARN_ALL_OFF) {
1298 PL_compiling.cop_warnings = newSVpvn(WARN_TAINTstring, WARNsize);
1302 scriptname = argv[0];
1305 scriptname = BIT_BUCKET; /* don't look for script or read stdin */
1307 else if (scriptname == Nullch) {
1309 if ( PerlLIO_isatty(PerlIO_fileno(PerlIO_stdin())) )
1317 open_script(scriptname,dosearch,sv,&fdscript);
1319 validate_suid(validarg, scriptname,fdscript);
1322 #if defined(SIGCHLD) || defined(SIGCLD)
1325 # define SIGCHLD SIGCLD
1327 Sighandler_t sigstate = rsignal_state(SIGCHLD);
1328 if (sigstate == SIG_IGN) {
1329 if (ckWARN(WARN_SIGNAL))
1330 Perl_warner(aTHX_ packWARN(WARN_SIGNAL),
1331 "Can't ignore signal CHLD, forcing to default");
1332 (void)rsignal(SIGCHLD, (Sighandler_t)SIG_DFL);
1338 #ifdef MACOS_TRADITIONAL
1339 if (PL_doextract || gMacPerl_AlwaysExtract) {
1344 if (cddir && PerlDir_chdir(cddir) < 0)
1345 Perl_croak(aTHX_ "Can't chdir to %s",cddir);
1349 PL_main_cv = PL_compcv = (CV*)NEWSV(1104,0);
1350 sv_upgrade((SV *)PL_compcv, SVt_PVCV);
1351 CvUNIQUE_on(PL_compcv);
1353 CvPADLIST(PL_compcv) = pad_new(0);
1354 #ifdef USE_5005THREADS
1355 CvOWNER(PL_compcv) = 0;
1356 New(666, CvMUTEXP(PL_compcv), 1, perl_mutex);
1357 MUTEX_INIT(CvMUTEXP(PL_compcv));
1358 #endif /* USE_5005THREADS */
1361 boot_core_UNIVERSAL();
1363 boot_core_xsutils();
1367 (*xsinit)(aTHX); /* in case linked C routines want magical variables */
1369 #if defined(VMS) || defined(WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(EPOC)
1375 # ifdef HAS_SOCKS5_INIT
1376 socks5_init(argv[0]);
1382 init_predump_symbols();
1383 /* init_postdump_symbols not currently designed to be called */
1384 /* more than once (ENV isn't cleared first, for example) */
1385 /* But running with -u leaves %ENV & @ARGV undefined! XXX */
1387 init_postdump_symbols(argc,argv,env);
1389 /* PL_unicode is turned on by -C or by $ENV{PERL_UNICODE}.
1390 * PL_utf8locale is conditionally turned on by
1391 * locale.c:Perl_init_i18nl10n() if the environment
1392 * look like the user wants to use UTF-8. */
1394 /* Requires init_predump_symbols(). */
1395 if (!(PL_unicode & PERL_UNICODE_LOCALE_FLAG) || PL_utf8locale) {
1400 /* Turn on UTF-8-ness on STDIN, STDOUT, STDERR
1401 * and the default open disciplines. */
1402 if ((PL_unicode & PERL_UNICODE_STDIN_FLAG) &&
1403 PL_stdingv && (io = GvIO(PL_stdingv)) &&
1405 PerlIO_binmode(aTHX_ fp, IoTYPE(io), 0, ":utf8");
1406 if ((PL_unicode & PERL_UNICODE_STDOUT_FLAG) &&
1407 PL_defoutgv && (io = GvIO(PL_defoutgv)) &&
1409 PerlIO_binmode(aTHX_ fp, IoTYPE(io), 0, ":utf8");
1410 if ((PL_unicode & PERL_UNICODE_STDERR_FLAG) &&
1411 PL_stderrgv && (io = GvIO(PL_stderrgv)) &&
1413 PerlIO_binmode(aTHX_ fp, IoTYPE(io), 0, ":utf8");
1414 if ((PL_unicode & PERL_UNICODE_INOUT_FLAG) &&
1415 (sv = GvSV(gv_fetchpv("\017PEN", TRUE, SVt_PV)))) {
1416 U32 in = PL_unicode & PERL_UNICODE_IN_FLAG;
1417 U32 out = PL_unicode & PERL_UNICODE_OUT_FLAG;
1420 sv_setpvn(sv, ":utf8\0:utf8", 11);
1422 sv_setpvn(sv, ":utf8\0", 6);
1425 sv_setpvn(sv, "\0:utf8", 6);
1431 if ((s = PerlEnv_getenv("PERL_SIGNALS"))) {
1432 if (strEQ(s, "unsafe"))
1433 PL_signals |= PERL_SIGNALS_UNSAFE_FLAG;
1434 else if (strEQ(s, "safe"))
1435 PL_signals &= ~PERL_SIGNALS_UNSAFE_FLAG;
1437 Perl_croak(aTHX_ "PERL_SIGNALS illegal: \"%s\"", s);
1442 /* now parse the script */
1444 SETERRNO(0,SS_NORMAL);
1446 #ifdef MACOS_TRADITIONAL
1447 if (gMacPerl_SyntaxError = (yyparse() || PL_error_count)) {
1449 Perl_croak(aTHX_ "%s had compilation errors.\n", MacPerl_MPWFileName(PL_origfilename));
1451 Perl_croak(aTHX_ "Execution of %s aborted due to compilation errors.\n",
1452 MacPerl_MPWFileName(PL_origfilename));
1456 if (yyparse() || PL_error_count) {
1458 Perl_croak(aTHX_ "%s had compilation errors.\n", PL_origfilename);
1460 Perl_croak(aTHX_ "Execution of %s aborted due to compilation errors.\n",
1465 CopLINE_set(PL_curcop, 0);
1466 PL_curstash = PL_defstash;
1467 PL_preprocess = FALSE;
1469 SvREFCNT_dec(PL_e_script);
1470 PL_e_script = Nullsv;
1477 SAVECOPFILE(PL_curcop);
1478 SAVECOPLINE(PL_curcop);
1479 gv_check(PL_defstash);
1486 if ((s=PerlEnv_getenv("PERL_DEBUG_MSTATS")) && atoi(s) >= 2)
1487 dump_mstats("after compilation:");
1496 =for apidoc perl_run
1498 Tells a Perl interpreter to run. See L<perlembed>.
1509 #ifdef USE_5005THREADS
1513 oldscope = PL_scopestack_ix;
1518 #ifdef PERL_FLEXIBLE_EXCEPTIONS
1520 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vrun_body), oldscope);
1526 cxstack_ix = -1; /* start context stack again */
1528 case 0: /* normal completion */
1529 #ifndef PERL_FLEXIBLE_EXCEPTIONS
1534 case 2: /* my_exit() */
1535 while (PL_scopestack_ix > oldscope)
1538 PL_curstash = PL_defstash;
1539 if (!(PL_exit_flags & PERL_EXIT_DESTRUCT_END) &&
1540 PL_endav && !PL_minus_c)
1541 call_list(oldscope, PL_endav);
1543 if (PerlEnv_getenv("PERL_DEBUG_MSTATS"))
1544 dump_mstats("after execution: ");
1546 ret = STATUS_NATIVE_EXPORT;
1550 POPSTACK_TO(PL_mainstack);
1553 PerlIO_printf(Perl_error_log, "panic: restartop\n");
1563 #ifdef PERL_FLEXIBLE_EXCEPTIONS
1565 S_vrun_body(pTHX_ va_list args)
1567 I32 oldscope = va_arg(args, I32);
1569 return run_body(oldscope);
1575 S_run_body(pTHX_ I32 oldscope)
1577 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s $` $& $' support.\n",
1578 PL_sawampersand ? "Enabling" : "Omitting"));
1580 if (!PL_restartop) {
1581 DEBUG_x(dump_all());
1582 DEBUG(PerlIO_printf(Perl_debug_log, "\nEXECUTING...\n\n"));
1583 DEBUG_S(PerlIO_printf(Perl_debug_log, "main thread is 0x%"UVxf"\n",
1587 #ifdef MACOS_TRADITIONAL
1588 PerlIO_printf(Perl_error_log, "%s%s syntax OK\n",
1589 (gMacPerl_ErrorFormat ? "# " : ""),
1590 MacPerl_MPWFileName(PL_origfilename));
1592 PerlIO_printf(Perl_error_log, "%s syntax OK\n", PL_origfilename);
1596 if (PERLDB_SINGLE && PL_DBsingle)
1597 sv_setiv(PL_DBsingle, 1);
1599 call_list(oldscope, PL_initav);
1605 PL_op = PL_restartop;
1609 else if (PL_main_start) {
1610 CvDEPTH(PL_main_cv) = 1;
1611 PL_op = PL_main_start;
1621 =head1 SV Manipulation Functions
1623 =for apidoc p||get_sv
1625 Returns the SV of the specified Perl scalar. If C<create> is set and the
1626 Perl variable does not exist then it will be created. If C<create> is not
1627 set and the variable does not exist then NULL is returned.
1633 Perl_get_sv(pTHX_ const char *name, I32 create)
1636 #ifdef USE_5005THREADS
1637 if (name[1] == '\0' && !isALPHA(name[0])) {
1638 PADOFFSET tmp = find_threadsv(name);
1639 if (tmp != NOT_IN_PAD)
1640 return THREADSV(tmp);
1642 #endif /* USE_5005THREADS */
1643 gv = gv_fetchpv(name, create, SVt_PV);
1650 =head1 Array Manipulation Functions
1652 =for apidoc p||get_av
1654 Returns the AV of the specified Perl array. If C<create> is set and the
1655 Perl variable does not exist then it will be created. If C<create> is not
1656 set and the variable does not exist then NULL is returned.
1662 Perl_get_av(pTHX_ const char *name, I32 create)
1664 GV* gv = gv_fetchpv(name, create, SVt_PVAV);
1673 =head1 Hash Manipulation Functions
1675 =for apidoc p||get_hv
1677 Returns the HV of the specified Perl hash. If C<create> is set and the
1678 Perl variable does not exist then it will be created. If C<create> is not
1679 set and the variable does not exist then NULL is returned.
1685 Perl_get_hv(pTHX_ const char *name, I32 create)
1687 GV* gv = gv_fetchpv(name, create, SVt_PVHV);
1696 =head1 CV Manipulation Functions
1698 =for apidoc p||get_cv
1700 Returns the CV of the specified Perl subroutine. If C<create> is set and
1701 the Perl subroutine does not exist then it will be declared (which has the
1702 same effect as saying C<sub name;>). If C<create> is not set and the
1703 subroutine does not exist then NULL is returned.
1709 Perl_get_cv(pTHX_ const char *name, I32 create)
1711 GV* gv = gv_fetchpv(name, create, SVt_PVCV);
1712 /* XXX unsafe for threads if eval_owner isn't held */
1713 /* XXX this is probably not what they think they're getting.
1714 * It has the same effect as "sub name;", i.e. just a forward
1716 if (create && !GvCVu(gv))
1717 return newSUB(start_subparse(FALSE, 0),
1718 newSVOP(OP_CONST, 0, newSVpv(name,0)),
1726 /* Be sure to refetch the stack pointer after calling these routines. */
1730 =head1 Callback Functions
1732 =for apidoc p||call_argv
1734 Performs a callback to the specified Perl sub. See L<perlcall>.
1740 Perl_call_argv(pTHX_ const char *sub_name, I32 flags, register char **argv)
1742 /* See G_* flags in cop.h */
1743 /* null terminated arg list */
1750 XPUSHs(sv_2mortal(newSVpv(*argv,0)));
1755 return call_pv(sub_name, flags);
1759 =for apidoc p||call_pv
1761 Performs a callback to the specified Perl sub. See L<perlcall>.
1767 Perl_call_pv(pTHX_ const char *sub_name, I32 flags)
1768 /* name of the subroutine */
1769 /* See G_* flags in cop.h */
1771 return call_sv((SV*)get_cv(sub_name, TRUE), flags);
1775 =for apidoc p||call_method
1777 Performs a callback to the specified Perl method. The blessed object must
1778 be on the stack. See L<perlcall>.
1784 Perl_call_method(pTHX_ const char *methname, I32 flags)
1785 /* name of the subroutine */
1786 /* See G_* flags in cop.h */
1788 return call_sv(sv_2mortal(newSVpv(methname,0)), flags | G_METHOD);
1791 /* May be called with any of a CV, a GV, or an SV containing the name. */
1793 =for apidoc p||call_sv
1795 Performs a callback to the Perl sub whose name is in the SV. See
1802 Perl_call_sv(pTHX_ SV *sv, I32 flags)
1803 /* See G_* flags in cop.h */
1806 LOGOP myop; /* fake syntax tree node */
1809 volatile I32 retval = 0;
1811 bool oldcatch = CATCH_GET;
1816 if (flags & G_DISCARD) {
1821 Zero(&myop, 1, LOGOP);
1822 myop.op_next = Nullop;
1823 if (!(flags & G_NOARGS))
1824 myop.op_flags |= OPf_STACKED;
1825 myop.op_flags |= ((flags & G_VOID) ? OPf_WANT_VOID :
1826 (flags & G_ARRAY) ? OPf_WANT_LIST :
1831 EXTEND(PL_stack_sp, 1);
1832 *++PL_stack_sp = sv;
1834 oldscope = PL_scopestack_ix;
1836 if (PERLDB_SUB && PL_curstash != PL_debstash
1837 /* Handle first BEGIN of -d. */
1838 && (PL_DBcv || (PL_DBcv = GvCV(PL_DBsub)))
1839 /* Try harder, since this may have been a sighandler, thus
1840 * curstash may be meaningless. */
1841 && (SvTYPE(sv) != SVt_PVCV || CvSTASH((CV*)sv) != PL_debstash)
1842 && !(flags & G_NODEBUG))
1843 PL_op->op_private |= OPpENTERSUB_DB;
1845 if (flags & G_METHOD) {
1846 Zero(&method_op, 1, UNOP);
1847 method_op.op_next = PL_op;
1848 method_op.op_ppaddr = PL_ppaddr[OP_METHOD];
1849 myop.op_ppaddr = PL_ppaddr[OP_ENTERSUB];
1850 PL_op = (OP*)&method_op;
1853 if (!(flags & G_EVAL)) {
1855 call_body((OP*)&myop, FALSE);
1856 retval = PL_stack_sp - (PL_stack_base + oldmark);
1857 CATCH_SET(oldcatch);
1860 myop.op_other = (OP*)&myop;
1862 /* we're trying to emulate pp_entertry() here */
1864 register PERL_CONTEXT *cx;
1865 I32 gimme = GIMME_V;
1870 push_return(Nullop);
1871 PUSHBLOCK(cx, (CXt_EVAL|CXp_TRYBLOCK), PL_stack_sp);
1873 PL_eval_root = PL_op; /* Only needed so that goto works right. */
1875 PL_in_eval = EVAL_INEVAL;
1876 if (flags & G_KEEPERR)
1877 PL_in_eval |= EVAL_KEEPERR;
1883 #ifdef PERL_FLEXIBLE_EXCEPTIONS
1885 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vcall_body),
1892 #ifndef PERL_FLEXIBLE_EXCEPTIONS
1894 call_body((OP*)&myop, FALSE);
1896 retval = PL_stack_sp - (PL_stack_base + oldmark);
1897 if (!(flags & G_KEEPERR))
1904 /* my_exit() was called */
1905 PL_curstash = PL_defstash;
1908 if (PL_statusvalue && !(PL_exit_flags & PERL_EXIT_EXPECTED))
1909 Perl_croak(aTHX_ "Callback called exit");
1914 PL_op = PL_restartop;
1918 PL_stack_sp = PL_stack_base + oldmark;
1919 if (flags & G_ARRAY)
1923 *++PL_stack_sp = &PL_sv_undef;
1928 if (PL_scopestack_ix > oldscope) {
1932 register PERL_CONTEXT *cx;
1944 if (flags & G_DISCARD) {
1945 PL_stack_sp = PL_stack_base + oldmark;
1954 #ifdef PERL_FLEXIBLE_EXCEPTIONS
1956 S_vcall_body(pTHX_ va_list args)
1958 OP *myop = va_arg(args, OP*);
1959 int is_eval = va_arg(args, int);
1961 call_body(myop, is_eval);
1967 S_call_body(pTHX_ OP *myop, int is_eval)
1969 if (PL_op == myop) {
1971 PL_op = Perl_pp_entereval(aTHX); /* this doesn't do a POPMARK */
1973 PL_op = Perl_pp_entersub(aTHX); /* this does */
1979 /* Eval a string. The G_EVAL flag is always assumed. */
1982 =for apidoc p||eval_sv
1984 Tells Perl to C<eval> the string in the SV.
1990 Perl_eval_sv(pTHX_ SV *sv, I32 flags)
1992 /* See G_* flags in cop.h */
1995 UNOP myop; /* fake syntax tree node */
1996 volatile I32 oldmark = SP - PL_stack_base;
1997 volatile I32 retval = 0;
2003 if (flags & G_DISCARD) {
2010 Zero(PL_op, 1, UNOP);
2011 EXTEND(PL_stack_sp, 1);
2012 *++PL_stack_sp = sv;
2013 oldscope = PL_scopestack_ix;
2015 if (!(flags & G_NOARGS))
2016 myop.op_flags = OPf_STACKED;
2017 myop.op_next = Nullop;
2018 myop.op_type = OP_ENTEREVAL;
2019 myop.op_flags |= ((flags & G_VOID) ? OPf_WANT_VOID :
2020 (flags & G_ARRAY) ? OPf_WANT_LIST :
2022 if (flags & G_KEEPERR)
2023 myop.op_flags |= OPf_SPECIAL;
2025 #ifdef PERL_FLEXIBLE_EXCEPTIONS
2027 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vcall_body),
2034 #ifndef PERL_FLEXIBLE_EXCEPTIONS
2036 call_body((OP*)&myop,TRUE);
2038 retval = PL_stack_sp - (PL_stack_base + oldmark);
2039 if (!(flags & G_KEEPERR))
2046 /* my_exit() was called */
2047 PL_curstash = PL_defstash;
2050 if (PL_statusvalue && !(PL_exit_flags & PERL_EXIT_EXPECTED))
2051 Perl_croak(aTHX_ "Callback called exit");
2056 PL_op = PL_restartop;
2060 PL_stack_sp = PL_stack_base + oldmark;
2061 if (flags & G_ARRAY)
2065 *++PL_stack_sp = &PL_sv_undef;
2071 if (flags & G_DISCARD) {
2072 PL_stack_sp = PL_stack_base + oldmark;
2082 =for apidoc p||eval_pv
2084 Tells Perl to C<eval> the given string and return an SV* result.
2090 Perl_eval_pv(pTHX_ const char *p, I32 croak_on_error)
2093 SV* sv = newSVpv(p, 0);
2095 eval_sv(sv, G_SCALAR);
2102 if (croak_on_error && SvTRUE(ERRSV)) {
2104 Perl_croak(aTHX_ SvPVx(ERRSV, n_a));
2110 /* Require a module. */
2113 =head1 Embedding Functions
2115 =for apidoc p||require_pv
2117 Tells Perl to C<require> the file named by the string argument. It is
2118 analogous to the Perl code C<eval "require '$file'">. It's even
2119 implemented that way; consider using load_module instead.
2124 Perl_require_pv(pTHX_ const char *pv)
2128 PUSHSTACKi(PERLSI_REQUIRE);
2130 sv = sv_newmortal();
2131 sv_setpv(sv, "require '");
2134 eval_sv(sv, G_DISCARD);
2140 Perl_magicname(pTHX_ char *sym, char *name, I32 namlen)
2144 if ((gv = gv_fetchpv(sym,TRUE, SVt_PV)))
2145 sv_magic(GvSV(gv), (SV*)gv, PERL_MAGIC_sv, name, namlen);
2149 S_usage(pTHX_ char *name) /* XXX move this out into a module ? */
2151 /* This message really ought to be max 23 lines.
2152 * Removed -h because the user already knows that option. Others? */
2154 static char *usage_msg[] = {
2155 "-0[octal] specify record separator (\\0, if no argument)",
2156 "-a autosplit mode with -n or -p (splits $_ into @F)",
2157 "-C enable native wide character system interfaces",
2158 "-c check syntax only (runs BEGIN and CHECK blocks)",
2159 "-d[:debugger] run program under debugger",
2160 "-D[number/list] set debugging flags (argument is a bit mask or alphabets)",
2161 "-e 'command' one line of program (several -e's allowed, omit programfile)",
2162 "-F/pattern/ split() pattern for -a switch (//'s are optional)",
2163 "-i[extension] edit <> files in place (makes backup if extension supplied)",
2164 "-Idirectory specify @INC/#include directory (several -I's allowed)",
2165 "-l[octal] enable line ending processing, specifies line terminator",
2166 "-[mM][-]module execute `use/no module...' before executing program",
2167 "-n assume 'while (<>) { ... }' loop around program",
2168 "-p assume loop like -n but print line also, like sed",
2169 "-P run program through C preprocessor before compilation",
2170 "-s enable rudimentary parsing for switches after programfile",
2171 "-S look for programfile using PATH environment variable",
2172 "-T enable tainting checks",
2173 "-t enable tainting warnings",
2174 "-u dump core after parsing program",
2175 "-U allow unsafe operations",
2176 "-v print version, subversion (includes VERY IMPORTANT perl info)",
2177 "-V[:variable] print configuration summary (or a single Config.pm variable)",
2178 "-w enable many useful warnings (RECOMMENDED)",
2179 "-W enable all warnings",
2180 "-X disable all warnings",
2181 "-x[directory] strip off text before #!perl line and perhaps cd to directory",
2185 char **p = usage_msg;
2187 PerlIO_printf(PerlIO_stdout(),
2188 "\nUsage: %s [switches] [--] [programfile] [arguments]",
2191 PerlIO_printf(PerlIO_stdout(), "\n %s", *p++);
2194 /* This routine handles any switches that can be given during run */
2197 Perl_moreswitches(pTHX_ char *s)
2207 SvREFCNT_dec(PL_rs);
2208 if (s[1] == 'x' && s[2]) {
2212 for (s += 2, e = s; *e; e++);
2214 flags = PERL_SCAN_SILENT_ILLDIGIT;
2215 rschar = (U32)grok_hex(s, &numlen, &flags, NULL);
2216 if (s + numlen < e) {
2217 rschar = 0; /* Grandfather -0xFOO as -0 -xFOO. */
2221 PL_rs = newSVpvn("", 0);
2222 SvGROW(PL_rs, (STRLEN)(UNISKIP(rschar) + 1));
2223 tmps = (U8*)SvPVX(PL_rs);
2224 uvchr_to_utf8(tmps, rschar);
2225 SvCUR_set(PL_rs, UNISKIP(rschar));
2230 rschar = (U32)grok_oct(s, &numlen, &flags, NULL);
2231 if (rschar & ~((U8)~0))
2232 PL_rs = &PL_sv_undef;
2233 else if (!rschar && numlen >= 2)
2234 PL_rs = newSVpvn("", 0);
2236 char ch = (char)rschar;
2237 PL_rs = newSVpvn(&ch, 1);
2244 PL_unicode = parse_unicode_opts(&s);
2249 while (*s && !isSPACE(*s)) ++s;
2251 PL_splitstr = savepv(PL_splitstr);
2264 /* The following permits -d:Mod to accepts arguments following an =
2265 in the fashion that -MSome::Mod does. */
2266 if (*s == ':' || *s == '=') {
2269 sv = newSVpv("use Devel::", 0);
2271 /* We now allow -d:Module=Foo,Bar */
2272 while(isALNUM(*s) || *s==':') ++s;
2274 sv_catpv(sv, start);
2276 sv_catpvn(sv, start, s-start);
2277 sv_catpv(sv, " split(/,/,q{");
2282 my_setenv("PERL5DB", SvPV(sv, PL_na));
2285 PL_perldb = PERLDB_ALL;
2293 if (isALPHA(s[1])) {
2294 /* if adding extra options, remember to update DEBUG_MASK */
2295 static char debopts[] = "psltocPmfrxu HXDSTRJvC";
2298 for (s++; *s && (d = strchr(debopts,*s)); s++)
2299 PL_debug |= 1 << (d - debopts);
2302 PL_debug = atoi(s+1);
2303 for (s++; isDIGIT(*s); s++) ;
2306 if (DEBUG_p_TEST_ && ckWARN_d(WARN_DEBUGGING))
2307 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING),
2308 "-Dp not implemented on this platform\n");
2310 PL_debug |= DEBUG_TOP_FLAG;
2311 #else /* !DEBUGGING */
2312 if (ckWARN_d(WARN_DEBUGGING))
2313 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING),
2314 "Recompile perl with -DDEBUGGING to use -D switch\n");
2315 for (s++; isALNUM(*s); s++) ;
2321 usage(PL_origargv[0]);
2325 Safefree(PL_inplace);
2326 #if defined(__CYGWIN__) /* do backup extension automagically */
2327 if (*(s+1) == '\0') {
2328 PL_inplace = savepv(".bak");
2331 #endif /* __CYGWIN__ */
2332 PL_inplace = savepv(s+1);
2334 for (s = PL_inplace; *s && !isSPACE(*s); s++) ;
2337 if (*s == '-') /* Additional switches on #! line. */
2341 case 'I': /* -I handled both here and in parse_body() */
2344 while (*s && isSPACE(*s))
2349 /* ignore trailing spaces (possibly followed by other switches) */
2351 for (e = p; *e && !isSPACE(*e); e++) ;
2355 } while (*p && *p != '-');
2356 e = savepvn(s, e-s);
2357 incpush(e, TRUE, TRUE, FALSE);
2364 Perl_croak(aTHX_ "No directory specified for -I");
2370 SvREFCNT_dec(PL_ors_sv);
2375 PL_ors_sv = newSVpvn("\n",1);
2376 numlen = 3 + (*s == '0');
2377 *SvPVX(PL_ors_sv) = (char)grok_oct(s, &numlen, &flags, NULL);
2381 if (RsPARA(PL_rs)) {
2382 PL_ors_sv = newSVpvn("\n\n",2);
2385 PL_ors_sv = newSVsv(PL_rs);
2392 PL_preambleav = newAV();
2394 SV *sv = newSVpvn("use assertions::activate split(/,/,q{",37);
2398 av_push(PL_preambleav, sv);
2401 av_push(PL_preambleav, newSVpvn("use assertions::activate",24));
2404 forbid_setid("-M"); /* XXX ? */
2407 forbid_setid("-m"); /* XXX ? */
2412 /* -M-foo == 'no foo' */
2413 if (*s == '-') { use = "no "; ++s; }
2414 sv = newSVpv(use,0);
2416 /* We allow -M'Module qw(Foo Bar)' */
2417 while(isALNUM(*s) || *s==':') ++s;
2419 sv_catpv(sv, start);
2420 if (*(start-1) == 'm') {
2422 Perl_croak(aTHX_ "Can't use '%c' after -mname", *s);
2423 sv_catpv( sv, " ()");
2427 Perl_croak(aTHX_ "Module name required with -%c option",
2429 sv_catpvn(sv, start, s-start);
2430 sv_catpv(sv, " split(/,/,q{");
2436 PL_preambleav = newAV();
2437 av_push(PL_preambleav, sv);
2440 Perl_croak(aTHX_ "No space allowed after -%c", *(s-1));
2452 PL_doswitches = TRUE;
2466 #ifdef MACOS_TRADITIONAL
2467 Perl_croak(aTHX_ "Believe me, you don't want to use \"-u\" on a Macintosh");
2469 PL_do_undump = TRUE;
2478 PerlIO_printf(PerlIO_stdout(),
2479 Perl_form(aTHX_ "\nThis is perl, v%"VDf" built for %s",
2480 PL_patchlevel, ARCHNAME));
2482 /* Adjust verbose output as in the perl that ships with the DG/UX OS from EMC */
2483 PerlIO_printf(PerlIO_stdout(),
2484 Perl_form(aTHX_ "\nThis is perl, version %vd\n", PL_patchlevel));
2485 PerlIO_printf(PerlIO_stdout(),
2486 Perl_form(aTHX_ " built under %s at %s %s\n",
2487 OSNAME, __DATE__, __TIME__));
2488 PerlIO_printf(PerlIO_stdout(),
2489 Perl_form(aTHX_ " OS Specific Release: %s\n",
2493 #if defined(LOCAL_PATCH_COUNT)
2494 if (LOCAL_PATCH_COUNT > 0)
2495 PerlIO_printf(PerlIO_stdout(),
2496 "\n(with %d registered patch%s, "
2497 "see perl -V for more detail)",
2498 (int)LOCAL_PATCH_COUNT,
2499 (LOCAL_PATCH_COUNT!=1) ? "es" : "");
2502 PerlIO_printf(PerlIO_stdout(),
2503 "\n\nCopyright 1987-2003, Larry Wall\n");
2504 #ifdef MACOS_TRADITIONAL
2505 PerlIO_printf(PerlIO_stdout(),
2506 "\nMac OS port Copyright 1991-2002, Matthias Neeracher;\n"
2507 "maintained by Chris Nandor\n");
2510 PerlIO_printf(PerlIO_stdout(),
2511 "\nMS-DOS port Copyright (c) 1989, 1990, Diomidis Spinellis\n");
2514 PerlIO_printf(PerlIO_stdout(),
2515 "djgpp v2 port (jpl5003c) by Hirofumi Watanabe, 1996\n"
2516 "djgpp v2 port (perl5004+) by Laszlo Molnar, 1997-1999\n");
2519 PerlIO_printf(PerlIO_stdout(),
2520 "\n\nOS/2 port Copyright (c) 1990, 1991, Raymond Chen, Kai Uwe Rommel\n"
2521 "Version 5 port Copyright (c) 1994-2002, Andreas Kaiser, Ilya Zakharevich\n");
2524 PerlIO_printf(PerlIO_stdout(),
2525 "atariST series port, ++jrb bammi@cadence.com\n");
2528 PerlIO_printf(PerlIO_stdout(),
2529 "BeOS port Copyright Tom Spindler, 1997-1999\n");
2532 PerlIO_printf(PerlIO_stdout(),
2533 "MPE/iX port Copyright by Mark Klein and Mark Bixby, 1996-2002\n");
2536 PerlIO_printf(PerlIO_stdout(),
2537 "MVS (OS390) port by Mortice Kern Systems, 1997-1999\n");
2540 PerlIO_printf(PerlIO_stdout(),
2541 "Stratus VOS port by Paul.Green@stratus.com, 1997-2002\n");
2544 PerlIO_printf(PerlIO_stdout(),
2545 "VM/ESA port by Neale Ferguson, 1998-1999\n");
2548 PerlIO_printf(PerlIO_stdout(),
2549 "BS2000 (POSIX) port by Start Amadeus GmbH, 1998-1999\n");
2552 PerlIO_printf(PerlIO_stdout(),
2553 "MiNT port by Guido Flohr, 1997-1999\n");
2556 PerlIO_printf(PerlIO_stdout(),
2557 "EPOC port by Olaf Flebbe, 1999-2002\n");
2560 PerlIO_printf(PerlIO_stdout(),"WINCE port by Rainer Keuchel, 2001-2002\n");
2561 PerlIO_printf(PerlIO_stdout(),"Built on " __DATE__ " " __TIME__ "\n\n");
2564 #ifdef BINARY_BUILD_NOTICE
2565 BINARY_BUILD_NOTICE;
2567 PerlIO_printf(PerlIO_stdout(),
2569 Perl may be copied only under the terms of either the Artistic License or the\n\
2570 GNU General Public License, which may be found in the Perl 5 source kit.\n\n\
2571 Complete documentation for Perl, including FAQ lists, should be found on\n\
2572 this system using `man perl' or `perldoc perl'. If you have access to the\n\
2573 Internet, point your browser at http://www.perl.com/, the Perl Home Page.\n\n");
2576 if (! (PL_dowarn & G_WARN_ALL_MASK))
2577 PL_dowarn |= G_WARN_ON;
2581 PL_dowarn = G_WARN_ALL_ON|G_WARN_ON;
2582 if (!specialWARN(PL_compiling.cop_warnings))
2583 SvREFCNT_dec(PL_compiling.cop_warnings);
2584 PL_compiling.cop_warnings = pWARN_ALL ;
2588 PL_dowarn = G_WARN_ALL_OFF;
2589 if (!specialWARN(PL_compiling.cop_warnings))
2590 SvREFCNT_dec(PL_compiling.cop_warnings);
2591 PL_compiling.cop_warnings = pWARN_NONE ;
2596 if (s[1] == '-') /* Additional switches on #! line. */
2601 #if defined(WIN32) || !defined(PERL_STRICT_CR)
2607 #ifdef ALTERNATE_SHEBANG
2608 case 'S': /* OS/2 needs -S on "extproc" line. */
2616 Perl_croak(aTHX_ "Can't emulate -%.1s on #! line",s);
2621 /* compliments of Tom Christiansen */
2623 /* unexec() can be found in the Gnu emacs distribution */
2624 /* Known to work with -DUNEXEC and using unexelf.c from GNU emacs-20.2 */
2627 Perl_my_unexec(pTHX)
2635 prog = newSVpv(BIN_EXP, 0);
2636 sv_catpv(prog, "/perl");
2637 file = newSVpv(PL_origfilename, 0);
2638 sv_catpv(file, ".perldump");
2640 unexec(SvPVX(file), SvPVX(prog), &etext, sbrk(0), 0);
2641 /* unexec prints msg to stderr in case of failure */
2642 PerlProc_exit(status);
2645 # include <lib$routines.h>
2646 lib$signal(SS$_DEBUG); /* ssdef.h #included from vmsish.h */
2648 ABORT(); /* for use with undump */
2653 /* initialize curinterp */
2659 # define PERLVAR(var,type)
2660 # define PERLVARA(var,n,type)
2661 # if defined(PERL_IMPLICIT_CONTEXT)
2662 # if defined(USE_5005THREADS)
2663 # define PERLVARI(var,type,init) PERL_GET_INTERP->var = init;
2664 # define PERLVARIC(var,type,init) PERL_GET_INTERP->var = init;
2665 # else /* !USE_5005THREADS */
2666 # define PERLVARI(var,type,init) aTHX->var = init;
2667 # define PERLVARIC(var,type,init) aTHX->var = init;
2668 # endif /* USE_5005THREADS */
2670 # define PERLVARI(var,type,init) PERL_GET_INTERP->var = init;
2671 # define PERLVARIC(var,type,init) PERL_GET_INTERP->var = init;
2673 # include "intrpvar.h"
2674 # ifndef USE_5005THREADS
2675 # include "thrdvar.h"
2682 # define PERLVAR(var,type)
2683 # define PERLVARA(var,n,type)
2684 # define PERLVARI(var,type,init) PL_##var = init;
2685 # define PERLVARIC(var,type,init) PL_##var = init;
2686 # include "intrpvar.h"
2687 # ifndef USE_5005THREADS
2688 # include "thrdvar.h"
2699 S_init_main_stash(pTHX)
2703 PL_curstash = PL_defstash = newHV();
2704 PL_curstname = newSVpvn("main",4);
2705 gv = gv_fetchpv("main::",TRUE, SVt_PVHV);
2706 SvREFCNT_dec(GvHV(gv));
2707 GvHV(gv) = (HV*)SvREFCNT_inc(PL_defstash);
2709 HvNAME(PL_defstash) = savepv("main");
2710 PL_incgv = gv_HVadd(gv_AVadd(gv_fetchpv("INC",TRUE, SVt_PVAV)));
2711 GvMULTI_on(PL_incgv);
2712 PL_hintgv = gv_fetchpv("\010",TRUE, SVt_PV); /* ^H */
2713 GvMULTI_on(PL_hintgv);
2714 PL_defgv = gv_fetchpv("_",TRUE, SVt_PVAV);
2715 PL_errgv = gv_HVadd(gv_fetchpv("@", TRUE, SVt_PV));
2716 GvMULTI_on(PL_errgv);
2717 PL_replgv = gv_fetchpv("\022", TRUE, SVt_PV); /* ^R */
2718 GvMULTI_on(PL_replgv);
2719 (void)Perl_form(aTHX_ "%240s",""); /* Preallocate temp - for immediate signals. */
2720 sv_grow(ERRSV, 240); /* Preallocate - for immediate signals. */
2721 sv_setpvn(ERRSV, "", 0);
2722 PL_curstash = PL_defstash;
2723 CopSTASH_set(&PL_compiling, PL_defstash);
2724 PL_debstash = GvHV(gv_fetchpv("DB::", GV_ADDMULTI, SVt_PVHV));
2725 PL_globalstash = GvHV(gv_fetchpv("CORE::GLOBAL::", GV_ADDMULTI, SVt_PVHV));
2726 /* We must init $/ before switches are processed. */
2727 sv_setpvn(get_sv("/", TRUE), "\n", 1);
2731 S_open_script(pTHX_ char *scriptname, bool dosearch, SV *sv, int *fdscript)
2735 char *cpp_discard_flag;
2741 PL_origfilename = savepv("-e");
2744 /* if find_script() returns, it returns a malloc()-ed value */
2745 PL_origfilename = scriptname = find_script(scriptname, dosearch, NULL, 1);
2747 if (strnEQ(scriptname, "/dev/fd/", 8) && isDIGIT(scriptname[8]) ) {
2748 char *s = scriptname + 8;
2749 *fdscript = atoi(s);
2753 scriptname = savepv(s + 1);
2754 Safefree(PL_origfilename);
2755 PL_origfilename = scriptname;
2760 CopFILE_free(PL_curcop);
2761 CopFILE_set(PL_curcop, PL_origfilename);
2762 if (strEQ(PL_origfilename,"-"))
2764 if (*fdscript >= 0) {
2765 PL_rsfp = PerlIO_fdopen(*fdscript,PERL_SCRIPT_MODE);
2766 # if defined(HAS_FCNTL) && defined(F_SETFD)
2768 /* ensure close-on-exec */
2769 fcntl(PerlIO_fileno(PL_rsfp),F_SETFD,1);
2772 else if (PL_preprocess) {
2773 char *cpp_cfg = CPPSTDIN;
2774 SV *cpp = newSVpvn("",0);
2775 SV *cmd = NEWSV(0,0);
2777 if (strEQ(cpp_cfg, "cppstdin"))
2778 Perl_sv_catpvf(aTHX_ cpp, "%s/", BIN_EXP);
2779 sv_catpv(cpp, cpp_cfg);
2782 sv_catpvn(sv, "-I", 2);
2783 sv_catpv(sv,PRIVLIB_EXP);
2786 DEBUG_P(PerlIO_printf(Perl_debug_log,
2787 "PL_preprocess: scriptname=\"%s\", cpp=\"%s\", sv=\"%s\", CPPMINUS=\"%s\"\n",
2788 scriptname, SvPVX (cpp), SvPVX (sv), CPPMINUS));
2790 # if defined(MSDOS) || defined(WIN32) || defined(VMS)
2797 cpp_discard_flag = "";
2799 cpp_discard_flag = "-C";
2803 perl = os2_execname(aTHX);
2805 perl = PL_origargv[0];
2809 /* This strips off Perl comments which might interfere with
2810 the C pre-processor, including #!. #line directives are
2811 deliberately stripped to avoid confusion with Perl's version
2812 of #line. FWP played some golf with it so it will fit
2813 into VMS's 255 character buffer.
2816 code = "(1../^#!.*perl/i)|/^\\s*#(?!\\s*((ifn?|un)def|(el|end)?if|define|include|else|error|pragma)\\b)/||!($|=1)||print";
2818 code = "/^\\s*#(?!\\s*((ifn?|un)def|(el|end)?if|define|include|else|error|pragma)\\b)/||!($|=1)||print";
2820 Perl_sv_setpvf(aTHX_ cmd, "\
2821 %s -ne%s%s%s %s | %"SVf" %s %"SVf" %s",
2822 perl, quote, code, quote, scriptname, cpp,
2823 cpp_discard_flag, sv, CPPMINUS);
2825 PL_doextract = FALSE;
2826 # ifdef IAMSUID /* actually, this is caught earlier */
2827 if (PL_euid != PL_uid && !PL_euid) { /* if running suidperl */
2829 (void)seteuid(PL_uid); /* musn't stay setuid root */
2831 # ifdef HAS_SETREUID
2832 (void)setreuid((Uid_t)-1, PL_uid);
2834 # ifdef HAS_SETRESUID
2835 (void)setresuid((Uid_t)-1, PL_uid, (Uid_t)-1);
2837 PerlProc_setuid(PL_uid);
2841 if (PerlProc_geteuid() != PL_uid)
2842 Perl_croak(aTHX_ "Can't do seteuid!\n");
2844 # endif /* IAMSUID */
2846 DEBUG_P(PerlIO_printf(Perl_debug_log,
2847 "PL_preprocess: cmd=\"%s\"\n",
2850 PL_rsfp = PerlProc_popen(SvPVX(cmd), "r");
2854 else if (!*scriptname) {
2855 forbid_setid("program input from stdin");
2856 PL_rsfp = PerlIO_stdin();
2859 PL_rsfp = PerlIO_open(scriptname,PERL_SCRIPT_MODE);
2860 # if defined(HAS_FCNTL) && defined(F_SETFD)
2862 /* ensure close-on-exec */
2863 fcntl(PerlIO_fileno(PL_rsfp),F_SETFD,1);
2868 # ifndef IAMSUID /* in case script is not readable before setuid */
2870 PerlLIO_stat(CopFILE(PL_curcop),&PL_statbuf) >= 0 &&
2871 PL_statbuf.st_mode & (S_ISUID|S_ISGID))
2874 PerlProc_execv(Perl_form(aTHX_ "%s/sperl"PERL_FS_VER_FMT,
2875 BIN_EXP, (int)PERL_REVISION,
2877 (int)PERL_SUBVERSION), PL_origargv);
2878 Perl_croak(aTHX_ "Can't do setuid\n");
2884 Perl_croak(aTHX_ "Can't open perl script: %s\n",
2887 Perl_croak(aTHX_ "Can't open perl script \"%s\": %s\n",
2888 CopFILE(PL_curcop), Strerror(errno));
2894 * I_SYSSTATVFS HAS_FSTATVFS
2896 * I_STATFS HAS_FSTATFS HAS_GETFSSTAT
2897 * I_MNTENT HAS_GETMNTENT HAS_HASMNTOPT
2898 * here so that metaconfig picks them up. */
2902 S_fd_on_nosuid_fs(pTHX_ int fd)
2904 int check_okay = 0; /* able to do all the required sys/libcalls */
2905 int on_nosuid = 0; /* the fd is on a nosuid fs */
2907 * Preferred order: fstatvfs(), fstatfs(), ustat()+getmnt(), getmntent().
2908 * fstatvfs() is UNIX98.
2909 * fstatfs() is 4.3 BSD.
2910 * ustat()+getmnt() is pre-4.3 BSD.
2911 * getmntent() is O(number-of-mounted-filesystems) and can hang on
2912 * an irrelevant filesystem while trying to reach the right one.
2915 #undef FD_ON_NOSUID_CHECK_OKAY /* found the syscalls to do the check? */
2917 # if !defined(FD_ON_NOSUID_CHECK_OKAY) && \
2918 defined(HAS_FSTATVFS)
2919 # define FD_ON_NOSUID_CHECK_OKAY
2920 struct statvfs stfs;
2922 check_okay = fstatvfs(fd, &stfs) == 0;
2923 on_nosuid = check_okay && (stfs.f_flag & ST_NOSUID);
2924 # endif /* fstatvfs */
2926 # if !defined(FD_ON_NOSUID_CHECK_OKAY) && \
2927 defined(PERL_MOUNT_NOSUID) && \
2928 defined(HAS_FSTATFS) && \
2929 defined(HAS_STRUCT_STATFS) && \
2930 defined(HAS_STRUCT_STATFS_F_FLAGS)
2931 # define FD_ON_NOSUID_CHECK_OKAY
2934 check_okay = fstatfs(fd, &stfs) == 0;
2935 on_nosuid = check_okay && (stfs.f_flags & PERL_MOUNT_NOSUID);
2936 # endif /* fstatfs */
2938 # if !defined(FD_ON_NOSUID_CHECK_OKAY) && \
2939 defined(PERL_MOUNT_NOSUID) && \
2940 defined(HAS_FSTAT) && \
2941 defined(HAS_USTAT) && \
2942 defined(HAS_GETMNT) && \
2943 defined(HAS_STRUCT_FS_DATA) && \
2945 # define FD_ON_NOSUID_CHECK_OKAY
2948 if (fstat(fd, &fdst) == 0) {
2950 if (ustat(fdst.st_dev, &us) == 0) {
2952 /* NOSTAT_ONE here because we're not examining fields which
2953 * vary between that case and STAT_ONE. */
2954 if (getmnt((int*)0, &fsd, (int)0, NOSTAT_ONE, us.f_fname) == 0) {
2955 size_t cmplen = sizeof(us.f_fname);
2956 if (sizeof(fsd.fd_req.path) < cmplen)
2957 cmplen = sizeof(fsd.fd_req.path);
2958 if (strnEQ(fsd.fd_req.path, us.f_fname, cmplen) &&
2959 fdst.st_dev == fsd.fd_req.dev) {
2961 on_nosuid = fsd.fd_req.flags & PERL_MOUNT_NOSUID;
2967 # endif /* fstat+ustat+getmnt */
2969 # if !defined(FD_ON_NOSUID_CHECK_OKAY) && \
2970 defined(HAS_GETMNTENT) && \
2971 defined(HAS_HASMNTOPT) && \
2972 defined(MNTOPT_NOSUID)
2973 # define FD_ON_NOSUID_CHECK_OKAY
2974 FILE *mtab = fopen("/etc/mtab", "r");
2975 struct mntent *entry;
2978 if (mtab && (fstat(fd, &stb) == 0)) {
2979 while (entry = getmntent(mtab)) {
2980 if (stat(entry->mnt_dir, &fsb) == 0
2981 && fsb.st_dev == stb.st_dev)
2983 /* found the filesystem */
2985 if (hasmntopt(entry, MNTOPT_NOSUID))
2988 } /* A single fs may well fail its stat(). */
2993 # endif /* getmntent+hasmntopt */
2996 Perl_croak(aTHX_ "Can't check filesystem of script \"%s\" for nosuid", PL_origfilename);
2999 #endif /* IAMSUID */
3002 S_validate_suid(pTHX_ char *validarg, char *scriptname, int fdscript)
3008 /* do we need to emulate setuid on scripts? */
3010 /* This code is for those BSD systems that have setuid #! scripts disabled
3011 * in the kernel because of a security problem. Merely defining DOSUID
3012 * in perl will not fix that problem, but if you have disabled setuid
3013 * scripts in the kernel, this will attempt to emulate setuid and setgid
3014 * on scripts that have those now-otherwise-useless bits set. The setuid
3015 * root version must be called suidperl or sperlN.NNN. If regular perl
3016 * discovers that it has opened a setuid script, it calls suidperl with
3017 * the same argv that it had. If suidperl finds that the script it has
3018 * just opened is NOT setuid root, it sets the effective uid back to the
3019 * uid. We don't just make perl setuid root because that loses the
3020 * effective uid we had before invoking perl, if it was different from the
3023 * DOSUID must be defined in both perl and suidperl, and IAMSUID must
3024 * be defined in suidperl only. suidperl must be setuid root. The
3025 * Configure script will set this up for you if you want it.
3031 if (PerlLIO_fstat(PerlIO_fileno(PL_rsfp),&PL_statbuf) < 0) /* normal stat is insecure */
3032 Perl_croak(aTHX_ "Can't stat script \"%s\"",PL_origfilename);
3033 if (fdscript < 0 && PL_statbuf.st_mode & (S_ISUID|S_ISGID)) {
3038 #ifndef HAS_SETREUID
3039 /* On this access check to make sure the directories are readable,
3040 * there is actually a small window that the user could use to make
3041 * filename point to an accessible directory. So there is a faint
3042 * chance that someone could execute a setuid script down in a
3043 * non-accessible directory. I don't know what to do about that.
3044 * But I don't think it's too important. The manual lies when
3045 * it says access() is useful in setuid programs.
3047 if (PerlLIO_access(CopFILE(PL_curcop),1)) /*double check*/
3048 Perl_croak(aTHX_ "Permission denied");
3050 /* If we can swap euid and uid, then we can determine access rights
3051 * with a simple stat of the file, and then compare device and
3052 * inode to make sure we did stat() on the same file we opened.
3053 * Then we just have to make sure he or she can execute it.
3060 setreuid(PL_euid,PL_uid) < 0
3063 setresuid(PL_euid,PL_uid,(Uid_t)-1) < 0
3066 || PerlProc_getuid() != PL_euid || PerlProc_geteuid() != PL_uid)
3067 Perl_croak(aTHX_ "Can't swap uid and euid"); /* really paranoid */
3068 if (PerlLIO_stat(CopFILE(PL_curcop),&tmpstatbuf) < 0)
3069 Perl_croak(aTHX_ "Permission denied"); /* testing full pathname here */
3070 #if defined(IAMSUID) && !defined(NO_NOSUID_CHECK)
3071 if (fd_on_nosuid_fs(PerlIO_fileno(PL_rsfp)))
3072 Perl_croak(aTHX_ "Permission denied");
3074 if (tmpstatbuf.st_dev != PL_statbuf.st_dev ||
3075 tmpstatbuf.st_ino != PL_statbuf.st_ino) {
3076 (void)PerlIO_close(PL_rsfp);
3077 Perl_croak(aTHX_ "Permission denied\n");
3081 setreuid(PL_uid,PL_euid) < 0
3083 # if defined(HAS_SETRESUID)
3084 setresuid(PL_uid,PL_euid,(Uid_t)-1) < 0
3087 || PerlProc_getuid() != PL_uid || PerlProc_geteuid() != PL_euid)
3088 Perl_croak(aTHX_ "Can't reswap uid and euid");
3089 if (!cando(S_IXUSR,FALSE,&PL_statbuf)) /* can real uid exec? */
3090 Perl_croak(aTHX_ "Permission denied\n");
3092 #endif /* HAS_SETREUID */
3093 #endif /* IAMSUID */
3095 if (!S_ISREG(PL_statbuf.st_mode))
3096 Perl_croak(aTHX_ "Permission denied");
3097 if (PL_statbuf.st_mode & S_IWOTH)
3098 Perl_croak(aTHX_ "Setuid/gid script is writable by world");
3099 PL_doswitches = FALSE; /* -s is insecure in suid */
3100 CopLINE_inc(PL_curcop);
3101 if (sv_gets(PL_linestr, PL_rsfp, 0) == Nullch ||
3102 strnNE(SvPV(PL_linestr,n_a),"#!",2) ) /* required even on Sys V */
3103 Perl_croak(aTHX_ "No #! line");
3104 s = SvPV(PL_linestr,n_a)+2;
3106 while (!isSPACE(*s)) s++;
3107 for (s2 = s; (s2 > SvPV(PL_linestr,n_a)+2 &&
3108 (isDIGIT(s2[-1]) || strchr("._-", s2[-1]))); s2--) ;
3109 if (strnNE(s2-4,"perl",4) && strnNE(s-9,"perl",4)) /* sanity check */
3110 Perl_croak(aTHX_ "Not a perl script");
3111 while (*s == ' ' || *s == '\t') s++;
3113 * #! arg must be what we saw above. They can invoke it by
3114 * mentioning suidperl explicitly, but they may not add any strange
3115 * arguments beyond what #! says if they do invoke suidperl that way.
3117 len = strlen(validarg);
3118 if (strEQ(validarg," PHOOEY ") ||
3119 strnNE(s,validarg,len) || !isSPACE(s[len]))
3120 Perl_croak(aTHX_ "Args must match #! line");
3123 if (PL_euid != PL_uid && (PL_statbuf.st_mode & S_ISUID) &&
3124 PL_euid == PL_statbuf.st_uid)
3126 Perl_croak(aTHX_ "YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
3127 FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
3128 #endif /* IAMSUID */
3130 if (PL_euid) { /* oops, we're not the setuid root perl */
3131 (void)PerlIO_close(PL_rsfp);
3134 PerlProc_execv(Perl_form(aTHX_ "%s/sperl"PERL_FS_VER_FMT, BIN_EXP,
3135 (int)PERL_REVISION, (int)PERL_VERSION,
3136 (int)PERL_SUBVERSION), PL_origargv);
3138 Perl_croak(aTHX_ "Can't do setuid\n");
3141 if (PL_statbuf.st_mode & S_ISGID && PL_statbuf.st_gid != PL_egid) {
3143 (void)setegid(PL_statbuf.st_gid);
3146 (void)setregid((Gid_t)-1,PL_statbuf.st_gid);
3148 #ifdef HAS_SETRESGID
3149 (void)setresgid((Gid_t)-1,PL_statbuf.st_gid,(Gid_t)-1);
3151 PerlProc_setgid(PL_statbuf.st_gid);
3155 if (PerlProc_getegid() != PL_statbuf.st_gid)
3156 Perl_croak(aTHX_ "Can't do setegid!\n");
3158 if (PL_statbuf.st_mode & S_ISUID) {
3159 if (PL_statbuf.st_uid != PL_euid)
3161 (void)seteuid(PL_statbuf.st_uid); /* all that for this */
3164 (void)setreuid((Uid_t)-1,PL_statbuf.st_uid);
3166 #ifdef HAS_SETRESUID
3167 (void)setresuid((Uid_t)-1,PL_statbuf.st_uid,(Uid_t)-1);
3169 PerlProc_setuid(PL_statbuf.st_uid);
3173 if (PerlProc_geteuid() != PL_statbuf.st_uid)
3174 Perl_croak(aTHX_ "Can't do seteuid!\n");
3176 else if (PL_uid) { /* oops, mustn't run as root */
3178 (void)seteuid((Uid_t)PL_uid);
3181 (void)setreuid((Uid_t)-1,(Uid_t)PL_uid);
3183 #ifdef HAS_SETRESUID
3184 (void)setresuid((Uid_t)-1,(Uid_t)PL_uid,(Uid_t)-1);
3186 PerlProc_setuid((Uid_t)PL_uid);
3190 if (PerlProc_geteuid() != PL_uid)
3191 Perl_croak(aTHX_ "Can't do seteuid!\n");
3194 if (!cando(S_IXUSR,TRUE,&PL_statbuf))
3195 Perl_croak(aTHX_ "Permission denied\n"); /* they can't do this */
3198 else if (PL_preprocess)
3199 Perl_croak(aTHX_ "-P not allowed for setuid/setgid script\n");
3200 else if (fdscript >= 0)
3201 Perl_croak(aTHX_ "fd script not allowed in suidperl\n");
3203 Perl_croak(aTHX_ "Script is not setuid/setgid in suidperl\n");
3205 /* We absolutely must clear out any saved ids here, so we */
3206 /* exec the real perl, substituting fd script for scriptname. */
3207 /* (We pass script name as "subdir" of fd, which perl will grok.) */
3208 PerlIO_rewind(PL_rsfp);
3209 PerlLIO_lseek(PerlIO_fileno(PL_rsfp),(Off_t)0,0); /* just in case rewind didn't */
3210 for (which = 1; PL_origargv[which] && PL_origargv[which] != scriptname; which++) ;
3211 if (!PL_origargv[which])
3212 Perl_croak(aTHX_ "Permission denied");
3213 PL_origargv[which] = savepv(Perl_form(aTHX_ "/dev/fd/%d/%s",
3214 PerlIO_fileno(PL_rsfp), PL_origargv[which]));
3215 #if defined(HAS_FCNTL) && defined(F_SETFD)
3216 fcntl(PerlIO_fileno(PL_rsfp),F_SETFD,0); /* ensure no close-on-exec */
3218 PerlProc_execv(Perl_form(aTHX_ "%s/perl"PERL_FS_VER_FMT, BIN_EXP,
3219 (int)PERL_REVISION, (int)PERL_VERSION,
3220 (int)PERL_SUBVERSION), PL_origargv);/* try again */
3221 Perl_croak(aTHX_ "Can't do setuid\n");
3222 #endif /* IAMSUID */
3224 if (PL_euid != PL_uid || PL_egid != PL_gid) { /* (suidperl doesn't exist, in fact) */
3225 #ifndef SETUID_SCRIPTS_ARE_SECURE_NOW
3226 PerlLIO_fstat(PerlIO_fileno(PL_rsfp),&PL_statbuf); /* may be either wrapped or real suid */
3227 if ((PL_euid != PL_uid && PL_euid == PL_statbuf.st_uid && PL_statbuf.st_mode & S_ISUID)
3229 (PL_egid != PL_gid && PL_egid == PL_statbuf.st_gid && PL_statbuf.st_mode & S_ISGID)
3232 Perl_croak(aTHX_ "YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
3233 FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
3234 #endif /* SETUID_SCRIPTS_ARE_SECURE_NOW */
3235 /* not set-id, must be wrapped */
3241 S_find_beginning(pTHX)
3243 register char *s, *s2;
3244 #ifdef MACOS_TRADITIONAL
3248 /* skip forward in input to the real script? */
3251 #ifdef MACOS_TRADITIONAL
3252 /* Since the Mac OS does not honor #! arguments for us, we do it ourselves */
3254 while (PL_doextract || gMacPerl_AlwaysExtract) {
3255 if ((s = sv_gets(PL_linestr, PL_rsfp, 0)) == Nullch) {
3256 if (!gMacPerl_AlwaysExtract)
3257 Perl_croak(aTHX_ "No Perl script found in input\n");
3259 if (PL_doextract) /* require explicit override ? */
3260 if (!OverrideExtract(PL_origfilename))
3261 Perl_croak(aTHX_ "User aborted script\n");
3263 PL_doextract = FALSE;
3265 /* Pater peccavi, file does not have #! */
3266 PerlIO_rewind(PL_rsfp);
3271 while (PL_doextract) {
3272 if ((s = sv_gets(PL_linestr, PL_rsfp, 0)) == Nullch)
3273 Perl_croak(aTHX_ "No Perl script found in input\n");
3276 if (*s == '#' && s[1] == '!' && ((s = instr(s,"perl")) || (s = instr(s2,"PERL")))) {
3277 PerlIO_ungetc(PL_rsfp, '\n'); /* to keep line count right */
3278 PL_doextract = FALSE;
3279 while (*s && !(isSPACE (*s) || *s == '#')) s++;
3281 while (*s == ' ' || *s == '\t') s++;
3283 while (isDIGIT(s2[-1]) || strchr("-._", s2[-1])) s2--;
3284 if (strnEQ(s2-4,"perl",4))
3286 while ((s = moreswitches(s)))
3289 #ifdef MACOS_TRADITIONAL
3290 /* We are always searching for the #!perl line in MacPerl,
3291 * so if we find it, still keep the line count correct
3292 * by counting lines we already skipped over
3294 for (; maclines > 0 ; maclines--)
3295 PerlIO_ungetc(PL_rsfp, '\n');
3299 /* gMacPerl_AlwaysExtract is false in MPW tool */
3300 } else if (gMacPerl_AlwaysExtract) {
3311 PL_uid = PerlProc_getuid();
3312 PL_euid = PerlProc_geteuid();
3313 PL_gid = PerlProc_getgid();
3314 PL_egid = PerlProc_getegid();
3316 PL_uid |= PL_gid << 16;
3317 PL_euid |= PL_egid << 16;
3319 /* Should not happen: */
3320 CHECK_MALLOC_TAINT(PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
3321 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
3325 /* This is used very early in the lifetime of the program. */
3327 Perl_doing_taint(int argc, char *argv[], char *envp[])
3329 int uid = PerlProc_getuid();
3330 int euid = PerlProc_geteuid();
3331 int gid = PerlProc_getgid();
3332 int egid = PerlProc_getegid();
3338 if (uid && (euid != uid || egid != gid))
3340 /* This is a really primitive check; $ENV{PERL_MALLOC_OPT} is
3341 ignored only if -T are the first chars together; otherwise one
3342 gets "Too late" message. */
3343 if ( argc > 1 && argv[1][0] == '-'
3344 && (argv[1][1] == 't' || argv[1][1] == 'T') )
3351 S_forbid_setid(pTHX_ char *s)
3353 if (PL_euid != PL_uid)
3354 Perl_croak(aTHX_ "No %s allowed while running setuid", s);
3355 if (PL_egid != PL_gid)
3356 Perl_croak(aTHX_ "No %s allowed while running setgid", s);
3360 Perl_init_debugger(pTHX)
3362 HV *ostash = PL_curstash;
3364 PL_curstash = PL_debstash;
3365 PL_dbargs = GvAV(gv_AVadd((gv_fetchpv("args", GV_ADDMULTI, SVt_PVAV))));
3366 AvREAL_off(PL_dbargs);
3367 PL_DBgv = gv_fetchpv("DB", GV_ADDMULTI, SVt_PVGV);
3368 PL_DBline = gv_fetchpv("dbline", GV_ADDMULTI, SVt_PVAV);
3369 PL_DBsub = gv_HVadd(gv_fetchpv("sub", GV_ADDMULTI, SVt_PVHV));
3370 sv_upgrade(GvSV(PL_DBsub), SVt_IV); /* IVX accessed if PERLDB_SUB_NN */
3371 PL_DBsingle = GvSV((gv_fetchpv("single", GV_ADDMULTI, SVt_PV)));
3372 sv_setiv(PL_DBsingle, 0);
3373 PL_DBtrace = GvSV((gv_fetchpv("trace", GV_ADDMULTI, SVt_PV)));
3374 sv_setiv(PL_DBtrace, 0);
3375 PL_DBsignal = GvSV((gv_fetchpv("signal", GV_ADDMULTI, SVt_PV)));
3376 sv_setiv(PL_DBsignal, 0);
3377 PL_DBassertion = GvSV((gv_fetchpv("assertion", GV_ADDMULTI, SVt_PV)));
3378 sv_setiv(PL_DBassertion, 0);
3379 PL_curstash = ostash;
3382 #ifndef STRESS_REALLOC
3383 #define REASONABLE(size) (size)
3385 #define REASONABLE(size) (1) /* unreasonable */
3389 Perl_init_stacks(pTHX)
3391 /* start with 128-item stack and 8K cxstack */
3392 PL_curstackinfo = new_stackinfo(REASONABLE(128),
3393 REASONABLE(8192/sizeof(PERL_CONTEXT) - 1));
3394 PL_curstackinfo->si_type = PERLSI_MAIN;
3395 PL_curstack = PL_curstackinfo->si_stack;
3396 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
3398 PL_stack_base = AvARRAY(PL_curstack);
3399 PL_stack_sp = PL_stack_base;
3400 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
3402 New(50,PL_tmps_stack,REASONABLE(128),SV*);
3405 PL_tmps_max = REASONABLE(128);
3407 New(54,PL_markstack,REASONABLE(32),I32);
3408 PL_markstack_ptr = PL_markstack;
3409 PL_markstack_max = PL_markstack + REASONABLE(32);
3413 New(54,PL_scopestack,REASONABLE(32),I32);
3414 PL_scopestack_ix = 0;
3415 PL_scopestack_max = REASONABLE(32);
3417 New(54,PL_savestack,REASONABLE(128),ANY);
3418 PL_savestack_ix = 0;
3419 PL_savestack_max = REASONABLE(128);
3421 New(54,PL_retstack,REASONABLE(16),OP*);
3423 PL_retstack_max = REASONABLE(16);
3431 while (PL_curstackinfo->si_next)
3432 PL_curstackinfo = PL_curstackinfo->si_next;
3433 while (PL_curstackinfo) {
3434 PERL_SI *p = PL_curstackinfo->si_prev;
3435 /* curstackinfo->si_stack got nuked by sv_free_arenas() */
3436 Safefree(PL_curstackinfo->si_cxstack);
3437 Safefree(PL_curstackinfo);
3438 PL_curstackinfo = p;
3440 Safefree(PL_tmps_stack);
3441 Safefree(PL_markstack);
3442 Safefree(PL_scopestack);
3443 Safefree(PL_savestack);
3444 Safefree(PL_retstack);
3453 lex_start(PL_linestr);
3455 PL_subname = newSVpvn("main",4);
3459 S_init_predump_symbols(pTHX)
3464 sv_setpvn(get_sv("\"", TRUE), " ", 1);
3465 PL_stdingv = gv_fetchpv("STDIN",TRUE, SVt_PVIO);
3466 GvMULTI_on(PL_stdingv);
3467 io = GvIOp(PL_stdingv);
3468 IoTYPE(io) = IoTYPE_RDONLY;
3469 IoIFP(io) = PerlIO_stdin();
3470 tmpgv = gv_fetchpv("stdin",TRUE, SVt_PV);
3472 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(io);
3474 tmpgv = gv_fetchpv("STDOUT",TRUE, SVt_PVIO);
3477 IoTYPE(io) = IoTYPE_WRONLY;
3478 IoOFP(io) = IoIFP(io) = PerlIO_stdout();
3480 tmpgv = gv_fetchpv("stdout",TRUE, SVt_PV);
3482 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(io);
3484 PL_stderrgv = gv_fetchpv("STDERR",TRUE, SVt_PVIO);
3485 GvMULTI_on(PL_stderrgv);
3486 io = GvIOp(PL_stderrgv);
3487 IoTYPE(io) = IoTYPE_WRONLY;
3488 IoOFP(io) = IoIFP(io) = PerlIO_stderr();
3489 tmpgv = gv_fetchpv("stderr",TRUE, SVt_PV);
3491 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(io);
3493 PL_statname = NEWSV(66,0); /* last filename we did stat on */
3496 Safefree(PL_osname);
3497 PL_osname = savepv(OSNAME);
3501 Perl_init_argv_symbols(pTHX_ register int argc, register char **argv)
3504 argc--,argv++; /* skip name of script */
3505 if (PL_doswitches) {
3506 for (; argc > 0 && **argv == '-'; argc--,argv++) {
3509 if (argv[0][1] == '-' && !argv[0][2]) {
3513 if ((s = strchr(argv[0], '='))) {
3515 sv_setpv(GvSV(gv_fetchpv(argv[0]+1,TRUE, SVt_PV)),s);
3518 sv_setiv(GvSV(gv_fetchpv(argv[0]+1,TRUE, SVt_PV)),1);
3521 if ((PL_argvgv = gv_fetchpv("ARGV",TRUE, SVt_PVAV))) {
3522 GvMULTI_on(PL_argvgv);
3523 (void)gv_AVadd(PL_argvgv);
3524 av_clear(GvAVn(PL_argvgv));
3525 for (; argc > 0; argc--,argv++) {
3526 SV *sv = newSVpv(argv[0],0);
3527 av_push(GvAVn(PL_argvgv),sv);
3528 if (!(PL_unicode & PERL_UNICODE_LOCALE_FLAG) || PL_utf8locale) {
3529 if (PL_unicode & PERL_UNICODE_ARGV_FLAG)
3532 if (PL_unicode & PERL_UNICODE_WIDESYSCALLS_FLAG) /* Sarathy? */
3533 (void)sv_utf8_decode(sv);
3538 #ifdef HAS_PROCSELFEXE
3539 /* This is a function so that we don't hold on to MAXPATHLEN
3540 bytes of stack longer than necessary
3543 S_procself_val(pTHX_ SV *sv, char *arg0)
3545 char buf[MAXPATHLEN];
3546 int len = readlink(PROCSELFEXE_PATH, buf, sizeof(buf) - 1);
3548 /* On Playstation2 Linux V1.0 (kernel 2.2.1) readlink(/proc/self/exe)
3549 includes a spurious NUL which will cause $^X to fail in system
3550 or backticks (this will prevent extensions from being built and
3551 many tests from working). readlink is not meant to add a NUL.
3552 Normal readlink works fine.
3554 if (len > 0 && buf[len-1] == '\0') {
3558 /* FreeBSD's implementation is acknowledged to be imperfect, sometimes
3559 returning the text "unknown" from the readlink rather than the path
3560 to the executable (or returning an error from the readlink). Any valid
3561 path has a '/' in it somewhere, so use that to validate the result.
3562 See http://www.freebsd.org/cgi/query-pr.cgi?pr=35703
3564 if (len > 0 && memchr(buf, '/', len)) {
3565 sv_setpvn(sv,buf,len);
3571 #endif /* HAS_PROCSELFEXE */
3574 S_init_postdump_symbols(pTHX_ register int argc, register char **argv, register char **env)
3580 PL_toptarget = NEWSV(0,0);
3581 sv_upgrade(PL_toptarget, SVt_PVFM);
3582 sv_setpvn(PL_toptarget, "", 0);
3583 PL_bodytarget = NEWSV(0,0);
3584 sv_upgrade(PL_bodytarget, SVt_PVFM);
3585 sv_setpvn(PL_bodytarget, "", 0);
3586 PL_formtarget = PL_bodytarget;
3590 init_argv_symbols(argc,argv);
3592 if ((tmpgv = gv_fetchpv("0",TRUE, SVt_PV))) {
3593 #ifdef MACOS_TRADITIONAL
3594 /* $0 is not majick on a Mac */
3595 sv_setpv(GvSV(tmpgv),MacPerl_MPWFileName(PL_origfilename));
3597 sv_setpv(GvSV(tmpgv),PL_origfilename);
3598 magicname("0", "0", 1);
3601 if ((tmpgv = gv_fetchpv("\030",TRUE, SVt_PV))) {/* $^X */
3602 #ifdef HAS_PROCSELFEXE
3603 S_procself_val(aTHX_ GvSV(tmpgv), PL_origargv[0]);
3606 sv_setpv(GvSV(tmpgv), os2_execname(aTHX));
3608 sv_setpv(GvSV(tmpgv),PL_origargv[0]);
3612 if ((PL_envgv = gv_fetchpv("ENV",TRUE, SVt_PVHV))) {
3614 GvMULTI_on(PL_envgv);
3615 hv = GvHVn(PL_envgv);
3616 hv_magic(hv, Nullgv, PERL_MAGIC_env);
3617 #ifdef USE_ENVIRON_ARRAY
3618 /* Note that if the supplied env parameter is actually a copy
3619 of the global environ then it may now point to free'd memory
3620 if the environment has been modified since. To avoid this
3621 problem we treat env==NULL as meaning 'use the default'
3626 # ifdef USE_ITHREADS
3627 && PL_curinterp == aTHX
3631 environ[0] = Nullch;
3634 for (; *env; env++) {
3635 if (!(s = strchr(*env,'=')))
3642 sv = newSVpv(s+1, 0);
3643 (void)hv_store(hv, *env, s - *env, sv, 0);
3647 #endif /* USE_ENVIRON_ARRAY */
3650 if ((tmpgv = gv_fetchpv("$",TRUE, SVt_PV))) {
3651 SvREADONLY_off(GvSV(tmpgv));
3652 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3653 SvREADONLY_on(GvSV(tmpgv));
3655 #ifdef THREADS_HAVE_PIDS
3656 PL_ppid = (IV)getppid();
3659 /* touch @F array to prevent spurious warnings 20020415 MJD */
3661 (void) get_av("main::F", TRUE | GV_ADDMULTI);
3663 /* touch @- and @+ arrays to prevent spurious warnings 20020415 MJD */
3664 (void) get_av("main::-", TRUE | GV_ADDMULTI);
3665 (void) get_av("main::+", TRUE | GV_ADDMULTI);
3669 S_init_perllib(pTHX)
3674 s = PerlEnv_getenv("PERL5LIB");
3676 incpush(s, TRUE, TRUE, TRUE);
3678 incpush(PerlEnv_getenv("PERLLIB"), FALSE, FALSE, TRUE);
3680 /* Treat PERL5?LIB as a possible search list logical name -- the
3681 * "natural" VMS idiom for a Unix path string. We allow each
3682 * element to be a set of |-separated directories for compatibility.
3686 if (my_trnlnm("PERL5LIB",buf,0))
3687 do { incpush(buf,TRUE,TRUE,TRUE); } while (my_trnlnm("PERL5LIB",buf,++idx));
3689 while (my_trnlnm("PERLLIB",buf,idx++)) incpush(buf,FALSE,FALSE,TRUE);
3693 /* Use the ~-expanded versions of APPLLIB (undocumented),
3694 ARCHLIB PRIVLIB SITEARCH SITELIB VENDORARCH and VENDORLIB
3697 incpush(APPLLIB_EXP, TRUE, TRUE, TRUE);
3701 incpush(ARCHLIB_EXP, FALSE, FALSE, TRUE);
3703 #ifdef MACOS_TRADITIONAL
3706 SV * privdir = NEWSV(55, 0);
3707 char * macperl = PerlEnv_getenv("MACPERL");
3712 Perl_sv_setpvf(aTHX_ privdir, "%slib:", macperl);
3713 if (PerlLIO_stat(SvPVX(privdir), &tmpstatbuf) >= 0 && S_ISDIR(tmpstatbuf.st_mode))
3714 incpush(SvPVX(privdir), TRUE, FALSE, TRUE);
3715 Perl_sv_setpvf(aTHX_ privdir, "%ssite_perl:", macperl);
3716 if (PerlLIO_stat(SvPVX(privdir), &tmpstatbuf) >= 0 && S_ISDIR(tmpstatbuf.st_mode))
3717 incpush(SvPVX(privdir), TRUE, FALSE, TRUE);
3719 SvREFCNT_dec(privdir);
3722 incpush(":", FALSE, FALSE, TRUE);
3725 # define PRIVLIB_EXP "/usr/local/lib/perl5:/usr/local/lib/perl"
3728 incpush(PRIVLIB_EXP, TRUE, FALSE, TRUE);
3730 incpush(PRIVLIB_EXP, FALSE, FALSE, TRUE);
3734 /* sitearch is always relative to sitelib on Windows for
3735 * DLL-based path intuition to work correctly */
3736 # if !defined(WIN32)
3737 incpush(SITEARCH_EXP, FALSE, FALSE, TRUE);
3743 /* this picks up sitearch as well */
3744 incpush(SITELIB_EXP, TRUE, FALSE, TRUE);
3746 incpush(SITELIB_EXP, FALSE, FALSE, TRUE);
3750 #ifdef SITELIB_STEM /* Search for version-specific dirs below here */
3751 incpush(SITELIB_STEM, FALSE, TRUE, TRUE);
3754 #ifdef PERL_VENDORARCH_EXP
3755 /* vendorarch is always relative to vendorlib on Windows for
3756 * DLL-based path intuition to work correctly */
3757 # if !defined(WIN32)
3758 incpush(PERL_VENDORARCH_EXP, FALSE, FALSE, TRUE);
3762 #ifdef PERL_VENDORLIB_EXP
3764 incpush(PERL_VENDORLIB_EXP, TRUE, FALSE, TRUE); /* this picks up vendorarch as well */
3766 incpush(PERL_VENDORLIB_EXP, FALSE, FALSE, TRUE);
3770 #ifdef PERL_VENDORLIB_STEM /* Search for version-specific dirs below here */
3771 incpush(PERL_VENDORLIB_STEM, FALSE, TRUE, TRUE);
3774 #ifdef PERL_OTHERLIBDIRS
3775 incpush(PERL_OTHERLIBDIRS, TRUE, TRUE, TRUE);
3779 incpush(".", FALSE, FALSE, TRUE);
3780 #endif /* MACOS_TRADITIONAL */
3783 #if defined(DOSISH) || defined(EPOC)
3784 # define PERLLIB_SEP ';'
3787 # define PERLLIB_SEP '|'
3789 # if defined(MACOS_TRADITIONAL)
3790 # define PERLLIB_SEP ','
3792 # define PERLLIB_SEP ':'
3796 #ifndef PERLLIB_MANGLE
3797 # define PERLLIB_MANGLE(s,n) (s)
3801 S_incpush(pTHX_ char *p, int addsubdirs, int addoldvers, int usesep)
3803 SV *subdir = Nullsv;
3808 if (addsubdirs || addoldvers) {
3809 subdir = sv_newmortal();
3812 /* Break at all separators */
3814 SV *libdir = NEWSV(55,0);
3817 /* skip any consecutive separators */
3819 while ( *p == PERLLIB_SEP ) {
3820 /* Uncomment the next line for PATH semantics */
3821 /* av_push(GvAVn(PL_incgv), newSVpvn(".", 1)); */
3826 if ( usesep && (s = strchr(p, PERLLIB_SEP)) != Nullch ) {
3827 sv_setpvn(libdir, PERLLIB_MANGLE(p, (STRLEN)(s - p)),
3832 sv_setpv(libdir, PERLLIB_MANGLE(p, 0));
3833 p = Nullch; /* break out */
3835 #ifdef MACOS_TRADITIONAL
3836 if (!strchr(SvPVX(libdir), ':')) {
3839 sv_setpv(libdir, MacPerl_CanonDir(SvPVX(libdir), buf, 0));
3841 if (SvPVX(libdir)[SvCUR(libdir)-1] != ':')
3842 sv_catpv(libdir, ":");
3846 * BEFORE pushing libdir onto @INC we may first push version- and
3847 * archname-specific sub-directories.
3849 if (addsubdirs || addoldvers) {
3850 #ifdef PERL_INC_VERSION_LIST
3851 /* Configure terminates PERL_INC_VERSION_LIST with a NULL */
3852 const char *incverlist[] = { PERL_INC_VERSION_LIST };
3853 const char **incver;
3860 if ((unix = tounixspec_ts(SvPV(libdir,len),Nullch)) != Nullch) {
3862 while (unix[len-1] == '/') len--; /* Cosmetic */
3863 sv_usepvn(libdir,unix,len);
3866 PerlIO_printf(Perl_error_log,
3867 "Failed to unixify @INC element \"%s\"\n",
3871 #ifdef MACOS_TRADITIONAL
3872 #define PERL_AV_SUFFIX_FMT ""
3873 #define PERL_ARCH_FMT "%s:"
3874 #define PERL_ARCH_FMT_PATH PERL_FS_VER_FMT PERL_AV_SUFFIX_FMT
3876 #define PERL_AV_SUFFIX_FMT "/"
3877 #define PERL_ARCH_FMT "/%s"
3878 #define PERL_ARCH_FMT_PATH PERL_AV_SUFFIX_FMT PERL_FS_VER_FMT
3880 /* .../version/archname if -d .../version/archname */
3881 Perl_sv_setpvf(aTHX_ subdir, "%"SVf PERL_ARCH_FMT_PATH PERL_ARCH_FMT,
3883 (int)PERL_REVISION, (int)PERL_VERSION,
3884 (int)PERL_SUBVERSION, ARCHNAME);
3885 if (PerlLIO_stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
3886 S_ISDIR(tmpstatbuf.st_mode))
3887 av_push(GvAVn(PL_incgv), newSVsv(subdir));
3889 /* .../version if -d .../version */
3890 Perl_sv_setpvf(aTHX_ subdir, "%"SVf PERL_ARCH_FMT_PATH, libdir,
3891 (int)PERL_REVISION, (int)PERL_VERSION,
3892 (int)PERL_SUBVERSION);
3893 if (PerlLIO_stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
3894 S_ISDIR(tmpstatbuf.st_mode))
3895 av_push(GvAVn(PL_incgv), newSVsv(subdir));
3897 /* .../archname if -d .../archname */
3898 Perl_sv_setpvf(aTHX_ subdir, "%"SVf PERL_ARCH_FMT, libdir, ARCHNAME);
3899 if (PerlLIO_stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
3900 S_ISDIR(tmpstatbuf.st_mode))
3901 av_push(GvAVn(PL_incgv), newSVsv(subdir));
3904 #ifdef PERL_INC_VERSION_LIST
3906 for (incver = incverlist; *incver; incver++) {
3907 /* .../xxx if -d .../xxx */
3908 Perl_sv_setpvf(aTHX_ subdir, "%"SVf PERL_ARCH_FMT, libdir, *incver);
3909 if (PerlLIO_stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
3910 S_ISDIR(tmpstatbuf.st_mode))
3911 av_push(GvAVn(PL_incgv), newSVsv(subdir));
3917 /* finally push this lib directory on the end of @INC */
3918 av_push(GvAVn(PL_incgv), libdir);
3922 #ifdef USE_5005THREADS
3923 STATIC struct perl_thread *
3924 S_init_main_thread(pTHX)
3926 #if !defined(PERL_IMPLICIT_CONTEXT)
3927 struct perl_thread *thr;
3931 Newz(53, thr, 1, struct perl_thread);
3932 PL_curcop = &PL_compiling;
3933 thr->interp = PERL_GET_INTERP;
3934 thr->cvcache = newHV();
3935 thr->threadsv = newAV();
3936 /* thr->threadsvp is set when find_threadsv is called */
3937 thr->specific = newAV();
3938 thr->flags = THRf_R_JOINABLE;
3939 MUTEX_INIT(&thr->mutex);
3940 /* Handcraft thrsv similarly to mess_sv */
3941 New(53, PL_thrsv, 1, SV);
3942 Newz(53, xpv, 1, XPV);
3943 SvFLAGS(PL_thrsv) = SVt_PV;
3944 SvANY(PL_thrsv) = (void*)xpv;
3945 SvREFCNT(PL_thrsv) = 1 << 30; /* practically infinite */
3946 SvPVX(PL_thrsv) = (char*)thr;
3947 SvCUR_set(PL_thrsv, sizeof(thr));
3948 SvLEN_set(PL_thrsv, sizeof(thr));
3949 *SvEND(PL_thrsv) = '\0'; /* in the trailing_nul field */
3950 thr->oursv = PL_thrsv;
3951 PL_chopset = " \n-";
3954 MUTEX_LOCK(&PL_threads_mutex);
3960 MUTEX_UNLOCK(&PL_threads_mutex);
3962 #ifdef HAVE_THREAD_INTERN
3963 Perl_init_thread_intern(thr);
3966 #ifdef SET_THREAD_SELF
3967 SET_THREAD_SELF(thr);
3969 thr->self = pthread_self();
3970 #endif /* SET_THREAD_SELF */
3974 * These must come after the thread self setting
3975 * because sv_setpvn does SvTAINT and the taint
3976 * fields thread selfness being set.
3978 PL_toptarget = NEWSV(0,0);
3979 sv_upgrade(PL_toptarget, SVt_PVFM);
3980 sv_setpvn(PL_toptarget, "", 0);
3981 PL_bodytarget = NEWSV(0,0);
3982 sv_upgrade(PL_bodytarget, SVt_PVFM);
3983 sv_setpvn(PL_bodytarget, "", 0);
3984 PL_formtarget = PL_bodytarget;
3985 thr->errsv = newSVpvn("", 0);
3986 (void) find_threadsv("@"); /* Ensure $@ is initialised early */
3989 PL_peepp = MEMBER_TO_FPTR(Perl_peep);
3990 PL_regcompp = MEMBER_TO_FPTR(Perl_pregcomp);
3991 PL_regexecp = MEMBER_TO_FPTR(Perl_regexec_flags);
3992 PL_regint_start = MEMBER_TO_FPTR(Perl_re_intuit_start);
3993 PL_regint_string = MEMBER_TO_FPTR(Perl_re_intuit_string);
3994 PL_regfree = MEMBER_TO_FPTR(Perl_pregfree);
3996 PL_reginterp_cnt = 0;
4000 #endif /* USE_5005THREADS */
4003 Perl_call_list(pTHX_ I32 oldscope, AV *paramList)
4006 line_t oldline = CopLINE(PL_curcop);
4012 while (AvFILL(paramList) >= 0) {
4013 cv = (CV*)av_shift(paramList);
4015 if (paramList == PL_beginav) {
4016 /* save PL_beginav for compiler */
4017 if (! PL_beginav_save)
4018 PL_beginav_save = newAV();
4019 av_push(PL_beginav_save, (SV*)cv);
4021 else if (paramList == PL_checkav) {
4022 /* save PL_checkav for compiler */
4023 if (! PL_checkav_save)
4024 PL_checkav_save = newAV();
4025 av_push(PL_checkav_save, (SV*)cv);
4030 #ifdef PERL_FLEXIBLE_EXCEPTIONS
4031 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vcall_list_body), cv);
4037 #ifndef PERL_FLEXIBLE_EXCEPTIONS
4041 (void)SvPV(atsv, len);
4043 PL_curcop = &PL_compiling;
4044 CopLINE_set(PL_curcop, oldline);
4045 if (paramList == PL_beginav)
4046 sv_catpv(atsv, "BEGIN failed--compilation aborted");
4048 Perl_sv_catpvf(aTHX_ atsv,
4049 "%s failed--call queue aborted",
4050 paramList == PL_checkav ? "CHECK"
4051 : paramList == PL_initav ? "INIT"
4053 while (PL_scopestack_ix > oldscope)
4056 Perl_croak(aTHX_ "%"SVf"", atsv);
4063 /* my_exit() was called */
4064 while (PL_scopestack_ix > oldscope)
4067 PL_curstash = PL_defstash;
4068 PL_curcop = &PL_compiling;
4069 CopLINE_set(PL_curcop, oldline);
4071 if (PL_statusvalue && !(PL_exit_flags & PERL_EXIT_EXPECTED)) {
4072 if (paramList == PL_beginav)
4073 Perl_croak(aTHX_ "BEGIN failed--compilation aborted");
4075 Perl_croak(aTHX_ "%s failed--call queue aborted",
4076 paramList == PL_checkav ? "CHECK"
4077 : paramList == PL_initav ? "INIT"
4084 PL_curcop = &PL_compiling;
4085 CopLINE_set(PL_curcop, oldline);
4088 PerlIO_printf(Perl_error_log, "panic: restartop\n");
4096 #ifdef PERL_FLEXIBLE_EXCEPTIONS
4098 S_vcall_list_body(pTHX_ va_list args)
4100 CV *cv = va_arg(args, CV*);
4101 return call_list_body(cv);
4106 S_call_list_body(pTHX_ CV *cv)
4108 PUSHMARK(PL_stack_sp);
4109 call_sv((SV*)cv, G_EVAL|G_DISCARD);
4114 Perl_my_exit(pTHX_ U32 status)
4116 DEBUG_S(PerlIO_printf(Perl_debug_log, "my_exit: thread %p, status %lu\n",
4117 thr, (unsigned long) status));
4126 STATUS_NATIVE_SET(status);
4133 Perl_my_failure_exit(pTHX)
4136 if (vaxc$errno & 1) {
4137 if (STATUS_NATIVE & 1) /* fortuitiously includes "-1" */
4138 STATUS_NATIVE_SET(44);
4141 if (!vaxc$errno && errno) /* unlikely */
4142 STATUS_NATIVE_SET(44);
4144 STATUS_NATIVE_SET(vaxc$errno);
4149 STATUS_POSIX_SET(errno);
4151 exitstatus = STATUS_POSIX >> 8;
4152 if (exitstatus & 255)
4153 STATUS_POSIX_SET(exitstatus);
4155 STATUS_POSIX_SET(255);
4162 S_my_exit_jump(pTHX)
4164 register PERL_CONTEXT *cx;
4169 SvREFCNT_dec(PL_e_script);
4170 PL_e_script = Nullsv;
4173 POPSTACK_TO(PL_mainstack);
4174 if (cxstack_ix >= 0) {
4177 POPBLOCK(cx,PL_curpm);
4185 read_e_script(pTHX_ int idx, SV *buf_sv, int maxlen)
4188 p = SvPVX(PL_e_script);
4189 nl = strchr(p, '\n');
4190 nl = (nl) ? nl+1 : SvEND(PL_e_script);
4192 filter_del(read_e_script);
4195 sv_catpvn(buf_sv, p, nl-p);
4196 sv_chop(PL_e_script, nl);