Skip to content

Commit

Permalink
Load 'r' and 'history' default aliases on interactive only
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
McDutchie committed Jul 16, 2020
1 parent a42ac7e commit 17f81eb
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 29 deletions.
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
21 changes: 0 additions & 21 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 10 additions & 1 deletion src/cmd/ksh93/data/aliases.c
Original file line number Diff line number Diff line change
Expand Up @@ -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[] =
Expand All @@ -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
};

2 changes: 2 additions & 0 deletions src/cmd/ksh93/include/shtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
11 changes: 5 additions & 6 deletions src/cmd/ksh93/sh/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/cmd/ksh93/sh/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/ksh93/tests/alias.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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))

0 comments on commit 17f81eb

Please sign in to comment.