1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#define FOR_microcom
20#include "toys.h"
21
22GLOBALS(
23 long s;
24
25 int fd, stok;
26 struct termios old_stdin, old_fd;
27)
28
29
30static void restore_states(int i)
31{
32 if (TT.stok) tcsetattr(0, TCSAFLUSH, &TT.old_stdin);
33 tcsetattr(TT.fd, TCSAFLUSH, &TT.old_fd);
34}
35
36void microcom_main(void)
37{
38 struct termios tio;
39 struct pollfd fds[2];
40 int i;
41
42
43 TT.fd = xopen(*toys.optargs, O_RDWR | O_NOCTTY | O_NDELAY);
44 if (-1==(i = fcntl(TT.fd, F_GETFL, 0)) || fcntl(TT.fd, F_SETFL, i&~O_NDELAY)
45 || tcgetattr(TT.fd, &TT.old_fd))
46 perror_exit_raw(*toys.optargs);
47
48
49 memcpy(&tio, &TT.old_fd, sizeof(struct termios));
50 cfmakeraw(&tio);
51 xsetspeed(&tio, TT.s);
52 if (tcsetattr(TT.fd, TCSAFLUSH, &tio)) perror_exit("set speed");
53 if (!set_terminal(0, 1, 0, &TT.old_stdin)) TT.stok++;
54
55 sigatexit(restore_states);
56
57 fds[0].fd = TT.fd;
58 fds[1].fd = 0;
59 fds[0].events = fds[1].events = POLLIN;
60
61 while (poll(fds, 2, -1) > 0) {
62
63
64 if (fds[0].revents) {
65 if (0 < (i = read(TT.fd, toybuf, sizeof(toybuf)))) xwrite(0, toybuf, i);
66 else break;
67 }
68
69
70 if (fds[1].revents) {
71 if (read(0, toybuf, 1) != 1) break;
72 if (!FLAG(X)) {
73 if (!*toybuf) {
74 tcsendbreak(TT.fd, 0);
75 continue;
76 } else if (*toybuf == (']'-'@')) break;
77 }
78 xwrite(TT.fd, toybuf, 1);
79 }
80 }
81}
82