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

Replace the hash alias with a proper builtin

This commit replaces the old hash alias with a proper builtin.
I based this builtin off of the code alias uses for handling
`alias -t --`, but with the hack for `--` removed as it has
no use in the new builtin. `alias -t --` will no longer work,
that hack is now gone.

While I was testing this builtin, I found a bug with hash tables
in non-forking subshells. If the hash table of a non-forking
subshell is changed, the parent shell's hash table is also changed.
As an example, running `(hash -r)` was resetting the parent shell's
hash table. The workaround is to force the subshell to fork if the
hash table will be changed.

src/cmd/ksh93/bltins/typeset.c:
 - Move the code for hash out of the alias builtin into a dedicated
   hash builtin. `alias -t --` is no longer supported.

src/cmd/ksh93/data/aliases.c:
 - Remove the old alias for hash from the table of predefined aliases.

src/cmd/ksh93/data/builtins.c:
 - Fix the broken entry for the hash builtin and add a man page for
   the new builtin.

src/cmd/ksh93/sh.1:
 - Replace the entry for the hash alias with a more detailed entry
   for the hash builtin.

src/cmd/ksh93/sh/name.c:
 - Force non-forking subshells to fork if the PATH is being reset
   to workaround a bug with the hash tree.

src/cmd/ksh93/tests/alias.sh:
 - Add a regression test for resetting a hash table, then adding
   a utility to the refreshed hash table.

src/cmd/ksh93/tests/subshell.sh:
 - Add regression tests for changing the hash table in subshells.

(cherry picked from commit d8428a833afe9270b61745ba3d6df355fe1d5499)
This commit is contained in:
Johnothan King 2020-06-10 04:00:35 -07:00 committed by Martijn Dekker
parent d1bd8f89b7
commit 102868f850
10 changed files with 121 additions and 32 deletions

View file

@ -24,6 +24,7 @@
* typeset [options] [arg...]
* alias [-ptx] [arg...]
* unalias [arg...]
* hash [-r] [utility...]
* builtin [-sd] [-f file] [name...]
* set [options] [name...]
* unset [-fnv] [name...]
@ -132,6 +133,49 @@ int b_readonly(int argc,char *argv[],Shbltin_t *context)
return(setall(argv,flag,tdata.sh->var_tree, &tdata));
}
int b_hash(int argc,char *argv[],Shbltin_t *context)
{
register unsigned flag = NV_NOARRAY | NV_NOSCOPE | NV_ASSIGN | NV_TAGGED;
register Dt_t *troot;
register int rflag=0, n;
struct tdata tdata;
NOT_USED(argc);
memset((void *)&tdata, 0, sizeof(tdata));
tdata.sh = context->shp;
troot = tdata.sh->alias_tree;
/* The hash utility should not be recognized as the alias builtin */
tdata.aflag = '-';
while((n = optget(argv,sh_opthash))) switch(n)
{
case 'r':
rflag = 1;
break;
case ':':
error(2,"%s",opt_info.arg);
break;
case '?':
error(ERROR_usage(0),"%s",opt_info.arg);
return 2;
}
if (error_info.errors)
errormsg(SH_DICT,ERROR_usage(2),"%s",optusage(NULL));
argv += (opt_info.index-1);
/* Handle -r */
if (rflag)
{
Namval_t *np = nv_search((char *)PATHNOD, tdata.sh->var_tree, HASH_BUCKET);
nv_putval(np, nv_getval(np), NV_RDONLY);
}
troot = tdata.sh->track_tree;
/* We must fork in subshells to avoid polluting the parent shell's hash table. */
if(tdata.sh->subshell && !tdata.sh->subshare)
sh_subfork();
return setall(argv, flag, troot, &tdata);
}
int b_alias(int argc,register char *argv[],Shbltin_t *context)
{
@ -174,28 +218,7 @@ int b_alias(int argc,register char *argv[],Shbltin_t *context)
errormsg(SH_DICT,ERROR_usage(2),"%s",optusage(NIL(char*)));
argv += (opt_info.index-1);
if(flag&NV_TAGGED)
{
/* hacks to handle hash -r | -- */
if(argv[1] && argv[1][0]=='-')
{
if(argv[1][1]=='r' && argv[1][2]==0)
{
Namval_t *np = nv_search((char*)PATHNOD,tdata.sh->var_tree,HASH_BUCKET);
nv_putval(np,nv_getval(np),NV_RDONLY);
argv++;
if(!argv[1])
return(0);
}
if(argv[1][0]=='-')
{
if(argv[1][1]=='-' && argv[1][2]==0)
argv++;
else
errormsg(SH_DICT, ERROR_exit(1), e_option, argv[1]);
}
}
troot = tdata.sh->track_tree;
}
}
if(context->shp->subshell && !context->shp->subshare)
sh_subfork();