tty.c - vx32 - Local 9vx git repository for patches.
(HTM) git clone git://r-36.net/vx32
(DIR) Log
(DIR) Files
(DIR) Refs
---
tty.c (1437B)
---
1 /*
2 * Terminal support (standard input/output).
3 */
4
5 #include "u.h"
6 #include <termios.h>
7 #include <sys/termios.h>
8 #include "lib.h"
9 #include "mem.h"
10 #include "dat.h"
11 #include "fns.h"
12 #include "error.h"
13
14 static int ttyprint = 0;
15 static int ttyecho = 0;
16 static struct termios ttprevmode;
17
18 /*
19 * Normal prints and console output go to standard output.
20 */
21 void
22 uartputs(char *buf, int n)
23 {
24 if(!ttyprint)
25 return;
26 write(1, buf, n);
27 }
28
29 void
30 restoretty(void)
31 {
32 if(ttyecho && tcsetattr(0, TCSANOW, &ttprevmode) < 0){
33 ttyecho = 0;
34 panic("could not restore previous tty mode");
35 }
36 }
37
38 void
39 bye(int sig)
40 {
41 restoretty();
42 exit(0);
43 }
44
45 void
46 uartreader(void *v)
47 {
48 char buf[256];
49 int n;
50 static struct termios ttmode;
51
52 /*
53 * Try to disable host echo, save
54 * current state to restore it at exit.
55 * If successful, remember to echo
56 * what gets typed ourselves.
57 */
58 if(tcgetattr(0, &ttprevmode) < 0)
59 /*
60 * We do not panic here so that
61 * 9vx can be run without a tty
62 */
63 goto Read;
64 if(tcgetattr(0, &ttmode) >= 0){
65 ttmode.c_lflag &= ~(ECHO|ICANON);
66 if(tcsetattr(0, TCSANOW, &ttmode) >= 0)
67 ttyecho = 1;
68 }
69 signal(SIGINT, bye);
70 signal(SIGTERM, bye);
71 Read:
72 while((n = read(0, buf, sizeof buf)) > 0)
73 echo(buf, n);
74 }
75
76 void
77 uartinit(int usetty)
78 {
79 ttyprint = usetty;
80 kbdq = qopen(4*1024, 0, 0, 0);
81 if(usetty)
82 kproc("*tty*", uartreader, nil);
83 }
84
85 void
86 uartecho(char *buf, int n)
87 {
88 if(ttyecho)
89 write(1, buf, n);
90 }
91