Implement the “FKSH functions have local scope for shell options” feature

for mksh but not lksh; bump to R51-alpha.

While here, tweak build scripts a bit, fixup MirBSD-specific Makefile
things, remove part of a comment that’s uninteresting.
This commit is contained in:
tg 2015-04-11 23:28:21 +00:00
parent 3251f18c12
commit cad9bc86d3
7 changed files with 118 additions and 42 deletions

View File

@ -1,9 +1,9 @@
#!/bin/sh
srcversion='$MirOS: src/bin/mksh/Build.sh,v 1.672 2014/12/08 12:20:40 tg Exp $'
srcversion='$MirOS: src/bin/mksh/Build.sh,v 1.673 2015/04/11 23:28:16 tg Exp $'
#-
# Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
# 2011, 2012, 2013, 2014
# Thorsten Glaser <tg@mirbsd.org>
# 2011, 2012, 2013, 2014, 2015
# Thorsten “mirabilos” Glaser <tg@mirbsd.org>
#
# Provided that these terms and disclaimer and all copyright notices
# are retained or reproduced in an accompanying document, permission
@ -1692,6 +1692,7 @@ if test 0 = $HAVE_CAN_LFS_SUS; then
ac_testn can_lfs_aix '!' can_lfs 0 "... with -D_LARGE_FILES=1" <lft.c
test 1 = $HAVE_CAN_LFS_AIX || CPPFLAGS=$save_CPPFLAGS
fi
rm -f lft.c
rmf lft* # end of large file support test
#
@ -1780,12 +1781,12 @@ else
HAVE_LINK_WORKS=x
ac_testinit link_works '' 'checking if the final link command may succeed'
fv=1
cat >conftest.c <<-'EOF'
cat >conftest.c <<-EOF
#define EXTERN
#define MKSH_INCLUDES_ONLY
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/Build.sh,v 1.672 2014/12/08 12:20:40 tg Exp $");
int main(void) { printf("Hello, World!\n"); return (isatty(0)); }
__RCSID("$srcversion");
int main(void) { printf("Hello, World!\\n"); return (isatty(0)); }
EOF
case $cm in
llvm)
@ -2325,7 +2326,7 @@ addsrcs '!' HAVE_STRLCPY strlcpy.c
addsrcs USE_PRINTF_BUILTIN printf.c
test 1 = "$USE_PRINTF_BUILTIN" && add_cppflags -DMKSH_PRINTF_BUILTIN
test 1 = "$HAVE_CAN_VERB" && CFLAGS="$CFLAGS -verbose"
add_cppflags -DMKSH_BUILD_R=509
add_cppflags -DMKSH_BUILD_R=510
$e $bi$me: Finished configuration testing, now producing output.$ao

View File

