From: Dmitry Volyntsev Date: Tue, 7 May 2019 16:50:39 +0000 (+0300) Subject: Shell: executing command from command line arguments. X-Git-Tag: 0.3.2~17 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=bf30b0738145b0b65127d22619aa38145a486ba3;p=njs.git Shell: executing command from command line arguments. --- diff --git a/njs/njs_shell.c b/njs/njs_shell.c index 2e6d245b..b2f7d49c 100644 --- a/njs/njs_shell.c +++ b/njs/njs_shell.c @@ -38,6 +38,7 @@ typedef struct { uint8_t version; char *file; + char *command; size_t n_paths; char **paths; } njs_opts_t; @@ -211,8 +212,10 @@ int main(int argc, char **argv) { char path[MAXPATHLEN], *p; + njs_vm_t *vm; nxt_int_t ret; njs_opts_t opts; + nxt_str_t command; njs_vm_opt_t vm_options; nxt_memzero(&opts, sizeof(njs_opts_t)); @@ -240,7 +243,13 @@ main(int argc, char **argv) goto done; } - memcpy(path + nxt_strlen(path), "/shell", sizeof("/shell")); + if (opts.command == NULL) { + memcpy(path + nxt_strlen(path), "/shell", sizeof("/shell")); + + } else { + memcpy(path + nxt_strlen(path), "/string", sizeof("/string")); + } + opts.file = path; } @@ -260,6 +269,14 @@ main(int argc, char **argv) if (opts.interactive) { ret = njs_interactive_shell(&opts, &vm_options); + } else if (opts.command) { + vm = njs_create_vm(&opts, &vm_options); + if (vm != NULL) { + command.start = (u_char *) opts.command; + command.length = nxt_strlen(opts.command); + ret = njs_process_script(vm_options.external, &opts, &command); + } + } else { ret = njs_process_file(&opts, &vm_options); } @@ -284,6 +301,7 @@ njs_get_options(njs_opts_t *opts, int argc, char** argv) "Interactive njs shell.\n" "\n" "Options:\n" + " -c specify the command to execute.\n" " -d print disassembled code.\n" " -p set path prefix for modules.\n" " -q disable interactive introduction prompt.\n" @@ -312,6 +330,17 @@ njs_get_options(njs_opts_t *opts, int argc, char** argv) (void) write(STDIN_FILENO, help, nxt_length(help)); return ret; + case 'c': + opts->interactive = 0; + + if (++i < argc) { + opts->command = argv[i]; + break; + } + + nxt_error("option \"-c\" requires argument\n"); + return NXT_ERROR; + case 'd': opts->disassemble = 1; break; diff --git a/njs/test/njs_expect_test.exp b/njs/test/njs_expect_test.exp index 592314ee..33445df7 100644 --- a/njs/test/njs_expect_test.exp +++ b/njs/test/njs_expect_test.exp @@ -649,6 +649,12 @@ njs_run "-p njs/test/module ./njs/test/module/recursive.js" \ njs_run "-h" "Interactive njs shell.\r\n\r\nOptions:" +# command + +njs_run "-c 'console.log(\"a b c\")'" "a b c" + +njs_run "-c 'console.log('" "SyntaxError: Unexpected token \"\" in string:1" + # disassemble njs_test {