From 5d6a04c8594f9441b12c1a7485a4106b03bd2b03 Mon Sep 17 00:00:00 2001 From: Allison Karlitskaya Date: Fri, 7 May 2021 10:59:09 +0200 Subject: [PATCH 1/2] shell: reduce duplication with startup files The logic for deciding which startup files to run currently includes two separate copies of the bashrc (system vs homedir), and the profile (system vs homedir, posix vs bash). Create two separate functions to handle the "profile" vs "bashrc" cases without duplicating the file lists. Meanwhile, the handling of the `--rcfile` option works by preseeding the option value to the default of "~/.bashrc" and overriding it when the user specifies the option. That works nicely, but only because we only consider exactly one possible bashrc file (which will stop being true with the following commit). Rework this a little to make the `--rcfile` option act as an override, which is set to NULL in case the flag isn't specified. In that case, we will fall back to the default bashrc. This commit is a pure refactor. It introduces no functional changes. --- shell.c | 85 +++++++++++++++++++++++++++------------------------------ 1 file changed, 40 insertions(+), 45 deletions(-) diff --git a/shell.c b/shell.c index ce8087f7..6327e72d 100644 --- a/shell.c +++ b/shell.c @@ -192,7 +192,7 @@ int have_devfd = 0; #endif /* The name of the .(shell)rc file. */ -static char *bashrc_file = DEFAULT_BASHRC; +static char *bashrc_file; /* Non-zero means to act more like the Bourne shell on startup. */ static int act_like_sh; @@ -1095,6 +1095,40 @@ execute_env_file (env_file) } } +/* Execute /etc/profile and one of the personal login shell + initialization files. */ +static void +execute_profile () +{ + maybe_execute_file (SYS_PROFILE, 1); + + if (!act_like_sh) + { + if (maybe_execute_file ("~/.bash_profile", 1) || + maybe_execute_file ("~/.bash_login", 1)) + return; + } + + maybe_execute_file ("~/.profile", 1); +} + +static void +execute_bashrc () +{ +#ifdef SYS_BASHRC +# if defined (__OPENNT) + maybe_execute_file (_prefixInstallPath(SYS_BASHRC, NULL, 0), 1); +# else + maybe_execute_file (SYS_BASHRC, 1); +# endif +#endif + + if (bashrc_file) + maybe_execute_file (bashrc_file, 1); + else + maybe_execute_file (DEFAULT_BASHRC, 1); +} + static void run_startup_files () { @@ -1117,17 +1151,7 @@ run_startup_files () /* If we were run by sshd or we think we were run by rshd, execute ~/.bashrc if we are a top-level shell. */ if ((run_by_ssh || isnetconn (fileno (stdin))) && shell_level < 2) - { -#ifdef SYS_BASHRC -# if defined (__OPENNT) - maybe_execute_file (_prefixInstallPath(SYS_BASHRC, NULL, 0), 1); -# else - maybe_execute_file (SYS_BASHRC, 1); -# endif -#endif - maybe_execute_file (bashrc_file, 1); - return; - } + execute_bashrc (); } #if defined (JOB_CONTROL) @@ -1150,18 +1174,8 @@ run_startup_files () /* We don't execute .bashrc for login shells. */ no_rc++; - /* Execute /etc/profile and one of the personal login shell - initialization files. */ if (no_profile == 0) - { - maybe_execute_file (SYS_PROFILE, 1); - - if (act_like_sh) /* sh */ - maybe_execute_file ("~/.profile", 1); - else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) && - (maybe_execute_file ("~/.bash_login", 1) == 0)) /* bash */ - maybe_execute_file ("~/.profile", 1); - } + execute_profile (); sourced_login = 1; } @@ -1186,32 +1200,13 @@ run_startup_files () /* We don't execute .bashrc for login shells. */ no_rc++; - /* Execute /etc/profile and one of the personal login shell - initialization files. */ if (no_profile == 0) - { - maybe_execute_file (SYS_PROFILE, 1); - - if (act_like_sh) /* sh */ - maybe_execute_file ("~/.profile", 1); - else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) && - (maybe_execute_file ("~/.bash_login", 1) == 0)) /* bash */ - maybe_execute_file ("~/.profile", 1); - } + execute_profile (); } /* bash */ if (act_like_sh == 0 && no_rc == 0) - { -#ifdef SYS_BASHRC -# if defined (__OPENNT) - maybe_execute_file (_prefixInstallPath(SYS_BASHRC, NULL, 0), 1); -# else - maybe_execute_file (SYS_BASHRC, 1); -# endif -#endif - maybe_execute_file (bashrc_file, 1); - } + execute_bashrc (); /* sh */ else if (act_like_sh && privileged_mode == 0 && sourced_env++ == 0) execute_env_file (get_string_value ("ENV")); @@ -2007,7 +2002,7 @@ shell_reinitialize () /* Ensure that the default startup file is used. (Except that we don't execute this file for reinitialized shells). */ - bashrc_file = DEFAULT_BASHRC; + bashrc_file = NULL; /* Delete all variables and functions. They will be reinitialized when the environment is parsed. */ -- 2.31.1