home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / PERL4036.ZIP / perl.c < prev    next >
C/C++ Source or Header  |  1993-02-26  |  38KB  |  1,463 lines

  1. char rcsid[] = "$RCSfile: perl.c,v $$Revision: 4.0.1.8 $$Date: 1993/02/05 19:39:30 $\nPatch level: ###\n";
  2. /*
  3.  *    Copyright (c) 1991, 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.  * $Log: perl.c,v $
  9.  * Revision 4.0.1.8  1993/02/05  19:39:30  lwall
  10.  * patch36: the taintanyway code wasn't tainting anyway
  11.  * patch36: Malformed cmd links core dump apparently fixed
  12.  *
  13.  * Revision 4.0.1.7  92/06/08  14:50:39  lwall
  14.  * patch20: PERLLIB now supports multiple directories
  15.  * patch20: running taintperl explicitly now does checks even if $< == $>
  16.  * patch20: -e 'cmd' no longer fails silently if /tmp runs out of space
  17.  * patch20: perl -P now uses location of sed determined by Configure
  18.  * patch20: form feed for formats is now specifiable via $^L
  19.  * patch20: paragraph mode now skips extra newlines automatically
  20.  * patch20: eval "1 #comment" didn't work
  21.  * patch20: couldn't require . files
  22.  * patch20: semantic compilation errors didn't abort execution
  23.  * 
  24.  * Revision 4.0.1.6  91/11/11  16:38:45  lwall
  25.  * patch19: default arg for shift was wrong after first subroutine definition
  26.  * patch19: op/regexp.t failed from missing arg to bcmp()
  27.  * 
  28.  * Revision 4.0.1.5  91/11/05  18:03:32  lwall
  29.  * patch11: random cleanup
  30.  * patch11: $0 was being truncated at times
  31.  * patch11: cppstdin now installed outside of source directory
  32.  * patch11: -P didn't allow use of #elif or #undef
  33.  * patch11: prepared for ctype implementations that don't define isascii()
  34.  * patch11: added eval {}
  35.  * patch11: eval confused by string containing null
  36.  * 
  37.  * Revision 4.0.1.4  91/06/10  01:23:07  lwall
  38.  * patch10: perl -v printed incorrect copyright notice
  39.  * 
  40.  * Revision 4.0.1.3  91/06/07  11:40:18  lwall
  41.  * patch4: changed old $^P to $^X
  42.  * 
  43.  * Revision 4.0.1.2  91/06/07  11:26:16  lwall
  44.  * patch4: new copyright notice
  45.  * patch4: added $^P variable to control calling of perldb routines
  46.  * patch4: added $^F variable to specify maximum system fd, default 2
  47.  * patch4: debugger lost track of lines in eval
  48.  * 
  49.  * Revision 4.0.1.1  91/04/11  17:49:05  lwall
  50.  * patch1: fixed undefined environ problem
  51.  * 
  52.  * Revision 4.0  91/03/20  01:37:44  lwall
  53.  * 4.0 baseline.
  54.  * 
  55.  */
  56.  
  57. /*SUPPRESS 560*/
  58.  
  59. #include "EXTERN.h"
  60. #include "perl.h"
  61. #include "perly.h"
  62. #include "patchlevel.h"
  63.  
  64. #ifdef OS2
  65. #include <fcntl.h>
  66. #endif
  67.  
  68. char *getenv();
  69.  
  70. #ifdef IAMSUID
  71. #ifndef DOSUID
  72. #define DOSUID
  73. #endif
  74. #endif
  75.  
  76. #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
  77. #ifdef DOSUID
  78. #undef DOSUID
  79. #endif
  80. #endif
  81.  
  82. static char* moreswitches();
  83. static void incpush();
  84. static char* cddir;
  85. static bool minus_c;
  86. static char patchlevel[6];
  87. static char *nrs = "\n";
  88. static int nrschar = '\n';      /* final char of rs, or 0777 if none */
  89. static int nrslen = 1;
  90.  
  91. main(argc,argv,env)
  92. int argc;
  93. char **argv;
  94. char **env;
  95. {
  96.     register STR *str;
  97.     register char *s;
  98.     char *scriptname;
  99.     char *getenv();
  100.     bool dosearch = FALSE;
  101. #ifdef DOSUID
  102.     char *validarg = "";
  103. #endif
  104.  
  105. #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
  106. #ifdef IAMSUID
  107. #undef IAMSUID
  108.     fatal("suidperl is no longer needed since the kernel can now execute\n\
  109. setuid perl scripts securely.\n");
  110. #endif
  111. #endif
  112.  
  113. #ifdef OS2
  114.     os2init(&argc, &argv);
  115. #endif
  116.  
  117.     origargv = argv;
  118.     origargc = argc;
  119.     origenviron = environ;
  120.     uid = (int)getuid();
  121.     euid = (int)geteuid();
  122.     gid = (int)getgid();
  123.     egid = (int)getegid();
  124.     sprintf(patchlevel,"%3.3s%2.2d", index(rcsid,'4'), PATCHLEVEL);
  125. #ifdef MSDOS
  126.     /*
  127.      * There is no way we can refer to them from Perl so close them to save
  128.      * space.  The other alternative would be to provide STDAUX and STDPRN
  129.      * filehandles.
  130.      */
  131.     (void)fclose(stdaux);
  132.     (void)fclose(stdprn);
  133. #endif
  134.     if (do_undump) {
  135.     origfilename = savestr(argv[0]);
  136.     do_undump = 0;
  137.     loop_ptr = -1;        /* start label stack again */
  138.     goto just_doit;
  139.     }
  140. #ifdef TAINT
  141. #ifndef DOSUID
  142.     if (uid == euid && gid == egid)
  143.     taintanyway = TRUE;        /* running taintperl explicitly */
  144. #endif
  145. #endif
  146.     (void)sprintf(index(rcsid,'#'), "%d\n", PATCHLEVEL);
  147.     linestr = Str_new(65,80);
  148.     str_nset(linestr,"",0);
  149.     str = str_make("",0);        /* first used for -I flags */
  150.     curstash = defstash = hnew(0);
  151.     curstname = str_make("main",4);
  152.     stab_xhash(stabent("_main",TRUE)) = defstash;
  153.     defstash->tbl_name = "main";
  154.     incstab = hadd(aadd(stabent("INC",TRUE)));
  155.     incstab->str_pok |= SP_MULTI;
  156.     for (argc--,argv++; argc > 0; argc--,argv++) {
  157.     if (argv[0][0] != '-' || !argv[0][1])
  158.         break;
  159. #ifdef DOSUID
  160.     if (*validarg)
  161.     validarg = " PHOOEY ";
  162.     else
  163.     validarg = argv[0];
  164. #endif
  165.     s = argv[0]+1;
  166.       reswitch:
  167.     switch (*s) {
  168.     case '0':
  169.     case 'a':
  170.     case 'c':
  171.     case 'd':
  172.     case 'D':
  173.     case 'i':
  174.     case 'l':
  175.     case 'n':
  176.     case 'p':
  177.     case 'u':
  178.     case 'U':
  179.     case 'v':
  180.     case 'w':
  181.         if (s = moreswitches(s))
  182.         goto reswitch;
  183.         break;
  184.  
  185.     case 'e':
  186. #ifdef TAINT
  187.         if (euid != uid || egid != gid)
  188.         fatal("No -e allowed in setuid scripts");
  189. #endif
  190.         if (!e_fp) {
  191.             e_tmpname = savestr(TMPPATH);
  192.         (void)mktemp(e_tmpname);
  193.         if (!*e_tmpname)
  194.             fatal("Can't mktemp()");
  195.         e_fp = fopen(e_tmpname,"w");
  196.         if (!e_fp)
  197.             fatal("Cannot open temporary file");
  198.         }
  199.         if (argv[1]) {
  200.         fputs(argv[1],e_fp);
  201.         argc--,argv++;
  202.         }
  203.         (void)putc('\n', e_fp);
  204.         break;
  205.     case 'I':
  206. #ifdef TAINT
  207.         if (euid != uid || egid != gid)
  208.         fatal("No -I allowed in setuid scripts");
  209. #endif
  210.         str_cat(str,"-");
  211.         str_cat(str,s);
  212.         str_cat(str," ");
  213.         if (*++s) {
  214.         (void)apush(stab_array(incstab),str_make(s,0));
  215.         }
  216.         else if (argv[1]) {
  217.         (void)apush(stab_array(incstab),str_make(argv[1],0));
  218.         str_cat(str,argv[1]);
  219.         argc--,argv++;
  220.         str_cat(str," ");
  221.         }
  222.         break;
  223.     case 'P':
  224. #ifdef TAINT
  225.         if (euid != uid || egid != gid)
  226.         fatal("No -P allowed in setuid scripts");
  227. #endif
  228.         preprocess = TRUE;
  229.         s++;
  230.         goto reswitch;
  231.     case 's':
  232. #ifdef TAINT
  233.         if (euid != uid || egid != gid)
  234.         fatal("No -s allowed in setuid scripts");
  235. #endif
  236.         doswitches = TRUE;
  237.         s++;
  238.         goto reswitch;
  239.     case 'S':
  240. #ifdef TAINT
  241.         if (euid != uid || egid != gid)
  242.         fatal("No -S allowed in setuid scripts");
  243. #endif
  244.         dosearch = TRUE;
  245.         s++;
  246.         goto reswitch;
  247.     case 'x':
  248.         doextract = TRUE;
  249.         s++;
  250.         if (*s)
  251.         cddir = savestr(s);
  252.         break;
  253.     case '-':
  254.         argc--,argv++;
  255.         goto switch_end;
  256.     case 0:
  257.         break;
  258.     default:
  259.         fatal("Unrecognized switch: -%s",s);
  260.     }
  261.     }
  262.   switch_end:
  263.     scriptname = argv[0];
  264.     if (e_fp) {
  265.     if (fflush(e_fp) || ferror(e_fp) || fclose(e_fp))
  266.         fatal("Can't write to temp file for -e: %s", strerror(errno));
  267.     argc++,argv--;
  268.     scriptname = e_tmpname;
  269.     }
  270.  
  271. #ifdef DOSISH
  272. #define PERLLIB_SEP ';'
  273. #else
  274. #define PERLLIB_SEP ':'
  275. #endif
  276. #ifndef TAINT        /* Can't allow arbitrary PERLLIB in setuid script */
  277.     incpush(getenv("PERLLIB"));
  278. #endif /* TAINT */
  279.  
  280. #ifndef PRIVLIB
  281. #define PRIVLIB "/usr/local/lib/perl"
  282. #endif
  283.     incpush(PRIVLIB);
  284.     (void)apush(stab_array(incstab),str_make(".",1));
  285.  
  286.     str_set(&str_no,No);
  287.     str_set(&str_yes,Yes);
  288.  
  289.     /* open script */
  290.  
  291.     if (scriptname == Nullch)
  292. #ifdef DOSISH
  293.     {
  294.     if ( isatty(fileno(stdin)) )
  295.       moreswitches("v");
  296.     scriptname = "-";
  297.     }
  298. #else
  299.     scriptname = "-";
  300. #endif
  301.     if (dosearch && !index(scriptname, '/')
  302. #ifdef DOSISH
  303.     && !index(scriptname, '\\') && access(scriptname, 0)
  304. #endif
  305.     && (s = getenv("PATH"))) {
  306.     char *xfound = Nullch, *xfailed = Nullch;
  307.     int len;
  308.  
  309.     bufend = s + strlen(s);
  310.     while (*s) {
  311. #ifndef DOSISH
  312.         s = cpytill(tokenbuf,s,bufend,':',&len);
  313. #else
  314. #ifdef atarist
  315.         for (len = 0; *s && *s != ',' && *s != ';'; tokenbuf[len++] = *s++);
  316.         tokenbuf[len] = '\0';
  317. #else
  318.         for (len = 0; *s && *s != ';'; tokenbuf[len++] = *s++);
  319.         tokenbuf[len] = '\0';
  320. #endif
  321. #endif
  322.         if (*s)
  323.         s++;
  324. #ifndef DOSISH
  325.         if (len && tokenbuf[len-1] != '/')
  326. #else
  327.         if (len && ((tokenbuf[len-1] != '\\') && (tokenbuf[len-1] != '/')))
  328. #endif
  329.         (void)strcat(tokenbuf+len,"/");
  330.         (void)strcat(tokenbuf+len,scriptname);
  331. #ifdef DEBUGGING
  332.         if (debug & 1)
  333.         fprintf(stderr,"Looking for %s\n",tokenbuf);
  334. #endif
  335.         if (stat(tokenbuf,&statbuf) < 0)        /* not there? */
  336.         continue;
  337.         if (S_ISREG(statbuf.st_mode)
  338.          && cando(S_IRUSR,TRUE,&statbuf) && cando(S_IXUSR,TRUE,&statbuf)) {
  339.         xfound = tokenbuf;              /* bingo! */
  340.         break;
  341.         }
  342.         if (!xfailed)
  343.         xfailed = savestr(tokenbuf);
  344.     }
  345.     if (!xfound)
  346.         fatal("Can't execute %s", xfailed ? xfailed : scriptname );
  347.     if (xfailed)
  348.         Safefree(xfailed);
  349.     scriptname = savestr(xfound);
  350.     }
  351.  
  352.     fdpid = anew(Nullstab);    /* for remembering popen pids by fd */
  353.     pidstatus = hnew(COEFFSIZE);/* for remembering status of dead pids */
  354.  
  355.     origfilename = savestr(scriptname);
  356.     curcmd->c_filestab = fstab(origfilename);
  357.     if (strEQ(origfilename,"-"))
  358.     scriptname = "";
  359.     if (preprocess) {
  360.     char *cpp = CPPSTDIN;
  361.  
  362.     if (strEQ(cpp,"cppstdin"))
  363.         sprintf(tokenbuf, "%s/%s", SCRIPTDIR, cpp);
  364.     else
  365.         sprintf(tokenbuf, "%s", cpp);
  366.     str_cat(str,"-I");
  367.     str_cat(str,PRIVLIB);
  368. #ifdef DOSISH
  369.     (void)sprintf(buf, "\
  370. sed %s -e \"/^[^#]/b\" \
  371.  -e \"/^#[     ]*include[     ]/b\" \
  372.  -e \"/^#[     ]*define[     ]/b\" \
  373.  -e \"/^#[     ]*if[     ]/b\" \
  374.  -e \"/^#[     ]*ifdef[     ]/b\" \
  375.  -e \"/^#[     ]*ifndef[     ]/b\" \
  376.  -e \"/^#[     ]*else/b\" \
  377.  -e \"/^#[     ]*elif[     ]/b\" \
  378.  -e \"/^#[     ]*undef[     ]/b\" \
  379.  -e \"/^#[     ]*endif/b\" \
  380.  -e \"s/^#.*//\" \
  381.  %s | %s -C %s %s",
  382.       (doextract ? "-e \"1,/^#/d\n\"" : ""),
  383. #else
  384.     (void)sprintf(buf, "\
  385. %s %s -e '/^[^#]/b' \
  386.  -e '/^#[     ]*include[     ]/b' \
  387.  -e '/^#[     ]*define[     ]/b' \
  388.  -e '/^#[     ]*if[     ]/b' \
  389.  -e '/^#[     ]*ifdef[     ]/b' \
  390.  -e '/^#[     ]*ifndef[     ]/b' \
  391.  -e '/^#[     ]*else/b' \
  392.  -e '/^#[     ]*elif[     ]/b' \
  393.  -e '/^#[     ]*undef[     ]/b' \
  394.  -e '/^#[     ]*endif/b' \
  395.  -e 's/^[     ]*#.*//' \
  396.  %s | %s -C %s %s",
  397. #ifdef LOC_SED
  398.       LOC_SED,
  399. #else
  400.       "sed",
  401. #endif
  402.       (doextract ? "-e '1,/^#/d\n'" : ""),
  403. #endif
  404.       scriptname, tokenbuf, str_get(str), CPPMINUS);
  405. #ifdef DEBUGGING
  406.     if (debug & 64) {
  407.         fputs(buf,stderr);
  408.         fputs("\n",stderr);
  409.     }
  410. #endif
  411.     doextract = FALSE;
  412. #ifdef IAMSUID                /* actually, this is caught earlier */
  413.     if (euid != uid && !euid) {    /* if running suidperl */
  414. #ifdef HAS_SETEUID
  415.         (void)seteuid(uid);        /* musn't stay setuid root */
  416. #else
  417. #ifdef HAS_SETREUID
  418.         (void)setreuid(-1, uid);
  419. #else
  420.         setuid(uid);
  421. #endif
  422. #endif
  423.         if (geteuid() != uid)
  424.         fatal("Can't do seteuid!\n");
  425.     }
  426. #endif /* IAMSUID */
  427.     rsfp = mypopen(buf,"r");
  428.     }
  429.     else if (!*scriptname) {
  430. #ifdef TAINT
  431.     if (euid != uid || egid != gid)
  432.         fatal("Can't take set-id script from stdin");
  433. #endif
  434.     rsfp = stdin;
  435.     }
  436.     else
  437.     rsfp = fopen(scriptname,"r");
  438.     if ((FILE*)rsfp == Nullfp) {
  439. #ifdef DOSUID
  440. #ifndef IAMSUID        /* in case script is not readable before setuid */
  441.     if (euid && stat(stab_val(curcmd->c_filestab)->str_ptr,&statbuf) >= 0 &&
  442.       statbuf.st_mode & (S_ISUID|S_ISGID)) {
  443.         (void)sprintf(buf, "%s/sperl%s", BIN, patchlevel);
  444.         execv(buf, origargv);    /* try again */
  445.         fatal("Can't do setuid\n");
  446.     }
  447. #endif
  448. #endif
  449.     fatal("Can't open perl script \"%s\": %s\n",
  450.       stab_val(curcmd->c_filestab)->str_ptr, strerror(errno));
  451.     }
  452.     str_free(str);        /* free -I directories */
  453.     str = Nullstr;
  454.  
  455.     /* do we need to emulate setuid on scripts? */
  456.  
  457.     /* This code is for those BSD systems that have setuid #! scripts disabled
  458.      * in the kernel because of a security problem.  Merely defining DOSUID
  459.      * in perl will not fix that problem, but if you have disabled setuid
  460.      * scripts in the kernel, this will attempt to emulate setuid and setgid
  461.      * on scripts that have those now-otherwise-useless bits set.  The setuid
  462.      * root version must be called suidperl or sperlN.NNN.  If regular perl
  463.      * discovers that it has opened a setuid script, it calls suidperl with
  464.      * the same argv that it had.  If suidperl finds that the script it has
  465.      * just opened is NOT setuid root, it sets the effective uid back to the
  466.      * uid.  We don't just make perl setuid root because that loses the
  467.      * effective uid we had before invoking perl, if it was different from the
  468.      * uid.
  469.      *
  470.      * DOSUID must be defined in both perl and suidperl, and IAMSUID must
  471.      * be defined in suidperl only.  suidperl must be setuid root.  The
  472.      * Configure script will set this up for you if you want it.
  473.      *
  474.      * There is also the possibility of have a script which is running
  475.      * set-id due to a C wrapper.  We want to do the TAINT checks
  476.      * on these set-id scripts, but don't want to have the overhead of
  477.      * them in normal perl, and can't use suidperl because it will lose
  478.      * the effective uid info, so we have an additional non-setuid root
  479.      * version called taintperl or tperlN.NNN that just does the TAINT checks.
  480.      */
  481.  
  482. #ifdef DOSUID
  483.     if (fstat(fileno(rsfp),&statbuf) < 0)    /* normal stat is insecure */
  484.     fatal("Can't stat script \"%s\"",origfilename);
  485.     if (statbuf.st_mode & (S_ISUID|S_ISGID)) {
  486.     int len;
  487.  
  488. #ifdef IAMSUID
  489. #ifndef HAS_SETREUID
  490.     /* On this access check to make sure the directories are readable,
  491.      * there is actually a small window that the user could use to make
  492.      * filename point to an accessible directory.  So there is a faint
  493.      * chance that someone could execute a setuid script down in a
  494.      * non-accessible directory.  I don't know what to do about that.
  495.      * But I don't think it's too important.  The manual lies when
  496.      * it says access() is useful in setuid programs.
  497.      */
  498.     if (access(stab_val(curcmd->c_filestab)->str_ptr,1))    /*double check*/
  499.         fatal("Permission denied");
  500. #else
  501.     /* If we can swap euid and uid, then we can determine access rights
  502.      * with a simple stat of the file, and then compare device and
  503.      * inode to make sure we did stat() on the same file we opened.
  504.      * Then we just have to make sure he or she can execute it.
  505.      */
  506.     {
  507.         struct stat tmpstatbuf;
  508.  
  509.         if (setreuid(euid,uid) < 0 || getuid() != euid || geteuid() != uid)
  510.         fatal("Can't swap uid and euid");    /* really paranoid */
  511.         if (stat(stab_val(curcmd->c_filestab)->str_ptr,&tmpstatbuf) < 0)
  512.         fatal("Permission denied");    /* testing full pathname here */
  513.         if (tmpstatbuf.st_dev != statbuf.st_dev ||
  514.         tmpstatbuf.st_ino != statbuf.st_ino) {
  515.         (void)fclose(rsfp);
  516.         if (rsfp = mypopen("mail root","w")) {    /* heh, heh */
  517.             fprintf(rsfp,
  518. "User %d tried to run dev %d ino %d in place of dev %d ino %d!\n\
  519. (Filename of set-id script was %s, uid %d gid %d.)\n\nSincerely,\nperl\n",
  520.             uid,tmpstatbuf.st_dev, tmpstatbuf.st_ino,
  521.             statbuf.st_dev, statbuf.st_ino,
  522.             stab_val(curcmd->c_filestab)->str_ptr,
  523.             statbuf.st_uid, statbuf.st_gid);
  524.             (void)mypclose(rsfp);
  525.         }
  526.         fatal("Permission denied\n");
  527.         }
  528.         if (setreuid(uid,euid) < 0 || getuid() != uid || geteuid() != euid)
  529.         fatal("Can't reswap uid and euid");
  530.         if (!cando(S_IXUSR,FALSE,&statbuf))        /* can real uid exec? */
  531.         fatal("Permission denied\n");
  532.     }
  533. #endif /* HAS_SETREUID */
  534. #endif /* IAMSUID */
  535.  
  536.     if (!S_ISREG(statbuf.st_mode))
  537.         fatal("Permission denied");
  538.     if (statbuf.st_mode & S_IWOTH)
  539.         fatal("Setuid/gid script is writable by world");
  540.     doswitches = FALSE;        /* -s is insecure in suid */
  541.     curcmd->c_line++;
  542.     if (fgets(tokenbuf,sizeof tokenbuf, rsfp) == Nullch ||
  543.       strnNE(tokenbuf,"#!",2) )    /* required even on Sys V */
  544.         fatal("No #! line");
  545.     s = tokenbuf+2;
  546.     if (*s == ' ') s++;
  547.     while (!isSPACE(*s)) s++;
  548.     if (strnNE(s-4,"perl",4) && strnNE(s-9,"perl",4))  /* sanity check */
  549.         fatal("Not a perl script");
  550.     while (*s == ' ' || *s == '\t') s++;
  551.     /*
  552.      * #! arg must be what we saw above.  They can invoke it by
  553.      * mentioning suidperl explicitly, but they may not add any strange
  554.      * arguments beyond what #! says if they do invoke suidperl that way.
  555.      */
  556.     len = strlen(validarg);
  557.     if (strEQ(validarg," PHOOEY ") ||
  558.         strnNE(s,validarg,len) || !isSPACE(s[len]))
  559.         fatal("Args must match #! line");
  560.  
  561. #ifndef IAMSUID
  562.     if (euid != uid && (statbuf.st_mode & S_ISUID) &&
  563.         euid == statbuf.st_uid)
  564.         if (!do_undump)
  565.         fatal("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
  566. FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
  567. #endif /* IAMSUID */
  568.  
  569.     if (euid) {    /* oops, we're not the setuid root perl */
  570.         (void)fclose(rsfp);
  571. #ifndef IAMSUID
  572.         (void)sprintf(buf, "%s/sperl%s", BIN, patchlevel);
  573.         execv(buf, origargv);    /* try again */
  574. #endif
  575.         fatal("Can't do setuid\n");
  576.     }
  577.  
  578.     if (statbuf.st_mode & S_ISGID && statbuf.st_gid != egid) {
  579. #ifdef HAS_SETEGID
  580.         (void)setegid(statbuf.st_gid);
  581. #else
  582. #ifdef HAS_SETREGID
  583.         (void)setregid((GIDTYPE)-1,statbuf.st_gid);
  584. #else
  585.         setgid(statbuf.st_gid);
  586. #endif
  587. #endif
  588.         if (getegid() != statbuf.st_gid)
  589.         fatal("Can't do setegid!\n");
  590.     }
  591.     if (statbuf.st_mode & S_ISUID) {
  592.         if (statbuf.st_uid != euid)
  593. #ifdef HAS_SETEUID
  594.         (void)seteuid(statbuf.st_uid);    /* all that for this */
  595. #else
  596. #ifdef HAS_SETREUID
  597.         (void)setreuid((UIDTYPE)-1,statbuf.st_uid);
  598. #else
  599.         setuid(statbuf.st_uid);
  600. #endif
  601. #endif
  602.         if (geteuid() != statbuf.st_uid)
  603.         fatal("Can't do seteuid!\n");
  604.     }
  605.     else if (uid) {            /* oops, mustn't run as root */
  606. #ifdef HAS_SETEUID
  607.         (void)seteuid((UIDTYPE)uid);
  608. #else
  609. #ifdef HAS_SETREUID
  610.         (void)setreuid((UIDTYPE)-1,(UIDTYPE)uid);
  611. #else
  612.         setuid((UIDTYPE)uid);
  613. #endif
  614. #endif
  615.         if (geteuid() != uid)
  616.         fatal("Can't do seteuid!\n");
  617.     }
  618.     uid = (int)getuid();
  619.     euid = (int)geteuid();
  620.     gid = (int)getgid();
  621.     egid = (int)getegid();
  622.     if (!cando(S_IXUSR,TRUE,&statbuf))
  623.         fatal("Permission denied\n");    /* they can't do this */
  624.     }
  625. #ifdef IAMSUID
  626.     else if (preprocess)
  627.     fatal("-P not allowed for setuid/setgid script\n");
  628.     else
  629.     fatal("Script is not setuid/setgid in suidperl\n");
  630. #else
  631. #ifndef TAINT        /* we aren't taintperl or suidperl */
  632.     /* script has a wrapper--can't run suidperl or we lose euid */
  633.     else if (euid != uid || egid != gid) {
  634.     (void)fclose(rsfp);
  635.     (void)sprintf(buf, "%s/tperl%s", BIN, patchlevel);
  636.     execv(buf, origargv);    /* try again */
  637.     fatal("Can't run setuid script with taint checks");
  638.     }
  639. #endif /* TAINT */
  640. #endif /* IAMSUID */
  641. #else /* !DOSUID */
  642. #ifndef TAINT        /* we aren't taintperl or suidperl */
  643.     if (euid != uid || egid != gid) {    /* (suidperl doesn't exist, in fact) */
  644. #ifndef SETUID_SCRIPTS_ARE_SECURE_NOW
  645.     fstat(fileno(rsfp),&statbuf);    /* may be either wrapped or real suid */
  646.     if ((euid != uid && euid == statbuf.st_uid && statbuf.st_mode & S_ISUID)
  647.         ||
  648.         (egid != gid && egid == statbuf.st_gid && statbuf.st_mode & S_ISGID)
  649.        )
  650.         if (!do_undump)
  651.         fatal("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
  652. FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
  653. #endif /* SETUID_SCRIPTS_ARE_SECURE_NOW */
  654.     /* not set-id, must be wrapped */
  655.     (void)fclose(rsfp);
  656.     (void)sprintf(buf, "%s/tperl%s", BIN, patchlevel);
  657.     execv(buf, origargv);    /* try again */
  658.     fatal("Can't run setuid script with taint checks");
  659.     }
  660. #endif /* TAINT */
  661. #endif /* DOSUID */
  662.  
  663. #if !defined(IAMSUID) && !defined(TAINT)
  664.  
  665.     /* skip forward in input to the real script? */
  666.  
  667.     while (doextract) {
  668.     if ((s = str_gets(linestr, rsfp, 0)) == Nullch)
  669.         fatal("No Perl script found in input\n");
  670.     if (*s == '#' && s[1] == '!' && instr(s,"perl")) {
  671.         ungetc('\n',rsfp);        /* to keep line count right */
  672.         doextract = FALSE;
  673.         if (s = instr(s,"perl -")) {
  674.         s += 6;
  675.         /*SUPPRESS 530*/
  676.         while (s = moreswitches(s)) ;
  677.         }
  678.         if (cddir && chdir(cddir) < 0)
  679.         fatal("Can't chdir to %s",cddir);
  680.     }
  681.     }
  682. #endif /* !defined(IAMSUID) && !defined(TAINT) */
  683.  
  684.     defstab = stabent("_",TRUE);
  685.  
  686.     subname = str_make("main",4);
  687.     if (perldb) {
  688.     debstash = hnew(0);
  689.     stab_xhash(stabent("_DB",TRUE)) = debstash;
  690.     curstash = debstash;
  691.     dbargs = stab_xarray(aadd((tmpstab = stabent("args",TRUE))));
  692.     tmpstab->str_pok |= SP_MULTI;
  693.     dbargs->ary_flags = 0;
  694.     DBstab = stabent("DB",TRUE);
  695.     DBstab->str_pok |= SP_MULTI;
  696.     DBline = stabent("dbline",TRUE);
  697.     DBline->str_pok |= SP_MULTI;
  698.     DBsub = hadd(tmpstab = stabent("sub",TRUE));
  699.     tmpstab->str_pok |= SP_MULTI;
  700.     DBsingle = stab_val((tmpstab = stabent("single",TRUE)));
  701.     tmpstab->str_pok |= SP_MULTI;
  702.     DBtrace = stab_val((tmpstab = stabent("trace",TRUE)));
  703.     tmpstab->str_pok |= SP_MULTI;
  704.     DBsignal = stab_val((tmpstab = stabent("signal",TRUE)));
  705.     tmpstab->str_pok |= SP_MULTI;
  706.     curstash = defstash;
  707.     }
  708.  
  709.     /* init tokener */
  710.  
  711.     bufend = bufptr = str_get(linestr);
  712.  
  713.     savestack = anew(Nullstab);        /* for saving non-local values */
  714.     stack = anew(Nullstab);        /* for saving non-local values */
  715.     stack->ary_flags = 0;        /* not a real array */
  716.     afill(stack,63); afill(stack,-1);    /* preextend stack */
  717.     afill(savestack,63); afill(savestack,-1);
  718.  
  719.     /* now parse the script */
  720.  
  721.     error_count = 0;
  722.     if (yyparse() || error_count) {
  723.     if (minus_c)
  724.         fatal("%s had compilation errors.\n", origfilename);
  725.     else {
  726.         fatal("Execution of %s aborted due to compilation errors.\n",
  727.         origfilename);
  728.     }
  729.     }
  730.  
  731.     New(50,loop_stack,128,struct loop);
  732. #ifdef DEBUGGING
  733.     if (debug) {
  734.     New(51,debname,128,char);
  735.     New(52,debdelim,128,char);
  736.     }
  737. #endif
  738.     curstash = defstash;
  739.  
  740.     preprocess = FALSE;
  741.     if (e_fp) {
  742.     e_fp = Nullfp;
  743.     (void)UNLINK(e_tmpname);
  744.     }
  745.  
  746.     /* initialize everything that won't change if we undump */
  747.  
  748.     if (sigstab = stabent("SIG",allstabs)) {
  749.     sigstab->str_pok |= SP_MULTI;
  750.     (void)hadd(sigstab);
  751.     }
  752.  
  753.     magicalize("!#?^~=-%.+&*()<>,\\/[|`':\004\t\020\024\027\006");
  754.     userinit();        /* in case linked C routines want magical variables */
  755.  
  756.     amperstab = stabent("&",allstabs);
  757.     leftstab = stabent("`",allstabs);
  758.     rightstab = stabent("'",allstabs);
  759.     sawampersand = (amperstab || leftstab || rightstab);
  760.     if (tmpstab = stabent(":",allstabs))
  761.     str_set(stab_val(tmpstab),chopset);
  762.     if (tmpstab = stabent("\024",allstabs))
  763.     time(&basetime);
  764.  
  765.     /* these aren't necessarily magical */
  766.     if (tmpstab = stabent("\014",allstabs)) {
  767.     str_set(stab_val(tmpstab),"\f");
  768.     formfeed = stab_val(tmpstab);
  769.     }
  770.     if (tmpstab = stabent(";",allstabs))
  771.     str_set(STAB_STR(tmpstab),"\034");
  772.     if (tmpstab = stabent("]",allstabs)) {
  773.     str = STAB_STR(tmpstab);
  774.     str_set(str,rcsid);
  775.     str->str_u.str_nval = atof(patchlevel);
  776.     str->str_nok = 1;
  777.     }
  778.     str_nset(stab_val(stabent("\"", TRUE)), " ", 1);
  779.  
  780.     stdinstab = stabent("STDIN",TRUE);
  781.     stdinstab->str_pok |= SP_MULTI;
  782.     if (!stab_io(stdinstab))
  783.     stab_io(stdinstab) = stio_new();
  784.     stab_io(stdinstab)->ifp = stdin;
  785.     tmpstab = stabent("stdin",TRUE);
  786.     stab_io(tmpstab) = stab_io(stdinstab);
  787.     tmpstab->str_pok |= SP_MULTI;
  788.  
  789.     tmpstab = stabent("STDOUT",TRUE);
  790.     tmpstab->str_pok |= SP_MULTI;
  791.     if (!stab_io(tmpstab))
  792.     stab_io(tmpstab) = stio_new();
  793.     stab_io(tmpstab)->ofp = stab_io(tmpstab)->ifp = stdout;
  794.     defoutstab = tmpstab;
  795.     tmpstab = stabent("stdout",TRUE);
  796.     stab_io(tmpstab) = stab_io(defoutstab);
  797.     tmpstab->str_pok |= SP_MULTI;
  798.  
  799.     curoutstab = stabent("STDERR",TRUE);
  800.     curoutstab->str_pok |= SP_MULTI;
  801.     if (!stab_io(curoutstab))
  802.     stab_io(curoutstab) = stio_new();
  803.     stab_io(curoutstab)->ofp = stab_io(curoutstab)->ifp = stderr;
  804.     tmpstab = stabent("stderr",TRUE);
  805.     stab_io(tmpstab) = stab_io(curoutstab);
  806.     tmpstab->str_pok |= SP_MULTI;
  807.     curoutstab = defoutstab;        /* switch back to STDOUT */
  808.  
  809.     statname = Str_new(66,0);        /* last filename we did stat on */
  810.  
  811.     /* now that script is parsed, we can modify record separator */
  812.  
  813.     rs = nrs;
  814.     rslen = nrslen;
  815.     rschar = nrschar;
  816.     rspara = (nrslen == 2);
  817.     str_nset(stab_val(stabent("/", TRUE)), rs, rslen);
  818.  
  819.     if (do_undump)
  820.     my_unexec();
  821.  
  822.   just_doit:        /* come here if running an undumped a.out */
  823.     argc--,argv++;    /* skip name of script */
  824.     if (doswitches) {
  825.     for (; argc > 0 && **argv == '-'; argc--,argv++) {
  826.         if (argv[0][1] == '-') {
  827.         argc--,argv++;
  828.         break;
  829.         }
  830.         if (s = index(argv[0], '=')) {
  831.         *s++ = '\0';
  832.         str_set(stab_val(stabent(argv[0]+1,TRUE)),s);
  833.         }
  834.         else
  835.         str_numset(stab_val(stabent(argv[0]+1,TRUE)),(double)1.0);
  836.     }
  837.     }
  838. #ifdef TAINT
  839.     tainted = 1;
  840. #endif
  841.     if (tmpstab = stabent("0",allstabs)) {
  842.     str_set(stab_val(tmpstab),origfilename);
  843.     magicname("0", Nullch, 0);
  844.     }
  845.     if (tmpstab = stabent("\030",allstabs))
  846.     str_set(stab_val(tmpstab),origargv[0]);
  847.     if (argvstab = stabent("ARGV",allstabs)) {
  848.     argvstab->str_pok |= SP_MULTI;
  849.     (void)aadd(argvstab);
  850.     aclear(stab_array(argvstab));
  851.     for (; argc > 0; argc--,argv++) {
  852.         (void)apush(stab_array(argvstab),str_make(argv[0],0));
  853.     }
  854.     }
  855. #ifdef TAINT
  856.     (void) stabent("ENV",TRUE);        /* must test PATH and IFS */
  857. #endif
  858.     if (envstab = stabent("ENV",allstabs)) {
  859.     envstab->str_pok |= SP_MULTI;
  860.     (void)hadd(envstab);
  861.     hclear(stab_hash(envstab), FALSE);
  862.     if (env != environ)
  863.         environ[0] = Nullch;
  864.     for (; *env; env++) {
  865.         if (!(s = index(*env,'=')))
  866.         continue;
  867.         *s++ = '\0';
  868.         str = str_make(s--,0);
  869.         str_magic(str, envstab, 'E', *env, s - *env);
  870.         (void)hstore(stab_hash(envstab), *env, s - *env, str, 0);
  871.         *s = '=';
  872.     }
  873.     }
  874. #ifdef TAINT
  875.     tainted = 0;
  876. #endif
  877.     if (tmpstab = stabent("$",allstabs))
  878.     str_numset(STAB_STR(tmpstab),(double)getpid());
  879.  
  880.     if (dowarn) {
  881.     stab_check('A','Z');
  882.     stab_check('a','z');
  883.     }
  884.  
  885.     if (setjmp(top_env))    /* sets goto_targ on longjump */
  886.     loop_ptr = -1;        /* start label stack again */
  887.  
  888. #ifdef DEBUGGING
  889.     if (debug & 1024)
  890.     dump_all();
  891.     if (debug)
  892.     fprintf(stderr,"\nEXECUTING...\n\n");
  893. #endif
  894.  
  895.     if (minus_c) {
  896.     fprintf(stderr,"%s syntax OK\n", origfilename);
  897.     exit(0);
  898.     }
  899.  
  900.     /* do it */
  901.  
  902.     (void) cmd_exec(main_root,G_SCALAR,-1);
  903.  
  904.     if (goto_targ)
  905.     fatal("Can't find label \"%s\"--aborting",goto_targ);
  906.     exit(0);
  907.     /* NOTREACHED */
  908. }
  909.  
  910. void
  911. magicalize(list)
  912. register char *list;
  913. {
  914.     char sym[2];
  915.  
  916.     sym[1] = '\0';
  917.     while (*sym = *list++)
  918.     magicname(sym, Nullch, 0);
  919. }
  920.  
  921. void
  922. magicname(sym,name,namlen)
  923. char *sym;
  924. char *name;
  925. int namlen;
  926. {
  927.     register STAB *stab;
  928.  
  929.     if (stab = stabent(sym,allstabs)) {
  930.     stab_flags(stab) = SF_VMAGIC;
  931.     str_magic(stab_val(stab), stab, 0, name, namlen);
  932.     }
  933. }
  934.  
  935. static void
  936. incpush(p)
  937. char *p;
  938. {
  939.     char *s;
  940.  
  941.     if (!p)
  942.     return;
  943.  
  944.     /* Break at all separators */
  945.     while (*p) {
  946.     /* First, skip any consecutive separators */
  947.     while ( *p == PERLLIB_SEP ) {
  948.         /* Uncomment the next line for PATH semantics */
  949.         /* (void)apush(stab_array(incstab), str_make(".", 1)); */
  950.         p++;
  951.     }
  952.     if ( (s = index(p, PERLLIB_SEP)) != Nullch ) {
  953.         (void)apush(stab_array(incstab), str_make(p, (int)(s - p)));
  954.         p = s + 1;
  955.     } else {
  956.         (void)apush(stab_array(incstab), str_make(p, 0));
  957.         break;
  958.     }
  959.     }
  960. }
  961.  
  962. void
  963. savelines(array, str)
  964. ARRAY *array;
  965. STR *str;
  966. {
  967.     register char *s = str->str_ptr;
  968.     register char *send = str->str_ptr + str->str_cur;
  969.     register char *t;
  970.     register int line = 1;
  971.  
  972.     while (s && s < send) {
  973.     STR *tmpstr = Str_new(85,0);
  974.  
  975.     t = index(s, '\n');
  976.     if (t)
  977.         t++;
  978.     else
  979.         t = send;
  980.  
  981.     str_nset(tmpstr, s, t - s);
  982.     astore(array, line++, tmpstr);
  983.     s = t;
  984.     }
  985. }
  986.  
  987. /* this routine is in perl.c by virtue of being sort of an alternate main() */
  988.  
  989. int
  990. do_eval(str,optype,stash,savecmd,gimme,arglast)
  991. STR *str;
  992. int optype;
  993. HASH *stash;
  994. int savecmd;
  995. int gimme;
  996. int *arglast;
  997. {
  998.     STR **st = stack->ary_array;
  999.     int retval;
  1000.     CMD *myroot = Nullcmd;
  1001.     ARRAY *ar;
  1002.     int i;
  1003.     CMD * VOLATILE oldcurcmd = curcmd;
  1004.     VOLATILE int oldtmps_base = tmps_base;
  1005.     VOLATILE int oldsave = savestack->ary_fill;
  1006.     VOLATILE int oldperldb = perldb;
  1007.     SPAT * VOLATILE oldspat = curspat;
  1008.     SPAT * VOLATILE oldlspat = lastspat;
  1009.     static char *last_eval = Nullch;
  1010.     static long last_elen = 0;
  1011.     static CMD *last_root = Nullcmd;
  1012.     VOLATILE int sp = arglast[0];
  1013.     char *specfilename;
  1014.     char *tmpfilename;
  1015.     int parsing = 1;
  1016.  
  1017.     tmps_base = tmps_max;
  1018.     if (curstash != stash) {
  1019.     (void)savehptr(&curstash);
  1020.     curstash = stash;
  1021.     }
  1022.     str_set(stab_val(stabent("@",TRUE)),"");
  1023.     if (curcmd->c_line == 0)        /* don't debug debugger... */
  1024.     perldb = FALSE;
  1025.     curcmd = &compiling;
  1026.     if (optype == O_EVAL) {        /* normal eval */
  1027.     curcmd->c_filestab = fstab("(eval)");
  1028.     curcmd->c_line = 1;
  1029.     str_sset(linestr,str);
  1030.     str_cat(linestr,";\n;\n");    /* be kind to them */
  1031.     if (perldb)
  1032.         savelines(stab_xarray(curcmd->c_filestab), linestr);
  1033.     }
  1034.     else {
  1035.     if (last_root && !in_eval) {
  1036.         Safefree(last_eval);
  1037.         last_eval = Nullch;
  1038.         cmd_free(last_root);
  1039.         last_root = Nullcmd;
  1040.     }
  1041.     specfilename = str_get(str);
  1042.     str_set(linestr,"");
  1043.     if (optype == O_REQUIRE && &str_undef !=
  1044.       hfetch(stab_hash(incstab), specfilename, strlen(specfilename), 0)) {
  1045.         curcmd = oldcurcmd;
  1046.         tmps_base = oldtmps_base;
  1047.         st[++sp] = &str_yes;
  1048.         perldb = oldperldb;
  1049.         return sp;
  1050.     }
  1051.     tmpfilename = savestr(specfilename);
  1052.     if (*tmpfilename == '/' ||
  1053.         (*tmpfilename == '.' && 
  1054.         (tmpfilename[1] == '/' ||
  1055.          (tmpfilename[1] == '.' && tmpfilename[2] == '/'))))
  1056.     {
  1057.         rsfp = fopen(tmpfilename,"r");
  1058.     }
  1059.     else {
  1060.         ar = stab_array(incstab);
  1061.         for (i = 0; i <= ar->ary_fill; i++) {
  1062.         (void)sprintf(buf, "%s/%s",
  1063.           str_get(afetch(ar,i,TRUE)), specfilename);
  1064.         rsfp = fopen(buf,"r");
  1065.         if (rsfp) {
  1066.             char *s = buf;
  1067.  
  1068.             if (*s == '.' && s[1] == '/')
  1069.             s += 2;
  1070.             Safefree(tmpfilename);
  1071.             tmpfilename = savestr(s);
  1072.             break;
  1073.         }
  1074.         }
  1075.     }
  1076.     curcmd->c_filestab = fstab(tmpfilename);
  1077.     Safefree(tmpfilename);
  1078.     tmpfilename = Nullch;
  1079.     if (!rsfp) {
  1080.         curcmd = oldcurcmd;
  1081.         tmps_base = oldtmps_base;
  1082.         if (optype == O_REQUIRE) {
  1083.         sprintf(tokenbuf,"Can't locate %s in @INC", specfilename);
  1084.         if (instr(tokenbuf,".h "))
  1085.             strcat(tokenbuf," (change .h to .ph maybe?)");
  1086.         if (instr(tokenbuf,".ph "))
  1087.             strcat(tokenbuf," (did you run h2ph?)");
  1088.         fatal("%s",tokenbuf);
  1089.         }
  1090.         if (gimme != G_ARRAY)
  1091.         st[++sp] = &str_undef;
  1092.         perldb = oldperldb;
  1093.         return sp;
  1094.     }
  1095.     curcmd->c_line = 0;
  1096.     }
  1097.     in_eval++;
  1098.     oldoldbufptr = oldbufptr = bufptr = str_get(linestr);
  1099.     bufend = bufptr + linestr->str_cur;
  1100.     if (++loop_ptr >= loop_max) {
  1101.     loop_max += 128;
  1102.     Renew(loop_stack, loop_max, struct loop);
  1103.     }
  1104.     loop_stack[loop_ptr].loop_label = "_EVAL_";
  1105.     loop_stack[loop_ptr].loop_sp = sp;
  1106. #ifdef DEBUGGING
  1107.     if (debug & 4) {
  1108.     deb("(Pushing label #%d _EVAL_)\n", loop_ptr);
  1109.     }
  1110. #endif
  1111.     eval_root = Nullcmd;
  1112.     if (setjmp(loop_stack[loop_ptr].loop_env)) {
  1113.     retval = 1;
  1114.     }
  1115.     else {
  1116.     error_count = 0;
  1117.     if (rsfp) {
  1118.         retval = yyparse();
  1119.         retval |= error_count;
  1120.     }
  1121.     else if (last_root && last_elen == bufend - bufptr
  1122.       && *bufptr == *last_eval && !bcmp(bufptr,last_eval,last_elen)){
  1123.         retval = 0;
  1124.         eval_root = last_root;    /* no point in reparsing */
  1125.     }
  1126.     else if (in_eval == 1 && !savecmd) {
  1127.         if (last_root) {
  1128.         Safefree(last_eval);
  1129.         last_eval = Nullch;
  1130.         cmd_free(last_root);
  1131.         }
  1132.         last_root = Nullcmd;
  1133.         last_elen = bufend - bufptr;
  1134.         last_eval = nsavestr(bufptr, last_elen);
  1135.         retval = yyparse();
  1136.         retval |= error_count;
  1137.         if (!retval)
  1138.         last_root = eval_root;
  1139.         if (!last_root) {
  1140.         Safefree(last_eval);
  1141.         last_eval = Nullch;
  1142.         }
  1143.     }
  1144.     else
  1145.         retval = yyparse();
  1146.     }
  1147.     myroot = eval_root;        /* in case cmd_exec does another eval! */
  1148.  
  1149.     if (retval || error_count) {
  1150.     st = stack->ary_array;
  1151.     sp = arglast[0];
  1152.     if (gimme != G_ARRAY)
  1153.         st[++sp] = &str_undef;
  1154.     if (parsing) {
  1155. #ifndef MANGLEDPARSE
  1156. #ifdef DEBUGGING
  1157.         if (debug & 128)
  1158.         fprintf(stderr,"Freeing eval_root %lx\n",(long)eval_root);
  1159. #endif
  1160.         cmd_free(eval_root);
  1161. #endif
  1162.         /*SUPPRESS 29*/ /*SUPPRESS 30*/
  1163.         if ((CMD*)eval_root == last_root)
  1164.         last_root = Nullcmd;
  1165.         eval_root = myroot = Nullcmd;
  1166.     }
  1167.     if (rsfp) {
  1168.         fclose(rsfp);
  1169.         rsfp = 0;
  1170.     }
  1171.     }
  1172.     else {
  1173.     parsing = 0;
  1174.     sp = cmd_exec(eval_root,gimme,sp);
  1175.     st = stack->ary_array;
  1176.     for (i = arglast[0] + 1; i <= sp; i++)
  1177.         st[i] = str_mortal(st[i]);
  1178.                 /* if we don't save result, free zaps it */
  1179.     if (savecmd)
  1180.         eval_root = myroot;
  1181.     else if (in_eval != 1 && myroot != last_root)
  1182.         cmd_free(myroot);
  1183.         if (eval_root == myroot)
  1184.         eval_root = Nullcmd;
  1185.     }
  1186.  
  1187.     perldb = oldperldb;
  1188.     in_eval--;
  1189. #ifdef DEBUGGING
  1190.     if (debug & 4) {
  1191.     char *tmps = loop_stack[loop_ptr].loop_label;
  1192.     deb("(Popping label #%d %s)\n",loop_ptr,
  1193.         tmps ? tmps : "" );
  1194.     }
  1195. #endif
  1196.     loop_ptr--;
  1197.     tmps_base = oldtmps_base;
  1198.     curspat = oldspat;
  1199.     lastspat = oldlspat;
  1200.     if (savestack->ary_fill > oldsave)    /* let them use local() */
  1201.     restorelist(oldsave);
  1202.  
  1203.     if (optype != O_EVAL) {
  1204.     if (retval) {
  1205.         if (optype == O_REQUIRE)
  1206.         fatal("%s", str_get(stab_val(stabent("@",TRUE))));
  1207.     }
  1208.     else {
  1209.         curcmd = oldcurcmd;
  1210.         if (gimme == G_SCALAR ? str_true(st[sp]) : sp > arglast[0]) {
  1211.         (void)hstore(stab_hash(incstab), specfilename,
  1212.           strlen(specfilename), str_smake(stab_val(curcmd->c_filestab)),
  1213.               0 );
  1214.         }
  1215.         else if (optype == O_REQUIRE)
  1216.         fatal("%s did not return a true value", specfilename);
  1217.     }
  1218.     }
  1219.     curcmd = oldcurcmd;
  1220.     return sp;
  1221. }
  1222.  
  1223. int
  1224. do_try(cmd,gimme,arglast)
  1225. CMD *cmd;
  1226. int gimme;
  1227. int *arglast;
  1228. {
  1229.     STR **st = stack->ary_array;
  1230.  
  1231.     CMD * VOLATILE oldcurcmd = curcmd;
  1232.     VOLATILE int oldtmps_base = tmps_base;
  1233.     VOLATILE int oldsave = savestack->ary_fill;
  1234.     SPAT * VOLATILE oldspat = curspat;
  1235.     SPAT * VOLATILE oldlspat = lastspat;
  1236.     VOLATILE int sp = arglast[0];
  1237.  
  1238.     tmps_base = tmps_max;
  1239.     str_set(stab_val(stabent("@",TRUE)),"");
  1240.     in_eval++;
  1241.     if (++loop_ptr >= loop_max) {
  1242.     loop_max += 128;
  1243.     Renew(loop_stack, loop_max, struct loop);
  1244.     }
  1245.     loop_stack[loop_ptr].loop_label = "_EVAL_";
  1246.     loop_stack[loop_ptr].loop_sp = sp;
  1247. #ifdef DEBUGGING
  1248.     if (debug & 4) {
  1249.     deb("(Pushing label #%d _EVAL_)\n", loop_ptr);
  1250.     }
  1251. #endif
  1252.     if (setjmp(loop_stack[loop_ptr].loop_env)) {
  1253.     st = stack->ary_array;
  1254.     sp = arglast[0];
  1255.     if (gimme != G_ARRAY)
  1256.         st[++sp] = &str_undef;
  1257.     }
  1258.     else {
  1259.     sp = cmd_exec(cmd,gimme,sp);
  1260.     st = stack->ary_array;
  1261. /*    for (i = arglast[0] + 1; i <= sp; i++)
  1262.         st[i] = str_mortal(st[i]);  not needed, I think */
  1263.                 /* if we don't save result, free zaps it */
  1264.     }
  1265.  
  1266.     in_eval--;
  1267. #ifdef DEBUGGING
  1268.     if (debug & 4) {
  1269.     char *tmps = loop_stack[loop_ptr].loop_label;
  1270.     deb("(Popping label #%d %s)\n",loop_ptr,
  1271.         tmps ? tmps : "" );
  1272.     }
  1273. #endif
  1274.     loop_ptr--;
  1275.     tmps_base = oldtmps_base;
  1276.     curspat = oldspat;
  1277.     lastspat = oldlspat;
  1278.     curcmd = oldcurcmd;
  1279.     if (savestack->ary_fill > oldsave)    /* let them use local() */
  1280.     restorelist(oldsave);
  1281.  
  1282.     return sp;
  1283. }
  1284.  
  1285. /* This routine handles any switches that can be given during run */
  1286.  
  1287. static char *
  1288. moreswitches(s)
  1289. char *s;
  1290. {
  1291.     int numlen;
  1292.  
  1293.     switch (*s) {
  1294.     case '0':
  1295.     nrschar = scanoct(s, 4, &numlen);
  1296.     nrs = nsavestr("\n",1);
  1297.     *nrs = nrschar;
  1298.     if (nrschar > 0377) {
  1299.         nrslen = 0;
  1300.         nrs = "";
  1301.     }
  1302.     else if (!nrschar && numlen >= 2) {
  1303.         nrslen = 2;
  1304.         nrs = "\n\n";
  1305.         nrschar = '\n';
  1306.     }
  1307.     return s + numlen;
  1308.     case 'a':
  1309.     minus_a = TRUE;
  1310.     s++;
  1311.     return s;
  1312.     case 'c':
  1313.     minus_c = TRUE;
  1314.     s++;
  1315.     return s;
  1316.     case 'd':
  1317. #ifdef TAINT
  1318.     if (euid != uid || egid != gid)
  1319.         fatal("No -d allowed in setuid scripts");
  1320. #endif
  1321.     perldb = TRUE;
  1322.     s++;
  1323.     return s;
  1324.     case 'D':
  1325. #ifdef DEBUGGING
  1326. #ifdef TAINT
  1327.     if (euid != uid || egid != gid)
  1328.         fatal("No -D allowed in setuid scripts");
  1329. #endif
  1330.     debug = atoi(s+1) | 32768;
  1331. #else
  1332.     warn("Recompile perl with -DDEBUGGING to use -D switch\n");
  1333. #endif
  1334.     /*SUPPRESS 530*/
  1335.     for (s++; isDIGIT(*s); s++) ;
  1336.     return s;
  1337.     case 'i':
  1338.     inplace = savestr(s+1);
  1339.     /*SUPPRESS 530*/
  1340.     for (s = inplace; *s && !isSPACE(*s); s++) ;
  1341.     *s = '\0';
  1342.     break;
  1343.     case 'I':
  1344. #ifdef TAINT
  1345.     if (euid != uid || egid != gid)
  1346.         fatal("No -I allowed in setuid scripts");
  1347. #endif
  1348.     if (*++s) {
  1349.         (void)apush(stab_array(incstab),str_make(s,0));
  1350.     }
  1351.     else
  1352.         fatal("No space allowed after -I");
  1353.     break;
  1354.     case 'l':
  1355.     minus_l = TRUE;
  1356.     s++;
  1357.     if (isDIGIT(*s)) {
  1358.         ors = savestr("\n");
  1359.         orslen = 1;
  1360.         *ors = scanoct(s, 3 + (*s == '0'), &numlen);
  1361.         s += numlen;
  1362.     }
  1363.     else {
  1364.         ors = nsavestr(nrs,nrslen);
  1365.         orslen = nrslen;
  1366.     }
  1367.     return s;
  1368.     case 'n':
  1369.     minus_n = TRUE;
  1370.     s++;
  1371.     return s;
  1372.     case 'p':
  1373.     minus_p = TRUE;
  1374.     s++;
  1375.     return s;
  1376.     case 'u':
  1377.     do_undump = TRUE;
  1378.     s++;
  1379.     return s;
  1380.     case 'U':
  1381.     unsafe = TRUE;
  1382.     s++;
  1383.     return s;
  1384.     case 'v':
  1385.     fputs("\nThis is perl, version 4.0\n\n",stdout);
  1386.     fputs(rcsid,stdout);
  1387.     fputs("\nCopyright (c) 1989, 1990, 1991, Larry Wall\n",stdout);
  1388. #ifdef MSDOS
  1389.     fputs("MS-DOS port Copyright (c) 1989, 1990, Diomidis Spinellis\n",
  1390.     stdout);
  1391. #endif
  1392. #ifdef OS2
  1393.         fputs("OS/2 port Copyright (c) 1990-1992, Raymond Chen, Kai Uwe Rommel\n",
  1394.         stdout);
  1395. #endif
  1396. #ifdef atarist
  1397.         fputs("atariST series port, ++jrb  bammi@cadence.com\n", stdout);
  1398. #endif
  1399.     fputs("\n\
  1400. Perl may be copied only under the terms of either the Artistic License or the\n\
  1401. GNU General Public License, which may be found in the Perl 4.0 source kit.\n",stdout);
  1402. #ifdef DOSISH
  1403.         usage(origargv[0]);
  1404. #endif
  1405.     exit(0);
  1406.     case 'w':
  1407.     dowarn = TRUE;
  1408.     s++;
  1409.     return s;
  1410.     case ' ':
  1411.     case '\n':
  1412.     case '\t':
  1413.     break;
  1414.     default:
  1415.     fatal("Switch meaningless after -x: -%s",s);
  1416.     }
  1417.     return Nullch;
  1418. }
  1419.  
  1420. /* compliments of Tom Christiansen */
  1421.  
  1422. /* unexec() can be found in the Gnu emacs distribution */
  1423.  
  1424. void
  1425. my_unexec()
  1426. {
  1427. #ifdef UNEXEC
  1428.     int    status;
  1429.     extern int etext;
  1430.     static char dumpname[BUFSIZ];
  1431.     static char perlpath[256];
  1432.  
  1433.     sprintf (dumpname, "%s.perldump", origfilename);
  1434.     sprintf (perlpath, "%s/perl", BIN);
  1435.  
  1436.     status = unexec(dumpname, perlpath, &etext, sbrk(0), 0);
  1437.     if (status)
  1438.     fprintf(stderr, "unexec of %s into %s failed!\n", perlpath, dumpname);
  1439.     exit(status);
  1440. #else
  1441. #ifdef OS2
  1442.     int file = open("perldump", O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0666);
  1443.     _core(file);
  1444.     close(file);
  1445.     fprintf(stderr, "Core dumped to file \"perldump\".\n");
  1446.     exit(0);
  1447. #else
  1448. #ifdef DOSISH
  1449.     abort();    /* nothing else to do */
  1450. #else /* ! MSDOS */
  1451. #   ifndef SIGABRT
  1452. #    define SIGABRT SIGILL
  1453. #   endif
  1454. #   ifndef SIGILL
  1455. #    define SIGILL 6        /* blech */
  1456. #   endif
  1457.     kill(getpid(),SIGABRT);    /* for use with undump */
  1458. #endif /* ! MSDOS */
  1459. #endif
  1460. #endif
  1461. }
  1462.  
  1463.