Home | History | Annotate | Line # | Download | only in csh
exec.c revision 1.9
      1  1.9  christos /*	$NetBSD: exec.c,v 1.9 1996/09/30 20:03:54 christos Exp $	*/
      2  1.6       cgd 
      3  1.1       cgd /*-
      4  1.5   mycroft  * Copyright (c) 1980, 1991, 1993
      5  1.5   mycroft  *	The Regents of the University of California.  All rights reserved.
      6  1.1       cgd  *
      7  1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8  1.1       cgd  * modification, are permitted provided that the following conditions
      9  1.1       cgd  * are met:
     10  1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11  1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12  1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14  1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.1       cgd  *    must display the following acknowledgement:
     17  1.1       cgd  *	This product includes software developed by the University of
     18  1.1       cgd  *	California, Berkeley and its contributors.
     19  1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20  1.1       cgd  *    may be used to endorse or promote products derived from this software
     21  1.1       cgd  *    without specific prior written permission.
     22  1.1       cgd  *
     23  1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1       cgd  * SUCH DAMAGE.
     34  1.1       cgd  */
     35  1.1       cgd 
     36  1.1       cgd #ifndef lint
     37  1.6       cgd #if 0
     38  1.8  christos static char sccsid[] = "@(#)exec.c	8.3 (Berkeley) 5/23/95";
     39  1.6       cgd #else
     40  1.9  christos static char rcsid[] = "$NetBSD: exec.c,v 1.9 1996/09/30 20:03:54 christos Exp $";
     41  1.6       cgd #endif
     42  1.1       cgd #endif /* not lint */
     43  1.1       cgd 
     44  1.1       cgd #include <sys/types.h>
     45  1.5   mycroft #include <sys/param.h>
     46  1.1       cgd #include <dirent.h>
     47  1.1       cgd #include <fcntl.h>
     48  1.5   mycroft #include <sys/stat.h>
     49  1.1       cgd #include <errno.h>
     50  1.1       cgd #include <stdlib.h>
     51  1.1       cgd #include <string.h>
     52  1.1       cgd #include <unistd.h>
     53  1.1       cgd #if __STDC__
     54  1.1       cgd # include <stdarg.h>
     55  1.1       cgd #else
     56  1.1       cgd # include <varargs.h>
     57  1.1       cgd #endif
     58  1.1       cgd 
     59  1.1       cgd #include "csh.h"
     60  1.1       cgd #include "extern.h"
     61  1.1       cgd 
     62  1.1       cgd /*
     63  1.1       cgd  * System level search and execute of a command.  We look in each directory
     64  1.1       cgd  * for the specified command name.  If the name contains a '/' then we
     65  1.1       cgd  * execute only the full path name.  If there is no search path then we
     66  1.1       cgd  * execute only full path names.
     67  1.1       cgd  */
     68  1.1       cgd extern char **environ;
     69  1.1       cgd 
     70  1.1       cgd /*
     71  1.1       cgd  * As we search for the command we note the first non-trivial error
     72  1.1       cgd  * message for presentation to the user.  This allows us often
     73  1.1       cgd  * to show that a file has the wrong mode/no access when the file
     74  1.1       cgd  * is not in the last component of the search path, so we must
     75  1.1       cgd  * go on after first detecting the error.
     76  1.1       cgd  */
     77  1.1       cgd static char *exerr;		/* Execution error message */
     78  1.1       cgd static Char *expath;		/* Path for exerr */
     79  1.1       cgd 
     80  1.1       cgd /*
     81  1.1       cgd  * Xhash is an array of HSHSIZ bits (HSHSIZ / 8 chars), which are used
     82  1.1       cgd  * to hash execs.  If it is allocated (havhash true), then to tell
     83  1.1       cgd  * whether ``name'' is (possibly) present in the i'th component
     84  1.1       cgd  * of the variable path, you look at the bit in xhash indexed by
     85  1.1       cgd  * hash(hashname("name"), i).  This is setup automatically
     86  1.1       cgd  * after .login is executed, and recomputed whenever ``path'' is
     87  1.1       cgd  * changed.
     88  1.1       cgd  * The two part hash function is designed to let texec() call the
     89  1.1       cgd  * more expensive hashname() only once and the simple hash() several
     90  1.1       cgd  * times (once for each path component checked).
     91  1.1       cgd  * Byte size is assumed to be 8.
     92  1.1       cgd  */
     93  1.1       cgd #define	HSHSIZ		8192	/* 1k bytes */
     94  1.1       cgd #define HSHMASK		(HSHSIZ - 1)
     95  1.1       cgd #define HSHMUL		243
     96  1.1       cgd static char xhash[HSHSIZ / 8];
     97  1.1       cgd 
     98  1.5   mycroft #define hash(a, b)	(((a) * HSHMUL + (b)) & HSHMASK)
     99  1.1       cgd #define bit(h, b)	((h)[(b) >> 3] & 1 << ((b) & 7))	/* bit test */
    100  1.1       cgd #define bis(h, b)	((h)[(b) >> 3] |= 1 << ((b) & 7))	/* bit set */
    101  1.1       cgd static int hits, misses;
    102  1.1       cgd 
    103  1.1       cgd /* Dummy search path for just absolute search when no path */
    104  1.1       cgd static Char *justabs[] = {STRNULL, 0};
    105  1.1       cgd 
    106  1.1       cgd static void	pexerr __P((void));
    107  1.1       cgd static void	texec __P((Char *, Char **));
    108  1.1       cgd static int	hashname __P((Char *));
    109  1.9  christos static int 	tellmewhat __P((struct wordent *, Char *));
    110  1.5   mycroft static int	executable __P((Char *, Char *, bool));
    111  1.5   mycroft static int	iscommand __P((Char *));
    112  1.5   mycroft 
    113  1.1       cgd 
    114  1.1       cgd void
    115  1.5   mycroft /*ARGSUSED*/
    116  1.5   mycroft doexec(v, t)
    117  1.5   mycroft     Char **v;
    118  1.5   mycroft     struct command *t;
    119  1.1       cgd {
    120  1.1       cgd     register Char *dp, **pv, **av, *sav;
    121  1.5   mycroft     register struct varent *pathv;
    122  1.1       cgd     register bool slash;
    123  1.1       cgd     register int hashval = 0, hashval1, i;
    124  1.1       cgd     Char   *blk[2];
    125  1.7   mycroft     sigset_t sigset;
    126  1.1       cgd 
    127  1.1       cgd     /*
    128  1.1       cgd      * Glob the command name. We will search $path even if this does something,
    129  1.1       cgd      * as in sh but not in csh.  One special case: if there is no PATH, then we
    130  1.1       cgd      * execute only commands which start with '/'.
    131  1.1       cgd      */
    132  1.1       cgd     blk[0] = t->t_dcom[0];
    133  1.1       cgd     blk[1] = 0;
    134  1.1       cgd     gflag = 0, tglob(blk);
    135  1.1       cgd     if (gflag) {
    136  1.1       cgd 	pv = globall(blk);
    137  1.1       cgd 	if (pv == 0) {
    138  1.5   mycroft 	    setname(vis_str(blk[0]));
    139  1.1       cgd 	    stderror(ERR_NAME | ERR_NOMATCH);
    140  1.1       cgd 	}
    141  1.1       cgd 	gargv = 0;
    142  1.1       cgd     }
    143  1.1       cgd     else
    144  1.1       cgd 	pv = saveblk(blk);
    145  1.1       cgd 
    146  1.1       cgd     trim(pv);
    147  1.1       cgd 
    148  1.1       cgd     exerr = 0;
    149  1.1       cgd     expath = Strsave(pv[0]);
    150  1.1       cgd     Vexpath = expath;
    151  1.1       cgd 
    152  1.5   mycroft     pathv = adrof(STRpath);
    153  1.5   mycroft     if (pathv == 0 && expath[0] != '/') {
    154  1.1       cgd 	blkfree(pv);
    155  1.1       cgd 	pexerr();
    156  1.1       cgd     }
    157  1.1       cgd     slash = any(short2str(expath), '/');
    158  1.1       cgd 
    159  1.1       cgd     /*
    160  1.1       cgd      * Glob the argument list, if necessary. Otherwise trim off the quote bits.
    161  1.1       cgd      */
    162  1.1       cgd     gflag = 0;
    163  1.1       cgd     av = &t->t_dcom[1];
    164  1.1       cgd     tglob(av);
    165  1.1       cgd     if (gflag) {
    166  1.1       cgd 	av = globall(av);
    167  1.1       cgd 	if (av == 0) {
    168  1.1       cgd 	    blkfree(pv);
    169  1.5   mycroft 	    setname(vis_str(expath));
    170  1.1       cgd 	    stderror(ERR_NAME | ERR_NOMATCH);
    171  1.1       cgd 	}
    172  1.1       cgd 	gargv = 0;
    173  1.1       cgd     }
    174  1.1       cgd     else
    175  1.1       cgd 	av = saveblk(av);
    176  1.1       cgd 
    177  1.1       cgd     blkfree(t->t_dcom);
    178  1.1       cgd     t->t_dcom = blkspl(pv, av);
    179  1.1       cgd     xfree((ptr_t) pv);
    180  1.1       cgd     xfree((ptr_t) av);
    181  1.1       cgd     av = t->t_dcom;
    182  1.1       cgd     trim(av);
    183  1.1       cgd 
    184  1.1       cgd     if (*av == NULL || **av == '\0')
    185  1.1       cgd 	pexerr();
    186  1.1       cgd 
    187  1.1       cgd     xechoit(av);		/* Echo command if -x */
    188  1.1       cgd     /*
    189  1.1       cgd      * Since all internal file descriptors are set to close on exec, we don't
    190  1.1       cgd      * need to close them explicitly here.  Just reorient ourselves for error
    191  1.1       cgd      * messages.
    192  1.1       cgd      */
    193  1.1       cgd     SHIN = 0;
    194  1.1       cgd     SHOUT = 1;
    195  1.5   mycroft     SHERR = 2;
    196  1.1       cgd     OLDSTD = 0;
    197  1.1       cgd     /*
    198  1.1       cgd      * We must do this AFTER any possible forking (like `foo` in glob) so that
    199  1.1       cgd      * this shell can still do subprocesses.
    200  1.1       cgd      */
    201  1.7   mycroft     sigemptyset(&sigset);
    202  1.7   mycroft     sigprocmask(SIG_SETMASK, &sigset, NULL);
    203  1.1       cgd     /*
    204  1.1       cgd      * If no path, no words in path, or a / in the filename then restrict the
    205  1.1       cgd      * command search.
    206  1.1       cgd      */
    207  1.5   mycroft     if (pathv == 0 || pathv->vec[0] == 0 || slash)
    208  1.1       cgd 	pv = justabs;
    209  1.1       cgd     else
    210  1.5   mycroft 	pv = pathv->vec;
    211  1.1       cgd     sav = Strspl(STRslash, *av);/* / command name for postpending */
    212  1.1       cgd     Vsav = sav;
    213  1.1       cgd     if (havhash)
    214  1.1       cgd 	hashval = hashname(*av);
    215  1.1       cgd     i = 0;
    216  1.1       cgd     hits++;
    217  1.1       cgd     do {
    218  1.1       cgd 	/*
    219  1.1       cgd 	 * Try to save time by looking at the hash table for where this command
    220  1.1       cgd 	 * could be.  If we are doing delayed hashing, then we put the names in
    221  1.1       cgd 	 * one at a time, as the user enters them.  This is kinda like Korn
    222  1.1       cgd 	 * Shell's "tracked aliases".
    223  1.1       cgd 	 */
    224  1.1       cgd 	if (!slash && pv[0][0] == '/' && havhash) {
    225  1.1       cgd 	    hashval1 = hash(hashval, i);
    226  1.1       cgd 	    if (!bit(xhash, hashval1))
    227  1.1       cgd 		goto cont;
    228  1.1       cgd 	}
    229  1.1       cgd 	if (pv[0][0] == 0 || eq(pv[0], STRdot))	/* don't make ./xxx */
    230  1.1       cgd 	    texec(*av, av);
    231  1.1       cgd 	else {
    232  1.1       cgd 	    dp = Strspl(*pv, sav);
    233  1.1       cgd 	    Vdp = dp;
    234  1.1       cgd 	    texec(dp, av);
    235  1.1       cgd 	    Vdp = 0;
    236  1.1       cgd 	    xfree((ptr_t) dp);
    237  1.1       cgd 	}
    238  1.1       cgd 	misses++;
    239  1.1       cgd cont:
    240  1.1       cgd 	pv++;
    241  1.1       cgd 	i++;
    242  1.1       cgd     } while (*pv);
    243  1.1       cgd     hits--;
    244  1.1       cgd     Vsav = 0;
    245  1.1       cgd     xfree((ptr_t) sav);
    246  1.1       cgd     pexerr();
    247  1.1       cgd }
    248  1.1       cgd 
    249  1.1       cgd static void
    250  1.1       cgd pexerr()
    251  1.1       cgd {
    252  1.1       cgd     /* Couldn't find the damn thing */
    253  1.1       cgd     if (expath) {
    254  1.5   mycroft 	setname(vis_str(expath));
    255  1.1       cgd 	Vexpath = 0;
    256  1.1       cgd 	xfree((ptr_t) expath);
    257  1.1       cgd 	expath = 0;
    258  1.1       cgd     }
    259  1.1       cgd     else
    260  1.1       cgd 	setname("");
    261  1.1       cgd     if (exerr)
    262  1.1       cgd 	stderror(ERR_NAME | ERR_STRING, exerr);
    263  1.1       cgd     stderror(ERR_NAME | ERR_COMMAND);
    264  1.1       cgd }
    265  1.1       cgd 
    266  1.1       cgd /*
    267  1.1       cgd  * Execute command f, arg list t.
    268  1.1       cgd  * Record error message if not found.
    269  1.1       cgd  * Also do shell scripts here.
    270  1.1       cgd  */
    271  1.1       cgd static void
    272  1.1       cgd texec(sf, st)
    273  1.1       cgd     Char   *sf;
    274  1.1       cgd     register Char **st;
    275  1.1       cgd {
    276  1.1       cgd     register char **t;
    277  1.1       cgd     register char *f;
    278  1.1       cgd     register struct varent *v;
    279  1.1       cgd     register Char **vp;
    280  1.1       cgd     Char   *lastsh[2];
    281  1.1       cgd     int     fd;
    282  1.1       cgd     unsigned char c;
    283  1.1       cgd     Char   *st0, **ost;
    284  1.1       cgd 
    285  1.1       cgd     /* The order for the conversions is significant */
    286  1.1       cgd     t = short2blk(st);
    287  1.1       cgd     f = short2str(sf);
    288  1.1       cgd     Vt = t;
    289  1.1       cgd     errno = 0;			/* don't use a previous error */
    290  1.1       cgd     (void) execve(f, t, environ);
    291  1.1       cgd     Vt = 0;
    292  1.1       cgd     blkfree((Char **) t);
    293  1.1       cgd     switch (errno) {
    294  1.1       cgd 
    295  1.1       cgd     case ENOEXEC:
    296  1.1       cgd 	/*
    297  1.1       cgd 	 * From: casper (at) fwi.uva.nl (Casper H.S. Dik) If we could not execute
    298  1.1       cgd 	 * it, don't feed it to the shell if it looks like a binary!
    299  1.1       cgd 	 */
    300  1.1       cgd 	if ((fd = open(f, O_RDONLY)) != -1) {
    301  1.1       cgd 	    if (read(fd, (char *) &c, 1) == 1) {
    302  1.1       cgd 		if (!Isprint(c) && (c != '\n' && c != '\t')) {
    303  1.1       cgd 		    (void) close(fd);
    304  1.1       cgd 		    /*
    305  1.1       cgd 		     * We *know* what ENOEXEC means.
    306  1.1       cgd 		     */
    307  1.1       cgd 		    stderror(ERR_ARCH, f, strerror(errno));
    308  1.1       cgd 		}
    309  1.1       cgd 	    }
    310  1.1       cgd #ifdef _PATH_BSHELL
    311  1.1       cgd 	    else
    312  1.1       cgd 		c = '#';
    313  1.1       cgd #endif
    314  1.1       cgd 	    (void) close(fd);
    315  1.1       cgd 	}
    316  1.1       cgd 	/*
    317  1.1       cgd 	 * If there is an alias for shell, then put the words of the alias in
    318  1.1       cgd 	 * front of the argument list replacing the command name. Note no
    319  1.1       cgd 	 * interpretation of the words at this point.
    320  1.1       cgd 	 */
    321  1.1       cgd 	v = adrof1(STRshell, &aliases);
    322  1.1       cgd 	if (v == 0) {
    323  1.1       cgd 	    vp = lastsh;
    324  1.1       cgd 	    vp[0] = adrof(STRshell) ? value(STRshell) : STR_SHELLPATH;
    325  1.1       cgd 	    vp[1] = NULL;
    326  1.1       cgd #ifdef _PATH_BSHELL
    327  1.1       cgd 	    if (fd != -1 && c != '#')
    328  1.1       cgd 		vp[0] = STR_BSHELL;
    329  1.1       cgd #endif
    330  1.1       cgd 	}
    331  1.1       cgd 	else
    332  1.1       cgd 	    vp = v->vec;
    333  1.1       cgd 	st0 = st[0];
    334  1.1       cgd 	st[0] = sf;
    335  1.1       cgd 	ost = st;
    336  1.1       cgd 	st = blkspl(vp, st);	/* Splice up the new arglst */
    337  1.1       cgd 	ost[0] = st0;
    338  1.1       cgd 	sf = *st;
    339  1.1       cgd 	/* The order for the conversions is significant */
    340  1.1       cgd 	t = short2blk(st);
    341  1.1       cgd 	f = short2str(sf);
    342  1.1       cgd 	xfree((ptr_t) st);
    343  1.1       cgd 	Vt = t;
    344  1.1       cgd 	(void) execve(f, t, environ);
    345  1.1       cgd 	Vt = 0;
    346  1.1       cgd 	blkfree((Char **) t);
    347  1.1       cgd 	/* The sky is falling, the sky is falling! */
    348  1.1       cgd 
    349  1.1       cgd     case ENOMEM:
    350  1.1       cgd 	stderror(ERR_SYSTEM, f, strerror(errno));
    351  1.1       cgd 
    352  1.1       cgd     case ENOENT:
    353  1.1       cgd 	break;
    354  1.1       cgd 
    355  1.1       cgd     default:
    356  1.1       cgd 	if (exerr == 0) {
    357  1.1       cgd 	    exerr = strerror(errno);
    358  1.1       cgd 	    if (expath)
    359  1.1       cgd 		xfree((ptr_t) expath);
    360  1.1       cgd 	    expath = Strsave(sf);
    361  1.1       cgd 	    Vexpath = expath;
    362  1.1       cgd 	}
    363  1.1       cgd     }
    364  1.1       cgd }
    365  1.1       cgd 
    366  1.1       cgd /*ARGSUSED*/
    367  1.1       cgd void
    368  1.1       cgd execash(t, kp)
    369  1.5   mycroft     Char  **t;
    370  1.1       cgd     register struct command *kp;
    371  1.1       cgd {
    372  1.5   mycroft     int     saveIN, saveOUT, saveDIAG, saveSTD;
    373  1.5   mycroft     int     oSHIN;
    374  1.5   mycroft     int     oSHOUT;
    375  1.5   mycroft     int     oSHERR;
    376  1.5   mycroft     int     oOLDSTD;
    377  1.5   mycroft     jmp_buf osetexit;
    378  1.5   mycroft     int	    my_reenter;
    379  1.5   mycroft     int     odidfds;
    380  1.5   mycroft     sig_t   osigint, osigquit, osigterm;
    381  1.5   mycroft 
    382  1.1       cgd     if (chkstop == 0 && setintr)
    383  1.1       cgd 	panystop(0);
    384  1.5   mycroft     /*
    385  1.5   mycroft      * Hmm, we don't really want to do that now because we might
    386  1.5   mycroft      * fail, but what is the choice
    387  1.5   mycroft      */
    388  1.1       cgd     rechist();
    389  1.5   mycroft 
    390  1.5   mycroft     osigint  = signal(SIGINT, parintr);
    391  1.5   mycroft     osigquit = signal(SIGQUIT, parintr);
    392  1.5   mycroft     osigterm = signal(SIGTERM, parterm);
    393  1.5   mycroft 
    394  1.5   mycroft     odidfds = didfds;
    395  1.5   mycroft     oSHIN = SHIN;
    396  1.5   mycroft     oSHOUT = SHOUT;
    397  1.5   mycroft     oSHERR = SHERR;
    398  1.5   mycroft     oOLDSTD = OLDSTD;
    399  1.5   mycroft 
    400  1.5   mycroft     saveIN = dcopy(SHIN, -1);
    401  1.5   mycroft     saveOUT = dcopy(SHOUT, -1);
    402  1.5   mycroft     saveDIAG = dcopy(SHERR, -1);
    403  1.5   mycroft     saveSTD = dcopy(OLDSTD, -1);
    404  1.5   mycroft 
    405  1.1       cgd     lshift(kp->t_dcom, 1);
    406  1.5   mycroft 
    407  1.5   mycroft     getexit(osetexit);
    408  1.5   mycroft 
    409  1.5   mycroft     if ((my_reenter = setexit()) == 0) {
    410  1.5   mycroft 	SHIN = dcopy(0, -1);
    411  1.5   mycroft 	SHOUT = dcopy(1, -1);
    412  1.5   mycroft 	SHERR = dcopy(2, -1);
    413  1.5   mycroft 	didfds = 0;
    414  1.5   mycroft 	doexec(t, kp);
    415  1.5   mycroft     }
    416  1.5   mycroft 
    417  1.5   mycroft     (void) signal(SIGINT, osigint);
    418  1.5   mycroft     (void) signal(SIGQUIT, osigquit);
    419  1.5   mycroft     (void) signal(SIGTERM, osigterm);
    420  1.5   mycroft 
    421  1.5   mycroft     doneinp = 0;
    422  1.5   mycroft     didfds = odidfds;
    423  1.5   mycroft     (void) close(SHIN);
    424  1.5   mycroft     (void) close(SHOUT);
    425  1.5   mycroft     (void) close(SHERR);
    426  1.5   mycroft     (void) close(OLDSTD);
    427  1.5   mycroft     SHIN = dmove(saveIN, oSHIN);
    428  1.5   mycroft     SHOUT = dmove(saveOUT, oSHOUT);
    429  1.5   mycroft     SHERR = dmove(saveDIAG, oSHERR);
    430  1.5   mycroft     OLDSTD = dmove(saveSTD, oOLDSTD);
    431  1.5   mycroft 
    432  1.5   mycroft     resexit(osetexit);
    433  1.5   mycroft     if (my_reenter)
    434  1.5   mycroft 	stderror(ERR_SILENT);
    435  1.1       cgd }
    436  1.1       cgd 
    437  1.1       cgd void
    438  1.1       cgd xechoit(t)
    439  1.1       cgd     Char  **t;
    440  1.1       cgd {
    441  1.1       cgd     if (adrof(STRecho)) {
    442  1.5   mycroft 	(void) fflush(csherr);
    443  1.5   mycroft 	blkpr(csherr, t);
    444  1.5   mycroft 	(void) fputc('\n', csherr);
    445  1.1       cgd     }
    446  1.1       cgd }
    447  1.1       cgd 
    448  1.1       cgd void
    449  1.5   mycroft /*ARGSUSED*/
    450  1.5   mycroft dohash(v, t)
    451  1.5   mycroft     Char **v;
    452  1.5   mycroft     struct command *t;
    453  1.1       cgd {
    454  1.1       cgd     DIR    *dirp;
    455  1.1       cgd     register struct dirent *dp;
    456  1.1       cgd     register int cnt;
    457  1.1       cgd     int     i = 0;
    458  1.5   mycroft     struct varent *pathv = adrof(STRpath);
    459  1.1       cgd     Char  **pv;
    460  1.1       cgd     int     hashval;
    461  1.1       cgd 
    462  1.1       cgd     havhash = 1;
    463  1.1       cgd     for (cnt = 0; cnt < sizeof xhash; cnt++)
    464  1.1       cgd 	xhash[cnt] = 0;
    465  1.5   mycroft     if (pathv == 0)
    466  1.1       cgd 	return;
    467  1.5   mycroft     for (pv = pathv->vec; *pv; pv++, i++) {
    468  1.1       cgd 	if (pv[0][0] != '/')
    469  1.1       cgd 	    continue;
    470  1.1       cgd 	dirp = opendir(short2str(*pv));
    471  1.1       cgd 	if (dirp == NULL)
    472  1.1       cgd 	    continue;
    473  1.1       cgd 	while ((dp = readdir(dirp)) != NULL) {
    474  1.1       cgd 	    if (dp->d_ino == 0)
    475  1.1       cgd 		continue;
    476  1.1       cgd 	    if (dp->d_name[0] == '.' &&
    477  1.1       cgd 		(dp->d_name[1] == '\0' ||
    478  1.5   mycroft 		 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
    479  1.1       cgd 		continue;
    480  1.1       cgd 	    hashval = hash(hashname(str2short(dp->d_name)), i);
    481  1.1       cgd 	    bis(xhash, hashval);
    482  1.1       cgd 	    /* tw_add_comm_name (dp->d_name); */
    483  1.1       cgd 	}
    484  1.1       cgd 	(void) closedir(dirp);
    485  1.1       cgd     }
    486  1.1       cgd }
    487  1.1       cgd 
    488  1.1       cgd void
    489  1.5   mycroft /*ARGSUSED*/
    490  1.5   mycroft dounhash(v, t)
    491  1.5   mycroft     Char **v;
    492  1.5   mycroft     struct command *t;
    493  1.1       cgd {
    494  1.1       cgd     havhash = 0;
    495  1.1       cgd }
    496  1.1       cgd 
    497  1.1       cgd void
    498  1.5   mycroft /*ARGSUSED*/
    499  1.5   mycroft hashstat(v, t)
    500  1.5   mycroft     Char **v;
    501  1.5   mycroft     struct command *t;
    502  1.1       cgd {
    503  1.1       cgd     if (hits + misses)
    504  1.5   mycroft 	(void) fprintf(cshout, "%d hits, %d misses, %d%%\n",
    505  1.5   mycroft 		       hits, misses, 100 * hits / (hits + misses));
    506  1.1       cgd }
    507  1.1       cgd 
    508  1.1       cgd /*
    509  1.1       cgd  * Hash a command name.
    510  1.1       cgd  */
    511  1.1       cgd static int
    512  1.1       cgd hashname(cp)
    513  1.1       cgd     register Char *cp;
    514  1.1       cgd {
    515  1.1       cgd     register long h = 0;
    516  1.1       cgd 
    517  1.1       cgd     while (*cp)
    518  1.1       cgd 	h = hash(h, *cp++);
    519  1.1       cgd     return ((int) h);
    520  1.5   mycroft }
    521  1.5   mycroft 
    522  1.5   mycroft static int
    523  1.5   mycroft iscommand(name)
    524  1.5   mycroft     Char   *name;
    525  1.5   mycroft {
    526  1.5   mycroft     register Char **pv;
    527  1.5   mycroft     register Char *sav;
    528  1.5   mycroft     register struct varent *v;
    529  1.5   mycroft     register bool slash = any(short2str(name), '/');
    530  1.5   mycroft     register int hashval = 0, hashval1, i;
    531  1.5   mycroft 
    532  1.5   mycroft     v = adrof(STRpath);
    533  1.5   mycroft     if (v == 0 || v->vec[0] == 0 || slash)
    534  1.5   mycroft 	pv = justabs;
    535  1.5   mycroft     else
    536  1.5   mycroft 	pv = v->vec;
    537  1.5   mycroft     sav = Strspl(STRslash, name);	/* / command name for postpending */
    538  1.5   mycroft     if (havhash)
    539  1.5   mycroft 	hashval = hashname(name);
    540  1.5   mycroft     i = 0;
    541  1.5   mycroft     do {
    542  1.5   mycroft 	if (!slash && pv[0][0] == '/' && havhash) {
    543  1.5   mycroft 	    hashval1 = hash(hashval, i);
    544  1.5   mycroft 	    if (!bit(xhash, hashval1))
    545  1.5   mycroft 		goto cont;
    546  1.5   mycroft 	}
    547  1.5   mycroft 	if (pv[0][0] == 0 || eq(pv[0], STRdot)) {	/* don't make ./xxx */
    548  1.5   mycroft 	    if (executable(NULL, name, 0)) {
    549  1.5   mycroft 		xfree((ptr_t) sav);
    550  1.5   mycroft 		return i + 1;
    551  1.5   mycroft 	    }
    552  1.5   mycroft 	}
    553  1.5   mycroft 	else {
    554  1.5   mycroft 	    if (executable(*pv, sav, 0)) {
    555  1.5   mycroft 		xfree((ptr_t) sav);
    556  1.5   mycroft 		return i + 1;
    557  1.5   mycroft 	    }
    558  1.5   mycroft 	}
    559  1.5   mycroft cont:
    560  1.5   mycroft 	pv++;
    561  1.5   mycroft 	i++;
    562  1.5   mycroft     } while (*pv);
    563  1.5   mycroft     xfree((ptr_t) sav);
    564  1.5   mycroft     return 0;
    565  1.5   mycroft }
    566  1.5   mycroft 
    567  1.5   mycroft /* Also by:
    568  1.5   mycroft  *  Andreas Luik <luik (at) isaak.isa.de>
    569  1.5   mycroft  *  I S A  GmbH - Informationssysteme fuer computerintegrierte Automatisierung
    570  1.5   mycroft  *  Azenberstr. 35
    571  1.5   mycroft  *  D-7000 Stuttgart 1
    572  1.5   mycroft  *  West-Germany
    573  1.5   mycroft  * is the executable() routine below and changes to iscommand().
    574  1.5   mycroft  * Thanks again!!
    575  1.5   mycroft  */
    576  1.5   mycroft 
    577  1.5   mycroft /*
    578  1.5   mycroft  * executable() examines the pathname obtained by concatenating dir and name
    579  1.5   mycroft  * (dir may be NULL), and returns 1 either if it is executable by us, or
    580  1.5   mycroft  * if dir_ok is set and the pathname refers to a directory.
    581  1.5   mycroft  * This is a bit kludgy, but in the name of optimization...
    582  1.5   mycroft  */
    583  1.5   mycroft static int
    584  1.5   mycroft executable(dir, name, dir_ok)
    585  1.5   mycroft     Char   *dir, *name;
    586  1.5   mycroft     bool    dir_ok;
    587  1.5   mycroft {
    588  1.5   mycroft     struct stat stbuf;
    589  1.5   mycroft     Char    path[MAXPATHLEN + 1], *dp, *sp;
    590  1.5   mycroft     char   *strname;
    591  1.5   mycroft 
    592  1.5   mycroft     if (dir && *dir) {
    593  1.5   mycroft 	for (dp = path, sp = dir; *sp; *dp++ = *sp++)
    594  1.5   mycroft 	    if (dp == &path[MAXPATHLEN + 1]) {
    595  1.5   mycroft 		*--dp = '\0';
    596  1.5   mycroft 		break;
    597  1.5   mycroft 	    }
    598  1.5   mycroft 	for (sp = name; *sp; *dp++ = *sp++)
    599  1.5   mycroft 	    if (dp == &path[MAXPATHLEN + 1]) {
    600  1.5   mycroft 		*--dp = '\0';
    601  1.5   mycroft 		break;
    602  1.5   mycroft 	    }
    603  1.5   mycroft 	*dp = '\0';
    604  1.5   mycroft 	strname = short2str(path);
    605  1.5   mycroft     }
    606  1.5   mycroft     else
    607  1.5   mycroft 	strname = short2str(name);
    608  1.5   mycroft     return (stat(strname, &stbuf) != -1 &&
    609  1.5   mycroft 	    ((S_ISREG(stbuf.st_mode) &&
    610  1.5   mycroft     /* save time by not calling access() in the hopeless case */
    611  1.5   mycroft 	      (stbuf.st_mode & (S_IXOTH | S_IXGRP | S_IXUSR)) &&
    612  1.5   mycroft 	      access(strname, X_OK) == 0) ||
    613  1.5   mycroft 	     (dir_ok && S_ISDIR(stbuf.st_mode))));
    614  1.5   mycroft }
    615  1.5   mycroft 
    616  1.5   mycroft /* The dowhich() is by:
    617  1.5   mycroft  *  Andreas Luik <luik (at) isaak.isa.de>
    618  1.5   mycroft  *  I S A  GmbH - Informationssysteme fuer computerintegrierte Automatisierung
    619  1.5   mycroft  *  Azenberstr. 35
    620  1.5   mycroft  *  D-7000 Stuttgart 1
    621  1.5   mycroft  *  West-Germany
    622  1.5   mycroft  * Thanks!!
    623  1.5   mycroft  */
    624  1.5   mycroft /*ARGSUSED*/
    625  1.5   mycroft void
    626  1.5   mycroft dowhich(v, c)
    627  1.5   mycroft     register Char **v;
    628  1.5   mycroft     struct command *c;
    629  1.5   mycroft {
    630  1.5   mycroft     struct wordent lex[3];
    631  1.5   mycroft     struct varent *vp;
    632  1.5   mycroft 
    633  1.5   mycroft     lex[0].next = &lex[1];
    634  1.5   mycroft     lex[1].next = &lex[2];
    635  1.5   mycroft     lex[2].next = &lex[0];
    636  1.5   mycroft 
    637  1.5   mycroft     lex[0].prev = &lex[2];
    638  1.5   mycroft     lex[1].prev = &lex[0];
    639  1.5   mycroft     lex[2].prev = &lex[1];
    640  1.5   mycroft 
    641  1.5   mycroft     lex[0].word = STRNULL;
    642  1.5   mycroft     lex[2].word = STRret;
    643  1.5   mycroft 
    644  1.5   mycroft     while (*++v) {
    645  1.5   mycroft 	if ((vp = adrof1(*v, &aliases)) != NULL) {
    646  1.5   mycroft 	    (void) fprintf(cshout, "%s: \t aliased to ", vis_str(*v));
    647  1.5   mycroft 	    blkpr(cshout, vp->vec);
    648  1.5   mycroft 	    (void) fputc('\n', cshout);
    649  1.9  christos 	    set(STRstatus, Strsave(STR0));
    650  1.5   mycroft 	}
    651  1.5   mycroft 	else {
    652  1.5   mycroft 	    lex[1].word = *v;
    653  1.9  christos 	    set(STRstatus, Strsave(tellmewhat(lex, NULL) ? STR0 : STR1));
    654  1.5   mycroft 	}
    655  1.5   mycroft     }
    656  1.5   mycroft }
    657  1.5   mycroft 
    658  1.9  christos static int
    659  1.9  christos tellmewhat(lexp, str)
    660  1.9  christos     struct wordent *lexp;
    661  1.9  christos     Char *str;
    662  1.5   mycroft {
    663  1.5   mycroft     register int i;
    664  1.5   mycroft     register struct biltins *bptr;
    665  1.9  christos     register struct wordent *sp = lexp->next;
    666  1.9  christos     bool    aliased = 0, found;
    667  1.8  christos     Char   *s0, *s1, *s2, *cmd;
    668  1.5   mycroft     Char    qc;
    669  1.5   mycroft 
    670  1.5   mycroft     if (adrof1(sp->word, &aliases)) {
    671  1.9  christos 	alias(lexp);
    672  1.9  christos 	sp = lexp->next;
    673  1.5   mycroft 	aliased = 1;
    674  1.5   mycroft     }
    675  1.5   mycroft 
    676  1.5   mycroft     s0 = sp->word;		/* to get the memory freeing right... */
    677  1.5   mycroft 
    678  1.5   mycroft     /* handle quoted alias hack */
    679  1.5   mycroft     if ((*(sp->word) & (QUOTE | TRIM)) == QUOTE)
    680  1.5   mycroft 	(sp->word)++;
    681  1.5   mycroft 
    682  1.5   mycroft     /* do quoting, if it hasn't been done */
    683  1.5   mycroft     s1 = s2 = sp->word;
    684  1.5   mycroft     while (*s2)
    685  1.5   mycroft 	switch (*s2) {
    686  1.5   mycroft 	case '\'':
    687  1.5   mycroft 	case '"':
    688  1.5   mycroft 	    qc = *s2++;
    689  1.5   mycroft 	    while (*s2 && *s2 != qc)
    690  1.5   mycroft 		*s1++ = *s2++ | QUOTE;
    691  1.5   mycroft 	    if (*s2)
    692  1.5   mycroft 		s2++;
    693  1.5   mycroft 	    break;
    694  1.5   mycroft 	case '\\':
    695  1.5   mycroft 	    if (*++s2)
    696  1.5   mycroft 		*s1++ = *s2++ | QUOTE;
    697  1.5   mycroft 	    break;
    698  1.5   mycroft 	default:
    699  1.5   mycroft 	    *s1++ = *s2++;
    700  1.5   mycroft 	}
    701  1.5   mycroft     *s1 = '\0';
    702  1.5   mycroft 
    703  1.5   mycroft     for (bptr = bfunc; bptr < &bfunc[nbfunc]; bptr++) {
    704  1.5   mycroft 	if (eq(sp->word, str2short(bptr->bname))) {
    705  1.9  christos 	    if (str == NULL) {
    706  1.9  christos 		if (aliased)
    707  1.9  christos 		    prlex(cshout, lexp);
    708  1.9  christos 		(void) fprintf(cshout, "%s: shell built-in command.\n",
    709  1.9  christos 			       vis_str(sp->word));
    710  1.9  christos 	    }
    711  1.9  christos 	    else
    712  1.9  christos 		(void) Strcpy(str, sp->word);
    713  1.5   mycroft 	    sp->word = s0;	/* we save and then restore this */
    714  1.9  christos 	    return 1;
    715  1.5   mycroft 	}
    716  1.5   mycroft     }
    717  1.5   mycroft 
    718  1.8  christos     sp->word = cmd = globone(sp->word, G_IGNORE);
    719  1.8  christos 
    720  1.9  christos     if ((i = iscommand(sp->word)) != 0) {
    721  1.5   mycroft 	register Char **pv;
    722  1.5   mycroft 	register struct varent *v;
    723  1.5   mycroft 	bool    slash = any(short2str(sp->word), '/');
    724  1.5   mycroft 
    725  1.5   mycroft 	v = adrof(STRpath);
    726  1.5   mycroft 	if (v == 0 || v->vec[0] == 0 || slash)
    727  1.5   mycroft 	    pv = justabs;
    728  1.5   mycroft 	else
    729  1.5   mycroft 	    pv = v->vec;
    730  1.5   mycroft 
    731  1.5   mycroft 	while (--i)
    732  1.5   mycroft 	    pv++;
    733  1.5   mycroft 	if (pv[0][0] == 0 || eq(pv[0], STRdot)) {
    734  1.8  christos 	    if (!slash) {
    735  1.8  christos 		sp->word = Strspl(STRdotsl, sp->word);
    736  1.9  christos 		prlex(cshout, lexp);
    737  1.8  christos 		xfree((ptr_t) sp->word);
    738  1.8  christos 	    }
    739  1.8  christos 	    else
    740  1.9  christos 		prlex(cshout, lexp);
    741  1.9  christos 	}
    742  1.9  christos 	else {
    743  1.9  christos 	    s1 = Strspl(*pv, STRslash);
    744  1.9  christos 	    sp->word = Strspl(s1, sp->word);
    745  1.9  christos 	    xfree((ptr_t) s1);
    746  1.9  christos 	    if (str == NULL)
    747  1.9  christos 		prlex(cshout, lexp);
    748  1.9  christos 	    else
    749  1.9  christos 		(void) Strcpy(str, sp->word);
    750  1.9  christos 	    xfree((ptr_t) sp->word);
    751  1.5   mycroft 	}
    752  1.9  christos 	found = 1;
    753  1.5   mycroft     }
    754  1.5   mycroft     else {
    755  1.9  christos  	if (str == NULL) {
    756  1.9  christos 	    if (aliased)
    757  1.9  christos 		prlex(cshout, lexp);
    758  1.9  christos 	    (void) fprintf(csherr,
    759  1.9  christos 			   "%s: Command not found.\n", vis_str(sp->word));
    760  1.9  christos 	}
    761  1.9  christos 	else
    762  1.9  christos 	    (void) Strcpy(str, sp->word);
    763  1.9  christos 	found = 0;
    764  1.5   mycroft     }
    765  1.5   mycroft     sp->word = s0;		/* we save and then restore this */
    766  1.8  christos     xfree((ptr_t) cmd);
    767  1.9  christos     return found;
    768  1.1       cgd }
    769