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

@ -1924,6 +1924,7 @@ unsigned size_from_HISTFILESIZE(const char *hp) FAST_FUNC;
# define MAX_HISTORY 0 # define MAX_HISTORY 0
# endif # endif
typedef const char *get_exe_name_t(int i) FAST_FUNC; typedef const char *get_exe_name_t(int i) FAST_FUNC;
typedef const char *sh_get_var_t(const char *name) FAST_FUNC;
typedef struct line_input_t { typedef struct line_input_t {
int flags; int flags;
int timeout; int timeout;
@ -1937,9 +1938,8 @@ typedef struct line_input_t {
# if ENABLE_SHELL_ASH || ENABLE_SHELL_HUSH # if ENABLE_SHELL_ASH || ENABLE_SHELL_HUSH
/* function to fetch additional application-specific names to match */ /* function to fetch additional application-specific names to match */
get_exe_name_t *get_exe_name; get_exe_name_t *get_exe_name;
# define EDITING_HAS_get_exe_name 1 /* function to fetch value of shell variable */
# else sh_get_var_t *sh_get_var;
# define EDITING_HAS_get_exe_name 0
# endif # endif
# endif # endif
# if MAX_HISTORY # if MAX_HISTORY
@ -1993,11 +1993,6 @@ int read_line_input(const char* prompt, char* command, int maxsize) FAST_FUNC;
read_line_input(prompt, command, maxsize) read_line_input(prompt, command, maxsize)
#endif #endif
#ifndef EDITING_HAS_get_exe_name
# define EDITING_HAS_get_exe_name 0
#endif
#ifndef COMM_LEN #ifndef COMM_LEN
# ifdef TASK_COMM_LEN # ifdef TASK_COMM_LEN
enum { COMM_LEN = TASK_COMM_LEN }; enum { COMM_LEN = TASK_COMM_LEN };

View File

@ -259,6 +259,16 @@ static const char *get_username_str(void)
static NOINLINE const char *get_homedir_or_NULL(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) if (!got_user_strings)
get_user_strings(); get_user_strings();
return home_pwd_buf; return home_pwd_buf;
@ -861,7 +871,7 @@ static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type)
continue; continue;
} }
# endif # endif
# if EDITING_HAS_get_exe_name # if ENABLE_SHELL_ASH || ENABLE_SHELL_HUSH
if (state->get_exe_name) { if (state->get_exe_name) {
i = 0; i = 0;
for (;;) { for (;;) {

View File

@ -9720,7 +9720,7 @@ evalpipe(union node *n, int flags)
} }
/* setinteractive needs this forward reference */ /* setinteractive needs this forward reference */
#if EDITING_HAS_get_exe_name #if ENABLE_FEATURE_EDITING
static const char *get_builtin_name(int i) FAST_FUNC; static const char *get_builtin_name(int i) FAST_FUNC;
#endif #endif
@ -9757,9 +9757,8 @@ setinteractive(int on)
#if ENABLE_FEATURE_EDITING #if ENABLE_FEATURE_EDITING
if (!line_input_state) { if (!line_input_state) {
line_input_state = new_line_input_t(FOR_SHELL | WITH_PATH_LOOKUP); line_input_state = new_line_input_t(FOR_SHELL | WITH_PATH_LOOKUP);
# if EDITING_HAS_get_exe_name
line_input_state->get_exe_name = get_builtin_name; line_input_state->get_exe_name = get_builtin_name;
# endif line_input_state->sh_get_var = lookupvar;
} }
#endif #endif
} }
@ -10262,7 +10261,7 @@ find_builtin(const char *name)
return bp; return bp;
} }
#if EDITING_HAS_get_exe_name #if ENABLE_FEATURE_EDITING
static const char * FAST_FUNC static const char * FAST_FUNC
get_builtin_name(int i) get_builtin_name(int i)
{ {

View File

@ -8188,7 +8188,7 @@ static const struct built_in_command *find_builtin(const char *name)
return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]);
} }
#if ENABLE_HUSH_JOB && EDITING_HAS_get_exe_name #if ENABLE_HUSH_JOB && ENABLE_FEATURE_EDITING
static const char * FAST_FUNC get_builtin_name(int i) static const char * FAST_FUNC get_builtin_name(int i)
{ {
if (/*i >= 0 && */ i < ARRAY_SIZE(bltins1)) { if (/*i >= 0 && */ i < ARRAY_SIZE(bltins1)) {
@ -10668,9 +10668,8 @@ int hush_main(int argc, char **argv)
# if ENABLE_FEATURE_EDITING # if ENABLE_FEATURE_EDITING
G.line_input_state = new_line_input_t(FOR_SHELL); G.line_input_state = new_line_input_t(FOR_SHELL);
# if EDITING_HAS_get_exe_name
G.line_input_state->get_exe_name = get_builtin_name; G.line_input_state->get_exe_name = get_builtin_name;
# endif G.line_input_state->sh_get_var = get_local_var_value;
# endif # endif
# if ENABLE_HUSH_SAVEHISTORY && MAX_HISTORY > 0 # if ENABLE_HUSH_SAVEHISTORY && MAX_HISTORY > 0
{ {