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*57472Sbostic static char sccsid[] = "@(#)tty.c 5.8 (Berkeley) 01/11/93"; 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 2157286Sbostic struct termios 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 { 3257286Sbostic useraw = 0; 3357286Sbostic 3455989Sbostic if (tcgetattr(STDIN_FILENO, &origtermio)) 35*57472Sbostic return (ERR); 3655989Sbostic 3755989Sbostic GT = (origtermio.c_oflag & OXTABS) == 0; 3856115Selan NONL = (origtermio.c_oflag & ONLCR) == 0; 3955989Sbostic 4055989Sbostic norawt = origtermio; 4155989Sbostic norawt.c_oflag &= ~OXTABS; 4255989Sbostic rawt = norawt; 4355989Sbostic cfmakeraw(&rawt); 4455989Sbostic 45*57472Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt) ? ERR : OK); 4655989Sbostic } 4755989Sbostic 4855989Sbostic int 4955989Sbostic raw() 5055989Sbostic { 5155989Sbostic useraw = __pfast = __rawmode = 1; 5255989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt)); 5355989Sbostic } 5455989Sbostic 5555989Sbostic int 5655989Sbostic noraw() 5755989Sbostic { 5855989Sbostic useraw = __pfast = __rawmode = 0; 5955989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt)); 6055989Sbostic } 6155989Sbostic 6255989Sbostic int 6355989Sbostic cbreak() 6455989Sbostic { 6555989Sbostic rawt.c_lflag &= ~ICANON; 6655989Sbostic norawt.c_lflag &= ~ICANON; 6755989Sbostic 6855989Sbostic __rawmode = 1; 6955989Sbostic if (useraw) 7055989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt)); 7155989Sbostic else 7255989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt)); 7355989Sbostic } 7455989Sbostic 7555989Sbostic int 7655989Sbostic nocbreak() 7755989Sbostic { 7855989Sbostic rawt.c_lflag |= ICANON; 7955989Sbostic norawt.c_lflag |= ICANON; 8055989Sbostic 8155989Sbostic __rawmode = 0; 8255989Sbostic if (useraw) 8355989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt)); 8455989Sbostic else 8555989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt)); 8655989Sbostic } 8755989Sbostic 8855989Sbostic int 8955989Sbostic echo() 9055989Sbostic { 9155989Sbostic rawt.c_lflag |= ECHO; 9255989Sbostic norawt.c_lflag |= ECHO; 9355989Sbostic 9455989Sbostic __echoit = 1; 9555989Sbostic if (useraw) 9655989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt)); 9755989Sbostic else 9855989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt)); 9955989Sbostic } 10055989Sbostic 10155989Sbostic int 10255989Sbostic noecho() 10355989Sbostic { 10455989Sbostic rawt.c_lflag &= ~ECHO; 10555989Sbostic norawt.c_lflag &= ~ECHO; 10655989Sbostic 10755989Sbostic __echoit = 0; 10855989Sbostic if (useraw) 10955989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt)); 11055989Sbostic else 11155989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt)); 11255989Sbostic } 11355989Sbostic 11455989Sbostic int 11555989Sbostic nl() 11655989Sbostic { 11755989Sbostic rawt.c_iflag |= ICRNL; 11855989Sbostic rawt.c_oflag |= ONLCR; 11956115Selan norawt.c_iflag |= ICRNL; 12056115Selan norawt.c_oflag |= ONLCR; 12155989Sbostic 12255989Sbostic __pfast = __rawmode; 12355989Sbostic if (useraw) 12455989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt)); 12555989Sbostic else 12655989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt)); 12755989Sbostic } 12855989Sbostic 12955989Sbostic int 13055989Sbostic nonl() 13155989Sbostic { 13255989Sbostic rawt.c_iflag &= ~ICRNL; 13355989Sbostic rawt.c_oflag &= ~ONLCR; 13456115Selan norawt.c_iflag &= ~ICRNL; 13556115Selan norawt.c_oflag &= ~ONLCR; 13655989Sbostic 13755989Sbostic __pfast = 1; 13855989Sbostic if (useraw) 13955989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt)); 14055989Sbostic else 14155989Sbostic return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt)); 14255989Sbostic } 14355989Sbostic 14455989Sbostic int 14555989Sbostic endwin() 14655989Sbostic { 14757286Sbostic if (curscr && curscr->flags & __WSTANDOUT) { 14857286Sbostic tputs(SE, 0, __cputchar); 14957286Sbostic curscr->flags &= ~__WSTANDOUT; 15055989Sbostic } 15155989Sbostic 15255989Sbostic (void)tputs(VE, 0, __cputchar); 15355989Sbostic (void)tputs(TE, 0, __cputchar); 15457355Selan mvcur(curscr->cury, curscr->cury, curscr->maxy - 1, 0); 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