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