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,
5 * 2011, 2012, 2013, 2014
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.133 2014/10/03 17:32:11 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 |
465 ARRAY | INT_U | INT_L;
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 } else if (tp->val.f == c_trap) {
608 t->u.evalflags &= ~DOTCOMEXEC;
612 tp = findcom(ap[0], fcflags & (FC_BI|FC_FUNC));
614 if (t->u.evalflags & DOTCOMEXEC)
617 if (keepasn_ok && (!ap[0] || (tp && (tp->flag & KEEPASN))))
620 /* create new variable/function block */
622 /* ksh functions don't keep assignments, POSIX functions do. */
623 if (keepasn_ok && tp && tp->type == CFUNC &&
624 !(tp->flag & FKSH)) {
625 bourne_function_call = true;
628 type_flags = LOCAL|LOCAL_COPY|EXPORT;
632 type_flags |= EXPORT;
634 change_xtrace(2, false);
635 for (i = 0; t->vars[i]; i++) {
636 /* do NOT lookup in the new var/fn block just created */
638 cp = evalstr(t->vars[i], DOASNTILDE | DOASNFIELD);
643 ccp = skip_varname(cp, true);
648 shf_write(cp, ccp - cp, shl_xtrace);
649 print_value_quoted(shl_xtrace, ccp);
650 shf_putc(' ', shl_xtrace);
652 /* but assign in there as usual */
653 typeset(cp, type_flags, 0, 0, 0);
654 if (bourne_function_call && !(type_flags & EXPORT))
655 typeset(cp, LOCAL | LOCAL_COPY | EXPORT, 0, 0, 0);
659 change_xtrace(2, false);
662 print_value_quoted(shl_xtrace, ap[rv]);
664 shf_putc(' ', shl_xtrace);
668 change_xtrace(1, false);
671 if ((cp = *ap) == NULL) {
675 if (Flag(FRESTRICTED) && vstrchr(cp, '/')) {
676 warningf(true, "%s: %s", cp, "restricted");
680 tp = findcom(cp, fcflags);
687 rv = call_builtin(tp, (const char **)ap, null);
688 if (!keepasn_ok && tp->val.f == c_shift) {
689 l_expand->argc = l_assign->argc;
690 l_expand->argv = l_assign->argv;
696 volatile unsigned char old_xflag;
697 volatile uint32_t old_inuse;
698 const char * volatile old_kshname;
700 if (!(tp->flag & ISSET)) {
704 rv = (tp->u2.errnov == ENOENT) ? 127 : 126;
705 warningf(true, "%s: %s %s: %s", cp,
706 "can't find", "function definition file",
707 cstrerror(tp->u2.errnov));
710 if (include(tp->u.fpath, 0, NULL, false) < 0) {
711 warningf(true, "%s: %s %s %s: %s", cp,
712 "can't open", "function definition file",
713 tp->u.fpath, cstrerror(errno));
717 if (!(ftp = findfunc(cp, hash(cp), false)) ||
718 !(ftp->flag & ISSET)) {
719 warningf(true, "%s: %s %s", cp,
720 "function not defined by", tp->u.fpath);
728 * ksh functions set $0 to function name, POSIX
729 * functions leave $0 unchanged.
731 old_kshname = kshname;
737 for (i = 0; *ap++ != NULL; i++)
739 e->loc->argc = i - 1;
741 * ksh-style functions handle getopts sanely,
742 * Bourne/POSIX functions are insane...
744 if (tp->flag & FKSH) {
745 e->loc->flags |= BF_DOGETOPTS;
746 e->loc->getopts_state = user_opt;
750 old_xflag = Flag(FXTRACE) ? 1 : 0;
751 change_xtrace((Flag(FXTRACEREC) ? old_xflag : 0) |
752 ((tp->flag & TRACE) ? 1 : 0), false);
753 old_inuse = tp->flag & FINUSE;
757 if (!(i = kshsetjmp(e->jbuf))) {
758 execute(tp->val.t, flags & XERROK, NULL);
762 kshname = old_kshname;
763 change_xtrace(old_xflag, false);
764 tp->flag = (tp->flag & ~FINUSE) | old_inuse;
767 * Were we deleted while executing? If so, free the
768 * execution tree. TODO: Unfortunately, the table entry
769 * is never re-used until the lookup table is expanded.
771 if ((tp->flag & (FDELETE|FINUSE)) == FDELETE) {
772 if (tp->flag & ALLOC) {
774 tfree(tp->val.t, tp->areap);
792 internal_errorf("%s %d", "CFUNC", i);
797 /* executable command */
801 if (!(tp->flag&ISSET)) {
802 if (tp->u2.errnov == ENOENT) {
804 warningf(true, "%s: %s", cp, "not found");
807 warningf(true, "%s: %s: %s", cp, "can't execute",
808 cstrerror(tp->u2.errnov));
813 /* set $_ to programme's full path */
814 /* setstr() can't fail here */
815 setstr(typeset("_", LOCAL | EXPORT, 0, INTEGER, 0),
816 tp->val.s, KSH_RETURN_ERROR);
821 #ifndef MKSH_UNEMPLOYED
825 setexecsig(&sigtraps[SIGINT], SS_RESTORE_ORIG);
826 setexecsig(&sigtraps[SIGQUIT], SS_RESTORE_ORIG);
830 /* to fork we set up a TEXEC node and call execute */
834 texec.str = tp->val.s;
836 rv = exchild(&texec, flags, xerrok, -1);
848 scriptexec(struct op *tp, const char **ap)
853 /* 64 == MAXINTERP in MirBSD <sys/param.h> */
857 union mksh_ccphack args, cap;
859 sh = str_val(global("EXECSHELL"));
861 sh = search_path(sh, path, X_OK, NULL);
863 sh = MKSH_DEFAULT_EXECSHELL;
865 *tp->args-- = tp->str;
868 if ((fd = open(tp->str, O_RDONLY | O_BINARY)) >= 0) {
869 /* read first MAXINTERP octets from file */
870 if (read(fd, buf, sizeof(buf)) <= 0)
871 /* read error -> no good */
875 /* skip UTF-8 Byte Order Mark, if present */
876 cp = (unsigned char *)buf;
877 if ((cp[0] == 0xEF) && (cp[1] == 0xBB) && (cp[2] == 0xBF))
879 /* save begin of shebang for later */
880 fd = (char *)cp - buf; /* either 0 or (if BOM) 3 */
882 /* scan for newline (or CR) or NUL _before_ end of buffer */
883 while ((size_t)((char *)cp - buf) < sizeof(buf))
884 if (*cp == '\0' || *cp == '\n' || *cp == '\r') {
889 /* if the shebang line is longer than MAXINTERP, bail out */
890 if ((size_t)((char *)cp - buf) >= sizeof(buf))
893 /* restore begin of shebang position (buf+0 or buf+3) */
894 cp = (unsigned char *)(buf + fd);
895 /* bail out if read error (above) or no shebang */
896 if ((cp[0] != '#') || (cp[1] != '!'))
900 /* skip whitespace before shell name */
901 while (*cp == ' ' || *cp == '\t')
903 /* just whitespace on the line? */
906 /* no, we actually found an interpreter name */
908 /* look for end of shell/interpreter name */
909 while (*cp != ' ' && *cp != '\t' && *cp != '\0')
914 /* skip spaces before arguments */
915 while (*cp == ' ' || *cp == '\t')
917 /* pass it all in ONE argument (historic reasons) */
919 *tp->args-- = (char *)cp;
922 if (buf[0] == 0x7F && buf[1] == 'E' && buf[2] == 'L' &&
924 errorf("%s: not executable: %d-bit ELF file", tp->str,
925 32 * ((uint8_t)buf[4]));
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 == /* "MZ" */ 0x4D5A) ||
935 (fd == /* gzip */ 0x1F8B))
936 errorf("%s: not executable: magic %04X", tp->str, fd);
943 execve(args.rw[0], args.rw, cap.rw);
945 /* report both the programme that was run and the bogus interpreter */
946 errorf("%s: %s: %s", tp->str, sh, cstrerror(errno));
950 shcomexec(const char **wp)
954 tp = ktsearch(&builtins, *wp, hash(*wp));
955 return (call_builtin(tp, wp, "shcomexec"));
959 * Search function tables for a function. If create set, a table entry
960 * is created if none is found.
963 findfunc(const char *name, uint32_t h, bool create)
966 struct tbl *tp = NULL;
968 for (l = e->loc; l; l = l->next) {
969 tp = ktsearch(&l->funs, name, h);
972 if (!l->next && create) {
973 tp = ktenter(&l->funs, name, h);
984 * define function. Returns 1 if function is being undefined (t == 0) and
985 * function did not exist, returns 0 otherwise.
988 define(const char *name, struct op *t)
992 bool was_set = false;
996 if (t != NULL && !tobool(t->u.ksh_func)) {
997 /* drop same-name aliases for POSIX functions */
998 if ((tp = ktsearch(&aliases, name, nhash)))
1002 while (/* CONSTCOND */ 1) {
1003 tp = findfunc(name, nhash, true);
1004 /* because findfunc:create=true */
1005 mkssert(tp != NULL);
1007 if (tp->flag & ISSET)
1010 * If this function is currently being executed, we zap
1011 * this table entry so findfunc() won't see it
1013 if (tp->flag & FINUSE) {
1015 /* ensure it won't be found */
1016 tp->flag &= ~DEFINED;
1017 tp->flag |= FDELETE;
1022 if (tp->flag & ALLOC) {
1023 tp->flag &= ~(ISSET|ALLOC);
1024 tfree(tp->val.t, tp->areap);
1030 return (was_set ? 0 : 1);
1033 tp->val.t = tcopy(t->left, tp->areap);
1034 tp->flag |= (ISSET|ALLOC);
1045 builtin(const char *name, int (*func) (const char **))
1048 uint32_t flag = DEFINED;
1050 /* see if any flags should be set for this builtin */
1053 /* command does variable assignment */
1055 else if (*name == '*')
1056 /* POSIX special builtin */
1063 tp = ktenter(&builtins, name, hash(name));
1073 * either function, hashed command, or built-in (in that order)
1076 findcom(const char *name, int flags)
1078 static struct tbl temp;
1079 uint32_t h = hash(name);
1080 struct tbl *tp = NULL, *tbi;
1081 /* insert if not found */
1082 unsigned char insert = Flag(FTRACKALL);
1083 /* for function autoloading */
1085 union mksh_cchack npath;
1087 if (vstrchr(name, '/')) {
1089 /* prevent FPATH search below */
1093 tbi = (flags & FC_BI) ? ktsearch(&builtins, name, h) : NULL;
1095 * POSIX says special builtins first, then functions, then
1096 * regular builtins, then search path...
1098 if ((flags & FC_SPECBI) && tbi && (tbi->flag & SPEC_BI))
1100 if (!tp && (flags & FC_FUNC)) {
1101 tp = findfunc(name, h, false);
1102 if (tp && !(tp->flag & ISSET)) {
1103 if ((fpath = str_val(global("FPATH"))) == null) {
1105 tp->u2.errnov = ENOENT;
1107 tp->u.fpath = search_path(name, fpath, R_OK,
1111 if (!tp && (flags & FC_NORMBI) && tbi)
1113 if (!tp && (flags & FC_PATH) && !(flags & FC_DEFPATH)) {
1114 tp = ktsearch(&taliases, name, h);
1115 if (tp && (tp->flag & ISSET) &&
1116 ksh_access(tp->val.s, X_OK) != 0) {
1117 if (tp->flag & ALLOC) {
1119 afree(tp->val.s, APERM);
1126 if ((!tp || (tp->type == CTALIAS && !(tp->flag&ISSET))) &&
1127 (flags & FC_PATH)) {
1129 if (insert && !(flags & FC_DEFPATH)) {
1130 tp = ktenter(&taliases, name, h);
1139 npath.ro = search_path(name,
1140 (flags & FC_DEFPATH) ? def_path : path,
1141 X_OK, &tp->u2.errnov);
1143 strdupx(tp->val.s, npath.ro, APERM);
1144 if (npath.ro != name)
1145 afree(npath.rw, ATEMP);
1146 tp->flag |= ISSET|ALLOC;
1147 } else if ((flags & FC_FUNC) &&
1148 (fpath = str_val(global("FPATH"))) != null &&
1149 (npath.ro = search_path(name, fpath, R_OK,
1150 &tp->u2.errnov)) != NULL) {
1152 * An undocumented feature of AT&T ksh is that
1153 * it searches FPATH if a command is not found,
1154 * even if the command hasn't been set up as an
1155 * autoloaded function (ie, no typeset -uf).
1161 tp->u.fpath = npath.ro;
1168 * flush executable commands with relative paths
1169 * (just relative or all?)
1177 for (ktwalk(&ts, &taliases); (tp = ktnext(&ts)) != NULL; )
1178 if ((tp->flag&ISSET) && (all || tp->val.s[0] != '/')) {
1179 if (tp->flag&ALLOC) {
1180 tp->flag &= ~(ALLOC|ISSET);
1181 afree(tp->val.s, APERM);
1187 /* check if path is something we want to find */
1189 search_access(const char *fn, int mode)
1193 if (stat(fn, &sb) < 0)
1194 /* file does not exist */
1196 /* LINTED use of access */
1197 if (access(fn, mode) < 0)
1198 /* file exists, but we can't access it */
1200 if (mode == X_OK && (!S_ISREG(sb.st_mode) ||
1201 !(sb.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH))))
1202 /* access(2) may say root can execute everything */
1203 return (S_ISDIR(sb.st_mode) ? EISDIR : EACCES);
1208 * search for command with PATH
1211 search_path(const char *name, const char *lpath,
1214 /* set if candidate found, but not suitable */
1223 if (vstrchr(name, '/')) {
1224 if ((ec = search_access(name, mode)) == 0) {
1230 goto search_path_err;
1233 namelen = strlen(name) + 1;
1234 Xinit(xs, xp, 128, ATEMP);
1237 while (sp != NULL) {
1238 xp = Xstring(xs, xp);
1239 if (!(p = cstrchr(sp, ':')))
1240 p = sp + strlen(sp);
1242 XcheckN(xs, xp, p - sp);
1243 memcpy(xp, sp, p - sp);
1248 XcheckN(xs, xp, namelen);
1249 memcpy(xp, name, namelen);
1250 if ((ev = search_access(Xstring(xs, xp), mode)) == 0) {
1251 name = Xclose(xs, xp + namelen);
1252 goto search_path_ok;
1254 /* accumulate non-ENOENT errors only */
1255 if (ev != ENOENT && ec == 0)
1263 *errnop = ec ? ec : ENOENT;
1268 call_builtin(struct tbl *tp, const char **wp, const char *where)
1273 internal_errorf("%s: %s", where, wp[0]);
1274 builtin_argv0 = wp[0];
1275 builtin_flag = tp->flag;
1276 shf_reopen(1, SHF_WR, shl_stdout);
1277 shl_stdout_ok = true;
1278 ksh_getopt_reset(&builtin_opt, GF_ERROR);
1279 rv = (*tp->val.f)(wp);
1280 shf_flush(shl_stdout);
1281 shl_stdout_ok = false;
1283 builtin_argv0 = NULL;
1288 * set up redirection, saving old fds in e->savefd
1291 iosetup(struct ioword *iop, struct tbl *tp)
1294 char *cp = iop->name;
1295 int iotype = iop->flag & IOTYPE;
1296 bool do_open = true, do_close = false;
1298 struct ioword iotmp;
1301 if (iotype != IOHERE)
1302 cp = evalonestr(cp, DOTILDE|(Flag(FTALKING_I) ? DOGLOB : 0));
1304 /* Used for tracing and error messages to print expanded cp */
1306 iotmp.name = (iotype == IOHERE) ? NULL : cp;
1307 iotmp.flag |= IONAMEXP;
1309 if (Flag(FXTRACE)) {
1310 change_xtrace(2, false);
1311 fptreef(shl_xtrace, 0, "%R", &iotmp);
1312 change_xtrace(1, false);
1321 flags = O_WRONLY | O_APPEND | O_CREAT;
1325 flags = O_WRONLY | O_CREAT | O_TRUNC;
1327 * The stat() is here to allow redirections to
1328 * things like /dev/null without error.
1330 if (Flag(FNOCLOBBER) && !(iop->flag & IOCLOB) &&
1331 (stat(cp, &statb) < 0 || S_ISREG(statb.st_mode)))
1336 flags = O_RDWR | O_CREAT;
1341 /* herein() returns -2 if error has been printed */
1342 u = herein(iop, NULL);
1343 /* cp may have wrong name */
1350 if (*cp == '-' && !cp[1]) {
1351 /* prevent error return below */
1354 } else if ((u = check_fd(cp,
1355 X_OK | ((iop->flag & IORDUP) ? R_OK : W_OK),
1359 warningf(true, "%s: %s",
1360 (sp = snptreef(NULL, 32, "%R", &iotmp)), emsg);
1365 /* "dup from" == "dup to" */
1372 if (Flag(FRESTRICTED) && (flags & O_CREAT)) {
1373 warningf(true, "%s: %s", cp, "restricted");
1376 u = open(cp, flags | O_BINARY, 0666);
1379 /* herein() may already have printed message */
1382 warningf(true, "can't %s %s: %s",
1383 iotype == IODUP ? "dup" :
1384 (iotype == IOREAD || iotype == IOHERE) ?
1385 "open" : "create", cp, cstrerror(u));
1389 /* Do not save if it has already been redirected (i.e. "cat >x >y"). */
1390 if (e->savefd[iop->unit] == 0) {
1391 /* If these are the same, it means unit was previously closed */
1393 e->savefd[iop->unit] = -1;
1396 * c_exec() assumes e->savefd[fd] set for any
1397 * redirections. Ask savefd() not to close iop->unit;
1398 * this allows error messages to be seen if iop->unit
1399 * is 2; also means we can't lose the fd (eg, both
1400 * dup2 below and dup2 in restfd() failing).
1402 e->savefd[iop->unit] = savefd(iop->unit);
1407 else if (u != iop->unit) {
1408 if (ksh_dup2(u, iop->unit, true) < 0) {
1413 warningf(true, "%s %s %s",
1414 "can't finish (dup) redirection",
1415 (sp = snptreef(NULL, 32, "%R", &iotmp)),
1418 if (iotype != IODUP)
1422 if (iotype != IODUP)
1425 * Touching any co-process fd in an empty exec
1426 * causes the shell to close its copies
1428 else if (tp && tp->type == CSHELL && tp->val.f == c_exec) {
1429 if (iop->flag & IORDUP)
1430 /* possible exec <&p */
1431 coproc_read_close(u);
1433 /* possible exec >&p */
1434 coproc_write_close(u);
1438 /* Clear any write errors */
1439 shf_reopen(2, SHF_WR, shl_out);
1444 * Process here documents by providing the content, either as
1445 * result (globally allocated) string or in a temp file; if
1446 * unquoted, the string is expanded first.
1449 hereinval(const char *content, int sub, char **resbuf, struct shf *shf)
1451 const char * volatile ccp = content;
1452 struct source *s, *osource;
1456 if (kshsetjmp(e->jbuf)) {
1459 /* special to iosetup(): don't print error */
1463 /* do substitutions on the content of heredoc */
1464 s = pushs(SSTRING, ATEMP);
1465 s->start = s->str = ccp;
1467 if (yylex(sub) != LWORD)
1468 internal_errorf("%s: %s", "herein", "yylex");
1470 ccp = evalstr(yylval.cp, 0);
1476 strdupx(*resbuf, ccp, APERM);
1483 herein(struct ioword *iop, char **resbuf)
1490 /* ksh -c 'cat << EOF' can cause this... */
1491 if (iop->heredoc == NULL) {
1492 warningf(true, "%s missing", "here document");
1493 /* special to iosetup(): don't print error */
1497 /* lexer substitution flags */
1498 i = (iop->flag & IOEVAL) ? (ONEWORD | HEREDOC) : 0;
1500 /* skip all the fd setup if we just want the value */
1502 return (hereinval(iop->heredoc, i, resbuf, NULL));
1505 * Create temp file to hold content (done before newenv
1506 * so temp doesn't get removed too soon).
1508 h = maketemp(ATEMP, TT_HEREDOC_EXP, &e->temps);
1509 if (!(shf = h->shf) || (fd = open(h->tffn, O_RDONLY | O_BINARY, 0)) < 0) {
1511 warningf(true, "can't %s temporary file %s: %s",
1512 !shf ? "create" : "open", h->tffn, cstrerror(i));
1515 /* special to iosetup(): don't print error */
1519 if (hereinval(iop->heredoc, i, NULL, shf) == -2) {
1521 /* special to iosetup(): don't print error */
1525 if (shf_close(shf) == EOF) {
1528 warningf(true, "can't %s temporary file %s: %s",
1529 "write", h->tffn, cstrerror(i));
1530 /* special to iosetup(): don't print error */
1538 * ksh special - the select command processing section
1539 * print the args in column form - assuming that we can
1542 do_selectargs(const char **ap, bool print_menu)
1544 static const char *read_args[] = {
1545 "read", "-r", "REPLY", NULL
1550 for (argct = 0; ap[argct]; argct++)
1552 while (/* CONSTCOND */ 1) {
1554 * Menu is printed if
1555 * - this is the first time around the select loop
1556 * - the user enters a blank line
1557 * - the REPLY parameter is empty
1559 if (print_menu || !*str_val(global("REPLY")))
1561 shellf("%s", str_val(global("PS3")));
1562 if (call_builtin(findcom("read", FC_BI), read_args, Tselect))
1564 s = str_val(global("REPLY"));
1565 if (*s && getn(s, &i))
1566 return ((i >= 1 && i <= argct) ? ap[i - 1] : null);
1571 struct select_menu_info {
1572 const char * const *args;
1576 /* format a single select menu item */
1578 select_fmt_entry(char *buf, size_t buflen, unsigned int i, const void *arg)
1580 const struct select_menu_info *smi =
1581 (const struct select_menu_info *)arg;
1583 shf_snprintf(buf, buflen, "%*u) %s",
1584 smi->num_width, i + 1, smi->args[i]);
1589 * print a select style menu
1592 pr_menu(const char * const *ap)
1594 struct select_menu_info smi;
1595 const char * const *pp;
1596 size_t acols = 0, aocts = 0, i;
1600 * width/column calculations were done once and saved, but this
1601 * means select can't be used recursively so we re-calculate
1602 * each time (could save in a structure that is returned, but
1603 * it's probably not worth the bother)
1607 * get dimensions of the list
1609 for (n = 0, pp = ap; *pp; n++, pp++) {
1613 i = utf_mbswidth(*pp);
1619 * we will print an index of the form "%d) " in front of
1620 * each entry, so get the maximum width of this
1622 for (i = n, smi.num_width = 1; i >= 10; i /= 10)
1626 print_columns(shl_out, n, select_fmt_entry, (void *)&smi,
1627 smi.num_width + 2 + aocts, smi.num_width + 2 + acols,
1632 plain_fmt_entry(char *buf, size_t buflen, unsigned int i, const void *arg)
1634 strlcpy(buf, ((const char * const *)arg)[i], buflen);
1639 pr_list(char * const *ap)
1641 size_t acols = 0, aocts = 0, i;
1645 for (n = 0, pp = ap; *pp; n++, pp++) {
1649 i = utf_mbswidth(*pp);
1654 print_columns(shl_out, n, plain_fmt_entry, (const void *)ap,
1655 aocts, acols, false);
1659 * [[ ... ]] evaluation routines
1663 * Test if the current token is a whatever. Accepts the current token if
1664 * it is. Returns 0 if it is not, non-zero if it is (in the case of
1665 * TM_UNOP and TM_BINOP, the returned value is a Test_op).
1668 dbteste_isa(Test_env *te, Test_meta meta)
1670 Test_op ret = TO_NONOP;
1675 return (meta == TM_END ? TO_NONNULL : TO_NONOP);
1677 /* unquoted word? */
1678 for (p = *te->pos.wp; *p == CHAR; p += 2)
1682 if (meta == TM_UNOP || meta == TM_BINOP) {
1684 /* longer than the longest operator */
1689 while (*p++ == CHAR &&
1690 (size_t)(q - buf) < sizeof(buf) - 1)
1693 ret = test_isop(meta, buf);
1695 } else if (meta == TM_END)
1698 ret = (uqword && !strcmp(*te->pos.wp,
1699 dbtest_tokens[(int)meta])) ? TO_NONNULL : TO_NONOP;
1701 /* Accept the token? */
1702 if (ret != TO_NONOP)
1709 dbteste_getopnd(Test_env *te, Test_op op, bool do_eval)
1711 const char *s = *te->pos.wp;
1721 if (op == TO_STEQL || op == TO_STNEQ)
1722 s = evalstr(s, DOTILDE | DOPAT);
1724 s = evalstr(s, DOTILDE);
1730 dbteste_error(Test_env *te, int offset, const char *msg)
1732 te->flags |= TEF_ERROR;
1733 internal_warningf("dbteste_error: %s (offset %d)", msg, offset);