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"
40 #include "panel.h" /* update_panels() */
42 #include "../vfs/vfs.h"
46 edition_post_exec (void)
50 /* FIXME: Missing on slang endwin? */
51 tty_reset_prog_mode ();
58 if (alternate_plus_minus
)
59 application_keypad_mode ();
64 edition_pre_exec (void)
66 if (clear_before_exec
)
69 if (!(console_flag
|| xterm_flag
))
76 tty_reset_shell_mode ();
80 numeric_keypad_mode ();
82 /* on xterms: maybe endwin did not leave the terminal on the shell
83 * screen page: do it now.
85 * Do not move this before endwin: in some systems rmcup includes
86 * a call to clear screen, so it will end up clearing the shell screen.
92 /* Set up the terminal before executing a program */
101 #ifdef HAVE_SUBSHELL_SUPPORT
103 do_possible_cd (const char *new_dir
)
105 if (!do_cd (new_dir
, cd_exact
))
106 message (D_ERROR
, _("Warning"),
107 _(" The Commander can't change to the directory that \n"
108 " the subshell claims you are in. Perhaps you have \n"
109 " deleted your working directory, or given yourself \n"
110 " extra access permissions with the \"su\" command? "));
112 #endif /* HAVE_SUBSHELL_SUPPORT */
115 do_execute (const char *lc_shell
, const char *command
, int flags
)
117 #ifdef HAVE_SUBSHELL_SUPPORT
118 char *new_dir
= NULL
;
119 #endif /* HAVE_SUBSHELL_SUPPORT */
122 char *old_vfs_dir
= 0;
124 if (!vfs_current_is_local ())
125 old_vfs_dir
= g_strdup (vfs_get_current_dir ());
126 #endif /* ENABLE_VFS */
131 handle_console (CONSOLE_RESTORE
);
133 if (!use_subshell
&& command
&& !(flags
& EXECUTE_INTERNAL
)) {
134 printf ("%s%s\n", prompt
, command
);
137 #ifdef HAVE_SUBSHELL_SUPPORT
138 if (use_subshell
&& !(flags
& EXECUTE_INTERNAL
)) {
141 /* We don't care if it died, higher level takes care of this */
143 invoke_subshell (command
, VISIBLY
, old_vfs_dir
? NULL
: &new_dir
);
145 invoke_subshell (command
, VISIBLY
, &new_dir
);
146 #endif /* !ENABLE_VFS */
148 #endif /* HAVE_SUBSHELL_SUPPORT */
149 my_system (flags
, lc_shell
, command
);
151 if (!(flags
& EXECUTE_INTERNAL
)) {
152 if ((pause_after_run
== pause_always
153 || (pause_after_run
== pause_on_dumb_terminals
&& !xterm_flag
154 && !console_flag
)) && !quit
155 #ifdef HAVE_SUBSHELL_SUPPORT
156 && subshell_state
!= RUNNING_COMMAND
157 #endif /* HAVE_SUBSHELL_SUPPORT */
159 printf (_("Press any key to continue..."));
167 if (output_lines
&& keybar_visible
) {
175 handle_console (CONSOLE_SAVE
);
176 edition_post_exec ();
178 #ifdef HAVE_SUBSHELL_SUPPORT
180 do_possible_cd (new_dir
);
182 #endif /* HAVE_SUBSHELL_SUPPORT */
186 mc_chdir (old_vfs_dir
);
187 g_free (old_vfs_dir
);
189 #endif /* ENABLE_VFS */
191 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
192 update_xterm_title_path ();
199 /* Executes a command */
201 shell_execute (const char *command
, int flags
)
205 if (flags
& EXECUTE_HIDE
) {
206 cmd
= g_strconcat (" ", command
, (char *) NULL
);
207 flags
^= EXECUTE_HIDE
;
210 #ifdef HAVE_SUBSHELL_SUPPORT
212 if (subshell_state
== INACTIVE
)
213 do_execute (shell
, cmd
? cmd
: command
, flags
| EXECUTE_AS_SHELL
);
215 message (D_ERROR
, MSG_ERROR
,
216 _(" The shell is already running a command "));
218 #endif /* HAVE_SUBSHELL_SUPPORT */
219 do_execute (shell
, cmd
? cmd
: command
, flags
| EXECUTE_AS_SHELL
);
228 do_execute (shell
, 0, 0);
235 #ifdef HAVE_SUBSHELL_SUPPORT
236 char *new_dir
= NULL
;
238 #endif /* HAVE_SUBSHELL_SUPPORT */
242 if (clear_before_exec
)
244 if (alternate_plus_minus
)
245 numeric_keypad_mode ();
247 /* With slang we don't want any of this, since there
248 * is no raw_mode supported
250 tty_reset_shell_mode ();
251 #endif /* !HAVE_SLANG */
258 handle_console (CONSOLE_RESTORE
);
260 #ifdef HAVE_SUBSHELL_SUPPORT
262 new_dir_p
= vfs_current_is_local ()? &new_dir
: NULL
;
263 if (invoke_subshell (NULL
, VISIBLY
, new_dir_p
))
264 quiet_quit_cmd (); /* User did `exit' or `logout': quit MC quietly */
266 #endif /* HAVE_SUBSHELL_SUPPORT */
268 if (output_starts_shell
) {
270 _("Type `exit' to return to the Midnight Commander"));
271 fprintf (stderr
, "\n\r\n\r");
273 my_system (EXECUTE_INTERNAL
, shell
, NULL
);
278 handle_console (CONSOLE_SAVE
);
282 tty_reset_prog_mode ();
285 /* Prevent screen flash when user did 'exit' or 'logout' within
292 if (alternate_plus_minus
)
293 application_keypad_mode ();
295 #ifdef HAVE_SUBSHELL_SUPPORT
299 do_possible_cd (new_dir
);
300 if (console_flag
&& output_lines
)
301 show_console_contents (output_start_y
,
302 LINES
- keybar_visible
- output_lines
-
303 1, LINES
- keybar_visible
- 1);
305 #endif /* HAVE_SUBSHELL_SUPPORT */
307 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
308 update_xterm_title_path ();
314 do_suspend_cmd (void)
318 if (console_flag
&& !use_subshell
)
319 handle_console (CONSOLE_RESTORE
);
323 struct sigaction sigtstp_action
;
325 /* Make sure that the SIGTSTP below will suspend us directly,
326 without calling ncurses' SIGTSTP handler; we *don't* want
327 ncurses to redraw the screen immediately after the SIGCONT */
328 sigaction (SIGTSTP
, &startup_handler
, &sigtstp_action
);
330 kill (getpid (), SIGTSTP
);
332 /* Restore previous SIGTSTP action */
333 sigaction (SIGTSTP
, &sigtstp_action
, NULL
);
337 if (console_flag
&& !use_subshell
)
338 handle_console (CONSOLE_SAVE
);
340 edition_post_exec ();
349 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
355 * Execute command on a filename that can be on VFS.
356 * Errors are reported to the user.
359 execute_with_vfs_arg (const char *command
, const char *filename
)
366 /* Simplest case, this file is local */
367 if (!filename
|| vfs_file_is_local (filename
)) {
368 fn
= vfs_canon_and_translate (filename
);
369 do_execute (command
, fn
, EXECUTE_INTERNAL
);
374 /* FIXME: Creation of new files on VFS is not supported */
378 localcopy
= mc_getlocalcopy (filename
);
379 if (localcopy
== NULL
) {
380 message (D_ERROR
, MSG_ERROR
, _(" Cannot fetch a local copy of %s "),
386 * filename can be an entry on panel, it can be changed by executing
387 * the command, so make a copy. Smarter VFS code would make the code
390 fn
= g_strdup (filename
);
391 mc_stat (localcopy
, &st
);
393 do_execute (command
, localcopy
, EXECUTE_INTERNAL
);
394 mc_stat (localcopy
, &st
);
395 mc_ungetlocalcopy (fn
, localcopy
, mtime
!= st
.st_mtime
);