implement “typeset -g”, From: Martijn Dekker <martijn@inlv.org>

also deprecate “global”; thanks!
This commit is contained in:
tg
2017-03-17 22:45:53 +00:00
parent 065f9554fa
commit 5b63e5dcb6
4 changed files with 51 additions and 49 deletions

28
check.t
View File

@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/check.t,v 1.759 2017/03/12 02:35:32 tg Exp $ # $MirOS: src/bin/mksh/check.t,v 1.760 2017/03/17 22:45:48 tg Exp $
# -*- mode: sh -*- # -*- mode: sh -*-
#- #-
# Copyright © 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, # 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 # (2013/12/02 20:39:44) http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/regress/bin/ksh/?sortby=date
expected-stdout: expected-stdout:
@(#)MIRBSD KSH R54 2017/03/11 @(#)MIRBSD KSH R54 2017/03/17
description: description:
Check version of shell. Check version of shell.
stdin: stdin:
@ -39,7 +39,7 @@ name: KSH_VERSION
category: shell:legacy-no category: shell:legacy-no
--- ---
expected-stdout: expected-stdout:
@(#)LEGACY KSH R54 2017/03/11 @(#)LEGACY KSH R54 2017/03/17
description: description:
Check version of legacy shell. Check version of legacy shell.
stdin: stdin:
@ -8004,11 +8004,11 @@ expected-stderr-pattern:
--- ---
name: typeset-1 name: typeset-1
description: description:
Check that global does what typeset is supposed to do Check that typeset -g works correctly
stdin: stdin:
set -A arrfoo 65 set -A arrfoo 65
foo() { foo() {
global -Uui16 arrfoo[*] typeset -g -Uui16 arrfoo[*]
} }
echo before ${arrfoo[0]} . echo before ${arrfoo[0]} .
foo foo
@ -8018,7 +8018,7 @@ stdin:
echo inside before ${arrbar[0]} . echo inside before ${arrbar[0]} .
arrbar[0]=97 arrbar[0]=97
echo inside changed ${arrbar[0]} . echo inside changed ${arrbar[0]} .
global -Uui16 arrbar[*] typeset -g -Uui16 arrbar[*]
echo inside typeset ${arrbar[0]} . echo inside typeset ${arrbar[0]} .
arrbar[0]=48 arrbar[0]=48
echo inside changed ${arrbar[0]} . echo inside changed ${arrbar[0]} .
@ -8792,21 +8792,21 @@ expected-stdout:
name: arrassign-fnc-global name: arrassign-fnc-global
description: description:
Check locality of array access inside a function Check locality of array access inside a function
with the mksh-specific global keyword with the bash4/mksh/yash/zsh typeset -g keyword
stdin: stdin:
function fn { function fn {
global x typeset -g x
x+=(f) x+=(f)
echo ".fn:${x[0]}.${x[1]}.${x[2]}.${x[3]}:" echo ".fn:${x[0]}.${x[1]}.${x[2]}.${x[3]}:"
} }
function rfn { function rfn {
set -A y set -A y
global y typeset -g y
y+=(f) y+=(f)
echo ".rfn:${y[0]}.${y[1]}.${y[2]}.${y[3]}:" echo ".rfn:${y[0]}.${y[1]}.${y[2]}.${y[3]}:"
} }
function fnr { function fnr {
global z typeset -g z
set -A z set -A z
z+=(f) z+=(f)
echo ".fnr:${z[0]}.${z[1]}.${z[2]}.${z[3]}:" echo ".fnr:${z[0]}.${z[1]}.${z[2]}.${z[3]}:"
@ -8944,21 +8944,21 @@ expected-stdout:
name: strassign-fnc-global name: strassign-fnc-global
description: description:
Check locality of string access inside a function Check locality of string access inside a function
with the mksh-specific global keyword with the bash4/mksh/yash/zsh typeset -g keyword
stdin: stdin:
function fn { function fn {
global x typeset -g x
x+=f x+=f
echo ".fn:$x:" echo ".fn:$x:"
} }
function rfn { function rfn {
y= y=
global y typeset -g y
y+=f y+=f
echo ".rfn:$y:" echo ".rfn:$y:"
} }
function fnr { function fnr {
global z typeset -g z
z= z=
z+=f z+=f
echo ".fnr:$z:" echo ".fnr:$z:"

10
funcs.c
View File

@ -38,7 +38,7 @@
#endif #endif
#endif #endif
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.327 2017/03/12 02:31:01 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.328 2017/03/17 22:45:50 tg Exp $");
#if HAVE_KILLPG #if HAVE_KILLPG
/* /*
@ -117,6 +117,7 @@ const struct builtin mkshbuiltins[] = {
{Tfalse, c_false}, {Tfalse, c_false},
{"fc", c_fc}, {"fc", c_fc},
{Tgetopts, c_getopts}, {Tgetopts, c_getopts},
/* deprecated, replaced by typeset -g */
{"^=global", c_typeset}, {"^=global", c_typeset},
{Tjobs, c_jobs}, {Tjobs, c_jobs},
{"kill", c_kill}, {"kill", c_kill},
@ -742,7 +743,7 @@ do_whence(const char **wp, int fcflags, bool vflag, bool iscommand)
return (rv); return (rv);
} }
/* typeset, global, export, and readonly */ /* typeset, global(deprecated), export, and readonly */
static void c_typeset_vardump(struct tbl *, uint32_t, int, bool, bool); static void c_typeset_vardump(struct tbl *, uint32_t, int, bool, bool);
static void c_typeset_vardump_recursive(struct block *, uint32_t, int, bool, static void c_typeset_vardump_recursive(struct block *, uint32_t, int, bool,
bool); bool);
@ -784,7 +785,7 @@ c_typeset(const char **wp)
} }
/* see comment below regarding possible opions */ /* see comment below regarding possible opions */
opts = istset ? "L#R#UZ#afi#lnprtux" : "p"; opts = istset ? "L#R#UZ#afgi#lnprtux" : "p";
builtin_opt.flags |= GF_PLUSOPT; builtin_opt.flags |= GF_PLUSOPT;
/* /*
@ -829,6 +830,9 @@ c_typeset(const char **wp)
case 'f': case 'f':
func = true; func = true;
break; break;
case 'g':
localv = (builtin_opt.info & GI_PLUS) ? true : false;
break;
case 'i': case 'i':
flag = INTEGER; flag = INTEGER;
basestr = builtin_opt.optarg; basestr = builtin_opt.optarg;

58
mksh.1
View File

@ -1,4 +1,4 @@
.\" $MirOS: src/bin/mksh/mksh.1,v 1.428 2017/03/14 11:32:56 tg Exp $ .\" $MirOS: src/bin/mksh/mksh.1,v 1.429 2017/03/17 22:45:51 tg Exp $
.\" $OpenBSD: ksh.1,v 1.160 2015/07/04 13:27:04 feinerer Exp $ .\" $OpenBSD: ksh.1,v 1.160 2015/07/04 13:27:04 feinerer Exp $
.\"- .\"-
.\" Copyright © 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, .\" Copyright © 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
@ -76,7 +76,7 @@
.\" with -mandoc, it might implement .Mx itself, but we want to .\" with -mandoc, it might implement .Mx itself, but we want to
.\" use our own definition. And .Dd must come *first*, always. .\" use our own definition. And .Dd must come *first*, always.
.\" .\"
.Dd $Mdocdate: March 14 2017 $ .Dd $Mdocdate: March 17 2017 $
.\" .\"
.\" Check which macro package we use, and do other -mdoc setup. .\" Check which macro package we use, and do other -mdoc setup.
.\" .\"
@ -3649,9 +3649,22 @@ resetting
.Ev OPTIND .Ev OPTIND
may lead to unexpected results. may lead to unexpected results.
.Pp .Pp
.It global Ar ... .It Xo
.Ic global
.Op Ic +\-aglpnrtUux
.Oo Fl L Ns Op Ar n
.No \*(Ba Fl R Ns Op Ar n
.No \*(Ba Fl Z Ns Op Ar n Oc
.Op Fl i Ns Op Ar n
.Oo Ar name
.Op Ns = Ns Ar value
.Ar ... Oc
.Xc
See See
.Ic typeset . .Ic typeset Fl g .
.No Deprecated , Em will
be removed from a future version of
.Nm .
.Pp .Pp
.It Xo .It Xo
.Ic hash .Ic hash
@ -4802,26 +4815,20 @@ traps in functions are not yet implemented.
A command that exits with a zero value. A command that exits with a zero value.
.Pp .Pp
.It Xo .It Xo
.Ic global .Ic typeset
.Oo Op Ic +\-alpnrtUux .Op Ic +\-aglpnrtUux
.Op Fl L Ns Op Ar n .Oo Fl L Ns Op Ar n
.Op Fl R Ns Op Ar n .No \*(Ba Fl R Ns Op Ar n
.Op Fl Z Ns Op Ar n .No \*(Ba Fl Z Ns Op Ar n Oc
.Op Fl i Ns Op Ar n .Op Fl i Ns Op Ar n
.No \*(Ba Fl f Op Fl tux Oc
.Oo Ar name .Oo Ar name
.Op Ns = Ns Ar value .Op Ns = Ns Ar value
.Ar ... Oc .Ar ... Oc
.Xc .Xc
.It Xo .It Xo
.Ic typeset .Ic typeset
.Oo Op Ic +\-alpnrtUux .Fl f Op Fl tux
.Op Fl LRZ Ns Op Ar n .Op Ar name ...
.Op Fl i Ns Op Ar n
.No \*(Ba Fl f Op Fl tux Oc
.Oo Ar name
.Op Ns = Ns Ar value
.Ar ... Oc
.Xc .Xc
Display or set parameter attributes. Display or set parameter attributes.
This is a declaration utility. This is a declaration utility.
@ -4847,20 +4854,9 @@ Values for parameters may optionally be specified.
For For
.Ar name Ns \&[*] , .Ar name Ns \&[*] ,
the change affects the entire array, and no value may be specified. the change affects the entire array, and no value may be specified.
.Pp Any parameters created by
If
.Ic typeset .Ic typeset
is used inside a function, any parameters specified are localised. have local scope by default.
This is not done by the otherwise identical
.Ic global .
.Em Note :
This means that
.Nm Ns 's Ic global
command is
.Em not
equivalent to other programming languages' as it does not allow a
function called from another function to access a parameter at truly
global scope, but only prevents putting an accessed one into local scope.
.Pp .Pp
When When
.Fl f .Fl f
@ -4880,6 +4876,8 @@ Indexed array attribute.
.It Fl f .It Fl f
Function mode. Function mode.
Display or set functions and their attributes, instead of parameters. Display or set functions and their attributes, instead of parameters.
.It Fl g
Create parameters in global scope, not in local scope.
.It Fl i Ns Op Ar n .It Fl i Ns Op Ar n
Integer attribute. Integer attribute.
.Ar n .Ar n

4
sh.h
View File

@ -175,9 +175,9 @@
#endif #endif
#ifdef EXTERN #ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.794 2017/03/12 02:04:14 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/sh.h,v 1.795 2017/03/17 22:45:53 tg Exp $");
#endif #endif
#define MKSH_VERSION "R54 2017/03/11" #define MKSH_VERSION "R54 2017/03/17"
/* arithmetic types: C implementation */ /* arithmetic types: C implementation */
#if !HAVE_CAN_INTTYPES #if !HAVE_CAN_INTTYPES