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