Added dowhich() and doprintf() builtins.
authorChristos Zoulas <christos@ucbvax.Berkeley.EDU>
Mon, 29 Jul 1991 05:14:23 +0000 (21:14 -0800)
committerChristos Zoulas <christos@ucbvax.Berkeley.EDU>
Mon, 29 Jul 1991 05:14:23 +0000 (21:14 -0800)
SCCS-vsn: bin/csh/exec.c 5.19
SCCS-vsn: bin/csh/extern.h 5.6
SCCS-vsn: bin/csh/func.c 5.22
SCCS-vsn: bin/csh/init.c 5.14

usr/src/bin/csh/exec.c
usr/src/bin/csh/extern.h
usr/src/bin/csh/func.c
usr/src/bin/csh/init.c

index 327ef63..92b8af0 100644 (file)
@@ -6,12 +6,14 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)exec.c     5.18 (Berkeley) %G%";
+static char sccsid[] = "@(#)exec.c     5.19 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/types.h>
 #endif /* not lint */
 
 #include <sys/types.h>
+#include <sys/param.h>
 #include <dirent.h>
 #include <fcntl.h>
 #include <dirent.h>
 #include <fcntl.h>
+#include <sys/stat.h>
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
@@ -72,6 +74,10 @@ static Char *justabs[] = {STRNULL, 0};
 static void    pexerr __P((void));
 static void    texec __P((Char *, Char **));
 static int     hashname __P((Char *));
 static void    pexerr __P((void));
 static void    texec __P((Char *, Char **));
 static int     hashname __P((Char *));
+static void    tellmewhat __P((struct wordent *));
+static int     executable __P((Char *, Char *, bool));
+static int     iscommand __P((Char *));
+
 
 void
 /*ARGSUSED*/
 
 void
 /*ARGSUSED*/
@@ -425,3 +431,227 @@ hashname(cp)
        h = hash(h, *cp++);
     return ((int) h);
 }
        h = hash(h, *cp++);
     return ((int) h);
 }
