Value accuracy of mouse_enabled global variable.
[midnight-commander.git] / src / execute.c
blob06bad56ecf9568446aac638aa5a1d2e4a6f702cc
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. */
18 /** \file execute.c
19 * \brief Source: execution routines
22 #include <config.h>
24 #include <signal.h>
25 #include <sys/stat.h>
26 #include <sys/time.h>
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"
34 #include "lib/util.h"
35 #include "lib/widget.h"
37 #include "filemanager/midnight.h"
38 #include "filemanager/layout.h" /* use_dash() */
39 #include "consaver/cons.saver.h"
40 #include "subshell.h"
41 #include "setup.h" /* clear_before_exec */
43 #include "execute.h"
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 /* --------------------------------------------------------------------------------------------- */
58 static void
59 edition_post_exec (void)
61 do_enter_ca_mode ();
63 /* FIXME: Missing on slang endwin? */
64 tty_reset_prog_mode ();
65 tty_flush_input ();
67 tty_keypad (TRUE);
68 tty_raw_mode ();
69 channels_up ();
70 enable_mouse ();
71 if (alternate_plus_minus)
72 application_keypad_mode ();
75 /* --------------------------------------------------------------------------------------------- */
77 static void
78 edition_pre_exec (void)
80 if (clear_before_exec)
81 clr_scr ();
82 else
84 if (!(mc_global.tty.console_flag || mc_global.tty.xterm_flag))
85 printf ("\n\n");
88 channels_down ();
89 disable_mouse ();
91 tty_reset_shell_mode ();
92 tty_keypad (FALSE);
93 tty_reset_screen ();
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.
103 do_exit_ca_mode ();
106 /* --------------------------------------------------------------------------------------------- */
108 #ifdef HAVE_SUBSHELL_SUPPORT
109 static void
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 /* --------------------------------------------------------------------------------------------- */
123 static void
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)
136 save_cwds_stat ();
137 pre_exec ();
138 if (mc_global.tty.console_flag)
139 handle_console (CONSOLE_RESTORE);
141 if (!mc_global.tty.use_subshell && command && !(flags & EXECUTE_INTERNAL))
143 printf ("%s%s\n", mc_prompt, command);
144 fflush (stdout);
146 #ifdef HAVE_SUBSHELL_SUPPORT
147 if (mc_global.tty.use_subshell && !(flags & EXECUTE_INTERNAL))
149 do_update_prompt ();
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);
154 else
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)) && quit == 0
163 #ifdef HAVE_SUBSHELL_SUPPORT
164 && subshell_state != RUNNING_COMMAND
165 #endif /* HAVE_SUBSHELL_SUPPORT */
168 printf (_("Press any key to continue..."));
169 fflush (stdout);
170 tty_raw_mode ();
171 get_key_code (0);
172 printf ("\r\n");
173 fflush (stdout);
175 if (mc_global.tty.console_flag)
177 if (output_lines && mc_global.keybar_visible)
179 putchar ('\n');
180 fflush (stdout);
185 if (mc_global.tty.console_flag)
186 handle_console (CONSOLE_SAVE);
187 edition_post_exec ();
189 #ifdef HAVE_SUBSHELL_SUPPORT
190 if (new_dir)
191 do_possible_cd (new_dir);
193 #endif /* HAVE_SUBSHELL_SUPPORT */
195 if (old_vfs_dir)
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 ();
207 do_refresh ();
208 use_dash (TRUE);
211 /* --------------------------------------------------------------------------------------------- */
213 static void
214 do_suspend_cmd (void)
216 pre_exec ();
218 if (mc_global.tty.console_flag && !mc_global.tty.use_subshell)
219 handle_console (CONSOLE_RESTORE);
221 #ifdef SIGTSTP
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);
235 #endif /* SIGTSTP */
237 if (mc_global.tty.console_flag && !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 */
249 void
250 pre_exec (void)
252 use_dash (FALSE);
253 edition_pre_exec ();
256 /* --------------------------------------------------------------------------------------------- */
257 /** Hide the terminal after executing a program */
258 void
259 post_exec (void)
261 edition_post_exec ();
262 use_dash (TRUE);
265 /* --------------------------------------------------------------------------------------------- */
266 /* Executes a command */
268 void
269 shell_execute (const char *command, int flags)
271 char *cmd = NULL;
273 if (flags & EXECUTE_HIDE)
275 cmd = g_strconcat (" ", command, (char *) NULL);
276 flags ^= EXECUTE_HIDE;
279 #ifdef HAVE_SUBSHELL_SUPPORT
280 if (mc_global.tty.use_subshell)
281 if (subshell_state == INACTIVE)
282 do_execute (shell, cmd ? cmd : command, flags | EXECUTE_AS_SHELL);
283 else
284 message (D_ERROR, MSG_ERROR, _("The shell is already running a command"));
285 else
286 #endif /* HAVE_SUBSHELL_SUPPORT */
287 do_execute (shell, cmd ? cmd : command, flags | EXECUTE_AS_SHELL);
289 g_free (cmd);
292 /* --------------------------------------------------------------------------------------------- */
294 void
295 exec_shell (void)
297 do_execute (shell, 0, 0);
300 /* --------------------------------------------------------------------------------------------- */
302 void
303 toggle_panels (void)
305 #ifdef HAVE_SUBSHELL_SUPPORT
306 char *new_dir = NULL;
307 char **new_dir_p;
308 #endif /* HAVE_SUBSHELL_SUPPORT */
310 channels_down ();
311 disable_mouse ();
312 if (clear_before_exec)
313 clr_scr ();
314 if (alternate_plus_minus)
315 numeric_keypad_mode ();
316 #ifndef HAVE_SLANG
317 /* With slang we don't want any of this, since there
318 * is no raw_mode supported
320 tty_reset_shell_mode ();
321 #endif /* !HAVE_SLANG */
322 tty_noecho ();
323 tty_keypad (FALSE);
324 tty_reset_screen ();
325 do_exit_ca_mode ();
326 tty_raw_mode ();
327 if (mc_global.tty.console_flag)
328 handle_console (CONSOLE_RESTORE);
330 #ifdef HAVE_SUBSHELL_SUPPORT
331 if (mc_global.tty.use_subshell)
333 new_dir_p = vfs_current_is_local ()? &new_dir : NULL;
334 invoke_subshell (NULL, VISIBLY, new_dir_p);
336 else
337 #endif /* HAVE_SUBSHELL_SUPPORT */
339 if (output_starts_shell)
341 fprintf (stderr, _("Type `exit' to return to the Midnight Commander"));
342 fprintf (stderr, "\n\r\n\r");
344 my_system (EXECUTE_INTERNAL, shell, NULL);
346 else
347 get_key_code (0);
350 if (mc_global.tty.console_flag)
351 handle_console (CONSOLE_SAVE);
353 do_enter_ca_mode ();
355 tty_reset_prog_mode ();
356 tty_keypad (TRUE);
358 /* Prevent screen flash when user did 'exit' or 'logout' within
359 subshell */
360 if ((quit & SUBSHELL_EXIT) != 0)
362 /* User did `exit' or `logout': quit MC */
363 if (quiet_quit_cmd ())
364 return;
366 quit = 0;
367 #ifdef HAVE_SUBSHELL_SUPPORT
368 /* restart subshell */
369 if (mc_global.tty.use_subshell)
370 init_subshell ();
371 #endif /* HAVE_SUBSHELL_SUPPORT */
374 enable_mouse ();
375 channels_up ();
376 if (alternate_plus_minus)
377 application_keypad_mode ();
379 #ifdef HAVE_SUBSHELL_SUPPORT
380 if (mc_global.tty.use_subshell)
382 load_prompt (0, NULL);
383 if (new_dir)
384 do_possible_cd (new_dir);
385 if (mc_global.tty.console_flag && output_lines)
386 show_console_contents (output_start_y,
387 LINES - mc_global.keybar_visible - output_lines -
388 1, LINES - mc_global.keybar_visible - 1);
390 #endif /* HAVE_SUBSHELL_SUPPORT */
392 if (mc_global.mc_run_mode == MC_RUN_FULL)
394 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
395 update_xterm_title_path ();
397 repaint_screen ();
400 /* --------------------------------------------------------------------------------------------- */
402 /* event callback */
403 gboolean
404 execute_suspend (const gchar * event_group_name, const gchar * event_name,
405 gpointer init_data, gpointer data)
407 (void) event_group_name;
408 (void) event_name;
409 (void) init_data;
410 (void) data;
412 if (mc_global.mc_run_mode == MC_RUN_FULL)
413 save_cwds_stat ();
414 do_suspend_cmd ();
415 if (mc_global.mc_run_mode == MC_RUN_FULL)
416 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
417 do_refresh ();
419 return TRUE;
422 /* --------------------------------------------------------------------------------------------- */
424 * Execute command on a filename that can be on VFS.
425 * Errors are reported to the user.
428 void
429 execute_with_vfs_arg (const char *command, const char *filename)
431 char *localcopy;
432 char *fn;
433 struct stat st;
434 time_t mtime;
435 vfs_path_t *vpath = vfs_path_from_str (filename);
437 /* Simplest case, this file is local */
438 if (!filename || vfs_file_is_local (vpath))
440 fn = vfs_path_to_str (vpath);
441 do_execute (command, fn, EXECUTE_INTERNAL);
442 g_free (fn);
443 vfs_path_free (vpath);
444 return;
446 vfs_path_free (vpath);
448 /* FIXME: Creation of new files on VFS is not supported */
449 if (!*filename)
450 return;
452 localcopy = mc_getlocalcopy (filename);
453 if (localcopy == NULL)
455 message (D_ERROR, MSG_ERROR, _("Cannot fetch a local copy of %s"), filename);
456 return;
460 * filename can be an entry on panel, it can be changed by executing
461 * the command, so make a copy. Smarter VFS code would make the code
462 * below unnecessary.
464 fn = g_strdup (filename);
465 mc_stat (localcopy, &st);
466 mtime = st.st_mtime;
467 do_execute (command, localcopy, EXECUTE_INTERNAL);
468 mc_stat (localcopy, &st);
469 mc_ungetlocalcopy (fn, localcopy, mtime != st.st_mtime);
470 g_free (localcopy);
471 g_free (fn);
474 /* --------------------------------------------------------------------------------------------- */