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
28 #include "lib/global.h"
30 #include "lib/tty/tty.h"
31 #include "lib/tty/key.h"
32 #include "lib/tty/win.h"
33 #include "lib/vfs/vfs.h"
35 #include "lib/widget.h"
37 #include "filemanager/midnight.h"
38 #include "filemanager/layout.h" /* use_dash() */
39 #include "consaver/cons.saver.h"
41 #include "setup.h" /* clear_before_exec */
45 /*** global variables ****************************************************************************/
47 int pause_after_run
= pause_on_dumb_terminals
;
49 /*** file scope macro definitions ****************************************************************/
51 /*** file scope type declarations ****************************************************************/
53 /*** file scope variables ************************************************************************/
55 /*** file scope functions ************************************************************************/
56 /* --------------------------------------------------------------------------------------------- */
59 edition_post_exec (void)
63 /* FIXME: Missing on slang endwin? */
64 tty_reset_prog_mode ();
71 if (mc_global
.tty
.alternate_plus_minus
)
72 application_keypad_mode ();
75 /* --------------------------------------------------------------------------------------------- */
78 edition_pre_exec (void)
80 if (clear_before_exec
)
84 if (!(mc_global
.tty
.console_flag
!= '\0' || mc_global
.tty
.xterm_flag
))
91 tty_reset_shell_mode ();
95 numeric_keypad_mode ();
97 /* on xterms: maybe endwin did not leave the terminal on the shell
98 * screen page: do it now.
100 * Do not move this before endwin: in some systems rmcup includes
101 * a call to clear screen, so it will end up clearing the shell screen.
106 /* --------------------------------------------------------------------------------------------- */
108 #ifdef HAVE_SUBSHELL_SUPPORT
110 do_possible_cd (const char *new_dir
)
112 if (!do_cd (new_dir
, cd_exact
))
113 message (D_ERROR
, _("Warning"),
114 _("The Commander can't change to the directory that\n"
115 "the subshell claims you are in. Perhaps you have\n"
116 "deleted your working directory, or given yourself\n"
117 "extra access permissions with the \"su\" command?"));
119 #endif /* HAVE_SUBSHELL_SUPPORT */
121 /* --------------------------------------------------------------------------------------------- */
124 do_execute (const char *lc_shell
, const char *command
, int flags
)
126 #ifdef HAVE_SUBSHELL_SUPPORT
127 char *new_dir
= NULL
;
128 #endif /* HAVE_SUBSHELL_SUPPORT */
130 char *old_vfs_dir
= 0;
132 if (!vfs_current_is_local ())
133 old_vfs_dir
= vfs_get_current_dir ();
135 if (mc_global
.mc_run_mode
== MC_RUN_FULL
)
138 if (mc_global
.tty
.console_flag
!= '\0')
139 handle_console (CONSOLE_RESTORE
);
141 if (!mc_global
.tty
.use_subshell
&& command
&& !(flags
& EXECUTE_INTERNAL
))
143 printf ("%s%s\n", mc_prompt
, command
);
146 #ifdef HAVE_SUBSHELL_SUPPORT
147 if (mc_global
.tty
.use_subshell
&& !(flags
& EXECUTE_INTERNAL
))
151 /* We don't care if it died, higher level takes care of this */
152 invoke_subshell (command
, VISIBLY
, old_vfs_dir
? NULL
: &new_dir
);
155 #endif /* HAVE_SUBSHELL_SUPPORT */
156 my_system (flags
, lc_shell
, command
);
158 if (!(flags
& EXECUTE_INTERNAL
))
160 if ((pause_after_run
== pause_always
161 || (pause_after_run
== pause_on_dumb_terminals
&& !mc_global
.tty
.xterm_flag
162 && mc_global
.tty
.console_flag
== '\0')) && quit
== 0
163 #ifdef HAVE_SUBSHELL_SUPPORT
164 && subshell_state
!= RUNNING_COMMAND
165 #endif /* HAVE_SUBSHELL_SUPPORT */
168 printf (_("Press any key to continue..."));
175 if (mc_global
.tty
.console_flag
!= '\0')
177 if (output_lines
&& mc_global
.keybar_visible
)
185 if (mc_global
.tty
.console_flag
!= '\0')
186 handle_console (CONSOLE_SAVE
);
187 edition_post_exec ();
189 #ifdef HAVE_SUBSHELL_SUPPORT
191 do_possible_cd (new_dir
);
193 #endif /* HAVE_SUBSHELL_SUPPORT */
197 mc_chdir (old_vfs_dir
);
198 g_free (old_vfs_dir
);
201 if (mc_global
.mc_run_mode
== MC_RUN_FULL
)
203 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
204 update_xterm_title_path ();
211 /* --------------------------------------------------------------------------------------------- */
214 do_suspend_cmd (void)
218 if (mc_global
.tty
.console_flag
!= '\0' && !mc_global
.tty
.use_subshell
)
219 handle_console (CONSOLE_RESTORE
);
223 struct sigaction sigtstp_action
;
225 /* Make sure that the SIGTSTP below will suspend us directly,
226 without calling ncurses' SIGTSTP handler; we *don't* want
227 ncurses to redraw the screen immediately after the SIGCONT */
228 sigaction (SIGTSTP
, &startup_handler
, &sigtstp_action
);
230 kill (getpid (), SIGTSTP
);
232 /* Restore previous SIGTSTP action */
233 sigaction (SIGTSTP
, &sigtstp_action
, NULL
);
237 if (mc_global
.tty
.console_flag
!= '\0' && !mc_global
.tty
.use_subshell
)
238 handle_console (CONSOLE_SAVE
);
240 edition_post_exec ();
243 /* --------------------------------------------------------------------------------------------- */
244 /*** public functions ****************************************************************************/
245 /* --------------------------------------------------------------------------------------------- */
247 /** Set up the terminal before executing a program */
256 /* --------------------------------------------------------------------------------------------- */
257 /** Hide the terminal after executing a program */
261 edition_post_exec ();
266 /* --------------------------------------------------------------------------------------------- */
267 /* Executes a command */
270 shell_execute (const char *command
, int flags
)
274 if (flags
& EXECUTE_HIDE
)
276 cmd
= g_strconcat (" ", command
, (char *) NULL
);
277 flags
^= EXECUTE_HIDE
;
280 #ifdef HAVE_SUBSHELL_SUPPORT
281 if (mc_global
.tty
.use_subshell
)
282 if (subshell_state
== INACTIVE
)
283 do_execute (shell
, cmd
? cmd
: command
, flags
| EXECUTE_AS_SHELL
);
285 message (D_ERROR
, MSG_ERROR
, _("The shell is already running a command"));
287 #endif /* HAVE_SUBSHELL_SUPPORT */
288 do_execute (shell
, cmd
? cmd
: command
, flags
| EXECUTE_AS_SHELL
);
293 /* --------------------------------------------------------------------------------------------- */
298 do_execute (shell
, 0, 0);
301 /* --------------------------------------------------------------------------------------------- */
306 #ifdef HAVE_SUBSHELL_SUPPORT
307 char *new_dir
= NULL
;
309 #endif /* HAVE_SUBSHELL_SUPPORT */
313 if (clear_before_exec
)
315 if (mc_global
.tty
.alternate_plus_minus
)
316 numeric_keypad_mode ();
318 /* With slang we don't want any of this, since there
319 * is no raw_mode supported
321 tty_reset_shell_mode ();
322 #endif /* !HAVE_SLANG */
328 if (mc_global
.tty
.console_flag
!= '\0')
329 handle_console (CONSOLE_RESTORE
);
331 #ifdef HAVE_SUBSHELL_SUPPORT
332 if (mc_global
.tty
.use_subshell
)
334 new_dir_p
= vfs_current_is_local ()? &new_dir
: NULL
;
335 invoke_subshell (NULL
, VISIBLY
, new_dir_p
);
338 #endif /* HAVE_SUBSHELL_SUPPORT */
340 if (output_starts_shell
)
342 fprintf (stderr
, _("Type `exit' to return to the Midnight Commander"));
343 fprintf (stderr
, "\n\r\n\r");
345 my_system (EXECUTE_INTERNAL
, shell
, NULL
);
351 if (mc_global
.tty
.console_flag
!= '\0')
352 handle_console (CONSOLE_SAVE
);
356 tty_reset_prog_mode ();
359 /* Prevent screen flash when user did 'exit' or 'logout' within
361 if ((quit
& SUBSHELL_EXIT
) != 0)
363 /* User did `exit' or `logout': quit MC */
364 if (quiet_quit_cmd ())
368 #ifdef HAVE_SUBSHELL_SUPPORT
369 /* restart subshell */
370 if (mc_global
.tty
.use_subshell
)
372 #endif /* HAVE_SUBSHELL_SUPPORT */
377 if (mc_global
.tty
.alternate_plus_minus
)
378 application_keypad_mode ();
380 #ifdef HAVE_SUBSHELL_SUPPORT
381 if (mc_global
.tty
.use_subshell
)
383 load_prompt (0, NULL
);
385 do_possible_cd (new_dir
);
386 if (mc_global
.tty
.console_flag
!= '\0' && output_lines
)
387 show_console_contents (output_start_y
,
388 LINES
- mc_global
.keybar_visible
- output_lines
-
389 1, LINES
- mc_global
.keybar_visible
- 1);
391 #endif /* HAVE_SUBSHELL_SUPPORT */
393 if (mc_global
.mc_run_mode
== MC_RUN_FULL
)
395 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
396 update_xterm_title_path ();
401 /* --------------------------------------------------------------------------------------------- */
405 execute_suspend (const gchar
* event_group_name
, const gchar
* event_name
,
406 gpointer init_data
, gpointer data
)
408 (void) event_group_name
;
413 if (mc_global
.mc_run_mode
== MC_RUN_FULL
)
416 if (mc_global
.mc_run_mode
== MC_RUN_FULL
)
417 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
423 /* --------------------------------------------------------------------------------------------- */
425 * Execute command on a filename that can be on VFS.
426 * Errors are reported to the user.
430 execute_with_vfs_arg (const char *command
, const char *filename
)
436 vfs_path_t
*vpath
= vfs_path_from_str (filename
);
438 /* Simplest case, this file is local */
439 if (!filename
|| vfs_file_is_local (vpath
))
441 fn
= vfs_path_to_str (vpath
);
442 do_execute (command
, fn
, EXECUTE_INTERNAL
);
444 vfs_path_free (vpath
);
447 vfs_path_free (vpath
);
449 /* FIXME: Creation of new files on VFS is not supported */
453 localcopy
= mc_getlocalcopy (filename
);
454 if (localcopy
== NULL
)
456 message (D_ERROR
, MSG_ERROR
, _("Cannot fetch a local copy of %s"), filename
);
461 * filename can be an entry on panel, it can be changed by executing
462 * the command, so make a copy. Smarter VFS code would make the code
465 fn
= g_strdup (filename
);
466 mc_stat (localcopy
, &st
);
468 do_execute (command
, localcopy
, EXECUTE_INTERNAL
);
469 mc_stat (localcopy
, &st
);
470 mc_ungetlocalcopy (fn
, localcopy
, mtime
!= st
.st_mtime
);
475 /* --------------------------------------------------------------------------------------------- */