efficient $(<<<foo) and $(<<EOF … ) implementation, requested by izabera

This commit is contained in:
tg 2016-06-26 00:44:59 +00:00
parent 582e745958
commit a41a62dad7
4 changed files with 54 additions and 15 deletions

28
check.t
View File

@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/check.t,v 1.738 2016/06/26 00:09:32 tg Exp $
# $MirOS: src/bin/mksh/check.t,v 1.739 2016/06/26 00:44:55 tg Exp $
# -*- mode: sh -*-
#-
# Copyright © 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
@ -30,7 +30,7 @@
# (2013/12/02 20:39:44) http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/regress/bin/ksh/?sortby=date
expected-stdout:
@(#)MIRBSD KSH R52 2016/05/17
@(#)MIRBSD KSH R52 2016/06/25
description:
Check version of shell.
stdin:
@ -39,7 +39,7 @@ name: KSH_VERSION
category: shell:legacy-no
---
expected-stdout:
@(#)LEGACY KSH R52 2016/05/17
@(#)LEGACY KSH R52 2016/06/25
description:
Check version of legacy shell.
stdin:
@ -2812,6 +2812,28 @@ expected-stdout:
C:echo line 5
x-en
---
name: heredoc-comsub-6
description:
Check here documents and here strings can be used
without a specific command, like $(<) (extension)
stdin:
foo=bar
x=$(<<<EO${foo}F)
echo "3<$x>"
y=$(<<-EOF
hi!
$foo) is not a problem
EOF)
echo "7<$y>"
expected-stdout:
3<EObarF>
7<hi!
bar) is not a problem>
---
name: heredoc-subshell-1
description:
Tests for here documents in subshells, taken from Austin ML

31
eval.c
View File

@ -23,7 +23,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/eval.c,v 1.188 2016/06/25 23:54:59 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/eval.c,v 1.189 2016/06/26 00:44:57 tg Exp $");
/*
* string expansion
@ -1326,14 +1326,31 @@ comsub(Expand *xp, const char *cp, int fn MKSH_A_UNUSED)
struct ioword *io = *t->ioact;
char *name;
if ((io->ioflag & IOTYPE) != IOREAD)
switch (io->ioflag & IOTYPE) {
case IOREAD:
shf = shf_open(name = evalstr(io->ioname, DOTILDE),
O_RDONLY, 0, SHF_MAPHI | SHF_CLEXEC);
if (shf == NULL)
warningf(!Flag(FTALKING), "%s: %s %s: %s",
name, "can't open", "$(<...) input",
cstrerror(errno));
break;
case IOHERE:
if (!herein(io, &name)) {
xp->str = name;
/* as $(…) requires, trim trailing newlines */
name += strlen(name);
while (name > xp->str && name[-1] == '\n')
--name;
*name = '\0';
return (XSUB);
}
shf = NULL;
break;
default:
errorf("%s: %s", T_funny_command,
snptreef(NULL, 32, "%R", io));
shf = shf_open(name = evalstr(io->ioname, DOTILDE), O_RDONLY,
0, SHF_MAPHI | SHF_CLEXEC);
if (shf == NULL)
warningf(!Flag(FTALKING), "%s: %s %s: %s", name,
"can't open", "$(<...) input", cstrerror(errno));
}
} else if (fn == FUNSUB) {
int ofd1;
struct temp *tf = NULL;

5
exec.c
View File

@ -23,7 +23,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.174 2016/06/26 00:09:35 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.175 2016/06/26 00:44:58 tg Exp $");
#ifndef MKSH_DEFAULT_EXECSHELL
#define MKSH_DEFAULT_EXECSHELL MKSH_UNIXROOT "/bin/sh"
@ -34,7 +34,6 @@ static int comexec(struct op *, struct tbl * volatile, const char **,
static void scriptexec(struct op *, const char **) MKSH_A_NORETURN;
static int call_builtin(struct tbl *, const char **, const char *, bool);
static int iosetup(struct ioword *, struct tbl *);
static int herein(struct ioword *, char **);
static const char *do_selectargs(const char **, bool);
static Test_op dbteste_isa(Test_env *, Test_meta);
static const char *dbteste_getopnd(Test_env *, Test_op, bool);
@ -1557,7 +1556,7 @@ hereinval(struct ioword *iop, int sub, char **resbuf, struct shf *shf)
return (0);
}
static int
int
herein(struct ioword *iop, char **resbuf)
{
int fd = -1;

5
sh.h
View File

@ -175,9 +175,9 @@
#endif
#ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.773 2016/06/25 23:49:50 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.774 2016/06/26 00:44:59 tg Exp $");
#endif
#define MKSH_VERSION "R52 2016/05/17"
#define MKSH_VERSION "R52 2016/06/25"
/* arithmetic types: C implementation */
#if !HAVE_CAN_INTTYPES
@ -1752,6 +1752,7 @@ int search_access(const char *, int);
const char *search_path(const char *, const char *, int, int *);
void pr_menu(const char * const *);
void pr_list(char * const *);
int herein(struct ioword *, char **);
/* expr.c */
int evaluate(const char *, mksh_ari_t *, int, bool);
int v_evaluate(struct tbl *, const char *, volatile int, bool);