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 #if !defined(STANDARD_C) && !defined(HAS_GETENV_PROTOTYPE)
24 char *getenv _((char *)); /* Usually in <stdlib.h> */
27 dEXTCONST char rcsid[] = "perl.c\nPatch level: ###\n";
35 #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
45 curcop = &compiling; \
52 laststype = OP_STAT; \
54 maxsysfd = MAXSYSFD; \
61 laststype = OP_STAT; \
64 static void find_beginning _((void));
65 static void forbid_setid _((char *));
66 static void incpush _((char *, int));
67 static void init_ids _((void));
68 static void init_debugger _((void));
69 static void init_lexer _((void));
70 static void init_main_stash _((void));
71 static void init_perllib _((void));
72 static void init_postdump_symbols _((int, char **, char **));
73 static void init_predump_symbols _((void));
74 static void init_stacks _((void));
75 static void my_exit_jump _((void)) __attribute__((noreturn));
76 static void nuke_stacks _((void));
77 static void open_script _((char *, bool, SV *));
78 static void usage _((char *));
79 static void validate_suid _((char *, char*));
81 static int fdscript = -1;
86 PerlInterpreter *sv_interp;
89 New(53, sv_interp, 1, PerlInterpreter);
94 perl_construct( sv_interp )
95 register PerlInterpreter *sv_interp;
97 if (!(curinterp = sv_interp))
101 Zero(sv_interp, 1, PerlInterpreter);
104 /* Init the real globals? */
106 linestr = NEWSV(65,80);
107 sv_upgrade(linestr,SVt_PVIV);
109 if (!SvREADONLY(&sv_undef)) {
110 SvREADONLY_on(&sv_undef);
114 SvREADONLY_on(&sv_no);
116 sv_setpv(&sv_yes,Yes);
118 SvREADONLY_on(&sv_yes);
121 nrs = newSVpv("\n", 1);
122 rs = SvREFCNT_inc(nrs);
128 * There is no way we can refer to them from Perl so close them to save
129 * space. The other alternative would be to provide STDAUX and STDPRN
132 (void)fclose(stdaux);
133 (void)fclose(stdprn);
139 perl_destruct_level = 1;
141 if(perl_destruct_level > 0)
147 start_env.je_prev = NULL;
148 start_env.je_ret = -1;
149 start_env.je_mustcatch = TRUE;
150 top_env = &start_env;
153 SET_NUMERIC_STANDARD();
154 #if defined(SUBVERSION) && SUBVERSION > 0
155 sprintf(patchlevel, "%7.5f", (double) 5
156 + ((double) PATCHLEVEL / (double) 1000)
157 + ((double) SUBVERSION / (double) 100000));
159 sprintf(patchlevel, "%5.3f", (double) 5 +
160 ((double) PATCHLEVEL / (double) 1000));
163 #if defined(LOCAL_PATCH_COUNT)
164 localpatches = local_patches; /* For possible -v */
167 PerlIO_init(); /* Hook to IO system */
169 fdpid = newAV(); /* for remembering popen pids by fd */
176 perl_destruct(sv_interp)
177 register PerlInterpreter *sv_interp;
179 int destruct_level; /* 0=none, 1=full, 2=full with checks */
183 if (!(curinterp = sv_interp))
186 destruct_level = perl_destruct_level;
190 if (s = getenv("PERL_DESTRUCT_LEVEL")) {
192 if (destruct_level < i)
198 /* unhook hooks which will soon be, or use, destroyed data */
199 SvREFCNT_dec(warnhook);
201 SvREFCNT_dec(diehook);
203 SvREFCNT_dec(parsehook);
209 /* We must account for everything. */
211 /* Destroy the main CV and syntax tree */
213 curpad = AvARRAY(comppad);
218 SvREFCNT_dec(main_cv);
223 * Try to destruct global references. We do this first so that the
224 * destructors and destructees still exist. Some sv's might remain.
225 * Non-referenced objects are on their own.
232 if (destruct_level == 0){
234 DEBUG_P(debprofdump());
236 /* The exit() function will do everything that needs doing. */
240 /* loosen bonds of global variables */
243 (void)PerlIO_close(rsfp);
247 /* Filters for program text */
248 SvREFCNT_dec(rsfp_filters);
249 rsfp_filters = Nullav;
261 sawampersand = FALSE; /* must save all match strings */
262 sawstudy = FALSE; /* do fbm_instr on all strings */
277 /* magical thingies */
279 Safefree(ofs); /* $, */
282 Safefree(ors); /* $\ */
285 SvREFCNT_dec(nrs); /* $\ helper */
288 multiline = 0; /* $* */
290 SvREFCNT_dec(statname);
294 /* defgv, aka *_ should be taken care of elsewhere */
296 #if 0 /* just about all regexp stuff, seems to be ok */
298 /* shortcuts to regexp stuff */
303 SAVEFREEOP(oldlastpm); /* for saving regexp context during debugger */
305 regprecomp = NULL; /* uncompiled string. */
306 regparse = NULL; /* Input-scan pointer. */
307 regxend = NULL; /* End of input for compile */
308 regnpar = 0; /* () count. */
309 regcode = NULL; /* Code-emit pointer; ®dummy = don't. */
310 regsize = 0; /* Code size. */
311 regnaughty = 0; /* How bad is this pattern? */
312 regsawback = 0; /* Did we see \1, ...? */
314 reginput = NULL; /* String-input pointer. */
315 regbol = NULL; /* Beginning of input, for ^ check. */
316 regeol = NULL; /* End of input, for $ check. */
317 regstartp = (char **)NULL; /* Pointer to startp array. */
318 regendp = (char **)NULL; /* Ditto for endp. */
319 reglastparen = 0; /* Similarly for lastparen. */
320 regtill = NULL; /* How far we are required to go. */
321 regflags = 0; /* are we folding, multilining? */
322 regprev = (char)NULL; /* char before regbol, \n if none */
326 /* clean up after study() */
327 SvREFCNT_dec(lastscream);
329 Safefree(screamfirst);
331 Safefree(screamnext);
334 /* startup and shutdown function lists */
335 SvREFCNT_dec(beginav);
340 /* temp stack during pp_sort() */
341 SvREFCNT_dec(sortstack);
344 /* shortcuts just get cleared */
354 /* reset so print() ends up where we expect */
357 /* Prepare to destruct main symbol table. */
364 if (destruct_level >= 2) {
365 if (scopestack_ix != 0)
366 warn("Unbalanced scopes: %ld more ENTERs than LEAVEs\n",
367 (long)scopestack_ix);
368 if (savestack_ix != 0)
369 warn("Unbalanced saves: %ld more saves than restores\n",
371 if (tmps_floor != -1)
372 warn("Unbalanced tmps: %ld more allocs than frees\n",
373 (long)tmps_floor + 1);
374 if (cxstack_ix != -1)
375 warn("Unbalanced context: %ld more PUSHes than POPs\n",
376 (long)cxstack_ix + 1);
379 /* Now absolutely destruct everything, somehow or other, loops or no. */
381 SvFLAGS(strtab) |= SVTYPEMASK; /* don't clean out strtab now */
382 while (sv_count != 0 && sv_count != last_sv_count) {
383 last_sv_count = sv_count;
386 SvFLAGS(strtab) &= ~SVTYPEMASK;
387 SvFLAGS(strtab) |= SVt_PVHV;
389 /* Destruct the global string table. */
391 /* Yell and reset the HeVAL() slots that are still holding refcounts,
392 * so that sv_free() won't fail on them.
401 array = HvARRAY(strtab);
405 warn("Unbalanced string table refcount: (%d) for \"%s\"",
406 HeVAL(hent) - Nullsv, HeKEY(hent));
407 HeVAL(hent) = Nullsv;
417 SvREFCNT_dec(strtab);
420 warn("Scalars leaked: %ld\n", (long)sv_count);
424 /* No SVs have survived, need to clean out */
428 Safefree(origfilename);
430 hints = 0; /* Reset hints. Should hints be per-interpreter ? */
432 DEBUG_P(debprofdump());
437 PerlInterpreter *sv_interp;
439 if (!(curinterp = sv_interp))
445 perl_parse(sv_interp, xsinit, argc, argv, env)
446 PerlInterpreter *sv_interp;
447 void (*xsinit)_((void));
454 char *scriptname = NULL;
455 VOL bool dosearch = FALSE;
461 #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
464 croak("suidperl is no longer needed since the kernel can now execute\n\
465 setuid perl scripts securely.\n");
469 if (!(curinterp = sv_interp))
472 #if defined(NeXT) && defined(__DYNAMIC__)
473 _dyld_lookup_and_bind
474 ("__environ", (unsigned long *) &environ_pointer, NULL);
479 #ifndef VMS /* VMS doesn't have environ array */
480 origenviron = environ;
486 /* Come here if running an undumped a.out. */
488 origfilename = savepv(argv[0]);
490 cxstack_ix = -1; /* start label stack again */
492 init_postdump_symbols(argc,argv,env);
497 curpad = AvARRAY(comppad);
502 SvREFCNT_dec(main_cv);
506 oldscope = scopestack_ix;
508 switch (JMPENV_PUSH) {
513 /* my_exit() was called */
514 while (scopestack_ix > oldscope)
518 call_list(oldscope, endav);
520 return STATUS_NATIVE_EXPORT;
523 PerlIO_printf(PerlIO_stderr(), "panic: top_env\n");
527 sv_setpvn(linestr,"",0);
528 sv = newSVpv("",0); /* first used for -I flags */
532 for (argc--,argv++; argc > 0; argc--,argv++) {
533 if (argv[0][0] != '-' || !argv[0][1])
537 validarg = " PHOOEY ";
562 if (s = moreswitches(s))
572 if (euid != uid || egid != gid)
573 croak("No -e allowed in setuid scripts");
575 e_tmpname = savepv(TMPPATH);
576 (void)mktemp(e_tmpname);
578 croak("Can't mktemp()");
579 e_fp = PerlIO_open(e_tmpname,"w");
581 croak("Cannot open temporary file");
586 PerlIO_puts(e_fp,argv[1]);
590 croak("No code specified for -e");
591 (void)PerlIO_putc(e_fp,'\n');
602 incpush(argv[1], TRUE);
603 sv_catpv(sv,argv[1]);
620 preambleav = newAV();
621 av_push(preambleav, newSVpv("use Config qw(myconfig config_vars)",0));
623 Sv = newSVpv("print myconfig();",0);
625 sv_catpv(Sv,"print \"\\nCharacteristics of this PERLSHR image: \\n\",");
627 sv_catpv(Sv,"print \"\\nCharacteristics of this binary (from libperl): \\n\",");
629 #if defined(DEBUGGING) || defined(NO_EMBED) || defined(MULTIPLICITY)
630 strcpy(buf,"\" Compile-time options:");
632 strcat(buf," DEBUGGING");
635 strcat(buf," NO_EMBED");
638 strcat(buf," MULTIPLICITY");
640 strcat(buf,"\\n\",");
643 #if defined(LOCAL_PATCH_COUNT)
644 if (LOCAL_PATCH_COUNT > 0) {
646 sv_catpv(Sv,"print \" Locally applied patches:\\n\",");
647 for (i = 1; i <= LOCAL_PATCH_COUNT; i++) {
648 if (localpatches[i]) {
649 sprintf(buf,"\" \\t%s\\n\",",localpatches[i]);
655 sprintf(buf,"\" Built under %s\\n\"",OSNAME);
659 sprintf(buf,",\" Compiled at %s %s\\n\"",__DATE__,__TIME__);
661 sprintf(buf,",\" Compiled on %s\\n\"",__DATE__);
667 @env = map { \"$_=\\\"$ENV{$_}\\\"\" } sort grep {/^PERL/} keys %ENV; \
668 print \" \\%ENV:\\n @env\\n\" if @env; \
669 print \" \\@INC:\\n @INC\\n\";");
672 Sv = newSVpv("config_vars(qw(",0);
677 av_push(preambleav, Sv);
678 scriptname = BIT_BUCKET; /* don't look for script or read stdin */
692 croak("Unrecognized switch: -%s",s);
697 if (!tainting && (s = getenv("PERL5OPT"))) {
708 if (!strchr("DIMUdmw", *s))
709 croak("Illegal switch in PERL5OPT: -%c", *s);
715 scriptname = argv[0];
717 if (PerlIO_flush(e_fp) || PerlIO_error(e_fp) || PerlIO_close(e_fp)) {
719 warn("Did you forget to compile with -DMULTIPLICITY?");
721 croak("Can't write to temp file for -e: %s", Strerror(errno));
725 scriptname = e_tmpname;
727 else if (scriptname == Nullch) {
729 if ( isatty(PerlIO_fileno(PerlIO_stdin())) )
737 open_script(scriptname,dosearch,sv);
739 validate_suid(validarg, scriptname);
744 main_cv = compcv = (CV*)NEWSV(1104,0);
745 sv_upgrade((SV *)compcv, SVt_PVCV);
749 av_push(comppad, Nullsv);
750 curpad = AvARRAY(comppad);
751 comppad_name = newAV();
752 comppad_name_fill = 0;
753 min_intro_pending = 0;
756 comppadlist = newAV();
757 AvREAL_off(comppadlist);
758 av_store(comppadlist, 0, (SV*)comppad_name);
759 av_store(comppadlist, 1, (SV*)comppad);
760 CvPADLIST(compcv) = comppadlist;
762 boot_core_UNIVERSAL();
764 (*xsinit)(); /* in case linked C routines want magical variables */
769 init_predump_symbols();
771 init_postdump_symbols(argc,argv,env);
775 /* now parse the script */
778 if (yyparse() || error_count) {
780 croak("%s had compilation errors.\n", origfilename);
782 croak("Execution of %s aborted due to compilation errors.\n",
786 curcop->cop_line = 0;
790 (void)UNLINK(e_tmpname);
795 /* now that script is parsed, we can modify record separator */
797 rs = SvREFCNT_inc(nrs);
798 sv_setsv(GvSV(gv_fetchpv("/", TRUE, SVt_PV)), rs);
809 #ifdef DEBUGGING_MSTATS
810 if ((s=getenv("PERL_DEBUG_MSTATS")) && atoi(s) >= 2)
811 dump_mstats("after compilation:");
822 PerlInterpreter *sv_interp;
827 if (!(curinterp = sv_interp))
830 oldscope = scopestack_ix;
832 switch (JMPENV_PUSH) {
834 cxstack_ix = -1; /* start context stack again */
837 /* my_exit() was called */
838 while (scopestack_ix > oldscope)
842 call_list(oldscope, endav);
844 #ifdef DEBUGGING_MSTATS
845 if (getenv("PERL_DEBUG_MSTATS"))
846 dump_mstats("after execution: ");
849 return STATUS_NATIVE_EXPORT;
852 PerlIO_printf(PerlIO_stderr(), "panic: restartop\n");
857 if (curstack != mainstack) {
859 SWITCHSTACK(curstack, mainstack);
864 DEBUG_r(PerlIO_printf(PerlIO_stderr(), "%s $` $& $' support.\n",
865 sawampersand ? "Enabling" : "Omitting"));
869 DEBUG(PerlIO_printf(Perl_debug_log, "\nEXECUTING...\n\n"));
872 PerlIO_printf(PerlIO_stderr(), "%s syntax OK\n", origfilename);
875 if (perldb && DBsingle)
876 sv_setiv(DBsingle, 1);
886 else if (main_start) {
887 CvDEPTH(main_cv) = 1;
898 perl_get_sv(name, create)
902 GV* gv = gv_fetchpv(name, create, SVt_PV);
909 perl_get_av(name, create)
913 GV* gv = gv_fetchpv(name, create, SVt_PVAV);
922 perl_get_hv(name, create)
926 GV* gv = gv_fetchpv(name, create, SVt_PVHV);
935 perl_get_cv(name, create)
939 GV* gv = gv_fetchpv(name, create, SVt_PVCV);
940 if (create && !GvCVu(gv))
941 return newSUB(start_subparse(FALSE, 0),
942 newSVOP(OP_CONST, 0, newSVpv(name,0)),
950 /* Be sure to refetch the stack pointer after calling these routines. */
953 perl_call_argv(subname, flags, argv)
955 I32 flags; /* See G_* flags in cop.h */
956 register char **argv; /* null terminated arg list */
963 XPUSHs(sv_2mortal(newSVpv(*argv,0)));
968 return perl_call_pv(subname, flags);
972 perl_call_pv(subname, flags)
973 char *subname; /* name of the subroutine */
974 I32 flags; /* See G_* flags in cop.h */
976 return perl_call_sv((SV*)perl_get_cv(subname, TRUE), flags);
980 perl_call_method(methname, flags)
981 char *methname; /* name of the subroutine */
982 I32 flags; /* See G_* flags in cop.h */
988 XPUSHs(sv_2mortal(newSVpv(methname,0)));
991 return perl_call_sv(*stack_sp--, flags);
994 /* May be called with any of a CV, a GV, or an SV containing the name. */
996 perl_call_sv(sv, flags)
998 I32 flags; /* See G_* flags in cop.h */
1000 LOGOP myop; /* fake syntax tree node */
1006 bool oldcatch = CATCH_GET;
1009 if (flags & G_DISCARD) {
1014 Zero(&myop, 1, LOGOP);
1015 myop.op_next = Nullop;
1016 if (!(flags & G_NOARGS))
1017 myop.op_flags |= OPf_STACKED;
1018 myop.op_flags |= ((flags & G_VOID) ? OPf_WANT_VOID :
1019 (flags & G_ARRAY) ? OPf_WANT_LIST :
1024 EXTEND(stack_sp, 1);
1027 oldscope = scopestack_ix;
1029 if (perldb && curstash != debstash
1030 /* Handle first BEGIN of -d. */
1031 && (DBcv || (DBcv = GvCV(DBsub)))
1032 /* Try harder, since this may have been a sighandler, thus
1033 * curstash may be meaningless. */
1034 && (SvTYPE(sv) != SVt_PVCV || CvSTASH((CV*)sv) != debstash))
1035 op->op_private |= OPpENTERSUB_DB;
1037 if (flags & G_EVAL) {
1038 cLOGOP->op_other = op;
1040 /* we're trying to emulate pp_entertry() here */
1042 register CONTEXT *cx;
1043 I32 gimme = GIMME_V;
1048 push_return(op->op_next);
1049 PUSHBLOCK(cx, CXt_EVAL, stack_sp);
1051 eval_root = op; /* Only needed so that goto works right. */
1054 if (flags & G_KEEPERR)
1057 sv_setpv(GvSV(errgv),"");
1061 switch (JMPENV_PUSH) {
1068 /* my_exit() was called */
1069 curstash = defstash;
1073 croak("Callback called exit");
1082 stack_sp = stack_base + oldmark;
1083 if (flags & G_ARRAY)
1087 *++stack_sp = &sv_undef;
1095 if (op == (OP*)&myop)
1099 retval = stack_sp - (stack_base + oldmark);
1100 if ((flags & G_EVAL) && !(flags & G_KEEPERR))
1101 sv_setpv(GvSV(errgv),"");
1104 if (flags & G_EVAL) {
1105 if (scopestack_ix > oldscope) {
1109 register CONTEXT *cx;
1121 CATCH_SET(oldcatch);
1123 if (flags & G_DISCARD) {
1124 stack_sp = stack_base + oldmark;
1132 /* Eval a string. The G_EVAL flag is always assumed. */
1135 perl_eval_sv(sv, flags)
1137 I32 flags; /* See G_* flags in cop.h */
1139 UNOP myop; /* fake syntax tree node */
1141 I32 oldmark = sp - stack_base;
1146 if (flags & G_DISCARD) {
1154 EXTEND(stack_sp, 1);
1156 oldscope = scopestack_ix;
1158 if (!(flags & G_NOARGS))
1159 myop.op_flags = OPf_STACKED;
1160 myop.op_next = Nullop;
1161 myop.op_type = OP_ENTEREVAL;
1162 myop.op_flags |= ((flags & G_VOID) ? OPf_WANT_VOID :
1163 (flags & G_ARRAY) ? OPf_WANT_LIST :
1165 if (flags & G_KEEPERR)
1166 myop.op_flags |= OPf_SPECIAL;
1168 switch (JMPENV_PUSH) {
1175 /* my_exit() was called */
1176 curstash = defstash;
1180 croak("Callback called exit");
1189 stack_sp = stack_base + oldmark;
1190 if (flags & G_ARRAY)
1194 *++stack_sp = &sv_undef;
1199 if (op == (OP*)&myop)
1200 op = pp_entereval();
1203 retval = stack_sp - (stack_base + oldmark);
1204 if (!(flags & G_KEEPERR))
1205 sv_setpv(GvSV(errgv),"");
1209 if (flags & G_DISCARD) {
1210 stack_sp = stack_base + oldmark;
1218 /* Require a module. */
1224 SV* sv = sv_newmortal();
1225 sv_setpv(sv, "require '");
1228 perl_eval_sv(sv, G_DISCARD);
1232 magicname(sym,name,namlen)
1239 if (gv = gv_fetchpv(sym,TRUE, SVt_PV))
1240 sv_magic(GvSV(gv), (SV*)gv, 0, name, namlen);
1244 usage(name) /* XXX move this out into a module ? */
1247 /* This message really ought to be max 23 lines.
1248 * Removed -h because the user already knows that opton. Others? */
1249 printf("\nUsage: %s [switches] [--] [programfile] [arguments]", name);
1250 printf("\n -0[octal] specify record separator (\\0, if no argument)");
1251 printf("\n -a autosplit mode with -n or -p (splits $_ into @F)");
1252 printf("\n -c check syntax only (runs BEGIN and END blocks)");
1253 printf("\n -d[:debugger] run scripts under debugger");
1254 printf("\n -D[number/list] set debugging flags (argument is a bit mask or flags)");
1255 printf("\n -e 'command' one line of script. Several -e's allowed. Omit [programfile].");
1256 printf("\n -F/pattern/ split() pattern for autosplit (-a). The //'s are optional.");
1257 printf("\n -i[extension] edit <> files in place (make backup if extension supplied)");
1258 printf("\n -Idirectory specify @INC/#include directory (may be used more then once)");
1259 printf("\n -l[octal] enable line ending processing, specifies line teminator");
1260 printf("\n -[mM][-]module.. executes `use/no module...' before executing your script.");
1261 printf("\n -n assume 'while (<>) { ... }' loop arround your script");
1262 printf("\n -p assume loop like -n but print line also like sed");
1263 printf("\n -P run script through C preprocessor before compilation");
1264 printf("\n -s enable some switch parsing for switches after script name");
1265 printf("\n -S look for the script using PATH environment variable");
1266 printf("\n -T turn on tainting checks");
1267 printf("\n -u dump core after parsing script");
1268 printf("\n -U allow unsafe operations");
1269 printf("\n -v print version number and patchlevel of perl");
1270 printf("\n -V[:variable] print perl configuration information");
1271 printf("\n -w TURN WARNINGS ON FOR COMPILATION OF YOUR SCRIPT.");
1272 printf("\n -x[directory] strip off text before #!perl line and perhaps cd to directory\n");
1275 /* This routine handles any switches that can be given during run */
1286 rschar = scan_oct(s, 4, &numlen);
1288 if (rschar & ~((U8)~0))
1290 else if (!rschar && numlen >= 2)
1291 nrs = newSVpv("", 0);
1294 nrs = newSVpv(&ch, 1);
1299 splitstr = savepv(s + 1);
1313 if (*s == ':' || *s == '=') {
1314 sprintf(buf, "use Devel::%s;", ++s);
1316 my_setenv("PERL5DB",buf);
1326 if (isALPHA(s[1])) {
1327 static char debopts[] = "psltocPmfrxuLHXD";
1330 for (s++; *s && (d = strchr(debopts,*s)); s++)
1331 debug |= 1 << (d - debopts);
1335 for (s++; isDIGIT(*s); s++) ;
1337 debug |= 0x80000000;
1339 warn("Recompile perl with -DDEBUGGING to use -D switch\n");
1340 for (s++; isALNUM(*s); s++) ;
1350 inplace = savepv(s+1);
1352 for (s = inplace; *s && !isSPACE(*s); s++) ;
1359 for (e = s; *e && !isSPACE(*e); e++) ;
1360 p = savepvn(s, e-s);
1367 croak("No space allowed after -I");
1377 *ors = scan_oct(s, 3 + (*s == '0'), &numlen);
1386 ors = SvPV(nrs, orslen);
1387 ors = savepvn(ors, orslen);
1391 forbid_setid("-M"); /* XXX ? */
1394 forbid_setid("-m"); /* XXX ? */
1398 /* -M-foo == 'no foo' */
1399 if (*s == '-') { use = "no "; ++s; }
1400 Sv = newSVpv(use,0);
1402 /* We allow -M'Module qw(Foo Bar)' */
1403 while(isALNUM(*s) || *s==':') ++s;
1405 sv_catpv(Sv, start);
1406 if (*(start-1) == 'm') {
1408 croak("Can't use '%c' after -mname", *s);
1409 sv_catpv( Sv, " ()");
1412 sv_catpvn(Sv, start, s-start);
1413 sv_catpv(Sv, " split(/,/,q{");
1418 if (preambleav == NULL)
1419 preambleav = newAV();
1420 av_push(preambleav, Sv);
1423 croak("No space allowed after -%c", *(s-1));
1440 croak("Too late for \"-T\" option");
1452 #if defined(SUBVERSION) && SUBVERSION > 0
1453 printf("\nThis is perl, version 5.%03d_%02d", PATCHLEVEL, SUBVERSION);
1455 printf("\nThis is perl, version %s",patchlevel);
1458 printf("\n\nCopyright 1987-1997, Larry Wall\n");
1460 printf("\n\nMS-DOS port Copyright (c) 1989, 1990, Diomidis Spinellis\n");
1463 printf("djgpp v2 port (jpl5003c) by Hirofumi Watanabe, 1996\n");
1466 printf("\n\nOS/2 port Copyright (c) 1990, 1991, Raymond Chen, Kai Uwe Rommel\n"
1467 "Version 5 port Copyright (c) 1994-1997, Andreas Kaiser, Ilya Zakharevich\n");
1470 printf("atariST series port, ++jrb bammi@cadence.com\n");
1473 Perl may be copied only under the terms of either the Artistic License or the\n\
1474 GNU General Public License, which may be found in the Perl 5.0 source kit.\n\n");
1482 if (s[1] == '-') /* Additional switches on #! line. */
1490 #ifdef ALTERNATE_SHEBANG
1491 case 'S': /* OS/2 needs -S on "extproc" line. */
1499 croak("Can't emulate -%.1s on #! line",s);
1504 /* compliments of Tom Christiansen */
1506 /* unexec() can be found in the Gnu emacs distribution */
1515 sprintf (buf, "%s.perldump", origfilename);
1516 sprintf (tokenbuf, "%s/perl", BIN_EXP);
1518 status = unexec(buf, tokenbuf, &etext, sbrk(0), 0);
1520 PerlIO_printf(PerlIO_stderr(), "unexec of %s into %s failed!\n", tokenbuf, buf);
1524 # include <lib$routines.h>
1525 lib$signal(SS$_DEBUG); /* ssdef.h #included from vmsish.h */
1527 ABORT(); /* for use with undump */
1537 /* Note that strtab is a rather special HV. Assumptions are made
1538 about not iterating on it, and not adding tie magic to it.
1539 It is properly deallocated in perl_destruct() */
1541 HvSHAREKEYS_off(strtab); /* mandatory */
1542 Newz(506,((XPVHV*)SvANY(strtab))->xhv_array,
1543 sizeof(HE*) * (((XPVHV*)SvANY(strtab))->xhv_max + 1), char);
1545 curstash = defstash = newHV();
1546 curstname = newSVpv("main",4);
1547 gv = gv_fetchpv("main::",TRUE, SVt_PVHV);
1548 SvREFCNT_dec(GvHV(gv));
1549 GvHV(gv) = (HV*)SvREFCNT_inc(defstash);
1551 HvNAME(defstash) = savepv("main");
1552 incgv = gv_HVadd(gv_AVadd(gv_fetchpv("INC",TRUE, SVt_PVAV)));
1554 defgv = gv_fetchpv("_",TRUE, SVt_PVAV);
1555 errgv = gv_HVadd(gv_fetchpv("@", TRUE, SVt_PV));
1557 sv_setpvn(GvSV(errgv), "", 0);
1558 curstash = defstash;
1559 compiling.cop_stash = defstash;
1560 debstash = GvHV(gv_fetchpv("DB::", GV_ADDMULTI, SVt_PVHV));
1561 /* We must init $/ before switches are processed. */
1562 sv_setpvn(GvSV(gv_fetchpv("/", TRUE, SVt_PV)), "\n", 1);
1565 #ifdef CAN_PROTOTYPE
1567 open_script(char *scriptname, bool dosearch, SV *sv)
1570 open_script(scriptname,dosearch,sv)
1576 char *xfound = Nullch;
1577 char *xfailed = Nullch;
1581 #if defined(DOSISH) && !defined(OS2) && !defined(atarist)
1582 #define SEARCH_EXTS ".bat", ".cmd", NULL
1585 # define SEARCH_EXTS ".pl", ".com", NULL
1587 /* additional extensions to try in each dir if scriptname not found */
1589 char *ext[] = { SEARCH_EXTS };
1590 int extidx = (strchr(scriptname,'.')) ? -1 : 0; /* has ext already */
1595 int hasdir, idx = 0, deftypes = 1;
1597 hasdir = (strpbrk(scriptname,":[</") != Nullch) ;
1598 /* The first time through, just add SEARCH_EXTS to whatever we
1599 * already have, so we can check for default file types. */
1600 while (deftypes || (!hasdir && my_trnlnm("DCL$PATH",tokenbuf,idx++)) ) {
1601 if (deftypes) { deftypes = 0; *tokenbuf = '\0'; }
1602 strcat(tokenbuf,scriptname);
1604 if (dosearch && !strchr(scriptname, '/') && (s = getenv("PATH"))) {
1606 bufend = s + strlen(s);
1609 s = cpytill(tokenbuf,s,bufend,':',&len);
1612 for (len = 0; *s && *s != ',' && *s != ';'; tokenbuf[len++] = *s++);
1613 tokenbuf[len] = '\0';
1615 for (len = 0; *s && *s != ';'; tokenbuf[len++] = *s++);
1616 tokenbuf[len] = '\0';
1622 if (len && tokenbuf[len-1] != '/')
1625 if (len && ((tokenbuf[len-1] != '\\') && (tokenbuf[len-1] != '/')))
1627 if (len && tokenbuf[len-1] != '\\')
1630 (void)strcat(tokenbuf+len,"/");
1631 (void)strcat(tokenbuf+len,scriptname);
1635 len = strlen(tokenbuf);
1636 if (extidx > 0) /* reset after previous loop */
1640 DEBUG_p(PerlIO_printf(Perl_debug_log, "Looking for %s\n",tokenbuf));
1641 retval = Stat(tokenbuf,&statbuf);
1643 } while ( retval < 0 /* not there */
1644 && extidx>=0 && ext[extidx] /* try an extension? */
1645 && strcpy(tokenbuf+len, ext[extidx++])
1650 if (S_ISREG(statbuf.st_mode)
1651 && cando(S_IRUSR,TRUE,&statbuf) && cando(S_IXUSR,TRUE,&statbuf)) {
1652 xfound = tokenbuf; /* bingo! */
1656 xfailed = savepv(tokenbuf);
1659 croak("Can't execute %s", xfailed ? xfailed : scriptname );
1662 scriptname = xfound;
1665 if (strnEQ(scriptname, "/dev/fd/", 8) && isDIGIT(scriptname[8]) ) {
1666 char *s = scriptname + 8;
1675 origfilename = savepv(e_tmpname ? "-e" : scriptname);
1676 curcop->cop_filegv = gv_fetchfile(origfilename);
1677 if (strEQ(origfilename,"-"))
1679 if (fdscript >= 0) {
1680 rsfp = PerlIO_fdopen(fdscript,"r");
1681 #if defined(HAS_FCNTL) && defined(F_SETFD)
1683 fcntl(PerlIO_fileno(rsfp),F_SETFD,1); /* ensure close-on-exec */
1686 else if (preprocess) {
1687 char *cpp = CPPSTDIN;
1689 if (strEQ(cpp,"cppstdin"))
1690 sprintf(tokenbuf, "%s/%s", BIN_EXP, cpp);
1692 sprintf(tokenbuf, "%s", cpp);
1694 sv_catpv(sv,PRIVLIB_EXP);
1696 (void)sprintf(buf, "\
1697 sed %s -e \"/^[^#]/b\" \
1698 -e \"/^#[ ]*include[ ]/b\" \
1699 -e \"/^#[ ]*define[ ]/b\" \
1700 -e \"/^#[ ]*if[ ]/b\" \
1701 -e \"/^#[ ]*ifdef[ ]/b\" \
1702 -e \"/^#[ ]*ifndef[ ]/b\" \
1703 -e \"/^#[ ]*else/b\" \
1704 -e \"/^#[ ]*elif[ ]/b\" \
1705 -e \"/^#[ ]*undef[ ]/b\" \
1706 -e \"/^#[ ]*endif/b\" \
1709 (doextract ? "-e \"1,/^#/d\n\"" : ""),
1711 (void)sprintf(buf, "\
1712 %s %s -e '/^[^#]/b' \
1713 -e '/^#[ ]*include[ ]/b' \
1714 -e '/^#[ ]*define[ ]/b' \
1715 -e '/^#[ ]*if[ ]/b' \
1716 -e '/^#[ ]*ifdef[ ]/b' \
1717 -e '/^#[ ]*ifndef[ ]/b' \
1718 -e '/^#[ ]*else/b' \
1719 -e '/^#[ ]*elif[ ]/b' \
1720 -e '/^#[ ]*undef[ ]/b' \
1721 -e '/^#[ ]*endif/b' \
1729 (doextract ? "-e '1,/^#/d\n'" : ""),
1731 scriptname, tokenbuf, SvPV(sv, na), CPPMINUS);
1733 #ifdef IAMSUID /* actually, this is caught earlier */
1734 if (euid != uid && !euid) { /* if running suidperl */
1736 (void)seteuid(uid); /* musn't stay setuid root */
1739 (void)setreuid((Uid_t)-1, uid);
1741 #ifdef HAS_SETRESUID
1742 (void)setresuid((Uid_t)-1, uid, (Uid_t)-1);
1748 if (geteuid() != uid)
1749 croak("Can't do seteuid!\n");
1751 #endif /* IAMSUID */
1752 rsfp = my_popen(buf,"r");
1754 else if (!*scriptname) {
1755 forbid_setid("program input from stdin");
1756 rsfp = PerlIO_stdin();
1759 rsfp = PerlIO_open(scriptname,"r");
1760 #if defined(HAS_FCNTL) && defined(F_SETFD)
1762 fcntl(PerlIO_fileno(rsfp),F_SETFD,1); /* ensure close-on-exec */
1770 #ifndef IAMSUID /* in case script is not readable before setuid */
1771 if (euid && Stat(SvPVX(GvSV(curcop->cop_filegv)),&statbuf) >= 0 &&
1772 statbuf.st_mode & (S_ISUID|S_ISGID)) {
1773 (void)sprintf(buf, "%s/sperl%s", BIN_EXP, patchlevel);
1774 execv(buf, origargv); /* try again */
1775 croak("Can't do setuid\n");
1779 croak("Can't open perl script \"%s\": %s\n",
1780 SvPVX(GvSV(curcop->cop_filegv)), Strerror(errno));
1785 validate_suid(validarg, scriptname)
1791 /* do we need to emulate setuid on scripts? */
1793 /* This code is for those BSD systems that have setuid #! scripts disabled
1794 * in the kernel because of a security problem. Merely defining DOSUID
1795 * in perl will not fix that problem, but if you have disabled setuid
1796 * scripts in the kernel, this will attempt to emulate setuid and setgid
1797 * on scripts that have those now-otherwise-useless bits set. The setuid
1798 * root version must be called suidperl or sperlN.NNN. If regular perl
1799 * discovers that it has opened a setuid script, it calls suidperl with
1800 * the same argv that it had. If suidperl finds that the script it has
1801 * just opened is NOT setuid root, it sets the effective uid back to the
1802 * uid. We don't just make perl setuid root because that loses the
1803 * effective uid we had before invoking perl, if it was different from the
1806 * DOSUID must be defined in both perl and suidperl, and IAMSUID must
1807 * be defined in suidperl only. suidperl must be setuid root. The
1808 * Configure script will set this up for you if you want it.
1814 if (Fstat(PerlIO_fileno(rsfp),&statbuf) < 0) /* normal stat is insecure */
1815 croak("Can't stat script \"%s\"",origfilename);
1816 if (fdscript < 0 && statbuf.st_mode & (S_ISUID|S_ISGID)) {
1820 #ifndef HAS_SETREUID
1821 /* On this access check to make sure the directories are readable,
1822 * there is actually a small window that the user could use to make
1823 * filename point to an accessible directory. So there is a faint
1824 * chance that someone could execute a setuid script down in a
1825 * non-accessible directory. I don't know what to do about that.
1826 * But I don't think it's too important. The manual lies when
1827 * it says access() is useful in setuid programs.
1829 if (access(SvPVX(GvSV(curcop->cop_filegv)),1)) /*double check*/
1830 croak("Permission denied");
1832 /* If we can swap euid and uid, then we can determine access rights
1833 * with a simple stat of the file, and then compare device and
1834 * inode to make sure we did stat() on the same file we opened.
1835 * Then we just have to make sure he or she can execute it.
1838 struct stat tmpstatbuf;
1842 setreuid(euid,uid) < 0
1845 setresuid(euid,uid,(Uid_t)-1) < 0
1848 || getuid() != euid || geteuid() != uid)
1849 croak("Can't swap uid and euid"); /* really paranoid */
1850 if (Stat(SvPVX(GvSV(curcop->cop_filegv)),&tmpstatbuf) < 0)
1851 croak("Permission denied"); /* testing full pathname here */
1852 if (tmpstatbuf.st_dev != statbuf.st_dev ||
1853 tmpstatbuf.st_ino != statbuf.st_ino) {
1854 (void)PerlIO_close(rsfp);
1855 if (rsfp = my_popen("/bin/mail root","w")) { /* heh, heh */
1857 "User %ld tried to run dev %ld ino %ld in place of dev %ld ino %ld!\n\
1858 (Filename of set-id script was %s, uid %ld gid %ld.)\n\nSincerely,\nperl\n",
1859 (long)uid,(long)tmpstatbuf.st_dev, (long)tmpstatbuf.st_ino,
1860 (long)statbuf.st_dev, (long)statbuf.st_ino,
1861 SvPVX(GvSV(curcop->cop_filegv)),
1862 (long)statbuf.st_uid, (long)statbuf.st_gid);
1863 (void)my_pclose(rsfp);
1865 croak("Permission denied\n");
1869 setreuid(uid,euid) < 0
1871 # if defined(HAS_SETRESUID)
1872 setresuid(uid,euid,(Uid_t)-1) < 0
1875 || getuid() != uid || geteuid() != euid)
1876 croak("Can't reswap uid and euid");
1877 if (!cando(S_IXUSR,FALSE,&statbuf)) /* can real uid exec? */
1878 croak("Permission denied\n");
1880 #endif /* HAS_SETREUID */
1881 #endif /* IAMSUID */
1883 if (!S_ISREG(statbuf.st_mode))
1884 croak("Permission denied");
1885 if (statbuf.st_mode & S_IWOTH)
1886 croak("Setuid/gid script is writable by world");
1887 doswitches = FALSE; /* -s is insecure in suid */
1889 if (sv_gets(linestr, rsfp, 0) == Nullch ||
1890 strnNE(SvPV(linestr,na),"#!",2) ) /* required even on Sys V */
1891 croak("No #! line");
1892 s = SvPV(linestr,na)+2;
1894 while (!isSPACE(*s)) s++;
1895 for (s2 = s; (s2 > SvPV(linestr,na)+2 &&
1896 (isDIGIT(s2[-1]) || strchr("._-", s2[-1]))); s2--) ;
1897 if (strnNE(s2-4,"perl",4) && strnNE(s-9,"perl",4)) /* sanity check */
1898 croak("Not a perl script");
1899 while (*s == ' ' || *s == '\t') s++;
1901 * #! arg must be what we saw above. They can invoke it by
1902 * mentioning suidperl explicitly, but they may not add any strange
1903 * arguments beyond what #! says if they do invoke suidperl that way.
1905 len = strlen(validarg);
1906 if (strEQ(validarg," PHOOEY ") ||
1907 strnNE(s,validarg,len) || !isSPACE(s[len]))
1908 croak("Args must match #! line");
1911 if (euid != uid && (statbuf.st_mode & S_ISUID) &&
1912 euid == statbuf.st_uid)
1914 croak("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
1915 FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
1916 #endif /* IAMSUID */
1918 if (euid) { /* oops, we're not the setuid root perl */
1919 (void)PerlIO_close(rsfp);
1921 (void)sprintf(buf, "%s/sperl%s", BIN_EXP, patchlevel);
1922 execv(buf, origargv); /* try again */
1924 croak("Can't do setuid\n");
1927 if (statbuf.st_mode & S_ISGID && statbuf.st_gid != egid) {
1929 (void)setegid(statbuf.st_gid);
1932 (void)setregid((Gid_t)-1,statbuf.st_gid);
1934 #ifdef HAS_SETRESGID
1935 (void)setresgid((Gid_t)-1,statbuf.st_gid,(Gid_t)-1);
1937 setgid(statbuf.st_gid);
1941 if (getegid() != statbuf.st_gid)
1942 croak("Can't do setegid!\n");
1944 if (statbuf.st_mode & S_ISUID) {
1945 if (statbuf.st_uid != euid)
1947 (void)seteuid(statbuf.st_uid); /* all that for this */
1950 (void)setreuid((Uid_t)-1,statbuf.st_uid);
1952 #ifdef HAS_SETRESUID
1953 (void)setresuid((Uid_t)-1,statbuf.st_uid,(Uid_t)-1);
1955 setuid(statbuf.st_uid);
1959 if (geteuid() != statbuf.st_uid)
1960 croak("Can't do seteuid!\n");
1962 else if (uid) { /* oops, mustn't run as root */
1964 (void)seteuid((Uid_t)uid);
1967 (void)setreuid((Uid_t)-1,(Uid_t)uid);
1969 #ifdef HAS_SETRESUID
1970 (void)setresuid((Uid_t)-1,(Uid_t)uid,(Uid_t)-1);
1976 if (geteuid() != uid)
1977 croak("Can't do seteuid!\n");
1980 if (!cando(S_IXUSR,TRUE,&statbuf))
1981 croak("Permission denied\n"); /* they can't do this */
1984 else if (preprocess)
1985 croak("-P not allowed for setuid/setgid script\n");
1986 else if (fdscript >= 0)
1987 croak("fd script not allowed in suidperl\n");
1989 croak("Script is not setuid/setgid in suidperl\n");
1991 /* We absolutely must clear out any saved ids here, so we */
1992 /* exec the real perl, substituting fd script for scriptname. */
1993 /* (We pass script name as "subdir" of fd, which perl will grok.) */
1994 PerlIO_rewind(rsfp);
1995 lseek(PerlIO_fileno(rsfp),(Off_t)0,0); /* just in case rewind didn't */
1996 for (which = 1; origargv[which] && origargv[which] != scriptname; which++) ;
1997 if (!origargv[which])
1998 croak("Permission denied");
1999 (void)sprintf(buf, "/dev/fd/%d/%.127s", PerlIO_fileno(rsfp), origargv[which]);
2000 origargv[which] = buf;
2002 #if defined(HAS_FCNTL) && defined(F_SETFD)
2003 fcntl(PerlIO_fileno(rsfp),F_SETFD,0); /* ensure no close-on-exec */
2006 (void)sprintf(tokenbuf, "%s/perl%s", BIN_EXP, patchlevel);
2007 execv(tokenbuf, origargv); /* try again */
2008 croak("Can't do setuid\n");
2009 #endif /* IAMSUID */
2011 if (euid != uid || egid != gid) { /* (suidperl doesn't exist, in fact) */
2012 #ifndef SETUID_SCRIPTS_ARE_SECURE_NOW
2013 Fstat(PerlIO_fileno(rsfp),&statbuf); /* may be either wrapped or real suid */
2014 if ((euid != uid && euid == statbuf.st_uid && statbuf.st_mode & S_ISUID)
2016 (egid != gid && egid == statbuf.st_gid && statbuf.st_mode & S_ISGID)
2019 croak("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
2020 FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
2021 #endif /* SETUID_SCRIPTS_ARE_SECURE_NOW */
2022 /* not set-id, must be wrapped */
2030 register char *s, *s2;
2032 /* skip forward in input to the real script? */
2036 if ((s = sv_gets(linestr, rsfp, 0)) == Nullch)
2037 croak("No Perl script found in input\n");
2038 if (*s == '#' && s[1] == '!' && (s = instr(s,"perl"))) {
2039 PerlIO_ungetc(rsfp, '\n'); /* to keep line count right */
2041 while (*s && !(isSPACE (*s) || *s == '#')) s++;
2043 while (*s == ' ' || *s == '\t') s++;
2045 while (isDIGIT(s2[-1]) || strchr("-._", s2[-1])) s2--;
2046 if (strnEQ(s2-4,"perl",4))
2048 while (s = moreswitches(s)) ;
2050 if (cddir && chdir(cddir) < 0)
2051 croak("Can't chdir to %s",cddir);
2059 uid = (int)getuid();
2060 euid = (int)geteuid();
2061 gid = (int)getgid();
2062 egid = (int)getegid();
2067 tainting |= (uid && (euid != uid || egid != gid));
2075 croak("No %s allowed while running setuid", s);
2077 croak("No %s allowed while running setgid", s);
2083 curstash = debstash;
2084 dbargs = GvAV(gv_AVadd((gv_fetchpv("args", GV_ADDMULTI, SVt_PVAV))));
2086 DBgv = gv_fetchpv("DB", GV_ADDMULTI, SVt_PVGV);
2087 DBline = gv_fetchpv("dbline", GV_ADDMULTI, SVt_PVAV);
2088 DBsub = gv_HVadd(gv_fetchpv("sub", GV_ADDMULTI, SVt_PVHV));
2089 DBsingle = GvSV((gv_fetchpv("single", GV_ADDMULTI, SVt_PV)));
2090 sv_setiv(DBsingle, 0);
2091 DBtrace = GvSV((gv_fetchpv("trace", GV_ADDMULTI, SVt_PV)));
2092 sv_setiv(DBtrace, 0);
2093 DBsignal = GvSV((gv_fetchpv("signal", GV_ADDMULTI, SVt_PV)));
2094 sv_setiv(DBsignal, 0);
2095 curstash = defstash;
2102 mainstack = curstack; /* remember in case we switch stacks */
2103 AvREAL_off(curstack); /* not a real array */
2104 av_extend(curstack,127);
2106 stack_base = AvARRAY(curstack);
2107 stack_sp = stack_base;
2108 stack_max = stack_base + 127;
2110 cxstack_max = 8192 / sizeof(CONTEXT) - 2; /* Use most of 8K. */
2111 New(50,cxstack,cxstack_max + 1,CONTEXT);
2114 New(50,tmps_stack,128,SV*);
2119 New(51,debname,128,char);
2120 New(52,debdelim,128,char);
2124 * The following stacks almost certainly should be per-interpreter,
2125 * but for now they're not. XXX
2129 markstack_ptr = markstack;
2131 New(54,markstack,64,I32);
2132 markstack_ptr = markstack;
2133 markstack_max = markstack + 64;
2139 New(54,scopestack,32,I32);
2141 scopestack_max = 32;
2147 New(54,savestack,128,ANY);
2149 savestack_max = 128;
2155 New(54,retstack,16,OP*);
2165 Safefree(tmps_stack);
2172 static PerlIO *tmpfp; /* moved outside init_lexer() because of UNICOS bug */
2180 subname = newSVpv("main",4);
2184 init_predump_symbols()
2189 sv_setpvn(GvSV(gv_fetchpv("\"", TRUE, SVt_PV)), " ", 1);
2191 stdingv = gv_fetchpv("STDIN",TRUE, SVt_PVIO);
2192 GvMULTI_on(stdingv);
2193 IoIFP(GvIOp(stdingv)) = PerlIO_stdin();
2194 tmpgv = gv_fetchpv("stdin",TRUE, SVt_PV);
2196 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(GvIOp(stdingv));
2198 tmpgv = gv_fetchpv("STDOUT",TRUE, SVt_PVIO);
2200 IoOFP(GvIOp(tmpgv)) = IoIFP(GvIOp(tmpgv)) = PerlIO_stdout();
2202 tmpgv = gv_fetchpv("stdout",TRUE, SVt_PV);
2204 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(GvIOp(defoutgv));
2206 othergv = gv_fetchpv("STDERR",TRUE, SVt_PVIO);
2207 GvMULTI_on(othergv);
2208 IoOFP(GvIOp(othergv)) = IoIFP(GvIOp(othergv)) = PerlIO_stderr();
2209 tmpgv = gv_fetchpv("stderr",TRUE, SVt_PV);
2211 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(GvIOp(othergv));
2213 statname = NEWSV(66,0); /* last filename we did stat on */
2216 osname = savepv(OSNAME);
2220 init_postdump_symbols(argc,argv,env)
2222 register char **argv;
2223 register char **env;
2229 argc--,argv++; /* skip name of script */
2231 for (; argc > 0 && **argv == '-'; argc--,argv++) {
2234 if (argv[0][1] == '-') {
2238 if (s = strchr(argv[0], '=')) {
2240 sv_setpv(GvSV(gv_fetchpv(argv[0]+1,TRUE, SVt_PV)),s);
2243 sv_setiv(GvSV(gv_fetchpv(argv[0]+1,TRUE, SVt_PV)),1);
2246 toptarget = NEWSV(0,0);
2247 sv_upgrade(toptarget, SVt_PVFM);
2248 sv_setpvn(toptarget, "", 0);
2249 bodytarget = NEWSV(0,0);
2250 sv_upgrade(bodytarget, SVt_PVFM);
2251 sv_setpvn(bodytarget, "", 0);
2252 formtarget = bodytarget;
2255 if (tmpgv = gv_fetchpv("0",TRUE, SVt_PV)) {
2256 sv_setpv(GvSV(tmpgv),origfilename);
2257 magicname("0", "0", 1);
2259 if (tmpgv = gv_fetchpv("\030",TRUE, SVt_PV))
2260 sv_setpv(GvSV(tmpgv),origargv[0]);
2261 if (argvgv = gv_fetchpv("ARGV",TRUE, SVt_PVAV)) {
2263 (void)gv_AVadd(argvgv);
2264 av_clear(GvAVn(argvgv));
2265 for (; argc > 0; argc--,argv++) {
2266 av_push(GvAVn(argvgv),newSVpv(argv[0],0));
2269 if (envgv = gv_fetchpv("ENV",TRUE, SVt_PVHV)) {
2274 #ifndef VMS /* VMS doesn't have environ array */
2275 /* Note that if the supplied env parameter is actually a copy
2276 of the global environ then it may now point to free'd memory
2277 if the environment has been modified since. To avoid this
2278 problem we treat env==NULL as meaning 'use the default'
2282 if (env != environ) {
2283 environ[0] = Nullch;
2284 hv_magic(hv, envgv, 'E');
2286 for (; *env; env++) {
2287 if (!(s = strchr(*env,'=')))
2290 sv = newSVpv(s--,0);
2291 sv_magic(sv, sv, 'e', *env, s - *env);
2292 (void)hv_store(hv, *env, s - *env, sv, 0);
2296 #ifdef DYNAMIC_ENV_FETCH
2297 HvNAME(hv) = savepv(ENV_HV_NAME);
2299 hv_magic(hv, envgv, 'E');
2302 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
2303 sv_setiv(GvSV(tmpgv), (IV)getpid());
2312 s = getenv("PERL5LIB");
2316 incpush(getenv("PERLLIB"), FALSE);
2318 /* Treat PERL5?LIB as a possible search list logical name -- the
2319 * "natural" VMS idiom for a Unix path string. We allow each
2320 * element to be a set of |-separated directories for compatibility.
2324 if (my_trnlnm("PERL5LIB",buf,0))
2325 do { incpush(buf,TRUE); } while (my_trnlnm("PERL5LIB",buf,++idx));
2327 while (my_trnlnm("PERLLIB",buf,idx++)) incpush(buf,FALSE);
2331 /* Use the ~-expanded versions of APPLIB (undocumented),
2332 ARCHLIB PRIVLIB SITEARCH SITELIB and OLDARCHLIB
2335 incpush(APPLLIB_EXP, FALSE);
2339 incpush(ARCHLIB_EXP, FALSE);
2342 #define PRIVLIB_EXP "/usr/local/lib/perl5:/usr/local/lib/perl"
2344 incpush(PRIVLIB_EXP, FALSE);
2347 incpush(SITEARCH_EXP, FALSE);
2350 incpush(SITELIB_EXP, FALSE);
2352 #ifdef OLDARCHLIB_EXP /* 5.00[01] compatibility */
2353 incpush(OLDARCHLIB_EXP, FALSE);
2357 incpush(".", FALSE);
2361 # define PERLLIB_SEP ';'
2364 # define PERLLIB_SEP '|'
2366 # define PERLLIB_SEP ':'
2369 #ifndef PERLLIB_MANGLE
2370 # define PERLLIB_MANGLE(s,n) (s)
2374 incpush(p, addsubdirs)
2378 SV *subdir = Nullsv;
2379 static char *archpat_auto;
2386 if (!archpat_auto) {
2387 STRLEN len = (sizeof(ARCHNAME) + strlen(patchlevel)
2388 + sizeof("//auto"));
2389 New(55, archpat_auto, len, char);
2390 sprintf(archpat_auto, "/%s/%s/auto", ARCHNAME, patchlevel);
2392 for (len = sizeof(ARCHNAME) + 2;
2393 archpat_auto[len] != '\0' && archpat_auto[len] != '/'; len++)
2394 if (archpat_auto[len] == '.') archpat_auto[len] = '_';
2399 /* Break at all separators */
2401 SV *libdir = newSV(0);
2404 /* skip any consecutive separators */
2405 while ( *p == PERLLIB_SEP ) {
2406 /* Uncomment the next line for PATH semantics */
2407 /* av_push(GvAVn(incgv), newSVpv(".", 1)); */
2411 if ( (s = strchr(p, PERLLIB_SEP)) != Nullch ) {
2412 sv_setpvn(libdir, PERLLIB_MANGLE(p, (STRLEN)(s - p)),
2417 sv_setpv(libdir, PERLLIB_MANGLE(p, 0));
2418 p = Nullch; /* break out */
2422 * BEFORE pushing libdir onto @INC we may first push version- and
2423 * archname-specific sub-directories.
2426 struct stat tmpstatbuf;
2431 if ((unix = tounixspec_ts(SvPV(libdir,na),Nullch)) != Nullch) {
2433 while (unix[len-1] == '/') len--; /* Cosmetic */
2434 sv_usepvn(libdir,unix,len);
2437 PerlIO_printf(PerlIO_stderr(),
2438 "Failed to unixify @INC element \"%s\"\n",
2441 /* .../archname/version if -d .../archname/version/auto */
2442 sv_setsv(subdir, libdir);
2443 sv_catpv(subdir, archpat_auto);
2444 if (Stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
2445 S_ISDIR(tmpstatbuf.st_mode))
2446 av_push(GvAVn(incgv),
2447 newSVpv(SvPVX(subdir), SvCUR(subdir) - sizeof "auto"));
2449 /* .../archname if -d .../archname/auto */
2450 sv_insert(subdir, SvCUR(libdir) + sizeof(ARCHNAME),
2451 strlen(patchlevel) + 1, "", 0);
2452 if (Stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
2453 S_ISDIR(tmpstatbuf.st_mode))
2454 av_push(GvAVn(incgv),
2455 newSVpv(SvPVX(subdir), SvCUR(subdir) - sizeof "auto"));
2458 /* finally push this lib directory on the end of @INC */
2459 av_push(GvAVn(incgv), libdir);
2462 SvREFCNT_dec(subdir);
2466 call_list(oldscope, list)
2472 line_t oldline = curcop->cop_line;
2474 while (AvFILL(list) >= 0) {
2475 CV *cv = (CV*)av_shift(list);
2479 switch (JMPENV_PUSH) {
2481 SV* atsv = GvSV(errgv);
2483 perl_call_sv((SV*)cv, G_EVAL|G_DISCARD);
2484 (void)SvPV(atsv, len);
2487 curcop = &compiling;
2488 curcop->cop_line = oldline;
2489 if (list == beginav)
2490 sv_catpv(atsv, "BEGIN failed--compilation aborted");
2492 sv_catpv(atsv, "END failed--cleanup aborted");
2493 while (scopestack_ix > oldscope)
2495 croak("%s", SvPVX(atsv));
2503 /* my_exit() was called */
2504 while (scopestack_ix > oldscope)
2506 curstash = defstash;
2508 call_list(oldscope, endav);
2511 curcop = &compiling;
2512 curcop->cop_line = oldline;
2514 if (list == beginav)
2515 croak("BEGIN failed--compilation aborted");
2517 croak("END failed--cleanup aborted");
2523 PerlIO_printf(PerlIO_stderr(), "panic: restartop\n");
2528 curcop = &compiling;
2529 curcop->cop_line = oldline;
2548 STATUS_NATIVE_SET(status);
2558 if (vaxc$errno & 1) {
2559 if (STATUS_NATIVE & 1) /* fortuitiously includes "-1" */
2560 STATUS_NATIVE_SET(44);
2563 if (!vaxc$errno && errno) /* unlikely */
2564 STATUS_NATIVE_SET(44);
2566 STATUS_NATIVE_SET(vaxc$errno);
2570 STATUS_POSIX_SET(errno);
2571 else if (STATUS_POSIX == 0)
2572 STATUS_POSIX_SET(255);
2580 register CONTEXT *cx;
2589 (void)UNLINK(e_tmpname);
2590 Safefree(e_tmpname);
2594 if (cxstack_ix >= 0) {