home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / lib / libcurses / tty.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-31  |  4.8 KB  |  206 lines

  1. /*-
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)tty.c    5.2 (Berkeley) 8/31/92";
  36. #endif /* not lint */
  37.  
  38. /*
  39.  * Terminal initialization routines.
  40.  */
  41. #include <sys/ioctl.h>
  42.  
  43. #include <curses.h>
  44. #include <termios.h>
  45. #include <unistd.h>
  46.  
  47. struct termios newtermio, origtermio;
  48. static struct termios norawt, rawt;
  49. static int useraw;
  50.  
  51. /*
  52.  * gettmode --
  53.  *    Do terminal type initialization.
  54.  */
  55. int
  56. gettmode()
  57. {
  58.     if (tcgetattr(STDIN_FILENO, &origtermio))
  59.         return (OK);
  60.  
  61.     GT = (origtermio.c_oflag & OXTABS) == 0;
  62.     NONL = (origtermio.c_oflag & ONLCR) == 0;
  63.  
  64.     norawt = origtermio;
  65.     norawt.c_oflag &= ~OXTABS;
  66.     rawt = norawt;
  67.     cfmakeraw(&rawt);
  68.  
  69.     return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt) ? ERR : OK);
  70. }
  71.  
  72. int
  73. raw()
  74. {
  75.     useraw = __pfast = __rawmode = 1;
  76.     return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
  77. }
  78.  
  79. int
  80. noraw()
  81. {
  82.     useraw = __pfast = __rawmode = 0;
  83.     return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
  84. }
  85.  
  86. int
  87. cbreak()
  88. {
  89.     rawt.c_lflag &= ~ICANON;
  90.     norawt.c_lflag &= ~ICANON;
  91.  
  92.     __rawmode = 1;
  93.     if (useraw)
  94.         return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
  95.     else
  96.         return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
  97. }
  98.  
  99. int
  100. nocbreak()
  101. {
  102.     rawt.c_lflag |= ICANON;
  103.     norawt.c_lflag |= ICANON;
  104.  
  105.     __rawmode = 0;
  106.     if (useraw) 
  107.         return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
  108.     else
  109.         return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
  110. }
  111.     
  112. int
  113. echo()
  114. {
  115.     rawt.c_lflag |= ECHO;
  116.     norawt.c_lflag |= ECHO;
  117.     
  118.     __echoit = 1;
  119.     if (useraw) 
  120.         return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
  121.     else
  122.         return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
  123. }
  124.  
  125. int
  126. noecho()
  127. {
  128.     rawt.c_lflag &= ~ECHO;
  129.     norawt.c_lflag &= ~ECHO;
  130.     
  131.     __echoit = 0;
  132.     if (useraw) 
  133.         return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
  134.     else
  135.         return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
  136. }
  137.  
  138. int
  139. nl()
  140. {
  141.     rawt.c_iflag |= ICRNL;
  142.     rawt.c_oflag |= ONLCR;
  143.     norawt.c_iflag |= ICRNL;
  144.     norawt.c_oflag |= ONLCR;
  145.  
  146.     __pfast = __rawmode;
  147.     if (useraw) 
  148.         return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
  149.     else
  150.         return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
  151. }
  152.  
  153. int
  154. nonl()
  155. {
  156.     rawt.c_iflag &= ~ICRNL;
  157.     rawt.c_oflag &= ~ONLCR;
  158.     norawt.c_iflag &= ~ICRNL;
  159.     norawt.c_oflag &= ~ONLCR;
  160.  
  161.     __pfast = 1;
  162.     if (useraw) 
  163.         return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
  164.     else
  165.         return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
  166. }
  167.  
  168. int
  169. endwin()
  170. {
  171.     if (curscr) {
  172.         if (curscr->_flags & _STANDOUT) {
  173.             tputs(SE, 0, __cputchar);
  174.             curscr->_flags &= ~_STANDOUT;
  175.         }
  176.         __endwin = 1;
  177.     }
  178.  
  179.     (void)tputs(VE, 0, __cputchar);
  180.     (void)tputs(TE, 0, __cputchar);
  181.     (void)fflush(stdout);
  182.  
  183.     __echoit = origtermio.c_lflag & ECHO;
  184.     __rawmode = origtermio.c_lflag & ICANON;
  185.     __pfast = origtermio.c_iflag & ICRNL ? __rawmode : 1;
  186.     return (tcsetattr(STDIN_FILENO, TCSADRAIN, &origtermio));
  187. }
  188.  
  189. /*
  190.  * The following routines, savetty and resetty are completely useless and
  191.  * are left in only as stubs.  If people actually use them they will almost
  192.  * certainly screw up the state of the world.
  193.  */
  194. static struct termios savedtty;
  195. int
  196. savetty()
  197. {
  198.     return (tcgetattr(STDIN_FILENO, &savedtty));
  199. }
  200.  
  201. int
  202. resetty()
  203. {
  204.     return (tcsetattr(STDIN_FILENO, TCSADRAIN, &savedtty));
  205. }
  206.