1 /* Execution routines for GNU Midnight Commander
2 Copyright (C) 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 * \brief Source: execution routines
30 #include "../src/tty/tty.h"
31 #include "../src/tty/key.h"
32 #include "../src/tty/win.h"
35 #include "cons.saver.h"
41 #include "../vfs/vfs.h"
45 edition_post_exec (void)
49 /* FIXME: Missing on slang endwin? */
50 tty_reset_prog_mode ();
57 if (alternate_plus_minus
)
58 application_keypad_mode ();
63 edition_pre_exec (void)
65 if (clear_before_exec
)
68 if (!(console_flag
|| xterm_flag
))
75 tty_reset_shell_mode ();
79 numeric_keypad_mode ();
81 /* on xterms: maybe endwin did not leave the terminal on the shell
82 * screen page: do it now.
84 * Do not move this before endwin: in some systems rmcup includes
85 * a call to clear screen, so it will end up clearing the shell screen.
91 /* Set up the terminal before executing a program */
101 do_execute (const char *shell
, const char *command
, int flags
)
103 #ifdef HAVE_SUBSHELL_SUPPORT
104 char *new_dir
= NULL
;
105 #endif /* HAVE_SUBSHELL_SUPPORT */
108 char *old_vfs_dir
= 0;
110 if (!vfs_current_is_local ())
111 old_vfs_dir
= g_strdup (vfs_get_current_dir ());
117 handle_console (CONSOLE_RESTORE
);
119 if (!use_subshell
&& command
&& !(flags
& EXECUTE_INTERNAL
)) {
120 printf ("%s%s\n", prompt
, command
);
123 #ifdef HAVE_SUBSHELL_SUPPORT
124 if (use_subshell
&& !(flags
& EXECUTE_INTERNAL
)) {
127 /* We don't care if it died, higher level takes care of this */
129 invoke_subshell (command
, VISIBLY
, old_vfs_dir
? NULL
: &new_dir
);
131 invoke_subshell (command
, VISIBLY
, &new_dir
);
132 #endif /* !USE_VFS */
134 #endif /* HAVE_SUBSHELL_SUPPORT */
135 my_system (flags
, shell
, command
);
137 if (!(flags
& EXECUTE_INTERNAL
)) {
138 if ((pause_after_run
== pause_always
139 || (pause_after_run
== pause_on_dumb_terminals
&& !xterm_flag
140 && !console_flag
)) && !quit
141 #ifdef HAVE_SUBSHELL_SUPPORT
142 && subshell_state
!= RUNNING_COMMAND
143 #endif /* HAVE_SUBSHELL_SUPPORT */
145 printf (_("Press any key to continue..."));
153 if (output_lines
&& keybar_visible
) {
161 handle_console (CONSOLE_SAVE
);
162 edition_post_exec ();
164 #ifdef HAVE_SUBSHELL_SUPPORT
166 do_possible_cd (new_dir
);
168 #endif /* HAVE_SUBSHELL_SUPPORT */
172 mc_chdir (old_vfs_dir
);
173 g_free (old_vfs_dir
);
177 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
178 update_xterm_title_path ();
185 /* Executes a command */
187 shell_execute (const char *command
, int flags
)
191 if (flags
& EXECUTE_HIDE
) {
192 cmd
= g_strconcat (" ", command
, (char *) NULL
);
193 flags
^= EXECUTE_HIDE
;
196 #ifdef HAVE_SUBSHELL_SUPPORT
198 if (subshell_state
== INACTIVE
)
199 do_execute (shell
, cmd
? cmd
: command
, flags
| EXECUTE_AS_SHELL
);
201 message (D_ERROR
, MSG_ERROR
,
202 _(" The shell is already running a command "));
204 #endif /* HAVE_SUBSHELL_SUPPORT */
205 do_execute (shell
, cmd
? cmd
: command
, flags
| EXECUTE_AS_SHELL
);
214 do_execute (shell
, 0, 0);
221 #ifdef HAVE_SUBSHELL_SUPPORT
222 char *new_dir
= NULL
;
224 #endif /* HAVE_SUBSHELL_SUPPORT */
228 if (clear_before_exec
)
230 if (alternate_plus_minus
)
231 numeric_keypad_mode ();
233 /* With slang we don't want any of this, since there
234 * is no tty_raw_mode supported
236 tty_reset_shell_mode ();
237 #endif /* !HAVE_SLANG */
244 handle_console (CONSOLE_RESTORE
);
246 #ifdef HAVE_SUBSHELL_SUPPORT
248 new_dir_p
= vfs_current_is_local ()? &new_dir
: NULL
;
249 if (invoke_subshell (NULL
, VISIBLY
, new_dir_p
))
250 quiet_quit_cmd (); /* User did `exit' or `logout': quit MC quietly */
252 #endif /* HAVE_SUBSHELL_SUPPORT */
254 if (output_starts_shell
) {
256 _("Type `exit' to return to the Midnight Commander"));
257 fprintf (stderr
, "\n\r\n\r");
259 my_system (EXECUTE_INTERNAL
, shell
, NULL
);
264 handle_console (CONSOLE_SAVE
);
268 tty_reset_prog_mode ();
271 /* Prevent screen flash when user did 'exit' or 'logout' within
278 if (alternate_plus_minus
)
279 application_keypad_mode ();
281 #ifdef HAVE_SUBSHELL_SUPPORT
285 do_possible_cd (new_dir
);
286 if (console_flag
&& output_lines
)
287 show_console_contents (output_start_y
,
288 LINES
- keybar_visible
- output_lines
-
289 1, LINES
- keybar_visible
- 1);
291 #endif /* HAVE_SUBSHELL_SUPPORT */
293 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
294 update_xterm_title_path ();
300 do_suspend_cmd (void)
304 if (console_flag
&& !use_subshell
)
305 handle_console (CONSOLE_RESTORE
);
309 struct sigaction sigtstp_action
;
311 /* Make sure that the SIGTSTP below will suspend us directly,
312 without calling ncurses' SIGTSTP handler; we *don't* want
313 ncurses to redraw the screen immediately after the SIGCONT */
314 sigaction (SIGTSTP
, &startup_handler
, &sigtstp_action
);
316 kill (getpid (), SIGTSTP
);
318 /* Restore previous SIGTSTP action */
319 sigaction (SIGTSTP
, &sigtstp_action
, NULL
);
323 if (console_flag
&& !use_subshell
)
324 handle_console (CONSOLE_SAVE
);
326 edition_post_exec ();
335 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
341 * Execute command on a filename that can be on VFS.
342 * Errors are reported to the user.
345 execute_with_vfs_arg (const char *command
, const char *filename
)
352 /* Simplest case, this file is local */
353 if (!filename
|| vfs_file_is_local (filename
)) {
354 fn
= vfs_canon_and_translate (filename
);
355 do_execute (command
, fn
, EXECUTE_INTERNAL
);
360 /* FIXME: Creation of new files on VFS is not supported */
364 localcopy
= mc_getlocalcopy (filename
);
365 if (localcopy
== NULL
) {
366 message (D_ERROR
, MSG_ERROR
, _(" Cannot fetch a local copy of %s "),
372 * filename can be an entry on panel, it can be changed by executing
373 * the command, so make a copy. Smarter VFS code would make the code
376 fn
= g_strdup (filename
);
377 mc_stat (localcopy
, &st
);
379 do_execute (command
, localcopy
, EXECUTE_INTERNAL
);
380 mc_stat (localcopy
, &st
);
381 mc_ungetlocalcopy (fn
, localcopy
, mtime
!= st
.st_mtime
);