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