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. */
27 #include "cons.saver.h"
33 #include "../vfs/vfs.h"
37 edition_post_exec (void)
41 /* FIXME: Missing on slang endwin? */
45 keypad (stdscr
, TRUE
);
49 if (alternate_plus_minus
)
50 application_keypad_mode ();
55 edition_pre_exec (void)
57 if (clear_before_exec
)
60 if (!(console_flag
|| xterm_flag
))
68 keypad (stdscr
, FALSE
);
71 numeric_keypad_mode ();
73 /* on xterms: maybe endwin did not leave the terminal on the shell
74 * screen page: do it now.
76 * Do not move this before endwin: in some systems rmcup includes
77 * a call to clear screen, so it will end up clearing the shell screen.
83 /* Set up the terminal before executing a program */
93 do_execute (const char *shell
, const char *command
, int flags
)
95 #ifdef HAVE_SUBSHELL_SUPPORT
97 #endif /* HAVE_SUBSHELL_SUPPORT */
100 char *old_vfs_dir
= 0;
102 if (!vfs_current_is_local ())
103 old_vfs_dir
= g_strdup (vfs_get_current_dir ());
109 handle_console (CONSOLE_RESTORE
);
111 if (!use_subshell
&& command
&& !(flags
& EXECUTE_INTERNAL
)) {
112 printf ("%s%s\n", prompt
, command
);
115 #ifdef HAVE_SUBSHELL_SUPPORT
116 if (use_subshell
&& !(flags
& EXECUTE_INTERNAL
)) {
119 /* We don't care if it died, higher level takes care of this */
121 invoke_subshell (command
, VISIBLY
, old_vfs_dir
? 0 : &new_dir
);
123 invoke_subshell (command
, VISIBLY
, &new_dir
);
124 #endif /* !USE_VFS */
126 #endif /* HAVE_SUBSHELL_SUPPORT */
127 my_system (flags
, shell
, command
);
129 if (!(flags
& EXECUTE_INTERNAL
)) {
130 if ((pause_after_run
== pause_always
131 || (pause_after_run
== pause_on_dumb_terminals
&& !xterm_flag
132 && !console_flag
)) && !quit
133 #ifdef HAVE_SUBSHELL_SUPPORT
134 && subshell_state
!= RUNNING_COMMAND
135 #endif /* HAVE_SUBSHELL_SUPPORT */
137 printf (_("Press any key to continue..."));
145 if (output_lines
&& keybar_visible
) {
153 handle_console (CONSOLE_SAVE
);
154 edition_post_exec ();
156 #ifdef HAVE_SUBSHELL_SUPPORT
158 do_possible_cd (new_dir
);
160 #endif /* HAVE_SUBSHELL_SUPPORT */
164 mc_chdir (old_vfs_dir
);
165 g_free (old_vfs_dir
);
169 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
170 update_xterm_title_path ();
177 /* Executes a command */
179 shell_execute (const char *command
, int flags
)
183 if (flags
& EXECUTE_HIDE
) {
184 cmd
= g_strconcat (" ", command
, (char *) NULL
);
185 flags
^= EXECUTE_HIDE
;
188 #ifdef HAVE_SUBSHELL_SUPPORT
190 if (subshell_state
== INACTIVE
)
191 do_execute (shell
, cmd
? cmd
: command
, flags
| EXECUTE_AS_SHELL
);
193 message (D_ERROR
, MSG_ERROR
,
194 _(" The shell is already running a command "));
196 #endif /* HAVE_SUBSHELL_SUPPORT */
197 do_execute (shell
, cmd
? cmd
: command
, flags
| EXECUTE_AS_SHELL
);
206 do_execute (shell
, 0, 0);
213 #ifdef HAVE_SUBSHELL_SUPPORT
214 char *new_dir
= NULL
;
216 #endif /* HAVE_SUBSHELL_SUPPORT */
220 if (clear_before_exec
)
222 if (alternate_plus_minus
)
223 numeric_keypad_mode ();
225 /* With slang we don't want any of this, since there
226 * is no mc_raw_mode supported
230 #endif /* !HAVE_SLANG */
231 keypad (stdscr
, FALSE
);
236 handle_console (CONSOLE_RESTORE
);
238 #ifdef HAVE_SUBSHELL_SUPPORT
240 new_dir_p
= vfs_current_is_local ()? &new_dir
: NULL
;
241 if (invoke_subshell (NULL
, VISIBLY
, new_dir_p
))
242 quiet_quit_cmd (); /* User did `exit' or `logout': quit MC quietly */
244 #endif /* HAVE_SUBSHELL_SUPPORT */
246 if (output_starts_shell
) {
248 _("Type `exit' to return to the Midnight Commander"));
249 fprintf (stderr
, "\n\r\n\r");
251 my_system (EXECUTE_INTERNAL
, shell
, NULL
);
256 handle_console (CONSOLE_SAVE
);
261 keypad (stdscr
, TRUE
);
263 /* Prevent screen flash when user did 'exit' or 'logout' within
270 if (alternate_plus_minus
)
271 application_keypad_mode ();
273 #ifdef HAVE_SUBSHELL_SUPPORT
277 do_possible_cd (new_dir
);
278 if (console_flag
&& output_lines
)
279 show_console_contents (output_start_y
,
280 LINES
- keybar_visible
- output_lines
-
281 1, LINES
- keybar_visible
- 1);
283 #endif /* HAVE_SUBSHELL_SUPPORT */
285 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
286 update_xterm_title_path ();
292 do_suspend_cmd (void)
296 if (console_flag
&& !use_subshell
)
297 handle_console (CONSOLE_RESTORE
);
301 struct sigaction sigtstp_action
;
303 /* Make sure that the SIGTSTP below will suspend us directly,
304 without calling ncurses' SIGTSTP handler; we *don't* want
305 ncurses to redraw the screen immediately after the SIGCONT */
306 sigaction (SIGTSTP
, &startup_handler
, &sigtstp_action
);
308 kill (getpid (), SIGTSTP
);
310 /* Restore previous SIGTSTP action */
311 sigaction (SIGTSTP
, &sigtstp_action
, NULL
);
315 if (console_flag
&& !use_subshell
)
316 handle_console (CONSOLE_SAVE
);
318 edition_post_exec ();
327 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
333 * Execute command on a filename that can be on VFS.
334 * Errors are reported to the user.
337 execute_with_vfs_arg (const char *command
, const char *filename
)
344 /* Simplest case, this file is local */
345 if (!filename
|| vfs_file_is_local (filename
)) {
346 fn
= vfs_canon_and_translate (filename
);
347 do_execute (command
, fn
, EXECUTE_INTERNAL
);
352 /* FIXME: Creation of new files on VFS is not supported */
356 localcopy
= mc_getlocalcopy (filename
);
357 if (localcopy
== NULL
) {
358 message (D_ERROR
, MSG_ERROR
, _(" Cannot fetch a local copy of %s "),
364 * filename can be an entry on panel, it can be changed by executing
365 * the command, so make a copy. Smarter VFS code would make the code
368 fn
= g_strdup (filename
);
369 mc_stat (localcopy
, &st
);
371 do_execute (command
, localcopy
, EXECUTE_INTERNAL
);
372 mc_stat (localcopy
, &st
);
373 mc_ungetlocalcopy (fn
, localcopy
, mtime
!= st
.st_mtime
);