execute.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. /*
  2. Execution routines for GNU Midnight Commander
  3. Copyright (C) 2003-2024
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2013
  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 execute.c
  20. * \brief Source: execution routines
  21. */
  22. #include <config.h>
  23. #include <signal.h>
  24. #include <string.h>
  25. #include <sys/stat.h>
  26. #include <sys/time.h>
  27. #include "lib/global.h"
  28. #include "lib/tty/tty.h"
  29. #include "lib/tty/key.h"
  30. #include "lib/tty/win.h"
  31. #include "lib/vfs/vfs.h"
  32. #include "lib/mcconfig.h"
  33. #include "lib/util.h"
  34. #include "lib/strutil.h" /* str_replace_all_substrings() */
  35. #include "lib/widget.h"
  36. #include "filemanager/filemanager.h"
  37. #include "filemanager/layout.h" /* use_dash() */
  38. #include "consaver/cons.saver.h"
  39. #ifdef ENABLE_SUBSHELL
  40. #include "subshell/subshell.h"
  41. #endif
  42. #include "setup.h" /* clear_before_exec */
  43. #include "execute.h"
  44. /*** global variables ****************************************************************************/
  45. int pause_after_run = pause_on_dumb_terminals;
  46. /*** file scope macro definitions ****************************************************************/
  47. /*** file scope type declarations ****************************************************************/
  48. /*** forward declarations (file scope functions) *************************************************/
  49. void do_execute (const char *shell, const char *command, int flags);
  50. void do_executev (const char *shell, int flags, char *const argv[]);
  51. char *execute_get_external_cmd_opts_from_config (const char *command,
  52. const vfs_path_t * filename_vpath,
  53. long start_line);
  54. /*** file scope variables ************************************************************************/
  55. /* --------------------------------------------------------------------------------------------- */
  56. /*** file scope functions ************************************************************************/
  57. /* --------------------------------------------------------------------------------------------- */
  58. static void
  59. edition_post_exec (void)
  60. {
  61. tty_enter_ca_mode ();
  62. /* FIXME: Missing on slang endwin? */
  63. tty_reset_prog_mode ();
  64. tty_flush_input ();
  65. tty_keypad (TRUE);
  66. tty_raw_mode ();
  67. channels_up ();
  68. enable_mouse ();
  69. enable_bracketed_paste ();
  70. if (mc_global.tty.alternate_plus_minus)
  71. application_keypad_mode ();
  72. }
  73. /* --------------------------------------------------------------------------------------------- */
  74. static void
  75. edition_pre_exec (void)
  76. {
  77. if (clear_before_exec)
  78. tty_clear_screen ();
  79. else
  80. {
  81. if (!(mc_global.tty.console_flag != '\0' || mc_global.tty.xterm_flag))
  82. printf ("\n\n");
  83. }
  84. channels_down ();
  85. disable_mouse ();
  86. disable_bracketed_paste ();
  87. tty_reset_shell_mode ();
  88. tty_keypad (FALSE);
  89. tty_reset_screen ();
  90. numeric_keypad_mode ();
  91. /* on xterms: maybe endwin did not leave the terminal on the shell
  92. * screen page: do it now.
  93. *
  94. * Do not move this before endwin: in some systems rmcup includes
  95. * a call to clear screen, so it will end up clearing the shell screen.
  96. */
  97. tty_exit_ca_mode ();
  98. }
  99. /* --------------------------------------------------------------------------------------------- */
  100. #ifdef ENABLE_SUBSHELL
  101. static void
  102. do_possible_cd (const vfs_path_t *new_dir_vpath)
  103. {
  104. if (!panel_cd (current_panel, new_dir_vpath, cd_exact))
  105. message (D_ERROR, _("Warning"), "%s",
  106. _("The Commander can't change to the directory that\n"
  107. "the subshell claims you are in. Perhaps you have\n"
  108. "deleted your working directory, or given yourself\n"
  109. "extra access permissions with the \"su\" command?"));
  110. }
  111. #endif /* ENABLE_SUBSHELL */
  112. /* --------------------------------------------------------------------------------------------- */
  113. static void
  114. do_suspend_cmd (void)
  115. {
  116. pre_exec ();
  117. if (mc_global.tty.console_flag != '\0' && !mc_global.tty.use_subshell)
  118. handle_console (CONSOLE_RESTORE);
  119. #ifdef SIGTSTP
  120. {
  121. struct sigaction sigtstp_action;
  122. memset (&sigtstp_action, 0, sizeof (sigtstp_action));
  123. /* Make sure that the SIGTSTP below will suspend us directly,
  124. without calling ncurses' SIGTSTP handler; we *don't* want
  125. ncurses to redraw the screen immediately after the SIGCONT */
  126. sigaction (SIGTSTP, &startup_handler, &sigtstp_action);
  127. kill (getpid (), SIGTSTP);
  128. /* Restore previous SIGTSTP action */
  129. sigaction (SIGTSTP, &sigtstp_action, NULL);
  130. }
  131. #endif /* SIGTSTP */
  132. if (mc_global.tty.console_flag != '\0' && !mc_global.tty.use_subshell)
  133. handle_console (CONSOLE_SAVE);
  134. edition_post_exec ();
  135. }
  136. /* --------------------------------------------------------------------------------------------- */
  137. static gboolean
  138. execute_prepare_with_vfs_arg (const vfs_path_t *filename_vpath, vfs_path_t **localcopy_vpath,
  139. time_t *mtime)
  140. {
  141. struct stat st;
  142. /* Simplest case, this file is local */
  143. if ((filename_vpath == NULL && vfs_file_is_local (vfs_get_raw_current_dir ()))
  144. || vfs_file_is_local (filename_vpath))
  145. return TRUE;
  146. /* FIXME: Creation of new files on VFS is not supported */
  147. if (filename_vpath == NULL)
  148. return FALSE;
  149. *localcopy_vpath = mc_getlocalcopy (filename_vpath);
  150. if (*localcopy_vpath == NULL)
  151. {
  152. message (D_ERROR, MSG_ERROR, _("Cannot fetch a local copy of %s"),
  153. vfs_path_as_str (filename_vpath));
  154. return FALSE;
  155. }
  156. mc_stat (*localcopy_vpath, &st);
  157. *mtime = st.st_mtime;
  158. return TRUE;
  159. }
  160. /* --------------------------------------------------------------------------------------------- */
  161. static void
  162. execute_cleanup_with_vfs_arg (const vfs_path_t *filename_vpath, vfs_path_t **localcopy_vpath,
  163. time_t *mtime)
  164. {
  165. if (*localcopy_vpath != NULL)
  166. {
  167. struct stat st;
  168. /*
  169. * filename can be an entry on panel, it can be changed by executing
  170. * the command, so make a copy. Smarter VFS code would make the code
  171. * below unnecessary.
  172. */
  173. mc_stat (*localcopy_vpath, &st);
  174. mc_ungetlocalcopy (filename_vpath, *localcopy_vpath, *mtime != st.st_mtime);
  175. vfs_path_free (*localcopy_vpath, TRUE);
  176. *localcopy_vpath = NULL;
  177. }
  178. }
  179. /* --------------------------------------------------------------------------------------------- */
  180. static char *
  181. execute_get_opts_from_cfg (const char *command, const char *default_str)
  182. {
  183. char *str_from_config;
  184. str_from_config =
  185. mc_config_get_string_raw (mc_global.main_config, CONFIG_EXT_EDITOR_VIEWER_SECTION, command,
  186. NULL);
  187. if (str_from_config == NULL)
  188. {
  189. mc_config_t *cfg;
  190. cfg = mc_config_init (mc_global.profile_name, TRUE);
  191. if (cfg == NULL)
  192. return g_strdup (default_str);
  193. str_from_config =
  194. mc_config_get_string_raw (cfg, CONFIG_EXT_EDITOR_VIEWER_SECTION, command, default_str);
  195. mc_config_deinit (cfg);
  196. }
  197. return str_from_config;
  198. }
  199. /* --------------------------------------------------------------------------------------------- */
  200. /*** public functions ****************************************************************************/
  201. /* --------------------------------------------------------------------------------------------- */
  202. char *
  203. execute_get_external_cmd_opts_from_config (const char *command, const vfs_path_t *filename_vpath,
  204. long start_line)
  205. {
  206. char *str_from_config, *return_str;
  207. char *parameter;
  208. if (filename_vpath == NULL)
  209. return g_strdup ("");
  210. parameter = g_shell_quote (vfs_path_get_last_path_str (filename_vpath));
  211. if (start_line <= 0)
  212. return parameter;
  213. str_from_config = execute_get_opts_from_cfg (command, "%filename");
  214. return_str = str_replace_all (str_from_config, "%filename", parameter);
  215. g_free (parameter);
  216. g_free (str_from_config);
  217. str_from_config = return_str;
  218. parameter = g_strdup_printf ("%ld", start_line);
  219. return_str = str_replace_all (str_from_config, "%lineno", parameter);
  220. g_free (parameter);
  221. g_free (str_from_config);
  222. return return_str;
  223. }
  224. /* --------------------------------------------------------------------------------------------- */
  225. void
  226. do_executev (const char *shell, int flags, char *const argv[])
  227. {
  228. #ifdef ENABLE_SUBSHELL
  229. vfs_path_t *new_dir_vpath = NULL;
  230. #endif /* ENABLE_SUBSHELL */
  231. vfs_path_t *old_vfs_dir_vpath = NULL;
  232. if (!vfs_current_is_local ())
  233. old_vfs_dir_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
  234. if (mc_global.mc_run_mode == MC_RUN_FULL)
  235. save_cwds_stat ();
  236. pre_exec ();
  237. if (mc_global.tty.console_flag != '\0')
  238. handle_console (CONSOLE_RESTORE);
  239. if (!mc_global.tty.use_subshell && *argv != NULL && (flags & EXECUTE_INTERNAL) == 0)
  240. {
  241. printf ("%s%s\n", mc_prompt, *argv);
  242. fflush (stdout);
  243. }
  244. #ifdef ENABLE_SUBSHELL
  245. if (mc_global.tty.use_subshell && (flags & EXECUTE_INTERNAL) == 0)
  246. {
  247. do_update_prompt ();
  248. /* We don't care if it died, higher level takes care of this */
  249. invoke_subshell (*argv, VISIBLY, old_vfs_dir_vpath != NULL ? NULL : &new_dir_vpath);
  250. }
  251. else
  252. #endif /* ENABLE_SUBSHELL */
  253. my_systemv_flags (flags, shell, argv);
  254. if ((flags & EXECUTE_INTERNAL) == 0)
  255. {
  256. if ((pause_after_run == pause_always
  257. || (pause_after_run == pause_on_dumb_terminals && !mc_global.tty.xterm_flag
  258. && mc_global.tty.console_flag == '\0')) && quit == 0
  259. #ifdef ENABLE_SUBSHELL
  260. && subshell_state != RUNNING_COMMAND
  261. #endif /* ENABLE_SUBSHELL */
  262. )
  263. {
  264. printf ("%s", _("Press any key to continue..."));
  265. fflush (stdout);
  266. tty_raw_mode ();
  267. get_key_code (0);
  268. printf ("\r\n");
  269. fflush (stdout);
  270. }
  271. if (mc_global.tty.console_flag != '\0' && output_lines != 0 && mc_global.keybar_visible)
  272. {
  273. putchar ('\n');
  274. fflush (stdout);
  275. }
  276. }
  277. if (mc_global.tty.console_flag != '\0')
  278. handle_console (CONSOLE_SAVE);
  279. edition_post_exec ();
  280. #ifdef ENABLE_SUBSHELL
  281. if (new_dir_vpath != NULL)
  282. {
  283. do_possible_cd (new_dir_vpath);
  284. vfs_path_free (new_dir_vpath, TRUE);
  285. }
  286. #endif /* ENABLE_SUBSHELL */
  287. if (old_vfs_dir_vpath != NULL)
  288. {
  289. mc_chdir (old_vfs_dir_vpath);
  290. vfs_path_free (old_vfs_dir_vpath, TRUE);
  291. }
  292. if (mc_global.mc_run_mode == MC_RUN_FULL)
  293. {
  294. update_panels (UP_OPTIMIZE, UP_KEEPSEL);
  295. update_xterm_title_path ();
  296. update_terminal_cwd ();
  297. }
  298. do_refresh ();
  299. use_dash (TRUE);
  300. }
  301. /* --------------------------------------------------------------------------------------------- */
  302. void
  303. do_execute (const char *shell, const char *command, int flags)
  304. {
  305. GPtrArray *args_array;
  306. args_array = g_ptr_array_new ();
  307. g_ptr_array_add (args_array, (char *) command);
  308. g_ptr_array_add (args_array, NULL);
  309. do_executev (shell, flags, (char *const *) args_array->pdata);
  310. g_ptr_array_free (args_array, TRUE);
  311. }
  312. /* --------------------------------------------------------------------------------------------- */
  313. /** Set up the terminal before executing a program */
  314. void
  315. pre_exec (void)
  316. {
  317. use_dash (FALSE);
  318. edition_pre_exec ();
  319. }
  320. /* --------------------------------------------------------------------------------------------- */
  321. /** Hide the terminal after executing a program */
  322. void
  323. post_exec (void)
  324. {
  325. edition_post_exec ();
  326. use_dash (TRUE);
  327. repaint_screen ();
  328. }
  329. /* --------------------------------------------------------------------------------------------- */
  330. /* Executes a command */
  331. void
  332. shell_execute (const char *command, int flags)
  333. {
  334. char *cmd = NULL;
  335. if ((flags & EXECUTE_HIDE) != 0)
  336. {
  337. cmd = g_strconcat (" ", command, (char *) NULL);
  338. flags ^= EXECUTE_HIDE;
  339. }
  340. #ifdef ENABLE_SUBSHELL
  341. if (mc_global.tty.use_subshell)
  342. {
  343. if (subshell_state == INACTIVE)
  344. do_execute (mc_global.shell->path, cmd != NULL ? cmd : command,
  345. flags | EXECUTE_AS_SHELL);
  346. else
  347. message (D_ERROR, MSG_ERROR, "%s", _("The shell is already running a command"));
  348. }
  349. else
  350. #endif /* ENABLE_SUBSHELL */
  351. do_execute (mc_global.shell->path, cmd != NULL ? cmd : command, flags | EXECUTE_AS_SHELL);
  352. g_free (cmd);
  353. }
  354. /* --------------------------------------------------------------------------------------------- */
  355. void
  356. toggle_subshell (void)
  357. {
  358. static gboolean message_flag = TRUE;
  359. #ifdef ENABLE_SUBSHELL
  360. vfs_path_t *new_dir_vpath = NULL;
  361. #endif /* ENABLE_SUBSHELL */
  362. SIG_ATOMIC_VOLATILE_T was_sigwinch = 0;
  363. if (!(mc_global.tty.xterm_flag || mc_global.tty.console_flag != '\0'
  364. || mc_global.tty.use_subshell || output_starts_shell))
  365. {
  366. if (message_flag)
  367. message (D_ERROR, MSG_ERROR,
  368. _("Not an xterm or Linux console;\nthe subshell cannot be toggled."));
  369. message_flag = FALSE;
  370. return;
  371. }
  372. channels_down ();
  373. disable_mouse ();
  374. disable_bracketed_paste ();
  375. if (clear_before_exec)
  376. tty_clear_screen ();
  377. if (mc_global.tty.alternate_plus_minus)
  378. numeric_keypad_mode ();
  379. #ifndef HAVE_SLANG
  380. /* With slang we don't want any of this, since there
  381. * is no raw_mode supported
  382. */
  383. tty_reset_shell_mode ();
  384. #endif /* !HAVE_SLANG */
  385. tty_noecho ();
  386. tty_keypad (FALSE);
  387. tty_reset_screen ();
  388. tty_exit_ca_mode ();
  389. tty_raw_mode ();
  390. if (mc_global.tty.console_flag != '\0')
  391. handle_console (CONSOLE_RESTORE);
  392. #ifdef ENABLE_SUBSHELL
  393. if (mc_global.tty.use_subshell)
  394. {
  395. vfs_path_t **new_dir_p;
  396. new_dir_p = vfs_current_is_local ()? &new_dir_vpath : NULL;
  397. invoke_subshell (NULL, VISIBLY, new_dir_p);
  398. }
  399. else
  400. #endif /* ENABLE_SUBSHELL */
  401. {
  402. if (output_starts_shell)
  403. {
  404. fputs (_("Type 'exit' to return to the Midnight Commander"), stderr);
  405. fputs ("\n\r\n\r", stderr);
  406. my_system (EXECUTE_INTERNAL, mc_global.shell->path, NULL);
  407. }
  408. else
  409. get_key_code (0);
  410. }
  411. if (mc_global.tty.console_flag != '\0')
  412. handle_console (CONSOLE_SAVE);
  413. tty_enter_ca_mode ();
  414. tty_reset_prog_mode ();
  415. tty_keypad (TRUE);
  416. /* Prevent screen flash when user did 'exit' or 'logout' within
  417. subshell */
  418. if ((quit & SUBSHELL_EXIT) != 0)
  419. {
  420. /* User did 'exit' or 'logout': quit MC */
  421. if (quiet_quit_cmd ())
  422. return;
  423. quit = 0;
  424. #ifdef ENABLE_SUBSHELL
  425. /* restart subshell */
  426. if (mc_global.tty.use_subshell)
  427. init_subshell ();
  428. #endif /* ENABLE_SUBSHELL */
  429. }
  430. enable_mouse ();
  431. enable_bracketed_paste ();
  432. channels_up ();
  433. if (mc_global.tty.alternate_plus_minus)
  434. application_keypad_mode ();
  435. /* HACK:
  436. * Save sigwinch flag that will be reset in mc_refresh() called via update_panels().
  437. * There is some problem with screen redraw in ncurses-based mc in this situation.
  438. */
  439. was_sigwinch = tty_got_winch ();
  440. tty_flush_winch ();
  441. #ifdef ENABLE_SUBSHELL
  442. if (mc_global.tty.use_subshell)
  443. {
  444. if (mc_global.mc_run_mode == MC_RUN_FULL)
  445. {
  446. if (new_dir_vpath != NULL)
  447. do_possible_cd (new_dir_vpath);
  448. }
  449. else if (new_dir_vpath != NULL && mc_chdir (new_dir_vpath) != -1)
  450. vfs_setup_cwd ();
  451. }
  452. vfs_path_free (new_dir_vpath, TRUE);
  453. #endif /* ENABLE_SUBSHELL */
  454. if (mc_global.mc_run_mode == MC_RUN_FULL)
  455. {
  456. update_panels (UP_OPTIMIZE, UP_KEEPSEL);
  457. update_xterm_title_path ();
  458. update_terminal_cwd ();
  459. }
  460. if (was_sigwinch != 0 || tty_got_winch ())
  461. dialog_change_screen_size ();
  462. else
  463. repaint_screen ();
  464. }
  465. /* --------------------------------------------------------------------------------------------- */
  466. /* event callback */
  467. gboolean
  468. execute_suspend (const gchar *event_group_name, const gchar *event_name,
  469. gpointer init_data, gpointer data)
  470. {
  471. (void) event_group_name;
  472. (void) event_name;
  473. (void) init_data;
  474. (void) data;
  475. if (mc_global.mc_run_mode == MC_RUN_FULL)
  476. save_cwds_stat ();
  477. do_suspend_cmd ();
  478. if (mc_global.mc_run_mode == MC_RUN_FULL)
  479. update_panels (UP_OPTIMIZE, UP_KEEPSEL);
  480. do_refresh ();
  481. return TRUE;
  482. }
  483. /* --------------------------------------------------------------------------------------------- */
  484. /**
  485. * Execute command on a filename that can be on VFS.
  486. * Errors are reported to the user.
  487. */
  488. void
  489. execute_with_vfs_arg (const char *command, const vfs_path_t *filename_vpath)
  490. {
  491. vfs_path_t *localcopy_vpath = NULL;
  492. const vfs_path_t *do_execute_vpath;
  493. time_t mtime;
  494. if (!execute_prepare_with_vfs_arg (filename_vpath, &localcopy_vpath, &mtime))
  495. return;
  496. do_execute_vpath = (localcopy_vpath == NULL) ? filename_vpath : localcopy_vpath;
  497. do_execute (command, vfs_path_get_last_path_str (do_execute_vpath), EXECUTE_INTERNAL);
  498. execute_cleanup_with_vfs_arg (filename_vpath, &localcopy_vpath, &mtime);
  499. }
  500. /* --------------------------------------------------------------------------------------------- */
  501. /**
  502. * Execute external editor or viewer.
  503. *
  504. * @param command editor/viewer to run
  505. * @param filename_vpath path for edit/view
  506. * @param start_line cursor will be placed at the 'start_line' position after opening file
  507. * if start_line is 0 or negative, no start line will be passed to editor/viewer
  508. */
  509. void
  510. execute_external_editor_or_viewer (const char *command, const vfs_path_t *filename_vpath,
  511. long start_line)
  512. {
  513. vfs_path_t *localcopy_vpath = NULL;
  514. const vfs_path_t *do_execute_vpath;
  515. char *extern_cmd_options;
  516. time_t mtime = 0;
  517. if (!execute_prepare_with_vfs_arg (filename_vpath, &localcopy_vpath, &mtime))
  518. return;
  519. do_execute_vpath = (localcopy_vpath == NULL) ? filename_vpath : localcopy_vpath;
  520. extern_cmd_options =
  521. execute_get_external_cmd_opts_from_config (command, do_execute_vpath, start_line);
  522. if (extern_cmd_options != NULL)
  523. {
  524. char **argv_cmd_options;
  525. int argv_count;
  526. if (g_shell_parse_argv (extern_cmd_options, &argv_count, &argv_cmd_options, NULL))
  527. {
  528. do_executev (command, EXECUTE_INTERNAL, argv_cmd_options);
  529. g_strfreev (argv_cmd_options);
  530. }
  531. else
  532. do_executev (command, EXECUTE_INTERNAL, NULL);
  533. g_free (extern_cmd_options);
  534. }
  535. execute_cleanup_with_vfs_arg (filename_vpath, &localcopy_vpath, &mtime);
  536. }
  537. /* --------------------------------------------------------------------------------------------- */