Import tcsh-6.17.00
[dragonfly.git] / contrib / tcsh-6 / sh.exec.c
CommitLineData
57e3f2b5 1/* $Header: /p/tcsh/cvsroot/tcsh/sh.exec.c,v 3.75 2009/06/25 21:15:37 christos Exp $ */
7d8fb588
MS
2/*
3 * sh.exec.c: Search, find, and execute a command!
4 */
5/*-
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 */
33#include "sh.h"
34
57e3f2b5 35RCSID("$tcsh: sh.exec.c,v 3.75 2009/06/25 21:15:37 christos Exp $")
7d8fb588
MS
36
37#include "tc.h"
38#include "tw.h"
39#ifdef WINNT_NATIVE
40#include <nt.const.h>
41#endif /*WINNT_NATIVE*/
42
43/*
44 * C shell
45 */
46
47#ifndef OLDHASH
48# define FASTHASH /* Fast hashing is the default */
49#endif /* OLDHASH */
50
51/*
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.
56 */
57
58/*
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.
64 */
65static char *exerr; /* Execution error message */
66static Char *expath; /* Path for exerr */
67
68/*
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.
73 */
74#define BITS_PER_BYTE 8
75
76#ifdef FASTHASH
77/*
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) presend 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.
85 */
86# define HSHMUL 241
87
88static unsigned long *xhash = NULL;
89static unsigned int hashlength = 0, uhashlength = 0;
90static unsigned int hashwidth = 0, uhashwidth = 0;
91static int hashdebug = 0;
92
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)
107
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) : \
111 lbit(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) : \
115 lbis(f,i))))))
116#else /* OLDHASH */
117/*
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
124 * changed.
125 */
126# define HSHSIZ 8192 /* 1k bytes */
127# define HSHMASK (HSHSIZ - 1)
128# define HSHMUL 243
129static char xhash[HSHSIZ / BITS_PER_BYTE];
130
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 */
134
135#endif /* FASTHASH */
136
137#ifdef VFORK
138static int hits, misses;
139#endif /* VFORK */
140
141/* Dummy search path for just absolute search when no path */
142static Char *justabs[] = {STRNULL, 0};
143
144static void pexerr (void);
145static void texec (Char *, Char **);
146int hashname (Char *);
147static int iscommand (Char *);
148
149void
150doexec(struct command *t, int do_glob)
151{
152 Char *dp, **pv, **av, *sav;
153 struct varent *v;
154 int slash, gflag;
155 int hashval, i;
156 Char *blk[2];
157
158 /*
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 '/'.
162 */
163 blk[0] = t->t_dcom[0];
164 blk[1] = 0;
165 gflag = 0;
166 if (do_glob)
167 gflag = tglob(blk);
168 if (gflag) {
169 pv = globall(blk, gflag);
170 if (pv == 0) {
171 setname(short2str(blk[0]));
172 stderror(ERR_NAME | ERR_NOMATCH);
173 }
174 }
175 else
176 pv = saveblk(blk);
177 cleanup_push(pv, blk_cleanup);
178
179 trim(pv);
180
181 exerr = 0;
182 expath = Strsave(pv[0]);
183#ifdef VFORK
184 Vexpath = expath;
185#endif /* VFORK */
186
187 v = adrof(STRpath);
188 if (v == 0 && expath[0] != '/' && expath[0] != '.')
189 pexerr();
190 slash = any(short2str(expath), '/');
191
192 /*
193 * Glob the argument list, if necessary. Otherwise trim off the quote bits.
194 */
195 gflag = 0;
196 av = &t->t_dcom[1];
197 if (do_glob)
198 gflag = tglob(av);
199 if (gflag) {
200 av = globall(av, gflag);
201 if (av == 0) {
202 setname(short2str(expath));
203 stderror(ERR_NAME | ERR_NOMATCH);
204 }
205 }
206 else
207 av = saveblk(av);
208
209 blkfree(t->t_dcom);
210 cleanup_ignore(pv);
211 cleanup_until(pv);
212 t->t_dcom = blkspl(pv, av);
213 xfree(pv);
214 xfree(av);
215 av = t->t_dcom;
216 trim(av);
217
218 if (*av == NULL || **av == '\0')
219 pexerr();
220
221 xechoit(av); /* Echo command if -x */
222#ifdef CLOSE_ON_EXEC
223 /*
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
226 * messages.
227 */
228 SHIN = 0;
229 SHOUT = 1;
230 SHDIAG = 2;
231 OLDSTD = 0;
232 isoutatty = isatty(SHOUT);
233 isdiagatty = isatty(SHDIAG);
234#else
235 closech(); /* Close random fd's */
236#endif
237 /*
238 * We must do this AFTER any possible forking (like `foo` in glob) so that
239 * this shell can still do subprocesses.
240 */
241 {
242 sigset_t set;
243 sigemptyset(&set);
244 sigaddset(&set, SIGINT);
245 sigaddset(&set, SIGCHLD);
246 sigprocmask(SIG_UNBLOCK, &set, NULL);
247 }
248 pintr_disabled = 0;
249 pchild_disabled = 0;
250
251 /*
252 * If no path, no words in path, or a / in the filename then restrict the
253 * command search.
254 */
255 if (v == NULL || v->vec == NULL || v->vec[0] == NULL || slash)
256 pv = justabs;
257 else
258 pv = v->vec;
259 sav = Strspl(STRslash, *av);/* / command name for postpending */
260#ifndef VFORK
261 cleanup_push(sav, xfree);
262#else /* VFORK */
263 Vsav = sav;
264#endif /* VFORK */
265 hashval = havhash ? hashname(*av) : 0;
266
267 i = 0;
268#ifdef VFORK
269 hits++;
270#endif /* VFORK */
271 do {
272 /*
273 * Try to save time by looking at the hash table for where this command
274 * could be. If we are doing delayed hashing, then we put the names in
275 * one at a time, as the user enters them. This is kinda like Korn
276 * Shell's "tracked aliases".
277 */
278 if (!slash && ABSOLUTEP(pv[0]) && havhash) {
279#ifdef FASTHASH
280 if (!bit(hashval, i))
281 goto cont;
282#else /* OLDHASH */
283 int hashval1 = hash(hashval, i);
284 if (!bit(xhash, hashval1))
285 goto cont;
286#endif /* FASTHASH */
287 }
288 if (pv[0][0] == 0 || eq(pv[0], STRdot)) /* don't make ./xxx */
289 texec(*av, av);
290 else {
291 dp = Strspl(*pv, sav);
292#ifndef VFORK
293 cleanup_push(dp, xfree);
294#else /* VFORK */
295 Vdp = dp;
296#endif /* VFORK */
297
298 texec(dp, av);
299#ifndef VFORK
300 cleanup_until(dp);
301#else /* VFORK */
302 Vdp = 0;
303 xfree(dp);
304#endif /* VFORK */
305 }
306#ifdef VFORK
307 misses++;
308#endif /* VFORK */
309cont:
310 pv++;
311 i++;
312 } while (*pv);
313#ifdef VFORK
314 hits--;
315#endif /* VFORK */
316#ifndef VFORK
317 cleanup_until(sav);
318#else /* VFORK */
319 Vsav = 0;
320 xfree(sav);
321#endif /* VFORK */
322 pexerr();
323}
324
325static void
326pexerr(void)
327{
328 /* Couldn't find the damn thing */
329 if (expath) {
330 setname(short2str(expath));
331#ifdef VFORK
332 Vexpath = 0;
333#endif /* VFORK */
334 xfree(expath);
335 expath = 0;
336 }
337 else
338 setname("");
339 if (exerr)
340 stderror(ERR_NAME | ERR_STRING, exerr);
341 stderror(ERR_NAME | ERR_COMMAND);
342}
343
344/*
345 * Execute command f, arg list t.
346 * Record error message if not found.
347 * Also do shell scripts here.
348 */
349static void
350texec(Char *sf, Char **st)
351{
352 char **t;
353 char *f;
354 struct varent *v;
355 Char **vp;
356 Char *lastsh[2];
357 char pref[2];
358 int fd;
359 Char *st0, **ost;
360
361 /* The order for the conversions is significant */
362 t = short2blk(st);
363 f = short2str(sf);
364#ifdef VFORK
365 Vt = t;
366#endif /* VFORK */
367 errno = 0; /* don't use a previous error */
368#ifdef apollo
369 /*
370 * If we try to execute an nfs mounted directory on the apollo, we
371 * hang forever. So until apollo fixes that..
372 */
373 {
374 struct stat stb;
375 if (stat(f, &stb) == 0 && S_ISDIR(stb.st_mode))
376 errno = EISDIR;
377 }
378 if (errno == 0)
379#endif /* apollo */
380 {
381#ifdef ISC_POSIX_EXEC_BUG
382 __setostype(0); /* "0" is "__OS_SYSV" in <sys/user.h> */
383#endif /* ISC_POSIX_EXEC_BUG */
384 (void) execv(f, t);
385#ifdef ISC_POSIX_EXEC_BUG
386 __setostype(1); /* "1" is "__OS_POSIX" in <sys/user.h> */
387#endif /* ISC_POSIX_EXEC_BUG */
388 }
389#ifdef VFORK
390 Vt = 0;
391#endif /* VFORK */
392 blkfree((Char **) t);
393 switch (errno) {
394
395 case ENOEXEC:
396#ifdef WINNT_NATIVE
397 nt_feed_to_cmd(f,t);
398#endif /* WINNT_NATIVE */
399 /*
400 * From: casper@fwi.uva.nl (Casper H.S. Dik) If we could not execute
401 * it, don't feed it to the shell if it looks like a binary!
402 */
403 if ((fd = xopen(f, O_RDONLY|O_LARGEFILE)) != -1) {
404 int nread;
7d8fb588
MS
405 if ((nread = xread(fd, pref, 2)) == 2) {
406 if (!isprint((unsigned char)pref[0]) &&
407 (pref[0] != '\n' && pref[0] != '\t')) {
408 int err;
409
410 err = errno;
411 xclose(fd);
412 /*
413 * We *know* what ENOEXEC means.
414 */
415 stderror(ERR_ARCH, f, strerror(err));
416 }
417 }
418 else if (nread < 0) {
419#ifdef convex
420 int err;
421
422 err = errno;
423 xclose(fd);
424 /* need to print error incase the file is migrated */
425 stderror(ERR_SYSTEM, f, strerror(err));
426#endif
427 }
428#ifdef _PATH_BSHELL
429 else {
430 pref[0] = '#';
431 pref[1] = '\0';
432 }
433#endif
434 }
435#ifdef HASHBANG
436 if (fd == -1 ||
437 pref[0] != '#' || pref[1] != '!' || hashbang(fd, &vp) == -1) {
438#endif /* HASHBANG */
439 /*
440 * If there is an alias for shell, then put the words of the alias in
441 * front of the argument list replacing the command name. Note no
442 * interpretation of the words at this point.
443 */
444 v = adrof1(STRshell, &aliases);
445 if (v == NULL || v->vec == NULL) {
446 vp = lastsh;
447 vp[0] = adrof(STRshell) ? varval(STRshell) : STR_SHELLPATH;
448 vp[1] = NULL;
449#ifdef _PATH_BSHELL
450 if (fd != -1
451# ifndef ISC /* Compatible with ISC's /bin/csh */
452 && pref[0] != '#'
453# endif /* ISC */
454 )
455 vp[0] = STR_BSHELL;
456#endif
457 vp = saveblk(vp);
458 }
459 else
460 vp = saveblk(v->vec);
461#ifdef HASHBANG
462 }
463#endif /* HASHBANG */
464 if (fd != -1)
465 xclose(fd);
466
467 st0 = st[0];
468 st[0] = sf;
469 ost = st;
470 st = blkspl(vp, st); /* Splice up the new arglst */
471 ost[0] = st0;
472 sf = *st;
473 /* The order for the conversions is significant */
474 t = short2blk(st);
475 f = short2str(sf);
476 xfree(st);
477 blkfree((Char **) vp);
478#ifdef VFORK
479 Vt = t;
480#endif /* VFORK */
481#ifdef ISC_POSIX_EXEC_BUG
482 __setostype(0); /* "0" is "__OS_SYSV" in <sys/user.h> */
483#endif /* ISC_POSIX_EXEC_BUG */
484 (void) execv(f, t);
485#ifdef ISC_POSIX_EXEC_BUG
486 __setostype(1); /* "1" is "__OS_POSIX" in <sys/user.h> */
487#endif /* ISC_POSIX_EXEC_BUG */
488#ifdef VFORK
489 Vt = 0;
490#endif /* VFORK */
491 blkfree((Char **) t);
492 /* The sky is falling, the sky is falling! */
493 stderror(ERR_SYSTEM, f, strerror(errno));
494 break;
495
496 case ENOMEM:
497 stderror(ERR_SYSTEM, f, strerror(errno));
498 break;
499
500#ifdef _IBMR2
501 case 0: /* execv fails and returns 0! */
502#endif /* _IBMR2 */
503 case ENOENT:
504 break;
505
506 default:
507 if (exerr == 0) {
508 exerr = strerror(errno);
509 xfree(expath);
510 expath = Strsave(sf);
511#ifdef VFORK
512 Vexpath = expath;
513#endif /* VFORK */
514 }
515 break;
516 }
517}
518
519struct execash_state
520{
521 int saveIN, saveOUT, saveDIAG, saveSTD;
522 int SHIN, SHOUT, SHDIAG, OLDSTD;
523 int didfds;
524#ifndef CLOSE_ON_EXEC
525 int didcch;
526#endif
527 struct sigaction sigint, sigquit, sigterm;
528};
529
530static void
531execash_cleanup(void *xstate)
532{
533 struct execash_state *state;
534
535 state = xstate;
536 sigaction(SIGINT, &state->sigint, NULL);
537 sigaction(SIGQUIT, &state->sigquit, NULL);
538 sigaction(SIGTERM, &state->sigterm, NULL);
539
540 doneinp = 0;
541#ifndef CLOSE_ON_EXEC
542 didcch = state->didcch;
543#endif /* CLOSE_ON_EXEC */
544 didfds = state->didfds;
545 xclose(SHIN);
546 xclose(SHOUT);
547 xclose(SHDIAG);
548 xclose(OLDSTD);
549 close_on_exec(SHIN = dmove(state->saveIN, state->SHIN), 1);
550 close_on_exec(SHOUT = dmove(state->saveOUT, state->SHOUT), 1);
551 close_on_exec(SHDIAG = dmove(state->saveDIAG, state->SHDIAG), 1);
552 close_on_exec(OLDSTD = dmove(state->saveSTD, state->OLDSTD), 1);
553}
554
555/*ARGSUSED*/
556void
557execash(Char **t, struct command *kp)
558{
559 struct execash_state state;
560
561 USE(t);
562 if (chkstop == 0 && setintr)
563 panystop(0);
564 /*
565 * Hmm, we don't really want to do that now because we might
566 * fail, but what is the choice
567 */
568 rechist(NULL, adrof(STRsavehist) != NULL);
569
570
571 sigaction(SIGINT, &parintr, &state.sigint);
572 sigaction(SIGQUIT, &parintr, &state.sigquit);
573 sigaction(SIGTERM, &parterm, &state.sigterm);
574
575 state.didfds = didfds;
576#ifndef CLOSE_ON_EXEC
577 state.didcch = didcch;
578#endif /* CLOSE_ON_EXEC */
579 state.SHIN = SHIN;
580 state.SHOUT = SHOUT;
581 state.SHDIAG = SHDIAG;
582 state.OLDSTD = OLDSTD;
583
584 (void)close_on_exec (state.saveIN = dcopy(SHIN, -1), 1);
585 (void)close_on_exec (state.saveOUT = dcopy(SHOUT, -1), 1);
586 (void)close_on_exec (state.saveDIAG = dcopy(SHDIAG, -1), 1);
587 (void)close_on_exec (state.saveSTD = dcopy(OLDSTD, -1), 1);
588
589 lshift(kp->t_dcom, 1);
590
591 (void)close_on_exec (SHIN = dcopy(0, -1), 1);
592 (void)close_on_exec (SHOUT = dcopy(1, -1), 1);
593 (void)close_on_exec (SHDIAG = dcopy(2, -1), 1);
594#ifndef CLOSE_ON_EXEC
595 didcch = 0;
596#endif /* CLOSE_ON_EXEC */
597 didfds = 0;
598 cleanup_push(&state, execash_cleanup);
599
600 /*
601 * Decrement the shell level
602 */
603 shlvl(-1);
604#ifdef WINNT_NATIVE
605 __nt_really_exec=1;
606#endif /* WINNT_NATIVE */
607 doexec(kp, 1);
608
609 cleanup_until(&state);
610}
611
612void
613xechoit(Char **t)
614{
615 if (adrof(STRecho)) {
616 int odidfds = didfds;
617 flush();
618 haderr = 1;
619 didfds = 0;
620 blkpr(t), xputchar('\n');
621 flush();
622 didfds = odidfds;
623 haderr = 0;
624 }
625}
626
627/*ARGSUSED*/
628void
629dohash(Char **vv, struct command *c)
630{
631#ifdef COMMENT
632 struct stat stb;
633#endif
634 DIR *dirp;
635 struct dirent *dp;
636 int i = 0;
637 struct varent *v = adrof(STRpath);
638 Char **pv;
639 int hashval;
640#ifdef WINNT_NATIVE
641 int is_windir; /* check if it is the windows directory */
642 USE(hashval);
643#endif /* WINNT_NATIVE */
644
645 USE(c);
646#ifdef FASTHASH
647 if (vv && vv[1]) {
648 uhashlength = atoi(short2str(vv[1]));
649 if (vv[2]) {
650 uhashwidth = atoi(short2str(vv[2]));
651 if ((uhashwidth != sizeof(unsigned char)) &&
652 (uhashwidth != sizeof(unsigned short)) &&
653 (uhashwidth != sizeof(unsigned long)))
654 uhashwidth = 0;
655 if (vv[3])
656 hashdebug = atoi(short2str(vv[3]));
657 }
658 }
659
660 if (uhashwidth)
661 hashwidth = uhashwidth;
662 else {
663 hashwidth = 0;
664 if (v == NULL)
665 return;
666 for (pv = v->vec; pv && *pv; pv++, hashwidth++)
667 continue;
668 if (hashwidth <= widthof(unsigned char))
669 hashwidth = sizeof(unsigned char);
670 else if (hashwidth <= widthof(unsigned short))
671 hashwidth = sizeof(unsigned short);
672 else if (hashwidth <= widthof(unsigned int))
673 hashwidth = sizeof(unsigned int);
674 else
675 hashwidth = sizeof(unsigned long);
676 }
677
678 if (uhashlength)
679 hashlength = uhashlength;
680 else
681 hashlength = hashwidth * (8*64);/* "average" files per dir in path */
682
683 xfree(xhash);
684 xhash = xcalloc(hashlength * hashwidth, 1);
685#endif /* FASTHASH */
686
687 (void) getusername(NULL); /* flush the tilde cashe */
688 tw_cmd_free();
689 havhash = 1;
690 if (v == NULL)
691 return;
692 for (pv = v->vec; pv && *pv; pv++, i++) {
693 if (!ABSOLUTEP(pv[0]))
694 continue;
695 dirp = opendir(short2str(*pv));
696 if (dirp == NULL)
697 continue;
698 cleanup_push(dirp, opendir_cleanup);
699#ifdef COMMENT /* this isn't needed. opendir won't open
700 * non-dirs */
701 if (fstat(dirp->dd_fd, &stb) < 0 || !S_ISDIR(stb.st_mode)) {
702 cleanup_until(dirp);
703 continue;
704 }
705#endif
706#ifdef WINNT_NATIVE
707 is_windir = nt_check_if_windir(short2str(*pv));
708#endif /* WINNT_NATIVE */
709 while ((dp = readdir(dirp)) != NULL) {
710 if (dp->d_ino == 0)
711 continue;
712 if (dp->d_name[0] == '.' &&
713 (dp->d_name[1] == '\0' ||
714 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
715 continue;
716#ifdef WINNT_NATIVE
717 nt_check_name_and_hash(is_windir, dp->d_name, i);
718#else /* !WINNT_NATIVE*/
719#if defined(_UWIN) || defined(__CYGWIN__)
720 /* Turn foo.{exe,com,bat} into foo since UWIN's readdir returns
721 * the file with the .exe, .com, .bat extension
722 */
723 {
724 ssize_t ext = strlen(dp->d_name) - 4;
725 if ((ext > 0) && (strcasecmp(&dp->d_name[ext], ".exe") == 0 ||
726 strcasecmp(&dp->d_name[ext], ".bat") == 0 ||
727 strcasecmp(&dp->d_name[ext], ".com") == 0)) {
728#ifdef __CYGWIN__
729 /* Also store the variation with extension. */
730 hashval = hashname(str2short(dp->d_name));
731 bis(hashval, i);
732#endif /* __CYGWIN__ */
733 dp->d_name[ext] = '\0';
734 }
735 }
736#endif /* _UWIN || __CYGWIN__ */
737# ifdef FASTHASH
738 hashval = hashname(str2short(dp->d_name));
739 bis(hashval, i);
740 if (hashdebug & 1)
741 xprintf(CGETS(13, 1, "hash=%-4d dir=%-2d prog=%s\n"),
742 hashname(str2short(dp->d_name)), i, dp->d_name);
743# else /* OLD HASH */
744 hashval = hash(hashname(str2short(dp->d_name)), i);
745 bis(xhash, hashval);
746# endif /* FASTHASH */
747 /* tw_add_comm_name (dp->d_name); */
748#endif /* WINNT_NATIVE */
749 }
750 cleanup_until(dirp);
751 }
752}
753
754/*ARGSUSED*/
755void
756dounhash(Char **v, struct command *c)
757{
758 USE(c);
759 USE(v);
760 havhash = 0;
761#ifdef FASTHASH
762 xfree(xhash);
763 xhash = NULL;
764#endif /* FASTHASH */
765}
766
767/*ARGSUSED*/
768void
769hashstat(Char **v, struct command *c)
770{
771 USE(c);
772 USE(v);
773#ifdef FASTHASH
774 if (havhash && hashlength && hashwidth)
775 xprintf(CGETS(13, 2, "%d hash buckets of %d bits each\n"),
776 hashlength, hashwidth*8);
777 if (hashdebug)
778 xprintf(CGETS(13, 3, "debug mask = 0x%08x\n"), hashdebug);
779#endif /* FASTHASH */
780#ifdef VFORK
781 if (hits + misses)
782 xprintf(CGETS(13, 4, "%d hits, %d misses, %d%%\n"),
783 hits, misses, 100 * hits / (hits + misses));
784#endif
785}
786
787
788/*
789 * Hash a command name.
790 */
791int
792hashname(Char *cp)
793{
794 unsigned long h;
795
796 for (h = 0; *cp; cp++)
797 h = hash(h, *cp);
798 return ((int) h);
799}
800
801static int
802iscommand(Char *name)
803{
804 Char **pv;
805 Char *sav;
806 struct varent *v;
807 int slash = any(short2str(name), '/');
808 int hashval, i;
809
810 v = adrof(STRpath);
811 if (v == NULL || v->vec == NULL || v->vec[0] == NULL || slash)
812 pv = justabs;
813 else
814 pv = v->vec;
815 sav = Strspl(STRslash, name); /* / command name for postpending */
816 hashval = havhash ? hashname(name) : 0;
817 i = 0;
818 do {
819 if (!slash && ABSOLUTEP(pv[0]) && havhash) {
820#ifdef FASTHASH
821 if (!bit(hashval, i))
822 goto cont;
823#else /* OLDHASH */
824 int hashval1 = hash(hashval, i);
825 if (!bit(xhash, hashval1))
826 goto cont;
827#endif /* FASTHASH */
828 }
829 if (pv[0][0] == 0 || eq(pv[0], STRdot)) { /* don't make ./xxx */
830 if (executable(NULL, name, 0)) {
831 xfree(sav);
832 return i + 1;
833 }
834 }
835 else {
836 if (executable(*pv, sav, 0)) {
837 xfree(sav);
838 return i + 1;
839 }
840 }
841cont:
842 pv++;
843 i++;
844 } while (*pv);
845 xfree(sav);
846 return 0;
847}
848
849/* Also by:
850 * Andreas Luik <luik@isaak.isa.de>
851 * I S A GmbH - Informationssysteme fuer computerintegrierte Automatisierung
852 * Azenberstr. 35
853 * D-7000 Stuttgart 1
854 * West-Germany
855 * is the executable() routine below and changes to iscommand().
856 * Thanks again!!
857 */
858
859#ifndef WINNT_NATIVE
860/*
861 * executable() examines the pathname obtained by concatenating dir and name
862 * (dir may be NULL), and returns 1 either if it is executable by us, or
863 * if dir_ok is set and the pathname refers to a directory.
864 * This is a bit kludgy, but in the name of optimization...
865 */
866int
867executable(const Char *dir, const Char *name, int dir_ok)
868{
869 struct stat stbuf;
870 char *strname;
871
872 if (dir && *dir) {
873 Char *path;
874
875 path = Strspl(dir, name);
876 strname = short2str(path);
877 xfree(path);
878 }
879 else
880 strname = short2str(name);
881
882 return (stat(strname, &stbuf) != -1 &&
883 ((dir_ok && S_ISDIR(stbuf.st_mode)) ||
884 (S_ISREG(stbuf.st_mode) &&
885 /* save time by not calling access() in the hopeless case */
886 (stbuf.st_mode & (S_IXOTH | S_IXGRP | S_IXUSR)) &&
887 access(strname, X_OK) == 0
888 )));
889}
890#endif /*!WINNT_NATIVE*/
891
892struct tellmewhat_s0_cleanup
893{
894 Char **dest, *val;
895};
896
897static void
898tellmewhat_s0_cleanup(void *xstate)
899{
900 struct tellmewhat_s0_cleanup *state;
901
902 state = xstate;
903 *state->dest = state->val;
904}
905
906int
907tellmewhat(struct wordent *lexp, Char **str)
908{
909 struct tellmewhat_s0_cleanup s0;
910 int i;
911 const struct biltins *bptr;
912 struct wordent *sp = lexp->next;
913 int aliased = 0, found;
914 Char *s1, *s2, *cmd;
915 Char qc;
916
917 if (adrof1(sp->word, &aliases)) {
918 alias(lexp);
919 sp = lexp->next;
920 aliased = 1;
921 }
922
923 s0.dest = &sp->word; /* to get the memory freeing right... */
924 s0.val = sp->word;
925 cleanup_push(&s0, tellmewhat_s0_cleanup);
926
927 /* handle quoted alias hack */
928 if ((*(sp->word) & (QUOTE | TRIM)) == QUOTE)
929 (sp->word)++;
930
931 /* do quoting, if it hasn't been done */
932 s1 = s2 = sp->word;
933 while (*s2)
934 switch (*s2) {
935 case '\'':
936 case '"':
937 qc = *s2++;
938 while (*s2 && *s2 != qc)
939 *s1++ = *s2++ | QUOTE;
940 if (*s2)
941 s2++;
942 break;
943 case '\\':
944 if (*++s2)
945 *s1++ = *s2++ | QUOTE;
946 break;
947 default:
948 *s1++ = *s2++;
949 }
950 *s1 = '\0';
951
952 for (bptr = bfunc; bptr < &bfunc[nbfunc]; bptr++) {
953 if (eq(sp->word, str2short(bptr->bname))) {
954 if (str == NULL) {
955 if (aliased)
956 prlex(lexp);
957 xprintf(CGETS(13, 5, "%S: shell built-in command.\n"),
958 sp->word);
959 flush();
960 }
961 else
962 *str = Strsave(sp->word);
963 cleanup_until(&s0);
964 return TRUE;
965 }
966 }
967#ifdef WINNT_NATIVE
968 for (bptr = nt_bfunc; bptr < &nt_bfunc[nt_nbfunc]; bptr++) {
969 if (eq(sp->word, str2short(bptr->bname))) {
970 if (str == NULL) {
971 if (aliased)
972 prlex(lexp);
973 xprintf(CGETS(13, 5, "%S: shell built-in command.\n"),
974 sp->word);
975 flush();
976 }
977 else
978 *str = Strsave(sp->word);
979 cleanup_until(&s0);
980 return TRUE;
981 }
982 }
983#endif /* WINNT_NATIVE*/
984
985 sp->word = cmd = globone(sp->word, G_IGNORE);
986 cleanup_push(cmd, xfree);
987
988 if ((i = iscommand(sp->word)) != 0) {
989 Char **pv;
990 struct varent *v;
991 int slash = any(short2str(sp->word), '/');
992
993 v = adrof(STRpath);
994 if (v == NULL || v->vec == NULL || v->vec[0] == NULL || slash)
995 pv = justabs;
996 else
997 pv = v->vec;
998
999 pv += i - 1;
1000 if (pv[0][0] == 0 || eq(pv[0], STRdot)) {
1001 if (!slash) {
1002 sp->word = Strspl(STRdotsl, sp->word);
1003 cleanup_push(sp->word, xfree);
1004 prlex(lexp);
1005 cleanup_until(sp->word);
1006 }
1007 else
1008 prlex(lexp);
1009 }
1010 else {
1011 s1 = Strspl(*pv, STRslash);
1012 sp->word = Strspl(s1, sp->word);
1013 xfree(s1);
1014 cleanup_push(sp->word, xfree);
1015 if (str == NULL)
1016 prlex(lexp);
1017 else
1018 *str = Strsave(sp->word);
1019 cleanup_until(sp->word);
1020 }
1021 found = 1;
1022 }
1023 else {
1024 if (str == NULL) {
1025 if (aliased)
1026 prlex(lexp);
1027 xprintf(CGETS(13, 6, "%S: Command not found.\n"), sp->word);
1028 flush();
1029 }
1030 else
1031 *str = Strsave(sp->word);
1032 found = 0;
1033 }
1034 cleanup_until(&s0);
1035 return found;
1036}
1037
1038/*
1039 * Builtin to look at and list all places a command may be defined:
1040 * aliases, shell builtins, and the path.
1041 *
1042 * Marc Horowitz <marc@mit.edu>
1043 * MIT Student Information Processing Board
1044 */
1045
1046/*ARGSUSED*/
1047void
1048dowhere(Char **v, struct command *c)
1049{
1050 int found = 1;
1051 USE(c);
1052 for (v++; *v; v++)
1053 found &= find_cmd(*v, 1);
1054 /* Make status nonzero if any command is not found. */
1055 if (!found)
1056 setcopy(STRstatus, STR1, VAR_READWRITE);
1057}
1058
1059int
1060find_cmd(Char *cmd, int prt)
1061{
1062 struct varent *var;
1063 const struct biltins *bptr;
1064 Char **pv;
1065 Char *sv;
1066 int hashval, i, ex, rval = 0;
1067
1068 if (prt && any(short2str(cmd), '/')) {
57e3f2b5 1069 xprintf("%s", CGETS(13, 7, "where: / in command makes no sense\n"));
7d8fb588
MS
1070 return rval;
1071 }
1072
1073 /* first, look for an alias */
1074
1075 if (prt && adrof1(cmd, &aliases)) {
1076 if ((var = adrof1(cmd, &aliases)) != NULL) {
1077 xprintf(CGETS(13, 8, "%S is aliased to "), cmd);
1078 if (var->vec != NULL)
1079 blkpr(var->vec);
1080 xputchar('\n');
1081 rval = 1;
1082 }
1083 }
1084
1085 /* next, look for a shell builtin */
1086
1087 for (bptr = bfunc; bptr < &bfunc[nbfunc]; bptr++) {
1088 if (eq(cmd, str2short(bptr->bname))) {
1089 rval = 1;
1090 if (prt)
1091 xprintf(CGETS(13, 9, "%S is a shell built-in\n"), cmd);
1092 else
1093 return rval;
1094 }
1095 }
1096#ifdef WINNT_NATIVE
1097 for (bptr = nt_bfunc; bptr < &nt_bfunc[nt_nbfunc]; bptr++) {
1098 if (eq(cmd, str2short(bptr->bname))) {
1099 rval = 1;
1100 if (prt)
1101 xprintf(CGETS(13, 9, "%S is a shell built-in\n"), cmd);
1102 else
1103 return rval;
1104 }
1105 }
1106#endif /* WINNT_NATIVE*/
1107
1108 /* last, look through the path for the command */
1109
1110 if ((var = adrof(STRpath)) == NULL)
1111 return rval;
1112
1113 hashval = havhash ? hashname(cmd) : 0;
1114
1115 sv = Strspl(STRslash, cmd);
1116 cleanup_push(sv, xfree);
1117
1118 for (pv = var->vec, i = 0; pv && *pv; pv++, i++) {
1119 if (havhash && !eq(*pv, STRdot)) {
1120#ifdef FASTHASH
1121 if (!bit(hashval, i))
1122 continue;
1123#else /* OLDHASH */
1124 int hashval1 = hash(hashval, i);
1125 if (!bit(xhash, hashval1))
1126 continue;
1127#endif /* FASTHASH */
1128 }
1129 ex = executable(*pv, sv, 0);
1130#ifdef FASTHASH
1131 if (!ex && (hashdebug & 2)) {
57e3f2b5 1132 xprintf("%s", CGETS(13, 10, "hash miss: "));
7d8fb588
MS
1133 ex = 1; /* Force printing */
1134 }
1135#endif /* FASTHASH */
1136 if (ex) {
1137 rval = 1;
1138 if (prt) {
1139 xprintf("%S/", *pv);
1140 xprintf("%S\n", cmd);
1141 }
1142 else
1143 return rval;
1144 }
1145 }
1146 cleanup_until(sv);
1147 return rval;
1148}
1149#ifdef WINNT_NATIVE
1150int hashval_extern(cp)
1151 Char *cp;
1152{
1153 return havhash?hashname(cp):0;
1154}
1155int bit_extern(val,i)
1156 int val;
1157 int i;
1158{
1159 return bit(val,i);
1160}
1161void bis_extern(val,i)
1162 int val;
1163 int i;
1164{
1165 bis(val,i);
1166}
1167#endif /* WINNT_NATIVE */
1168