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