155989Sbostic /*- 255989Sbostic * Copyright (c) 1992 The Regents of the University of California. 355989Sbostic * All rights reserved. 455989Sbostic * 555989Sbostic * %sccs.include.redist.c% 655989Sbostic */ 755989Sbostic 855989Sbostic #ifndef lint 9*56115Selan static char sccsid[] = "@(#)tty.c 5.2 (Berkeley) 08/31/92"; 1055989Sbostic #endif /* not lint */ 1155989Sbostic 1255989Sbostic /* 1355989Sbostic * Terminal initialization routines. 1455989Sbostic */ 1555989Sbostic #include <sys/ioctl.h> 1655989Sbostic 1755989Sbostic #include <curses.h> 1855989Sbostic #include <termios.h> 1955989Sbostic #include <unistd.h> 2055989Sbostic 2155989Sbostic struct termios newtermio, origtermio; 2255989Sbostic static struct termios norawt, rawt; 2355989Sbostic static int useraw; 2455989Sbostic 2555989Sbostic /* 2655989Sbostic * gettmode -- 2755989Sbostic * Do terminal type initialization. 2855989Sbostic */ 2955989Sbostic int 3055989Sbostic gettmode() 3155989Sbostic { 3255989Sbostic if (tcgetattr(STDIN_FILENO, &origtermio)) 3355989Sbostic return (OK); 3455989Sbostic 3555989Sbostic GT = (origtermio.c_oflag & OXTABS) == 0; 36*56115Selan NONL = (origtermio.c_oflag & ONLCR) == 0; 3755989Sbostic 3855989Sbostic norawt = origtermio; 3955989Sbostic norawt.c_oflag &= ~OXTABS; 4055989Sbostic rawt = norawt; 4155989Sbostic cfmakeraw(&rawt); 4255989Sbostic 4355989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt) ? ERR : OK); 4455989Sbostic } 4555989Sbostic 4655989Sbostic int 4755989Sbostic raw() 4855989Sbostic { 4955989Sbostic useraw = __pfast = __rawmode = 1; 5055989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt)); 5155989Sbostic } 5255989Sbostic 5355989Sbostic int 5455989Sbostic noraw() 5555989Sbostic { 5655989Sbostic useraw = __pfast = __rawmode = 0; 5755989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt)); 5855989Sbostic } 5955989Sbostic 6055989Sbostic int 6155989Sbostic cbreak() 6255989Sbostic { 6355989Sbostic rawt.c_lflag &= ~ICANON; 6455989Sbostic norawt.c_lflag &= ~ICANON; 6555989Sbostic 6655989Sbostic __rawmode = 1; 6755989Sbostic if (useraw) 6855989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt)); 6955989Sbostic else 7055989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt)); 7155989Sbostic } 7255989Sbostic 7355989Sbostic int 7455989Sbostic nocbreak() 7555989Sbostic { 7655989Sbostic rawt.c_lflag |= ICANON; 7755989Sbostic norawt.c_lflag |= ICANON; 7855989Sbostic 7955989Sbostic __rawmode = 0; 8055989Sbostic if (useraw) 8155989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt)); 8255989Sbostic else 8355989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt)); 8455989Sbostic } 8555989Sbostic 8655989Sbostic int 8755989Sbostic echo() 8855989Sbostic { 8955989Sbostic rawt.c_lflag |= ECHO; 9055989Sbostic norawt.c_lflag |= ECHO; 9155989Sbostic 9255989Sbostic __echoit = 1; 9355989Sbostic if (useraw) 9455989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt)); 9555989Sbostic else 9655989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt)); 9755989Sbostic } 9855989Sbostic 9955989Sbostic int 10055989Sbostic noecho() 10155989Sbostic { 10255989Sbostic rawt.c_lflag &= ~ECHO; 10355989Sbostic norawt.c_lflag &= ~ECHO; 10455989Sbostic 10555989Sbostic __echoit = 0; 10655989Sbostic if (useraw) 10755989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt)); 10855989Sbostic else 10955989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt)); 11055989Sbostic } 11155989Sbostic 11255989Sbostic int 11355989Sbostic nl() 11455989Sbostic { 11555989Sbostic rawt.c_iflag |= ICRNL; 11655989Sbostic rawt.c_oflag |= ONLCR; 117*56115Selan norawt.c_iflag |= ICRNL; 118*56115Selan norawt.c_oflag |= ONLCR; 11955989Sbostic 12055989Sbostic __pfast = __rawmode; 12155989Sbostic if (useraw) 12255989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt)); 12355989Sbostic else 12455989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt)); 12555989Sbostic } 12655989Sbostic 12755989Sbostic int 12855989Sbostic nonl() 12955989Sbostic { 13055989Sbostic rawt.c_iflag &= ~ICRNL; 13155989Sbostic rawt.c_oflag &= ~ONLCR; 132*56115Selan norawt.c_iflag &= ~ICRNL; 133*56115Selan norawt.c_oflag &= ~ONLCR; 13455989Sbostic 13555989Sbostic __pfast = 1; 13655989Sbostic if (useraw) 13755989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt)); 13855989Sbostic else 13955989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt)); 14055989Sbostic } 14155989Sbostic 14255989Sbostic int 14355989Sbostic endwin() 14455989Sbostic { 14555989Sbostic if (curscr) { 14655989Sbostic if (curscr->_flags & _STANDOUT) { 14755989Sbostic tputs(SE, 0, __cputchar); 14855989Sbostic curscr->_flags &= ~_STANDOUT; 14955989Sbostic } 15055989Sbostic __endwin = 1; 15155989Sbostic } 15255989Sbostic 15355989Sbostic (void)tputs(VE, 0, __cputchar); 15455989Sbostic (void)tputs(TE, 0, __cputchar); 15555989Sbostic (void)fflush(stdout); 15655989Sbostic 15755989Sbostic __echoit = origtermio.c_lflag & ECHO; 15855989Sbostic __rawmode = origtermio.c_lflag & ICANON; 15955989Sbostic __pfast = origtermio.c_iflag & ICRNL ? __rawmode : 1; 16055989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &origtermio)); 16155989Sbostic } 16255989Sbostic 16355989Sbostic /* 16455989Sbostic * The following routines, savetty and resetty are completely useless and 16555989Sbostic * are left in only as stubs. If people actually use them they will almost 16655989Sbostic * certainly screw up the state of the world. 16755989Sbostic */ 16855989Sbostic static struct termios savedtty; 16955989Sbostic int 17055989Sbostic savetty() 17155989Sbostic { 17255989Sbostic return (tcgetattr(STDIN_FILENO, &savedtty)); 17355989Sbostic } 17455989Sbostic 17555989Sbostic int 17655989Sbostic resetty() 17755989Sbostic { 17855989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &savedtty)); 17955989Sbostic } 180