1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.exec.c,v 3.80 2014/07/11 14:57:55 christos Exp $ */
3 * sh.exec.c: Search, find, and execute a command!
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 RCSID("$tcsh: sh.exec.c,v 3.80 2014/07/11 14:57:55 christos Exp $")
41 #endif /*WINNT_NATIVE*/
48 # define FASTHASH /* Fast hashing is the default */
52 * System level search and execute of a command.
53 * We look in each directory for the specified command name.
54 * If the name contains a '/' then we execute only the full path name.
55 * If there is no search path then we execute only full path names.
59 * As we search for the command we note the first non-trivial error
60 * message for presentation to the user. This allows us often
61 * to show that a file has the wrong mode/no access when the file
62 * is not in the last component of the search path, so we must
63 * go on after first detecting the error.
65 static char *exerr
; /* Execution error message */
66 static Char
*expath
; /* Path for exerr */
69 * The two part hash function is designed to let texec() call the
70 * more expensive hashname() only once and the simple hash() several
71 * times (once for each path component checked).
72 * Byte size is assumed to be 8.
74 #define BITS_PER_BYTE 8
78 * xhash is an array of hash buckets which are used to hash execs. If
79 * it is allocated (havhash true), then to tell if ``name'' is
80 * (possibly) present in the i'th component of the variable path, look
81 * at the [hashname(name)] bucket of size [hashwidth] bytes, in the [i
82 * mod size*8]'th bit. The cache size is defaults to a length of 1024
83 * buckets, each 1 byte wide. This implementation guarantees that
84 * objects n bytes wide will be aligned on n byte boundaries.
88 static unsigned long *xhash
= NULL
;
89 static unsigned int hashlength
= 0, uhashlength
= 0;
90 static unsigned int hashwidth
= 0, uhashwidth
= 0;
91 static int hashdebug
= 0;
93 # define hash(a, b) (((a) * HSHMUL + (b)) % (hashlength))
94 # define widthof(t) (sizeof(t) * BITS_PER_BYTE)
95 # define tbit(f, i, t) (((t *) xhash)[(f)] & \
96 (1UL << (i & (widthof(t) - 1))))
97 # define tbis(f, i, t) (((t *) xhash)[(f)] |= \
98 (1UL << (i & (widthof(t) - 1))))
99 # define cbit(f, i) tbit(f, i, unsigned char)
100 # define cbis(f, i) tbis(f, i, unsigned char)
101 # define sbit(f, i) tbit(f, i, unsigned short)
102 # define sbis(f, i) tbis(f, i, unsigned short)
103 # define ibit(f, i) tbit(f, i, unsigned int)
104 # define ibis(f, i) tbis(f, i, unsigned int)
105 # define lbit(f, i) tbit(f, i, unsigned long)
106 # define lbis(f, i) tbis(f, i, unsigned long)
108 # define bit(f, i) (hashwidth==sizeof(unsigned char) ? cbit(f,i) : \
109 ((hashwidth==sizeof(unsigned short) ? sbit(f,i) : \
110 ((hashwidth==sizeof(unsigned int) ? ibit(f,i) : \
112 # define bis(f, i) (hashwidth==sizeof(unsigned char) ? cbis(f,i) : \
113 ((hashwidth==sizeof(unsigned short) ? sbis(f,i) : \
114 ((hashwidth==sizeof(unsigned int) ? ibis(f,i) : \
118 * Xhash is an array of HSHSIZ bits (HSHSIZ / 8 chars), which are used
119 * to hash execs. If it is allocated (havhash true), then to tell
120 * whether ``name'' is (possibly) present in the i'th component
121 * of the variable path, you look at the bit in xhash indexed by
122 * hash(hashname("name"), i). This is setup automatically
123 * after .login is executed, and recomputed whenever ``path'' is
126 # define HSHSIZ 8192 /* 1k bytes */
127 # define HSHMASK (HSHSIZ - 1)
129 static char xhash
[HSHSIZ
/ BITS_PER_BYTE
];
131 # define hash(a, b) (((a) * HSHMUL + (b)) & HSHMASK)
132 # define bit(h, b) ((h)[(b) >> 3] & 1 << ((b) & 7)) /* bit test */
133 # define bis(h, b) ((h)[(b) >> 3] |= 1 << ((b) & 7)) /* bit set */
135 #endif /* FASTHASH */
138 static int hits
, misses
;
141 /* Dummy search path for just absolute search when no path */
142 static Char
*justabs
[] = {STRNULL
, 0};
144 static void pexerr (void) __attribute__((__noreturn__
));
145 static void texec (Char
*, Char
**);
146 int hashname (Char
*);
147 static int iscommand (Char
*);
150 doexec(struct command
*t
, int do_glob
)
152 Char
*dp
, **pv
, **opv
, **av
, *sav
;
154 int slash
, gflag
, rehashed
;
159 * Glob the command name. We will search $path even if this does something,
160 * as in sh but not in csh. One special case: if there is no PATH, then we
161 * execute only commands which start with '/'.
163 blk
[0] = t
->t_dcom
[0];
169 pv
= globall(blk
, gflag
);
171 setname(short2str(blk
[0]));
172 stderror(ERR_NAME
| ERR_NOMATCH
);
177 cleanup_push(pv
, blk_cleanup
);
182 expath
= Strsave(pv
[0]);
188 if (v
== 0 && expath
[0] != '/' && expath
[0] != '.')
190 slash
= any(short2str(expath
), '/');
193 * Glob the argument list, if necessary. Otherwise trim off the quote bits.
200 av
= globall(av
, gflag
);
202 setname(short2str(expath
));
203 stderror(ERR_NAME
| ERR_NOMATCH
);
212 t
->t_dcom
= blkspl(pv
, av
);
218 if (*av
== NULL
|| **av
== '\0')
221 xechoit(av
); /* Echo command if -x */
224 * Since all internal file descriptors are set to close on exec, we don't
225 * need to close them explicitly here. Just reorient ourselves for error
232 isoutatty
= isatty(SHOUT
);
233 isdiagatty
= isatty(SHDIAG
);
235 closech(); /* Close random fd's */
238 * We must do this AFTER any possible forking (like `foo` in glob) so that
239 * this shell can still do subprocesses.
244 sigaddset(&set
, SIGINT
);
245 sigaddset(&set
, SIGCHLD
);
246 sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
252 * If no path, no words in path, or a / in the filename then restrict the
255 if (v
== NULL
|| v
->vec
== NULL
|| v
->vec
[0] == NULL
|| slash
)
259 sav
= Strspl(STRslash
, *av
);/* / command name for postpending */
261 cleanup_push(sav
, xfree
);
265 hashval
= havhash
? hashname(*av
) : 0;
276 * Try to save time by looking at the hash table for where this command
277 * could be. If we are doing delayed hashing, then we put the names in
278 * one at a time, as the user enters them. This is kinda like Korn
279 * Shell's "tracked aliases".
281 if (!slash
&& ABSOLUTEP(pv
[0]) && havhash
) {
283 if (!bit(hashval
, i
))
286 int hashval1
= hash(hashval
, i
);
287 if (!bit(xhash
, hashval1
))
289 #endif /* FASTHASH */
291 if (pv
[0][0] == 0 || eq(pv
[0], STRdot
)) /* don't make ./xxx */
294 dp
= Strspl(*pv
, sav
);
296 cleanup_push(dp
, xfree
);
319 if (adrof(STRautorehash
) && !rehashed
&& havhash
&& opv
!= justabs
) {
336 /* Couldn't find the damn thing */
338 setname(short2str(expath
));
348 stderror(ERR_NAME
| ERR_STRING
, exerr
);
349 stderror(ERR_NAME
| ERR_COMMAND
);
353 * Execute command f, arg list t.
354 * Record error message if not found.
355 * Also do shell scripts here.
358 texec(Char
*sf
, Char
**st
)
369 /* The order for the conversions is significant */
375 errno
= 0; /* don't use a previous error */
378 * If we try to execute an nfs mounted directory on the apollo, we
379 * hang forever. So until apollo fixes that..
383 if (stat(f
, &stb
) == 0 && S_ISDIR(stb
.st_mode
))
389 #ifdef ISC_POSIX_EXEC_BUG
390 __setostype(0); /* "0" is "__OS_SYSV" in <sys/user.h> */
391 #endif /* ISC_POSIX_EXEC_BUG */
393 #ifdef ISC_POSIX_EXEC_BUG
394 __setostype(1); /* "1" is "__OS_POSIX" in <sys/user.h> */
395 #endif /* ISC_POSIX_EXEC_BUG */
400 blkfree((Char
**) t
);
406 #endif /* WINNT_NATIVE */
408 * From: casper@fwi.uva.nl (Casper H.S. Dik) If we could not execute
409 * it, don't feed it to the shell if it looks like a binary!
411 if ((fd
= xopen(f
, O_RDONLY
|O_LARGEFILE
)) != -1) {
413 if ((nread
= xread(fd
, pref
, 2)) == 2) {
414 if (!isprint((unsigned char)pref
[0]) &&
415 (pref
[0] != '\n' && pref
[0] != '\t')) {
421 * We *know* what ENOEXEC means.
423 stderror(ERR_ARCH
, f
, strerror(err
));
426 else if (nread
< 0) {
432 /* need to print error incase the file is migrated */
433 stderror(ERR_SYSTEM
, f
, strerror(err
));
445 pref
[0] != '#' || pref
[1] != '!' || hashbang(fd
, &vp
) == -1) {
446 #endif /* HASHBANG */
448 * If there is an alias for shell, then put the words of the alias in
449 * front of the argument list replacing the command name. Note no
450 * interpretation of the words at this point.
452 v
= adrof1(STRshell
, &aliases
);
453 if (v
== NULL
|| v
->vec
== NULL
) {
455 vp
[0] = adrof(STRshell
) ? varval(STRshell
) : STR_SHELLPATH
;
459 # ifndef ISC /* Compatible with ISC's /bin/csh */
468 vp
= saveblk(v
->vec
);
471 #endif /* HASHBANG */
478 st
= blkspl(vp
, st
); /* Splice up the new arglst */
481 /* The order for the conversions is significant */
485 blkfree((Char
**) vp
);
489 #ifdef ISC_POSIX_EXEC_BUG
490 __setostype(0); /* "0" is "__OS_SYSV" in <sys/user.h> */
491 #endif /* ISC_POSIX_EXEC_BUG */
493 #ifdef ISC_POSIX_EXEC_BUG
494 __setostype(1); /* "1" is "__OS_POSIX" in <sys/user.h> */
495 #endif /* ISC_POSIX_EXEC_BUG */
499 blkfree((Char
**) t
);
500 /* The sky is falling, the sky is falling! */
501 stderror(ERR_SYSTEM
, f
, strerror(errno
));
505 stderror(ERR_SYSTEM
, f
, strerror(errno
));
509 case 0: /* execv fails and returns 0! */
516 exerr
= strerror(errno
);
518 expath
= Strsave(sf
);
529 int saveIN
, saveOUT
, saveDIAG
, saveSTD
;
530 int SHIN
, SHOUT
, SHDIAG
, OLDSTD
;
532 #ifndef CLOSE_ON_EXEC
535 struct sigaction sigint
, sigquit
, sigterm
;
539 execash_cleanup(void *xstate
)
541 struct execash_state
*state
;
544 sigaction(SIGINT
, &state
->sigint
, NULL
);
545 sigaction(SIGQUIT
, &state
->sigquit
, NULL
);
546 sigaction(SIGTERM
, &state
->sigterm
, NULL
);
549 #ifndef CLOSE_ON_EXEC
550 didcch
= state
->didcch
;
551 #endif /* CLOSE_ON_EXEC */
552 didfds
= state
->didfds
;
557 close_on_exec(SHIN
= dmove(state
->saveIN
, state
->SHIN
), 1);
558 close_on_exec(SHOUT
= dmove(state
->saveOUT
, state
->SHOUT
), 1);
559 close_on_exec(SHDIAG
= dmove(state
->saveDIAG
, state
->SHDIAG
), 1);
560 close_on_exec(OLDSTD
= dmove(state
->saveSTD
, state
->OLDSTD
), 1);
565 execash(Char
**t
, struct command
*kp
)
567 struct execash_state state
;
570 if (chkstop
== 0 && setintr
)
573 * Hmm, we don't really want to do that now because we might
574 * fail, but what is the choice
576 rechist(NULL
, adrof(STRsavehist
) != NULL
);
579 sigaction(SIGINT
, &parintr
, &state
.sigint
);
580 sigaction(SIGQUIT
, &parintr
, &state
.sigquit
);
581 sigaction(SIGTERM
, &parterm
, &state
.sigterm
);
583 state
.didfds
= didfds
;
584 #ifndef CLOSE_ON_EXEC
585 state
.didcch
= didcch
;
586 #endif /* CLOSE_ON_EXEC */
589 state
.SHDIAG
= SHDIAG
;
590 state
.OLDSTD
= OLDSTD
;
592 (void)close_on_exec (state
.saveIN
= dcopy(SHIN
, -1), 1);
593 (void)close_on_exec (state
.saveOUT
= dcopy(SHOUT
, -1), 1);
594 (void)close_on_exec (state
.saveDIAG
= dcopy(SHDIAG
, -1), 1);
595 (void)close_on_exec (state
.saveSTD
= dcopy(OLDSTD
, -1), 1);
597 lshift(kp
->t_dcom
, 1);
599 (void)close_on_exec (SHIN
= dcopy(0, -1), 1);
600 (void)close_on_exec (SHOUT
= dcopy(1, -1), 1);
601 (void)close_on_exec (SHDIAG
= dcopy(2, -1), 1);
602 #ifndef CLOSE_ON_EXEC
604 #endif /* CLOSE_ON_EXEC */
606 cleanup_push(&state
, execash_cleanup
);
609 * Decrement the shell level
614 #endif /* WINNT_NATIVE */
617 cleanup_until(&state
);
623 if (adrof(STRecho
)) {
624 int odidfds
= didfds
;
628 blkpr(t
), xputchar('\n');
637 dohash(Char
**vv
, struct command
*c
)
645 struct varent
*v
= adrof(STRpath
);
649 int is_windir
; /* check if it is the windows directory */
651 #endif /* WINNT_NATIVE */
656 uhashlength
= atoi(short2str(vv
[1]));
658 uhashwidth
= atoi(short2str(vv
[2]));
659 if ((uhashwidth
!= sizeof(unsigned char)) &&
660 (uhashwidth
!= sizeof(unsigned short)) &&
661 (uhashwidth
!= sizeof(unsigned long)))
664 hashdebug
= atoi(short2str(vv
[3]));
669 hashwidth
= uhashwidth
;
674 for (pv
= v
->vec
; pv
&& *pv
; pv
++, hashwidth
++)
676 if (hashwidth
<= widthof(unsigned char))
677 hashwidth
= sizeof(unsigned char);
678 else if (hashwidth
<= widthof(unsigned short))
679 hashwidth
= sizeof(unsigned short);
680 else if (hashwidth
<= widthof(unsigned int))
681 hashwidth
= sizeof(unsigned int);
683 hashwidth
= sizeof(unsigned long);
687 hashlength
= uhashlength
;
689 hashlength
= hashwidth
* (8*64);/* "average" files per dir in path */
692 xhash
= xcalloc(hashlength
* hashwidth
, 1);
693 #endif /* FASTHASH */
695 (void) getusername(NULL
); /* flush the tilde cashe */
700 for (pv
= v
->vec
; pv
&& *pv
; pv
++, i
++) {
701 if (!ABSOLUTEP(pv
[0]))
703 dirp
= opendir(short2str(*pv
));
706 cleanup_push(dirp
, opendir_cleanup
);
707 #ifdef COMMENT /* this isn't needed. opendir won't open
709 if (fstat(dirp
->dd_fd
, &stb
) < 0 || !S_ISDIR(stb
.st_mode
)) {
715 is_windir
= nt_check_if_windir(short2str(*pv
));
716 #endif /* WINNT_NATIVE */
717 while ((dp
= readdir(dirp
)) != NULL
) {
720 if (dp
->d_name
[0] == '.' &&
721 (dp
->d_name
[1] == '\0' ||
722 (dp
->d_name
[1] == '.' && dp
->d_name
[2] == '\0')))
725 nt_check_name_and_hash(is_windir
, dp
->d_name
, i
);
726 #else /* !WINNT_NATIVE*/
727 #if defined(_UWIN) || defined(__CYGWIN__)
728 /* Turn foo.{exe,com,bat} into foo since UWIN's readdir returns
729 * the file with the .exe, .com, .bat extension
731 * Same for Cygwin, but only for .exe and .com extension.
734 ssize_t ext
= strlen(dp
->d_name
) - 4;
735 if ((ext
> 0) && (strcasecmp(&dp
->d_name
[ext
], ".exe") == 0 ||
737 strcasecmp(&dp
->d_name
[ext
], ".bat") == 0 ||
739 strcasecmp(&dp
->d_name
[ext
], ".com") == 0)) {
741 /* Also store the variation with extension. */
742 hashval
= hashname(str2short(dp
->d_name
));
744 #endif /* __CYGWIN__ */
745 dp
->d_name
[ext
] = '\0';
748 #endif /* _UWIN || __CYGWIN__ */
750 hashval
= hashname(str2short(dp
->d_name
));
753 xprintf(CGETS(13, 1, "hash=%-4d dir=%-2d prog=%s\n"),
754 hashname(str2short(dp
->d_name
)), i
, dp
->d_name
);
755 # else /* OLD HASH */
756 hashval
= hash(hashname(str2short(dp
->d_name
)), i
);
758 # endif /* FASTHASH */
759 /* tw_add_comm_name (dp->d_name); */
760 #endif /* WINNT_NATIVE */
768 dounhash(Char
**v
, struct command
*c
)
776 #endif /* FASTHASH */
781 hashstat(Char
**v
, struct command
*c
)
786 if (havhash
&& hashlength
&& hashwidth
)
787 xprintf(CGETS(13, 2, "%d hash buckets of %d bits each\n"),
788 hashlength
, hashwidth
*8);
790 xprintf(CGETS(13, 3, "debug mask = 0x%08x\n"), hashdebug
);
791 #endif /* FASTHASH */
794 xprintf(CGETS(13, 4, "%d hits, %d misses, %d%%\n"),
795 hits
, misses
, 100 * hits
/ (hits
+ misses
));
801 * Hash a command name.
808 for (h
= 0; *cp
; cp
++)
814 iscommand(Char
*name
)
819 int slash
= any(short2str(name
), '/');
820 int hashval
, rehashed
, i
;
823 if (v
== NULL
|| v
->vec
== NULL
|| v
->vec
[0] == NULL
|| slash
)
827 sav
= Strspl(STRslash
, name
); /* / command name for postpending */
828 hashval
= havhash
? hashname(name
) : 0;
835 if (!slash
&& ABSOLUTEP(pv
[0]) && havhash
) {
837 if (!bit(hashval
, i
))
840 int hashval1
= hash(hashval
, i
);
841 if (!bit(xhash
, hashval1
))
843 #endif /* FASTHASH */
845 if (pv
[0][0] == 0 || eq(pv
[0], STRdot
)) { /* don't make ./xxx */
846 if (executable(NULL
, name
, 0)) {
852 if (executable(*pv
, sav
, 0)) {
861 if (adrof(STRautorehash
) && !rehashed
&& havhash
&& opv
!= justabs
) {
871 * Andreas Luik <luik@isaak.isa.de>
872 * I S A GmbH - Informationssysteme fuer computerintegrierte Automatisierung
876 * is the executable() routine below and changes to iscommand().
882 * executable() examines the pathname obtained by concatenating dir and name
883 * (dir may be NULL), and returns 1 either if it is executable by us, or
884 * if dir_ok is set and the pathname refers to a directory.
885 * This is a bit kludgy, but in the name of optimization...
888 executable(const Char
*dir
, const Char
*name
, int dir_ok
)
896 path
= Strspl(dir
, name
);
897 strname
= short2str(path
);
901 strname
= short2str(name
);
903 return (stat(strname
, &stbuf
) != -1 &&
904 ((dir_ok
&& S_ISDIR(stbuf
.st_mode
)) ||
905 (S_ISREG(stbuf
.st_mode
) &&
906 /* save time by not calling access() in the hopeless case */
907 (stbuf
.st_mode
& (S_IXOTH
| S_IXGRP
| S_IXUSR
)) &&
908 access(strname
, X_OK
) == 0
911 #endif /*!WINNT_NATIVE*/
913 struct tellmewhat_s0_cleanup
919 tellmewhat_s0_cleanup(void *xstate
)
921 struct tellmewhat_s0_cleanup
*state
;
924 *state
->dest
= state
->val
;
928 tellmewhat(struct wordent
*lexp
, Char
**str
)
930 struct tellmewhat_s0_cleanup s0
;
932 const struct biltins
*bptr
;
933 struct wordent
*sp
= lexp
->next
;
934 int aliased
= 0, found
;
938 if (adrof1(sp
->word
, &aliases
)) {
944 s0
.dest
= &sp
->word
; /* to get the memory freeing right... */
946 cleanup_push(&s0
, tellmewhat_s0_cleanup
);
948 /* handle quoted alias hack */
949 if ((*(sp
->word
) & (QUOTE
| TRIM
)) == QUOTE
)
952 /* do quoting, if it hasn't been done */
959 while (*s2
&& *s2
!= qc
)
960 *s1
++ = *s2
++ | QUOTE
;
966 *s1
++ = *s2
++ | QUOTE
;
973 for (bptr
= bfunc
; bptr
< &bfunc
[nbfunc
]; bptr
++) {
974 if (eq(sp
->word
, str2short(bptr
->bname
))) {
978 xprintf(CGETS(13, 5, "%S: shell built-in command.\n"),
983 *str
= Strsave(sp
->word
);
989 for (bptr
= nt_bfunc
; bptr
< &nt_bfunc
[nt_nbfunc
]; bptr
++) {
990 if (eq(sp
->word
, str2short(bptr
->bname
))) {
994 xprintf(CGETS(13, 5, "%S: shell built-in command.\n"),
999 *str
= Strsave(sp
->word
);
1004 #endif /* WINNT_NATIVE*/
1006 sp
->word
= cmd
= globone(sp
->word
, G_IGNORE
);
1007 cleanup_push(cmd
, xfree
);
1009 if ((i
= iscommand(sp
->word
)) != 0) {
1012 int slash
= any(short2str(sp
->word
), '/');
1015 if (v
== NULL
|| v
->vec
== NULL
|| v
->vec
[0] == NULL
|| slash
)
1021 if (pv
[0][0] == 0 || eq(pv
[0], STRdot
)) {
1023 sp
->word
= Strspl(STRdotsl
, sp
->word
);
1024 cleanup_push(sp
->word
, xfree
);
1026 cleanup_until(sp
->word
);
1032 s1
= Strspl(*pv
, STRslash
);
1033 sp
->word
= Strspl(s1
, sp
->word
);
1035 cleanup_push(sp
->word
, xfree
);
1039 *str
= Strsave(sp
->word
);
1040 cleanup_until(sp
->word
);
1048 xprintf(CGETS(13, 6, "%S: Command not found.\n"), sp
->word
);
1052 *str
= Strsave(sp
->word
);
1060 * Builtin to look at and list all places a command may be defined:
1061 * aliases, shell builtins, and the path.
1063 * Marc Horowitz <marc@mit.edu>
1064 * MIT Student Information Processing Board
1069 dowhere(Char
**v
, struct command
*c
)
1074 if (adrof(STRautorehash
))
1077 found
&= find_cmd(*v
, 1);
1078 /* Make status nonzero if any command is not found. */
1080 setcopy(STRstatus
, STR1
, VAR_READWRITE
);
1084 find_cmd(Char
*cmd
, int prt
)
1087 const struct biltins
*bptr
;
1090 int hashval
, rehashed
, i
, ex
, rval
= 0;
1092 if (prt
&& any(short2str(cmd
), '/')) {
1093 xprintf("%s", CGETS(13, 7, "where: / in command makes no sense\n"));
1097 /* first, look for an alias */
1099 if (prt
&& adrof1(cmd
, &aliases
)) {
1100 if ((var
= adrof1(cmd
, &aliases
)) != NULL
) {
1101 xprintf(CGETS(13, 8, "%S is aliased to "), cmd
);
1102 if (var
->vec
!= NULL
)
1109 /* next, look for a shell builtin */
1111 for (bptr
= bfunc
; bptr
< &bfunc
[nbfunc
]; bptr
++) {
1112 if (eq(cmd
, str2short(bptr
->bname
))) {
1115 xprintf(CGETS(13, 9, "%S is a shell built-in\n"), cmd
);
1121 for (bptr
= nt_bfunc
; bptr
< &nt_bfunc
[nt_nbfunc
]; bptr
++) {
1122 if (eq(cmd
, str2short(bptr
->bname
))) {
1125 xprintf(CGETS(13, 9, "%S is a shell built-in\n"), cmd
);
1130 #endif /* WINNT_NATIVE*/
1132 /* last, look through the path for the command */
1134 if ((var
= adrof(STRpath
)) == NULL
)
1137 hashval
= havhash
? hashname(cmd
) : 0;
1139 sv
= Strspl(STRslash
, cmd
);
1140 cleanup_push(sv
, xfree
);
1144 for (pv
= var
->vec
, i
= 0; pv
&& *pv
; pv
++, i
++) {
1145 if (havhash
&& !eq(*pv
, STRdot
)) {
1147 if (!bit(hashval
, i
))
1150 int hashval1
= hash(hashval
, i
);
1151 if (!bit(xhash
, hashval1
))
1153 #endif /* FASTHASH */
1155 ex
= executable(*pv
, sv
, 0);
1157 if (!ex
&& (hashdebug
& 2)) {
1158 xprintf("%s", CGETS(13, 10, "hash miss: "));
1159 ex
= 1; /* Force printing */
1161 #endif /* FASTHASH */
1165 xprintf("%S/", *pv
);
1166 xprintf("%S\n", cmd
);
1173 * If we are printing, we are being called from dowhere() which it
1174 * has rehashed already
1176 if (!prt
&& adrof(STRautorehash
) && !rehashed
&& havhash
) {
1185 int hashval_extern(cp
)
1188 return havhash
?hashname(cp
):0;
1190 int bit_extern(val
,i
)
1196 void bis_extern(val
,i
)
1202 #endif /* WINNT_NATIVE */