From 140a738f31f63ab4dec8ecb264e35b0dc9998ddd Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Thu, 11 Apr 2019 20:09:42 +0300 Subject: [PATCH] Shell: added shebang support. --- njs/njs_shell.c | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/njs/njs_shell.c b/njs/njs_shell.c index 0761084e..583473ec 100644 --- a/njs/njs_shell.c +++ b/njs/njs_shell.c @@ -465,7 +465,7 @@ njs_process_file(njs_opts_t *opts, njs_vm_opt_t *vm_options) ssize_t n; njs_vm_t *vm; nxt_int_t ret; - nxt_str_t script; + nxt_str_t source, script; struct stat sb; file = opts->file; @@ -495,15 +495,15 @@ njs_process_file(njs_opts_t *opts, njs_vm_opt_t *vm_options) size = sb.st_size; } - script.length = 0; - script.start = realloc(NULL, size); - if (script.start == NULL) { + source.length = 0; + source.start = realloc(NULL, size); + if (source.start == NULL) { nxt_error("alloc failed while reading '%s'\n", file); ret = NXT_ERROR; goto done; } - p = script.start; + p = source.start; end = p + size; for ( ;; ) { @@ -523,23 +523,23 @@ njs_process_file(njs_opts_t *opts, njs_vm_opt_t *vm_options) if (p + n > end) { size *= 2; - start = realloc(script.start, size); + start = realloc(source.start, size); if (start == NULL) { nxt_error("alloc failed while reading '%s'\n", file); ret = NXT_ERROR; goto done; } - script.start = start; + source.start = start; - p = script.start + script.length; - end = script.start + size; + p = source.start + source.length; + end = source.start + size; } memcpy(p, buf, n); p += n; - script.length += n; + source.length += n; } vm = njs_create_vm(opts, vm_options); @@ -548,6 +548,22 @@ njs_process_file(njs_opts_t *opts, njs_vm_opt_t *vm_options) goto done; } + script = source; + + /* shebang */ + + if (script.length > 2 && memcmp(script.start, "#!", 2) == 0) { + p = nxt_strlchr(script.start, script.start + script.length, '\n'); + + if (p != NULL) { + script.length -= (p + 1 - script.start); + script.start = p + 1; + + } else { + script.length = 0; + } + } + ret = njs_process_script(vm_options->external, opts, &script); if (ret != NXT_OK) { ret = NXT_ERROR; @@ -558,8 +574,8 @@ njs_process_file(njs_opts_t *opts, njs_vm_opt_t *vm_options) done: - if (script.start != NULL) { - free(script.start); + if (source.start != NULL) { + free(source.start); } close_fd: -- 2.47.3