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