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