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