2 Execution routines for GNU Midnight Commander
4 Copyright (C) 2003, 2004, 2005, 2007, 2011
5 The Free Software Foundation, Inc.
7 This file is part of the Midnight Commander.
9 The Midnight Commander is free software: you can redistribute it
10 and/or modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation, either version 3 of the License,
12 or (at your option) any later version.
14 The Midnight Commander is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * \brief Source: execution routines
33 #include "lib/global.h"
35 #include "lib/tty/tty.h"
36 #include "lib/tty/key.h"
37 #include "lib/tty/win.h"
38 #include "lib/vfs/vfs.h"
39 #include "lib/mcconfig.h"
41 #include "lib/widget.h"
43 #include "filemanager/midnight.h"
44 #include "filemanager/layout.h" /* use_dash() */
45 #include "consaver/cons.saver.h"
46 #ifdef ENABLE_SUBSHELL
49 #include "setup.h" /* clear_before_exec */
53 /*** global variables ****************************************************************************/
55 int pause_after_run
= pause_on_dumb_terminals
;
57 /*** file scope macro definitions ****************************************************************/
59 /*** file scope type declarations ****************************************************************/
61 /*** file scope variables ************************************************************************/
63 /*** file scope functions ************************************************************************/
65 void do_execute (const char *shell
, const char *command
, int flags
);
67 /* --------------------------------------------------------------------------------------------- */
70 edition_post_exec (void)
74 /* FIXME: Missing on slang endwin? */
75 tty_reset_prog_mode ();
82 if (mc_global
.tty
.alternate_plus_minus
)
83 application_keypad_mode ();
86 /* --------------------------------------------------------------------------------------------- */
89 edition_pre_exec (void)
91 if (clear_before_exec
)
95 if (!(mc_global
.tty
.console_flag
!= '\0' || mc_global
.tty
.xterm_flag
))
102 tty_reset_shell_mode ();
106 numeric_keypad_mode ();
108 /* on xterms: maybe endwin did not leave the terminal on the shell
109 * screen page: do it now.
111 * Do not move this before endwin: in some systems rmcup includes
112 * a call to clear screen, so it will end up clearing the shell screen.
117 /* --------------------------------------------------------------------------------------------- */
119 #ifdef ENABLE_SUBSHELL
121 do_possible_cd (const vfs_path_t
* new_dir_vpath
)
123 if (!do_cd (new_dir_vpath
, cd_exact
))
124 message (D_ERROR
, _("Warning"),
125 _("The Commander can't change to the directory that\n"
126 "the subshell claims you are in. Perhaps you have\n"
127 "deleted your working directory, or given yourself\n"
128 "extra access permissions with the \"su\" command?"));
130 #endif /* ENABLE_SUBSHELL */
132 /* --------------------------------------------------------------------------------------------- */
135 do_suspend_cmd (void)
139 if (mc_global
.tty
.console_flag
!= '\0' && !mc_global
.tty
.use_subshell
)
140 handle_console (CONSOLE_RESTORE
);
144 struct sigaction sigtstp_action
;
146 /* Make sure that the SIGTSTP below will suspend us directly,
147 without calling ncurses' SIGTSTP handler; we *don't* want
148 ncurses to redraw the screen immediately after the SIGCONT */
149 sigaction (SIGTSTP
, &startup_handler
, &sigtstp_action
);
151 kill (getpid (), SIGTSTP
);
153 /* Restore previous SIGTSTP action */
154 sigaction (SIGTSTP
, &sigtstp_action
, NULL
);
158 if (mc_global
.tty
.console_flag
!= '\0' && !mc_global
.tty
.use_subshell
)
159 handle_console (CONSOLE_SAVE
);
161 edition_post_exec ();
164 /* --------------------------------------------------------------------------------------------- */
165 /*** public functions ****************************************************************************/
166 /* --------------------------------------------------------------------------------------------- */
169 do_execute (const char *shell
, const char *command
, int flags
)
171 #ifdef ENABLE_SUBSHELL
172 vfs_path_t
*new_dir_vpath
= NULL
;
173 #endif /* ENABLE_SUBSHELL */
175 vfs_path_t
*old_vfs_dir_vpath
= NULL
;
177 if (!vfs_current_is_local ())
178 old_vfs_dir_vpath
= vfs_path_clone (vfs_get_raw_current_dir ());
180 if (mc_global
.mc_run_mode
== MC_RUN_FULL
)
183 if (mc_global
.tty
.console_flag
!= '\0')
184 handle_console (CONSOLE_RESTORE
);
186 if (!mc_global
.tty
.use_subshell
&& command
&& !(flags
& EXECUTE_INTERNAL
))
188 printf ("%s%s\n", mc_prompt
, command
);
191 #ifdef ENABLE_SUBSHELL
192 if (mc_global
.tty
.use_subshell
&& !(flags
& EXECUTE_INTERNAL
))
196 /* We don't care if it died, higher level takes care of this */
197 invoke_subshell (command
, VISIBLY
, old_vfs_dir_vpath
!= NULL
? NULL
: &new_dir_vpath
);
200 #endif /* ENABLE_SUBSHELL */
201 my_system (flags
, shell
, command
);
203 if (!(flags
& EXECUTE_INTERNAL
))
205 if ((pause_after_run
== pause_always
206 || (pause_after_run
== pause_on_dumb_terminals
&& !mc_global
.tty
.xterm_flag
207 && mc_global
.tty
.console_flag
== '\0')) && quit
== 0
208 #ifdef ENABLE_SUBSHELL
209 && subshell_state
!= RUNNING_COMMAND
210 #endif /* ENABLE_SUBSHELL */
213 printf (_("Press any key to continue..."));
220 if (mc_global
.tty
.console_flag
!= '\0')
222 if (output_lines
&& mc_global
.keybar_visible
)
230 if (mc_global
.tty
.console_flag
!= '\0')
231 handle_console (CONSOLE_SAVE
);
232 edition_post_exec ();
234 #ifdef ENABLE_SUBSHELL
235 if (new_dir_vpath
!= NULL
)
237 do_possible_cd (new_dir_vpath
);
238 vfs_path_free (new_dir_vpath
);
241 #endif /* ENABLE_SUBSHELL */
243 if (old_vfs_dir_vpath
!= NULL
)
245 mc_chdir (old_vfs_dir_vpath
);
246 vfs_path_free (old_vfs_dir_vpath
);
249 if (mc_global
.mc_run_mode
== MC_RUN_FULL
)
251 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
252 update_xterm_title_path ();
259 /* --------------------------------------------------------------------------------------------- */
261 /** Set up the terminal before executing a program */
270 /* --------------------------------------------------------------------------------------------- */
271 /** Hide the terminal after executing a program */
275 edition_post_exec ();
280 /* --------------------------------------------------------------------------------------------- */
281 /* Executes a command */
284 shell_execute (const char *command
, int flags
)
288 if (flags
& EXECUTE_HIDE
)
290 cmd
= g_strconcat (" ", command
, (char *) NULL
);
291 flags
^= EXECUTE_HIDE
;
294 #ifdef ENABLE_SUBSHELL
295 if (mc_global
.tty
.use_subshell
)
296 if (subshell_state
== INACTIVE
)
297 do_execute (mc_global
.tty
.shell
, cmd
? cmd
: command
, flags
| EXECUTE_AS_SHELL
);
299 message (D_ERROR
, MSG_ERROR
, _("The shell is already running a command"));
301 #endif /* ENABLE_SUBSHELL */
302 do_execute (mc_global
.tty
.shell
, cmd
? cmd
: command
, flags
| EXECUTE_AS_SHELL
);
307 /* --------------------------------------------------------------------------------------------- */
312 do_execute (mc_global
.tty
.shell
, 0, 0);
315 /* --------------------------------------------------------------------------------------------- */
320 #ifdef ENABLE_SUBSHELL
321 vfs_path_t
*new_dir_vpath
= NULL
;
322 vfs_path_t
**new_dir_p
;
323 #endif /* ENABLE_SUBSHELL */
325 SIG_ATOMIC_VOLATILE_T was_sigwinch
= 0;
329 if (clear_before_exec
)
331 if (mc_global
.tty
.alternate_plus_minus
)
332 numeric_keypad_mode ();
334 /* With slang we don't want any of this, since there
335 * is no raw_mode supported
337 tty_reset_shell_mode ();
338 #endif /* !HAVE_SLANG */
344 if (mc_global
.tty
.console_flag
!= '\0')
345 handle_console (CONSOLE_RESTORE
);
347 #ifdef ENABLE_SUBSHELL
348 if (mc_global
.tty
.use_subshell
)
350 new_dir_p
= vfs_current_is_local ()? &new_dir_vpath
: NULL
;
351 invoke_subshell (NULL
, VISIBLY
, new_dir_p
);
354 #endif /* ENABLE_SUBSHELL */
356 if (output_starts_shell
)
358 fprintf (stderr
, _("Type `exit' to return to the Midnight Commander"));
359 fprintf (stderr
, "\n\r\n\r");
361 my_system (EXECUTE_INTERNAL
, mc_global
.tty
.shell
, NULL
);
367 if (mc_global
.tty
.console_flag
!= '\0')
368 handle_console (CONSOLE_SAVE
);
372 tty_reset_prog_mode ();
375 /* Prevent screen flash when user did 'exit' or 'logout' within
377 if ((quit
& SUBSHELL_EXIT
) != 0)
379 /* User did `exit' or `logout': quit MC */
380 if (quiet_quit_cmd ())
384 #ifdef ENABLE_SUBSHELL
385 /* restart subshell */
386 if (mc_global
.tty
.use_subshell
)
388 #endif /* ENABLE_SUBSHELL */
393 if (mc_global
.tty
.alternate_plus_minus
)
394 application_keypad_mode ();
397 * Save sigwinch flag that will be reset in mc_refresh() called via update_panels().
398 * There is some problem with screen redraw in ncurses-based mc in this situation.
400 was_sigwinch
= mc_global
.tty
.winch_flag
;
401 mc_global
.tty
.winch_flag
= 0;
403 #ifdef ENABLE_SUBSHELL
404 if (mc_global
.tty
.use_subshell
)
407 if (new_dir_vpath
!= NULL
)
408 do_possible_cd (new_dir_vpath
);
409 if (mc_global
.tty
.console_flag
!= '\0' && output_lines
)
410 show_console_contents (output_start_y
,
411 LINES
- mc_global
.keybar_visible
- output_lines
-
412 1, LINES
- mc_global
.keybar_visible
- 1);
415 vfs_path_free (new_dir_vpath
);
416 #endif /* ENABLE_SUBSHELL */
418 if (mc_global
.mc_run_mode
== MC_RUN_FULL
)
420 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
421 update_xterm_title_path ();
424 if (was_sigwinch
!= 0 || mc_global
.tty
.winch_flag
!= 0)
425 dialog_change_screen_size ();
430 /* --------------------------------------------------------------------------------------------- */
434 execute_suspend (const gchar
* event_group_name
, const gchar
* event_name
,
435 gpointer init_data
, gpointer data
)
437 (void) event_group_name
;
442 if (mc_global
.mc_run_mode
== MC_RUN_FULL
)
445 if (mc_global
.mc_run_mode
== MC_RUN_FULL
)
446 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
452 /* --------------------------------------------------------------------------------------------- */
454 * Execute command on a filename that can be on VFS.
455 * Errors are reported to the user.
459 execute_with_vfs_arg (const char *command
, const vfs_path_t
* filename_vpath
)
463 vfs_path_t
*localcopy_vpath
;
465 /* Simplest case, this file is local */
466 if ((filename_vpath
== NULL
&& vfs_file_is_local (vfs_get_raw_current_dir ()))
467 || vfs_file_is_local (filename_vpath
))
469 do_execute (command
, vfs_path_get_last_path_str (filename_vpath
), EXECUTE_INTERNAL
);
473 /* FIXME: Creation of new files on VFS is not supported */
474 if (filename_vpath
== NULL
)
477 localcopy_vpath
= mc_getlocalcopy (filename_vpath
);
478 if (localcopy_vpath
== NULL
)
482 filename
= vfs_path_to_str (filename_vpath
);
483 message (D_ERROR
, MSG_ERROR
, _("Cannot fetch a local copy of %s"), filename
);
489 * filename can be an entry on panel, it can be changed by executing
490 * the command, so make a copy. Smarter VFS code would make the code
493 mc_stat (localcopy_vpath
, &st
);
495 do_execute (command
, vfs_path_get_last_path_str (localcopy_vpath
), EXECUTE_INTERNAL
);
496 mc_stat (localcopy_vpath
, &st
);
497 mc_ungetlocalcopy (filename_vpath
, localcopy_vpath
, mtime
!= st
.st_mtime
);
498 vfs_path_free (localcopy_vpath
);
501 /* --------------------------------------------------------------------------------------------- */