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