2009-03-22 18:47:38 +01:00
|
|
|
/* $OpenBSD: exec.c,v 1.49 2009/01/29 23:27:26 jaredy 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,
|
2013-01-01 21:45:04 +01:00
|
|
|
* 2011, 2012, 2013
|
2009-05-16 18:59:42 +02:00
|
|
|
* Thorsten Glaser <tg@mirbsd.org>
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
2013-02-17 06:40:17 +01:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.116 2013/02/17 05:40:15 tg Exp $");
|
2010-03-27 16:29:00 +01:00
|
|
|
|
|
|
|
#ifndef MKSH_DEFAULT_EXECSHELL
|
|
|
|
#define MKSH_DEFAULT_EXECSHELL "/bin/sh"
|
|
|
|
#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;
|
2012-12-04 02:18:34 +01:00
|
|
|
static int call_builtin(struct tbl *, const char **, const char *);
|
2007-03-04 01:13:17 +01:00
|
|
|
static int iosetup(struct ioword *, struct tbl *);
|
2013-01-19 20:47:13 +01:00
|
|
|
static int herein(struct ioword *, char **);
|
2007-03-04 01:13:17 +01:00
|
|
|
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 *);
|
2011-09-07 17:24:22 +02:00
|
|
|
static int search_access(const char *, int);
|
2012-10-03 17:13:34 +02:00
|
|
|
/* XXX: horrible kludge to fit within the framework */
|
|
|
|
static char *plain_fmt_entry(char *, size_t, unsigned int, const void *);
|
|
|
|
static char *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;
|
2011-01-09 22:57:29 +01:00
|
|
|
char *cp;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
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) {
|
2011-01-09 22:57:29 +01:00
|
|
|
/* check if this is 'var=<<EOF' */
|
|
|
|
if (
|
|
|
|
/* we have zero arguments, i.e. no programme to run */
|
|
|
|
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") */
|
|
|
|
(t->ioact[0]->flag & IOTYPE) == IOHERE &&
|
|
|
|
/* 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=") */
|
|
|
|
ccp[0] == CHAR && ccp[1] == '=' && ccp[2] == EOS &&
|
|
|
|
/* plus we can have a here document content */
|
2013-01-19 20:47:13 +01:00
|
|
|
herein(t->ioact[0], &cp) == 0 && cp && *cp) {
|
2011-01-09 22:57:29 +01:00
|
|
|
char *sp = cp, *dp;
|
|
|
|
size_t n = ccp - t->vars[0] + 2, z;
|
|
|
|
|
|
|
|
/* drop redirection (will be garbage collected) */
|
|
|
|
t->ioact = NULL;
|
|
|
|
|
|
|
|
/* set variable to its expanded value */
|
|
|
|
z = strlen(cp) + 1;
|
|
|
|
if (notoktomul(z, 2) || notoktoadd(z * 2, n))
|
2011-09-07 17:24:22 +02:00
|
|
|
internal_errorf(Toomem, (unsigned long)-1);
|
2011-01-09 22:57:29 +01:00
|
|
|
dp = alloc(z * 2 + n, ATEMP);
|
|
|
|
memcpy(dp, t->vars[0], n);
|
|
|
|
t->vars[0] = dp;
|
|
|
|
dp += n;
|
|
|
|
while (*sp) {
|
|
|
|
*dp++ = QCHAR;
|
|
|
|
*dp++ = *sp++;
|
|
|
|
}
|
|
|
|
*dp = EOS;
|
|
|
|
/* free the expanded value */
|
|
|
|
afree(cp, APERM);
|
|
|
|
}
|
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
/*
|
|
|
|
* Clear subst_exstat before argument expansion. Used by
|
2005-05-23 05:06:10 +02:00
|
|
|
* null commands (see comexec() and c_eval()) and by c_set().
|
|
|
|
*/
|
|
|
|
subst_exstat = 0;
|
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
/* for $LINENO */
|
|
|
|
current_lineno = t->lineno;
|
2005-05-23 05:06:10 +02: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 (Flag(FXTRACE) && ap[0]) {
|
2010-08-28 22:22:24 +02:00
|
|
|
shf_puts(substitute(str_val(global("PS4")), 0),
|
|
|
|
shl_out);
|
2005-05-23 05:06:10 +02:00
|
|
|
for (i = 0; ap[i]; i++)
|
2007-07-22 15:34:52 +02:00
|
|
|
shf_fprintf(shl_out, "%s%c", ap[i],
|
|
|
|
ap[i + 1] ? ' ' : '\n');
|
2005-05-23 05:06:10 +02:00
|
|
|
shf_flush(shl_out);
|
|
|
|
}
|
|
|
|
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:
|
2009-03-22 18:47:38 +01:00
|
|
|
rv = execute(t->left, XERROK, xerrok);
|
|
|
|
if ((rv == 0) == (t->type == TAND))
|
|
|
|
rv = execute(t->right, XERROK, xerrok);
|
|
|
|
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;
|
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 {
|
|
|
|
/* TSELECT */
|
2005-05-23 05:06:10 +02:00
|
|
|
for (;;) {
|
2011-01-09 22:57:29 +01:00
|
|
|
if (!(ccp = do_selectargs(ap, is_first))) {
|
2005-05-23 05:06:10 +02:00
|
|
|
rv = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
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);
|
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;
|
2011-01-09 22:57:29 +01:00
|
|
|
ccp = evalstr(t->str, DOTILDE);
|
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))) {
|
|
|
|
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 */
|
2005-05-23 05:06:10 +02:00
|
|
|
s = t->args[0];
|
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
|
2012-12-18 00:18:11 +01:00
|
|
|
errorf("%s: %s", s, 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 |
|
|
|
|
ARRAY | INT_U;
|
|
|
|
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;
|
2013-01-06 19:51:43 +01:00
|
|
|
bool keepasn_ok;
|
2005-05-23 05:06:10 +02:00
|
|
|
int fcflags = FC_BI|FC_FUNC|FC_PATH;
|
2009-10-02 20:08:37 +02:00
|
|
|
bool bourne_function_call = false;
|
2010-04-08 15:21:08 +02:00
|
|
|
struct block *l_expand, *l_assign;
|
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
|
|
|
/*
|
|
|
|
* snag the last argument for $_ XXX not the same as AT&T ksh,
|
2005-05-23 05:06:10 +02:00
|
|
|
* 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-07-04 14:34:24 +02:00
|
|
|
if (Flag(FTALKING) && *(lastp = ap)) {
|
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
|
|
|
*/
|
2013-01-06 19:51:43 +01:00
|
|
|
keepasn_ok = true;
|
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)
|
2011-09-07 17:24:22 +02:00
|
|
|
errorf("%s: %s: %s", Tbuiltin, cp, "not a builtin");
|
2005-05-23 05:06:10 +02:00
|
|
|
continue;
|
|
|
|
} else if (tp->val.f == c_exec) {
|
|
|
|
if (ap[1] == NULL)
|
|
|
|
break;
|
|
|
|
ap++;
|
|
|
|
flags |= XEXEC;
|
|
|
|
} else if (tp->val.f == c_command) {
|
|
|
|
int optc, saw_p = 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
|
|
|
/*
|
|
|
|
* 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')
|
|
|
|
saw_p = 1;
|
|
|
|
if (optc != EOF)
|
• 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)) {
|
2010-08-28 20:50:58 +02:00
|
|
|
warningf(true, "%s: %s",
|
|
|
|
"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.
|
|
|
|
*/
|
2013-01-06 19:51:43 +01:00
|
|
|
keepasn_ok = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (!ap[0]) {
|
|
|
|
/* ensure command with no args exits with 0 */
|
|
|
|
subst_exstat = 0;
|
|
|
|
break;
|
|
|
|
}
|
2011-07-07 23:24:53 +02:00
|
|
|
#ifndef MKSH_NO_EXTERNAL_CAT
|
2010-08-28 19:21:46 +02:00
|
|
|
} else if (tp->val.f == c_cat) {
|
|
|
|
/*
|
|
|
|
* if we have any flags, do not use the builtin
|
|
|
|
* in theory, we could allow -u, but that would
|
|
|
|
* mean to use ksh_getopt here and possibly ad-
|
|
|
|
* ded complexity and more code and isn't worth
|
2011-02-18 23:26:13 +01:00
|
|
|
* additional hassle (and the builtin must call
|
|
|
|
* ksh_getopt already but can't come back here)
|
2010-08-28 19:21:46 +02:00
|
|
|
*/
|
|
|
|
if (ap[1] && ap[1][0] == '-' && ap[1][1] != '\0' &&
|
|
|
|
/* argument, begins with -, is not - or -- */
|
|
|
|
(ap[1][1] != '-' || ap[1][2] != '\0'))
|
|
|
|
/* don't look for builtins or functions */
|
|
|
|
fcflags = FC_PATH;
|
|
|
|
else
|
|
|
|
/* go on, use the builtin */
|
|
|
|
break;
|
2012-10-21 23:26:41 +02:00
|
|
|
#endif
|
2012-11-30 21:19:16 +01:00
|
|
|
#if !defined(MKSH_SMALL)
|
2012-10-21 23:26:41 +02:00
|
|
|
} else if (tp->val.f == c_trap) {
|
|
|
|
t->u.evalflags &= ~DOTCOMEXEC;
|
|
|
|
break;
|
2011-07-07 23:24:53 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
} else
|
|
|
|
break;
|
|
|
|
tp = findcom(ap[0], fcflags & (FC_BI|FC_FUNC));
|
|
|
|
}
|
2012-11-30 21:19:16 +01:00
|
|
|
#if !defined(MKSH_SMALL)
|
2012-10-21 23:26:41 +02:00
|
|
|
if (t->u.evalflags & DOTCOMEXEC)
|
|
|
|
flags |= XEXEC;
|
|
|
|
#endif
|
2010-04-08 15:21:08 +02:00
|
|
|
l_expand = e->loc;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (keepasn_ok && (!ap[0] || (tp && (tp->flag & KEEPASN))))
|
|
|
|
type_flags = 0;
|
|
|
|
else {
|
|
|
|
/* create new variable/function block */
|
|
|
|
newblock();
|
|
|
|
/* ksh functions don't keep assignments, POSIX functions do. */
|
|
|
|
if (keepasn_ok && tp && tp->type == CFUNC &&
|
|
|
|
!(tp->flag & FKSH)) {
|
2009-10-02 20:08:37 +02:00
|
|
|
bourne_function_call = true;
|
2009-10-15 18:25:15 +02:00
|
|
|
type_flags = EXPORT;
|
2005-05-23 05:06:10 +02:00
|
|
|
} else
|
|
|
|
type_flags = LOCAL|LOCAL_COPY|EXPORT;
|
|
|
|
}
|
2010-04-08 15:21:08 +02:00
|
|
|
l_assign = e->loc;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (Flag(FEXPORT))
|
|
|
|
type_flags |= EXPORT;
|
|
|
|
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;
|
2005-05-23 05:06:10 +02:00
|
|
|
cp = evalstr(t->vars[i], DOASNTILDE);
|
2010-04-08 15:21:08 +02:00
|
|
|
e->loc = l_assign;
|
|
|
|
/* but assign in there as usual */
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
if (Flag(FXTRACE)) {
|
|
|
|
if (i == 0)
|
2010-08-28 22:22:24 +02:00
|
|
|
shf_puts(substitute(str_val(global("PS4")), 0),
|
|
|
|
shl_out);
|
2007-07-22 15:34:52 +02:00
|
|
|
shf_fprintf(shl_out, "%s%c", cp,
|
|
|
|
t->vars[i + 1] ? ' ' : '\n');
|
2005-05-23 05:06:10 +02:00
|
|
|
if (!t->vars[i + 1])
|
|
|
|
shf_flush(shl_out);
|
|
|
|
}
|
|
|
|
typeset(cp, type_flags, 0, 0, 0);
|
|
|
|
if (bourne_function_call && !(type_flags & EXPORT))
|
|
|
|
typeset(cp, LOCAL|LOCAL_COPY|EXPORT, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((cp = *ap) == NULL) {
|
|
|
|
rv = subst_exstat;
|
|
|
|
goto Leave;
|
|
|
|
} else if (!tp) {
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
if (Flag(FRESTRICTED) && vstrchr(cp, '/')) {
|
2010-08-28 20:50:58 +02:00
|
|
|
warningf(true, "%s: %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:
|
2012-12-04 02:18:34 +01:00
|
|
|
rv = call_builtin(tp, (const char **)ap, null);
|
2013-01-06 19:51:43 +01:00
|
|
|
if (!keepasn_ok && tp->val.f == c_shift) {
|
|
|
|
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: {
|
2009-08-08 15:08:53 +02:00
|
|
|
volatile unsigned char old_xflag;
|
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;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (!(tp->flag & ISSET)) {
|
|
|
|
struct tbl *ftp;
|
|
|
|
|
|
|
|
if (!tp->u.fpath) {
|
2011-09-07 17:24:22 +02:00
|
|
|
rv = (tp->u2.errnov == ENOENT) ? 127 : 126;
|
|
|
|
warningf(true, "%s: %s %s: %s", cp,
|
|
|
|
"can't find", "function definition file",
|
2012-12-18 00:18:11 +01:00
|
|
|
cstrerror(tp->u2.errnov));
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
}
|
2012-10-30 21:13:20 +01:00
|
|
|
if (include(tp->u.fpath, 0, NULL, false) < 0) {
|
2007-06-07 01:28:17 +02:00
|
|
|
rv = errno;
|
2010-08-28 22:22:24 +02:00
|
|
|
warningf(true, "%s: %s %s %s: %s", cp,
|
|
|
|
"can't open", "function definition file",
|
2012-12-18 00:18:11 +01:00
|
|
|
tp->u.fpath, cstrerror(rv));
|
2005-05-23 05:06:10 +02:00
|
|
|
rv = 127;
|
|
|
|
break;
|
|
|
|
}
|
2009-08-28 22:30:59 +02:00
|
|
|
if (!(ftp = findfunc(cp, hash(cp), false)) ||
|
2005-05-23 05:06:10 +02:00
|
|
|
!(ftp->flag & ISSET)) {
|
2010-08-28 20:50:58 +02:00
|
|
|
warningf(true, "%s: %s %s", cp,
|
|
|
|
"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);
|
|
|
|
}
|
|
|
|
|
|
|
|
old_xflag = Flag(FXTRACE);
|
2011-04-17 17:40:35 +02:00
|
|
|
Flag(FXTRACE) |= tp->flag & TRACE ? 1 : 0;
|
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;
|
|
|
|
}
|
|
|
|
kshname = old_kshname;
|
|
|
|
Flag(FXTRACE) = old_xflag;
|
|
|
|
tp->flag = (tp->flag & ~FINUSE) | old_inuse;
|
• 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
|
|
|
|
* execution tree. TODO: Unfortunately, the table entry
|
|
|
|
* is never re-used until the lookup table is expanded.
|
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);
|
2010-08-28 22:22:24 +02:00
|
|
|
internal_errorf("%s %d", "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;
|
2011-09-07 17:24:22 +02:00
|
|
|
warningf(true, "%s: %s", cp, "not found");
|
|
|
|
} else {
|
|
|
|
rv = 126;
|
|
|
|
warningf(true, "%s: %s: %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;
|
|
|
|
}
|
|
|
|
|
2005-07-04 14:34:24 +02:00
|
|
|
/* set $_ to programme's full path */
|
|
|
|
/* setstr() can't fail here */
|
|
|
|
setstr(typeset("_", LOCAL|EXPORT, 0, INTEGER, 0),
|
|
|
|
tp->val.s, KSH_RETURN_ERROR);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (flags&XEXEC) {
|
|
|
|
j_exit();
|
2009-09-20 18:40:58 +02:00
|
|
|
if (!(flags&XBGND)
|
|
|
|
#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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* to fork we set up a TEXEC node and call execute */
|
|
|
|
texec.type = TEXEC;
|
• 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
|
|
|
/* for tprint */
|
|
|
|
texec.left = t;
|
2005-05-23 05:06:10 +02:00
|
|
|
texec.str = tp->val.s;
|
|
|
|
texec.args = ap;
|
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
|
|
|
unsigned char *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
|
|
|
/* 64 == MAXINTERP in MirBSD <sys/param.h> */
|
|
|
|
char buf[64];
|
2007-04-15 14:09:57 +02:00
|
|
|
int fd;
|
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
|
|
|
|
2006-11-10 02:44:40 +01:00
|
|
|
sh = str_val(global("EXECSHELL"));
|
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
|
2007-04-15 14:09:57 +02:00
|
|
|
if ((fd = open(tp->str, O_RDONLY)) >= 0) {
|
|
|
|
/* read first MAXINTERP octets from file */
|
2009-06-08 22:06:50 +02:00
|
|
|
if (read(fd, buf, sizeof(buf)) <= 0)
|
2007-04-15 14:09:57 +02:00
|
|
|
/* read error -> no good */
|
|
|
|
buf[0] = '\0';
|
|
|
|
close(fd);
|
2011-04-02 12:30:11 +02:00
|
|
|
|
|
|
|
/* skip UTF-8 Byte Order Mark, if present */
|
2007-04-15 14:09:57 +02:00
|
|
|
cp = (unsigned char *)buf;
|
2011-04-02 12:30:11 +02:00
|
|
|
if ((cp[0] == 0xEF) && (cp[1] == 0xBB) && (cp[2] == 0xBF))
|
|
|
|
cp += 3;
|
|
|
|
/* save begin of shebang for later */
|
|
|
|
fd = (char *)cp - buf; /* either 0 or (if BOM) 3 */
|
|
|
|
|
|
|
|
/* scan for newline (or CR) or NUL _before_ end of buffer */
|
2009-06-08 22:06:50 +02:00
|
|
|
while ((char *)cp < (buf + sizeof(buf)))
|
2007-04-17 23:26:34 +02:00
|
|
|
if (*cp == '\0' || *cp == '\n' || *cp == '\r') {
|
2007-04-15 14:09:57 +02:00
|
|
|
*cp = '\0';
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
++cp;
|
|
|
|
/* if the shebang line is longer than MAXINTERP, bail out */
|
2009-06-08 22:06:50 +02:00
|
|
|
if ((char *)cp >= (buf + sizeof(buf)))
|
2007-04-15 14:09:57 +02:00
|
|
|
goto noshebang;
|
2011-04-02 12:30:11 +02:00
|
|
|
|
|
|
|
/* restore begin of shebang position (buf+0 or buf+3) */
|
|
|
|
cp = (unsigned char *)(buf + fd);
|
2007-04-15 14:09:57 +02:00
|
|
|
/* bail out if read error (above) or no shebang */
|
|
|
|
if ((cp[0] != '#') || (cp[1] != '!'))
|
|
|
|
goto noshebang;
|
2011-04-02 12:30:11 +02:00
|
|
|
|
2007-04-15 14:09:57 +02:00
|
|
|
cp += 2;
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
noshebang:
|
2007-06-15 23:22:40 +02:00
|
|
|
fd = buf[0] << 8 | buf[1];
|
|
|
|
if ((fd == /* OMAGIC */ 0407) ||
|
|
|
|
(fd == /* NMAGIC */ 0410) ||
|
|
|
|
(fd == /* ZMAGIC */ 0413) ||
|
|
|
|
(fd == /* QMAGIC */ 0314) ||
|
|
|
|
(fd == /* ECOFF_I386 */ 0x4C01) ||
|
|
|
|
(fd == /* ECOFF_M68K */ 0x0150 || fd == 0x5001) ||
|
|
|
|
(fd == /* ECOFF_SH */ 0x0500 || fd == 0x0005) ||
|
|
|
|
(fd == 0x7F45 && buf[2] == 'L' && buf[3] == 'F') ||
|
2009-05-16 20:40:09 +02:00
|
|
|
(fd == /* "MZ" */ 0x4D5A) ||
|
2007-06-15 23:22:40 +02:00
|
|
|
(fd == /* gzip */ 0x1F8B))
|
|
|
|
errorf("%s: not executable: magic %04X", tp->str, fd);
|
|
|
|
}
|
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 */
|
2012-12-18 00:18:11 +01:00
|
|
|
errorf("%s: %s: %s", tp->str, sh, cstrerror(errno));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
shcomexec(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct tbl *tp;
|
|
|
|
|
2009-08-28 22:30:59 +02:00
|
|
|
tp = ktsearch(&builtins, *wp, hash(*wp));
|
2012-12-04 02:18:34 +01:00
|
|
|
return (call_builtin(tp, wp, "shcomexec"));
|
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);
|
|
|
|
|
|
|
|
if (t != NULL && !tobool(t->u.ksh_func)) {
|
|
|
|
/* drop same-name aliases for POSIX functions */
|
|
|
|
if ((tp = ktsearch(&aliases, name, nhash)))
|
|
|
|
ktdelete(tp);
|
|
|
|
}
|
|
|
|
|
2011-03-13 02:20:25 +01:00
|
|
|
while (/* CONSTCOND */ 1) {
|
2011-05-07 02:51:12 +02:00
|
|
|
tp = findfunc(name, nhash, true);
|
2012-12-04 02:18:34 +01:00
|
|
|
/* because findfunc:create=true */
|
|
|
|
mkssert(tp != NULL);
|
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) {
|
|
|
|
tp->flag &= ~(ISSET|ALLOC);
|
|
|
|
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;
|
2011-07-05 22:12:20 +02:00
|
|
|
uint32_t flag;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* see if any flags should be set for this builtin */
|
|
|
|
for (flag = 0; ; name++) {
|
• 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 (*name == '=')
|
|
|
|
/* command does variable assignment */
|
2005-05-23 05:06:10 +02:00
|
|
|
flag |= KEEPASN;
|
• 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 if (*name == '*')
|
|
|
|
/* POSIX special builtin */
|
2005-05-23 05:06:10 +02:00
|
|
|
flag |= SPEC_BI;
|
• 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 if (*name == '+')
|
|
|
|
/* POSIX regular builtin */
|
2005-05-23 05:06:10 +02:00
|
|
|
flag |= REG_BI;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-08-28 22:30:59 +02:00
|
|
|
tp = ktenter(&builtins, name, hash(name));
|
2005-05-23 05:06:10 +02:00
|
|
|
tp->flag = DEFINED | flag;
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
if (vstrchr(name, '/')) {
|
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
|
2005-05-23 05:06:10 +02:00
|
|
|
* POSIX regular builtins, then search path...
|
|
|
|
*/
|
|
|
|
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)) {
|
|
|
|
if ((fpath = str_val(global("FPATH"))) == null) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!tp && (flags & FC_REGBI) && tbi && (tbi->flag & REG_BI))
|
|
|
|
tp = tbi;
|
|
|
|
if (!tp && (flags & FC_UNREGBI) && tbi)
|
|
|
|
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) &&
|
|
|
|
(fpath = str_val(global("FPATH"))) != 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; )
|
2005-05-23 05:06:10 +02:00
|
|
|
if ((tp->flag&ISSET) && (all || tp->val.s[0] != '/')) {
|
|
|
|
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 */
|
|
|
|
static int
|
|
|
|
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 */
|
|
|
|
if (access(fn, mode) < 0)
|
|
|
|
/* file exists, but we can't access it */
|
|
|
|
return (errno);
|
|
|
|
if (mode == X_OK && (!S_ISREG(sb.st_mode) ||
|
|
|
|
!(sb.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH))))
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
if (vstrchr(name, '/')) {
|
2011-09-07 17:24:22 +02:00
|
|
|
if ((ec = search_access(name, mode)) == 0) {
|
|
|
|
search_path_ok:
|
|
|
|
if (errnop)
|
|
|
|
*errnop = 0;
|
2006-11-12 15:58:16 +01:00
|
|
|
return (name);
|
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);
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
if (!(p = cstrchr(sp, ':')))
|
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;
|
|
|
|
*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
|
2012-12-04 02:18:34 +01:00
|
|
|
call_builtin(struct tbl *tp, const char **wp, const char *where)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
2012-12-04 02:18:34 +01:00
|
|
|
if (!tp)
|
|
|
|
internal_errorf("%s: %s", where, wp[0]);
|
2005-05-23 05:06:10 +02:00
|
|
|
builtin_argv0 = wp[0];
|
|
|
|
builtin_flag = tp->flag;
|
|
|
|
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_flag = 0;
|
|
|
|
builtin_argv0 = NULL;
|
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;
|
|
|
|
char *cp = iop->name;
|
|
|
|
int iotype = iop->flag & IOTYPE;
|
2012-06-24 22:05:23 +02:00
|
|
|
bool do_open = true, do_close = false;
|
|
|
|
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;
|
|
|
|
iotmp.name = (iotype == IOHERE) ? NULL : cp;
|
|
|
|
iotmp.flag |= IONAMEXP;
|
|
|
|
|
|
|
|
if (Flag(FXTRACE))
|
|
|
|
shellf("%s%s\n",
|
|
|
|
substitute(str_val(global("PS4")), 0),
|
|
|
|
snptreef(NULL, 32, "%R", &iotmp));
|
|
|
|
|
|
|
|
switch (iotype) {
|
|
|
|
case IOREAD:
|
|
|
|
flags = O_RDONLY;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IOCAT:
|
|
|
|
flags = O_WRONLY | O_APPEND | O_CREAT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IOWRITE:
|
|
|
|
flags = O_WRONLY | O_CREAT | O_TRUNC;
|
• 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
|
|
|
/*
|
|
|
|
* The stat() is here to allow redirections to
|
2005-05-23 05:06:10 +02:00
|
|
|
* things like /dev/null without error.
|
|
|
|
*/
|
|
|
|
if (Flag(FNOCLOBBER) && !(iop->flag & IOCLOB) &&
|
|
|
|
(stat(cp, &statb) < 0 || S_ISREG(statb.st_mode)))
|
|
|
|
flags |= O_EXCL;
|
|
|
|
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;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (*cp == '-' && !cp[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
|
|
|
/* 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,
|
|
|
|
X_OK | ((iop->flag & IORDUP) ? R_OK : W_OK),
|
|
|
|
&emsg)) < 0) {
|
|
|
|
warningf(true, "%s: %s",
|
|
|
|
snptreef(NULL, 32, "%R", &iotmp), emsg);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (u == 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)) {
|
2010-08-28 20:50:58 +02:00
|
|
|
warningf(true, "%s: %s", cp, "restricted");
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
u = open(cp, flags, 0666);
|
|
|
|
}
|
|
|
|
if (u < 0) {
|
|
|
|
/* herein() may already have printed message */
|
2007-06-07 01:28:17 +02:00
|
|
|
if (u == -1) {
|
|
|
|
u = errno;
|
2010-08-28 22:22:24 +02:00
|
|
|
warningf(true, "can't %s %s: %s",
|
2005-05-23 05:06:10 +02:00
|
|
|
iotype == IODUP ? "dup" :
|
|
|
|
(iotype == IOREAD || iotype == IOHERE) ?
|
2012-12-18 00:18:11 +01:00
|
|
|
"open" : "create", 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 */
|
|
|
|
if (u == iop->unit)
|
|
|
|
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);
|
|
|
|
else if (u != iop->unit) {
|
|
|
|
if (ksh_dup2(u, iop->unit, true) < 0) {
|
2012-11-30 20:02:10 +01:00
|
|
|
int eno;
|
2007-06-07 01:28:17 +02:00
|
|
|
|
2012-11-30 20:02:10 +01:00
|
|
|
eno = errno;
|
2010-08-28 22:22:24 +02:00
|
|
|
warningf(true, "%s %s %s",
|
|
|
|
"can't finish (dup) redirection",
|
2005-05-23 05:06:10 +02:00
|
|
|
snptreef(NULL, 32, "%R", &iotmp),
|
2012-12-18 00:18:11 +01:00
|
|
|
cstrerror(eno));
|
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) {
|
• 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 (iop->flag & IORDUP)
|
|
|
|
/* 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
|
2011-01-09 22:57:29 +01:00
|
|
|
hereinval(const char *content, int sub, char **resbuf, struct shf *shf)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2012-12-22 01:03:42 +01:00
|
|
|
const char * volatile ccp = content;
|
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);
|
|
|
|
}
|
|
|
|
if (sub) {
|
|
|
|
/* 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)
|
2011-01-09 22:57:29 +01:00
|
|
|
internal_errorf("%s: %s", "herein", "yylex");
|
|
|
|
source = osource;
|
|
|
|
ccp = evalstr(yylval.cp, 0);
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static 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;
|
|
|
|
|
|
|
|
/* ksh -c 'cat << EOF' can cause this... */
|
2013-01-19 20:47:13 +01:00
|
|
|
if (iop->heredoc == NULL) {
|
2010-08-28 22:22:24 +02:00
|
|
|
warningf(true, "%s missing", "here document");
|
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
|
|
|
}
|
|
|
|
|
2013-01-19 20:47:13 +01:00
|
|
|
/* lexer substitution flags */
|
2013-02-17 06:40:17 +01:00
|
|
|
i = (iop->flag & 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)
|
2013-01-19 20:47:13 +01:00
|
|
|
return (hereinval(iop->heredoc, 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);
|
2012-04-14 18:07:48 +02:00
|
|
|
if (!(shf = h->shf) || (fd = open(h->tffn, O_RDONLY, 0)) < 0) {
|
2011-01-09 22:57:29 +01:00
|
|
|
i = errno;
|
2005-05-23 05:06:10 +02:00
|
|
|
warningf(true, "can't %s temporary file %s: %s",
|
2012-12-18 00:18:11 +01:00
|
|
|
!shf ? "create" : "open", 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
|
|
|
}
|
|
|
|
|
2013-01-19 20:47:13 +01:00
|
|
|
if (hereinval(iop->heredoc, 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
|
|
|
}
|
|
|
|
|
|
|
|
if (shf_close(shf) == EOF) {
|
2007-06-07 01:28:17 +02:00
|
|
|
i = errno;
|
2005-05-23 05:06:10 +02:00
|
|
|
close(fd);
|
2012-04-14 18:07:48 +02:00
|
|
|
warningf(true, "can't %s temporary file %s: %s",
|
2012-12-18 00:18:11 +01:00
|
|
|
"write", 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[] = {
|
|
|
|
"read", "-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);
|
2010-09-15 23:08:19 +02:00
|
|
|
shellf("%s", str_val(global("PS3")));
|
2012-12-04 02:18:34 +01:00
|
|
|
if (call_builtin(findcom("read", FC_BI), read_args, Tselect))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
s = str_val(global("REPLY"));
|
|
|
|
if (*s) {
|
2006-11-10 05:03:59 +01:00
|
|
|
getn(s, &i);
|
2009-06-08 22:06:50 +02:00
|
|
|
return ((i >= 1 && i <= argct) ? ap[i - 1] : null);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
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 */
|
|
|
|
static char *
|
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]);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (buf);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
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;
|
2008-04-19 19:21:55 +02:00
|
|
|
print_columns(shl_out, n, select_fmt_entry, (void *)&smi,
|
2009-11-10 00:35:11 +01:00
|
|
|
smi.num_width + 2 + aocts, smi.num_width + 2 + acols,
|
|
|
|
true);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
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);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (buf);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2012-10-03 17:13:34 +02:00
|
|
|
void
|
2009-11-10 00:35:11 +01:00
|
|
|
pr_list(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
|
|
|
|
2008-04-19 19:21:55 +02:00
|
|
|
print_columns(shl_out, n, plain_fmt_entry, (const void *)ap,
|
2009-11-10 00:35:11 +01:00
|
|
|
aocts, acols, false);
|
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;
|
2005-05-23 05:06:10 +02:00
|
|
|
int 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;
|
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)
|
|
|
|
s = evalstr(s, DOTILDE | DOPAT);
|
|
|
|
else
|
|
|
|
s = evalstr(s, DOTILDE);
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (s);
|
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
|
|
|
}
|