+
+static int
+iscommand(name)
+    Char   *name;
+{
+    register Char **pv;
+    register Char *sav;
+    register struct varent *v;
+    register bool slash = any(short2str(name), '/');
+    register int hashval = 0, hashval1, i;
+
+    v = adrof(STRpath);
+    if (v == 0 || v->vec[0] == 0 || slash)
+       pv = justabs;
+    else
+       pv = v->vec;
+    sav = Strspl(STRslash, name);      /* / command name for postpending */
+    if (havhash)
+       hashval = hashname(name);
+    i = 0;
+    do {
+       if (!slash && pv[0][0] == '/' && havhash) {
+           hashval1 = hash(hashval, i);
+           if (!bit(xhash, hashval1))
+               goto cont;
+       }
+       if (pv[0][0] == 0 || eq(pv[0], STRdot)) {       /* don't make ./xxx */
+           if (executable(NULL, name, 0)) {
+               xfree((ptr_t) sav);
+               return i + 1;
+           }
+       }
+       else {
+           if (executable(*pv, sav, 0)) {
+               xfree((ptr_t) sav);
+               return i + 1;
+           }
+       }
+cont:
+       pv++;
+       i++;
+    } while (*pv);
+    xfree((ptr_t) sav);
+    return 0;
+}
+
+/* Also by:
+ *  Andreas Luik <luik@isaak.isa.de>
+ *  I S A  GmbH - Informationssysteme fuer computerintegrierte Automatisierung
+ *  Azenberstr. 35
+ *  D-7000 Stuttgart 1
+ *  West-Germany
+ * is the executable() routine below and changes to iscommand().
+ * Thanks again!!
+ */
+
+/*
+ * executable() examines the pathname obtained by concatenating dir and name
+ * (dir may be NULL), and returns 1 either if it is executable by us, or
+ * if dir_ok is set and the pathname refers to a directory.
+ * This is a bit kludgy, but in the name of optimization...
+ */
+static int
+executable(dir, name, dir_ok)
+    Char   *dir, *name;
+    bool    dir_ok;
+{
+    struct stat stbuf;
+    Char    path[MAXPATHLEN + 1], *dp, *sp;
+    char   *strname;
+
+    if (dir && *dir) {
+       for (dp = path, sp = dir; *sp; *dp++ = *sp++)
+           if (dp == &path[MAXPATHLEN + 1]) {
+               *--dp = '\0';
+               break;
+           }
+       for (sp = name; *sp; *dp++ = *sp++)
+           if (dp == &path[MAXPATHLEN + 1]) {
+               *--dp = '\0';
+               break;
+           }
+       *dp = '\0';
+       strname = short2str(path);
+    }
+    else
+       strname = short2str(name);
+    return (stat(strname, &stbuf) != -1 &&
+           ((S_ISREG(stbuf.st_mode) &&
+    /* save time by not calling access() in the hopeless case */
+             (stbuf.st_mode & (S_IXOTH | S_IXGRP | S_IXUSR)) &&
+             access(strname, X_OK) == 0) ||
+            (dir_ok && S_ISDIR(stbuf.st_mode))));
+}
+
+/* The dowhich() is by:
+ *  Andreas Luik <luik@isaak.isa.de>
+ *  I S A  GmbH - Informationssysteme fuer computerintegrierte Automatisierung
+ *  Azenberstr. 35
+ *  D-7000 Stuttgart 1
+ *  West-Germany
+ * Thanks!!
+ */
+/*ARGSUSED*/
+void
+dowhich(v, c)
+    register Char **v;
+    struct command *c;
+{
+    struct wordent lex[3];
+    struct varent *vp;
+
+    lex[0].next = &lex[1];
+    lex[1].next = &lex[2];
+    lex[2].next = &lex[0];
+
+    lex[0].prev = &lex[2];
+    lex[1].prev = &lex[0];
+    lex[2].prev = &lex[1];
+
+    lex[0].word = STRNULL;
+    lex[2].word = STRret;
+
+    while (*++v) {
+       if (vp = adrof1(*v, &aliases)) {
+           (void) fprintf(cshout, "%s: \t aliased to ", short2str(*v));
+           blkpr(cshout, vp->vec);
+           (void) fputc('\n', cshout);
+       }
+       else {
+           lex[1].word = *v;
+           tellmewhat(lex);
+       }
+    }
+}
+
+static void
+tellmewhat(lex)
+    struct wordent *lex;
+{
+    register int i;
+    register struct biltins *bptr;
+    register struct wordent *sp = lex->next;
+    bool    aliased = 0;
+    Char   *s0, *s1, *s2;
+    Char    qc;
+
+    if (adrof1(sp->word, &aliases)) {
+       alias(lex);
+       sp = lex->next;
+       aliased = 1;
+    }
+
+    s0 = sp->word;             /* to get the memory freeing right... */
+
+    /* handle quoted alias hack */
+    if ((*(sp->word) & (QUOTE | TRIM)) == QUOTE)
+       (sp->word)++;
+
+    /* do quoting, if it hasn't been done */
+    s1 = s2 = sp->word;
+    while (*s2)
+       switch (*s2) {
+       case '\'':
+       case '"':
+           qc = *s2++;
+           while (*s2 && *s2 != qc)
+               *s1++ = *s2++ | QUOTE;
+           if (*s2)
+               s2++;
+           break;
+       case '\\':
+           if (*++s2)
+               *s1++ = *s2++ | QUOTE;
+           break;
+       default:
+           *s1++ = *s2++;
+       }
+    *s1 = '\0';
+
+    for (bptr = bfunc; bptr < &bfunc[nbfunc]; bptr++) {
+       if (eq(sp->word, str2short(bptr->bname))) {
+           if (aliased)
+               prlex(cshout, lex);
+           (void) fprintf(cshout, "%s: shell built-in command.\n", 
+                          short2str(sp->word));
+           sp->word = s0;      /* we save and then restore this */
+           return;
+       }
+    }
+
+    if (i = iscommand(strip(sp->word))) {
+       register Char **pv;
+       register struct varent *v;
+       bool    slash = any(short2str(sp->word), '/');
+
+       v = adrof(STRpath);
+       if (v == 0 || v->vec[0] == 0 || slash)
+           pv = justabs;
+       else
+           pv = v->vec;
+
+       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);
+           sp->word = s0;      /* we save and then restore this */
+           return;
+       }
+       s1 = Strspl(*pv, STRslash);
+       sp->word = Strspl(s1, sp->word);
+       xfree((ptr_t) s1);
+       prlex(cshout, lex);
+       xfree((ptr_t) sp->word);
+    }
+    else {
+       if (aliased)
+           prlex(cshout, lex);
+       (void) fprintf(csherr, "%s: Command not found.\n", short2str(sp->word));
+    }
+    sp->word = s0;             /* we save and then restore this */
+}
index 69b7156..a5e2fdf 100644 (file)
@@ -4,7 +4,7 @@
  *
  * %sccs.include.redist.c%
  *
  *
  * %sccs.include.redist.c%
  *
