source: trunk/src/kash/exec.c@ 3437

Last change on this file since 3437 was 3437, checked in by bird, 5 years ago

kash: refactoring evalcommand - complicated, part II.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 28.9 KB
Line 
1/* $NetBSD: exec.c,v 1.37 2003/08/07 09:05:31 agc Exp $ */
2
3/*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#if 0
36#ifndef lint
37static char sccsid[] = "@(#)exec.c 8.4 (Berkeley) 6/8/95";
38#else
39__RCSID("$NetBSD: exec.c,v 1.37 2003/08/07 09:05:31 agc Exp $");
40#endif /* not lint */
41#endif
42
43#include <sys/types.h>
44#include <errno.h>
45#include <stdio.h>
46#include <stdlib.h>
47
48/*
49 * When commands are first encountered, they are entered in a hash table.
50 * This ensures that a full path search will not have to be done for them
51 * on each invocation.
52 *
53 * We should investigate converting to a linear search, even though that
54 * would make the command name "hash" a misnomer.
55 */
56
57#include "shell.h"
58#include "main.h"
59#include "nodes.h"
60#include "parser.h"
61#include "redir.h"
62#include "eval.h"
63#include "exec.h"
64#include "builtins.h"
65#include "var.h"
66#include "options.h"
67#include "input.h"
68#include "output.h"
69#include "syntax.h"
70#include "memalloc.h"
71#include "error.h"
72#include "init.h"
73#include "mystring.h"
74#include "show.h"
75#include "jobs.h"
76#include "alias.h"
77#ifdef __INNOTEK_LIBC__
78#include <InnoTekLIBC/backend.h>
79#endif
80#include "shinstance.h"
81
82//#define CMDTABLESIZE 31 /* should be prime */
83//#define ARB 1 /* actual size determined at run time */
84//
85//
86//
87//struct tblentry {
88// struct tblentry *next; /* next entry in hash chain */
89// union param param; /* definition of builtin function */
90// short cmdtype; /* index identifying command */
91// char rehash; /* if set, cd done since entry created */
92// char cmdname[ARB]; /* name of command */
93//};
94//
95//
96//STATIC struct tblentry *cmdtable[CMDTABLESIZE];
97//STATIC int builtinloc = -1; /* index in path of %builtin, or -1 */
98//int exerrno = 0; /* Last exec error */
99
100
101STATIC void tryexec(shinstance *, char *, char **, char **, int);
102STATIC void execinterp(shinstance *, char **, char **);
103STATIC void printentry(shinstance *, struct tblentry *, int);
104STATIC void clearcmdentry(shinstance *, int);
105STATIC struct tblentry *cmdlookup(shinstance *, const char *, int);
106STATIC void delete_cmd_entry(shinstance *);
107#ifdef PC_EXE_EXTS
108STATIC int stat_pc_exec_exts(shinstance *, char *fullname, struct stat *st, int has_ext);
109#endif
110
111
112extern char *const parsekwd[];
113
114/*
115 * Exec a program. Never returns. If you change this routine, you may
116 * have to change the find_command routine as well.
117 */
118
119SH_NORETURN_1 void
120shellexec(shinstance *psh, char **argv, char **envp, const char *path, int idx)
121{
122 char *cmdname;
123 int e;
124 const char *argv0 = argv[0];
125 int argv0len = (int)strlen(argv0);
126 char kmkcmd[48];
127#ifdef PC_EXE_EXTS
128 int has_ext = argv0len - 4;
129 has_ext = has_ext > 0
130 && argv0[has_ext] == '.'
131 /* use strstr and upper/lower permuated extensions to avoid multiple strcasecmp calls. */
132 && strstr("exe;" "Exe;" "EXe;" "EXE;" "ExE;" "eXe;" "eXE;" "exE;"
133 "cmd;" "Cmd;" "CMd;" "CMD;" "CmD;" "cMd;" "cMD;" "cmD;"
134 "com;" "Com;" "COm;" "COM;" "CoM;" "cOm;" "cOM;" "coM;"
135 "bat;" "Bat;" "BAt;" "BAT;" "BaT;" "bAt;" "bAT;" "baT;"
136 "btm;" "Btm;" "BTm;" "BTM;" "BtM;" "bTm;" "bTM;" "btM;",
137 argv0 + has_ext + 1)
138 != NULL;
139#else
140 const int has_ext = 1;
141#endif
142 TRACE((psh, "shellexec: argv[0]=%s idx=%d\n", argv0, idx));
143 if (strchr(argv0, '/') != NULL) {
144 cmdname = stalloc(psh, argv0len + 5);
145 strcpy(cmdname, argv0);
146 tryexec(psh, cmdname, argv, envp, has_ext);
147 TRACE((psh, "shellexec: cmdname=%s\n", cmdname));
148 stunalloc(psh, cmdname);
149 e = errno;
150 } else {
151 /* Before we search the PATH, transform kmk_builtin_% to kmk_% so we don't
152 need to be too careful mixing internal and external kmk commands. */
153 if ( argv0len > 12
154 && argv0len < 42
155 && strncmp(argv0, "kmk_builtin_", 12) == 0
156 && strpbrk(argv0 + 12, "./\\-:;<>") == NULL) {
157 memcpy(kmkcmd, "kmk_", 4);
158 memcpy(&kmkcmd[4], argv0 + 12, argv0len + 1 - 8);
159 TRACE((psh, "shellexec: dropped '_builtin' from %s to %s\n", argv0, kmkcmd));
160 argv0len -= 8;
161 argv0 = kmkcmd;
162 }
163
164 e = ENOENT;
165 while ((cmdname = padvance(psh, &path, argv0)) != NULL) {
166 if (--idx < 0 && psh->pathopt == NULL) {
167 tryexec(psh, cmdname, argv, envp, has_ext);
168 if (errno != ENOENT && errno != ENOTDIR)
169 e = errno;
170 }
171 stunalloc(psh, cmdname);
172 }
173 }
174
175 /* Map to POSIX errors */
176 switch (e) {
177 case EACCES:
178 psh->exerrno = 126;
179 break;
180 case ENOENT:
181 psh->exerrno = 127;
182 break;
183 default:
184 psh->exerrno = 2;
185 break;
186 }
187 TRACE((psh, "shellexec failed for '%s', errno %d, suppressint %d\n",
188 argv[0], e, psh->suppressint ));
189 exerror(psh, EXEXEC, "%s: %s", argv[0], errmsg(psh, e, E_EXEC));
190 /* NOTREACHED */
191}
192
193
194STATIC void
195tryexec(shinstance *psh, char *cmd, char **argv, char **envp, int has_ext)
196{
197 int e;
198#ifdef EXEC_HASH_BANG_SCRIPT
199 char *p;
200#endif
201#ifdef PC_EXE_EXTS
202 /* exploit the effect of stat_pc_exec_exts which adds the
203 * correct extentions to the file.
204 */
205 struct stat st;
206 if (!has_ext)
207 stat_pc_exec_exts(psh, cmd, &st, 0);
208#endif
209#if defined(__INNOTEK_LIBC__) && defined(EXEC_HASH_BANG_SCRIPT)
210 __libc_Back_gfProcessHandleHashBangScripts = 0;
211#endif
212
213#ifdef SYSV
214 do {
215 sh_execve(psh, cmd, argv, envp);
216 } while (errno == EINTR);
217#else
218 sh_execve(psh, cmd, (const char * const*)argv, (const char * const*)envp);
219#endif
220 e = errno;
221 if (e == ENOEXEC) {
222 initshellproc(psh);
223 setinputfile(psh, cmd, 0);
224 psh->commandname = psh->arg0 = savestr(psh, argv[0]);
225#ifdef EXEC_HASH_BANG_SCRIPT
226 pgetc(psh); pungetc(psh); /* fill up input buffer */
227 p = psh->parsenextc;
228 if (psh->parsenleft > 2 && p[0] == '#' && p[1] == '!') {
229 argv[0] = cmd;
230 execinterp(psh, argv, envp);
231 }
232#endif
233 setparam(psh, argv + 1);
234 exraise(psh, EXSHELLPROC);
235 }
236 errno = e;
237}
238
239#ifdef EXEC_HASH_BANG_SCRIPT
240
241/*
242 * Checks if NAME is the (base) name of the shell executable or something
243 * very similar.
244 */
245STATIC int
246is_shell_exe_name(const char *name)
247{
248 return equal(name, "kmk_ash")
249 || equal(name, "kmk_sh")
250 || equal(name, "kash")
251 || equal(name, "sh");
252}
253
254/*
255 * Execute an interpreter introduced by "#!", for systems where this
256 * feature has not been built into the kernel. If the interpreter is
257 * the shell, return (effectively ignoring the "#!"). If the execution
258 * of the interpreter fails, exit.
259 *
260 * This code peeks inside the input buffer in order to avoid actually
261 * reading any input. It would benefit from a rewrite.
262 */
263
264#define NEWARGS 16
265
266STATIC void
267execinterp(shinstance *psh, char **argv, char **envp)
268{
269 int n;
270 char *inp;
271 char *outp;
272 char c;
273 char *p;
274 char **ap;
275 char *newargs[NEWARGS];
276 intptr_t i;
277 char **ap2;
278 char **new;
279
280 /* Split the string into arguments. */
281 n = psh->parsenleft - 2;
282 inp = psh->parsenextc + 2;
283 ap = newargs;
284 for (;;) {
285 while (--n >= 0 && (*inp == ' ' || *inp == '\t'))
286 inp++;
287 if (n < 0)
288 goto bad;
289 if ((c = *inp++) == '\n')
290 break;
291 if (ap == &newargs[NEWARGS])
292bad: error(psh, "Bad #! line");
293 STARTSTACKSTR(psh, outp);
294 do {
295 STPUTC(psh, c, outp);
296 } while (--n >= 0 && (c = *inp++) != ' ' && c != '\t' && c != '\n');
297 STPUTC(psh, '\0', outp);
298 n++, inp--;
299 *ap++ = grabstackstr(psh, outp);
300 }
301
302 /* /usr/bin/env emulation, very common with kash/kmk_ash. */
303 i = ap - newargs;
304 if (i > 1 && equal(newargs[0], "/usr/bin/env")) {
305 if ( !strchr(newargs[1], '=')
306 && newargs[1][0] != '-') {
307 /* shellexec below searches the PATH for us, so just
308 drop /usr/bin/env. */
309 TRACE((psh, "hash bang /usr/bin/env utility, dropping /usr/bin/env\n"));
310 ap--;
311 i--;
312 for (n = 0; n < i; n++)
313 newargs[n] = newargs[n + 1];
314 } /* else: complicated invocation */
315 }
316
317 /* If the interpreter is the shell or a similar shell, there is
318 no need to exec. */
319 if (i == 1) {
320 p = strrchr(newargs[0], '/');
321 if (!p)
322 p = newargs[0];
323 if (is_shell_exe_name(p)) {
324 TRACE((psh, "hash bang self\n"));
325 return;
326 }
327 }
328
329 /* Combine the two argument lists and exec. */
330 i = (char *)ap - (char *)newargs; /* size in bytes */
331 if (i == 0)
332 error(psh, "Bad #! line");
333 for (ap2 = argv ; *ap2++ != NULL ; );
334 new = ckmalloc(psh, i + ((char *)ap2 - (char *)argv));
335 ap = newargs, ap2 = new;
336 while ((i -= sizeof (char **)) >= 0)
337 *ap2++ = *ap++;
338 ap = argv;
339 while ((*ap2++ = *ap++))
340 /* nothing*/;
341 TRACE((psh, "hash bang '%s'\n", new[0]));
342 shellexec(psh, new, envp, pathval(psh), 0);
343 /* NOTREACHED */
344}
345
346#endif /* EXEC_HASH_BANG_SCRIPT */
347
348
349/*
350 * Do a path search. The variable path (passed by reference) should be
351 * set to the start of the path before the first call; padvance will update
352 * this value as it proceeds. Successive calls to padvance will return
353 * the possible path expansions in sequence. If an option (indicated by
354 * a percent sign) appears in the path entry then the global variable
355 * psh->pathopt will be set to point to it; otherwise psh->pathopt will be set to
356 * NULL.
357 */
358
359//const char *pathopt;
360
361char *
362padvance(shinstance *psh, const char **path, const char *name)
363{
364 const char *p;
365 char *q;
366 const char *start;
367 int len;
368
369 if (*path == NULL)
370 return NULL;
371 start = *path;
372#ifdef PC_PATH_SEP
373 for (p = start ; *p && *p != ';' && *p != '%' ; p++);
374#else
375 for (p = start ; *p && *p != ':' && *p != '%' ; p++);
376#endif
377 len = (int)(p - start + strlen(name) + 2); /* "2" is for '/' and '\0' */
378#ifdef PC_EXE_EXTS
379 len += 4; /* "4" is for .exe/.com/.cmd/.bat/.btm */
380#endif
381 while (stackblocksize(psh) < len)
382 growstackblock(psh);
383 q = stackblock(psh);
384 if (p != start) {
385 memcpy(q, start, p - start);
386 q += p - start;
387 *q++ = '/';
388 }
389 strcpy(q, name);
390 psh->pathopt = NULL;
391 if (*p == '%') {
392 psh->pathopt = ++p;
393#ifdef PC_PATH_SEP
394 while (*p && *p != ';') p++;
395#else
396 while (*p && *p != ':') p++;
397#endif
398 }
399#ifdef PC_PATH_SEP
400 if (*p == ';')
401#else
402 if (*p == ':')
403#endif
404 *path = p + 1;
405 else
406 *path = NULL;
407 return stalloc(psh, len);
408}
409
410
411#ifdef PC_EXE_EXTS
412STATIC int stat_pc_exec_exts(shinstance *psh, char *fullname, struct stat *st, int has_ext)
413{
414 /* skip the SYSV crap */
415 if (shfile_stat(&psh->fdtab, fullname, st) >= 0)
416 return 0;
417 if (!has_ext && errno == ENOENT)
418 {
419 char *psz = strchr(fullname, '\0');
420 memcpy(psz, ".exe", 5);
421 if (shfile_stat(&psh->fdtab, fullname, st) >= 0)
422 return 0;
423 if (errno != ENOENT && errno != ENOTDIR)
424 return -1;
425
426 memcpy(psz, ".cmd", 5);
427 if (shfile_stat(&psh->fdtab, fullname, st) >= 0)
428 return 0;
429 if (errno != ENOENT && errno != ENOTDIR)
430 return -1;
431
432 memcpy(psz, ".bat", 5);
433 if (shfile_stat(&psh->fdtab, fullname, st) >= 0)
434 return 0;
435 if (errno != ENOENT && errno != ENOTDIR)
436 return -1;
437
438 memcpy(psz, ".com", 5);
439 if (shfile_stat(&psh->fdtab, fullname, st) >= 0)
440 return 0;
441 if (errno != ENOENT && errno != ENOTDIR)
442 return -1;
443
444 memcpy(psz, ".btm", 5);
445 if (shfile_stat(&psh->fdtab, fullname, st) >= 0)
446 return 0;
447 *psz = '\0';
448 }
449 return -1;
450}
451#endif /* PC_EXE_EXTS */
452
453
454
455/*** Command hashing code ***/
456
457
458int
459hashcmd(shinstance *psh, int argc, char **argv)
460{
461 struct tblentry **pp;
462 struct tblentry *cmdp;
463 int c;
464 int verbose;
465 struct cmdentry entry;
466 char *name;
467
468 verbose = 0;
469 while ((c = nextopt(psh, "rv")) != '\0') {
470 if (c == 'r') {
471 clearcmdentry(psh, 0);
472 } else if (c == 'v') {
473 verbose++;
474 }
475 }
476 if (*psh->argptr == NULL) {
477 for (pp = psh->cmdtable ; pp < &psh->cmdtable[CMDTABLESIZE] ; pp++) {
478 for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
479 if (verbose || cmdp->cmdtype == CMDNORMAL)
480 printentry(psh, cmdp, verbose);
481 }
482 }
483 return 0;
484 }
485 while ((name = *psh->argptr) != NULL) {
486 if ((cmdp = cmdlookup(psh, name, 0)) != NULL
487 && (cmdp->cmdtype == CMDNORMAL
488 || (cmdp->cmdtype == CMDBUILTIN && psh->builtinloc >= 0)))
489 delete_cmd_entry(psh);
490 find_command(psh, name, &entry, DO_ERR, pathval(psh));
491 if (verbose) {
492 if (entry.cmdtype != CMDUNKNOWN) { /* if no error msg */
493 cmdp = cmdlookup(psh, name, 0);
494 printentry(psh, cmdp, verbose);
495 }
496 output_flushall(psh);
497 }
498 psh->argptr++;
499 }
500 return 0;
501}
502
503
504STATIC void
505printentry(shinstance *psh, struct tblentry *cmdp, int verbose)
506{
507 int idx;
508 const char *path;
509 char *name;
510
511 switch (cmdp->cmdtype) {
512 case CMDNORMAL:
513 idx = cmdp->param.index;
514 path = pathval(psh);
515 do {
516 name = padvance(psh, &path, cmdp->cmdname);
517 stunalloc(psh, name);
518 } while (--idx >= 0);
519 out1str(psh, name);
520 break;
521 case CMDSPLBLTIN:
522 out1fmt(psh, "special builtin %s", cmdp->cmdname);
523 break;
524 case CMDBUILTIN:
525 out1fmt(psh, "builtin %s", cmdp->cmdname);
526 break;
527 case CMDFUNCTION:
528 out1fmt(psh, "function %s", cmdp->cmdname);
529 if (verbose) {
530 struct procstat ps;
531 INTOFF;
532 commandtext(psh, &ps, cmdp->param.func);
533 INTON;
534 out1str(psh, "() { ");
535 out1str(psh, ps.cmd);
536 out1str(psh, "; }");
537 }
538 break;
539 default:
540 error(psh, "internal error: %s cmdtype %d", cmdp->cmdname, cmdp->cmdtype);
541 }
542 if (cmdp->rehash)
543 out1c(psh, '*');
544 out1c(psh, '\n');
545}
546
547
548
549/*
550 * Resolve a command name. If you change this routine, you may have to
551 * change the shellexec routine as well.
552 */
553
554void
555find_command(shinstance *psh, char *name, struct cmdentry *entry, int act, const char *path)
556{
557 struct tblentry *cmdp, loc_cmd;
558 int idx;
559 int prev;
560 char *fullname;
561 struct stat statb;
562 int e;
563 int (*bltin)(shinstance*,int,char **);
564 int argv0len = (int)strlen(name);
565 char kmkcmd[48];
566#ifdef PC_EXE_EXTS
567 int has_ext = argv0len - 4;
568 has_ext = has_ext > 0
569 && name[has_ext] == '.'
570 /* use strstr and upper/lower permuated extensions to avoid multiple strcasecmp calls. */
571 && strstr("exe;" "Exe;" "EXe;" "EXE;" "ExE;" "eXe;" "eXE;" "exE;"
572 "cmd;" "Cmd;" "CMd;" "CMD;" "CmD;" "cMd;" "cMD;" "cmD;"
573 "com;" "Com;" "COm;" "COM;" "CoM;" "cOm;" "cOM;" "coM;"
574 "bat;" "Bat;" "BAt;" "BAT;" "BaT;" "bAt;" "bAT;" "baT;"
575 "btm;" "Btm;" "BTm;" "BTM;" "BtM;" "bTm;" "bTM;" "btM;",
576 name + has_ext + 1)
577 != NULL;
578#endif
579
580 /* If name contains a slash, don't use PATH or hash table */
581 if (strchr(name, '/') != NULL) {
582 if (act & DO_ABS) {
583 while (shfile_stat(&psh->fdtab, name, &statb) < 0) {
584#ifdef SYSV
585 if (errno == EINTR)
586 continue;
587#endif
588 if (errno != ENOENT && errno != ENOTDIR)
589 e = errno;
590 entry->cmdtype = CMDUNKNOWN;
591 entry->u.index = -1;
592 return;
593 }
594 entry->cmdtype = CMDNORMAL;
595 entry->u.index = -1;
596 return;
597 }
598 entry->cmdtype = CMDNORMAL;
599 entry->u.index = 0;
600 return;
601 }
602
603 if (path != pathval(psh))
604 act |= DO_ALTPATH;
605
606 if (act & DO_ALTPATH && strstr(path, "%builtin") != NULL)
607 act |= DO_ALTBLTIN;
608
609 /* If name is in the table, check answer will be ok */
610 if ((cmdp = cmdlookup(psh, name, 0)) != NULL) {
611 do {
612 switch (cmdp->cmdtype) {
613 case CMDNORMAL:
614 if (act & DO_ALTPATH) {
615 cmdp = NULL;
616 continue;
617 }
618 break;
619 case CMDFUNCTION:
620 if (act & DO_NOFUNC) {
621 cmdp = NULL;
622 continue;
623 }
624 break;
625 case CMDBUILTIN:
626 if ((act & DO_ALTBLTIN) || psh->builtinloc >= 0) {
627 cmdp = NULL;
628 continue;
629 }
630 break;
631 }
632 /* if not invalidated by cd, we're done */
633 if (cmdp->rehash == 0)
634 goto success;
635 } while (0);
636 }
637
638 /* If %builtin not in path, check for builtin next */
639 if ((act & DO_ALTPATH ? !(act & DO_ALTBLTIN) : psh->builtinloc < 0) &&
640 (bltin = find_builtin(psh, name)) != 0)
641 goto builtin_success;
642
643 /* We have to search path. */
644 prev = -1; /* where to start */
645 if (cmdp) { /* doing a rehash */
646 if (cmdp->cmdtype == CMDBUILTIN)
647 prev = psh->builtinloc;
648 else
649 prev = cmdp->param.index;
650 }
651
652 /* Before we search the PATH, transform kmk_builtin_% to kmk_% so we don't
653 need to be too careful mixing internal and external kmk command. */
654 if ( argv0len > 12
655 && argv0len < (int)sizeof(kmkcmd)
656 && strncmp(name, "kmk_builtin_", 12) == 0
657 && strpbrk(name + 12, "./\\-:;<>") == NULL) {
658 memcpy(kmkcmd, "kmk_", 4);
659 memcpy(&kmkcmd[4], name + 12, argv0len + 1 - 8);
660 TRACE((psh, "find_command: dropped '_builtin' from %s to %s\n", name, kmkcmd));
661 argv0len -= 8;
662 name = kmkcmd;
663 }
664
665 e = ENOENT;
666 idx = -1;
667loop:
668 while ((fullname = padvance(psh, &path, name)) != NULL) {
669 stunalloc(psh, fullname);
670 idx++;
671 if (psh->pathopt) {
672 if (prefix("builtin", psh->pathopt)) {
673 if ((bltin = find_builtin(psh, name)) == 0)
674 goto loop;
675 goto builtin_success;
676 } else if (prefix("func", psh->pathopt)) {
677 /* handled below */
678 } else {
679 /* ignore unimplemented options */
680 goto loop;
681 }
682 }
683 /* if rehash, don't redo absolute path names */
684 if (fullname[0] == '/' && idx <= prev) {
685 if (idx < prev)
686 goto loop;
687 TRACE((psh, "searchexec \"%s\": no change\n", name));
688 goto success;
689 }
690#ifdef PC_EXE_EXTS
691 while (stat_pc_exec_exts(psh, fullname, &statb, has_ext) < 0) {
692#else
693 while (shfile_stat(&psh->fdtab, fullname, &statb) < 0) {
694#endif
695#ifdef SYSV
696 if (errno == EINTR)
697 continue;
698#endif
699 if (errno != ENOENT && errno != ENOTDIR)
700 e = errno;
701
702 goto loop;
703 }
704 e = EACCES; /* if we fail, this will be the error */
705 if (!S_ISREG(statb.st_mode))
706 goto loop;
707 if (psh->pathopt) { /* this is a %func directory */
708 if (act & DO_NOFUNC)
709 goto loop;
710 stalloc(psh, strlen(fullname) + 1);
711 readcmdfile(psh, fullname);
712 if ((cmdp = cmdlookup(psh, name, 0)) == NULL ||
713 cmdp->cmdtype != CMDFUNCTION)
714 error(psh, "%s not defined in %s", name, fullname);
715 stunalloc(psh, fullname);
716 goto success;
717 }
718#ifdef notdef
719 /* XXX this code stops root executing stuff, and is buggy
720 if you need a group from the group list. */
721 if (statb.st_uid == sh_geteuid(psh)) {
722 if ((statb.st_mode & 0100) == 0)
723 goto loop;
724 } else if (statb.st_gid == sh_getegid(psh)) {
725 if ((statb.st_mode & 010) == 0)
726 goto loop;
727 } else {
728 if ((statb.st_mode & 01) == 0)
729 goto loop;
730 }
731#endif
732 TRACE((psh, "searchexec \"%s\" returns \"%s\"\n", name, fullname));
733 INTOFF;
734 if (act & DO_ALTPATH) {
735 stalloc(psh, strlen(fullname) + 1);
736 cmdp = &loc_cmd;
737 } else
738 cmdp = cmdlookup(psh, name, 1);
739 cmdp->cmdtype = CMDNORMAL;
740 cmdp->param.index = idx;
741 INTON;
742 goto success;
743 }
744
745 /* We failed. If there was an entry for this command, delete it */
746 if (cmdp)
747 delete_cmd_entry(psh);
748 if (act & DO_ERR)
749 outfmt(psh->out2, "%s: %s\n", name, errmsg(psh, e, E_EXEC));
750 entry->cmdtype = CMDUNKNOWN;
751 return;
752
753builtin_success:
754 INTOFF;
755 if (act & DO_ALTPATH)
756 cmdp = &loc_cmd;
757 else
758 cmdp = cmdlookup(psh, name, 1);
759 if (cmdp->cmdtype == CMDFUNCTION)
760 /* DO_NOFUNC must have been set */
761 cmdp = &loc_cmd;
762 cmdp->cmdtype = CMDBUILTIN;
763 cmdp->param.bltin = bltin;
764 INTON;
765success:
766 cmdp->rehash = 0;
767 entry->cmdtype = cmdp->cmdtype;
768 entry->u = cmdp->param;
769}
770
771
772
773/*
774 * Search the table of builtin commands.
775 */
776
777int
778(*find_builtin(shinstance *psh, char *name))(shinstance *psh, int, char **)
779{
780 const struct builtincmd *bp;
781
782 for (bp = builtincmd ; bp->name ; bp++) {
783 if (*bp->name == *name && equal(bp->name, name))
784 return bp->builtin;
785 }
786 return 0;
787}
788
789int
790(*find_splbltin(shinstance *psh, char *name))(shinstance *psh, int, char **)
791{
792 const struct builtincmd *bp;
793
794 for (bp = splbltincmd ; bp->name ; bp++) {
795 if (*bp->name == *name && equal(bp->name, name))
796 return bp->builtin;
797 }
798 return 0;
799}
800
801/*
802 * At shell startup put special builtins into hash table.
803 * ensures they are executed first (see posix).
804 * We stop functions being added with the same name
805 * (as they are impossible to call)
806 */
807
808void
809hash_special_builtins(shinstance *psh)
810{
811 const struct builtincmd *bp;
812 struct tblentry *cmdp;
813
814 for (bp = splbltincmd ; bp->name ; bp++) {
815 cmdp = cmdlookup(psh, bp->name, 1);
816 cmdp->cmdtype = CMDSPLBLTIN;
817 cmdp->param.bltin = bp->builtin;
818 }
819}
820
821
822
823/*
824 * Called when a cd is done. Marks all commands so the next time they
825 * are executed they will be rehashed.
826 */
827
828void
829hashcd(shinstance *psh)
830{
831 struct tblentry **pp;
832 struct tblentry *cmdp;
833
834 for (pp = psh->cmdtable ; pp < &psh->cmdtable[CMDTABLESIZE] ; pp++) {
835 for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
836 if (cmdp->cmdtype == CMDNORMAL
837 || (cmdp->cmdtype == CMDBUILTIN && psh->builtinloc >= 0))
838 cmdp->rehash = 1;
839 }
840 }
841}
842
843
844
845/*
846 * Fix command hash table when PATH changed.
847 * Called before PATH is changed. The argument is the new value of PATH;
848 * pathval(psh) still returns the old value at this point.
849 * Called with interrupts off.
850 */
851
852void
853changepath(shinstance *psh, const char *newval)
854{
855 const char *old, *new;
856 int idx;
857 int firstchange;
858 int bltin;
859
860 old = pathval(psh);
861 new = newval;
862 firstchange = 9999; /* assume no change */
863 idx = 0;
864 bltin = -1;
865 for (;;) {
866 if (*old != *new) {
867 firstchange = idx;
868#ifdef PC_PATH_SEP
869 if ((*old == '\0' && *new == ';')
870 || (*old == ';' && *new == '\0'))
871#else
872 if ((*old == '\0' && *new == ':')
873 || (*old == ':' && *new == '\0'))
874#endif
875 firstchange++;
876 old = new; /* ignore subsequent differences */
877 }
878 if (*new == '\0')
879 break;
880 if (*new == '%' && bltin < 0 && prefix("builtin", new + 1))
881 bltin = idx;
882#ifdef PC_PATH_SEP
883 if (*new == ';') {
884#else
885 if (*new == ':') {
886#endif
887 idx++;
888 }
889 new++, old++;
890 }
891 if (psh->builtinloc < 0 && bltin >= 0)
892 psh->builtinloc = bltin; /* zap builtins */
893 if (psh->builtinloc >= 0 && bltin < 0)
894 firstchange = 0;
895 clearcmdentry(psh, firstchange);
896 psh->builtinloc = bltin;
897}
898
899
900/*
901 * Clear out command entries. The argument specifies the first entry in
902 * PATH which has changed.
903 */
904
905STATIC void
906clearcmdentry(shinstance *psh, int firstchange)
907{
908 struct tblentry **tblp;
909 struct tblentry **pp;
910 struct tblentry *cmdp;
911
912 INTOFF;
913 for (tblp = psh->cmdtable ; tblp < &psh->cmdtable[CMDTABLESIZE] ; tblp++) {
914 pp = tblp;
915 while ((cmdp = *pp) != NULL) {
916 if ((cmdp->cmdtype == CMDNORMAL &&
917 cmdp->param.index >= firstchange)
918 || (cmdp->cmdtype == CMDBUILTIN &&
919 psh->builtinloc >= firstchange)) {
920 *pp = cmdp->next;
921 ckfree(psh, cmdp);
922 } else {
923 pp = &cmdp->next;
924 }
925 }
926 }
927 INTON;
928}
929
930
931/*
932 * Delete all functions.
933 */
934
935#ifdef mkinit
936MKINIT void deletefuncs(struct shinstance *);
937MKINIT void hash_special_builtins(struct shinstance *);
938
939INIT {
940 hash_special_builtins(psh);
941}
942
943SHELLPROC {
944 deletefuncs(psh);
945}
946#endif
947
948void
949deletefuncs(shinstance *psh)
950{
951 struct tblentry **tblp;
952 struct tblentry **pp;
953 struct tblentry *cmdp;
954
955 INTOFF;
956 for (tblp = psh->cmdtable ; tblp < &psh->cmdtable[CMDTABLESIZE] ; tblp++) {
957 pp = tblp;
958 while ((cmdp = *pp) != NULL) {
959 if (cmdp->cmdtype == CMDFUNCTION) {
960 *pp = cmdp->next;
961 freefunc(psh, cmdp->param.func);
962 ckfree(psh, cmdp);
963 } else {
964 pp = &cmdp->next;
965 }
966 }
967 }
968 INTON;
969}
970
971
972
973/*
974 * Locate a command in the command hash table. If "add" is nonzero,
975 * add the command to the table if it is not already present. The
976 * variable "lastcmdentry" is set to point to the address of the link
977 * pointing to the entry, so that delete_cmd_entry can delete the
978 * entry.
979 */
980
981struct tblentry **lastcmdentry;
982
983
984STATIC struct tblentry *
985cmdlookup(shinstance *psh, const char *name, int add)
986{
987 int hashval;
988 const char *p;
989 struct tblentry *cmdp;
990 struct tblentry **pp;
991
992 p = name;
993 hashval = *p << 4;
994 while (*p)
995 hashval += *p++;
996 hashval &= 0x7FFF;
997 pp = &psh->cmdtable[hashval % CMDTABLESIZE];
998 for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
999 if (equal(cmdp->cmdname, name))
1000 break;
1001 pp = &cmdp->next;
1002 }
1003 if (add && cmdp == NULL) {
1004 INTOFF;
1005 cmdp = *pp = ckmalloc(psh, sizeof (struct tblentry) - ARB
1006 + strlen(name) + 1);
1007 cmdp->next = NULL;
1008 cmdp->cmdtype = CMDUNKNOWN;
1009 cmdp->rehash = 0;
1010 strcpy(cmdp->cmdname, name);
1011 INTON;
1012 }
1013 lastcmdentry = pp;
1014 return cmdp;
1015}
1016
1017/*
1018 * Delete the command entry returned on the last lookup.
1019 */
1020
1021STATIC void
1022delete_cmd_entry(shinstance *psh)
1023{
1024 struct tblentry *cmdp;
1025
1026 INTOFF;
1027 cmdp = *lastcmdentry;
1028 *lastcmdentry = cmdp->next;
1029 ckfree(psh, cmdp);
1030 INTON;
1031}
1032
1033
1034
1035#ifdef notdef
1036void
1037getcmdentry(shinstance *psh, char *name, struct cmdentry *entry)
1038{
1039 struct tblentry *cmdp = cmdlookup(psh, name, 0);
1040
1041 if (cmdp) {
1042 entry->u = cmdp->param;
1043 entry->cmdtype = cmdp->cmdtype;
1044 } else {
1045 entry->cmdtype = CMDUNKNOWN;
1046 entry->u.index = 0;
1047 }
1048}
1049#endif
1050
1051
1052/*
1053 * Add a new command entry, replacing any existing command entry for
1054 * the same name - except special builtins.
1055 */
1056
1057STATIC void
1058addcmdentry(shinstance *psh, char *name, struct cmdentry *entry)
1059{
1060 struct tblentry *cmdp;
1061
1062 INTOFF;
1063 cmdp = cmdlookup(psh, name, 1);
1064 if (cmdp->cmdtype != CMDSPLBLTIN) {
1065 if (cmdp->cmdtype == CMDFUNCTION) {
1066 freefunc(psh, cmdp->param.func);
1067 }
1068 cmdp->cmdtype = entry->cmdtype;
1069 cmdp->param = entry->u;
1070 }
1071 INTON;
1072}
1073
1074
1075/*
1076 * Define a shell function.
1077 */
1078
1079void
1080defun(shinstance *psh, char *name, union node *func)
1081{
1082 struct cmdentry entry;
1083
1084 INTOFF;
1085 entry.cmdtype = CMDFUNCTION;
1086 entry.u.func = copyfunc(psh, func);
1087 addcmdentry(psh, name, &entry);
1088 INTON;
1089}
1090
1091
1092/*
1093 * Delete a function if it exists.
1094 */
1095
1096int
1097unsetfunc(shinstance *psh, char *name)
1098{
1099 struct tblentry *cmdp;
1100
1101 if ((cmdp = cmdlookup(psh, name, 0)) != NULL &&
1102 cmdp->cmdtype == CMDFUNCTION) {
1103 freefunc(psh, cmdp->param.func);
1104 delete_cmd_entry(psh);
1105 return (0);
1106 }
1107 return (1);
1108}
1109
1110/*
1111 * Locate and print what a word is...
1112 * also used for 'command -[v|V]'
1113 */
1114
1115int
1116typecmd(shinstance *psh, int argc, char **argv)
1117{
1118 struct cmdentry entry;
1119 struct tblentry *cmdp;
1120 char * const *pp;
1121 struct alias *ap;
1122 int err = 0;
1123 char *arg;
1124 int c;
1125 int V_flag = 0;
1126 int v_flag = 0;
1127 int p_flag = 0;
1128
1129 while ((c = nextopt(psh, "vVp")) != 0) {
1130 switch (c) {
1131 case 'v': v_flag = 1; break;
1132 case 'V': V_flag = 1; break;
1133 case 'p': p_flag = 1; break;
1134 }
1135 }
1136
1137 if (p_flag && (v_flag || V_flag))
1138 error(psh, "cannot specify -p with -v or -V");
1139
1140 while ((arg = *psh->argptr++)) {
1141 if (!v_flag)
1142 out1str(psh, arg);
1143 /* First look at the keywords */
1144 for (pp = parsekwd; *pp; pp++)
1145 if (**pp == *arg && equal(*pp, arg))
1146 break;
1147
1148 if (*pp) {
1149 if (v_flag)
1150 err = 1;
1151 else
1152 out1str(psh, " is a shell keyword\n");
1153 continue;
1154 }
1155
1156 /* Then look at the aliases */
1157 if ((ap = lookupalias(psh, arg, 1)) != NULL) {
1158 if (!v_flag)
1159 out1fmt(psh, " is an alias for \n");
1160 out1fmt(psh, "%s\n", ap->val);
1161 continue;
1162 }
1163
1164 /* Then check if it is a tracked alias */
1165 if ((cmdp = cmdlookup(psh, arg, 0)) != NULL) {
1166 entry.cmdtype = cmdp->cmdtype;
1167 entry.u = cmdp->param;
1168 } else {
1169 /* Finally use brute force */
1170 find_command(psh, arg, &entry, DO_ABS, pathval(psh));
1171 }
1172
1173 switch (entry.cmdtype) {
1174 case CMDNORMAL: {
1175 if (strchr(arg, '/') == NULL) {
1176 const char *path = pathval(psh);
1177 char *name;
1178 int j = entry.u.index;
1179 do {
1180 name = padvance(psh, &path, arg);
1181 stunalloc(psh, name);
1182 } while (--j >= 0);
1183 if (!v_flag)
1184 out1fmt(psh, " is%s ",
1185 cmdp ? " a tracked alias for" : "");
1186 out1fmt(psh, "%s\n", name);
1187 } else {
1188 if (shfile_access(&psh->fdtab, arg, X_OK) == 0) {
1189 if (!v_flag)
1190 out1fmt(psh, " is ");
1191 out1fmt(psh, "%s\n", arg);
1192 } else {
1193 if (!v_flag)
1194 out1fmt(psh, ": %s\n",
1195 sh_strerror(psh, errno));
1196 else
1197 err = 126;
1198 }
1199 }
1200 break;
1201 }
1202 case CMDFUNCTION:
1203 if (!v_flag)
1204 out1str(psh, " is a shell function\n");
1205 else
1206 out1fmt(psh, "%s\n", arg);
1207 break;
1208
1209 case CMDBUILTIN:
1210 if (!v_flag)
1211 out1str(psh, " is a shell builtin\n");
1212 else
1213 out1fmt(psh, "%s\n", arg);
1214 break;
1215
1216 case CMDSPLBLTIN:
1217 if (!v_flag)
1218 out1str(psh, " is a special shell builtin\n");
1219 else
1220 out1fmt(psh, "%s\n", arg);
1221 break;
1222
1223 default:
1224 if (!v_flag)
1225 out1str(psh, ": not found\n");
1226 err = 127;
1227 break;
1228 }
1229 }
1230 return err;
1231}
Note: See TracBrowser for help on using the repository browser.