Home | History | Annotate | Line # | Download | only in libcurses
tty.c revision 1.9
      1  1.9     phil /*	$NetBSD: tty.c,v 1.9 1997/11/13 00:40:28 phil 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.9     phil __RCSID("$NetBSD: tty.c,v 1.9 1997/11/13 00:40:28 phil 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.8     phil int __endwin;
     66  1.2      cgd static struct termios cbreakt, rawt, *curt;
     67  1.1  mycroft static int useraw;
     68  1.1  mycroft 
     69  1.2      cgd #ifndef	OXTABS
     70  1.2      cgd #ifdef	XTABS			/* SMI uses XTABS. */
     71  1.2      cgd #define	OXTABS	XTABS
     72  1.2      cgd #else
     73  1.2      cgd #define	OXTABS	0
     74  1.2      cgd #endif
     75  1.2      cgd #endif
     76  1.2      cgd 
     77  1.1  mycroft /*
     78  1.1  mycroft  * gettmode --
     79  1.1  mycroft  *	Do terminal type initialization.
     80  1.1  mycroft  */
     81  1.1  mycroft int
     82  1.1  mycroft gettmode()
     83  1.1  mycroft {
     84  1.2      cgd 	useraw = 0;
     85  1.2      cgd 
     86  1.2      cgd 	if (tcgetattr(STDIN_FILENO, &__orig_termios))
     87  1.2      cgd 		return (ERR);
     88  1.2      cgd 
     89  1.2      cgd 	__baset = __orig_termios;
     90  1.2      cgd 	__baset.c_oflag &= ~OXTABS;
     91  1.1  mycroft 
     92  1.2      cgd 	GT = 0;		/* historical. was used before we wired OXTABS off */
     93  1.2      cgd 	NONL = (__baset.c_oflag & ONLCR) == 0;
     94  1.1  mycroft 
     95  1.2      cgd 	/*
     96  1.2      cgd 	 * XXX
     97  1.2      cgd 	 * System V and SMI systems overload VMIN and VTIME, such that
     98  1.2      cgd 	 * VMIN is the same as the VEOF element, and VTIME is the same
     99  1.2      cgd 	 * as the VEOL element.  This means that, if VEOF was ^D, the
    100  1.2      cgd 	 * default VMIN is 4.  Majorly stupid.
    101  1.2      cgd 	 */
    102  1.2      cgd 	cbreakt = __baset;
    103  1.2      cgd 	cbreakt.c_lflag &= ~ICANON;
    104  1.2      cgd 	cbreakt.c_cc[VMIN] = 1;
    105  1.2      cgd 	cbreakt.c_cc[VTIME] = 0;
    106  1.2      cgd 
    107  1.2      cgd 	rawt = cbreakt;
    108  1.2      cgd 	rawt.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|INLCR|IGNCR|ICRNL|IXON);
    109  1.2      cgd 	rawt.c_oflag &= ~OPOST;
    110  1.2      cgd 	rawt.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
    111  1.2      cgd 
    112  1.2      cgd 	/*
    113  1.2      cgd 	 * In general, curses should leave hardware-related settings alone.
    114  1.2      cgd 	 * This includes parity and word size.  Older versions set the tty
    115  1.2      cgd 	 * to 8 bits, no parity in raw(), but this is considered to be an
    116  1.2      cgd 	 * artifact of the old tty interface.  If it's desired to change
    117  1.2      cgd 	 * parity and word size, the TCSASOFT bit has to be removed from the
    118  1.2      cgd 	 * calls that switch to/from "raw" mode.
    119  1.2      cgd 	 */
    120  1.2      cgd 	if (!__tcaction) {
    121  1.2      cgd 		rawt.c_iflag &= ~ISTRIP;
    122  1.2      cgd 		rawt.c_cflag &= ~(CSIZE|PARENB);
    123  1.2      cgd 		rawt.c_cflag |= CS8;
    124  1.2      cgd 	}
    125  1.1  mycroft 
    126  1.2      cgd 	curt = &__baset;
    127  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    128  1.2      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    129  1.1  mycroft }
    130  1.1  mycroft 
    131  1.1  mycroft int
    132  1.1  mycroft raw()
    133  1.1  mycroft {
    134  1.9     phil 	/* Check if we need to restart ... */
    135  1.9     phil 	if (__endwin) {
    136  1.9     phil 		__endwin = 0;
    137  1.9     phil 		__restartwin();
    138  1.9     phil 	}
    139  1.9     phil 
    140  1.1  mycroft 	useraw = __pfast = __rawmode = 1;
    141  1.2      cgd 	curt = &rawt;
    142  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    143  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    144  1.1  mycroft }
    145  1.1  mycroft 
    146  1.1  mycroft int
    147  1.1  mycroft noraw()
    148  1.1  mycroft {
    149  1.9     phil 	/* Check if we need to restart ... */
    150  1.9     phil 	if (__endwin) {
    151  1.9     phil 		__endwin = 0;
    152  1.9     phil 		__restartwin();
    153  1.9     phil 	}
    154  1.9     phil 
    155  1.1  mycroft 	useraw = __pfast = __rawmode = 0;
    156  1.2      cgd 	curt = &__baset;
    157  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    158  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    159  1.1  mycroft }
    160  1.1  mycroft 
    161  1.1  mycroft int
    162  1.1  mycroft cbreak()
    163  1.1  mycroft {
    164  1.9     phil 	/* Check if we need to restart ... */
    165  1.9     phil 	if (__endwin) {
    166  1.9     phil 		__endwin = 0;
    167  1.9     phil 		__restartwin();
    168  1.9     phil 	}
    169  1.9     phil 
    170  1.1  mycroft 	__rawmode = 1;
    171  1.2      cgd 	curt = useraw ? &rawt : &cbreakt;
    172  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    173  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    174  1.1  mycroft }
    175  1.1  mycroft 
    176  1.1  mycroft int
    177  1.1  mycroft nocbreak()
    178  1.1  mycroft {
    179  1.9     phil 	/* Check if we need to restart ... */
    180  1.9     phil 	if (__endwin) {
    181  1.9     phil 		__endwin = 0;
    182  1.9     phil 		__restartwin();
    183  1.9     phil 	}
    184  1.1  mycroft 
    185  1.1  mycroft 	__rawmode = 0;
    186  1.2      cgd 	curt = useraw ? &rawt : &__baset;
    187  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    188  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    189  1.1  mycroft }
    190  1.1  mycroft 
    191  1.1  mycroft int
    192  1.1  mycroft echo()
    193  1.1  mycroft {
    194  1.9     phil 	/* Check if we need to restart ... */
    195  1.9     phil 	if (__endwin) {
    196  1.9     phil 		__endwin = 0;
    197  1.9     phil 		__restartwin();
    198  1.9     phil 	}
    199  1.9     phil 
    200  1.1  mycroft 	rawt.c_lflag |= ECHO;
    201  1.2      cgd 	cbreakt.c_lflag |= ECHO;
    202  1.2      cgd 	__baset.c_lflag |= ECHO;
    203  1.1  mycroft 
    204  1.1  mycroft 	__echoit = 1;
    205  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    206  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    207  1.1  mycroft }
    208  1.1  mycroft 
    209  1.1  mycroft int
    210  1.1  mycroft noecho()
    211  1.1  mycroft {
    212  1.9     phil 	/* Check if we need to restart ... */
    213  1.9     phil 	if (__endwin) {
    214  1.9     phil 		__endwin = 0;
    215  1.9     phil 		__restartwin();
    216  1.9     phil 	}
    217  1.9     phil 
    218  1.1  mycroft 	rawt.c_lflag &= ~ECHO;
    219  1.2      cgd 	cbreakt.c_lflag &= ~ECHO;
    220  1.2      cgd 	__baset.c_lflag &= ~ECHO;
    221  1.1  mycroft 
    222  1.1  mycroft 	__echoit = 0;
    223  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    224  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    225  1.1  mycroft }
    226  1.1  mycroft 
    227  1.1  mycroft int
    228  1.1  mycroft nl()
    229  1.1  mycroft {
    230  1.9     phil 	/* Check if we need to restart ... */
    231  1.9     phil 	if (__endwin) {
    232  1.9     phil 		__endwin = 0;
    233  1.9     phil 		__restartwin();
    234  1.9     phil 	}
    235  1.9     phil 
    236  1.1  mycroft 	rawt.c_iflag |= ICRNL;
    237  1.1  mycroft 	rawt.c_oflag |= ONLCR;
    238  1.2      cgd 	cbreakt.c_iflag |= ICRNL;
    239  1.2      cgd 	cbreakt.c_oflag |= ONLCR;
    240  1.2      cgd 	__baset.c_iflag |= ICRNL;
    241  1.2      cgd 	__baset.c_oflag |= ONLCR;
    242  1.1  mycroft 
    243  1.1  mycroft 	__pfast = __rawmode;
    244  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    245  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    246  1.1  mycroft }
    247  1.1  mycroft 
    248  1.1  mycroft int
    249  1.1  mycroft nonl()
    250  1.1  mycroft {
    251  1.9     phil 	/* Check if we need to restart ... */
    252  1.9     phil 	if (__endwin) {
    253  1.9     phil 		__endwin = 0;
    254  1.9     phil 		__restartwin();
    255  1.9     phil 	}
    256  1.9     phil 
    257  1.1  mycroft 	rawt.c_iflag &= ~ICRNL;
    258  1.1  mycroft 	rawt.c_oflag &= ~ONLCR;
    259  1.2      cgd 	cbreakt.c_iflag &= ~ICRNL;
    260  1.2      cgd 	cbreakt.c_oflag &= ~ONLCR;
    261  1.2      cgd 	__baset.c_iflag &= ~ICRNL;
    262  1.2      cgd 	__baset.c_oflag &= ~ONLCR;
    263  1.1  mycroft 
    264  1.1  mycroft 	__pfast = 1;
    265  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    266  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    267  1.2      cgd }
    268  1.2      cgd 
    269  1.2      cgd void
    270  1.2      cgd __startwin()
    271  1.2      cgd {
    272  1.6      cgd 	static char *stdbuf;
    273  1.6      cgd 	static size_t len;
    274  1.6      cgd 
    275  1.2      cgd 	(void)fflush(stdout);
    276  1.6      cgd 
    277  1.6      cgd 	/*
    278  1.6      cgd 	 * Some C libraries default to a 1K buffer when talking to a tty.
    279  1.6      cgd 	 * With a larger screen, especially across a network, we'd like
    280  1.6      cgd 	 * to get it to all flush in a single write.  Make it twice as big
    281  1.6      cgd 	 * as just the characters (so that we have room for cursor motions
    282  1.6      cgd 	 * and standout information) but no more than 8K.
    283  1.6      cgd 	 */
    284  1.6      cgd 	if (stdbuf == NULL) {
    285  1.6      cgd 		if ((len = LINES * COLS * 2) > 8192)
    286  1.6      cgd 			len = 8192;
    287  1.6      cgd 		if ((stdbuf = malloc(len)) == NULL)
    288  1.6      cgd 			len = 0;
    289  1.6      cgd 	}
    290  1.6      cgd 	(void)setvbuf(stdout, stdbuf, _IOFBF, len);
    291  1.2      cgd 
    292  1.2      cgd 	tputs(TI, 0, __cputchar);
    293  1.2      cgd 	tputs(VS, 0, __cputchar);
    294  1.1  mycroft }
    295  1.1  mycroft 
    296  1.1  mycroft int
    297  1.1  mycroft endwin()
    298  1.1  mycroft {
    299  1.8     phil 	__endwin = 1;
    300  1.8     phil 	return __stopwin();
    301  1.1  mycroft }
    302  1.1  mycroft 
    303  1.1  mycroft /*
    304  1.1  mycroft  * The following routines, savetty and resetty are completely useless and
    305  1.1  mycroft  * are left in only as stubs.  If people actually use them they will almost
    306  1.1  mycroft  * certainly screw up the state of the world.
    307  1.1  mycroft  */
    308  1.1  mycroft static struct termios savedtty;
    309  1.1  mycroft int
    310  1.1  mycroft savetty()
    311  1.1  mycroft {
    312  1.6      cgd 	return (tcgetattr(STDIN_FILENO, &savedtty) ? ERR : OK);
    313  1.1  mycroft }
    314  1.1  mycroft 
    315  1.1  mycroft int
    316  1.1  mycroft resetty()
    317  1.1  mycroft {
    318  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    319  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, &savedtty) ? ERR : OK);
    320  1.1  mycroft }
    321