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. */
22 #include <mhl/memory.h>
23 #include <mhl/string.h>
30 #include "cons.saver.h"
39 edition_post_exec (void)
43 /* FIXME: Missing on slang endwin? */
47 keypad (stdscr
, TRUE
);
51 if (alternate_plus_minus
)
52 application_keypad_mode ();
57 edition_pre_exec (void)
59 if (clear_before_exec
)
62 if (!(console_flag
|| xterm_flag
))
70 keypad (stdscr
, FALSE
);
73 numeric_keypad_mode ();
75 /* on xterms: maybe endwin did not leave the terminal on the shell
76 * screen page: do it now.
78 * Do not move this before endwin: in some systems rmcup includes
79 * a call to clear screen, so it will end up clearing the shell screen.
85 /* Set up the terminal before executing a program */
95 do_execute (const char *shell
, const char *command
, int flags
)
97 #ifdef HAVE_SUBSHELL_SUPPORT
99 #endif /* HAVE_SUBSHELL_SUPPORT */
102 char *old_vfs_dir
= 0;
104 if (!vfs_current_is_local ())
105 old_vfs_dir
= g_strdup (vfs_get_current_dir ());
111 handle_console (CONSOLE_RESTORE
);
113 if (!use_subshell
&& command
&& !(flags
& EXECUTE_INTERNAL
)) {
114 printf ("%s%s\n", prompt
, command
);
117 #ifdef HAVE_SUBSHELL_SUPPORT
118 if (use_subshell
&& !(flags
& EXECUTE_INTERNAL
)) {
121 /* We don't care if it died, higher level takes care of this */
123 invoke_subshell (command
, VISIBLY
, old_vfs_dir
? 0 : &new_dir
);
125 invoke_subshell (command
, VISIBLY
, &new_dir
);
126 #endif /* !USE_VFS */
128 #endif /* HAVE_SUBSHELL_SUPPORT */
129 my_system (flags
, shell
, command
);
131 if (!(flags
& EXECUTE_INTERNAL
)) {
132 if ((pause_after_run
== pause_always
133 || (pause_after_run
== pause_on_dumb_terminals
&& !xterm_flag
134 && !console_flag
)) && !quit
135 #ifdef HAVE_SUBSHELL_SUPPORT
136 && subshell_state
!= RUNNING_COMMAND
137 #endif /* HAVE_SUBSHELL_SUPPORT */
139 printf (_("Press any key to continue..."));
147 if (output_lines
&& keybar_visible
) {
155 handle_console (CONSOLE_SAVE
);
156 edition_post_exec ();
158 #ifdef HAVE_SUBSHELL_SUPPORT
160 do_possible_cd (new_dir
);
162 #endif /* HAVE_SUBSHELL_SUPPORT */
166 mc_chdir (old_vfs_dir
);
167 g_free (old_vfs_dir
);
171 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
172 update_xterm_title_path ();
179 /* Executes a command */
181 shell_execute (const char *command
, int flags
)
185 if (flags
& EXECUTE_HIDE
) {
186 cmd
= g_strconcat (" ", command
, (char *) NULL
);
187 flags
^= EXECUTE_HIDE
;
190 #ifdef HAVE_SUBSHELL_SUPPORT
192 if (subshell_state
== INACTIVE
)
193 do_execute (shell
, cmd
? cmd
: command
, flags
| EXECUTE_AS_SHELL
);
195 message (D_ERROR
, MSG_ERROR
,
196 _(" The shell is already running a command "));
198 #endif /* HAVE_SUBSHELL_SUPPORT */
199 do_execute (shell
, cmd
? cmd
: command
, flags
| EXECUTE_AS_SHELL
);
208 do_execute (shell
, 0, 0);
215 #ifdef HAVE_SUBSHELL_SUPPORT
216 char *new_dir
= NULL
;
218 #endif /* HAVE_SUBSHELL_SUPPORT */
222 if (clear_before_exec
)
224 if (alternate_plus_minus
)
225 numeric_keypad_mode ();
227 /* With slang we don't want any of this, since there
228 * is no mc_raw_mode supported
232 #endif /* !HAVE_SLANG */
233 keypad (stdscr
, FALSE
);
238 handle_console (CONSOLE_RESTORE
);
240 #ifdef HAVE_SUBSHELL_SUPPORT
242 new_dir_p
= vfs_current_is_local ()? &new_dir
: NULL
;
243 if (invoke_subshell (NULL
, VISIBLY
, new_dir_p
))
244 quiet_quit_cmd (); /* User did `exit' or `logout': quit MC quietly */
246 #endif /* HAVE_SUBSHELL_SUPPORT */
248 if (output_starts_shell
) {
250 _("Type `exit' to return to the Midnight Commander"));
251 fprintf (stderr
, "\n\r\n\r");
253 my_system (EXECUTE_INTERNAL
, shell
, NULL
);
258 handle_console (CONSOLE_SAVE
);
263 keypad (stdscr
, TRUE
);
265 /* Prevent screen flash when user did 'exit' or 'logout' within
272 if (alternate_plus_minus
)
273 application_keypad_mode ();
275 #ifdef HAVE_SUBSHELL_SUPPORT
279 do_possible_cd (new_dir
);
280 if (console_flag
&& output_lines
)
281 show_console_contents (output_start_y
,
282 LINES
- keybar_visible
- output_lines
-
283 1, LINES
- keybar_visible
- 1);
285 #endif /* HAVE_SUBSHELL_SUPPORT */
287 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
288 update_xterm_title_path ();
294 do_suspend_cmd (void)
298 if (console_flag
&& !use_subshell
)
299 handle_console (CONSOLE_RESTORE
);
303 struct sigaction sigtstp_action
;
305 /* Make sure that the SIGTSTP below will suspend us directly,
306 without calling ncurses' SIGTSTP handler; we *don't* want
307 ncurses to redraw the screen immediately after the SIGCONT */
308 sigaction (SIGTSTP
, &startup_handler
, &sigtstp_action
);
310 kill (getpid (), SIGTSTP
);
312 /* Restore previous SIGTSTP action */
313 sigaction (SIGTSTP
, &sigtstp_action
, NULL
);
317 if (console_flag
&& !use_subshell
)
318 handle_console (CONSOLE_SAVE
);
320 edition_post_exec ();
329 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
335 * Execute command on a filename that can be on VFS.
336 * Errors are reported to the user.
339 execute_with_vfs_arg (const char *command
, const char *filename
)
346 /* Simplest case, this file is local */
347 if (!filename
|| vfs_file_is_local (filename
)) {
348 do_execute (command
, filename
, 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
);