3 * Copyright (c) 1987-2002 Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "A ship then new they built for him/of mithril and of elven glass" --Bilbo
15 #define PERL_IN_PERL_C
17 #include "patchlevel.h" /* for local_patches */
19 /* XXX If this causes problems, set i_unistd=undef in the hint file. */
24 #if !defined(STANDARD_C) && !defined(HAS_GETENV_PROTOTYPE) && !defined(PERL_MICRO)
25 char *getenv (char *); /* Usually in <stdlib.h> */
28 static I32 read_e_script(pTHX_ int idx, SV *buf_sv, int maxlen);
36 #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
42 #if defined(USE_5005THREADS)
43 # define INIT_TLS_AND_INTERP \
45 if (!PL_curinterp) { \
46 PERL_SET_INTERP(my_perl); \
52 # if defined(USE_ITHREADS)
53 # define INIT_TLS_AND_INTERP \
55 if (!PL_curinterp) { \
56 PERL_SET_INTERP(my_perl); \
59 PERL_SET_THX(my_perl); \
63 PERL_SET_THX(my_perl); \
67 # define INIT_TLS_AND_INTERP \
69 if (!PL_curinterp) { \
70 PERL_SET_INTERP(my_perl); \
72 PERL_SET_THX(my_perl); \
77 #ifdef PERL_IMPLICIT_SYS
79 perl_alloc_using(struct IPerlMem* ipM, struct IPerlMem* ipMS,
80 struct IPerlMem* ipMP, struct IPerlEnv* ipE,
81 struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
82 struct IPerlDir* ipD, struct IPerlSock* ipS,
83 struct IPerlProc* ipP)
85 PerlInterpreter *my_perl;
86 /* New() needs interpreter, so call malloc() instead */
87 my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
89 Zero(my_perl, 1, PerlInterpreter);
105 =head1 Embedding Functions
107 =for apidoc perl_alloc
109 Allocates a new Perl interpreter. See L<perlembed>.
117 PerlInterpreter *my_perl;
119 /* New() needs interpreter, so call malloc() instead */
120 my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
123 Zero(my_perl, 1, PerlInterpreter);
126 #endif /* PERL_IMPLICIT_SYS */
129 =for apidoc perl_construct
131 Initializes a new Perl interpreter. See L<perlembed>.
137 perl_construct(pTHXx)
139 #ifdef USE_5005THREADS
141 struct perl_thread *thr = NULL;
142 #endif /* FAKE_THREADS */
143 #endif /* USE_5005THREADS */
147 PL_perl_destruct_level = 1;
149 if (PL_perl_destruct_level > 0)
153 /* Init the real globals (and main thread)? */
155 #ifdef USE_5005THREADS
156 MUTEX_INIT(&PL_sv_mutex);
158 * Safe to use basic SV functions from now on (though
159 * not things like mortals or tainting yet).
161 MUTEX_INIT(&PL_eval_mutex);
162 COND_INIT(&PL_eval_cond);
163 MUTEX_INIT(&PL_threads_mutex);
164 COND_INIT(&PL_nthreads_cond);
165 # ifdef EMULATE_ATOMIC_REFCOUNTS
166 MUTEX_INIT(&PL_svref_mutex);
167 # endif /* EMULATE_ATOMIC_REFCOUNTS */
169 MUTEX_INIT(&PL_cred_mutex);
170 MUTEX_INIT(&PL_sv_lock_mutex);
171 MUTEX_INIT(&PL_fdpid_mutex);
173 thr = init_main_thread();
174 #endif /* USE_5005THREADS */
176 #ifdef PERL_FLEXIBLE_EXCEPTIONS
177 PL_protect = MEMBER_TO_FPTR(Perl_default_protect); /* for exceptions */
180 PL_curcop = &PL_compiling; /* needed by ckWARN, right away */
182 PL_linestr = NEWSV(65,79);
183 sv_upgrade(PL_linestr,SVt_PVIV);
185 if (!SvREADONLY(&PL_sv_undef)) {
186 /* set read-only and try to insure than we wont see REFCNT==0
189 SvREADONLY_on(&PL_sv_undef);
190 SvREFCNT(&PL_sv_undef) = (~(U32)0)/2;
192 sv_setpv(&PL_sv_no,PL_No);
194 SvREADONLY_on(&PL_sv_no);
195 SvREFCNT(&PL_sv_no) = (~(U32)0)/2;
197 sv_setpv(&PL_sv_yes,PL_Yes);
199 SvREADONLY_on(&PL_sv_yes);
200 SvREFCNT(&PL_sv_yes) = (~(U32)0)/2;
203 PL_sighandlerp = Perl_sighandler;
204 PL_pidstatus = newHV();
207 PL_rs = newSVpvn("\n", 1);
212 PL_lex_state = LEX_NOTPARSING;
218 SET_NUMERIC_STANDARD();
222 PL_patchlevel = NEWSV(0,4);
223 (void)SvUPGRADE(PL_patchlevel, SVt_PVNV);
224 if (PERL_REVISION > 127 || PERL_VERSION > 127 || PERL_SUBVERSION > 127)
225 SvGROW(PL_patchlevel, UTF8_MAXLEN*3+1);
226 s = (U8*)SvPVX(PL_patchlevel);
227 /* Build version strings using "native" characters */
228 s = uvchr_to_utf8(s, (UV)PERL_REVISION);
229 s = uvchr_to_utf8(s, (UV)PERL_VERSION);
230 s = uvchr_to_utf8(s, (UV)PERL_SUBVERSION);
232 SvCUR_set(PL_patchlevel, s - (U8*)SvPVX(PL_patchlevel));
233 SvPOK_on(PL_patchlevel);
234 SvNVX(PL_patchlevel) = (NV)PERL_REVISION
235 + ((NV)PERL_VERSION / (NV)1000)
236 #if defined(PERL_SUBVERSION) && PERL_SUBVERSION > 0
237 + ((NV)PERL_SUBVERSION / (NV)1000000)
240 SvNOK_on(PL_patchlevel); /* dual valued */
241 SvUTF8_on(PL_patchlevel);
242 SvREADONLY_on(PL_patchlevel);
245 #if defined(LOCAL_PATCH_COUNT)
246 PL_localpatches = local_patches; /* For possible -v */
249 #ifdef HAVE_INTERP_INTERN
253 PerlIO_init(aTHX); /* Hook to IO system */
255 PL_fdpid = newAV(); /* for remembering popen pids by fd */
256 PL_modglobal = newHV(); /* pointers to per-interpreter module globals */
257 PL_errors = newSVpvn("",0);
259 PL_regex_padav = newAV();
260 av_push(PL_regex_padav,(SV*)newAV()); /* First entry is an array of empty elements */
261 PL_regex_pad = AvARRAY(PL_regex_padav);
263 #ifdef USE_REENTRANT_API
264 New(31337, PL_reentrant_buffer,1, REBUF);
265 New(31337, PL_reentrant_buffer->tmbuff,1, struct tm);
268 /* Note that strtab is a rather special HV. Assumptions are made
269 about not iterating on it, and not adding tie magic to it.
270 It is properly deallocated in perl_destruct() */
273 #ifdef USE_5005THREADS
274 MUTEX_INIT(&PL_strtab_mutex);
276 HvSHAREKEYS_off(PL_strtab); /* mandatory */
277 hv_ksplit(PL_strtab, 512);
279 #if defined(__DYNAMIC__) && (defined(NeXT) || defined(__NeXT__))
280 _dyld_lookup_and_bind
281 ("__environ", (unsigned long *) &environ_pointer, NULL);
284 #ifdef USE_ENVIRON_ARRAY
285 PL_origenviron = environ;
292 =for apidoc perl_destruct
294 Shuts down a Perl interpreter. See L<perlembed>.
302 volatile int destruct_level; /* 0=none, 1=full, 2=full with checks */
304 #ifdef USE_5005THREADS
307 #endif /* USE_5005THREADS */
309 /* wait for all pseudo-forked children to finish */
310 PERL_WAIT_FOR_CHILDREN;
312 #ifdef USE_5005THREADS
314 /* Pass 1 on any remaining threads: detach joinables, join zombies */
316 MUTEX_LOCK(&PL_threads_mutex);
317 DEBUG_S(PerlIO_printf(Perl_debug_log,
318 "perl_destruct: waiting for %d threads...\n",
320 for (t = thr->next; t != thr; t = t->next) {
321 MUTEX_LOCK(&t->mutex);
322 switch (ThrSTATE(t)) {
325 DEBUG_S(PerlIO_printf(Perl_debug_log,
326 "perl_destruct: joining zombie %p\n", t));
327 ThrSETSTATE(t, THRf_DEAD);
328 MUTEX_UNLOCK(&t->mutex);
331 * The SvREFCNT_dec below may take a long time (e.g. av
332 * may contain an object scalar whose destructor gets
333 * called) so we have to unlock threads_mutex and start
336 MUTEX_UNLOCK(&PL_threads_mutex);
338 SvREFCNT_dec((SV*)av);
339 DEBUG_S(PerlIO_printf(Perl_debug_log,
340 "perl_destruct: joined zombie %p OK\n", t));
342 case THRf_R_JOINABLE:
343 DEBUG_S(PerlIO_printf(Perl_debug_log,
344 "perl_destruct: detaching thread %p\n", t));
345 ThrSETSTATE(t, THRf_R_DETACHED);
347 * We unlock threads_mutex and t->mutex in the opposite order
348 * from which we locked them just so that DETACH won't
349 * deadlock if it panics. It's only a breach of good style
350 * not a bug since they are unlocks not locks.
352 MUTEX_UNLOCK(&PL_threads_mutex);
354 MUTEX_UNLOCK(&t->mutex);
357 DEBUG_S(PerlIO_printf(Perl_debug_log,
358 "perl_destruct: ignoring %p (state %u)\n",
360 MUTEX_UNLOCK(&t->mutex);
361 /* fall through and out */
364 /* We leave the above "Pass 1" loop with threads_mutex still locked */
366 /* Pass 2 on remaining threads: wait for the thread count to drop to one */
367 while (PL_nthreads > 1)
369 DEBUG_S(PerlIO_printf(Perl_debug_log,
370 "perl_destruct: final wait for %d threads\n",
372 COND_WAIT(&PL_nthreads_cond, &PL_threads_mutex);
374 /* At this point, we're the last thread */
375 MUTEX_UNLOCK(&PL_threads_mutex);
376 DEBUG_S(PerlIO_printf(Perl_debug_log, "perl_destruct: armageddon has arrived\n"));
377 MUTEX_DESTROY(&PL_threads_mutex);
378 COND_DESTROY(&PL_nthreads_cond);
380 #endif /* !defined(FAKE_THREADS) */
381 #endif /* USE_5005THREADS */
383 destruct_level = PL_perl_destruct_level;
387 if ((s = PerlEnv_getenv("PERL_DESTRUCT_LEVEL"))) {
389 if (destruct_level < i)
396 if(PL_exit_flags & PERL_EXIT_DESTRUCT_END) {
401 if (PL_endav && !PL_minus_c)
402 call_list(PL_scopestack_ix, PL_endav);
408 /* We must account for everything. */
410 /* Destroy the main CV and syntax tree */
412 PL_curpad = AvARRAY(PL_comppad);
413 op_free(PL_main_root);
414 PL_main_root = Nullop;
416 PL_curcop = &PL_compiling;
417 PL_main_start = Nullop;
418 SvREFCNT_dec(PL_main_cv);
422 /* Tell PerlIO we are about to tear things apart in case
423 we have layers which are using resources that should
427 PerlIO_destruct(aTHX);
429 if (PL_sv_objcount) {
431 * Try to destruct global references. We do this first so that the
432 * destructors and destructees still exist. Some sv's might remain.
433 * Non-referenced objects are on their own.
438 /* unhook hooks which will soon be, or use, destroyed data */
439 SvREFCNT_dec(PL_warnhook);
440 PL_warnhook = Nullsv;
441 SvREFCNT_dec(PL_diehook);
444 /* call exit list functions */
445 while (PL_exitlistlen-- > 0)
446 PL_exitlist[PL_exitlistlen].fn(aTHX_ PL_exitlist[PL_exitlistlen].ptr);
448 Safefree(PL_exitlist);
450 if (destruct_level == 0){
452 DEBUG_P(debprofdump());
454 #if defined(PERLIO_LAYERS)
455 /* No more IO - including error messages ! */
456 PerlIO_cleanup(aTHX);
459 /* The exit() function will do everything that needs doing. */
460 return STATUS_NATIVE_EXPORT;;
463 /* jettison our possibly duplicated environment */
464 /* if PERL_USE_SAFE_PUTENV is defined environ will not have been copied
465 * so we certainly shouldn't free it here
467 #if defined(USE_ENVIRON_ARRAY) && !defined(PERL_USE_SAFE_PUTENV)
468 if (environ != PL_origenviron) {
471 for (i = 0; environ[i]; i++)
472 safesysfree(environ[i]);
474 /* Must use safesysfree() when working with environ. */
475 safesysfree(environ);
477 environ = PL_origenviron;
482 /* the syntax tree is shared between clones
483 * so op_free(PL_main_root) only ReREFCNT_dec's
484 * REGEXPs in the parent interpreter
485 * we need to manually ReREFCNT_dec for the clones
488 I32 i = AvFILLp(PL_regex_padav) + 1;
489 SV **ary = AvARRAY(PL_regex_padav);
493 REGEXP *re = INT2PTR(REGEXP *,SvIVX(resv));
495 if (SvFLAGS(resv) & SVf_BREAK) {
496 /* this is PL_reg_curpm, already freed
497 * flag is set in regexec.c:S_regtry
499 SvFLAGS(resv) &= ~SVf_BREAK;
501 else if(SvREPADTMP(resv)) {
502 SvREPADTMP_off(resv);
509 SvREFCNT_dec(PL_regex_padav);
510 PL_regex_padav = Nullav;
514 /* loosen bonds of global variables */
517 (void)PerlIO_close(PL_rsfp);
521 /* Filters for program text */
522 SvREFCNT_dec(PL_rsfp_filters);
523 PL_rsfp_filters = Nullav;
526 PL_preprocess = FALSE;
532 PL_doswitches = FALSE;
533 PL_dowarn = G_WARN_OFF;
534 PL_doextract = FALSE;
535 PL_sawampersand = FALSE; /* must save all match strings */
538 Safefree(PL_inplace);
540 SvREFCNT_dec(PL_patchlevel);
543 SvREFCNT_dec(PL_e_script);
544 PL_e_script = Nullsv;
547 while (--PL_origargc >= 0) {
548 Safefree(PL_origargv[PL_origargc]);
550 Safefree(PL_origargv);
552 /* magical thingies */
554 SvREFCNT_dec(PL_ofs_sv); /* $, */
557 SvREFCNT_dec(PL_ors_sv); /* $\ */
560 SvREFCNT_dec(PL_rs); /* $/ */
563 PL_multiline = 0; /* $* */
564 Safefree(PL_osname); /* $^O */
567 SvREFCNT_dec(PL_statname);
568 PL_statname = Nullsv;
571 /* defgv, aka *_ should be taken care of elsewhere */
573 /* clean up after study() */
574 SvREFCNT_dec(PL_lastscream);
575 PL_lastscream = Nullsv;
576 Safefree(PL_screamfirst);
578 Safefree(PL_screamnext);
582 Safefree(PL_efloatbuf);
583 PL_efloatbuf = Nullch;
586 /* startup and shutdown function lists */
587 SvREFCNT_dec(PL_beginav);
588 SvREFCNT_dec(PL_beginav_save);
589 SvREFCNT_dec(PL_endav);
590 SvREFCNT_dec(PL_checkav);
591 SvREFCNT_dec(PL_initav);
593 PL_beginav_save = Nullav;
598 /* shortcuts just get cleared */
604 PL_argvoutgv = Nullgv;
606 PL_stderrgv = Nullgv;
607 PL_last_in_gv = Nullgv;
609 PL_debstash = Nullhv;
611 /* reset so print() ends up where we expect */
614 SvREFCNT_dec(PL_argvout_stack);
615 PL_argvout_stack = Nullav;
617 SvREFCNT_dec(PL_modglobal);
618 PL_modglobal = Nullhv;
619 SvREFCNT_dec(PL_preambleav);
620 PL_preambleav = Nullav;
621 SvREFCNT_dec(PL_subname);
623 SvREFCNT_dec(PL_linestr);
625 SvREFCNT_dec(PL_pidstatus);
626 PL_pidstatus = Nullhv;
627 SvREFCNT_dec(PL_toptarget);
628 PL_toptarget = Nullsv;
629 SvREFCNT_dec(PL_bodytarget);
630 PL_bodytarget = Nullsv;
631 PL_formtarget = Nullsv;
633 /* free locale stuff */
634 #ifdef USE_LOCALE_COLLATE
635 Safefree(PL_collation_name);
636 PL_collation_name = Nullch;
639 #ifdef USE_LOCALE_NUMERIC
640 Safefree(PL_numeric_name);
641 PL_numeric_name = Nullch;
642 SvREFCNT_dec(PL_numeric_radix_sv);
645 /* clear utf8 character classes */
646 SvREFCNT_dec(PL_utf8_alnum);
647 SvREFCNT_dec(PL_utf8_alnumc);
648 SvREFCNT_dec(PL_utf8_ascii);
649 SvREFCNT_dec(PL_utf8_alpha);
650 SvREFCNT_dec(PL_utf8_space);
651 SvREFCNT_dec(PL_utf8_cntrl);
652 SvREFCNT_dec(PL_utf8_graph);
653 SvREFCNT_dec(PL_utf8_digit);
654 SvREFCNT_dec(PL_utf8_upper);
655 SvREFCNT_dec(PL_utf8_lower);
656 SvREFCNT_dec(PL_utf8_print);
657 SvREFCNT_dec(PL_utf8_punct);
658 SvREFCNT_dec(PL_utf8_xdigit);
659 SvREFCNT_dec(PL_utf8_mark);
660 SvREFCNT_dec(PL_utf8_toupper);
661 SvREFCNT_dec(PL_utf8_totitle);
662 SvREFCNT_dec(PL_utf8_tolower);
663 SvREFCNT_dec(PL_utf8_tofold);
664 PL_utf8_alnum = Nullsv;
665 PL_utf8_alnumc = Nullsv;
666 PL_utf8_ascii = Nullsv;
667 PL_utf8_alpha = Nullsv;
668 PL_utf8_space = Nullsv;
669 PL_utf8_cntrl = Nullsv;
670 PL_utf8_graph = Nullsv;
671 PL_utf8_digit = Nullsv;
672 PL_utf8_upper = Nullsv;
673 PL_utf8_lower = Nullsv;
674 PL_utf8_print = Nullsv;
675 PL_utf8_punct = Nullsv;
676 PL_utf8_xdigit = Nullsv;
677 PL_utf8_mark = Nullsv;
678 PL_utf8_toupper = Nullsv;
679 PL_utf8_totitle = Nullsv;
680 PL_utf8_tolower = Nullsv;
681 PL_utf8_tofold = Nullsv;
683 if (!specialWARN(PL_compiling.cop_warnings))
684 SvREFCNT_dec(PL_compiling.cop_warnings);
685 PL_compiling.cop_warnings = Nullsv;
686 if (!specialCopIO(PL_compiling.cop_io))
687 SvREFCNT_dec(PL_compiling.cop_io);
688 PL_compiling.cop_io = Nullsv;
689 CopFILE_free(&PL_compiling);
690 CopSTASH_free(&PL_compiling);
692 /* Prepare to destruct main symbol table. */
697 SvREFCNT_dec(PL_curstname);
698 PL_curstname = Nullsv;
700 /* clear queued errors */
701 SvREFCNT_dec(PL_errors);
705 if (destruct_level >= 2 && ckWARN_d(WARN_INTERNAL)) {
706 if (PL_scopestack_ix != 0)
707 Perl_warner(aTHX_ WARN_INTERNAL,
708 "Unbalanced scopes: %ld more ENTERs than LEAVEs\n",
709 (long)PL_scopestack_ix);
710 if (PL_savestack_ix != 0)
711 Perl_warner(aTHX_ WARN_INTERNAL,
712 "Unbalanced saves: %ld more saves than restores\n",
713 (long)PL_savestack_ix);
714 if (PL_tmps_floor != -1)
715 Perl_warner(aTHX_ WARN_INTERNAL,"Unbalanced tmps: %ld more allocs than frees\n",
716 (long)PL_tmps_floor + 1);
717 if (cxstack_ix != -1)
718 Perl_warner(aTHX_ WARN_INTERNAL,"Unbalanced context: %ld more PUSHes than POPs\n",
719 (long)cxstack_ix + 1);
722 /* Now absolutely destruct everything, somehow or other, loops or no. */
723 SvFLAGS(PL_fdpid) |= SVTYPEMASK; /* don't clean out pid table now */
724 SvFLAGS(PL_strtab) |= SVTYPEMASK; /* don't clean out strtab now */
726 /* the 2 is for PL_fdpid and PL_strtab */
727 while (PL_sv_count > 2 && sv_clean_all())
730 SvFLAGS(PL_fdpid) &= ~SVTYPEMASK;
731 SvFLAGS(PL_fdpid) |= SVt_PVAV;
732 SvFLAGS(PL_strtab) &= ~SVTYPEMASK;
733 SvFLAGS(PL_strtab) |= SVt_PVHV;
735 AvREAL_off(PL_fdpid); /* no surviving entries */
736 SvREFCNT_dec(PL_fdpid); /* needed in io_close() */
739 #ifdef HAVE_INTERP_INTERN
743 /* Destruct the global string table. */
745 /* Yell and reset the HeVAL() slots that are still holding refcounts,
746 * so that sv_free() won't fail on them.
754 max = HvMAX(PL_strtab);
755 array = HvARRAY(PL_strtab);
758 if (hent && ckWARN_d(WARN_INTERNAL)) {
759 Perl_warner(aTHX_ WARN_INTERNAL,
760 "Unbalanced string table refcount: (%d) for \"%s\"",
761 HeVAL(hent) - Nullsv, HeKEY(hent));
762 HeVAL(hent) = Nullsv;
772 SvREFCNT_dec(PL_strtab);
775 /* free the pointer table used for cloning */
776 ptr_table_free(PL_ptr_table);
779 /* free special SVs */
781 SvREFCNT(&PL_sv_yes) = 0;
782 sv_clear(&PL_sv_yes);
783 SvANY(&PL_sv_yes) = NULL;
784 SvFLAGS(&PL_sv_yes) = 0;
786 SvREFCNT(&PL_sv_no) = 0;
788 SvANY(&PL_sv_no) = NULL;
789 SvFLAGS(&PL_sv_no) = 0;
791 SvREFCNT(&PL_sv_undef) = 0;
792 SvREADONLY_off(&PL_sv_undef);
794 if (PL_sv_count != 0 && ckWARN_d(WARN_INTERNAL))
795 Perl_warner(aTHX_ WARN_INTERNAL,"Scalars leaked: %ld\n", (long)PL_sv_count);
797 #if defined(PERLIO_LAYERS)
798 /* No more IO - including error messages ! */
799 PerlIO_cleanup(aTHX);
802 Safefree(PL_origfilename);
803 Safefree(PL_reg_start_tmp);
805 Safefree(PL_reg_curpm);
806 Safefree(PL_reg_poscache);
807 Safefree(HeKEY_hek(&PL_hv_fetch_ent_mh));
808 Safefree(PL_op_mask);
809 Safefree(PL_psig_ptr);
810 Safefree(PL_psig_name);
811 Safefree(PL_bitcount);
812 Safefree(PL_psig_pend);
814 PL_hints = 0; /* Reset hints. Should hints be per-interpreter ? */
816 DEBUG_P(debprofdump());
817 #ifdef USE_5005THREADS
818 MUTEX_DESTROY(&PL_strtab_mutex);
819 MUTEX_DESTROY(&PL_sv_mutex);
820 MUTEX_DESTROY(&PL_eval_mutex);
821 MUTEX_DESTROY(&PL_cred_mutex);
822 MUTEX_DESTROY(&PL_fdpid_mutex);
823 COND_DESTROY(&PL_eval_cond);
824 #ifdef EMULATE_ATOMIC_REFCOUNTS
825 MUTEX_DESTROY(&PL_svref_mutex);
826 #endif /* EMULATE_ATOMIC_REFCOUNTS */
828 /* As the penultimate thing, free the non-arena SV for thrsv */
829 Safefree(SvPVX(PL_thrsv));
830 Safefree(SvANY(PL_thrsv));
833 #endif /* USE_5005THREADS */
835 #ifdef USE_REENTRANT_API
836 Safefree(PL_reentrant_buffer->tmbuff);
837 Safefree(PL_reentrant_buffer);
842 /* As the absolutely last thing, free the non-arena SV for mess() */
845 /* it could have accumulated taint magic */
846 if (SvTYPE(PL_mess_sv) >= SVt_PVMG) {
849 for (mg = SvMAGIC(PL_mess_sv); mg; mg = moremagic) {
850 moremagic = mg->mg_moremagic;
851 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global
853 Safefree(mg->mg_ptr);
857 /* we know that type >= SVt_PV */
858 (void)SvOOK_off(PL_mess_sv);
859 Safefree(SvPVX(PL_mess_sv));
860 Safefree(SvANY(PL_mess_sv));
861 Safefree(PL_mess_sv);
864 return STATUS_NATIVE_EXPORT;
868 =for apidoc perl_free
870 Releases a Perl interpreter. See L<perlembed>.
878 #if defined(WIN32) || defined(NETWARE)
879 # if defined(PERL_IMPLICIT_SYS)
881 void *host = nw_internal_host;
883 void *host = w32_internal_host;
887 nw5_delete_internal_host(host);
889 win32_delete_internal_host(host);
900 Perl_call_atexit(pTHX_ ATEXIT_t fn, void *ptr)
902 Renew(PL_exitlist, PL_exitlistlen+1, PerlExitListEntry);
903 PL_exitlist[PL_exitlistlen].fn = fn;
904 PL_exitlist[PL_exitlistlen].ptr = ptr;
909 =for apidoc perl_parse
911 Tells a Perl interpreter to parse a Perl script. See L<perlembed>.
917 perl_parse(pTHXx_ XSINIT_t xsinit, int argc, char **argv, char **env)
922 #ifdef USE_5005THREADS
926 #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
929 Perl_croak(aTHX_ "suidperl is no longer needed since the kernel can now execute\n\
930 setuid perl scripts securely.\n");
936 /* we copy rather than point to argv
937 * since perl_clone will copy and perl_destruct
938 * has no way of knowing if we've made a copy or
942 New(0, PL_origargv, i+1, char*);
943 PL_origargv[i] = '\0';
945 PL_origargv[i] = savepv(argv[i]);
953 /* Come here if running an undumped a.out. */
955 PL_origfilename = savepv(argv[0]);
956 PL_do_undump = FALSE;
957 cxstack_ix = -1; /* start label stack again */
959 init_postdump_symbols(argc,argv,env);
964 PL_curpad = AvARRAY(PL_comppad);
965 op_free(PL_main_root);
966 PL_main_root = Nullop;
968 PL_main_start = Nullop;
969 SvREFCNT_dec(PL_main_cv);
973 oldscope = PL_scopestack_ix;
974 PL_dowarn = G_WARN_OFF;
976 #ifdef PERL_FLEXIBLE_EXCEPTIONS
977 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vparse_body), env, xsinit);
983 #ifndef PERL_FLEXIBLE_EXCEPTIONS
984 parse_body(env,xsinit);
987 call_list(oldscope, PL_checkav);
994 /* my_exit() was called */
995 while (PL_scopestack_ix > oldscope)
998 PL_curstash = PL_defstash;
1000 call_list(oldscope, PL_checkav);
1001 ret = STATUS_NATIVE_EXPORT;
1004 PerlIO_printf(Perl_error_log, "panic: top_env\n");
1012 #ifdef PERL_FLEXIBLE_EXCEPTIONS
1014 S_vparse_body(pTHX_ va_list args)
1016 char **env = va_arg(args, char**);
1017 XSINIT_t xsinit = va_arg(args, XSINIT_t);
1019 return parse_body(env, xsinit);
1024 S_parse_body(pTHX_ char **env, XSINIT_t xsinit)
1026 int argc = PL_origargc;
1027 char **argv = PL_origargv;
1028 char *scriptname = NULL;
1030 VOL bool dosearch = FALSE;
1031 char *validarg = "";
1035 char *cddir = Nullch;
1037 sv_setpvn(PL_linestr,"",0);
1038 sv = newSVpvn("",0); /* first used for -I flags */
1042 for (argc--,argv++; argc > 0; argc--,argv++) {
1043 if (argv[0][0] != '-' || !argv[0][1])
1047 validarg = " PHOOEY ";
1056 win32_argv2utf8(argc-1, argv+1);
1059 #ifndef PERL_STRICT_CR
1083 if ((s = moreswitches(s)))
1088 if( !PL_tainting ) {
1089 PL_taint_warn = TRUE;
1096 PL_taint_warn = FALSE;
1101 #ifdef MACOS_TRADITIONAL
1102 /* ignore -e for Dev:Pseudo argument */
1103 if (argv[1] && !strcmp(argv[1], "Dev:Pseudo"))
1106 if (PL_euid != PL_uid || PL_egid != PL_gid)
1107 Perl_croak(aTHX_ "No -e allowed in setuid scripts");
1109 PL_e_script = newSVpvn("",0);
1110 filter_add(read_e_script, NULL);
1113 sv_catpv(PL_e_script, s);
1115 sv_catpv(PL_e_script, argv[1]);
1119 Perl_croak(aTHX_ "No code specified for -e");
1120 sv_catpv(PL_e_script, "\n");
1123 case 'I': /* -I handled both here and in moreswitches() */
1125 if (!*++s && (s=argv[1]) != Nullch) {
1130 STRLEN len = strlen(s);
1131 p = savepvn(s, len);
1132 incpush(p, TRUE, TRUE);
1133 sv_catpvn(sv, "-I", 2);
1134 sv_catpvn(sv, p, len);
1135 sv_catpvn(sv, " ", 1);
1139 Perl_croak(aTHX_ "No directory specified for -I");
1143 PL_preprocess = TRUE;
1153 PL_preambleav = newAV();
1154 av_push(PL_preambleav, newSVpv("use Config qw(myconfig config_vars)",0));
1156 PL_Sv = newSVpv("print myconfig();",0);
1158 sv_catpv(PL_Sv,"print \"\\nCharacteristics of this PERLSHR image: \\n\",");
1160 sv_catpv(PL_Sv,"print \"\\nCharacteristics of this binary (from libperl): \\n\",");
1162 sv_catpv(PL_Sv,"\" Compile-time options:");
1164 sv_catpv(PL_Sv," DEBUGGING");
1166 # ifdef MULTIPLICITY
1167 sv_catpv(PL_Sv," MULTIPLICITY");
1169 # ifdef USE_5005THREADS
1170 sv_catpv(PL_Sv," USE_5005THREADS");
1172 # ifdef USE_ITHREADS
1173 sv_catpv(PL_Sv," USE_ITHREADS");
1175 # ifdef USE_64_BIT_INT
1176 sv_catpv(PL_Sv," USE_64_BIT_INT");
1178 # ifdef USE_64_BIT_ALL
1179 sv_catpv(PL_Sv," USE_64_BIT_ALL");
1181 # ifdef USE_LONG_DOUBLE
1182 sv_catpv(PL_Sv," USE_LONG_DOUBLE");
1184 # ifdef USE_LARGE_FILES
1185 sv_catpv(PL_Sv," USE_LARGE_FILES");
1188 sv_catpv(PL_Sv," USE_SOCKS");
1190 # ifdef PERL_IMPLICIT_CONTEXT
1191 sv_catpv(PL_Sv," PERL_IMPLICIT_CONTEXT");
1193 # ifdef PERL_IMPLICIT_SYS
1194 sv_catpv(PL_Sv," PERL_IMPLICIT_SYS");
1196 sv_catpv(PL_Sv,"\\n\",");
1198 #if defined(LOCAL_PATCH_COUNT)
1199 if (LOCAL_PATCH_COUNT > 0) {
1201 sv_catpv(PL_Sv,"\" Locally applied patches:\\n\",");
1202 for (i = 1; i <= LOCAL_PATCH_COUNT; i++) {
1203 if (PL_localpatches[i])
1204 Perl_sv_catpvf(aTHX_ PL_Sv,"q\" \t%s\n\",",PL_localpatches[i]);
1208 Perl_sv_catpvf(aTHX_ PL_Sv,"\" Built under %s\\n\"",OSNAME);
1211 Perl_sv_catpvf(aTHX_ PL_Sv,",\" Compiled at %s %s\\n\"",__DATE__,__TIME__);
1213 Perl_sv_catpvf(aTHX_ PL_Sv,",\" Compiled on %s\\n\"",__DATE__);
1216 sv_catpv(PL_Sv, "; \
1218 @env = map { \"$_=\\\"$ENV{$_}\\\"\" } sort grep {/^PERL/} keys %ENV; ");
1221 push @env, \"CYGWIN=\\\"$ENV{CYGWIN}\\\"\";");
1224 print \" \\%ENV:\\n @env\\n\" if @env; \
1225 print \" \\@INC:\\n @INC\\n\";");
1228 PL_Sv = newSVpv("config_vars(qw(",0);
1229 sv_catpv(PL_Sv, ++s);
1230 sv_catpv(PL_Sv, "))");
1233 av_push(PL_preambleav, PL_Sv);
1234 scriptname = BIT_BUCKET; /* don't look for script or read stdin */
1237 PL_doextract = TRUE;
1245 if (!*++s || isSPACE(*s)) {
1249 /* catch use of gnu style long options */
1250 if (strEQ(s, "version")) {
1254 if (strEQ(s, "help")) {
1261 Perl_croak(aTHX_ "Unrecognized switch: -%s (-h will show valid options)",s);
1267 #ifndef SECURE_INTERNAL_GETENV
1270 (s = PerlEnv_getenv("PERL5OPT")))
1275 if (*s == '-' && *(s+1) == 'T') {
1277 PL_taint_warn = FALSE;
1280 char *popt_copy = Nullch;
1293 if (!strchr("DIMUdmtw", *s))
1294 Perl_croak(aTHX_ "Illegal switch in PERL5OPT: -%c", *s);
1298 popt_copy = SvPVX(sv_2mortal(newSVpv(popt,0)));
1299 s = popt_copy + (s - popt);
1300 d = popt_copy + (d - popt);
1307 if( !PL_tainting ) {
1308 PL_taint_warn = TRUE;
1318 if (PL_taint_warn && PL_dowarn != G_WARN_ALL_OFF) {
1319 PL_compiling.cop_warnings = newSVpvn(WARN_TAINTstring, WARNsize);
1323 scriptname = argv[0];
1326 scriptname = BIT_BUCKET; /* don't look for script or read stdin */
1328 else if (scriptname == Nullch) {
1330 if ( PerlLIO_isatty(PerlIO_fileno(PerlIO_stdin())) )
1338 open_script(scriptname,dosearch,sv,&fdscript);
1340 validate_suid(validarg, scriptname,fdscript);
1343 #if defined(SIGCHLD) || defined(SIGCLD)
1346 # define SIGCHLD SIGCLD
1348 Sighandler_t sigstate = rsignal_state(SIGCHLD);
1349 if (sigstate == SIG_IGN) {
1350 if (ckWARN(WARN_SIGNAL))
1351 Perl_warner(aTHX_ WARN_SIGNAL,
1352 "Can't ignore signal CHLD, forcing to default");
1353 (void)rsignal(SIGCHLD, (Sighandler_t)SIG_DFL);
1359 #ifdef MACOS_TRADITIONAL
1360 if (PL_doextract || gMacPerl_AlwaysExtract) {
1365 if (cddir && PerlDir_chdir(cddir) < 0)
1366 Perl_croak(aTHX_ "Can't chdir to %s",cddir);
1370 PL_main_cv = PL_compcv = (CV*)NEWSV(1104,0);
1371 sv_upgrade((SV *)PL_compcv, SVt_PVCV);
1372 CvUNIQUE_on(PL_compcv);
1374 PL_comppad = newAV();
1375 av_push(PL_comppad, Nullsv);
1376 PL_curpad = AvARRAY(PL_comppad);
1377 PL_comppad_name = newAV();
1378 PL_comppad_name_fill = 0;
1379 PL_min_intro_pending = 0;
1381 #ifdef USE_5005THREADS
1382 av_store(PL_comppad_name, 0, newSVpvn("@_", 2));
1383 PL_curpad[0] = (SV*)newAV();
1384 SvPADMY_on(PL_curpad[0]); /* XXX Needed? */
1385 CvOWNER(PL_compcv) = 0;
1386 New(666, CvMUTEXP(PL_compcv), 1, perl_mutex);
1387 MUTEX_INIT(CvMUTEXP(PL_compcv));
1388 #endif /* USE_5005THREADS */
1390 comppadlist = newAV();
1391 AvREAL_off(comppadlist);
1392 av_store(comppadlist, 0, (SV*)PL_comppad_name);
1393 av_store(comppadlist, 1, (SV*)PL_comppad);
1394 CvPADLIST(PL_compcv) = comppadlist;
1397 boot_core_UNIVERSAL();
1399 boot_core_xsutils();
1403 (*xsinit)(aTHX); /* in case linked C routines want magical variables */
1405 #if defined(VMS) || defined(WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(EPOC)
1411 # ifdef HAS_SOCKS5_INIT
1412 socks5_init(argv[0]);
1418 init_predump_symbols();
1419 /* init_postdump_symbols not currently designed to be called */
1420 /* more than once (ENV isn't cleared first, for example) */
1421 /* But running with -u leaves %ENV & @ARGV undefined! XXX */
1423 init_postdump_symbols(argc,argv,env);
1427 /* now parse the script */
1429 SETERRNO(0,SS$_NORMAL);
1431 #ifdef MACOS_TRADITIONAL
1432 if (gMacPerl_SyntaxError = (yyparse() || PL_error_count)) {
1434 Perl_croak(aTHX_ "%s had compilation errors.\n", MacPerl_MPWFileName(PL_origfilename));
1436 Perl_croak(aTHX_ "Execution of %s aborted due to compilation errors.\n",
1437 MacPerl_MPWFileName(PL_origfilename));
1441 if (yyparse() || PL_error_count) {
1443 Perl_croak(aTHX_ "%s had compilation errors.\n", PL_origfilename);
1445 Perl_croak(aTHX_ "Execution of %s aborted due to compilation errors.\n",
1450 CopLINE_set(PL_curcop, 0);
1451 PL_curstash = PL_defstash;
1452 PL_preprocess = FALSE;
1454 SvREFCNT_dec(PL_e_script);
1455 PL_e_script = Nullsv;
1459 Not sure that this is still the right place to do this now that we
1460 no longer use PL_nrs. HVDS 2001/09/09
1462 sv_setsv(get_sv("/", TRUE), PL_rs);
1468 SAVECOPFILE(PL_curcop);
1469 SAVECOPLINE(PL_curcop);
1470 gv_check(PL_defstash);
1477 if ((s=PerlEnv_getenv("PERL_DEBUG_MSTATS")) && atoi(s) >= 2)
1478 dump_mstats("after compilation:");
1487 =for apidoc perl_run
1489 Tells a Perl interpreter to run. See L<perlembed>.
1500 #ifdef USE_5005THREADS
1504 oldscope = PL_scopestack_ix;
1509 #ifdef PERL_FLEXIBLE_EXCEPTIONS
1511 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vrun_body), oldscope);
1517 cxstack_ix = -1; /* start context stack again */
1519 case 0: /* normal completion */
1520 #ifndef PERL_FLEXIBLE_EXCEPTIONS
1525 case 2: /* my_exit() */
1526 while (PL_scopestack_ix > oldscope)
1529 PL_curstash = PL_defstash;
1530 if (!(PL_exit_flags & PERL_EXIT_DESTRUCT_END) &&
1531 PL_endav && !PL_minus_c)
1532 call_list(oldscope, PL_endav);
1534 if (PerlEnv_getenv("PERL_DEBUG_MSTATS"))
1535 dump_mstats("after execution: ");
1537 ret = STATUS_NATIVE_EXPORT;
1541 POPSTACK_TO(PL_mainstack);
1544 PerlIO_printf(Perl_error_log, "panic: restartop\n");
1554 #ifdef PERL_FLEXIBLE_EXCEPTIONS
1556 S_vrun_body(pTHX_ va_list args)
1558 I32 oldscope = va_arg(args, I32);
1560 return run_body(oldscope);
1566 S_run_body(pTHX_ I32 oldscope)
1568 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s $` $& $' support.\n",
1569 PL_sawampersand ? "Enabling" : "Omitting"));
1571 if (!PL_restartop) {
1572 DEBUG_x(dump_all());
1573 DEBUG(PerlIO_printf(Perl_debug_log, "\nEXECUTING...\n\n"));
1574 DEBUG_S(PerlIO_printf(Perl_debug_log, "main thread is 0x%"UVxf"\n",
1578 #ifdef MACOS_TRADITIONAL
1579 PerlIO_printf(Perl_error_log, "# %s syntax OK\n", MacPerl_MPWFileName(PL_origfilename));
1581 PerlIO_printf(Perl_error_log, "%s syntax OK\n", PL_origfilename);
1585 if (PERLDB_SINGLE && PL_DBsingle)
1586 sv_setiv(PL_DBsingle, 1);
1588 call_list(oldscope, PL_initav);
1594 PL_op = PL_restartop;
1598 else if (PL_main_start) {
1599 CvDEPTH(PL_main_cv) = 1;
1600 PL_op = PL_main_start;
1610 =head1 SV Manipulation Functions
1612 =for apidoc p||get_sv
1614 Returns the SV of the specified Perl scalar. If C<create> is set and the
1615 Perl variable does not exist then it will be created. If C<create> is not
1616 set and the variable does not exist then NULL is returned.
1622 Perl_get_sv(pTHX_ const char *name, I32 create)
1625 #ifdef USE_5005THREADS
1626 if (name[1] == '\0' && !isALPHA(name[0])) {
1627 PADOFFSET tmp = find_threadsv(name);
1628 if (tmp != NOT_IN_PAD)
1629 return THREADSV(tmp);
1631 #endif /* USE_5005THREADS */
1632 gv = gv_fetchpv(name, create, SVt_PV);
1639 =head1 Array Manipulation Functions
1641 =for apidoc p||get_av
1643 Returns the AV of the specified Perl array. If C<create> is set and the
1644 Perl variable does not exist then it will be created. If C<create> is not
1645 set and the variable does not exist then NULL is returned.
1651 Perl_get_av(pTHX_ const char *name, I32 create)
1653 GV* gv = gv_fetchpv(name, create, SVt_PVAV);
1662 =head1 Hash Manipulation Functions
1664 =for apidoc p||get_hv
1666 Returns the HV of the specified Perl hash. If C<create> is set and the
1667 Perl variable does not exist then it will be created. If C<create> is not
1668 set and the variable does not exist then NULL is returned.
1674 Perl_get_hv(pTHX_ const char *name, I32 create)
1676 GV* gv = gv_fetchpv(name, create, SVt_PVHV);
1685 =head1 CV Manipulation Functions
1687 =for apidoc p||get_cv
1689 Returns the CV of the specified Perl subroutine. If C<create> is set and
1690 the Perl subroutine does not exist then it will be declared (which has the
1691 same effect as saying C<sub name;>). If C<create> is not set and the
1692 subroutine does not exist then NULL is returned.
1698 Perl_get_cv(pTHX_ const char *name, I32 create)
1700 GV* gv = gv_fetchpv(name, create, SVt_PVCV);
1701 /* XXX unsafe for threads if eval_owner isn't held */
1702 /* XXX this is probably not what they think they're getting.
1703 * It has the same effect as "sub name;", i.e. just a forward
1705 if (create && !GvCVu(gv))
1706 return newSUB(start_subparse(FALSE, 0),
1707 newSVOP(OP_CONST, 0, newSVpv(name,0)),
1715 /* Be sure to refetch the stack pointer after calling these routines. */
1719 =head1 Callback Functions
1721 =for apidoc p||call_argv
1723 Performs a callback to the specified Perl sub. See L<perlcall>.
1729 Perl_call_argv(pTHX_ const char *sub_name, I32 flags, register char **argv)
1731 /* See G_* flags in cop.h */
1732 /* null terminated arg list */
1739 XPUSHs(sv_2mortal(newSVpv(*argv,0)));
1744 return call_pv(sub_name, flags);
1748 =for apidoc p||call_pv
1750 Performs a callback to the specified Perl sub. See L<perlcall>.
1756 Perl_call_pv(pTHX_ const char *sub_name, I32 flags)
1757 /* name of the subroutine */
1758 /* See G_* flags in cop.h */
1760 return call_sv((SV*)get_cv(sub_name, TRUE), flags);
1764 =for apidoc p||call_method
1766 Performs a callback to the specified Perl method. The blessed object must
1767 be on the stack. See L<perlcall>.
1773 Perl_call_method(pTHX_ const char *methname, I32 flags)
1774 /* name of the subroutine */
1775 /* See G_* flags in cop.h */
1777 return call_sv(sv_2mortal(newSVpv(methname,0)), flags | G_METHOD);
1780 /* May be called with any of a CV, a GV, or an SV containing the name. */
1782 =for apidoc p||call_sv
1784 Performs a callback to the Perl sub whose name is in the SV. See
1791 Perl_call_sv(pTHX_ SV *sv, I32 flags)
1792 /* See G_* flags in cop.h */
1795 LOGOP myop; /* fake syntax tree node */
1798 volatile I32 retval = 0;
1800 bool oldcatch = CATCH_GET;
1805 if (flags & G_DISCARD) {
1810 Zero(&myop, 1, LOGOP);
1811 myop.op_next = Nullop;
1812 if (!(flags & G_NOARGS))
1813 myop.op_flags |= OPf_STACKED;
1814 myop.op_flags |= ((flags & G_VOID) ? OPf_WANT_VOID :
1815 (flags & G_ARRAY) ? OPf_WANT_LIST :
1820 EXTEND(PL_stack_sp, 1);
1821 *++PL_stack_sp = sv;
1823 oldscope = PL_scopestack_ix;
1825 if (PERLDB_SUB && PL_curstash != PL_debstash
1826 /* Handle first BEGIN of -d. */
1827 && (PL_DBcv || (PL_DBcv = GvCV(PL_DBsub)))
1828 /* Try harder, since this may have been a sighandler, thus
1829 * curstash may be meaningless. */
1830 && (SvTYPE(sv) != SVt_PVCV || CvSTASH((CV*)sv) != PL_debstash)
1831 && !(flags & G_NODEBUG))
1832 PL_op->op_private |= OPpENTERSUB_DB;
1834 if (flags & G_METHOD) {
1835 Zero(&method_op, 1, UNOP);
1836 method_op.op_next = PL_op;
1837 method_op.op_ppaddr = PL_ppaddr[OP_METHOD];
1838 myop.op_ppaddr = PL_ppaddr[OP_ENTERSUB];
1839 PL_op = (OP*)&method_op;
1842 if (!(flags & G_EVAL)) {
1844 call_body((OP*)&myop, FALSE);
1845 retval = PL_stack_sp - (PL_stack_base + oldmark);
1846 CATCH_SET(oldcatch);
1849 myop.op_other = (OP*)&myop;
1851 /* we're trying to emulate pp_entertry() here */
1853 register PERL_CONTEXT *cx;
1854 I32 gimme = GIMME_V;
1859 push_return(Nullop);
1860 PUSHBLOCK(cx, (CXt_EVAL|CXp_TRYBLOCK), PL_stack_sp);
1862 PL_eval_root = PL_op; /* Only needed so that goto works right. */
1864 PL_in_eval = EVAL_INEVAL;
1865 if (flags & G_KEEPERR)
1866 PL_in_eval |= EVAL_KEEPERR;
1872 #ifdef PERL_FLEXIBLE_EXCEPTIONS
1874 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vcall_body),
1881 #ifndef PERL_FLEXIBLE_EXCEPTIONS
1883 call_body((OP*)&myop, FALSE);
1885 retval = PL_stack_sp - (PL_stack_base + oldmark);
1886 if (!(flags & G_KEEPERR))
1893 /* my_exit() was called */
1894 PL_curstash = PL_defstash;
1897 if (PL_statusvalue && !(PL_exit_flags & PERL_EXIT_EXPECTED))
1898 Perl_croak(aTHX_ "Callback called exit");
1903 PL_op = PL_restartop;
1907 PL_stack_sp = PL_stack_base + oldmark;
1908 if (flags & G_ARRAY)
1912 *++PL_stack_sp = &PL_sv_undef;
1917 if (PL_scopestack_ix > oldscope) {
1921 register PERL_CONTEXT *cx;
1933 if (flags & G_DISCARD) {
1934 PL_stack_sp = PL_stack_base + oldmark;
1943 #ifdef PERL_FLEXIBLE_EXCEPTIONS
1945 S_vcall_body(pTHX_ va_list args)
1947 OP *myop = va_arg(args, OP*);
1948 int is_eval = va_arg(args, int);
1950 call_body(myop, is_eval);
1956 S_call_body(pTHX_ OP *myop, int is_eval)
1958 if (PL_op == myop) {
1960 PL_op = Perl_pp_entereval(aTHX); /* this doesn't do a POPMARK */
1962 PL_op = Perl_pp_entersub(aTHX); /* this does */
1968 /* Eval a string. The G_EVAL flag is always assumed. */
1971 =for apidoc p||eval_sv
1973 Tells Perl to C<eval> the string in the SV.
1979 Perl_eval_sv(pTHX_ SV *sv, I32 flags)
1981 /* See G_* flags in cop.h */
1984 UNOP myop; /* fake syntax tree node */
1985 volatile I32 oldmark = SP - PL_stack_base;
1986 volatile I32 retval = 0;
1992 if (flags & G_DISCARD) {
1999 Zero(PL_op, 1, UNOP);
2000 EXTEND(PL_stack_sp, 1);
2001 *++PL_stack_sp = sv;
2002 oldscope = PL_scopestack_ix;
2004 if (!(flags & G_NOARGS))
2005 myop.op_flags = OPf_STACKED;
2006 myop.op_next = Nullop;
2007 myop.op_type = OP_ENTEREVAL;
2008 myop.op_flags |= ((flags & G_VOID) ? OPf_WANT_VOID :
2009 (flags & G_ARRAY) ? OPf_WANT_LIST :
2011 if (flags & G_KEEPERR)
2012 myop.op_flags |= OPf_SPECIAL;
2014 #ifdef PERL_FLEXIBLE_EXCEPTIONS
2016 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vcall_body),
2023 #ifndef PERL_FLEXIBLE_EXCEPTIONS
2025 call_body((OP*)&myop,TRUE);
2027 retval = PL_stack_sp - (PL_stack_base + oldmark);
2028 if (!(flags & G_KEEPERR))
2035 /* my_exit() was called */
2036 PL_curstash = PL_defstash;
2039 if (PL_statusvalue && !(PL_exit_flags & PERL_EXIT_EXPECTED))
2040 Perl_croak(aTHX_ "Callback called exit");
2045 PL_op = PL_restartop;
2049 PL_stack_sp = PL_stack_base + oldmark;
2050 if (flags & G_ARRAY)
2054 *++PL_stack_sp = &PL_sv_undef;
2060 if (flags & G_DISCARD) {
2061 PL_stack_sp = PL_stack_base + oldmark;
2071 =for apidoc p||eval_pv
2073 Tells Perl to C<eval> the given string and return an SV* result.
2079 Perl_eval_pv(pTHX_ const char *p, I32 croak_on_error)
2082 SV* sv = newSVpv(p, 0);
2084 eval_sv(sv, G_SCALAR);
2091 if (croak_on_error && SvTRUE(ERRSV)) {
2093 Perl_croak(aTHX_ SvPVx(ERRSV, n_a));
2099 /* Require a module. */
2102 =head1 Embedding Functions
2104 =for apidoc p||require_pv
2106 Tells Perl to C<require> the file named by the string argument. It is
2107 analogous to the Perl code C<eval "require '$file'">. It's even
2108 implemented that way; consider using Perl_load_module instead.
2113 Perl_require_pv(pTHX_ const char *pv)
2117 PUSHSTACKi(PERLSI_REQUIRE);
2119 sv = sv_newmortal();
2120 sv_setpv(sv, "require '");
2123 eval_sv(sv, G_DISCARD);
2129 Perl_magicname(pTHX_ char *sym, char *name, I32 namlen)
2133 if ((gv = gv_fetchpv(sym,TRUE, SVt_PV)))
2134 sv_magic(GvSV(gv), (SV*)gv, PERL_MAGIC_sv, name, namlen);
2138 S_usage(pTHX_ char *name) /* XXX move this out into a module ? */
2140 /* This message really ought to be max 23 lines.
2141 * Removed -h because the user already knows that option. Others? */
2143 static char *usage_msg[] = {
2144 "-0[octal] specify record separator (\\0, if no argument)",
2145 "-a autosplit mode with -n or -p (splits $_ into @F)",
2146 "-C enable native wide character system interfaces",
2147 "-c check syntax only (runs BEGIN and CHECK blocks)",
2148 "-d[:debugger] run program under debugger",
2149 "-D[number/list] set debugging flags (argument is a bit mask or alphabets)",
2150 "-e 'command' one line of program (several -e's allowed, omit programfile)",
2151 "-F/pattern/ split() pattern for -a switch (//'s are optional)",
2152 "-i[extension] edit <> files in place (makes backup if extension supplied)",
2153 "-Idirectory specify @INC/#include directory (several -I's allowed)",
2154 "-l[octal] enable line ending processing, specifies line terminator",
2155 "-[mM][-]module execute `use/no module...' before executing program",
2156 "-n assume 'while (<>) { ... }' loop around program",
2157 "-p assume loop like -n but print line also, like sed",
2158 "-P run program through C preprocessor before compilation",
2159 "-s enable rudimentary parsing for switches after programfile",
2160 "-S look for programfile using PATH environment variable",
2161 "-T enable tainting checks",
2162 "-u dump core after parsing program",
2163 "-U allow unsafe operations",
2164 "-v print version, subversion (includes VERY IMPORTANT perl info)",
2165 "-V[:variable] print configuration summary (or a single Config.pm variable)",
2166 "-w enable many useful warnings (RECOMMENDED)",
2167 "-W enable all warnings",
2168 "-X disable all warnings",
2169 "-x[directory] strip off text before #!perl line and perhaps cd to directory",
2173 char **p = usage_msg;
2175 PerlIO_printf(PerlIO_stdout(),
2176 "\nUsage: %s [switches] [--] [programfile] [arguments]",
2179 PerlIO_printf(PerlIO_stdout(), "\n %s", *p++);
2182 /* This routine handles any switches that can be given during run */
2185 Perl_moreswitches(pTHX_ char *s)
2195 rschar = (U32)grok_oct(s, &numlen, &flags, NULL);
2196 SvREFCNT_dec(PL_rs);
2197 if (rschar & ~((U8)~0))
2198 PL_rs = &PL_sv_undef;
2199 else if (!rschar && numlen >= 2)
2200 PL_rs = newSVpvn("", 0);
2203 PL_rs = newSVpvn(&ch, 1);
2208 PL_widesyscalls = TRUE;
2214 while (*s && !isSPACE(*s)) ++s;
2216 PL_splitstr = savepv(PL_splitstr);
2229 /* The following permits -d:Mod to accepts arguments following an =
2230 in the fashion that -MSome::Mod does. */
2231 if (*s == ':' || *s == '=') {
2234 sv = newSVpv("use Devel::", 0);
2236 /* We now allow -d:Module=Foo,Bar */
2237 while(isALNUM(*s) || *s==':') ++s;
2239 sv_catpv(sv, start);
2241 sv_catpvn(sv, start, s-start);
2242 sv_catpv(sv, " split(/,/,q{");
2247 my_setenv("PERL5DB", SvPV(sv, PL_na));
2250 PL_perldb = PERLDB_ALL;
2258 if (isALPHA(s[1])) {
2259 /* if adding extra options, remember to update DEBUG_MASK */
2260 static char debopts[] = "psltocPmfrxuLHXDSTRJ";
2263 for (s++; *s && (d = strchr(debopts,*s)); s++)
2264 PL_debug |= 1 << (d - debopts);
2267 PL_debug = atoi(s+1);
2268 for (s++; isDIGIT(*s); s++) ;
2270 PL_debug |= DEBUG_TOP_FLAG;
2272 if (ckWARN_d(WARN_DEBUGGING))
2273 Perl_warner(aTHX_ WARN_DEBUGGING,
2274 "Recompile perl with -DDEBUGGING to use -D switch\n");
2275 for (s++; isALNUM(*s); s++) ;
2281 usage(PL_origargv[0]);
2285 Safefree(PL_inplace);
2286 PL_inplace = savepv(s+1);
2288 for (s = PL_inplace; *s && !isSPACE(*s); s++) ;
2291 if (*s == '-') /* Additional switches on #! line. */
2295 case 'I': /* -I handled both here and in parse_body() */
2298 while (*s && isSPACE(*s))
2303 /* ignore trailing spaces (possibly followed by other switches) */
2305 for (e = p; *e && !isSPACE(*e); e++) ;
2309 } while (*p && *p != '-');
2310 e = savepvn(s, e-s);
2311 incpush(e, TRUE, TRUE);
2318 Perl_croak(aTHX_ "No directory specified for -I");
2324 SvREFCNT_dec(PL_ors_sv);
2329 PL_ors_sv = newSVpvn("\n",1);
2330 numlen = 3 + (*s == '0');
2331 *SvPVX(PL_ors_sv) = (char)grok_oct(s, &numlen, &flags, NULL);
2335 if (RsPARA(PL_rs)) {
2336 PL_ors_sv = newSVpvn("\n\n",2);
2339 PL_ors_sv = newSVsv(PL_rs);
2344 forbid_setid("-M"); /* XXX ? */
2347 forbid_setid("-m"); /* XXX ? */
2352 /* -M-foo == 'no foo' */
2353 if (*s == '-') { use = "no "; ++s; }
2354 sv = newSVpv(use,0);
2356 /* We allow -M'Module qw(Foo Bar)' */
2357 while(isALNUM(*s) || *s==':') ++s;
2359 sv_catpv(sv, start);
2360 if (*(start-1) == 'm') {
2362 Perl_croak(aTHX_ "Can't use '%c' after -mname", *s);
2363 sv_catpv( sv, " ()");
2367 Perl_croak(aTHX_ "Module name required with -%c option",
2369 sv_catpvn(sv, start, s-start);
2370 sv_catpv(sv, " split(/,/,q{");
2376 PL_preambleav = newAV();
2377 av_push(PL_preambleav, sv);
2380 Perl_croak(aTHX_ "No space allowed after -%c", *(s-1));
2392 PL_doswitches = TRUE;
2397 Perl_croak(aTHX_ "Too late for \"-t\" option");
2402 Perl_croak(aTHX_ "Too late for \"-T\" option");
2406 #ifdef MACOS_TRADITIONAL
2407 Perl_croak(aTHX_ "Believe me, you don't want to use \"-u\" on a Macintosh");
2409 PL_do_undump = TRUE;
2418 PerlIO_printf(PerlIO_stdout(),
2419 Perl_form(aTHX_ "\nThis is perl, v%"VDf" built for %s",
2420 PL_patchlevel, ARCHNAME));
2422 /* Adjust verbose output as in the perl that ships with the DG/UX OS from EMC */
2423 PerlIO_printf(PerlIO_stdout(),
2424 Perl_form(aTHX_ "\nThis is perl, version %vd\n", PL_patchlevel));
2425 PerlIO_printf(PerlIO_stdout(),
2426 Perl_form(aTHX_ " built under %s at %s %s\n",
2427 OSNAME, __DATE__, __TIME__));
2428 PerlIO_printf(PerlIO_stdout(),
2429 Perl_form(aTHX_ " OS Specific Release: %s\n",
2433 #if defined(LOCAL_PATCH_COUNT)
2434 if (LOCAL_PATCH_COUNT > 0)
2435 PerlIO_printf(PerlIO_stdout(),
2436 "\n(with %d registered patch%s, "
2437 "see perl -V for more detail)",
2438 (int)LOCAL_PATCH_COUNT,
2439 (LOCAL_PATCH_COUNT!=1) ? "es" : "");
2442 PerlIO_printf(PerlIO_stdout(),
2443 "\n\nCopyright 1987-2002, Larry Wall\n");
2444 #ifdef MACOS_TRADITIONAL
2445 PerlIO_printf(PerlIO_stdout(),
2446 "\nMac OS port Copyright 1991-2002, Matthias Neeracher;\n"
2447 "maintained by Chris Nandor\n");
2450 PerlIO_printf(PerlIO_stdout(),
2451 "\nMS-DOS port Copyright (c) 1989, 1990, Diomidis Spinellis\n");
2454 PerlIO_printf(PerlIO_stdout(),
2455 "djgpp v2 port (jpl5003c) by Hirofumi Watanabe, 1996\n"
2456 "djgpp v2 port (perl5004+) by Laszlo Molnar, 1997-1999\n");
2459 PerlIO_printf(PerlIO_stdout(),
2460 "\n\nOS/2 port Copyright (c) 1990, 1991, Raymond Chen, Kai Uwe Rommel\n"
2461 "Version 5 port Copyright (c) 1994-2002, Andreas Kaiser, Ilya Zakharevich\n");
2464 PerlIO_printf(PerlIO_stdout(),
2465 "atariST series port, ++jrb bammi@cadence.com\n");
2468 PerlIO_printf(PerlIO_stdout(),
2469 "BeOS port Copyright Tom Spindler, 1997-1999\n");
2472 PerlIO_printf(PerlIO_stdout(),
2473 "MPE/iX port Copyright by Mark Klein and Mark Bixby, 1996-2002\n");
2476 PerlIO_printf(PerlIO_stdout(),
2477 "MVS (OS390) port by Mortice Kern Systems, 1997-1999\n");
2480 PerlIO_printf(PerlIO_stdout(),
2481 "Stratus VOS port by Paul_Green@stratus.com, 1997-2002\n");
2484 PerlIO_printf(PerlIO_stdout(),
2485 "VM/ESA port by Neale Ferguson, 1998-1999\n");
2488 PerlIO_printf(PerlIO_stdout(),
2489 "BS2000 (POSIX) port by Start Amadeus GmbH, 1998-1999\n");
2492 PerlIO_printf(PerlIO_stdout(),
2493 "MiNT port by Guido Flohr, 1997-1999\n");
2496 PerlIO_printf(PerlIO_stdout(),
2497 "EPOC port by Olaf Flebbe, 1999-2002\n");
2500 printf("WINCE port by Rainer Keuchel, 2001-2002\n");
2501 printf("Built on " __DATE__ " " __TIME__ "\n\n");
2504 #ifdef BINARY_BUILD_NOTICE
2505 BINARY_BUILD_NOTICE;
2507 PerlIO_printf(PerlIO_stdout(),
2509 Perl may be copied only under the terms of either the Artistic License or the\n\
2510 GNU General Public License, which may be found in the Perl 5 source kit.\n\n\
2511 Complete documentation for Perl, including FAQ lists, should be found on\n\
2512 this system using `man perl' or `perldoc perl'. If you have access to the\n\
2513 Internet, point your browser at http://www.perl.com/, the Perl Home Page.\n\n");
2516 if (! (PL_dowarn & G_WARN_ALL_MASK))
2517 PL_dowarn |= G_WARN_ON;
2521 PL_dowarn = G_WARN_ALL_ON|G_WARN_ON;
2522 if (!specialWARN(PL_compiling.cop_warnings))
2523 SvREFCNT_dec(PL_compiling.cop_warnings);
2524 PL_compiling.cop_warnings = pWARN_ALL ;
2528 PL_dowarn = G_WARN_ALL_OFF;
2529 if (!specialWARN(PL_compiling.cop_warnings))
2530 SvREFCNT_dec(PL_compiling.cop_warnings);
2531 PL_compiling.cop_warnings = pWARN_NONE ;
2536 if (s[1] == '-') /* Additional switches on #! line. */
2541 #if defined(WIN32) || !defined(PERL_STRICT_CR)
2547 #ifdef ALTERNATE_SHEBANG
2548 case 'S': /* OS/2 needs -S on "extproc" line. */
2556 Perl_croak(aTHX_ "Can't emulate -%.1s on #! line",s);
2561 /* compliments of Tom Christiansen */
2563 /* unexec() can be found in the Gnu emacs distribution */
2564 /* Known to work with -DUNEXEC and using unexelf.c from GNU emacs-20.2 */
2567 Perl_my_unexec(pTHX)
2575 prog = newSVpv(BIN_EXP, 0);
2576 sv_catpv(prog, "/perl");
2577 file = newSVpv(PL_origfilename, 0);
2578 sv_catpv(file, ".perldump");
2580 unexec(SvPVX(file), SvPVX(prog), &etext, sbrk(0), 0);
2581 /* unexec prints msg to stderr in case of failure */
2582 PerlProc_exit(status);
2585 # include <lib$routines.h>
2586 lib$signal(SS$_DEBUG); /* ssdef.h #included from vmsish.h */
2588 ABORT(); /* for use with undump */
2593 /* initialize curinterp */
2599 # define PERLVAR(var,type)
2600 # define PERLVARA(var,n,type)
2601 # if defined(PERL_IMPLICIT_CONTEXT)
2602 # if defined(USE_5005THREADS)
2603 # define PERLVARI(var,type,init) PERL_GET_INTERP->var = init;
2604 # define PERLVARIC(var,type,init) PERL_GET_INTERP->var = init;
2605 # else /* !USE_5005THREADS */
2606 # define PERLVARI(var,type,init) aTHX->var = init;
2607 # define PERLVARIC(var,type,init) aTHX->var = init;
2608 # endif /* USE_5005THREADS */
2610 # define PERLVARI(var,type,init) PERL_GET_INTERP->var = init;
2611 # define PERLVARIC(var,type,init) PERL_GET_INTERP->var = init;
2613 # include "intrpvar.h"
2614 # ifndef USE_5005THREADS
2615 # include "thrdvar.h"
2622 # define PERLVAR(var,type)
2623 # define PERLVARA(var,n,type)
2624 # define PERLVARI(var,type,init) PL_##var = init;
2625 # define PERLVARIC(var,type,init) PL_##var = init;
2626 # include "intrpvar.h"
2627 # ifndef USE_5005THREADS
2628 # include "thrdvar.h"
2639 S_init_main_stash(pTHX)
2645 PL_curstash = PL_defstash = newHV();
2646 PL_curstname = newSVpvn("main",4);
2647 gv = gv_fetchpv("main::",TRUE, SVt_PVHV);
2648 SvREFCNT_dec(GvHV(gv));
2649 GvHV(gv) = (HV*)SvREFCNT_inc(PL_defstash);
2651 HvNAME(PL_defstash) = savepv("main");
2652 PL_incgv = gv_HVadd(gv_AVadd(gv_fetchpv("INC",TRUE, SVt_PVAV)));
2653 GvMULTI_on(PL_incgv);
2654 PL_hintgv = gv_fetchpv("\010",TRUE, SVt_PV); /* ^H */
2655 GvMULTI_on(PL_hintgv);
2656 PL_defgv = gv_fetchpv("_",TRUE, SVt_PVAV);
2657 PL_errgv = gv_HVadd(gv_fetchpv("@", TRUE, SVt_PV));
2658 GvMULTI_on(PL_errgv);
2659 PL_replgv = gv_fetchpv("\022", TRUE, SVt_PV); /* ^R */
2660 GvMULTI_on(PL_replgv);
2661 (void)Perl_form(aTHX_ "%240s",""); /* Preallocate temp - for immediate signals. */
2662 sv_grow(ERRSV, 240); /* Preallocate - for immediate signals. */
2663 sv_setpvn(ERRSV, "", 0);
2664 PL_curstash = PL_defstash;
2665 CopSTASH_set(&PL_compiling, PL_defstash);
2666 PL_debstash = GvHV(gv_fetchpv("DB::", GV_ADDMULTI, SVt_PVHV));
2667 PL_globalstash = GvHV(gv_fetchpv("CORE::GLOBAL::", GV_ADDMULTI, SVt_PVHV));
2668 PL_nullstash = GvHV(gv_fetchpv("<none>::", GV_ADDMULTI, SVt_PVHV));
2669 /* We must init $/ before switches are processed. */
2670 sv_setpvn(get_sv("/", TRUE), "\n", 1);
2674 S_open_script(pTHX_ char *scriptname, bool dosearch, SV *sv, int *fdscript)
2678 char *cpp_discard_flag;
2684 PL_origfilename = savepv("-e");
2687 /* if find_script() returns, it returns a malloc()-ed value */
2688 PL_origfilename = scriptname = find_script(scriptname, dosearch, NULL, 1);
2690 if (strnEQ(scriptname, "/dev/fd/", 8) && isDIGIT(scriptname[8]) ) {
2691 char *s = scriptname + 8;
2692 *fdscript = atoi(s);
2696 scriptname = savepv(s + 1);
2697 Safefree(PL_origfilename);
2698 PL_origfilename = scriptname;
2703 CopFILE_free(PL_curcop);
2704 CopFILE_set(PL_curcop, PL_origfilename);
2705 if (strEQ(PL_origfilename,"-"))
2707 if (*fdscript >= 0) {
2708 PL_rsfp = PerlIO_fdopen(*fdscript,PERL_SCRIPT_MODE);
2709 # if defined(HAS_FCNTL) && defined(F_SETFD)
2711 /* ensure close-on-exec */
2712 fcntl(PerlIO_fileno(PL_rsfp),F_SETFD,1);
2715 else if (PL_preprocess) {
2716 char *cpp_cfg = CPPSTDIN;
2717 SV *cpp = newSVpvn("",0);
2718 SV *cmd = NEWSV(0,0);
2720 if (strEQ(cpp_cfg, "cppstdin"))
2721 Perl_sv_catpvf(aTHX_ cpp, "%s/", BIN_EXP);
2722 sv_catpv(cpp, cpp_cfg);
2725 sv_catpvn(sv, "-I", 2);
2726 sv_catpv(sv,PRIVLIB_EXP);
2729 DEBUG_P(PerlIO_printf(Perl_debug_log,
2730 "PL_preprocess: scriptname=\"%s\", cpp=\"%s\", sv=\"%s\", CPPMINUS=\"%s\"\n",
2731 scriptname, SvPVX (cpp), SvPVX (sv), CPPMINUS));
2733 # if defined(MSDOS) || defined(WIN32) || defined(VMS)
2740 cpp_discard_flag = "";
2742 cpp_discard_flag = "-C";
2746 perl = os2_execname(aTHX);
2748 perl = PL_origargv[0];
2752 /* This strips off Perl comments which might interfere with
2753 the C pre-processor, including #!. #line directives are
2754 deliberately stripped to avoid confusion with Perl's version
2755 of #line. FWP played some golf with it so it will fit
2756 into VMS's 255 character buffer.
2759 code = "(1../^#!.*perl/i)|/^\\s*#(?!\\s*((ifn?|un)def|(el|end)?if|define|include|else|error|pragma)\\b)/||!($|=1)||print";
2761 code = "/^\\s*#(?!\\s*((ifn?|un)def|(el|end)?if|define|include|else|error|pragma)\\b)/||!($|=1)||print";
2763 Perl_sv_setpvf(aTHX_ cmd, "\
2764 %s -ne%s%s%s %s | %"SVf" %s %"SVf" %s",
2765 perl, quote, code, quote, scriptname, cpp,
2766 cpp_discard_flag, sv, CPPMINUS);
2768 PL_doextract = FALSE;
2769 # ifdef IAMSUID /* actually, this is caught earlier */
2770 if (PL_euid != PL_uid && !PL_euid) { /* if running suidperl */
2772 (void)seteuid(PL_uid); /* musn't stay setuid root */
2774 # ifdef HAS_SETREUID
2775 (void)setreuid((Uid_t)-1, PL_uid);
2777 # ifdef HAS_SETRESUID
2778 (void)setresuid((Uid_t)-1, PL_uid, (Uid_t)-1);
2780 PerlProc_setuid(PL_uid);
2784 if (PerlProc_geteuid() != PL_uid)
2785 Perl_croak(aTHX_ "Can't do seteuid!\n");
2787 # endif /* IAMSUID */
2789 DEBUG_P(PerlIO_printf(Perl_debug_log,
2790 "PL_preprocess: cmd=\"%s\"\n",
2793 PL_rsfp = PerlProc_popen(SvPVX(cmd), "r");
2797 else if (!*scriptname) {
2798 forbid_setid("program input from stdin");
2799 PL_rsfp = PerlIO_stdin();
2802 PL_rsfp = PerlIO_open(scriptname,PERL_SCRIPT_MODE);
2803 # if defined(HAS_FCNTL) && defined(F_SETFD)
2805 /* ensure close-on-exec */
2806 fcntl(PerlIO_fileno(PL_rsfp),F_SETFD,1);
2811 # ifndef IAMSUID /* in case script is not readable before setuid */
2813 PerlLIO_stat(CopFILE(PL_curcop),&PL_statbuf) >= 0 &&
2814 PL_statbuf.st_mode & (S_ISUID|S_ISGID))
2817 PerlProc_execv(Perl_form(aTHX_ "%s/sperl"PERL_FS_VER_FMT,
2818 BIN_EXP, (int)PERL_REVISION,
2820 (int)PERL_SUBVERSION), PL_origargv);
2821 Perl_croak(aTHX_ "Can't do setuid\n");
2827 Perl_croak(aTHX_ "Can't open perl script: %s\n",
2830 Perl_croak(aTHX_ "Can't open perl script \"%s\": %s\n",
2831 CopFILE(PL_curcop), Strerror(errno));
2837 * I_SYSSTATVFS HAS_FSTATVFS
2839 * I_STATFS HAS_FSTATFS HAS_GETFSSTAT
2840 * I_MNTENT HAS_GETMNTENT HAS_HASMNTOPT
2841 * here so that metaconfig picks them up. */
2845 S_fd_on_nosuid_fs(pTHX_ int fd)
2847 int check_okay = 0; /* able to do all the required sys/libcalls */
2848 int on_nosuid = 0; /* the fd is on a nosuid fs */
2850 * Preferred order: fstatvfs(), fstatfs(), ustat()+getmnt(), getmntent().
2851 * fstatvfs() is UNIX98.
2852 * fstatfs() is 4.3 BSD.
2853 * ustat()+getmnt() is pre-4.3 BSD.
2854 * getmntent() is O(number-of-mounted-filesystems) and can hang on
2855 * an irrelevant filesystem while trying to reach the right one.
2858 #undef FD_ON_NOSUID_CHECK_OKAY /* found the syscalls to do the check? */
2860 # if !defined(FD_ON_NOSUID_CHECK_OKAY) && \
2861 defined(HAS_FSTATVFS)
2862 # define FD_ON_NOSUID_CHECK_OKAY
2863 struct statvfs stfs;
2865 check_okay = fstatvfs(fd, &stfs) == 0;
2866 on_nosuid = check_okay && (stfs.f_flag & ST_NOSUID);
2867 # endif /* fstatvfs */
2869 # if !defined(FD_ON_NOSUID_CHECK_OKAY) && \
2870 defined(PERL_MOUNT_NOSUID) && \
2871 defined(HAS_FSTATFS) && \
2872 defined(HAS_STRUCT_STATFS) && \
2873 defined(HAS_STRUCT_STATFS_F_FLAGS)
2874 # define FD_ON_NOSUID_CHECK_OKAY
2877 check_okay = fstatfs(fd, &stfs) == 0;
2878 on_nosuid = check_okay && (stfs.f_flags & PERL_MOUNT_NOSUID);
2879 # endif /* fstatfs */
2881 # if !defined(FD_ON_NOSUID_CHECK_OKAY) && \
2882 defined(PERL_MOUNT_NOSUID) && \
2883 defined(HAS_FSTAT) && \
2884 defined(HAS_USTAT) && \
2885 defined(HAS_GETMNT) && \
2886 defined(HAS_STRUCT_FS_DATA) && \
2888 # define FD_ON_NOSUID_CHECK_OKAY
2891 if (fstat(fd, &fdst) == 0) {
2893 if (ustat(fdst.st_dev, &us) == 0) {
2895 /* NOSTAT_ONE here because we're not examining fields which
2896 * vary between that case and STAT_ONE. */
2897 if (getmnt((int*)0, &fsd, (int)0, NOSTAT_ONE, us.f_fname) == 0) {
2898 size_t cmplen = sizeof(us.f_fname);
2899 if (sizeof(fsd.fd_req.path) < cmplen)
2900 cmplen = sizeof(fsd.fd_req.path);
2901 if (strnEQ(fsd.fd_req.path, us.f_fname, cmplen) &&
2902 fdst.st_dev == fsd.fd_req.dev) {
2904 on_nosuid = fsd.fd_req.flags & PERL_MOUNT_NOSUID;
2910 # endif /* fstat+ustat+getmnt */
2912 # if !defined(FD_ON_NOSUID_CHECK_OKAY) && \
2913 defined(HAS_GETMNTENT) && \
2914 defined(HAS_HASMNTOPT) && \
2915 defined(MNTOPT_NOSUID)
2916 # define FD_ON_NOSUID_CHECK_OKAY
2917 FILE *mtab = fopen("/etc/mtab", "r");
2918 struct mntent *entry;
2919 struct stat stb, fsb;
2921 if (mtab && (fstat(fd, &stb) == 0)) {
2922 while (entry = getmntent(mtab)) {
2923 if (stat(entry->mnt_dir, &fsb) == 0
2924 && fsb.st_dev == stb.st_dev)
2926 /* found the filesystem */
2928 if (hasmntopt(entry, MNTOPT_NOSUID))
2931 } /* A single fs may well fail its stat(). */
2936 # endif /* getmntent+hasmntopt */
2939 Perl_croak(aTHX_ "Can't check filesystem of script \"%s\" for nosuid", PL_origfilename);
2942 #endif /* IAMSUID */
2945 S_validate_suid(pTHX_ char *validarg, char *scriptname, int fdscript)
2951 /* do we need to emulate setuid on scripts? */
2953 /* This code is for those BSD systems that have setuid #! scripts disabled
2954 * in the kernel because of a security problem. Merely defining DOSUID
2955 * in perl will not fix that problem, but if you have disabled setuid
2956 * scripts in the kernel, this will attempt to emulate setuid and setgid
2957 * on scripts that have those now-otherwise-useless bits set. The setuid
2958 * root version must be called suidperl or sperlN.NNN. If regular perl
2959 * discovers that it has opened a setuid script, it calls suidperl with
2960 * the same argv that it had. If suidperl finds that the script it has
2961 * just opened is NOT setuid root, it sets the effective uid back to the
2962 * uid. We don't just make perl setuid root because that loses the
2963 * effective uid we had before invoking perl, if it was different from the
2966 * DOSUID must be defined in both perl and suidperl, and IAMSUID must
2967 * be defined in suidperl only. suidperl must be setuid root. The
2968 * Configure script will set this up for you if you want it.
2974 if (PerlLIO_fstat(PerlIO_fileno(PL_rsfp),&PL_statbuf) < 0) /* normal stat is insecure */
2975 Perl_croak(aTHX_ "Can't stat script \"%s\"",PL_origfilename);
2976 if (fdscript < 0 && PL_statbuf.st_mode & (S_ISUID|S_ISGID)) {
2981 #ifndef HAS_SETREUID
2982 /* On this access check to make sure the directories are readable,
2983 * there is actually a small window that the user could use to make
2984 * filename point to an accessible directory. So there is a faint
2985 * chance that someone could execute a setuid script down in a
2986 * non-accessible directory. I don't know what to do about that.
2987 * But I don't think it's too important. The manual lies when
2988 * it says access() is useful in setuid programs.
2990 if (PerlLIO_access(CopFILE(PL_curcop),1)) /*double check*/
2991 Perl_croak(aTHX_ "Permission denied");
2993 /* If we can swap euid and uid, then we can determine access rights
2994 * with a simple stat of the file, and then compare device and
2995 * inode to make sure we did stat() on the same file we opened.
2996 * Then we just have to make sure he or she can execute it.
2999 struct stat tmpstatbuf;
3003 setreuid(PL_euid,PL_uid) < 0
3006 setresuid(PL_euid,PL_uid,(Uid_t)-1) < 0
3009 || PerlProc_getuid() != PL_euid || PerlProc_geteuid() != PL_uid)
3010 Perl_croak(aTHX_ "Can't swap uid and euid"); /* really paranoid */
3011 if (PerlLIO_stat(CopFILE(PL_curcop),&tmpstatbuf) < 0)
3012 Perl_croak(aTHX_ "Permission denied"); /* testing full pathname here */
3013 #if defined(IAMSUID) && !defined(NO_NOSUID_CHECK)
3014 if (fd_on_nosuid_fs(PerlIO_fileno(PL_rsfp)))
3015 Perl_croak(aTHX_ "Permission denied");
3017 if (tmpstatbuf.st_dev != PL_statbuf.st_dev ||
3018 tmpstatbuf.st_ino != PL_statbuf.st_ino) {
3019 (void)PerlIO_close(PL_rsfp);
3020 Perl_croak(aTHX_ "Permission denied\n");
3024 setreuid(PL_uid,PL_euid) < 0
3026 # if defined(HAS_SETRESUID)
3027 setresuid(PL_uid,PL_euid,(Uid_t)-1) < 0
3030 || PerlProc_getuid() != PL_uid || PerlProc_geteuid() != PL_euid)
3031 Perl_croak(aTHX_ "Can't reswap uid and euid");
3032 if (!cando(S_IXUSR,FALSE,&PL_statbuf)) /* can real uid exec? */
3033 Perl_croak(aTHX_ "Permission denied\n");
3035 #endif /* HAS_SETREUID */
3036 #endif /* IAMSUID */
3038 if (!S_ISREG(PL_statbuf.st_mode))
3039 Perl_croak(aTHX_ "Permission denied");
3040 if (PL_statbuf.st_mode & S_IWOTH)
3041 Perl_croak(aTHX_ "Setuid/gid script is writable by world");
3042 PL_doswitches = FALSE; /* -s is insecure in suid */
3043 CopLINE_inc(PL_curcop);
3044 if (sv_gets(PL_linestr, PL_rsfp, 0) == Nullch ||
3045 strnNE(SvPV(PL_linestr,n_a),"#!",2) ) /* required even on Sys V */
3046 Perl_croak(aTHX_ "No #! line");
3047 s = SvPV(PL_linestr,n_a)+2;
3049 while (!isSPACE(*s)) s++;
3050 for (s2 = s; (s2 > SvPV(PL_linestr,n_a)+2 &&
3051 (isDIGIT(s2[-1]) || strchr("._-", s2[-1]))); s2--) ;
3052 if (strnNE(s2-4,"perl",4) && strnNE(s-9,"perl",4)) /* sanity check */
3053 Perl_croak(aTHX_ "Not a perl script");
3054 while (*s == ' ' || *s == '\t') s++;
3056 * #! arg must be what we saw above. They can invoke it by
3057 * mentioning suidperl explicitly, but they may not add any strange
3058 * arguments beyond what #! says if they do invoke suidperl that way.
3060 len = strlen(validarg);
3061 if (strEQ(validarg," PHOOEY ") ||
3062 strnNE(s,validarg,len) || !isSPACE(s[len]))
3063 Perl_croak(aTHX_ "Args must match #! line");
3066 if (PL_euid != PL_uid && (PL_statbuf.st_mode & S_ISUID) &&
3067 PL_euid == PL_statbuf.st_uid)
3069 Perl_croak(aTHX_ "YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
3070 FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
3071 #endif /* IAMSUID */
3073 if (PL_euid) { /* oops, we're not the setuid root perl */
3074 (void)PerlIO_close(PL_rsfp);
3077 PerlProc_execv(Perl_form(aTHX_ "%s/sperl"PERL_FS_VER_FMT, BIN_EXP,
3078 (int)PERL_REVISION, (int)PERL_VERSION,
3079 (int)PERL_SUBVERSION), PL_origargv);
3081 Perl_croak(aTHX_ "Can't do setuid\n");
3084 if (PL_statbuf.st_mode & S_ISGID && PL_statbuf.st_gid != PL_egid) {
3086 (void)setegid(PL_statbuf.st_gid);
3089 (void)setregid((Gid_t)-1,PL_statbuf.st_gid);
3091 #ifdef HAS_SETRESGID
3092 (void)setresgid((Gid_t)-1,PL_statbuf.st_gid,(Gid_t)-1);
3094 PerlProc_setgid(PL_statbuf.st_gid);
3098 if (PerlProc_getegid() != PL_statbuf.st_gid)
3099 Perl_croak(aTHX_ "Can't do setegid!\n");
3101 if (PL_statbuf.st_mode & S_ISUID) {
3102 if (PL_statbuf.st_uid != PL_euid)
3104 (void)seteuid(PL_statbuf.st_uid); /* all that for this */
3107 (void)setreuid((Uid_t)-1,PL_statbuf.st_uid);
3109 #ifdef HAS_SETRESUID
3110 (void)setresuid((Uid_t)-1,PL_statbuf.st_uid,(Uid_t)-1);
3112 PerlProc_setuid(PL_statbuf.st_uid);
3116 if (PerlProc_geteuid() != PL_statbuf.st_uid)
3117 Perl_croak(aTHX_ "Can't do seteuid!\n");
3119 else if (PL_uid) { /* oops, mustn't run as root */
3121 (void)seteuid((Uid_t)PL_uid);
3124 (void)setreuid((Uid_t)-1,(Uid_t)PL_uid);
3126 #ifdef HAS_SETRESUID
3127 (void)setresuid((Uid_t)-1,(Uid_t)PL_uid,(Uid_t)-1);
3129 PerlProc_setuid((Uid_t)PL_uid);
3133 if (PerlProc_geteuid() != PL_uid)
3134 Perl_croak(aTHX_ "Can't do seteuid!\n");
3137 if (!cando(S_IXUSR,TRUE,&PL_statbuf))
3138 Perl_croak(aTHX_ "Permission denied\n"); /* they can't do this */
3141 else if (PL_preprocess)
3142 Perl_croak(aTHX_ "-P not allowed for setuid/setgid script\n");
3143 else if (fdscript >= 0)
3144 Perl_croak(aTHX_ "fd script not allowed in suidperl\n");
3146 Perl_croak(aTHX_ "Script is not setuid/setgid in suidperl\n");
3148 /* We absolutely must clear out any saved ids here, so we */
3149 /* exec the real perl, substituting fd script for scriptname. */
3150 /* (We pass script name as "subdir" of fd, which perl will grok.) */
3151 PerlIO_rewind(PL_rsfp);
3152 PerlLIO_lseek(PerlIO_fileno(PL_rsfp),(Off_t)0,0); /* just in case rewind didn't */
3153 for (which = 1; PL_origargv[which] && PL_origargv[which] != scriptname; which++) ;
3154 if (!PL_origargv[which])
3155 Perl_croak(aTHX_ "Permission denied");
3156 PL_origargv[which] = savepv(Perl_form(aTHX_ "/dev/fd/%d/%s",
3157 PerlIO_fileno(PL_rsfp), PL_origargv[which]));
3158 #if defined(HAS_FCNTL) && defined(F_SETFD)
3159 fcntl(PerlIO_fileno(PL_rsfp),F_SETFD,0); /* ensure no close-on-exec */
3161 PerlProc_execv(Perl_form(aTHX_ "%s/perl"PERL_FS_VER_FMT, BIN_EXP,
3162 (int)PERL_REVISION, (int)PERL_VERSION,
3163 (int)PERL_SUBVERSION), PL_origargv);/* try again */
3164 Perl_croak(aTHX_ "Can't do setuid\n");
3165 #endif /* IAMSUID */
3167 if (PL_euid != PL_uid || PL_egid != PL_gid) { /* (suidperl doesn't exist, in fact) */
3168 #ifndef SETUID_SCRIPTS_ARE_SECURE_NOW
3169 PerlLIO_fstat(PerlIO_fileno(PL_rsfp),&PL_statbuf); /* may be either wrapped or real suid */
3170 if ((PL_euid != PL_uid && PL_euid == PL_statbuf.st_uid && PL_statbuf.st_mode & S_ISUID)
3172 (PL_egid != PL_gid && PL_egid == PL_statbuf.st_gid && PL_statbuf.st_mode & S_ISGID)
3175 Perl_croak(aTHX_ "YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
3176 FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
3177 #endif /* SETUID_SCRIPTS_ARE_SECURE_NOW */
3178 /* not set-id, must be wrapped */
3184 S_find_beginning(pTHX)
3186 register char *s, *s2;
3188 /* skip forward in input to the real script? */
3191 #ifdef MACOS_TRADITIONAL
3192 /* Since the Mac OS does not honor #! arguments for us, we do it ourselves */
3194 while (PL_doextract || gMacPerl_AlwaysExtract) {
3195 if ((s = sv_gets(PL_linestr, PL_rsfp, 0)) == Nullch) {
3196 if (!gMacPerl_AlwaysExtract)
3197 Perl_croak(aTHX_ "No Perl script found in input\n");
3199 if (PL_doextract) /* require explicit override ? */
3200 if (!OverrideExtract(PL_origfilename))
3201 Perl_croak(aTHX_ "User aborted script\n");
3203 PL_doextract = FALSE;
3205 /* Pater peccavi, file does not have #! */
3206 PerlIO_rewind(PL_rsfp);
3211 while (PL_doextract) {
3212 if ((s = sv_gets(PL_linestr, PL_rsfp, 0)) == Nullch)
3213 Perl_croak(aTHX_ "No Perl script found in input\n");
3216 if (*s == '#' && s[1] == '!' && ((s = instr(s,"perl")) || (s = instr(s2,"PERL")))) {
3217 PerlIO_ungetc(PL_rsfp, '\n'); /* to keep line count right */
3218 PL_doextract = FALSE;
3219 while (*s && !(isSPACE (*s) || *s == '#')) s++;
3221 while (*s == ' ' || *s == '\t') s++;
3223 while (isDIGIT(s2[-1]) || strchr("-._", s2[-1])) s2--;
3224 if (strnEQ(s2-4,"perl",4))
3226 while ((s = moreswitches(s)))
3229 #ifdef MACOS_TRADITIONAL
3240 PL_uid = PerlProc_getuid();
3241 PL_euid = PerlProc_geteuid();
3242 PL_gid = PerlProc_getgid();
3243 PL_egid = PerlProc_getegid();
3245 PL_uid |= PL_gid << 16;
3246 PL_euid |= PL_egid << 16;
3248 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
3252 S_forbid_setid(pTHX_ char *s)
3254 if (PL_euid != PL_uid)
3255 Perl_croak(aTHX_ "No %s allowed while running setuid", s);
3256 if (PL_egid != PL_gid)
3257 Perl_croak(aTHX_ "No %s allowed while running setgid", s);
3261 Perl_init_debugger(pTHX)
3263 HV *ostash = PL_curstash;
3265 PL_curstash = PL_debstash;
3266 PL_dbargs = GvAV(gv_AVadd((gv_fetchpv("args", GV_ADDMULTI, SVt_PVAV))));
3267 AvREAL_off(PL_dbargs);
3268 PL_DBgv = gv_fetchpv("DB", GV_ADDMULTI, SVt_PVGV);
3269 PL_DBline = gv_fetchpv("dbline", GV_ADDMULTI, SVt_PVAV);
3270 PL_DBsub = gv_HVadd(gv_fetchpv("sub", GV_ADDMULTI, SVt_PVHV));
3271 sv_upgrade(GvSV(PL_DBsub), SVt_IV); /* IVX accessed if PERLDB_SUB_NN */
3272 PL_DBsingle = GvSV((gv_fetchpv("single", GV_ADDMULTI, SVt_PV)));
3273 sv_setiv(PL_DBsingle, 0);
3274 PL_DBtrace = GvSV((gv_fetchpv("trace", GV_ADDMULTI, SVt_PV)));
3275 sv_setiv(PL_DBtrace, 0);
3276 PL_DBsignal = GvSV((gv_fetchpv("signal", GV_ADDMULTI, SVt_PV)));
3277 sv_setiv(PL_DBsignal, 0);
3278 PL_curstash = ostash;
3281 #ifndef STRESS_REALLOC
3282 #define REASONABLE(size) (size)
3284 #define REASONABLE(size) (1) /* unreasonable */
3288 Perl_init_stacks(pTHX)
3290 /* start with 128-item stack and 8K cxstack */
3291 PL_curstackinfo = new_stackinfo(REASONABLE(128),
3292 REASONABLE(8192/sizeof(PERL_CONTEXT) - 1));
3293 PL_curstackinfo->si_type = PERLSI_MAIN;
3294 PL_curstack = PL_curstackinfo->si_stack;
3295 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
3297 PL_stack_base = AvARRAY(PL_curstack);
3298 PL_stack_sp = PL_stack_base;
3299 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
3301 New(50,PL_tmps_stack,REASONABLE(128),SV*);
3304 PL_tmps_max = REASONABLE(128);
3306 New(54,PL_markstack,REASONABLE(32),I32);
3307 PL_markstack_ptr = PL_markstack;
3308 PL_markstack_max = PL_markstack + REASONABLE(32);
3312 New(54,PL_scopestack,REASONABLE(32),I32);
3313 PL_scopestack_ix = 0;
3314 PL_scopestack_max = REASONABLE(32);
3316 New(54,PL_savestack,REASONABLE(128),ANY);
3317 PL_savestack_ix = 0;
3318 PL_savestack_max = REASONABLE(128);
3320 New(54,PL_retstack,REASONABLE(16),OP*);
3322 PL_retstack_max = REASONABLE(16);
3330 while (PL_curstackinfo->si_next)
3331 PL_curstackinfo = PL_curstackinfo->si_next;
3332 while (PL_curstackinfo) {
3333 PERL_SI *p = PL_curstackinfo->si_prev;
3334 /* curstackinfo->si_stack got nuked by sv_free_arenas() */
3335 Safefree(PL_curstackinfo->si_cxstack);
3336 Safefree(PL_curstackinfo);
3337 PL_curstackinfo = p;
3339 Safefree(PL_tmps_stack);
3340 Safefree(PL_markstack);
3341 Safefree(PL_scopestack);
3342 Safefree(PL_savestack);
3343 Safefree(PL_retstack);
3352 lex_start(PL_linestr);
3354 PL_subname = newSVpvn("main",4);
3358 S_init_predump_symbols(pTHX)
3363 sv_setpvn(get_sv("\"", TRUE), " ", 1);
3364 PL_stdingv = gv_fetchpv("STDIN",TRUE, SVt_PVIO);
3365 GvMULTI_on(PL_stdingv);
3366 io = GvIOp(PL_stdingv);
3367 IoTYPE(io) = IoTYPE_RDONLY;
3368 IoIFP(io) = PerlIO_stdin();
3369 tmpgv = gv_fetchpv("stdin",TRUE, SVt_PV);
3371 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(io);
3373 tmpgv = gv_fetchpv("STDOUT",TRUE, SVt_PVIO);
3376 IoTYPE(io) = IoTYPE_WRONLY;
3377 IoOFP(io) = IoIFP(io) = PerlIO_stdout();
3379 tmpgv = gv_fetchpv("stdout",TRUE, SVt_PV);
3381 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(io);
3383 PL_stderrgv = gv_fetchpv("STDERR",TRUE, SVt_PVIO);
3384 GvMULTI_on(PL_stderrgv);
3385 io = GvIOp(PL_stderrgv);
3386 IoTYPE(io) = IoTYPE_WRONLY;
3387 IoOFP(io) = IoIFP(io) = PerlIO_stderr();
3388 tmpgv = gv_fetchpv("stderr",TRUE, SVt_PV);
3390 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(io);
3392 PL_statname = NEWSV(66,0); /* last filename we did stat on */
3395 Safefree(PL_osname);
3396 PL_osname = savepv(OSNAME);
3400 Perl_init_argv_symbols(pTHX_ register int argc, register char **argv)
3403 argc--,argv++; /* skip name of script */
3404 if (PL_doswitches) {
3405 for (; argc > 0 && **argv == '-'; argc--,argv++) {
3408 if (argv[0][1] == '-' && !argv[0][2]) {
3412 if ((s = strchr(argv[0], '='))) {
3414 sv_setpv(GvSV(gv_fetchpv(argv[0]+1,TRUE, SVt_PV)),s);
3417 sv_setiv(GvSV(gv_fetchpv(argv[0]+1,TRUE, SVt_PV)),1);
3420 if ((PL_argvgv = gv_fetchpv("ARGV",TRUE, SVt_PVAV))) {
3421 GvMULTI_on(PL_argvgv);
3422 (void)gv_AVadd(PL_argvgv);
3423 av_clear(GvAVn(PL_argvgv));
3424 for (; argc > 0; argc--,argv++) {
3425 SV *sv = newSVpv(argv[0],0);
3426 av_push(GvAVn(PL_argvgv),sv);
3427 if (PL_widesyscalls)
3428 (void)sv_utf8_decode(sv);
3433 #ifdef HAS_PROCSELFEXE
3434 /* This is a function so that we don't hold on to MAXPATHLEN
3435 bytes of stack longer than necessary
3438 S_procself_val(pTHX_ SV *sv, char *arg0)
3440 char buf[MAXPATHLEN];
3441 int len = readlink(PROCSELFEXE_PATH, buf, sizeof(buf) - 1);
3443 sv_setpvn(sv,buf,len);
3449 #endif /* HAS_PROCSELFEXE */
3452 S_init_postdump_symbols(pTHX_ register int argc, register char **argv, register char **env)
3458 PL_toptarget = NEWSV(0,0);
3459 sv_upgrade(PL_toptarget, SVt_PVFM);
3460 sv_setpvn(PL_toptarget, "", 0);
3461 PL_bodytarget = NEWSV(0,0);
3462 sv_upgrade(PL_bodytarget, SVt_PVFM);
3463 sv_setpvn(PL_bodytarget, "", 0);
3464 PL_formtarget = PL_bodytarget;
3468 init_argv_symbols(argc,argv);
3470 if ((tmpgv = gv_fetchpv("0",TRUE, SVt_PV))) {
3471 #ifdef MACOS_TRADITIONAL
3472 /* $0 is not majick on a Mac */
3473 sv_setpv(GvSV(tmpgv),MacPerl_MPWFileName(PL_origfilename));
3475 sv_setpv(GvSV(tmpgv),PL_origfilename);
3476 magicname("0", "0", 1);
3479 if ((tmpgv = gv_fetchpv("\030",TRUE, SVt_PV))) {/* $^X */
3480 #ifdef HAS_PROCSELFEXE
3481 S_procself_val(aTHX_ GvSV(tmpgv), PL_origargv[0]);
3484 sv_setpv(GvSV(tmpgv), os2_execname(aTHX));
3486 sv_setpv(GvSV(tmpgv),PL_origargv[0]);
3490 if ((PL_envgv = gv_fetchpv("ENV",TRUE, SVt_PVHV))) {
3492 GvMULTI_on(PL_envgv);
3493 hv = GvHVn(PL_envgv);
3494 hv_magic(hv, Nullgv, PERL_MAGIC_env);
3495 #ifdef USE_ENVIRON_ARRAY
3496 /* Note that if the supplied env parameter is actually a copy
3497 of the global environ then it may now point to free'd memory
3498 if the environment has been modified since. To avoid this
3499 problem we treat env==NULL as meaning 'use the default'
3504 environ[0] = Nullch;
3506 for (; *env; env++) {
3507 if (!(s = strchr(*env,'=')))
3514 sv = newSVpv(s+1, 0);
3515 (void)hv_store(hv, *env, s - *env, sv, 0);
3519 #endif /* USE_ENVIRON_ARRAY */
3522 if ((tmpgv = gv_fetchpv("$",TRUE, SVt_PV))) {
3523 SvREADONLY_off(GvSV(tmpgv));
3524 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3525 SvREADONLY_on(GvSV(tmpgv));
3530 S_init_perllib(pTHX)
3535 s = PerlEnv_getenv("PERL5LIB");
3537 incpush(s, TRUE, TRUE);
3539 incpush(PerlEnv_getenv("PERLLIB"), FALSE, FALSE);
3541 /* Treat PERL5?LIB as a possible search list logical name -- the
3542 * "natural" VMS idiom for a Unix path string. We allow each
3543 * element to be a set of |-separated directories for compatibility.
3547 if (my_trnlnm("PERL5LIB",buf,0))
3548 do { incpush(buf,TRUE,TRUE); } while (my_trnlnm("PERL5LIB",buf,++idx));
3550 while (my_trnlnm("PERLLIB",buf,idx++)) incpush(buf,FALSE,FALSE);
3554 /* Use the ~-expanded versions of APPLLIB (undocumented),
3555 ARCHLIB PRIVLIB SITEARCH SITELIB VENDORARCH and VENDORLIB
3558 incpush(APPLLIB_EXP, TRUE, TRUE);
3562 incpush(ARCHLIB_EXP, FALSE, FALSE);
3564 #ifdef MACOS_TRADITIONAL
3566 struct stat tmpstatbuf;
3567 SV * privdir = NEWSV(55, 0);
3568 char * macperl = PerlEnv_getenv("MACPERL");
3573 Perl_sv_setpvf(aTHX_ privdir, "%slib:", macperl);
3574 if (PerlLIO_stat(SvPVX(privdir), &tmpstatbuf) >= 0 && S_ISDIR(tmpstatbuf.st_mode))
3575 incpush(SvPVX(privdir), TRUE, FALSE);
3576 Perl_sv_setpvf(aTHX_ privdir, "%ssite_perl:", macperl);
3577 if (PerlLIO_stat(SvPVX(privdir), &tmpstatbuf) >= 0 && S_ISDIR(tmpstatbuf.st_mode))
3578 incpush(SvPVX(privdir), TRUE, FALSE);
3580 SvREFCNT_dec(privdir);
3583 incpush(":", FALSE, FALSE);
3586 # define PRIVLIB_EXP "/usr/local/lib/perl5:/usr/local/lib/perl"
3589 incpush(PRIVLIB_EXP, TRUE, FALSE);
3591 incpush(PRIVLIB_EXP, FALSE, FALSE);
3595 /* sitearch is always relative to sitelib on Windows for
3596 * DLL-based path intuition to work correctly */
3597 # if !defined(WIN32)
3598 incpush(SITEARCH_EXP, FALSE, FALSE);
3604 incpush(SITELIB_EXP, TRUE, FALSE); /* this picks up sitearch as well */
3606 incpush(SITELIB_EXP, FALSE, FALSE);
3610 #ifdef SITELIB_STEM /* Search for version-specific dirs below here */
3611 incpush(SITELIB_STEM, FALSE, TRUE);
3614 #ifdef PERL_VENDORARCH_EXP
3615 /* vendorarch is always relative to vendorlib on Windows for
3616 * DLL-based path intuition to work correctly */
3617 # if !defined(WIN32)
3618 incpush(PERL_VENDORARCH_EXP, FALSE, FALSE);
3622 #ifdef PERL_VENDORLIB_EXP
3624 incpush(PERL_VENDORLIB_EXP, TRUE, FALSE); /* this picks up vendorarch as well */
3626 incpush(PERL_VENDORLIB_EXP, FALSE, FALSE);
3630 #ifdef PERL_VENDORLIB_STEM /* Search for version-specific dirs below here */
3631 incpush(PERL_VENDORLIB_STEM, FALSE, TRUE);
3634 #ifdef PERL_OTHERLIBDIRS
3635 incpush(PERL_OTHERLIBDIRS, TRUE, TRUE);
3639 incpush(".", FALSE, FALSE);
3640 #endif /* MACOS_TRADITIONAL */
3643 #if defined(DOSISH) || defined(EPOC)
3644 # define PERLLIB_SEP ';'
3647 # define PERLLIB_SEP '|'
3649 # if defined(MACOS_TRADITIONAL)
3650 # define PERLLIB_SEP ','
3652 # define PERLLIB_SEP ':'
3656 #ifndef PERLLIB_MANGLE
3657 # define PERLLIB_MANGLE(s,n) (s)
3661 S_incpush(pTHX_ char *p, int addsubdirs, int addoldvers)
3663 SV *subdir = Nullsv;
3668 if (addsubdirs || addoldvers) {
3669 subdir = sv_newmortal();
3672 /* Break at all separators */
3674 SV *libdir = NEWSV(55,0);
3677 /* skip any consecutive separators */
3678 while ( *p == PERLLIB_SEP ) {
3679 /* Uncomment the next line for PATH semantics */
3680 /* av_push(GvAVn(PL_incgv), newSVpvn(".", 1)); */
3684 if ( (s = strchr(p, PERLLIB_SEP)) != Nullch ) {
3685 sv_setpvn(libdir, PERLLIB_MANGLE(p, (STRLEN)(s - p)),
3690 sv_setpv(libdir, PERLLIB_MANGLE(p, 0));
3691 p = Nullch; /* break out */
3693 #ifdef MACOS_TRADITIONAL
3694 if (!strchr(SvPVX(libdir), ':'))
3695 sv_insert(libdir, 0, 0, ":", 1);
3696 if (SvPVX(libdir)[SvCUR(libdir)-1] != ':')
3697 sv_catpv(libdir, ":");
3701 * BEFORE pushing libdir onto @INC we may first push version- and
3702 * archname-specific sub-directories.
3704 if (addsubdirs || addoldvers) {
3705 #ifdef PERL_INC_VERSION_LIST
3706 /* Configure terminates PERL_INC_VERSION_LIST with a NULL */
3707 const char *incverlist[] = { PERL_INC_VERSION_LIST };
3708 const char **incver;
3710 struct stat tmpstatbuf;
3715 if ((unix = tounixspec_ts(SvPV(libdir,len),Nullch)) != Nullch) {
3717 while (unix[len-1] == '/') len--; /* Cosmetic */
3718 sv_usepvn(libdir,unix,len);
3721 PerlIO_printf(Perl_error_log,
3722 "Failed to unixify @INC element \"%s\"\n",
3726 #ifdef MACOS_TRADITIONAL
3727 #define PERL_AV_SUFFIX_FMT ""
3728 #define PERL_ARCH_FMT "%s:"
3729 #define PERL_ARCH_FMT_PATH PERL_FS_VER_FMT PERL_AV_SUFFIX_FMT
3731 #define PERL_AV_SUFFIX_FMT "/"
3732 #define PERL_ARCH_FMT "/%s"
3733 #define PERL_ARCH_FMT_PATH PERL_AV_SUFFIX_FMT PERL_FS_VER_FMT
3735 /* .../version/archname if -d .../version/archname */
3736 Perl_sv_setpvf(aTHX_ subdir, "%"SVf PERL_ARCH_FMT_PATH PERL_ARCH_FMT,
3738 (int)PERL_REVISION, (int)PERL_VERSION,
3739 (int)PERL_SUBVERSION, ARCHNAME);
3740 if (PerlLIO_stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
3741 S_ISDIR(tmpstatbuf.st_mode))
3742 av_push(GvAVn(PL_incgv), newSVsv(subdir));
3744 /* .../version if -d .../version */
3745 Perl_sv_setpvf(aTHX_ subdir, "%"SVf PERL_ARCH_FMT_PATH, libdir,
3746 (int)PERL_REVISION, (int)PERL_VERSION,
3747 (int)PERL_SUBVERSION);
3748 if (PerlLIO_stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
3749 S_ISDIR(tmpstatbuf.st_mode))
3750 av_push(GvAVn(PL_incgv), newSVsv(subdir));
3752 /* .../archname if -d .../archname */
3753 Perl_sv_setpvf(aTHX_ subdir, "%"SVf PERL_ARCH_FMT, libdir, ARCHNAME);
3754 if (PerlLIO_stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
3755 S_ISDIR(tmpstatbuf.st_mode))
3756 av_push(GvAVn(PL_incgv), newSVsv(subdir));
3759 #ifdef PERL_INC_VERSION_LIST
3761 for (incver = incverlist; *incver; incver++) {
3762 /* .../xxx if -d .../xxx */
3763 Perl_sv_setpvf(aTHX_ subdir, "%"SVf PERL_ARCH_FMT, libdir, *incver);
3764 if (PerlLIO_stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
3765 S_ISDIR(tmpstatbuf.st_mode))
3766 av_push(GvAVn(PL_incgv), newSVsv(subdir));
3772 /* finally push this lib directory on the end of @INC */
3773 av_push(GvAVn(PL_incgv), libdir);
3777 #ifdef USE_5005THREADS
3778 STATIC struct perl_thread *
3779 S_init_main_thread(pTHX)
3781 #if !defined(PERL_IMPLICIT_CONTEXT)
3782 struct perl_thread *thr;
3786 Newz(53, thr, 1, struct perl_thread);
3787 PL_curcop = &PL_compiling;
3788 thr->interp = PERL_GET_INTERP;
3789 thr->cvcache = newHV();
3790 thr->threadsv = newAV();
3791 /* thr->threadsvp is set when find_threadsv is called */
3792 thr->specific = newAV();
3793 thr->flags = THRf_R_JOINABLE;
3794 MUTEX_INIT(&thr->mutex);
3795 /* Handcraft thrsv similarly to mess_sv */
3796 New(53, PL_thrsv, 1, SV);
3797 Newz(53, xpv, 1, XPV);
3798 SvFLAGS(PL_thrsv) = SVt_PV;
3799 SvANY(PL_thrsv) = (void*)xpv;
3800 SvREFCNT(PL_thrsv) = 1 << 30; /* practically infinite */
3801 SvPVX(PL_thrsv) = (char*)thr;
3802 SvCUR_set(PL_thrsv, sizeof(thr));
3803 SvLEN_set(PL_thrsv, sizeof(thr));
3804 *SvEND(PL_thrsv) = '\0'; /* in the trailing_nul field */
3805 thr->oursv = PL_thrsv;
3806 PL_chopset = " \n-";
3809 MUTEX_LOCK(&PL_threads_mutex);
3815 MUTEX_UNLOCK(&PL_threads_mutex);
3817 #ifdef HAVE_THREAD_INTERN
3818 Perl_init_thread_intern(thr);
3821 #ifdef SET_THREAD_SELF
3822 SET_THREAD_SELF(thr);
3824 thr->self = pthread_self();
3825 #endif /* SET_THREAD_SELF */
3829 * These must come after the thread self setting
3830 * because sv_setpvn does SvTAINT and the taint
3831 * fields thread selfness being set.
3833 PL_toptarget = NEWSV(0,0);
3834 sv_upgrade(PL_toptarget, SVt_PVFM);
3835 sv_setpvn(PL_toptarget, "", 0);
3836 PL_bodytarget = NEWSV(0,0);
3837 sv_upgrade(PL_bodytarget, SVt_PVFM);
3838 sv_setpvn(PL_bodytarget, "", 0);
3839 PL_formtarget = PL_bodytarget;
3840 thr->errsv = newSVpvn("", 0);
3841 (void) find_threadsv("@"); /* Ensure $@ is initialised early */
3844 PL_peepp = MEMBER_TO_FPTR(Perl_peep);
3845 PL_regcompp = MEMBER_TO_FPTR(Perl_pregcomp);
3846 PL_regexecp = MEMBER_TO_FPTR(Perl_regexec_flags);
3847 PL_regint_start = MEMBER_TO_FPTR(Perl_re_intuit_start);
3848 PL_regint_string = MEMBER_TO_FPTR(Perl_re_intuit_string);
3849 PL_regfree = MEMBER_TO_FPTR(Perl_pregfree);
3851 PL_reginterp_cnt = 0;
3855 #endif /* USE_5005THREADS */
3858 Perl_call_list(pTHX_ I32 oldscope, AV *paramList)
3861 line_t oldline = CopLINE(PL_curcop);
3867 while (AvFILL(paramList) >= 0) {
3868 cv = (CV*)av_shift(paramList);
3869 if (PL_savebegin && (paramList == PL_beginav)) {
3870 /* save PL_beginav for compiler */
3871 if (! PL_beginav_save)
3872 PL_beginav_save = newAV();
3873 av_push(PL_beginav_save, (SV*)cv);
3877 #ifdef PERL_FLEXIBLE_EXCEPTIONS
3878 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vcall_list_body), cv);
3884 #ifndef PERL_FLEXIBLE_EXCEPTIONS
3888 (void)SvPV(atsv, len);
3891 PL_curcop = &PL_compiling;
3892 CopLINE_set(PL_curcop, oldline);
3893 if (paramList == PL_beginav)
3894 sv_catpv(atsv, "BEGIN failed--compilation aborted");
3896 Perl_sv_catpvf(aTHX_ atsv,
3897 "%s failed--call queue aborted",
3898 paramList == PL_checkav ? "CHECK"
3899 : paramList == PL_initav ? "INIT"
3901 while (PL_scopestack_ix > oldscope)
3904 Perl_croak(aTHX_ "%s", SvPVx(atsv, n_a));
3911 /* my_exit() was called */
3912 while (PL_scopestack_ix > oldscope)
3915 PL_curstash = PL_defstash;
3916 PL_curcop = &PL_compiling;
3917 CopLINE_set(PL_curcop, oldline);
3919 if (PL_statusvalue && !(PL_exit_flags & PERL_EXIT_EXPECTED)) {
3920 if (paramList == PL_beginav)
3921 Perl_croak(aTHX_ "BEGIN failed--compilation aborted");
3923 Perl_croak(aTHX_ "%s failed--call queue aborted",
3924 paramList == PL_checkav ? "CHECK"
3925 : paramList == PL_initav ? "INIT"
3932 PL_curcop = &PL_compiling;
3933 CopLINE_set(PL_curcop, oldline);
3936 PerlIO_printf(Perl_error_log, "panic: restartop\n");
3944 #ifdef PERL_FLEXIBLE_EXCEPTIONS
3946 S_vcall_list_body(pTHX_ va_list args)
3948 CV *cv = va_arg(args, CV*);
3949 return call_list_body(cv);
3954 S_call_list_body(pTHX_ CV *cv)
3956 PUSHMARK(PL_stack_sp);
3957 call_sv((SV*)cv, G_EVAL|G_DISCARD);
3962 Perl_my_exit(pTHX_ U32 status)
3964 DEBUG_S(PerlIO_printf(Perl_debug_log, "my_exit: thread %p, status %lu\n",
3965 thr, (unsigned long) status));
3974 STATUS_NATIVE_SET(status);
3981 Perl_my_failure_exit(pTHX)
3984 if (vaxc$errno & 1) {
3985 if (STATUS_NATIVE & 1) /* fortuitiously includes "-1" */
3986 STATUS_NATIVE_SET(44);
3989 if (!vaxc$errno && errno) /* unlikely */
3990 STATUS_NATIVE_SET(44);
3992 STATUS_NATIVE_SET(vaxc$errno);
3997 STATUS_POSIX_SET(errno);
3999 exitstatus = STATUS_POSIX >> 8;
4000 if (exitstatus & 255)
4001 STATUS_POSIX_SET(exitstatus);
4003 STATUS_POSIX_SET(255);
4010 S_my_exit_jump(pTHX)
4012 register PERL_CONTEXT *cx;
4017 SvREFCNT_dec(PL_e_script);
4018 PL_e_script = Nullsv;
4021 POPSTACK_TO(PL_mainstack);
4022 if (cxstack_ix >= 0) {
4025 POPBLOCK(cx,PL_curpm);
4033 read_e_script(pTHX_ int idx, SV *buf_sv, int maxlen)
4036 p = SvPVX(PL_e_script);
4037 nl = strchr(p, '\n');
4038 nl = (nl) ? nl+1 : SvEND(PL_e_script);
4040 filter_del(read_e_script);
4043 sv_catpvn(buf_sv, p, nl-p);
4044 sv_chop(PL_e_script, nl);