Home | History | Annotate | Line # | Download | only in libcurses
tty.c revision 1.7
      1  1.7    mikel /*	$NetBSD: tty.c,v 1.7 1997/07/22 07:37:11 mikel Exp $	*/
      2  1.7    mikel 
      3  1.1  mycroft /*-
      4  1.6      cgd  * Copyright (c) 1992, 1993, 1994
      5  1.2      cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.1  mycroft  *
      7  1.1  mycroft  * Redistribution and use in source and binary forms, with or without
      8  1.1  mycroft  * modification, are permitted provided that the following conditions
      9  1.1  mycroft  * are met:
     10  1.1  mycroft  * 1. Redistributions of source code must retain the above copyright
     11  1.1  mycroft  *    notice, this list of conditions and the following disclaimer.
     12  1.1  mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  mycroft  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  mycroft  *    documentation and/or other materials provided with the distribution.
     15  1.1  mycroft  * 3. All advertising materials mentioning features or use of this software
     16  1.1  mycroft  *    must display the following acknowledgement:
     17  1.1  mycroft  *	This product includes software developed by the University of
     18  1.1  mycroft  *	California, Berkeley and its contributors.
     19  1.1  mycroft  * 4. Neither the name of the University nor the names of its contributors
     20  1.1  mycroft  *    may be used to endorse or promote products derived from this software
     21  1.1  mycroft  *    without specific prior written permission.
     22  1.1  mycroft  *
     23  1.1  mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1  mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1  mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1  mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1  mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1  mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1  mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1  mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1  mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1  mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1  mycroft  * SUCH DAMAGE.
     34  1.1  mycroft  */
     35  1.1  mycroft 
     36  1.7    mikel #include <sys/cdefs.h>
     37  1.1  mycroft #ifndef lint
     38  1.7    mikel #if 0
     39  1.6      cgd static char sccsid[] = "@(#)tty.c	8.5 (Berkeley) 8/13/94";
     40  1.7    mikel #else
     41  1.7    mikel __RCSID("$NetBSD: tty.c,v 1.7 1997/07/22 07:37:11 mikel Exp $");
     42  1.7    mikel #endif
     43  1.1  mycroft #endif /* not lint */
     44  1.1  mycroft 
     45  1.6      cgd #include <stdlib.h>
     46  1.1  mycroft #include <termios.h>
     47  1.1  mycroft #include <unistd.h>
     48  1.1  mycroft 
     49  1.6      cgd #include "curses.h"
     50  1.6      cgd 
     51  1.2      cgd /*
     52  1.2      cgd  * In general, curses should leave tty hardware settings alone (speed, parity,
     53  1.2      cgd  * word size).  This is most easily done in BSD by using TCSASOFT on all
     54  1.2      cgd  * tcsetattr calls.  On other systems, it would be better to get and restore
     55  1.2      cgd  * those attributes at each change, or at least when stopped and restarted.
     56  1.2      cgd  * See also the comments in getterm().
     57  1.2      cgd  */
     58  1.2      cgd #ifdef TCSASOFT
     59  1.2      cgd int __tcaction = 1;			/* Ignore hardware settings. */
     60  1.2      cgd #else
     61  1.2      cgd int __tcaction = 0;
     62  1.2      cgd #endif
     63  1.2      cgd 
     64  1.2      cgd struct termios __orig_termios, __baset;
     65  1.2      cgd static struct termios cbreakt, rawt, *curt;
     66  1.1  mycroft static int useraw;
     67  1.1  mycroft 
     68  1.2      cgd #ifndef	OXTABS
     69  1.2      cgd #ifdef	XTABS			/* SMI uses XTABS. */
     70  1.2      cgd #define	OXTABS	XTABS
     71  1.2      cgd #else
     72  1.2      cgd #define	OXTABS	0
     73  1.2      cgd #endif
     74  1.2      cgd #endif
     75  1.2      cgd 
     76  1.1  mycroft /*
     77  1.1  mycroft  * gettmode --
     78  1.1  mycroft  *	Do terminal type initialization.
     79  1.1  mycroft  */
     80  1.1  mycroft int
     81  1.1  mycroft gettmode()
     82  1.1  mycroft {
     83  1.2      cgd 	useraw = 0;
     84  1.2      cgd 
     85  1.2      cgd 	if (tcgetattr(STDIN_FILENO, &__orig_termios))
     86  1.2      cgd 		return (ERR);
     87  1.2      cgd 
     88  1.2      cgd 	__baset = __orig_termios;
     89  1.2      cgd 	__baset.c_oflag &= ~OXTABS;
     90  1.1  mycroft 
     91  1.2      cgd 	GT = 0;		/* historical. was used before we wired OXTABS off */
     92  1.2      cgd 	NONL = (__baset.c_oflag & ONLCR) == 0;
     93  1.1  mycroft 
     94  1.2      cgd 	/*
     95  1.2      cgd 	 * XXX
     96  1.2      cgd 	 * System V and SMI systems overload VMIN and VTIME, such that
     97  1.2      cgd 	 * VMIN is the same as the VEOF element, and VTIME is the same
     98  1.2      cgd 	 * as the VEOL element.  This means that, if VEOF was ^D, the
     99  1.2      cgd 	 * default VMIN is 4.  Majorly stupid.
    100  1.2      cgd 	 */
    101  1.2      cgd 	cbreakt = __baset;
    102  1.2      cgd 	cbreakt.c_lflag &= ~ICANON;
    103  1.2      cgd 	cbreakt.c_cc[VMIN] = 1;
    104  1.2      cgd 	cbreakt.c_cc[VTIME] = 0;
    105  1.2      cgd 
    106  1.2      cgd 	rawt = cbreakt;
    107  1.2      cgd 	rawt.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|INLCR|IGNCR|ICRNL|IXON);
    108  1.2      cgd 	rawt.c_oflag &= ~OPOST;
    109  1.2      cgd 	rawt.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
    110  1.2      cgd 
    111  1.2      cgd 	/*
    112  1.2      cgd 	 * In general, curses should leave hardware-related settings alone.
    113  1.2      cgd 	 * This includes parity and word size.  Older versions set the tty
    114  1.2      cgd 	 * to 8 bits, no parity in raw(), but this is considered to be an
    115  1.2      cgd 	 * artifact of the old tty interface.  If it's desired to change
    116  1.2      cgd 	 * parity and word size, the TCSASOFT bit has to be removed from the
    117  1.2      cgd 	 * calls that switch to/from "raw" mode.
    118  1.2      cgd 	 */
    119  1.2      cgd 	if (!__tcaction) {
    120  1.2      cgd 		rawt.c_iflag &= ~ISTRIP;
    121  1.2      cgd 		rawt.c_cflag &= ~(CSIZE|PARENB);
    122  1.2      cgd 		rawt.c_cflag |= CS8;
    123  1.2      cgd 	}
    124  1.1  mycroft 
    125  1.2      cgd 	curt = &__baset;
    126  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    127  1.2      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    128  1.1  mycroft }
    129  1.1  mycroft 
    130  1.1  mycroft int
    131  1.1  mycroft raw()
    132  1.1  mycroft {
    133  1.1  mycroft 	useraw = __pfast = __rawmode = 1;
    134  1.2      cgd 	curt = &rawt;
    135  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    136  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    137  1.1  mycroft }
    138  1.1  mycroft 
    139  1.1  mycroft int
    140  1.1  mycroft noraw()
    141  1.1  mycroft {
    142  1.1  mycroft 	useraw = __pfast = __rawmode = 0;
    143  1.2      cgd 	curt = &__baset;
    144  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    145  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    146  1.1  mycroft }
    147  1.1  mycroft 
    148  1.1  mycroft int
    149  1.1  mycroft cbreak()
    150  1.1  mycroft {
    151  1.1  mycroft 
    152  1.1  mycroft 	__rawmode = 1;
    153  1.2      cgd 	curt = useraw ? &rawt : &cbreakt;
    154  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    155  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    156  1.1  mycroft }
    157  1.1  mycroft 
    158  1.1  mycroft int
    159  1.1  mycroft nocbreak()
    160  1.1  mycroft {
    161  1.1  mycroft 
    162  1.1  mycroft 	__rawmode = 0;
    163  1.2      cgd 	curt = useraw ? &rawt : &__baset;
    164  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    165  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    166  1.1  mycroft }
    167  1.1  mycroft 
    168  1.1  mycroft int
    169  1.1  mycroft echo()
    170  1.1  mycroft {
    171  1.1  mycroft 	rawt.c_lflag |= ECHO;
    172  1.2      cgd 	cbreakt.c_lflag |= ECHO;
    173  1.2      cgd 	__baset.c_lflag |= ECHO;
    174  1.1  mycroft 
    175  1.1  mycroft 	__echoit = 1;
    176  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    177  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    178  1.1  mycroft }
    179  1.1  mycroft 
    180  1.1  mycroft int
    181  1.1  mycroft noecho()
    182  1.1  mycroft {
    183  1.1  mycroft 	rawt.c_lflag &= ~ECHO;
    184  1.2      cgd 	cbreakt.c_lflag &= ~ECHO;
    185  1.2      cgd 	__baset.c_lflag &= ~ECHO;
    186  1.1  mycroft 
    187  1.1  mycroft 	__echoit = 0;
    188  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    189  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    190  1.1  mycroft }
    191  1.1  mycroft 
    192  1.1  mycroft int
    193  1.1  mycroft nl()
    194  1.1  mycroft {
    195  1.1  mycroft 	rawt.c_iflag |= ICRNL;
    196  1.1  mycroft 	rawt.c_oflag |= ONLCR;
    197  1.2      cgd 	cbreakt.c_iflag |= ICRNL;
    198  1.2      cgd 	cbreakt.c_oflag |= ONLCR;
    199  1.2      cgd 	__baset.c_iflag |= ICRNL;
    200  1.2      cgd 	__baset.c_oflag |= ONLCR;
    201  1.1  mycroft 
    202  1.1  mycroft 	__pfast = __rawmode;
    203  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    204  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    205  1.1  mycroft }
    206  1.1  mycroft 
    207  1.1  mycroft int
    208  1.1  mycroft nonl()
    209  1.1  mycroft {
    210  1.1  mycroft 	rawt.c_iflag &= ~ICRNL;
    211  1.1  mycroft 	rawt.c_oflag &= ~ONLCR;
    212  1.2      cgd 	cbreakt.c_iflag &= ~ICRNL;
    213  1.2      cgd 	cbreakt.c_oflag &= ~ONLCR;
    214  1.2      cgd 	__baset.c_iflag &= ~ICRNL;
    215  1.2      cgd 	__baset.c_oflag &= ~ONLCR;
    216  1.1  mycroft 
    217  1.1  mycroft 	__pfast = 1;
    218  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    219  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    220  1.2      cgd }
    221  1.2      cgd 
    222  1.2      cgd void
    223  1.2      cgd __startwin()
    224  1.2      cgd {
    225  1.6      cgd 	static char *stdbuf;
    226  1.6      cgd 	static size_t len;
    227  1.6      cgd 
    228  1.2      cgd 	(void)fflush(stdout);
    229  1.6      cgd 
    230  1.6      cgd 	/*
    231  1.6      cgd 	 * Some C libraries default to a 1K buffer when talking to a tty.
    232  1.6      cgd 	 * With a larger screen, especially across a network, we'd like
    233  1.6      cgd 	 * to get it to all flush in a single write.  Make it twice as big
    234  1.6      cgd 	 * as just the characters (so that we have room for cursor motions
    235  1.6      cgd 	 * and standout information) but no more than 8K.
    236  1.6      cgd 	 */
    237  1.6      cgd 	if (stdbuf == NULL) {
    238  1.6      cgd 		if ((len = LINES * COLS * 2) > 8192)
    239  1.6      cgd 			len = 8192;
    240  1.6      cgd 		if ((stdbuf = malloc(len)) == NULL)
    241  1.6      cgd 			len = 0;
    242  1.6      cgd 	}
    243  1.6      cgd 	(void)setvbuf(stdout, stdbuf, _IOFBF, len);
    244  1.2      cgd 
    245  1.2      cgd 	tputs(TI, 0, __cputchar);
    246  1.2      cgd 	tputs(VS, 0, __cputchar);
    247  1.1  mycroft }
    248  1.1  mycroft 
    249  1.1  mycroft int
    250  1.1  mycroft endwin()
    251  1.1  mycroft {
    252  1.2      cgd 	__restore_stophandler();
    253  1.2      cgd 
    254  1.2      cgd 	if (curscr != NULL) {
    255  1.2      cgd 		if (curscr->flags & __WSTANDOUT) {
    256  1.1  mycroft 			tputs(SE, 0, __cputchar);
    257  1.2      cgd 			curscr->flags &= ~__WSTANDOUT;
    258  1.1  mycroft 		}
    259  1.2      cgd 		__mvcur(curscr->cury, curscr->cury, curscr->maxy - 1, 0, 0);
    260  1.1  mycroft 	}
    261  1.1  mycroft 
    262  1.1  mycroft 	(void)tputs(VE, 0, __cputchar);
    263  1.1  mycroft 	(void)tputs(TE, 0, __cputchar);
    264  1.1  mycroft 	(void)fflush(stdout);
    265  1.2      cgd 	(void)setvbuf(stdout, NULL, _IOLBF, 0);
    266  1.1  mycroft 
    267  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    268  1.5  mycroft 	    TCSASOFT | TCSADRAIN : TCSADRAIN, &__orig_termios) ? ERR : OK);
    269  1.1  mycroft }
    270  1.1  mycroft 
    271  1.1  mycroft /*
    272  1.1  mycroft  * The following routines, savetty and resetty are completely useless and
    273  1.1  mycroft  * are left in only as stubs.  If people actually use them they will almost
    274  1.1  mycroft  * certainly screw up the state of the world.
    275  1.1  mycroft  */
    276  1.1  mycroft static struct termios savedtty;
    277  1.1  mycroft int
    278  1.1  mycroft savetty()
    279  1.1  mycroft {
    280  1.6      cgd 	return (tcgetattr(STDIN_FILENO, &savedtty) ? ERR : OK);
    281  1.1  mycroft }
    282  1.1  mycroft 
    283  1.1  mycroft int
    284  1.1  mycroft resetty()
    285  1.1  mycroft {
    286  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    287  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, &savedtty) ? ERR : OK);
    288  1.1  mycroft }
    289