Welcome! Log In Create A New Profile

Advanced

[njs] Shell: introduced scripts arguments support.

Dmitry Volyntsev
April 17, 2020 01:06PM
details: https://hg.nginx.org/njs/rev/e077780227e0
branches:
changeset: 1374:e077780227e0
user: Dmitry Volyntsev <xeioex@nginx.com>
date: Fri Apr 17 17:03:59 2020 +0000
description:
Shell: introduced scripts arguments support.

diffstat:

src/njs_shell.c | 34 +++++++++++++++++++++++++++-------
test/njs_expect_test.exp | 6 +++++-
test/script_args.js | 2 ++
3 files changed, 34 insertions(+), 8 deletions(-)

diffs (119 lines):

diff -r 3a650363913a -r e077780227e0 src/njs_shell.c
--- a/src/njs_shell.c Tue Apr 14 12:43:09 2020 +0000
+++ b/src/njs_shell.c Fri Apr 17 17:03:59 2020 +0000
@@ -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 ar
" -t script|module source code type (script is default).\n"
" -v print njs version and exit.\n"
" -u disable \"unsafe\" mode.\n"
- " <filename> | - 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 ar
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 ar

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 ar
}
}

+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 -r 3a650363913a -r e077780227e0 test/njs_expect_test.exp
--- a/test/njs_expect_test.exp Tue Apr 14 12:43:09 2020 +0000
+++ b/test/njs_expect_test.exp Fri Apr 17 17:03:59 2020 +0000
@@ -801,7 +801,7 @@ njs_run {"-c" "console.log("} "SyntaxErr
# 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)"
njs_run {"-c" "console.log(process.ppid)"} "\\d+"


+# script args
+
+njs_run {"test/script_args.js" "A" "B"} "AB"
+
# disassemble

njs_test {
diff -r 3a650363913a -r e077780227e0 test/script_args.js
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/script_args.js Fri Apr 17 17:03:59 2020 +0000
@@ -0,0 +1,2 @@
+var argv = process.argv.slice(2);
+console.log(argv[0] + argv[1])
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[njs] Shell: introduced scripts arguments support.

Dmitry Volyntsev 384 April 17, 2020 01:06PM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 150
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready