Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1 | /* terminal.c -- controlling the terminal with termcap. */ |
| 2 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3 | /* Copyright (C) 1996-2009 Free Software Foundation, Inc. |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 5 | This file is part of the GNU Readline Library (Readline), a library |
| 6 | for reading lines of text with interactive input and history editing. |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 7 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 8 | Readline is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 11 | (at your option) any later version. |
| 12 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 13 | Readline is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 16 | GNU General Public License for more details. |
| 17 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 18 | You should have received a copy of the GNU General Public License |
| 19 | along with Readline. If not, see <http://www.gnu.org/licenses/>. |
| 20 | */ |
| 21 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 22 | #define READLINE_LIBRARY |
| 23 | |
| 24 | #if defined (HAVE_CONFIG_H) |
| 25 | # include <config.h> |
| 26 | #endif |
| 27 | |
| 28 | #include <sys/types.h> |
| 29 | #include "posixstat.h" |
| 30 | #include <fcntl.h> |
| 31 | #if defined (HAVE_SYS_FILE_H) |
| 32 | # include <sys/file.h> |
| 33 | #endif /* HAVE_SYS_FILE_H */ |
| 34 | |
| 35 | #if defined (HAVE_UNISTD_H) |
| 36 | # include <unistd.h> |
| 37 | #endif /* HAVE_UNISTD_H */ |
| 38 | |
| 39 | #if defined (HAVE_STDLIB_H) |
| 40 | # include <stdlib.h> |
| 41 | #else |
| 42 | # include "ansi_stdlib.h" |
| 43 | #endif /* HAVE_STDLIB_H */ |
| 44 | |
| 45 | #if defined (HAVE_LOCALE_H) |
| 46 | # include <locale.h> |
| 47 | #endif |
| 48 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 49 | #include <stdio.h> |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 50 | |
| 51 | /* System-specific feature definitions and include files. */ |
| 52 | #include "rldefs.h" |
| 53 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 54 | #if defined (GWINSZ_IN_SYS_IOCTL) && !defined (TIOCGWINSZ) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 55 | # include <sys/ioctl.h> |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 56 | #endif /* GWINSZ_IN_SYS_IOCTL && !TIOCGWINSZ */ |
| 57 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 58 | #ifdef __MSDOS__ |
| 59 | # include <pc.h> |
| 60 | #endif |
| 61 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 62 | #include "rltty.h" |
| 63 | #include "tcap.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 64 | |
| 65 | /* Some standard library routines. */ |
| 66 | #include "readline.h" |
| 67 | #include "history.h" |
| 68 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 69 | #include "rlprivate.h" |
| 70 | #include "rlshell.h" |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 71 | #include "xmalloc.h" |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 72 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 73 | #if defined (__MINGW32__) |
| 74 | # include <windows.h> |
| 75 | # include <wincon.h> |
| 76 | |
| 77 | static void _win_get_screensize PARAMS((int *, int *)); |
| 78 | #endif |
| 79 | |
| 80 | #if defined (__EMX__) |
| 81 | static void _emx_get_screensize PARAMS((int *, int *)); |
| 82 | #endif |
| 83 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 84 | /* If the calling application sets this to a non-zero value, readline will |
| 85 | use the $LINES and $COLUMNS environment variables to set its idea of the |
| 86 | window size before interrogating the kernel. */ |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 87 | int rl_prefer_env_winsize = 0; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 88 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 89 | /* If this is non-zero, readline will set LINES and COLUMNS in the |
| 90 | environment when it handles SIGWINCH. */ |
| 91 | int rl_change_environment = 1; |
| 92 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 93 | /* **************************************************************** */ |
| 94 | /* */ |
| 95 | /* Terminal and Termcap */ |
| 96 | /* */ |
| 97 | /* **************************************************************** */ |
| 98 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 99 | #ifndef __MSDOS__ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 100 | static char *term_buffer = (char *)NULL; |
| 101 | static char *term_string_buffer = (char *)NULL; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 102 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 103 | |
| 104 | static int tcap_initialized; |
| 105 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 106 | #if !defined (__linux__) && !defined (NCURSES_VERSION) |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 107 | # if defined (__EMX__) || defined (NEED_EXTERN_PC) |
| 108 | extern |
| 109 | # endif /* __EMX__ || NEED_EXTERN_PC */ |
| 110 | char PC, *BC, *UP; |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 111 | #endif /* !__linux__ && !NCURSES_VERSION */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 112 | |
| 113 | /* Some strings to control terminal actions. These are output by tputs (). */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 114 | char *_rl_term_clreol; |
| 115 | char *_rl_term_clrpag; |
| 116 | char *_rl_term_cr; |
| 117 | char *_rl_term_backspace; |
| 118 | char *_rl_term_goto; |
| 119 | char *_rl_term_pc; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 120 | |
| 121 | /* Non-zero if we determine that the terminal can do character insertion. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 122 | int _rl_terminal_can_insert = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 123 | |
| 124 | /* How to insert characters. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 125 | char *_rl_term_im; |
| 126 | char *_rl_term_ei; |
| 127 | char *_rl_term_ic; |
| 128 | char *_rl_term_ip; |
| 129 | char *_rl_term_IC; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 130 | |
| 131 | /* How to delete characters. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 132 | char *_rl_term_dc; |
| 133 | char *_rl_term_DC; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 134 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 135 | char *_rl_term_forward_char; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 136 | |
| 137 | /* How to go up a line. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 138 | char *_rl_term_up; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 139 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 140 | /* A visible bell; char if the terminal can be made to flash the screen. */ |
| 141 | static char *_rl_visible_bell; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 142 | |
| 143 | /* Non-zero means the terminal can auto-wrap lines. */ |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 144 | int _rl_term_autowrap = -1; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 145 | |
| 146 | /* Non-zero means that this terminal has a meta key. */ |
| 147 | static int term_has_meta; |
| 148 | |
| 149 | /* The sequences to write to turn on and off the meta key, if this |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 150 | terminal has one. */ |
| 151 | static char *_rl_term_mm; |
| 152 | static char *_rl_term_mo; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 153 | |
| 154 | /* The key sequences output by the arrow keys, if this terminal has any. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 155 | static char *_rl_term_ku; |
| 156 | static char *_rl_term_kd; |
| 157 | static char *_rl_term_kr; |
| 158 | static char *_rl_term_kl; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 159 | |
| 160 | /* How to initialize and reset the arrow keys, if this terminal has any. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 161 | static char *_rl_term_ks; |
| 162 | static char *_rl_term_ke; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 163 | |
| 164 | /* The key sequences sent by the Home and End keys, if any. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 165 | static char *_rl_term_kh; |
| 166 | static char *_rl_term_kH; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 167 | static char *_rl_term_at7; /* @7 */ |
| 168 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 169 | /* Delete key */ |
| 170 | static char *_rl_term_kD; |
| 171 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 172 | /* Insert key */ |
| 173 | static char *_rl_term_kI; |
| 174 | |
| 175 | /* Cursor control */ |
| 176 | static char *_rl_term_vs; /* very visible */ |
| 177 | static char *_rl_term_ve; /* normal */ |
| 178 | |
| 179 | static void bind_termcap_arrow_keys PARAMS((Keymap)); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 180 | |
| 181 | /* Variables that hold the screen dimensions, used by the display code. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 182 | int _rl_screenwidth, _rl_screenheight, _rl_screenchars; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 183 | |
| 184 | /* Non-zero means the user wants to enable the keypad. */ |
| 185 | int _rl_enable_keypad; |
| 186 | |
| 187 | /* Non-zero means the user wants to enable a meta key. */ |
| 188 | int _rl_enable_meta = 1; |
| 189 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 190 | #if defined (__EMX__) |
| 191 | static void |
| 192 | _emx_get_screensize (swp, shp) |
| 193 | int *swp, *shp; |
| 194 | { |
| 195 | int sz[2]; |
| 196 | |
| 197 | _scrsize (sz); |
| 198 | |
| 199 | if (swp) |
| 200 | *swp = sz[0]; |
| 201 | if (shp) |
| 202 | *shp = sz[1]; |
| 203 | } |
| 204 | #endif |
| 205 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 206 | #if defined (__MINGW32__) |
| 207 | static void |
| 208 | _win_get_screensize (swp, shp) |
| 209 | int *swp, *shp; |
| 210 | { |
| 211 | HANDLE hConOut; |
| 212 | CONSOLE_SCREEN_BUFFER_INFO scr; |
| 213 | |
| 214 | hConOut = GetStdHandle (STD_OUTPUT_HANDLE); |
| 215 | if (hConOut != INVALID_HANDLE_VALUE) |
| 216 | { |
| 217 | if (GetConsoleScreenBufferInfo (hConOut, &scr)) |
| 218 | { |
| 219 | *swp = scr.dwSize.X; |
| 220 | *shp = scr.srWindow.Bottom - scr.srWindow.Top + 1; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | #endif |
| 225 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 226 | /* Get readline's idea of the screen size. TTY is a file descriptor open |
| 227 | to the terminal. If IGNORE_ENV is true, we do not pay attention to the |
| 228 | values of $LINES and $COLUMNS. The tests for TERM_STRING_BUFFER being |
| 229 | non-null serve to check whether or not we have initialized termcap. */ |
| 230 | void |
| 231 | _rl_get_screen_size (tty, ignore_env) |
| 232 | int tty, ignore_env; |
| 233 | { |
| 234 | char *ss; |
| 235 | #if defined (TIOCGWINSZ) |
| 236 | struct winsize window_size; |
| 237 | #endif /* TIOCGWINSZ */ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 238 | int wr, wc; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 239 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 240 | wr = wc = -1; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 241 | #if defined (TIOCGWINSZ) |
| 242 | if (ioctl (tty, TIOCGWINSZ, &window_size) == 0) |
| 243 | { |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 244 | wc = (int) window_size.ws_col; |
| 245 | wr = (int) window_size.ws_row; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 246 | } |
| 247 | #endif /* TIOCGWINSZ */ |
| 248 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 249 | #if defined (__EMX__) |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 250 | _emx_get_screensize (&wc, &wr); |
| 251 | #elif defined (__MINGW32__) |
| 252 | _win_get_screensize (&wc, &wr); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 253 | #endif |
| 254 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 255 | if (ignore_env || rl_prefer_env_winsize == 0) |
| 256 | { |
| 257 | _rl_screenwidth = wc; |
| 258 | _rl_screenheight = wr; |
| 259 | } |
| 260 | else |
| 261 | _rl_screenwidth = _rl_screenheight = -1; |
| 262 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 263 | /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 264 | is unset. If we prefer the environment, check it first before |
| 265 | assigning the value returned by the kernel. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 266 | if (_rl_screenwidth <= 0) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 267 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 268 | if (ignore_env == 0 && (ss = sh_get_env_value ("COLUMNS"))) |
| 269 | _rl_screenwidth = atoi (ss); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 270 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 271 | if (_rl_screenwidth <= 0) |
| 272 | _rl_screenwidth = wc; |
| 273 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 274 | #if defined (__DJGPP__) |
| 275 | if (_rl_screenwidth <= 0) |
| 276 | _rl_screenwidth = ScreenCols (); |
| 277 | #else |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 278 | if (_rl_screenwidth <= 0 && term_string_buffer) |
| 279 | _rl_screenwidth = tgetnum ("co"); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 280 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /* Environment variable LINES overrides setting of "li" if IGNORE_ENV |
| 284 | is unset. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 285 | if (_rl_screenheight <= 0) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 286 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 287 | if (ignore_env == 0 && (ss = sh_get_env_value ("LINES"))) |
| 288 | _rl_screenheight = atoi (ss); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 289 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 290 | if (_rl_screenheight <= 0) |
| 291 | _rl_screenheight = wr; |
| 292 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 293 | #if defined (__DJGPP__) |
| 294 | if (_rl_screenheight <= 0) |
| 295 | _rl_screenheight = ScreenRows (); |
| 296 | #else |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 297 | if (_rl_screenheight <= 0 && term_string_buffer) |
| 298 | _rl_screenheight = tgetnum ("li"); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 299 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /* If all else fails, default to 80x24 terminal. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 303 | if (_rl_screenwidth <= 1) |
| 304 | _rl_screenwidth = 80; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 305 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 306 | if (_rl_screenheight <= 0) |
| 307 | _rl_screenheight = 24; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 308 | |
| 309 | /* If we're being compiled as part of bash, set the environment |
| 310 | variables $LINES and $COLUMNS to new values. Otherwise, just |
| 311 | do a pair of putenv () or setenv () calls. */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 312 | if (rl_change_environment) |
| 313 | sh_set_lines_and_columns (_rl_screenheight, _rl_screenwidth); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 314 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 315 | if (_rl_term_autowrap == 0) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 316 | _rl_screenwidth--; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 317 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 318 | _rl_screenchars = _rl_screenwidth * _rl_screenheight; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | void |
| 322 | _rl_set_screen_size (rows, cols) |
| 323 | int rows, cols; |
| 324 | { |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 325 | if (_rl_term_autowrap == -1) |
| 326 | _rl_init_terminal_io (rl_terminal_name); |
| 327 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 328 | if (rows > 0) |
| 329 | _rl_screenheight = rows; |
| 330 | if (cols > 0) |
| 331 | { |
| 332 | _rl_screenwidth = cols; |
| 333 | if (_rl_term_autowrap == 0) |
| 334 | _rl_screenwidth--; |
| 335 | } |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 336 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 337 | if (rows > 0 || cols > 0) |
| 338 | _rl_screenchars = _rl_screenwidth * _rl_screenheight; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 341 | void |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 342 | rl_set_screen_size (rows, cols) |
| 343 | int rows, cols; |
| 344 | { |
| 345 | _rl_set_screen_size (rows, cols); |
| 346 | } |
| 347 | |
| 348 | void |
| 349 | rl_get_screen_size (rows, cols) |
| 350 | int *rows, *cols; |
| 351 | { |
| 352 | if (rows) |
| 353 | *rows = _rl_screenheight; |
| 354 | if (cols) |
| 355 | *cols = _rl_screenwidth; |
| 356 | } |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 357 | |
| 358 | void |
| 359 | rl_reset_screen_size () |
| 360 | { |
| 361 | _rl_get_screen_size (fileno (rl_instream), 0); |
| 362 | } |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 363 | |
| 364 | void |
| 365 | _rl_sigwinch_resize_terminal () |
| 366 | { |
| 367 | _rl_get_screen_size (fileno (rl_instream), 1); |
| 368 | } |
| 369 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 370 | void |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 371 | rl_resize_terminal () |
| 372 | { |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 373 | _rl_get_screen_size (fileno (rl_instream), 1); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 374 | if (_rl_echoing_p) |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 375 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 376 | if (CUSTOM_REDISPLAY_FUNC ()) |
| 377 | rl_forced_update_display (); |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 378 | else if (RL_ISSTATE(RL_STATE_REDISPLAYING) == 0) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 379 | _rl_redisplay_after_sigwinch (); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 383 | struct _tc_string { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 384 | const char * const tc_var; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 385 | char **tc_value; |
| 386 | }; |
| 387 | |
| 388 | /* This should be kept sorted, just in case we decide to change the |
| 389 | search algorithm to something smarter. */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 390 | static const struct _tc_string tc_strings[] = |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 391 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 392 | { "@7", &_rl_term_at7 }, |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 393 | { "DC", &_rl_term_DC }, |
| 394 | { "IC", &_rl_term_IC }, |
| 395 | { "ce", &_rl_term_clreol }, |
| 396 | { "cl", &_rl_term_clrpag }, |
| 397 | { "cr", &_rl_term_cr }, |
| 398 | { "dc", &_rl_term_dc }, |
| 399 | { "ei", &_rl_term_ei }, |
| 400 | { "ic", &_rl_term_ic }, |
| 401 | { "im", &_rl_term_im }, |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 402 | { "kD", &_rl_term_kD }, /* delete */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 403 | { "kH", &_rl_term_kH }, /* home down ?? */ |
| 404 | { "kI", &_rl_term_kI }, /* insert */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 405 | { "kd", &_rl_term_kd }, |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 406 | { "ke", &_rl_term_ke }, /* end keypad mode */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 407 | { "kh", &_rl_term_kh }, /* home */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 408 | { "kl", &_rl_term_kl }, |
| 409 | { "kr", &_rl_term_kr }, |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 410 | { "ks", &_rl_term_ks }, /* start keypad mode */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 411 | { "ku", &_rl_term_ku }, |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 412 | { "le", &_rl_term_backspace }, |
| 413 | { "mm", &_rl_term_mm }, |
| 414 | { "mo", &_rl_term_mo }, |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 415 | { "nd", &_rl_term_forward_char }, |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 416 | { "pc", &_rl_term_pc }, |
| 417 | { "up", &_rl_term_up }, |
| 418 | { "vb", &_rl_visible_bell }, |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 419 | { "vs", &_rl_term_vs }, |
| 420 | { "ve", &_rl_term_ve }, |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 421 | }; |
| 422 | |
| 423 | #define NUM_TC_STRINGS (sizeof (tc_strings) / sizeof (struct _tc_string)) |
| 424 | |
| 425 | /* Read the desired terminal capability strings into BP. The capabilities |
| 426 | are described in the TC_STRINGS table. */ |
| 427 | static void |
| 428 | get_term_capabilities (bp) |
| 429 | char **bp; |
| 430 | { |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 431 | #if !defined (__DJGPP__) /* XXX - doesn't DJGPP have a termcap library? */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 432 | register int i; |
| 433 | |
| 434 | for (i = 0; i < NUM_TC_STRINGS; i++) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 435 | *(tc_strings[i].tc_value) = tgetstr ((char *)tc_strings[i].tc_var, bp); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 436 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 437 | tcap_initialized = 1; |
| 438 | } |
| 439 | |
| 440 | int |
| 441 | _rl_init_terminal_io (terminal_name) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 442 | const char *terminal_name; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 443 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 444 | const char *term; |
| 445 | char *buffer; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 446 | int tty, tgetent_ret; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 447 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 448 | term = terminal_name ? terminal_name : sh_get_env_value ("TERM"); |
| 449 | _rl_term_clrpag = _rl_term_cr = _rl_term_clreol = (char *)NULL; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 450 | tty = rl_instream ? fileno (rl_instream) : 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 451 | |
| 452 | if (term == 0) |
| 453 | term = "dumb"; |
| 454 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 455 | #ifdef __MSDOS__ |
| 456 | _rl_term_im = _rl_term_ei = _rl_term_ic = _rl_term_IC = (char *)NULL; |
| 457 | _rl_term_up = _rl_term_dc = _rl_term_DC = _rl_visible_bell = (char *)NULL; |
| 458 | _rl_term_ku = _rl_term_kd = _rl_term_kl = _rl_term_kr = (char *)NULL; |
| 459 | _rl_term_mm = _rl_term_mo = (char *)NULL; |
| 460 | _rl_terminal_can_insert = term_has_meta = _rl_term_autowrap = 0; |
| 461 | _rl_term_cr = "\r"; |
| 462 | _rl_term_clreol = _rl_term_clrpag = _rl_term_backspace = (char *)NULL; |
| 463 | _rl_term_goto = _rl_term_pc = _rl_term_ip = (char *)NULL; |
| 464 | _rl_term_ks = _rl_term_ke =_rl_term_vs = _rl_term_ve = (char *)NULL; |
| 465 | _rl_term_kh = _rl_term_kH = _rl_term_at7 = _rl_term_kI = (char *)NULL; |
| 466 | #if defined(HACK_TERMCAP_MOTION) |
| 467 | _rl_term_forward_char = (char *)NULL; |
| 468 | #endif |
| 469 | |
| 470 | _rl_get_screen_size (tty, 0); |
| 471 | #else /* !__MSDOS__ */ |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 472 | /* I've separated this out for later work on not calling tgetent at all |
| 473 | if the calling application has supplied a custom redisplay function, |
| 474 | (and possibly if the application has supplied a custom input function). */ |
| 475 | if (CUSTOM_REDISPLAY_FUNC()) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 476 | { |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 477 | tgetent_ret = -1; |
| 478 | } |
| 479 | else |
| 480 | { |
| 481 | if (term_string_buffer == 0) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 482 | term_string_buffer = (char *)xmalloc(2032); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 483 | |
| 484 | if (term_buffer == 0) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 485 | term_buffer = (char *)xmalloc(4080); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 486 | |
| 487 | buffer = term_string_buffer; |
| 488 | |
| 489 | tgetent_ret = tgetent (term_buffer, term); |
| 490 | } |
| 491 | |
| 492 | if (tgetent_ret <= 0) |
| 493 | { |
| 494 | FREE (term_string_buffer); |
| 495 | FREE (term_buffer); |
| 496 | buffer = term_buffer = term_string_buffer = (char *)NULL; |
| 497 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 498 | _rl_term_autowrap = 0; /* used by _rl_get_screen_size */ |
| 499 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 500 | /* Allow calling application to set default height and width, using |
| 501 | rl_set_screen_size */ |
| 502 | if (_rl_screenwidth <= 0 || _rl_screenheight <= 0) |
| 503 | { |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 504 | #if defined (__EMX__) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 505 | _emx_get_screensize (&_rl_screenwidth, &_rl_screenheight); |
| 506 | _rl_screenwidth--; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 507 | #else /* !__EMX__ */ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 508 | _rl_get_screen_size (tty, 0); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 509 | #endif /* !__EMX__ */ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 510 | } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 511 | |
| 512 | /* Defaults. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 513 | if (_rl_screenwidth <= 0 || _rl_screenheight <= 0) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 514 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 515 | _rl_screenwidth = 79; |
| 516 | _rl_screenheight = 24; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /* Everything below here is used by the redisplay code (tputs). */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 520 | _rl_screenchars = _rl_screenwidth * _rl_screenheight; |
| 521 | _rl_term_cr = "\r"; |
| 522 | _rl_term_im = _rl_term_ei = _rl_term_ic = _rl_term_IC = (char *)NULL; |
| 523 | _rl_term_up = _rl_term_dc = _rl_term_DC = _rl_visible_bell = (char *)NULL; |
| 524 | _rl_term_ku = _rl_term_kd = _rl_term_kl = _rl_term_kr = (char *)NULL; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 525 | _rl_term_kh = _rl_term_kH = _rl_term_kI = _rl_term_kD = (char *)NULL; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 526 | _rl_term_ks = _rl_term_ke = _rl_term_at7 = (char *)NULL; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 527 | _rl_term_mm = _rl_term_mo = (char *)NULL; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 528 | _rl_term_ve = _rl_term_vs = (char *)NULL; |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 529 | _rl_term_forward_char = (char *)NULL; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 530 | _rl_terminal_can_insert = term_has_meta = 0; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 531 | |
| 532 | /* Reasonable defaults for tgoto(). Readline currently only uses |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 533 | tgoto if _rl_term_IC or _rl_term_DC is defined, but just in case we |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 534 | change that later... */ |
| 535 | PC = '\0'; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 536 | BC = _rl_term_backspace = "\b"; |
| 537 | UP = _rl_term_up; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 538 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | get_term_capabilities (&buffer); |
| 543 | |
| 544 | /* Set up the variables that the termcap library expects the application |
| 545 | to provide. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 546 | PC = _rl_term_pc ? *_rl_term_pc : 0; |
| 547 | BC = _rl_term_backspace; |
| 548 | UP = _rl_term_up; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 549 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 550 | if (!_rl_term_cr) |
| 551 | _rl_term_cr = "\r"; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 552 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 553 | _rl_term_autowrap = tgetflag ("am") && tgetflag ("xn"); |
| 554 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 555 | /* Allow calling application to set default height and width, using |
| 556 | rl_set_screen_size */ |
| 557 | if (_rl_screenwidth <= 0 || _rl_screenheight <= 0) |
| 558 | _rl_get_screen_size (tty, 0); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 559 | |
| 560 | /* "An application program can assume that the terminal can do |
| 561 | character insertion if *any one of* the capabilities `IC', |
| 562 | `im', `ic' or `ip' is provided." But we can't do anything if |
| 563 | only `ip' is provided, so... */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 564 | _rl_terminal_can_insert = (_rl_term_IC || _rl_term_im || _rl_term_ic); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 565 | |
| 566 | /* Check to see if this terminal has a meta key and clear the capability |
| 567 | variables if there is none. */ |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 568 | term_has_meta = tgetflag ("km") != 0; |
| 569 | if (term_has_meta == 0) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 570 | _rl_term_mm = _rl_term_mo = (char *)NULL; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 571 | #endif /* !__MSDOS__ */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 572 | |
| 573 | /* Attempt to find and bind the arrow keys. Do not override already |
| 574 | bound keys in an overzealous attempt, however. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 575 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 576 | bind_termcap_arrow_keys (emacs_standard_keymap); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 577 | |
| 578 | #if defined (VI_MODE) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 579 | bind_termcap_arrow_keys (vi_movement_keymap); |
| 580 | bind_termcap_arrow_keys (vi_insertion_keymap); |
| 581 | #endif /* VI_MODE */ |
| 582 | |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | /* Bind the arrow key sequences from the termcap description in MAP. */ |
| 587 | static void |
| 588 | bind_termcap_arrow_keys (map) |
| 589 | Keymap map; |
| 590 | { |
| 591 | Keymap xkeymap; |
| 592 | |
| 593 | xkeymap = _rl_keymap; |
| 594 | _rl_keymap = map; |
| 595 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 596 | rl_bind_keyseq_if_unbound (_rl_term_ku, rl_get_previous_history); |
| 597 | rl_bind_keyseq_if_unbound (_rl_term_kd, rl_get_next_history); |
| 598 | rl_bind_keyseq_if_unbound (_rl_term_kr, rl_forward_char); |
| 599 | rl_bind_keyseq_if_unbound (_rl_term_kl, rl_backward_char); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 600 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 601 | rl_bind_keyseq_if_unbound (_rl_term_kh, rl_beg_of_line); /* Home */ |
| 602 | rl_bind_keyseq_if_unbound (_rl_term_at7, rl_end_of_line); /* End */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 603 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 604 | rl_bind_keyseq_if_unbound (_rl_term_kD, rl_delete); |
| 605 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 606 | _rl_keymap = xkeymap; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | char * |
| 610 | rl_get_termcap (cap) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 611 | const char *cap; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 612 | { |
| 613 | register int i; |
| 614 | |
| 615 | if (tcap_initialized == 0) |
| 616 | return ((char *)NULL); |
| 617 | for (i = 0; i < NUM_TC_STRINGS; i++) |
| 618 | { |
| 619 | if (tc_strings[i].tc_var[0] == cap[0] && strcmp (tc_strings[i].tc_var, cap) == 0) |
| 620 | return *(tc_strings[i].tc_value); |
| 621 | } |
| 622 | return ((char *)NULL); |
| 623 | } |
| 624 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 625 | /* Re-initialize the terminal considering that the TERM/TERMCAP variable |
| 626 | has changed. */ |
| 627 | int |
| 628 | rl_reset_terminal (terminal_name) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 629 | const char *terminal_name; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 630 | { |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 631 | _rl_screenwidth = _rl_screenheight = 0; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 632 | _rl_init_terminal_io (terminal_name); |
| 633 | return 0; |
| 634 | } |
| 635 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 636 | /* A function for the use of tputs () */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 637 | #ifdef _MINIX |
| 638 | void |
| 639 | _rl_output_character_function (c) |
| 640 | int c; |
| 641 | { |
| 642 | putc (c, _rl_out_stream); |
| 643 | } |
| 644 | #else /* !_MINIX */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 645 | int |
| 646 | _rl_output_character_function (c) |
| 647 | int c; |
| 648 | { |
| 649 | return putc (c, _rl_out_stream); |
| 650 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 651 | #endif /* !_MINIX */ |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 652 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 653 | /* Write COUNT characters from STRING to the output stream. */ |
| 654 | void |
| 655 | _rl_output_some_chars (string, count) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 656 | const char *string; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 657 | int count; |
| 658 | { |
| 659 | fwrite (string, 1, count, _rl_out_stream); |
| 660 | } |
| 661 | |
| 662 | /* Move the cursor back. */ |
| 663 | int |
| 664 | _rl_backspace (count) |
| 665 | int count; |
| 666 | { |
| 667 | register int i; |
| 668 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 669 | #ifndef __MSDOS__ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 670 | if (_rl_term_backspace) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 671 | for (i = 0; i < count; i++) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 672 | tputs (_rl_term_backspace, 1, _rl_output_character_function); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 673 | else |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 674 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 675 | for (i = 0; i < count; i++) |
| 676 | putc ('\b', _rl_out_stream); |
| 677 | return 0; |
| 678 | } |
| 679 | |
| 680 | /* Move to the start of the next line. */ |
| 681 | int |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 682 | rl_crlf () |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 683 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 684 | #if defined (NEW_TTY_DRIVER) || defined (__MINT__) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 685 | if (_rl_term_cr) |
| 686 | tputs (_rl_term_cr, 1, _rl_output_character_function); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 687 | #endif /* NEW_TTY_DRIVER || __MINT__ */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 688 | putc ('\n', _rl_out_stream); |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | /* Ring the terminal bell. */ |
| 693 | int |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 694 | rl_ding () |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 695 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 696 | if (_rl_echoing_p) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 697 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 698 | switch (_rl_bell_preference) |
| 699 | { |
| 700 | case NO_BELL: |
| 701 | default: |
| 702 | break; |
| 703 | case VISIBLE_BELL: |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 704 | if (_rl_visible_bell) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 705 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 706 | #ifdef __DJGPP__ |
| 707 | ScreenVisualBell (); |
| 708 | #else |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 709 | tputs (_rl_visible_bell, 1, _rl_output_character_function); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 710 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 711 | break; |
| 712 | } |
| 713 | /* FALLTHROUGH */ |
| 714 | case AUDIBLE_BELL: |
| 715 | fprintf (stderr, "\007"); |
| 716 | fflush (stderr); |
| 717 | break; |
| 718 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 719 | return (0); |
| 720 | } |
| 721 | return (-1); |
| 722 | } |
| 723 | |
| 724 | /* **************************************************************** */ |
| 725 | /* */ |
| 726 | /* Controlling the Meta Key and Keypad */ |
| 727 | /* */ |
| 728 | /* **************************************************************** */ |
| 729 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 730 | static int enabled_meta = 0; /* flag indicating we enabled meta mode */ |
| 731 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 732 | void |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 733 | _rl_enable_meta_key () |
| 734 | { |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 735 | #if !defined (__DJGPP__) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 736 | if (term_has_meta && _rl_term_mm) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 737 | { |
| 738 | tputs (_rl_term_mm, 1, _rl_output_character_function); |
| 739 | enabled_meta = 1; |
| 740 | } |
| 741 | #endif |
| 742 | } |
| 743 | |
| 744 | void |
| 745 | _rl_disable_meta_key () |
| 746 | { |
| 747 | #if !defined (__DJGPP__) |
| 748 | if (term_has_meta && _rl_term_mo && enabled_meta) |
| 749 | { |
| 750 | tputs (_rl_term_mo, 1, _rl_output_character_function); |
| 751 | enabled_meta = 0; |
| 752 | } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 753 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | void |
| 757 | _rl_control_keypad (on) |
| 758 | int on; |
| 759 | { |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 760 | #if !defined (__DJGPP__) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 761 | if (on && _rl_term_ks) |
| 762 | tputs (_rl_term_ks, 1, _rl_output_character_function); |
| 763 | else if (!on && _rl_term_ke) |
| 764 | tputs (_rl_term_ke, 1, _rl_output_character_function); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 765 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 766 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 767 | |
| 768 | /* **************************************************************** */ |
| 769 | /* */ |
| 770 | /* Controlling the Cursor */ |
| 771 | /* */ |
| 772 | /* **************************************************************** */ |
| 773 | |
| 774 | /* Set the cursor appropriately depending on IM, which is one of the |
| 775 | insert modes (insert or overwrite). Insert mode gets the normal |
| 776 | cursor. Overwrite mode gets a very visible cursor. Only does |
| 777 | anything if we have both capabilities. */ |
| 778 | void |
| 779 | _rl_set_cursor (im, force) |
| 780 | int im, force; |
| 781 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 782 | #ifndef __MSDOS__ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 783 | if (_rl_term_ve && _rl_term_vs) |
| 784 | { |
| 785 | if (force || im != rl_insert_mode) |
| 786 | { |
| 787 | if (im == RL_IM_OVERWRITE) |
| 788 | tputs (_rl_term_vs, 1, _rl_output_character_function); |
| 789 | else |
| 790 | tputs (_rl_term_ve, 1, _rl_output_character_function); |
| 791 | } |
| 792 | } |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 793 | #endif |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 794 | } |