Debian Codegraph @

libsrc/readline/rltty.c

raw
1 /* rltty.c -- functions to prepare and restore the terminal for readline's
2 use. */
3
4 /* Copyright (C) 1992 Free Software Foundation, Inc.
5
6 This file is part of the GNU Readline Library, a library for
7 reading lines of text with interactive input and history editing.
8
9 The GNU Readline Library is free software; you can redistribute it
10 and/or modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 1, or
12 (at your option) any later version.
13
14 The GNU Readline Library is distributed in the hope that it will be
15 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 The GNU General Public License is often shipped with GNU software, and
20 is generally kept in a file called COPYING or LICENSE. If you do not
21 have a copy of the license, write to the Free Software Foundation,
22 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #define READLINE_LIBRARY
24
25 #include <sys/types.h>
26 #include <signal.h>
27 #include <errno.h>
28 #include <stdio.h>
29
30 #if defined (HAVE_UNISTD_H)
31 # include <unistd.h>
32 #endif /* HAVE_UNISTD_H */
33
34 #include "rldefs.h"
35 #include "readline.h"
36
37 #if !defined (errno)
38 extern int errno;
39 #endif /* !errno */
40
41 extern int readline_echoing_p;
42 extern int _rl_eof_char;
43
44 #if defined (__GO32__)
45 # include <sys/pc.h>
46 # undef HANDLE_SIGNALS
47 #endif /* __GO32__ */
48
49 /* **************************************************************** */
50 /* */
51 /* Signal Management */
52 /* */
53 /* **************************************************************** */
54
55 #if defined (HAVE_POSIX_SIGNALS)
56 static sigset_t sigint_set, sigint_oset;
57 #else /* !HAVE_POSIX_SIGNALS */
58 # if defined (HAVE_BSD_SIGNALS)
59 static int sigint_oldmask;
60 # endif /* HAVE_BSD_SIGNALS */
61 #endif /* !HAVE_POSIX_SIGNALS */
62
63 static int sigint_blocked = 0;
64
65 /* Cause SIGINT to not be delivered until the corresponding call to
66 release_sigint(). */
67 static void
68 block_sigint ()
69 {
70 if (sigint_blocked)
71 return;
72
73 #if defined (HAVE_POSIX_SIGNALS)
74 sigemptyset (&sigint_set);
75 sigemptyset (&sigint_oset);
76 sigaddset (&sigint_set, SIGINT);
77 sigprocmask (SIG_BLOCK, &sigint_set, &sigint_oset);
78 #else /* !HAVE_POSIX_SIGNALS */
79 # if defined (HAVE_BSD_SIGNALS)
80 sigint_oldmask = sigblock (sigmask (SIGINT));
81 # else /* !HAVE_BSD_SIGNALS */
82 # if defined (HAVE_USG_SIGHOLD)
83 sighold (SIGINT);
84 # endif /* HAVE_USG_SIGHOLD */
85 # endif /* !HAVE_BSD_SIGNALS */
86 #endif /* !HAVE_POSIX_SIGNALS */
87 sigint_blocked = 1;
88 }
89
90 /* Allow SIGINT to be delivered. */
91 static void
92 release_sigint ()
93 {
94 if (!sigint_blocked)
95 return;
96
97 #if defined (HAVE_POSIX_SIGNALS)
98 sigprocmask (SIG_SETMASK, &sigint_oset, (sigset_t *)NULL);
99 #else
100 # if defined (HAVE_BSD_SIGNALS)
101 sigsetmask (sigint_oldmask);
102 # else /* !HAVE_BSD_SIGNALS */
103 # if defined (HAVE_USG_SIGHOLD)
104 sigrelse (SIGINT);
105 # endif /* HAVE_USG_SIGHOLD */
106 # endif /* !HAVE_BSD_SIGNALS */
107 #endif /* !HAVE_POSIX_SIGNALS */
108
109 sigint_blocked = 0;
110 }
111
112 /* **************************************************************** */
113 /* */
114 /* Controlling the Meta Key and Keypad */
115 /* */
116 /* **************************************************************** */
117
118 extern int term_has_meta;
119 extern char *term_mm;
120 extern char *term_mo;
121
122 extern char *term_ks;
123 extern char *term_ke;
124
125 static int
126 outchar (c)
127 int c;
128 {
129 return putc (c, rl_outstream);
130 }
131
132 /* Turn on/off the meta key depending on ON. */
133 static void
134 control_meta_key (on)
135 int on;
136 {
137 if (term_has_meta)
138 {
139 if (on && term_mm)
140 tputs (term_mm, 1, outchar);
141 else if (!on && term_mo)
142 tputs (term_mo, 1, outchar);
143 }
144 }
145
146 static void
147 control_keypad (on)
148 int on;
149 {
150 if (on && term_ks)
151 tputs (term_ks, 1, outchar);
152 else if (!on && term_ke)
153 tputs (term_ke, 1, outchar);
154 }
155
156 /* **************************************************************** */
157 /* */
158 /* Saving and Restoring the TTY */
159 /* */
160 /* **************************************************************** */
161
162 /* Non-zero means that the terminal is in a prepped state. */
163 static int terminal_prepped = 0;
164
165 /* If non-zero, means that this process has called tcflow(fd, TCOOFF)
166 and output is suspended. */
167 #if defined (__ksr1__)
168 static int ksrflow = 0;
169 #endif
170 #if defined (NEW_TTY_DRIVER)
171
172 /* Values for the `flags' field of a struct bsdtty. This tells which
173 elements of the struct bsdtty have been fetched from the system and
174 are valid. */
175 #define SGTTY_SET 0x01
176 #define LFLAG_SET 0x02
177 #define TCHARS_SET 0x04
178 #define LTCHARS_SET 0x08
179
180 struct bsdtty {
181 struct sgttyb sgttyb; /* Basic BSD tty driver information. */
182 int lflag; /* Local mode flags, like LPASS8. */
183 #if defined (TIOCGETC)
184 struct tchars tchars; /* Terminal special characters, including ^S and ^Q. */
185 #endif
186 #if defined (TIOCGLTC)
187 struct ltchars ltchars; /* 4.2 BSD editing characters */
188 #endif
189 int flags; /* Bitmap saying which parts of the struct are valid. */
190 };
191
192 #define TIOTYPE struct bsdtty
193
194 static TIOTYPE otio;
195
196 static int
197 get_tty_settings (tty, tiop)
198 int tty;
199 TIOTYPE *tiop;
200 {
201 #if !defined (SHELL) && defined (TIOCGWINSZ)
202 struct winsize w;
203
204 if (ioctl (tty, TIOCGWINSZ, &w) == 0)
205 (void) ioctl (tty, TIOCSWINSZ, &w);
206 #endif
207
208 tiop->flags = tiop->lflag = 0;
209
210 ioctl (tty, TIOCGETP, &(tiop->sgttyb));
211 tiop->flags |= SGTTY_SET;
212
213 #if defined (TIOCLGET)
214 ioctl (tty, TIOCLGET, &(tiop->lflag));
215 tiop->flags |= LFLAG_SET;
216 #endif
217
218 #if defined (TIOCGETC)
219 ioctl (tty, TIOCGETC, &(tiop->tchars));
220 tiop->flags |= TCHARS_SET;
221 #endif
222
223 #if defined (TIOCGLTC)
224 ioctl (tty, TIOCGLTC, &(tiop->ltchars));
225 tiop->flags |= LTCHARS_SET;
226 #endif
227
228 return 0;
229 }
230
231 set_tty_settings (tty, tiop)
232 int tty;
233 TIOTYPE *tiop;
234 {
235 if (tiop->flags & SGTTY_SET)
236 {
237 ioctl (tty, TIOCSETN, &(tiop->sgttyb));
238 tiop->flags &= ~SGTTY_SET;
239 }
240 readline_echoing_p = 1;
241
242 #if defined (TIOCLSET)
243 if (tiop->flags & LFLAG_SET)
244 {
245 ioctl (tty, TIOCLSET, &(tiop->lflag));
246 tiop->flags &= ~LFLAG_SET;
247 }
248 #endif
249
250 #if defined (TIOCSETC)
251 if (tiop->flags & TCHARS_SET)
252 {
253 ioctl (tty, TIOCSETC, &(tiop->tchars));
254 tiop->flags &= ~TCHARS_SET;
255 }
256 #endif
257
258 #if defined (TIOCSLTC)
259 if (tiop->flags & LTCHARS_SET)
260 {
261 ioctl (tty, TIOCSLTC, &(tiop->ltchars));
262 tiop->flags &= ~LTCHARS_SET;
263 }
264 #endif
265
266 return 0;
267 }
268
269 static void
270 prepare_terminal_settings (meta_flag, otio, tiop)
271 int meta_flag;
272 TIOTYPE otio, *tiop;
273 {
274 #if !defined (__GO32__)
275 readline_echoing_p = (otio.sgttyb.sg_flags & ECHO);
276
277 /* Copy the original settings to the structure we're going to use for
278 our settings. */
279 tiop->sgttyb = otio.sgttyb;
280 tiop->lflag = otio.lflag;
281 #if defined (TIOCGETC)
282 tiop->tchars = otio.tchars;
283 #endif
284 #if defined (TIOCGLTC)
285 tiop->ltchars = otio.ltchars;
286 #endif
287 tiop->flags = otio.flags;
288
289 /* First, the basic settings to put us into character-at-a-time, no-echo
290 input mode. */
291 tiop->sgttyb.sg_flags &= ~(ECHO | CRMOD);
292 tiop->sgttyb.sg_flags |= CBREAK;
293
294 /* If this terminal doesn't care how the 8th bit is used, then we can
295 use it for the meta-key. If only one of even or odd parity is
296 specified, then the terminal is using parity, and we cannot. */
297 #if !defined (ANYP)
298 # define ANYP (EVENP | ODDP)
299 #endif
300 if (((otio.sgttyb.sg_flags & ANYP) == ANYP) ||
301 ((otio.sgttyb.sg_flags & ANYP) == 0))
302 {
303 tiop->sgttyb.sg_flags |= ANYP;
304
305 /* Hack on local mode flags if we can. */
306 #if defined (TIOCLGET)
307 # if defined (LPASS8)
308 tiop->lflag |= LPASS8;
309 # endif /* LPASS8 */
310 #endif /* TIOCLGET */
311 }
312
313 #if defined (TIOCGETC)
314 # if defined (USE_XON_XOFF)
315 /* Get rid of terminal output start and stop characters. */
316 tiop->tchars.t_stopc = -1; /* C-s */
317 tiop->tchars.t_startc = -1; /* C-q */
318
319 /* If there is an XON character, bind it to restart the output. */
320 if (otio.tchars.t_startc != -1)
321 rl_bind_key (otio.tchars.t_startc, rl_restart_output);
322 # endif /* USE_XON_XOFF */
323
324 /* If there is an EOF char, bind _rl_eof_char to it. */
325 if (otio.tchars.t_eofc != -1)
326 _rl_eof_char = otio.tchars.t_eofc;
327
328 # if defined (NO_KILL_INTR)
329 /* Get rid of terminal-generated SIGQUIT and SIGINT. */
330 tiop->tchars.t_quitc = -1; /* C-\ */
331 tiop->tchars.t_intrc = -1; /* C-c */
332 # endif /* NO_KILL_INTR */
333 #endif /* TIOCGETC */
334
335 #if defined (TIOCGLTC)
336 /* Make the interrupt keys go away. Just enough to make people happy. */
337 tiop->ltchars.t_dsuspc = -1; /* C-y */
338 tiop->ltchars.t_lnextc = -1; /* C-v */
339 #endif /* TIOCGLTC */
340 #endif /* !__GO32__ */
341 }
342
343 #else /* !defined (NEW_TTY_DRIVER) */
344
345 #if !defined (VMIN)
346 # define VMIN VEOF
347 #endif
348
349 #if !defined (VTIME)
350 # define VTIME VEOL
351 #endif
352
353 #if defined (TERMIOS_TTY_DRIVER)
354 # define TIOTYPE struct termios
355 # define DRAIN_OUTPUT(fd) tcdrain (fd)
356 # define GETATTR(tty, tiop) (tcgetattr (tty, tiop))
357 # define SETATTR(tty, tiop) (tcsetattr (tty, TCSANOW, tiop))
358 #else
359 # define TIOTYPE struct termio
360 # define DRAIN_OUTPUT(fd)
361 # define GETATTR(tty, tiop) (ioctl (tty, TCGETA, tiop))
362 # define SETATTR(tty, tiop) (ioctl (tty, TCSETA, tiop))
363 #endif /* !TERMIOS_TTY_DRIVER */
364
365 static TIOTYPE otio;
366
367 static int
368 get_tty_settings (tty, tiop)
369 int tty;
370 TIOTYPE *tiop;
371 {
372 #if !defined (SHELL) && defined (TIOCGWINSZ)
373 struct winsize w;
374
375 if (ioctl (tty, TIOCGWINSZ, &w) == 0)
376 (void) ioctl (tty, TIOCSWINSZ, &w);
377 #endif
378
379 while (GETATTR (tty, tiop) < 0)
380 {
381 if (errno != EINTR)
382 return -1;
383 errno = 0;
384 }
385 return 0;
386 }
387
388 static int
389 set_tty_settings (tty, tiop)
390 int tty;
391 TIOTYPE *tiop;
392 {
393 while (SETATTR (tty, tiop) < 0)
394 {
395 if (errno != EINTR)
396 return -1;
397 errno = 0;
398 }
399
400 #if 0
401
402 #if defined (TERMIOS_TTY_DRIVER)
403 # if defined (__ksr1__)
404 if (ksrflow)
405 {
406 ksrflow = 0;
407 tcflow (tty, TCOON);
408 }
409 # else /* !ksr1 */
410 tcflow (tty, TCOON); /* Simulate a ^Q. */
411 # endif /* !ksr1 */
412 #else
413 ioctl (tty, TCXONC, 1); /* Simulate a ^Q. */
414 #endif /* !TERMIOS_TTY_DRIVER */
415
416 #endif
417
418 return 0;
419 }
420
421 static void
422 prepare_terminal_settings (meta_flag, otio, tiop)
423 int meta_flag;
424 TIOTYPE otio, *tiop;
425 {
426 readline_echoing_p = (otio.c_lflag & ECHO);
427
428 tiop->c_lflag &= ~(ICANON | ECHO);
429
430 if ((unsigned char) otio.c_cc[VEOF] != (unsigned char) _POSIX_VDISABLE)
431 _rl_eof_char = otio.c_cc[VEOF];
432
433 #if defined (USE_XON_XOFF)
434 #if defined (IXANY)
435 tiop->c_iflag &= ~(IXON | IXOFF | IXANY);
436 #else
437 /* `strict' Posix systems do not define IXANY. */
438 tiop->c_iflag &= ~(IXON | IXOFF);
439 #endif /* IXANY */
440 #endif /* USE_XON_XOFF */
441
442 /* Only turn this off if we are using all 8 bits. */
443 if (((tiop->c_cflag & CSIZE) == CS8) || meta_flag)
444 tiop->c_iflag &= ~(ISTRIP | INPCK);
445
446 /* Make sure we differentiate between CR and NL on input. */
447 tiop->c_iflag &= ~(ICRNL | INLCR);
448
449 #if !defined (HANDLE_SIGNALS)
450 tiop->c_lflag &= ~ISIG;
451 #else
452 tiop->c_lflag |= ISIG;
453 #endif
454
455 tiop->c_cc[VMIN] = 1;
456 tiop->c_cc[VTIME] = 0;
457
458 /* Turn off characters that we need on Posix systems with job control,
459 just to be sure. This includes ^Y and ^V. This should not really
460 be necessary. */
461 #if defined (TERMIOS_TTY_DRIVER) && defined (_POSIX_VDISABLE)
462
463 #if defined (VLNEXT)
464 tiop->c_cc[VLNEXT] = _POSIX_VDISABLE;
465 #endif
466
467 #if defined (VDSUSP)
468 tiop->c_cc[VDSUSP] = _POSIX_VDISABLE;
469 #endif
470
471 #endif /* TERMIOS_TTY_DRIVER && _POSIX_VDISABLE */
472 }
473 #endif /* NEW_TTY_DRIVER */
474
475 /* Put the terminal in CBREAK mode so that we can detect key presses. */
476 void
477 rl_prep_terminal (meta_flag)
478 int meta_flag;
479 {
480 #if !defined (__GO32__)
481 int tty = fileno (rl_instream);
482 TIOTYPE tio;
483
484 if (terminal_prepped)
485 return;
486
487 /* Try to keep this function from being INTerrupted. */
488 block_sigint ();
489
490 if (get_tty_settings (tty, &tio) < 0)
491 {
492 release_sigint ();
493 return;
494 }
495
496 otio = tio;
497
498 prepare_terminal_settings (meta_flag, otio, &tio);
499
500 if (set_tty_settings (tty, &tio) < 0)
501 {
502 release_sigint ();
503 return;
504 }
505
506 control_meta_key (1);
507 control_keypad (1);
508 terminal_prepped = 1;
509
510 release_sigint ();
511 #endif /* !__GO32__ */
512 }
513
514 /* Restore the terminal's normal settings and modes. */
515 void
516 rl_deprep_terminal ()
517 {
518 #if !defined (__GO32__)
519 int tty = fileno (rl_instream);
520
521 if (!terminal_prepped)
522 return;
523
524 /* Try to keep this function from being INTerrupted. */
525 block_sigint ();
526
527 if (set_tty_settings (tty, &otio) < 0)
528 {
529 release_sigint ();
530 return;
531 }
532
533 control_meta_key (0);
534 control_keypad (0);
535 terminal_prepped = 0;
536
537 release_sigint ();
538 #endif /* !__GO32__ */
539 }
540
541 /* **************************************************************** */
542 /* */
543 /* Bogus Flow Control */
544 /* */
545 /* **************************************************************** */
546
547 rl_restart_output (count, key)
548 int count, key;
549 {
550 int fildes = fileno (rl_outstream);
551 #if defined (TIOCSTART)
552 #if defined (apollo)
553 ioctl (&fildes, TIOCSTART, 0);
554 #else
555 ioctl (fildes, TIOCSTART, 0);
556 #endif /* apollo */
557
558 #else /* !TIOCSTART */
559 # if defined (TERMIOS_TTY_DRIVER)
560 # if defined (__ksr1__)
561 if (ksrflow)
562 {
563 ksrflow = 0;
564 tcflow (fildes, TCOON);
565 }
566 # else /* !ksr1 */
567 tcflow (fildes, TCOON); /* Simulate a ^Q. */
568 # endif /* !ksr1 */
569 # else /* !TERMIOS_TTY_DRIVER */
570 # if defined (TCXONC)
571 ioctl (fildes, TCXONC, TCOON);
572 # endif /* TCXONC */
573 # endif /* !TERMIOS_TTY_DRIVER */
574 #endif /* !TIOCSTART */
575
576 return 0;
577 }
578
579 rl_stop_output (count, key)
580 int count, key;
581 {
582 int fildes = fileno (rl_instream);
583
584 #if defined (TIOCSTOP)
585 # if defined (apollo)
586 ioctl (&fildes, TIOCSTOP, 0);
587 # else
588 ioctl (fildes, TIOCSTOP, 0);
589 # endif /* apollo */
590 #else /* !TIOCSTOP */
591 # if defined (TERMIOS_TTY_DRIVER)
592 # if defined (__ksr1__)
593 ksrflow = 1;
594 # endif /* ksr1 */
595 tcflow (fildes, TCOOFF);
596 # else
597 # if defined (TCXONC)
598 ioctl (fildes, TCXONC, TCOON);
599 # endif /* TCXONC */
600 # endif /* !TERMIOS_TTY_DRIVER */
601 #endif /* !TIOCSTOP */
602
603 return 0;
604 }
605
606 /* **************************************************************** */
607 /* */
608 /* Default Key Bindings */
609 /* */
610 /* **************************************************************** */
611 void
612 rltty_set_default_bindings (kmap)
613 Keymap kmap;
614 {
615 TIOTYPE ttybuff;
616 int tty = fileno (rl_instream);
617
618 #if defined (NEW_TTY_DRIVER)
619
620 #define SET_SPECIAL(sc, func) \
621 do \
622 { \
623 int ic; \
624 ic = sc; \
625 if (ic != -1 && kmap[ic].type == ISFUNC) \
626 kmap[ic].function = func; \
627 } \
628 while (0)
629
630 if (get_tty_settings (tty, &ttybuff) == 0)
631 {
632 if (ttybuff.flags & SGTTY_SET)
633 {
634 SET_SPECIAL (ttybuff.sgttyb.sg_erase, rl_rubout);
635 SET_SPECIAL (ttybuff.sgttyb.sg_kill, rl_unix_line_discard);
636 }
637
638 # if defined (TIOCGLTC)
639 if (ttybuff.flags & LTCHARS_SET)
640 {
641 SET_SPECIAL (ttybuff.ltchars.t_werasc, rl_unix_word_rubout);
642 SET_SPECIAL (ttybuff.ltchars.t_lnextc, rl_quoted_insert);
643 }
644 # endif /* TIOCGLTC */
645 }
646
647 #else /* !NEW_TTY_DRIVER */
648
649 #define SET_SPECIAL(sc, func) \
650 do \
651 { \
652 unsigned char uc; \
653 uc = ttybuff.c_cc[sc]; \
654 if (uc != (unsigned char)_POSIX_VDISABLE && kmap[uc].type == ISFUNC) \
655 kmap[uc].function = func; \
656 } \
657 while (0)
658
659 if (get_tty_settings (tty, &ttybuff) == 0)
660 {
661 SET_SPECIAL (VERASE, rl_rubout);
662 SET_SPECIAL (VKILL, rl_unix_line_discard);
663
664 # if defined (VLNEXT) && defined (TERMIOS_TTY_DRIVER)
665 SET_SPECIAL (VLNEXT, rl_quoted_insert);
666 # endif /* VLNEXT && TERMIOS_TTY_DRIVER */
667
668 # if defined (VWERASE) && defined (TERMIOS_TTY_DRIVER)
669 SET_SPECIAL (VWERASE, rl_unix_word_rubout);
670 # endif /* VWERASE && TERMIOS_TTY_DRIVER */
671 }
672 #endif /* !NEW_TTY_DRIVER */
673 }