Skip to content

Commit

Permalink
Fix ${!foo@} and ${!foo*} to include 'foo' itself in search
Browse files Browse the repository at this point in the history
These expansions are supposed to yield all variable names beginning
with the indicated prefix. This should include the variable name
that is identical to the prefix (as 'prefix' begins with 'prefix').

This bugfix is backported from the abandoned ksh 93v- beta, so AT&T
intended this change. It also makes ksh work like bash in this.

src/cmd/ksh93/sh/macro.c: varsub(): M_NAMESCAN:
- Check if the prefix itself exists. If so, start with that.

src/cmd/ksh93/tests/variables.sh:
- Add tests for these expansions.

src/cmd/ksh93/sh.1:
- Fix the incomplete documentation of these expansions.

src/cmd/ksh93/COMPATIBILITY:
- Note the change as it's potentially incompatible in corner cases.

Resolves: #183
  • Loading branch information
McDutchie committed Mar 9, 2021
1 parent e586377 commit 4a8072e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 3 deletions.
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ For full details, see the git log at: https://github.com/ksh93/ksh

Any uppercase BUG_* names are modernish shell bug IDs.

2021-03-09:

- The ${!foo@} and ${!foo*} expansions yield variable names beginning with foo,
but excluded 'foo' itself. The fix for this is now backported from 93v- beta.

2021-03-07:

- Fixed the typeset -p display of short integers without an assigned value.
Expand Down
3 changes: 3 additions & 0 deletions src/cmd/ksh93/COMPATIBILITY
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ For more details, see the NEWS file and for complete details, see the git log.
match a normal (non-**) glob pattern. For example, if '/lnk' is a
symlink to a directory, '/lnk/**' and '/l?k/**' now work as expected.

18. The variable name search expansions ${!prefix@} and ${!prefix*} now
also include the variable 'prefix' itself in the possible results.

____________________________________________________________________________

KSH-93 VS. KSH-88
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/ksh93/include/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */
#define SH_RELEASE_SVER "1.0.0-alpha" /* semantic version number: https://semver.org */
#define SH_RELEASE_DATE "2021-03-07" /* must be in this format for $((.sh.version)) */
#define SH_RELEASE_DATE "2021-03-09" /* must be in this format for $((.sh.version)) */
#define SH_RELEASE_CPYR "(c) 2020-2021 Contributors to ksh " SH_RELEASE_FORK

/* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */
Expand Down
7 changes: 6 additions & 1 deletion src/cmd/ksh93/sh.1
Original file line number Diff line number Diff line change
Expand Up @@ -1274,9 +1274,14 @@ and
inclusive using the same quoting rules as
.BR @ .
.TP
.PD 0
\f3${!\fP\f2prefix\^\fP\f3@}\fP
.TP
\f3${!\fP\f2prefix\^\fP\f3*}\fP
Expands to the names of the variables whose names begin with
These both expand to the names of the variables whose names begin with
.IR prefix .
The expansions otherwise work like \f3$@\fP and \f3$*\fP, respectively
(see under \f2Quoting\fP below).
.TP
\f3${\fP\f2parameter\^\fP\f3:\-\fP\f2word\^\fP\f3}\fP
If
Expand Down
7 changes: 6 additions & 1 deletion src/cmd/ksh93/sh/macro.c
Original file line number Diff line number Diff line change
Expand Up @@ -1517,10 +1517,15 @@ static int varsub(Mac_t *mp)
}
else
{
/* M_NAMESCAN: ${!prefix@} or ${!prefix*}. These work like $@, $*. */
dolmax = strlen(id);
dolg = -1;
nextname(mp,id,0);
v = nextname(mp,id,dolmax);
/* Check if the prefix (id) itself exists. If so, start with that. */
if(nv_open(id,mp->shp->var_tree,NV_NOREF|NV_NOADD|NV_VARNAME|NV_NOFAIL))
v = id;
else
v = nextname(mp,id,dolmax);
}
}
else if(type==M_SUBNAME)
Expand Down
11 changes: 11 additions & 0 deletions src/cmd/ksh93/tests/variables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1247,5 +1247,16 @@ exp=42.0000000000
[[ $got == "$exp" ]] || err_exit "\${a:=b}: expansion not working for float type (expected '$exp', got '$got')"
[[ $a == "$exp" ]] || err_exit "\${a:=b}: a was not assigned the correct float value (expected '$exp', got '$a')"

# ======
# ${!FOO@} and ${!FOO*} expansions did not include FOO itself
# https://github.com/ksh93/ksh/issues/183
unset foo "${!foo@}"
exp='foo foobar fool'
got=$(IFS=/; foo=bar foobar=fbar fool=pity; print -r -- "${!foo@}")
[[ $got == "$exp" ]] || err_exit "\${!foo@}: expected $(printf %q "$exp"), got $(printf %q "$got")"
exp='foo/foobar/fool'
got=$(IFS=/; foo=bar foobar=fbar fool=pity; print -r -- "${!foo*}")
[[ $got == "$exp" ]] || err_exit "\${!foo*}: expected $(printf %q "$exp"), got $(printf %q "$got")"

# ======
exit $((Errors<125?Errors:125))

0 comments on commit 4a8072e

Please sign in to comment.