home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / EDITOR / NVI179B / NVI179B.ZIP / curses / tty.c < prev    next >
C/C++ Source or Header  |  1997-06-01  |  7KB  |  286 lines

  1. /*-
  2.  * Copyright (c) 1992, 1993, 1994
  3.  *    The Regents of the University of California.  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    8.6 (Berkeley) 1/10/95";
  36. #endif /* not lint */
  37.  
  38. #include <stdlib.h>
  39. #include <termios.h>
  40. #include <unistd.h>
  41.  
  42. #include "curses.h"
  43.  
  44. /*
  45.  * In general, curses should leave tty hardware settings alone (speed, parity,
  46.  * word size).  This is most easily done in BSD by using TCSASOFT on all
  47.  * tcsetattr calls.  On other systems, it would be better to get and restore
  48.  * those attributes at each change, or at least when stopped and restarted.
  49.  * See also the comments in getterm().
  50.  */
  51. #ifdef TCSASOFT
  52. int __tcaction = 1;            /* Ignore hardware settings. */
  53. #else
  54. int __tcaction = 0;
  55. #endif
  56.  
  57. struct termios __orig_termios, __baset;
  58. static struct termios cbreakt, rawt, *curt;
  59. static int useraw;
  60.  
  61. #ifndef    OXTABS
  62. #ifdef    XTABS            /* SMI uses XTABS. */
  63. #define    OXTABS    XTABS
  64. #else
  65. #define    OXTABS    0
  66. #endif
  67. #endif
  68.  
  69. #ifndef ONLCR
  70. #define ONLCR 0
  71. #endif
  72.  
  73. /*
  74.  * gettmode --
  75.  *    Do terminal type initialization.
  76.  */
  77. int
  78. gettmode()
  79. {
  80.     useraw = 0;
  81.     
  82.     if (tcgetattr(STDIN_FILENO, &__orig_termios))
  83.         return (ERR);
  84.  
  85.     __baset = __orig_termios;
  86.     __baset.c_oflag &= ~OXTABS;
  87.  
  88.     GT = 0;        /* historical. was used before we wired OXTABS off */
  89.     NONL = (__baset.c_oflag & ONLCR) == 0;
  90.  
  91.     /*
  92.      * XXX
  93.      * System V and SMI systems overload VMIN and VTIME, such that
  94.      * VMIN is the same as the VEOF element, and VTIME is the same
  95.      * as the VEOL element.  This means that, if VEOF was ^D, the
  96.      * default VMIN is 4.  Majorly stupid.
  97.      */
  98.     cbreakt = __baset;
  99.     cbreakt.c_lflag &= ~ICANON;
  100.     cbreakt.c_cc[VMIN] = 1;
  101.     cbreakt.c_cc[VTIME] = 0;
  102.  
  103.     rawt = cbreakt;
  104.     rawt.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|INLCR|IGNCR|ICRNL|IXON);
  105.     rawt.c_oflag &= ~OPOST;
  106.     rawt.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  107.  
  108.     /*
  109.      * In general, curses should leave hardware-related settings alone.
  110.      * This includes parity and word size.  Older versions set the tty
  111.      * to 8 bits, no parity in raw(), but this is considered to be an
  112.      * artifact of the old tty interface.  If it's desired to change
  113.      * parity and word size, the TCSASOFT bit has to be removed from the
  114.      * calls that switch to/from "raw" mode.
  115.      */
  116.     if (!__tcaction) {
  117.         rawt.c_iflag &= ~ISTRIP;
  118.         rawt.c_cflag &= ~(CSIZE|PARENB);
  119.         rawt.c_cflag |= CS8;
  120.     }
  121.  
  122.     curt = &__baset;
  123.     return (tcsetattr(STDIN_FILENO, __tcaction ?
  124.         TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
  125. }
  126.  
  127. int
  128. raw()
  129. {
  130.     useraw = __pfast = __rawmode = 1;
  131.     curt = &rawt;
  132.     return (tcsetattr(STDIN_FILENO, __tcaction ?
  133.         TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
  134. }
  135.  
  136. int
  137. noraw()
  138. {
  139.     useraw = __pfast = __rawmode = 0;
  140.     curt = &__baset;
  141.     return (tcsetattr(STDIN_FILENO, __tcaction ?
  142.         TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
  143. }
  144.  
  145. int
  146. cbreak()
  147. {
  148.  
  149.     __rawmode = 1;
  150.     curt = useraw ? &rawt : &cbreakt;
  151.     return (tcsetattr(STDIN_FILENO, __tcaction ?
  152.         TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
  153. }
  154.  
  155. int
  156. nocbreak()
  157. {
  158.  
  159.     __rawmode = 0;
  160.     curt = useraw ? &rawt : &__baset;
  161.     return (tcsetattr(STDIN_FILENO, __tcaction ?
  162.         TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
  163. }
  164.     
  165. int
  166. echo()
  167. {
  168.     rawt.c_lflag |= ECHO;
  169.     cbreakt.c_lflag |= ECHO;
  170.     __baset.c_lflag |= ECHO;
  171.     
  172.     __echoit = 1;
  173.     return (tcsetattr(STDIN_FILENO, __tcaction ?
  174.         TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
  175. }
  176.  
  177. int
  178. noecho()
  179. {
  180.     rawt.c_lflag &= ~ECHO;
  181.     cbreakt.c_lflag &= ~ECHO;
  182.     __baset.c_lflag &= ~ECHO;
  183.     
  184.     __echoit = 0;
  185.     return (tcsetattr(STDIN_FILENO, __tcaction ?
  186.         TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
  187. }
  188.  
  189. int
  190. nl()
  191. {
  192.     rawt.c_iflag |= ICRNL;
  193.     rawt.c_oflag |= ONLCR;
  194.     cbreakt.c_iflag |= ICRNL;
  195.     cbreakt.c_oflag |= ONLCR;
  196.     __baset.c_iflag |= ICRNL;
  197.     __baset.c_oflag |= ONLCR;
  198.  
  199.     __pfast = __rawmode;
  200.     return (tcsetattr(STDIN_FILENO, __tcaction ?
  201.         TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
  202. }
  203.  
  204. int
  205. nonl()
  206. {
  207.     rawt.c_iflag &= ~ICRNL;
  208.     rawt.c_oflag &= ~ONLCR;
  209.     cbreakt.c_iflag &= ~ICRNL;
  210.     cbreakt.c_oflag &= ~ONLCR;
  211.     __baset.c_iflag &= ~ICRNL;
  212.     __baset.c_oflag &= ~ONLCR;
  213.  
  214.     __pfast = 1;
  215.     return (tcsetattr(STDIN_FILENO, __tcaction ?
  216.         TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
  217. }
  218.  
  219. void
  220. __startwin()
  221. {
  222.     static char *stdbuf;
  223.     static size_t len;
  224.  
  225.     (void)fflush(stdout);
  226.  
  227.     /*
  228.      * Some C libraries default to a 1K buffer when talking to a tty.
  229.      * With a larger screen, especially across a network, we'd like
  230.      * to get it to all flush in a single write.  Make it twice as big
  231.      * as just the characters (so that we have room for cursor motions
  232.      * and standout information) but no more than 8K.
  233.      */
  234.     if (stdbuf == NULL) {
  235.         if ((len = LINES * COLS * 2) > 8192)
  236.             len = 8192;
  237.         if ((stdbuf = malloc(len)) == NULL)
  238.             len = 0;
  239.     }
  240.     (void)setvbuf(stdout, stdbuf, _IOFBF, len);
  241.  
  242.     tputs(TI, 0, __cputchar);
  243.     tputs(VS, 0, __cputchar);
  244. }
  245.  
  246. int
  247. endwin()
  248. {
  249.     __restore_stophandler();
  250.  
  251.     if (curscr != NULL) {
  252.         if (curscr->flags & __WSTANDOUT) {
  253.             tputs(SE, 0, __cputchar);
  254.             curscr->flags &= ~__WSTANDOUT;
  255.         }
  256.         __mvcur(curscr->cury, curscr->curx, curscr->maxy - 1, 0, 0);
  257.     }
  258.  
  259.     (void)tputs(VE, 0, __cputchar);
  260.     (void)tputs(TE, 0, __cputchar);
  261.     (void)fflush(stdout);
  262.     (void)setvbuf(stdout, NULL, _IOLBF, 0);
  263.  
  264.     return (tcsetattr(STDIN_FILENO, __tcaction ?
  265.         TCSASOFT | TCSADRAIN : TCSADRAIN, &__orig_termios) ? ERR : OK);
  266. }
  267.  
  268. /*
  269.  * The following routines, savetty and resetty are completely useless and
  270.  * are left in only as stubs.  If people actually use them they will almost
  271.  * certainly screw up the state of the world.
  272.  */
  273. static struct termios savedtty;
  274. int
  275. savetty()
  276. {
  277.     return (tcgetattr(STDIN_FILENO, &savedtty) ? ERR : OK);
  278. }
  279.  
  280. int
  281. resetty()
  282. {
  283.     return (tcsetattr(STDIN_FILENO, __tcaction ?
  284.         TCSASOFT | TCSADRAIN : TCSADRAIN, &savedtty) ? ERR : OK);
  285. }
  286.