[njs] Shell: added shebang support.

Dmitry Volyntsev xeioex at nginx.com
Thu Apr 11 17:14:37 UTC 2019


details:   https://hg.nginx.org/njs/rev/442f18a804b0
branches:  
changeset: 883:442f18a804b0
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Apr 11 20:09:42 2019 +0300
description:
Shell: added shebang support.

diffstat:

 njs/njs_shell.c |  40 ++++++++++++++++++++++++++++------------
 1 files changed, 28 insertions(+), 12 deletions(-)

diffs (95 lines):

diff -r e05e9b801355 -r 442f18a804b0 njs/njs_shell.c
--- a/njs/njs_shell.c	Thu Apr 11 20:09:41 2019 +0300
+++ b/njs/njs_shell.c	Thu Apr 11 20:09:42 2019 +0300
@@ -465,7 +465,7 @@ njs_process_file(njs_opts_t *opts, njs_v
     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_v
         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_v
         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_v
         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_v
 
 done:
 
-    if (script.start != NULL) {
-        free(script.start);
+    if (source.start != NULL) {
+        free(source.start);
     }
 
 close_fd:


More information about the nginx-devel mailing list