Repository navigation
Expand file tree
/
Copy pathqshell.cpp
More file actions
376 lines (323 loc) · 8.34 KB
/
Copy pathqshell.cpp
File metadata and controls
376 lines (323 loc) · 8.34 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/**
* @ Author: luoqi
* @ Create Time: 2025-04-03 16:01
* @ Modified by: luoqi
* @ Modified time: 2025-04-29 17:10
* @ Description:
*/
#ifdef _WIN32
#include <Windows.h>
#include <conio.h>
#else
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#endif
#include <cstdarg>
#include <algorithm>
#include "qshell.h"
#ifndef _WIN32
static struct termios g_org_termios;
static bool g_termios_saved = false;
#endif
void set_echo(bool enable)
{
#ifdef _WIN32
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
GetConsoleMode(hStdin, &mode);
if(enable) {
mode |= ENABLE_ECHO_INPUT;
} else {
mode &= ~ENABLE_ECHO_INPUT;
}
SetConsoleMode(hStdin, mode);
#else
if(enable) {
if(g_termios_saved) {
tcsetattr(STDIN_FILENO, TCSANOW, &g_org_termios);
g_termios_saved = false;
}
} else {
if(!g_termios_saved) {
if(tcgetattr(STDIN_FILENO, &g_org_termios) != 0) {
printf("set echo: tcgetattr failed\r\n");
return;
}
g_termios_saved = true;
}
struct termios raw = g_org_termios;
/* cfmakeraw() replicates "stty raw": it disables canonical mode,
* echo, signals, and CR->NL input translation (ICRNL). Without it
* the Enter key would arrive as LF ('\n'), which qcli treats as a
* no-op on an empty line. */
cfmakeraw(&raw);
raw.c_cc[VMIN] = 1;
raw.c_cc[VTIME] = 0;
if(tcsetattr(STDIN_FILENO, TCSANOW, &raw) != 0) {
printf("set echo: tcsetattr failed\r\n");
}
}
#endif
}
int keyboard_getch()
{
char c = 0;
#ifdef _WIN32
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
GetConsoleMode(hStdin, &mode);
SetConsoleMode(hStdin, mode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT));
DWORD result = WaitForSingleObject(hStdin, 5);
if(result == WAIT_OBJECT_0) {
if(_kbhit()) {
c = _getch();
}
}
SetConsoleMode(hStdin, mode);
#else
c = getchar();
#endif
return c;
}
QShell::QShell(QcliPrint print, GetChFunc getch)
{
this->getch = getch;
if(qcli_init(&cli, print) == QCLI_ERR_NONE) {
inited = true;
}
}
QShell::~QShell()
{
for(auto it = cmds_addr.begin(); it != cmds_addr.end(); ++it) {
QcliCmd *cmd = (QcliCmd *)(*it);
delete cmd;
}
cmds_addr.clear();
exit();
}
void QShell::init(QcliPrint print, GetChFunc getch)
{
if(inited) {
return;
}
this->getch = getch;
if(qcli_init(&cli, print) == QCLI_ERR_NONE) {
inited = true;
}
}
int QShell::start()
{
if(!inited) {
return -1;
}
thr = std::thread(&QShell::exec, this);
return 0;
}
int QShell::exit()
{
is_exit = true;
if(thr.joinable()) {
thr.join();
}
return 0;
}
int QShell::println(const char *fmt, ...)
{
if(fmt == nullptr) {
return -1;
}
va_list args1, args2;
va_start(args1, fmt);
va_copy(args2, args1);
int len = vsnprintf(nullptr, 0, fmt, args1);
va_end(args1);
if(len < 0) {
va_end(args2);
return -1;
}
std::vector<char> buf(len + 1);
int sz = vsnprintf(buf.data(), len + 1, fmt, args2);
va_end(args2);
if(sz < 0) {
cli.print(" #! QShell::println: vsnprintf failed\r\n");
return -1;
}
if(sz != len) {
cli.print(" #! QShell::println: length mismatch, expected: %d, actual: %d\r\n", len, sz);
return -1;
}
cli.print("%s\r\n", buf.data());
return 0;
}
int QShell::print(const char *fmt, ...)
{
if(fmt == nullptr) {
return -1;
}
va_list args1, args2;
va_start(args1, fmt);
va_copy(args2, args1);
int len = vsnprintf(nullptr, 0, fmt, args1);
va_end(args1);
if(len < 0) {
va_end(args2);
return -1;
}
std::vector<char> buf(len + 1);
int sz = vsnprintf(buf.data(), len + 1, fmt, args2);
va_end(args2);
if(sz < 0) {
cli.print(" #! QShell::print: vsnprintf failed\r\n");
return -1;
}
if(sz != len) {
cli.print(" #! QShell::print: length mismatch, expected: %d, actual: %d\r\n", len, sz);
return -1;
}
cli.print("%s", buf.data());
return 0;
}
int QShell::cmd_add(const char *name, QShellCmdHandler handler, const char *desc)
{
if(name == nullptr || handler == nullptr || desc == nullptr) {
return -1;
}
QcliCmd *cmd = new QcliCmd;
int ret = qcli_add(&cli, cmd, name, handler, desc);
if(ret != 0) {
delete cmd;
return ret;
}
cmds_addr.push_back((uintptr_t)cmd);
return ret;
}
int QShell::cmd_del(const char *name)
{
if(name == nullptr) {
return -1;
}
QcliCmd *cmd = qcli_find(&cli, name);
int ret = qcli_del(&cli, name);
if(ret == 0) {
for(auto it = cmds_addr.begin(); it != cmds_addr.end(); ++it) {
if(*it == (uintptr_t)cmd) {
cmds_addr.erase(it);
break;
}
}
delete cmd;
}
return ret;
}
int QShell::cmd_sub_add(const char *parent_name, const char *subcmd_name, QShellCmdHandler handler, const char *desc)
{
if(parent_name == nullptr || subcmd_name == nullptr || handler == nullptr || desc == nullptr) {
return -1;
}
// The new qcli core does not maintain a per-command subcommand list;
// subcommands are dispatched through qcli_args_trick() with a QcliTable.
// Store the registration here so args_exec/args_help can build the table
// on demand, merging it with any static table supplied by the caller.
sub_cmds[parent_name].push_back({ subcmd_name, handler, desc });
return 0;
}
int QShell::xstr(std::string str)
{
if(str.empty()) {
return -1;
}
return qcli_xstr(&cli, (char *)str.c_str());
}
void QShell::exec()
{
set_echo(false);
while(!is_exit) {
int c = 0;
if(getch != nullptr) {
c = getch();
} else {
c = keyboard_getch();
}
if(c == 0) {
continue;
}
if(c == EOF) {
// stdin closed: exit the shell cleanly instead of busy-looping
break;
}
if(c == 3) { // ctrl+c
cli.print("\33[2K");
cli.print("\033[H\033[J");
cli.print("\r\n -QSH EXIT-\r\n");
break;
}
// qcli_exec's internal state machine handles escape sequences
// (ESC / 0xe0) byte-by-byte, so no manual pre-parsing is needed.
qcli_exec(&cli, c);
}
set_echo(true);
if(on_exit) {
on_exit();
}
}
int QShell::execc(char c)
{
qcli_exec(&cli, c);
return 0;
}
int QShell::args_help(const char *parent, ArgsTable *table, size_t sz)
{
if(table == nullptr) {
return -1;
}
// Merge the caller's static table with subcommands registered via
// cmd_sub_add() for this parent.
std::vector<QcliTable> merged;
size_t static_n = sz / sizeof(ArgsTable);
for(size_t i = 0; i < static_n; i++) {
merged.push_back(table[i]);
}
auto it = sub_cmds.find(parent ? parent : "");
if(it != sub_cmds.end()) {
for(auto &sc : it->second) {
merged.push_back({ sc.name.c_str(), sc.cb, sc.desc.c_str() });
}
}
size_t l = 0;
for(auto &e : merged) {
l = std::max(l, strlen(e.name));
}
for(auto &e : merged) {
cli.print(" %-*s %s\r\n", (int)l, e.name, e.desc);
}
return 0;
}
int QShell::args_exec(int argc, char **argv, const ArgsTable *table, size_t table_size)
{
// Merge the static table with subcommands registered via cmd_sub_add()
// for the parent command (argv[0]).
std::vector<QcliTable> merged;
size_t static_n = table_size / sizeof(ArgsTable);
for(size_t i = 0; i < static_n; i++) {
merged.push_back(table[i]);
}
if(argc > 0 && argv != nullptr && argv[0] != nullptr) {
auto it = sub_cmds.find(argv[0]);
if(it != sub_cmds.end()) {
for(auto &sc : it->second) {
merged.push_back({ sc.name.c_str(), sc.cb, sc.desc.c_str() });
}
}
}
return qcli_args_trick(argc, argv, merged.data(), merged.size() * sizeof(QcliTable));
}
void QShell::title(void)
{
qcli_title(&cli);
}
void QShell::exit_hook_set(Hook hook)
{
on_exit = hook;
}