]> git.ipfire.org Git - thirdparty/bash.git/blob - lib/readline/terminal.c
Imported from ../bash-2.04.tar.gz.
[thirdparty/bash.git] / lib / readline / terminal.c
1 /* terminal.c -- controlling the terminal with termcap. */
2
3 /* Copyright (C) 1996 Free Software Foundation, Inc.
4
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
7
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.
12
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.
17
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
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 #include "rltty.h"
59 #include "tcap.h"
60
61 /* Some standard library routines. */
62 #include "readline.h"
63 #include "history.h"
64
65 #include "rlprivate.h"
66 #include "rlshell.h"
67
68 /* **************************************************************** */
69 /* */
70 /* Terminal and Termcap */
71 /* */
72 /* **************************************************************** */
73
74 static char *term_buffer = (char *)NULL;
75 static char *term_string_buffer = (char *)NULL;
76
77 static int tcap_initialized;
78
79 /* Non-zero means this terminal can't really do anything. */
80 static int dumb_term;
81
82 #if !defined (__linux__)
83 # if defined (__EMX__) || defined (NEED_EXTERN_PC)
84 extern
85 # endif /* __EMX__ || NEED_EXTERN_PC */
86 char PC, *BC, *UP;
87 #endif /* __linux__ */
88
89 /* Some strings to control terminal actions. These are output by tputs (). */
90 char *term_goto, *term_clreol, *term_cr, *term_clrpag, *term_backspace;
91 char *term_pc;
92
93 /* Non-zero if we determine that the terminal can do character insertion. */
94 int terminal_can_insert = 0;
95
96 /* How to insert characters. */
97 char *term_im, *term_ei, *term_ic, *term_ip, *term_IC;
98
99 /* How to delete characters. */
100 char *term_dc, *term_DC;
101
102 #if defined (HACK_TERMCAP_MOTION)
103 char *term_forward_char;
104 #endif /* HACK_TERMCAP_MOTION */
105
106 /* How to go up a line. */
107 char *term_up;
108
109 /* A visible bell, if the terminal can be made to flash the screen. */
110 static char *visible_bell;
111
112 /* Non-zero means the terminal can auto-wrap lines. */
113 int _rl_term_autowrap;
114
115 /* Non-zero means that this terminal has a meta key. */
116 static int term_has_meta;
117
118 /* The sequences to write to turn on and off the meta key, if this
119 terminal has one. */
120 static char *term_mm, *term_mo;
121
122 /* The key sequences output by the arrow keys, if this terminal has any. */
123 static char *term_ku, *term_kd, *term_kr, *term_kl;
124
125 /* How to initialize and reset the arrow keys, if this terminal has any. */
126 static char *term_ks, *term_ke;
127
128 /* The key sequences sent by the Home and End keys, if any. */
129 static char *term_kh, *term_kH;
130
131 /* Variables that hold the screen dimensions, used by the display code. */
132 int screenwidth, screenheight, screenchars;
133
134 /* Non-zero means the user wants to enable the keypad. */
135 int _rl_enable_keypad;
136
137 /* Non-zero means the user wants to enable a meta key. */
138 int _rl_enable_meta = 1;
139
140 #if defined (__EMX__)
141 static void
142 _emx_get_screensize (swp, shp)
143 int *swp, *shp;
144 {
145 int sz[2];
146
147 _scrsize (sz);
148
149 if (swp)
150 *swp = sz[0];
151 if (shp)
152 *shp = sz[1];
153 }
154 #endif
155
156 /* Get readline's idea of the screen size. TTY is a file descriptor open
157 to the terminal. If IGNORE_ENV is true, we do not pay attention to the
158 values of $LINES and $COLUMNS. The tests for TERM_STRING_BUFFER being
159 non-null serve to check whether or not we have initialized termcap. */
160 void
161 _rl_get_screen_size (tty, ignore_env)
162 int tty, ignore_env;
163 {
164 char *ss;
165 #if defined (TIOCGWINSZ)
166 struct winsize window_size;
167 #endif /* TIOCGWINSZ */
168
169 #if defined (TIOCGWINSZ)
170 if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
171 {
172 screenwidth = (int) window_size.ws_col;
173 screenheight = (int) window_size.ws_row;
174 }
175 #endif /* TIOCGWINSZ */
176
177 #if defined (__EMX__)
178 _emx_get_screensize (&screenwidth, &screenheight);
179 #endif
180
181 /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
182 is unset. */
183 if (screenwidth <= 0)
184 {
185 if (ignore_env == 0 && (ss = get_env_value ("COLUMNS")))
186 screenwidth = atoi (ss);
187
188 #if !defined (__DJGPP__)
189 if (screenwidth <= 0 && term_string_buffer)
190 screenwidth = tgetnum ("co");
191 #endif
192 }
193
194 /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
195 is unset. */
196 if (screenheight <= 0)
197 {
198 if (ignore_env == 0 && (ss = get_env_value ("LINES")))
199 screenheight = atoi (ss);
200
201 #if !defined (__DJGPP__)
202 if (screenheight <= 0 && term_string_buffer)
203 screenheight = tgetnum ("li");
204 #endif
205 }
206
207 /* If all else fails, default to 80x24 terminal. */
208 if (screenwidth <= 1)
209 screenwidth = 80;
210
211 if (screenheight <= 0)
212 screenheight = 24;
213
214 /* If we're being compiled as part of bash, set the environment
215 variables $LINES and $COLUMNS to new values. Otherwise, just
216 do a pair of putenv () or setenv () calls. */
217 set_lines_and_columns (screenheight, screenwidth);
218
219 if (_rl_term_autowrap == 0)
220 screenwidth--;
221
222 screenchars = screenwidth * screenheight;
223 }
224
225 void
226 _rl_set_screen_size (rows, cols)
227 int rows, cols;
228 {
229 screenheight = rows;
230 screenwidth = cols;
231
232 if (_rl_term_autowrap == 0)
233 screenwidth--;
234
235 screenchars = screenwidth * screenheight;
236 }
237
238 void
239 rl_resize_terminal ()
240 {
241 if (readline_echoing_p)
242 {
243 _rl_get_screen_size (fileno (rl_instream), 1);
244 _rl_redisplay_after_sigwinch ();
245 }
246 }
247
248 struct _tc_string {
249 char *tc_var;
250 char **tc_value;
251 };
252
253 /* This should be kept sorted, just in case we decide to change the
254 search algorithm to something smarter. */
255 static struct _tc_string tc_strings[] =
256 {
257 { "DC", &term_DC },
258 { "IC", &term_IC },
259 { "ce", &term_clreol },
260 { "cl", &term_clrpag },
261 { "cr", &term_cr },
262 { "dc", &term_dc },
263 { "ei", &term_ei },
264 { "ic", &term_ic },
265 { "im", &term_im },
266 { "kd", &term_kd },
267 { "kh", &term_kh }, /* home */
268 { "kH", &term_kH }, /* end */
269 { "kl", &term_kl },
270 { "kr", &term_kr },
271 { "ku", &term_ku },
272 { "ks", &term_ks },
273 { "ke", &term_ke },
274 { "le", &term_backspace },
275 { "mm", &term_mm },
276 { "mo", &term_mo },
277 #if defined (HACK_TERMCAP_MOTION)
278 { "nd", &term_forward_char },
279 #endif
280 { "pc", &term_pc },
281 { "up", &term_up },
282 { "vb", &visible_bell },
283 };
284
285 #define NUM_TC_STRINGS (sizeof (tc_strings) / sizeof (struct _tc_string))
286
287 /* Read the desired terminal capability strings into BP. The capabilities
288 are described in the TC_STRINGS table. */
289 static void
290 get_term_capabilities (bp)
291 char **bp;
292 {
293 #if !defined (__DJGPP__) /* XXX - doesn't DJGPP have a termcap library? */
294 register int i;
295
296 for (i = 0; i < NUM_TC_STRINGS; i++)
297 *(tc_strings[i].tc_value) = tgetstr (tc_strings[i].tc_var, bp);
298 #endif
299 tcap_initialized = 1;
300 }
301
302 #define CUSTOM_REDISPLAY_FUNC() (rl_redisplay_function != rl_redisplay)
303 #define CUSTOM_INPUT_FUNC() (rl_getc_function != rl_getc)
304
305 int
306 _rl_init_terminal_io (terminal_name)
307 char *terminal_name;
308 {
309 char *term, *buffer;
310 int tty, tgetent_ret;
311 Keymap xkeymap;
312
313 term = terminal_name ? terminal_name : get_env_value ("TERM");
314 term_clrpag = term_cr = term_clreol = (char *)NULL;
315 tty = rl_instream ? fileno (rl_instream) : 0;
316 screenwidth = screenheight = 0;
317
318 if (term == 0)
319 term = "dumb";
320
321 /* I've separated this out for later work on not calling tgetent at all
322 if the calling application has supplied a custom redisplay function,
323 (and possibly if the application has supplied a custom input function). */
324 if (CUSTOM_REDISPLAY_FUNC())
325 {
326 tgetent_ret = -1;
327 }
328 else
329 {
330 if (term_string_buffer == 0)
331 term_string_buffer = xmalloc(2032);
332
333 if (term_buffer == 0)
334 term_buffer = xmalloc(4080);
335
336 buffer = term_string_buffer;
337
338 tgetent_ret = tgetent (term_buffer, term);
339 }
340
341 if (tgetent_ret <= 0)
342 {
343 FREE (term_string_buffer);
344 FREE (term_buffer);
345 buffer = term_buffer = term_string_buffer = (char *)NULL;
346
347 dumb_term = 1;
348 _rl_term_autowrap = 0; /* used by _rl_get_screen_size */
349
350 #if defined (__EMX__)
351 _emx_get_screensize (&screenwidth, &screenheight);
352 screenwidth--;
353 #else /* !__EMX__ */
354 _rl_get_screen_size (tty, 0);
355 #endif /* !__EMX__ */
356
357 /* Defaults. */
358 if (screenwidth <= 0 || screenheight <= 0)
359 {
360 screenwidth = 79;
361 screenheight = 24;
362 }
363
364 /* Everything below here is used by the redisplay code (tputs). */
365 screenchars = screenwidth * screenheight;
366 term_cr = "\r";
367 term_im = term_ei = term_ic = term_IC = (char *)NULL;
368 term_up = term_dc = term_DC = visible_bell = (char *)NULL;
369 term_ku = term_kd = term_kl = term_kr = (char *)NULL;
370 term_mm = term_mo = (char *)NULL;
371 #if defined (HACK_TERMCAP_MOTION)
372 term_forward_char = (char *)NULL;
373 #endif
374 terminal_can_insert = term_has_meta = 0;
375
376 /* Reasonable defaults for tgoto(). Readline currently only uses
377 tgoto if term_IC or term_DC is defined, but just in case we
378 change that later... */
379 PC = '\0';
380 BC = term_backspace = "\b";
381 UP = term_up;
382
383 return 0;
384 }
385
386 get_term_capabilities (&buffer);
387
388 /* Set up the variables that the termcap library expects the application
389 to provide. */
390 PC = term_pc ? *term_pc : 0;
391 BC = term_backspace;
392 UP = term_up;
393
394 if (!term_cr)
395 term_cr = "\r";
396
397 _rl_term_autowrap = tgetflag ("am") && tgetflag ("xn");
398
399 _rl_get_screen_size (tty, 0);
400
401 /* "An application program can assume that the terminal can do
402 character insertion if *any one of* the capabilities `IC',
403 `im', `ic' or `ip' is provided." But we can't do anything if
404 only `ip' is provided, so... */
405 terminal_can_insert = (term_IC || term_im || term_ic);
406
407 /* Check to see if this terminal has a meta key and clear the capability
408 variables if there is none. */
409 term_has_meta = (tgetflag ("km") || tgetflag ("MT"));
410 if (!term_has_meta)
411 term_mm = term_mo = (char *)NULL;
412
413 /* Attempt to find and bind the arrow keys. Do not override already
414 bound keys in an overzealous attempt, however. */
415 xkeymap = _rl_keymap;
416
417 _rl_keymap = emacs_standard_keymap;
418 _rl_bind_if_unbound (term_ku, rl_get_previous_history);
419 _rl_bind_if_unbound (term_kd, rl_get_next_history);
420 _rl_bind_if_unbound (term_kr, rl_forward);
421 _rl_bind_if_unbound (term_kl, rl_backward);
422
423 _rl_bind_if_unbound (term_kh, rl_beg_of_line); /* Home */
424 _rl_bind_if_unbound (term_kH, rl_end_of_line); /* End */
425
426 #if defined (VI_MODE)
427 _rl_keymap = vi_movement_keymap;
428 _rl_bind_if_unbound (term_ku, rl_get_previous_history);
429 _rl_bind_if_unbound (term_kd, rl_get_next_history);
430 _rl_bind_if_unbound (term_kr, rl_forward);
431 _rl_bind_if_unbound (term_kl, rl_backward);
432
433 _rl_bind_if_unbound (term_kh, rl_beg_of_line); /* Home */
434 _rl_bind_if_unbound (term_kH, rl_end_of_line); /* End */
435 #endif /* VI_MODE */
436
437 _rl_keymap = xkeymap;
438
439 return 0;
440 }
441
442 char *
443 rl_get_termcap (cap)
444 char *cap;
445 {
446 register int i;
447
448 if (tcap_initialized == 0)
449 return ((char *)NULL);
450 for (i = 0; i < NUM_TC_STRINGS; i++)
451 {
452 if (tc_strings[i].tc_var[0] == cap[0] && strcmp (tc_strings[i].tc_var, cap) == 0)
453 return *(tc_strings[i].tc_value);
454 }
455 return ((char *)NULL);
456 }
457
458 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
459 has changed. */
460 int
461 rl_reset_terminal (terminal_name)
462 char *terminal_name;
463 {
464 _rl_init_terminal_io (terminal_name);
465 return 0;
466 }
467
468 /* A function for the use of tputs () */
469 #ifdef _MINIX
470 void
471 _rl_output_character_function (c)
472 int c;
473 {
474 putc (c, _rl_out_stream);
475 }
476 #else /* !_MINIX */
477 int
478 _rl_output_character_function (c)
479 int c;
480 {
481 return putc (c, _rl_out_stream);
482 }
483 #endif /* !_MINIX */
484
485 /* Write COUNT characters from STRING to the output stream. */
486 void
487 _rl_output_some_chars (string, count)
488 char *string;
489 int count;
490 {
491 fwrite (string, 1, count, _rl_out_stream);
492 }
493
494 /* Move the cursor back. */
495 int
496 _rl_backspace (count)
497 int count;
498 {
499 register int i;
500
501 if (term_backspace)
502 for (i = 0; i < count; i++)
503 tputs (term_backspace, 1, _rl_output_character_function);
504 else
505 for (i = 0; i < count; i++)
506 putc ('\b', _rl_out_stream);
507 return 0;
508 }
509
510 /* Move to the start of the next line. */
511 int
512 crlf ()
513 {
514 #if defined (NEW_TTY_DRIVER)
515 if (term_cr)
516 tputs (term_cr, 1, _rl_output_character_function);
517 #endif /* NEW_TTY_DRIVER */
518 putc ('\n', _rl_out_stream);
519 return 0;
520 }
521
522 /* Ring the terminal bell. */
523 int
524 ding ()
525 {
526 if (readline_echoing_p)
527 {
528 switch (_rl_bell_preference)
529 {
530 case NO_BELL:
531 default:
532 break;
533 case VISIBLE_BELL:
534 if (visible_bell)
535 {
536 tputs (visible_bell, 1, _rl_output_character_function);
537 break;
538 }
539 /* FALLTHROUGH */
540 case AUDIBLE_BELL:
541 fprintf (stderr, "\007");
542 fflush (stderr);
543 break;
544 }
545 return (0);
546 }
547 return (-1);
548 }
549
550 /* **************************************************************** */
551 /* */
552 /* Controlling the Meta Key and Keypad */
553 /* */
554 /* **************************************************************** */
555
556 void
557 _rl_enable_meta_key ()
558 {
559 #if !defined (__DJGPP__)
560 if (term_has_meta && term_mm)
561 tputs (term_mm, 1, _rl_output_character_function);
562 #endif
563 }
564
565 void
566 _rl_control_keypad (on)
567 int on;
568 {
569 #if !defined (__DJGPP__)
570 if (on && term_ks)
571 tputs (term_ks, 1, _rl_output_character_function);
572 else if (!on && term_ke)
573 tputs (term_ke, 1, _rl_output_character_function);
574 #endif
575 }