- *     @(#)extern.h    5.5 (Berkeley) %G%
+ *     @(#)extern.h    5.6 (Berkeley) %G%
  */
 
 #include <sys/cdefs.h>
  */
 
 #include <sys/cdefs.h>
@@ -65,6 +65,7 @@ void  stderror __P((int, ...));
 void   doexec __P((Char **, struct command *));
 void   dohash __P((Char **, struct command *));
 void   dounhash __P((Char **, struct command *));
 void   doexec __P((Char **, struct command *));
 void   dohash __P((Char **, struct command *));
 void   dounhash __P((Char **, struct command *));
+void   dowhich __P((Char **, struct command *));
 void   execash __P((Char **, struct command *));
 void   hashstat __P((Char **, struct command *));
 void   xechoit __P((Char **));
 void   execash __P((Char **, struct command *));
 void   hashstat __P((Char **, struct command *));
 void   xechoit __P((Char **));
@@ -103,6 +104,7 @@ void        dologin __P((Char **, struct command *));
 void   dologout __P((Char **, struct command *));
 void   donohup __P((Char **, struct command *));
 void   doonintr __P((Char **, struct command *));
 void   dologout __P((Char **, struct command *));
 void   donohup __P((Char **, struct command *));
 void   doonintr __P((Char **, struct command *));
+void   doprintf __P((Char **, struct command *));
 void   dorepeat __P((Char **, struct command *));
 void   dosetenv __P((Char **, struct command *));
 void   dosuspend __P((Char **, struct command *));
 void   dorepeat __P((Char **, struct command *));
 void   dosetenv __P((Char **, struct command *));
 void   dosuspend __P((Char **, struct command *));
index 647325c..400b02d 100644 (file)
@@ -6,7 +6,7 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)func.c     5.21 (Berkeley) %G%";
+static char sccsid[] = "@(#)func.c     5.22 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/types.h>
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -1408,3 +1408,21 @@ doeval(v, t)
     if (my_reenter)
        stderror(ERR_SILENT);
 }
     if (my_reenter)
        stderror(ERR_SILENT);
 }
+
+void
+/*ARGSUSED*/
+doprintf(v, t)
+    Char **v;
+    struct command *t;
+{
+    char **c;
+    extern int progprintf __P((int, char **));
+    int ret;
+    
+    ret = progprintf(blklen(v), c = short2blk(v));
+
+    blkfree((Char **) c);
+    if (ret)
+       stderror(ERR_SILENT);
+}
+
index 81c205d..369f98e 100644 (file)
@@ -6,7 +6,7 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)init.c     5.13 (Berkeley) %G%";
+static char sccsid[] = "@(#)init.c     5.14 (Berkeley) %G%";
 #endif /* not lint */
 
 #if __STDC__
 #endif /* not lint */
 
 #if __STDC__
@@ -60,6 +60,7 @@ struct biltins bfunc[] =
     "notify",  donotify,       0, INF,
     "onintr",  doonintr,       0, 2,
     "popd",    dopopd,         0, INF,
     "notify",  donotify,       0, INF,
     "onintr",  doonintr,       0, 2,
     "popd",    dopopd,         0, INF,
+    "printf",  doprintf,       1, INF,
     "pushd",   dopushd,        0, INF,
     "rehash",  dohash,         0, 0,
     "repeat",  dorepeat,       2, INF,
     "pushd",   dopushd,        0, INF,
     "rehash",  dohash,         0, 0,
     "repeat",  dorepeat,       2, INF,
@@ -78,6 +79,7 @@ struct biltins bfunc[] =
     "unset",   unset,          1, INF,
     "unsetenv", dounsetenv,    1, INF,
     "wait",    dowait,         0, 0,
     "unset",   unset,          1, INF,
     "unsetenv", dounsetenv,    1, INF,
     "wait",    dowait,         0, 0,
+    "which",   dowhich,        1, INF,
     "while",   dowhile,        1, INF,
 };
 int     nbfunc = sizeof bfunc / sizeof *bfunc;
     "while",   dowhile,        1, INF,
 };
 int     nbfunc = sizeof bfunc / sizeof *bfunc;