VirtualBox

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

Last change on this file since 2648 was 2648, checked in by bird, 12 years ago

Made TRACE2 and TRACE2V safe. Fixed DEBUG bug where va_copy was missing. Introduced sh_strerror.

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

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