BSD 4_4_Lite2 release
[unix-history] / usr / src / bin / csh / exec.c
index 92b8af0..49c04ae 100644 (file)
@@ -1,12 +1,38 @@
 /*-
 /*-
- * Copyright (c) 1980, 1991 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1980, 1991, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  *
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)exec.c     5.19 (Berkeley) %G%";
+static char sccsid[] = "@(#)exec.c     8.3 (Berkeley) 5/23/95";
 #endif /* not lint */
 
 #include <sys/types.h>
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -63,7 +89,7 @@ static Char *expath;          /* Path for exerr */
 #define HSHMUL         243
 static char xhash[HSHSIZ / 8];
 
 #define HSHMUL         243
 static char xhash[HSHSIZ / 8];
 
-#define hash(a, b)     ((a) * HSHMUL + (b) & HSHMASK)
+#define hash(a, b)     (((a) * HSHMUL + (b)) & HSHMASK)
 #define bit(h, b)      ((h)[(b) >> 3] & 1 << ((b) & 7))        /* bit test */
 #define bis(h, b)      ((h)[(b) >> 3] |= 1 << ((b) & 7))       /* bit set */
 static int hits, misses;
 #define bit(h, b)      ((h)[(b) >> 3] & 1 << ((b) & 7))        /* bit test */
 #define bis(h, b)      ((h)[(b) >> 3] |= 1 << ((b) & 7))       /* bit set */
 static int hits, misses;
@@ -90,6 +116,7 @@ doexec(v, t)
     register bool slash;
     register int hashval = 0, hashval1, i;
     Char   *blk[2];
     register bool slash;
     register int hashval = 0, hashval1, i;
     Char   *blk[2];
+    sigset_t sigset;
 
     /*
      * Glob the command name. We will search $path even if this does something,
 
     /*
      * Glob the command name. We will search $path even if this does something,
@@ -102,7 +129,7 @@ doexec(v, t)
     if (gflag) {
        pv = globall(blk);
        if (pv == 0) {
     if (gflag) {
        pv = globall(blk);
        if (pv == 0) {
-           setname(short2str(blk[0]));
+           setname(vis_str(blk[0]));
            stderror(ERR_NAME | ERR_NOMATCH);
        }
        gargv = 0;
            stderror(ERR_NAME | ERR_NOMATCH);
        }
        gargv = 0;
@@ -133,7 +160,7 @@ doexec(v, t)
        av = globall(av);
        if (av == 0) {
            blkfree(pv);
        av = globall(av);
        if (av == 0) {
            blkfree(pv);
-           setname(short2str(expath));
+           setname(vis_str(expath));
            stderror(ERR_NAME | ERR_NOMATCH);
        }
        gargv = 0;
            stderror(ERR_NAME | ERR_NOMATCH);
        }
        gargv = 0;
@@ -165,7 +192,8 @@ doexec(v, t)
      * We must do this AFTER any possible forking (like `foo` in glob) so that
      * this shell can still do subprocesses.
      */
      * We must do this AFTER any possible forking (like `foo` in glob) so that
      * this shell can still do subprocesses.
      */
