xref: /plan9-contrib/sys/src/ape/lib/v/plan9/tty.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
1 #define _POSIX_SOURCE
2 #define _RESEARCH_SOURCE
3 #include <termios.h>
4 #include <sys/types.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7 #include <libv.h>
8 
9 static int rconsfd = -1;
10 
11 /* fd is ignored */
12 
13 tty_echooff(int fd)
14 {
15 	struct termios tio;
16 
17 	if(tcgetattr(fd, &tio) < 0){
18 		/* pcons not running; use /dev/rcons */
19 		if(rconsfd >= 0)
20 			return 0;
21 		rconsfd = open("/dev/rcons", O_RDONLY);
22 		if(rconsfd < 0)
23 			return -1;
24 		return 0;
25 	}
26 	tio.c_lflag &= ~ECHO;
27 	if(tcsetattr(fd, 0, &tio) < 0)
28 		return -1;
29 	return 0;
30 }
31 
32 tty_echoon(int fd)
33 {
34 	struct termios tio;
35 
36 	if(tcgetattr(fd, &tio) < 0){
37 		if(rconsfd >= 0){
38 			close(rconsfd);
39 			rconsfd = -1;
40 			return 0;
41 		}
42 		return -1;
43 	}
44 	tio.c_lflag |= ECHO;
45 	if(tcsetattr(fd, 0, &tio) < 0)
46 		return -1;
47 	return 0;
48 }
49