home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / perl / patch / perl.c < prev    next >
C/C++ Source or Header  |  1995-12-06  |  40KB  |  1,786 lines

  1. /*    perl.c
  2.  *
  3.  *    Copyright (c) 1987-1994 Larry Wall
  4.  *
  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.
  7.  *
  8.  */
  9.  
  10. /*
  11.  * "A ship then new they built for him/of mithril and of elven glass" --Bilbo
  12.  */
  13.  
  14. #include "EXTERN.h"
  15. #include "perl.h"
  16. #include "patchlevel.h"
  17.  
  18. /* Omit -- it causes too much grief on mixed systems.
  19. #ifdef I_UNISTD
  20. #include <unistd.h>
  21. #endif
  22. */
  23.  
  24. char rcsid[] = "perl.c\nPatch level: ###\n";
  25.  
  26. #ifdef IAMSUID
  27. #ifndef DOSUID
  28. #define DOSUID
  29. #endif
  30. #endif
  31.  
  32. #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
  33. #ifdef DOSUID
  34. #undef DOSUID
  35. #endif
  36. #endif
  37.  
  38. static void find_beginning _((void));
  39. static void incpush _((char *));
  40. static void init_ids _((void));
  41. static void init_debugger _((void));
  42. static void init_lexer _((void));
  43. static void init_main_stash _((void));
  44. static void init_perllib _((void));
  45. static void init_postdump_symbols _((int, char **, char **));
  46. static void init_predump_symbols _((void));
  47. static void init_stacks _((void));
  48. static void open_script _((char *, bool, SV *));
  49. static void validate_suid _((char *));
  50.  
  51. PerlInterpreter *
  52. perl_alloc()
  53. {
  54.     PerlInterpreter *sv_interp;
  55.  
  56.     curinterp = 0;
  57.     New(53, sv_interp, 1, PerlInterpreter);
  58.     return sv_interp;
  59. }
  60.  
  61. void
  62. perl_construct( sv_interp )
  63. register PerlInterpreter *sv_interp;
  64. {
  65.     if (!(curinterp = sv_interp))
  66.     return;
  67.  
  68. #ifdef MULTIPLICITY
  69.     Zero(sv_interp, 1, PerlInterpreter);
  70. #endif
  71.  
  72.     /* Init the real globals? */
  73.     if (!linestr) {
  74.     linestr = NEWSV(65,80);
  75.     sv_upgrade(linestr,SVt_PVIV);
  76.  
  77.     SvREADONLY_on(&sv_undef);
  78.  
  79.     sv_setpv(&sv_no,No);
  80.     SvNV(&sv_no);
  81.     SvREADONLY_on(&sv_no);
  82.  
  83.     sv_setpv(&sv_yes,Yes);
  84.     SvNV(&sv_yes);
  85.     SvREADONLY_on(&sv_yes);
  86.  
  87. #ifdef MSDOS
  88.     /*
  89.      * There is no way we can refer to them from Perl so close them to save
  90.      * space.  The other alternative would be to provide STDAUX and STDPRN
  91.      * filehandles.
  92.      */
  93.     (void)fclose(stdaux);
  94.     (void)fclose(stdprn);
  95. #endif
  96.     }
  97.  
  98. #ifdef MULTIPLICITY
  99.     chopset    = " \n-";
  100.     copline    = NOLINE;
  101.     curcop    = &compiling;
  102.     dlmax    = 128;
  103.     laststatval    = -1;
  104.     laststype    = OP_STAT;
  105.     maxscream    = -1;
  106.     maxsysfd    = MAXSYSFD;
  107.     nrs        = "\n";
  108.     nrschar    = '\n';
  109.     nrslen    = 1;
  110.     rs        = "\n";
  111.     rschar    = '\n';
  112.     rsfp    = Nullfp;
  113.     rslen    = 1;
  114.     statname    = Nullsv;
  115.     tmps_floor    = -1;
  116. #endif
  117.  
  118.     init_ids();
  119.     sprintf(patchlevel, "%5.3f", 5.0 + (PATCHLEVEL / 1000.0));
  120.  
  121.     fdpid = newAV();    /* for remembering popen pids by fd */
  122.     pidstatus = newHV();/* for remembering status of dead pids */
  123.  
  124.     init_stacks();
  125.     ENTER;
  126. }
  127.  
  128. void
  129. perl_destruct(sv_interp)
  130. register PerlInterpreter *sv_interp;
  131. {
  132.     int destruct_level;  /* 0=none, 1=full, 2=full with checks */
  133.     I32 last_sv_count;
  134.     HV *hv;
  135.  
  136.     if (!(curinterp = sv_interp))
  137.     return;
  138.  
  139.     destruct_level = perl_destruct_level;
  140.     LEAVE;
  141.     FREETMPS;
  142.  
  143.     if (sv_objcount) {
  144.     /* We must account for everything.  First the syntax tree. */
  145.     if (main_root) {
  146.         curpad = AvARRAY(comppad);
  147.         op_free(main_root);
  148.         main_root = 0;
  149.     }
  150.     }
  151.     if (sv_objcount) {
  152.     /*
  153.      * Try to destruct global references.  We do this first so that the
  154.      * destructors and destructees still exist.  Some sv's might remain.
  155.      * Non-referenced objects are on their own.
  156.      */
  157.     
  158.     dirty = TRUE;
  159.     sv_clean_objs();
  160.     }
  161.  
  162.     if (destruct_level == 0){
  163.  
  164.     DEBUG_P(debprofdump());
  165.     
  166.     /* The exit() function will do everything that needs doing. */
  167.     return;
  168.     }
  169.     
  170.     /* Prepare to destruct main symbol table.  */
  171.     hv = defstash;
  172.     defstash = 0;
  173.     SvREFCNT_dec(hv);
  174.  
  175.     FREETMPS;
  176.     if (destruct_level >= 2) {
  177.     if (scopestack_ix != 0)
  178.         warn("Unbalanced scopes: %d more ENTERs than LEAVEs\n", scopestack_ix);
  179.     if (savestack_ix != 0)
  180.         warn("Unbalanced saves: %d more saves than restores\n", savestack_ix);
  181.     if (tmps_floor != -1)
  182.         warn("Unbalanced tmps: %d more allocs than frees\n", tmps_floor + 1);
  183.     if (cxstack_ix != -1)
  184.         warn("Unbalanced context: %d more PUSHes than POPs\n", cxstack_ix + 1);
  185.     }
  186.  
  187.     /* Now absolutely destruct everything, somehow or other, loops or no. */
  188.     last_sv_count = 0;
  189.     while (sv_count != 0 && sv_count != last_sv_count) {
  190.     last_sv_count = sv_count;
  191.     sv_clean_all();
  192.     }
  193.     if (sv_count != 0)
  194.     warn("Scalars leaked: %d\n", sv_count);
  195.     
  196.     DEBUG_P(debprofdump());
  197. }
  198.  
  199. void
  200. perl_free(sv_interp)
  201. PerlInterpreter *sv_interp;
  202. {
  203.     if (!(curinterp = sv_interp))
  204.     return;
  205.     Safefree(sv_interp);
  206. }
  207. #if !defined(STANDARD_C) && !defined(HAS_GETENV_PROTOTYPE)
  208. char *getenv _((char *)); /* Usually in <stdlib.h> */
  209. #endif
  210.  
  211. int
  212. perl_parse(sv_interp, xsinit, argc, argv, env)
  213. PerlInterpreter *sv_interp;
  214. void (*xsinit)_((void));
  215. int argc;
  216. char **argv;
  217. char **env;
  218. {
  219.     register SV *sv;
  220.     register char *s;
  221.     char *scriptname;
  222.     VOL bool dosearch = FALSE;
  223.     char *validarg = "";
  224.     AV* comppadlist;
  225.  
  226. #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
  227. #ifdef IAMSUID
  228. #undef IAMSUID
  229.     croak("suidperl is no longer needed since the kernel can now execute\n\
  230. setuid perl scripts securely.\n");
  231. #endif
  232. #endif
  233.  
  234.     if (!(curinterp = sv_interp))
  235.     return 255;
  236.  
  237. #if defined(NeXT) && defined(__DYNAMIC__)
  238.     _dyld_lookup_and_bind
  239.     ("__environ", (unsigned long *) &environ_pointer, NULL);
  240. #endif /* environ */
  241.  
  242.     origargv = argv;
  243.     origargc = argc;
  244. #ifndef VMS  /* VMS doesn't have environ array */
  245.     origenviron = environ;
  246. #endif
  247.  
  248.     if (do_undump) {
  249.  
  250.     /* Come here if running an undumped a.out. */
  251.  
  252.     origfilename = savepv(argv[0]);
  253.     do_undump = FALSE;
  254.     cxstack_ix = -1;        /* start label stack again */
  255.     init_ids();
  256.     init_postdump_symbols(argc,argv,env);
  257.     return 0;
  258.     }
  259.  
  260.     if (main_root)
  261.     op_free(main_root);
  262.     main_root = 0;
  263.  
  264.     switch (setjmp(top_env)) {
  265.     case 1:
  266. #ifdef VMS
  267.     statusvalue = 255;
  268. #else
  269.     statusvalue = 1;
  270. #endif
  271.     case 2:
  272.     curstash = defstash;
  273.     if (endav)
  274.         calllist(endav);
  275.     return(statusvalue);    /* my_exit() was called */
  276.     case 3:
  277.     fprintf(stderr, "panic: top_env\n");
  278.     return 1;
  279.     }
  280.  
  281.     sv_setpvn(linestr,"",0);
  282.     sv = newSVpv("",0);        /* first used for -I flags */
  283.     SAVEFREESV(sv);
  284.     init_main_stash();
  285.     for (argc--,argv++; argc > 0; argc--,argv++) {
  286.     if (argv[0][0] != '-' || !argv[0][1])
  287.         break;
  288. #ifdef DOSUID
  289.     if (*validarg)
  290.     validarg = " PHOOEY ";
  291.     else
  292.     validarg = argv[0];
  293. #endif
  294.     s = argv[0]+1;
  295.       reswitch:
  296.     switch (*s) {
  297.     case '0':
  298.     case 'F':
  299.     case 'a':
  300.     case 'c':
  301.     case 'd':
  302.     case 'D':
  303.     case 'i':
  304.     case 'l':
  305.     case 'n':
  306.     case 'p':
  307.     case 's':
  308.     case 'T':
  309.     case 'u':
  310.     case 'U':
  311.     case 'v':
  312.     case 'w':
  313.         if (s = moreswitches(s))
  314.         goto reswitch;
  315.         break;
  316.  
  317.     case 'e':
  318.         if (euid != uid || egid != gid)
  319.         croak("No -e allowed in setuid scripts");
  320.         if (!e_fp) {
  321.             e_tmpname = savepv(TMPPATH);
  322.         (void)mktemp(e_tmpname);
  323.         if (!*e_tmpname)
  324.             croak("Can't mktemp()");
  325.         e_fp = fopen(e_tmpname,"w");
  326.         if (!e_fp)
  327.             croak("Cannot open temporary file");
  328.         }
  329.         if (argv[1]) {
  330.         fputs(argv[1],e_fp);
  331.         argc--,argv++;
  332.         }
  333.         (void)putc('\n', e_fp);
  334.         break;
  335.     case 'I':
  336.         taint_not("-I");
  337.         sv_catpv(sv,"-");
  338.         sv_catpv(sv,s);
  339.         sv_catpv(sv," ");
  340.         if (*++s) {
  341.         av_push(GvAVn(incgv),newSVpv(s,0));
  342.         }
  343.         else if (argv[1]) {
  344.         av_push(GvAVn(incgv),newSVpv(argv[1],0));
  345.         sv_catpv(sv,argv[1]);
  346.         argc--,argv++;
  347.         sv_catpv(sv," ");
  348.         }
  349.         break;
  350.     case 'P':
  351.         taint_not("-P");
  352.         preprocess = TRUE;
  353.         s++;
  354.         goto reswitch;
  355.     case 'S':
  356.         taint_not("-S");
  357.         dosearch = TRUE;
  358.         s++;
  359.         goto reswitch;
  360.     case 'x':
  361.         doextract = TRUE;
  362.         s++;
  363.         if (*s)
  364.         cddir = savepv(s);
  365.         break;
  366.     case '-':
  367.         argc--,argv++;
  368.         goto switch_end;
  369.     case 0:
  370.         break;
  371.     default:
  372.         croak("Unrecognized switch: -%s",s);
  373.     }
  374.     }
  375.   switch_end:
  376.     scriptname = argv[0];
  377.     if (e_fp) {
  378.     if (fflush(e_fp) || ferror(e_fp) || fclose(e_fp))
  379.         croak("Can't write to temp file for -e: %s", Strerror(errno));
  380.     argc++,argv--;
  381.     scriptname = e_tmpname;
  382.     }
  383.     else if (scriptname == Nullch) {
  384. #ifdef MSDOS
  385.     if ( isatty(fileno(stdin)) )
  386.         moreswitches("v");
  387. #endif
  388.     scriptname = "-";
  389.     }
  390.  
  391.     init_perllib();
  392.  
  393.     open_script(scriptname,dosearch,sv);
  394.  
  395.     validate_suid(validarg);
  396.  
  397.     if (doextract)
  398.     find_beginning();
  399.  
  400.     compcv = (CV*)NEWSV(1104,0);
  401.     sv_upgrade((SV *)compcv, SVt_PVCV);
  402.  
  403.     pad = newAV();
  404.     comppad = pad;
  405.     av_push(comppad, Nullsv);
  406.     curpad = AvARRAY(comppad);
  407.     padname = newAV();
  408.     comppad_name = padname;
  409.     comppad_name_fill = 0;
  410.     min_intro_pending = 0;
  411.     padix = 0;
  412.  
  413.     comppadlist = newAV();
  414.     AvREAL_off(comppadlist);
  415.     av_store(comppadlist, 0, SvREFCNT_inc((SV*)comppad_name));
  416.     av_store(comppadlist, 1, SvREFCNT_inc((SV*)comppad));
  417.     CvPADLIST(compcv) = comppadlist;
  418.  
  419.     if (xsinit)
  420.     (*xsinit)();    /* in case linked C routines want magical variables */
  421. #ifdef VMS
  422.     init_os_extras();
  423. #endif
  424.  
  425.     init_predump_symbols();
  426.     if (!do_undump)
  427.     init_postdump_symbols(argc,argv,env);
  428.  
  429.     init_lexer();
  430.  
  431.     /* now parse the script */
  432.  
  433.     error_count = 0;
  434.     if (yyparse() || error_count) {
  435.     if (minus_c)
  436.         croak("%s had compilation errors.\n", origfilename);
  437.     else {
  438.         croak("Execution of %s aborted due to compilation errors.\n",
  439.         origfilename);
  440.     }
  441.     }
  442.     curcop->cop_line = 0;
  443.     curstash = defstash;
  444.     preprocess = FALSE;
  445.     if (e_fp) {
  446.     e_fp = Nullfp;
  447.     (void)UNLINK(e_tmpname);
  448.     }
  449.  
  450.     /* now that script is parsed, we can modify record separator */
  451.  
  452.     rs = nrs;
  453.     rslen = nrslen;
  454.     rschar = nrschar;
  455.     rspara = (nrslen == 2);
  456.     sv_setpvn(GvSV(gv_fetchpv("/", TRUE, SVt_PV)), rs, rslen);
  457.  
  458.     if (do_undump)
  459.     my_unexec();
  460.  
  461.     if (dowarn)
  462.     gv_check(defstash);
  463.  
  464.     LEAVE;
  465.     FREETMPS;
  466.     ENTER;
  467.     restartop = 0;
  468.     return 0;
  469. }
  470.  
  471. int
  472. perl_run(sv_interp)
  473. PerlInterpreter *sv_interp;
  474. {
  475.     if (!(curinterp = sv_interp))
  476.     return 255;
  477.     switch (setjmp(top_env)) {
  478.     case 1:
  479.     cxstack_ix = -1;        /* start context stack again */
  480.     break;
  481.     case 2:
  482.     curstash = defstash;
  483.     if (endav)
  484.         calllist(endav);
  485.     FREETMPS;
  486.     return(statusvalue);        /* my_exit() was called */
  487.     case 3:
  488.     if (!restartop) {
  489.         fprintf(stderr, "panic: restartop\n");
  490.         FREETMPS;
  491.         return 1;
  492.     }
  493.     if (stack != mainstack) {
  494.         dSP;
  495.         SWITCHSTACK(stack, mainstack);
  496.     }
  497.     break;
  498.     }
  499.  
  500.     if (!restartop) {
  501.     DEBUG_x(dump_all());
  502.     DEBUG(fprintf(stderr,"\nEXECUTING...\n\n"));
  503.  
  504.     if (minus_c) {
  505.         fprintf(stderr,"%s syntax OK\n", origfilename);
  506.         my_exit(0);
  507.     }
  508.     if (perldb && DBsingle)
  509.        sv_setiv(DBsingle, 1); 
  510.     }
  511.  
  512.     /* do it */
  513.  
  514.     if (restartop) {
  515.     op = restartop;
  516.     restartop = 0;
  517.     run();
  518.     }
  519.     else if (main_start) {
  520.     op = main_start;
  521.     run();
  522.     }
  523.  
  524.     my_exit(0);
  525.     return 0;
  526. }
  527.  
  528. void
  529. my_exit(status)
  530. U32 status;
  531. {
  532.     register CONTEXT *cx;
  533.     I32 gimme;
  534.     SV **newsp;
  535.  
  536.     statusvalue = FIXSTATUS(status);
  537.     if (cxstack_ix >= 0) {
  538.     if (cxstack_ix > 0)
  539.         dounwind(0);
  540.     POPBLOCK(cx,curpm);
  541.     LEAVE;
  542.     }
  543.     longjmp(top_env, 2);
  544. }
  545.  
  546. SV*
  547. perl_get_sv(name, create)
  548. char* name;
  549. I32 create;
  550. {
  551.     GV* gv = gv_fetchpv(name, create, SVt_PV);
  552.     if (gv)
  553.     return GvSV(gv);
  554.     return Nullsv;
  555. }
  556.  
  557. AV*
  558. perl_get_av(name, create)
  559. char* name;
  560. I32 create;
  561. {
  562.     GV* gv = gv_fetchpv(name, create, SVt_PVAV);
  563.     if (create)
  564.         return GvAVn(gv);
  565.     if (gv)
  566.     return GvAV(gv);
  567.     return Nullav;
  568. }
  569.  
  570. HV*
  571. perl_get_hv(name, create)
  572. char* name;
  573. I32 create;
  574. {
  575.     GV* gv = gv_fetchpv(name, create, SVt_PVHV);
  576.     if (create)
  577.         return GvHVn(gv);
  578.     if (gv)
  579.     return GvHV(gv);
  580.     return Nullhv;
  581. }
  582.  
  583. CV*
  584. perl_get_cv(name, create)
  585. char* name;
  586. I32 create;
  587. {
  588.     GV* gv = gv_fetchpv(name, create, SVt_PVCV);
  589.     if (create && !GvCV(gv))
  590.         return newSUB(start_subparse(),
  591.               newSVOP(OP_CONST, 0, newSVpv(name,0)),
  592.               Nullop);
  593.     if (gv)
  594.     return GvCV(gv);
  595.     return Nullcv;
  596. }
  597.  
  598. /* Be sure to refetch the stack pointer after calling these routines. */
  599.  
  600. I32
  601. perl_call_argv(subname, flags, argv)
  602. char *subname;
  603. I32 flags;        /* See G_* flags in cop.h */
  604. register char **argv;    /* null terminated arg list */
  605. {
  606.     dSP;
  607.  
  608.     PUSHMARK(sp);
  609.     if (argv) {
  610.     while (*argv) {
  611.         XPUSHs(sv_2mortal(newSVpv(*argv,0)));
  612.         argv++;
  613.     }
  614.     PUTBACK;
  615.     }
  616.     return perl_call_pv(subname, flags);
  617. }
  618.  
  619. I32
  620. perl_call_pv(subname, flags)
  621. char *subname;        /* name of the subroutine */
  622. I32 flags;        /* See G_* flags in cop.h */
  623. {
  624.     return perl_call_sv((SV*)perl_get_cv(subname, TRUE), flags);
  625. }
  626.  
  627. I32
  628. perl_call_method(methname, flags)
  629. char *methname;        /* name of the subroutine */
  630. I32 flags;        /* See G_* flags in cop.h */
  631. {
  632.     dSP;
  633.     OP myop;
  634.     if (!op)
  635.     op = &myop;
  636.     XPUSHs(sv_2mortal(newSVpv(methname,0)));
  637.     PUTBACK;
  638.     pp_method();
  639.     return perl_call_sv(*stack_sp--, flags);
  640. }
  641.  
  642. /* May be called with any of a CV, a GV, or an SV containing the name. */
  643. I32
  644. perl_call_sv(sv, flags)
  645. SV* sv;
  646. I32 flags;        /* See G_* flags in cop.h */
  647. {
  648.     LOGOP myop;        /* fake syntax tree node */
  649.     SV** sp = stack_sp;
  650.     I32 oldmark = TOPMARK;
  651.     I32 retval;
  652.     jmp_buf oldtop;
  653.     I32 oldscope;
  654.     
  655.     if (flags & G_DISCARD) {
  656.     ENTER;
  657.     SAVETMPS;
  658.     }
  659.  
  660.     SAVESPTR(op);
  661.     op = (OP*)&myop;
  662.     Zero(op, 1, LOGOP);
  663.     EXTEND(stack_sp, 1);
  664.     *++stack_sp = sv;
  665.     oldscope = scopestack_ix;
  666.  
  667.     if (!(flags & G_NOARGS))
  668.     myop.op_flags = OPf_STACKED;
  669.     myop.op_next = Nullop;
  670.     myop.op_flags |= OPf_KNOW;
  671.     if (flags & G_ARRAY)
  672.       myop.op_flags |= OPf_LIST;
  673.  
  674.     if (flags & G_EVAL) {
  675.     Copy(top_env, oldtop, 1, jmp_buf);
  676.  
  677.     cLOGOP->op_other = op;
  678.     markstack_ptr--;
  679.     pp_entertry();
  680.     markstack_ptr++;
  681.  
  682.     restart:
  683.     switch (setjmp(top_env)) {
  684.     case 0:
  685.         break;
  686.     case 1:
  687. #ifdef VMS
  688.         statusvalue = 255;    /* XXX I don't think we use 1 anymore. */
  689. #else
  690.     statusvalue = 1;
  691. #endif
  692.         /* FALL THROUGH */
  693.     case 2:
  694.         /* my_exit() was called */
  695.         curstash = defstash;
  696.         FREETMPS;
  697.         Copy(oldtop, top_env, 1, jmp_buf);
  698.         if (statusvalue)
  699.         croak("Callback called exit");
  700.         my_exit(statusvalue);
  701.         /* NOTREACHED */
  702.     case 3:
  703.         if (restartop) {
  704.         op = restartop;
  705.         restartop = 0;
  706.         goto restart;
  707.         }
  708.         stack_sp = stack_base + oldmark;
  709.         if (flags & G_ARRAY)
  710.         retval = 0;
  711.         else {
  712.         retval = 1;
  713.         *++stack_sp = &sv_undef;
  714.         }
  715.         goto cleanup;
  716.     }
  717.     }
  718.  
  719.     if (op == (OP*)&myop)
  720.     op = pp_entersub();
  721.     if (op)
  722.     run();
  723.     retval = stack_sp - (stack_base + oldmark);
  724.     if (flags & G_EVAL)
  725.     sv_setpv(GvSV(gv_fetchpv("@",TRUE, SVt_PV)),"");
  726.  
  727.   cleanup:
  728.     if (flags & G_EVAL) {
  729.     if (scopestack_ix > oldscope) {
  730.         SV **newsp;
  731.         PMOP *newpm;
  732.         I32 gimme;
  733.         register CONTEXT *cx;
  734.         I32 optype;
  735.  
  736.         POPBLOCK(cx,newpm);
  737.         POPEVAL(cx);
  738.         pop_return();
  739.         curpm = newpm;
  740.         LEAVE;
  741.     }
  742.     Copy(oldtop, top_env, 1, jmp_buf);
  743.     }
  744.     if (flags & G_DISCARD) {
  745.     stack_sp = stack_base + oldmark;
  746.     retval = 0;
  747.     FREETMPS;
  748.     LEAVE;
  749.     }
  750.     return retval;
  751. }
  752.  
  753. /* Older forms, here grandfathered. */
  754.  
  755. #ifdef DEPRECATED
  756. I32
  757. perl_callargv(subname, spix, gimme, argv)
  758. char *subname;
  759. register I32 spix;    /* current stack pointer index */
  760. I32 gimme;        /* See G_* flags in cop.h */
  761. register char **argv;    /* null terminated arg list, NULL for no arglist */
  762. {
  763.     stack_sp = stack_base + spix;
  764.     return spix + perl_call_argv(subname, gimme, argv);
  765. }
  766.  
  767. I32
  768. perl_callpv(subname, spix, gimme, hasargs, numargs)
  769. char *subname;
  770. I32 spix;        /* stack pointer index after args are pushed */
  771. I32 gimme;        /* See G_* flags in cop.h */
  772. I32 hasargs;        /* whether to create a @_ array for routine */
  773. I32 numargs;        /* how many args are pushed on the stack */
  774. {
  775.     stack_sp = stack_base + spix;
  776.     PUSHMARK(stack_sp - numargs);
  777.     return spix - numargs + perl_call_sv((SV*)perl_get_cv(subname, TRUE),
  778.                 gimme, hasargs, numargs);
  779. }
  780.  
  781. I32
  782. perl_callsv(sv, spix, gimme, hasargs, numargs)
  783. SV* sv;
  784. I32 spix;        /* stack pointer index after args are pushed */
  785. I32 gimme;        /* See G_* flags in cop.h */
  786. I32 hasargs;        /* whether to create a @_ array for routine */
  787. I32 numargs;        /* how many args are pushed on the stack */
  788. {
  789.     stack_sp = stack_base + spix;
  790.     PUSHMARK(stack_sp - numargs);
  791.     return spix - numargs + perl_call_sv(sv, gimme, hasargs, numargs);
  792. }
  793. #endif
  794.  
  795. /* Require a module. */
  796.  
  797. void
  798. perl_requirepv(pv)
  799. char* pv;
  800. {
  801.     UNOP myop;        /* fake syntax tree node */
  802.     SV* sv;
  803.     dSP;
  804.     
  805.     ENTER;
  806.     SAVETMPS;
  807.     SAVESPTR(op);
  808.     sv = sv_newmortal();
  809.     sv_setpv(sv, pv);
  810.     op = (OP*)&myop;
  811.     Zero(op, 1, UNOP);
  812.     XPUSHs(sv);
  813.  
  814.     myop.op_type = OP_REQUIRE;
  815.     myop.op_next = Nullop;
  816.     myop.op_private = 1;
  817.     myop.op_flags = OPf_KNOW;
  818.  
  819.     PUTBACK;
  820.     if (op = pp_require())
  821.     run();
  822.     stack_sp--;
  823.     FREETMPS;
  824.     LEAVE;
  825. }
  826.  
  827. void
  828. magicname(sym,name,namlen)
  829. char *sym;
  830. char *name;
  831. I32 namlen;
  832. {
  833.     register GV *gv;
  834.  
  835.     if (gv = gv_fetchpv(sym,TRUE, SVt_PV))
  836.     sv_magic(GvSV(gv), (SV*)gv, 0, name, namlen);
  837. }
  838.  
  839. #if defined(DOSISH)
  840. #    define PERLLIB_SEP ';'
  841. #else
  842. #  if defined(VMS)
  843. #    define PERLLIB_SEP '|'
  844. #  else
  845. #    define PERLLIB_SEP ':'
  846. #  endif
  847. #endif
  848.  
  849. static void
  850. incpush(p)
  851. char *p;
  852. {
  853.     char *s;
  854.  
  855.     if (!p)
  856.     return;
  857.  
  858.     /* Break at all separators */
  859.     while (*p) {
  860.     /* First, skip any consecutive separators */
  861.     while ( *p == PERLLIB_SEP ) {
  862.         /* Uncomment the next line for PATH semantics */
  863.         /* av_push(GvAVn(incgv), newSVpv(".", 1)); */
  864.         p++;
  865.     }
  866.     if ( (s = strchr(p, PERLLIB_SEP)) != Nullch ) {
  867.         av_push(GvAVn(incgv), newSVpv(p, (STRLEN)(s - p)));
  868.         p = s + 1;
  869.     } else {
  870.         av_push(GvAVn(incgv), newSVpv(p, 0));
  871.         break;
  872.     }
  873.     }
  874. }
  875.  
  876. /* This routine handles any switches that can be given during run */
  877.  
  878. char *
  879. moreswitches(s)
  880. char *s;
  881. {
  882.     I32 numlen;
  883.  
  884.     switch (*s) {
  885.     case '0':
  886.     nrschar = scan_oct(s, 4, &numlen);
  887.     nrs = savepvn("\n",1);
  888.     *nrs = nrschar;
  889.     if (nrschar > 0377) {
  890.         nrslen = 0;
  891.         nrs = "";
  892.     }
  893.     else if (!nrschar && numlen >= 2) {
  894.         nrslen = 2;
  895.         nrs = "\n\n";
  896.         nrschar = '\n';
  897.     }
  898.     return s + numlen;
  899.     case 'F':
  900.     minus_F = TRUE;
  901.     splitstr = savepv(s + 1);
  902.     s += strlen(s);
  903.     return s;
  904.     case 'a':
  905.     minus_a = TRUE;
  906.     s++;
  907.     return s;
  908.     case 'c':
  909.     minus_c = TRUE;
  910.     s++;
  911.     return s;
  912.     case 'd':
  913.     taint_not("-d");
  914.     if (!perldb) {
  915.         perldb = TRUE;
  916.         init_debugger();
  917.     }
  918.     s++;
  919.     return s;
  920.     case 'D':
  921. #ifdef DEBUGGING
  922.     taint_not("-D");
  923.     if (isALPHA(s[1])) {
  924.         static char debopts[] = "psltocPmfrxuLHXD";
  925.         char *d;
  926.  
  927.         for (s++; *s && (d = strchr(debopts,*s)); s++)
  928.         debug |= 1 << (d - debopts);
  929.     }
  930.     else {
  931.         debug = atoi(s+1);
  932.         for (s++; isDIGIT(*s); s++) ;
  933.     }
  934.     debug |= 0x80000000;
  935. #else
  936.     warn("Recompile perl with -DDEBUGGING to use -D switch\n");
  937.     for (s++; isALNUM(*s); s++) ;
  938. #endif
  939.     /*SUPPRESS 530*/
  940.     return s;
  941.     case 'i':
  942.     if (inplace)
  943.         Safefree(inplace);
  944.     inplace = savepv(s+1);
  945.     /*SUPPRESS 530*/
  946.     for (s = inplace; *s && !isSPACE(*s); s++) ;
  947.     *s = '\0';
  948.     break;
  949.     case 'I':
  950.     taint_not("-I");
  951.     if (*++s) {
  952.         char *e;
  953.         for (e = s; *e && !isSPACE(*e); e++) ;
  954.         av_push(GvAVn(incgv),newSVpv(s,e-s));
  955.         if (*e)
  956.         return e;
  957.     }
  958.     else
  959.         croak("No space allowed after -I");
  960.     break;
  961.     case 'l':
  962.     minus_l = TRUE;
  963.     s++;
  964.     if (ors)
  965.         Safefree(ors);
  966.     if (isDIGIT(*s)) {
  967.         ors = savepv("\n");
  968.         orslen = 1;
  969.         *ors = scan_oct(s, 3 + (*s == '0'), &numlen);
  970.         s += numlen;
  971.     }
  972.     else {
  973.         ors = savepvn(nrs,nrslen);
  974.         orslen = nrslen;
  975.     }
  976.     return s;
  977.     case 'n':
  978.     minus_n = TRUE;
  979.     s++;
  980.     return s;
  981.     case 'p':
  982.     minus_p = TRUE;
  983.     s++;
  984.     return s;
  985.     case 's':
  986.     taint_not("-s");
  987.     doswitches = TRUE;
  988.     s++;
  989.     return s;
  990.     case 'T':
  991.     tainting = TRUE;
  992.     s++;
  993.     return s;
  994.     case 'u':
  995.     do_undump = TRUE;
  996.     s++;
  997.     return s;
  998.     case 'U':
  999.     unsafe = TRUE;
  1000.     s++;
  1001.     return s;
  1002.     case 'v':
  1003.     printf("\nThis is perl, version %s\n\n",patchlevel);
  1004.     fputs("\tUnofficial patchlevel 1m.\n",stdout);
  1005.     fputs("\nCopyright 1987-1994, Larry Wall\n",stdout);
  1006. #ifdef MSDOS
  1007.     fputs("MS-DOS port Copyright (c) 1989, 1990, Diomidis Spinellis\n",
  1008.     stdout);
  1009. #ifdef OS2
  1010.         fputs("OS/2 port Copyright (c) 1990, 1991, Raymond Chen, Kai Uwe Rommel\n",
  1011.         stdout);
  1012. #endif
  1013. #endif
  1014. #ifdef atarist
  1015.         fputs("atariST series port, ++jrb  bammi@cadence.com\n", stdout);
  1016. #endif
  1017.     fputs("\n\
  1018. Perl may be copied only under the terms of either the Artistic License or the\n\
  1019. GNU General Public License, which may be found in the Perl 5.0 source kit.\n",stdout);
  1020. #ifdef MSDOS
  1021.         usage(origargv[0]);
  1022. #endif
  1023.     exit(0);
  1024.     case 'w':
  1025.     dowarn = TRUE;
  1026.     s++;
  1027.     return s;
  1028.     case '*':
  1029.     case ' ':
  1030.     if (s[1] == '-')    /* Additional switches on #! line. */
  1031.         return s+2;
  1032.     break;
  1033.     case '-':
  1034.     case 0:
  1035.     case '\n':
  1036.     case '\t':
  1037.     break;
  1038.     case 'P':
  1039.     if (preprocess)
  1040.         return s+1;
  1041.     /* FALL THROUGH */
  1042.     default:
  1043.     croak("Can't emulate -%.1s on #! line",s);
  1044.     }
  1045.     return Nullch;
  1046. }
  1047.  
  1048. /* compliments of Tom Christiansen */
  1049.  
  1050. /* unexec() can be found in the Gnu emacs distribution */
  1051.  
  1052. void
  1053. my_unexec()
  1054. {
  1055. #ifdef UNEXEC
  1056.     int    status;
  1057.     extern int etext;
  1058.  
  1059.     sprintf (buf, "%s.perldump", origfilename);
  1060.     sprintf (tokenbuf, "%s/perl", BIN);
  1061.  
  1062.     status = unexec(buf, tokenbuf, &etext, sbrk(0), 0);
  1063.     if (status)
  1064.     fprintf(stderr, "unexec of %s into %s failed!\n", tokenbuf, buf);
  1065.     exit(status);
  1066. #else
  1067.     ABORT();        /* for use with undump */
  1068. #endif
  1069. }
  1070.  
  1071. static void
  1072. init_main_stash()
  1073. {
  1074.     GV *gv;
  1075.     curstash = defstash = newHV();
  1076.     curstname = newSVpv("main",4);
  1077.     gv = gv_fetchpv("main::",TRUE, SVt_PVHV);
  1078.     SvREFCNT_dec(GvHV(gv));
  1079.     GvHV(gv) = (HV*)SvREFCNT_inc(defstash);
  1080.     SvREADONLY_on(gv);
  1081.     HvNAME(defstash) = savepv("main");
  1082.     incgv = gv_HVadd(gv_AVadd(gv_fetchpv("INC",TRUE, SVt_PVAV)));
  1083.     SvMULTI_on(incgv);
  1084.     defgv = gv_fetchpv("_",TRUE, SVt_PVAV);
  1085.     curstash = defstash;
  1086.     compiling.cop_stash = defstash;
  1087.     debstash = GvHV(gv_fetchpv("DB::", GV_ADDMULTI, SVt_PVHV));
  1088. }
  1089.  
  1090. #ifdef CAN_PROTOTYPE
  1091. static void
  1092. open_script(char *scriptname, bool dosearch, SV *sv)
  1093. #else
  1094. static void
  1095. open_script(scriptname,dosearch,sv)
  1096. char *scriptname;
  1097. bool dosearch;
  1098. SV *sv;
  1099. #endif
  1100. {
  1101.     char *xfound = Nullch;
  1102.     char *xfailed = Nullch;
  1103.     register char *s;
  1104.     I32 len;
  1105.  
  1106.     if (dosearch && !strchr(scriptname, '/') && (s = getenv("PATH"))) {
  1107.  
  1108.     bufend = s + strlen(s);
  1109.     while (*s) {
  1110. #ifndef DOSISH
  1111.         s = cpytill(tokenbuf,s,bufend,':',&len);
  1112. #else
  1113. #ifdef atarist
  1114.         for (len = 0; *s && *s != ',' && *s != ';'; tokenbuf[len++] = *s++);
  1115.         tokenbuf[len] = '\0';
  1116. #else
  1117.         for (len = 0; *s && *s != ';'; tokenbuf[len++] = *s++);
  1118.         tokenbuf[len] = '\0';
  1119. #endif
  1120. #endif
  1121.         if (*s)
  1122.         s++;
  1123. #ifndef DOSISH
  1124.         if (len && tokenbuf[len-1] != '/')
  1125. #else
  1126. #ifdef atarist
  1127.         if (len && ((tokenbuf[len-1] != '\\') && (tokenbuf[len-1] != '/')))
  1128. #else
  1129.         if (len && tokenbuf[len-1] != '\\')
  1130. #endif
  1131. #endif
  1132.         (void)strcat(tokenbuf+len,"/");
  1133.         (void)strcat(tokenbuf+len,scriptname);
  1134.         DEBUG_p(fprintf(stderr,"Looking for %s\n",tokenbuf));
  1135.         if (Stat(tokenbuf,&statbuf) < 0)        /* not there? */
  1136.         continue;
  1137.         if (S_ISREG(statbuf.st_mode)
  1138.          && cando(S_IRUSR,TRUE,&statbuf) && cando(S_IXUSR,TRUE,&statbuf)) {
  1139.         xfound = tokenbuf;              /* bingo! */
  1140.         break;
  1141.         }
  1142.         if (!xfailed)
  1143.         xfailed = savepv(tokenbuf);
  1144.     }
  1145.     if (!xfound)
  1146.         croak("Can't execute %s", xfailed ? xfailed : scriptname );
  1147.     if (xfailed)
  1148.         Safefree(xfailed);
  1149.     scriptname = xfound;
  1150.     }
  1151.  
  1152.     origfilename = savepv(e_fp ? "-e" : scriptname);
  1153.     curcop->cop_filegv = gv_fetchfile(origfilename);
  1154.     if (strEQ(origfilename,"-"))
  1155.     scriptname = "";
  1156.     if (preprocess) {
  1157.     char *cpp = CPPSTDIN;
  1158.  
  1159.     if (strEQ(cpp,"cppstdin"))
  1160.         sprintf(tokenbuf, "%s/%s", SCRIPTDIR, cpp);
  1161.     else
  1162.         sprintf(tokenbuf, "%s", cpp);
  1163.     sv_catpv(sv,"-I");
  1164.     sv_catpv(sv,PRIVLIB_EXP);
  1165. #ifdef MSDOS
  1166.     (void)sprintf(buf, "\
  1167. sed %s -e \"/^[^#]/b\" \
  1168.  -e \"/^#[     ]*include[     ]/b\" \
  1169.  -e \"/^#[     ]*define[     ]/b\" \
  1170.  -e \"/^#[     ]*if[     ]/b\" \
  1171.  -e \"/^#[     ]*ifdef[     ]/b\" \
  1172.  -e \"/^#[     ]*ifndef[     ]/b\" \
  1173.  -e \"/^#[     ]*else/b\" \
  1174.  -e \"/^#[     ]*elif[     ]/b\" \
  1175.  -e \"/^#[     ]*undef[     ]/b\" \
  1176.  -e \"/^#[     ]*endif/b\" \
  1177.  -e \"s/^#.*//\" \
  1178.  %s | %s -C %s %s",
  1179.       (doextract ? "-e \"1,/^#/d\n\"" : ""),
  1180. #else
  1181.     (void)sprintf(buf, "\
  1182. %s %s -e '/^[^#]/b' \
  1183.  -e '/^#[     ]*include[     ]/b' \
  1184.  -e '/^#[     ]*define[     ]/b' \
  1185.  -e '/^#[     ]*if[     ]/b' \
  1186.  -e '/^#[     ]*ifdef[     ]/b' \
  1187.  -e '/^#[     ]*ifndef[     ]/b' \
  1188.  -e '/^#[     ]*else/b' \
  1189.  -e '/^#[     ]*elif[     ]/b' \
  1190.  -e '/^#[     ]*undef[     ]/b' \
  1191.  -e '/^#[     ]*endif/b' \
  1192.  -e 's/^[     ]*#.*//' \
  1193.  %s | %s -C %s %s",
  1194. #ifdef LOC_SED
  1195.       LOC_SED,
  1196. #else
  1197.       "sed",
  1198. #endif
  1199.       (doextract ? "-e '1,/^#/d\n'" : ""),
  1200. #endif
  1201.       scriptname, tokenbuf, SvPV(sv, na), CPPMINUS);
  1202.     doextract = FALSE;
  1203. #ifdef IAMSUID                /* actually, this is caught earlier */
  1204.     if (euid != uid && !euid) {    /* if running suidperl */
  1205. #ifdef HAS_SETEUID
  1206.         (void)seteuid(uid);        /* musn't stay setuid root */
  1207. #else
  1208. #ifdef HAS_SETREUID
  1209.         (void)setreuid((Uid_t)-1, uid);
  1210. #else
  1211. #ifdef HAS_SETRESUID
  1212.         (void)setresuid((Uid_t)-1, uid, (Uid_t)-1);
  1213. #else
  1214.         setuid(uid);
  1215. #endif
  1216. #endif
  1217. #endif
  1218.         if (geteuid() != uid)
  1219.         croak("Can't do seteuid!\n");
  1220.     }
  1221. #endif /* IAMSUID */
  1222.     rsfp = my_popen(buf,"r");
  1223.     }
  1224.     else if (!*scriptname) {
  1225.     taint_not("program input from stdin");
  1226.     rsfp = stdin;
  1227.     }
  1228.     else
  1229.     rsfp = fopen(scriptname,"r");
  1230.     if ((FILE*)rsfp == Nullfp) {
  1231. #ifdef DOSUID
  1232. #ifndef IAMSUID        /* in case script is not readable before setuid */
  1233.     if (euid && Stat(SvPVX(GvSV(curcop->cop_filegv)),&statbuf) >= 0 &&
  1234.       statbuf.st_mode & (S_ISUID|S_ISGID)) {
  1235.         (void)sprintf(buf, "%s/sperl%s", BIN, patchlevel);
  1236.         execv(buf, origargv);    /* try again */
  1237.         croak("Can't do setuid\n");
  1238.     }
  1239. #endif
  1240. #endif
  1241.     croak("Can't open perl script \"%s\": %s\n",
  1242.       SvPVX(GvSV(curcop->cop_filegv)), Strerror(errno));
  1243.     }
  1244. }
  1245.  
  1246. static void
  1247. validate_suid(validarg)
  1248. char *validarg;
  1249. {
  1250.     /* do we need to emulate setuid on scripts? */
  1251.  
  1252.     /* This code is for those BSD systems that have setuid #! scripts disabled
  1253.      * in the kernel because of a security problem.  Merely defining DOSUID
  1254.      * in perl will not fix that problem, but if you have disabled setuid
  1255.      * scripts in the kernel, this will attempt to emulate setuid and setgid
  1256.      * on scripts that have those now-otherwise-useless bits set.  The setuid
  1257.      * root version must be called suidperl or sperlN.NNN.  If regular perl
  1258.      * discovers that it has opened a setuid script, it calls suidperl with
  1259.      * the same argv that it had.  If suidperl finds that the script it has
  1260.      * just opened is NOT setuid root, it sets the effective uid back to the
  1261.      * uid.  We don't just make perl setuid root because that loses the
  1262.      * effective uid we had before invoking perl, if it was different from the
  1263.      * uid.
  1264.      *
  1265.      * DOSUID must be defined in both perl and suidperl, and IAMSUID must
  1266.      * be defined in suidperl only.  suidperl must be setuid root.  The
  1267.      * Configure script will set this up for you if you want it.
  1268.      */
  1269.  
  1270. #ifdef DOSUID
  1271.     char *s;
  1272.  
  1273.     if (Fstat(fileno(rsfp),&statbuf) < 0)    /* normal stat is insecure */
  1274.     croak("Can't stat script \"%s\"",origfilename);
  1275.     if (statbuf.st_mode & (S_ISUID|S_ISGID)) {
  1276.     I32 len;
  1277.  
  1278. #ifdef IAMSUID
  1279. #ifndef HAS_SETREUID
  1280.     /* On this access check to make sure the directories are readable,
  1281.      * there is actually a small window that the user could use to make
  1282.      * filename point to an accessible directory.  So there is a faint
  1283.      * chance that someone could execute a setuid script down in a
  1284.      * non-accessible directory.  I don't know what to do about that.
  1285.      * But I don't think it's too important.  The manual lies when
  1286.      * it says access() is useful in setuid programs.
  1287.      */
  1288.     if (access(SvPVX(GvSV(curcop->cop_filegv)),1))    /*double check*/
  1289.         croak("Permission denied");
  1290. #else
  1291.     /* If we can swap euid and uid, then we can determine access rights
  1292.      * with a simple stat of the file, and then compare device and
  1293.      * inode to make sure we did stat() on the same file we opened.
  1294.      * Then we just have to make sure he or she can execute it.
  1295.      */
  1296.     {
  1297.         struct stat tmpstatbuf;
  1298.  
  1299.         if (
  1300. #ifdef HAS_SETREUID
  1301.         setreuid(euid,uid) < 0
  1302. #else
  1303. # if HAS_SETRESUID
  1304.         setresuid(euid,uid,(Uid_t)-1) < 0
  1305. # endif
  1306. #endif
  1307.         || getuid() != euid || geteuid() != uid)
  1308.         croak("Can't swap uid and euid");    /* really paranoid */
  1309.         if (Stat(SvPVX(GvSV(curcop->cop_filegv)),&tmpstatbuf) < 0)
  1310.         croak("Permission denied");    /* testing full pathname here */
  1311.         if (tmpstatbuf.st_dev != statbuf.st_dev ||
  1312.         tmpstatbuf.st_ino != statbuf.st_ino) {
  1313.         (void)fclose(rsfp);
  1314.         if (rsfp = my_popen("/bin/mail root","w")) {    /* heh, heh */
  1315.             fprintf(rsfp,
  1316. "User %d tried to run dev %d ino %d in place of dev %d ino %d!\n\
  1317. (Filename of set-id script was %s, uid %d gid %d.)\n\nSincerely,\nperl\n",
  1318.             uid,tmpstatbuf.st_dev, tmpstatbuf.st_ino,
  1319.             statbuf.st_dev, statbuf.st_ino,
  1320.             SvPVX(GvSV(curcop->cop_filegv)),
  1321.             statbuf.st_uid, statbuf.st_gid);
  1322.             (void)my_pclose(rsfp);
  1323.         }
  1324.         croak("Permission denied\n");
  1325.         }
  1326.         if (
  1327. #ifdef HAS_SETREUID
  1328.               setreuid(uid,euid) < 0
  1329. #else
  1330. # if defined(HAS_SETRESUID)
  1331.               setresuid(uid,euid,(Uid_t)-1) < 0
  1332. # endif
  1333. #endif
  1334.               || getuid() != uid || geteuid() != euid)
  1335.         croak("Can't reswap uid and euid");
  1336.         if (!cando(S_IXUSR,FALSE,&statbuf))        /* can real uid exec? */
  1337.         croak("Permission denied\n");
  1338.     }
  1339. #endif /* HAS_SETREUID */
  1340. #endif /* IAMSUID */
  1341.  
  1342.     if (!S_ISREG(statbuf.st_mode))
  1343.         croak("Permission denied");
  1344.     if (statbuf.st_mode & S_IWOTH)
  1345.         croak("Setuid/gid script is writable by world");
  1346.     doswitches = FALSE;        /* -s is insecure in suid */
  1347.     curcop->cop_line++;
  1348.     if (fgets(tokenbuf,sizeof tokenbuf, rsfp) == Nullch ||
  1349.       strnNE(tokenbuf,"#!",2) )    /* required even on Sys V */
  1350.         croak("No #! line");
  1351.     s = tokenbuf+2;
  1352.     if (*s == ' ') s++;
  1353.     while (!isSPACE(*s)) s++;
  1354.     if (strnNE(s-4,"perl",4) && strnNE(s-9,"perl",4))  /* sanity check */
  1355.         croak("Not a perl script");
  1356.     while (*s == ' ' || *s == '\t') s++;
  1357.     /*
  1358.      * #! arg must be what we saw above.  They can invoke it by
  1359.      * mentioning suidperl explicitly, but they may not add any strange
  1360.      * arguments beyond what #! says if they do invoke suidperl that way.
  1361.      */
  1362.     len = strlen(validarg);
  1363.     if (strEQ(validarg," PHOOEY ") ||
  1364.         strnNE(s,validarg,len) || !isSPACE(s[len]))
  1365.         croak("Args must match #! line");
  1366.  
  1367. #ifndef IAMSUID
  1368.     if (euid != uid && (statbuf.st_mode & S_ISUID) &&
  1369.         euid == statbuf.st_uid)
  1370.         if (!do_undump)
  1371.         croak("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
  1372. FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
  1373. #endif /* IAMSUID */
  1374.  
  1375.     if (euid) {    /* oops, we're not the setuid root perl */
  1376.         (void)fclose(rsfp);
  1377. #ifndef IAMSUID
  1378.         (void)sprintf(buf, "%s/sperl%s", BIN, patchlevel);
  1379.         execv(buf, origargv);    /* try again */
  1380. #endif
  1381.         croak("Can't do setuid\n");
  1382.     }
  1383.  
  1384.     if (statbuf.st_mode & S_ISGID && statbuf.st_gid != egid) {
  1385. #ifdef HAS_SETEGID
  1386.         (void)setegid(statbuf.st_gid);
  1387. #else
  1388. #ifdef HAS_SETREGID
  1389.            (void)setregid((Gid_t)-1,statbuf.st_gid);
  1390. #else
  1391. #ifdef HAS_SETRESGID
  1392.            (void)setresgid((Gid_t)-1,statbuf.st_gid,(Gid_t)-1);
  1393. #else
  1394.         setgid(statbuf.st_gid);
  1395. #endif
  1396. #endif
  1397. #endif
  1398.         if (getegid() != statbuf.st_gid)
  1399.         croak("Can't do setegid!\n");
  1400.     }
  1401.     if (statbuf.st_mode & S_ISUID) {
  1402.         if (statbuf.st_uid != euid)
  1403. #ifdef HAS_SETEUID
  1404.         (void)seteuid(statbuf.st_uid);    /* all that for this */
  1405. #else
  1406. #ifdef HAS_SETREUID
  1407.                 (void)setreuid((Uid_t)-1,statbuf.st_uid);
  1408. #else
  1409. #ifdef HAS_SETRESUID
  1410.                 (void)setresuid((Uid_t)-1,statbuf.st_uid,(Uid_t)-1);
  1411. #else
  1412.         setuid(statbuf.st_uid);
  1413. #endif
  1414. #endif
  1415. #endif
  1416.         if (geteuid() != statbuf.st_uid)
  1417.         croak("Can't do seteuid!\n");
  1418.     }
  1419.     else if (uid) {            /* oops, mustn't run as root */
  1420. #ifdef HAS_SETEUID
  1421.           (void)seteuid((Uid_t)uid);
  1422. #else
  1423. #ifdef HAS_SETREUID
  1424.           (void)setreuid((Uid_t)-1,(Uid_t)uid);
  1425. #else
  1426. #ifdef HAS_SETRESUID
  1427.           (void)setresuid((Uid_t)-1,(Uid_t)uid,(Uid_t)-1);
  1428. #else
  1429.           setuid((Uid_t)uid);
  1430. #endif
  1431. #endif
  1432. #endif
  1433.         if (geteuid() != uid)
  1434.         croak("Can't do seteuid!\n");
  1435.     }
  1436.     init_ids();
  1437.     if (!cando(S_IXUSR,TRUE,&statbuf))
  1438.         croak("Permission denied\n");    /* they can't do this */
  1439.     }
  1440. #ifdef IAMSUID
  1441.     else if (preprocess)
  1442.     croak("-P not allowed for setuid/setgid script\n");
  1443.     else
  1444.     croak("Script is not setuid/setgid in suidperl\n");
  1445. #endif /* IAMSUID */
  1446. #else /* !DOSUID */
  1447.     if (euid != uid || egid != gid) {    /* (suidperl doesn't exist, in fact) */
  1448. #ifndef SETUID_SCRIPTS_ARE_SECURE_NOW
  1449.     Fstat(fileno(rsfp),&statbuf);    /* may be either wrapped or real suid */
  1450.     if ((euid != uid && euid == statbuf.st_uid && statbuf.st_mode & S_ISUID)
  1451.         ||
  1452.         (egid != gid && egid == statbuf.st_gid && statbuf.st_mode & S_ISGID)
  1453.        )
  1454.         if (!do_undump)
  1455.         croak("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
  1456. FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
  1457. #endif /* SETUID_SCRIPTS_ARE_SECURE_NOW */
  1458.     /* not set-id, must be wrapped */
  1459.     }
  1460. #endif /* DOSUID */
  1461. }
  1462.  
  1463. static void
  1464. find_beginning()
  1465. {
  1466.     register char *s;
  1467.  
  1468.     /* skip forward in input to the real script? */
  1469.  
  1470.     taint_not("-x");
  1471.     while (doextract) {
  1472.     if ((s = sv_gets(linestr, rsfp, 0)) == Nullch)
  1473.         croak("No Perl script found in input\n");
  1474.     if (*s == '#' && s[1] == '!' && instr(s,"perl")) {
  1475.         ungetc('\n',rsfp);        /* to keep line count right */
  1476.         doextract = FALSE;
  1477.         if (s = instr(s,"perl -")) {
  1478.         s += 6;
  1479.         /*SUPPRESS 530*/
  1480.         while (s = moreswitches(s)) ;
  1481.         }
  1482.         if (cddir && chdir(cddir) < 0)
  1483.         croak("Can't chdir to %s",cddir);
  1484.     }
  1485.     }
  1486. }
  1487.  
  1488. static void
  1489. init_ids()
  1490. {
  1491.     uid = (int)getuid();
  1492.     euid = (int)geteuid();
  1493.     gid = (int)getgid();
  1494.     egid = (int)getegid();
  1495. #ifdef VMS
  1496.     uid |= gid << 16;
  1497.     euid |= egid << 16;
  1498. #endif
  1499.     tainting |= (euid != uid || egid != gid);
  1500. }
  1501.  
  1502. static void
  1503. init_debugger()
  1504. {
  1505.     curstash = debstash;
  1506.     dbargs = GvAV(gv_AVadd((gv_fetchpv("args", GV_ADDMULTI, SVt_PVAV))));
  1507.     AvREAL_off(dbargs);
  1508.     DBgv = gv_fetchpv("DB", GV_ADDMULTI, SVt_PVGV);
  1509.     DBline = gv_fetchpv("dbline", GV_ADDMULTI, SVt_PVAV);
  1510.     DBsub = gv_HVadd(gv_fetchpv("sub", GV_ADDMULTI, SVt_PVHV));
  1511.     DBsingle = GvSV((gv_fetchpv("single", GV_ADDMULTI, SVt_PV)));
  1512.     DBtrace = GvSV((gv_fetchpv("trace", GV_ADDMULTI, SVt_PV)));
  1513.     DBsignal = GvSV((gv_fetchpv("signal", GV_ADDMULTI, SVt_PV)));
  1514.     curstash = defstash;
  1515. }
  1516.  
  1517. static void
  1518. init_stacks()
  1519. {
  1520.     stack = newAV();
  1521.     mainstack = stack;            /* remember in case we switch stacks */
  1522.     AvREAL_off(stack);            /* not a real array */
  1523.     av_extend(stack,127);
  1524.  
  1525.     stack_base = AvARRAY(stack);
  1526.     stack_sp = stack_base;
  1527.     stack_max = stack_base + 127;
  1528.  
  1529.     New(54,markstack,64,I32);
  1530.     markstack_ptr = markstack;
  1531.     markstack_max = markstack + 64;
  1532.  
  1533.     New(54,scopestack,32,I32);
  1534.     scopestack_ix = 0;
  1535.     scopestack_max = 32;
  1536.  
  1537.     New(54,savestack,128,ANY);
  1538.     savestack_ix = 0;
  1539.     savestack_max = 128;
  1540.  
  1541.     New(54,retstack,16,OP*);
  1542.     retstack_ix = 0;
  1543.     retstack_max = 16;
  1544.  
  1545.     New(50,cxstack,128,CONTEXT);
  1546.     cxstack_ix    = -1;
  1547.     cxstack_max    = 128;
  1548.  
  1549.     New(50,tmps_stack,128,SV*);
  1550.     tmps_ix = -1;
  1551.     tmps_max = 128;
  1552.  
  1553.     DEBUG( {
  1554.     New(51,debname,128,char);
  1555.     New(52,debdelim,128,char);
  1556.     } )
  1557. }
  1558.  
  1559. static FILE *tmpfp;  /* moved outside init_lexer() because of UNICOS bug */
  1560. static void
  1561. init_lexer()
  1562. {
  1563.     tmpfp = rsfp;
  1564.  
  1565.     lex_start(linestr);
  1566.     rsfp = tmpfp;
  1567.     subname = newSVpv("main",4);
  1568. }
  1569.  
  1570. static void
  1571. init_predump_symbols()
  1572. {
  1573.     GV *tmpgv;
  1574.     GV *othergv;
  1575.  
  1576.     sv_setpvn(GvSV(gv_fetchpv("\"", TRUE, SVt_PV)), " ", 1);
  1577.  
  1578.     stdingv = gv_fetchpv("STDIN",TRUE, SVt_PVIO);
  1579.     SvMULTI_on(stdingv);
  1580.     IoIFP(GvIOp(stdingv)) = stdin;
  1581.     tmpgv = gv_fetchpv("stdin",TRUE, SVt_PV);
  1582.     GvIOp(tmpgv) = (IO*)SvREFCNT_inc(GvIOp(stdingv));
  1583.     SvMULTI_on(tmpgv);
  1584.  
  1585.     tmpgv = gv_fetchpv("STDOUT",TRUE, SVt_PVIO);
  1586.     SvMULTI_on(tmpgv);
  1587.     IoOFP(GvIOp(tmpgv)) = IoIFP(GvIOp(tmpgv)) = stdout;
  1588.     defoutgv = tmpgv;
  1589.     tmpgv = gv_fetchpv("stdout",TRUE, SVt_PV);
  1590.     GvIOp(tmpgv) = (IO*)SvREFCNT_inc(GvIOp(defoutgv));
  1591.     SvMULTI_on(tmpgv);
  1592.  
  1593.     othergv = gv_fetchpv("STDERR",TRUE, SVt_PVIO);
  1594.     SvMULTI_on(othergv);
  1595.     IoOFP(GvIOp(othergv)) = IoIFP(GvIOp(othergv)) = stderr;
  1596.     tmpgv = gv_fetchpv("stderr",TRUE, SVt_PV);
  1597.     GvIOp(tmpgv) = (IO*)SvREFCNT_inc(GvIOp(othergv));
  1598.     SvMULTI_on(tmpgv);
  1599.  
  1600.     statname = NEWSV(66,0);        /* last filename we did stat on */
  1601. }
  1602.  
  1603. static void
  1604. init_postdump_symbols(argc,argv,env)
  1605. register int argc;
  1606. register char **argv;
  1607. register char **env;
  1608. {
  1609.     char *s;
  1610.     SV *sv;
  1611.     GV* tmpgv;
  1612.  
  1613.     argc--,argv++;    /* skip name of script */
  1614.     if (doswitches) {
  1615.     for (; argc > 0 && **argv == '-'; argc--,argv++) {
  1616.         if (!argv[0][1])
  1617.         break;
  1618.         if (argv[0][1] == '-') {
  1619.         argc--,argv++;
  1620.         break;
  1621.         }
  1622.         if (s = strchr(argv[0], '=')) {
  1623.         *s++ = '\0';
  1624.         sv_setpv(GvSV(gv_fetchpv(argv[0]+1,TRUE, SVt_PV)),s);
  1625.         }
  1626.         else
  1627.         sv_setiv(GvSV(gv_fetchpv(argv[0]+1,TRUE, SVt_PV)),1);
  1628.     }
  1629.     }
  1630.     toptarget = NEWSV(0,0);
  1631.     sv_upgrade(toptarget, SVt_PVFM);
  1632.     sv_setpvn(toptarget, "", 0);
  1633.     bodytarget = NEWSV(0,0);
  1634.     sv_upgrade(bodytarget, SVt_PVFM);
  1635.     sv_setpvn(bodytarget, "", 0);
  1636.     formtarget = bodytarget;
  1637.  
  1638.     tainted = 1;
  1639.     if (tmpgv = gv_fetchpv("0",TRUE, SVt_PV)) {
  1640.     sv_setpv(GvSV(tmpgv),origfilename);
  1641.     magicname("0", "0", 1);
  1642.     }
  1643.     if (tmpgv = gv_fetchpv("\024",TRUE, SVt_PV))
  1644.     time(&basetime);
  1645.     if (tmpgv = gv_fetchpv("\030",TRUE, SVt_PV))
  1646.     sv_setpv(GvSV(tmpgv),origargv[0]);
  1647.     if (argvgv = gv_fetchpv("ARGV",TRUE, SVt_PVAV)) {
  1648.     SvMULTI_on(argvgv);
  1649.     (void)gv_AVadd(argvgv);
  1650.     av_clear(GvAVn(argvgv));
  1651.     for (; argc > 0; argc--,argv++) {
  1652.         av_push(GvAVn(argvgv),newSVpv(argv[0],0));
  1653.     }
  1654.     }
  1655.     if (envgv = gv_fetchpv("ENV",TRUE, SVt_PVHV)) {
  1656.     HV *hv;
  1657.     SvMULTI_on(envgv);
  1658.     hv = GvHVn(envgv);
  1659.     hv_clear(hv);
  1660. #ifndef VMS  /* VMS doesn't have environ array */
  1661.     if (env != environ) {
  1662.         environ[0] = Nullch;
  1663.         hv_magic(hv, envgv, 'E');
  1664.     }
  1665. #endif
  1666. #ifdef DYNAMIC_ENV_FETCH
  1667.     HvNAME(hv) = savepv(ENV_HV_NAME);
  1668. #endif
  1669.     for (; *env; env++) {
  1670.         if (!(s = strchr(*env,'=')))
  1671.         continue;
  1672.         *s++ = '\0';
  1673.         sv = newSVpv(s--,0);
  1674.         sv_magic(sv, sv, 'e', *env, s - *env);
  1675.         (void)hv_store(hv, *env, s - *env, sv, 0);
  1676.         *s = '=';
  1677.     }
  1678.     hv_magic(hv, envgv, 'E');
  1679.     }
  1680.     tainted = 0;
  1681.     if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
  1682.     sv_setiv(GvSV(tmpgv),(I32)getpid());
  1683.  
  1684. }
  1685.  
  1686. static void
  1687. init_perllib()
  1688. {
  1689.     char *s;
  1690.     if (!tainting) {
  1691.     s = getenv("PERL5LIB");
  1692.     if (s)
  1693.         incpush(s);
  1694.     else
  1695.         incpush(getenv("PERLLIB"));
  1696.     }
  1697.  
  1698. #ifdef SITELIB_EXP
  1699.     incpush(SITELIB_EXP);
  1700. #endif
  1701. #ifdef ARCHLIB_EXP
  1702.     incpush(ARCHLIB_EXP);
  1703. #endif
  1704. #ifndef PRIVLIB_EXP
  1705. #define PRIVLIB_EXP "/usr/local/lib/perl5:/usr/local/lib/perl"
  1706. #endif
  1707.     incpush(PRIVLIB_EXP);
  1708.     
  1709.     av_push(GvAVn(incgv),newSVpv(".",1));
  1710. }
  1711.  
  1712. void
  1713. calllist(list)
  1714. AV* list;
  1715. {
  1716.     jmp_buf oldtop;
  1717.     STRLEN len;
  1718.     line_t oldline = curcop->cop_line;
  1719.  
  1720.     Copy(top_env, oldtop, 1, jmp_buf);
  1721.  
  1722.     while (AvFILL(list) >= 0) {
  1723.     CV *cv = (CV*)av_shift(list);
  1724.  
  1725.     SAVEFREESV(cv);
  1726.  
  1727.     switch (setjmp(top_env)) {
  1728.     case 0: {
  1729.         SV* atsv = GvSV(gv_fetchpv("@",TRUE, SVt_PV));
  1730.         PUSHMARK(stack_sp);
  1731.         perl_call_sv((SV*)cv, G_EVAL|G_DISCARD);
  1732.         (void)SvPV(atsv, len);
  1733.         if (len) {
  1734.             Copy(oldtop, top_env, 1, jmp_buf);
  1735.             curcop = &compiling;
  1736.             curcop->cop_line = oldline;
  1737.             if (list == beginav)
  1738.             sv_catpv(atsv, "BEGIN failed--compilation aborted");
  1739.             else
  1740.             sv_catpv(atsv, "END failed--cleanup aborted");
  1741.             croak("%s", SvPVX(atsv));
  1742.         }
  1743.         }
  1744.         break;
  1745.     case 1:
  1746. #ifdef VMS
  1747.         statusvalue = 255;    /* XXX I don't think we use 1 anymore. */
  1748. #else
  1749.     statusvalue = 1;
  1750. #endif
  1751.         /* FALL THROUGH */
  1752.     case 2:
  1753.         /* my_exit() was called */
  1754.         curstash = defstash;
  1755.         if (endav)
  1756.         calllist(endav);
  1757.         FREETMPS;
  1758.         Copy(oldtop, top_env, 1, jmp_buf);
  1759.         curcop = &compiling;
  1760.         curcop->cop_line = oldline;
  1761.         if (statusvalue) {
  1762.         if (list == beginav)
  1763.             croak("BEGIN failed--compilation aborted");
  1764.         else
  1765.             croak("END failed--cleanup aborted");
  1766.         }
  1767.         my_exit(statusvalue);
  1768.         /* NOTREACHED */
  1769.         return;
  1770.     case 3:
  1771.         if (!restartop) {
  1772.         fprintf(stderr, "panic: restartop\n");
  1773.         FREETMPS;
  1774.         break;
  1775.         }
  1776.         Copy(oldtop, top_env, 1, jmp_buf);
  1777.         curcop = &compiling;
  1778.         curcop->cop_line = oldline;
  1779.         longjmp(top_env, 3);
  1780.     }
  1781.     }
  1782.  
  1783.     Copy(oldtop, top_env, 1, jmp_buf);
  1784. }
  1785.  
  1786.