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 calllist(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 calllist(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) {
986 myop.op_flags |= OPf_STACKED;
987 myop.op_next = Nullop;
988 myop.op_flags |= OPf_KNOW;
990 myop.op_flags |= OPf_LIST;
997 oldscope = scopestack_ix;
999 if (perldb && curstash != debstash
1000 /* Handle first BEGIN of -d. */
1001 && (DBcv || (DBcv = GvCV(DBsub)))
1002 /* Try harder, since this may have been a sighandler, thus
1003 * curstash may be meaningless. */
1004 && (SvTYPE(sv) != SVt_PVCV || CvSTASH((CV*)sv) != debstash))
1005 op->op_private |= OPpENTERSUB_DB;
1007 if (flags & G_EVAL) {
1008 Copy(top_env, oldtop, 1, Sigjmp_buf);
1010 cLOGOP->op_other = op;
1012 /* we're trying to emulate pp_entertry() here */
1014 register CONTEXT *cx;
1020 push_return(op->op_next);
1021 PUSHBLOCK(cx, CXt_EVAL, stack_sp);
1023 eval_root = op; /* Only needed so that goto works right. */
1026 if (flags & G_KEEPERR)
1029 sv_setpv(GvSV(errgv),"");
1034 switch (Sigsetjmp(top_env,1)) {
1041 /* my_exit() was called */
1042 curstash = defstash;
1044 Copy(oldtop, top_env, 1, Sigjmp_buf);
1046 croak("Callback called exit");
1056 stack_sp = stack_base + oldmark;
1057 if (flags & G_ARRAY)
1061 *++stack_sp = &sv_undef;
1069 if (op == (OP*)&myop)
1073 retval = stack_sp - (stack_base + oldmark);
1074 if ((flags & G_EVAL) && !(flags & G_KEEPERR))
1075 sv_setpv(GvSV(errgv),"");
1078 if (flags & G_EVAL) {
1079 if (scopestack_ix > oldscope) {
1083 register CONTEXT *cx;
1092 Copy(oldtop, top_env, 1, Sigjmp_buf);
1095 mustcatch = oldmustcatch;
1097 if (flags & G_DISCARD) {
1098 stack_sp = stack_base + oldmark;
1106 /* Eval a string. The G_EVAL flag is always assumed. */
1109 perl_eval_sv(sv, flags)
1111 I32 flags; /* See G_* flags in cop.h */
1113 UNOP myop; /* fake syntax tree node */
1115 I32 oldmark = sp - stack_base;
1120 if (flags & G_DISCARD) {
1128 EXTEND(stack_sp, 1);
1130 oldscope = scopestack_ix;
1132 if (!(flags & G_NOARGS))
1133 myop.op_flags = OPf_STACKED;
1134 myop.op_next = Nullop;
1135 myop.op_type = OP_ENTEREVAL;
1136 myop.op_flags |= OPf_KNOW;
1137 if (flags & G_KEEPERR)
1138 myop.op_flags |= OPf_SPECIAL;
1139 if (flags & G_ARRAY)
1140 myop.op_flags |= OPf_LIST;
1142 Copy(top_env, oldtop, 1, Sigjmp_buf);
1145 switch (Sigsetjmp(top_env,1)) {
1152 /* my_exit() was called */
1153 curstash = defstash;
1155 Copy(oldtop, top_env, 1, Sigjmp_buf);
1157 croak("Callback called exit");
1167 stack_sp = stack_base + oldmark;
1168 if (flags & G_ARRAY)
1172 *++stack_sp = &sv_undef;
1177 if (op == (OP*)&myop)
1178 op = pp_entereval();
1181 retval = stack_sp - (stack_base + oldmark);
1182 if (!(flags & G_KEEPERR))
1183 sv_setpv(GvSV(errgv),"");
1186 Copy(oldtop, top_env, 1, Sigjmp_buf);
1187 if (flags & G_DISCARD) {
1188 stack_sp = stack_base + oldmark;
1196 /* Require a module. */
1202 SV* sv = sv_newmortal();
1203 sv_setpv(sv, "require '");
1206 perl_eval_sv(sv, G_DISCARD);
1210 magicname(sym,name,namlen)
1217 if (gv = gv_fetchpv(sym,TRUE, SVt_PV))
1218 sv_magic(GvSV(gv), (SV*)gv, 0, name, namlen);
1222 usage(name) /* XXX move this out into a module ? */
1225 /* This message really ought to be max 23 lines.
1226 * Removed -h because the user already knows that opton. Others? */
1227 printf("\nUsage: %s [switches] [--] [programfile] [arguments]", name);
1228 printf("\n -0[octal] specify record separator (\\0, if no argument)");
1229 printf("\n -a autosplit mode with -n or -p (splits $_ into @F)");
1230 printf("\n -c check syntax only (runs BEGIN and END blocks)");
1231 printf("\n -d[:debugger] run scripts under debugger");
1232 printf("\n -D[number/list] set debugging flags (argument is a bit mask or flags)");
1233 printf("\n -e 'command' one line of script. Several -e's allowed. Omit [programfile].");
1234 printf("\n -F/pattern/ split() pattern for autosplit (-a). The //'s are optional.");
1235 printf("\n -i[extension] edit <> files in place (make backup if extension supplied)");
1236 printf("\n -Idirectory specify @INC/#include directory (may be used more then once)");
1237 printf("\n -l[octal] enable line ending processing, specifies line teminator");
1238 printf("\n -[mM][-]module.. executes `use/no module...' before executing your script.");
1239 printf("\n -n assume 'while (<>) { ... }' loop arround your script");
1240 printf("\n -p assume loop like -n but print line also like sed");
1241 printf("\n -P run script through C preprocessor before compilation");
1242 printf("\n -s enable some switch parsing for switches after script name");
1243 printf("\n -S look for the script using PATH environment variable");
1244 printf("\n -T turn on tainting checks");
1245 printf("\n -u dump core after parsing script");
1246 printf("\n -U allow unsafe operations");
1247 printf("\n -v print version number and patchlevel of perl");
1248 printf("\n -V[:variable] print perl configuration information");
1249 printf("\n -w TURN WARNINGS ON FOR COMPILATION OF YOUR SCRIPT.");
1250 printf("\n -x[directory] strip off text before #!perl line and perhaps cd to directory\n");
1253 /* This routine handles any switches that can be given during run */
1264 rschar = scan_oct(s, 4, &numlen);
1266 if (rschar & ~((U8)~0))
1268 else if (!rschar && numlen >= 2)
1269 nrs = newSVpv("", 0);
1272 nrs = newSVpv(&ch, 1);
1277 splitstr = savepv(s + 1);
1291 if (*s == ':' || *s == '=') {
1292 sprintf(buf, "use Devel::%s;", ++s);
1294 my_setenv("PERL5DB",buf);
1304 if (isALPHA(s[1])) {
1305 static char debopts[] = "psltocPmfrxuLHXD";
1308 for (s++; *s && (d = strchr(debopts,*s)); s++)
1309 debug |= 1 << (d - debopts);
1313 for (s++; isDIGIT(*s); s++) ;
1315 debug |= 0x80000000;
1317 warn("Recompile perl with -DDEBUGGING to use -D switch\n");
1318 for (s++; isALNUM(*s); s++) ;
1328 inplace = savepv(s+1);
1330 for (s = inplace; *s && !isSPACE(*s); s++) ;
1337 for (e = s; *e && !isSPACE(*e); e++) ;
1338 p = savepvn(s, e-s);
1345 croak("No space allowed after -I");
1355 *ors = scan_oct(s, 3 + (*s == '0'), &numlen);
1364 ors = SvPV(nrs, orslen);
1365 ors = savepvn(ors, orslen);
1369 forbid_setid("-M"); /* XXX ? */
1372 forbid_setid("-m"); /* XXX ? */
1376 /* -M-foo == 'no foo' */
1377 if (*s == '-') { use = "no "; ++s; }
1378 Sv = newSVpv(use,0);
1380 /* We allow -M'Module qw(Foo Bar)' */
1381 while(isALNUM(*s) || *s==':') ++s;
1383 sv_catpv(Sv, start);
1384 if (*(start-1) == 'm') {
1386 croak("Can't use '%c' after -mname", *s);
1387 sv_catpv( Sv, " ()");
1390 sv_catpvn(Sv, start, s-start);
1391 sv_catpv(Sv, " split(/,/,q{");
1396 if (preambleav == NULL)
1397 preambleav = newAV();
1398 av_push(preambleav, Sv);
1401 croak("No space allowed after -%c", *(s-1));
1418 croak("Too late for \"-T\" option");
1430 #if defined(SUBVERSION) && SUBVERSION > 0
1431 printf("\nThis is perl, version 5.%03d_%02d", PATCHLEVEL, SUBVERSION);
1433 printf("\nThis is perl, version %s",patchlevel);
1436 printf("\n\nCopyright 1987-1997, Larry Wall\n");
1438 printf("\n\nMS-DOS port Copyright (c) 1989, 1990, Diomidis Spinellis\n");
1441 printf("djgpp v2 port (jpl5003c) by Hirofumi Watanabe, 1996\n");
1444 printf("\n\nOS/2 port Copyright (c) 1990, 1991, Raymond Chen, Kai Uwe Rommel\n"
1445 "Version 5 port Copyright (c) 1994-1997, Andreas Kaiser, Ilya Zakharevich\n");
1448 printf("atariST series port, ++jrb bammi@cadence.com\n");
1451 Perl may be copied only under the terms of either the Artistic License or the\n\
1452 GNU General Public License, which may be found in the Perl 5.0 source kit.\n\n");
1460 if (s[1] == '-') /* Additional switches on #! line. */
1468 #ifdef ALTERNATE_SHEBANG
1469 case 'S': /* OS/2 needs -S on "extproc" line. */
1477 croak("Can't emulate -%.1s on #! line",s);
1482 /* compliments of Tom Christiansen */
1484 /* unexec() can be found in the Gnu emacs distribution */
1493 sprintf (buf, "%s.perldump", origfilename);
1494 sprintf (tokenbuf, "%s/perl", BIN_EXP);
1496 status = unexec(buf, tokenbuf, &etext, sbrk(0), 0);
1498 PerlIO_printf(PerlIO_stderr(), "unexec of %s into %s failed!\n", tokenbuf, buf);
1502 # include <lib$routines.h>
1503 lib$signal(SS$_DEBUG); /* ssdef.h #included from vmsish.h */
1505 ABORT(); /* for use with undump */
1515 /* Note that strtab is a rather special HV. Assumptions are made
1516 about not iterating on it, and not adding tie magic to it.
1517 It is properly deallocated in perl_destruct() */
1519 HvSHAREKEYS_off(strtab); /* mandatory */
1520 Newz(506,((XPVHV*)SvANY(strtab))->xhv_array,
1521 sizeof(HE*) * (((XPVHV*)SvANY(strtab))->xhv_max + 1), char);
1523 curstash = defstash = newHV();
1524 curstname = newSVpv("main",4);
1525 gv = gv_fetchpv("main::",TRUE, SVt_PVHV);
1526 SvREFCNT_dec(GvHV(gv));
1527 GvHV(gv) = (HV*)SvREFCNT_inc(defstash);
1529 HvNAME(defstash) = savepv("main");
1530 incgv = gv_HVadd(gv_AVadd(gv_fetchpv("INC",TRUE, SVt_PVAV)));
1532 defgv = gv_fetchpv("_",TRUE, SVt_PVAV);
1533 errgv = gv_HVadd(gv_fetchpv("@", TRUE, SVt_PV));
1535 sv_setpvn(GvSV(errgv), "", 0);
1536 curstash = defstash;
1537 compiling.cop_stash = defstash;
1538 debstash = GvHV(gv_fetchpv("DB::", GV_ADDMULTI, SVt_PVHV));
1539 /* We must init $/ before switches are processed. */
1540 sv_setpvn(GvSV(gv_fetchpv("/", TRUE, SVt_PV)), "\n", 1);
1543 #ifdef CAN_PROTOTYPE
1545 open_script(char *scriptname, bool dosearch, SV *sv)
1548 open_script(scriptname,dosearch,sv)
1554 char *xfound = Nullch;
1555 char *xfailed = Nullch;
1559 #if defined(DOSISH) && !defined(OS2) && !defined(atarist)
1560 #define SEARCH_EXTS ".bat", ".cmd", NULL
1563 # define SEARCH_EXTS ".pl", ".com", NULL
1565 /* additional extensions to try in each dir if scriptname not found */
1567 char *ext[] = { SEARCH_EXTS };
1568 int extidx = (strchr(scriptname,'.')) ? -1 : 0; /* has ext already */
1573 int hasdir, idx = 0, deftypes = 1;
1575 hasdir = (strpbrk(scriptname,":[</") != Nullch) ;
1576 /* The first time through, just add SEARCH_EXTS to whatever we
1577 * already have, so we can check for default file types. */
1578 while (deftypes || (!hasdir && my_trnlnm("DCL$PATH",tokenbuf,idx++)) ) {
1579 if (deftypes) { deftypes = 0; *tokenbuf = '\0'; }
1580 strcat(tokenbuf,scriptname);
1582 if (dosearch && !strchr(scriptname, '/') && (s = getenv("PATH"))) {
1584 bufend = s + strlen(s);
1587 s = cpytill(tokenbuf,s,bufend,':',&len);
1590 for (len = 0; *s && *s != ',' && *s != ';'; tokenbuf[len++] = *s++);
1591 tokenbuf[len] = '\0';
1593 for (len = 0; *s && *s != ';'; tokenbuf[len++] = *s++);
1594 tokenbuf[len] = '\0';
1600 if (len && tokenbuf[len-1] != '/')
1603 if (len && ((tokenbuf[len-1] != '\\') && (tokenbuf[len-1] != '/')))
1605 if (len && tokenbuf[len-1] != '\\')
1608 (void)strcat(tokenbuf+len,"/");
1609 (void)strcat(tokenbuf+len,scriptname);
1613 len = strlen(tokenbuf);
1614 if (extidx > 0) /* reset after previous loop */
1618 DEBUG_p(PerlIO_printf(Perl_debug_log, "Looking for %s\n",tokenbuf));
1619 retval = Stat(tokenbuf,&statbuf);
1621 } while ( retval < 0 /* not there */
1622 && extidx>=0 && ext[extidx] /* try an extension? */
1623 && strcpy(tokenbuf+len, ext[extidx++])
1628 if (S_ISREG(statbuf.st_mode)
1629 && cando(S_IRUSR,TRUE,&statbuf) && cando(S_IXUSR,TRUE,&statbuf)) {
1630 xfound = tokenbuf; /* bingo! */
1634 xfailed = savepv(tokenbuf);
1637 croak("Can't execute %s", xfailed ? xfailed : scriptname );
1640 scriptname = xfound;
1643 if (strnEQ(scriptname, "/dev/fd/", 8) && isDIGIT(scriptname[8]) ) {
1644 char *s = scriptname + 8;
1653 origfilename = savepv(e_tmpname ? "-e" : scriptname);
1654 curcop->cop_filegv = gv_fetchfile(origfilename);
1655 if (strEQ(origfilename,"-"))
1657 if (fdscript >= 0) {
1658 rsfp = PerlIO_fdopen(fdscript,"r");
1659 #if defined(HAS_FCNTL) && defined(F_SETFD)
1661 fcntl(PerlIO_fileno(rsfp),F_SETFD,1); /* ensure close-on-exec */
1664 else if (preprocess) {
1665 char *cpp = CPPSTDIN;
1667 if (strEQ(cpp,"cppstdin"))
1668 sprintf(tokenbuf, "%s/%s", BIN_EXP, cpp);
1670 sprintf(tokenbuf, "%s", cpp);
1672 sv_catpv(sv,PRIVLIB_EXP);
1674 (void)sprintf(buf, "\
1675 sed %s -e \"/^[^#]/b\" \
1676 -e \"/^#[ ]*include[ ]/b\" \
1677 -e \"/^#[ ]*define[ ]/b\" \
1678 -e \"/^#[ ]*if[ ]/b\" \
1679 -e \"/^#[ ]*ifdef[ ]/b\" \
1680 -e \"/^#[ ]*ifndef[ ]/b\" \
1681 -e \"/^#[ ]*else/b\" \
1682 -e \"/^#[ ]*elif[ ]/b\" \
1683 -e \"/^#[ ]*undef[ ]/b\" \
1684 -e \"/^#[ ]*endif/b\" \
1687 (doextract ? "-e \"1,/^#/d\n\"" : ""),
1689 (void)sprintf(buf, "\
1690 %s %s -e '/^[^#]/b' \
1691 -e '/^#[ ]*include[ ]/b' \
1692 -e '/^#[ ]*define[ ]/b' \
1693 -e '/^#[ ]*if[ ]/b' \
1694 -e '/^#[ ]*ifdef[ ]/b' \
1695 -e '/^#[ ]*ifndef[ ]/b' \
1696 -e '/^#[ ]*else/b' \
1697 -e '/^#[ ]*elif[ ]/b' \
1698 -e '/^#[ ]*undef[ ]/b' \
1699 -e '/^#[ ]*endif/b' \
1707 (doextract ? "-e '1,/^#/d\n'" : ""),
1709 scriptname, tokenbuf, SvPV(sv, na), CPPMINUS);
1711 #ifdef IAMSUID /* actually, this is caught earlier */
1712 if (euid != uid && !euid) { /* if running suidperl */
1714 (void)seteuid(uid); /* musn't stay setuid root */
1717 (void)setreuid((Uid_t)-1, uid);
1719 #ifdef HAS_SETRESUID
1720 (void)setresuid((Uid_t)-1, uid, (Uid_t)-1);
1726 if (geteuid() != uid)
1727 croak("Can't do seteuid!\n");
1729 #endif /* IAMSUID */
1730 rsfp = my_popen(buf,"r");
1732 else if (!*scriptname) {
1733 forbid_setid("program input from stdin");
1734 rsfp = PerlIO_stdin();
1737 rsfp = PerlIO_open(scriptname,"r");
1738 #if defined(HAS_FCNTL) && defined(F_SETFD)
1740 fcntl(PerlIO_fileno(rsfp),F_SETFD,1); /* ensure close-on-exec */
1748 #ifndef IAMSUID /* in case script is not readable before setuid */
1749 if (euid && Stat(SvPVX(GvSV(curcop->cop_filegv)),&statbuf) >= 0 &&
1750 statbuf.st_mode & (S_ISUID|S_ISGID)) {
1751 (void)sprintf(buf, "%s/sperl%s", BIN_EXP, patchlevel);
1752 execv(buf, origargv); /* try again */
1753 croak("Can't do setuid\n");
1757 croak("Can't open perl script \"%s\": %s\n",
1758 SvPVX(GvSV(curcop->cop_filegv)), Strerror(errno));
1763 validate_suid(validarg, scriptname)
1769 /* do we need to emulate setuid on scripts? */
1771 /* This code is for those BSD systems that have setuid #! scripts disabled
1772 * in the kernel because of a security problem. Merely defining DOSUID
1773 * in perl will not fix that problem, but if you have disabled setuid
1774 * scripts in the kernel, this will attempt to emulate setuid and setgid
1775 * on scripts that have those now-otherwise-useless bits set. The setuid
1776 * root version must be called suidperl or sperlN.NNN. If regular perl
1777 * discovers that it has opened a setuid script, it calls suidperl with
1778 * the same argv that it had. If suidperl finds that the script it has
1779 * just opened is NOT setuid root, it sets the effective uid back to the
1780 * uid. We don't just make perl setuid root because that loses the
1781 * effective uid we had before invoking perl, if it was different from the
1784 * DOSUID must be defined in both perl and suidperl, and IAMSUID must
1785 * be defined in suidperl only. suidperl must be setuid root. The
1786 * Configure script will set this up for you if you want it.
1792 if (Fstat(PerlIO_fileno(rsfp),&statbuf) < 0) /* normal stat is insecure */
1793 croak("Can't stat script \"%s\"",origfilename);
1794 if (fdscript < 0 && statbuf.st_mode & (S_ISUID|S_ISGID)) {
1798 #ifndef HAS_SETREUID
1799 /* On this access check to make sure the directories are readable,
1800 * there is actually a small window that the user could use to make
1801 * filename point to an accessible directory. So there is a faint
1802 * chance that someone could execute a setuid script down in a
1803 * non-accessible directory. I don't know what to do about that.
1804 * But I don't think it's too important. The manual lies when
1805 * it says access() is useful in setuid programs.
1807 if (access(SvPVX(GvSV(curcop->cop_filegv)),1)) /*double check*/
1808 croak("Permission denied");
1810 /* If we can swap euid and uid, then we can determine access rights
1811 * with a simple stat of the file, and then compare device and
1812 * inode to make sure we did stat() on the same file we opened.
1813 * Then we just have to make sure he or she can execute it.
1816 struct stat tmpstatbuf;
1820 setreuid(euid,uid) < 0
1823 setresuid(euid,uid,(Uid_t)-1) < 0
1826 || getuid() != euid || geteuid() != uid)
1827 croak("Can't swap uid and euid"); /* really paranoid */
1828 if (Stat(SvPVX(GvSV(curcop->cop_filegv)),&tmpstatbuf) < 0)
1829 croak("Permission denied"); /* testing full pathname here */
1830 if (tmpstatbuf.st_dev != statbuf.st_dev ||
1831 tmpstatbuf.st_ino != statbuf.st_ino) {
1832 (void)PerlIO_close(rsfp);
1833 if (rsfp = my_popen("/bin/mail root","w")) { /* heh, heh */
1835 "User %ld tried to run dev %ld ino %ld in place of dev %ld ino %ld!\n\
1836 (Filename of set-id script was %s, uid %ld gid %ld.)\n\nSincerely,\nperl\n",
1837 (long)uid,(long)tmpstatbuf.st_dev, (long)tmpstatbuf.st_ino,
1838 (long)statbuf.st_dev, (long)statbuf.st_ino,
1839 SvPVX(GvSV(curcop->cop_filegv)),
1840 (long)statbuf.st_uid, (long)statbuf.st_gid);
1841 (void)my_pclose(rsfp);
1843 croak("Permission denied\n");
1847 setreuid(uid,euid) < 0
1849 # if defined(HAS_SETRESUID)
1850 setresuid(uid,euid,(Uid_t)-1) < 0
1853 || getuid() != uid || geteuid() != euid)
1854 croak("Can't reswap uid and euid");
1855 if (!cando(S_IXUSR,FALSE,&statbuf)) /* can real uid exec? */
1856 croak("Permission denied\n");
1858 #endif /* HAS_SETREUID */
1859 #endif /* IAMSUID */
1861 if (!S_ISREG(statbuf.st_mode))
1862 croak("Permission denied");
1863 if (statbuf.st_mode & S_IWOTH)
1864 croak("Setuid/gid script is writable by world");
1865 doswitches = FALSE; /* -s is insecure in suid */
1867 if (sv_gets(linestr, rsfp, 0) == Nullch ||
1868 strnNE(SvPV(linestr,na),"#!",2) ) /* required even on Sys V */
1869 croak("No #! line");
1870 s = SvPV(linestr,na)+2;
1872 while (!isSPACE(*s)) s++;
1873 for (s2 = s; (s2 > SvPV(linestr,na)+2 &&
1874 (isDIGIT(s2[-1]) || strchr("._-", s2[-1]))); s2--) ;
1875 if (strnNE(s2-4,"perl",4) && strnNE(s-9,"perl",4)) /* sanity check */
1876 croak("Not a perl script");
1877 while (*s == ' ' || *s == '\t') s++;
1879 * #! arg must be what we saw above. They can invoke it by
1880 * mentioning suidperl explicitly, but they may not add any strange
1881 * arguments beyond what #! says if they do invoke suidperl that way.
1883 len = strlen(validarg);
1884 if (strEQ(validarg," PHOOEY ") ||
1885 strnNE(s,validarg,len) || !isSPACE(s[len]))
1886 croak("Args must match #! line");
1889 if (euid != uid && (statbuf.st_mode & S_ISUID) &&
1890 euid == statbuf.st_uid)
1892 croak("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
1893 FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
1894 #endif /* IAMSUID */
1896 if (euid) { /* oops, we're not the setuid root perl */
1897 (void)PerlIO_close(rsfp);
1899 (void)sprintf(buf, "%s/sperl%s", BIN_EXP, patchlevel);
1900 execv(buf, origargv); /* try again */
1902 croak("Can't do setuid\n");
1905 if (statbuf.st_mode & S_ISGID && statbuf.st_gid != egid) {
1907 (void)setegid(statbuf.st_gid);
1910 (void)setregid((Gid_t)-1,statbuf.st_gid);
1912 #ifdef HAS_SETRESGID
1913 (void)setresgid((Gid_t)-1,statbuf.st_gid,(Gid_t)-1);
1915 setgid(statbuf.st_gid);
1919 if (getegid() != statbuf.st_gid)
1920 croak("Can't do setegid!\n");
1922 if (statbuf.st_mode & S_ISUID) {
1923 if (statbuf.st_uid != euid)
1925 (void)seteuid(statbuf.st_uid); /* all that for this */
1928 (void)setreuid((Uid_t)-1,statbuf.st_uid);
1930 #ifdef HAS_SETRESUID
1931 (void)setresuid((Uid_t)-1,statbuf.st_uid,(Uid_t)-1);
1933 setuid(statbuf.st_uid);
1937 if (geteuid() != statbuf.st_uid)
1938 croak("Can't do seteuid!\n");
1940 else if (uid) { /* oops, mustn't run as root */
1942 (void)seteuid((Uid_t)uid);
1945 (void)setreuid((Uid_t)-1,(Uid_t)uid);
1947 #ifdef HAS_SETRESUID
1948 (void)setresuid((Uid_t)-1,(Uid_t)uid,(Uid_t)-1);
1954 if (geteuid() != uid)
1955 croak("Can't do seteuid!\n");
1958 if (!cando(S_IXUSR,TRUE,&statbuf))
1959 croak("Permission denied\n"); /* they can't do this */
1962 else if (preprocess)
1963 croak("-P not allowed for setuid/setgid script\n");
1964 else if (fdscript >= 0)
1965 croak("fd script not allowed in suidperl\n");
1967 croak("Script is not setuid/setgid in suidperl\n");
1969 /* We absolutely must clear out any saved ids here, so we */
1970 /* exec the real perl, substituting fd script for scriptname. */
1971 /* (We pass script name as "subdir" of fd, which perl will grok.) */
1972 PerlIO_rewind(rsfp);
1973 lseek(PerlIO_fileno(rsfp),(Off_t)0,0); /* just in case rewind didn't */
1974 for (which = 1; origargv[which] && origargv[which] != scriptname; which++) ;
1975 if (!origargv[which])
1976 croak("Permission denied");
1977 (void)sprintf(buf, "/dev/fd/%d/%.127s", PerlIO_fileno(rsfp), origargv[which]);
1978 origargv[which] = buf;
1980 #if defined(HAS_FCNTL) && defined(F_SETFD)
1981 fcntl(PerlIO_fileno(rsfp),F_SETFD,0); /* ensure no close-on-exec */
1984 (void)sprintf(tokenbuf, "%s/perl%s", BIN_EXP, patchlevel);
1985 execv(tokenbuf, origargv); /* try again */
1986 croak("Can't do setuid\n");
1987 #endif /* IAMSUID */
1989 if (euid != uid || egid != gid) { /* (suidperl doesn't exist, in fact) */
1990 #ifndef SETUID_SCRIPTS_ARE_SECURE_NOW
1991 Fstat(PerlIO_fileno(rsfp),&statbuf); /* may be either wrapped or real suid */
1992 if ((euid != uid && euid == statbuf.st_uid && statbuf.st_mode & S_ISUID)
1994 (egid != gid && egid == statbuf.st_gid && statbuf.st_mode & S_ISGID)
1997 croak("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
1998 FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
1999 #endif /* SETUID_SCRIPTS_ARE_SECURE_NOW */
2000 /* not set-id, must be wrapped */
2008 register char *s, *s2;
2010 /* skip forward in input to the real script? */
2014 if ((s = sv_gets(linestr, rsfp, 0)) == Nullch)
2015 croak("No Perl script found in input\n");
2016 if (*s == '#' && s[1] == '!' && (s = instr(s,"perl"))) {
2017 PerlIO_ungetc(rsfp, '\n'); /* to keep line count right */
2019 while (*s && !(isSPACE (*s) || *s == '#')) s++;
2021 while (*s == ' ' || *s == '\t') s++;
2023 while (isDIGIT(s2[-1]) || strchr("-._", s2[-1])) s2--;
2024 if (strnEQ(s2-4,"perl",4))
2026 while (s = moreswitches(s)) ;
2028 if (cddir && chdir(cddir) < 0)
2029 croak("Can't chdir to %s",cddir);
2037 uid = (int)getuid();
2038 euid = (int)geteuid();
2039 gid = (int)getgid();
2040 egid = (int)getegid();
2045 tainting |= (uid && (euid != uid || egid != gid));
2053 croak("No %s allowed while running setuid", s);
2055 croak("No %s allowed while running setgid", s);
2061 curstash = debstash;
2062 dbargs = GvAV(gv_AVadd((gv_fetchpv("args", GV_ADDMULTI, SVt_PVAV))));
2064 DBgv = gv_fetchpv("DB", GV_ADDMULTI, SVt_PVGV);
2065 DBline = gv_fetchpv("dbline", GV_ADDMULTI, SVt_PVAV);
2066 DBsub = gv_HVadd(gv_fetchpv("sub", GV_ADDMULTI, SVt_PVHV));
2067 DBsingle = GvSV((gv_fetchpv("single", GV_ADDMULTI, SVt_PV)));
2068 sv_setiv(DBsingle, 0);
2069 DBtrace = GvSV((gv_fetchpv("trace", GV_ADDMULTI, SVt_PV)));
2070 sv_setiv(DBtrace, 0);
2071 DBsignal = GvSV((gv_fetchpv("signal", GV_ADDMULTI, SVt_PV)));
2072 sv_setiv(DBsignal, 0);
2073 curstash = defstash;
2080 mainstack = curstack; /* remember in case we switch stacks */
2081 AvREAL_off(curstack); /* not a real array */
2082 av_extend(curstack,127);
2084 stack_base = AvARRAY(curstack);
2085 stack_sp = stack_base;
2086 stack_max = stack_base + 127;
2088 cxstack_max = 8192 / sizeof(CONTEXT) - 2; /* Use most of 8K. */
2089 New(50,cxstack,cxstack_max + 1,CONTEXT);
2092 New(50,tmps_stack,128,SV*);
2097 New(51,debname,128,char);
2098 New(52,debdelim,128,char);
2102 * The following stacks almost certainly should be per-interpreter,
2103 * but for now they're not. XXX
2107 markstack_ptr = markstack;
2109 New(54,markstack,64,I32);
2110 markstack_ptr = markstack;
2111 markstack_max = markstack + 64;
2117 New(54,scopestack,32,I32);
2119 scopestack_max = 32;
2125 New(54,savestack,128,ANY);
2127 savestack_max = 128;
2133 New(54,retstack,16,OP*);
2143 Safefree(tmps_stack);
2150 static PerlIO *tmpfp; /* moved outside init_lexer() because of UNICOS bug */
2158 subname = newSVpv("main",4);
2162 init_predump_symbols()
2167 sv_setpvn(GvSV(gv_fetchpv("\"", TRUE, SVt_PV)), " ", 1);
2169 stdingv = gv_fetchpv("STDIN",TRUE, SVt_PVIO);
2170 GvMULTI_on(stdingv);
2171 IoIFP(GvIOp(stdingv)) = PerlIO_stdin();
2172 tmpgv = gv_fetchpv("stdin",TRUE, SVt_PV);
2174 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(GvIOp(stdingv));
2176 tmpgv = gv_fetchpv("STDOUT",TRUE, SVt_PVIO);
2178 IoOFP(GvIOp(tmpgv)) = IoIFP(GvIOp(tmpgv)) = PerlIO_stdout();
2180 tmpgv = gv_fetchpv("stdout",TRUE, SVt_PV);
2182 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(GvIOp(defoutgv));
2184 othergv = gv_fetchpv("STDERR",TRUE, SVt_PVIO);
2185 GvMULTI_on(othergv);
2186 IoOFP(GvIOp(othergv)) = IoIFP(GvIOp(othergv)) = PerlIO_stderr();
2187 tmpgv = gv_fetchpv("stderr",TRUE, SVt_PV);
2189 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(GvIOp(othergv));
2191 statname = NEWSV(66,0); /* last filename we did stat on */
2194 osname = savepv(OSNAME);
2198 init_postdump_symbols(argc,argv,env)
2200 register char **argv;
2201 register char **env;
2207 argc--,argv++; /* skip name of script */
2209 for (; argc > 0 && **argv == '-'; argc--,argv++) {
2212 if (argv[0][1] == '-') {
2216 if (s = strchr(argv[0], '=')) {
2218 sv_setpv(GvSV(gv_fetchpv(argv[0]+1,TRUE, SVt_PV)),s);
2221 sv_setiv(GvSV(gv_fetchpv(argv[0]+1,TRUE, SVt_PV)),1);
2224 toptarget = NEWSV(0,0);
2225 sv_upgrade(toptarget, SVt_PVFM);
2226 sv_setpvn(toptarget, "", 0);
2227 bodytarget = NEWSV(0,0);
2228 sv_upgrade(bodytarget, SVt_PVFM);
2229 sv_setpvn(bodytarget, "", 0);
2230 formtarget = bodytarget;
2233 if (tmpgv = gv_fetchpv("0",TRUE, SVt_PV)) {
2234 sv_setpv(GvSV(tmpgv),origfilename);
2235 magicname("0", "0", 1);
2237 if (tmpgv = gv_fetchpv("\030",TRUE, SVt_PV))
2238 sv_setpv(GvSV(tmpgv),origargv[0]);
2239 if (argvgv = gv_fetchpv("ARGV",TRUE, SVt_PVAV)) {
2241 (void)gv_AVadd(argvgv);
2242 av_clear(GvAVn(argvgv));
2243 for (; argc > 0; argc--,argv++) {
2244 av_push(GvAVn(argvgv),newSVpv(argv[0],0));
2247 if (envgv = gv_fetchpv("ENV",TRUE, SVt_PVHV)) {
2252 #ifndef VMS /* VMS doesn't have environ array */
2253 /* Note that if the supplied env parameter is actually a copy
2254 of the global environ then it may now point to free'd memory
2255 if the environment has been modified since. To avoid this
2256 problem we treat env==NULL as meaning 'use the default'
2260 if (env != environ) {
2261 environ[0] = Nullch;
2262 hv_magic(hv, envgv, 'E');
2264 for (; *env; env++) {
2265 if (!(s = strchr(*env,'=')))
2268 sv = newSVpv(s--,0);
2269 sv_magic(sv, sv, 'e', *env, s - *env);
2270 (void)hv_store(hv, *env, s - *env, sv, 0);
2274 #ifdef DYNAMIC_ENV_FETCH
2275 HvNAME(hv) = savepv(ENV_HV_NAME);
2277 hv_magic(hv, envgv, 'E');
2280 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
2281 sv_setiv(GvSV(tmpgv), (IV)getpid());
2290 s = getenv("PERL5LIB");
2294 incpush(getenv("PERLLIB"), FALSE);
2296 /* Treat PERL5?LIB as a possible search list logical name -- the
2297 * "natural" VMS idiom for a Unix path string. We allow each
2298 * element to be a set of |-separated directories for compatibility.
2302 if (my_trnlnm("PERL5LIB",buf,0))
2303 do { incpush(buf,TRUE); } while (my_trnlnm("PERL5LIB",buf,++idx));
2305 while (my_trnlnm("PERLLIB",buf,idx++)) incpush(buf,FALSE);
2309 /* Use the ~-expanded versions of APPLIB (undocumented),
2310 ARCHLIB PRIVLIB SITEARCH SITELIB and OLDARCHLIB
2313 incpush(APPLLIB_EXP, FALSE);
2317 incpush(ARCHLIB_EXP, FALSE);
2320 #define PRIVLIB_EXP "/usr/local/lib/perl5:/usr/local/lib/perl"
2322 incpush(PRIVLIB_EXP, FALSE);
2325 incpush(SITEARCH_EXP, FALSE);
2328 incpush(SITELIB_EXP, FALSE);
2330 #ifdef OLDARCHLIB_EXP /* 5.00[01] compatibility */
2331 incpush(OLDARCHLIB_EXP, FALSE);
2335 incpush(".", FALSE);
2339 # define PERLLIB_SEP ';'
2342 # define PERLLIB_SEP '|'
2344 # define PERLLIB_SEP ':'
2347 #ifndef PERLLIB_MANGLE
2348 # define PERLLIB_MANGLE(s,n) (s)
2352 incpush(p, addsubdirs)
2356 SV *subdir = Nullsv;
2357 static char *archpat_auto;
2364 if (!archpat_auto) {
2365 STRLEN len = (sizeof(ARCHNAME) + strlen(patchlevel)
2366 + sizeof("//auto"));
2367 New(55, archpat_auto, len, char);
2368 sprintf(archpat_auto, "/%s/%s/auto", ARCHNAME, patchlevel);
2370 for (len = sizeof(ARCHNAME) + 2;
2371 archpat_auto[len] != '\0' && archpat_auto[len] != '/'; len++)
2372 if (archpat_auto[len] == '.') archpat_auto[len] = '_';
2377 /* Break at all separators */
2379 SV *libdir = newSV(0);
2382 /* skip any consecutive separators */
2383 while ( *p == PERLLIB_SEP ) {
2384 /* Uncomment the next line for PATH semantics */
2385 /* av_push(GvAVn(incgv), newSVpv(".", 1)); */
2389 if ( (s = strchr(p, PERLLIB_SEP)) != Nullch ) {
2390 sv_setpvn(libdir, PERLLIB_MANGLE(p, (STRLEN)(s - p)),
2395 sv_setpv(libdir, PERLLIB_MANGLE(p, 0));
2396 p = Nullch; /* break out */
2400 * BEFORE pushing libdir onto @INC we may first push version- and
2401 * archname-specific sub-directories.
2404 struct stat tmpstatbuf;
2409 if ((unix = tounixspec_ts(SvPV(libdir,na),Nullch)) != Nullch) {
2411 while (unix[len-1] == '/') len--; /* Cosmetic */
2412 sv_usepvn(libdir,unix,len);
2415 PerlIO_printf(PerlIO_stderr(),
2416 "Failed to unixify @INC element \"%s\"\n",
2419 /* .../archname/version if -d .../archname/version/auto */
2420 sv_setsv(subdir, libdir);
2421 sv_catpv(subdir, archpat_auto);
2422 if (Stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
2423 S_ISDIR(tmpstatbuf.st_mode))
2424 av_push(GvAVn(incgv),
2425 newSVpv(SvPVX(subdir), SvCUR(subdir) - sizeof "auto"));
2427 /* .../archname if -d .../archname/auto */
2428 sv_insert(subdir, SvCUR(libdir) + sizeof(ARCHNAME),
2429 strlen(patchlevel) + 1, "", 0);
2430 if (Stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
2431 S_ISDIR(tmpstatbuf.st_mode))
2432 av_push(GvAVn(incgv),
2433 newSVpv(SvPVX(subdir), SvCUR(subdir) - sizeof "auto"));
2436 /* finally push this lib directory on the end of @INC */
2437 av_push(GvAVn(incgv), libdir);
2440 SvREFCNT_dec(subdir);
2444 calllist(oldscope, list)
2450 line_t oldline = curcop->cop_line;
2452 Copy(top_env, oldtop, 1, Sigjmp_buf);
2454 while (AvFILL(list) >= 0) {
2455 CV *cv = (CV*)av_shift(list);
2459 switch (Sigsetjmp(top_env,1)) {
2461 SV* atsv = GvSV(errgv);
2463 perl_call_sv((SV*)cv, G_EVAL|G_DISCARD);
2464 (void)SvPV(atsv, len);
2466 Copy(oldtop, top_env, 1, Sigjmp_buf);
2467 curcop = &compiling;
2468 curcop->cop_line = oldline;
2469 if (list == beginav)
2470 sv_catpv(atsv, "BEGIN failed--compilation aborted");
2472 sv_catpv(atsv, "END failed--cleanup aborted");
2473 while (scopestack_ix > oldscope)
2475 croak("%s", SvPVX(atsv));
2483 /* my_exit() was called */
2484 while (scopestack_ix > oldscope)
2486 curstash = defstash;
2488 calllist(oldscope, endav);
2490 Copy(oldtop, top_env, 1, Sigjmp_buf);
2491 curcop = &compiling;
2492 curcop->cop_line = oldline;
2494 if (list == beginav)
2495 croak("BEGIN failed--compilation aborted");
2497 croak("END failed--cleanup aborted");
2503 PerlIO_printf(PerlIO_stderr(), "panic: restartop\n");
2507 Copy(oldtop, top_env, 1, Sigjmp_buf);
2508 curcop = &compiling;
2509 curcop->cop_line = oldline;
2510 Siglongjmp(top_env, 3);
2514 Copy(oldtop, top_env, 1, Sigjmp_buf);
2529 STATUS_NATIVE_SET(status);
2539 if (vaxc$errno & 1) {
2540 if (STATUS_NATIVE & 1) /* fortuitiously includes "-1" */
2541 STATUS_NATIVE_SET(44);
2544 if (!vaxc$errno && errno) /* unlikely */
2545 STATUS_NATIVE_SET(44);
2547 STATUS_NATIVE_SET(vaxc$errno);
2551 STATUS_POSIX_SET(errno);
2552 else if (STATUS_POSIX == 0)
2553 STATUS_POSIX_SET(255);
2561 register CONTEXT *cx;
2570 (void)UNLINK(e_tmpname);
2571 Safefree(e_tmpname);
2575 if (cxstack_ix >= 0) {
2582 Siglongjmp(top_env, 2);