@ -1,8 +1,8 @@
# $MirOS: src/bin/mksh/Makefile,v 1.140 2015/03/20 23:37:17 tg Exp $
# $MirOS: src/bin/mksh/Makefile,v 1.141 2015/04/11 23:28:17 tg Exp $
#-
# Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
# 2011, 2012, 2013, 2014, 2015
# Thorsten Glaser <tg@mirbsd.org>
# Thorsten “mirabilos” Glaser <tg@mirbsd.org>
#
# Provided that these terms and disclaimer and all copyright notices
# are retained or reproduced in an accompanying document, permission
@ -28,6 +28,8 @@ NOMAN= Yes
.include <bsd.own.mk>
SRCDIR= ${.CURDIR}
PROG= mksh
SRCS= edit.c eval.c exec.c expr.c funcs.c histrap.c jobs.c \
lalloc.c lex.c main.c misc.c shf.c syn.c tree.c var.c
@ -55,7 +57,7 @@ CPPFLAGS+= -DMKSH_ASSUME_UTF8 -DMKSH_DISABLE_DEPRECATED \
-DHAVE_SETGROUPS=1 -DHAVE_STRERROR=0 -DHAVE_STRSIGNAL=0 \
-DHAVE_STRLCPY=1 -DHAVE_FLOCK_DECL=1 -DHAVE_REVOKE_DECL=1 \
-DHAVE_SYS_ERRLIST_DECL=1 -DHAVE_SYS_SIGLIST_DECL=1 \
-DHAVE_PERSISTENT_HISTORY=1 -DMKSH_BUILD_R=509
-DHAVE_PERSISTENT_HISTORY=1 -DMKSH_BUILD_R=510
CPPFLAGS+= -D${${PROG:L}_tf:C/(Mir${MAN:E}{0,1}){2}/4/:S/x/mksh_BUILD/:U}
CPPFLAGS+= -I.
COPTS+= -std=c89 -Wall
@ -82,11 +84,11 @@ LINKS+= ${BINDIR}/${PROG} ${BINDIR}/${_i}
MLINKS+= ${PROG}.1 ${_i}.1
.endfor
OPTGENS!= cd ${.CURDIR:Q} && echo *.opt
OPTGENS!= cd ${SRCDIR:Q} && echo *.opt
.for _i in ${OPTGENS}
GENERATED+= ${_i:R}.gen
${_i:R}.gen: ${_i} ${.CURDIR}/Build.sh
/bin/sh ${.CURDIR:Q}/Build.sh -G ${.CURDIR:Q}/${_i:Q}
${_i:R}.gen: ${_i} ${SRCDIR}/Build.sh
/bin/sh ${SRCDIR:Q}/Build.sh -G ${SRCDIR:Q}/${_i:Q}
.endfor
CLEANFILES+= ${GENERATED}
@ -96,8 +98,8 @@ regress: ${PROG} check.pl check.t
-rm -rf regress-dir
mkdir -p regress-dir
echo export FNORD=666 >regress-dir/.mkshrc
HOME=$$(realpath regress-dir) perl ${.CURDIR}/check.pl \
-s ${.CURDIR}/check.t -v -p ./${PROG} \
HOME=$$(realpath regress-dir) perl ${SRCDIR}/check.pl \
-s ${SRCDIR}/check.t -v -p ./${PROG} \
-C shell:legacy-no,int:32,fastbox
test-build: .PHONY
@ -109,14 +111,14 @@ test-build: .PHONY
cd build-dir; env CC=${CC:Q} CFLAGS=${CFLAGS:M*:Q} \
CPPFLAGS=${CPPFLAGS:M*:Q} LDFLAGS=${LDFLAGS:M*:Q} \
LIBS= NOWARN=-Wno-error TARGET_OS= CPP= /bin/sh \
${.CURDIR}/Build.sh -Q -r ${_TBF} && ./test.sh -v -f
${SRCDIR}/Build.sh -Q -r ${_TBF} && ./test.sh -v -f
CLEANFILES+= lksh.cat1
test-build-lksh: .PHONY
cd ${.CURDIR} && exec ${MAKE} lksh.cat1 test-build _TBF=-L
cd ${SRCDIR} && exec ${MAKE} lksh.cat1 test-build _TBF=-L
bothmans: .PHONY
cd ${.CURDIR} && exec ${MAKE} MAN='lksh.1 mksh.1' __MANALL
cd ${SRCDIR} && exec ${MAKE} MAN='lksh.1 mksh.1' __MANALL
cleandir: clean-extra
@ -126,7 +128,7 @@ clean-extra: .PHONY
mksh_tf=xMakefile${OStype:S/${MACHINE_OS}/1/1g}${OSNAME}
distribution:
sed 's!\$$I''d\([:$$]\)!$$M''irSecuCron\1!g' \
${.CURDIR}/dot.mkshrc >${DESTDIR}/etc/skel/.mkshrc
${SRCDIR}/dot.mkshrc >${DESTDIR}/etc/skel/.mkshrc
chown ${BINOWN}:${CONFGRP} ${DESTDIR}/etc/skel/.mkshrc
chmod 0644 ${DESTDIR}/etc/skel/.mkshrc
@ -150,7 +152,7 @@ cats: ${MANALL} ${MANALL:S/.cat/.ps/}
. error Adjust here.
.endif
.for _m _n in mksh 1
x=$$(ident ${.CURDIR:Q}/${_m}.${_n} | \
x=$$(ident ${SRCDIR:Q}/${_m}.${_n} | \
awk '/MirOS:/ { print $$4$$5; }' | \
tr -dc 0-9); (( $${#x} == 14 )) || exit 1; exec \
${MKSH} ${BSDSRCDIR:Q}/contrib/hosted/tg/ps2pdfmir -c \
@ -182,5 +184,5 @@ d: all
.endif
dr:
p=$$(realpath ${PROG:Q}) && cd ${.CURDIR:Q} && exec ${MKSH} \
p=$$(realpath ${PROG:Q}) && cd ${SRCDIR:Q} && exec ${MKSH} \
${BSDSRCDIR:Q}/contrib/hosted/tg/sdmksh "$$p"

65
check.t
View File

@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/check.t,v 1.688 2015/04/11 21:18:45 tg Exp $
# $MirOS: src/bin/mksh/check.t,v 1.689 2015/04/11 23:28:17 tg Exp $
# -*- mode: sh -*-
#-
# Copyright © 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
@ -30,7 +30,7 @@
# (2013/12/02 20:39:44) http://openbsd.cs.toronto.edu/cgi-bin/cvsweb/src/regress/bin/ksh/?sortby=date
expected-stdout:
@(#)MIRBSD KSH R50 2015/03/20
@(#)MIRBSD KSH R51 2015/04/11
description:
Check version of shell.
stdin:
@ -39,7 +39,7 @@ name: KSH_VERSION
category: shell:legacy-no
---
expected-stdout:
@(#)LEGACY KSH R50 2015/03/20
@(#)LEGACY KSH R51 2015/04/11
description:
Check version of legacy shell.
stdin:
@ -12102,3 +12102,62 @@ expected-stderr:
[(p:sh)(f1:sh)(f2:sh)] print '(o1:shx)'
[(p:sh)(f1:sh)(f2:sh)] set +x
---
name: fksh-flags
description:
Check that FKSH functions have their own shell flags
category: shell:legacy-no
stdin:
[[ $KSH_VERSION = Version* ]] && set +B
function foo {
set +f
set -e
echo 2 "${-/s}" .
}
set -fh
echo 1 "${-/s}" .
foo
echo 3 "${-/s}" .
expected-stdout:
1 fh .
2 eh .
3 fh .
---
name: fksh-flags-legacy
description:
Check that even FKSH functions share the shell flags
category: shell:legacy-yes
stdin:
[[ $KSH_VERSION = Version* ]] && set +B
foo() {
set +f
set -e
echo 2 "${-/s}" .
}
set -fh
echo 1 "${-/s}" .
foo
echo 3 "${-/s}" .
expected-stdout:
1 fh .
2 eh .
3 eh .
---
name: fsh-flags
description:
Check that !FKSH functions share the shell flags
stdin:
[[ $KSH_VERSION = Version* ]] && set +B
foo() {
set +f
set -e
echo 2 "${-/s}" .
}
set -fh
echo 1 "${-/s}" .
foo
echo 3 "${-/s}" .
expected-stdout:
1 fh .
2 eh .
3 eh .
---

22
exec.c
View File

@ -23,7 +23,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.148 2015/04/11 22:03:29 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.149 2015/04/11 23:28:19 tg Exp $");
#ifndef MKSH_DEFAULT_EXECSHELL
#define MKSH_DEFAULT_EXECSHELL "/bin/sh"
@ -714,9 +714,9 @@ comexec(struct op *t, struct tbl * volatile tp, const char **ap,
/* function call */
case CFUNC: {
volatile unsigned char old_xflag;
volatile uint32_t old_inuse;
const char * volatile old_kshname;
volatile uint8_t old_flags[FNFLAGS];
if (!(tp->flag & ISSET)) {
struct tbl *ftp;
@ -768,8 +768,9 @@ comexec(struct op *t, struct tbl * volatile tp, const char **ap,
getopts_reset(1);
}
old_xflag = Flag(FXTRACE) ? 1 : 0;
change_xtrace((Flag(FXTRACEREC) ? old_xflag : 0) |
for (type_flags = 0; type_flags < FNFLAGS; ++type_flags)
old_flags[type_flags] = shell_flags[type_flags];
change_xtrace((Flag(FXTRACEREC) ? Flag(FXTRACE) : 0) |
((tp->flag & TRACE) ? 1 : 0), false);
old_inuse = tp->flag & FINUSE;
tp->flag |= FINUSE;
@ -781,13 +782,20 @@ comexec(struct op *t, struct tbl * volatile tp, const char **ap,
}
kshname = old_kshname;
change_xtrace(old_xflag, false);
change_xtrace(old_flags[(int)FXTRACE], false);
#ifndef MKSH_LEGACY_MODE
if (tp->flag & FKSH) {
/* Korn style functions restore Flags on return */
old_flags[(int)FXTRACE] = Flag(FXTRACE);
for (type_flags = 0; type_flags < FNFLAGS; ++type_flags)
shell_flags[type_flags] = old_flags[type_flags];
}
#endif
tp->flag = (tp->flag & ~FINUSE) | old_inuse;
/*
* 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.
* execution tree.
*/
if ((tp->flag & (FDELETE|FINUSE)) == FDELETE) {
if (tp->flag & ALLOC) {

10
lksh.1
View File

@ -1,4 +1,4 @@
.\" $MirOS: src/bin/mksh/lksh.1,v 1.8 2015/03/21 00:12:46 tg Exp $
.\" $MirOS: src/bin/mksh/lksh.1,v 1.9 2015/04/11 23:28:19 tg Exp $
.\"-
.\" Copyright (c) 2008, 2009, 2010, 2012, 2013, 2015
.\" Thorsten “mirabilos” Glaser <tg@mirbsd.org>
@ -72,7 +72,7 @@
.\" with -mandoc, it might implement .Mx itself, but we want to
.\" use our own definition. And .Dd must come *first*, always.
.\"
.Dd $Mdocdate: March 21 2015 $
.Dd $Mdocdate: April 11 2015 $
.\"
.\" Check which macro package we use, and do other -mdoc setup.
.\"
@ -272,6 +272,12 @@ In
aliases have precedence; the name must be quoted or
.Ic unalias Ns ed
to access it.
.It
Functions defined with the
.Ic function
reserved word share the shell flags
.Pq Ic set -o
instead of locally scoping them.
.El
.Sh SEE ALSO
.Xr mksh 1

12
mksh.1
View File

@ -1,4 +1,4 @@
.\" $MirOS: src/bin/mksh/mksh.1,v 1.359 2015/04/11 22:35:10 tg Exp $
.\" $MirOS: src/bin/mksh/mksh.1,v 1.360 2015/04/11 23:28:19 tg Exp $
.\" $OpenBSD: ksh.1,v 1.159 2015/03/25 12:10:52 jca Exp $
.\"-
.\" Copyright © 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
@ -1204,7 +1204,7 @@ and
.Ic return
work, and in that
.Ic exit
terminates the parent shell.
terminates the parent shell; flags are shared.
.Pp
Another variant of substitution are the valsubs (value substitutions)
.Pf ${\*(Ba\& Ns Ar command Ns \&;}
@ -2903,6 +2903,10 @@ untouched, so using
inside a function interferes with using
.Ic getopts
outside the function).
.It
Shell options
.Pq Ic set Fl o
have local scope, i.e. changes inside a function are reset upon its exit.
.El
.Pp
In the future, the following differences may also be added:
@ -2916,10 +2920,6 @@ trapped) will have their default effect in a function.
.It
The EXIT trap, if set in a function, will be executed after the function
returns.
.It
Shell options
.Pq Ic set Fl o
have local scope, i.e. changes inside a function are reset upon its exit.
.El
.Ss Command execution
After evaluation of command-line arguments, redirections, and parameter

6
sh.h
View File

@ -169,9 +169,9 @@
#endif
#ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.722 2015/04/11 22:03:31 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.723 2015/04/11 23:28:21 tg Exp $");
#endif
#define MKSH_VERSION "R50 2015/03/20"
#define MKSH_VERSION "R51 2015/04/11"
/* arithmetic types: C implementation */
#if !HAVE_CAN_INTTYPES
@ -537,7 +537,7 @@ char *ucstrstr(char *, const char *);
#define mkssert(e) do { } while (/* CONSTCOND */ 0)
#endif
#if (!defined(MKSH_BUILDMAKEFILE4BSD) && !defined(MKSH_BUILDSH)) || (MKSH_BUILD_R != 509)
#if (!defined(MKSH_BUILDMAKEFILE4BSD) && !defined(MKSH_BUILDSH)) || (MKSH_BUILD_R != 510)
#error Must run Build.sh to compile this.
extern void thiswillneverbedefinedIhope(void);
int