From 07898d8a4566ffb1f403c81563e36918841c7c1d Mon Sep 17 00:00:00 2001 From: tg Date: Fri, 20 Mar 2015 23:37:55 +0000 Subject: [PATCH] =?UTF-8?q?only=20lksh=20now=20implies=20unalias=20for=20a?= =?UTF-8?q?=20POSIX=20function=20definition=20(used=20e.g.=20in=20Debian?= =?UTF-8?q?=20sysvinit=20scripts=20that=20use=20a=20=E2=80=9Cstop=E2=80=9D?= =?UTF-8?q?=20function)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- check.t | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++------ exec.c | 4 +- lksh.1 | 13 ++++++- sh.h | 4 +- 4 files changed, 117 insertions(+), 16 deletions(-) diff --git a/check.t b/check.t index 485bc68..4dde011 100644 --- a/check.t +++ b/check.t @@ -1,4 +1,4 @@ -# $MirOS: src/bin/mksh/check.t,v 1.686 2015/03/20 23:37:27 tg Exp $ +# $MirOS: src/bin/mksh/check.t,v 1.687 2015/03/20 23:37:52 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/13 + @(#)MIRBSD KSH R50 2015/03/20 description: Check version of shell. stdin: @@ -39,7 +39,7 @@ name: KSH_VERSION category: shell:legacy-no --- expected-stdout: - @(#)LEGACY KSH R50 2015/03/13 + @(#)LEGACY KSH R50 2015/03/20 description: Check version of legacy shell. stdin: @@ -7522,6 +7522,20 @@ expected-stdout: name: aliases-funcdef-1 description: Check if POSIX functions take precedences over aliases +category: shell:legacy-no +stdin: + alias foo='echo makro' + foo() { + echo funktion + } + foo +expected-stdout: + makro +--- +name: aliases-funcdef-1-legacy +description: + Check if POSIX functions take precedences over aliases +category: shell:legacy-yes stdin: alias foo='echo makro' foo() { @@ -7534,6 +7548,20 @@ expected-stdout: name: aliases-funcdef-2 description: Check if POSIX functions take precedences over aliases +category: shell:legacy-no +stdin: + alias foo='echo makro' + foo () { + echo funktion + } + foo +expected-stdout: + makro +--- +name: aliases-funcdef-2-legacy +description: + Check if POSIX functions take precedences over aliases +category: shell:legacy-yes stdin: alias foo='echo makro' foo () { @@ -8699,28 +8727,87 @@ expected-exit: e != 0 expected-stderr-pattern: /\.: missing argument.*\n.*\.: missing argument/ --- -name: alias-function-no-conflict +name: alias-function-no-conflict-legacy description: - make aliases not conflict with functions - note: for ksh-like functions, the order of preference is - different; bash outputs baz instead of bar in line 2 below + make aliases not conflict with functions, legacy version: + undefine these aliases upon definition of the function + note: for ksh functions, the order of preference differs in GNU bash +category: shell:legacy-yes stdin: + # POSIX function overrides and removes alias alias foo='echo bar' + foo foo() { echo baz } + foo + unset -f foo + foo 2>/dev/null || echo rab + # alias overrides ksh function alias korn='echo bar' + korn function korn { echo baz } - foo korn + # alias temporarily overrides POSIX function + bla() { + echo bfn + } + bla + alias bla='echo bal' + bla + unalias bla + bla +expected-stdout: + bar + baz + rab + bar + bar + bfn + bal + bfn +--- +name: alias-function-no-conflict +description: + make aliases not conflict with function definitions +category: shell:legacy-no +stdin: + # POSIX function can be defined, but alias overrides it + alias foo='echo bar' + foo + foo() { + echo baz + } + foo unset -f foo foo 2>/dev/null || echo rab + # alias overrides ksh function + alias korn='echo bar' + korn + function korn { + echo baz + } + korn + # alias temporarily overrides POSIX function + bla() { + echo bfn + } + bla + alias bla='echo bal' + bla + unalias bla + bla expected-stdout: - baz bar - rab + bar + bar + bar + bar + bfn + bal + bfn --- name: bash-function-parens description: @@ -8732,12 +8819,13 @@ stdin: echo "$1 {" echo ' echo "bar='\''$0'\'\" echo '}' - echo ${2:-foo} + print -r -- "${2:-foo}" } mk 'function foo' >f-korn mk 'foo ()' >f-dash mk 'function foo ()' >f-bash - mk 'function stop ()' stop >f-stop + # lksh can do without the backslash, too (cf. aliases-funcdef-2-legacy) + mk 'function stop ()' '\stop' >f-stop print '#!'"$__progname"'\nprint -r -- "${0%/f-argh}"' >f-argh chmod +x f-* u=$(./f-argh) diff --git a/exec.c b/exec.c index ba812ee..f57ae11 100644 --- a/exec.c +++ b/exec.c @@ -23,7 +23,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.146 2015/02/19 22:26:49 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.147 2015/03/20 23:37:54 tg Exp $"); #ifndef MKSH_DEFAULT_EXECSHELL #define MKSH_DEFAULT_EXECSHELL "/bin/sh" @@ -1020,11 +1020,13 @@ define(const char *name, struct op *t) nhash = hash(name); +#ifdef MKSH_LEGACY_MODE if (t != NULL && !tobool(t->u.ksh_func)) { /* drop same-name aliases for POSIX functions */ if ((tp = ktsearch(&aliases, name, nhash))) ktdelete(tp); } +#endif while (/* CONSTCOND */ 1) { tp = findfunc(name, nhash, true); diff --git a/lksh.1 b/lksh.1 index 28c6a18..fc89c42 100644 --- a/lksh.1 +++ b/lksh.1 @@ -1,4 +1,4 @@ -.\" $MirOS: src/bin/mksh/lksh.1,v 1.6 2015/03/20 23:37:29 tg Exp $ +.\" $MirOS: src/bin/mksh/lksh.1,v 1.7 2015/03/20 23:37:54 tg Exp $ .\"- .\" Copyright (c) 2008, 2009, 2010, 2012, 2013, 2015 .\" Thorsten “mirabilos” Glaser @@ -256,6 +256,17 @@ or mode and .Nm lksh do not keep file descriptors \*(Gt 2 private from sub-processes. +.It +.Nm lksh +undefines an alias when a +.Tn POSIX +function with the same name is defined, +to make that function immediately callable. +In +.Nm mksh , +aliases have precedence; the name must be quoted or +.Ic unalias Ns ed +to access it. .El .Sh SEE ALSO .Xr mksh 1 diff --git a/sh.h b/sh.h index 679eec1..b84d318 100644 --- a/sh.h +++ b/sh.h @@ -169,9 +169,9 @@ #endif #ifdef EXTERN -__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.720 2015/03/20 21:46:41 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.721 2015/03/20 23:37:55 tg Exp $"); #endif -#define MKSH_VERSION "R50 2015/03/13" +#define MKSH_VERSION "R50 2015/03/20" /* arithmetic types: C implementation */ #if !HAVE_CAN_INTTYPES