]> git.kaiwu.me - njs.git/commitdiff
Shell: executing command from command line arguments.
authorDmitry Volyntsev <xeioex@nginx.com>
Tue, 7 May 2019 16:50:39 +0000 (19:50 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Tue, 7 May 2019 16:50:39 +0000 (19:50 +0300)
njs/njs_shell.c
njs/test/njs_expect_test.exp

index 2e6d245bb0a4fe0faabba5dc0bb91ec4c8e368c2..b2f7d49cfec99813f186d90258dc6f4d8b01f07c 100644 (file)
@@ -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;
index 592314ee7445a2a3ee07d7a7521e4b8071b91598..33445df73a4d8b54bcd2b33e2566a1a02fd4d29b 100644 (file)
@@ -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 {