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