2015-10-09 18:11:19 +02:00
|
|
|
/* $OpenBSD: exec.c,v 1.52 2015/09/10 22:48:58 nicm Exp $ */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-05-16 18:59:42 +02:00
|
|
|
/*-
|
2012-04-14 18:07:48 +02:00
|
|
|
* Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
|
2017-03-11 23:58:51 +01:00
|
|
|
* 2011, 2012, 2013, 2014, 2015, 2016, 2017
|
2015-10-09 17:28:20 +02:00
|
|
|
* mirabilos <m@mirbsd.org>
|
2009-05-16 18:59:42 +02:00
|
|
|
*
|
|
|
|
* Provided that these terms and disclaimer and all copyright notices
|
|
|
|
* are retained or reproduced in an accompanying document, permission
|
|
|
|
* is granted to deal in this work without restriction, including un-
|
|
|
|
* limited rights to use, publicly perform, distribute, sell, modify,
|
|
|
|
* merge, give away, or sublicence.
|
|
|
|
*
|
|
|
|
* This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
|
|
|
|
* the utmost extent permitted by applicable law, neither express nor
|
|
|
|
* implied; without malicious intent or gross negligence. In no event
|
|
|
|
* may a licensor, author or contributor be held liable for indirect,
|
|
|
|
* direct, other damage, loss, or other issues arising in any way out
|
|
|
|
* of dealing in the work, even if advised of the possibility of such
|
|
|
|
* damage or existence of a defect, except proven that it results out
|
|
|
|
* of said person's immediate fault when using the work as intended.
|
|
|
|
*/
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
#include "sh.h"
|
|
|
|
|
2017-04-02 17:02:40 +02:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.194 2017/04/02 15:02:40 tg Exp $");
|
2010-03-27 16:29:00 +01:00
|
|
|
|
|
|
|
#ifndef MKSH_DEFAULT_EXECSHELL
|
2015-07-10 21:36:38 +02:00
|
|
|
#define MKSH_DEFAULT_EXECSHELL MKSH_UNIXROOT "/bin/sh"
|
2010-03-27 16:29:00 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-01-09 22:57:29 +01:00
|
|
|
static int comexec(struct op *, struct tbl * volatile, const char **,
|
2009-03-22 18:47:38 +01:00
|
|
|
int volatile, volatile int *);
|
2009-12-12 23:27:10 +01:00
|
|
|
static void scriptexec(struct op *, const char **) MKSH_A_NORETURN;
|
2014-10-12 23:58:53 +02:00
|
|
|
static int call_builtin(struct tbl *, const char **, const char *, bool);
|
2007-03-04 01:13:17 +01:00
|
|
|
static int iosetup(struct ioword *, struct tbl *);
|
|
|
|
static const char *do_selectargs(const char **, bool);
|
2010-07-18 00:09:40 +02:00
|
|
|
static Test_op dbteste_isa(Test_env *, Test_meta);
|
2008-04-02 00:20:20 +02:00
|
|
|
static const char *dbteste_getopnd(Test_env *, Test_op, bool);
|
2007-03-04 01:13:17 +01:00
|
|
|
static void dbteste_error(Test_env *, int, const char *);
|
2012-10-03 17:13:34 +02:00
|
|
|
/* XXX: horrible kludge to fit within the framework */
|
2015-07-05 16:43:08 +02:00
|
|
|
static void plain_fmt_entry(char *, size_t, unsigned int, const void *);
|
|
|
|
static void select_fmt_entry(char *, size_t, unsigned int, const void *);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* execute command tree
|
|
|
|
*/
|
|
|
|
int
|
2011-01-09 22:57:29 +01:00
|
|
|
execute(struct op * volatile t,
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/* if XEXEC don't fork */
|
|
|
|
volatile int flags,
|
2009-03-22 19:28:35 +01:00
|
|
|
volatile int * volatile xerrok)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
int i;
|
2009-03-22 19:20:36 +01:00
|
|
|
volatile int rv = 0, dummy = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
int pv[2];
|
2011-08-27 20:06:52 +02:00
|
|
|
const char ** volatile ap = NULL;
|
2007-03-04 01:13:17 +01:00
|
|
|
char ** volatile up;
|
2011-01-09 22:57:29 +01:00
|
|
|
const char *s, *ccp;
|
2005-05-23 05:06:10 +02:00
|
|
|
struct ioword **iowp;
|
|
|
|
struct tbl *tp = NULL;
|
|
|
|
|
|
|
|
if (t == NULL)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-03-22 19:20:36 +01:00
|
|
|
/* Caller doesn't care if XERROK should propagate. */
|
|
|
|
if (xerrok == NULL)
|
|
|
|
xerrok = &dummy;
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
if ((flags&XFORK) && !(flags&XEXEC) && t->type != TPIPE)
|
2009-03-22 18:47:38 +01:00
|
|
|
/* run in sub-process */
|
2009-06-08 22:06:50 +02:00
|
|
|
return (exchild(t, flags & ~XTIME, xerrok, -1));
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
newenv(E_EXEC);
|
|
|
|
if (trap)
|
|
|
|
runtraps(0);
|
|
|
|
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/* we want to run an executable, do some variance checks */
|
|
|
|
if (t->type == TCOM) {
|
2016-06-26 02:09:35 +02:00
|
|
|
/*
|
|
|
|
* Clear subst_exstat before argument expansion. Used by
|
|
|
|
* null commands (see comexec() and c_eval()) and by c_set().
|
|
|
|
*/
|
|
|
|
subst_exstat = 0;
|
|
|
|
|
|
|
|
/* for $LINENO */
|
|
|
|
current_lineno = t->lineno;
|
|
|
|
|
2011-01-09 22:57:29 +01:00
|
|
|
/* check if this is 'var=<<EOF' */
|
|
|
|
if (
|
2016-06-26 02:09:35 +02:00
|
|
|
/* we have zero arguments, i.e. no program to run */
|
2011-01-09 22:57:29 +01:00
|
|
|
t->args[0] == NULL &&
|
|
|
|
/* we have exactly one variable assignment */
|
|
|
|
t->vars[0] != NULL && t->vars[1] == NULL &&
|
|
|
|
/* we have exactly one I/O redirection */
|
|
|
|
t->ioact != NULL && t->ioact[0] != NULL &&
|
|
|
|
t->ioact[1] == NULL &&
|
|
|
|
/* of type "here document" (or "here string") */
|
2015-04-12 00:03:32 +02:00
|
|
|
(t->ioact[0]->ioflag & IOTYPE) == IOHERE &&
|
2011-01-09 22:57:29 +01:00
|
|
|
/* the variable assignment begins with a valid varname */
|
|
|
|
(ccp = skip_wdvarname(t->vars[0], true)) != t->vars[0] &&
|
|
|
|
/* and has no right-hand side (i.e. "varname=") */
|
2014-10-12 21:55:01 +02:00
|
|
|
ccp[0] == CHAR && ((ccp[1] == '=' && ccp[2] == EOS) ||
|
|
|
|
/* or "varname+=" */ (ccp[1] == '+' && ccp[2] == CHAR &&
|
2016-06-26 02:09:35 +02:00
|
|
|
ccp[3] == '=' && ccp[4] == EOS))) {
|
|
|
|
char *cp, *dp;
|
|
|
|
|
|
|
|
if ((rv = herein(t->ioact[0], &cp) /*? 1 : 0*/))
|
|
|
|
cp = NULL;
|
2016-07-25 02:04:48 +02:00
|
|
|
dp = shf_smprintf(Tf_ss, evalstr(t->vars[0],
|
2016-06-26 02:09:35 +02:00
|
|
|
DOASNTILDE | DOSCALAR), rv ? null : cp);
|
|
|
|
typeset(dp, Flag(FEXPORT) ? EXPORT : 0, 0, 0, 0);
|
2011-01-09 22:57:29 +01:00
|
|
|
/* free the expanded value */
|
|
|
|
afree(cp, APERM);
|
2016-06-26 02:09:35 +02:00
|
|
|
afree(dp, ATEMP);
|
|
|
|
goto Break;
|
2011-01-09 22:57:29 +01:00
|
|
|
}
|
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
/*
|
|
|
|
* POSIX says expand command words first, then redirections,
|
2005-05-23 05:06:10 +02:00
|
|
|
* and assignments last..
|
|
|
|
*/
|
2007-03-04 01:13:17 +01:00
|
|
|
up = eval(t->args, t->u.evalflags | DOBLANK | DOGLOB | DOTILDE);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (flags & XTIME)
|
|
|
|
/* Allow option parsing (bizarre, but POSIX) */
|
2007-03-04 01:13:17 +01:00
|
|
|
timex_hook(t, &up);
|
|
|
|
ap = (const char **)up;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (ap[0])
|
|
|
|
tp = findcom(ap[0], FC_BI|FC_FUNC);
|
|
|
|
}
|
|
|
|
flags &= ~XTIME;
|
|
|
|
|
|
|
|
if (t->ioact != NULL || t->type == TPIPE || t->type == TCOPROC) {
|
2010-09-14 23:26:19 +02:00
|
|
|
e->savefd = alloc2(NUFILE, sizeof(short), ATEMP);
|
2007-01-12 11:18:22 +01:00
|
|
|
/* initialise to not redirected */
|
2009-06-08 22:06:50 +02:00
|
|
|
memset(e->savefd, 0, NUFILE * sizeof(short));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2011-02-18 23:26:13 +01:00
|
|
|
/* mark for replacement later (unless TPIPE) */
|
|
|
|
vp_pipest->flag |= INT_L;
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
/* do redirection, to be restored in quitenv() */
|
|
|
|
if (t->ioact != NULL)
|
|
|
|
for (iowp = t->ioact; *iowp != NULL; iowp++) {
|
|
|
|
if (iosetup(*iowp, tp) < 0) {
|
|
|
|
exstat = rv = 1;
|
2011-01-30 02:35:35 +01:00
|
|
|
/*
|
|
|
|
* Redirection failures for special commands
|
2005-05-23 05:06:10 +02:00
|
|
|
* cause (non-interactive) shell to exit.
|
|
|
|
*/
|
|
|
|
if (tp && tp->type == CSHELL &&
|
|
|
|
(tp->flag & SPEC_BI))
|
2007-07-22 16:01:50 +02:00
|
|
|
errorfz();
|
2005-05-23 05:06:10 +02:00
|
|
|
/* Deal with FERREXIT, quitenv(), etc. */
|
|
|
|
goto Break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (t->type) {
|
|
|
|
case TCOM:
|
2009-03-22 18:47:38 +01:00
|
|
|
rv = comexec(t, tp, (const char **)ap, flags, xerrok);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TPAREN:
|
2009-03-22 18:47:38 +01:00
|
|
|
rv = execute(t->left, flags | XFORK, xerrok);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TPIPE:
|
|
|
|
flags |= XFORK;
|
|
|
|
flags &= ~XEXEC;
|
2006-05-10 20:54:13 +02:00
|
|
|
e->savefd[0] = savefd(0);
|
|
|
|
e->savefd[1] = savefd(1);
|
2005-05-23 05:06:10 +02:00
|
|
|
while (t->type == TPIPE) {
|
|
|
|
openpipe(pv);
|
2011-01-30 02:35:35 +01:00
|
|
|
/* stdout of curr */
|
|
|
|
ksh_dup2(pv[1], 1, false);
|
2009-06-10 20:12:51 +02:00
|
|
|
/**
|
|
|
|
* Let exchild() close pv[0] in child
|
2005-05-23 05:06:10 +02:00
|
|
|
* (if this isn't done, commands like
|
2009-06-10 20:12:51 +02:00
|
|
|
* (: ; cat /etc/termcap) | sleep 1
|
|
|
|
* will hang forever).
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
2009-03-22 18:47:38 +01:00
|
|
|
exchild(t->left, flags | XPIPEO | XCCLOSE,
|
|
|
|
NULL, pv[0]);
|
2011-01-30 02:35:35 +01:00
|
|
|
/* stdin of next */
|
|
|
|
ksh_dup2(pv[0], 0, false);
|
2005-05-23 05:06:10 +02:00
|
|
|
closepipe(pv);
|
|
|
|
flags |= XPIPEI;
|
|
|
|
t = t->right;
|
|
|
|
}
|
2011-01-30 02:35:35 +01:00
|
|
|
/* stdout of last */
|
|
|
|
restfd(1, e->savefd[1]);
|
|
|
|
/* no need to re-restore this */
|
|
|
|
e->savefd[1] = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
/* Let exchild() close 0 in parent, after fork, before wait */
|
2011-02-18 23:26:13 +01:00
|
|
|
i = exchild(t, flags | XPCLOSE | XPIPEST, xerrok, 0);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (!(flags&XBGND) && !(flags&XXCOM))
|
|
|
|
rv = i;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TLIST:
|
|
|
|
while (t->type == TLIST) {
|
2009-03-22 18:47:38 +01:00
|
|
|
execute(t->left, flags & XERROK, NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
t = t->right;
|
|
|
|
}
|
2009-03-22 18:47:38 +01:00
|
|
|
rv = execute(t, flags & XERROK, xerrok);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
2009-06-10 20:12:51 +02:00
|
|
|
case TCOPROC: {
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2009-06-10 20:12:51 +02:00
|
|
|
sigset_t omask;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
/*
|
|
|
|
* Block sigchild as we are using things changed in the
|
2005-05-23 05:06:10 +02:00
|
|
|
* signal handler
|
|
|
|
*/
|
|
|
|
sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
|
|
|
|
e->type = E_ERRH;
|
2012-03-31 19:30:00 +02:00
|
|
|
if ((i = kshsetjmp(e->jbuf))) {
|
2005-05-23 05:06:10 +02:00
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
|
|
|
quitenv(NULL);
|
|
|
|
unwind(i);
|
2006-05-10 20:54:13 +02:00
|
|
|
/* NOTREACHED */
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
/* Already have a (live) co-process? */
|
|
|
|
if (coproc.job && coproc.write >= 0)
|
|
|
|
errorf("coprocess already exists");
|
|
|
|
|
|
|
|
/* Can we re-use the existing co-process pipe? */
|
|
|
|
coproc_cleanup(true);
|
|
|
|
|
|
|
|
/* do this before opening pipes, in case these fail */
|
2006-05-10 20:54:13 +02:00
|
|
|
e->savefd[0] = savefd(0);
|
|
|
|
e->savefd[1] = savefd(1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
openpipe(pv);
|
|
|
|
if (pv[0] != 0) {
|
|
|
|
ksh_dup2(pv[0], 0, false);
|
|
|
|
close(pv[0]);
|
|
|
|
}
|
|
|
|
coproc.write = pv[1];
|
|
|
|
coproc.job = NULL;
|
|
|
|
|
|
|
|
if (coproc.readw >= 0)
|
|
|
|
ksh_dup2(coproc.readw, 1, false);
|
|
|
|
else {
|
|
|
|
openpipe(pv);
|
|
|
|
coproc.read = pv[0];
|
|
|
|
ksh_dup2(pv[1], 1, false);
|
2011-01-30 02:35:35 +01:00
|
|
|
/* closed before first read */
|
|
|
|
coproc.readw = pv[1];
|
2005-05-23 05:06:10 +02:00
|
|
|
coproc.njobs = 0;
|
|
|
|
/* create new coprocess id */
|
|
|
|
++coproc.id;
|
|
|
|
}
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
2011-01-30 02:35:35 +01:00
|
|
|
/* no more need for error handler */
|
|
|
|
e->type = E_EXEC;
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
/*
|
|
|
|
* exchild() closes coproc.* in child after fork,
|
2005-05-23 05:06:10 +02:00
|
|
|
* will also increment coproc.njobs when the
|
|
|
|
* job is actually created.
|
|
|
|
*/
|
|
|
|
flags &= ~XEXEC;
|
2009-03-22 18:47:38 +01:00
|
|
|
exchild(t->left, flags | XBGND | XFORK | XCOPROC | XCCLOSE,
|
|
|
|
NULL, coproc.readw);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
2009-06-10 20:12:51 +02:00
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
case TASYNC:
|
2011-01-30 02:35:35 +01:00
|
|
|
/*
|
|
|
|
* XXX non-optimal, I think - "(foo &)", forks for (),
|
2009-06-10 20:12:51 +02:00
|
|
|
* forks again for async... parent should optimise
|
2005-05-23 05:06:10 +02:00
|
|
|
* this to "foo &"...
|
|
|
|
*/
|
2009-03-22 18:47:38 +01:00
|
|
|
rv = execute(t->left, (flags&~XEXEC)|XBGND|XFORK, xerrok);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TOR:
|
|
|
|
case TAND:
|
2016-10-03 00:21:47 +02:00
|
|
|
rv = execute(t->left, XERROK, NULL);
|
2009-03-22 18:47:38 +01:00
|
|
|
if ((rv == 0) == (t->type == TAND))
|
2013-07-21 20:36:03 +02:00
|
|
|
rv = execute(t->right, flags & XERROK, xerrok);
|
|
|
|
else {
|
|
|
|
flags |= XERROK;
|
|
|
|
if (xerrok)
|
|
|
|
*xerrok = 1;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TBANG:
|
2009-03-22 18:47:38 +01:00
|
|
|
rv = !execute(t->right, XERROK, xerrok);
|
|
|
|
flags |= XERROK;
|
|
|
|
if (xerrok)
|
|
|
|
*xerrok = 1;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
2009-06-10 20:12:51 +02:00
|
|
|
case TDBRACKET: {
|
2005-05-23 05:06:10 +02:00
|
|
|
Test_env te;
|
|
|
|
|
|
|
|
te.flags = TEF_DBRACKET;
|
|
|
|
te.pos.wp = t->args;
|
|
|
|
te.isa = dbteste_isa;
|
|
|
|
te.getopnd = dbteste_getopnd;
|
2007-05-13 20:49:00 +02:00
|
|
|
te.eval = test_eval;
|
2005-05-23 05:06:10 +02:00
|
|
|
te.error = dbteste_error;
|
|
|
|
|
|
|
|
rv = test_parse(&te);
|
|
|
|
break;
|
2009-06-10 20:12:51 +02:00
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
case TFOR:
|
2009-06-10 20:12:51 +02:00
|
|
|
case TSELECT: {
|
2005-05-23 05:06:10 +02:00
|
|
|
volatile bool is_first = true;
|
2013-04-26 21:40:45 +02:00
|
|
|
|
2007-03-04 01:13:17 +01:00
|
|
|
ap = (t->vars == NULL) ? e->loc->argv + 1 :
|
|
|
|
(const char **)eval((const char **)t->vars,
|
|
|
|
DOBLANK | DOGLOB | DOTILDE);
|
2005-05-23 05:06:10 +02:00
|
|
|
e->type = E_LOOP;
|
2012-03-31 19:30:00 +02:00
|
|
|
while ((i = kshsetjmp(e->jbuf))) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if ((e->flags&EF_BRKCONT_PASS) ||
|
|
|
|
(i != LBREAK && i != LCONTIN)) {
|
|
|
|
quitenv(NULL);
|
|
|
|
unwind(i);
|
|
|
|
} else if (i == LBREAK) {
|
|
|
|
rv = 0;
|
|
|
|
goto Break;
|
|
|
|
}
|
|
|
|
}
|
2011-01-30 02:35:35 +01:00
|
|
|
/* in case of a continue */
|
|
|
|
rv = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (t->type == TFOR) {
|
|
|
|
while (*ap != NULL) {
|
|
|
|
setstr(global(t->str), *ap++, KSH_UNWIND_ERROR);
|
2009-03-22 18:47:38 +01:00
|
|
|
rv = execute(t->left, flags & XERROK, xerrok);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2011-01-30 02:35:35 +01:00
|
|
|
} else {
|
2016-08-24 22:40:00 +02:00
|
|
|
do_TSELECT:
|
|
|
|
if ((ccp = do_selectargs(ap, is_first))) {
|
2005-05-23 05:06:10 +02:00
|
|
|
is_first = false;
|
2011-01-09 22:57:29 +01:00
|
|
|
setstr(global(t->str), ccp, KSH_UNWIND_ERROR);
|
2009-12-05 18:43:50 +01:00
|
|
|
execute(t->left, flags & XERROK, xerrok);
|
2016-08-24 22:40:00 +02:00
|
|
|
goto do_TSELECT;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2016-08-24 22:40:00 +02:00
|
|
|
rv = 1;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
break;
|
2009-06-10 20:12:51 +02:00
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
case TWHILE:
|
|
|
|
case TUNTIL:
|
|
|
|
e->type = E_LOOP;
|
2012-03-31 19:30:00 +02:00
|
|
|
while ((i = kshsetjmp(e->jbuf))) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if ((e->flags&EF_BRKCONT_PASS) ||
|
|
|
|
(i != LBREAK && i != LCONTIN)) {
|
|
|
|
quitenv(NULL);
|
|
|
|
unwind(i);
|
|
|
|
} else if (i == LBREAK) {
|
|
|
|
rv = 0;
|
|
|
|
goto Break;
|
|
|
|
}
|
|
|
|
}
|
2011-01-30 02:35:35 +01:00
|
|
|
/* in case of a continue */
|
|
|
|
rv = 0;
|
2009-03-22 18:47:38 +01:00
|
|
|
while ((execute(t->left, XERROK, NULL) == 0) ==
|
|
|
|
(t->type == TWHILE))
|
|
|
|
rv = execute(t->right, flags & XERROK, xerrok);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TIF:
|
|
|
|
case TELIF:
|
|
|
|
if (t->right == NULL)
|
2011-01-30 02:35:35 +01:00
|
|
|
/* should be error */
|
|
|
|
break;
|
2009-03-22 18:47:38 +01:00
|
|
|
rv = execute(t->left, XERROK, NULL) == 0 ?
|
|
|
|
execute(t->right->left, flags & XERROK, xerrok) :
|
|
|
|
execute(t->right->right, flags & XERROK, xerrok);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TCASE:
|
2011-05-29 04:18:57 +02:00
|
|
|
i = 0;
|
2015-02-19 22:58:19 +01:00
|
|
|
ccp = evalstr(t->str, DOTILDE | DOSCALAR);
|
2011-05-29 04:18:57 +02:00
|
|
|
for (t = t->left; t != NULL && t->type == TPAT; t = t->right) {
|
|
|
|
for (ap = (const char **)t->vars; *ap; ap++) {
|
|
|
|
if (i || ((s = evalstr(*ap, DOTILDE|DOPAT)) &&
|
|
|
|
gmatchx(ccp, s, false))) {
|
2016-08-01 23:38:07 +02:00
|
|
|
record_match(ccp);
|
2011-05-29 04:18:57 +02:00
|
|
|
rv = execute(t->left, flags & XERROK,
|
|
|
|
xerrok);
|
|
|
|
i = 0;
|
|
|
|
switch (t->u.charflag) {
|
|
|
|
case '&':
|
|
|
|
i = 1;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case '|':
|
|
|
|
goto TCASE_next;
|
|
|
|
}
|
|
|
|
goto TCASE_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i = 0;
|
|
|
|
TCASE_next:
|
|
|
|
/* empty */;
|
|
|
|
}
|
|
|
|
TCASE_out:
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TBRACE:
|
2009-03-22 18:47:38 +01:00
|
|
|
rv = execute(t->left, flags & XERROK, xerrok);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TFUNCT:
|
|
|
|
rv = define(t->str, t);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TTIME:
|
2011-01-30 02:35:35 +01:00
|
|
|
/*
|
|
|
|
* Clear XEXEC so nested execute() call doesn't exit
|
2005-05-23 05:06:10 +02:00
|
|
|
* (allows "ls -l | time grep foo").
|
|
|
|
*/
|
2009-03-22 18:47:38 +01:00
|
|
|
rv = timex(t, flags & ~XEXEC, xerrok);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
case TEXEC:
|
|
|
|
/* an eval'd TCOM */
|
2007-03-04 01:13:17 +01:00
|
|
|
up = makenv();
|
2005-05-23 05:06:10 +02:00
|
|
|
restoresigs();
|
|
|
|
cleanup_proc_env();
|
2007-03-04 01:13:17 +01:00
|
|
|
{
|
|
|
|
union mksh_ccphack cargs;
|
|
|
|
|
|
|
|
cargs.ro = t->args;
|
|
|
|
execve(t->str, cargs.rw, up);
|
2007-06-07 01:28:17 +02:00
|
|
|
rv = errno;
|
2007-03-04 01:13:17 +01:00
|
|
|
}
|
2007-06-07 01:28:17 +02:00
|
|
|
if (rv == ENOEXEC)
|
2007-03-04 01:13:17 +01:00
|
|
|
scriptexec(t, (const char **)up);
|
2005-05-23 05:06:10 +02:00
|
|
|
else
|
2016-07-25 02:04:48 +02:00
|
|
|
errorf(Tf_sD_s, t->str, cstrerror(rv));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2006-08-01 15:43:28 +02:00
|
|
|
Break:
|
2012-10-21 23:39:06 +02:00
|
|
|
exstat = rv & 0xFF;
|
2011-02-18 23:26:13 +01:00
|
|
|
if (vp_pipest->flag & INT_L) {
|
|
|
|
unset(vp_pipest, 1);
|
|
|
|
vp_pipest->flag = DEFINED | ISSET | INTEGER | RDONLY |
|
2014-06-11 00:17:10 +02:00
|
|
|
ARRAY | INT_U | INT_L;
|
2011-02-18 23:26:13 +01:00
|
|
|
vp_pipest->val.i = rv;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
/* restores IO */
|
|
|
|
quitenv(NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
if ((flags&XEXEC))
|
2011-01-30 02:35:35 +01:00
|
|
|
/* exit child */
|
|
|
|
unwind(LEXIT);
|
2009-03-22 18:47:38 +01:00
|
|
|
if (rv != 0 && !(flags & XERROK) &&
|
|
|
|
(xerrok == NULL || !*xerrok)) {
|
2013-01-01 21:45:04 +01:00
|
|
|
if (Flag(FERREXIT) & 0x80) {
|
|
|
|
/* inside eval */
|
|
|
|
Flag(FERREXIT) = 0;
|
|
|
|
} else {
|
|
|
|
trapsig(ksh_SIGERR);
|
|
|
|
if (Flag(FERREXIT))
|
|
|
|
unwind(LERROR);
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* execute simple command
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
2011-01-09 22:57:29 +01:00
|
|
|
comexec(struct op *t, struct tbl * volatile tp, const char **ap,
|
2009-03-22 18:47:38 +01:00
|
|
|
volatile int flags, volatile int *xerrok)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
volatile int rv = 0;
|
2007-03-04 01:13:17 +01:00
|
|
|
const char *cp;
|
|
|
|
const char **lastp;
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/* Must be static (XXX but why?) */
|
|
|
|
static struct op texec;
|
2005-05-23 05:06:10 +02:00
|
|
|
int type_flags;
|
2014-10-12 23:58:53 +02:00
|
|
|
bool resetspec;
|
2005-05-23 05:06:10 +02:00
|
|
|
int fcflags = FC_BI|FC_FUNC|FC_PATH;
|
2010-04-08 15:21:08 +02:00
|
|
|
struct block *l_expand, *l_assign;
|
2015-02-06 11:09:07 +01:00
|
|
|
int optc;
|
2015-02-06 11:56:49 +01:00
|
|
|
const char *exec_argv0 = NULL;
|
|
|
|
bool exec_clrenv = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2015-02-06 11:09:07 +01:00
|
|
|
/* snag the last argument for $_ */
|
2005-07-04 14:34:24 +02:00
|
|
|
if (Flag(FTALKING) && *(lastp = ap)) {
|
2015-02-06 11:09:07 +01:00
|
|
|
/*
|
|
|
|
* XXX not the same as AT&T ksh, which only seems to set $_
|
|
|
|
* after a newline (but not in functions/dot scripts, but in
|
|
|
|
* interactive and script) - perhaps save last arg here and
|
|
|
|
* set it in shell()?.
|
|
|
|
*/
|
2005-05-23 05:06:10 +02:00
|
|
|
while (*++lastp)
|
|
|
|
;
|
|
|
|
/* setstr() can't fail here */
|
|
|
|
setstr(typeset("_", LOCAL, 0, INTEGER, 0), *--lastp,
|
|
|
|
KSH_RETURN_ERROR);
|
|
|
|
}
|
|
|
|
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/**
|
|
|
|
* Deal with the shell builtins builtin, exec and command since
|
2009-06-10 20:12:51 +02:00
|
|
|
* they can be followed by other commands. This must be done before
|
2006-01-30 13:37:24 +01:00
|
|
|
* we know if we should create a local block which must be done
|
2005-05-23 05:06:10 +02:00
|
|
|
* before we can do a path search (in case the assignments change
|
|
|
|
* PATH).
|
|
|
|
* Odd cases:
|
2009-06-10 20:12:51 +02:00
|
|
|
* FOO=bar exec >/dev/null FOO is kept but not exported
|
|
|
|
* FOO=bar exec foobar FOO is exported
|
|
|
|
* FOO=bar command exec >/dev/null FOO is neither kept nor exported
|
|
|
|
* FOO=bar command FOO is neither kept nor exported
|
|
|
|
* PATH=... foobar use new PATH in foobar search
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
2014-10-12 23:58:53 +02:00
|
|
|
resetspec = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
while (tp && tp->type == CSHELL) {
|
2010-08-28 17:48:20 +02:00
|
|
|
/* undo effects of command */
|
|
|
|
fcflags = FC_BI|FC_FUNC|FC_PATH;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (tp->val.f == c_builtin) {
|
2010-09-05 21:51:35 +02:00
|
|
|
if ((cp = *++ap) == NULL ||
|
|
|
|
(!strcmp(cp, "--") && (cp = *++ap) == NULL)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
tp = NULL;
|
|
|
|
break;
|
|
|
|
}
|
2010-09-05 21:51:35 +02:00
|
|
|
if ((tp = findcom(cp, FC_BI)) == NULL)
|
2016-07-25 02:04:48 +02:00
|
|
|
errorf(Tf_sD_sD_s, Tbuiltin, cp, Tnot_found);
|
2016-07-25 01:10:04 +02:00
|
|
|
if (tp->type == CSHELL && (tp->flag & LOW_BI))
|
2015-07-06 19:48:37 +02:00
|
|
|
break;
|
2005-05-23 05:06:10 +02:00
|
|
|
continue;
|
|
|
|
} else if (tp->val.f == c_exec) {
|
|
|
|
if (ap[1] == NULL)
|
|
|
|
break;
|
2015-02-06 11:56:49 +01:00
|
|
|
ksh_getopt_reset(&builtin_opt, GF_ERROR);
|
|
|
|
while ((optc = ksh_getopt(ap, &builtin_opt, "a:c")) != -1)
|
|
|
|
switch (optc) {
|
|
|
|
case 'a':
|
|
|
|
exec_argv0 = builtin_opt.optarg;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
exec_clrenv = true;
|
|
|
|
/* ensure we can actually do this */
|
|
|
|
resetspec = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rv = 2;
|
|
|
|
goto Leave;
|
|
|
|
}
|
|
|
|
ap += builtin_opt.optind;
|
2005-05-23 05:06:10 +02:00
|
|
|
flags |= XEXEC;
|
|
|
|
} else if (tp->val.f == c_command) {
|
2015-02-06 10:42:46 +01:00
|
|
|
bool saw_p = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/*
|
|
|
|
* Ugly dealing with options in two places (here
|
|
|
|
* and in c_command(), but such is life)
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
ksh_getopt_reset(&builtin_opt, 0);
|
|
|
|
while ((optc = ksh_getopt(ap, &builtin_opt, ":p")) == 'p')
|
2015-02-06 10:42:46 +01:00
|
|
|
saw_p = true;
|
2015-02-06 11:09:07 +01:00
|
|
|
if (optc != -1)
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/* command -vV or something */
|
|
|
|
break;
|
2005-05-23 05:06:10 +02:00
|
|
|
/* don't look for functions */
|
|
|
|
fcflags = FC_BI|FC_PATH;
|
|
|
|
if (saw_p) {
|
|
|
|
if (Flag(FRESTRICTED)) {
|
2016-07-25 02:04:48 +02:00
|
|
|
warningf(true, Tf_sD_s,
|
2010-08-28 20:50:58 +02:00
|
|
|
"command -p", "restricted");
|
2005-05-23 05:06:10 +02:00
|
|
|
rv = 1;
|
|
|
|
goto Leave;
|
|
|
|
}
|
|
|
|
fcflags |= FC_DEFPATH;
|
|
|
|
}
|
|
|
|
ap += builtin_opt.optind;
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/*
|
|
|
|
* POSIX says special builtins lose their status
|
2005-05-23 05:06:10 +02:00
|
|
|
* if accessed using command.
|
|
|
|
*/
|
2014-10-12 23:58:53 +02:00
|
|
|
resetspec = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (!ap[0]) {
|
|
|
|
/* ensure command with no args exits with 0 */
|
|
|
|
subst_exstat = 0;
|
|
|
|
break;
|
|
|
|
}
|
2016-07-25 01:10:04 +02:00
|
|
|
} else if (tp->flag & LOW_BI) {
|
2015-07-05 17:45:18 +02:00
|
|
|
/* if we have any flags, do not use the builtin */
|
2016-07-25 01:10:04 +02:00
|
|
|
if ((ap[1] && ap[1][0] == '-' && ap[1][1] != '\0' &&
|
2010-08-28 19:21:46 +02:00
|
|
|
/* argument, begins with -, is not - or -- */
|
2016-07-25 01:10:04 +02:00
|
|
|
(ap[1][1] != '-' || ap[1][2] != '\0')) ||
|
|
|
|
/* always prefer the external utility */
|
|
|
|
(tp->flag & LOWER_BI)) {
|
|
|
|
struct tbl *ext_cmd;
|
|
|
|
|
|
|
|
ext_cmd = findcom(tp->name, FC_PATH | FC_FUNC);
|
|
|
|
if (ext_cmd && (ext_cmd->type != CTALIAS ||
|
|
|
|
(ext_cmd->flag & ISSET)))
|
|
|
|
tp = ext_cmd;
|
2015-07-06 19:48:37 +02:00
|
|
|
}
|
|
|
|
break;
|
2012-10-21 23:26:41 +02:00
|
|
|
} else if (tp->val.f == c_trap) {
|
|
|
|
t->u.evalflags &= ~DOTCOMEXEC;
|
|
|
|
break;
|
2005-05-23 05:06:10 +02:00
|
|
|
} else
|
|
|
|
break;
|
|
|
|
tp = findcom(ap[0], fcflags & (FC_BI|FC_FUNC));
|
|
|
|
}
|
2012-10-21 23:26:41 +02:00
|
|
|
if (t->u.evalflags & DOTCOMEXEC)
|
|
|
|
flags |= XEXEC;
|
2010-04-08 15:21:08 +02:00
|
|
|
l_expand = e->loc;
|
2014-10-12 23:58:53 +02:00
|
|
|
if (!resetspec && (!ap[0] || (tp && (tp->flag & KEEPASN))))
|
2005-05-23 05:06:10 +02:00
|
|
|
type_flags = 0;
|
|
|
|
else {
|
|
|
|
/* create new variable/function block */
|
|
|
|
newblock();
|
|
|
|
/* ksh functions don't keep assignments, POSIX functions do. */
|
2014-10-12 23:58:53 +02:00
|
|
|
if (!resetspec && tp && tp->type == CFUNC &&
|
2015-02-06 11:09:07 +01:00
|
|
|
!(tp->flag & FKSH))
|
2009-10-15 18:25:15 +02:00
|
|
|
type_flags = EXPORT;
|
2015-02-06 11:09:07 +01:00
|
|
|
else
|
2005-05-23 05:06:10 +02:00
|
|
|
type_flags = LOCAL|LOCAL_COPY|EXPORT;
|
|
|
|
}
|
2010-04-08 15:21:08 +02:00
|
|
|
l_assign = e->loc;
|
2015-02-06 11:56:49 +01:00
|
|
|
if (exec_clrenv)
|
|
|
|
l_assign->flags |= BF_STOPENV;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (Flag(FEXPORT))
|
|
|
|
type_flags |= EXPORT;
|
2013-07-21 20:47:24 +02:00
|
|
|
if (Flag(FXTRACE))
|
|
|
|
change_xtrace(2, false);
|
2005-05-23 05:06:10 +02:00
|
|
|
for (i = 0; t->vars[i]; i++) {
|
2010-04-08 15:21:08 +02:00
|
|
|
/* do NOT lookup in the new var/fn block just created */
|
|
|
|
e->loc = l_expand;
|
2014-10-19 23:53:08 +02:00
|
|
|
cp = evalstr(t->vars[i], DOASNTILDE | DOSCALAR);
|
2010-04-08 15:21:08 +02:00
|
|
|
e->loc = l_assign;
|
2013-04-26 21:40:45 +02:00
|
|
|
if (Flag(FXTRACE)) {
|
|
|
|
const char *ccp;
|
|
|
|
|
|
|
|
ccp = skip_varname(cp, true);
|
|
|
|
if (*ccp == '+')
|
|
|
|
++ccp;
|
|
|
|
if (*ccp == '=')
|
|
|
|
++ccp;
|
2013-07-21 20:47:24 +02:00
|
|
|
shf_write(cp, ccp - cp, shl_xtrace);
|
|
|
|
print_value_quoted(shl_xtrace, ccp);
|
|
|
|
shf_putc(' ', shl_xtrace);
|
2013-04-26 21:40:45 +02:00
|
|
|
}
|
2010-04-08 15:21:08 +02:00
|
|
|
/* but assign in there as usual */
|
2005-05-23 05:06:10 +02:00
|
|
|
typeset(cp, type_flags, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
2013-04-26 21:40:45 +02:00
|
|
|
if (Flag(FXTRACE)) {
|
2013-07-21 20:47:24 +02:00
|
|
|
change_xtrace(2, false);
|
2013-04-26 21:47:07 +02:00
|
|
|
if (ap[rv = 0]) {
|
2013-04-26 21:40:45 +02:00
|
|
|
xtrace_ap_loop:
|
2013-07-21 20:47:24 +02:00
|
|
|
print_value_quoted(shl_xtrace, ap[rv]);
|
2013-04-26 21:47:07 +02:00
|
|
|
if (ap[++rv]) {
|
2013-07-21 20:47:24 +02:00
|
|
|
shf_putc(' ', shl_xtrace);
|
2013-04-26 21:47:07 +02:00
|
|
|
goto xtrace_ap_loop;
|
|
|
|
}
|
2013-04-26 21:40:45 +02:00
|
|
|
}
|
2013-07-21 20:47:24 +02:00
|
|
|
change_xtrace(1, false);
|
2013-04-26 21:40:45 +02:00
|
|
|
}
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
if ((cp = *ap) == NULL) {
|
|
|
|
rv = subst_exstat;
|
|
|
|
goto Leave;
|
|
|
|
} else if (!tp) {
|
2016-11-12 00:31:39 +01:00
|
|
|
if (Flag(FRESTRICTED) && mksh_vdirsep(cp)) {
|
2016-07-25 02:04:48 +02:00
|
|
|
warningf(true, Tf_sD_s, cp, "restricted");
|
2005-05-23 05:06:10 +02:00
|
|
|
rv = 1;
|
|
|
|
goto Leave;
|
|
|
|
}
|
|
|
|
tp = findcom(cp, fcflags);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (tp->type) {
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
|
|
|
|
/* shell built-in */
|
|
|
|
case CSHELL:
|
2015-07-06 19:48:37 +02:00
|
|
|
do_call_builtin:
|
2014-10-12 23:58:53 +02:00
|
|
|
rv = call_builtin(tp, (const char **)ap, null, resetspec);
|
|
|
|
if (resetspec && tp->val.f == c_shift) {
|
2013-01-06 19:51:43 +01:00
|
|
|
l_expand->argc = l_assign->argc;
|
|
|
|
l_expand->argv = l_assign->argv;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/* function call */
|
|
|
|
case CFUNC: {
|
2011-07-05 22:12:20 +02:00
|
|
|
volatile uint32_t old_inuse;
|
2011-01-09 22:57:29 +01:00
|
|
|
const char * volatile old_kshname;
|
2015-04-12 01:28:21 +02:00
|
|
|
volatile uint8_t old_flags[FNFLAGS];
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (!(tp->flag & ISSET)) {
|
|
|
|
struct tbl *ftp;
|
|
|
|
|
|
|
|
if (!tp->u.fpath) {
|
2016-07-25 01:07:19 +02:00
|
|
|
fpath_error:
|
2011-09-07 17:24:22 +02:00
|
|
|
rv = (tp->u2.errnov == ENOENT) ? 127 : 126;
|
2016-07-25 02:04:48 +02:00
|
|
|
warningf(true, Tf_sD_s_sD_s, cp,
|
|
|
|
Tcant_find, Tfile_fd,
|
2012-12-18 00:18:11 +01:00
|
|
|
cstrerror(tp->u2.errnov));
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-07-25 01:07:19 +02:00
|
|
|
errno = 0;
|
|
|
|
if (include(tp->u.fpath, 0, NULL, false) < 0 ||
|
|
|
|
!(ftp = findfunc(cp, hash(cp), false)) ||
|
|
|
|
!(ftp->flag & ISSET)) {
|
|
|
|
rv = errno;
|
2016-07-25 01:10:04 +02:00
|
|
|
if ((ftp = findcom(cp, FC_BI)) &&
|
|
|
|
(ftp->type == CSHELL) &&
|
|
|
|
(ftp->flag & LOW_BI)) {
|
|
|
|
tp = ftp;
|
2015-07-09 22:20:45 +02:00
|
|
|
goto do_call_builtin;
|
|
|
|
}
|
2016-07-25 01:07:19 +02:00
|
|
|
if (rv) {
|
|
|
|
tp->u2.errnov = rv;
|
|
|
|
cp = tp->u.fpath;
|
|
|
|
goto fpath_error;
|
|
|
|
}
|
2016-07-25 02:04:48 +02:00
|
|
|
warningf(true, Tf_sD_s_s, cp,
|
2010-08-28 20:50:58 +02:00
|
|
|
"function not defined by", tp->u.fpath);
|
2005-05-23 05:06:10 +02:00
|
|
|
rv = 127;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tp = ftp;
|
|
|
|
}
|
|
|
|
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/*
|
|
|
|
* ksh functions set $0 to function name, POSIX
|
|
|
|
* functions leave $0 unchanged.
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
old_kshname = kshname;
|
|
|
|
if (tp->flag & FKSH)
|
|
|
|
kshname = ap[0];
|
|
|
|
else
|
2007-03-04 01:13:17 +01:00
|
|
|
ap[0] = kshname;
|
2005-05-23 05:06:10 +02:00
|
|
|
e->loc->argv = ap;
|
|
|
|
for (i = 0; *ap++ != NULL; i++)
|
|
|
|
;
|
|
|
|
e->loc->argc = i - 1;
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/*
|
|
|
|
* ksh-style functions handle getopts sanely,
|
2009-10-02 20:08:37 +02:00
|
|
|
* Bourne/POSIX functions are insane...
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
if (tp->flag & FKSH) {
|
|
|
|
e->loc->flags |= BF_DOGETOPTS;
|
|
|
|
e->loc->getopts_state = user_opt;
|
|
|
|
getopts_reset(1);
|
|
|
|
}
|
|
|
|
|
2015-04-12 01:28:21 +02:00
|
|
|
for (type_flags = 0; type_flags < FNFLAGS; ++type_flags)
|
|
|
|
old_flags[type_flags] = shell_flags[type_flags];
|
|
|
|
change_xtrace((Flag(FXTRACEREC) ? Flag(FXTRACE) : 0) |
|
2013-07-21 20:47:24 +02:00
|
|
|
((tp->flag & TRACE) ? 1 : 0), false);
|
2005-05-23 05:06:10 +02:00
|
|
|
old_inuse = tp->flag & FINUSE;
|
|
|
|
tp->flag |= FINUSE;
|
|
|
|
|
|
|
|
e->type = E_FUNC;
|
2012-03-31 19:30:00 +02:00
|
|
|
if (!(i = kshsetjmp(e->jbuf))) {
|
2013-02-15 19:50:14 +01:00
|
|
|
execute(tp->val.t, flags & XERROK, NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
i = LRETURN;
|
|
|
|
}
|
2013-07-21 20:47:24 +02:00
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
kshname = old_kshname;
|
2015-04-12 01:28:21 +02:00
|
|
|
change_xtrace(old_flags[(int)FXTRACE], false);
|
|
|
|
#ifndef MKSH_LEGACY_MODE
|
|
|
|
if (tp->flag & FKSH) {
|
|
|
|
/* Korn style functions restore Flags on return */
|
|
|
|
old_flags[(int)FXTRACE] = Flag(FXTRACE);
|
|
|
|
for (type_flags = 0; type_flags < FNFLAGS; ++type_flags)
|
|
|
|
shell_flags[type_flags] = old_flags[type_flags];
|
|
|
|
}
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
tp->flag = (tp->flag & ~FINUSE) | old_inuse;
|
2013-07-21 20:47:24 +02:00
|
|
|
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/*
|
|
|
|
* Were we deleted while executing? If so, free the
|
2015-04-12 01:28:21 +02:00
|
|
|
* execution tree.
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
if ((tp->flag & (FDELETE|FINUSE)) == FDELETE) {
|
|
|
|
if (tp->flag & ALLOC) {
|
|
|
|
tp->flag &= ~ALLOC;
|
|
|
|
tfree(tp->val.t, tp->areap);
|
|
|
|
}
|
|
|
|
tp->flag = 0;
|
|
|
|
}
|
|
|
|
switch (i) {
|
|
|
|
case LRETURN:
|
|
|
|
case LERROR:
|
2012-10-21 23:39:06 +02:00
|
|
|
rv = exstat & 0xFF;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case LINTR:
|
|
|
|
case LEXIT:
|
|
|
|
case LLEAVE:
|
|
|
|
case LSHELL:
|
|
|
|
quitenv(NULL);
|
|
|
|
unwind(i);
|
2006-05-10 20:54:13 +02:00
|
|
|
/* NOTREACHED */
|
2005-05-23 05:06:10 +02:00
|
|
|
default:
|
|
|
|
quitenv(NULL);
|
2016-07-25 02:04:48 +02:00
|
|
|
internal_errorf(Tf_sd, "CFUNC", i);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
break;
|
2009-06-10 20:12:51 +02:00
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/* executable command */
|
|
|
|
case CEXEC:
|
|
|
|
/* tracked alias */
|
|
|
|
case CTALIAS:
|
2005-05-23 05:06:10 +02:00
|
|
|
if (!(tp->flag&ISSET)) {
|
2011-09-07 17:24:22 +02:00
|
|
|
if (tp->u2.errnov == ENOENT) {
|
2005-05-23 05:06:10 +02:00
|
|
|
rv = 127;
|
2016-07-25 02:04:48 +02:00
|
|
|
warningf(true, Tf_sD_s, cp, Tnot_found);
|
2011-09-07 17:24:22 +02:00
|
|
|
} else {
|
|
|
|
rv = 126;
|
2016-07-25 02:04:48 +02:00
|
|
|
warningf(true, Tf_sD_sD_s, cp, "can't execute",
|
2012-12-18 00:18:11 +01:00
|
|
|
cstrerror(tp->u2.errnov));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-02-06 11:56:49 +01:00
|
|
|
/* set $_ to program's full path */
|
2005-07-04 14:34:24 +02:00
|
|
|
/* setstr() can't fail here */
|
2014-05-27 15:22:46 +02:00
|
|
|
setstr(typeset("_", LOCAL | EXPORT, 0, INTEGER, 0),
|
2005-07-04 14:34:24 +02:00
|
|
|
tp->val.s, KSH_RETURN_ERROR);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2015-02-06 11:56:49 +01:00
|
|
|
/* to fork, we set up a TEXEC node and call execute */
|
|
|
|
texec.type = TEXEC;
|
|
|
|
/* for vistree/dumptree */
|
|
|
|
texec.left = t;
|
|
|
|
texec.str = tp->val.s;
|
|
|
|
texec.args = ap;
|
|
|
|
|
|
|
|
/* in this case we do not fork, of course */
|
|
|
|
if (flags & XEXEC) {
|
|
|
|
if (exec_argv0)
|
|
|
|
texec.args[0] = exec_argv0;
|
2005-05-23 05:06:10 +02:00
|
|
|
j_exit();
|
2015-02-06 11:56:49 +01:00
|
|
|
if (!(flags & XBGND)
|
2009-09-20 18:40:58 +02:00
|
|
|
#ifndef MKSH_UNEMPLOYED
|
|
|
|
|| Flag(FMONITOR)
|
|
|
|
#endif
|
|
|
|
) {
|
2005-05-23 05:06:10 +02:00
|
|
|
setexecsig(&sigtraps[SIGINT], SS_RESTORE_ORIG);
|
|
|
|
setexecsig(&sigtraps[SIGQUIT], SS_RESTORE_ORIG);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-22 18:47:38 +01:00
|
|
|
rv = exchild(&texec, flags, xerrok, -1);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
}
|
2006-08-01 15:43:28 +02:00
|
|
|
Leave:
|
2005-05-23 05:06:10 +02:00
|
|
|
if (flags & XEXEC) {
|
2012-10-21 23:39:06 +02:00
|
|
|
exstat = rv & 0xFF;
|
2005-05-23 05:06:10 +02:00
|
|
|
unwind(LLEAVE);
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-03-04 01:13:17 +01:00
|
|
|
scriptexec(struct op *tp, const char **ap)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2006-11-12 15:58:16 +01:00
|
|
|
const char *sh;
|
2007-04-18 02:59:20 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2007-04-15 14:09:57 +02:00
|
|
|
int fd;
|
2014-11-25 23:46:58 +01:00
|
|
|
unsigned char buf[68];
|
2007-04-18 02:59:20 +02:00
|
|
|
#endif
|
2007-03-04 01:13:17 +01:00
|
|
|
union mksh_ccphack args, cap;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2016-07-25 02:04:48 +02:00
|
|
|
sh = str_val(global(TEXECSHELL));
|
2005-05-23 05:06:10 +02:00
|
|
|
if (sh && *sh)
|
2011-09-07 17:24:22 +02:00
|
|
|
sh = search_path(sh, path, X_OK, NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (!sh || !*sh)
|
2010-03-27 16:29:00 +01:00
|
|
|
sh = MKSH_DEFAULT_EXECSHELL;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
*tp->args-- = tp->str;
|
2007-04-15 14:09:57 +02:00
|
|
|
|
2007-04-18 02:59:20 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2015-07-09 22:52:43 +02:00
|
|
|
if ((fd = binopen2(tp->str, O_RDONLY)) >= 0) {
|
2014-11-25 23:46:58 +01:00
|
|
|
unsigned char *cp;
|
|
|
|
unsigned short m;
|
|
|
|
ssize_t n;
|
|
|
|
|
2017-04-02 16:41:01 +02:00
|
|
|
#if defined(__OS2__) && defined(MKSH_WITH_TEXTMODE)
|
2015-05-16 06:20:53 +02:00
|
|
|
setmode(fd, O_TEXT);
|
|
|
|
#endif
|
2014-11-25 23:46:58 +01:00
|
|
|
/* read first couple of octets from file */
|
|
|
|
n = read(fd, buf, sizeof(buf) - 1);
|
2007-04-15 14:09:57 +02:00
|
|
|
close(fd);
|
2014-11-25 23:46:58 +01:00
|
|
|
/* read error or short read? */
|
|
|
|
if (n < 5)
|
|
|
|
goto nomagic;
|
|
|
|
/* terminate buffer */
|
|
|
|
buf[n] = '\0';
|
2011-04-02 12:30:11 +02:00
|
|
|
|
|
|
|
/* skip UTF-8 Byte Order Mark, if present */
|
2014-11-25 23:46:58 +01:00
|
|
|
cp = buf + (n = ((buf[0] == 0xEF) && (buf[1] == 0xBB) &&
|
|
|
|
(buf[2] == 0xBF)) ? 3 : 0);
|
|
|
|
|
|
|
|
/* scan for newline or NUL (end of buffer) */
|
|
|
|
while (*cp && *cp != '\n')
|
|
|
|
++cp;
|
2007-04-15 14:09:57 +02:00
|
|
|
/* if the shebang line is longer than MAXINTERP, bail out */
|
2014-11-25 23:46:58 +01:00
|
|
|
if (!*cp)
|
2007-04-15 14:09:57 +02:00
|
|
|
goto noshebang;
|
2014-11-25 23:46:58 +01:00
|
|
|
/* replace newline by NUL */
|
|
|
|
*cp = '\0';
|
2011-04-02 12:30:11 +02:00
|
|
|
|
2016-09-01 14:59:12 +02:00
|
|
|
/* restore start of shebang position (buf+0 or buf+3) */
|
2014-11-25 23:46:58 +01:00
|
|
|
cp = buf + n;
|
|
|
|
/* bail out if no shebang magic found */
|
2015-07-09 21:46:43 +02:00
|
|
|
if (cp[0] == '#' && cp[1] == '!')
|
|
|
|
cp += 2;
|
|
|
|
#ifdef __OS2__
|
|
|
|
else if (!strncmp(cp, Textproc, 7) &&
|
|
|
|
(cp[7] == ' ' || cp[7] == '\t'))
|
|
|
|
cp += 8;
|
|
|
|
#endif
|
|
|
|
else
|
2007-04-15 14:09:57 +02:00
|
|
|
goto noshebang;
|
|
|
|
/* skip whitespace before shell name */
|
|
|
|
while (*cp == ' ' || *cp == '\t')
|
|
|
|
++cp;
|
|
|
|
/* just whitespace on the line? */
|
|
|
|
if (*cp == '\0')
|
|
|
|
goto noshebang;
|
|
|
|
/* no, we actually found an interpreter name */
|
|
|
|
sh = (char *)cp;
|
|
|
|
/* look for end of shell/interpreter name */
|
|
|
|
while (*cp != ' ' && *cp != '\t' && *cp != '\0')
|
|
|
|
++cp;
|
|
|
|
/* any arguments? */
|
|
|
|
if (*cp) {
|
|
|
|
*cp++ = '\0';
|
|
|
|
/* skip spaces before arguments */
|
|
|
|
while (*cp == ' ' || *cp == '\t')
|
|
|
|
++cp;
|
|
|
|
/* pass it all in ONE argument (historic reasons) */
|
|
|
|
if (*cp)
|
|
|
|
*tp->args-- = (char *)cp;
|
|
|
|
}
|
2015-06-11 09:11:06 +02:00
|
|
|
#ifdef __OS2__
|
|
|
|
/*
|
2015-06-11 15:16:17 +02:00
|
|
|
* Search shell/interpreter name without directory in PATH
|
|
|
|
* if specified path does not exist
|
2015-06-11 09:11:06 +02:00
|
|
|
*/
|
2016-11-12 05:22:58 +01:00
|
|
|
if (mksh_vdirsep(sh) && !search_path(sh, path, X_OK, NULL)) {
|
2015-06-11 15:16:17 +02:00
|
|
|
cp = search_path(_getname(sh), path, X_OK, NULL);
|
|
|
|
if (cp)
|
|
|
|
sh = cp;
|
|
|
|
}
|
2015-06-11 09:11:06 +02:00
|
|
|
#endif
|
2014-11-25 23:46:58 +01:00
|
|
|
goto nomagic;
|
2007-04-15 14:09:57 +02:00
|
|
|
noshebang:
|
2014-11-25 23:46:58 +01:00
|
|
|
m = buf[0] << 8 | buf[1];
|
|
|
|
if (m == 0x7F45 && buf[2] == 'L' && buf[3] == 'F')
|
2014-06-24 20:38:31 +02:00
|
|
|
errorf("%s: not executable: %d-bit ELF file", tp->str,
|
2014-11-25 23:46:58 +01:00
|
|
|
32 * buf[4]);
|
|
|
|
if ((m == /* OMAGIC */ 0407) ||
|
|
|
|
(m == /* NMAGIC */ 0410) ||
|
|
|
|
(m == /* ZMAGIC */ 0413) ||
|
|
|
|
(m == /* QMAGIC */ 0314) ||
|
|
|
|
(m == /* ECOFF_I386 */ 0x4C01) ||
|
|
|
|
(m == /* ECOFF_M68K */ 0x0150 || m == 0x5001) ||
|
|
|
|
(m == /* ECOFF_SH */ 0x0500 || m == 0x0005) ||
|
2015-07-09 21:59:14 +02:00
|
|
|
(m == /* bzip */ 0x425A) || (m == /* "MZ" */ 0x4D5A) ||
|
|
|
|
(m == /* "NE" */ 0x4E45) || (m == /* "LX" */ 0x4C58) ||
|
2015-12-31 13:58:43 +01:00
|
|
|
(m == /* ksh93 */ 0x0B13) || (m == /* LZIP */ 0x4C5A) ||
|
2015-07-09 21:59:14 +02:00
|
|
|
(m == /* xz */ 0xFD37 && buf[2] == 'z' && buf[3] == 'X' &&
|
|
|
|
buf[4] == 'Z') || (m == /* 7zip */ 0x377A) ||
|
2014-11-25 23:46:58 +01:00
|
|
|
(m == /* gzip */ 0x1F8B) || (m == /* .Z */ 0x1F9D))
|
|
|
|
errorf("%s: not executable: magic %04X", tp->str, m);
|
2015-05-12 07:10:12 +02:00
|
|
|
#ifdef __OS2__
|
|
|
|
cp = _getext(tp->str);
|
|
|
|
if (cp && (!stricmp(cp, ".cmd") || !stricmp(cp, ".bat"))) {
|
|
|
|
/* execute .cmd and .bat with OS2_SHELL, usually CMD.EXE */
|
2017-04-02 17:02:40 +02:00
|
|
|
sh = str_val(global("OS2_SHELL"));
|
2015-05-12 07:10:12 +02:00
|
|
|
*tp->args-- = "/c";
|
2017-03-26 00:18:21 +01:00
|
|
|
/* convert slahes to backslashes */
|
2015-05-12 07:10:12 +02:00
|
|
|
for (cp = tp->str; *cp; cp++) {
|
|
|
|
if (*cp == '/')
|
|
|
|
*cp = '\\';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2014-11-25 23:46:58 +01:00
|
|
|
nomagic:
|
|
|
|
;
|
2007-06-15 23:22:40 +02:00
|
|
|
}
|
2007-04-18 02:59:20 +02:00
|
|
|
#endif
|
2007-03-04 01:13:17 +01:00
|
|
|
args.ro = tp->args;
|
|
|
|
*args.ro = sh;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2007-03-04 01:13:17 +01:00
|
|
|
cap.ro = ap;
|
|
|
|
execve(args.rw[0], args.rw, cap.rw);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2007-04-18 02:59:20 +02:00
|
|
|
/* report both the programme that was run and the bogus interpreter */
|
2016-07-25 02:04:48 +02:00
|
|
|
errorf(Tf_sD_sD_s, tp->str, sh, cstrerror(errno));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2017-03-12 00:22:36 +01:00
|
|
|
/* actual 'builtin' built-in utility call is handled in comexec() */
|
2005-05-23 05:06:10 +02:00
|
|
|
int
|
2017-03-12 00:22:36 +01:00
|
|
|
c_builtin(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2017-03-12 03:04:15 +01:00
|
|
|
return (call_builtin(get_builtin(*wp), wp, Tbuiltin, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct tbl *
|
|
|
|
get_builtin(const char *s)
|
|
|
|
{
|
|
|
|
return (s && *s ? ktsearch(&builtins, s, hash(s)) : NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-06-10 20:12:51 +02:00
|
|
|
* Search function tables for a function. If create set, a table entry
|
2005-05-23 05:06:10 +02:00
|
|
|
* is created if none is found.
|
|
|
|
*/
|
|
|
|
struct tbl *
|
2009-08-28 22:30:59 +02:00
|
|
|
findfunc(const char *name, uint32_t h, bool create)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct block *l;
|
|
|
|
struct tbl *tp = NULL;
|
|
|
|
|
|
|
|
for (l = e->loc; l; l = l->next) {
|
2009-08-28 22:30:59 +02:00
|
|
|
tp = ktsearch(&l->funs, name, h);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (tp)
|
|
|
|
break;
|
|
|
|
if (!l->next && create) {
|
2009-08-28 22:30:59 +02:00
|
|
|
tp = ktenter(&l->funs, name, h);
|
2005-05-23 05:06:10 +02:00
|
|
|
tp->flag = DEFINED;
|
|
|
|
tp->type = CFUNC;
|
|
|
|
tp->val.t = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (tp);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-06-10 20:12:51 +02:00
|
|
|
* define function. Returns 1 if function is being undefined (t == 0) and
|
2005-05-23 05:06:10 +02:00
|
|
|
* function did not exist, returns 0 otherwise.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
define(const char *name, struct op *t)
|
|
|
|
{
|
2011-05-07 02:51:12 +02:00
|
|
|
uint32_t nhash;
|
2005-05-23 05:06:10 +02:00
|
|
|
struct tbl *tp;
|
2009-04-07 21:13:11 +02:00
|
|
|
bool was_set = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-05-07 02:51:12 +02:00
|
|
|
nhash = hash(name);
|
|
|
|
|
2011-03-13 02:20:25 +01:00
|
|
|
while (/* CONSTCOND */ 1) {
|
2011-05-07 02:51:12 +02:00
|
|
|
tp = findfunc(name, nhash, true);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (tp->flag & ISSET)
|
2009-04-07 21:13:11 +02:00
|
|
|
was_set = true;
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/*
|
|
|
|
* If this function is currently being executed, we zap
|
|
|
|
* this table entry so findfunc() won't see it
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
if (tp->flag & FINUSE) {
|
|
|
|
tp->name[0] = '\0';
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/* ensure it won't be found */
|
|
|
|
tp->flag &= ~DEFINED;
|
2005-05-23 05:06:10 +02:00
|
|
|
tp->flag |= FDELETE;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tp->flag & ALLOC) {
|
2016-04-09 18:41:07 +02:00
|
|
|
tp->flag &= ~(ISSET|ALLOC|FKSH);
|
2005-05-23 05:06:10 +02:00
|
|
|
tfree(tp->val.t, tp->areap);
|
|
|
|
}
|
|
|
|
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
if (t == NULL) {
|
|
|
|
/* undefine */
|
2009-08-28 22:30:59 +02:00
|
|
|
ktdelete(tp);
|
2009-04-07 21:13:11 +02:00
|
|
|
return (was_set ? 0 : 1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tp->val.t = tcopy(t->left, tp->areap);
|
|
|
|
tp->flag |= (ISSET|ALLOC);
|
|
|
|
if (t->u.ksh_func)
|
|
|
|
tp->flag |= FKSH;
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* add builtin
|
|
|
|
*/
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
const char *
|
2007-03-04 01:13:17 +01:00
|
|
|
builtin(const char *name, int (*func) (const char **))
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct tbl *tp;
|
2013-04-26 23:22:50 +02:00
|
|
|
uint32_t flag = DEFINED;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* see if any flags should be set for this builtin */
|
2016-07-25 01:10:04 +02:00
|
|
|
flags_loop:
|
|
|
|
switch (*name) {
|
|
|
|
case '=':
|
|
|
|
/* command does variable assignment */
|
|
|
|
flag |= KEEPASN;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
/* POSIX special builtin */
|
|
|
|
flag |= SPEC_BI;
|
|
|
|
break;
|
|
|
|
case '~':
|
|
|
|
/* external utility overrides built-in utility, always */
|
|
|
|
flag |= LOWER_BI;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case '!':
|
|
|
|
/* external utility overrides built-in utility, with flags */
|
|
|
|
flag |= LOW_BI;
|
|
|
|
break;
|
2017-03-12 03:04:15 +01:00
|
|
|
case '-':
|
|
|
|
/* is declaration utility if argv[1] is one (POSIX: command) */
|
|
|
|
flag |= DECL_FWDR;
|
|
|
|
break;
|
|
|
|
case '^':
|
|
|
|
/* is declaration utility (POSIX: export, readonly) */
|
|
|
|
flag |= DECL_UTIL;
|
|
|
|
break;
|
2016-07-25 01:10:04 +02:00
|
|
|
default:
|
|
|
|
goto flags_seen;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2016-07-25 01:10:04 +02:00
|
|
|
++name;
|
|
|
|
goto flags_loop;
|
|
|
|
flags_seen:
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2016-07-25 01:10:04 +02:00
|
|
|
/* enter into the builtins hash table */
|
2009-08-28 22:30:59 +02:00
|
|
|
tp = ktenter(&builtins, name, hash(name));
|
2013-04-26 23:22:50 +02:00
|
|
|
tp->flag = flag;
|
2005-05-23 05:06:10 +02:00
|
|
|
tp->type = CSHELL;
|
|
|
|
tp->val.f = func;
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
|
2016-07-25 01:10:04 +02:00
|
|
|
/* return name, for direct builtin call check in main.c */
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
return (name);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* find command
|
|
|
|
* either function, hashed command, or built-in (in that order)
|
|
|
|
*/
|
|
|
|
struct tbl *
|
|
|
|
findcom(const char *name, int flags)
|
|
|
|
{
|
|
|
|
static struct tbl temp;
|
2009-08-28 20:54:01 +02:00
|
|
|
uint32_t h = hash(name);
|
2005-05-23 05:06:10 +02:00
|
|
|
struct tbl *tp = NULL, *tbi;
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/* insert if not found */
|
|
|
|
unsigned char insert = Flag(FTRACKALL);
|
|
|
|
/* for function autoloading */
|
|
|
|
char *fpath;
|
2007-09-09 20:06:42 +02:00
|
|
|
union mksh_cchack npath;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2017-03-22 01:20:53 +01:00
|
|
|
if (mksh_vdirsep(name)
|
2017-04-02 15:08:07 +02:00
|
|
|
#ifdef MKSH_DOSPATH
|
2017-03-22 01:20:53 +01:00
|
|
|
&& (strcmp(name, T_builtin) != 0)
|
|
|
|
#endif
|
|
|
|
) {
|
2005-05-23 05:06:10 +02:00
|
|
|
insert = 0;
|
|
|
|
/* prevent FPATH search below */
|
|
|
|
flags &= ~FC_FUNC;
|
|
|
|
goto Search;
|
|
|
|
}
|
2009-08-28 22:30:59 +02:00
|
|
|
tbi = (flags & FC_BI) ? ktsearch(&builtins, name, h) : NULL;
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/*
|
|
|
|
* POSIX says special builtins first, then functions, then
|
2013-04-26 23:22:50 +02:00
|
|
|
* regular builtins, then search path...
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
if ((flags & FC_SPECBI) && tbi && (tbi->flag & SPEC_BI))
|
|
|
|
tp = tbi;
|
|
|
|
if (!tp && (flags & FC_FUNC)) {
|
2009-08-28 22:30:59 +02:00
|
|
|
tp = findfunc(name, h, false);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (tp && !(tp->flag & ISSET)) {
|
2016-07-25 02:04:48 +02:00
|
|
|
if ((fpath = str_val(global(TFPATH))) == null) {
|
2005-05-23 05:06:10 +02:00
|
|
|
tp->u.fpath = NULL;
|
2011-09-07 17:24:22 +02:00
|
|
|
tp->u2.errnov = ENOENT;
|
2005-05-23 05:06:10 +02:00
|
|
|
} else
|
2011-09-07 17:24:22 +02:00
|
|
|
tp->u.fpath = search_path(name, fpath, R_OK,
|
|
|
|
&tp->u2.errnov);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
2013-04-26 23:22:50 +02:00
|
|
|
if (!tp && (flags & FC_NORMBI) && tbi)
|
2005-05-23 05:06:10 +02:00
|
|
|
tp = tbi;
|
|
|
|
if (!tp && (flags & FC_PATH) && !(flags & FC_DEFPATH)) {
|
2009-08-28 22:30:59 +02:00
|
|
|
tp = ktsearch(&taliases, name, h);
|
2011-09-07 17:24:22 +02:00
|
|
|
if (tp && (tp->flag & ISSET) &&
|
|
|
|
ksh_access(tp->val.s, X_OK) != 0) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (tp->flag & ALLOC) {
|
|
|
|
tp->flag &= ~ALLOC;
|
|
|
|
afree(tp->val.s, APERM);
|
|
|
|
}
|
|
|
|
tp->flag &= ~ISSET;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-01 15:43:28 +02:00
|
|
|
Search:
|
2005-05-23 05:06:10 +02:00
|
|
|
if ((!tp || (tp->type == CTALIAS && !(tp->flag&ISSET))) &&
|
|
|
|
(flags & FC_PATH)) {
|
|
|
|
if (!tp) {
|
|
|
|
if (insert && !(flags & FC_DEFPATH)) {
|
2009-08-28 22:30:59 +02:00
|
|
|
tp = ktenter(&taliases, name, h);
|
2005-05-23 05:06:10 +02:00
|
|
|
tp->type = CTALIAS;
|
|
|
|
} else {
|
|
|
|
tp = &temp;
|
|
|
|
tp->type = CEXEC;
|
|
|
|
}
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/* make ~ISSET */
|
|
|
|
tp->flag = DEFINED;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2011-09-07 17:24:22 +02:00
|
|
|
npath.ro = search_path(name,
|
|
|
|
(flags & FC_DEFPATH) ? def_path : path,
|
|
|
|
X_OK, &tp->u2.errnov);
|
2007-09-09 20:06:42 +02:00
|
|
|
if (npath.ro) {
|
2008-10-28 15:32:43 +01:00
|
|
|
strdupx(tp->val.s, npath.ro, APERM);
|
|
|
|
if (npath.ro != name)
|
|
|
|
afree(npath.rw, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
tp->flag |= ISSET|ALLOC;
|
|
|
|
} else if ((flags & FC_FUNC) &&
|
2016-07-25 02:04:48 +02:00
|
|
|
(fpath = str_val(global(TFPATH))) != null &&
|
2011-09-07 17:24:22 +02:00
|
|
|
(npath.ro = search_path(name, fpath, R_OK,
|
|
|
|
&tp->u2.errnov)) != NULL) {
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/*
|
|
|
|
* An undocumented feature of AT&T ksh is that
|
|
|
|
* it searches FPATH if a command is not found,
|
|
|
|
* even if the command hasn't been set up as an
|
|
|
|
* autoloaded function (ie, no typeset -uf).
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
tp = &temp;
|
|
|
|
tp->type = CFUNC;
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/* make ~ISSET */
|
|
|
|
tp->flag = DEFINED;
|
2007-09-09 20:06:42 +02:00
|
|
|
tp->u.fpath = npath.ro;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (tp);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* flush executable commands with relative paths
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
* (just relative or all?)
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
void
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
flushcom(bool all)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct tbl *tp;
|
|
|
|
struct tstate ts;
|
|
|
|
|
2006-01-29 21:04:54 +01:00
|
|
|
for (ktwalk(&ts, &taliases); (tp = ktnext(&ts)) != NULL; )
|
2015-07-10 21:36:38 +02:00
|
|
|
if ((tp->flag&ISSET) && (all || !mksh_abspath(tp->val.s))) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (tp->flag&ALLOC) {
|
|
|
|
tp->flag &= ~(ALLOC|ISSET);
|
|
|
|
afree(tp->val.s, APERM);
|
|
|
|
}
|
|
|
|
tp->flag &= ~ISSET;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-07 17:24:22 +02:00
|
|
|
/* check if path is something we want to find */
|
2015-10-09 23:36:59 +02:00
|
|
|
int
|
2011-09-07 17:24:22 +02:00
|
|
|
search_access(const char *fn, int mode)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2011-09-07 17:24:22 +02:00
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if (stat(fn, &sb) < 0)
|
|
|
|
/* file does not exist */
|
|
|
|
return (ENOENT);
|
|
|
|
/* LINTED use of access */
|
2015-10-09 17:28:20 +02:00
|
|
|
if (access(fn, mode) < 0) {
|
2011-09-07 17:24:22 +02:00
|
|
|
/* file exists, but we can't access it */
|
2015-10-09 17:28:20 +02:00
|
|
|
int eno;
|
|
|
|
|
|
|
|
eno = errno;
|
|
|
|
return (eno ? eno : EACCES);
|
|
|
|
}
|
2016-11-12 00:31:39 +01:00
|
|
|
#ifdef __OS2__
|
|
|
|
/* treat all files as executable on OS/2 */
|
2016-11-12 04:54:48 +01:00
|
|
|
sb.st_mode |= S_IXUSR | S_IXGRP | S_IXOTH;
|
2016-11-12 00:31:39 +01:00
|
|
|
#endif
|
2011-09-07 17:24:22 +02:00
|
|
|
if (mode == X_OK && (!S_ISREG(sb.st_mode) ||
|
2016-11-12 00:31:39 +01:00
|
|
|
!(sb.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))))
|
2011-09-07 17:24:22 +02:00
|
|
|
/* access(2) may say root can execute everything */
|
|
|
|
return (S_ISDIR(sb.st_mode) ? EISDIR : EACCES);
|
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2015-05-07 05:59:55 +02:00
|
|
|
#ifdef __OS2__
|
|
|
|
/* check if path is something we want to find, adding executable extensions */
|
|
|
|
#define search_access(fn, mode) access_ex((search_access), (fn), (mode))
|
|
|
|
#else
|
|
|
|
#define search_access(fn, mode) (search_access)((fn), (mode))
|
|
|
|
#endif
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
/*
|
|
|
|
* search for command with PATH
|
|
|
|
*/
|
2006-11-12 15:58:16 +01:00
|
|
|
const char *
|
2011-09-07 17:24:22 +02:00
|
|
|
search_path(const char *name, const char *lpath,
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/* R_OK or X_OK */
|
|
|
|
int mode,
|
|
|
|
/* set if candidate found, but not suitable */
|
|
|
|
int *errnop)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
const char *sp, *p;
|
|
|
|
char *xp;
|
|
|
|
XString xs;
|
2011-08-27 20:06:52 +02:00
|
|
|
size_t namelen;
|
2011-09-07 17:24:22 +02:00
|
|
|
int ec = 0, ev;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2016-11-12 00:31:39 +01:00
|
|
|
if (mksh_vdirsep(name)) {
|
2011-09-07 17:24:22 +02:00
|
|
|
if ((ec = search_access(name, mode)) == 0) {
|
|
|
|
search_path_ok:
|
|
|
|
if (errnop)
|
|
|
|
*errnop = 0;
|
2015-05-07 05:59:55 +02:00
|
|
|
#ifndef __OS2__
|
2006-11-12 15:58:16 +01:00
|
|
|
return (name);
|
2015-05-07 05:59:55 +02:00
|
|
|
#else
|
2017-03-26 00:05:25 +01:00
|
|
|
return (real_exec_name(name));
|
2015-05-07 05:59:55 +02:00
|
|
|
#endif
|
2011-09-07 17:24:22 +02:00
|
|
|
}
|
|
|
|
goto search_path_err;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namelen = strlen(name) + 1;
|
|
|
|
Xinit(xs, xp, 128, ATEMP);
|
|
|
|
|
|
|
|
sp = lpath;
|
|
|
|
while (sp != NULL) {
|
|
|
|
xp = Xstring(xs, xp);
|
2015-07-10 21:36:38 +02:00
|
|
|
if (!(p = cstrchr(sp, MKSH_PATHSEPC)))
|
2005-05-23 05:06:10 +02:00
|
|
|
p = sp + strlen(sp);
|
|
|
|
if (p != sp) {
|
|
|
|
XcheckN(xs, xp, p - sp);
|
|
|
|
memcpy(xp, sp, p - sp);
|
|
|
|
xp += p - sp;
|
2016-12-23 07:41:30 +01:00
|
|
|
#ifdef __OS2__
|
|
|
|
if (xp > Xstring(xs, xp) && mksh_cdirsep(xp[-1]))
|
|
|
|
xp--;
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
*xp++ = '/';
|
|
|
|
}
|
|
|
|
sp = p;
|
|
|
|
XcheckN(xs, xp, namelen);
|
|
|
|
memcpy(xp, name, namelen);
|
2011-09-07 17:24:22 +02:00
|
|
|
if ((ev = search_access(Xstring(xs, xp), mode)) == 0) {
|
|
|
|
name = Xclose(xs, xp + namelen);
|
|
|
|
goto search_path_ok;
|
|
|
|
}
|
|
|
|
/* accumulate non-ENOENT errors only */
|
|
|
|
if (ev != ENOENT && ec == 0)
|
|
|
|
ec = ev;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (*sp++ == '\0')
|
|
|
|
sp = NULL;
|
|
|
|
}
|
|
|
|
Xfree(xs, xp);
|
2011-09-07 17:24:22 +02:00
|
|
|
search_path_err:
|
|
|
|
if (errnop)
|
|
|
|
*errnop = ec ? ec : ENOENT;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2014-10-12 23:58:53 +02:00
|
|
|
call_builtin(struct tbl *tp, const char **wp, const char *where, bool resetspec)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
2012-12-04 02:18:34 +01:00
|
|
|
if (!tp)
|
2016-07-25 02:04:48 +02:00
|
|
|
internal_errorf(Tf_sD_s, where, wp[0]);
|
2005-05-23 05:06:10 +02:00
|
|
|
builtin_argv0 = wp[0];
|
2017-03-11 23:58:51 +01:00
|
|
|
builtin_spec = tobool(!resetspec && (tp->flag & SPEC_BI));
|
2005-05-23 05:06:10 +02:00
|
|
|
shf_reopen(1, SHF_WR, shl_stdout);
|
2010-08-28 17:39:20 +02:00
|
|
|
shl_stdout_ok = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
ksh_getopt_reset(&builtin_opt, GF_ERROR);
|
|
|
|
rv = (*tp->val.f)(wp);
|
|
|
|
shf_flush(shl_stdout);
|
2010-08-28 17:39:20 +02:00
|
|
|
shl_stdout_ok = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
builtin_argv0 = NULL;
|
2014-10-12 23:58:53 +02:00
|
|
|
builtin_spec = false;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set up redirection, saving old fds in e->savefd
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
iosetup(struct ioword *iop, struct tbl *tp)
|
|
|
|
{
|
|
|
|
int u = -1;
|
2015-10-09 21:29:50 +02:00
|
|
|
char *cp = iop->ioname;
|
2015-04-12 00:03:32 +02:00
|
|
|
int iotype = iop->ioflag & IOTYPE;
|
2015-12-31 22:03:47 +01:00
|
|
|
bool do_open = true, do_close = false, do_fstat = false;
|
2012-06-24 22:05:23 +02:00
|
|
|
int flags = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
struct ioword iotmp;
|
|
|
|
struct stat statb;
|
|
|
|
|
|
|
|
if (iotype != IOHERE)
|
|
|
|
cp = evalonestr(cp, DOTILDE|(Flag(FTALKING_I) ? DOGLOB : 0));
|
|
|
|
|
|
|
|
/* Used for tracing and error messages to print expanded cp */
|
|
|
|
iotmp = *iop;
|
2015-10-09 21:29:50 +02:00
|
|
|
iotmp.ioname = (iotype == IOHERE) ? NULL : cp;
|
2015-04-12 00:03:32 +02:00
|
|
|
iotmp.ioflag |= IONAMEXP;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2013-07-21 20:47:24 +02:00
|
|
|
if (Flag(FXTRACE)) {
|
|
|
|
change_xtrace(2, false);
|
2016-07-25 02:04:48 +02:00
|
|
|
fptreef(shl_xtrace, 0, Tft_R, &iotmp);
|
2013-07-21 20:47:24 +02:00
|
|
|
change_xtrace(1, false);
|
|
|
|
}
|
2013-04-26 21:40:45 +02:00
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
switch (iotype) {
|
|
|
|
case IOREAD:
|
|
|
|
flags = O_RDONLY;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IOCAT:
|
|
|
|
flags = O_WRONLY | O_APPEND | O_CREAT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IOWRITE:
|
2015-12-31 22:03:47 +01:00
|
|
|
if (Flag(FNOCLOBBER) && !(iop->ioflag & IOCLOB)) {
|
|
|
|
/* >file under set -C */
|
|
|
|
if (stat(cp, &statb)) {
|
|
|
|
/* nonexistent file */
|
|
|
|
flags = O_WRONLY | O_CREAT | O_EXCL;
|
|
|
|
} else if (S_ISREG(statb.st_mode)) {
|
|
|
|
/* regular file, refuse clobbering */
|
|
|
|
goto clobber_refused;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* allow redirections to things
|
|
|
|
* like /dev/null without error
|
|
|
|
*/
|
|
|
|
flags = O_WRONLY;
|
|
|
|
/* but check again after opening */
|
|
|
|
do_fstat = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* >|file or set +C */
|
|
|
|
flags = O_WRONLY | O_CREAT | O_TRUNC;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IORDWR:
|
|
|
|
flags = O_RDWR | O_CREAT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IOHERE:
|
2012-06-24 22:05:23 +02:00
|
|
|
do_open = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
/* herein() returns -2 if error has been printed */
|
2013-01-19 20:47:13 +01:00
|
|
|
u = herein(iop, NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
/* cp may have wrong name */
|
|
|
|
break;
|
|
|
|
|
2009-06-10 20:12:51 +02:00
|
|
|
case IODUP: {
|
2005-05-23 05:06:10 +02:00
|
|
|
const char *emsg;
|
|
|
|
|
2012-06-24 22:05:23 +02:00
|
|
|
do_open = false;
|
2015-04-29 20:32:44 +02:00
|
|
|
if (ksh_isdash(cp)) {
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/* prevent error return below */
|
|
|
|
u = 1009;
|
2012-06-24 22:05:23 +02:00
|
|
|
do_close = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
} else if ((u = check_fd(cp,
|
2015-04-12 00:03:32 +02:00
|
|
|
X_OK | ((iop->ioflag & IORDUP) ? R_OK : W_OK),
|
2005-05-23 05:06:10 +02:00
|
|
|
&emsg)) < 0) {
|
2013-03-24 01:56:27 +01:00
|
|
|
char *sp;
|
|
|
|
|
2016-07-25 02:04:48 +02:00
|
|
|
warningf(true, Tf_sD_s,
|
|
|
|
(sp = snptreef(NULL, 32, Tft_R, &iotmp)), emsg);
|
2013-03-24 01:56:27 +01:00
|
|
|
afree(sp, ATEMP);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2015-04-12 00:03:32 +02:00
|
|
|
if (u == (int)iop->unit)
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/* "dup from" == "dup to" */
|
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
2012-06-24 22:05:23 +02:00
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (do_open) {
|
|
|
|
if (Flag(FRESTRICTED) && (flags & O_CREAT)) {
|
2016-07-25 02:04:48 +02:00
|
|
|
warningf(true, Tf_sD_s, cp, "restricted");
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2015-07-09 22:52:43 +02:00
|
|
|
u = binopen3(cp, flags, 0666);
|
2015-12-31 22:03:47 +01:00
|
|
|
if (do_fstat && u >= 0) {
|
|
|
|
/* prevent race conditions */
|
|
|
|
if (fstat(u, &statb) || S_ISREG(statb.st_mode)) {
|
|
|
|
close(u);
|
|
|
|
clobber_refused:
|
|
|
|
u = -1;
|
|
|
|
errno = EEXIST;
|
|
|
|
}
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (u < 0) {
|
|
|
|
/* herein() may already have printed message */
|
2007-06-07 01:28:17 +02:00
|
|
|
if (u == -1) {
|
|
|
|
u = errno;
|
2016-07-25 02:04:48 +02:00
|
|
|
warningf(true, Tf_cant,
|
2005-05-23 05:06:10 +02:00
|
|
|
iotype == IODUP ? "dup" :
|
|
|
|
(iotype == IOREAD || iotype == IOHERE) ?
|
2016-07-25 02:04:48 +02:00
|
|
|
Topen : Tcreate, cp, cstrerror(u));
|
2007-06-07 01:28:17 +02:00
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
/* Do not save if it has already been redirected (i.e. "cat >x >y"). */
|
|
|
|
if (e->savefd[iop->unit] == 0) {
|
|
|
|
/* If these are the same, it means unit was previously closed */
|
2015-04-12 00:03:32 +02:00
|
|
|
if (u == (int)iop->unit)
|
2005-05-23 05:06:10 +02:00
|
|
|
e->savefd[iop->unit] = -1;
|
|
|
|
else
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/*
|
|
|
|
* c_exec() assumes e->savefd[fd] set for any
|
2009-06-10 20:12:51 +02:00
|
|
|
* redirections. Ask savefd() not to close iop->unit;
|
2005-05-23 05:06:10 +02:00
|
|
|
* this allows error messages to be seen if iop->unit
|
|
|
|
* is 2; also means we can't lose the fd (eg, both
|
|
|
|
* dup2 below and dup2 in restfd() failing).
|
|
|
|
*/
|
2006-05-10 20:54:13 +02:00
|
|
|
e->savefd[iop->unit] = savefd(iop->unit);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (do_close)
|
|
|
|
close(iop->unit);
|
2015-04-12 00:03:32 +02:00
|
|
|
else if (u != (int)iop->unit) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (ksh_dup2(u, iop->unit, true) < 0) {
|
2012-11-30 20:02:10 +01:00
|
|
|
int eno;
|
2013-03-24 01:56:27 +01:00
|
|
|
char *sp;
|
2007-06-07 01:28:17 +02:00
|
|
|
|
2012-11-30 20:02:10 +01:00
|
|
|
eno = errno;
|
2016-07-25 02:04:48 +02:00
|
|
|
warningf(true, Tf_s_sD_s, Tredirection_dup,
|
|
|
|
(sp = snptreef(NULL, 32, Tft_R, &iotmp)),
|
2012-12-18 00:18:11 +01:00
|
|
|
cstrerror(eno));
|
2013-03-24 01:56:27 +01:00
|
|
|
afree(sp, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (iotype != IODUP)
|
|
|
|
close(u);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (iotype != IODUP)
|
|
|
|
close(u);
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/*
|
|
|
|
* Touching any co-process fd in an empty exec
|
2005-05-23 05:06:10 +02:00
|
|
|
* causes the shell to close its copies
|
|
|
|
*/
|
|
|
|
else if (tp && tp->type == CSHELL && tp->val.f == c_exec) {
|
2015-04-12 00:03:32 +02:00
|
|
|
if (iop->ioflag & IORDUP)
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/* possible exec <&p */
|
2005-05-23 05:06:10 +02:00
|
|
|
coproc_read_close(u);
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
else
|
|
|
|
/* possible exec >&p */
|
2005-05-23 05:06:10 +02:00
|
|
|
coproc_write_close(u);
|
|
|
|
}
|
|
|
|
}
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
if (u == 2)
|
|
|
|
/* Clear any write errors */
|
2005-05-23 05:06:10 +02:00
|
|
|
shf_reopen(2, SHF_WR, shl_out);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-01-09 22:57:29 +01:00
|
|
|
* Process here documents by providing the content, either as
|
|
|
|
* result (globally allocated) string or in a temp file; if
|
|
|
|
* unquoted, the string is expanded first.
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
static int
|
2015-09-06 21:47:01 +02:00
|
|
|
hereinval(struct ioword *iop, int sub, char **resbuf, struct shf *shf)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2015-09-06 21:47:01 +02:00
|
|
|
const char * volatile ccp = iop->heredoc;
|
2011-01-09 22:57:29 +01:00
|
|
|
struct source *s, *osource;
|
|
|
|
|
|
|
|
osource = source;
|
|
|
|
newenv(E_ERRH);
|
2012-03-31 19:30:00 +02:00
|
|
|
if (kshsetjmp(e->jbuf)) {
|
2011-01-09 22:57:29 +01:00
|
|
|
source = osource;
|
|
|
|
quitenv(shf);
|
|
|
|
/* special to iosetup(): don't print error */
|
|
|
|
return (-2);
|
|
|
|
}
|
2015-09-06 21:47:01 +02:00
|
|
|
if (iop->ioflag & IOHERESTR) {
|
|
|
|
ccp = evalstr(iop->delim, DOHERESTR | DOSCALAR | DOHEREDOC);
|
|
|
|
} else if (sub) {
|
2011-01-09 22:57:29 +01:00
|
|
|
/* do substitutions on the content of heredoc */
|
|
|
|
s = pushs(SSTRING, ATEMP);
|
2012-12-22 01:03:42 +01:00
|
|
|
s->start = s->str = ccp;
|
2011-01-09 22:57:29 +01:00
|
|
|
source = s;
|
2013-01-19 20:47:13 +01:00
|
|
|
if (yylex(sub) != LWORD)
|
2016-01-21 19:24:45 +01:00
|
|
|
internal_errorf("herein: yylex");
|
2011-01-09 22:57:29 +01:00
|
|
|
source = osource;
|
2015-02-19 23:26:50 +01:00
|
|
|
ccp = evalstr(yylval.cp, DOSCALAR | DOHEREDOC);
|
2012-12-22 01:03:42 +01:00
|
|
|
}
|
2011-01-09 22:57:29 +01:00
|
|
|
|
|
|
|
if (resbuf == NULL)
|
|
|
|
shf_puts(ccp, shf);
|
|
|
|
else
|
|
|
|
strdupx(*resbuf, ccp, APERM);
|
|
|
|
|
|
|
|
quitenv(NULL);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2016-06-26 02:44:59 +02:00
|
|
|
int
|
2013-01-19 20:47:13 +01:00
|
|
|
herein(struct ioword *iop, char **resbuf)
|
2011-01-09 22:57:29 +01:00
|
|
|
{
|
|
|
|
int fd = -1;
|
|
|
|
struct shf *shf;
|
2005-05-23 05:06:10 +02:00
|
|
|
struct temp *h;
|
|
|
|
int i;
|
|
|
|
|
2013-01-19 20:47:13 +01:00
|
|
|
/* lexer substitution flags */
|
2015-04-12 00:03:32 +02:00
|
|
|
i = (iop->ioflag & IOEVAL) ? (ONEWORD | HEREDOC) : 0;
|
2013-01-19 20:47:13 +01:00
|
|
|
|
2011-01-09 22:57:29 +01:00
|
|
|
/* skip all the fd setup if we just want the value */
|
|
|
|
if (resbuf != NULL)
|
2015-09-06 21:47:01 +02:00
|
|
|
return (hereinval(iop, i, resbuf, NULL));
|
2011-01-09 22:57:29 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create temp file to hold content (done before newenv
|
|
|
|
* so temp doesn't get removed too soon).
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
h = maketemp(ATEMP, TT_HEREDOC_EXP, &e->temps);
|
2015-07-09 22:52:43 +02:00
|
|
|
if (!(shf = h->shf) || (fd = binopen3(h->tffn, O_RDONLY, 0)) < 0) {
|
2011-01-09 22:57:29 +01:00
|
|
|
i = errno;
|
2016-07-25 02:04:48 +02:00
|
|
|
warningf(true, Tf_temp,
|
|
|
|
!shf ? Tcreate : Topen, h->tffn, cstrerror(i));
|
2005-05-23 05:06:10 +02:00
|
|
|
if (shf)
|
|
|
|
shf_close(shf);
|
2011-01-09 22:57:29 +01:00
|
|
|
/* special to iosetup(): don't print error */
|
|
|
|
return (-2);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2015-09-06 21:47:01 +02:00
|
|
|
if (hereinval(iop, i, NULL, shf) == -2) {
|
2005-05-23 05:06:10 +02:00
|
|
|
close(fd);
|
2011-01-09 22:57:29 +01:00
|
|
|
/* special to iosetup(): don't print error */
|
|
|
|
return (-2);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2015-02-06 11:09:07 +01:00
|
|
|
if (shf_close(shf) == -1) {
|
2007-06-07 01:28:17 +02:00
|
|
|
i = errno;
|
2005-05-23 05:06:10 +02:00
|
|
|
close(fd);
|
2016-07-25 02:04:48 +02:00
|
|
|
warningf(true, Tf_temp,
|
|
|
|
Twrite, h->tffn, cstrerror(i));
|
2011-01-09 22:57:29 +01:00
|
|
|
/* special to iosetup(): don't print error */
|
|
|
|
return (-2);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (fd);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ksh special - the select command processing section
|
|
|
|
* print the args in column form - assuming that we can
|
|
|
|
*/
|
2007-03-04 01:13:17 +01:00
|
|
|
static const char *
|
|
|
|
do_selectargs(const char **ap, bool print_menu)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2007-03-04 01:13:17 +01:00
|
|
|
static const char *read_args[] = {
|
2016-07-25 02:04:48 +02:00
|
|
|
Tread, "-r", "REPLY", NULL
|
2005-05-23 05:06:10 +02:00
|
|
|
};
|
|
|
|
char *s;
|
|
|
|
int i, argct;
|
|
|
|
|
|
|
|
for (argct = 0; ap[argct]; argct++)
|
|
|
|
;
|
2011-03-13 02:20:25 +01:00
|
|
|
while (/* CONSTCOND */ 1) {
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/*-
|
|
|
|
* Menu is printed if
|
2005-05-23 05:06:10 +02:00
|
|
|
* - this is the first time around the select loop
|
|
|
|
* - the user enters a blank line
|
|
|
|
* - the REPLY parameter is empty
|
|
|
|
*/
|
|
|
|
if (print_menu || !*str_val(global("REPLY")))
|
|
|
|
pr_menu(ap);
|
2016-07-25 02:04:48 +02:00
|
|
|
shellf(Tf_s, str_val(global("PS3")));
|
|
|
|
if (call_builtin(findcom(Tread, FC_BI), read_args, Tselect,
|
2014-10-12 23:58:53 +02:00
|
|
|
false))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (NULL);
|
2015-04-19 16:40:09 +02:00
|
|
|
if (*(s = str_val(global("REPLY"))))
|
|
|
|
return ((getn(s, &i) && i >= 1 && i <= argct) ?
|
|
|
|
ap[i - 1] : null);
|
2012-10-03 17:13:34 +02:00
|
|
|
print_menu = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct select_menu_info {
|
2009-11-10 00:35:11 +01:00
|
|
|
const char * const *args;
|
2007-03-04 01:13:17 +01:00
|
|
|
int num_width;
|
2006-01-29 21:04:54 +01:00
|
|
|
};
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* format a single select menu item */
|
2015-07-05 16:43:08 +02:00
|
|
|
static void
|
2012-10-03 17:13:34 +02:00
|
|
|
select_fmt_entry(char *buf, size_t buflen, unsigned int i, const void *arg)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2006-11-12 15:58:16 +01:00
|
|
|
const struct select_menu_info *smi =
|
|
|
|
(const struct select_menu_info *)arg;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2012-10-03 17:13:34 +02:00
|
|
|
shf_snprintf(buf, buflen, "%*u) %s",
|
2005-05-23 05:06:10 +02:00
|
|
|
smi->num_width, i + 1, smi->args[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* print a select style menu
|
|
|
|
*/
|
2012-10-03 17:13:34 +02:00
|
|
|
void
|
2009-11-10 00:35:11 +01:00
|
|
|
pr_menu(const char * const *ap)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct select_menu_info smi;
|
2009-11-10 00:35:11 +01:00
|
|
|
const char * const *pp;
|
2011-08-27 20:06:52 +02:00
|
|
|
size_t acols = 0, aocts = 0, i;
|
2012-10-03 17:13:34 +02:00
|
|
|
unsigned int n;
|
2016-11-11 21:14:18 +01:00
|
|
|
struct columnise_opts co;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-11-10 00:35:11 +01:00
|
|
|
/*
|
|
|
|
* width/column calculations were done once and saved, but this
|
|
|
|
* means select can't be used recursively so we re-calculate
|
|
|
|
* each time (could save in a structure that is returned, but
|
|
|
|
* it's probably not worth the bother)
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get dimensions of the list
|
|
|
|
*/
|
2009-11-10 00:35:11 +01:00
|
|
|
for (n = 0, pp = ap; *pp; n++, pp++) {
|
|
|
|
i = strlen(*pp);
|
|
|
|
if (i > aocts)
|
|
|
|
aocts = i;
|
2009-11-28 15:28:03 +01:00
|
|
|
i = utf_mbswidth(*pp);
|
2009-11-10 00:35:11 +01:00
|
|
|
if (i > acols)
|
|
|
|
acols = i;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2009-11-10 00:35:11 +01:00
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
/*
|
2009-11-10 00:35:11 +01:00
|
|
|
* we will print an index of the form "%d) " in front of
|
|
|
|
* each entry, so get the maximum width of this
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
2009-11-10 00:35:11 +01:00
|
|
|
for (i = n, smi.num_width = 1; i >= 10; i /= 10)
|
|
|
|
smi.num_width++;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
smi.args = ap;
|
2016-11-11 21:14:18 +01:00
|
|
|
co.shf = shl_out;
|
2016-11-11 22:13:25 +01:00
|
|
|
co.linesep = '\n';
|
|
|
|
co.prefcol = co.do_last = true;
|
2016-11-11 21:14:18 +01:00
|
|
|
print_columns(&co, n, select_fmt_entry, (void *)&smi,
|
|
|
|
smi.num_width + 2 + aocts, smi.num_width + 2 + acols);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2015-07-05 16:43:08 +02:00
|
|
|
static void
|
2012-10-03 17:13:34 +02:00
|
|
|
plain_fmt_entry(char *buf, size_t buflen, unsigned int i, const void *arg)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2010-08-28 22:22:24 +02:00
|
|
|
strlcpy(buf, ((const char * const *)arg)[i], buflen);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2012-10-03 17:13:34 +02:00
|
|
|
void
|
2016-11-11 21:14:18 +01:00
|
|
|
pr_list(struct columnise_opts *cop, char * const *ap)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2011-08-27 20:06:52 +02:00
|
|
|
size_t acols = 0, aocts = 0, i;
|
2012-10-03 17:13:34 +02:00
|
|
|
unsigned int n;
|
2009-11-10 00:35:11 +01:00
|
|
|
char * const *pp;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-11-10 00:35:11 +01:00
|
|
|
for (n = 0, pp = ap; *pp; n++, pp++) {
|
|
|
|
i = strlen(*pp);
|
|
|
|
if (i > aocts)
|
|
|
|
aocts = i;
|
2009-11-28 15:28:03 +01:00
|
|
|
i = utf_mbswidth(*pp);
|
2009-11-10 00:35:11 +01:00
|
|
|
if (i > acols)
|
|
|
|
acols = i;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2009-11-10 00:35:11 +01:00
|
|
|
|
2016-11-11 21:14:18 +01:00
|
|
|
print_columns(cop, n, plain_fmt_entry, (const void *)ap,
|
|
|
|
aocts, acols);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* [[ ... ]] evaluation routines
|
|
|
|
*/
|
|
|
|
|
2010-07-18 00:09:40 +02:00
|
|
|
/*
|
|
|
|
* Test if the current token is a whatever. Accepts the current token if
|
2009-06-10 20:12:51 +02:00
|
|
|
* it is. Returns 0 if it is not, non-zero if it is (in the case of
|
2005-05-23 05:06:10 +02:00
|
|
|
* TM_UNOP and TM_BINOP, the returned value is a Test_op).
|
|
|
|
*/
|
2010-07-18 00:09:40 +02:00
|
|
|
static Test_op
|
2005-05-23 05:06:10 +02:00
|
|
|
dbteste_isa(Test_env *te, Test_meta meta)
|
|
|
|
{
|
2010-07-18 00:09:40 +02:00
|
|
|
Test_op ret = TO_NONOP;
|
2013-09-10 18:30:50 +02:00
|
|
|
bool uqword;
|
2007-03-04 01:13:17 +01:00
|
|
|
const char *p;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (!*te->pos.wp)
|
2010-07-18 00:09:40 +02:00
|
|
|
return (meta == TM_END ? TO_NONNULL : TO_NONOP);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* unquoted word? */
|
|
|
|
for (p = *te->pos.wp; *p == CHAR; p += 2)
|
|
|
|
;
|
|
|
|
uqword = *p == EOS;
|
|
|
|
|
|
|
|
if (meta == TM_UNOP || meta == TM_BINOP) {
|
|
|
|
if (uqword) {
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/* longer than the longest operator */
|
|
|
|
char buf[8];
|
2005-05-23 05:06:10 +02:00
|
|
|
char *q = buf;
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
|
2012-10-03 19:24:23 +02:00
|
|
|
p = *te->pos.wp;
|
|
|
|
while (*p++ == CHAR &&
|
|
|
|
(size_t)(q - buf) < sizeof(buf) - 1)
|
|
|
|
*q++ = *p++;
|
2005-05-23 05:06:10 +02:00
|
|
|
*q = '\0';
|
2005-10-08 21:31:00 +02:00
|
|
|
ret = test_isop(meta, buf);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
} else if (meta == TM_END)
|
2010-07-18 00:09:40 +02:00
|
|
|
ret = TO_NONOP;
|
2005-05-23 05:06:10 +02:00
|
|
|
else
|
2010-07-18 00:09:40 +02:00
|
|
|
ret = (uqword && !strcmp(*te->pos.wp,
|
|
|
|
dbtest_tokens[(int)meta])) ? TO_NONNULL : TO_NONOP;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* Accept the token? */
|
2010-07-18 00:09:40 +02:00
|
|
|
if (ret != TO_NONOP)
|
2005-05-23 05:06:10 +02:00
|
|
|
te->pos.wp++;
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (ret);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
2008-04-02 00:20:20 +02:00
|
|
|
dbteste_getopnd(Test_env *te, Test_op op, bool do_eval)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2007-03-04 01:13:17 +01:00
|
|
|
const char *s = *te->pos.wp;
|
2015-02-19 22:58:19 +01:00
|
|
|
int flags = DOTILDE | DOSCALAR;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (!s)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
te->pos.wp++;
|
|
|
|
|
|
|
|
if (!do_eval)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (null);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (op == TO_STEQL || op == TO_STNEQ)
|
2015-02-19 22:58:19 +01:00
|
|
|
flags |= DOPAT;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2015-02-19 22:58:19 +01:00
|
|
|
return (evalstr(s, flags));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dbteste_error(Test_env *te, int offset, const char *msg)
|
|
|
|
{
|
|
|
|
te->flags |= TEF_ERROR;
|
2007-05-13 19:51:24 +02:00
|
|
|
internal_warningf("dbteste_error: %s (offset %d)", msg, offset);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|