⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subshell.c

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 C
📖 第 1 页 / 共 3 页
字号:
/* {{{ Copyright notice */

/* Concurrent shell support for the Midnight Commander
   Copyright (C) 1994, 1995 Dugan Porter

   This program is free software; you can redistribute it and/or
   modify it under the terms of Version 2 of the GNU General Public
   License, as published by the Free Software Foundation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* }}} */

#include <config.h>
#ifdef HAVE_SUBSHELL_SUPPORT

/* {{{ Declarations */

#include <stdio.h>
#include <stdlib.h>	/* For errno, putenv, etc.	      */
#include <errno.h>	/* For errno on SunOS systems	      */
#include <termios.h>	/* tcgetattr(), struct termios, etc.  */
#if (!defined(__IBMC__) && !defined(__IBMCPP__))
#include <sys/types.h>	/* Required by unistd.h below	      */
#endif
#include <sys/ioctl.h>	/* For ioctl() (surprise, surprise)   */
#include <fcntl.h>	/* For open(), etc.		      */
#include <string.h>	/* strstr(), strcpy(), etc.	      */
#include <signal.h>	/* sigaction(), sigprocmask(), etc.   */
#ifndef SCO_FLAVOR
#	include <sys/time.h>	/* select(), gettimeofday(), etc.     */
#endif /* SCO_FLAVOR */
#include <sys/stat.h>	/* Required by dir.h & panel.h below  */
#include <sys/param.h>	/* Required by panel.h below	      */
#include "tty.h"

#ifdef HAVE_UNISTD_H
#   include <unistd.h>	/* For pipe, fork, setsid, access etc */
#endif

#ifdef HAVE_SYS_SELECT_H
#   include <sys/select.h>
#endif

#ifdef HAVE_SYS_WAIT_H
#   include <sys/wait.h> /* For waitpid() */
#endif

#ifndef WEXITSTATUS
#   define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
#endif

#ifndef WIFEXITED
#   define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
#endif

#ifdef HAVE_GRANTPT
#   include <stropts.h> /* For I_PUSH			      */
#else
#   include <grp.h>	/* For the group struct & getgrnam()  */
#endif

#ifdef SCO_FLAVOR
#   include <grp.h>	/* For the group struct & getgrnam()  */
#endif /* SCO_FLAVOR */

#ifdef __QNX__
#   include <unix.h>	/* exec*() from <process.h> */
#endif

#include "dir.h"	/* Required by panel.h below	      */
#include "util.h"	/* Required by panel.h		      */
#include "panel.h"	/* For WPanel and current_panel	      */
#include "dialog.h"	/* For query_dialog()		      */
#include "main.h"	/* For cpanel, quit & init_sigchld()  */
#include "global.h"	/* For home_dir			      */
#include "cons.saver.h"	/* For handle_console(), etc.	      */
#include "key.h"	/* XCTRL and ALT macros		      */
#include "subshell.h"

/* Local functions */
static int feed_subshell (int how, int fail_on_error);
static void synchronize (void);
static int pty_open_master (char *pty_name);
static int pty_open_slave (const char *pty_name);

/* }}} */
/* {{{ Definitions */

#ifndef STDIN_FILENO
#    define STDIN_FILENO 0
#endif

#ifndef STDOUT_FILENO
#    define STDOUT_FILENO 1
#endif

#ifndef STDERR_FILENO
#    define STDERR_FILENO 2
#endif

/* If using a subshell for evaluating commands this is true */
int use_subshell =
#ifdef SUBSHELL_OPTIONAL
FALSE;
#else
TRUE;
#endif

/* File descriptor of the pseudoterminal used by the subshell */
int subshell_pty = 0;

/* If true, the child forked in init_subshell will wait in a loop to be attached by gdb */
int debug_subshell = 0;

/* The key for switching back to MC from the subshell */
char subshell_switch_key = XCTRL('o');

/* State of the subshell:
 * INACTIVE: the default state; awaiting a command
 * ACTIVE: remain in the shell until the user hits `subshell_switch_key'
 * RUNNING_COMMAND: return to MC when the current command finishes */
enum subshell_state_enum subshell_state;

/* Holds the latest prompt captured from the subshell */
char *subshell_prompt = NULL;

/* Initial length of the buffer for the subshell's prompt */
#define INITIAL_PROMPT_SIZE 10

