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