shell.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. Provides a functions for working with shell.
  3. Copyright (C) 2006-2016
  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 string 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. /* No fish as fallback because it is so much different from other shells and
  60. * in a way exotic (even though user-friendly by name) that we should not
  61. * present it as a subshell without the user's explicit intention. We rather
  62. * will not use a subshell but just a command line.
  63. * else if (access("/bin/fish", X_OK) == 0)
  64. * mc_global.tty.shell = g_strdup ("/bin/fish");
  65. */
  66. else
  67. /* Fallback and last resort: system default shell */
  68. mc_shell->path = g_strdup ("/bin/sh");
  69. return mc_shell;
  70. }
  71. /* --------------------------------------------------------------------------------------------- */
  72. static char *
  73. mc_shell_get_name_env (void)
  74. {
  75. const char *shell_env;
  76. char *shell_name = NULL;
  77. shell_env = g_getenv ("SHELL");
  78. if ((shell_env == NULL) || (shell_env[0] == '\0'))
  79. {
  80. /* 2nd choice: user login shell */
  81. struct passwd *pwd;
  82. pwd = getpwuid (geteuid ());
  83. if (pwd != NULL)
  84. shell_name = g_strdup (pwd->pw_shell);
  85. }
  86. else
  87. /* 1st choice: SHELL environment variable */
  88. shell_name = g_strdup (shell_env);
  89. return shell_name;
  90. }
  91. /* --------------------------------------------------------------------------------------------- */
  92. static mc_shell_t *
  93. mc_shell_get_from_env (void)
  94. {
  95. mc_shell_t *mc_shell = NULL;
  96. char *shell_name;
  97. shell_name = mc_shell_get_name_env ();
  98. if (shell_name != NULL)
  99. {
  100. mc_shell = g_new0 (mc_shell_t, 1);
  101. mc_shell->path = shell_name;
  102. }
  103. return mc_shell;
  104. }
  105. /* --------------------------------------------------------------------------------------------- */
  106. static void
  107. mc_shell_recognize_real_path (mc_shell_t * mc_shell)
  108. {
  109. if (strstr (mc_shell->path, "/zsh") != NULL || strstr (mc_shell->real_path, "/zsh") != NULL
  110. || getenv ("ZSH_VERSION") != NULL)
  111. {
  112. /* Also detects ksh symlinked to zsh */
  113. mc_shell->type = SHELL_ZSH;
  114. mc_shell->name = "zsh";
  115. }
  116. else if (strstr (mc_shell->path, "/tcsh") != NULL
  117. || strstr (mc_shell->real_path, "/tcsh") != NULL)
  118. {
  119. /* Also detects csh symlinked to tcsh */
  120. mc_shell->type = SHELL_TCSH;
  121. mc_shell->name = "tcsh";
  122. }
  123. else if (strstr (mc_shell->path, "/fish") != NULL
  124. || strstr (mc_shell->real_path, "/fish") != NULL)
  125. {
  126. mc_shell->type = SHELL_FISH;
  127. mc_shell->name = "fish";
  128. }
  129. else if (strstr (mc_shell->path, "/dash") != NULL
  130. || strstr (mc_shell->real_path, "/dash") != NULL)
  131. {
  132. /* Debian ash (also found if symlinked to by ash/sh) */
  133. mc_shell->type = SHELL_DASH;
  134. mc_shell->name = "dash";
  135. }
  136. else if (strstr (mc_shell->real_path, "/busybox") != NULL)
  137. {
  138. /* If shell is symlinked to busybox, assume it is an ash, even though theoretically
  139. * it could also be a hush (a mini shell for non-MMU systems deactivated by default).
  140. * For simplicity's sake we assume that busybox always contains an ash, not a hush.
  141. * On embedded platforms or on server systems, /bin/sh often points to busybox.
  142. * Sometimes even bash is symlinked to busybox (CONFIG_FEATURE_BASH_IS_ASH option),
  143. * so we need to check busybox symlinks *before* checking for the name "bash"
  144. * in order to avoid that case. */
  145. mc_shell->type = SHELL_ASH_BUSYBOX;
  146. mc_shell->name = mc_shell->path;
  147. }
  148. else
  149. mc_shell->type = SHELL_NONE;
  150. }
  151. /* --------------------------------------------------------------------------------------------- */
  152. static void
  153. mc_shell_recognize_path (mc_shell_t * mc_shell)
  154. {
  155. /* If shell is not symlinked to busybox, it is safe to assume it is a real shell */
  156. if (strstr (mc_shell->path, "/bash") != NULL || getenv ("BASH") != NULL)
  157. {
  158. mc_shell->type = SHELL_BASH;
  159. mc_shell->name = "bash";
  160. }
  161. else if (strstr (mc_shell->path, "/sh") != NULL || getenv ("SH") != NULL)
  162. {
  163. mc_shell->type = SHELL_SH;
  164. mc_shell->name = "sh";
  165. }
  166. else if (strstr (mc_shell->path, "/ash") != NULL || getenv ("ASH") != NULL)
  167. {
  168. mc_shell->type = SHELL_ASH_BUSYBOX;
  169. mc_shell->name = "ash";
  170. }
  171. else
  172. mc_shell->type = SHELL_NONE;
  173. }
  174. /* --------------------------------------------------------------------------------------------- */
  175. /*** public functions ****************************************************************************/
  176. /* --------------------------------------------------------------------------------------------- */
  177. void
  178. mc_shell_init (void)
  179. {
  180. mc_shell_t *mc_shell;
  181. mc_shell = mc_shell_get_from_env ();
  182. if (mc_shell == NULL)
  183. mc_shell = mc_shell_get_installed_in_system ();
  184. mc_shell->real_path = mc_realpath (mc_shell->path, rp_shell);
  185. /* Find out what type of shell we have. Also consider real paths (resolved symlinks)
  186. * because e.g. csh might point to tcsh, ash to dash or busybox, sh to anything. */
  187. if (mc_shell->real_path != NULL)
  188. mc_shell_recognize_real_path (mc_shell);
  189. if (mc_shell->type == SHELL_NONE)
  190. mc_shell_recognize_path (mc_shell);
  191. mc_global.tty.use_subshell = mc_shell->type != SHELL_NONE;
  192. mc_global.shell = mc_shell;
  193. }
  194. /* --------------------------------------------------------------------------------------------- */
  195. void
  196. mc_shell_deinit (void)
  197. {
  198. if (mc_global.shell != NULL)
  199. {
  200. g_free (mc_global.shell->path);
  201. MC_PTR_FREE (mc_global.shell);
  202. }
  203. }
  204. /* --------------------------------------------------------------------------------------------- */