shell.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. Provides a functions for working with shell.
  3. Copyright (C) 2006-2021
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2015.
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /** \file shell.c
  20. * \brief Source: provides a functions for working with shell.
  21. */
  22. #include <config.h>
  23. #include <pwd.h> /* for username in xterm title */
  24. #include <stdarg.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include "global.h"
  28. #include "util.h"
  29. /*** global variables ****************************************************************************/
  30. /*** file scope macro definitions ****************************************************************/
  31. /*** file scope type declarations ****************************************************************/
  32. /*** file scope variables ************************************************************************/
  33. static char rp_shell[PATH_MAX];
  34. /*** file scope functions ************************************************************************/
  35. /* --------------------------------------------------------------------------------------------- */
  36. /**
  37. * Get a system shell.
  38. *
  39. * @return newly allocated mc_shell_t object with shell name
  40. */
  41. static mc_shell_t *
  42. mc_shell_get_installed_in_system (void)
  43. {
  44. mc_shell_t *mc_shell;
  45. mc_shell = g_new0 (mc_shell_t, 1);
  46. /* 3rd choice: look for existing shells supported as MC subshells. */
  47. if (access ("/bin/bash", X_OK) == 0)
  48. mc_shell->path = g_strdup ("/bin/bash");
  49. else if (access ("/bin/ash", X_OK) == 0)
  50. mc_shell->path = g_strdup ("/bin/ash");
  51. else if (access ("/bin/dash", X_OK) == 0)
  52. mc_shell->path = g_strdup ("/bin/dash");
  53. else if (access ("/bin/busybox", X_OK) == 0)
  54. mc_shell->path = g_strdup ("/bin/busybox");
  55. else if (access ("/bin/zsh", X_OK) == 0)
  56. mc_shell->path = g_strdup ("/bin/zsh");
  57. else if (access ("/bin/tcsh", X_OK) == 0)
  58. mc_shell->path = g_strdup ("/bin/tcsh");
  59. else if (access ("/bin/csh", X_OK) == 0)
  60. mc_shell->path = g_strdup ("/bin/csh");
  61. /* No fish as fallback because it is so much different from other shells and
  62. * in a way exotic (even though user-friendly by name) that we should not
  63. * present it as a subshell without the user's explicit intention. We rather
  64. * will not use a subshell but just a command line.
  65. * else if (access("/bin/fish", X_OK) == 0)
  66. * mc_global.tty.shell = g_strdup ("/bin/fish");
  67. */
  68. else
  69. /* Fallback and last resort: system default shell */
  70. mc_shell->path = g_strdup ("/bin/sh");
  71. return mc_shell;
  72. }
  73. /* --------------------------------------------------------------------------------------------- */
  74. static char *
  75. mc_shell_get_name_env (void)
  76. {
  77. const char *shell_env;
  78. char *shell_name = NULL;
  79. shell_env = g_getenv ("SHELL");
  80. if ((shell_env == NULL) || (shell_env[0] == '\0'))
  81. {
  82. /* 2nd choice: user login shell */
  83. struct passwd *pwd;
  84. pwd = getpwuid (geteuid ());
  85. if (pwd != NULL)
  86. shell_name = g_strdup (pwd->pw_shell);
  87. }
  88. else
  89. /* 1st choice: SHELL environment variable */
  90. shell_name = g_strdup (shell_env);
  91. return shell_name;
  92. }
  93. /* --------------------------------------------------------------------------------------------- */
  94. static mc_shell_t *
  95. mc_shell_get_from_env (void)
  96. {
  97. mc_shell_t *mc_shell = NULL;
  98. char *shell_name;
  99. shell_name = mc_shell_get_name_env ();
  100. if (shell_name != NULL)
  101. {
  102. mc_shell = g_new0 (mc_shell_t, 1);
  103. mc_shell->path = shell_name;
  104. }
  105. return mc_shell;
  106. }
  107. /* --------------------------------------------------------------------------------------------- */
  108. static void
  109. mc_shell_recognize_real_path (mc_shell_t * mc_shell)
  110. {
  111. if (strstr (mc_shell->path, "/zsh") != NULL || strstr (mc_shell->real_path, "/zsh") != NULL
  112. || getenv ("ZSH_VERSION") != NULL)
  113. {
  114. /* Also detects ksh symlinked to zsh */
  115. mc_shell->type = SHELL_ZSH;
  116. mc_shell->name = "zsh";
  117. }
  118. else if (strstr (mc_shell->path, "/tcsh") != NULL
  119. || strstr (mc_shell->real_path, "/tcsh") != NULL)
  120. {
  121. /* Also detects csh symlinked to tcsh */
  122. mc_shell->type = SHELL_TCSH;
  123. mc_shell->name = "tcsh";
  124. }
  125. else if (strstr (mc_shell->path, "/csh") != NULL
  126. || strstr (mc_shell->real_path, "/csh") != NULL)
  127. {
  128. mc_shell->type = SHELL_TCSH;
  129. mc_shell->name = "csh";
  130. }
  131. else if (strstr (mc_shell->path, "/fish") != NULL
  132. || strstr (mc_shell->real_path, "/fish") != NULL)
  133. {
  134. mc_shell->type = SHELL_FISH;
  135. mc_shell->name = "fish";
  136. }
  137. else if (strstr (mc_shell->path, "/dash") != NULL
  138. || strstr (mc_shell->real_path, "/dash") != NULL)
  139. {
  140. /* Debian ash (also found if symlinked to by ash/sh) */
  141. mc_shell->type = SHELL_DASH;
  142. mc_shell->name = "dash";
  143. }
  144. else if (strstr (mc_shell->real_path, "/busybox") != NULL)
  145. {
  146. /* If shell is symlinked to busybox, assume it is an ash, even though theoretically
  147. * it could also be a hush (a mini shell for non-MMU systems deactivated by default).
  148. * For simplicity's sake we assume that busybox always contains an ash, not a hush.
  149. * On embedded platforms or on server systems, /bin/sh often points to busybox.
  150. * Sometimes even bash is symlinked to busybox (CONFIG_FEATURE_BASH_IS_ASH option),
  151. * so we need to check busybox symlinks *before* checking for the name "bash"
  152. * in order to avoid that case. */
  153. mc_shell->type = SHELL_ASH_BUSYBOX;
  154. mc_shell->name = mc_shell->path;
  155. }
  156. else
  157. mc_shell->type = SHELL_NONE;
  158. }
  159. /* --------------------------------------------------------------------------------------------- */
  160. static void
  161. mc_shell_recognize_path (mc_shell_t * mc_shell)
  162. {
  163. /* If shell is not symlinked to busybox, it is safe to assume it is a real shell */
  164. if (strstr (mc_shell->path, "/bash") != NULL || getenv ("BASH") != NULL)
  165. {
  166. mc_shell->type = SHELL_BASH;
  167. mc_shell->name = "bash";
  168. }
  169. else if (strstr (mc_shell->path, "/sh") != NULL || getenv ("SH") != NULL)
  170. {
  171. mc_shell->type = SHELL_SH;
  172. mc_shell->name = "sh";
  173. }
  174. else if (strstr (mc_shell->path, "/ash") != NULL || getenv ("ASH") != NULL)
  175. {
  176. mc_shell->type = SHELL_ASH_BUSYBOX;
  177. mc_shell->name = "ash";
  178. }
  179. else
  180. mc_shell->type = SHELL_NONE;
  181. }
  182. /* --------------------------------------------------------------------------------------------- */
  183. /*** public functions ****************************************************************************/
  184. /* --------------------------------------------------------------------------------------------- */
  185. void
  186. mc_shell_init (void)
  187. {
  188. mc_shell_t *mc_shell;
  189. mc_shell = mc_shell_get_from_env ();
  190. if (mc_shell == NULL)
  191. mc_shell = mc_shell_get_installed_in_system ();
  192. mc_shell->real_path = mc_realpath (mc_shell->path, rp_shell);
  193. /* Find out what type of shell we have. Also consider real paths (resolved symlinks)
  194. * because e.g. csh might point to tcsh, ash to dash or busybox, sh to anything. */
  195. if (mc_shell->real_path != NULL)
  196. mc_shell_recognize_real_path (mc_shell);
  197. if (mc_shell->type == SHELL_NONE)
  198. mc_shell_recognize_path (mc_shell);
  199. if (mc_shell->type == SHELL_NONE)
  200. mc_global.tty.use_subshell = FALSE;
  201. mc_global.shell = mc_shell;
  202. }
  203. /* --------------------------------------------------------------------------------------------- */
  204. void
  205. mc_shell_deinit (void)
  206. {
  207. if (mc_global.shell != NULL)
  208. {
  209. g_free (mc_global.shell->path);
  210. MC_PTR_FREE (mc_global.shell);
  211. }
  212. }
  213. /* --------------------------------------------------------------------------------------------- */