VirtualBox

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

最後變更 在這個檔案從3569是 3568,由 bird 提交於 3 年 前

kash: Corrected has_ext for when suffix is set via the suffix index in shellexec. Removed bogus assertion from tryexec.

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

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