home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / utilities / cli / gnuinfo / Source / c / terminal < prev    next >
Encoding:
Text File  |  1994-10-01  |  18.5 KB  |  747 lines

  1. #include "defines.h"
  2. /* terminal.c -- How to handle the physical terminal for Info. */
  3.  
  4. /* This file is part of GNU Info, a program for reading online documentation
  5.    stored in Info format.
  6.  
  7.    This file has appeared in prior works by the Free Software Foundation;
  8.    thus it carries copyright dates from 1988 through 1993.
  9.  
  10.    Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993 Free Software
  11.    Foundation, Inc.
  12.  
  13.    This program is free software; you can redistribute it and/or modify
  14.    it under the terms of the GNU General Public License as published by
  15.    the Free Software Foundation; either version 2, or (at your option)
  16.    any later version.
  17.  
  18.    This program is distributed in the hope that it will be useful,
  19.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.    GNU General Public License for more details.
  22.  
  23.    You should have received a copy of the GNU General Public License
  24.    along with this program; if not, write to the Free Software
  25.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26.  
  27.    Written by Brian Fox (bfox@ai.mit.edu). */
  28.  
  29. #include <stdio.h>
  30. #include <sys/types.h>
  31. #include "terminal.h"
  32. #include "termdep.h"
  33.  
  34. extern void *xmalloc (), *xrealloc ();
  35.  
  36. /* The Unix termcap interface code. */
  37.  
  38. extern int tgetnum (), tgetflag (), tgetent ();
  39. extern char *tgetstr (), *tgoto ();
  40. extern char *getenv ();
  41. extern void tputs ();
  42.  
  43. /* Function "hooks".  If you make one of these point to a function, that
  44.    function is called when appropriate instead of its namesake.  Your
  45.    function is called with exactly the same arguments that were passed
  46.    to the namesake function. */
  47. VFunction *terminal_begin_inverse_hook = (VFunction *)NULL;
  48. VFunction *terminal_end_inverse_hook = (VFunction *)NULL;
  49. VFunction *terminal_prep_terminal_hook = (VFunction *)NULL;
  50. VFunction *terminal_unprep_terminal_hook = (VFunction *)NULL;
  51. VFunction *terminal_up_line_hook = (VFunction *)NULL;
  52. VFunction *terminal_down_line_hook = (VFunction *)NULL;
  53. VFunction *terminal_clear_screen_hook = (VFunction *)NULL;
  54. VFunction *terminal_clear_to_eol_hook = (VFunction *)NULL;
  55. VFunction *terminal_get_screen_size_hook = (VFunction *)NULL;
  56. VFunction *terminal_goto_xy_hook = (VFunction *)NULL;
  57. VFunction *terminal_initialize_terminal_hook = (VFunction *)NULL;
  58. VFunction *terminal_new_terminal_hook = (VFunction *)NULL;
  59. VFunction *terminal_put_text_hook = (VFunction *)NULL;
  60. VFunction *terminal_ring_bell_hook = (VFunction *)NULL;
  61. VFunction *terminal_write_chars_hook = (VFunction *)NULL;
  62. VFunction *terminal_scroll_terminal_hook = (VFunction *)NULL;
  63.  
  64. /* **************************************************************** */
  65. /*                                    */
  66. /*            Terminal and Termcap                */
  67. /*                                    */
  68. /* **************************************************************** */
  69.  
  70. /* On Solaris2, sys/types.h #includes sys/reg.h, which #defines PC.
  71.    Unfortunately, PC is a global variable used by the termcap library. */
  72. #undef PC
  73.  
  74. /* TERMCAP requires these variables, whether we access them or not. */
  75. char PC;
  76. char *BC, *UP;
  77. short ospeed;
  78.  
  79. /* A buffer which holds onto the current terminal description, and a pointer
  80.    used to float within it. */
  81. static char *term_buffer = (char *)NULL;
  82. static char *term_string_buffer = (char *)NULL;
  83.  
  84. /* Some strings to control terminal actions.  These are output by tputs (). */
  85. static char *term_goto, *term_clreol, *term_cr, *term_clrpag;
  86. static char *term_begin_use, *term_end_use;
  87. static char *term_AL, *term_DL, *term_al, *term_dl;
  88.  
  89. /* How to go up a line. */
  90. static char *term_up;
  91.  
  92. /* How to go down a line. */
  93. static char *term_dn;
  94.  
  95. /* An audible bell, if the terminal can be made to make noise. */
  96. static char *audible_bell;
  97.  
  98. /* A visible bell, if the terminal can be made to flash the screen. */
  99. static char *visible_bell;
  100.  
  101. /* The string to write to turn on the meta key, if this term has one. */
  102. static char *term_mm;
  103.  
  104. /* The string to write to turn off the meta key, if this term has one. */
  105. static char *term_mo;
  106.  
  107. /* The string to turn on inverse mode, if this term has one. */
  108. static char *term_invbeg;
  109.  
  110. /* The string to turn off inverse mode, if this term has one. */
  111. static char *term_invend;
  112.  
  113. static void
  114. output_character_function (c)
  115.      int c;
  116. {
  117.   putc (c, stdout);
  118. }
  119.  
  120. /* Macro to send STRING to the terminal. */
  121. #define send_to_terminal(string) \
  122.   do { \
  123.     if (string) \
  124.       tputs (string, 1, output_character_function); \
  125.      } while (0)
  126.  
  127. /* Tell the terminal that we will be doing cursor addressable motion. */
  128. static void
  129. terminal_begin_using_terminal ()
  130. {
  131.   send_to_terminal (term_begin_use);
  132. }
  133.  
  134. /* Tell the terminal that we will not be doing any more cursor addressable
  135.    motion. */
  136. static void
  137. terminal_end_using_terminal ()
  138. {
  139.   send_to_terminal (term_end_use);
  140. }
  141.  
  142. /* **************************************************************** */
  143. /*                                    */
  144. /*             Necessary Terminal Functions            */
  145. /*                                    */
  146. /* **************************************************************** */
  147.  
  148. /* The functions and variables on this page implement the user visible
  149.    portion of the terminal interface. */
  150.  
  151. /* The width and height of the terminal. */
  152. int screenwidth, screenheight;
  153.  
  154. /* Non-zero means this terminal can't really do anything. */
  155. int terminal_is_dumb_p = 0;
  156.  
  157. /* Non-zero means that this terminal has a meta key. */
  158. int terminal_has_meta_p = 0;
  159.  
  160. /* Non-zero means that this terminal can produce a visible bell. */
  161. int terminal_has_visible_bell_p = 0;
  162.  
  163. /* Non-zero means to use that visible bell if at all possible. */
  164. int terminal_use_visible_bell_p = 0;
  165.  
  166. /* Non-zero means that the terminal can do scrolling. */
  167. int terminal_can_scroll = 0;
  168.  
  169. /* The key sequences output by the arrow keys, if this terminal has any. */
  170. char *term_ku, *term_kd, *term_kr, *term_kl;
  171.  
  172. /* Move the cursor to the terminal location of X and Y. */
  173. void
  174. terminal_goto_xy (x, y)
  175.      int x, y;
  176. {
  177.   if (terminal_goto_xy_hook)
  178.     (*terminal_goto_xy_hook) (x, y);
  179.   else
  180.     {
  181.       if (term_goto)
  182.     tputs (tgoto (term_goto, x, y), 1, output_character_function);
  183.     }
  184. }
  185.  
  186. /* Print STRING to the terminal at the current position. */
  187. void
  188. terminal_put_text (string)
  189.      char *string;
  190. {
  191.   if (terminal_put_text_hook)
  192.     (*terminal_put_text_hook) (string);
  193.   else
  194.     {
  195.       printf ("%s", string);
  196.     }
  197. }
  198.  
  199. /* Print NCHARS from STRING to the terminal at the current position. */
  200. void
  201. terminal_write_chars (string, nchars)
  202.      char *string;
  203.      int nchars;
  204. {
  205.   if (terminal_write_chars_hook)
  206.     (*terminal_write_chars_hook) (string, nchars);
  207.   else
  208.     {
  209.       if (nchars)
  210.     fwrite (string, 1, nchars, stdout);
  211.     }
  212. }
  213.  
  214. /* Clear from the current position of the cursor to the end of the line. */
  215. void
  216. terminal_clear_to_eol ()
  217. {
  218.   if (terminal_clear_to_eol_hook)
  219.     (*terminal_clear_to_eol_hook) ();
  220.   else
  221.     {
  222.       send_to_terminal (term_clreol);
  223.     }
  224. }
  225.  
  226. /* Clear the entire terminal screen. */
  227. void
  228. terminal_clear_screen ()
  229. {
  230.   if (terminal_clear_screen_hook)
  231.     (*terminal_clear_screen_hook) ();
  232.   else
  233.     {
  234.       send_to_terminal (term_clrpag);
  235.     }
  236. }
  237.  
  238. /* Move the cursor up one line. */
  239. void
  240. terminal_up_line ()
  241. {
  242.   if (terminal_up_line_hook)
  243.     (*terminal_up_line_hook) ();
  244.   else
  245.     {
  246.       send_to_terminal (term_up);
  247.     }
  248. }
  249.  
  250. /* Move the cursor down one line. */
  251. void
  252. terminal_down_line ()
  253. {
  254.   if (terminal_down_line_hook)
  255.     (*terminal_down_line_hook) ();
  256.   else
  257.     {
  258.       send_to_terminal (term_dn);
  259.     }
  260. }
  261.  
  262. /* Turn on reverse video if possible. */
  263. void
  264. terminal_begin_inverse ()
  265. {
  266.   if (terminal_begin_inverse_hook)
  267.     (*terminal_begin_inverse_hook) ();
  268.   else
  269.     {
  270.       send_to_terminal (term_invbeg);
  271.     }
  272. }
  273.  
  274. /* Turn off reverse video if possible. */
  275. void
  276. terminal_end_inverse ()
  277. {
  278.   if (terminal_end_inverse_hook)
  279.     (*terminal_end_inverse_hook) ();
  280.   else
  281.     {
  282.       send_to_terminal (term_invend);
  283.     }
  284. }
  285.  
  286. /* Ring the terminal bell.  The bell is run visibly if it both has one and
  287.    terminal_use_visible_bell_p is non-zero. */
  288. void
  289. terminal_ring_bell ()
  290. {
  291.   if (terminal_ring_bell_hook)
  292.     (*terminal_ring_bell_hook) ();
  293.   else
  294.     {
  295.       if (terminal_has_visible_bell_p && terminal_use_visible_bell_p)
  296.     send_to_terminal (visible_bell);
  297.       else
  298.     send_to_terminal (audible_bell);
  299.     }
  300. }
  301.  
  302. /* At the line START, delete COUNT lines from the terminal display. */
  303. static void
  304. terminal_delete_lines (start, count)
  305.      int start, count;
  306. {
  307.   int lines;
  308.  
  309.   /* Normalize arguments. */
  310.   if (start < 0)
  311.     start = 0;
  312.  
  313.   lines = screenheight - start;
  314.   terminal_goto_xy (0, start);
  315.   if (term_DL)
  316.     tputs (tgoto (term_DL, 0, count), lines, output_character_function);
  317.   else
  318.     {
  319.       while (count--)
  320.     tputs (term_dl, lines, output_character_function);
  321.     }
  322.  
  323.   fflush (stdout);
  324. }
  325.  
  326. /* At the line START, insert COUNT lines in the terminal display. */
  327. static void
  328. terminal_insert_lines (start, count)
  329.      int start, count;
  330. {
  331.   int lines;
  332.  
  333.   /* Normalize arguments. */
  334.   if (start < 0)
  335.     start = 0;
  336.  
  337.   lines = screenheight - start;
  338.   terminal_goto_xy (0, start);
  339.  
  340.   if (term_AL)
  341.     tputs (tgoto (term_AL, 0, count), lines, output_character_function);
  342.   else
  343.     {
  344.       while (count--)
  345.     tputs (term_al, lines, output_character_function);
  346.     }
  347.  
  348.   fflush (stdout);
  349. }
  350.  
  351. /* Scroll an area of the terminal, starting with the region from START
  352.    to END, AMOUNT lines.  If AMOUNT is negative, the lines are scrolled
  353.    towards the top of the screen, else they are scrolled towards the
  354.    bottom of the screen. */
  355. void
  356. terminal_scroll_terminal (start, end, amount)
  357.      int start, end, amount;
  358. {
  359.   if (!terminal_can_scroll)
  360.     return;
  361.  
  362.   /* Any scrolling at all? */
  363.   if (amount == 0)
  364.     return;
  365.  
  366.   if (terminal_scroll_terminal_hook)
  367.     (*terminal_scroll_terminal_hook) (start, end, amount);
  368.   else
  369.     {
  370.       /* If we are scrolling down, delete AMOUNT lines at END.  Then insert
  371.      AMOUNT lines at START. */
  372.       if (amount > 0)
  373.     {
  374.       terminal_delete_lines (end, amount);
  375.       terminal_insert_lines (start, amount);
  376.     }
  377.  
  378.       /* If we are scrolling up, delete AMOUNT lines before START.  This
  379.      actually does the upwards scroll.  Then, insert AMOUNT lines
  380.      after the already scrolled region (i.e., END - AMOUNT). */
  381.       if (amount < 0)
  382.     {
  383.       int abs_amount = -amount;
  384.       terminal_delete_lines (start - abs_amount, abs_amount);
  385.       terminal_insert_lines (end - abs_amount, abs_amount);
  386.     }
  387.     }
  388. }
  389.  
  390. /* Re-initialize the terminal considering that the TERM/TERMCAP variable
  391.    has changed. */
  392. void
  393. terminal_new_terminal (terminal_name)
  394.      char *terminal_name;
  395. {
  396.   if (terminal_new_terminal_hook)
  397.     (*terminal_new_terminal_hook) (terminal_name);
  398.   else
  399.     {
  400.       terminal_initialize_terminal (terminal_name);
  401.     }
  402. }
  403.  
  404. /* Set the global variables SCREENWIDTH and SCREENHEIGHT. */
  405. void
  406. terminal_get_screen_size ()
  407. {
  408.   if (terminal_get_screen_size_hook)
  409.     (*terminal_get_screen_size_hook) ();
  410.   else
  411.     {
  412.       screenwidth = screenheight = 0;
  413.  
  414. #if defined (TIOCGWINSZ)
  415.       {
  416.     struct winsize window_size;
  417.  
  418.     if (ioctl (fileno (stdout), TIOCGWINSZ, &window_size) == 0)
  419.       {
  420.         screenwidth = (int) window_size.ws_col;
  421.         screenheight = (int) window_size.ws_row;
  422.       }
  423.       }
  424. #endif                /* TIOCGWINSZ */
  425.  
  426.       /* Environment variable COLUMNS overrides setting of "co". */
  427.       if (screenwidth <= 0)
  428.     {
  429.       char *sw = getenv ("COLUMNS");
  430.  
  431.       if (sw)
  432.         screenwidth = atoi (sw);
  433.  
  434.       if (screenwidth <= 0)
  435.         screenwidth = tgetnum ("co");
  436.     }
  437.  
  438.       /* Environment variable LINES overrides setting of "li". */
  439.       if (screenheight <= 0)
  440.     {
  441.       char *sh = getenv ("LINES");
  442.  
  443.       if (sh)
  444.         screenheight = atoi (sh);
  445.  
  446.       if (screenheight <= 0)
  447.         screenheight = tgetnum ("li");
  448.     }
  449.  
  450.       /* If all else fails, default to 80x24 terminal. */
  451.       if (screenwidth <= 0)
  452.     screenwidth = 80;
  453.  
  454.       if (screenheight <= 0)
  455.     screenheight = 24;
  456.     }
  457. }
  458.  
  459. /* Initialize the terminal which is known as TERMINAL_NAME.  If this terminal
  460.    doesn't have cursor addressability, TERMINAL_IS_DUMB_P becomes non-zero.
  461.    The variables SCREENHEIGHT and SCREENWIDTH are set to the dimensions that
  462.    this terminal actually has.  The variable TERMINAL_HAS_META_P becomes non-
  463.    zero if this terminal supports a Meta key.  Finally, the terminal screen is
  464.    cleared. */
  465. void
  466. terminal_initialize_terminal (terminal_name)
  467.      char *terminal_name;
  468. {
  469.   char *term, *buffer;
  470.  
  471.   terminal_is_dumb_p = 0;
  472.  
  473.   if (terminal_initialize_terminal_hook)
  474.     {
  475.       (*terminal_initialize_terminal_hook) (terminal_name);
  476.       return;
  477.     }
  478.  
  479.   term = terminal_name ? terminal_name : getenv ("TERM");
  480.  
  481.   if (!term_string_buffer)
  482.     term_string_buffer = (char *)xmalloc (2048);
  483.  
  484.   if (!term_buffer)
  485.     term_buffer = (char *)xmalloc (2048);
  486.  
  487.   buffer = term_string_buffer;
  488.  
  489.   term_clrpag = term_cr = term_clreol = (char *)NULL;
  490.  
  491.   if (!term)
  492.     term = "dumb";
  493.  
  494.   if (tgetent (term_buffer, term) <= 0)
  495.     {
  496.       terminal_is_dumb_p = 1;
  497.       screenwidth = 80;
  498.       screenheight = 24;
  499.       term_cr = "\r";
  500.       term_up = term_dn = audible_bell = visible_bell = (char *)NULL;
  501.       term_ku = term_kd = term_kl = term_kr = (char *)NULL;
  502.       return;
  503.     }
  504.  
  505.   BC = tgetstr ("pc", &buffer);
  506.   PC = BC ? *BC : 0;
  507.  
  508. #if defined (TIOCGETP)
  509.   {
  510.     struct sgttyb sg;
  511.  
  512.     if (ioctl (fileno (stdout), TIOCGETP, &sg) != -1)
  513.       ospeed = sg.sg_ospeed;
  514.     else
  515.       ospeed = B9600;
  516.   }
  517. #else
  518.   ospeed = B9600;
  519. #endif                /* !TIOCGETP */
  520.  
  521.   term_cr = tgetstr ("cr", &buffer);
  522.   term_clreol = tgetstr ("ce", &buffer);
  523.   term_clrpag = tgetstr ("cl", &buffer);
  524.   term_goto = tgetstr ("cm", &buffer);
  525.  
  526.   /* Find out about this terminals scrolling capability. */
  527.   term_AL = tgetstr ("AL", &buffer);
  528.   term_DL = tgetstr ("DL", &buffer);
  529.   term_al = tgetstr ("al", &buffer);
  530.   term_dl = tgetstr ("dl", &buffer);
  531.  
  532.   terminal_can_scroll = ((term_AL || term_al) && (term_DL || term_dl));
  533.  
  534.   term_invbeg = tgetstr ("mr", &buffer);
  535.   if (term_invbeg)
  536.     term_invend = tgetstr ("me", &buffer);
  537.   else
  538.     term_invend = (char *)NULL;
  539.  
  540.   if (!term_cr)
  541.     term_cr =  "\r";
  542.  
  543.   terminal_get_screen_size ();
  544.  
  545.   term_up = tgetstr ("up", &buffer);
  546.   term_dn = tgetstr ("dn", &buffer);
  547.   visible_bell = tgetstr ("vb", &buffer);
  548.   terminal_has_visible_bell_p = (visible_bell != (char *)NULL);
  549.   audible_bell = tgetstr ("bl", &buffer);
  550.   if (!audible_bell)
  551.     audible_bell = "\007";
  552.   term_begin_use = tgetstr ("ti", &buffer);
  553.   term_end_use = tgetstr ("te", &buffer);
  554.  
  555.   /* Check to see if this terminal has a meta key. */
  556.   terminal_has_meta_p = (tgetflag ("km") || tgetflag ("MT"));
  557.   if (terminal_has_meta_p)
  558.     {
  559.       term_mm = tgetstr ("mm", &buffer);
  560.       term_mo = tgetstr ("mo", &buffer);
  561.     }
  562.   else
  563.     {
  564.       term_mm = (char *)NULL;
  565.       term_mo = (char *)NULL;
  566.     }
  567.  
  568.   /* Attempt to find the arrow keys.  */
  569.   term_ku = tgetstr ("ku", &buffer);
  570.   term_kd = tgetstr ("kd", &buffer);
  571.   term_kr = tgetstr ("kr", &buffer);
  572.   term_kl = tgetstr ("kl", &buffer);
  573.  
  574.   /* If this terminal is not cursor addressable, then it is really dumb. */
  575.   if (!term_goto)
  576.     terminal_is_dumb_p = 1;
  577.  
  578.   terminal_begin_using_terminal ();
  579. }
  580.  
  581. /* **************************************************************** */
  582. /*                                    */
  583. /*         How to Read Characters From the Terminal        */
  584. /*                                    */
  585. /* **************************************************************** */
  586.  
  587. #if defined (TIOCGETC)
  588. /* A buffer containing the terminal interrupt characters upon entry
  589.    to Info. */
  590. struct tchars original_tchars;
  591. #endif
  592.  
  593. #if defined (TIOCGLTC)
  594. /* A buffer containing the local terminal mode characters upon entry
  595.    to Info. */
  596. struct ltchars original_ltchars;
  597. #endif
  598.  
  599. #if defined (HAVE_TERMIO_H)
  600. /* A buffer containing the terminal mode flags upon entry to info. */
  601. struct termio original_termio, ttybuff;
  602. #else /* !HAVE_TERMIO_H */
  603. /* Buffers containing the terminal mode flags upon entry to info. */
  604. int original_tty_flags = 0;
  605. int original_lmode;
  606. struct sgttyb ttybuff;
  607. #endif /* !HAVE_TERMIO_H */
  608.  
  609. /* Prepare to start using the terminal to read characters singly. */
  610. void
  611. terminal_prep_terminal ()
  612. {
  613.   int tty;
  614.  
  615.   if (terminal_prep_terminal_hook)
  616.     {
  617.       (*terminal_prep_terminal_hook) ();
  618.       return;
  619.     }
  620.  
  621.   tty = fileno (stdin);
  622.  
  623. #if defined (HAVE_TERMIO_H)
  624.   ioctl (tty, TCGETA, &original_termio);
  625.   ioctl (tty, TCGETA, &ttybuff);
  626.   ttybuff.c_iflag &= (~ISTRIP & ~INLCR & ~IGNCR & ~ICRNL & ~IXON);
  627.   ttybuff.c_oflag &= (~ONLCR & ~OCRNL);
  628.   ttybuff.c_lflag &= (~ICANON & ~ECHO);
  629.  
  630.   ttybuff.c_cc[VMIN] = 1;
  631.   ttybuff.c_cc[VTIME] = 0;
  632.  
  633.   if (ttybuff.c_cc[VINTR] = '\177')
  634.     ttybuff.c_cc[VINTR] = -1;
  635.  
  636.   if (ttybuff.c_cc[VQUIT] = '\177')
  637.     ttybuff.c_cc[VQUIT] = -1;
  638.  
  639.   ioctl (tty, TCSETA, &ttybuff);
  640.  
  641. #else /* !HAVE_TERMIO_H */
  642.  
  643.   ioctl (tty, TIOCGETP, &ttybuff);
  644.  
  645.   if (!original_tty_flags)
  646.     original_tty_flags = ttybuff.sg_flags;
  647.  
  648.   /* Make this terminal pass 8 bits around while we are using it. */
  649. #if defined (PASS8)
  650.   ttybuff.sg_flags |= PASS8;
  651. #endif /* PASS8 */
  652.  
  653. #if defined (TIOCLGET) && defined (LPASS8)
  654.   {
  655.     int flags;
  656.     ioctl (tty, TIOCLGET, &flags);
  657.     original_lmode = flags;
  658.     flags |= LPASS8;
  659.     ioctl (tty, TIOCLSET, &flags);
  660.   }
  661. #endif /* TIOCLGET && LPASS8 */
  662.  
  663. #if defined (TIOCGETC)
  664.   {
  665.     struct tchars temp;
  666.  
  667.     ioctl (tty, TIOCGETC, &original_tchars);
  668.     temp = original_tchars;
  669.  
  670.     /* C-s and C-q. */
  671.     temp.t_startc = temp.t_stopc = -1;
  672.  
  673.     /* Often set to C-d. */
  674.     temp.t_eofc = -1;
  675.  
  676.     /* If the a quit or interrupt character conflicts with one of our
  677.        commands, then make it go away. */
  678.     if (temp.t_intrc == '\177')
  679.       temp.t_intrc = -1;
  680.  
  681.     if (temp.t_quitc == '\177')
  682.       temp.t_quitc = -1;
  683.  
  684.     ioctl (tty, TIOCSETC, &temp);
  685.   }
  686. #endif /* TIOCGETC */
  687.  
  688. #if defined (TIOCGLTC)
  689.   {
  690.     struct ltchars temp;
  691.  
  692.     ioctl (tty, TIOCGLTC, &original_ltchars);
  693.     temp = original_ltchars;
  694.  
  695.     /* Make the interrupt keys go away.  Just enough to make people happy. */
  696.     temp.t_lnextc = -1;        /* C-v. */
  697.     temp.t_dsuspc = -1;        /* C-y. */
  698.     temp.t_flushc = -1;        /* C-o. */
  699.     ioctl (tty, TIOCSLTC, &temp);
  700.   }
  701. #endif /* TIOCGLTC */
  702.  
  703.   ttybuff.sg_flags &= ~ECHO;
  704.   ttybuff.sg_flags |= CBREAK;
  705.   ioctl (tty, TIOCSETN, &ttybuff);
  706. #endif /* !HAVE_TERMIO_H */
  707. }
  708.  
  709. /* Restore the tty settings back to what they were before we started using
  710.    this terminal. */
  711. void
  712. terminal_unprep_terminal ()
  713. {
  714.   int tty;
  715.  
  716.   if (terminal_unprep_terminal_hook)
  717.     {
  718.       (*terminal_unprep_terminal_hook) ();
  719.       return;
  720.     }
  721.  
  722.   tty = fileno (stdin);
  723.  
  724. #if defined (HAVE_TERMIO_H)
  725.   ioctl (tty, TCSETA, &original_termio);
  726. #else /* !HAVE_TERMIO_H */
  727.   ioctl (tty, TIOCGETP, &ttybuff);
  728.   ttybuff.sg_flags = original_tty_flags;
  729.   ioctl (tty, TIOCSETN, &ttybuff);
  730.  
  731. #if defined (TIOCGETC)
  732.   ioctl (tty, TIOCSETC, &original_tchars);
  733. #endif /* TIOCGETC */
  734.  
  735. #if defined (TIOCGLTC)
  736.   ioctl (tty, TIOCSLTC, &original_ltchars);
  737. #endif /* TIOCGLTC */
  738.  
  739. #if defined (TIOCLGET) && defined (LPASS8)
  740.   ioctl (tty, TIOCLSET, &original_lmode);
  741. #endif /* TIOCLGET && LPASS8 */
  742.  
  743. #endif /* !HAVE_TERMIO_H */
  744.   terminal_end_using_terminal ();
  745. }
  746.  
  747.