1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

Fix unsetting aliases in subshells

Aliases can now be correctly unset within subshell environments
(such as ( ... ), $(command substitutions), etc), as well as
non-subshell "shared" command substitutions (${ ...; }). Before,
attempts to unset aliases within these were silently ignored.

Prior discussion: https://github.com/att/ast/issues/108

Subshell alias trees are only referenced in a few places in the
code, *and* have always been broken, so this commit gets rid of the
whole notion of a subshell alias tree. Instead, there is now just
one flat alias tree, and subshells fork into a separate process
when aliases are set or unset within them. It is not really
conceivable that this could be a performance-sensitive operation,
or even a common one, so this is a clean fix with no downside.

src/cmd/ksh93/include/defs.h:
- Remove sh_subaliastree() definition.

src/cmd/ksh93/sh/subshell.c:
- Remove salias element (pointer to subshell alias tree) from
  subshell struct.
- Remove sh_subaliastree() function.
- sh_subshell(): Remove alias subshell tree cleanup.

src/cmd/ksh93/bltins/typeset.c:
- b_alias(): If in subshell, fork before setting alias.
- b_unalias(): If in subshell, fork before unsetting alias.
- unall(): Remove sh_subaliastree() call.

src/cmd/ksh93/sh/name.c:
- nv_open(): Remove sh_subaliastree() call.

src/cmd/ksh93/tests/subshell.sh:
- Add regression tests for unsetting or redefining aliases within
  subshells.

(cherry picked from commit 12a15605b9521a2564a6e657905705a060e89095)
This commit is contained in:
Martijn Dekker 2020-05-29 08:27:53 +01:00
parent 047cb3303c
commit ec888867fd
6 changed files with 36 additions and 32 deletions

View file

@ -1390,8 +1390,6 @@ Namval_t *nv_open(const char *name, Dt_t *root, int flags)
msg = e_aliname;
while((c= *(unsigned char*)cp++) && (c!='=') && (c!='/') &&
(c>=0x200 || !(c=sh_lexstates[ST_NORM][c]) || c==S_EPAT || c==S_COLON));
if(shp->subshell && c=='=')
root = sh_subaliastree(1);
if(c= *--cp)
*cp = 0;
np = nv_search(name, root, (flags&NV_NOADD)?0:NV_ADD);

View file

@ -71,7 +71,6 @@ static struct subshell
Dt_t *var; /* variable table at time of subshell */
struct Link *svar; /* save shell variable table */
Dt_t *sfun; /* function scope for subshell */
Dt_t *salias;/* alias scope for subshell */
Pathcomp_t *pathlist; /* for PATH variable */
#if (ERROR_VERSION >= 20030214L)
struct Error_context_s *errcontext;
@ -375,24 +374,6 @@ static void nv_restore(struct subshell *sp)
sp->shpwd=save;
}
/*
* return pointer to alias tree
* create new one if in a subshell and one doesn't exist and create is non-zero
*/
Dt_t *sh_subaliastree(int create)
{
register struct subshell *sp = subshell_data;
if(!sp || sp->shp->curenv==0)
return(sh.alias_tree);
if(!sp->salias && create)
{
sp->salias = dtopen(&_Nvdisc,Dtoset);
dtview(sp->salias,sp->shp->alias_tree);
sp->shp->alias_tree = sp->salias;
}
return(sp->salias);
}
/*
* return pointer to function tree
* create new one if in a subshell and one doesn't exist and create is non-zero
@ -710,12 +691,6 @@ Sfio_t *sh_subshell(Shell_t *shp,Shnode_t *t, volatile int flags, int comsub)
{
int n;
shp->options = sp->options;
if(sp->salias)
{
shp->alias_tree = dtview(sp->salias,0);
table_unset(sp->salias,0);
dtclose(sp->salias);
}
if(sp->sfun)
{
shp->fun_tree = dtview(sp->sfun,0);