[njs] Shell: executing command from command line arguments.

Dmitry Volyntsev xeioex at nginx.com
Tue May 7 16:51:00 UTC 2019


details:   https://hg.nginx.org/njs/rev/1b95bc101b4c
branches:  
changeset: 956:1b95bc101b4c
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue May 07 19:50:39 2019 +0300
description:
Shell: executing command from command line arguments.

diffstat:

 njs/njs_shell.c              |  31 ++++++++++++++++++++++++++++++-
 njs/test/njs_expect_test.exp |   6 ++++++
 2 files changed, 36 insertions(+), 1 deletions(-)

diffs (94 lines):

diff -r 6adf4063e1bb -r 1b95bc101b4c njs/njs_shell.c
--- a/njs/njs_shell.c	Tue May 07 19:50:38 2019 +0300
+++ b/njs/njs_shell.c	Tue May 07 19:50:39 2019 +0300
@@ -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 ar
         "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 ar
             (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 -r 6adf4063e1bb -r 1b95bc101b4c njs/test/njs_expect_test.exp
--- a/njs/test/njs_expect_test.exp	Tue May 07 19:50:38 2019 +0300
+++ b/njs/test/njs_expect_test.exp	Tue May 07 19:50:39 2019 +0300
@@ -649,6 +649,12 @@ njs_run "-p njs/test/module ./njs/test/m
 
 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 {


More information about the nginx-devel mailing list