ash,hush: use HOME for tab completion and prompts

ash and hush correctly use the value of HOME for tilde expansion.
However the line editing code in libbb obtains the user's home
directory by calling getpwuid().  Thus tildes in tab completion
and prompts may be interpreted differently than in tilde expansion.

When the line editing code is invoked from a shell make it use the
shell's interpretation of tilde.  This is similar to how GNU readline
and bash collaborate.

function                                             old     new   delta
get_homedir_or_NULL                                   29      72     +43
optschanged                                          119     126      +7
hush_main                                           1204    1211      +7
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 57/0)               Total: 57 bytes

v2: Always check for HOME before trying the password database:  this
    is what GNU readline does.

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Ron Yorston
2022-03-24 12:17:25 +00:00
committed by Denys Vlasenko
parent 95fec31be6
commit 7d1c7d8337
4 changed files with 19 additions and 16 deletions

View File

@@ -259,6 +259,16 @@ static const char *get_username_str(void)
static NOINLINE const char *get_homedir_or_NULL(void)
{
const char *home;
# if ENABLE_SHELL_ASH || ENABLE_SHELL_HUSH
home = state->sh_get_var ? state->sh_get_var("HOME") : getenv("HOME");
# else
home = getenv("HOME");
# endif
if (home != NULL && home[0] != '\0')
return home;
if (!got_user_strings)
get_user_strings();
return home_pwd_buf;
@@ -861,7 +871,7 @@ static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type)
continue;
}
# endif
# if EDITING_HAS_get_exe_name
# if ENABLE_SHELL_ASH || ENABLE_SHELL_HUSH
if (state->get_exe_name) {
i = 0;
for (;;) {