From 17f81ebedb440ed2297097446499a7016d5428e2 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Thu, 16 Jul 2020 05:57:23 +0100 Subject: [PATCH] Load 'r' and 'history' default aliases on interactive only These two default aliases are useful on interactive shells. In scripts, they interfere with possible function or command names. As of this commit, these final two default aliases are only loaded for interactive shells, leaving zero default aliases for scripts. This completes the project to get rid of misguided default aliases. src/cmd/ksh93/include/shtable.h, src/cmd/ksh93/data/aliases.c: src/cmd/ksh93/sh/init.c: - Add empty alias table shtab_noaliases[] for scripts. - Rename inittree() to sh_inittree() and make it external. - nv_init(), sh_reinit(): Initialise empty alias tree for scripts. src/cmd/ksh93/sh/main.c: sh_main(): - If interactive, reinitialise alias tree for interactive shells. src/cmd/ksh93/tests/alias.sh: - To test default alias removal, launch shell with -i. --- NEWS | 3 +++ TODO | 21 --------------------- src/cmd/ksh93/data/aliases.c | 11 ++++++++++- src/cmd/ksh93/include/shtable.h | 2 ++ src/cmd/ksh93/sh/init.c | 11 +++++------ src/cmd/ksh93/sh/main.c | 2 ++ src/cmd/ksh93/tests/alias.sh | 2 +- 7 files changed, 23 insertions(+), 29 deletions(-) diff --git a/NEWS b/NEWS index 423f524479a4..29354db92301 100644 --- a/NEWS +++ b/NEWS @@ -10,6 +10,9 @@ Any uppercase BUG_* names are modernish shell bug IDs. that 'unalias -a' does not remove them. Shell functions can now use these names, which improves compatibility with POSIX shell scripts. +- The two default aliases that are left, 'history' and 'r', are now only + loaded on interactive shells, leaving zero default aliases for scripts. + - The End key escape sequence '^[[F' is now handled in the emacs and vi editing modes. The End key moves the cursor to the end of the line (in contrast to the Home key doing the opposite). diff --git a/TODO b/TODO index 514183e3750c..ca1db81f3cfb 100644 --- a/TODO +++ b/TODO @@ -14,27 +14,6 @@ Fix build system: to take effect. The machine-generated Mamfiles are now used as a fallback, but they are not meant to be edited by hand. -______ -Fix or remove broken or misguided default aliases: - -- Make proper builtins out of the following scripting-related aliases, so - that 'unalias -a' does not eliminate them. If done correctly, this causes - no other change in behaviour. It would be good practice to 'unalias -a' in - a script to start with a clean slate, except ksh has always made that - impossible without losing these. Default aliases should be to facilitate - interactive use. - - autoload='typeset -fu' - - compound='typeset -C' - - float='typeset -lE' - - functions='typeset -f' - - integer='typeset -li' - - nameref='typeset -n' - Keep these default aliases for the benefit of interactive shells: - + history='hist -l' - + r='hist -s' - To avoid interfering with shell functions by those names that POSIX - scripts may set, those should only initialise on interactive shells. - ______ Fix currently known bugs affecting shell scripting. These are identified by their modernish IDs. For exact details, see code/comments in: diff --git a/src/cmd/ksh93/data/aliases.c b/src/cmd/ksh93/data/aliases.c index 734c7574cd05..1b9bd9779154 100644 --- a/src/cmd/ksh93/data/aliases.c +++ b/src/cmd/ksh93/data/aliases.c @@ -24,7 +24,7 @@ #include "FEATURE/dynamic" /* - * This is the table of built-in aliases. These should be exported. + * Table of built-in aliases for interactive shells. */ const struct shtable2 shtab_aliases[] = @@ -34,3 +34,12 @@ const struct shtable2 shtab_aliases[] = "", 0, (char*)0 }; +/* + * Empty table of built-in aliases for non-interactive shells. + */ + +const struct shtable2 shtab_noaliases[] = +{ + "", 0, (char*)0 +}; + diff --git a/src/cmd/ksh93/include/shtable.h b/src/cmd/ksh93/include/shtable.h index e851ae51bac8..4575f2e9c82b 100644 --- a/src/cmd/ksh93/include/shtable.h +++ b/src/cmd/ksh93/include/shtable.h @@ -56,10 +56,12 @@ extern const Shtable_t shtab_options[]; extern const Shtable_t shtab_attributes[]; extern const struct shtable2 shtab_variables[]; extern const struct shtable2 shtab_aliases[]; +extern const struct shtable2 shtab_noaliases[]; extern const struct shtable2 shtab_signals[]; extern const struct shtable3 shtab_builtins[]; extern const Shtable_t shtab_reserved[]; extern const Shtable_t *sh_locate(const char*, const Shtable_t*, int); extern int sh_lookopt(const char*, int*); +extern Dt_t *sh_inittree(Shell_t*, const struct shtable2*); #endif /* SH_TABLE_H */ diff --git a/src/cmd/ksh93/sh/init.c b/src/cmd/ksh93/sh/init.c index 0ae81856e188..1e87fd0fb758 100644 --- a/src/cmd/ksh93/sh/init.c +++ b/src/cmd/ksh93/sh/init.c @@ -217,7 +217,6 @@ static int lctype; static int nbltins; static void env_init(Shell_t*); static Init_t *nv_init(Shell_t*); -static Dt_t *inittree(Shell_t*,const struct shtable2*); static int shlvl; #ifdef _WINIX @@ -1561,7 +1560,7 @@ int sh_reinit(char *argv[]) nv_delete(np,dp,NV_NOFREE); } dtclose(shp->alias_tree); - shp->alias_tree = inittree(shp,shtab_aliases); + shp->alias_tree = sh_inittree(shp,shtab_noaliases); shp->last_root = shp->var_tree; shp->inuse_bits = 0; if(shp->userinit) @@ -1760,7 +1759,7 @@ static Init_t *nv_init(Shell_t *shp) shp->nvfun.last = (char*)shp; shp->nvfun.nofree = 1; ip->sh = shp; - shp->var_base = shp->var_tree = inittree(shp,shtab_variables); + shp->var_base = shp->var_tree = sh_inittree(shp,shtab_variables); SHLVL->nvalue.ip = &shlvl; ip->IFS_init.hdr.disc = &IFS_disc; ip->PATH_init.disc = &RESTRICTED_disc; @@ -1853,9 +1852,9 @@ static Init_t *nv_init(Shell_t *shp) (MCHKNOD)->nvalue.lp = (&sh_mailchk); (OPTINDNOD)->nvalue.lp = (&shp->st.optindex); /* set up the seconds clock */ - shp->alias_tree = inittree(shp,shtab_aliases); + shp->alias_tree = sh_inittree(shp,shtab_noaliases); shp->track_tree = dtopen(&_Nvdisc,Dtset); - shp->bltin_tree = inittree(shp,(const struct shtable2*)shtab_builtins); + shp->bltin_tree = sh_inittree(shp,(const struct shtable2*)shtab_builtins); shp->fun_tree = dtopen(&_Nvdisc,Dtoset); dtview(shp->fun_tree,shp->bltin_tree); nv_mount(DOTSHNOD, "type", shp->typedict=dtopen(&_Nvdisc,Dtoset)); @@ -1878,7 +1877,7 @@ static Init_t *nv_init(Shell_t *shp) * initialize name-value pairs */ -static Dt_t *inittree(Shell_t *shp,const struct shtable2 *name_vals) +Dt_t *sh_inittree(Shell_t *shp,const struct shtable2 *name_vals) { register Namval_t *np; register const struct shtable2 *tp; diff --git a/src/cmd/ksh93/sh/main.c b/src/cmd/ksh93/sh/main.c index 36650f6cb974..037fccda34c6 100644 --- a/src/cmd/ksh93/sh/main.c +++ b/src/cmd/ksh93/sh/main.c @@ -176,6 +176,8 @@ int sh_main(int ac, char *av[], Shinit_f userinit) { sh_onoption(SH_BGNICE); sh_onoption(SH_RC); + free(shp->alias_tree); + shp->alias_tree = sh_inittree(shp,shtab_aliases); } if(!sh_isoption(SH_RC) && (sh_isoption(SH_BASH) && !sh_isoption(SH_POSIX) #if SHOPT_REMOTE diff --git a/src/cmd/ksh93/tests/alias.sh b/src/cmd/ksh93/tests/alias.sh index 084fdb1ab13a..f9d1cf3cf512 100755 --- a/src/cmd/ksh93/tests/alias.sh +++ b/src/cmd/ksh93/tests/alias.sh @@ -110,7 +110,7 @@ unalias foo unalias foo && err_exit 'unalias should return non-zero when a previously set alias is unaliased twice' # Removing a predefined alias should work without an error from free(3) -$SHELL -c 'unalias history' 2> /dev/null || err_exit 'removing a predefined alias does not work' +$SHELL -i -c 'unalias history' 2> /dev/null || err_exit 'removing a predefined alias does not work' # ====== exit $((Errors<125?Errors:125))