-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal_example.cpp
291 lines (199 loc) · 5.84 KB
/
terminal_example.cpp
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "lapel_tty.h"
#include "lapel_shell.h"
#include "lapel_config.h"
#include "my_system.h"
#include "lapel_default_history.h"
#include "lapel_command_heap.h"
#include <iostream>
#include <pty.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/select.h>
FILE *logfile;
static void ushell_at_exit(void);
static struct termios orig_termios;
static int rawmode;
static int atexit_registered;
static bool resize_pending;
extern char *optarg;
extern int optind, opterr, optopt;
void sig_handler(int signo)
{
switch(signo){
case SIGWINCH:
resize_pending = true;
break;
default:
break;
}
}
class MyTTY : public Lapel::TTY {
public:
MyTTY(int fd_in, int fd_out)
:
fd_in(fd_in),
fd_out(fd_out)
{
}
void put_char(char c)
{
(void)write(fd_out, &c, 1);;
}
bool get_char(char &c)
{
return (read(fd_in, &c, 1) == 1);
}
private:
int fd_in;
int fd_out;
};
// thanks linenoise
static int enable_raw_mode(int fd)
{
struct termios raw;
if (!isatty(STDIN_FILENO)) goto fatal;
if (!atexit_registered) {
atexit(ushell_at_exit);
atexit_registered = 1;
}
if (tcgetattr(fd,&orig_termios) == -1) goto fatal;
raw = orig_termios; /* modify the original mode */
/* input modes: no break, no CR to NL, no parity check, no strip char,
* no start/stop output control. */
raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
/* output modes - disable post processing */
raw.c_oflag &= ~(OPOST);
/* control modes - set 8 bit chars */
raw.c_cflag |= (CS8);
/* local modes - choing off, canonical off, no extended functions,
* no signal chars (^Z,^C) */
//raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
raw.c_lflag &= ~(ECHO | ICANON | IEXTEN);
/* control chars - set return condition: min number of bytes and timer.
* We want read to return every single byte, without timeout. */
raw.c_cc[VMIN] = 1; raw.c_cc[VTIME] = 0; /* 1 byte, no timer */
/* put terminal in raw mode after flushing */
if (tcsetattr(fd,TCSAFLUSH,&raw) < 0) goto fatal;
rawmode = 1;
return 0;
fatal:
errno = ENOTTY;
return -1;
}
// thanks linenoise
static void disable_raw_mode(int fd)
{
/* Don't even check the return value as it's too late. */
if (rawmode && tcsetattr(fd,TCSAFLUSH,&orig_termios) != -1)
rawmode = 0;
}
static void ushell_at_exit(void)
{
disable_raw_mode(STDIN_FILENO);
}
static bool get_size(size_t &rows, size_t &cols)
{
bool retval = false;
struct winsize ws;
if((ioctl(1, TIOCGWINSZ, &ws) == 0) && (ws.ws_col > 0)){
rows = ws.ws_row;
cols = ws.ws_col;
retval = true;
}
return retval;
}
int main(int argc, char **argv)
{
const char *username = nullptr;
uint64_t role = 0;
bool logout_permitted = false;
int opt;
while((opt = getopt(argc, argv, "u:r:lh")) != -1){
switch(opt){
case 'u': // oob_login with this user
username = optarg;
break;
case 'r': // oob_login with this role
if(sscanf(optarg, "%" PRIu64, &role) != 1){
fprintf(stderr, "-r <integer>\n");
exit(EXIT_FAILURE);
}
break;
case 'l':
logout_permitted = true;
break;
case 'h':
printf("terminal_example\n");
printf("\n");
printf("options:\n");
printf("\n");
printf("-u <string> oob username\n");
printf("-r <uint64> role for user\n");
printf("-l logout permitted\n");
printf("-h print this message\n");
exit(EXIT_SUCCESS);
break;
default:
break;
}
}
signal(SIGWINCH, sig_handler);
if(enable_raw_mode(STDIN_FILENO) == 0){
logfile = fopen("debug.log", "w");
LAPEL_DEBUG("app", "begin")
MyTTY tty(STDIN_FILENO, STDOUT_FILENO);
MySystem system;
Lapel::DefaultHistory history(120, 5);
Lapel::Shell shell(system, tty, history, 120, 20);
shell.add_user("root", "root", Lapel::ROLE_ALL);
shell.add_user("admin", "", Lapel::ROLE_ALL);
Lapel::CommandHeap commands;
shell.set_user_commands(commands);
for(unsigned i=0; i < 100; i++){
char buffer[20];
snprintf(buffer, sizeof(buffer), "dummy%u", i);
commands.add_command(buffer, 0, nullptr, nullptr);
}
commands.add_command("nothing", 0, nullptr, nullptr);
if(username != nullptr){
Lapel::User user(username, role);
shell.oob_login(user, logout_permitted);
}
{
size_t rows, cols;
if(get_size(rows, cols)){
shell.set_size(rows, cols);
}
}
shell.refresh();
fd_set readers;
FD_ZERO(&readers);
FD_SET(STDIN_FILENO, &readers);
while(true){
char c;
int ret = select(STDIN_FILENO + 1, &readers, nullptr, nullptr, nullptr);
if(resize_pending){
size_t rows, cols;
if(get_size(cols, rows)){
LAPEL_DEBUG("app", "resize: rows=%u cols=%u", (unsigned)rows, (unsigned)cols)
shell.set_size(rows, cols);
shell.refresh();
}
resize_pending = false;
}
if(ret > 0){
if(tty.get_char(c)){
shell.process(c);
}
else{
break;
}
}
}
disable_raw_mode(STDIN_FILENO);
}
exit(EXIT_SUCCESS);
}