1 /* $OpenBSD: exec.c,v 1.50 2013/06/10 21:09:27 millert Exp $ */
4 * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
6 * Thorsten Glaser <tg@mirbsd.org>
8 * Provided that these terms and disclaimer and all copyright notices
9 * are retained or reproduced in an accompanying document, permission
10 * is granted to deal in this work without restriction, including un-
11 * limited rights to use, publicly perform, distribute, sell, modify,
12 * merge, give away, or sublicence.
14 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
15 * the utmost extent permitted by applicable law, neither express nor
16 * implied; without malicious intent or gross negligence. In no event
17 * may a licensor, author or contributor be held liable for indirect,
18 * direct, other damage, loss, or other issues arising in any way out
19 * of dealing in the work, even if advised of the possibility of such
20 * damage or existence of a defect, except proven that it results out
21 * of said person's immediate fault when using the work as intended.
26 __RCSID("$MirOS: src/bin/mksh/exec.c,v 1.125 2013/07/21 20:44:44 tg Exp $");
28 #ifndef MKSH_DEFAULT_EXECSHELL
29 #define MKSH_DEFAULT_EXECSHELL "/bin/sh"
32 static int comexec(struct op *, struct tbl * volatile, const char **,
33 int volatile, volatile int *);
34 static void scriptexec(struct op *, const char **) MKSH_A_NORETURN;
35 static int call_builtin(struct tbl *, const char **, const char *);
36 static int iosetup(struct ioword *, struct tbl *);
37 static int herein(struct ioword *, char **);
38 static const char *do_selectargs(const char **, bool);
39 static Test_op dbteste_isa(Test_env *, Test_meta);
40 static const char *dbteste_getopnd(Test_env *, Test_op, bool);
41 static void dbteste_error(Test_env *, int, const char *);
42 static int search_access(const char *, int);
43 /* XXX: horrible kludge to fit within the framework */
44 static char *plain_fmt_entry(char *, size_t, unsigned int, const void *);
45 static char *select_fmt_entry(char *, size_t, unsigned int, const void *);
48 * execute command tree
51 execute(struct op * volatile t,
52 /* if XEXEC don't fork */
54 volatile int * volatile xerrok)
57 volatile int rv = 0, dummy = 0;
59 const char ** volatile ap = NULL;
63 struct tbl *tp = NULL;
69 /* Caller doesn't care if XERROK should propagate. */
73 if ((flags&XFORK) && !(flags&XEXEC) && t->type != TPIPE)
74 /* run in sub-process */
75 return (exchild(t, flags & ~XTIME, xerrok, -1));
81 /* we want to run an executable, do some variance checks */
82 if (t->type == TCOM) {
83 /* check if this is 'var=<<EOF' */
85 /* we have zero arguments, i.e. no programme to run */
87 /* we have exactly one variable assignment */
88 t->vars[0] != NULL && t->vars[1] == NULL &&
89 /* we have exactly one I/O redirection */
90 t->ioact != NULL && t->ioact[0] != NULL &&
91 t->ioact[1] == NULL &&
92 /* of type "here document" (or "here string") */
93 (t->ioact[0]->flag & IOTYPE) == IOHERE &&
94 /* the variable assignment begins with a valid varname */
95 (ccp = skip_wdvarname(t->vars[0], true)) != t->vars[0] &&
96 /* and has no right-hand side (i.e. "varname=") */
97 ccp[0] == CHAR && ccp[1] == '=' && ccp[2] == EOS &&
98 /* plus we can have a here document content */
99 herein(t->ioact[0], &cp) == 0 && cp && *cp) {
101 size_t n = ccp - t->vars[0] + 2, z;
103 /* drop redirection (will be garbage collected) */
106 /* set variable to its expanded value */
108 if (notoktomul(z, 2) || notoktoadd(z * 2, n))
109 internal_errorf(Toomem, (size_t)-1);
110 dp = alloc(z * 2 + n, ATEMP);
111 memcpy(dp, t->vars[0], n);
119 /* free the expanded value */
124 * Clear subst_exstat before argument expansion. Used by
125 * null commands (see comexec() and c_eval()) and by c_set().
130 current_lineno = t->lineno;
133 * POSIX says expand command words first, then redirections,
134 * and assignments last..
136 up = eval(t->args, t->u.evalflags | DOBLANK | DOGLOB | DOTILDE);
138 /* Allow option parsing (bizarre, but POSIX) */
140 ap = (const char **)up;
142 tp = findcom(ap[0], FC_BI|FC_FUNC);
146 if (t->ioact != NULL || t->type == TPIPE || t->type == TCOPROC) {
147 e->savefd = alloc2(NUFILE, sizeof(short), ATEMP);
148 /* initialise to not redirected */
149 memset(e->savefd, 0, NUFILE * sizeof(short));
152 /* mark for replacement later (unless TPIPE) */
153 vp_pipest->flag |= INT_L;
155 /* do redirection, to be restored in quitenv() */
156 if (t->ioact != NULL)
157 for (iowp = t->ioact; *iowp != NULL; iowp++) {
158 if (iosetup(*iowp, tp) < 0) {
161 * Redirection failures for special commands
162 * cause (non-interactive) shell to exit.
164 if (tp && tp->type == CSHELL &&
165 (tp->flag & SPEC_BI))
167 /* Deal with FERREXIT, quitenv(), etc. */
174 rv = comexec(t, tp, (const char **)ap, flags, xerrok);
178 rv = execute(t->left, flags | XFORK, xerrok);
184 e->savefd[0] = savefd(0);
185 e->savefd[1] = savefd(1);
186 while (t->type == TPIPE) {
189 ksh_dup2(pv[1], 1, false);
191 * Let exchild() close pv[0] in child
192 * (if this isn't done, commands like
193 * (: ; cat /etc/termcap) | sleep 1
194 * will hang forever).
196 exchild(t->left, flags | XPIPEO | XCCLOSE,
199 ksh_dup2(pv[0], 0, false);
205 restfd(1, e->savefd[1]);
206 /* no need to re-restore this */
208 /* Let exchild() close 0 in parent, after fork, before wait */
209 i = exchild(t, flags | XPCLOSE | XPIPEST, xerrok, 0);
210 if (!(flags&XBGND) && !(flags&XXCOM))
215 while (t->type == TLIST) {
216 execute(t->left, flags & XERROK, NULL);
219 rv = execute(t, flags & XERROK, xerrok);
223 #ifndef MKSH_NOPROSPECTOFWORK
227 * Block sigchild as we are using things changed in the
230 sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
232 if ((i = kshsetjmp(e->jbuf))) {
233 sigprocmask(SIG_SETMASK, &omask, NULL);
239 /* Already have a (live) co-process? */
240 if (coproc.job && coproc.write >= 0)
241 errorf("coprocess already exists");
243 /* Can we re-use the existing co-process pipe? */
244 coproc_cleanup(true);
246 /* do this before opening pipes, in case these fail */
247 e->savefd[0] = savefd(0);
248 e->savefd[1] = savefd(1);
252 ksh_dup2(pv[0], 0, false);
255 coproc.write = pv[1];
258 if (coproc.readw >= 0)
259 ksh_dup2(coproc.readw, 1, false);
263 ksh_dup2(pv[1], 1, false);
264 /* closed before first read */
265 coproc.readw = pv[1];
267 /* create new coprocess id */
270 #ifndef MKSH_NOPROSPECTOFWORK
271 sigprocmask(SIG_SETMASK, &omask, NULL);
272 /* no more need for error handler */
277 * exchild() closes coproc.* in child after fork,
278 * will also increment coproc.njobs when the
279 * job is actually created.
282 exchild(t->left, flags | XBGND | XFORK | XCOPROC | XCCLOSE,
289 * XXX non-optimal, I think - "(foo &)", forks for (),
290 * forks again for async... parent should optimise
293 rv = execute(t->left, (flags&~XEXEC)|XBGND|XFORK, xerrok);
298 rv = execute(t->left, XERROK, xerrok);
299 if ((rv == 0) == (t->type == TAND))
300 rv = execute(t->right, flags & XERROK, xerrok);
309 rv = !execute(t->right, XERROK, xerrok);
318 te.flags = TEF_DBRACKET;
320 te.isa = dbteste_isa;
321 te.getopnd = dbteste_getopnd;
323 te.error = dbteste_error;
325 rv = test_parse(&te);
331 volatile bool is_first = true;
333 ap = (t->vars == NULL) ? e->loc->argv + 1 :
334 (const char **)eval((const char **)t->vars,
335 DOBLANK | DOGLOB | DOTILDE);
337 while ((i = kshsetjmp(e->jbuf))) {
338 if ((e->flags&EF_BRKCONT_PASS) ||
339 (i != LBREAK && i != LCONTIN)) {
342 } else if (i == LBREAK) {
347 /* in case of a continue */
349 if (t->type == TFOR) {
350 while (*ap != NULL) {
351 setstr(global(t->str), *ap++, KSH_UNWIND_ERROR);
352 rv = execute(t->left, flags & XERROK, xerrok);
357 if (!(ccp = do_selectargs(ap, is_first))) {
362 setstr(global(t->str), ccp, KSH_UNWIND_ERROR);
363 execute(t->left, flags & XERROK, xerrok);
372 while ((i = kshsetjmp(e->jbuf))) {
373 if ((e->flags&EF_BRKCONT_PASS) ||
374 (i != LBREAK && i != LCONTIN)) {
377 } else if (i == LBREAK) {
382 /* in case of a continue */
384 while ((execute(t->left, XERROK, NULL) == 0) ==
386 rv = execute(t->right, flags & XERROK, xerrok);
391 if (t->right == NULL)
392 /* should be error */
394 rv = execute(t->left, XERROK, NULL) == 0 ?
395 execute(t->right->left, flags & XERROK, xerrok) :
396 execute(t->right->right, flags & XERROK, xerrok);
401 ccp = evalstr(t->str, DOTILDE);
402 for (t = t->left; t != NULL && t->type == TPAT; t = t->right) {
403 for (ap = (const char **)t->vars; *ap; ap++) {
404 if (i || ((s = evalstr(*ap, DOTILDE|DOPAT)) &&
405 gmatchx(ccp, s, false))) {
406 rv = execute(t->left, flags & XERROK,
409 switch (t->u.charflag) {
427 rv = execute(t->left, flags & XERROK, xerrok);
431 rv = define(t->str, t);
436 * Clear XEXEC so nested execute() call doesn't exit
437 * (allows "ls -l | time grep foo").
439 rv = timex(t, flags & ~XEXEC, xerrok);
449 union mksh_ccphack cargs;
452 execve(t->str, cargs.rw, up);
456 scriptexec(t, (const char **)up);
458 errorf("%s: %s", s, cstrerror(rv));
462 if (vp_pipest->flag & INT_L) {
464 vp_pipest->flag = DEFINED | ISSET | INTEGER | RDONLY |
466 vp_pipest->val.i = rv;
474 if (rv != 0 && !(flags & XERROK) &&
475 (xerrok == NULL || !*xerrok)) {
476 if (Flag(FERREXIT) & 0x80) {
489 * execute simple command
493 comexec(struct op *t, struct tbl * volatile tp, const char **ap,
494 volatile int flags, volatile int *xerrok)
500 /* Must be static (XXX but why?) */
501 static struct op texec;
504 int fcflags = FC_BI|FC_FUNC|FC_PATH;
505 bool bourne_function_call = false;
506 struct block *l_expand, *l_assign;
509 * snag the last argument for $_ XXX not the same as AT&T ksh,
510 * which only seems to set $_ after a newline (but not in
511 * functions/dot scripts, but in interactive and script) -
512 * perhaps save last arg here and set it in shell()?.
514 if (Flag(FTALKING) && *(lastp = ap)) {
517 /* setstr() can't fail here */
518 setstr(typeset("_", LOCAL, 0, INTEGER, 0), *--lastp,
523 * Deal with the shell builtins builtin, exec and command since
524 * they can be followed by other commands. This must be done before
525 * we know if we should create a local block which must be done
526 * before we can do a path search (in case the assignments change
529 * FOO=bar exec >/dev/null FOO is kept but not exported
530 * FOO=bar exec foobar FOO is exported
531 * FOO=bar command exec >/dev/null FOO is neither kept nor exported
532 * FOO=bar command FOO is neither kept nor exported
533 * PATH=... foobar use new PATH in foobar search
536 while (tp && tp->type == CSHELL) {
537 /* undo effects of command */
538 fcflags = FC_BI|FC_FUNC|FC_PATH;
539 if (tp->val.f == c_builtin) {
540 if ((cp = *++ap) == NULL ||
541 (!strcmp(cp, "--") && (cp = *++ap) == NULL)) {
545 if ((tp = findcom(cp, FC_BI)) == NULL)
546 errorf("%s: %s: %s", Tbuiltin, cp, "not a builtin");
548 } else if (tp->val.f == c_exec) {
553 } else if (tp->val.f == c_command) {
557 * Ugly dealing with options in two places (here
558 * and in c_command(), but such is life)
560 ksh_getopt_reset(&builtin_opt, 0);
561 while ((optc = ksh_getopt(ap, &builtin_opt, ":p")) == 'p')
564 /* command -vV or something */
566 /* don't look for functions */
567 fcflags = FC_BI|FC_PATH;
569 if (Flag(FRESTRICTED)) {
570 warningf(true, "%s: %s",
571 "command -p", "restricted");
575 fcflags |= FC_DEFPATH;
577 ap += builtin_opt.optind;
579 * POSIX says special builtins lose their status
580 * if accessed using command.
584 /* ensure command with no args exits with 0 */
588 #ifndef MKSH_NO_EXTERNAL_CAT
589 } else if (tp->val.f == c_cat) {
591 * if we have any flags, do not use the builtin
592 * in theory, we could allow -u, but that would
593 * mean to use ksh_getopt here and possibly ad-
594 * ded complexity and more code and isn't worth
595 * additional hassle (and the builtin must call
596 * ksh_getopt already but can't come back here)
598 if (ap[1] && ap[1][0] == '-' && ap[1][1] != '\0' &&
599 /* argument, begins with -, is not - or -- */
600 (ap[1][1] != '-' || ap[1][2] != '\0'))
601 /* don't look for builtins or functions */
604 /* go on, use the builtin */
607 #if !defined(MKSH_SMALL)
608 } else if (tp->val.f == c_trap) {
609 t->u.evalflags &= ~DOTCOMEXEC;
614 tp = findcom(ap[0], fcflags & (FC_BI|FC_FUNC));
616 #if !defined(MKSH_SMALL)
617 if (t->u.evalflags & DOTCOMEXEC)
621 if (keepasn_ok && (!ap[0] || (tp && (tp->flag & KEEPASN))))
624 /* create new variable/function block */
626 /* ksh functions don't keep assignments, POSIX functions do. */
627 if (keepasn_ok && tp && tp->type == CFUNC &&
628 !(tp->flag & FKSH)) {
629 bourne_function_call = true;
632 type_flags = LOCAL|LOCAL_COPY|EXPORT;
636 type_flags |= EXPORT;
638 change_xtrace(2, false);
639 for (i = 0; t->vars[i]; i++) {
640 /* do NOT lookup in the new var/fn block just created */
642 cp = evalstr(t->vars[i], DOASNTILDE);
647 ccp = skip_varname(cp, true);
652 shf_write(cp, ccp - cp, shl_xtrace);
653 print_value_quoted(shl_xtrace, ccp);
654 shf_putc(' ', shl_xtrace);
656 /* but assign in there as usual */
657 typeset(cp, type_flags, 0, 0, 0);
658 if (bourne_function_call && !(type_flags & EXPORT))
659 typeset(cp, LOCAL|LOCAL_COPY|EXPORT, 0, 0, 0);
663 change_xtrace(2, false);
666 print_value_quoted(shl_xtrace, ap[rv]);
668 shf_putc(' ', shl_xtrace);
672 change_xtrace(1, false);
675 if ((cp = *ap) == NULL) {
679 if (Flag(FRESTRICTED) && vstrchr(cp, '/')) {
680 warningf(true, "%s: %s", cp, "restricted");
684 tp = findcom(cp, fcflags);
691 rv = call_builtin(tp, (const char **)ap, null);
692 if (!keepasn_ok && tp->val.f == c_shift) {
693 l_expand->argc = l_assign->argc;
694 l_expand->argv = l_assign->argv;
700 volatile unsigned char old_xflag;
701 volatile uint32_t old_inuse;
702 const char * volatile old_kshname;
704 if (!(tp->flag & ISSET)) {
708 rv = (tp->u2.errnov == ENOENT) ? 127 : 126;
709 warningf(true, "%s: %s %s: %s", cp,
710 "can't find", "function definition file",
711 cstrerror(tp->u2.errnov));
714 if (include(tp->u.fpath, 0, NULL, false) < 0) {
715 warningf(true, "%s: %s %s %s: %s", cp,
716 "can't open", "function definition file",
717 tp->u.fpath, cstrerror(errno));
721 if (!(ftp = findfunc(cp, hash(cp), false)) ||
722 !(ftp->flag & ISSET)) {
723 warningf(true, "%s: %s %s", cp,
724 "function not defined by", tp->u.fpath);
732 * ksh functions set $0 to function name, POSIX
733 * functions leave $0 unchanged.
735 old_kshname = kshname;
741 for (i = 0; *ap++ != NULL; i++)
743 e->loc->argc = i - 1;
745 * ksh-style functions handle getopts sanely,
746 * Bourne/POSIX functions are insane...
748 if (tp->flag & FKSH) {
749 e->loc->flags |= BF_DOGETOPTS;
750 e->loc->getopts_state = user_opt;
754 old_xflag = Flag(FXTRACE) ? 1 : 0;
755 change_xtrace((Flag(FXTRACEREC) ? old_xflag : 0) |
756 ((tp->flag & TRACE) ? 1 : 0), false);
757 old_inuse = tp->flag & FINUSE;
761 if (!(i = kshsetjmp(e->jbuf))) {
762 execute(tp->val.t, flags & XERROK, NULL);
766 kshname = old_kshname;
767 change_xtrace(old_xflag, false);
768 tp->flag = (tp->flag & ~FINUSE) | old_inuse;
771 * Were we deleted while executing? If so, free the
772 * execution tree. TODO: Unfortunately, the table entry
773 * is never re-used until the lookup table is expanded.
775 if ((tp->flag & (FDELETE|FINUSE)) == FDELETE) {
776 if (tp->flag & ALLOC) {
778 tfree(tp->val.t, tp->areap);
796 internal_errorf("%s %d", "CFUNC", i);
801 /* executable command */
805 if (!(tp->flag&ISSET)) {
806 if (tp->u2.errnov == ENOENT) {
808 warningf(true, "%s: %s", cp, "not found");
811 warningf(true, "%s: %s: %s", cp, "can't execute",
812 cstrerror(tp->u2.errnov));
817 /* set $_ to programme's full path */
818 /* setstr() can't fail here */
819 setstr(typeset("_", LOCAL|EXPORT, 0, INTEGER, 0),
820 tp->val.s, KSH_RETURN_ERROR);
825 #ifndef MKSH_UNEMPLOYED
829 setexecsig(&sigtraps[SIGINT], SS_RESTORE_ORIG);
830 setexecsig(&sigtraps[SIGQUIT], SS_RESTORE_ORIG);
834 /* to fork we set up a TEXEC node and call execute */
838 texec.str = tp->val.s;
840 rv = exchild(&texec, flags, xerrok, -1);
852 scriptexec(struct op *tp, const char **ap)
857 /* 64 == MAXINTERP in MirBSD <sys/param.h> */
861 union mksh_ccphack args, cap;
863 sh = str_val(global("EXECSHELL"));
865 sh = search_path(sh, path, X_OK, NULL);
867 sh = MKSH_DEFAULT_EXECSHELL;
869 *tp->args-- = tp->str;
872 if ((fd = open(tp->str, O_RDONLY)) >= 0) {
873 /* read first MAXINTERP octets from file */
874 if (read(fd, buf, sizeof(buf)) <= 0)
875 /* read error -> no good */
879 /* skip UTF-8 Byte Order Mark, if present */
880 cp = (unsigned char *)buf;
881 if ((cp[0] == 0xEF) && (cp[1] == 0xBB) && (cp[2] == 0xBF))
883 /* save begin of shebang for later */
884 fd = (char *)cp - buf; /* either 0 or (if BOM) 3 */
886 /* scan for newline (or CR) or NUL _before_ end of buffer */
887 while ((char *)cp < (buf + sizeof(buf)))
888 if (*cp == '\0' || *cp == '\n' || *cp == '\r') {
893 /* if the shebang line is longer than MAXINTERP, bail out */
894 if ((char *)cp >= (buf + sizeof(buf)))
897 /* restore begin of shebang position (buf+0 or buf+3) */
898 cp = (unsigned char *)(buf + fd);
899 /* bail out if read error (above) or no shebang */
900 if ((cp[0] != '#') || (cp[1] != '!'))
904 /* skip whitespace before shell name */
905 while (*cp == ' ' || *cp == '\t')
907 /* just whitespace on the line? */
910 /* no, we actually found an interpreter name */
912 /* look for end of shell/interpreter name */
913 while (*cp != ' ' && *cp != '\t' && *cp != '\0')
918 /* skip spaces before arguments */
919 while (*cp == ' ' || *cp == '\t')
921 /* pass it all in ONE argument (historic reasons) */
923 *tp->args-- = (char *)cp;
926 fd = buf[0] << 8 | buf[1];
927 if ((fd == /* OMAGIC */ 0407) ||
928 (fd == /* NMAGIC */ 0410) ||
929 (fd == /* ZMAGIC */ 0413) ||
930 (fd == /* QMAGIC */ 0314) ||
931 (fd == /* ECOFF_I386 */ 0x4C01) ||
932 (fd == /* ECOFF_M68K */ 0x0150 || fd == 0x5001) ||
933 (fd == /* ECOFF_SH */ 0x0500 || fd == 0x0005) ||
934 (fd == 0x7F45 && buf[2] == 'L' && buf[3] == 'F') ||
935 (fd == /* "MZ" */ 0x4D5A) ||
936 (fd == /* gzip */ 0x1F8B))
937 errorf("%s: not executable: magic %04X", tp->str, fd);
944 execve(args.rw[0], args.rw, cap.rw);
946 /* report both the programme that was run and the bogus interpreter */
947 errorf("%s: %s: %s", tp->str, sh, cstrerror(errno));
951 shcomexec(const char **wp)
955 tp = ktsearch(&builtins, *wp, hash(*wp));
956 return (call_builtin(tp, wp, "shcomexec"));
960 * Search function tables for a function. If create set, a table entry
961 * is created if none is found.
964 findfunc(const char *name, uint32_t h, bool create)
967 struct tbl *tp = NULL;
969 for (l = e->loc; l; l = l->next) {
970 tp = ktsearch(&l->funs, name, h);
973 if (!l->next && create) {
974 tp = ktenter(&l->funs, name, h);
985 * define function. Returns 1 if function is being undefined (t == 0) and
986 * function did not exist, returns 0 otherwise.
989 define(const char *name, struct op *t)
993 bool was_set = false;
997 if (t != NULL && !tobool(t->u.ksh_func)) {
998 /* drop same-name aliases for POSIX functions */
999 if ((tp = ktsearch(&aliases, name, nhash)))
1003 while (/* CONSTCOND */ 1) {
1004 tp = findfunc(name, nhash, true);
1005 /* because findfunc:create=true */
1006 mkssert(tp != NULL);
1008 if (tp->flag & ISSET)
1011 * If this function is currently being executed, we zap
1012 * this table entry so findfunc() won't see it
1014 if (tp->flag & FINUSE) {
1016 /* ensure it won't be found */
1017 tp->flag &= ~DEFINED;
1018 tp->flag |= FDELETE;
1023 if (tp->flag & ALLOC) {
1024 tp->flag &= ~(ISSET|ALLOC);
1025 tfree(tp->val.t, tp->areap);
1031 return (was_set ? 0 : 1);
1034 tp->val.t = tcopy(t->left, tp->areap);
1035 tp->flag |= (ISSET|ALLOC);
1046 builtin(const char *name, int (*func) (const char **))
1049 uint32_t flag = DEFINED;
1051 /* see if any flags should be set for this builtin */
1054 /* command does variable assignment */
1056 else if (*name == '*')
1057 /* POSIX special builtin */
1064 tp = ktenter(&builtins, name, hash(name));
1074 * either function, hashed command, or built-in (in that order)
1077 findcom(const char *name, int flags)
1079 static struct tbl temp;
1080 uint32_t h = hash(name);
1081 struct tbl *tp = NULL, *tbi;
1082 /* insert if not found */
1083 unsigned char insert = Flag(FTRACKALL);
1084 /* for function autoloading */
1086 union mksh_cchack npath;
1088 if (vstrchr(name, '/')) {
1090 /* prevent FPATH search below */
1094 tbi = (flags & FC_BI) ? ktsearch(&builtins, name, h) : NULL;
1096 * POSIX says special builtins first, then functions, then
1097 * regular builtins, then search path...
1099 if ((flags & FC_SPECBI) && tbi && (tbi->flag & SPEC_BI))
1101 if (!tp && (flags & FC_FUNC)) {
1102 tp = findfunc(name, h, false);
1103 if (tp && !(tp->flag & ISSET)) {
1104 if ((fpath = str_val(global("FPATH"))) == null) {
1106 tp->u2.errnov = ENOENT;
1108 tp->u.fpath = search_path(name, fpath, R_OK,
1112 if (!tp && (flags & FC_NORMBI) && tbi)
1114 if (!tp && (flags & FC_PATH) && !(flags & FC_DEFPATH)) {
1115 tp = ktsearch(&taliases, name, h);
1116 if (tp && (tp->flag & ISSET) &&
1117 ksh_access(tp->val.s, X_OK) != 0) {
1118 if (tp->flag & ALLOC) {
1120 afree(tp->val.s, APERM);
1127 if ((!tp || (tp->type == CTALIAS && !(tp->flag&ISSET))) &&
1128 (flags & FC_PATH)) {
1130 if (insert && !(flags & FC_DEFPATH)) {
1131 tp = ktenter(&taliases, name, h);
1140 npath.ro = search_path(name,
1141 (flags & FC_DEFPATH) ? def_path : path,
1142 X_OK, &tp->u2.errnov);
1144 strdupx(tp->val.s, npath.ro, APERM);
1145 if (npath.ro != name)
1146 afree(npath.rw, ATEMP);
1147 tp->flag |= ISSET|ALLOC;
1148 } else if ((flags & FC_FUNC) &&
1149 (fpath = str_val(global("FPATH"))) != null &&
1150 (npath.ro = search_path(name, fpath, R_OK,
1151 &tp->u2.errnov)) != NULL) {
1153 * An undocumented feature of AT&T ksh is that
1154 * it searches FPATH if a command is not found,
1155 * even if the command hasn't been set up as an
1156 * autoloaded function (ie, no typeset -uf).
1162 tp->u.fpath = npath.ro;
1169 * flush executable commands with relative paths
1170 * (just relative or all?)
1178 for (ktwalk(&ts, &taliases); (tp = ktnext(&ts)) != NULL; )
1179 if ((tp->flag&ISSET) && (all || tp->val.s[0] != '/')) {
1180 if (tp->flag&ALLOC) {
1181 tp->flag &= ~(ALLOC|ISSET);
1182 afree(tp->val.s, APERM);
1188 /* check if path is something we want to find */
1190 search_access(const char *fn, int mode)
1194 if (stat(fn, &sb) < 0)
1195 /* file does not exist */
1197 /* LINTED use of access */
1198 if (access(fn, mode) < 0)
1199 /* file exists, but we can't access it */
1201 if (mode == X_OK && (!S_ISREG(sb.st_mode) ||
1202 !(sb.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH))))
1203 /* access(2) may say root can execute everything */
1204 return (S_ISDIR(sb.st_mode) ? EISDIR : EACCES);
1209 * search for command with PATH
1212 search_path(const char *name, const char *lpath,
1215 /* set if candidate found, but not suitable */
1224 if (vstrchr(name, '/')) {
1225 if ((ec = search_access(name, mode)) == 0) {
1231 goto search_path_err;
1234 namelen = strlen(name) + 1;
1235 Xinit(xs, xp, 128, ATEMP);
1238 while (sp != NULL) {
1239 xp = Xstring(xs, xp);
1240 if (!(p = cstrchr(sp, ':')))
1241 p = sp + strlen(sp);
1243 XcheckN(xs, xp, p - sp);
1244 memcpy(xp, sp, p - sp);
1249 XcheckN(xs, xp, namelen);
1250 memcpy(xp, name, namelen);
1251 if ((ev = search_access(Xstring(xs, xp), mode)) == 0) {
1252 name = Xclose(xs, xp + namelen);
1253 goto search_path_ok;
1255 /* accumulate non-ENOENT errors only */
1256 if (ev != ENOENT && ec == 0)
1264 *errnop = ec ? ec : ENOENT;
1269 call_builtin(struct tbl *tp, const char **wp, const char *where)
1274 internal_errorf("%s: %s", where, wp[0]);
1275 builtin_argv0 = wp[0];
1276 builtin_flag = tp->flag;
1277 shf_reopen(1, SHF_WR, shl_stdout);
1278 shl_stdout_ok = true;
1279 ksh_getopt_reset(&builtin_opt, GF_ERROR);
1280 rv = (*tp->val.f)(wp);
1281 shf_flush(shl_stdout);
1282 shl_stdout_ok = false;
1284 builtin_argv0 = NULL;
1289 * set up redirection, saving old fds in e->savefd
1292 iosetup(struct ioword *iop, struct tbl *tp)
1295 char *cp = iop->name;
1296 int iotype = iop->flag & IOTYPE;
1297 bool do_open = true, do_close = false;
1299 struct ioword iotmp;
1302 if (iotype != IOHERE)
1303 cp = evalonestr(cp, DOTILDE|(Flag(FTALKING_I) ? DOGLOB : 0));
1305 /* Used for tracing and error messages to print expanded cp */
1307 iotmp.name = (iotype == IOHERE) ? NULL : cp;
1308 iotmp.flag |= IONAMEXP;
1310 if (Flag(FXTRACE)) {
1311 change_xtrace(2, false);
1312 fptreef(shl_xtrace, 0, "%R", &iotmp);
1313 change_xtrace(1, false);
1322 flags = O_WRONLY | O_APPEND | O_CREAT;
1326 flags = O_WRONLY | O_CREAT | O_TRUNC;
1328 * The stat() is here to allow redirections to
1329 * things like /dev/null without error.
1331 if (Flag(FNOCLOBBER) && !(iop->flag & IOCLOB) &&
1332 (stat(cp, &statb) < 0 || S_ISREG(statb.st_mode)))
1337 flags = O_RDWR | O_CREAT;
1342 /* herein() returns -2 if error has been printed */
1343 u = herein(iop, NULL);
1344 /* cp may have wrong name */
1351 if (*cp == '-' && !cp[1]) {
1352 /* prevent error return below */
1355 } else if ((u = check_fd(cp,
1356 X_OK | ((iop->flag & IORDUP) ? R_OK : W_OK),
1360 warningf(true, "%s: %s",
1361 (sp = snptreef(NULL, 32, "%R", &iotmp)), emsg);
1366 /* "dup from" == "dup to" */
1373 if (Flag(FRESTRICTED) && (flags & O_CREAT)) {
1374 warningf(true, "%s: %s", cp, "restricted");
1377 u = open(cp, flags, 0666);
1380 /* herein() may already have printed message */
1383 warningf(true, "can't %s %s: %s",
1384 iotype == IODUP ? "dup" :
1385 (iotype == IOREAD || iotype == IOHERE) ?
1386 "open" : "create", cp, cstrerror(u));
1390 /* Do not save if it has already been redirected (i.e. "cat >x >y"). */
1391 if (e->savefd[iop->unit] == 0) {
1392 /* If these are the same, it means unit was previously closed */
1394 e->savefd[iop->unit] = -1;
1397 * c_exec() assumes e->savefd[fd] set for any
1398 * redirections. Ask savefd() not to close iop->unit;
1399 * this allows error messages to be seen if iop->unit
1400 * is 2; also means we can't lose the fd (eg, both
1401 * dup2 below and dup2 in restfd() failing).
1403 e->savefd[iop->unit] = savefd(iop->unit);
1408 else if (u != iop->unit) {
1409 if (ksh_dup2(u, iop->unit, true) < 0) {
1414 warningf(true, "%s %s %s",
1415 "can't finish (dup) redirection",
1416 (sp = snptreef(NULL, 32, "%R", &iotmp)),
1419 if (iotype != IODUP)
1423 if (iotype != IODUP)
1426 * Touching any co-process fd in an empty exec
1427 * causes the shell to close its copies
1429 else if (tp && tp->type == CSHELL && tp->val.f == c_exec) {
1430 if (iop->flag & IORDUP)
1431 /* possible exec <&p */
1432 coproc_read_close(u);
1434 /* possible exec >&p */
1435 coproc_write_close(u);
1439 /* Clear any write errors */
1440 shf_reopen(2, SHF_WR, shl_out);
1445 * Process here documents by providing the content, either as
1446 * result (globally allocated) string or in a temp file; if
1447 * unquoted, the string is expanded first.
1450 hereinval(const char *content, int sub, char **resbuf, struct shf *shf)
1452 const char * volatile ccp = content;
1453 struct source *s, *osource;
1457 if (kshsetjmp(e->jbuf)) {
1460 /* special to iosetup(): don't print error */
1464 /* do substitutions on the content of heredoc */
1465 s = pushs(SSTRING, ATEMP);
1466 s->start = s->str = ccp;
1468 if (yylex(sub) != LWORD)
1469 internal_errorf("%s: %s", "herein", "yylex");
1471 ccp = evalstr(yylval.cp, 0);
1477 strdupx(*resbuf, ccp, APERM);
1484 herein(struct ioword *iop, char **resbuf)
1491 /* ksh -c 'cat << EOF' can cause this... */
1492 if (iop->heredoc == NULL) {
1493 warningf(true, "%s missing", "here document");
1494 /* special to iosetup(): don't print error */
1498 /* lexer substitution flags */
1499 i = (iop->flag & IOEVAL) ? (ONEWORD | HEREDOC) : 0;
1501 /* skip all the fd setup if we just want the value */
1503 return (hereinval(iop->heredoc, i, resbuf, NULL));
1506 * Create temp file to hold content (done before newenv
1507 * so temp doesn't get removed too soon).
1509 h = maketemp(ATEMP, TT_HEREDOC_EXP, &e->temps);
1510 if (!(shf = h->shf) || (fd = open(h->tffn, O_RDONLY, 0)) < 0) {
1512 warningf(true, "can't %s temporary file %s: %s",
1513 !shf ? "create" : "open", h->tffn, cstrerror(i));
1516 /* special to iosetup(): don't print error */
1520 if (hereinval(iop->heredoc, i, NULL, shf) == -2) {
1522 /* special to iosetup(): don't print error */
1526 if (shf_close(shf) == EOF) {
1529 warningf(true, "can't %s temporary file %s: %s",
1530 "write", h->tffn, cstrerror(i));
1531 /* special to iosetup(): don't print error */
1539 * ksh special - the select command processing section
1540 * print the args in column form - assuming that we can
1543 do_selectargs(const char **ap, bool print_menu)
1545 static const char *read_args[] = {
1546 "read", "-r", "REPLY", NULL
1551 for (argct = 0; ap[argct]; argct++)
1553 while (/* CONSTCOND */ 1) {
1555 * Menu is printed if
1556 * - this is the first time around the select loop
1557 * - the user enters a blank line
1558 * - the REPLY parameter is empty
1560 if (print_menu || !*str_val(global("REPLY")))
1562 shellf("%s", str_val(global("PS3")));
1563 if (call_builtin(findcom("read", FC_BI), read_args, Tselect))
1565 s = str_val(global("REPLY"));
1566 if (*s && getn(s, &i))
1567 return ((i >= 1 && i <= argct) ? ap[i - 1] : null);
1572 struct select_menu_info {
1573 const char * const *args;
1577 /* format a single select menu item */
1579 select_fmt_entry(char *buf, size_t buflen, unsigned int i, const void *arg)
1581 const struct select_menu_info *smi =
1582 (const struct select_menu_info *)arg;
1584 shf_snprintf(buf, buflen, "%*u) %s",
1585 smi->num_width, i + 1, smi->args[i]);
1590 * print a select style menu
1593 pr_menu(const char * const *ap)
1595 struct select_menu_info smi;
1596 const char * const *pp;
1597 size_t acols = 0, aocts = 0, i;
1601 * width/column calculations were done once and saved, but this
1602 * means select can't be used recursively so we re-calculate
1603 * each time (could save in a structure that is returned, but
1604 * it's probably not worth the bother)
1608 * get dimensions of the list
1610 for (n = 0, pp = ap; *pp; n++, pp++) {
1614 i = utf_mbswidth(*pp);
1620 * we will print an index of the form "%d) " in front of
1621 * each entry, so get the maximum width of this
1623 for (i = n, smi.num_width = 1; i >= 10; i /= 10)
1627 print_columns(shl_out, n, select_fmt_entry, (void *)&smi,
1628 smi.num_width + 2 + aocts, smi.num_width + 2 + acols,
1633 plain_fmt_entry(char *buf, size_t buflen, unsigned int i, const void *arg)
1635 strlcpy(buf, ((const char * const *)arg)[i], buflen);
1640 pr_list(char * const *ap)
1642 size_t acols = 0, aocts = 0, i;
1646 for (n = 0, pp = ap; *pp; n++, pp++) {
1650 i = utf_mbswidth(*pp);
1655 print_columns(shl_out, n, plain_fmt_entry, (const void *)ap,
1656 aocts, acols, false);
1660 * [[ ... ]] evaluation routines
1664 * Test if the current token is a whatever. Accepts the current token if
1665 * it is. Returns 0 if it is not, non-zero if it is (in the case of
1666 * TM_UNOP and TM_BINOP, the returned value is a Test_op).
1669 dbteste_isa(Test_env *te, Test_meta meta)
1671 Test_op ret = TO_NONOP;
1676 return (meta == TM_END ? TO_NONNULL : TO_NONOP);
1678 /* unquoted word? */
1679 for (p = *te->pos.wp; *p == CHAR; p += 2)
1683 if (meta == TM_UNOP || meta == TM_BINOP) {
1685 /* longer than the longest operator */
1690 while (*p++ == CHAR &&
1691 (size_t)(q - buf) < sizeof(buf) - 1)
1694 ret = test_isop(meta, buf);
1696 } else if (meta == TM_END)
1699 ret = (uqword && !strcmp(*te->pos.wp,
1700 dbtest_tokens[(int)meta])) ? TO_NONNULL : TO_NONOP;
1702 /* Accept the token? */
1703 if (ret != TO_NONOP)
1710 dbteste_getopnd(Test_env *te, Test_op op, bool do_eval)
1712 const char *s = *te->pos.wp;
1722 if (op == TO_STEQL || op == TO_STNEQ)
1723 s = evalstr(s, DOTILDE | DOPAT);
1725 s = evalstr(s, DOTILDE);
1731 dbteste_error(Test_env *te, int offset, const char *msg)
1733 te->flags |= TEF_ERROR;
1734 internal_warningf("dbteste_error: %s (offset %d)", msg, offset);