Update base version to 4.6.1.
[midnight-commander.git] / src / execute.c
blob9f4ea3afab2660487cae86d90e46d8bc08fd51c3
1 /* Execution routines for GNU Midnight Commander
2 Copyright (C) 1994-2003 The Free Software Foundation
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 #include <config.h>
20 #include <signal.h>
22 #include "global.h"
23 #include "tty.h"
24 #include "win.h"
25 #include "key.h"
26 #include "main.h"
27 #include "cons.saver.h"
28 #include "subshell.h"
29 #include "layout.h"
30 #include "dialog.h"
31 #include "wtools.h"
32 #include "execute.h"
35 static void
36 edition_post_exec (void)
38 do_enter_ca_mode ();
40 /* FIXME: Missing on slang endwin? */
41 reset_prog_mode ();
42 flushinp ();
44 keypad (stdscr, TRUE);
45 mc_raw_mode ();
46 channels_up ();
47 enable_mouse ();
48 if (alternate_plus_minus)
49 application_keypad_mode ();
53 static void
54 edition_pre_exec (void)
56 if (clear_before_exec)
57 clr_scr ();
58 else {
59 if (!(console_flag || xterm_flag))
60 printf ("\n\n");
63 channels_down ();
64 disable_mouse ();
66 reset_shell_mode ();
67 keypad (stdscr, FALSE);
68 endwin ();
70 numeric_keypad_mode ();
72 /* on xterms: maybe endwin did not leave the terminal on the shell
73 * screen page: do it now.
75 * Do not move this before endwin: in some systems rmcup includes
76 * a call to clear screen, so it will end up clearing the shell screen.
78 do_exit_ca_mode ();
82 /* Set up the terminal before executing a program */
83 static void
84 pre_exec (void)
86 use_dash (0);
87 edition_pre_exec ();
91 static void
92 do_execute (const char *shell, const char *command, int flags)
94 #ifdef HAVE_SUBSHELL_SUPPORT
95 char *new_dir = NULL;
96 #endif /* HAVE_SUBSHELL_SUPPORT */
98 #ifdef USE_VFS
99 char *old_vfs_dir = 0;
101 if (!vfs_current_is_local ())
102 old_vfs_dir = g_strdup (vfs_get_current_dir ());
103 #endif /* USE_VFS */
105 save_cwds_stat ();
106 pre_exec ();
107 if (console_flag)
108 handle_console (CONSOLE_RESTORE);
110 if (!use_subshell && command && !(flags & EXECUTE_INTERNAL)) {
111 printf ("%s%s\n", prompt, command);
113 #ifdef HAVE_SUBSHELL_SUPPORT
114 if (use_subshell && !(flags & EXECUTE_INTERNAL)) {
115 do_update_prompt ();
117 /* We don't care if it died, higher level takes care of this */
118 #ifdef USE_VFS
119 invoke_subshell (command, VISIBLY, old_vfs_dir ? 0 : &new_dir);
120 #else
121 invoke_subshell (command, VISIBLY, &new_dir);
122 #endif /* !USE_VFS */
123 } else
124 #endif /* HAVE_SUBSHELL_SUPPORT */
125 my_system (flags, shell, command);
127 if (!(flags & EXECUTE_INTERNAL)) {
128 if ((pause_after_run == pause_always
129 || (pause_after_run == pause_on_dumb_terminals && !xterm_flag
130 && !console_flag)) && !quit
131 #ifdef HAVE_SUBSHELL_SUPPORT
132 && subshell_state != RUNNING_COMMAND
133 #endif /* HAVE_SUBSHELL_SUPPORT */
135 printf (_("Press any key to continue..."));
136 fflush (stdout);
137 mc_raw_mode ();
138 get_key_code (0);
139 printf ("\r\n");
140 fflush (stdout);
142 if (console_flag) {
143 if (output_lines && keybar_visible) {
144 putchar ('\n');
145 fflush (stdout);
150 if (console_flag)
151 handle_console (CONSOLE_SAVE);
152 edition_post_exec ();
154 #ifdef HAVE_SUBSHELL_SUPPORT
155 if (new_dir)
156 do_possible_cd (new_dir);
158 #endif /* HAVE_SUBSHELL_SUPPORT */
160 #ifdef USE_VFS
161 if (old_vfs_dir) {
162 mc_chdir (old_vfs_dir);
163 g_free (old_vfs_dir);
165 #endif /* USE_VFS */
167 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
168 update_xterm_title_path ();
170 do_refresh ();
171 use_dash (TRUE);
175 /* Executes a command */
176 void
177 shell_execute (const char *command, int flags)
179 char *cmd = NULL;
181 if (flags & EXECUTE_HIDE) {
182 cmd = g_strconcat (" ", command, (char *) NULL);
183 flags ^= EXECUTE_HIDE;
186 #ifdef HAVE_SUBSHELL_SUPPORT
187 if (use_subshell)
188 if (subshell_state == INACTIVE)
189 do_execute (shell, cmd ? cmd : command, flags | EXECUTE_AS_SHELL);
190 else
191 message (1, MSG_ERROR,
192 _(" The shell is already running a command "));
193 else
194 #endif /* HAVE_SUBSHELL_SUPPORT */
195 do_execute (shell, cmd ? cmd : command, flags | EXECUTE_AS_SHELL);
197 g_free (cmd);
201 void
202 exec_shell (void)
204 do_execute (shell, 0, 0);
208 void
209 toggle_panels (void)
211 #ifdef HAVE_SUBSHELL_SUPPORT
212 char *new_dir = NULL;
213 char **new_dir_p;
214 #endif /* HAVE_SUBSHELL_SUPPORT */
216 channels_down ();
217 disable_mouse ();
218 if (clear_before_exec)
219 clr_scr ();
220 if (alternate_plus_minus)
221 numeric_keypad_mode ();
222 #ifndef HAVE_SLANG
223 /* With slang we don't want any of this, since there
224 * is no mc_raw_mode supported
226 reset_shell_mode ();
227 noecho ();
228 #endif /* !HAVE_SLANG */
229 keypad (stdscr, FALSE);
230 endwin ();
231 do_exit_ca_mode ();
232 mc_raw_mode ();
233 if (console_flag)
234 handle_console (CONSOLE_RESTORE);
236 #ifdef HAVE_SUBSHELL_SUPPORT
237 if (use_subshell) {
238 new_dir_p = vfs_current_is_local ()? &new_dir : NULL;
239 if (invoke_subshell (NULL, VISIBLY, new_dir_p))
240 quiet_quit_cmd (); /* User did `exit' or `logout': quit MC quietly */
241 } else
242 #endif /* HAVE_SUBSHELL_SUPPORT */
244 if (output_starts_shell) {
245 fprintf (stderr,
246 _("Type `exit' to return to the Midnight Commander"));
247 fprintf (stderr, "\n\r\n\r");
249 my_system (EXECUTE_INTERNAL, shell, NULL);
250 } else
251 get_key_code (0);
253 if (console_flag)
254 handle_console (CONSOLE_SAVE);
256 do_enter_ca_mode ();
258 reset_prog_mode ();
259 keypad (stdscr, TRUE);
261 /* Prevent screen flash when user did 'exit' or 'logout' within
262 subshell */
263 if (quit)
264 return;
266 enable_mouse ();
267 channels_up ();
268 if (alternate_plus_minus)
269 application_keypad_mode ();
271 #ifdef HAVE_SUBSHELL_SUPPORT
272 if (use_subshell) {
273 load_prompt (0, 0);
274 if (new_dir)
275 do_possible_cd (new_dir);
276 if (console_flag && output_lines)
277 show_console_contents (output_start_y,
278 LINES - keybar_visible - output_lines -
279 1, LINES - keybar_visible - 1);
281 #endif /* HAVE_SUBSHELL_SUPPORT */
283 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
284 update_xterm_title_path ();
285 do_refresh ();
289 static void
290 do_suspend_cmd (void)
292 pre_exec ();
294 if (console_flag && !use_subshell)
295 handle_console (CONSOLE_RESTORE);
297 #ifdef SIGTSTP
299 struct sigaction sigtstp_action;
301 /* Make sure that the SIGTSTP below will suspend us directly,
302 without calling ncurses' SIGTSTP handler; we *don't* want
303 ncurses to redraw the screen immediately after the SIGCONT */
304 sigaction (SIGTSTP, &startup_handler, &sigtstp_action);
306 kill (getpid (), SIGTSTP);
308 /* Restore previous SIGTSTP action */
309 sigaction (SIGTSTP, &sigtstp_action, NULL);
311 #endif /* SIGTSTP */
313 if (console_flag && !use_subshell)
314 handle_console (CONSOLE_SAVE);
316 edition_post_exec ();
320 void
321 suspend_cmd (void)
323 save_cwds_stat ();
324 do_suspend_cmd ();
325 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
326 do_refresh ();
331 * Execute command on a filename that can be on VFS.
332 * Errors are reported to the user.
334 void
335 execute_with_vfs_arg (const char *command, const char *filename)
337 char *localcopy;
338 char *fn;
339 struct stat st;
340 time_t mtime;
342 /* Simplest case, this file is local */
343 if (!filename || vfs_file_is_local (filename)) {
344 do_execute (command, filename, EXECUTE_INTERNAL);
345 return;
348 /* FIXME: Creation of new files on VFS is not supported */
349 if (!*filename)
350 return;
352 localcopy = mc_getlocalcopy (filename);
353 if (localcopy == NULL) {
354 message (1, MSG_ERROR, _(" Cannot fetch a local copy of %s "),
355 filename);
356 return;
360 * filename can be an entry on panel, it can be changed by executing
361 * the command, so make a copy. Smarter VFS code would make the code
362 * below unnecessary.
364 fn = g_strdup (filename);
365 mc_stat (localcopy, &st);
366 mtime = st.st_mtime;
367 do_execute (command, localcopy, EXECUTE_INTERNAL);
368 mc_stat (localcopy, &st);
369 mc_ungetlocalcopy (fn, localcopy, mtime != st.st_mtime);
370 g_free (localcopy);
371 g_free (fn);