/* Used by the child process to indicate failure to start the subshell */
#define FORK_FAILURE 69  /* Arbitrary */

/* Initial length of the buffer for all I/O with the subshell */
#define INITIAL_PTY_BUFFER_SIZE 100  /* Arbitrary; but keep it >= 80 */

/* For pipes */
enum {READ=0, WRITE=1};


/* Local variables */

static char *pty_buffer;	/* For reading/writing on the subshell's pty */
static int pty_buffer_size;	/* The buffer grows as needed */
static int subshell_pipe[2];	/* To pass CWD info from the subshell to MC */
static pid_t subshell_pid = 1;	/* The subshell's process ID */
static char subshell_cwd[MC_MAXPATHLEN+1];  /* One extra char for final '\n' */

/* Subshell type (gleaned from the SHELL environment variable, if available) */
static enum {BASH, TCSH, ZSH} subshell_type;

/* Flag to indicate whether the subshell is ready for next command */
static int subshell_ready;

/* The following two flags can be changed by the SIGCHLD handler. This is */
/* OK, because the `int' type is updated atomically on all known machines */
static volatile int subshell_alive, subshell_stopped;

/* We store the terminal's initial mode here so that we can configure
   the pty similarly, and also so we can restore the real terminal to
   sanity if we have to exit abruptly */
static struct termios shell_mode;

/* This counter indicates how many characters of prompt we have read */
/* FIXME: try to figure out why this had to become global */
static int prompt_pos;

/* }}} */

/* {{{ init_subshell */

/*
 *  Fork the subshell, and set up many, many things.
 *
 *  Possibly modifies the global variables:
 *	shell_mode
 *	subshell_type, subshell_alive, subshell_stopped, subshell_pid
 *	use_subshell - Is set to FALSE if we can't run the subshell
 *	quit - Can be set to SUBSHELL_EXIT by the SIGCHLD handler
 */

#ifdef HAVE_GRANTPT
#    define SYNC_PTY_SIDES
#else
#    define SYNC_PTY_SIDES
#endif

#undef SYNC_PTY_SIDES

#ifdef SYNC_PTY_SIDES
/* Handler for SIGUSR1 (used below), does nothing but accept the signal */
static void sigusr1_handler (int sig)
{
}
#endif

