2 * sh.exec.c: Search, find, and execute a command!
5 * Copyright (c) 1980, 1991 The Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #endif /*WINNT_NATIVE*/
44 # define FASTHASH /* Fast hashing is the default */
48 * System level search and execute of a command.
49 * We look in each directory for the specified command name.
50 * If the name contains a '/' then we execute only the full path name.
51 * If there is no search path then we execute only full path names.
55 * As we search for the command we note the first non-trivial error
56 * message for presentation to the user. This allows us often
57 * to show that a file has the wrong mode/no access when the file
58 * is not in the last component of the search path, so we must
59 * go on after first detecting the error.
61 static char *exerr
; /* Execution error message */
62 static Char
*expath
; /* Path for exerr */
65 * The two part hash function is designed to let texec() call the
66 * more expensive hashname() only once and the simple hash() several
67 * times (once for each path component checked).
68 * Byte size is assumed to be 8.
70 #define BITS_PER_BYTE 8
74 * xhash is an array of hash buckets which are used to hash execs. If
75 * it is allocated (havhash true), then to tell if ``name'' is
76 * (possibly) present in the i'th component of the variable path, look
77 * at the [hashname(name)] bucket of size [hashwidth] bytes, in the [i
78 * mod size*8]'th bit. The cache size is defaults to a length of 1024
79 * buckets, each 1 byte wide. This implementation guarantees that
80 * objects n bytes wide will be aligned on n byte boundaries.
84 static unsigned long *xhash
= NULL
;
85 static unsigned int hashlength
= 0, uhashlength
= 0;
86 static unsigned int hashwidth
= 0, uhashwidth
= 0;
87 static int hashdebug
= 0;
89 # define hash(a, b) (((a) * HSHMUL + (b)) % (hashlength))
90 # define widthof(t) (sizeof(t) * BITS_PER_BYTE)
91 # define tbit(f, i, t) (((t *) xhash)[(f)] & \
92 (1UL << (i & (widthof(t) - 1))))
93 # define tbis(f, i, t) (((t *) xhash)[(f)] |= \
94 (1UL << (i & (widthof(t) - 1))))
95 # define cbit(f, i) tbit(f, i, unsigned char)
96 # define cbis(f, i) tbis(f, i, unsigned char)
97 # define sbit(f, i) tbit(f, i, unsigned short)
98 # define sbis(f, i) tbis(f, i, unsigned short)
99 # define ibit(f, i) tbit(f, i, unsigned int)
100 # define ibis(f, i) tbis(f, i, unsigned int)
101 # define lbit(f, i) tbit(f, i, unsigned long)
102 # define lbis(f, i) tbis(f, i, unsigned long)
104 # define bit(f, i) (hashwidth==sizeof(unsigned char) ? cbit(f,i) : \
105 ((hashwidth==sizeof(unsigned short) ? sbit(f,i) : \
106 ((hashwidth==sizeof(unsigned int) ? ibit(f,i) : \
108 # define bis(f, i) (hashwidth==sizeof(unsigned char) ? cbis(f,i) : \
109 ((hashwidth==sizeof(unsigned short) ? sbis(f,i) : \
110 ((hashwidth==sizeof(unsigned int) ? ibis(f,i) : \
114 * Xhash is an array of HSHSIZ bits (HSHSIZ / 8 chars), which are used
115 * to hash execs. If it is allocated (havhash true), then to tell
116 * whether ``name'' is (possibly) present in the i'th component
117 * of the variable path, you look at the bit in xhash indexed by
118 * hash(hashname("name"), i). This is setup automatically
119 * after .login is executed, and recomputed whenever ``path'' is
122 # define HSHSIZ 8192 /* 1k bytes */
123 # define HSHMASK (HSHSIZ - 1)
125 static char xhash
[HSHSIZ
/ BITS_PER_BYTE
];
127 # define hash(a, b) (((a) * HSHMUL + (b)) & HSHMASK)
128 # define bit(h, b) ((h)[(b) >> 3] & 1 << ((b) & 7)) /* bit test */
129 # define bis(h, b) ((h)[(b) >> 3] |= 1 << ((b) & 7)) /* bit set */
131 #endif /* FASTHASH */
134 static int hits
, misses
;
137 /* Dummy search path for just absolute search when no path */
138 static Char
*justabs
[] = {STRNULL
, 0};
140 static void pexerr (void) __attribute__((__noreturn__
));
141 static void texec (Char
*, Char
**);
142 int hashname (Char
*);
143 static int iscommand (Char
*);
146 doexec(struct command
*t
, int do_glob
)
148 Char
*dp
, **pv
, **opv
, **av
, *sav
;
150 int slash
, gflag
, rehashed
;
155 * Glob the command name. We will search $path even if this does something,
156 * as in sh but not in csh. One special case: if there is no PATH, then we
157 * execute only commands which start with '/'.
159 blk
[0] = t
->t_dcom
[0];
165 pv
= globall(blk
, gflag
);
167 setname(short2str(blk
[0]));
168 stderror(ERR_NAME
| ERR_NOMATCH
);
173 cleanup_push(pv
, blk_cleanup
);
178 expath
= Strsave(pv
[0]);
184 if (v
== 0 && expath
[0] != '/' && expath
[0] != '.')
186 slash
= any(short2str(expath
), '/');
189 * Glob the argument list, if necessary. Otherwise trim off the quote bits.
196 av
= globall(av
, gflag
);
198 setname(short2str(expath
));
199 stderror(ERR_NAME
| ERR_NOMATCH
);
208 t
->t_dcom
= blkspl(pv
, av
);
214 if (*av
== NULL
|| **av
== '\0')
217 xechoit(av
); /* Echo command if -x */
220 * Since all internal file descriptors are set to close on exec, we don't
221 * need to close them explicitly here. Just reorient ourselves for error
228 isoutatty
= isatty(SHOUT
);
229 isdiagatty
= isatty(SHDIAG
);
231 closech(); /* Close random fd's */
234 * We must do this AFTER any possible forking (like `foo` in glob) so that
235 * this shell can still do subprocesses.
240 sigaddset(&set
, SIGINT
);
241 sigaddset(&set
, SIGCHLD
);
242 sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
248 * If no path, no words in path, or a / in the filename then restrict the
251 if (v
== NULL
|| v
->vec
== NULL
|| v
->vec
[0] == NULL
|| slash
)
255 sav
= Strspl(STRslash
, *av
);/* / command name for postpending */
257 cleanup_push(sav
, xfree
);
261 hashval
= havhash
? hashname(*av
) : 0;
272 * Try to save time by looking at the hash table for where this command
273 * could be. If we are doing delayed hashing, then we put the names in
274 * one at a time, as the user enters them. This is kinda like Korn
275 * Shell's "tracked aliases".
277 if (!slash
&& ABSOLUTEP(pv
[0]) && havhash
) {
279 if (!bit(hashval
, i
))
282 int hashval1
= hash(hashval
, i
);
283 if (!bit(xhash
, hashval1
))
285 #endif /* FASTHASH */
287 if (pv
[0][0] == 0 || eq(pv
[0], STRdot
)) /* don't make ./xxx */
290 dp
= Strspl(*pv
, sav
);
292 cleanup_push(dp
, xfree
);
315 if (adrof(STRautorehash
) && !rehashed
&& havhash
&& opv
!= justabs
) {
332 /* Couldn't find the damn thing */
334 setname(short2str(expath
));
344 stderror(ERR_NAME
| ERR_STRING
, exerr
);
345 stderror(ERR_NAME
| ERR_COMMAND
);
349 * Execute command f, arg list t.
350 * Record error message if not found.
351 * Also do shell scripts here.
354 texec(Char
*sf
, Char
**st
)
365 /* The order for the conversions is significant */
371 errno
= 0; /* don't use a previous error */
374 * If we try to execute an nfs mounted directory on the apollo, we
375 * hang forever. So until apollo fixes that..
379 if (stat(f
, &stb
) == 0 && S_ISDIR(stb
.st_mode
))
385 #ifdef ISC_POSIX_EXEC_BUG
386 __setostype(0); /* "0" is "__OS_SYSV" in <sys/user.h> */
387 #endif /* ISC_POSIX_EXEC_BUG */
389 #ifdef ISC_POSIX_EXEC_BUG
390 __setostype(1); /* "1" is "__OS_POSIX" in <sys/user.h> */
391 #endif /* ISC_POSIX_EXEC_BUG */
396 blkfree((Char
**) t
);
402 #endif /* WINNT_NATIVE */
404 * From: casper@fwi.uva.nl (Casper H.S. Dik) If we could not execute
405 * it, don't feed it to the shell if it looks like a binary!
407 if ((fd
= xopen(f
, O_RDONLY
|O_LARGEFILE
)) != -1) {
409 if ((nread
= xread(fd
, pref
, 2)) == 2) {
410 if (!isprint((unsigned char)pref
[0]) &&
411 (pref
[0] != '\n' && pref
[0] != '\t')) {
417 * We *know* what ENOEXEC means.
419 stderror(ERR_ARCH
, f
, strerror(err
));
422 else if (nread
< 0) {
428 /* need to print error incase the file is migrated */
429 stderror(ERR_SYSTEM
, f
, strerror(err
));
441 pref
[0] != '#' || pref
[1] != '!' || hashbang(fd
, &vp
) == -1) {
442 #endif /* HASHBANG */
444 * If there is an alias for shell, then put the words of the alias in
445 * front of the argument list replacing the command name. Note no
446 * interpretation of the words at this point.
448 v
= adrof1(STRshell
, &aliases
);
449 if (v
== NULL
|| v
->vec
== NULL
) {
451 vp
[0] = adrof(STRshell
) ? varval(STRshell
) : STR_SHELLPATH
;
455 # ifndef ISC /* Compatible with ISC's /bin/csh */
464 vp
= saveblk(v
->vec
);
467 #endif /* HASHBANG */
474 st
= blkspl(vp
, st
); /* Splice up the new arglst */
477 /* The order for the conversions is significant */
481 blkfree((Char
**) vp
);
485 #ifdef ISC_POSIX_EXEC_BUG
486 __setostype(0); /* "0" is "__OS_SYSV" in <sys/user.h> */
487 #endif /* ISC_POSIX_EXEC_BUG */
489 #ifdef ISC_POSIX_EXEC_BUG
490 __setostype(1); /* "1" is "__OS_POSIX" in <sys/user.h> */
491 #endif /* ISC_POSIX_EXEC_BUG */
495 blkfree((Char
**) t
);
496 /* The sky is falling, the sky is falling! */
497 stderror(ERR_SYSTEM
, f
, strerror(errno
));
501 stderror(ERR_SYSTEM
, f
, strerror(errno
));
505 case 0: /* execv fails and returns 0! */
512 exerr
= strerror(errno
);
514 expath
= Strsave(sf
);
525 int saveIN
, saveOUT
, saveDIAG
, saveSTD
;
526 int SHIN
, SHOUT
, SHDIAG
, OLDSTD
;
528 #ifndef CLOSE_ON_EXEC
531 struct sigaction sigint
, sigquit
, sigterm
;
535 execash_cleanup(void *xstate
)
537 struct execash_state
*state
;
540 sigaction(SIGINT
, &state
->sigint
, NULL
);
541 sigaction(SIGQUIT
, &state
->sigquit
, NULL
);
542 sigaction(SIGTERM
, &state
->sigterm
, NULL
);
545 #ifndef CLOSE_ON_EXEC
546 didcch
= state
->didcch
;
547 #endif /* CLOSE_ON_EXEC */
548 didfds
= state
->didfds
;
553 close_on_exec(SHIN
= dmove(state
->saveIN
, state
->SHIN
), 1);
554 close_on_exec(SHOUT
= dmove(state
->saveOUT
, state
->SHOUT
), 1);
555 close_on_exec(SHDIAG
= dmove(state
->saveDIAG
, state
->SHDIAG
), 1);
556 close_on_exec(OLDSTD
= dmove(state
->saveSTD
, state
->OLDSTD
), 1);
561 execash(Char
**t
, struct command
*kp
)
563 struct execash_state state
;
566 if (chkstop
== 0 && setintr
)
569 * Hmm, we don't really want to do that now because we might
570 * fail, but what is the choice
572 rechist(NULL
, adrof(STRsavehist
) != NULL
);
575 sigaction(SIGINT
, &parintr
, &state
.sigint
);
576 sigaction(SIGQUIT
, &parintr
, &state
.sigquit
);
577 sigaction(SIGTERM
, &parterm
, &state
.sigterm
);
579 state
.didfds
= didfds
;
580 #ifndef CLOSE_ON_EXEC
581 state
.didcch
= didcch
;
582 #endif /* CLOSE_ON_EXEC */
585 state
.SHDIAG
= SHDIAG
;
586 state
.OLDSTD
= OLDSTD
;
588 (void)close_on_exec (state
.saveIN
= dcopy(SHIN
, -1), 1);
589 (void)close_on_exec (state
.saveOUT
= dcopy(SHOUT
, -1), 1);
590 (void)close_on_exec (state
.saveDIAG
= dcopy(SHDIAG
, -1), 1);
591 (void)close_on_exec (state
.saveSTD
= dcopy(OLDSTD
, -1), 1);
593 lshift(kp
->t_dcom
, 1);
595 (void)close_on_exec (SHIN
= dcopy(0, -1), 1);
596 (void)close_on_exec (SHOUT
= dcopy(1, -1), 1);
597 (void)close_on_exec (SHDIAG
= dcopy(2, -1), 1);
598 #ifndef CLOSE_ON_EXEC
600 #endif /* CLOSE_ON_EXEC */
602 cleanup_push(&state
, execash_cleanup
);
605 * Decrement the shell level, if not in a subshell
607 if (mainpid
== getpid())
611 #endif /* WINNT_NATIVE */
614 cleanup_until(&state
);
620 if (adrof(STRecho
)) {
621 int odidfds
= didfds
;
625 blkpr(t
), xputchar('\n');
634 dohash(Char
**vv
, struct command
*c
)
642 struct varent
*v
= adrof(STRpath
);
646 int is_windir
; /* check if it is the windows directory */
648 #endif /* WINNT_NATIVE */
653 uhashlength
= atoi(short2str(vv
[1]));
655 uhashwidth
= atoi(short2str(vv
[2]));
656 if ((uhashwidth
!= sizeof(unsigned char)) &&
657 (uhashwidth
!= sizeof(unsigned short)) &&
658 (uhashwidth
!= sizeof(unsigned long)))
661 hashdebug
= atoi(short2str(vv
[3]));
666 hashwidth
= uhashwidth
;
671 for (pv
= v
->vec
; pv
&& *pv
; pv
++, hashwidth
++)
673 if (hashwidth
<= widthof(unsigned char))
674 hashwidth
= sizeof(unsigned char);
675 else if (hashwidth
<= widthof(unsigned short))
676 hashwidth
= sizeof(unsigned short);
677 else if (hashwidth
<= widthof(unsigned int))
678 hashwidth
= sizeof(unsigned int);
680 hashwidth
= sizeof(unsigned long);
684 hashlength
= uhashlength
;
686 hashlength
= hashwidth
* (8*64);/* "average" files per dir in path */
689 xhash
= xcalloc(hashlength
* hashwidth
, 1);
690 #endif /* FASTHASH */
692 (void) getusername(NULL
); /* flush the tilde cashe */
697 for (pv
= v
->vec
; pv
&& *pv
; pv
++, i
++) {
698 if (!ABSOLUTEP(pv
[0]))
700 dirp
= opendir(short2str(*pv
));
703 cleanup_push(dirp
, opendir_cleanup
);
704 #ifdef COMMENT /* this isn't needed. opendir won't open
706 if (fstat(dirp
->dd_fd
, &stb
) < 0 || !S_ISDIR(stb
.st_mode
)) {
712 is_windir
= nt_check_if_windir(short2str(*pv
));
713 #endif /* WINNT_NATIVE */
714 while ((dp
= readdir(dirp
)) != NULL
) {
717 if (dp
->d_name
[0] == '.' &&
718 (dp
->d_name
[1] == '\0' ||
719 (dp
->d_name
[1] == '.' && dp
->d_name
[2] == '\0')))
722 nt_check_name_and_hash(is_windir
, dp
->d_name
, i
);
723 #else /* !WINNT_NATIVE*/
724 #if defined(_UWIN) || defined(__CYGWIN__)
725 /* Turn foo.{exe,com,bat} into foo since UWIN's readdir returns
726 * the file with the .exe, .com, .bat extension
728 * Same for Cygwin, but only for .exe and .com extension.
731 ssize_t ext
= strlen(dp
->d_name
) - 4;
732 if ((ext
> 0) && (strcasecmp(&dp
->d_name
[ext
], ".exe") == 0 ||
734 strcasecmp(&dp
->d_name
[ext
], ".bat") == 0 ||
736 strcasecmp(&dp
->d_name
[ext
], ".com") == 0)) {
738 /* Also store the variation with extension. */
739 hashval
= hashname(str2short(dp
->d_name
));
741 #endif /* __CYGWIN__ */
742 dp
->d_name
[ext
] = '\0';
745 #endif /* _UWIN || __CYGWIN__ */
747 hashval
= hashname(str2short(dp
->d_name
));
750 xprintf(CGETS(13, 1, "hash=%-4d dir=%-2d prog=%s\n"),
751 hashname(str2short(dp
->d_name
)), i
, dp
->d_name
);
752 # else /* OLD HASH */
753 hashval
= hash(hashname(str2short(dp
->d_name
)), i
);
755 # endif /* FASTHASH */
756 /* tw_add_comm_name (dp->d_name); */
757 #endif /* WINNT_NATIVE */
765 dounhash(Char
**v
, struct command
*c
)
773 #endif /* FASTHASH */
778 hashstat(Char
**v
, struct command
*c
)
783 if (havhash
&& hashlength
&& hashwidth
)
784 xprintf(CGETS(13, 2, "%d hash buckets of %d bits each\n"),
785 hashlength
, hashwidth
*8);
787 xprintf(CGETS(13, 3, "debug mask = 0x%08x\n"), hashdebug
);
788 #endif /* FASTHASH */
791 xprintf(CGETS(13, 4, "%d hits, %d misses, %d%%\n"),
792 hits
, misses
, 100 * hits
/ (hits
+ misses
));
798 * Hash a command name.
805 for (h
= 0; *cp
; cp
++)
811 iscommand(Char
*name
)
816 int slash
= any(short2str(name
), '/');
817 int hashval
, rehashed
, i
;
820 if (v
== NULL
|| v
->vec
== NULL
|| v
->vec
[0] == NULL
|| slash
)
824 sav
= Strspl(STRslash
, name
); /* / command name for postpending */
825 hashval
= havhash
? hashname(name
) : 0;
832 if (!slash
&& ABSOLUTEP(pv
[0]) && havhash
) {
834 if (!bit(hashval
, i
))
837 int hashval1
= hash(hashval
, i
);
838 if (!bit(xhash
, hashval1
))
840 #endif /* FASTHASH */
842 if (pv
[0][0] == 0 || eq(pv
[0], STRdot
)) { /* don't make ./xxx */
843 if (executable(NULL
, name
, 0)) {
849 if (executable(*pv
, sav
, 0)) {
858 if (adrof(STRautorehash
) && !rehashed
&& havhash
&& opv
!= justabs
) {
868 * Andreas Luik <luik@isaak.isa.de>
869 * I S A GmbH - Informationssysteme fuer computerintegrierte Automatisierung
873 * is the executable() routine below and changes to iscommand().
879 * executable() examines the pathname obtained by concatenating dir and name
880 * (dir may be NULL), and returns 1 either if it is executable by us, or
881 * if dir_ok is set and the pathname refers to a directory.
882 * This is a bit kludgy, but in the name of optimization...
885 executable(const Char
*dir
, const Char
*name
, int dir_ok
)
893 path
= Strspl(dir
, name
);
894 strname
= short2str(path
);
898 strname
= short2str(name
);
900 return (stat(strname
, &stbuf
) != -1 &&
901 ((dir_ok
&& S_ISDIR(stbuf
.st_mode
)) ||
902 (S_ISREG(stbuf
.st_mode
) &&
903 /* save time by not calling access() in the hopeless case */
904 (stbuf
.st_mode
& (S_IXOTH
| S_IXGRP
| S_IXUSR
)) &&
905 access(strname
, X_OK
) == 0
908 #endif /*!WINNT_NATIVE*/
910 struct tellmewhat_s0_cleanup
916 tellmewhat_s0_cleanup(void *xstate
)
918 struct tellmewhat_s0_cleanup
*state
;
921 *state
->dest
= state
->val
;
925 tellmewhat(struct wordent
*lexp
, Char
**str
)
927 struct tellmewhat_s0_cleanup s0
;
929 const struct biltins
*bptr
;
930 struct wordent
*sp
= lexp
->next
;
931 int aliased
= 0, found
;
935 if (adrof1(sp
->word
, &aliases
)) {
941 s0
.dest
= &sp
->word
; /* to get the memory freeing right... */
943 cleanup_push(&s0
, tellmewhat_s0_cleanup
);
945 /* handle quoted alias hack */
946 if ((*(sp
->word
) & (QUOTE
| TRIM
)) == QUOTE
)
949 /* do quoting, if it hasn't been done */
956 while (*s2
&& *s2
!= qc
)
957 *s1
++ = *s2
++ | QUOTE
;
963 *s1
++ = *s2
++ | QUOTE
;
970 for (bptr
= bfunc
; bptr
< &bfunc
[nbfunc
]; bptr
++) {
971 if (eq(sp
->word
, str2short(bptr
->bname
))) {
975 xprintf(CGETS(13, 5, "%" TCSH_S
": shell built-in command.\n"),
980 *str
= Strsave(sp
->word
);
986 for (bptr
= nt_bfunc
; bptr
< &nt_bfunc
[nt_nbfunc
]; bptr
++) {
987 if (eq(sp
->word
, str2short(bptr
->bname
))) {
991 xprintf(CGETS(13, 5, "%" TCSH_S
": shell built-in command.\n"),
996 *str
= Strsave(sp
->word
);
1001 #endif /* WINNT_NATIVE*/
1003 sp
->word
= cmd
= globone(sp
->word
, G_IGNORE
);
1004 cleanup_push(cmd
, xfree
);
1006 if ((i
= iscommand(sp
->word
)) != 0) {
1009 int slash
= any(short2str(sp
->word
), '/');
1012 if (v
== NULL
|| v
->vec
== NULL
|| v
->vec
[0] == NULL
|| slash
)
1018 if (pv
[0][0] == 0 || eq(pv
[0], STRdot
)) {
1020 sp
->word
= Strspl(STRdotsl
, sp
->word
);
1021 cleanup_push(sp
->word
, xfree
);
1023 cleanup_until(sp
->word
);
1029 s1
= Strspl(*pv
, STRslash
);
1030 sp
->word
= Strspl(s1
, sp
->word
);
1032 cleanup_push(sp
->word
, xfree
);
1036 *str
= Strsave(sp
->word
);
1037 cleanup_until(sp
->word
);
1045 xprintf(CGETS(13, 6, "%" TCSH_S
": Command not found.\n"),
1050 *str
= Strsave(sp
->word
);
1058 * Builtin to look at and list all places a command may be defined:
1059 * aliases, shell builtins, and the path.
1061 * Marc Horowitz <marc@mit.edu>
1062 * MIT Student Information Processing Board
1067 dowhere(Char
**v
, struct command
*c
)
1072 if (adrof(STRautorehash
))
1075 found
&= find_cmd(*v
, 1);
1076 /* Make status nonzero if any command is not found. */
1082 find_cmd(Char
*cmd
, int prt
)
1085 const struct biltins
*bptr
;
1088 int hashval
, rehashed
, i
, ex
, rval
= 0;
1090 if (prt
&& any(short2str(cmd
), '/')) {
1091 xprintf("%s", CGETS(13, 7, "where: / in command makes no sense\n"));
1095 /* first, look for an alias */
1097 if (prt
&& adrof1(cmd
, &aliases
)) {
1098 if ((var
= adrof1(cmd
, &aliases
)) != NULL
) {
1099 xprintf(CGETS(13, 8, "%" TCSH_S
" is aliased to "), cmd
);
1100 if (var
->vec
!= NULL
)
1107 /* next, look for a shell builtin */
1109 for (bptr
= bfunc
; bptr
< &bfunc
[nbfunc
]; bptr
++) {
1110 if (eq(cmd
, str2short(bptr
->bname
))) {
1113 xprintf(CGETS(13, 9, "%" TCSH_S
" is a shell built-in\n"), cmd
);
1119 for (bptr
= nt_bfunc
; bptr
< &nt_bfunc
[nt_nbfunc
]; bptr
++) {
1120 if (eq(cmd
, str2short(bptr
->bname
))) {
1123 xprintf(CGETS(13, 9, "%" TCSH_S
" is a shell built-in\n"), cmd
);
1128 #endif /* WINNT_NATIVE*/
1130 /* last, look through the path for the command */
1132 if ((var
= adrof(STRpath
)) == NULL
)
1135 hashval
= havhash
? hashname(cmd
) : 0;
1137 sv
= Strspl(STRslash
, cmd
);
1138 cleanup_push(sv
, xfree
);
1142 for (pv
= var
->vec
, i
= 0; pv
&& *pv
; pv
++, i
++) {
1143 if (havhash
&& !eq(*pv
, STRdot
)) {
1145 if (!bit(hashval
, i
))
1148 int hashval1
= hash(hashval
, i
);
1149 if (!bit(xhash
, hashval1
))
1151 #endif /* FASTHASH */
1153 ex
= executable(*pv
, sv
, 0);
1155 if (!ex
&& (hashdebug
& 2)) {
1156 xprintf("%s", CGETS(13, 10, "hash miss: "));
1157 ex
= 1; /* Force printing */
1159 #endif /* FASTHASH */
1163 xprintf("%" TCSH_S
"/", *pv
);
1164 xprintf("%" TCSH_S
"\n", cmd
);
1171 * If we are printing, we are being called from dowhere() which it
1172 * has rehashed already
1174 if (!prt
&& adrof(STRautorehash
) && !rehashed
&& havhash
) {
1183 int hashval_extern(cp
)
1186 return havhash
?hashname(cp
):0;
1188 int bit_extern(val
,i
)
1194 void bis_extern(val
,i
)
1200 #endif /* WINNT_NATIVE */