3 * Copyright (c) 1987-1997 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
16 #include "patchlevel.h"
18 /* XXX If this causes problems, set i_unistd=undef in the hint file. */
23 dEXTCONST char rcsid[] = "perl.c\nPatch level: ###\n";
31 #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
41 curcop = &compiling; \
48 laststype = OP_STAT; \
50 maxsysfd = MAXSYSFD; \
57 laststype = OP_STAT; \
60 static void find_beginning _((void));
61 static void forbid_setid _((char *));
62 static void incpush _((char *, int));
63 static void init_ids _((void));
64 static void init_debugger _((void));
65 static void init_lexer _((void));
66 static void init_main_stash _((void));
67 static void init_perllib _((void));
68 static void init_postdump_symbols _((int, char **, char **));
69 static void init_predump_symbols _((void));
70 static void init_stacks _((void));
71 static void my_exit_jump _((void)) __attribute__((noreturn));
72 static void nuke_stacks _((void));
73 static void open_script _((char *, bool, SV *));
74 static void usage _((char *));
75 static void validate_suid _((char *, char*));
77 static int fdscript = -1;
82 PerlInterpreter *sv_interp;
85 New(53, sv_interp, 1, PerlInterpreter);
90 perl_construct( sv_interp )
91 register PerlInterpreter *sv_interp;
93 if (!(curinterp = sv_interp))
97 Zero(sv_interp, 1, PerlInterpreter);
100 /* Init the real globals? */
102 linestr = NEWSV(65,80);
103 sv_upgrade(linestr,SVt_PVIV);
105 if (!SvREADONLY(&sv_undef)) {
106 SvREADONLY_on(&sv_undef);
110 SvREADONLY_on(&sv_no);
112 sv_setpv(&sv_yes,Yes);
114 SvREADONLY_on(&sv_yes);
117 nrs = newSVpv("\n", 1);
118 rs = SvREFCNT_inc(nrs);
124 * There is no way we can refer to them from Perl so close them to save
125 * space. The other alternative would be to provide STDAUX and STDPRN
128 (void)fclose(stdaux);
129 (void)fclose(stdprn);
135 perl_destruct_level = 1;
137 if(perl_destruct_level > 0)
145 SET_NUMERIC_STANDARD();
146 #if defined(SUBVERSION) && SUBVERSION > 0
147 sprintf(patchlevel, "%7.5f", (double) 5
148 + ((double) PATCHLEVEL / (double) 1000)
149 + ((double) SUBVERSION / (double) 100000));
151 sprintf(patchlevel, "%5.3f", (double) 5 +
152 ((double) PATCHLEVEL / (double) 1000));
155 #if defined(LOCAL_PATCH_COUNT)
156 localpatches = local_patches; /* For possible -v */
159 PerlIO_init(); /* Hook to IO system */
161 fdpid = newAV(); /* for remembering popen pids by fd */
168 perl_destruct(sv_interp)
169 register PerlInterpreter *sv_interp;
171 int destruct_level; /* 0=none, 1=full, 2=full with checks */
175 if (!(curinterp = sv_interp))
178 destruct_level = perl_destruct_level;
182 if (s = getenv("PERL_DESTRUCT_LEVEL")) {
184 if (destruct_level < i)
190 /* unhook hooks which will soon be, or use, destroyed data */
191 SvREFCNT_dec(warnhook);
193 SvREFCNT_dec(diehook);
195 SvREFCNT_dec(parsehook);
201 /* We must account for everything. */
203 /* Destroy the main CV and syntax tree */
205 curpad = AvARRAY(comppad);
210 SvREFCNT_dec(main_cv);
215 * Try to destruct global references. We do this first so that the
216 * destructors and destructees still exist. Some sv's might remain.
217 * Non-referenced objects are on their own.
224 if (destruct_level == 0){
226 DEBUG_P(debprofdump());
228 /* The exit() function will do everything that needs doing. */
232 /* loosen bonds of global variables */
235 (void)PerlIO_close(rsfp);
239 /* Filters for program text */
240 SvREFCNT_dec(rsfp_filters);
241 rsfp_filters = Nullav;
253 sawampersand = FALSE; /* must save all match strings */
254 sawstudy = FALSE; /* do fbm_instr on all strings */
269 /* magical thingies */
271 Safefree(ofs); /* $, */
274 Safefree(ors); /* $\ */
277 SvREFCNT_dec(nrs); /* $\ helper */
280 multiline = 0; /* $* */
282 SvREFCNT_dec(statname);
286 /* defgv, aka *_ should be taken care of elsewhere */
288 #if 0 /* just about all regexp stuff, seems to be ok */
290 /* shortcuts to regexp stuff */
295 SAVEFREEOP(oldlastpm); /* for saving regexp context during debugger */
297 regprecomp = NULL; /* uncompiled string. */
298 regparse = NULL; /* Input-scan pointer. */
299 regxend = NULL; /* End of input for compile */
300 regnpar = 0; /* () count. */
301 regcode = NULL; /* Code-emit pointer; ®dummy = don't. */
302 regsize = 0; /* Code size. */
303 regnaughty = 0; /* How bad is this pattern? */
304 regsawback = 0; /* Did we see \1, ...? */
306 reginput = NULL; /* String-input pointer. */
307 regbol = NULL; /* Beginning of input, for ^ check. */
308 regeol = NULL; /* End of input, for $ check. */
309 regstartp = (char **)NULL; /* Pointer to startp array. */
310 regendp = (char **)NULL; /* Ditto for endp. */
311 reglastparen = 0; /* Similarly for lastparen. */
312 regtill = NULL; /* How far we are required to go. */
313 regflags = 0; /* are we folding, multilining? */
314 regprev = (char)NULL; /* char before regbol, \n if none */
318 /* clean up after study() */
319 SvREFCNT_dec(lastscream);
321 Safefree(screamfirst);
323 Safefree(screamnext);
326 /* startup and shutdown function lists */
327 SvREFCNT_dec(beginav);
332 /* temp stack during pp_sort() */
333 SvREFCNT_dec(sortstack);
336 /* shortcuts just get cleared */
346 /* reset so print() ends up where we expect */
349 /* Prepare to destruct main symbol table. */
356 if (destruct_level >= 2) {
357 if (scopestack_ix != 0)
358 warn("Unbalanced scopes: %ld more ENTERs than LEAVEs\n",
359 (long)scopestack_ix);
360 if (savestack_ix != 0)
361 warn("Unbalanced saves: %ld more saves than restores\n",
363 if (tmps_floor != -1)
364 warn("Unbalanced tmps: %ld more allocs than frees\n",
365 (long)tmps_floor + 1);
366 if (cxstack_ix != -1)
367 warn("Unbalanced context: %ld more PUSHes than POPs\n",
368 (long)cxstack_ix + 1);
371 /* Now absolutely destruct everything, somehow or other, loops or no. */
373 SvFLAGS(strtab) |= SVTYPEMASK; /* don't clean out strtab now */
374 while (sv_count != 0 && sv_count != last_sv_count) {
375 last_sv_count = sv_count;
378 SvFLAGS(strtab) &= ~SVTYPEMASK;
379 SvFLAGS(strtab) |= SVt_PVHV;
381 /* Destruct the global string table. */
383 /* Yell and reset the HeVAL() slots that are still holding refcounts,
384 * so that sv_free() won't fail on them.
393 array = HvARRAY(strtab);
397 warn("Unbalanced string table refcount: (%d) for \"%s\"",
398 HeVAL(hent) - Nullsv, HeKEY(hent));
399 HeVAL(hent) = Nullsv;
409 SvREFCNT_dec(strtab);
412 warn("Scalars leaked: %ld\n", (long)sv_count);
416 /* No SVs have survived, need to clean out */
420 Safefree(origfilename);
422 hints = 0; /* Reset hints. Should hints be per-interpreter ? */
424 DEBUG_P(debprofdump());
429 PerlInterpreter *sv_interp;
431 if (!(curinterp = sv_interp))
435 #if !defined(STANDARD_C) && !defined(HAS_GETENV_PROTOTYPE)
436 char *getenv _((char *)); /* Usually in <stdlib.h> */
440 perl_parse(sv_interp, xsinit, argc, argv, env)
441 PerlInterpreter *sv_interp;
442 void (*xsinit)_((void));
449 char *scriptname = NULL;
450 VOL bool dosearch = FALSE;
455 #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
458 croak("suidperl is no longer needed since the kernel can now execute\n\
459 setuid perl scripts securely.\n");
463 if (!(curinterp = sv_interp))
466 #if defined(NeXT) && defined(__DYNAMIC__)
467 _dyld_lookup_and_bind
468 ("__environ", (unsigned long *) &environ_pointer, NULL);
473 #ifndef VMS /* VMS doesn't have environ array */
474 origenviron = environ;
480 /* Come here if running an undumped a.out. */
482 origfilename = savepv(argv[0]);
484 cxstack_ix = -1; /* start label stack again */
486 init_postdump_symbols(argc,argv,env);
491 curpad = AvARRAY(comppad);
496 SvREFCNT_dec(main_cv);
500 oldscope = scopestack_ix;
503 switch (Sigsetjmp(top_env,1)) {
508 /* my_exit() was called */
509 while (scopestack_ix > oldscope)
513 call_list(oldscope, endav);
514 return STATUS_NATIVE_EXPORT;
517 PerlIO_printf(PerlIO_stderr(), "panic: top_env\n");
521 sv_setpvn(linestr,"",0);
522 sv = newSVpv("",0); /* first used for -I flags */
525 for (argc--,argv++; argc > 0; argc--,argv++) {
526 if (argv[0][0] != '-' || !argv[0][1])
530 validarg = " PHOOEY ";
555 if (s = moreswitches(s))
565 if (euid != uid || egid != gid)
566 croak("No -e allowed in setuid scripts");
568 e_tmpname = savepv(TMPPATH);
569 (void)mktemp(e_tmpname);
571 croak("Can't mktemp()");
572 e_fp = PerlIO_open(e_tmpname,"w");
574 croak("Cannot open temporary file");
579 PerlIO_puts(e_fp,argv[1]);
583 croak("No code specified for -e");
584 (void)PerlIO_putc(e_fp,'\n');
595 incpush(argv[1], TRUE);
596 sv_catpv(sv,argv[1]);
613 preambleav = newAV();
614 av_push(preambleav, newSVpv("use Config qw(myconfig config_vars)",0));
616 Sv = newSVpv("print myconfig();",0);
618 sv_catpv(Sv,"print \"\\nCharacteristics of this PERLSHR image: \\n\",");
620 sv_catpv(Sv,"print \"\\nCharacteristics of this binary (from libperl): \\n\",");
622 #if defined(DEBUGGING) || defined(NOEMBED) || defined(MULTIPLICITY)
623 strcpy(buf,"\" Compile-time options:");
625 strcat(buf," DEBUGGING");
628 strcat(buf," NOEMBED");
631 strcat(buf," MULTIPLICITY");
633 strcat(buf,"\\n\",");
636 #if defined(LOCAL_PATCH_COUNT)
637 if (LOCAL_PATCH_COUNT > 0)
639 sv_catpv(Sv,"print \" Locally applied patches:\\n\",");
640 for (i = 1; i <= LOCAL_PATCH_COUNT; i++) {
641 if (localpatches[i]) {
642 sprintf(buf,"\" \\t%s\\n\",",localpatches[i]);
648 sprintf(buf,"\" Built under %s\\n\",",OSNAME);
652 sprintf(buf,"\" Compiled at %s %s\\n\"",__DATE__,__TIME__);
654 sprintf(buf,"\" Compiled on %s\\n\"",__DATE__);
658 sv_catpv(Sv,"; $\"=\"\\n \"; print \" \\@INC:\\n @INC\\n\"");
661 Sv = newSVpv("config_vars(qw(",0);
666 av_push(preambleav, Sv);
667 scriptname = BIT_BUCKET; /* don't look for script or read stdin */
681 croak("Unrecognized switch: -%s",s);
686 scriptname = argv[0];
688 if (PerlIO_flush(e_fp) || PerlIO_error(e_fp) || PerlIO_close(e_fp)) {
690 warn("Did you forget to compile with -DMULTIPLICITY?");
692 croak("Can't write to temp file for -e: %s", Strerror(errno));
696 scriptname = e_tmpname;
698 else if (scriptname == Nullch) {
700 if ( isatty(PerlIO_fileno(PerlIO_stdin())) )
708 open_script(scriptname,dosearch,sv);
710 validate_suid(validarg, scriptname);
715 main_cv = compcv = (CV*)NEWSV(1104,0);
716 sv_upgrade((SV *)compcv, SVt_PVCV);
720 av_push(comppad, Nullsv);
721 curpad = AvARRAY(comppad);
722 comppad_name = newAV();
723 comppad_name_fill = 0;
724 min_intro_pending = 0;
727 comppadlist = newAV();
728 AvREAL_off(comppadlist);
729 av_store(comppadlist, 0, (SV*)comppad_name);
730 av_store(comppadlist, 1, (SV*)comppad);
731 CvPADLIST(compcv) = comppadlist;
733 boot_core_UNIVERSAL();
735 (*xsinit)(); /* in case linked C routines want magical variables */
740 init_predump_symbols();
742 init_postdump_symbols(argc,argv,env);
746 /* now parse the script */
749 if (yyparse() || error_count) {
751 croak("%s had compilation errors.\n", origfilename);
753 croak("Execution of %s aborted due to compilation errors.\n",
757 curcop->cop_line = 0;
761 (void)UNLINK(e_tmpname);
766 /* now that script is parsed, we can modify record separator */
768 rs = SvREFCNT_inc(nrs);
769 sv_setsv(GvSV(gv_fetchpv("/", TRUE, SVt_PV)), rs);
780 #ifdef DEBUGGING_MSTATS
781 if ((s=getenv("PERL_DEBUG_MSTATS")) && atoi(s) >= 2)
782 dump_mstats("after compilation:");
792 PerlInterpreter *sv_interp;
796 if (!(curinterp = sv_interp))
799 oldscope = scopestack_ix;
801 switch (Sigsetjmp(top_env,1)) {
803 cxstack_ix = -1; /* start context stack again */
806 /* my_exit() was called */
807 while (scopestack_ix > oldscope)
811 call_list(oldscope, endav);
813 #ifdef DEBUGGING_MSTATS
814 if (getenv("PERL_DEBUG_MSTATS"))
815 dump_mstats("after execution: ");
817 return STATUS_NATIVE_EXPORT;
821 PerlIO_printf(PerlIO_stderr(), "panic: restartop\n");
825 if (curstack != mainstack) {
827 SWITCHSTACK(curstack, mainstack);
832 DEBUG_r(PerlIO_printf(PerlIO_stderr(), "%s $` $& $' support.\n",
833 sawampersand ? "Enabling" : "Omitting"));
837 DEBUG(PerlIO_printf(Perl_debug_log, "\nEXECUTING...\n\n"));
840 PerlIO_printf(PerlIO_stderr(), "%s syntax OK\n", origfilename);
843 if (perldb && DBsingle)
844 sv_setiv(DBsingle, 1);
854 else if (main_start) {
855 CvDEPTH(main_cv) = 1;
865 perl_get_sv(name, create)
869 GV* gv = gv_fetchpv(name, create, SVt_PV);
876 perl_get_av(name, create)
880 GV* gv = gv_fetchpv(name, create, SVt_PVAV);
889 perl_get_hv(name, create)
893 GV* gv = gv_fetchpv(name, create, SVt_PVHV);
902 perl_get_cv(name, create)
906 GV* gv = gv_fetchpv(name, create, SVt_PVCV);
907 if (create && !GvCVu(gv))
908 return newSUB(start_subparse(FALSE, 0),
909 newSVOP(OP_CONST, 0, newSVpv(name,0)),
917 /* Be sure to refetch the stack pointer after calling these routines. */
920 perl_call_argv(subname, flags, argv)
922 I32 flags; /* See G_* flags in cop.h */
923 register char **argv; /* null terminated arg list */
930 XPUSHs(sv_2mortal(newSVpv(*argv,0)));
935 return perl_call_pv(subname, flags);
939 perl_call_pv(subname, flags)
940 char *subname; /* name of the subroutine */
941 I32 flags; /* See G_* flags in cop.h */
943 return perl_call_sv((SV*)perl_get_cv(subname, TRUE), flags);
947 perl_call_method(methname, flags)
948 char *methname; /* name of the subroutine */
949 I32 flags; /* See G_* flags in cop.h */
955 XPUSHs(sv_2mortal(newSVpv(methname,0)));
958 return perl_call_sv(*stack_sp--, flags);
961 /* May be called with any of a CV, a GV, or an SV containing the name. */
963 perl_call_sv(sv, flags)
965 I32 flags; /* See G_* flags in cop.h */
967 LOGOP myop; /* fake syntax tree node */
974 bool oldmustcatch = mustcatch;
976 if (flags & G_DISCARD) {
981 Zero(&myop, 1, LOGOP);
982 if (!(flags & G_NOARGS))
983 myop.op_flags |= OPf_STACKED;
984 myop.op_next = Nullop;
985 myop.op_flags |= OPf_KNOW;
987 myop.op_flags |= OPf_LIST;
994 oldscope = scopestack_ix;
996 if (perldb && curstash != debstash
997 /* Handle first BEGIN of -d. */
998 && (DBcv || (DBcv = GvCV(DBsub)))
999 /* Try harder, since this may have been a sighandler, thus
1000 * curstash may be meaningless. */
1001 && (SvTYPE(sv) != SVt_PVCV || CvSTASH((CV*)sv) != debstash))
1002 op->op_private |= OPpENTERSUB_DB;
1004 if (flags & G_EVAL) {
1005 Copy(top_env, oldtop, 1, Sigjmp_buf);
1007 cLOGOP->op_other = op;
1009 /* we're trying to emulate pp_entertry() here */
1011 register CONTEXT *cx;
1017 push_return(op->op_next);
1018 PUSHBLOCK(cx, CXt_EVAL, stack_sp);
1020 eval_root = op; /* Only needed so that goto works right. */
1023 if (flags & G_KEEPERR)
1026 sv_setpv(GvSV(errgv),"");
1031 switch (Sigsetjmp(top_env,1)) {
1038 /* my_exit() was called */
1039 curstash = defstash;
1041 Copy(oldtop, top_env, 1, Sigjmp_buf);
1043 croak("Callback called exit");
1053 stack_sp = stack_base + oldmark;
1054 if (flags & G_ARRAY)
1058 *++stack_sp = &sv_undef;
1066 if (op == (OP*)&myop)
1070 retval = stack_sp - (stack_base + oldmark);
1071 if ((flags & G_EVAL) && !(flags & G_KEEPERR))
1072 sv_setpv(GvSV(errgv),"");
1075 if (flags & G_EVAL) {
1076 if (scopestack_ix > oldscope) {
1080 register CONTEXT *cx;
1089 Copy(oldtop, top_env, 1, Sigjmp_buf);
1092 mustcatch = oldmustcatch;
1094 if (flags & G_DISCARD) {
1095 stack_sp = stack_base + oldmark;
1103 /* Eval a string. The G_EVAL flag is always assumed. */
1106 perl_eval_sv(sv, flags)
1108 I32 flags; /* See G_* flags in cop.h */
1110 UNOP myop; /* fake syntax tree node */
1112 I32 oldmark = sp - stack_base;
1117 if (flags & G_DISCARD) {
1125 EXTEND(stack_sp, 1);
1127 oldscope = scopestack_ix;
1129 if (!(flags & G_NOARGS))
1130 myop.op_flags = OPf_STACKED;
1131 myop.op_next = Nullop;
1132 myop.op_type = OP_ENTEREVAL;
1133 myop.op_flags |= OPf_KNOW;
1134 if (flags & G_KEEPERR)
1135 myop.op_flags |= OPf_SPECIAL;
1136 if (flags & G_ARRAY)
1137 myop.op_flags |= OPf_LIST;
1139 Copy(top_env, oldtop, 1, Sigjmp_buf);
1142 switch (Sigsetjmp(top_env,1)) {
1149 /* my_exit() was called */
1150 curstash = defstash;
1152 Copy(oldtop, top_env, 1, Sigjmp_buf);
1154 croak("Callback called exit");
1164 stack_sp = stack_base + oldmark;
1165 if (flags & G_ARRAY)
1169 *++stack_sp = &sv_undef;
1174 if (op == (OP*)&myop)
1175 op = pp_entereval();
1178 retval = stack_sp - (stack_base + oldmark);
1179 if (!(flags & G_KEEPERR))
1180 sv_setpv(GvSV(errgv),"");
1183 Copy(oldtop, top_env, 1, Sigjmp_buf);
1184 if (flags & G_DISCARD) {
1185 stack_sp = stack_base + oldmark;
1193 /* Require a module. */
1199 SV* sv = sv_newmortal();
1200 sv_setpv(sv, "require '");
1203 perl_eval_sv(sv, G_DISCARD);
1207 magicname(sym,name,namlen)
1214 if (gv = gv_fetchpv(sym,TRUE, SVt_PV))
1215 sv_magic(GvSV(gv), (SV*)gv, 0, name, namlen);
1219 usage(name) /* XXX move this out into a module ? */
1222 /* This message really ought to be max 23 lines.
1223 * Removed -h because the user already knows that opton. Others? */
1224 printf("\nUsage: %s [switches] [--] [programfile] [arguments]", name);
1225 printf("\n -0[octal] specify record separator (\\0, if no argument)");
1226 printf("\n -a autosplit mode with -n or -p (splits $_ into @F)");
1227 printf("\n -c check syntax only (runs BEGIN and END blocks)");
1228 printf("\n -d[:debugger] run scripts under debugger");
1229 printf("\n -D[number/list] set debugging flags (argument is a bit mask or flags)");
1230 printf("\n -e 'command' one line of script. Several -e's allowed. Omit [programfile].");
1231 printf("\n -F/pattern/ split() pattern for autosplit (-a). The //'s are optional.");
1232 printf("\n -i[extension] edit <> files in place (make backup if extension supplied)");
1233 printf("\n -Idirectory specify @INC/#include directory (may be used more then once)");
1234 printf("\n -l[octal] enable line ending processing, specifies line teminator");
1235 printf("\n -[mM][-]module.. executes `use/no module...' before executing your script.");
1236 printf("\n -n assume 'while (<>) { ... }' loop arround your script");
1237 printf("\n -p assume loop like -n but print line also like sed");
1238 printf("\n -P run script through C preprocessor before compilation");
1239 printf("\n -s enable some switch parsing for switches after script name");
1240 printf("\n -S look for the script using PATH environment variable");
1241 printf("\n -T turn on tainting checks");
1242 printf("\n -u dump core after parsing script");
1243 printf("\n -U allow unsafe operations");
1244 printf("\n -v print version number and patchlevel of perl");
1245 printf("\n -V[:variable] print perl configuration information");
1246 printf("\n -w TURN WARNINGS ON FOR COMPILATION OF YOUR SCRIPT.");
1247 printf("\n -x[directory] strip off text before #!perl line and perhaps cd to directory\n");
1250 /* This routine handles any switches that can be given during run */
1261 rschar = scan_oct(s, 4, &numlen);
1263 if (rschar & ~((U8)~0))
1265 else if (!rschar && numlen >= 2)
1266 nrs = newSVpv("", 0);
1269 nrs = newSVpv(&ch, 1);
1274 splitstr = savepv(s + 1);
1288 if (*s == ':' || *s == '=') {
1289 sprintf(buf, "use Devel::%s;", ++s);
1291 my_setenv("PERL5DB",buf);
1301 if (isALPHA(s[1])) {
1302 static char debopts[] = "psltocPmfrxuLHXD";
1305 for (s++; *s && (d = strchr(debopts,*s)); s++)
1306 debug |= 1 << (d - debopts);
1310 for (s++; isDIGIT(*s); s++) ;
1312 debug |= 0x80000000;
1314 warn("Recompile perl with -DDEBUGGING to use -D switch\n");
1315 for (s++; isALNUM(*s); s++) ;
1325 inplace = savepv(s+1);
1327 for (s = inplace; *s && !isSPACE(*s); s++) ;
1334 for (e = s; *e && !isSPACE(*e); e++) ;
1335 p = savepvn(s, e-s);
1342 croak("No space allowed after -I");
1352 *ors = scan_oct(s, 3 + (*s == '0'), &numlen);
1361 ors = SvPV(nrs, orslen);
1362 ors = savepvn(ors, orslen);
1366 forbid_setid("-M"); /* XXX ? */
1369 forbid_setid("-m"); /* XXX ? */
1373 /* -M-foo == 'no foo' */
1374 if (*s == '-') { use = "no "; ++s; }
1375 Sv = newSVpv(use,0);
1377 /* We allow -M'Module qw(Foo Bar)' */
1378 while(isALNUM(*s) || *s==':') ++s;
1380 sv_catpv(Sv, start);
1381 if (*(start-1) == 'm') {
1383 croak("Can't use '%c' after -mname", *s);
1384 sv_catpv( Sv, " ()");
1387 sv_catpvn(Sv, start, s-start);
1388 sv_catpv(Sv, " split(/,/,q{");
1393 if (preambleav == NULL)
1394 preambleav = newAV();
1395 av_push(preambleav, Sv);
1398 croak("No space allowed after -%c", *(s-1));
1415 croak("Too late for \"-T\" option");
1427 #if defined(SUBVERSION) && SUBVERSION > 0
1428 printf("\nThis is perl, version 5.%03d_%02d", PATCHLEVEL, SUBVERSION);
1430 printf("\nThis is perl, version %s",patchlevel);
1433 printf("\n\nCopyright 1987-1997, Larry Wall\n");
1435 printf("\n\nMS-DOS port Copyright (c) 1989, 1990, Diomidis Spinellis\n");
1438 printf("djgpp v2 port (jpl5003c) by Hirofumi Watanabe, 1996\n");
1441 printf("\n\nOS/2 port Copyright (c) 1990, 1991, Raymond Chen, Kai Uwe Rommel\n"
1442 "Version 5 port Copyright (c) 1994-1997, Andreas Kaiser, Ilya Zakharevich\n");
1445 printf("atariST series port, ++jrb bammi@cadence.com\n");
1448 Perl may be copied only under the terms of either the Artistic License or the\n\
1449 GNU General Public License, which may be found in the Perl 5.0 source kit.\n\n");
1457 if (s[1] == '-') /* Additional switches on #! line. */
1465 #ifdef ALTERNATE_SHEBANG
1466 case 'S': /* OS/2 needs -S on "extproc" line. */
1474 croak("Can't emulate -%.1s on #! line",s);
1479 /* compliments of Tom Christiansen */
1481 /* unexec() can be found in the Gnu emacs distribution */
1490 sprintf (buf, "%s.perldump", origfilename);
1491 sprintf (tokenbuf, "%s/perl", BIN_EXP);
1493 status = unexec(buf, tokenbuf, &etext, sbrk(0), 0);
1495 PerlIO_printf(PerlIO_stderr(), "unexec of %s into %s failed!\n", tokenbuf, buf);
1499 # include <lib$routines.h>
1500 lib$signal(SS$_DEBUG); /* ssdef.h #included from vmsish.h */
1502 ABORT(); /* for use with undump */
1512 /* Note that strtab is a rather special HV. Assumptions are made
1513 about not iterating on it, and not adding tie magic to it.
1514 It is properly deallocated in perl_destruct() */
1516 HvSHAREKEYS_off(strtab); /* mandatory */
1517 Newz(506,((XPVHV*)SvANY(strtab))->xhv_array,
1518 sizeof(HE*) * (((XPVHV*)SvANY(strtab))->xhv_max + 1), char);
1520 curstash = defstash = newHV();
1521 curstname = newSVpv("main",4);
1522 gv = gv_fetchpv("main::",TRUE, SVt_PVHV);
1523 SvREFCNT_dec(GvHV(gv));
1524 GvHV(gv) = (HV*)SvREFCNT_inc(defstash);
1526 HvNAME(defstash) = savepv("main");
1527 incgv = gv_HVadd(gv_AVadd(gv_fetchpv("INC",TRUE, SVt_PVAV)));
1529 defgv = gv_fetchpv("_",TRUE, SVt_PVAV);
1530 errgv = gv_HVadd(gv_fetchpv("@", TRUE, SVt_PV));
1532 sv_setpvn(GvSV(errgv), "", 0);
1533 curstash = defstash;
1534 compiling.cop_stash = defstash;
1535 debstash = GvHV(gv_fetchpv("DB::", GV_ADDMULTI, SVt_PVHV));
1536 /* We must init $/ before switches are processed. */
1537 sv_setpvn(GvSV(gv_fetchpv("/", TRUE, SVt_PV)), "\n", 1);
1540 #ifdef CAN_PROTOTYPE
1542 open_script(char *scriptname, bool dosearch, SV *sv)
1545 open_script(scriptname,dosearch,sv)
1551 char *xfound = Nullch;
1552 char *xfailed = Nullch;
1556 #if defined(DOSISH) && !defined(OS2) && !defined(atarist)
1557 #define SEARCH_EXTS ".bat", ".cmd", NULL
1560 # define SEARCH_EXTS ".pl", ".com", NULL
1562 /* additional extensions to try in each dir if scriptname not found */
1564 char *ext[] = { SEARCH_EXTS };
1565 int extidx = (strchr(scriptname,'.')) ? -1 : 0; /* has ext already */
1570 int hasdir, idx = 0, deftypes = 1;
1572 hasdir = (strpbrk(scriptname,":[</") != Nullch) ;
1573 /* The first time through, just add SEARCH_EXTS to whatever we
1574 * already have, so we can check for default file types. */
1575 while (deftypes || (!hasdir && my_trnlnm("DCL$PATH",tokenbuf,idx++)) ) {
1576 if (deftypes) { deftypes = 0; *tokenbuf = '\0'; }
1577 strcat(tokenbuf,scriptname);
1579 if (dosearch && !strchr(scriptname, '/') && (s = getenv("PATH"))) {
1581 bufend = s + strlen(s);
1584 s = cpytill(tokenbuf,s,bufend,':',&len);
1587 for (len = 0; *s && *s != ',' && *s != ';'; tokenbuf[len++] = *s++);
1588 tokenbuf[len] = '\0';
1590 for (len = 0; *s && *s != ';'; tokenbuf[len++] = *s++);
1591 tokenbuf[len] = '\0';
1597 if (len && tokenbuf[len-1] != '/')
1600 if (len && ((tokenbuf[len-1] != '\\') && (tokenbuf[len-1] != '/')))
1602 if (len && tokenbuf[len-1] != '\\')
1605 (void)strcat(tokenbuf+len,"/");
1606 (void)strcat(tokenbuf+len,scriptname);
1610 len = strlen(tokenbuf);
1611 if (extidx > 0) /* reset after previous loop */
1615 DEBUG_p(PerlIO_printf(Perl_debug_log, "Looking for %s\n",tokenbuf));
1616 retval = Stat(tokenbuf,&statbuf);
1618 } while ( retval < 0 /* not there */
1619 && extidx>=0 && ext[extidx] /* try an extension? */
1620 && strcpy(tokenbuf+len, ext[extidx++])
1625 if (S_ISREG(statbuf.st_mode)
1626 && cando(S_IRUSR,TRUE,&statbuf) && cando(S_IXUSR,TRUE,&statbuf)) {
1627 xfound = tokenbuf; /* bingo! */
1631 xfailed = savepv(tokenbuf);
1634 croak("Can't execute %s", xfailed ? xfailed : scriptname );
1637 scriptname = xfound;
1640 if (strnEQ(scriptname, "/dev/fd/", 8) && isDIGIT(scriptname[8]) ) {
1641 char *s = scriptname + 8;
1650 origfilename = savepv(e_tmpname ? "-e" : scriptname);
1651 curcop->cop_filegv = gv_fetchfile(origfilename);
1652 if (strEQ(origfilename,"-"))
1654 if (fdscript >= 0) {
1655 rsfp = PerlIO_fdopen(fdscript,"r");
1656 #if defined(HAS_FCNTL) && defined(F_SETFD)
1658 fcntl(PerlIO_fileno(rsfp),F_SETFD,1); /* ensure close-on-exec */
1661 else if (preprocess) {
1662 char *cpp = CPPSTDIN;
1664 if (strEQ(cpp,"cppstdin"))
1665 sprintf(tokenbuf, "%s/%s", BIN_EXP, cpp);
1667 sprintf(tokenbuf, "%s", cpp);
1669 sv_catpv(sv,PRIVLIB_EXP);
1671 (void)sprintf(buf, "\
1672 sed %s -e \"/^[^#]/b\" \
1673 -e \"/^#[ ]*include[ ]/b\" \
1674 -e \"/^#[ ]*define[ ]/b\" \
1675 -e \"/^#[ ]*if[ ]/b\" \
1676 -e \"/^#[ ]*ifdef[ ]/b\" \
1677 -e \"/^#[ ]*ifndef[ ]/b\" \
1678 -e \"/^#[ ]*else/b\" \
1679 -e \"/^#[ ]*elif[ ]/b\" \
1680 -e \"/^#[ ]*undef[ ]/b\" \
1681 -e \"/^#[ ]*endif/b\" \
1684 (doextract ? "-e \"1,/^#/d\n\"" : ""),
1686 (void)sprintf(buf, "\
1687 %s %s -e '/^[^#]/b' \
1688 -e '/^#[ ]*include[ ]/b' \
1689 -e '/^#[ ]*define[ ]/b' \
1690 -e '/^#[ ]*if[ ]/b' \
1691 -e '/^#[ ]*ifdef[ ]/b' \
1692 -e '/^#[ ]*ifndef[ ]/b' \
1693 -e '/^#[ ]*else/b' \
1694 -e '/^#[ ]*elif[ ]/b' \
1695 -e '/^#[ ]*undef[ ]/b' \
1696 -e '/^#[ ]*endif/b' \
1704 (doextract ? "-e '1,/^#/d\n'" : ""),
1706 scriptname, tokenbuf, SvPV(sv, na), CPPMINUS);
1708 #ifdef IAMSUID /* actually, this is caught earlier */
1709 if (euid != uid && !euid) { /* if running suidperl */
1711 (void)seteuid(uid); /* musn't stay setuid root */
1714 (void)setreuid((Uid_t)-1, uid);
1716 #ifdef HAS_SETRESUID
1717 (void)setresuid((Uid_t)-1, uid, (Uid_t)-1);
1723 if (geteuid() != uid)
1724 croak("Can't do seteuid!\n");
1726 #endif /* IAMSUID */
1727 rsfp = my_popen(buf,"r");
1729 else if (!*scriptname) {
1730 forbid_setid("program input from stdin");
1731 rsfp = PerlIO_stdin();
1734 rsfp = PerlIO_open(scriptname,"r");
1735 #if defined(HAS_FCNTL) && defined(F_SETFD)
1737 fcntl(PerlIO_fileno(rsfp),F_SETFD,1); /* ensure close-on-exec */
1745 #ifndef IAMSUID /* in case script is not readable before setuid */
1746 if (euid && Stat(SvPVX(GvSV(curcop->cop_filegv)),&statbuf) >= 0 &&
1747 statbuf.st_mode & (S_ISUID|S_ISGID)) {
1748 (void)sprintf(buf, "%s/sperl%s", BIN_EXP, patchlevel);
1749 execv(buf, origargv); /* try again */
1750 croak("Can't do setuid\n");
1754 croak("Can't open perl script \"%s\": %s\n",
1755 SvPVX(GvSV(curcop->cop_filegv)), Strerror(errno));
1760 validate_suid(validarg, scriptname)
1766 /* do we need to emulate setuid on scripts? */
1768 /* This code is for those BSD systems that have setuid #! scripts disabled
1769 * in the kernel because of a security problem. Merely defining DOSUID
1770 * in perl will not fix that problem, but if you have disabled setuid
1771 * scripts in the kernel, this will attempt to emulate setuid and setgid
1772 * on scripts that have those now-otherwise-useless bits set. The setuid
1773 * root version must be called suidperl or sperlN.NNN. If regular perl
1774 * discovers that it has opened a setuid script, it calls suidperl with
1775 * the same argv that it had. If suidperl finds that the script it has
1776 * just opened is NOT setuid root, it sets the effective uid back to the
1777 * uid. We don't just make perl setuid root because that loses the
1778 * effective uid we had before invoking perl, if it was different from the
1781 * DOSUID must be defined in both perl and suidperl, and IAMSUID must
1782 * be defined in suidperl only. suidperl must be setuid root. The
1783 * Configure script will set this up for you if you want it.
1789 if (Fstat(PerlIO_fileno(rsfp),&statbuf) < 0) /* normal stat is insecure */
1790 croak("Can't stat script \"%s\"",origfilename);
1791 if (fdscript < 0 && statbuf.st_mode & (S_ISUID|S_ISGID)) {
1795 #ifndef HAS_SETREUID
1796 /* On this access check to make sure the directories are readable,
1797 * there is actually a small window that the user could use to make
1798 * filename point to an accessible directory. So there is a faint
1799 * chance that someone could execute a setuid script down in a
1800 * non-accessible directory. I don't know what to do about that.
1801 * But I don't think it's too important. The manual lies when
1802 * it says access() is useful in setuid programs.
1804 if (access(SvPVX(GvSV(curcop->cop_filegv)),1)) /*double check*/
1805 croak("Permission denied");
1807 /* If we can swap euid and uid, then we can determine access rights
1808 * with a simple stat of the file, and then compare device and
1809 * inode to make sure we did stat() on the same file we opened.
1810 * Then we just have to make sure he or she can execute it.
1813 struct stat tmpstatbuf;
1817 setreuid(euid,uid) < 0
1820 setresuid(euid,uid,(Uid_t)-1) < 0
1823 || getuid() != euid || geteuid() != uid)
1824 croak("Can't swap uid and euid"); /* really paranoid */
1825 if (Stat(SvPVX(GvSV(curcop->cop_filegv)),&tmpstatbuf) < 0)
1826 croak("Permission denied"); /* testing full pathname here */
1827 if (tmpstatbuf.st_dev != statbuf.st_dev ||
1828 tmpstatbuf.st_ino != statbuf.st_ino) {
1829 (void)PerlIO_close(rsfp);
1830 if (rsfp = my_popen("/bin/mail root","w")) { /* heh, heh */
1832 "User %ld tried to run dev %ld ino %ld in place of dev %ld ino %ld!\n\
1833 (Filename of set-id script was %s, uid %ld gid %ld.)\n\nSincerely,\nperl\n",
1834 (long)uid,(long)tmpstatbuf.st_dev, (long)tmpstatbuf.st_ino,
1835 (long)statbuf.st_dev, (long)statbuf.st_ino,
1836 SvPVX(GvSV(curcop->cop_filegv)),
1837 (long)statbuf.st_uid, (long)statbuf.st_gid);
1838 (void)my_pclose(rsfp);
1840 croak("Permission denied\n");
1844 setreuid(uid,euid) < 0
1846 # if defined(HAS_SETRESUID)
1847 setresuid(uid,euid,(Uid_t)-1) < 0
1850 || getuid() != uid || geteuid() != euid)
1851 croak("Can't reswap uid and euid");
1852 if (!cando(S_IXUSR,FALSE,&statbuf)) /* can real uid exec? */
1853 croak("Permission denied\n");
1855 #endif /* HAS_SETREUID */
1856 #endif /* IAMSUID */
1858 if (!S_ISREG(statbuf.st_mode))
1859 croak("Permission denied");
1860 if (statbuf.st_mode & S_IWOTH)
1861 croak("Setuid/gid script is writable by world");
1862 doswitches = FALSE; /* -s is insecure in suid */
1864 if (sv_gets(linestr, rsfp, 0) == Nullch ||
1865 strnNE(SvPV(linestr,na),"#!",2) ) /* required even on Sys V */
1866 croak("No #! line");
1867 s = SvPV(linestr,na)+2;
1869 while (!isSPACE(*s)) s++;
1870 for (s2 = s; (s2 > SvPV(linestr,na)+2 &&
1871 (isDIGIT(s2[-1]) || strchr("._-", s2[-1]))); s2--) ;
1872 if (strnNE(s2-4,"perl",4) && strnNE(s-9,"perl",4)) /* sanity check */
1873 croak("Not a perl script");
1874 while (*s == ' ' || *s == '\t') s++;
1876 * #! arg must be what we saw above. They can invoke it by
1877 * mentioning suidperl explicitly, but they may not add any strange
1878 * arguments beyond what #! says if they do invoke suidperl that way.
1880 len = strlen(validarg);
1881 if (strEQ(validarg," PHOOEY ") ||
1882 strnNE(s,validarg,len) || !isSPACE(s[len]))
1883 croak("Args must match #! line");
1886 if (euid != uid && (statbuf.st_mode & S_ISUID) &&
1887 euid == statbuf.st_uid)
1889 croak("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
1890 FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
1891 #endif /* IAMSUID */
1893 if (euid) { /* oops, we're not the setuid root perl */
1894 (void)PerlIO_close(rsfp);
1896 (void)sprintf(buf, "%s/sperl%s", BIN_EXP, patchlevel);
1897 execv(buf, origargv); /* try again */
1899 croak("Can't do setuid\n");
1902 if (statbuf.st_mode & S_ISGID && statbuf.st_gid != egid) {
1904 (void)setegid(statbuf.st_gid);
1907 (void)setregid((Gid_t)-1,statbuf.st_gid);
1909 #ifdef HAS_SETRESGID
1910 (void)setresgid((Gid_t)-1,statbuf.st_gid,(Gid_t)-1);
1912 setgid(statbuf.st_gid);
1916 if (getegid() != statbuf.st_gid)
1917 croak("Can't do setegid!\n");
1919 if (statbuf.st_mode & S_ISUID) {
1920 if (statbuf.st_uid != euid)
1922 (void)seteuid(statbuf.st_uid); /* all that for this */
1925 (void)setreuid((Uid_t)-1,statbuf.st_uid);
1927 #ifdef HAS_SETRESUID
1928 (void)setresuid((Uid_t)-1,statbuf.st_uid,(Uid_t)-1);
1930 setuid(statbuf.st_uid);
1934 if (geteuid() != statbuf.st_uid)
1935 croak("Can't do seteuid!\n");
1937 else if (uid) { /* oops, mustn't run as root */
1939 (void)seteuid((Uid_t)uid);
1942 (void)setreuid((Uid_t)-1,(Uid_t)uid);
1944 #ifdef HAS_SETRESUID
1945 (void)setresuid((Uid_t)-1,(Uid_t)uid,(Uid_t)-1);
1951 if (geteuid() != uid)
1952 croak("Can't do seteuid!\n");
1955 if (!cando(S_IXUSR,TRUE,&statbuf))
1956 croak("Permission denied\n"); /* they can't do this */
1959 else if (preprocess)
1960 croak("-P not allowed for setuid/setgid script\n");
1961 else if (fdscript >= 0)
1962 croak("fd script not allowed in suidperl\n");
1964 croak("Script is not setuid/setgid in suidperl\n");
1966 /* We absolutely must clear out any saved ids here, so we */
1967 /* exec the real perl, substituting fd script for scriptname. */
1968 /* (We pass script name as "subdir" of fd, which perl will grok.) */
1969 PerlIO_rewind(rsfp);
1970 lseek(PerlIO_fileno(rsfp),(Off_t)0,0); /* just in case rewind didn't */
1971 for (which = 1; origargv[which] && origargv[which] != scriptname; which++) ;
1972 if (!origargv[which])
1973 croak("Permission denied");
1974 (void)sprintf(buf, "/dev/fd/%d/%.127s", PerlIO_fileno(rsfp), origargv[which]);
1975 origargv[which] = buf;
1977 #if defined(HAS_FCNTL) && defined(F_SETFD)
1978 fcntl(PerlIO_fileno(rsfp),F_SETFD,0); /* ensure no close-on-exec */
1981 (void)sprintf(tokenbuf, "%s/perl%s", BIN_EXP, patchlevel);
1982 execv(tokenbuf, origargv); /* try again */
1983 croak("Can't do setuid\n");
1984 #endif /* IAMSUID */
1986 if (euid != uid || egid != gid) { /* (suidperl doesn't exist, in fact) */
1987 #ifndef SETUID_SCRIPTS_ARE_SECURE_NOW
1988 Fstat(PerlIO_fileno(rsfp),&statbuf); /* may be either wrapped or real suid */
1989 if ((euid != uid && euid == statbuf.st_uid && statbuf.st_mode & S_ISUID)
1991 (egid != gid && egid == statbuf.st_gid && statbuf.st_mode & S_ISGID)
1994 croak("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
1995 FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
1996 #endif /* SETUID_SCRIPTS_ARE_SECURE_NOW */
1997 /* not set-id, must be wrapped */
2005 register char *s, *s2;
2007 /* skip forward in input to the real script? */
2011 if ((s = sv_gets(linestr, rsfp, 0)) == Nullch)
2012 croak("No Perl script found in input\n");
2013 if (*s == '#' && s[1] == '!' && (s = instr(s,"perl"))) {
2014 PerlIO_ungetc(rsfp, '\n'); /* to keep line count right */
2016 while (*s && !(isSPACE (*s) || *s == '#')) s++;
2018 while (*s == ' ' || *s == '\t') s++;
2020 while (isDIGIT(s2[-1]) || strchr("-._", s2[-1])) s2--;
2021 if (strnEQ(s2-4,"perl",4))
2023 while (s = moreswitches(s)) ;
2025 if (cddir && chdir(cddir) < 0)
2026 croak("Can't chdir to %s",cddir);
2034 uid = (int)getuid();
2035 euid = (int)geteuid();
2036 gid = (int)getgid();
2037 egid = (int)getegid();
2042 tainting |= (uid && (euid != uid || egid != gid));
2050 croak("No %s allowed while running setuid", s);
2052 croak("No %s allowed while running setgid", s);
2058 curstash = debstash;
2059 dbargs = GvAV(gv_AVadd((gv_fetchpv("args", GV_ADDMULTI, SVt_PVAV))));
2061 DBgv = gv_fetchpv("DB", GV_ADDMULTI, SVt_PVGV);
2062 DBline = gv_fetchpv("dbline", GV_ADDMULTI, SVt_PVAV);
2063 DBsub = gv_HVadd(gv_fetchpv("sub", GV_ADDMULTI, SVt_PVHV));
2064 DBsingle = GvSV((gv_fetchpv("single", GV_ADDMULTI, SVt_PV)));
2065 sv_setiv(DBsingle, 0);
2066 DBtrace = GvSV((gv_fetchpv("trace", GV_ADDMULTI, SVt_PV)));
2067 sv_setiv(DBtrace, 0);
2068 DBsignal = GvSV((gv_fetchpv("signal", GV_ADDMULTI, SVt_PV)));
2069 sv_setiv(DBsignal, 0);
2070 curstash = defstash;
2077 mainstack = curstack; /* remember in case we switch stacks */
2078 AvREAL_off(curstack); /* not a real array */
2079 av_extend(curstack,127);
2081 stack_base = AvARRAY(curstack);
2082 stack_sp = stack_base;
2083 stack_max = stack_base + 127;
2085 cxstack_max = 8192 / sizeof(CONTEXT) - 2; /* Use most of 8K. */
2086 New(50,cxstack,cxstack_max + 1,CONTEXT);
2089 New(50,tmps_stack,128,SV*);
2094 New(51,debname,128,char);
2095 New(52,debdelim,128,char);
2099 * The following stacks almost certainly should be per-interpreter,
2100 * but for now they're not. XXX
2104 markstack_ptr = markstack;
2106 New(54,markstack,64,I32);
2107 markstack_ptr = markstack;
2108 markstack_max = markstack + 64;
2114 New(54,scopestack,32,I32);
2116 scopestack_max = 32;
2122 New(54,savestack,128,ANY);
2124 savestack_max = 128;
2130 New(54,retstack,16,OP*);
2140 Safefree(tmps_stack);
2147 static PerlIO *tmpfp; /* moved outside init_lexer() because of UNICOS bug */
2155 subname = newSVpv("main",4);
2159 init_predump_symbols()
2164 sv_setpvn(GvSV(gv_fetchpv("\"", TRUE, SVt_PV)), " ", 1);
2166 stdingv = gv_fetchpv("STDIN",TRUE, SVt_PVIO);
2167 GvMULTI_on(stdingv);
2168 IoIFP(GvIOp(stdingv)) = PerlIO_stdin();
2169 tmpgv = gv_fetchpv("stdin",TRUE, SVt_PV);
2171 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(GvIOp(stdingv));
2173 tmpgv = gv_fetchpv("STDOUT",TRUE, SVt_PVIO);
2175 IoOFP(GvIOp(tmpgv)) = IoIFP(GvIOp(tmpgv)) = PerlIO_stdout();
2177 tmpgv = gv_fetchpv("stdout",TRUE, SVt_PV);
2179 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(GvIOp(defoutgv));
2181 othergv = gv_fetchpv("STDERR",TRUE, SVt_PVIO);
2182 GvMULTI_on(othergv);
2183 IoOFP(GvIOp(othergv)) = IoIFP(GvIOp(othergv)) = PerlIO_stderr();
2184 tmpgv = gv_fetchpv("stderr",TRUE, SVt_PV);
2186 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(GvIOp(othergv));
2188 statname = NEWSV(66,0); /* last filename we did stat on */
2191 osname = savepv(OSNAME);
2195 init_postdump_symbols(argc,argv,env)
2197 register char **argv;
2198 register char **env;
2204 argc--,argv++; /* skip name of script */
2206 for (; argc > 0 && **argv == '-'; argc--,argv++) {
2209 if (argv[0][1] == '-') {
2213 if (s = strchr(argv[0], '=')) {
2215 sv_setpv(GvSV(gv_fetchpv(argv[0]+1,TRUE, SVt_PV)),s);
2218 sv_setiv(GvSV(gv_fetchpv(argv[0]+1,TRUE, SVt_PV)),1);
2221 toptarget = NEWSV(0,0);
2222 sv_upgrade(toptarget, SVt_PVFM);
2223 sv_setpvn(toptarget, "", 0);
2224 bodytarget = NEWSV(0,0);
2225 sv_upgrade(bodytarget, SVt_PVFM);
2226 sv_setpvn(bodytarget, "", 0);
2227 formtarget = bodytarget;
2230 if (tmpgv = gv_fetchpv("0",TRUE, SVt_PV)) {
2231 sv_setpv(GvSV(tmpgv),origfilename);
2232 magicname("0", "0", 1);
2234 if (tmpgv = gv_fetchpv("\030",TRUE, SVt_PV))
2235 sv_setpv(GvSV(tmpgv),origargv[0]);
2236 if (argvgv = gv_fetchpv("ARGV",TRUE, SVt_PVAV)) {
2238 (void)gv_AVadd(argvgv);
2239 av_clear(GvAVn(argvgv));
2240 for (; argc > 0; argc--,argv++) {
2241 av_push(GvAVn(argvgv),newSVpv(argv[0],0));
2244 if (envgv = gv_fetchpv("ENV",TRUE, SVt_PVHV)) {
2249 #ifndef VMS /* VMS doesn't have environ array */
2250 /* Note that if the supplied env parameter is actually a copy
2251 of the global environ then it may now point to free'd memory
2252 if the environment has been modified since. To avoid this
2253 problem we treat env==NULL as meaning 'use the default'
2257 if (env != environ) {
2258 environ[0] = Nullch;
2259 hv_magic(hv, envgv, 'E');
2261 for (; *env; env++) {
2262 if (!(s = strchr(*env,'=')))
2265 sv = newSVpv(s--,0);
2266 sv_magic(sv, sv, 'e', *env, s - *env);
2267 (void)hv_store(hv, *env, s - *env, sv, 0);
2271 #ifdef DYNAMIC_ENV_FETCH
2272 HvNAME(hv) = savepv(ENV_HV_NAME);
2274 hv_magic(hv, envgv, 'E');
2277 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
2278 sv_setiv(GvSV(tmpgv), (IV)getpid());
2287 s = getenv("PERL5LIB");
2291 incpush(getenv("PERLLIB"), FALSE);
2293 /* Treat PERL5?LIB as a possible search list logical name -- the
2294 * "natural" VMS idiom for a Unix path string. We allow each
2295 * element to be a set of |-separated directories for compatibility.
2299 if (my_trnlnm("PERL5LIB",buf,0))
2300 do { incpush(buf,TRUE); } while (my_trnlnm("PERL5LIB",buf,++idx));
2302 while (my_trnlnm("PERLLIB",buf,idx++)) incpush(buf,FALSE);
2306 /* Use the ~-expanded versions of APPLIB (undocumented),
2307 ARCHLIB PRIVLIB SITEARCH SITELIB and OLDARCHLIB
2310 incpush(APPLLIB_EXP, FALSE);
2314 incpush(ARCHLIB_EXP, FALSE);
2317 #define PRIVLIB_EXP "/usr/local/lib/perl5:/usr/local/lib/perl"
2319 incpush(PRIVLIB_EXP, FALSE);
2322 incpush(SITEARCH_EXP, FALSE);
2325 incpush(SITELIB_EXP, FALSE);
2327 #ifdef OLDARCHLIB_EXP /* 5.00[01] compatibility */
2328 incpush(OLDARCHLIB_EXP, FALSE);
2332 incpush(".", FALSE);
2336 # define PERLLIB_SEP ';'
2339 # define PERLLIB_SEP '|'
2341 # define PERLLIB_SEP ':'
2344 #ifndef PERLLIB_MANGLE
2345 # define PERLLIB_MANGLE(s,n) (s)
2349 incpush(p, addsubdirs)
2353 SV *subdir = Nullsv;
2354 static char *archpat_auto;
2361 if (!archpat_auto) {
2362 STRLEN len = (sizeof(ARCHNAME) + strlen(patchlevel)
2363 + sizeof("//auto"));
2364 New(55, archpat_auto, len, char);
2365 sprintf(archpat_auto, "/%s/%s/auto", ARCHNAME, patchlevel);
2367 for (len = sizeof(ARCHNAME) + 2;
2368 archpat_auto[len] != '\0' && archpat_auto[len] != '/'; len++)
2369 if (archpat_auto[len] == '.') archpat_auto[len] = '_';
2374 /* Break at all separators */
2376 SV *libdir = newSV(0);
2379 /* skip any consecutive separators */
2380 while ( *p == PERLLIB_SEP ) {
2381 /* Uncomment the next line for PATH semantics */
2382 /* av_push(GvAVn(incgv), newSVpv(".", 1)); */
2386 if ( (s = strchr(p, PERLLIB_SEP)) != Nullch ) {
2387 sv_setpvn(libdir, PERLLIB_MANGLE(p, (STRLEN)(s - p)),
2392 sv_setpv(libdir, PERLLIB_MANGLE(p, 0));
2393 p = Nullch; /* break out */
2397 * BEFORE pushing libdir onto @INC we may first push version- and
2398 * archname-specific sub-directories.
2401 struct stat tmpstatbuf;
2406 if ((unix = tounixspec_ts(SvPV(libdir,na),Nullch)) != Nullch) {
2408 while (unix[len-1] == '/') len--; /* Cosmetic */
2409 sv_usepvn(libdir,unix,len);
2412 PerlIO_printf(PerlIO_stderr(),
2413 "Failed to unixify @INC element \"%s\"\n",
2416 /* .../archname/version if -d .../archname/version/auto */
2417 sv_setsv(subdir, libdir);
2418 sv_catpv(subdir, archpat_auto);
2419 if (Stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
2420 S_ISDIR(tmpstatbuf.st_mode))
2421 av_push(GvAVn(incgv),
2422 newSVpv(SvPVX(subdir), SvCUR(subdir) - sizeof "auto"));
2424 /* .../archname if -d .../archname/auto */
2425 sv_insert(subdir, SvCUR(libdir) + sizeof(ARCHNAME),
2426 strlen(patchlevel) + 1, "", 0);
2427 if (Stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
2428 S_ISDIR(tmpstatbuf.st_mode))
2429 av_push(GvAVn(incgv),
2430 newSVpv(SvPVX(subdir), SvCUR(subdir) - sizeof "auto"));
2433 /* finally push this lib directory on the end of @INC */
2434 av_push(GvAVn(incgv), libdir);
2437 SvREFCNT_dec(subdir);
2441 call_list(oldscope, list)
2447 line_t oldline = curcop->cop_line;
2449 Copy(top_env, oldtop, 1, Sigjmp_buf);
2451 while (AvFILL(list) >= 0) {
2452 CV *cv = (CV*)av_shift(list);
2456 switch (Sigsetjmp(top_env,1)) {
2458 SV* atsv = GvSV(errgv);
2460 perl_call_sv((SV*)cv, G_EVAL|G_DISCARD);
2461 (void)SvPV(atsv, len);
2463 Copy(oldtop, top_env, 1, Sigjmp_buf);
2464 curcop = &compiling;
2465 curcop->cop_line = oldline;
2466 if (list == beginav)
2467 sv_catpv(atsv, "BEGIN failed--compilation aborted");
2469 sv_catpv(atsv, "END failed--cleanup aborted");
2470 while (scopestack_ix > oldscope)
2472 croak("%s", SvPVX(atsv));
2480 /* my_exit() was called */
2481 while (scopestack_ix > oldscope)
2483 curstash = defstash;
2485 call_list(oldscope, endav);
2487 Copy(oldtop, top_env, 1, Sigjmp_buf);
2488 curcop = &compiling;
2489 curcop->cop_line = oldline;
2491 if (list == beginav)
2492 croak("BEGIN failed--compilation aborted");
2494 croak("END failed--cleanup aborted");
2500 PerlIO_printf(PerlIO_stderr(), "panic: restartop\n");
2504 Copy(oldtop, top_env, 1, Sigjmp_buf);
2505 curcop = &compiling;
2506 curcop->cop_line = oldline;
2507 Siglongjmp(top_env, 3);
2511 Copy(oldtop, top_env, 1, Sigjmp_buf);
2526 STATUS_NATIVE_SET(status);
2536 if (vaxc$errno & 1) {
2537 if (STATUS_NATIVE & 1) /* fortuitiously includes "-1" */
2538 STATUS_NATIVE_SET(44);
2541 if (!vaxc$errno && errno) /* unlikely */
2542 STATUS_NATIVE_SET(44);
2544 STATUS_NATIVE_SET(vaxc$errno);
2548 STATUS_POSIX_SET(errno);
2549 else if (STATUS_POSIX == 0)
2550 STATUS_POSIX_SET(255);
2558 register CONTEXT *cx;
2567 (void)UNLINK(e_tmpname);
2568 Safefree(e_tmpname);
2572 if (cxstack_ix >= 0) {
2579 Siglongjmp(top_env, 2);