aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2022-03-24 12:17:25 +0000
committerDenys Vlasenko <vda.linux@googlemail.com>2022-06-26 18:05:50 +0200
commit7d1c7d8337853ca99e6ec1150ac4b834cfed5cd3 (patch)
tree6fd6c647b5373ed376070d66ac6ca4acc507dee7
parent95fec31be601bfdced6bed7007a33b26e968e0db (diff)
downloadbusybox-w32-7d1c7d8337853ca99e6ec1150ac4b834cfed5cd3.tar.gz
busybox-w32-7d1c7d8337853ca99e6ec1150ac4b834cfed5cd3.tar.bz2
busybox-w32-7d1c7d8337853ca99e6ec1150ac4b834cfed5cd3.zip
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>
-rw-r--r--include/libbb.h11
-rw-r--r--libbb/lineedit.c12
-rw-r--r--shell/ash.c7
-rw-r--r--shell/hush.c5
4 files changed, 19 insertions, 16 deletions
diff --git a/include/libbb.h b/include/libbb.h
index 6aeec249d..abbc9ac59 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1924,6 +1924,7 @@ unsigned size_from_HISTFILESIZE(const char *hp) FAST_FUNC;
1924# define MAX_HISTORY 0 1924# define MAX_HISTORY 0
1925# endif 1925# endif
1926typedef const char *get_exe_name_t(int i) FAST_FUNC; 1926typedef const char *get_exe_name_t(int i) FAST_FUNC;
1927typedef const char *sh_get_var_t(const char *name) FAST_FUNC;
1927typedef struct line_input_t { 1928typedef struct line_input_t {
1928 int flags; 1929 int flags;
1929 int timeout; 1930 int timeout;
@@ -1937,9 +1938,8 @@ typedef struct line_input_t {
1937# if ENABLE_SHELL_ASH || ENABLE_SHELL_HUSH 1938# if ENABLE_SHELL_ASH || ENABLE_SHELL_HUSH
1938 /* function to fetch additional application-specific names to match */ 1939 /* function to fetch additional application-specific names to match */
1939 get_exe_name_t *get_exe_name; 1940 get_exe_name_t *get_exe_name;
1940# define EDITING_HAS_get_exe_name 1 1941 /* function to fetch value of shell variable */
1941# else 1942 sh_get_var_t *sh_get_var;
1942# define EDITING_HAS_get_exe_name 0
1943# endif 1943# endif
1944# endif 1944# endif
1945# if MAX_HISTORY 1945# if MAX_HISTORY
@@ -1993,11 +1993,6 @@ int read_line_input(const char* prompt, char* command, int maxsize) FAST_FUNC;
1993 read_line_input(prompt, command, maxsize) 1993 read_line_input(prompt, command, maxsize)
1994#endif 1994#endif
1995 1995
1996#ifndef EDITING_HAS_get_exe_name
1997# define EDITING_HAS_get_exe_name 0
1998#endif
1999
2000
2001#ifndef COMM_LEN 1996#ifndef COMM_LEN
2002# ifdef TASK_COMM_LEN 1997# ifdef TASK_COMM_LEN
2003enum { COMM_LEN = TASK_COMM_LEN }; 1998enum { COMM_LEN = TASK_COMM_LEN };
diff --git a/libbb/lineedit.c b/libbb/lineedit.c
index 82624757e..b685399f9 100644
--- a/libbb/lineedit.c
+++ b/libbb/lineedit.c
@@ -259,6 +259,16 @@ static const char *get_username_str(void)
259 259
260static NOINLINE const char *get_homedir_or_NULL(void) 260static NOINLINE const char *get_homedir_or_NULL(void)
261{ 261{
262 const char *home;
263
264# if ENABLE_SHELL_ASH || ENABLE_SHELL_HUSH
265 home = state->sh_get_var ? state->sh_get_var("HOME") : getenv("HOME");
266# else
267 home = getenv("HOME");
268# endif
269 if (home != NULL && home[0] != '\0')
270 return home;
271
262 if (!got_user_strings) 272 if (!got_user_strings)
263 get_user_strings(); 273 get_user_strings();
264 return home_pwd_buf; 274 return home_pwd_buf;
@@ -861,7 +871,7 @@ static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type)
861 continue; 871 continue;
862 } 872 }
863# endif 873# endif
864# if EDITING_HAS_get_exe_name 874# if ENABLE_SHELL_ASH || ENABLE_SHELL_HUSH
865 if (state->get_exe_name) { 875 if (state->get_exe_name) {
866 i = 0; 876 i = 0;
867 for (;;) { 877 for (;;) {
diff --git a/shell/ash.c b/shell/ash.c
index ef4a47afe..d29de37b7 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -9720,7 +9720,7 @@ evalpipe(union node *n, int flags)
9720} 9720}
9721 9721
9722/* setinteractive needs this forward reference */ 9722/* setinteractive needs this forward reference */
9723#if EDITING_HAS_get_exe_name 9723#if ENABLE_FEATURE_EDITING
9724static const char *get_builtin_name(int i) FAST_FUNC; 9724static const char *get_builtin_name(int i) FAST_FUNC;
9725#endif 9725#endif
9726 9726
@@ -9757,9 +9757,8 @@ setinteractive(int on)
9757#if ENABLE_FEATURE_EDITING 9757#if ENABLE_FEATURE_EDITING
9758 if (!line_input_state) { 9758 if (!line_input_state) {
9759 line_input_state = new_line_input_t(FOR_SHELL | WITH_PATH_LOOKUP); 9759 line_input_state = new_line_input_t(FOR_SHELL | WITH_PATH_LOOKUP);
9760# if EDITING_HAS_get_exe_name
9761 line_input_state->get_exe_name = get_builtin_name; 9760 line_input_state->get_exe_name = get_builtin_name;
9762# endif 9761 line_input_state->sh_get_var = lookupvar;
9763 } 9762 }
9764#endif 9763#endif
9765 } 9764 }
@@ -10262,7 +10261,7 @@ find_builtin(const char *name)
10262 return bp; 10261 return bp;
10263} 10262}
10264 10263
10265#if EDITING_HAS_get_exe_name 10264#if ENABLE_FEATURE_EDITING
10266static const char * FAST_FUNC 10265static const char * FAST_FUNC
10267get_builtin_name(int i) 10266get_builtin_name(int i)
10268{ 10267{
diff --git a/shell/hush.c b/shell/hush.c
index ae81f0da5..051b123e7 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -8188,7 +8188,7 @@ static const struct built_in_command *find_builtin(const char *name)
8188 return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); 8188 return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]);
8189} 8189}
8190 8190
8191#if ENABLE_HUSH_JOB && EDITING_HAS_get_exe_name 8191#if ENABLE_HUSH_JOB && ENABLE_FEATURE_EDITING
8192static const char * FAST_FUNC get_builtin_name(int i) 8192static const char * FAST_FUNC get_builtin_name(int i)
8193{ 8193{
8194 if (/*i >= 0 && */ i < ARRAY_SIZE(bltins1)) { 8194 if (/*i >= 0 && */ i < ARRAY_SIZE(bltins1)) {
@@ -10668,9 +10668,8 @@ int hush_main(int argc, char **argv)
10668 10668
10669# if ENABLE_FEATURE_EDITING 10669# if ENABLE_FEATURE_EDITING
10670 G.line_input_state = new_line_input_t(FOR_SHELL); 10670 G.line_input_state = new_line_input_t(FOR_SHELL);
10671# if EDITING_HAS_get_exe_name
10672 G.line_input_state->get_exe_name = get_builtin_name; 10671 G.line_input_state->get_exe_name = get_builtin_name;
10673# endif 10672 G.line_input_state->sh_get_var = get_local_var_value;
10674# endif 10673# endif
10675# if ENABLE_HUSH_SAVEHISTORY && MAX_HISTORY > 0 10674# if ENABLE_HUSH_SAVEHISTORY && MAX_HISTORY > 0
10676 { 10675 {