]> git.kaiwu.me - njs.git/commitdiff
Shell: added options for custom exit failure code.
authorDmitry Volyntsev <xeioex@nginx.com>
Wed, 26 Jan 2022 17:24:57 +0000 (17:24 +0000)
committerDmitry Volyntsev <xeioex@nginx.com>
Wed, 26 Jan 2022 17:24:57 +0000 (17:24 +0000)
src/njs_shell.c

index 8fb3cf140e2798799ac375752189e0478d2f874b..257591de38ebd91d851a810af082559b12417ede 100644 (file)
@@ -36,6 +36,7 @@ typedef struct {
     uint8_t                 version;
     uint8_t                 ast;
     uint8_t                 unhandled_rejection;
+    int                     exit_code;
 
     char                    *file;
     char                    *command;
@@ -319,7 +320,7 @@ done:
 
     njs_options_free(&opts);
 
-    return (ret == NJS_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
+    return (ret == NJS_OK) ? EXIT_SUCCESS : opts.exit_code;
 }
 
 
@@ -344,6 +345,7 @@ njs_options_parse(njs_opts_t *opts, int argc, char **argv)
         "  -a                print AST.\n"
         "  -c                specify the command to execute.\n"
         "  -d                print disassembled code.\n"
+        "  -e                set failure exit code.\n"
         "  -f                disabled denormals mode.\n"
         "  -p                set path prefix for modules.\n"
         "  -q                disable interactive introduction prompt.\n"
@@ -357,8 +359,14 @@ njs_options_parse(njs_opts_t *opts, int argc, char **argv)
     ret = NJS_DONE;
 
     opts->denormals = 1;
+    opts->exit_code = EXIT_FAILURE;
     opts->unhandled_rejection = NJS_VM_OPT_UNHANDLED_REJECTION_THROW;
 
+    p = getenv("NJS_EXIT_CODE");
+    if (p != NULL) {
+        opts->exit_code = atoi(p);
+    }
+
     for (i = 1; i < argc; i++) {
 
         p = argv[i];
@@ -396,6 +404,15 @@ njs_options_parse(njs_opts_t *opts, int argc, char **argv)
             opts->disassemble = 1;
             break;
 
+        case 'e':
+            if (++i < argc) {
+                opts->exit_code = atoi(argv[i]);
+                break;
+            }
+
+            njs_stderror("option \"-e\" requires argument\n");
+            return NJS_ERROR;
+
         case 'f':
 
 #if !(NJS_HAVE_DENORMALS_CONTROL)