Skip to content

Commit 7c986ac

Browse files
committed
lua: add -i CLI option to force interactive mode
`tarantool -i SCRIPT` forces Tarantool to enter into interactive mode after executing SCRIPT or stdin. A part of #1265
1 parent b8ba547 commit 7c986ac

File tree

4 files changed

+53
-18
lines changed

4 files changed

+53
-18
lines changed

src/lua/init.c

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,24 @@ tarantool_lua_slab_cache()
465465
return &cord()->slabc;
466466
}
467467

468+
/**
469+
* Push argument and call a function on the top of Lua stack
470+
*/
471+
static void
472+
lua_main(lua_State *L, int argc, char **argv)
473+
{
474+
assert(lua_isfunction(L, -1));
475+
lua_checkstack(L, argc - 1);
476+
for (int i = 1; i < argc; i++)
477+
lua_pushstring(L, argv[i]);
478+
if (luaT_call(L, lua_gettop(L) - 1, 0) != 0) {
479+
struct error *e = diag_last_error(&fiber()->diag);
480+
panic("%s", e->errmsg);
481+
}
482+
/* clear the stack from return values. */
483+
lua_settop(L, 0);
484+
}
485+
468486
/**
469487
* Execute start-up script.
470488
*/
@@ -473,6 +491,7 @@ run_script_f(va_list ap)
473491
{
474492
struct lua_State *L = va_arg(ap, struct lua_State *);
475493
const char *path = va_arg(ap, const char *);
494+
bool interactive = va_arg(ap, int);
476495
int argc = va_arg(ap, int);
477496
char **argv = va_arg(ap, char **);
478497

@@ -487,11 +506,21 @@ run_script_f(va_list ap)
487506
/* Execute script. */
488507
if (luaL_loadfile(L, path) != 0)
489508
panic("%s", lua_tostring(L, -1));
509+
lua_main(L, argc, argv);
490510
} else if (!isatty(STDIN_FILENO)) {
491511
/* Execute stdin */
492512
if (luaL_loadfile(L, NULL) != 0)
493513
panic("%s", lua_tostring(L, -1));
514+
lua_main(L, argc, argv);
494515
} else {
516+
interactive = true;
517+
}
518+
519+
/*
520+
* Start interactive mode when it was explicitly requested
521+
* by "-i" option or stdin is TTY or there are no script.
522+
*/
523+
if (interactive) {
495524
say_crit("version %s\ntype 'help' for interactive help",
496525
tarantool_version());
497526
/* get console.start from package.loaded */
@@ -501,17 +530,9 @@ run_script_f(va_list ap)
501530
lua_remove(L, -2); /* remove package.loaded.console */
502531
lua_remove(L, -2); /* remove package.loaded */
503532
start_loop = false;
504-
}
505-
lua_checkstack(L, argc - 1);
506-
for (int i = 1; i < argc; i++)
507-
lua_pushstring(L, argv[i]);
508-
if (luaT_call(L, lua_gettop(L) - 1, 0) != 0) {
509-
struct error *e = diag_last_error(&fiber()->diag);
510-
panic("%s", e->errmsg);
533+
lua_main(L, argc, argv);
511534
}
512535

513-
/* clear the stack from return values. */
514-
lua_settop(L, 0);
515536
/*
516537
* Lua script finished. Stop the auxiliary event loop and
517538
* return control back to tarantool_lua_run_script.
@@ -521,7 +542,7 @@ run_script_f(va_list ap)
521542
}
522543

523544
void
524-
tarantool_lua_run_script(char *path, int argc, char **argv)
545+
tarantool_lua_run_script(char *path, bool interactive, int argc, char **argv)
525546
{
526547
const char *title = path ? basename(path) : "interactive";
527548
/*
@@ -534,7 +555,7 @@ tarantool_lua_run_script(char *path, int argc, char **argv)
534555
script_fiber = fiber_new(title, run_script_f);
535556
if (script_fiber == NULL)
536557
panic("%s", diag_last_error(diag_get())->errmsg);
537-
fiber_start(script_fiber, tarantool_L, path, argc, argv);
558+
fiber_start(script_fiber, tarantool_L, path, interactive, argc, argv);
538559

539560
/*
540561
* Run an auxiliary event loop to re-schedule run_script fiber.

src/lua/init.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,13 @@ tarantool_lua_tostring(struct lua_State *L, int index);
7070
/**
7171
* Load and execute start-up file
7272
*
73-
* @param L is a Lua State.
73+
* @param interactive force interactive mode
74+
* @param argc argc the number of command line arguments
75+
* @param argv argv command line arguments
7476
*/
7577
void
76-
tarantool_lua_run_script(char *path, int argc, char **argv);
78+
tarantool_lua_run_script(char *path, bool force_interactive, int argc,
79+
char **argv);
7780

