[gjs] Bug 584560 – Add support for UNIX shebang



commit 4bca3f22a1fc6be6c45bc52b2f37e068058b08b3
Author: David Zeuthen <davidz redhat com>
Date:   Mon Jun 1 22:32:58 2009 -0400

    Bug 584560 â?? Add support for UNIX shebang
    
    This patch adds support for handling scripts that start with a UNIX
    shebang, for example
    
     #!/usr/bin/env gjs-console
    
     // Javascript code here
    
    See http://en.wikipedia.org/wiki/Shebang_%28Unix%29 for more
    information.
    
    Signed-off-by: David Zeuthen <davidz redhat com>
---
 gjs/context.c |   17 ++++++++++++++++-
 1 files changed, 16 insertions(+), 1 deletions(-)

diff --git a/gjs/context.c b/gjs/context.c
index ec7013a..eee03fe 100644
--- a/gjs/context.c
+++ b/gjs/context.c
@@ -653,6 +653,7 @@ gjs_context_eval(GjsContext *js_context,
                  int          *exit_status_p,
                  GError      **error)
 {
+    int line_number;
     jsval retval;
     gboolean success;
 
@@ -666,6 +667,20 @@ gjs_context_eval(GjsContext *js_context,
      */
     success = TRUE;
 
+    /* handle scripts with UNIX shebangs */
+    line_number = 1;
+    if (script != NULL && script[0] == '#' && script[1] == '!') {
+        const char *s;
+
+        s = (const char *) strstr (script, "\n");
+        if (s != NULL) {
+            if (script_len > 0)
+                script_len -= (s + 1 - script);
+            script = s + 1;
+            line_number = 2;
+        }
+    }
+
     /* log and clear exception if it's set (should not be, normally...) */
     if (gjs_log_exception(js_context->context,
                              NULL)) {
@@ -679,7 +694,7 @@ gjs_context_eval(GjsContext *js_context,
                            script,
                            script_len >= 0 ? script_len : (gssize) strlen(script),
                            filename,
-                           1, /* line number */
+                           line_number,
                            &retval)) {
         char *message;
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]