-
-
Notifications
You must be signed in to change notification settings - Fork 603
/
Copy pathcli.c
459 lines (386 loc) · 12.3 KB
/
cli.c
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <signal.h>
#include <getopt.h>
#include <signal.h>
#include <gnu/libc-version.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <histedit.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <termios.h>
#define OSV_CLI_LUA "/cli-app/cli.lua"
#define OSV_CLI_LUA_PATH "/usr/share/lua/5.3/?.lua;/usr/share/lua/5.3/?/init.lua;/cli-app/lib/?.lua;/cli-app/lua/share/?.lua"
#define OSV_CLI_LUA_CPATH "/usr/lib/lua/5.3/?.so;/cli-app/lua/lib/?.so"
#define OSV_CLI_COMMANDS_PATH "/cli-app/commands"
#define HOST_CLI_LUA "cli.lua"
#define HOST_CLI_LUA_PATH "lib/?.lua;./lib/share/lua/5.3/?.lua;../lua/install/lua_modules/share/lua/5.3/?.lua;../lua/install/lua_modules/share/lua/5.3/?/init.lua"
#define HOST_CLI_LUA_CPATH "./lib/lib/lua/5.3/?.so;../lua/install/lua_modules/lib/lua/5.3/?.so"
#define HOST_CLI_COMMANDS_PATH "./commands"
#define PROMPT_MAXLEN 128
static char sprompt[PROMPT_MAXLEN];
/* Console size handling */
static int con_height = 24;
static int con_width = 80;
void cli_sigwinch_handler(int);
void cli_console_size_dirty();
/* Report console size back to Lua */
static int cli_lua_console_dim(lua_State *);
static lua_State *L;
lua_State *cli_luaL_newstate();
void cli_luaL_renewstate(lua_State**);
char *cli_prompt(EditLine*);
/* Command execution interrupt */
static int cli_interrupted = 0;
static int cli_lua_interrupted(lua_State *);
static void signal_handler(int sig);
/* Context */
#define CTXC 5
static char* ctxv[CTXC];
static struct {
char* name; char* env; struct option lopt;
} ctx[CTXC] = {
{"api", "OSV_API", {"api", required_argument, 0, 'a'}},
{"ssl_key", "OSV_SSL_KEY", {"key", required_argument, 0, 'k'}},
{"ssl_cert", "OSV_SSL_CERT", {"cert", required_argument, 0, 'c'}},
{"ssl_cacert", "OSV_SSL_CACERT", {"cacert", required_argument, 0, 'C'}},
{"ssl_verify", "OSV_SSL_VERIFY", {"verify", required_argument, 0, 'V'}}
};
/* Misc */
void print_usage();
static int is_osv() {
return strcmp("OSv", gnu_get_libc_release()) == 0;
}
int main (int argc, char* argv[]) {
if (is_osv()) {
putenv("TERM=vt100-qemu");
cli_console_size_dirty();
} else {
struct winsize sz;
ioctl(0, TIOCGWINSZ, &sz);
if (sz.ws_col > 0 && sz.ws_row > 0) {
con_width = sz.ws_col;
con_height = sz.ws_row;
signal(SIGWINCH, cli_sigwinch_handler);
}
}
int i;
/* Context from environment variables */
for (i=0; i<CTXC; i++) {
ctxv[i] = getenv(ctx[i].env);
}
/* Flags */
char *test_command = NULL;
/* Build options for getopt_long() */
int opt = 0;
static struct option long_options[2 + CTXC + 1] = {
{"test", required_argument, 0, 'T'},
{"help", no_argument, 0, 'h'}
};
for (i=0; i<CTXC; i++) {
long_options[i + 2] = ctx[i].lopt;
}
long_options[2 + CTXC] = (struct option){0};
/* Scan command line options */
int long_index = 0;
while ((opt = getopt_long(argc, argv, "+:hT:a:",
long_options, &long_index)) != -1) {
/* Check if this option belongs to ctx (context) */
for (i=0; i<CTXC; i++) {
if (ctx[i].lopt.val == opt) {
ctxv[i] = optarg;
break;
}
}
if (i<CTXC) { continue; } /* long opt found, skip rest */
/* Other options */
switch (opt) {
case 'T': test_command = optarg;
break;
case 'h':
print_usage(argv[0]);
exit(0);
break;
case '?':
default:
fprintf(stderr, "%s: unknown option `%s' is invalid\n",
argv[0], argv[optind-1]);
exit(2);
break;
}
}
/* Lua state */
L = cli_luaL_newstate();
if (L == NULL) {
exit(2);
}
if (test_command != NULL) {
if (L == NULL) {
exit(2);
}
/* Set console height and width to 24x80 */
con_height = 24;
con_width = 80;
lua_getglobal(L, "cli_command_test");
lua_pushstring(L, test_command);
int error = lua_pcall(L, 1, 0, 0);
if (error) {
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
exit(1);
}
} else if (optind < argc) {
/* If we have more arguments, the user is running a single command */
if (L != NULL) {
lua_getglobal(L, "cli_command_single");
lua_createtable(L, argc, 0);
for (i=1; i<argc; i++) {
lua_pushinteger(L, i);
lua_pushstring(L, argv[i]);
lua_settable(L, -3);
}
lua_pushinteger(L, optind);
int error = lua_pcall(L, 2, 0, 0);
if (error) {
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
}
} else {
/* Start a shell */
/* Install signal handler only in interactive */
struct sigaction act, oldact;
memset(&act, 0, sizeof(act));
act.sa_handler = &signal_handler;
sigaction(SIGINT, &act, &oldact);
/* This holds all the state for our line editor */
EditLine *el;
/* This holds the info for our history */
History *cli_history;
/* Temp variables */
int keepreading = 1;
HistEvent ev;
/* editline */
int el_count = 0;
char *el_line;
/* Initialize the EditLine state to use our prompt function and
emacs style editing. */
el = el_init(argv[0], stdin, stdout, stderr);
el_set(el, EL_PROMPT, &cli_prompt);
el_set(el, EL_SIGNAL, 1);
el_set(el, EL_EDITOR, "emacs");
/* Initialize the history */
cli_history = history_init();
if (cli_history == 0) {
fprintf(stderr, "history could not be initialized\n");
}
/* Set the size of the history */
history(cli_history, &ev, H_SETSIZE, 800);
/* This sets up the call back functions for history functionality */
el_set(el, EL_HIST, history, cli_history);
while (keepreading) {
/* el_count is the number of characters read.
line is a const char* of our command line with the tailing \n */
el_line = (char *) el_gets(el, &el_count);
/* If lua failed to load previously, retry */
if (L == NULL) {
cli_luaL_renewstate(&L);
}
/* with zero input (^D), reset the lua state */
if (L != NULL && el_count == 0) {
cli_luaL_renewstate(&L);
} else if (el_count == 1) {
/* Ignore empty line */
} else if (L != NULL && el_count > 0) {
/* Remove tailing \n */
el_line[strlen(el_line)-1] = '\0';
/* Add commands to history. Don't add empty lines */
if (strlen(el_line) > 0) {
history(cli_history, &ev, H_ENTER, el_line);
}
/* Typing reset is a special case which, like ^D,
will reset the lua state */
if (strcmp(el_line, "reset") == 0) {
cli_luaL_renewstate(&L);
} else {
/* Pass the line, as is, to cli() */
lua_getglobal(L, "cli_command");
lua_pushstring(L, el_line);
/* Reset "interrupted" state */
cli_interrupted = 0;
int error = lua_pcall(L, 1, 0, 0);
if (error) {
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
}
}
}
history_end(cli_history);
el_end(el);
}
if (L != NULL) {
lua_close(L);
}
return 0;
}
void cli_luaL_renewstate(lua_State **L) {
fprintf(stderr, "\nRestarting shell\n");
if (*L != NULL) {
lua_close(*L);
}
*L = cli_luaL_newstate();
}
void cli_lua_settable(lua_State *L, char *table, char *key, const char *value) {
lua_getglobal(L, table);
lua_pushstring(L, key);
lua_pushstring(L, value);
lua_settable(L, -3);
lua_pop(L, 1);
}
lua_State *cli_luaL_newstate() {
lua_State *L = luaL_newstate();
luaL_openlibs(L);
if (is_osv()) {
cli_lua_settable(L, "package", "path", OSV_CLI_LUA_PATH);
cli_lua_settable(L, "package", "cpath", OSV_CLI_LUA_CPATH);
} else {
cli_lua_settable(L, "package", "path", HOST_CLI_LUA_PATH);
cli_lua_settable(L, "package", "cpath", HOST_CLI_LUA_CPATH);
}
int error = luaL_loadfile(L, is_osv() ? OSV_CLI_LUA : HOST_CLI_LUA) || lua_pcall(L, 0, 0, 0);
if (error) {
fprintf(stderr, "Failed to load shell: %s\n", lua_tostring(L, -1));
lua_pop(L, 1);
lua_close(L);
return NULL;
}
cli_lua_settable(L, "context", "commands_path", is_osv() ? OSV_CLI_COMMANDS_PATH : HOST_CLI_COMMANDS_PATH);
for (int i=0; i<CTXC; i++) {
if (ctxv[i]) {
cli_lua_settable(L, "context", ctx[i].name, ctxv[i]);
}
}
/* Bind some functions into Lua */
lua_pushcfunction(L, cli_lua_console_dim);
lua_setglobal(L, "cli_console_dim");
lua_pushcfunction(L, cli_lua_interrupted);
lua_setglobal(L, "cli_interrupted");
return L;
}
char *cli_prompt(EditLine *e) {
/* Get the shell prompt from Lua */
lua_getglobal(L, "prompt");
int error = lua_pcall(L, 0, 1, 0);
if (error) {
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
} else {
if (!lua_isnil(L, -1)) {
const char *lprompt = lua_tostring(L, -1);
int len = strlen(lprompt);
snprintf(sprompt, len < PROMPT_MAXLEN ? len+1 : PROMPT_MAXLEN, "%s", lprompt);
lua_pop(L, 1);
return sprompt;
}
}
/* Default to an empty prompt in case of an error */
return (char *)"# ";
}
/* Signal handler for SIGWINCH (change in window size) */
void cli_sigwinch_handler(int sig) {
struct winsize sz;
ioctl(0, TIOCGWINSZ, &sz);
con_width = sz.ws_col;
con_height = sz.ws_row;
}
/* Uses some ANSI sequences in raw mode to find the window size */
void cli_console_size_dirty() {
/* Switch to raw and save current */
struct termios t_prev, t_raw;
tcgetattr(0, &t_prev);
cfmakeraw(&t_raw);
tcsetattr(0, TCSANOW, &t_raw);
/* Get current cursor location */
printf("\033[6n");
fflush(stdout);
struct pollfd pfd = {0, POLLIN, 0};
if (!(poll(&pfd, 1, 500))) {
goto giveup;
}
// QEMU can throw us a curveball here... If we get any response to our
// escape sequence above, we expected it to be from a real terminal emulator
// the user is running, and look - as expected - like \033[%d;%dR. But when
// QEMU's "-serial vc" is used (this is the default if serial isn't set
// explictly on QEMU's command line) - QEMU sends us an unexpected response,
// just a single escape character! So if we see an escape character, and
// nothing more after it, this is QEMU playing tricks on us, and there is no
// real terminal answering us, so give up.
if (getchar() != '\033') {
goto giveup;
}
if (!(poll(&pfd, 1, 100))) {
goto giveup;
}
int cur_h, cur_w;
int res = scanf("[%d;%dR", &cur_h, &cur_w);
if (!res) {
goto giveup;
}
/* Set cursor location to 999x999 and query it again */
printf("\033[999;999H\033[6n");
fflush(stdout);
int width, height;
res = scanf("\033[%d;%dR", &height, &width);
if (res) {
con_height = height;
con_width = width;
}
/* Return the cursor to its original location */
printf("\033[%d;%dH", cur_h, cur_w);
fflush(stdout);
giveup:
tcsetattr(0, TCSANOW, &t_prev);
}
/* Returns console size to Lua */
static int cli_lua_console_dim(lua_State *L) {
lua_pushnumber(L, con_height);
lua_pushnumber(L, con_width);
return 2;
}
/* Interrupt a running command */
static int cli_lua_interrupted(lua_State *L) {
lua_pushboolean(L, cli_interrupted);
return 1;
}
static void signal_handler(int sig) {
if (sig == SIGINT) {
cli_interrupted = 1;
}
}
void print_usage(char* program) {
printf(
"Usage: %s [OPTIONS] [COMMAND [OPTIONS]] \n\n"
"Options:\n\n"
"-a, --api=[URL] OSv API URL (default: http://127.0.0.1:8000)\n"
" environment variable: OSV_API\n"
" --key=[FILE] private key filename, if using HTTPS\n"
" environment variable: OSV_SSL_KEY\n"
" --cert=[FILE] certificate filename, if using HTTPS\n"
" environment variable: OSV_SSL_CERT\n"
" --cacert=[FILE] CA certificate filename, if using HTTPS\n"
" environment variable: OSV_SSL_CACERT\n"
" --verify=[MODE] peer certificate verification, if using HTTPS\n"
" possible modes: none, peer, fail_if_no_peer_cert,\n"
" client_once. default: peer.\n"
" environment variable: OSV_SSL_VERIFY\n"
"-T, --test=[COMMAND] run tests for a specifiec command\n"
"-h, --help print this help and exit\n\n"
"For more help on the CLI, see "
"https://github.com/cloudius-systems/osv/wiki/Command-Line-Interface-(CLI)\n",
program);
}