summaryrefslogtreecommitdiff
path: root/cmd-line-utils/readline/terminal.c
diff options
context:
space:
mode:
Diffstat (limited to 'cmd-line-utils/readline/terminal.c')
-rw-r--r--cmd-line-utils/readline/terminal.c141
1 files changed, 108 insertions, 33 deletions
diff --git a/cmd-line-utils/readline/terminal.c b/cmd-line-utils/readline/terminal.c
index 4b900c5d860..547f6f5dfe5 100644
--- a/cmd-line-utils/readline/terminal.c
+++ b/cmd-line-utils/readline/terminal.c
@@ -1,6 +1,6 @@
/* terminal.c -- controlling the terminal with termcap. */
-/* Copyright (C) 1996 Free Software Foundation, Inc.
+/* Copyright (C) 1996-2006 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.
@@ -21,7 +21,9 @@
59 Temple Place, Suite 330, Boston, MA 02111 USA. */
#define READLINE_LIBRARY
-#include "config_readline.h"
+#if defined (HAVE_CONFIG_H)
+# include <config.h>
+#endif
#include <sys/types.h>
#include "posixstat.h"
@@ -64,9 +66,25 @@
#include "rlshell.h"
#include "xmalloc.h"
+#if defined (__MINGW32__)
+# include <windows.h>
+# include <wincon.h>
+
+static void _win_get_screensize PARAMS((int *, int *));
+#endif
+
+#if defined (__EMX__)
+static void _emx_get_screensize PARAMS((int *, int *));
+#endif
+
#define CUSTOM_REDISPLAY_FUNC() (rl_redisplay_function != rl_redisplay)
#define CUSTOM_INPUT_FUNC() (rl_getc_function != rl_getc)
+/* If the calling application sets this to a non-zero value, readline will
+ use the $LINES and $COLUMNS environment variables to set its idea of the
+ window size before interrogating the kernel. */
+int rl_prefer_env_winsize = 0;
+
/* **************************************************************** */
/* */
/* Terminal and Termcap */
@@ -107,9 +125,7 @@ char *_rl_term_IC;
char *_rl_term_dc;
char *_rl_term_DC;
-#if defined (HACK_TERMCAP_MOTION)
char *_rl_term_forward_char;
-#endif /* HACK_TERMCAP_MOTION */
/* How to go up a line. */
char *_rl_term_up;
@@ -118,7 +134,7 @@ char *_rl_term_up;
static char *_rl_visible_bell;
/* Non-zero means the terminal can auto-wrap lines. */
-int _rl_term_autowrap;
+int _rl_term_autowrap = -1;
/* Non-zero means that this terminal has a meta key. */
static int term_has_meta;
@@ -143,6 +159,9 @@ static char *_rl_term_kh;
static char *_rl_term_kH;
static char *_rl_term_at7; /* @7 */
+/* Delete key */
+static char *_rl_term_kD;
+
/* Insert key */
static char *_rl_term_kI;
@@ -177,6 +196,26 @@ _emx_get_screensize (swp, shp)
}
#endif
+#if defined (__MINGW32__)
+static void
+_win_get_screensize (swp, shp)
+ int *swp, *shp;
+{
+ HANDLE hConOut;
+ CONSOLE_SCREEN_BUFFER_INFO scr;
+
+ hConOut = GetStdHandle (STD_OUTPUT_HANDLE);
+ if (hConOut != INVALID_HANDLE_VALUE)
+ {
+ if (GetConsoleScreenBufferInfo (hConOut, &scr))
+ {
+ *swp = scr.dwSize.X;
+ *shp = scr.srWindow.Bottom - scr.srWindow.Top + 1;
+ }
+ }
+}
+#endif
+
/* Get readline's idea of the screen size. TTY is a file descriptor open
to the terminal. If IGNORE_ENV is true, we do not pay attention to the
values of $LINES and $COLUMNS. The tests for TERM_STRING_BUFFER being
@@ -189,26 +228,42 @@ _rl_get_screen_size (tty, ignore_env)
#if defined (TIOCGWINSZ)
struct winsize window_size;
#endif /* TIOCGWINSZ */
+ int wr, wc;
+ wr = wc = -1;
#if defined (TIOCGWINSZ)
if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
{
- _rl_screenwidth = (int) window_size.ws_col;
- _rl_screenheight = (int) window_size.ws_row;
+ wc = (int) window_size.ws_col;
+ wr = (int) window_size.ws_row;
}
#endif /* TIOCGWINSZ */
#if defined (__EMX__)
- _emx_get_screensize (&_rl_screenwidth, &_rl_screenheight);
+ _emx_get_screensize (&wc, &wr);
+#elif defined (__MINGW32__)
+ _win_get_screensize (&wc, &wr);
#endif
+ if (ignore_env || rl_prefer_env_winsize == 0)
+ {
+ _rl_screenwidth = wc;
+ _rl_screenheight = wr;
+ }
+ else
+ _rl_screenwidth = _rl_screenheight = -1;
+
/* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
- is unset. */
+ is unset. If we prefer the environment, check it first before
+ assigning the value returned by the kernel. */
if (_rl_screenwidth <= 0)
{
if (ignore_env == 0 && (ss = sh_get_env_value ("COLUMNS")))
_rl_screenwidth = atoi (ss);
+ if (_rl_screenwidth <= 0)
+ _rl_screenwidth = wc;
+
#if !defined (__DJGPP__)
if (_rl_screenwidth <= 0 && term_string_buffer)
_rl_screenwidth = tgetnum ("co");
@@ -222,6 +277,9 @@ _rl_get_screen_size (tty, ignore_env)
if (ignore_env == 0 && (ss = sh_get_env_value ("LINES")))
_rl_screenheight = atoi (ss);
+ if (_rl_screenheight <= 0)
+ _rl_screenheight = wr;
+
#if !defined (__DJGPP__)
if (_rl_screenheight <= 0 && term_string_buffer)
_rl_screenheight = tgetnum ("li");
@@ -250,16 +308,20 @@ void
_rl_set_screen_size (rows, cols)
int rows, cols;
{
- if (rows == 0 || cols == 0)
- return;
-
- _rl_screenheight = rows;
- _rl_screenwidth = cols;
+ if (_rl_term_autowrap == -1)
+ _rl_init_terminal_io (rl_terminal_name);
- if (_rl_term_autowrap == 0)
- _rl_screenwidth--;
+ if (rows > 0)
+ _rl_screenheight = rows;
+ if (cols > 0)
+ {
+ _rl_screenwidth = cols;
+ if (_rl_term_autowrap == 0)
+ _rl_screenwidth--;
+ }
- _rl_screenchars = _rl_screenwidth * _rl_screenheight;
+ if (rows > 0 || cols > 0)
+ _rl_screenchars = _rl_screenwidth * _rl_screenheight;
}
void
@@ -278,6 +340,12 @@ rl_get_screen_size (rows, cols)
if (cols)
*cols = _rl_screenwidth;
}
+
+void
+rl_reset_screen_size ()
+{
+ _rl_get_screen_size (fileno (rl_instream), 0);
+}
void
rl_resize_terminal ()
@@ -311,6 +379,7 @@ static struct _tc_string tc_strings[] =
{ "ei", &_rl_term_ei },
{ "ic", &_rl_term_ic },
{ "im", &_rl_term_im },
+ { "kD", &_rl_term_kD }, /* delete */
{ "kH", &_rl_term_kH }, /* home down ?? */
{ "kI", &_rl_term_kI }, /* insert */
{ "kd", &_rl_term_kd },
@@ -323,9 +392,7 @@ static struct _tc_string tc_strings[] =
{ "le", &_rl_term_backspace },
{ "mm", &_rl_term_mm },
{ "mo", &_rl_term_mo },
-#if defined (HACK_TERMCAP_MOTION)
{ "nd", &_rl_term_forward_char },
-#endif
{ "pc", &_rl_term_pc },
{ "up", &_rl_term_up },
{ "vb", &_rl_visible_bell },
@@ -344,7 +411,7 @@ get_term_capabilities (bp)
#if !defined (__DJGPP__) /* XXX - doesn't DJGPP have a termcap library? */
register int i;
- for (i = 0; i < (int) NUM_TC_STRINGS; i++)
+ for (i = 0; i < NUM_TC_STRINGS; i++)
*(tc_strings[i].tc_value) = tgetstr ((char *)tc_strings[i].tc_var, bp);
#endif
tcap_initialized = 1;
@@ -361,7 +428,6 @@ _rl_init_terminal_io (terminal_name)
term = terminal_name ? terminal_name : sh_get_env_value ("TERM");
_rl_term_clrpag = _rl_term_cr = _rl_term_clreol = (char *)NULL;
tty = rl_instream ? fileno (rl_instream) : 0;
- _rl_screenwidth = _rl_screenheight = 0;
if (term == 0)
term = "dumb";
@@ -394,12 +460,17 @@ _rl_init_terminal_io (terminal_name)
_rl_term_autowrap = 0; /* used by _rl_get_screen_size */
+ /* Allow calling application to set default height and width, using
+ rl_set_screen_size */
+ if (_rl_screenwidth <= 0 || _rl_screenheight <= 0)
+ {
#if defined (__EMX__)
- _emx_get_screensize (&_rl_screenwidth, &_rl_screenheight);
- _rl_screenwidth--;
+ _emx_get_screensize (&_rl_screenwidth, &_rl_screenheight);
+ _rl_screenwidth--;
#else /* !__EMX__ */
- _rl_get_screen_size (tty, 0);
+ _rl_get_screen_size (tty, 0);
#endif /* !__EMX__ */
+ }
/* Defaults. */
if (_rl_screenwidth <= 0 || _rl_screenheight <= 0)
@@ -410,24 +481,22 @@ _rl_init_terminal_io (terminal_name)
/* Everything below here is used by the redisplay code (tputs). */
_rl_screenchars = _rl_screenwidth * _rl_screenheight;
- _rl_term_cr = (char*) "\r";
+ _rl_term_cr = "\r";
_rl_term_im = _rl_term_ei = _rl_term_ic = _rl_term_IC = (char *)NULL;
_rl_term_up = _rl_term_dc = _rl_term_DC = _rl_visible_bell = (char *)NULL;
_rl_term_ku = _rl_term_kd = _rl_term_kl = _rl_term_kr = (char *)NULL;
- _rl_term_kh = _rl_term_kH = _rl_term_kI = (char *)NULL;
+ _rl_term_kh = _rl_term_kH = _rl_term_kI = _rl_term_kD = (char *)NULL;
_rl_term_ks = _rl_term_ke = _rl_term_at7 = (char *)NULL;
_rl_term_mm = _rl_term_mo = (char *)NULL;
_rl_term_ve = _rl_term_vs = (char *)NULL;
-#if defined (HACK_TERMCAP_MOTION)
- term_forward_char = (char *)NULL;
-#endif
+ _rl_term_forward_char = (char *)NULL;
_rl_terminal_can_insert = term_has_meta = 0;
/* Reasonable defaults for tgoto(). Readline currently only uses
tgoto if _rl_term_IC or _rl_term_DC is defined, but just in case we
change that later... */
PC = '\0';
- BC = _rl_term_backspace = (char*) "\b";
+ BC = _rl_term_backspace = "\b";
UP = _rl_term_up;
return 0;
@@ -442,11 +511,14 @@ _rl_init_terminal_io (terminal_name)
UP = _rl_term_up;
if (!_rl_term_cr)
- _rl_term_cr = (char*) "\r";
+ _rl_term_cr = "\r";
_rl_term_autowrap = tgetflag ("am") && tgetflag ("xn");
- _rl_get_screen_size (tty, 0);
+ /* Allow calling application to set default height and width, using
+ rl_set_screen_size */
+ if (_rl_screenwidth <= 0 || _rl_screenheight <= 0)
+ _rl_get_screen_size (tty, 0);
/* "An application program can assume that the terminal can do
character insertion if *any one of* the capabilities `IC',
@@ -491,6 +563,8 @@ bind_termcap_arrow_keys (map)
rl_bind_keyseq_if_unbound (_rl_term_kh, rl_beg_of_line); /* Home */
rl_bind_keyseq_if_unbound (_rl_term_at7, rl_end_of_line); /* End */
+ rl_bind_keyseq_if_unbound (_rl_term_kD, rl_delete);
+
_rl_keymap = xkeymap;
}
@@ -502,7 +576,7 @@ rl_get_termcap (cap)
if (tcap_initialized == 0)
return ((char *)NULL);
- for (i = 0; i < (int) NUM_TC_STRINGS; i++)
+ for (i = 0; i < NUM_TC_STRINGS; i++)
{
if (tc_strings[i].tc_var[0] == cap[0] && strcmp (tc_strings[i].tc_var, cap) == 0)
return *(tc_strings[i].tc_value);
@@ -516,6 +590,7 @@ int
rl_reset_terminal (terminal_name)
const char *terminal_name;
{
+ _rl_screenwidth = _rl_screenheight = 0;
_rl_init_terminal_io (terminal_name);
return 0;
}