1 /*- 2 * Copyright (c) 1992 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)tty.c 5.8 (Berkeley) 01/11/93"; 10 #endif /* not lint */ 11 12 /* 13 * Terminal initialization routines. 14 */ 15 #include <sys/ioctl.h> 16 17 #include <curses.h> 18 #include <termios.h> 19 #include <unistd.h> 20 21 struct termios origtermio; 22 static struct termios norawt, rawt; 23 static int useraw; 24 25 /* 26 * gettmode -- 27 * Do terminal type initialization. 28 */ 29 int 30 gettmode() 31 { 32 useraw = 0; 33 34 if (tcgetattr(STDIN_FILENO, &origtermio)) 35 return (ERR); 36 37 GT = (origtermio.c_oflag & OXTABS) == 0; 38 NONL = (origtermio.c_oflag & ONLCR) == 0; 39 40 norawt = origtermio; 41 norawt.c_oflag &= ~OXTABS; 42 rawt = norawt; 43 cfmakeraw(&rawt); 44 45 return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt) ? ERR : OK); 46 } 47 48 int 49 raw() 50 { 51 useraw = __pfast = __rawmode = 1; 52 return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt)); 53 } 54 55 int 56 noraw() 57 { 58 useraw = __pfast = __rawmode = 0; 59 return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt)); 60 } 61 62 int 63 cbreak() 64 { 65 rawt.c_lflag &= ~ICANON; 66 norawt.c_lflag &= ~ICANON; 67 68 __rawmode = 1; 69 if (useraw) 70 return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt)); 71 else 72 return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt)); 73 } 74 75 int 76 nocbreak() 77 { 78 rawt.c_lflag |= ICANON; 79 norawt.c_lflag |= ICANON; 80 81 __rawmode = 0; 82 if (useraw) 83 return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt)); 84 else 85 return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt)); 86 } 87 88 int 89 echo() 90 { 91 rawt.c_lflag |= ECHO; 92 norawt.c_lflag |= ECHO; 93 94 __echoit = 1; 95 if (useraw) 96 return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt)); 97 else 98 return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt)); 99 } 100 101 int 102 noecho() 103 { 104 rawt.c_lflag &= ~ECHO; 105 norawt.c_lflag &= ~ECHO; 106 107 __echoit = 0; 108 if (useraw) 109 return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt)); 110 else 111 return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt)); 112 } 113 114 int 115 nl() 116 { 117 rawt.c_iflag |= ICRNL; 118 rawt.c_oflag |= ONLCR; 119 norawt.c_iflag |= ICRNL; 120 norawt.c_oflag |= ONLCR; 121 122 __pfast = __rawmode; 123 if (useraw) 124 return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt)); 125 else 126 return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt)); 127 } 128 129 int 130 nonl() 131 { 132 rawt.c_iflag &= ~ICRNL; 133 rawt.c_oflag &= ~ONLCR; 134 norawt.c_iflag &= ~ICRNL; 135 norawt.c_oflag &= ~ONLCR; 136 137 __pfast = 1; 138 if (useraw) 139 return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt)); 140 else 141 return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt)); 142 } 143 144 int 145 endwin() 146 { 147 if (curscr && curscr->flags & __WSTANDOUT) { 148 tputs(SE, 0, __cputchar); 149 curscr->flags &= ~__WSTANDOUT; 150 } 151 152 (void)tputs(VE, 0, __cputchar); 153 (void)tputs(TE, 0, __cputchar); 154 mvcur(curscr->cury, curscr->cury, curscr->maxy - 1, 0); 155 (void)fflush(stdout); 156 157 __echoit = origtermio.c_lflag & ECHO; 158 __rawmode = origtermio.c_lflag & ICANON; 159 __pfast = origtermio.c_iflag & ICRNL ? __rawmode : 1; 160 return (tcsetattr(STDIN_FILENO, TCSADRAIN, &origtermio)); 161 } 162 163 /* 164 * The following routines, savetty and resetty are completely useless and 165 * are left in only as stubs. If people actually use them they will almost 166 * certainly screw up the state of the world. 167 */ 168 static struct termios savedtty; 169 int 170 savetty() 171 { 172 return (tcgetattr(STDIN_FILENO, &savedtty)); 173 } 174 175 int 176 resetty() 177 { 178 return (tcsetattr(STDIN_FILENO, TCSADRAIN, &savedtty)); 179 } 180