Split file src/keybind.[ch] to lib/keybind.[ch] and src/keybind-defaults.[ch].
[midnight-commander.git] / src / subshell.c
blob8215e618f9f7b435b29aeaba0631f3f089aa09a7
1 /* Concurrent shell support for the Midnight Commander
2 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of Version 2 of the GNU General Public
7 License, as published by the Free Software Foundation.
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 /** \file subshell.c
20 * \brief Source: concurrent shell support
23 #include <config.h>
25 #ifdef HAVE_SUBSHELL_SUPPORT
27 #ifndef _GNU_SOURCE
28 #define _GNU_SOURCE 1
29 #endif
31 #include <ctype.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <signal.h>
37 #include <fcntl.h>
38 #include <sys/types.h>
39 #include <sys/wait.h>
40 #ifdef HAVE_SYS_IOCTL_H
41 #include <sys/ioctl.h>
42 #endif
43 #include <termios.h>
44 #include <unistd.h>
46 #ifdef HAVE_STROPTS_H
47 #include <stropts.h> /* For I_PUSH */
48 #endif /* HAVE_STROPTS_H */
50 #include "lib/global.h"
51 #include "lib/tty/tty.h" /* LINES */
52 #include "lib/tty/key.h" /* XCTRL */
53 #include "lib/vfs/mc-vfs/vfs.h"
54 #include "lib/strutil.h"
55 #include "lib/fileloc.h"
56 #include "lib/util.h"
57 #include "lib/widget.h"
59 #include "panel.h" /* current_panel */
60 #include "main.h" /* do_update_prompt() */
61 #include "consaver/cons.saver.h" /* handle_console() */
62 #include "subshell.h"
64 /*** global variables ****************************************************************************/
66 /* If using a subshell for evaluating commands this is true */
67 int use_subshell =
68 #ifdef SUBSHELL_OPTIONAL
69 FALSE;
70 #else
71 TRUE;
72 #endif
74 /* File descriptors of the pseudoterminal used by the subshell */
75 int subshell_pty = 0;
77 /* State of the subshell:
78 * INACTIVE: the default state; awaiting a command
79 * ACTIVE: remain in the shell until the user hits `subshell_switch_key'
80 * RUNNING_COMMAND: return to MC when the current command finishes */
81 enum subshell_state_enum subshell_state;
83 /* Holds the latest prompt captured from the subshell */
84 char *subshell_prompt = NULL;
86 /*** file scope macro definitions ****************************************************************/
88 #ifndef WEXITSTATUS
89 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
90 #endif
92 #ifndef WIFEXITED
93 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
94 #endif
96 #ifndef STDIN_FILENO
97 #define STDIN_FILENO 0
98 #endif
100 #ifndef STDOUT_FILENO
101 #define STDOUT_FILENO 1
102 #endif
104 #ifndef STDERR_FILENO
105 #define STDERR_FILENO 2
106 #endif
108 /* Initial length of the buffer for the subshell's prompt */
109 #define INITIAL_PROMPT_SIZE 10
111 /* Used by the child process to indicate failure to start the subshell */
112 #define FORK_FAILURE 69 /* Arbitrary */
114 /* Length of the buffer for all I/O with the subshell */
115 #define PTY_BUFFER_SIZE BUF_SMALL /* Arbitrary; but keep it >= 80 */
117 /*** file scope type declarations ****************************************************************/
119 /* For pipes */
120 enum
122 READ = 0,
123 WRITE = 1
126 /* Subshell type (gleaned from the SHELL environment variable, if available) */
127 static enum
129 BASH,
130 TCSH,
131 ZSH,
132 FISH
133 } subshell_type;
135 /*** file scope variables ************************************************************************/
137 /* tcsh closes all non-standard file descriptors, so we have to use a pipe */
138 static char tcsh_fifo[128];
140 static int subshell_pty_slave = -1;
142 /* The key for switching back to MC from the subshell */
143 /* *INDENT-OFF* */
144 static const char subshell_switch_key = XCTRL ('o') & 255;
145 /* *INDENT-ON* */
147 /* For reading/writing on the subshell's pty */
148 static char pty_buffer[PTY_BUFFER_SIZE] = "\0";
150 /* To pass CWD info from the subshell to MC */
151 static int subshell_pipe[2];
153 /* The subshell's process ID */
154 static pid_t subshell_pid = 1;
156 /* One extra char for final '\n' */
157 static char subshell_cwd[MC_MAXPATHLEN + 1];
159 /* Flag to indicate whether the subshell is ready for next command */
160 static int subshell_ready;
162 /* The following two flags can be changed by the SIGCHLD handler. This is */
163 /* OK, because the `int' type is updated atomically on all known machines */
164 static volatile int subshell_alive, subshell_stopped;
166 /* We store the terminal's initial mode here so that we can configure
167 the pty similarly, and also so we can restore the real terminal to
168 sanity if we have to exit abruptly */
169 static struct termios shell_mode;
171 /* This is a transparent mode for the terminal where MC is running on */
172 /* It is used when the shell is active, so that the control signals */
173 /* are delivered to the shell pty */
174 static struct termios raw_mode;
176 /* This counter indicates how many characters of prompt we have read */
177 /* FIXME: try to figure out why this had to become global */
178 static int prompt_pos;
181 /*** file scope functions ************************************************************************/
182 /* --------------------------------------------------------------------------------------------- */
184 static void init_raw_mode (void);
185 static gboolean feed_subshell (int how, int fail_on_error);
186 static void synchronize (void);
187 static int pty_open_master (char *pty_name);
188 static int pty_open_slave (const char *pty_name);
189 static int resize_tty (int fd);
191 /* --------------------------------------------------------------------------------------------- */
193 * Write all data, even if the write() call is interrupted.
196 static ssize_t
197 write_all (int fd, const void *buf, size_t count)
199 ssize_t ret;
200 ssize_t written = 0;
201 while (count > 0)
203 ret = write (fd, (const unsigned char *) buf + written, count);
204 if (ret < 0)
206 if (errno == EINTR)
208 continue;
210 else
212 return written > 0 ? written : ret;
215 count -= ret;
216 written += ret;
218 return written;
221 /* --------------------------------------------------------------------------------------------- */
223 * Prepare child process to running the shell and run it.
225 * Modifies the global variables (in the child process only):
226 * shell_mode
228 * Returns: never.
231 static void
232 init_subshell_child (const char *pty_name)
234 const char *init_file = NULL;
235 pid_t mc_sid;
237 (void) pty_name;
238 setsid (); /* Get a fresh terminal session */
240 /* Make sure that it has become our controlling terminal */
242 /* Redundant on Linux and probably most systems, but just in case: */
244 #ifdef TIOCSCTTY
245 ioctl (subshell_pty_slave, TIOCSCTTY, 0);
246 #endif
248 /* Configure its terminal modes and window size */
250 /* Set up the pty with the same termios flags as our own tty */
251 if (tcsetattr (subshell_pty_slave, TCSANOW, &shell_mode))
253 fprintf (stderr, "Cannot set pty terminal modes: %s\r\n", unix_error_string (errno));
254 _exit (FORK_FAILURE);
257 /* Set the pty's size (80x25 by default on Linux) according to the */
258 /* size of the real terminal as calculated by ncurses, if possible */
259 resize_tty (subshell_pty_slave);
261 /* Set up the subshell's environment and init file name */
263 /* It simplifies things to change to our home directory here, */
264 /* and the user's startup file may do a `cd' command anyway */
266 int ret;
267 ret = chdir (home_dir); /* FIXME? What about when we re-run the subshell? */
270 /* Set MC_SID to prevent running one mc from another */
271 mc_sid = getsid (0);
272 if (mc_sid != -1)
274 char sid_str[BUF_SMALL];
275 g_snprintf (sid_str, sizeof (sid_str), "MC_SID=%ld", (long) mc_sid);
276 putenv (g_strdup (sid_str));
279 switch (subshell_type)
281 case BASH:
282 init_file = MC_USERCONF_DIR PATH_SEP_STR "bashrc";
283 if (access (init_file, R_OK) == -1)
284 init_file = ".bashrc";
286 /* Make MC's special commands not show up in bash's history */
287 putenv ((char *) "HISTCONTROL=ignorespace");
289 /* Allow alternative readline settings for MC */
290 if (access (MC_USERCONF_DIR PATH_SEP_STR "inputrc", R_OK) == 0)
291 putenv ((char *) "INPUTRC=" MC_USERCONF_DIR PATH_SEP_STR "/inputrc");
293 break;
295 /* TODO: Find a way to pass initfile to TCSH and ZSH */
296 case TCSH:
297 case ZSH:
298 case FISH:
299 break;
301 default:
302 fprintf (stderr, __FILE__ ": unimplemented subshell type %d\r\n", subshell_type);
303 _exit (FORK_FAILURE);
306 /* Attach all our standard file descriptors to the pty */
308 /* This is done just before the fork, because stderr must still */
309 /* be connected to the real tty during the above error messages; */
310 /* otherwise the user will never see them. */
312 dup2 (subshell_pty_slave, STDIN_FILENO);
313 dup2 (subshell_pty_slave, STDOUT_FILENO);
314 dup2 (subshell_pty_slave, STDERR_FILENO);
316 close (subshell_pipe[READ]);
317 close (subshell_pty_slave); /* These may be FD_CLOEXEC, but just in case... */
318 /* Close master side of pty. This is important; apart from */
319 /* freeing up the descriptor for use in the subshell, it also */
320 /* means that when MC exits, the subshell will get a SIGHUP and */
321 /* exit too, because there will be no more descriptors pointing */
322 /* at the master side of the pty and so it will disappear. */
323 close (subshell_pty);
325 /* Execute the subshell at last */
327 switch (subshell_type)
329 case BASH:
330 execl (shell, "bash", "-rcfile", init_file, (char *) NULL);
331 break;
333 case TCSH:
334 execl (shell, "tcsh", (char *) NULL);
335 break;
337 case ZSH:
338 /* Use -g to exclude cmds beginning with space from history
339 * and -Z to use the line editor on non-interactive term */
340 execl (shell, "zsh", "-Z", "-g", (char *) NULL);
342 break;
344 case FISH:
345 execl (shell, "fish", (char *) NULL);
346 break;
349 /* If we get this far, everything failed miserably */
350 _exit (FORK_FAILURE);
354 /* --------------------------------------------------------------------------------------------- */
356 * Check MC_SID to prevent running one mc from another.
357 * Return:
358 * 0 if no parent mc in our session was found,
359 * 1 if parent mc was found and the user wants to continue,
360 * 2 if parent mc was found and the user wants to quit mc.
363 static int
364 check_sid (void)
366 pid_t my_sid, old_sid;
367 const char *sid_str;
368 int r;
370 sid_str = getenv ("MC_SID");
371 if (!sid_str)
372 return 0;
374 old_sid = (pid_t) strtol (sid_str, NULL, 0);
375 if (!old_sid)
376 return 0;
378 my_sid = getsid (0);
379 if (my_sid == -1)
380 return 0;
382 /* The parent mc is in a different session, it's OK */
383 if (old_sid != my_sid)
384 return 0;
386 r = query_dialog (_("Warning"),
387 _("GNU Midnight Commander is already\n"
388 "running on this terminal.\n"
389 "Subshell support will be disabled."), D_ERROR, 2, _("&OK"), _("&Quit"));
390 if (r != 0)
392 return 2;
395 return 1;
398 /* --------------------------------------------------------------------------------------------- */
400 static void
401 init_raw_mode ()
403 static int initialized = 0;
405 /* MC calls tty_reset_shell_mode() in pre_exec() to set the real tty to its */
406 /* original settings. However, here we need to make this tty very raw, */
407 /* so that all keyboard signals, XON/XOFF, etc. will get through to the */
408 /* pty. So, instead of changing the code for execute(), pre_exec(), */
409 /* etc, we just set up the modes we need here, before each command. */
411 if (initialized == 0) /* First time: initialise `raw_mode' */
413 tcgetattr (STDOUT_FILENO, &raw_mode);
414 raw_mode.c_lflag &= ~ICANON; /* Disable line-editing chars, etc. */
415 raw_mode.c_lflag &= ~ISIG; /* Disable intr, quit & suspend chars */
416 raw_mode.c_lflag &= ~ECHO; /* Disable input echoing */
417 raw_mode.c_iflag &= ~IXON; /* Pass ^S/^Q to subshell undisturbed */
418 raw_mode.c_iflag &= ~ICRNL; /* Don't translate CRs into LFs */
419 raw_mode.c_oflag &= ~OPOST; /* Don't postprocess output */
420 raw_mode.c_cc[VTIME] = 0; /* IE: wait forever, and return as */
421 raw_mode.c_cc[VMIN] = 1; /* soon as a character is available */
422 initialized = 1;
426 /* --------------------------------------------------------------------------------------------- */
427 /** Feed the subshell our keyboard input until it says it's finished */
429 static gboolean
430 feed_subshell (int how, int fail_on_error)
432 fd_set read_set; /* For `select' */
433 int maxfdp;
434 int bytes; /* For the return value from `read' */
435 int i; /* Loop counter */
437 struct timeval wtime; /* Maximum time we wait for the subshell */
438 struct timeval *wptr;
440 /* we wait up to 10 seconds if fail_on_error, forever otherwise */
441 wtime.tv_sec = 10;
442 wtime.tv_usec = 0;
443 wptr = fail_on_error ? &wtime : NULL;
445 while (TRUE)
447 if (!subshell_alive)
448 return FALSE;
450 /* Prepare the file-descriptor set and call `select' */
452 FD_ZERO (&read_set);
453 FD_SET (subshell_pty, &read_set);
454 FD_SET (subshell_pipe[READ], &read_set);
455 maxfdp = max (subshell_pty, subshell_pipe[READ]);
456 if (how == VISIBLY)
458 FD_SET (STDIN_FILENO, &read_set);
459 maxfdp = max (maxfdp, STDIN_FILENO);
462 if (select (maxfdp + 1, &read_set, NULL, NULL, wptr) == -1)
465 /* Despite using SA_RESTART, we still have to check for this */
466 if (errno == EINTR)
467 continue; /* try all over again */
468 tcsetattr (STDOUT_FILENO, TCSANOW, &shell_mode);
469 fprintf (stderr, "select (FD_SETSIZE, &read_set...): %s\r\n",
470 unix_error_string (errno));
471 exit (EXIT_FAILURE);
474 if (FD_ISSET (subshell_pty, &read_set))
475 /* Read from the subshell, write to stdout */
477 /* This loop improves performance by reducing context switches
478 by a factor of 20 or so... unfortunately, it also hangs MC
479 randomly, because of an apparent Linux bug. Investigate. */
480 /* for (i=0; i<5; ++i) * FIXME -- experimental */
482 bytes = read (subshell_pty, pty_buffer, sizeof (pty_buffer));
484 /* The subshell has died */
485 if (bytes == -1 && errno == EIO && !subshell_alive)
486 return FALSE;
488 if (bytes <= 0)
490 tcsetattr (STDOUT_FILENO, TCSANOW, &shell_mode);
491 fprintf (stderr, "read (subshell_pty...): %s\r\n", unix_error_string (errno));
492 exit (EXIT_FAILURE);
495 if (how == VISIBLY)
496 write_all (STDOUT_FILENO, pty_buffer, bytes);
499 else if (FD_ISSET (subshell_pipe[READ], &read_set))
500 /* Read the subshell's CWD and capture its prompt */
502 bytes = read (subshell_pipe[READ], subshell_cwd, MC_MAXPATHLEN + 1);
503 if (bytes <= 0)
505 tcsetattr (STDOUT_FILENO, TCSANOW, &shell_mode);
506 fprintf (stderr, "read (subshell_pipe[READ]...): %s\r\n",
507 unix_error_string (errno));
508 exit (EXIT_FAILURE);
511 subshell_cwd[bytes - 1] = 0; /* Squash the final '\n' */
513 synchronize ();
515 subshell_ready = TRUE;
516 if (subshell_state == RUNNING_COMMAND)
518 subshell_state = INACTIVE;
519 return TRUE;
523 else if (FD_ISSET (STDIN_FILENO, &read_set))
524 /* Read from stdin, write to the subshell */
526 bytes = read (STDIN_FILENO, pty_buffer, sizeof (pty_buffer));
527 if (bytes <= 0)
529 tcsetattr (STDOUT_FILENO, TCSANOW, &shell_mode);
530 fprintf (stderr,
531 "read (STDIN_FILENO, pty_buffer...): %s\r\n", unix_error_string (errno));
532 exit (EXIT_FAILURE);
535 for (i = 0; i < bytes; ++i)
536 if (pty_buffer[i] == subshell_switch_key)
538 write_all (subshell_pty, pty_buffer, i);
539 if (subshell_ready)
540 subshell_state = INACTIVE;
541 return TRUE;
544 write_all (subshell_pty, pty_buffer, bytes);
546 if (pty_buffer[bytes - 1] == '\n' || pty_buffer[bytes - 1] == '\r')
547 subshell_ready = FALSE;
549 else
550 return FALSE;
554 /* --------------------------------------------------------------------------------------------- */
556 * Wait until the subshell dies or stops. If it stops, make it resume.
557 * Possibly modifies the globals `subshell_alive' and `subshell_stopped'
560 static void
561 synchronize (void)
563 sigset_t sigchld_mask, old_mask;
565 sigemptyset (&sigchld_mask);
566 sigaddset (&sigchld_mask, SIGCHLD);
567 sigprocmask (SIG_BLOCK, &sigchld_mask, &old_mask);
570 * SIGCHLD should not be blocked, but we unblock it just in case.
571 * This is known to be useful for cygwin 1.3.12 and older.
573 sigdelset (&old_mask, SIGCHLD);
575 /* Wait until the subshell has stopped */
576 while (subshell_alive && !subshell_stopped)
577 sigsuspend (&old_mask);
579 if (subshell_state != ACTIVE)
581 /* Discard all remaining data from stdin to the subshell */
582 tcflush (subshell_pty_slave, TCIFLUSH);
585 subshell_stopped = FALSE;
586 kill (subshell_pid, SIGCONT);
588 sigprocmask (SIG_SETMASK, &old_mask, NULL);
589 /* We can't do any better without modifying the shell(s) */
592 /* pty opening functions */
594 #ifdef HAVE_GRANTPT
596 /* System V version of pty_open_master */
598 static int
599 pty_open_master (char *pty_name)
601 char *slave_name;
602 int pty_master;
604 #ifdef HAVE_POSIX_OPENPT
605 pty_master = posix_openpt (O_RDWR);
606 #elif HAVE_GETPT
607 /* getpt () is a GNU extension (glibc 2.1.x) */
608 pty_master = getpt ();
609 #elif IS_AIX
610 strcpy (pty_name, "/dev/ptc");
611 pty_master = open (pty_name, O_RDWR);
612 #else
613 strcpy (pty_name, "/dev/ptmx");
614 pty_master = open (pty_name, O_RDWR);
615 #endif
617 if (pty_master == -1)
618 return -1;
620 if (grantpt (pty_master) == -1 /* Grant access to slave */
621 || unlockpt (pty_master) == -1 /* Clear slave's lock flag */
622 || !(slave_name = ptsname (pty_master))) /* Get slave's name */
624 close (pty_master);
625 return -1;
627 strcpy (pty_name, slave_name);
628 return pty_master;
631 /* --------------------------------------------------------------------------------------------- */
632 /** System V version of pty_open_slave */
634 static int
635 pty_open_slave (const char *pty_name)
637 int pty_slave = open (pty_name, O_RDWR);
639 if (pty_slave == -1)
641 fprintf (stderr, "open (%s, O_RDWR): %s\r\n", pty_name, unix_error_string (errno));
642 return -1;
644 #if !defined(__osf__) && !defined(__linux__)
645 #if defined (I_FIND) && defined (I_PUSH)
646 if (!ioctl (pty_slave, I_FIND, "ptem"))
647 if (ioctl (pty_slave, I_PUSH, "ptem") == -1)
649 fprintf (stderr, "ioctl (%d, I_PUSH, \"ptem\") failed: %s\r\n",
650 pty_slave, unix_error_string (errno));
651 close (pty_slave);
652 return -1;
655 if (!ioctl (pty_slave, I_FIND, "ldterm"))
656 if (ioctl (pty_slave, I_PUSH, "ldterm") == -1)
658 fprintf (stderr,
659 "ioctl (%d, I_PUSH, \"ldterm\") failed: %s\r\n",
660 pty_slave, unix_error_string (errno));
661 close (pty_slave);
662 return -1;
664 #if !defined(sgi) && !defined(__sgi)
665 if (!ioctl (pty_slave, I_FIND, "ttcompat"))
666 if (ioctl (pty_slave, I_PUSH, "ttcompat") == -1)
668 fprintf (stderr,
669 "ioctl (%d, I_PUSH, \"ttcompat\") failed: %s\r\n",
670 pty_slave, unix_error_string (errno));
671 close (pty_slave);
672 return -1;
674 #endif /* sgi || __sgi */
675 #endif /* I_FIND && I_PUSH */
676 #endif /* __osf__ || __linux__ */
678 fcntl (pty_slave, F_SETFD, FD_CLOEXEC);
679 return pty_slave;
682 #else /* !HAVE_GRANTPT */
684 /* --------------------------------------------------------------------------------------------- */
685 /** BSD version of pty_open_master */
686 static int
687 pty_open_master (char *pty_name)
689 int pty_master;
690 const char *ptr1, *ptr2;
692 strcpy (pty_name, "/dev/ptyXX");
693 for (ptr1 = "pqrstuvwxyzPQRST"; *ptr1; ++ptr1)
695 pty_name[8] = *ptr1;
696 for (ptr2 = "0123456789abcdef"; *ptr2 != '\0'; ++ptr2)
698 pty_name[9] = *ptr2;
700 /* Try to open master */
701 pty_master = open (pty_name, O_RDWR);
702 if (pty_master == -1)
704 if (errno == ENOENT) /* Different from EIO */
705 return -1; /* Out of pty devices */
706 continue; /* Try next pty device */
708 pty_name[5] = 't'; /* Change "pty" to "tty" */
709 if (access (pty_name, 6) != 0)
711 close (pty_master);
712 pty_name[5] = 'p';
713 continue;
715 return pty_master;
718 return -1; /* Ran out of pty devices */
721 /* --------------------------------------------------------------------------------------------- */
722 /** BSD version of pty_open_slave */
724 static int
725 pty_open_slave (const char *pty_name)
727 int pty_slave;
728 struct group *group_info = getgrnam ("tty");
730 if (group_info != NULL)
732 /* The following two calls will only succeed if we are root */
733 /* [Commented out while permissions problem is investigated] */
734 /* chown (pty_name, getuid (), group_info->gr_gid); FIXME */
735 /* chmod (pty_name, S_IRUSR | S_IWUSR | S_IWGRP); FIXME */
737 pty_slave = open (pty_name, O_RDWR);
738 if (pty_slave == -1)
739 fprintf (stderr, "open (pty_name, O_RDWR): %s\r\n", pty_name);
740 fcntl (pty_slave, F_SETFD, FD_CLOEXEC);
741 return pty_slave;
743 #endif /* !HAVE_GRANTPT */
744 /* --------------------------------------------------------------------------------------------- */
745 /*** public functions ****************************************************************************/
746 /* --------------------------------------------------------------------------------------------- */
748 /* --------------------------------------------------------------------------------------------- */
750 * Fork the subshell, and set up many, many things.
752 * Possibly modifies the global variables:
753 * subshell_type, subshell_alive, subshell_stopped, subshell_pid
754 * use_subshell - Is set to FALSE if we can't run the subshell
755 * quit - Can be set to SUBSHELL_EXIT by the SIGCHLD handler
758 void
759 init_subshell (void)
761 /* This must be remembered across calls to init_subshell() */
762 static char pty_name[BUF_SMALL];
763 char precmd[BUF_SMALL];
765 switch (check_sid ())
767 case 1:
768 use_subshell = FALSE;
769 return;
770 case 2:
771 use_subshell = FALSE;
772 midnight_shutdown = 1;
773 return;
776 /* Take the current (hopefully pristine) tty mode and make */
777 /* a raw mode based on it now, before we do anything else with it */
778 init_raw_mode ();
780 if (subshell_pty == 0)
781 { /* First time through */
782 /* Find out what type of shell we have */
784 if (strstr (shell, "/zsh") || getenv ("ZSH_VERSION"))
785 subshell_type = ZSH;
786 else if (strstr (shell, "/tcsh"))
787 subshell_type = TCSH;
788 else if (strstr (shell, "/csh"))
789 subshell_type = TCSH;
790 else if (strstr (shell, "/bash") || getenv ("BASH"))
791 subshell_type = BASH;
792 else if (strstr (shell, "/fish"))
793 subshell_type = FISH;
794 else
796 use_subshell = FALSE;
797 return;
800 /* Open a pty for talking to the subshell */
802 /* FIXME: We may need to open a fresh pty each time on SVR4 */
804 subshell_pty = pty_open_master (pty_name);
805 if (subshell_pty == -1)
807 fprintf (stderr, "Cannot open master side of pty: %s\r\n", unix_error_string (errno));
808 use_subshell = FALSE;
809 return;
811 subshell_pty_slave = pty_open_slave (pty_name);
812 if (subshell_pty_slave == -1)
814 fprintf (stderr, "Cannot open slave side of pty %s: %s\r\n",
815 pty_name, unix_error_string (errno));
816 use_subshell = FALSE;
817 return;
820 /* Create a pipe for receiving the subshell's CWD */
822 if (subshell_type == TCSH)
824 g_snprintf (tcsh_fifo, sizeof (tcsh_fifo), "%s/mc.pipe.%d",
825 mc_tmpdir (), (int) getpid ());
826 if (mkfifo (tcsh_fifo, 0600) == -1)
828 fprintf (stderr, "mkfifo(%s) failed: %s\r\n", tcsh_fifo, unix_error_string (errno));
829 use_subshell = FALSE;
830 return;
833 /* Opening the FIFO as O_RDONLY or O_WRONLY causes deadlock */
835 if ((subshell_pipe[READ] = open (tcsh_fifo, O_RDWR)) == -1
836 || (subshell_pipe[WRITE] = open (tcsh_fifo, O_RDWR)) == -1)
838 fprintf (stderr, _("Cannot open named pipe %s\n"), tcsh_fifo);
839 perror (__FILE__ ": open");
840 use_subshell = FALSE;
841 return;
844 else /* subshell_type is BASH or ZSH */ if (pipe (subshell_pipe))
846 perror (__FILE__ ": couldn't create pipe");
847 use_subshell = FALSE;
848 return;
852 /* Fork the subshell */
854 subshell_alive = TRUE;
855 subshell_stopped = FALSE;
856 subshell_pid = fork ();
858 if (subshell_pid == -1)
860 fprintf (stderr, "Cannot spawn the subshell process: %s\r\n", unix_error_string (errno));
861 /* We exit here because, if the process table is full, the */
862 /* other method of running user commands won't work either */
863 exit (EXIT_FAILURE);
866 if (subshell_pid == 0)
868 /* We are in the child process */
869 init_subshell_child (pty_name);
872 /* Set up `precmd' or equivalent for reading the subshell's CWD */
874 switch (subshell_type)
876 case BASH:
877 g_snprintf (precmd, sizeof (precmd),
878 " PROMPT_COMMAND='pwd>&%d;kill -STOP $$'\n", subshell_pipe[WRITE]);
879 break;
881 case ZSH:
882 g_snprintf (precmd, sizeof (precmd),
883 " precmd(){ pwd>&%d;kill -STOP $$ }\n", subshell_pipe[WRITE]);
884 break;
886 case TCSH:
887 g_snprintf (precmd, sizeof (precmd),
888 "set echo_style=both;"
889 "alias precmd 'echo $cwd:q >>%s;kill -STOP $$'\n", tcsh_fifo);
890 break;
891 case FISH:
892 g_snprintf (precmd, sizeof (precmd),
893 "function fish_prompt ; pwd>&%d;kill -STOP %%self; end\n",
894 subshell_pipe[WRITE]);
895 break;
898 write_all (subshell_pty, precmd, strlen (precmd));
900 /* Wait until the subshell has started up and processed the command */
902 subshell_state = RUNNING_COMMAND;
903 tty_enable_interrupt_key ();
904 if (!feed_subshell (QUIETLY, TRUE))
906 use_subshell = FALSE;
908 tty_disable_interrupt_key ();
909 if (!subshell_alive)
910 use_subshell = FALSE; /* Subshell died instantly, so don't use it */
913 /* --------------------------------------------------------------------------------------------- */
916 invoke_subshell (const char *command, int how, char **new_dir)
918 char *pcwd;
920 /* Make the MC terminal transparent */
921 tcsetattr (STDOUT_FILENO, TCSANOW, &raw_mode);
923 /* Make the subshell change to MC's working directory */
924 if (new_dir)
925 do_subshell_chdir (current_panel->cwd, TRUE, 1);
927 if (command == NULL) /* The user has done "C-o" from MC */
929 if (subshell_state == INACTIVE)
931 subshell_state = ACTIVE;
932 /* FIXME: possibly take out this hack; the user can
933 re-play it by hitting C-hyphen a few times! */
934 if (subshell_ready)
935 write_all (subshell_pty, " \b", 2); /* Hack to make prompt reappear */
938 else /* MC has passed us a user command */
940 if (how == QUIETLY)
941 write_all (subshell_pty, " ", 1);
942 /* FIXME: if command is long (>8KB ?) we go comma */
943 write_all (subshell_pty, command, strlen (command));
944 write_all (subshell_pty, "\n", 1);
945 subshell_state = RUNNING_COMMAND;
946 subshell_ready = FALSE;
949 feed_subshell (how, FALSE);
951 pcwd = vfs_translate_path_n (current_panel->cwd);
952 if (new_dir && subshell_alive && strcmp (subshell_cwd, pcwd))
953 *new_dir = subshell_cwd; /* Make MC change to the subshell's CWD */
954 g_free (pcwd);
956 /* Restart the subshell if it has died by SIGHUP, SIGQUIT, etc. */
957 while (!subshell_alive && !quit && use_subshell)
958 init_subshell ();
960 prompt_pos = 0;
962 return quit;
966 /* --------------------------------------------------------------------------------------------- */
969 read_subshell_prompt (void)
971 static int prompt_size = INITIAL_PROMPT_SIZE;
972 int bytes = 0, i, rc = 0;
973 struct timeval timeleft = { 0, 0 };
975 fd_set tmp;
976 FD_ZERO (&tmp);
977 FD_SET (subshell_pty, &tmp);
979 if (subshell_prompt == NULL)
980 { /* First time through */
981 subshell_prompt = g_malloc (prompt_size);
982 *subshell_prompt = '\0';
983 prompt_pos = 0;
986 while (subshell_alive && (rc = select (subshell_pty + 1, &tmp, NULL, NULL, &timeleft)))
988 /* Check for `select' errors */
989 if (rc == -1)
991 if (errno == EINTR)
992 continue;
993 else
995 fprintf (stderr, "select (FD_SETSIZE, &tmp...): %s\r\n", unix_error_string (errno));
996 exit (EXIT_FAILURE);
1000 bytes = read (subshell_pty, pty_buffer, sizeof (pty_buffer));
1002 /* Extract the prompt from the shell output */
1004 for (i = 0; i < bytes; ++i)
1005 if (pty_buffer[i] == '\n' || pty_buffer[i] == '\r')
1007 prompt_pos = 0;
1009 else
1011 if (!pty_buffer[i])
1012 continue;
1014 subshell_prompt[prompt_pos++] = pty_buffer[i];
1015 if (prompt_pos == prompt_size)
1016 subshell_prompt = g_realloc (subshell_prompt, prompt_size *= 2);
1019 subshell_prompt[prompt_pos] = '\0';
1021 if (rc == 0 && bytes == 0)
1022 return FALSE;
1023 return TRUE;
1026 /* --------------------------------------------------------------------------------------------- */
1027 /** Resize given terminal using TIOCSWINSZ, return ioctl() result */
1029 static int
1030 resize_tty (int fd)
1032 #if defined TIOCSWINSZ
1033 struct winsize tty_size;
1035 tty_size.ws_row = LINES;
1036 tty_size.ws_col = COLS;
1037 tty_size.ws_xpixel = tty_size.ws_ypixel = 0;
1039 return ioctl (fd, TIOCSWINSZ, &tty_size);
1040 #else
1041 return 0;
1042 #endif
1045 /* --------------------------------------------------------------------------------------------- */
1046 /** Resize subshell_pty */
1048 void
1049 resize_subshell (void)
1051 if (use_subshell == 0)
1052 return;
1054 resize_tty (subshell_pty);
1057 /* --------------------------------------------------------------------------------------------- */
1060 exit_subshell (void)
1062 int subshell_quit = TRUE;
1064 if (subshell_state != INACTIVE && subshell_alive)
1065 subshell_quit =
1066 !query_dialog (_("Warning"),
1067 _("The shell is still active. Quit anyway?"),
1068 D_NORMAL, 2, _("&Yes"), _("&No"));
1070 if (subshell_quit)
1072 if (subshell_type == TCSH)
1074 if (unlink (tcsh_fifo) == -1)
1075 fprintf (stderr, "Cannot remove named pipe %s: %s\r\n",
1076 tcsh_fifo, unix_error_string (errno));
1079 g_free (subshell_prompt);
1080 subshell_prompt = NULL;
1081 pty_buffer[0] = '\0';
1084 return subshell_quit;
1088 /* --------------------------------------------------------------------------------------------- */
1090 * Carefully quote directory name to allow entering any directory safely,
1091 * no matter what weird characters it may contain in its name.
1092 * NOTE: Treat directory name an untrusted data, don't allow it to cause
1093 * executing any commands in the shell. Escape all control characters.
1094 * Use following technique:
1096 * printf(1) with format string containing a single conversion specifier,
1097 * "b", and an argument which contains a copy of the string passed to
1098 * subshell_name_quote() with all characters, except digits and letters,
1099 * replaced by the backslash-escape sequence \0nnn, where "nnn" is the
1100 * numeric value of the character converted to octal number.
1102 * cd "`printf "%b" 'ABC\0nnnDEF\0nnnXYZ'`"
1106 static char *
1107 subshell_name_quote (const char *s)
1109 char *ret, *d;
1110 const char *su, *n;
1111 const char *quote_cmd_start, *quote_cmd_end;
1112 int c;
1114 if (subshell_type == FISH)
1116 quote_cmd_start = "(printf \"%b\" '";
1117 quote_cmd_end = "')";
1119 else
1121 quote_cmd_start = "\"`printf \"%b\" '";
1122 quote_cmd_end = "'`\"";
1125 /* Factor 5 because we need \, 0 and 3 other digits per character. */
1126 d = ret = g_try_malloc (1 + (5 * strlen (s)) + (strlen (quote_cmd_start))
1127 + (strlen (quote_cmd_end)));
1128 if (d == NULL)
1129 return NULL;
1131 /* Prevent interpreting leading `-' as a switch for `cd' */
1132 if (*s == '-')
1134 *d++ = '.';
1135 *d++ = '/';
1138 /* Copy the beginning of the command to the buffer */
1139 strcpy (d, quote_cmd_start);
1140 d += strlen (quote_cmd_start);
1143 * Print every character except digits and letters as a backslash-escape
1144 * sequence of the form \0nnn, where "nnn" is the numeric value of the
1145 * character converted to octal number.
1147 su = s;
1148 for (; su[0] != '\0';)
1150 n = str_cget_next_char_safe (su);
1151 if (str_isalnum (su))
1153 memcpy (d, su, n - su);
1154 d += n - su;
1156 else
1158 for (c = 0; c < n - su; c++)
1160 sprintf (d, "\\0%03o", (unsigned char) su[c]);
1161 d += 5;
1164 su = n;
1167 strcpy (d, quote_cmd_end);
1169 return ret;
1173 /* --------------------------------------------------------------------------------------------- */
1174 /** If it actually changed the directory it returns true */
1176 void
1177 do_subshell_chdir (const char *directory, int do_update, int reset_prompt)
1179 char *pcwd;
1180 char *temp;
1181 char *translate;
1183 pcwd = vfs_translate_path_n (current_panel->cwd);
1185 if (!(subshell_state == INACTIVE && strcmp (subshell_cwd, pcwd)))
1187 /* We have to repaint the subshell prompt if we read it from
1188 * the main program. Please note that in the code after this
1189 * if, the cd command that is sent will make the subshell
1190 * repaint the prompt, so we don't have to paint it. */
1191 if (do_update)
1192 do_update_prompt ();
1193 g_free (pcwd);
1194 return;
1197 /* The initial space keeps this out of the command history (in bash
1198 because we set "HISTCONTROL=ignorespace") */
1199 write_all (subshell_pty, " cd ", 4);
1200 if (*directory)
1202 translate = vfs_translate_path_n (directory);
1203 if (translate)
1205 temp = subshell_name_quote (translate);
1206 if (temp)
1208 write_all (subshell_pty, temp, strlen (temp));
1209 g_free (temp);
1211 else
1213 /* Should not happen unless the directory name is so long
1214 that we don't have memory to quote it. */
1215 write_all (subshell_pty, ".", 1);
1217 g_free (translate);
1219 else
1221 write_all (subshell_pty, ".", 1);
1224 else
1226 write_all (subshell_pty, "/", 1);
1228 write_all (subshell_pty, "\n", 1);
1230 subshell_state = RUNNING_COMMAND;
1231 feed_subshell (QUIETLY, FALSE);
1233 if (subshell_alive)
1235 int bPathNotEq = strcmp (subshell_cwd, pcwd);
1237 if (bPathNotEq && subshell_type == TCSH)
1239 char rp_subshell_cwd[PATH_MAX];
1240 char rp_current_panel_cwd[PATH_MAX];
1242 char *p_subshell_cwd = mc_realpath (subshell_cwd, rp_subshell_cwd);
1243 char *p_current_panel_cwd = mc_realpath (pcwd, rp_current_panel_cwd);
1245 if (p_subshell_cwd == NULL)
1246 p_subshell_cwd = subshell_cwd;
1247 if (p_current_panel_cwd == NULL)
1248 p_current_panel_cwd = pcwd;
1249 bPathNotEq = strcmp (p_subshell_cwd, p_current_panel_cwd);
1252 if (bPathNotEq && strcmp (pcwd, "."))
1254 char *cwd = strip_password (g_strdup (pcwd), 1);
1255 fprintf (stderr, _("Warning: Cannot change to %s.\n"), cwd);
1256 g_free (cwd);
1260 if (reset_prompt)
1261 prompt_pos = 0;
1262 update_prompt = FALSE;
1264 g_free (pcwd);
1265 /* Make sure that MC never stores the CWD in a silly format */
1266 /* like /usr////lib/../bin, or the strcmp() above will fail */
1269 /* --------------------------------------------------------------------------------------------- */
1271 void
1272 subshell_get_console_attributes (void)
1274 /* Get our current terminal modes */
1276 if (tcgetattr (STDOUT_FILENO, &shell_mode))
1278 fprintf (stderr, "Cannot get terminal settings: %s\r\n", unix_error_string (errno));
1279 use_subshell = FALSE;
1280 return;
1284 /* --------------------------------------------------------------------------------------------- */
1286 * Figure out whether the subshell has stopped, exited or been killed
1287 * Possibly modifies: `subshell_alive', `subshell_stopped' and `quit' */
1289 void
1290 sigchld_handler (int sig)
1292 int status;
1293 pid_t pid;
1295 (void) sig;
1297 pid = waitpid (subshell_pid, &status, WUNTRACED | WNOHANG);
1299 if (pid == subshell_pid)
1301 /* Figure out what has happened to the subshell */
1303 if (WIFSTOPPED (status))
1305 if (WSTOPSIG (status) == SIGSTOP)
1307 /* The subshell has received a SIGSTOP signal */
1308 subshell_stopped = TRUE;
1310 else
1312 /* The user has suspended the subshell. Revive it */
1313 kill (subshell_pid, SIGCONT);
1316 else
1318 /* The subshell has either exited normally or been killed */
1319 subshell_alive = FALSE;
1320 delete_select_channel (subshell_pty);
1321 if (WIFEXITED (status) && WEXITSTATUS (status) != FORK_FAILURE)
1322 quit |= SUBSHELL_EXIT; /* Exited normally */
1325 #ifdef __linux__
1326 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
1328 if (pid == cons_saver_pid)
1331 if (WIFSTOPPED (status))
1332 /* Someone has stopped cons.saver - restart it */
1333 kill (pid, SIGCONT);
1334 else
1336 /* cons.saver has died - disable confole saving */
1337 handle_console (CONSOLE_DONE);
1338 console_flag = 0;
1342 #endif /* __linux__ */
1344 /* If we got here, some other child exited; ignore it */
1347 /* --------------------------------------------------------------------------------------------- */
1349 #endif /* HAVE_SUBSHELL_SUPPORT */