VirtualBox

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

Last change on this file since 3324 was 3057, checked in by bird, 8 years ago

kash: rewrite kmk_builtin_% to kmk_% on the fly to allow harmless mixing of external and builtin kmk commands.

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