7881
extern char *history;
7982

src/main.cc

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,12 +525,13 @@ print_help(const char *program)
525525
{
526526
puts("Tarantool - a Lua application server");
527527
puts("");
528-
printf("Usage: %s script.lua [OPTIONS]\n", program);
528+
printf("Usage: %s script.lua [OPTIONS] [SCRIPT [ARGS]]\n", program);
529529
puts("");
530530
puts("All command line options are passed to the interpreted script.");
531531
puts("When no script name is provided, the server responds to:");
532532
puts(" -h, --help\t\t\tdisplay this help and exit");
533533
puts(" -V, --version\t\t\tprint program version and exit");
534+
puts(" -i\t\t\t\tenter interactive mode after executing 'SCRIPT'");
534535
puts("");
535536
puts("Please visit project home page at http://tarantool.org");
536537
puts("to see online documentation, submit bugs or contribute a patch.");
@@ -547,12 +548,15 @@ main(int argc, char **argv)
547548
fprintf(stderr, "Failed to set locale to C.UTF-8\n");
548549
fpconv_check();
549550

551+
/* Enter interactive mode after executing 'script' */
552+
bool interactive = false;
553+
550554
static struct option longopts[] = {
551555
{"help", no_argument, 0, 'h'},
552556
{"version", no_argument, 0, 'V'},
553557
{NULL, 0, 0, 0},
554558
};
555-
static const char *opts = "+hV";
559+
static const char *opts = "+hVi";
556560

557561
int ch;
558562
while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
@@ -563,6 +567,10 @@ main(int argc, char **argv)
563567
case 'h':
564568
print_help(basename(argv[0]));
565569
return 0;
570+
case 'i':
571+
/* Force interactive mode */
572+
interactive = true;
573+
break;
566574
default:
567575
/* "invalid option" is printed by getopt */
568576
return EX_USAGE;
@@ -647,7 +655,8 @@ main(int argc, char **argv)
647655
* is why script must run only after the server was fully
648656
* initialized.
649657
*/
650-
tarantool_lua_run_script(script, main_argc, main_argv);
658+
tarantool_lua_run_script(script, interactive, main_argc,
659+
main_argv);
651660
/*
652661
* Start event loop after executing Lua script if signal_cb()
653662
* wasn't triggered and there is some new events. Initial value

test/box-py/args.result

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
tarantool --help
22
Tarantool - a Lua application server
33

4-
Usage: tarantool script.lua [OPTIONS]
4+
Usage: tarantool script.lua [OPTIONS] [SCRIPT [ARGS]]
55

66
All command line options are passed to the interpreted script.
77
When no script name is provided, the server responds to:
88
-h, --help display this help and exit
99
-V, --version print program version and exit
10+
-i enter interactive mode after executing 'SCRIPT'
1011

1112
Please visit project home page at http://tarantool.org
1213
to see online documentation, submit bugs or contribute a patch.
1314

1415
tarantool -h
1516
Tarantool - a Lua application server
1617

17-
Usage: tarantool script.lua [OPTIONS]
18+
Usage: tarantool script.lua [OPTIONS] [SCRIPT [ARGS]]
1819

1920
All command line options are passed to the interpreted script.
2021
When no script name is provided, the server responds to:
2122
-h, --help display this help and exit
2223
-V, --version print program version and exit
24+
-i enter interactive mode after executing 'SCRIPT'
2325

2426
Please visit project home page at http://tarantool.org
2527
to see online documentation, submit bugs or contribute a patch.

0 commit comments

Comments
 (0)