void init_subshell (void)
{
    /* {{{ Local variables */

    /* This must be remembered across calls to init_subshell() */
    static char pty_name[40];
    int pty_slave;

    /* Braindead tcsh can't redirect output to a file descriptor? */
    char tcsh_fifo[sizeof "/tmp/mc.pipe.1234567890"];


#ifdef SYNC_PTY_SIDES
	/* Used to wait for a SIGUSR1 signal from the subprocess */
	sigset_t sigusr1_mask, old_mask;
#endif

    /* }}} */

    if (subshell_pty == 0)  /* First time through */
    {
	/* {{{ Find out what type of shell we have */

	if (strstr (shell, "/zsh"))
	    subshell_type = ZSH;
	else if (strstr (shell, "/tcsh"))
	    subshell_type = TCSH;
	else if (strstr (shell, "/bash") || getenv ("BASH"))
	    subshell_type = BASH;
	else
	{
	    use_subshell = FALSE;
	    return;
	}

	/* }}} */
	/* {{{ Open a pty for talking to the subshell */

	/* FIXME: We may need to open a fresh pty each time on SVR4 */

	subshell_pty = pty_open_master (pty_name);
	if (subshell_pty == -1)
	{
	    fputs (__FILE__": couldn't open master side of pty\n", stderr);
	    perror ("pty_open_master");
	    use_subshell = FALSE;
	    return;
	}
	pty_slave = pty_open_slave (pty_name);
	if (pty_slave == -1)
	{
	    fprintf (stderr, "couldn't open slave side of pty (%s)\n\r",
		     pty_name);
	    use_subshell = FALSE;
	    return;
	}


	/* }}} */
	/* {{{ Initialise the pty's I/O buffer */

	pty_buffer_size = INITIAL_PTY_BUFFER_SIZE;
	pty_buffer = (char *) malloc (pty_buffer_size);

	/* }}} */
	/* {{{ Create a pipe for receiving the subshell's CWD */

	if (subshell_type == TCSH)
	{
	    sprintf (tcsh_fifo, "/tmp/mc.pipe.%d", getpid ());
	    if (mkfifo (tcsh_fifo, 0600) == -1)
	    {
		perror (__FILE__": mkfifo");
		use_subshell = FALSE;
		return;
	    }

	    /* Opening the FIFO as O_RDONLY or O_WRONLY causes deadlock */

	    if ((subshell_pipe[READ] = open (tcsh_fifo, O_RDWR)) == -1 ||
		(subshell_pipe[WRITE] = open (tcsh_fifo, O_RDWR)) == -1)
	    {
		fprintf (stderr, _("Couldn't open named pipe %s\n"), tcsh_fifo);
		perror (__FILE__": open");
		use_subshell = FALSE;
		return;
	    }
	}
	else  /* subshell_type is BASH or ZSH */
	    if (pipe (subshell_pipe))
	    {
		perror (__FILE__": couldn't create pipe");
		use_subshell = FALSE;
		return;
	    }

	/* }}} */
    }

    /* {{{ Define a handler for the sigusr1 signal */

#ifdef SYNC_PTY_SIDES
	sigemptyset (&sigusr1_mask);
	sigaddset (&sigusr1_mask, SIGUSR1);
	sigprocmask (SIG_BLOCK, &sigusr1_mask, &old_mask);
	signal (SIGUSR1, sigusr1_handler);
#endif

    /* }}} */
    /* {{{ Fork the subshell */

    subshell_alive = TRUE;
    subshell_stopped = FALSE;
    subshell_pid = fork ();

    if (subshell_pid == -1)
    {
	perror (__FILE__": couldn't spawn the subshell process");
	/* We exit here because, if the process table is full, the */
	/* other method of running user commands won't work either */
	exit (1);
    }

   /* }}} */

    if (subshell_pid == 0)  /* We are in the child process */
    {
	char *init_file = NULL;

	setsid ();  /* Get a fresh terminal session */

	/* {{{ Open the slave side of the pty: again */
	pty_slave = pty_open_slave (pty_name);

	/* This must be done before closing the master side of the pty, */
	/* or it will fail on certain idiotic systems, such as Solaris.	*/

	/* Close master side of pty.  This is important; apart from	*/
	/* freeing up the descriptor for use in the subshell, it also	*/
	/* means that when MC exits, the subshell will get a SIGHUP and	*/
	/* exit too, because there will be no more descriptors pointing	*/
	/* at the master side of the pty and so it will disappear.	*/

	close (subshell_pty);

#ifdef SYNC_PTY_SIDES
	    /* Give our parent process (MC) the go-ahead */
	    kill (getppid (), SIGUSR1);
#endif

	/* }}} */
	/* {{{ Make sure that it has become our controlling terminal */

	/* Redundant on Linux and probably most systems, but just in case: */

#	ifdef TIOCSCTTY
	ioctl (pty_slave, TIOCSCTTY, 0);
#	endif

	/* }}} */
	/* {{{ Configure its terminal modes and window size */

	/* Set up the pty with the same termios flags as our own tty, plus  */
	/* TOSTOP, which keeps background processes from writing to the pty */

	shell_mode.c_lflag |= TOSTOP;  /* So background writers get SIGTTOU */
	if (tcsetattr (pty_slave, TCSANOW, &shell_mode))
	{
	    perror (__FILE__": couldn't set pty terminal modes");
	    _exit (FORK_FAILURE);
	}

	/* Set the pty's size (80x25 by default on Linux) according to the */
	/* size of the real terminal as calculated by ncurses, if possible */
#	if defined TIOCSWINSZ && !defined SCO_FLAVOR
	{
	    struct winsize tty_size;
	    tty_size.ws_row = LINES;
	    tty_size.ws_col = COLS;
	    tty_size.ws_xpixel = tty_size.ws_ypixel = 0;

	    if (ioctl (pty_slave, TIOCSWINSZ, &tty_size))
		perror (__FILE__": couldn't set pty size");
	}
#	endif

	/* }}} */
	/* {{{ Set up the subshell's environment and init file name */

	/* It simplifies things to change to our home directory here, */
	/* and the user's startup file may do a `cd' command anyway   */
	chdir (home_dir);  /* FIXME? What about when we re-run the subshell? */

	switch (subshell_type)
	{
	    case BASH:
		init_file = ".mc/bashrc";
		if (access (init_file, R_OK) == -1)
		    init_file = ".bashrc";

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -