* gdbarch.h (target_gdbarch): Add comment documenting this global.
[gdb/SamB.git] / readline / terminal.c
blob46b50e77e24a37b303ac9cbd48d0a214ae018184
1 /* terminal.c -- controlling the terminal with termcap. */
3 /* Copyright (C) 1996-2005 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
8 The GNU Readline Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2, or
11 (at your option) any later version.
13 The GNU Readline Library is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
21 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
22 #define READLINE_LIBRARY
24 #if defined (HAVE_CONFIG_H)
25 # include <config.h>
26 #endif
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 */
35 #if defined (HAVE_UNISTD_H)
36 # include <unistd.h>
37 #endif /* HAVE_UNISTD_H */
39 #if defined (HAVE_STDLIB_H)
40 # include <stdlib.h>
41 #else
42 # include "ansi_stdlib.h"
43 #endif /* HAVE_STDLIB_H */
45 #if defined (HAVE_LOCALE_H)
46 # include <locale.h>
47 #endif
49 #include <stdio.h>
51 /* System-specific feature definitions and include files. */
52 #include "rldefs.h"
54 #if defined (GWINSZ_IN_SYS_IOCTL) && !defined (TIOCGWINSZ)
55 # include <sys/ioctl.h>
56 #endif /* GWINSZ_IN_SYS_IOCTL && !TIOCGWINSZ */
58 #ifdef __MSDOS__
59 # include <pc.h>
60 #endif
62 #include "rltty.h"
63 #include "tcap.h"
65 /* Some standard library routines. */
66 #include "readline.h"
67 #include "history.h"
69 #include "rlprivate.h"
70 #include "rlshell.h"
71 #include "xmalloc.h"
73 #if defined (__MINGW32__)
74 # include <windows.h>
75 # include <wincon.h>
76 #endif
78 #define CUSTOM_REDISPLAY_FUNC() (rl_redisplay_function != rl_redisplay)
79 #define CUSTOM_INPUT_FUNC() (rl_getc_function != rl_getc)
81 int rl_prefer_env_winsize;
83 /* **************************************************************** */
84 /* */
85 /* Terminal and Termcap */
86 /* */
87 /* **************************************************************** */
89 #ifndef __MSDOS__
90 static char *term_buffer = (char *)NULL;
91 static char *term_string_buffer = (char *)NULL;
92 #endif /* !__MSDOS__ */
94 static int tcap_initialized;
96 #if !defined (__linux__)
97 # if defined (__EMX__) || defined (NEED_EXTERN_PC)
98 extern
99 # endif /* __EMX__ || NEED_EXTERN_PC */
100 char PC, *BC, *UP;
101 #endif /* __linux__ */
103 /* Some strings to control terminal actions. These are output by tputs (). */
104 char *_rl_term_clreol;
105 char *_rl_term_clrpag;
106 char *_rl_term_cr;
107 char *_rl_term_backspace;
108 char *_rl_term_goto;
109 char *_rl_term_pc;
111 /* Non-zero if we determine that the terminal can do character insertion. */
112 int _rl_terminal_can_insert = 0;
114 /* How to insert characters. */
115 char *_rl_term_im;
116 char *_rl_term_ei;
117 char *_rl_term_ic;
118 char *_rl_term_ip;
119 char *_rl_term_IC;
121 /* How to delete characters. */
122 char *_rl_term_dc;
123 char *_rl_term_DC;
125 #if defined (HACK_TERMCAP_MOTION)
126 char *_rl_term_forward_char;
127 #endif /* HACK_TERMCAP_MOTION */
129 /* How to go up a line. */
130 char *_rl_term_up;
132 /* A visible bell; char if the terminal can be made to flash the screen. */
133 static char *_rl_visible_bell;
135 /* Non-zero means the terminal can auto-wrap lines. */
136 int _rl_term_autowrap = -1;
138 /* Non-zero means that this terminal has a meta key. */
139 static int term_has_meta;
141 /* The sequences to write to turn on and off the meta key, if this
142 terminal has one. */
143 static char *_rl_term_mm;
144 static char *_rl_term_mo;
146 /* The key sequences output by the arrow keys, if this terminal has any. */
147 static char *_rl_term_ku;
148 static char *_rl_term_kd;
149 static char *_rl_term_kr;
150 static char *_rl_term_kl;
152 /* How to initialize and reset the arrow keys, if this terminal has any. */
153 static char *_rl_term_ks;
154 static char *_rl_term_ke;
156 /* The key sequences sent by the Home and End keys, if any. */
157 static char *_rl_term_kh;
158 static char *_rl_term_kH;
159 static char *_rl_term_at7; /* @7 */
161 /* Delete key */
162 static char *_rl_term_kD;
164 /* Insert key */
165 static char *_rl_term_kI;
167 /* Cursor control */
168 static char *_rl_term_vs; /* very visible */
169 static char *_rl_term_ve; /* normal */
171 static void bind_termcap_arrow_keys PARAMS((Keymap));
173 /* Variables that hold the screen dimensions, used by the display code. */
174 int _rl_screenwidth, _rl_screenheight, _rl_screenchars;
176 /* Non-zero means the user wants to enable the keypad. */
177 int _rl_enable_keypad;
179 /* Non-zero means the user wants to enable a meta key. */
180 int _rl_enable_meta = 1;
182 #if defined (__EMX__)
183 static void
184 _emx_get_screensize (swp, shp)
185 int *swp, *shp;
187 int sz[2];
189 _scrsize (sz);
191 if (swp)
192 *swp = sz[0];
193 if (shp)
194 *shp = sz[1];
196 #endif
198 /* Get readline's idea of the screen size. TTY is a file descriptor open
199 to the terminal. If IGNORE_ENV is true, we do not pay attention to the
200 values of $LINES and $COLUMNS. The tests for TERM_STRING_BUFFER being
201 non-null serve to check whether or not we have initialized termcap. */
202 void
203 _rl_get_screen_size (tty, ignore_env)
204 int tty, ignore_env;
206 char *ss;
207 #if defined (TIOCGWINSZ)
208 struct winsize window_size;
209 #endif /* TIOCGWINSZ */
210 int wr, wc;
212 wr = wc = -1;
213 #if defined (TIOCGWINSZ)
214 if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
216 wc = (int) window_size.ws_col;
217 wr = (int) window_size.ws_row;
219 #endif /* TIOCGWINSZ */
221 /* For MinGW, we get the console size from the Windows API. */
222 #if defined (__MINGW32__)
223 HANDLE hConOut = GetStdHandle (STD_OUTPUT_HANDLE);
224 if (hConOut != INVALID_HANDLE_VALUE)
226 CONSOLE_SCREEN_BUFFER_INFO scr;
227 if (GetConsoleScreenBufferInfo (hConOut, &scr))
229 wc = scr.dwSize.X;
230 wr = scr.srWindow.Bottom - scr.srWindow.Top + 1;
233 #endif
235 #if defined (__EMX__)
236 _emx_get_screensize (&_rl_screenwidth, &_rl_screenheight);
237 #endif
239 if (ignore_env || rl_prefer_env_winsize == 0)
241 _rl_screenwidth = wc;
242 _rl_screenheight = wr;
244 else
245 _rl_screenwidth = _rl_screenheight = -1;
247 /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
248 is unset. If we prefer the environment, check it first before
249 assigning the value returned by the kernel. */
250 if (_rl_screenwidth <= 0)
252 if (ignore_env == 0 && (ss = sh_get_env_value ("COLUMNS")))
253 _rl_screenwidth = atoi (ss);
255 if (_rl_screenwidth <= 0)
256 _rl_screenwidth = wc;
258 #if defined (__DJGPP__)
259 if (_rl_screenwidth <= 0)
260 _rl_screenwidth = ScreenCols ();
261 #else
262 if (_rl_screenwidth <= 0 && term_string_buffer)
263 _rl_screenwidth = tgetnum ("co");
264 #endif
267 /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
268 is unset. */
269 if (_rl_screenheight <= 0)
271 if (ignore_env == 0 && (ss = sh_get_env_value ("LINES")))
272 _rl_screenheight = atoi (ss);
274 if (_rl_screenheight <= 0)
275 _rl_screenheight = wr;
277 #if defined (__DJGPP__)
278 if (_rl_screenheight <= 0)
279 _rl_screenheight = ScreenRows ();
280 #else
281 if (_rl_screenheight <= 0 && term_string_buffer)
282 _rl_screenheight = tgetnum ("li");
283 #endif
286 /* If all else fails, default to 80x24 terminal. */
287 if (_rl_screenwidth <= 1)
288 _rl_screenwidth = 80;
290 if (_rl_screenheight <= 0)
291 _rl_screenheight = 24;
293 /* If we're being compiled as part of bash, set the environment
294 variables $LINES and $COLUMNS to new values. Otherwise, just
295 do a pair of putenv () or setenv () calls. */
296 sh_set_lines_and_columns (_rl_screenheight, _rl_screenwidth);
298 if (_rl_term_autowrap == 0)
299 _rl_screenwidth--;
301 _rl_screenchars = _rl_screenwidth * _rl_screenheight;
304 void
305 _rl_set_screen_size (rows, cols)
306 int rows, cols;
308 if (_rl_term_autowrap == -1)
309 _rl_init_terminal_io (rl_terminal_name);
311 if (rows > 0)
312 _rl_screenheight = rows;
313 if (cols > 0)
315 _rl_screenwidth = cols;
316 if (_rl_term_autowrap == 0)
317 _rl_screenwidth--;
320 if (rows > 0 || cols > 0)
321 _rl_screenchars = _rl_screenwidth * _rl_screenheight;
324 void
325 rl_set_screen_size (rows, cols)
326 int rows, cols;
328 _rl_set_screen_size (rows, cols);
331 void
332 rl_get_screen_size (rows, cols)
333 int *rows, *cols;
335 if (rows)
336 *rows = _rl_screenheight;
337 if (cols)
338 *cols = _rl_screenwidth;
341 void
342 rl_reset_screen_size ()
344 _rl_get_screen_size (fileno (rl_instream), 0);
347 void
348 rl_resize_terminal ()
350 if (readline_echoing_p)
352 _rl_get_screen_size (fileno (rl_instream), 1);
353 if (CUSTOM_REDISPLAY_FUNC ())
354 rl_forced_update_display ();
355 else
356 _rl_redisplay_after_sigwinch ();
360 struct _tc_string {
361 const char *tc_var;
362 char **tc_value;
365 /* This should be kept sorted, just in case we decide to change the
366 search algorithm to something smarter. */
367 static struct _tc_string tc_strings[] =
369 { "@7", &_rl_term_at7 },
370 { "DC", &_rl_term_DC },
371 { "IC", &_rl_term_IC },
372 { "ce", &_rl_term_clreol },
373 { "cl", &_rl_term_clrpag },
374 { "cr", &_rl_term_cr },
375 { "dc", &_rl_term_dc },
376 { "ei", &_rl_term_ei },
377 { "ic", &_rl_term_ic },
378 { "im", &_rl_term_im },
379 { "kD", &_rl_term_kD }, /* delete */
380 { "kH", &_rl_term_kH }, /* home down ?? */
381 { "kI", &_rl_term_kI }, /* insert */
382 { "kd", &_rl_term_kd },
383 { "ke", &_rl_term_ke }, /* end keypad mode */
384 { "kh", &_rl_term_kh }, /* home */
385 { "kl", &_rl_term_kl },
386 { "kr", &_rl_term_kr },
387 { "ks", &_rl_term_ks }, /* start keypad mode */
388 { "ku", &_rl_term_ku },
389 { "le", &_rl_term_backspace },
390 { "mm", &_rl_term_mm },
391 { "mo", &_rl_term_mo },
392 #if defined (HACK_TERMCAP_MOTION)
393 { "nd", &_rl_term_forward_char },
394 #endif
395 { "pc", &_rl_term_pc },
396 { "up", &_rl_term_up },
397 { "vb", &_rl_visible_bell },
398 { "vs", &_rl_term_vs },
399 { "ve", &_rl_term_ve },
402 #define NUM_TC_STRINGS (sizeof (tc_strings) / sizeof (struct _tc_string))
404 /* Read the desired terminal capability strings into BP. The capabilities
405 are described in the TC_STRINGS table. */
406 static void
407 get_term_capabilities (bp)
408 char **bp;
410 #if !defined (__DJGPP__) /* XXX - doesn't DJGPP have a termcap library? */
411 register int i;
413 for (i = 0; i < NUM_TC_STRINGS; i++)
414 *(tc_strings[i].tc_value) = tgetstr ((char *)tc_strings[i].tc_var, bp);
415 #endif
416 tcap_initialized = 1;
420 _rl_init_terminal_io (terminal_name)
421 const char *terminal_name;
423 const char *term;
424 char *buffer;
425 int tty, tgetent_ret;
427 term = terminal_name ? terminal_name : sh_get_env_value ("TERM");
428 _rl_term_clrpag = _rl_term_cr = _rl_term_clreol = (char *)NULL;
429 tty = rl_instream ? fileno (rl_instream) : 0;
431 if (term == 0)
432 term = "dumb";
434 #ifdef __MSDOS__
435 _rl_term_im = _rl_term_ei = _rl_term_ic = _rl_term_IC = (char *)NULL;
436 _rl_term_up = _rl_term_dc = _rl_term_DC = _rl_visible_bell = (char *)NULL;
437 _rl_term_ku = _rl_term_kd = _rl_term_kl = _rl_term_kr = (char *)NULL;
438 _rl_term_mm = _rl_term_mo = (char *)NULL;
439 _rl_terminal_can_insert = term_has_meta = _rl_term_autowrap = 0;
440 _rl_term_cr = "\r";
441 _rl_term_clreol = _rl_term_clrpag = _rl_term_backspace = (char *)NULL;
442 _rl_term_goto = _rl_term_pc = _rl_term_ip = (char *)NULL;
443 _rl_term_ks = _rl_term_ke =_rl_term_vs = _rl_term_ve = (char *)NULL;
444 _rl_term_kh = _rl_term_kH = _rl_term_at7 = _rl_term_kI = (char *)NULL;
445 #if defined(HACK_TERMCAP_MOTION)
446 _rl_term_forward_char = (char *)NULL;
447 #endif
449 _rl_get_screen_size (tty, 0);
450 #else /* !__MSDOS__ */
451 /* I've separated this out for later work on not calling tgetent at all
452 if the calling application has supplied a custom redisplay function,
453 (and possibly if the application has supplied a custom input function). */
454 if (CUSTOM_REDISPLAY_FUNC())
456 tgetent_ret = -1;
458 else
460 if (term_string_buffer == 0)
461 term_string_buffer = (char *)xmalloc(2032);
463 if (term_buffer == 0)
464 term_buffer = (char *)xmalloc(4080);
466 buffer = term_string_buffer;
468 tgetent_ret = tgetent (term_buffer, term);
471 if (tgetent_ret <= 0)
473 FREE (term_string_buffer);
474 FREE (term_buffer);
475 buffer = term_buffer = term_string_buffer = (char *)NULL;
477 _rl_term_autowrap = 0; /* used by _rl_get_screen_size */
479 /* Allow calling application to set default height and width, using
480 rl_set_screen_size */
481 if (_rl_screenwidth <= 0 || _rl_screenheight <= 0)
483 #if defined (__EMX__)
484 _emx_get_screensize (&_rl_screenwidth, &_rl_screenheight);
485 _rl_screenwidth--;
486 #else /* !__EMX__ */
487 _rl_get_screen_size (tty, 0);
488 #endif /* !__EMX__ */
491 /* Defaults. */
492 if (_rl_screenwidth <= 0 || _rl_screenheight <= 0)
494 _rl_screenwidth = 79;
495 _rl_screenheight = 24;
498 /* Everything below here is used by the redisplay code (tputs). */
499 _rl_screenchars = _rl_screenwidth * _rl_screenheight;
500 _rl_term_cr = "\r";
501 _rl_term_im = _rl_term_ei = _rl_term_ic = _rl_term_IC = (char *)NULL;
502 _rl_term_up = _rl_term_dc = _rl_term_DC = _rl_visible_bell = (char *)NULL;
503 _rl_term_ku = _rl_term_kd = _rl_term_kl = _rl_term_kr = (char *)NULL;
504 _rl_term_kh = _rl_term_kH = _rl_term_kI = _rl_term_kD = (char *)NULL;
505 _rl_term_ks = _rl_term_ke = _rl_term_at7 = (char *)NULL;
506 _rl_term_mm = _rl_term_mo = (char *)NULL;
507 _rl_term_ve = _rl_term_vs = (char *)NULL;
508 #if defined (HACK_TERMCAP_MOTION)
509 term_forward_char = (char *)NULL;
510 #endif
511 _rl_terminal_can_insert = term_has_meta = 0;
513 /* Reasonable defaults for tgoto(). Readline currently only uses
514 tgoto if _rl_term_IC or _rl_term_DC is defined, but just in case we
515 change that later... */
516 PC = '\0';
517 BC = _rl_term_backspace = "\b";
518 UP = _rl_term_up;
520 return 0;
523 get_term_capabilities (&buffer);
525 /* Set up the variables that the termcap library expects the application
526 to provide. */
527 PC = _rl_term_pc ? *_rl_term_pc : 0;
528 BC = _rl_term_backspace;
529 UP = _rl_term_up;
531 if (!_rl_term_cr)
532 _rl_term_cr = "\r";
534 _rl_term_autowrap = tgetflag ("am") && tgetflag ("xn");
536 /* Allow calling application to set default height and width, using
537 rl_set_screen_size */
538 if (_rl_screenwidth <= 0 || _rl_screenheight <= 0)
539 _rl_get_screen_size (tty, 0);
541 /* "An application program can assume that the terminal can do
542 character insertion if *any one of* the capabilities `IC',
543 `im', `ic' or `ip' is provided." But we can't do anything if
544 only `ip' is provided, so... */
545 _rl_terminal_can_insert = (_rl_term_IC || _rl_term_im || _rl_term_ic);
547 /* Check to see if this terminal has a meta key and clear the capability
548 variables if there is none. */
549 term_has_meta = (tgetflag ("km") || tgetflag ("MT"));
550 if (!term_has_meta)
551 _rl_term_mm = _rl_term_mo = (char *)NULL;
553 #endif /* !__MSDOS__ */
555 /* Attempt to find and bind the arrow keys. Do not override already
556 bound keys in an overzealous attempt, however. */
558 bind_termcap_arrow_keys (emacs_standard_keymap);
560 #if defined (VI_MODE)
561 bind_termcap_arrow_keys (vi_movement_keymap);
562 bind_termcap_arrow_keys (vi_insertion_keymap);
563 #endif /* VI_MODE */
565 return 0;
568 /* Bind the arrow key sequences from the termcap description in MAP. */
569 static void
570 bind_termcap_arrow_keys (map)
571 Keymap map;
573 Keymap xkeymap;
575 xkeymap = _rl_keymap;
576 _rl_keymap = map;
578 rl_bind_keyseq_if_unbound (_rl_term_ku, rl_get_previous_history);
579 rl_bind_keyseq_if_unbound (_rl_term_kd, rl_get_next_history);
580 rl_bind_keyseq_if_unbound (_rl_term_kr, rl_forward_char);
581 rl_bind_keyseq_if_unbound (_rl_term_kl, rl_backward_char);
583 rl_bind_keyseq_if_unbound (_rl_term_kh, rl_beg_of_line); /* Home */
584 rl_bind_keyseq_if_unbound (_rl_term_at7, rl_end_of_line); /* End */
586 rl_bind_keyseq_if_unbound (_rl_term_kD, rl_delete);
588 _rl_keymap = xkeymap;
591 char *
592 rl_get_termcap (cap)
593 const char *cap;
595 register int i;
597 if (tcap_initialized == 0)
598 return ((char *)NULL);
599 for (i = 0; i < NUM_TC_STRINGS; i++)
601 if (tc_strings[i].tc_var[0] == cap[0] && strcmp (tc_strings[i].tc_var, cap) == 0)
602 return *(tc_strings[i].tc_value);
604 return ((char *)NULL);
607 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
608 has changed. */
610 rl_reset_terminal (terminal_name)
611 const char *terminal_name;
613 _rl_screenwidth = _rl_screenheight = 0;
614 _rl_init_terminal_io (terminal_name);
615 return 0;
618 /* A function for the use of tputs () */
619 #ifdef _MINIX
620 void
621 _rl_output_character_function (c)
622 int c;
624 putc (c, _rl_out_stream);
626 #else /* !_MINIX */
628 _rl_output_character_function (c)
629 int c;
631 return putc (c, _rl_out_stream);
633 #endif /* !_MINIX */
635 /* Write COUNT characters from STRING to the output stream. */
636 void
637 _rl_output_some_chars (string, count)
638 const char *string;
639 int count;
641 fwrite (string, 1, count, _rl_out_stream);
644 /* Move the cursor back. */
646 _rl_backspace (count)
647 int count;
649 register int i;
651 #ifndef __MSDOS__
652 if (_rl_term_backspace)
653 for (i = 0; i < count; i++)
654 tputs (_rl_term_backspace, 1, _rl_output_character_function);
655 else
656 #endif
657 for (i = 0; i < count; i++)
658 putc ('\b', _rl_out_stream);
659 return 0;
662 /* Move to the start of the next line. */
664 rl_crlf ()
666 #if defined (NEW_TTY_DRIVER)
667 if (_rl_term_cr)
668 tputs (_rl_term_cr, 1, _rl_output_character_function);
669 #endif /* NEW_TTY_DRIVER */
670 putc ('\n', _rl_out_stream);
671 return 0;
674 /* Ring the terminal bell. */
676 rl_ding ()
678 if (readline_echoing_p)
680 switch (_rl_bell_preference)
682 case NO_BELL:
683 default:
684 break;
685 case VISIBLE_BELL:
686 #ifdef __MSDOS__
687 ScreenVisualBell ();
688 break;
689 #else
690 if (_rl_visible_bell)
692 tputs (_rl_visible_bell, 1, _rl_output_character_function);
693 break;
695 /* FALLTHROUGH */
696 #endif
697 case AUDIBLE_BELL:
698 fprintf (stderr, "\007");
699 fflush (stderr);
700 break;
702 return (0);
704 return (-1);
707 /* **************************************************************** */
708 /* */
709 /* Controlling the Meta Key and Keypad */
710 /* */
711 /* **************************************************************** */
713 void
714 _rl_enable_meta_key ()
716 #if !defined (__DJGPP__)
717 if (term_has_meta && _rl_term_mm)
718 tputs (_rl_term_mm, 1, _rl_output_character_function);
719 #endif
722 void
723 _rl_control_keypad (on)
724 int on;
726 #if !defined (__DJGPP__)
727 if (on && _rl_term_ks)
728 tputs (_rl_term_ks, 1, _rl_output_character_function);
729 else if (!on && _rl_term_ke)
730 tputs (_rl_term_ke, 1, _rl_output_character_function);
731 #endif
734 /* **************************************************************** */
735 /* */
736 /* Controlling the Cursor */
737 /* */
738 /* **************************************************************** */
740 /* Set the cursor appropriately depending on IM, which is one of the
741 insert modes (insert or overwrite). Insert mode gets the normal
742 cursor. Overwrite mode gets a very visible cursor. Only does
743 anything if we have both capabilities. */
744 void
745 _rl_set_cursor (im, force)
746 int im, force;
748 #ifndef __MSDOS__
749 if (_rl_term_ve && _rl_term_vs)
751 if (force || im != rl_insert_mode)
753 if (im == RL_IM_OVERWRITE)
754 tputs (_rl_term_vs, 1, _rl_output_character_function);
755 else
756 tputs (_rl_term_ve, 1, _rl_output_character_function);
759 #endif