Home | History | Annotate | Line # | Download | only in libcurses
tty.c revision 1.8
      1  1.8     phil /*	$NetBSD: tty.c,v 1.8 1997/09/12 21:08:26 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.8     phil __RCSID("$NetBSD: tty.c,v 1.8 1997/09/12 21:08:26 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.1  mycroft 	useraw = __pfast = __rawmode = 1;
    135  1.2      cgd 	curt = &rawt;
    136  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    137  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    138  1.1  mycroft }
    139  1.1  mycroft 
    140  1.1  mycroft int
    141  1.1  mycroft noraw()
    142  1.1  mycroft {
    143  1.1  mycroft 	useraw = __pfast = __rawmode = 0;
    144  1.2      cgd 	curt = &__baset;
    145  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    146  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    147  1.1  mycroft }
    148  1.1  mycroft 
    149  1.1  mycroft int
    150  1.1  mycroft cbreak()
    151  1.1  mycroft {
    152  1.1  mycroft 
    153  1.1  mycroft 	__rawmode = 1;
    154  1.2      cgd 	curt = useraw ? &rawt : &cbreakt;
    155  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    156  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    157  1.1  mycroft }
    158  1.1  mycroft 
    159  1.1  mycroft int
    160  1.1  mycroft nocbreak()
    161  1.1  mycroft {
    162  1.1  mycroft 
    163  1.1  mycroft 	__rawmode = 0;
    164  1.2      cgd 	curt = useraw ? &rawt : &__baset;
    165  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    166  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    167  1.1  mycroft }
    168  1.1  mycroft 
    169  1.1  mycroft int
    170  1.1  mycroft echo()
    171  1.1  mycroft {
    172  1.1  mycroft 	rawt.c_lflag |= ECHO;
    173  1.2      cgd 	cbreakt.c_lflag |= ECHO;
    174  1.2      cgd 	__baset.c_lflag |= ECHO;
    175  1.1  mycroft 
    176  1.1  mycroft 	__echoit = 1;
    177  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    178  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    179  1.1  mycroft }
    180  1.1  mycroft 
    181  1.1  mycroft int
    182  1.1  mycroft noecho()
    183  1.1  mycroft {
    184  1.1  mycroft 	rawt.c_lflag &= ~ECHO;
    185  1.2      cgd 	cbreakt.c_lflag &= ~ECHO;
    186  1.2      cgd 	__baset.c_lflag &= ~ECHO;
    187  1.1  mycroft 
    188  1.1  mycroft 	__echoit = 0;
    189  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    190  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    191  1.1  mycroft }
    192  1.1  mycroft 
    193  1.1  mycroft int
    194  1.1  mycroft nl()
    195  1.1  mycroft {
    196  1.1  mycroft 	rawt.c_iflag |= ICRNL;
    197  1.1  mycroft 	rawt.c_oflag |= ONLCR;
    198  1.2      cgd 	cbreakt.c_iflag |= ICRNL;
    199  1.2      cgd 	cbreakt.c_oflag |= ONLCR;
    200  1.2      cgd 	__baset.c_iflag |= ICRNL;
    201  1.2      cgd 	__baset.c_oflag |= ONLCR;
    202  1.1  mycroft 
    203  1.1  mycroft 	__pfast = __rawmode;
    204  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    205  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    206  1.1  mycroft }
    207  1.1  mycroft 
    208  1.1  mycroft int
    209  1.1  mycroft nonl()
    210  1.1  mycroft {
    211  1.1  mycroft 	rawt.c_iflag &= ~ICRNL;
    212  1.1  mycroft 	rawt.c_oflag &= ~ONLCR;
    213  1.2      cgd 	cbreakt.c_iflag &= ~ICRNL;
    214  1.2      cgd 	cbreakt.c_oflag &= ~ONLCR;
    215  1.2      cgd 	__baset.c_iflag &= ~ICRNL;
    216  1.2      cgd 	__baset.c_oflag &= ~ONLCR;
    217  1.1  mycroft 
    218  1.1  mycroft 	__pfast = 1;
    219  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    220  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    221  1.2      cgd }
    222  1.2      cgd 
    223  1.2      cgd void
    224  1.2      cgd __startwin()
    225  1.2      cgd {
    226  1.6      cgd 	static char *stdbuf;
    227  1.6      cgd 	static size_t len;
    228  1.6      cgd 
    229  1.2      cgd 	(void)fflush(stdout);
    230  1.6      cgd 
    231  1.6      cgd 	/*
    232  1.6      cgd 	 * Some C libraries default to a 1K buffer when talking to a tty.
    233  1.6      cgd 	 * With a larger screen, especially across a network, we'd like
    234  1.6      cgd 	 * to get it to all flush in a single write.  Make it twice as big
    235  1.6      cgd 	 * as just the characters (so that we have room for cursor motions
    236  1.6      cgd 	 * and standout information) but no more than 8K.
    237  1.6      cgd 	 */
    238  1.6      cgd 	if (stdbuf == NULL) {
    239  1.6      cgd 		if ((len = LINES * COLS * 2) > 8192)
    240  1.6      cgd 			len = 8192;
    241  1.6      cgd 		if ((stdbuf = malloc(len)) == NULL)
    242  1.6      cgd 			len = 0;
    243  1.6      cgd 	}
    244  1.6      cgd 	(void)setvbuf(stdout, stdbuf, _IOFBF, len);
    245  1.2      cgd 
    246  1.2      cgd 	tputs(TI, 0, __cputchar);
    247  1.2      cgd 	tputs(VS, 0, __cputchar);
    248  1.1  mycroft }
    249  1.1  mycroft 
    250  1.1  mycroft int
    251  1.1  mycroft endwin()
    252  1.1  mycroft {
    253  1.8     phil 	__endwin = 1;
    254  1.8     phil 	return __stopwin();
    255  1.1  mycroft }
    256  1.1  mycroft 
    257  1.1  mycroft /*
    258  1.1  mycroft  * The following routines, savetty and resetty are completely useless and
    259  1.1  mycroft  * are left in only as stubs.  If people actually use them they will almost
    260  1.1  mycroft  * certainly screw up the state of the world.
    261  1.1  mycroft  */
    262  1.1  mycroft static struct termios savedtty;
    263  1.1  mycroft int
    264  1.1  mycroft savetty()
    265  1.1  mycroft {
    266  1.6      cgd 	return (tcgetattr(STDIN_FILENO, &savedtty) ? ERR : OK);
    267  1.1  mycroft }
    268  1.1  mycroft 
    269  1.1  mycroft int
    270  1.1  mycroft resetty()
    271  1.1  mycroft {
    272  1.2      cgd 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    273  1.6      cgd 	    TCSASOFT | TCSADRAIN : TCSADRAIN, &savedtty) ? ERR : OK);
    274  1.1  mycroft }
    275