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