📄 rltty.c
字号:
/* rltty.c -- functions to prepare and restore the terminal for readline's use. *//* Copyright (C) 1992 Free Software Foundation, Inc. This file is part of the GNU Readline Library, a library for reading lines of text with interactive input and history editing. The GNU Readline Library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. The GNU Readline Library 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. The GNU General Public License is often shipped with GNU software, and is generally kept in a file called COPYING or LICENSE. If you do not have a copy of the license, write to the Free Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */#define READLINE_LIBRARY#if defined (HAVE_CONFIG_H)# include <config.h>#endif#include <sys/types.h>#include <signal.h>#include <errno.h>#include <stdio.h>#if defined (HAVE_UNISTD_H)# include <unistd.h>#endif /* HAVE_UNISTD_H */#include "rldefs.h"#if defined (GWINSZ_IN_SYS_IOCTL)# include <sys/ioctl.h>#endif /* GWINSZ_IN_SYS_IOCTL */#include "rltty.h"#include "readline.h"#include "rlprivate.h"#if !defined (errno)extern int errno;#endif /* !errno */rl_vintfunc_t *rl_prep_term_function = rl_prep_terminal;rl_voidfunc_t *rl_deprep_term_function = rl_deprep_terminal;static void block_sigint PARAMS((void));static void release_sigint PARAMS((void));static void set_winsize PARAMS((int));/* **************************************************************** *//* *//* Signal Management *//* *//* **************************************************************** */#if defined (HAVE_POSIX_SIGNALS)static sigset_t sigint_set, sigint_oset;#else /* !HAVE_POSIX_SIGNALS */# if defined (HAVE_BSD_SIGNALS)static int sigint_oldmask;# endif /* HAVE_BSD_SIGNALS */#endif /* !HAVE_POSIX_SIGNALS */static int sigint_blocked;/* Cause SIGINT to not be delivered until the corresponding call to release_sigint(). */static voidblock_sigint (){ if (sigint_blocked) return;#if defined (HAVE_POSIX_SIGNALS) sigemptyset (&sigint_set); sigemptyset (&sigint_oset); sigaddset (&sigint_set, SIGINT); sigprocmask (SIG_BLOCK, &sigint_set, &sigint_oset);#else /* !HAVE_POSIX_SIGNALS */# if defined (HAVE_BSD_SIGNALS) sigint_oldmask = sigblock (sigmask (SIGINT));# else /* !HAVE_BSD_SIGNALS */# if defined (HAVE_USG_SIGHOLD) sighold (SIGINT);# endif /* HAVE_USG_SIGHOLD */# endif /* !HAVE_BSD_SIGNALS */#endif /* !HAVE_POSIX_SIGNALS */ sigint_blocked = 1;}/* Allow SIGINT to be delivered. */static voidrelease_sigint (){ if (sigint_blocked == 0) return;#if defined (HAVE_POSIX_SIGNALS) sigprocmask (SIG_SETMASK, &sigint_oset, (sigset_t *)NULL);#else# if defined (HAVE_BSD_SIGNALS) sigsetmask (sigint_oldmask);# else /* !HAVE_BSD_SIGNALS */# if defined (HAVE_USG_SIGHOLD) sigrelse (SIGINT);# endif /* HAVE_USG_SIGHOLD */# endif /* !HAVE_BSD_SIGNALS */#endif /* !HAVE_POSIX_SIGNALS */ sigint_blocked = 0;}/* **************************************************************** *//* *//* Saving and Restoring the TTY *//* *//* **************************************************************** *//* Non-zero means that the terminal is in a prepped state. */static int terminal_prepped;static _RL_TTY_CHARS _rl_tty_chars, _rl_last_tty_chars;/* If non-zero, means that this process has called tcflow(fd, TCOOFF) and output is suspended. */#if defined (__ksr1__)static int ksrflow;#endif/* Dummy call to force a backgrounded readline to stop before it tries to get the tty settings. */static voidset_winsize (tty) int tty;{#if defined (TIOCGWINSZ) struct winsize w; if (ioctl (tty, TIOCGWINSZ, &w) == 0) (void) ioctl (tty, TIOCSWINSZ, &w);#endif /* TIOCGWINSZ */}#if defined (NEW_TTY_DRIVER)/* Values for the `flags' field of a struct bsdtty. This tells which elements of the struct bsdtty have been fetched from the system and are valid. */#define SGTTY_SET 0x01#define LFLAG_SET 0x02#define TCHARS_SET 0x04#define LTCHARS_SET 0x08struct bsdtty { struct sgttyb sgttyb; /* Basic BSD tty driver information. */ int lflag; /* Local mode flags, like LPASS8. */#if defined (TIOCGETC) struct tchars tchars; /* Terminal special characters, including ^S and ^Q. */#endif#if defined (TIOCGLTC) struct ltchars ltchars; /* 4.2 BSD editing characters */#endif int flags; /* Bitmap saying which parts of the struct are valid. */};#define TIOTYPE struct bsdttystatic TIOTYPE otio;static void save_tty_chars PARAMS((TIOTYPE *));static int _get_tty_settings PARAMS((int, TIOTYPE *));static int get_tty_settings PARAMS((int, TIOTYPE *));static int _set_tty_settings PARAMS((int, TIOTYPE *));static int set_tty_settings PARAMS((int, TIOTYPE *));static void prepare_terminal_settings PARAMS((int, TIOTYPE, TIOTYPE *));static void set_special_char PARAMS((Keymap, TIOTYPE *, int, rl_command_func_t));static voidsave_tty_chars (tiop) TIOTYPE *tiop;{ _rl_last_tty_chars = _rl_tty_chars; if (tiop->flags & SGTTY_SET) { _rl_tty_chars.t_erase = tiop->sgttyb.sg_erase; _rl_tty_chars.t_kill = tiop->sgttyb.sg_kill; } if (tiop->flags & TCHARS_SET) { _rl_tty_chars.t_intr = tiop->tchars.t_intrc; _rl_tty_chars.t_quit = tiop->tchars.t_quitc; _rl_tty_chars.t_start = tiop->tchars.t_startc; _rl_tty_chars.t_stop = tiop->tchars.t_stopc; _rl_tty_chars.t_eof = tiop->tchars.t_eofc; _rl_tty_chars.t_eol = '\n'; _rl_tty_chars.t_eol2 = tiop->tchars.t_brkc; } if (tiop->flags & LTCHARS_SET) { _rl_tty_chars.t_susp = tiop->ltchars.t_suspc; _rl_tty_chars.t_dsusp = tiop->ltchars.t_dsuspc; _rl_tty_chars.t_reprint = tiop->ltchars.t_rprntc; _rl_tty_chars.t_flush = tiop->ltchars.t_flushc; _rl_tty_chars.t_werase = tiop->ltchars.t_werasc; _rl_tty_chars.t_lnext = tiop->ltchars.t_lnextc; } _rl_tty_chars.t_status = -1;}static intget_tty_settings (tty, tiop) int tty; TIOTYPE *tiop;{ set_winsize (tty); tiop->flags = tiop->lflag = 0; if (ioctl (tty, TIOCGETP, &(tiop->sgttyb)) < 0) return -1; tiop->flags |= SGTTY_SET;#if defined (TIOCLGET) if (ioctl (tty, TIOCLGET, &(tiop->lflag)) == 0) tiop->flags |= LFLAG_SET;#endif#if defined (TIOCGETC) if (ioctl (tty, TIOCGETC, &(tiop->tchars)) == 0) tiop->flags |= TCHARS_SET;#endif#if defined (TIOCGLTC) if (ioctl (tty, TIOCGLTC, &(tiop->ltchars)) == 0) tiop->flags |= LTCHARS_SET;#endif return 0;}static intset_tty_settings (tty, tiop) int tty; TIOTYPE *tiop;{ if (tiop->flags & SGTTY_SET) { ioctl (tty, TIOCSETN, &(tiop->sgttyb)); tiop->flags &= ~SGTTY_SET; } readline_echoing_p = 1;#if defined (TIOCLSET) if (tiop->flags & LFLAG_SET) { ioctl (tty, TIOCLSET, &(tiop->lflag)); tiop->flags &= ~LFLAG_SET; }#endif#if defined (TIOCSETC) if (tiop->flags & TCHARS_SET) { ioctl (tty, TIOCSETC, &(tiop->tchars)); tiop->flags &= ~TCHARS_SET; }#endif#if defined (TIOCSLTC) if (tiop->flags & LTCHARS_SET) { ioctl (tty, TIOCSLTC, &(tiop->ltchars)); tiop->flags &= ~LTCHARS_SET; }#endif return 0;}static voidprepare_terminal_settings (meta_flag, oldtio, tiop) int meta_flag; TIOTYPE oldtio, *tiop;{ readline_echoing_p = (oldtio.sgttyb.sg_flags & ECHO); /* Copy the original settings to the structure we're going to use for our settings. */ tiop->sgttyb = oldtio.sgttyb; tiop->lflag = oldtio.lflag;#if defined (TIOCGETC) tiop->tchars = oldtio.tchars;#endif#if defined (TIOCGLTC) tiop->ltchars = oldtio.ltchars;#endif tiop->flags = oldtio.flags; /* First, the basic settings to put us into character-at-a-time, no-echo input mode. */ tiop->sgttyb.sg_flags &= ~(ECHO | CRMOD); tiop->sgttyb.sg_flags |= CBREAK; /* If this terminal doesn't care how the 8th bit is used, then we can use it for the meta-key. If only one of even or odd parity is specified, then the terminal is using parity, and we cannot. */#if !defined (ANYP)# define ANYP (EVENP | ODDP)#endif if (((oldtio.sgttyb.sg_flags & ANYP) == ANYP) || ((oldtio.sgttyb.sg_flags & ANYP) == 0)) { tiop->sgttyb.sg_flags |= ANYP; /* Hack on local mode flags if we can. */#if defined (TIOCLGET)# if defined (LPASS8) tiop->lflag |= LPASS8;# endif /* LPASS8 */#endif /* TIOCLGET */ }#if defined (TIOCGETC)# if defined (USE_XON_XOFF) /* Get rid of terminal output start and stop characters. */ tiop->tchars.t_stopc = -1; /* C-s */ tiop->tchars.t_startc = -1; /* C-q */ /* If there is an XON character, bind it to restart the output. */ if (oldtio.tchars.t_startc != -1) rl_bind_key (oldtio.tchars.t_startc, rl_restart_output);# endif /* USE_XON_XOFF */ /* If there is an EOF char, bind _rl_eof_char to it. */ if (oldtio.tchars.t_eofc != -1) _rl_eof_char = oldtio.tchars.t_eofc;# if defined (NO_KILL_INTR) /* Get rid of terminal-generated SIGQUIT and SIGINT. */ tiop->tchars.t_quitc = -1; /* C-\ */ tiop->tchars.t_intrc = -1; /* C-c */# endif /* NO_KILL_INTR */#endif /* TIOCGETC */#if defined (TIOCGLTC) /* Make the interrupt keys go away. Just enough to make people happy. */ tiop->ltchars.t_dsuspc = -1; /* C-y */ tiop->ltchars.t_lnextc = -1; /* C-v */#endif /* TIOCGLTC */}#else /* !defined (NEW_TTY_DRIVER) */#if !defined (VMIN)# define VMIN VEOF#endif#if !defined (VTIME)# define VTIME VEOL#endif#if defined (TERMIOS_TTY_DRIVER)# define TIOTYPE struct termios# define DRAIN_OUTPUT(fd) tcdrain (fd)# define GETATTR(tty, tiop) (tcgetattr (tty, tiop))# ifdef M_UNIX# define SETATTR(tty, tiop) (tcsetattr (tty, TCSANOW, tiop))# else# define SETATTR(tty, tiop) (tcsetattr (tty, TCSADRAIN, tiop))# endif /* !M_UNIX */#else# define TIOTYPE struct termio# define DRAIN_OUTPUT(fd)# define GETATTR(tty, tiop) (ioctl (tty, TCGETA, tiop))# define SETATTR(tty, tiop) (ioctl (tty, TCSETAW, tiop))#endif /* !TERMIOS_TTY_DRIVER */static TIOTYPE otio;static void save_tty_chars PARAMS((TIOTYPE *));static int _get_tty_settings PARAMS((int, TIOTYPE *));static int get_tty_settings PARAMS((int, TIOTYPE *));static int _set_tty_settings PARAMS((int, TIOTYPE *));static int set_tty_settings PARAMS((int, TIOTYPE *));static void prepare_terminal_settings PARAMS((int, TIOTYPE, TIOTYPE *));static void set_special_char PARAMS((Keymap, TIOTYPE *, int, rl_command_func_t));static void _rl_bind_tty_special_chars PARAMS((Keymap, TIOTYPE));#if defined (FLUSHO)# define OUTPUT_BEING_FLUSHED(tp) (tp->c_lflag & FLUSHO)#else# define OUTPUT_BEING_FLUSHED(tp) 0#endifstatic voidsave_tty_chars (tiop) TIOTYPE *tiop;{ _rl_last_tty_chars = _rl_tty_chars; _rl_tty_chars.t_eof = tiop->c_cc[VEOF]; _rl_tty_chars.t_eol = tiop->c_cc[VEOL];#ifdef VEOL2 _rl_tty_chars.t_eol2 = tiop->c_cc[VEOL2];#endif _rl_tty_chars.t_erase = tiop->c_cc[VERASE];#ifdef VWERASE _rl_tty_chars.t_werase = tiop->c_cc[VWERASE];#endif _rl_tty_chars.t_kill = tiop->c_cc[VKILL];#ifdef VREPRINT _rl_tty_chars.t_reprint = tiop->c_cc[VREPRINT];#endif _rl_tty_chars.t_intr = tiop->c_cc[VINTR]; _rl_tty_chars.t_quit = tiop->c_cc[VQUIT];#ifdef VSUSP _rl_tty_chars.t_susp = tiop->c_cc[VSUSP];#endif#ifdef VDSUSP _rl_tty_chars.t_dsusp = tiop->c_cc[VDSUSP];#endif#ifdef VSTART _rl_tty_chars.t_start = tiop->c_cc[VSTART];#endif#ifdef VSTOP _rl_tty_chars.t_stop = tiop->c_cc[VSTOP];#endif#ifdef VLNEXT _rl_tty_chars.t_lnext = tiop->c_cc[VLNEXT];#endif#ifdef VDISCARD _rl_tty_chars.t_flush = tiop->c_cc[VDISCARD];#endif#ifdef VSTATUS _rl_tty_chars.t_status = tiop->c_cc[VSTATUS];#endif}#if defined (_AIX) || defined (_AIX41)/* Currently this is only used on AIX */static voidrltty_warning (msg) char *msg;{ fprintf (stderr, "readline: warning: %s\n", msg);}#endif#if defined (_AIX)voidsetopost(tp)TIOTYPE *tp;{ if ((tp->c_oflag & OPOST) == 0) { rltty_warning ("turning on OPOST for terminal\r"); tp->c_oflag |= OPOST|ONLCR; }}#endifstatic int_get_tty_settings (tty, tiop) int tty; TIOTYPE *tiop;{ int ioctl_ret;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -