Blame SOURCES/scl-utils-20130529-shebang.patch

0e5a20
0e5a20
https://bugzilla.redhat.com/show_bug.cgi?id=1372700
0e5a20
0e5a20
Backported from scl-utils-2.0.
0e5a20
0e5a20
--- scl-utils-20130529/scl.c.shebang
0e5a20
+++ scl-utils-20130529/scl.c
0e5a20
@@ -35,6 +35,74 @@
0e5a20
 #define SCL_CONF_DIR "/etc/scl/conf/"
0e5a20
 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
0e5a20
 
0e5a20
+static int count_words(const char *str, char ch)
0e5a20
+{
0e5a20
+    int count = 0;
0e5a20
+    bool inside_word = false;
0e5a20
+
0e5a20
+    while (*str != '\0') {
0e5a20
+        if (*str != ch) {
0e5a20
+            if (!inside_word) {
0e5a20
+                count++;
0e5a20
+            }
0e5a20
+            inside_word = true;
0e5a20
+        } else {
0e5a20
+            inside_word = false;
0e5a20
+        }
0e5a20
+        str++;
0e5a20
+    }
0e5a20
+
0e5a20
+    return count;
0e5a20
+}
0e5a20
+
0e5a20
+/**
0e5a20
+ * Procedure to parse shebang line and transform it to standard command
0e5a20
+ *
0e5a20
+ * In general, the shebang line has n parts at the beginnning:
0e5a20
+ * argv[0] => the name of the utility stated in shebang
0e5a20
+ * argv[1] => all the arguments as one string
0e5a20
+ * argv[2] => the script which was executed
0e5a20
+ * argv[n] => argument for executed script
0e5a20
+ *
0e5a20
+ * Considering the following shebang line in script ./test.py:
0e5a20
+ * #!/usr/bin/scl enable collectionX
0e5a20
+ * argv[0] = "/usr/bin/scl"
0e5a20
+ * argv[1] = "enable collectionX"
0e5a20
+ * argv[2] = "./test.py"
0e5a20
+ * argv[n] = "argn"
0e5a20
+ */
0e5a20
+static void parse_shebang(int argc, char *argv[],
0e5a20
+			  int *_shebang_argc, char ***_shebang_argv) {
0e5a20
+    int i = 0;
0e5a20
+    char *p;
0e5a20
+    int shebang_argc;
0e5a20
+    char **shebang_argv;
0e5a20
+
0e5a20
+    shebang_argc = count_words(argv[1], ' ') + argc - 1;
0e5a20
+    shebang_argv = (char **)malloc(sizeof(char *) * shebang_argc);
0e5a20
+    if (shebang_argv == NULL) {
0e5a20
+        fprintf(stderr, "Can't allocate memory.\n");
0e5a20
+        exit(EXIT_FAILURE);
0e5a20
+    }
0e5a20
+
0e5a20
+    shebang_argv[i++] = argv[0];
0e5a20
+
0e5a20
+    p = strtok(argv[1], " ");
0e5a20
+    while (p != NULL) {
0e5a20
+        shebang_argv[i++] = p;
0e5a20
+        p = strtok(NULL, " ");
0e5a20
+    }
0e5a20
+
0e5a20
+    while (i < shebang_argc) {
0e5a20
+        shebang_argv[i] = argv[i - (shebang_argc - argc)];
0e5a20
+        i++;
0e5a20
+    }
0e5a20
+
0e5a20
+    *_shebang_argc = shebang_argc;
0e5a20
+    *_shebang_argv = shebang_argv;
0e5a20
+}
0e5a20
+
0e5a20
+
0e5a20
 static void check_asprintf( char **strp, const char *fmt, ... ) {
0e5a20
 	va_list args;
0e5a20
 
0e5a20
@@ -648,6 +716,17 @@
0e5a20
 		}
0e5a20
 	}
0e5a20
 
0e5a20
+	if (argc >= 3 && strchr(argv[1], ' ') != NULL) {
0e5a20
+		/* Apparently a shebang line */
0e5a20
+		char **fake_argv = argv;
0e5a20
+		int fake_argc = argc;
0e5a20
+		
0e5a20
+		parse_shebang(argc, argv, &fake_argc, &fake_argv);
0e5a20
+		
0e5a20
+		argc = fake_argc;
0e5a20
+		argv = fake_argv;
0e5a20
+	}
0e5a20
+
0e5a20
 	for (i = 0; i < argc; i++) {
0e5a20
 		if (strcmp(argv[i], "--") == 0) {
0e5a20
 			break;