1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
#include <fcntl.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include "console.h"
#include "helpers.h"
int console_init(struct console *cons) {
int master = open("/dev/ptmx", O_RDWR | O_NOCTTY);
if (master < 0) {
return -1;
}
if (grantpt(master) || unlockpt(master)) {
close(master);
return -1;
}
int flags = fcntl(master, F_GETFL, 0);
if (flags < 0) {
close(master);
return -1;
}
flags |= O_NONBLOCK;
if (fcntl(master, F_SETFL, flags) < 0) {
close(master);
return -1;
}
cons->master = master;
cons->isesc = 0;
return 0;
}
int console_deinit(struct console *cons) {
close(cons->master);
return 0;
}
void console_enter(struct console *cons, PANEL *pan) {
(void)cons;
keypad(stdscr, FALSE);
curs_set(TRUE);
raw();
top_panel(pan);
}
void console_leave(struct console *cons, PANEL *pan) {
(void)cons;
(void)pan;
keypad(stdscr, TRUE);
curs_set(FALSE);
cbreak();
}
int console_update(struct console *cons, PANEL *pan) {
char c;
int did_read = 0;
while (read(cons->master, &c, 1) > 0) {
did_read = 1;
if (!cons->isesc) {
if (c == 0x1b) {
cons->isesc = 1;
} else if (c != 0x0d) {
pprintw(pan, "%c", c);
}
} else {
if (c == 'm') {
cons->isesc = 0;
}
}
}
return did_read;
}
void console_input(struct console *cons, int ch) {
write(cons->master, &ch, 1);
}
int console_configslave(struct console *cons) {
if (setsid() < 0) {
return -1;
}
int slave = open(ptsname(cons->master), O_RDWR);
if (slave < 0) {
return -1;
}
if (ioctl(slave, TIOCSCTTY, 0) < 0) {
close(slave);
return -1;
}
dup2(slave, STDIN_FILENO);
dup2(slave, STDOUT_FILENO);
dup2(slave, STDERR_FILENO);
return slave;
}
|