-    (void) sigsetmask((sigset_t) 0);
+    sigemptyset(&sigset);
+    sigprocmask(SIG_SETMASK, &sigset, NULL);
     /*
      * If no path, no words in path, or a / in the filename then restrict the
      * command search.
     /*
      * If no path, no words in path, or a / in the filename then restrict the
      * command search.
@@ -217,7 +245,7 @@ pexerr()
 {
     /* Couldn't find the damn thing */
     if (expath) {
 {
     /* Couldn't find the damn thing */
     if (expath) {
-       setname(short2str(expath));
+       setname(vis_str(expath));
        Vexpath = 0;
        xfree((ptr_t) expath);
        expath = 0;
        Vexpath = 0;
        xfree((ptr_t) expath);
        expath = 0;
@@ -331,20 +359,73 @@ texec(sf, st)
 
 /*ARGSUSED*/
 void
 
 /*ARGSUSED*/
 void
-execash(v, t)
-    Char  **v;
-    register struct command *t;
+execash(t, kp)
+    Char  **t;
+    register struct command *kp;
 {
 {
+    int     saveIN, saveOUT, saveDIAG, saveSTD;
+    int     oSHIN;
+    int     oSHOUT;
+    int     oSHERR;
+    int     oOLDSTD;
+    jmp_buf osetexit;
+    int            my_reenter;
+    int     odidfds;
+    sig_t   osigint, osigquit, osigterm;
+
     if (chkstop == 0 && setintr)
        panystop(0);
     if (chkstop == 0 && setintr)
        panystop(0);
+    /*
+     * Hmm, we don't really want to do that now because we might
+     * fail, but what is the choice
+     */
     rechist();
     rechist();
-    (void) signal(SIGINT, parintr);
-    (void) signal(SIGQUIT, parintr);
-    (void) signal(SIGTERM, parterm);   /* if doexec loses, screw */
-    lshift(t->t_dcom, 1);
-    exiterr = 1;
-    doexec(NULL, t);
-    /* NOTREACHED */
+
+    osigint  = signal(SIGINT, parintr);
+    osigquit = signal(SIGQUIT, parintr);
+    osigterm = signal(SIGTERM, parterm);
+
+    odidfds = didfds;
+    oSHIN = SHIN;
+    oSHOUT = SHOUT;
+    oSHERR = SHERR;
+    oOLDSTD = OLDSTD;
+
+    saveIN = dcopy(SHIN, -1);
+    saveOUT = dcopy(SHOUT, -1);
+    saveDIAG = dcopy(SHERR, -1);
+    saveSTD = dcopy(OLDSTD, -1);
+
+    lshift(kp->t_dcom, 1);
+
+    getexit(osetexit);
+
+    if ((my_reenter = setexit()) == 0) {
+       SHIN = dcopy(0, -1);
+       SHOUT = dcopy(1, -1);
+       SHERR = dcopy(2, -1);
+       didfds = 0;
+       doexec(t, kp);
+    }
+
+    (void) signal(SIGINT, osigint);
+    (void) signal(SIGQUIT, osigquit);
+    (void) signal(SIGTERM, osigterm);
+
+    doneinp = 0;
+    didfds = odidfds;
+    (void) close(SHIN);
+    (void) close(SHOUT);
+    (void) close(SHERR);
+    (void) close(OLDSTD);
+    SHIN = dmove(saveIN, oSHIN);
+    SHOUT = dmove(saveOUT, oSHOUT);
+    SHERR = dmove(saveDIAG, oSHERR);
+    OLDSTD = dmove(saveSTD, oOLDSTD);
+
+    resexit(osetexit);
+    if (my_reenter)
+       stderror(ERR_SILENT);
 }
 
 void
 }
 
 void
@@ -388,7 +469,7 @@ dohash(v, t)
                continue;
            if (dp->d_name[0] == '.' &&
                (dp->d_name[1] == '\0' ||
                continue;
            if (dp->d_name[0] == '.' &&
                (dp->d_name[1] == '\0' ||
-                dp->d_name[1] == '.' && dp->d_name[2] == '\0'))
+                (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
                continue;
            hashval = hash(hashname(str2short(dp->d_name)), i);
            bis(xhash, hashval);
                continue;
            hashval = hash(hashname(str2short(dp->d_name)), i);
            bis(xhash, hashval);
@@ -555,8 +636,8 @@ dowhich(v, c)
     lex[2].word = STRret;
 
     while (*++v) {
     lex[2].word = STRret;
 
     while (*++v) {
-       if (vp = adrof1(*v, &aliases)) {
-           (void) fprintf(cshout, "%s: \t aliased to ", short2str(*v));
+       if ((vp = adrof1(*v, &aliases)) != NULL) {
+           (void) fprintf(cshout, "%s: \t aliased to ", vis_str(*v));
            blkpr(cshout, vp->vec);
            (void) fputc('\n', cshout);
        }
            blkpr(cshout, vp->vec);
            (void) fputc('\n', cshout);
        }
@@ -575,7 +656,7 @@ tellmewhat(lex)
     register struct biltins *bptr;
     register struct wordent *sp = lex->next;
     bool    aliased = 0;
     register struct biltins *bptr;
     register struct wordent *sp = lex->next;
     bool    aliased = 0;
-    Char   *s0, *s1, *s2;
+    Char   *s0, *s1, *s2, *cmd;
     Char    qc;
 
     if (adrof1(sp->word, &aliases)) {
     Char    qc;
 
     if (adrof1(sp->word, &aliases)) {
@@ -616,13 +697,15 @@ tellmewhat(lex)
            if (aliased)
                prlex(cshout, lex);
            (void) fprintf(cshout, "%s: shell built-in command.\n", 
            if (aliased)
                prlex(cshout, lex);
            (void) fprintf(cshout, "%s: shell built-in command.\n", 
-                          short2str(sp->word));
+                          vis_str(sp->word));
            sp->word = s0;      /* we save and then restore this */
            return;
        }
     }
 
            sp->word = s0;      /* we save and then restore this */
            return;
        }
     }
 
-    if (i = iscommand(strip(sp->word))) {
+    sp->word = cmd = globone(sp->word, G_IGNORE);
+
+    if ((i = iscommand(strip(sp->word))) != 0) {
        register Char **pv;
        register struct varent *v;
        bool    slash = any(short2str(sp->word), '/');
        register Char **pv;
        register struct varent *v;
        bool    slash = any(short2str(sp->word), '/');
@@ -636,10 +719,15 @@ tellmewhat(lex)
        while (--i)
            pv++;
        if (pv[0][0] == 0 || eq(pv[0], STRdot)) {
        while (--i)
            pv++;
        if (pv[0][0] == 0 || eq(pv[0], STRdot)) {
-           sp->word = Strspl(STRdotsl, sp->word);
-           prlex(cshout, lex);
-           xfree((ptr_t) sp->word);
+           if (!slash) {
+               sp->word = Strspl(STRdotsl, sp->word);
+               prlex(cshout, lex);
+               xfree((ptr_t) sp->word);
+           }
+           else
+               prlex(cshout, lex);
            sp->word = s0;      /* we save and then restore this */
            sp->word = s0;      /* we save and then restore this */
+           xfree((ptr_t) cmd);
            return;
        }
        s1 = Strspl(*pv, STRslash);
            return;
        }
        s1 = Strspl(*pv, STRslash);
@@ -651,7 +739,8 @@ tellmewhat(lex)
     else {
        if (aliased)
            prlex(cshout, lex);
     else {
        if (aliased)
            prlex(cshout, lex);
-       (void) fprintf(csherr, "%s: Command not found.\n", short2str(sp->word));
+       (void) fprintf(csherr, "%s: Command not found.\n", vis_str(sp->word));
     }
     sp->word = s0;             /* we save and then restore this */
     }
     sp->word = s0;             /* we save and then restore this */
+    xfree((ptr_t) cmd);
 }
 }