forked from SFTtech/openage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.cpp
More file actions
184 lines (151 loc) · 3.88 KB
/
tests.cpp
File metadata and controls
184 lines (151 loc) · 3.88 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2014-2017 the openage authors. See copying.md for legal info.
#include <vector>
#include <string>
#ifdef _MSC_VER
#define STDOUT_FILENO 1
#else
#include <unistd.h>
#endif
#include "../util/fds.h"
#include "../util/pty.h"
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include "../log/log.h"
#include "../error/error.h"
#include "buf.h"
#include "console.h"
#include "draw.h"
namespace openage {
namespace console {
namespace tests {
// TODO: move to util
int max(int a, int b) {
return (a > b) ? a : b;
}
// TODO: test for console coordinates and resizing
void render() {
console::Buf buf{{80, 25}, 1337, 80};
buf.write("Hello, brave new console world!\n\n\n\n");
buf.write("stuff, lol.\n\n");
buf.write("\x1b[1mbold stuff, lol.\x1b[m\n\n");
buf.write("\x1b[5;31;1mred bold blinking stuff, lol, ... also, this text seems to be quite long, maybe even wider than the terminal width. i wonder what could \x1b[7mhappen.\x1b[m\n\n");
for (int i = 0; i < 18; i++) {
buf.write("rofl\n");
}
buf.scroll(100);
util::FD outfd{STDOUT_FILENO};
console::draw::to_terminal(&buf, &outfd, true);
}
void interactive() {
#ifndef _WIN32
console::Buf buf{{80, 25}, 1337, 80};
struct winsize ws;
ws.ws_col = buf.dims.x;
ws.ws_row = buf.dims.y;
ws.ws_xpixel = buf.dims.x * 8;
ws.ws_ypixel = buf.dims.y * 13;
int amaster;
switch (forkpty(&amaster, nullptr, nullptr, &ws)) {
case -1:
throw Error(MSG(err) << "fork() failed: " << strerror(errno));
case 0: {
// we are the child, spawn a shell
const char *shell = getenv("SHELL");
if (shell == nullptr) {
shell = "/bin/sh";
}
execl(shell, shell, nullptr);
throw Error(MSG(err) << "execl(\"" << shell << "\", \"" << shell << "\", nullptr) failed: " << strerror(errno));
}
default:
//we are the parent
break;
}
util::FD ptyout{amaster}; // for writing to the slave
util::FD ptyin{&ptyout, true}; // for reading the slave's output
util::FD termout{STDOUT_FILENO}; // for writing to the terminal
util::FD termin{STDIN_FILENO, true}; // for reading the user input
// hide cursor
termout.puts("\x1b[?25l");
// set canon input mode
termin.setinputmodecanon();
// set amaster to auto-close
ptyout.close_on_destroy = true;
constexpr int rdbuf_size = 4096;
char rdbuf[rdbuf_size];
int nfds = max(termin.fd, ptyin.fd) + 1;
bool loop = true;
console::draw::to_terminal(&buf, &termout, true);
while (loop) {
fd_set rfds;
struct timeval tv;
// Watch stdin (fd 0) to see when it has input.
FD_ZERO(&rfds);
FD_SET(ptyin.fd, &rfds);
FD_SET(termin.fd, &rfds);
tv.tv_sec = 0;
tv.tv_usec = 1000000 / 60;
switch (select(nfds, &rfds, NULL, NULL, &tv)) {
case -1:
// error
break;
case 0:
// timeout
break;
default:
// success
if (FD_ISSET(termin.fd, &rfds)) {
ssize_t retval = read(termin.fd, rdbuf, rdbuf_size);
switch (retval) {
case -1:
// error... probably EWOULDBLOCK. ignore.
break;
case 0:
// EOF on stdin... huh... well... that was unexpected... TODO
break;
default:
if (ptyout.write(rdbuf, retval) != retval) {
// for some reason, we couldn't write all input to
// a master.
loop = false;
}
break;
}
}
if (FD_ISSET(ptyin.fd, &rfds)) {
ssize_t retval = read(ptyin.fd, rdbuf, rdbuf_size);
switch (retval) {
case -1:
switch(errno) {
case EIO:
loop = false;
break;
default:
// probably EWOULDBLOCK. ignore.
break;
}
break;
case 0:
// EOF on amaster
loop = false;
break;
default:
for (int i = 0; i < retval; i++) {
buf.write(rdbuf[i]);
}
}
}
break;
}
if (tv.tv_usec == 0) {
console::draw::to_terminal(&buf, &termout, false);
tv.tv_usec = 1000000 / 60;
}
}
// show cursor
termout.puts("\x1b[?25h");
#endif /* _WIN32 */
}
}}} // namespace openage::console::tests