VirtualBox

source: kBuild/trunk/src/kash/exec.c@ 3456

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

kash: Hammering on threaded mode.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette