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; \
65 static void find_beginning _((void));
66 static void forbid_setid _((char *));
67 static void incpush _((char *, int));
68 static void init_ids _((void));
69 static void init_debugger _((void));
70 static void init_lexer _((void));
71 static void init_main_stash _((void));
72 static void init_perllib _((void));
73 static void init_postdump_symbols _((int, char **, char **));
74 static void init_predump_symbols _((void));
75 static void init_stacks _((void));
76 static void my_exit_jump _((void)) __attribute__((noreturn));
77 static void nuke_stacks _((void));
78 static void open_script _((char *, bool, SV *));
79 static void usage _((char *));
80 static void validate_suid _((char *, char*));
82 static int fdscript = -1;
87 PerlInterpreter *sv_interp;
90 New(53, sv_interp, 1, PerlInterpreter);
95 perl_construct( sv_interp )
96 register PerlInterpreter *sv_interp;
98 if (!(curinterp = sv_interp))
102 Zero(sv_interp, 1, PerlInterpreter);
105 /* Init the real globals? */
107 linestr = NEWSV(65,80);
108 sv_upgrade(linestr,SVt_PVIV);
110 if (!SvREADONLY(&sv_undef)) {
111 SvREADONLY_on(&sv_undef);
115 SvREADONLY_on(&sv_no);
117 sv_setpv(&sv_yes,Yes);
119 SvREADONLY_on(&sv_yes);
122 nrs = newSVpv("\n", 1);
123 rs = SvREFCNT_inc(nrs);
129 * There is no way we can refer to them from Perl so close them to save
130 * space. The other alternative would be to provide STDAUX and STDPRN
133 (void)fclose(stdaux);
134 (void)fclose(stdprn);
140 perl_destruct_level = 1;
142 if(perl_destruct_level > 0)
147 lex_state = LEX_NOTPARSING;
149 start_env.je_prev = NULL;
150 start_env.je_ret = -1;
151 start_env.je_mustcatch = TRUE;
152 top_env = &start_env;
155 SET_NUMERIC_STANDARD();
156 #if defined(SUBVERSION) && SUBVERSION > 0
157 sprintf(patchlevel, "%7.5f", (double) 5
158 + ((double) PATCHLEVEL / (double) 1000)
159 + ((double) SUBVERSION / (double) 100000));
161 sprintf(patchlevel, "%5.3f", (double) 5 +
162 ((double) PATCHLEVEL / (double) 1000));
165 #if defined(LOCAL_PATCH_COUNT)
166 localpatches = local_patches; /* For possible -v */
169 PerlIO_init(); /* Hook to IO system */
171 fdpid = newAV(); /* for remembering popen pids by fd */
178 perl_destruct(sv_interp)
179 register PerlInterpreter *sv_interp;
181 int destruct_level; /* 0=none, 1=full, 2=full with checks */
185 if (!(curinterp = sv_interp))
188 destruct_level = perl_destruct_level;
192 if (s = getenv("PERL_DESTRUCT_LEVEL")) {
194 if (destruct_level < i)
203 /* We must account for everything. */
205 /* Destroy the main CV and syntax tree */
207 curpad = AvARRAY(comppad);
212 SvREFCNT_dec(main_cv);
217 * Try to destruct global references. We do this first so that the
218 * destructors and destructees still exist. Some sv's might remain.
219 * Non-referenced objects are on their own.
226 /* unhook hooks which will soon be, or use, destroyed data */
227 SvREFCNT_dec(warnhook);
229 SvREFCNT_dec(diehook);
231 SvREFCNT_dec(parsehook);
234 if (destruct_level == 0){
236 DEBUG_P(debprofdump());
238 /* The exit() function will do everything that needs doing. */
242 /* loosen bonds of global variables */
245 (void)PerlIO_close(rsfp);
249 /* Filters for program text */
250 SvREFCNT_dec(rsfp_filters);
251 rsfp_filters = Nullav;
263 sawampersand = FALSE; /* must save all match strings */
264 sawstudy = FALSE; /* do fbm_instr on all strings */
279 /* magical thingies */
281 Safefree(ofs); /* $, */
284 Safefree(ors); /* $\ */
287 SvREFCNT_dec(nrs); /* $\ helper */
290 multiline = 0; /* $* */
292 SvREFCNT_dec(statname);
296 /* defgv, aka *_ should be taken care of elsewhere */
298 #if 0 /* just about all regexp stuff, seems to be ok */
300 /* shortcuts to regexp stuff */
305 SAVEFREEOP(oldlastpm); /* for saving regexp context during debugger */
307 regprecomp = NULL; /* uncompiled string. */
308 regparse = NULL; /* Input-scan pointer. */
309 regxend = NULL; /* End of input for compile */
310 regnpar = 0; /* () count. */
311 regcode = NULL; /* Code-emit pointer; ®dummy = don't. */
312 regsize = 0; /* Code size. */
313 regnaughty = 0; /* How bad is this pattern? */
314 regsawback = 0; /* Did we see \1, ...? */
316 reginput = NULL; /* String-input pointer. */
317 regbol = NULL; /* Beginning of input, for ^ check. */
318 regeol = NULL; /* End of input, for $ check. */
319 regstartp = (char **)NULL; /* Pointer to startp array. */
320 regendp = (char **)NULL; /* Ditto for endp. */
321 reglastparen = 0; /* Similarly for lastparen. */
322 regtill = NULL; /* How far we are required to go. */
323 regflags = 0; /* are we folding, multilining? */
324 regprev = (char)NULL; /* char before regbol, \n if none */
328 /* clean up after study() */
329 SvREFCNT_dec(lastscream);
331 Safefree(screamfirst);
333 Safefree(screamnext);
336 /* startup and shutdown function lists */
337 SvREFCNT_dec(beginav);
342 /* temp stack during pp_sort() */
343 SvREFCNT_dec(sortstack);
346 /* shortcuts just get cleared */
356 /* reset so print() ends up where we expect */
359 /* Prepare to destruct main symbol table. */
366 if (destruct_level >= 2) {
367 if (scopestack_ix != 0)
368 warn("Unbalanced scopes: %ld more ENTERs than LEAVEs\n",
369 (long)scopestack_ix);
370 if (savestack_ix != 0)
371 warn("Unbalanced saves: %ld more saves than restores\n",
373 if (tmps_floor != -1)
374 warn("Unbalanced tmps: %ld more allocs than frees\n",
375 (long)tmps_floor + 1);
376 if (cxstack_ix != -1)
377 warn("Unbalanced context: %ld more PUSHes than POPs\n",
378 (long)cxstack_ix + 1);
381 /* Now absolutely destruct everything, somehow or other, loops or no. */
383 SvFLAGS(strtab) |= SVTYPEMASK; /* don't clean out strtab now */
384 while (sv_count != 0 && sv_count != last_sv_count) {
385 last_sv_count = sv_count;
388 SvFLAGS(strtab) &= ~SVTYPEMASK;
389 SvFLAGS(strtab) |= SVt_PVHV;
391 /* Destruct the global string table. */
393 /* Yell and reset the HeVAL() slots that are still holding refcounts,
394 * so that sv_free() won't fail on them.
403 array = HvARRAY(strtab);
407 warn("Unbalanced string table refcount: (%d) for \"%s\"",
408 HeVAL(hent) - Nullsv, HeKEY(hent));
409 HeVAL(hent) = Nullsv;
419 SvREFCNT_dec(strtab);
422 warn("Scalars leaked: %ld\n", (long)sv_count);
426 /* No SVs have survived, need to clean out */
430 Safefree(origfilename);
432 hints = 0; /* Reset hints. Should hints be per-interpreter ? */
434 DEBUG_P(debprofdump());
436 /* As the absolutely last thing, free the non-arena SV for mess() */
439 /* we know that type >= SVt_PV */
441 Safefree(SvPVX(mess_sv));
442 Safefree(SvANY(mess_sv));
450 PerlInterpreter *sv_interp;
452 if (!(curinterp = sv_interp))
458 perl_parse(sv_interp, xsinit, argc, argv, env)
459 PerlInterpreter *sv_interp;
460 void (*xsinit)_((void));
467 char *scriptname = NULL;
468 VOL bool dosearch = FALSE;
475 #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
478 croak("suidperl is no longer needed since the kernel can now execute\n\
479 setuid perl scripts securely.\n");
483 if (!(curinterp = sv_interp))
486 #if defined(NeXT) && defined(__DYNAMIC__)
487 _dyld_lookup_and_bind
488 ("__environ", (unsigned long *) &environ_pointer, NULL);
493 #ifndef VMS /* VMS doesn't have environ array */
494 origenviron = environ;
500 /* Come here if running an undumped a.out. */
502 origfilename = savepv(argv[0]);
504 cxstack_ix = -1; /* start label stack again */
506 init_postdump_symbols(argc,argv,env);
511 curpad = AvARRAY(comppad);
516 SvREFCNT_dec(main_cv);
520 oldscope = scopestack_ix;
528 /* my_exit() was called */
529 while (scopestack_ix > oldscope)
534 call_list(oldscope, endav);
536 return STATUS_NATIVE_EXPORT;
539 PerlIO_printf(PerlIO_stderr(), "panic: top_env\n");
543 sv_setpvn(linestr,"",0);
544 sv = newSVpv("",0); /* first used for -I flags */
548 for (argc--,argv++; argc > 0; argc--,argv++) {
549 if (argv[0][0] != '-' || !argv[0][1])
553 validarg = " PHOOEY ";
578 if (s = moreswitches(s))
588 if (euid != uid || egid != gid)
589 croak("No -e allowed in setuid scripts");
591 e_tmpname = savepv(TMPPATH);
592 (void)mktemp(e_tmpname);
594 croak("Can't mktemp()");
595 e_fp = PerlIO_open(e_tmpname,"w");
597 croak("Cannot open temporary file");
602 PerlIO_puts(e_fp,argv[1]);
606 croak("No code specified for -e");
607 (void)PerlIO_putc(e_fp,'\n');
609 case 'I': /* -I handled both here and in moreswitches() */
611 if (!*++s && (s=argv[1]) != Nullch) {
614 while (s && isSPACE(*s))
618 for (e = s; *e && !isSPACE(*e); e++) ;
625 } /* XXX else croak? */
639 preambleav = newAV();
640 av_push(preambleav, newSVpv("use Config qw(myconfig config_vars)",0));
642 Sv = newSVpv("print myconfig();",0);
644 sv_catpv(Sv,"print \"\\nCharacteristics of this PERLSHR image: \\n\",");
646 sv_catpv(Sv,"print \"\\nCharacteristics of this binary (from libperl): \\n\",");
648 #if defined(DEBUGGING) || defined(NO_EMBED) || defined(MULTIPLICITY)
649 sv_catpv(Sv,"\" Compile-time options:");
651 sv_catpv(Sv," DEBUGGING");
654 sv_catpv(Sv," NO_EMBED");
657 sv_catpv(Sv," MULTIPLICITY");
659 sv_catpv(Sv,"\\n\",");
661 #if defined(LOCAL_PATCH_COUNT)
662 if (LOCAL_PATCH_COUNT > 0) {
664 sv_catpv(Sv,"\" Locally applied patches:\\n\",");
665 for (i = 1; i <= LOCAL_PATCH_COUNT; i++) {
667 sv_catpvf(Sv,"\" \\t%s\\n\",",localpatches[i]);
671 sv_catpvf(Sv,"\" Built under %s\\n\"",OSNAME);
674 sv_catpvf(Sv,",\" Compiled at %s %s\\n\"",__DATE__,__TIME__);
676 sv_catpvf(Sv,",\" Compiled on %s\\n\"",__DATE__);
681 @env = map { \"$_=\\\"$ENV{$_}\\\"\" } sort grep {/^PERL/} keys %ENV; \
682 print \" \\%ENV:\\n @env\\n\" if @env; \
683 print \" \\@INC:\\n @INC\\n\";");
686 Sv = newSVpv("config_vars(qw(",0);
691 av_push(preambleav, Sv);
692 scriptname = BIT_BUCKET; /* don't look for script or read stdin */
703 if (!*++s || isSPACE(*s)) {
707 /* catch use of gnu style long options */
708 if (strEQ(s, "version")) {
712 if (strEQ(s, "help")) {
719 croak("Unrecognized switch: -%s (-h will show valid options)",s);
724 if (!tainting && (s = getenv("PERL5OPT"))) {
735 if (!strchr("DIMUdmw", *s))
736 croak("Illegal switch in PERL5OPT: -%c", *s);
742 scriptname = argv[0];
744 if (PerlIO_flush(e_fp) || PerlIO_error(e_fp) || PerlIO_close(e_fp)) {
746 warn("Did you forget to compile with -DMULTIPLICITY?");
748 croak("Can't write to temp file for -e: %s", Strerror(errno));
752 scriptname = e_tmpname;
754 else if (scriptname == Nullch) {
756 if ( isatty(PerlIO_fileno(PerlIO_stdin())) )
764 open_script(scriptname,dosearch,sv);
766 validate_suid(validarg, scriptname);
771 main_cv = compcv = (CV*)NEWSV(1104,0);
772 sv_upgrade((SV *)compcv, SVt_PVCV);
776 av_push(comppad, Nullsv);
777 curpad = AvARRAY(comppad);
778 comppad_name = newAV();
779 comppad_name_fill = 0;
780 min_intro_pending = 0;
783 comppadlist = newAV();
784 AvREAL_off(comppadlist);
785 av_store(comppadlist, 0, (SV*)comppad_name);
786 av_store(comppadlist, 1, (SV*)comppad);
787 CvPADLIST(compcv) = comppadlist;
789 boot_core_UNIVERSAL();
791 (*xsinit)(); /* in case linked C routines want magical variables */
792 #if defined(VMS) || defined(WIN32)
796 init_predump_symbols();
798 init_postdump_symbols(argc,argv,env);
802 /* now parse the script */
805 if (yyparse() || error_count) {
807 croak("%s had compilation errors.\n", origfilename);
809 croak("Execution of %s aborted due to compilation errors.\n",
813 curcop->cop_line = 0;
817 (void)UNLINK(e_tmpname);
822 /* now that script is parsed, we can modify record separator */
824 rs = SvREFCNT_inc(nrs);
825 sv_setsv(GvSV(gv_fetchpv("/", TRUE, SVt_PV)), rs);
837 if ((s=getenv("PERL_DEBUG_MSTATS")) && atoi(s) >= 2)
838 dump_mstats("after compilation:");
849 PerlInterpreter *sv_interp;
855 if (!(curinterp = sv_interp))
858 oldscope = scopestack_ix;
863 cxstack_ix = -1; /* start context stack again */
866 /* my_exit() was called */
867 while (scopestack_ix > oldscope)
872 call_list(oldscope, endav);
874 if (getenv("PERL_DEBUG_MSTATS"))
875 dump_mstats("after execution: ");
878 return STATUS_NATIVE_EXPORT;
881 PerlIO_printf(PerlIO_stderr(), "panic: restartop\n");
886 if (curstack != mainstack) {
888 SWITCHSTACK(curstack, mainstack);
893 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s $` $& $' support.\n",
894 sawampersand ? "Enabling" : "Omitting"));
898 DEBUG(PerlIO_printf(Perl_debug_log, "\nEXECUTING...\n\n"));
901 PerlIO_printf(PerlIO_stderr(), "%s syntax OK\n", origfilename);
904 if (PERLDB_SINGLE && DBsingle)
905 sv_setiv(DBsingle, 1);
915 else if (main_start) {
916 CvDEPTH(main_cv) = 1;
927 perl_get_sv(name, create)
931 GV* gv = gv_fetchpv(name, create, SVt_PV);
938 perl_get_av(name, create)
942 GV* gv = gv_fetchpv(name, create, SVt_PVAV);
951 perl_get_hv(name, create)
955 GV* gv = gv_fetchpv(name, create, SVt_PVHV);
964 perl_get_cv(name, create)
968 GV* gv = gv_fetchpv(name, create, SVt_PVCV);
969 if (create && !GvCVu(gv))
970 return newSUB(start_subparse(FALSE, 0),
971 newSVOP(OP_CONST, 0, newSVpv(name,0)),
979 /* Be sure to refetch the stack pointer after calling these routines. */
982 perl_call_argv(subname, flags, argv)
984 I32 flags; /* See G_* flags in cop.h */
985 register char **argv; /* null terminated arg list */
992 XPUSHs(sv_2mortal(newSVpv(*argv,0)));
997 return perl_call_pv(subname, flags);
1001 perl_call_pv(subname, flags)
1002 char *subname; /* name of the subroutine */
1003 I32 flags; /* See G_* flags in cop.h */
1005 return perl_call_sv((SV*)perl_get_cv(subname, TRUE), flags);
1009 perl_call_method(methname, flags)
1010 char *methname; /* name of the subroutine */
1011 I32 flags; /* See G_* flags in cop.h */
1017 XPUSHs(sv_2mortal(newSVpv(methname,0)));
1020 return perl_call_sv(*stack_sp--, flags);
1023 /* May be called with any of a CV, a GV, or an SV containing the name. */
1025 perl_call_sv(sv, flags)
1027 I32 flags; /* See G_* flags in cop.h */
1029 LOGOP myop; /* fake syntax tree node */
1035 bool oldcatch = CATCH_GET;
1040 if (flags & G_DISCARD) {
1045 Zero(&myop, 1, LOGOP);
1046 myop.op_next = Nullop;
1047 if (!(flags & G_NOARGS))
1048 myop.op_flags |= OPf_STACKED;
1049 myop.op_flags |= ((flags & G_VOID) ? OPf_WANT_VOID :
1050 (flags & G_ARRAY) ? OPf_WANT_LIST :
1055 EXTEND(stack_sp, 1);
1058 oldscope = scopestack_ix;
1060 if (PERLDB_SUB && curstash != debstash
1061 /* Handle first BEGIN of -d. */
1062 && (DBcv || (DBcv = GvCV(DBsub)))
1063 /* Try harder, since this may have been a sighandler, thus
1064 * curstash may be meaningless. */
1065 && (SvTYPE(sv) != SVt_PVCV || CvSTASH((CV*)sv) != debstash))
1066 op->op_private |= OPpENTERSUB_DB;
1068 if (flags & G_EVAL) {
1069 cLOGOP->op_other = op;
1071 /* we're trying to emulate pp_entertry() here */
1073 register CONTEXT *cx;
1074 I32 gimme = GIMME_V;
1079 push_return(op->op_next);
1080 PUSHBLOCK(cx, CXt_EVAL, stack_sp);
1082 eval_root = op; /* Only needed so that goto works right. */
1085 if (flags & G_KEEPERR)
1088 sv_setpv(GvSV(errgv),"");
1100 /* my_exit() was called */
1101 curstash = defstash;
1105 croak("Callback called exit");
1114 stack_sp = stack_base + oldmark;
1115 if (flags & G_ARRAY)
1119 *++stack_sp = &sv_undef;
1127 if (op == (OP*)&myop)
1131 retval = stack_sp - (stack_base + oldmark);
1132 if ((flags & G_EVAL) && !(flags & G_KEEPERR))
1133 sv_setpv(GvSV(errgv),"");
1136 if (flags & G_EVAL) {
1137 if (scopestack_ix > oldscope) {
1141 register CONTEXT *cx;
1153 CATCH_SET(oldcatch);
1155 if (flags & G_DISCARD) {
1156 stack_sp = stack_base + oldmark;
1165 /* Eval a string. The G_EVAL flag is always assumed. */
1168 perl_eval_sv(sv, flags)
1170 I32 flags; /* See G_* flags in cop.h */
1172 UNOP myop; /* fake syntax tree node */
1174 I32 oldmark = sp - stack_base;
1181 if (flags & G_DISCARD) {
1189 EXTEND(stack_sp, 1);
1191 oldscope = scopestack_ix;
1193 if (!(flags & G_NOARGS))
1194 myop.op_flags = OPf_STACKED;
1195 myop.op_next = Nullop;
1196 myop.op_type = OP_ENTEREVAL;
1197 myop.op_flags |= ((flags & G_VOID) ? OPf_WANT_VOID :
1198 (flags & G_ARRAY) ? OPf_WANT_LIST :
1200 if (flags & G_KEEPERR)
1201 myop.op_flags |= OPf_SPECIAL;
1211 /* my_exit() was called */
1212 curstash = defstash;
1216 croak("Callback called exit");
1225 stack_sp = stack_base + oldmark;
1226 if (flags & G_ARRAY)
1230 *++stack_sp = &sv_undef;
1235 if (op == (OP*)&myop)
1236 op = pp_entereval();
1239 retval = stack_sp - (stack_base + oldmark);
1240 if (!(flags & G_KEEPERR))
1241 sv_setpv(GvSV(errgv),"");
1245 if (flags & G_DISCARD) {
1246 stack_sp = stack_base + oldmark;
1256 perl_eval_pv(p, croak_on_error)
1261 SV* sv = newSVpv(p, 0);
1264 perl_eval_sv(sv, G_SCALAR);
1271 if (croak_on_error && SvTRUE(GvSV(errgv)))
1272 croak(SvPVx(GvSV(errgv), na));
1277 /* Require a module. */
1283 SV* sv = sv_newmortal();
1284 sv_setpv(sv, "require '");
1287 perl_eval_sv(sv, G_DISCARD);
1291 magicname(sym,name,namlen)
1298 if (gv = gv_fetchpv(sym,TRUE, SVt_PV))
1299 sv_magic(GvSV(gv), (SV*)gv, 0, name, namlen);
1303 usage(name) /* XXX move this out into a module ? */
1306 /* This message really ought to be max 23 lines.
1307 * Removed -h because the user already knows that opton. Others? */
1309 static char *usage[] = {
1310 "-0[octal] specify record separator (\\0, if no argument)",
1311 "-a autosplit mode with -n or -p (splits $_ into @F)",
1312 "-c check syntax only (runs BEGIN and END blocks)",
1313 "-d[:debugger] run scripts under debugger",
1314 "-D[number/list] set debugging flags (argument is a bit mask or flags)",
1315 "-e 'command' one line of script. Several -e's allowed. Omit [programfile].",
1316 "-F/pattern/ split() pattern for autosplit (-a). The //'s are optional.",
1317 "-i[extension] edit <> files in place (make backup if extension supplied)",
1318 "-Idirectory specify @INC/#include directory (may be used more than once)",
1319 "-l[octal] enable line ending processing, specifies line terminator",
1320 "-[mM][-]module.. executes `use/no module...' before executing your script.",
1321 "-n assume 'while (<>) { ... }' loop around your script",
1322 "-p assume loop like -n but print line also like sed",
1323 "-P run script through C preprocessor before compilation",
1324 "-s enable some switch parsing for switches after script name",
1325 "-S look for the script using PATH environment variable",
1326 "-T turn on tainting checks",
1327 "-u dump core after parsing script",
1328 "-U allow unsafe operations",
1329 "-v print version number and patchlevel of perl",
1330 "-V[:variable] print perl configuration information",
1331 "-w TURN WARNINGS ON FOR COMPILATION OF YOUR SCRIPT. Recommended.",
1332 "-x[directory] strip off text before #!perl line and perhaps cd to directory",
1338 printf("\nUsage: %s [switches] [--] [programfile] [arguments]", name);
1340 printf("\n %s", *p++);
1343 /* This routine handles any switches that can be given during run */
1354 rschar = scan_oct(s, 4, &numlen);
1356 if (rschar & ~((U8)~0))
1358 else if (!rschar && numlen >= 2)
1359 nrs = newSVpv("", 0);
1362 nrs = newSVpv(&ch, 1);
1367 splitstr = savepv(s + 1);
1381 if (*s == ':' || *s == '=') {
1382 my_setenv("PERL5DB", form("use Devel::%s;", ++s));
1386 perldb = PERLDB_ALL;
1393 if (isALPHA(s[1])) {
1394 static char debopts[] = "psltocPmfrxuLHXD";
1397 for (s++; *s && (d = strchr(debopts,*s)); s++)
1398 debug |= 1 << (d - debopts);
1402 for (s++; isDIGIT(*s); s++) ;
1404 debug |= 0x80000000;
1406 warn("Recompile perl with -DDEBUGGING to use -D switch\n");
1407 for (s++; isALNUM(*s); s++) ;
1417 inplace = savepv(s+1);
1419 for (s = inplace; *s && !isSPACE(*s); s++) ;
1423 case 'I': /* -I handled both here and in parse_perl() */
1426 while (*s && isSPACE(*s))
1430 for (e = s; *e && !isSPACE(*e); e++) ;
1431 p = savepvn(s, e-s);
1437 croak("No space allowed after -I");
1447 *ors = scan_oct(s, 3 + (*s == '0'), &numlen);
1456 ors = SvPV(nrs, orslen);
1457 ors = savepvn(ors, orslen);
1461 forbid_setid("-M"); /* XXX ? */
1464 forbid_setid("-m"); /* XXX ? */
1468 /* -M-foo == 'no foo' */
1469 if (*s == '-') { use = "no "; ++s; }
1470 Sv = newSVpv(use,0);
1472 /* We allow -M'Module qw(Foo Bar)' */
1473 while(isALNUM(*s) || *s==':') ++s;
1475 sv_catpv(Sv, start);
1476 if (*(start-1) == 'm') {
1478 croak("Can't use '%c' after -mname", *s);
1479 sv_catpv( Sv, " ()");
1482 sv_catpvn(Sv, start, s-start);
1483 sv_catpv(Sv, " split(/,/,q{");
1488 if (preambleav == NULL)
1489 preambleav = newAV();
1490 av_push(preambleav, Sv);
1493 croak("No space allowed after -%c", *(s-1));
1510 croak("Too late for \"-T\" option");
1522 #if defined(SUBVERSION) && SUBVERSION > 0
1523 printf("\nThis is perl, version 5.%03d_%02d built for %s",
1524 PATCHLEVEL, SUBVERSION, ARCHNAME);
1526 printf("\nThis is perl, version %s built for %s",
1527 patchlevel, ARCHNAME);
1529 #if defined(LOCAL_PATCH_COUNT)
1530 if (LOCAL_PATCH_COUNT > 0)
1531 printf("\n(with %d registered patch%s, see perl -V for more detail)",
1532 LOCAL_PATCH_COUNT, (LOCAL_PATCH_COUNT!=1) ? "es" : "");
1535 printf("\n\nCopyright 1987-1997, Larry Wall\n");
1537 printf("\nMS-DOS port Copyright (c) 1989, 1990, Diomidis Spinellis\n");
1540 printf("djgpp v2 port (jpl5003c) by Hirofumi Watanabe, 1996\n");
1543 printf("\n\nOS/2 port Copyright (c) 1990, 1991, Raymond Chen, Kai Uwe Rommel\n"
1544 "Version 5 port Copyright (c) 1994-1997, Andreas Kaiser, Ilya Zakharevich\n");
1547 printf("atariST series port, ++jrb bammi@cadence.com\n");
1550 Perl may be copied only under the terms of either the Artistic License or the\n\
1551 GNU General Public License, which may be found in the Perl 5.0 source kit.\n\n");
1559 if (s[1] == '-') /* Additional switches on #! line. */
1567 #ifdef ALTERNATE_SHEBANG
1568 case 'S': /* OS/2 needs -S on "extproc" line. */
1576 croak("Can't emulate -%.1s on #! line",s);
1581 /* compliments of Tom Christiansen */
1583 /* unexec() can be found in the Gnu emacs distribution */
1594 prog = newSVpv(BIN_EXP);
1595 sv_catpv(prog, "/perl");
1596 file = newSVpv(origfilename);
1597 sv_catpv(file, ".perldump");
1599 status = unexec(SvPVX(file), SvPVX(prog), &etext, sbrk(0), 0);
1601 PerlIO_printf(PerlIO_stderr(), "unexec of %s into %s failed!\n",
1602 SvPVX(prog), SvPVX(file));
1606 # include <lib$routines.h>
1607 lib$signal(SS$_DEBUG); /* ssdef.h #included from vmsish.h */
1609 ABORT(); /* for use with undump */
1619 /* Note that strtab is a rather special HV. Assumptions are made
1620 about not iterating on it, and not adding tie magic to it.
1621 It is properly deallocated in perl_destruct() */
1623 HvSHAREKEYS_off(strtab); /* mandatory */
1624 Newz(506,((XPVHV*)SvANY(strtab))->xhv_array,
1625 sizeof(HE*) * (((XPVHV*)SvANY(strtab))->xhv_max + 1), char);
1627 curstash = defstash = newHV();
1628 curstname = newSVpv("main",4);
1629 gv = gv_fetchpv("main::",TRUE, SVt_PVHV);
1630 SvREFCNT_dec(GvHV(gv));
1631 GvHV(gv) = (HV*)SvREFCNT_inc(defstash);
1633 HvNAME(defstash) = savepv("main");
1634 incgv = gv_HVadd(gv_AVadd(gv_fetchpv("INC",TRUE, SVt_PVAV)));
1636 defgv = gv_fetchpv("_",TRUE, SVt_PVAV);
1637 errgv = gv_HVadd(gv_fetchpv("@", TRUE, SVt_PV));
1639 (void)form("%240s",""); /* Preallocate temp - for immediate signals. */
1640 sv_grow(GvSV(errgv), 240); /* Preallocate - for immediate signals. */
1641 sv_setpvn(GvSV(errgv), "", 0);
1642 curstash = defstash;
1643 compiling.cop_stash = defstash;
1644 debstash = GvHV(gv_fetchpv("DB::", GV_ADDMULTI, SVt_PVHV));
1645 /* We must init $/ before switches are processed. */
1646 sv_setpvn(GvSV(gv_fetchpv("/", TRUE, SVt_PV)), "\n", 1);
1649 #ifdef CAN_PROTOTYPE
1651 open_script(char *scriptname, bool dosearch, SV *sv)
1654 open_script(scriptname,dosearch,sv)
1660 char *xfound = Nullch;
1661 char *xfailed = Nullch;
1665 #if defined(DOSISH) && !defined(OS2) && !defined(atarist)
1666 # define SEARCH_EXTS ".bat", ".cmd", NULL
1667 # define MAX_EXT_LEN 4
1670 # define SEARCH_EXTS ".cmd", ".btm", ".bat", ".pl", NULL
1671 # define MAX_EXT_LEN 4
1674 # define SEARCH_EXTS ".pl", ".com", NULL
1675 # define MAX_EXT_LEN 4
1677 /* additional extensions to try in each dir if scriptname not found */
1679 char *ext[] = { SEARCH_EXTS };
1680 int extidx = 0, i = 0;
1681 char *curext = Nullch;
1683 # define MAX_EXT_LEN 0
1687 * If dosearch is true and if scriptname does not contain path
1688 * delimiters, search the PATH for scriptname.
1690 * If SEARCH_EXTS is also defined, will look for each
1691 * scriptname{SEARCH_EXTS} whenever scriptname is not found
1692 * while searching the PATH.
1694 * Assuming SEARCH_EXTS is C<".foo",".bar",NULL>, PATH search
1695 * proceeds as follows:
1697 * + look for ./scriptname{,.foo,.bar}
1698 * + search the PATH for scriptname{,.foo,.bar}
1701 * + look *only* in the PATH for scriptname{,.foo,.bar} (note
1702 * this will not look in '.' if it's not in the PATH)
1707 int hasdir, idx = 0, deftypes = 1;
1710 hasdir = (strpbrk(scriptname,":[</") != Nullch) ;
1711 /* The first time through, just add SEARCH_EXTS to whatever we
1712 * already have, so we can check for default file types. */
1714 (!hasdir && my_trnlnm("DCL$PATH",tokenbuf,idx++)) )
1720 if ((strlen(tokenbuf) + strlen(scriptname)
1721 + MAX_EXT_LEN) >= sizeof tokenbuf)
1722 continue; /* don't search dir with too-long name */
1723 strcat(tokenbuf, scriptname);
1727 if (strEQ(scriptname, "-"))
1729 if (dosearch) { /* Look in '.' first. */
1730 char *cur = scriptname;
1732 if ((curext = strrchr(scriptname,'.'))) /* possible current ext */
1734 if (strEQ(ext[i++],curext)) {
1735 extidx = -1; /* already has an ext */
1740 DEBUG_p(PerlIO_printf(Perl_debug_log,
1741 "Looking for %s\n",cur));
1742 if (Stat(cur,&statbuf) >= 0) {
1750 if (cur == scriptname) {
1751 len = strlen(scriptname);
1752 if (len+MAX_EXT_LEN+1 >= sizeof(tokenbuf))
1754 cur = strcpy(tokenbuf, scriptname);
1756 } while (extidx >= 0 && ext[extidx] /* try an extension? */
1757 && strcpy(tokenbuf+len, ext[extidx++]));
1762 if (dosearch && !strchr(scriptname, '/')
1764 && !strchr(scriptname, '\\')
1766 && (s = getenv("PATH"))) {
1769 bufend = s + strlen(s);
1770 while (s < bufend) {
1771 #if defined(atarist) || defined(DOSISH)
1776 && *s != ';'; len++, s++) {
1777 if (len < sizeof tokenbuf)
1780 if (len < sizeof tokenbuf)
1781 tokenbuf[len] = '\0';
1782 #else /* ! (atarist || DOSISH) */
1783 s = delimcpy(tokenbuf, tokenbuf + sizeof tokenbuf, s, bufend,
1786 #endif /* ! (atarist || DOSISH) */
1789 if (len + 1 + strlen(scriptname) + MAX_EXT_LEN >= sizeof tokenbuf)
1790 continue; /* don't search dir with too-long name */
1792 #if defined(atarist) || defined(DOSISH)
1793 && tokenbuf[len - 1] != '/'
1794 && tokenbuf[len - 1] != '\\'
1797 tokenbuf[len++] = '/';
1798 if (len == 2 && tokenbuf[0] == '.')
1800 (void)strcpy(tokenbuf + len, scriptname);
1804 len = strlen(tokenbuf);
1805 if (extidx > 0) /* reset after previous loop */
1809 DEBUG_p(PerlIO_printf(Perl_debug_log, "Looking for %s\n",tokenbuf));
1810 retval = Stat(tokenbuf,&statbuf);
1812 } while ( retval < 0 /* not there */
1813 && extidx>=0 && ext[extidx] /* try an extension? */
1814 && strcpy(tokenbuf+len, ext[extidx++])
1819 if (S_ISREG(statbuf.st_mode)
1820 && cando(S_IRUSR,TRUE,&statbuf)
1822 && cando(S_IXUSR,TRUE,&statbuf)
1826 xfound = tokenbuf; /* bingo! */
1830 xfailed = savepv(tokenbuf);
1833 if (!xfound && !seen_dot && !xfailed && (Stat(scriptname,&statbuf) < 0))
1835 seen_dot = 1; /* Disable message. */
1837 croak("Can't %s %s%s%s",
1838 (xfailed ? "execute" : "find"),
1839 (xfailed ? xfailed : scriptname),
1840 (xfailed ? "" : " on PATH"),
1841 (xfailed || seen_dot) ? "" : ", '.' not in PATH");
1844 scriptname = xfound;
1847 if (strnEQ(scriptname, "/dev/fd/", 8) && isDIGIT(scriptname[8]) ) {
1848 char *s = scriptname + 8;
1857 origfilename = savepv(e_tmpname ? "-e" : scriptname);
1858 curcop->cop_filegv = gv_fetchfile(origfilename);
1859 if (strEQ(origfilename,"-"))
1861 if (fdscript >= 0) {
1862 rsfp = PerlIO_fdopen(fdscript,"r");
1863 #if defined(HAS_FCNTL) && defined(F_SETFD)
1865 fcntl(PerlIO_fileno(rsfp),F_SETFD,1); /* ensure close-on-exec */
1868 else if (preprocess) {
1869 char *cpp_cfg = CPPSTDIN;
1870 SV *cpp = NEWSV(0,0);
1871 SV *cmd = NEWSV(0,0);
1873 if (strEQ(cpp_cfg, "cppstdin"))
1874 sv_catpvf(cpp, "%s/", BIN_EXP);
1875 sv_catpv(cpp, cpp_cfg);
1878 sv_catpv(sv,PRIVLIB_EXP);
1882 sed %s -e \"/^[^#]/b\" \
1883 -e \"/^#[ ]*include[ ]/b\" \
1884 -e \"/^#[ ]*define[ ]/b\" \
1885 -e \"/^#[ ]*if[ ]/b\" \
1886 -e \"/^#[ ]*ifdef[ ]/b\" \
1887 -e \"/^#[ ]*ifndef[ ]/b\" \
1888 -e \"/^#[ ]*else/b\" \
1889 -e \"/^#[ ]*elif[ ]/b\" \
1890 -e \"/^#[ ]*undef[ ]/b\" \
1891 -e \"/^#[ ]*endif/b\" \
1894 (doextract ? "-e \"1,/^#/d\n\"" : ""),
1897 %s %s -e '/^[^#]/b' \
1898 -e '/^#[ ]*include[ ]/b' \
1899 -e '/^#[ ]*define[ ]/b' \
1900 -e '/^#[ ]*if[ ]/b' \
1901 -e '/^#[ ]*ifdef[ ]/b' \
1902 -e '/^#[ ]*ifndef[ ]/b' \
1903 -e '/^#[ ]*else/b' \
1904 -e '/^#[ ]*elif[ ]/b' \
1905 -e '/^#[ ]*undef[ ]/b' \
1906 -e '/^#[ ]*endif/b' \
1914 (doextract ? "-e '1,/^#/d\n'" : ""),
1916 scriptname, cpp, sv, CPPMINUS);
1918 #ifdef IAMSUID /* actually, this is caught earlier */
1919 if (euid != uid && !euid) { /* if running suidperl */
1921 (void)seteuid(uid); /* musn't stay setuid root */
1924 (void)setreuid((Uid_t)-1, uid);
1926 #ifdef HAS_SETRESUID
1927 (void)setresuid((Uid_t)-1, uid, (Uid_t)-1);
1933 if (geteuid() != uid)
1934 croak("Can't do seteuid!\n");
1936 #endif /* IAMSUID */
1937 rsfp = my_popen(SvPVX(cmd), "r");
1941 else if (!*scriptname) {
1942 forbid_setid("program input from stdin");
1943 rsfp = PerlIO_stdin();
1946 rsfp = PerlIO_open(scriptname,"r");
1947 #if defined(HAS_FCNTL) && defined(F_SETFD)
1949 fcntl(PerlIO_fileno(rsfp),F_SETFD,1); /* ensure close-on-exec */
1957 #ifndef IAMSUID /* in case script is not readable before setuid */
1958 if (euid && Stat(SvPVX(GvSV(curcop->cop_filegv)),&statbuf) >= 0 &&
1959 statbuf.st_mode & (S_ISUID|S_ISGID)) {
1961 execv(form("%s/sperl%s", BIN_EXP, patchlevel), origargv);
1962 croak("Can't do setuid\n");
1966 croak("Can't open perl script \"%s\": %s\n",
1967 SvPVX(GvSV(curcop->cop_filegv)), Strerror(errno));
1972 validate_suid(validarg, scriptname)
1978 /* do we need to emulate setuid on scripts? */
1980 /* This code is for those BSD systems that have setuid #! scripts disabled
1981 * in the kernel because of a security problem. Merely defining DOSUID
1982 * in perl will not fix that problem, but if you have disabled setuid
1983 * scripts in the kernel, this will attempt to emulate setuid and setgid
1984 * on scripts that have those now-otherwise-useless bits set. The setuid
1985 * root version must be called suidperl or sperlN.NNN. If regular perl
1986 * discovers that it has opened a setuid script, it calls suidperl with
1987 * the same argv that it had. If suidperl finds that the script it has
1988 * just opened is NOT setuid root, it sets the effective uid back to the
1989 * uid. We don't just make perl setuid root because that loses the
1990 * effective uid we had before invoking perl, if it was different from the
1993 * DOSUID must be defined in both perl and suidperl, and IAMSUID must
1994 * be defined in suidperl only. suidperl must be setuid root. The
1995 * Configure script will set this up for you if you want it.
2001 if (Fstat(PerlIO_fileno(rsfp),&statbuf) < 0) /* normal stat is insecure */
2002 croak("Can't stat script \"%s\"",origfilename);
2003 if (fdscript < 0 && statbuf.st_mode & (S_ISUID|S_ISGID)) {
2007 #ifndef HAS_SETREUID
2008 /* On this access check to make sure the directories are readable,
2009 * there is actually a small window that the user could use to make
2010 * filename point to an accessible directory. So there is a faint
2011 * chance that someone could execute a setuid script down in a
2012 * non-accessible directory. I don't know what to do about that.
2013 * But I don't think it's too important. The manual lies when
2014 * it says access() is useful in setuid programs.
2016 if (access(SvPVX(GvSV(curcop->cop_filegv)),1)) /*double check*/
2017 croak("Permission denied");
2019 /* If we can swap euid and uid, then we can determine access rights
2020 * with a simple stat of the file, and then compare device and
2021 * inode to make sure we did stat() on the same file we opened.
2022 * Then we just have to make sure he or she can execute it.
2025 struct stat tmpstatbuf;
2029 setreuid(euid,uid) < 0
2032 setresuid(euid,uid,(Uid_t)-1) < 0
2035 || getuid() != euid || geteuid() != uid)
2036 croak("Can't swap uid and euid"); /* really paranoid */
2037 if (Stat(SvPVX(GvSV(curcop->cop_filegv)),&tmpstatbuf) < 0)
2038 croak("Permission denied"); /* testing full pathname here */
2039 if (tmpstatbuf.st_dev != statbuf.st_dev ||
2040 tmpstatbuf.st_ino != statbuf.st_ino) {
2041 (void)PerlIO_close(rsfp);
2042 if (rsfp = my_popen("/bin/mail root","w")) { /* heh, heh */
2044 "User %ld tried to run dev %ld ino %ld in place of dev %ld ino %ld!\n\
2045 (Filename of set-id script was %s, uid %ld gid %ld.)\n\nSincerely,\nperl\n",
2046 (long)uid,(long)tmpstatbuf.st_dev, (long)tmpstatbuf.st_ino,
2047 (long)statbuf.st_dev, (long)statbuf.st_ino,
2048 SvPVX(GvSV(curcop->cop_filegv)),
2049 (long)statbuf.st_uid, (long)statbuf.st_gid);
2050 (void)my_pclose(rsfp);
2052 croak("Permission denied\n");
2056 setreuid(uid,euid) < 0
2058 # if defined(HAS_SETRESUID)
2059 setresuid(uid,euid,(Uid_t)-1) < 0
2062 || getuid() != uid || geteuid() != euid)
2063 croak("Can't reswap uid and euid");
2064 if (!cando(S_IXUSR,FALSE,&statbuf)) /* can real uid exec? */
2065 croak("Permission denied\n");
2067 #endif /* HAS_SETREUID */
2068 #endif /* IAMSUID */
2070 if (!S_ISREG(statbuf.st_mode))
2071 croak("Permission denied");
2072 if (statbuf.st_mode & S_IWOTH)
2073 croak("Setuid/gid script is writable by world");
2074 doswitches = FALSE; /* -s is insecure in suid */
2076 if (sv_gets(linestr, rsfp, 0) == Nullch ||
2077 strnNE(SvPV(linestr,na),"#!",2) ) /* required even on Sys V */
2078 croak("No #! line");
2079 s = SvPV(linestr,na)+2;
2081 while (!isSPACE(*s)) s++;
2082 for (s2 = s; (s2 > SvPV(linestr,na)+2 &&
2083 (isDIGIT(s2[-1]) || strchr("._-", s2[-1]))); s2--) ;
2084 if (strnNE(s2-4,"perl",4) && strnNE(s-9,"perl",4)) /* sanity check */
2085 croak("Not a perl script");
2086 while (*s == ' ' || *s == '\t') s++;
2088 * #! arg must be what we saw above. They can invoke it by
2089 * mentioning suidperl explicitly, but they may not add any strange
2090 * arguments beyond what #! says if they do invoke suidperl that way.
2092 len = strlen(validarg);
2093 if (strEQ(validarg," PHOOEY ") ||
2094 strnNE(s,validarg,len) || !isSPACE(s[len]))
2095 croak("Args must match #! line");
2098 if (euid != uid && (statbuf.st_mode & S_ISUID) &&
2099 euid == statbuf.st_uid)
2101 croak("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
2102 FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
2103 #endif /* IAMSUID */
2105 if (euid) { /* oops, we're not the setuid root perl */
2106 (void)PerlIO_close(rsfp);
2109 execv(form("%s/sperl%s", BIN_EXP, patchlevel), origargv);
2111 croak("Can't do setuid\n");
2114 if (statbuf.st_mode & S_ISGID && statbuf.st_gid != egid) {
2116 (void)setegid(statbuf.st_gid);
2119 (void)setregid((Gid_t)-1,statbuf.st_gid);
2121 #ifdef HAS_SETRESGID
2122 (void)setresgid((Gid_t)-1,statbuf.st_gid,(Gid_t)-1);
2124 setgid(statbuf.st_gid);
2128 if (getegid() != statbuf.st_gid)
2129 croak("Can't do setegid!\n");
2131 if (statbuf.st_mode & S_ISUID) {
2132 if (statbuf.st_uid != euid)
2134 (void)seteuid(statbuf.st_uid); /* all that for this */
2137 (void)setreuid((Uid_t)-1,statbuf.st_uid);
2139 #ifdef HAS_SETRESUID
2140 (void)setresuid((Uid_t)-1,statbuf.st_uid,(Uid_t)-1);
2142 setuid(statbuf.st_uid);
2146 if (geteuid() != statbuf.st_uid)
2147 croak("Can't do seteuid!\n");
2149 else if (uid) { /* oops, mustn't run as root */
2151 (void)seteuid((Uid_t)uid);
2154 (void)setreuid((Uid_t)-1,(Uid_t)uid);
2156 #ifdef HAS_SETRESUID
2157 (void)setresuid((Uid_t)-1,(Uid_t)uid,(Uid_t)-1);
2163 if (geteuid() != uid)
2164 croak("Can't do seteuid!\n");
2167 if (!cando(S_IXUSR,TRUE,&statbuf))
2168 croak("Permission denied\n"); /* they can't do this */
2171 else if (preprocess)
2172 croak("-P not allowed for setuid/setgid script\n");
2173 else if (fdscript >= 0)
2174 croak("fd script not allowed in suidperl\n");
2176 croak("Script is not setuid/setgid in suidperl\n");
2178 /* We absolutely must clear out any saved ids here, so we */
2179 /* exec the real perl, substituting fd script for scriptname. */
2180 /* (We pass script name as "subdir" of fd, which perl will grok.) */
2181 PerlIO_rewind(rsfp);
2182 lseek(PerlIO_fileno(rsfp),(Off_t)0,0); /* just in case rewind didn't */
2183 for (which = 1; origargv[which] && origargv[which] != scriptname; which++) ;
2184 if (!origargv[which])
2185 croak("Permission denied");
2186 origargv[which] = savepv(form("/dev/fd/%d/%s",
2187 PerlIO_fileno(rsfp), origargv[which]));
2188 #if defined(HAS_FCNTL) && defined(F_SETFD)
2189 fcntl(PerlIO_fileno(rsfp),F_SETFD,0); /* ensure no close-on-exec */
2191 execv(form("%s/perl%s", BIN_EXP, patchlevel), origargv); /* try again */
2192 croak("Can't do setuid\n");
2193 #endif /* IAMSUID */
2195 if (euid != uid || egid != gid) { /* (suidperl doesn't exist, in fact) */
2196 #ifndef SETUID_SCRIPTS_ARE_SECURE_NOW
2197 Fstat(PerlIO_fileno(rsfp),&statbuf); /* may be either wrapped or real suid */
2198 if ((euid != uid && euid == statbuf.st_uid && statbuf.st_mode & S_ISUID)
2200 (egid != gid && egid == statbuf.st_gid && statbuf.st_mode & S_ISGID)
2203 croak("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
2204 FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
2205 #endif /* SETUID_SCRIPTS_ARE_SECURE_NOW */
2206 /* not set-id, must be wrapped */
2214 register char *s, *s2;
2216 /* skip forward in input to the real script? */
2220 if ((s = sv_gets(linestr, rsfp, 0)) == Nullch)
2221 croak("No Perl script found in input\n");
2222 if (*s == '#' && s[1] == '!' && (s = instr(s,"perl"))) {
2223 PerlIO_ungetc(rsfp, '\n'); /* to keep line count right */
2225 while (*s && !(isSPACE (*s) || *s == '#')) s++;
2227 while (*s == ' ' || *s == '\t') s++;
2229 while (isDIGIT(s2[-1]) || strchr("-._", s2[-1])) s2--;
2230 if (strnEQ(s2-4,"perl",4))
2232 while (s = moreswitches(s)) ;
2234 if (cddir && chdir(cddir) < 0)
2235 croak("Can't chdir to %s",cddir);
2243 uid = (int)getuid();
2244 euid = (int)geteuid();
2245 gid = (int)getgid();
2246 egid = (int)getegid();
2251 tainting |= (uid && (euid != uid || egid != gid));
2259 croak("No %s allowed while running setuid", s);
2261 croak("No %s allowed while running setgid", s);
2267 curstash = debstash;
2268 dbargs = GvAV(gv_AVadd((gv_fetchpv("args", GV_ADDMULTI, SVt_PVAV))));
2270 DBgv = gv_fetchpv("DB", GV_ADDMULTI, SVt_PVGV);
2271 DBline = gv_fetchpv("dbline", GV_ADDMULTI, SVt_PVAV);
2272 DBsub = gv_HVadd(gv_fetchpv("sub", GV_ADDMULTI, SVt_PVHV));
2273 DBsingle = GvSV((gv_fetchpv("single", GV_ADDMULTI, SVt_PV)));
2274 sv_setiv(DBsingle, 0);
2275 DBtrace = GvSV((gv_fetchpv("trace", GV_ADDMULTI, SVt_PV)));
2276 sv_setiv(DBtrace, 0);
2277 DBsignal = GvSV((gv_fetchpv("signal", GV_ADDMULTI, SVt_PV)));
2278 sv_setiv(DBsignal, 0);
2279 curstash = defstash;
2286 mainstack = curstack; /* remember in case we switch stacks */
2287 AvREAL_off(curstack); /* not a real array */
2288 av_extend(curstack,127);
2290 stack_base = AvARRAY(curstack);
2291 stack_sp = stack_base;
2292 stack_max = stack_base + 127;
2294 cxstack_max = 8192 / sizeof(CONTEXT) - 2; /* Use most of 8K. */
2295 New(50,cxstack,cxstack_max + 1,CONTEXT);
2298 New(50,tmps_stack,128,SV*);
2303 New(51,debname,128,char);
2304 New(52,debdelim,128,char);
2308 * The following stacks almost certainly should be per-interpreter,
2309 * but for now they're not. XXX
2313 markstack_ptr = markstack;
2315 New(54,markstack,64,I32);
2316 markstack_ptr = markstack;
2317 markstack_max = markstack + 64;
2323 New(54,scopestack,32,I32);
2325 scopestack_max = 32;
2331 New(54,savestack,128,ANY);
2333 savestack_max = 128;
2339 New(54,retstack,16,OP*);
2349 Safefree(tmps_stack);
2356 static PerlIO *tmpfp; /* moved outside init_lexer() because of UNICOS bug */
2365 subname = newSVpv("main",4);
2369 init_predump_symbols()
2374 sv_setpvn(GvSV(gv_fetchpv("\"", TRUE, SVt_PV)), " ", 1);
2376 stdingv = gv_fetchpv("STDIN",TRUE, SVt_PVIO);
2377 GvMULTI_on(stdingv);
2378 IoIFP(GvIOp(stdingv)) = PerlIO_stdin();
2379 tmpgv = gv_fetchpv("stdin",TRUE, SVt_PV);
2381 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(GvIOp(stdingv));
2383 tmpgv = gv_fetchpv("STDOUT",TRUE, SVt_PVIO);
2385 IoOFP(GvIOp(tmpgv)) = IoIFP(GvIOp(tmpgv)) = PerlIO_stdout();
2387 tmpgv = gv_fetchpv("stdout",TRUE, SVt_PV);
2389 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(GvIOp(defoutgv));
2391 othergv = gv_fetchpv("STDERR",TRUE, SVt_PVIO);
2392 GvMULTI_on(othergv);
2393 IoOFP(GvIOp(othergv)) = IoIFP(GvIOp(othergv)) = PerlIO_stderr();
2394 tmpgv = gv_fetchpv("stderr",TRUE, SVt_PV);
2396 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(GvIOp(othergv));
2398 statname = NEWSV(66,0); /* last filename we did stat on */
2401 osname = savepv(OSNAME);
2405 init_postdump_symbols(argc,argv,env)
2407 register char **argv;
2408 register char **env;
2414 argc--,argv++; /* skip name of script */
2416 for (; argc > 0 && **argv == '-'; argc--,argv++) {
2419 if (argv[0][1] == '-') {
2423 if (s = strchr(argv[0], '=')) {
2425 sv_setpv(GvSV(gv_fetchpv(argv[0]+1,TRUE, SVt_PV)),s);
2428 sv_setiv(GvSV(gv_fetchpv(argv[0]+1,TRUE, SVt_PV)),1);
2431 toptarget = NEWSV(0,0);
2432 sv_upgrade(toptarget, SVt_PVFM);
2433 sv_setpvn(toptarget, "", 0);
2434 bodytarget = NEWSV(0,0);
2435 sv_upgrade(bodytarget, SVt_PVFM);
2436 sv_setpvn(bodytarget, "", 0);
2437 formtarget = bodytarget;
2440 if (tmpgv = gv_fetchpv("0",TRUE, SVt_PV)) {
2441 sv_setpv(GvSV(tmpgv),origfilename);
2442 magicname("0", "0", 1);
2444 if (tmpgv = gv_fetchpv("\030",TRUE, SVt_PV))
2445 sv_setpv(GvSV(tmpgv),origargv[0]);
2446 if (argvgv = gv_fetchpv("ARGV",TRUE, SVt_PVAV)) {
2448 (void)gv_AVadd(argvgv);
2449 av_clear(GvAVn(argvgv));
2450 for (; argc > 0; argc--,argv++) {
2451 av_push(GvAVn(argvgv),newSVpv(argv[0],0));
2454 if (envgv = gv_fetchpv("ENV",TRUE, SVt_PVHV)) {
2458 hv_magic(hv, envgv, 'E');
2459 #ifndef VMS /* VMS doesn't have environ array */
2460 /* Note that if the supplied env parameter is actually a copy
2461 of the global environ then it may now point to free'd memory
2462 if the environment has been modified since. To avoid this
2463 problem we treat env==NULL as meaning 'use the default'
2468 environ[0] = Nullch;
2469 for (; *env; env++) {
2470 if (!(s = strchr(*env,'=')))
2476 sv = newSVpv(s--,0);
2477 (void)hv_store(hv, *env, s - *env, sv, 0);
2479 #if defined(__BORLANDC__) && defined(USE_WIN32_RTL_ENV)
2480 /* Sins of the RTL. See note in my_setenv(). */
2481 (void)putenv(savepv(*env));
2485 #ifdef DYNAMIC_ENV_FETCH
2486 HvNAME(hv) = savepv(ENV_HV_NAME);
2490 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
2491 sv_setiv(GvSV(tmpgv), (IV)getpid());
2500 s = getenv("PERL5LIB");
2504 incpush(getenv("PERLLIB"), FALSE);
2506 /* Treat PERL5?LIB as a possible search list logical name -- the
2507 * "natural" VMS idiom for a Unix path string. We allow each
2508 * element to be a set of |-separated directories for compatibility.
2512 if (my_trnlnm("PERL5LIB",buf,0))
2513 do { incpush(buf,TRUE); } while (my_trnlnm("PERL5LIB",buf,++idx));
2515 while (my_trnlnm("PERLLIB",buf,idx++)) incpush(buf,FALSE);
2519 /* Use the ~-expanded versions of APPLLIB (undocumented),
2520 ARCHLIB PRIVLIB SITEARCH SITELIB and OLDARCHLIB
2523 incpush(APPLLIB_EXP, FALSE);
2527 incpush(ARCHLIB_EXP, FALSE);
2530 #define PRIVLIB_EXP "/usr/local/lib/perl5:/usr/local/lib/perl"
2532 incpush(PRIVLIB_EXP, FALSE);
2535 incpush(SITEARCH_EXP, FALSE);
2538 incpush(SITELIB_EXP, FALSE);
2540 #ifdef OLDARCHLIB_EXP /* 5.00[01] compatibility */
2541 incpush(OLDARCHLIB_EXP, FALSE);
2545 incpush(".", FALSE);
2549 # define PERLLIB_SEP ';'
2552 # define PERLLIB_SEP '|'
2554 # define PERLLIB_SEP ':'
2557 #ifndef PERLLIB_MANGLE
2558 # define PERLLIB_MANGLE(s,n) (s)
2562 incpush(p, addsubdirs)
2566 SV *subdir = Nullsv;
2567 static char *archpat_auto;
2574 if (!archpat_auto) {
2575 STRLEN len = (sizeof(ARCHNAME) + strlen(patchlevel)
2576 + sizeof("//auto"));
2577 New(55, archpat_auto, len, char);
2578 sprintf(archpat_auto, "/%s/%s/auto", ARCHNAME, patchlevel);
2580 for (len = sizeof(ARCHNAME) + 2;
2581 archpat_auto[len] != '\0' && archpat_auto[len] != '/'; len++)
2582 if (archpat_auto[len] == '.') archpat_auto[len] = '_';
2587 /* Break at all separators */
2589 SV *libdir = newSV(0);
2592 /* skip any consecutive separators */
2593 while ( *p == PERLLIB_SEP ) {
2594 /* Uncomment the next line for PATH semantics */
2595 /* av_push(GvAVn(incgv), newSVpv(".", 1)); */
2599 if ( (s = strchr(p, PERLLIB_SEP)) != Nullch ) {
2600 sv_setpvn(libdir, PERLLIB_MANGLE(p, (STRLEN)(s - p)),
2605 sv_setpv(libdir, PERLLIB_MANGLE(p, 0));
2606 p = Nullch; /* break out */
2610 * BEFORE pushing libdir onto @INC we may first push version- and
2611 * archname-specific sub-directories.
2614 struct stat tmpstatbuf;
2619 if ((unix = tounixspec_ts(SvPV(libdir,na),Nullch)) != Nullch) {
2621 while (unix[len-1] == '/') len--; /* Cosmetic */
2622 sv_usepvn(libdir,unix,len);
2625 PerlIO_printf(PerlIO_stderr(),
2626 "Failed to unixify @INC element \"%s\"\n",
2629 /* .../archname/version if -d .../archname/version/auto */
2630 sv_setsv(subdir, libdir);
2631 sv_catpv(subdir, archpat_auto);
2632 if (Stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
2633 S_ISDIR(tmpstatbuf.st_mode))
2634 av_push(GvAVn(incgv),
2635 newSVpv(SvPVX(subdir), SvCUR(subdir) - sizeof "auto"));
2637 /* .../archname if -d .../archname/auto */
2638 sv_insert(subdir, SvCUR(libdir) + sizeof(ARCHNAME),
2639 strlen(patchlevel) + 1, "", 0);
2640 if (Stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
2641 S_ISDIR(tmpstatbuf.st_mode))
2642 av_push(GvAVn(incgv),
2643 newSVpv(SvPVX(subdir), SvCUR(subdir) - sizeof "auto"));
2646 /* finally push this lib directory on the end of @INC */
2647 av_push(GvAVn(incgv), libdir);
2650 SvREFCNT_dec(subdir);
2654 call_list(oldscope, list)
2658 line_t oldline = curcop->cop_line;
2663 while (AvFILL(list) >= 0) {
2664 CV *cv = (CV*)av_shift(list);
2671 SV* atsv = GvSV(errgv);
2673 perl_call_sv((SV*)cv, G_EVAL|G_DISCARD);
2674 (void)SvPV(atsv, len);
2677 curcop = &compiling;
2678 curcop->cop_line = oldline;
2679 if (list == beginav)
2680 sv_catpv(atsv, "BEGIN failed--compilation aborted");
2682 sv_catpv(atsv, "END failed--cleanup aborted");
2683 while (scopestack_ix > oldscope)
2685 croak("%s", SvPVX(atsv));
2693 /* my_exit() was called */
2694 while (scopestack_ix > oldscope)
2697 curstash = defstash;
2699 call_list(oldscope, endav);
2701 curcop = &compiling;
2702 curcop->cop_line = oldline;
2704 if (list == beginav)
2705 croak("BEGIN failed--compilation aborted");
2707 croak("END failed--cleanup aborted");
2713 PerlIO_printf(PerlIO_stderr(), "panic: restartop\n");
2718 curcop = &compiling;
2719 curcop->cop_line = oldline;
2738 STATUS_NATIVE_SET(status);
2748 if (vaxc$errno & 1) {
2749 if (STATUS_NATIVE & 1) /* fortuitiously includes "-1" */
2750 STATUS_NATIVE_SET(44);
2753 if (!vaxc$errno && errno) /* unlikely */
2754 STATUS_NATIVE_SET(44);
2756 STATUS_NATIVE_SET(vaxc$errno);
2760 STATUS_POSIX_SET(errno);
2761 else if (STATUS_POSIX == 0)
2762 STATUS_POSIX_SET(255);
2770 register CONTEXT *cx;
2779 (void)UNLINK(e_tmpname);
2780 Safefree(e_tmpname);
2784 if (cxstack_ix >= 0) {