Updated italian translation
[midnight-commander.git] / src / execute.c
blob19899612cb0040b206929eb6a300558f08d8e7a6
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 #include <config.h>
19 #include <signal.h> /* kill() */
20 #include "global.h"
21 #include "tty.h"
22 #include "win.h"
23 #include "key.h"
24 #include "main.h"
25 #include "cons.saver.h"
26 #include "subshell.h"
27 #include "layout.h"
28 #include "dialog.h"
29 #include "wtools.h"
30 #include "execute.h"
33 static void
34 edition_post_exec (void)
36 do_enter_ca_mode ();
38 /* FIXME: Missing on slang endwin? */
39 reset_prog_mode ();
40 flushinp ();
42 keypad (stdscr, TRUE);
43 mc_raw_mode ();
44 channels_up ();
45 enable_mouse ();
46 if (alternate_plus_minus)
47 application_keypad_mode ();
51 static void
52 edition_pre_exec (void)
54 if (clear_before_exec)
55 clr_scr ();
56 else {
57 if (!(console_flag || xterm_flag))
58 printf ("\n\n");
61 channels_down ();
62 disable_mouse ();
64 reset_shell_mode ();
65 keypad (stdscr, FALSE);
66 endwin ();
68 numeric_keypad_mode ();
70 /* on xterms: maybe endwin did not leave the terminal on the shell
71 * screen page: do it now.
73 * Do not move this before endwin: in some systems rmcup includes
74 * a call to clear screen, so it will end up clearing the shell screen.
76 do_exit_ca_mode ();
80 /* Set up the terminal before executing a program */
81 static void
82 pre_exec (void)
84 use_dash (0);
85 edition_pre_exec ();
89 static void
90 do_execute (const char *shell, const char *command, int flags)
92 #ifdef HAVE_SUBSHELL_SUPPORT
93 char *new_dir = NULL;
94 #endif /* HAVE_SUBSHELL_SUPPORT */
96 #ifdef USE_VFS
97 char *old_vfs_dir = 0;
99 if (!vfs_current_is_local ())
100 old_vfs_dir = g_strdup (vfs_get_current_dir ());
101 #endif /* USE_VFS */
103 save_cwds_stat ();
104 pre_exec ();
105 if (console_flag)
106 handle_console (CONSOLE_RESTORE);
108 if (!use_subshell && command && !(flags & EXECUTE_INTERNAL)) {
109 printf ("%s%s\n", prompt, command);
111 #ifdef HAVE_SUBSHELL_SUPPORT
112 if (use_subshell && !(flags & EXECUTE_INTERNAL)) {
113 do_update_prompt ();
115 /* We don't care if it died, higher level takes care of this */
116 #ifdef USE_VFS
117 invoke_subshell (command, VISIBLY, old_vfs_dir ? 0 : &new_dir);
118 #else
119 invoke_subshell (command, VISIBLY, &new_dir);
120 #endif /* !USE_VFS */
121 } else
122 #endif /* HAVE_SUBSHELL_SUPPORT */
123 my_system (flags, shell, command);
125 if (!(flags & EXECUTE_INTERNAL)) {
126 if ((pause_after_run == pause_always
127 || (pause_after_run == pause_on_dumb_terminals && !xterm_flag
128 && !console_flag)) && !quit
129 #ifdef HAVE_SUBSHELL_SUPPORT
130 && subshell_state != RUNNING_COMMAND
131 #endif /* HAVE_SUBSHELL_SUPPORT */
133 printf (_("Press any key to continue..."));
134 fflush (stdout);
135 mc_raw_mode ();
136 get_key_code (0);
137 printf ("\r\n");
138 fflush (stdout);
140 if (console_flag) {
141 if (output_lines && keybar_visible) {
142 putchar ('\n');
143 fflush (stdout);
148 if (console_flag)
149 handle_console (CONSOLE_SAVE);
150 edition_post_exec ();
152 #ifdef HAVE_SUBSHELL_SUPPORT
153 if (new_dir)
154 do_possible_cd (new_dir);
156 #endif /* HAVE_SUBSHELL_SUPPORT */
158 #ifdef USE_VFS
159 if (old_vfs_dir) {
160 mc_chdir (old_vfs_dir);
161 g_free (old_vfs_dir);
163 #endif /* USE_VFS */
165 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
166 update_xterm_title_path ();
168 do_refresh ();
169 use_dash (TRUE);
173 /* Executes a command */
174 void
175 shell_execute (const char *command, int flags)
177 #ifdef HAVE_SUBSHELL_SUPPORT
178 if (use_subshell)
179 if (subshell_state == INACTIVE)
180 do_execute (shell, command, flags | EXECUTE_AS_SHELL);
181 else
182 message (1, MSG_ERROR,
183 _(" The shell is already running a command "));
184 else
185 #endif /* HAVE_SUBSHELL_SUPPORT */
186 do_execute (shell, command, flags | EXECUTE_AS_SHELL);
190 void
191 exec_shell (void)
193 do_execute (shell, 0, 0);
197 void
198 toggle_panels (void)
200 #ifdef HAVE_SUBSHELL_SUPPORT
201 char *new_dir = NULL;
202 char **new_dir_p;
203 #endif /* HAVE_SUBSHELL_SUPPORT */
205 channels_down ();
206 disable_mouse ();
207 if (clear_before_exec)
208 clr_scr ();
209 if (alternate_plus_minus)
210 numeric_keypad_mode ();
211 #ifndef HAVE_SLANG
212 /* With slang we don't want any of this, since there
213 * is no mc_raw_mode supported
215 reset_shell_mode ();
216 noecho ();
217 #endif /* !HAVE_SLANG */
218 keypad (stdscr, FALSE);
219 endwin ();
220 do_exit_ca_mode ();
221 mc_raw_mode ();
222 if (console_flag)
223 handle_console (CONSOLE_RESTORE);
225 #ifdef HAVE_SUBSHELL_SUPPORT
226 if (use_subshell) {
227 new_dir_p = vfs_current_is_local ()? &new_dir : NULL;
228 if (invoke_subshell (NULL, VISIBLY, new_dir_p))
229 quiet_quit_cmd (); /* User did `exit' or `logout': quit MC quietly */
230 } else
231 #endif /* HAVE_SUBSHELL_SUPPORT */
233 if (output_starts_shell) {
234 fprintf (stderr,
235 _("Type `exit' to return to the Midnight Commander"));
236 fprintf (stderr, "\n\r\n\r");
238 my_system (EXECUTE_AS_SHELL, shell, NULL);
239 } else
240 get_key_code (0);
242 if (console_flag)
243 handle_console (CONSOLE_SAVE);
245 do_enter_ca_mode ();
247 reset_prog_mode ();
248 keypad (stdscr, TRUE);
250 /* Prevent screen flash when user did 'exit' or 'logout' within
251 subshell */
252 if (quit)
253 return;
255 enable_mouse ();
256 channels_up ();
257 if (alternate_plus_minus)
258 application_keypad_mode ();
260 #ifdef HAVE_SUBSHELL_SUPPORT
261 if (use_subshell) {
262 load_prompt (0, 0);
263 if (new_dir)
264 do_possible_cd (new_dir);
265 if (console_flag && output_lines)
266 show_console_contents (output_start_y,
267 LINES - keybar_visible - output_lines -
268 1, LINES - keybar_visible - 1);
270 #endif /* HAVE_SUBSHELL_SUPPORT */
272 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
273 update_xterm_title_path ();
274 do_refresh ();
278 static void
279 do_suspend_cmd (void)
281 pre_exec ();
283 if (console_flag && !use_subshell)
284 handle_console (CONSOLE_RESTORE);
286 #ifdef SIGTSTP
288 struct sigaction sigtstp_action;
290 /* Make sure that the SIGTSTP below will suspend us directly,
291 without calling ncurses' SIGTSTP handler; we *don't* want
292 ncurses to redraw the screen immediately after the SIGCONT */
293 sigaction (SIGTSTP, &startup_handler, &sigtstp_action);
295 kill (getpid (), SIGTSTP);
297 /* Restore previous SIGTSTP action */
298 sigaction (SIGTSTP, &sigtstp_action, NULL);
300 #endif /* SIGTSTP */
302 if (console_flag && !use_subshell)
303 handle_console (CONSOLE_SAVE);
305 edition_post_exec ();
309 void
310 suspend_cmd (void)
312 save_cwds_stat ();
313 do_suspend_cmd ();
314 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
315 do_refresh ();
320 * Execute command on a filename that can be on VFS.
321 * Errors are reported to the user.
323 void
324 execute_with_vfs_arg (const char *command, const char *filename)
326 char *localcopy;
327 char *fn;
328 struct stat st;
329 time_t mtime;
331 /* Simplest case, this file is local */
332 if (!filename || vfs_file_is_local (filename)) {
333 do_execute (command, filename, EXECUTE_INTERNAL);
334 return;
337 /* FIXME: Creation of new files on VFS is not supported */
338 if (!*filename)
339 return;
341 localcopy = mc_getlocalcopy (filename);
342 if (localcopy == NULL) {
343 message (1, MSG_ERROR, _(" Cannot fetch a local copy of %s "),
344 filename);
345 return;
349 * filename can be an entry on panel, it can be changed by executing
350 * the command, so make a copy. Smarter VFS code would make the code
351 * below unnecessary.
353 fn = g_strdup (filename);
354 mc_stat (localcopy, &st);
355 mtime = st.st_mtime;
356 do_execute (command, localcopy, EXECUTE_INTERNAL);
357 mc_stat (localcopy, &st);
358 mc_ungetlocalcopy (fn, localcopy, mtime != st.st_mtime);
359 g_free (fn);