From: Dmitry Volyntsev Date: Fri, 17 Apr 2020 17:03:59 +0000 (+0000) Subject: Shell: introduced scripts arguments support. X-Git-Tag: 0.4.0~6 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=0e83dbda34c8c7aed2c559aefaeb7acd55f09497;p=njs.git Shell: introduced scripts arguments support. --- diff --git a/src/njs_shell.c b/src/njs_shell.c index 7c521bda..4e306137 100644 --- a/src/njs_shell.c +++ b/src/njs_shell.c @@ -39,6 +39,8 @@ typedef struct { char *command; size_t n_paths; char **paths; + char **argv; + njs_uint_t argc; } njs_opts_t; @@ -264,8 +266,8 @@ main(int argc, char **argv) vm_options.ops = &njs_console_ops; vm_options.external = &njs_console; - vm_options.argv = argv; - vm_options.argc = argc; + vm_options.argv = opts.argv; + vm_options.argc = opts.argc; if (opts.interactive) { ret = njs_interactive_shell(&opts, &vm_options); @@ -295,12 +297,15 @@ done: static njs_int_t njs_get_options(njs_opts_t *opts, int argc, char **argv) { - char *p, **paths; - njs_int_t i, ret; + char *p, **paths; + njs_int_t i, ret; + njs_uint_t n; static const char help[] = "Interactive njs shell.\n" "\n" + "njs [options] [-c string | script.js | -] [script args]" + "\n" "Options:\n" " -c specify the command to execute.\n" " -d print disassembled code.\n" @@ -311,7 +316,7 @@ njs_get_options(njs_opts_t *opts, int argc, char **argv) " -t script|module source code type (script is default).\n" " -v print njs version and exit.\n" " -u disable \"unsafe\" mode.\n" - " | - run code from a file or stdin.\n"; + " script.js | - run code from a file or stdin.\n"; ret = NJS_DONE; @@ -324,7 +329,7 @@ njs_get_options(njs_opts_t *opts, int argc, char **argv) if (p[0] != '-' || (p[0] == '-' && p[1] == '\0')) { opts->interactive = 0; opts->file = argv[i]; - continue; + goto done; } p++; @@ -340,7 +345,7 @@ njs_get_options(njs_opts_t *opts, int argc, char **argv) if (++i < argc) { opts->command = argv[i]; - break; + goto done; } njs_stderror("option \"-c\" requires argument\n"); @@ -418,6 +423,21 @@ njs_get_options(njs_opts_t *opts, int argc, char **argv) } } +done: + + opts->argc = njs_max(argc - i + 1, 2); + opts->argv = malloc(sizeof(char*) * opts->argc); + if (opts->argv == NULL) { + njs_stderror("failed to alloc argv\n"); + return NJS_ERROR; + } + + opts->argv[0] = argv[0]; + opts->argv[1] = (opts->file != NULL) ? opts->file : (char *) ""; + for (n = 2; n < opts->argc; n++) { + opts->argv[n] = argv[i + n - 1]; + } + return NJS_OK; } diff --git a/test/njs_expect_test.exp b/test/njs_expect_test.exp index 5905cf30..99131ab9 100644 --- a/test/njs_expect_test.exp +++ b/test/njs_expect_test.exp @@ -801,7 +801,7 @@ njs_run {"-c" "console.log("} "SyntaxError: Unexpected end of input in string:1" # process njs_run {"-c" "console.log(typeof process.argv)"} "object" -njs_run {"-c" "console.log(process.argv[3])" "AAA"} "AAA" +njs_run {"-c" "console.log(process.argv.slice(2))" "AAA"} "AAA" njs_run {"-c" "console.log(typeof process.env)"} "object" njs_run {"-c" "console.log(process.env.HOME != undefined)"} "true" @@ -812,6 +812,10 @@ njs_run {"-c" "console.log(process.pid)"} "\\d+" njs_run {"-c" "console.log(process.ppid)"} "\\d+" +# script args + +njs_run {"test/script_args.js" "A" "B"} "AB" + # disassemble njs_test { diff --git a/test/script_args.js b/test/script_args.js new file mode 100644 index 00000000..3233a28a --- /dev/null +++ b/test/script_args.js @@ -0,0 +1,2 @@ +var argv = process.argv.slice(2); +console.log(argv[0] + argv[1])