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.13 (Berkeley) 05/16/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 __orig_termios; 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, &__orig_termios)) 35 return (ERR); 36 37 GT = (__orig_termios.c_oflag & OXTABS) == 0; 38 NONL = (__orig_termios.c_oflag & ONLCR) == 0; 39 40 norawt = __orig_termios; 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 void 145 __startwin() 146 { 147 (void)fflush(stdout); 148 (void)setvbuf(stdout, NULL, _IOFBF, 0); 149 150 tputs(TI, 0, __cputchar); 151 tputs(VS, 0, __cputchar); 152 } 153 154 int 155 endwin() 156 { 157 if (curscr != NULL) { 158 if (curscr->flags & __WSTANDOUT) { 159 tputs(SE, 0, __cputchar); 160 curscr->flags &= ~__WSTANDOUT; 161 } 162 __mvcur(curscr->cury, curscr->cury, curscr->maxy - 1, 0, 0); 163 } 164 165 (void)tputs(VE, 0, __cputchar); 166 (void)tputs(TE, 0, __cputchar); 167 (void)fflush(stdout); 168 (void)setvbuf(stdout, NULL, _IOLBF, 0); 169 170 return (tcsetattr(STDIN_FILENO, TCSADRAIN, &__orig_termios)); 171 } 172 173 /* 174 * The following routines, savetty and resetty are completely useless and 175 * are left in only as stubs. If people actually use them they will almost 176 * certainly screw up the state of the world. 177 */ 178 static struct termios savedtty; 179 int 180 savetty() 181 { 182 return (tcgetattr(STDIN_FILENO, &savedtty)); 183 } 184 185 int 186 resetty() 187 { 188 return (tcsetattr(STDIN_FILENO, TCSADRAIN, &savedtty)); 189 } 190