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