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

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

kash: Use kHlpAssert instead of assert.h (debugger stops on the assertion rather than at exit process code).

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