From 4a9812128c030efedfce7a5ffddb88535833b80b Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Wed, 26 Jan 2022 17:24:57 +0000 Subject: [PATCH] Shell: added options for custom exit failure code. --- src/njs_shell.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/njs_shell.c b/src/njs_shell.c index 8fb3cf14..257591de 100644 --- a/src/njs_shell.c +++ b/src/njs_shell.c @@ -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) -- 2.47.3