Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--as-args option #59

Closed
philshafer opened this issue Nov 16, 2024 · 1 comment
Closed

--as-args option #59

philshafer opened this issue Nov 16, 2024 · 1 comment
Assignees

Comments

@philshafer
Copy link
Contributor

Parameters are good, but sometimes a script wants just straight arguments, so "--as-args" turns any remaining arguments into values in a global parameter named "$ARGV".

Need to think of a way for this to work with "-n", since #!/usr/bin/env slaxproc -n --as-args won't work. Maybe take a number of arguments before starting ARGV?

Thanks,
Phil

@philshafer philshafer self-assigned this Nov 16, 2024
@philshafer
Copy link
Contributor Author

I'm pitching this idea for now, since there's no way to build elements as arguments, only strings. I did it with strings and, well, it stinks. Here's the patch, just for history.....

Thanks,
Phil

diff --git a/slaxproc/slaxproc.c b/slaxproc/slaxproc.c
index 9a7685c..a730e67 100644
--- a/slaxproc/slaxproc.c
+++ b/slaxproc/slaxproc.c
@@ -56,6 +56,7 @@ static char *opt_xpath;		/* XPath expresion to match on */
 
 static slaxDebugFlags_t opt_debugger;	/* Invoke the debugger */
 
+static int opt_as_args;		/* Turn arguments into a global variable  */
 static int opt_dump_tree;	/* Dump parsed element tree */
 static int opt_empty_input;	/* Use an empty input file */
 static int opt_html;		/* Parse input as HTML */
@@ -80,6 +81,7 @@ static struct opts {
     int o_do_xml_to_json;
     int o_do_xml_to_yaml;
 
+    int o_as_args;
     int o_dump_tree;
     int o_encoding;
     int o_expression;
@@ -168,6 +170,7 @@ static struct option long_opts[] = {
     { "xml-to-yaml", no_argument, &opts.o_do_xml_to_yaml, 1 },
     { "xslt-to-slax", no_argument, NULL, 's' },
 
+    { "as-args", no_argument, &opts.o_as_args, 1 },
     { "debug", no_argument, NULL, 'd' },
     { "dump-tree", no_argument, &opts.o_dump_tree, 1 },
     { "empty", no_argument, NULL, 'E' },
@@ -1277,6 +1280,9 @@ main (int argc UNUSED, char **argv)
 
 /* Non-mode flags start here */
 
+	    } else if (opts.o_as_args) {
+		opt_as_args = TRUE;
+
 	    } else if (opts.o_dump_tree) {
 		opt_dump_tree = TRUE;
 
@@ -1358,6 +1364,36 @@ main (int argc UNUSED, char **argv)
     if (cp)
 	slaxIncludeAddPath(cp);
 
+    /* Do we ignore any remainging arguments? */
+    if (opt_ignore_arguments) {
+	static char *null_argv[] = { NULL };
+	argv = null_argv;
+    }
+
+    /* Convert remaining arguments into generic arguments */
+    if (opt_as_args) {
+	char pname[32];
+	int pnumber = 1;
+
+	for (; *argv; argv++) {
+
+	    snprintf(pname, sizeof(pname), "slax-arg-%d", pnumber++);
+
+	    char *pvalue = *argv;
+	    int plen = strlen(pvalue);
+	    char quote = strrchr(pvalue, '\"') ? '\'' : '\"';
+	    char *tvalue = alloca(plen + 3);
+	    tvalue[0] = quote;
+	    memcpy(tvalue + 1, pvalue, plen);
+	    tvalue[plen + 1] = quote;
+	    tvalue[plen + 2] = '\0';
+
+	    nbparams += 1;
+	    slaxDataListAddNul(&plist, pname);
+	    slaxDataListAddNul(&plist, tvalue);
+	}
+    }
+
     params = alloca((nbparams * 2 + 1) * sizeof(*params));
     i = 0;
     SLAXDATALIST_FOREACH(dnp, &plist) {
@@ -1424,11 +1460,6 @@ main (int argc UNUSED, char **argv)
 	slaxTraceToFile(trace_fp);
     }
 
-    if (opt_ignore_arguments) {
-	static char *null_argv[] = { NULL };
-	argv = null_argv;
-    }
-
     if (!TAILQ_EMPTY(&mini_templates))
 	build_mini_template();
 
diff --git a/tests/core/test-empty-40.slax b/tests/core/test-empty-40.slax
index 9897e06..648477e 100644
--- a/tests/core/test-empty-40.slax
+++ b/tests/core/test-empty-40.slax
@@ -1,5 +1,15 @@
 version 1.3;
 
+param $slax-arg-1;
+param $slax-arg-2;
+param $slax-arg-3;
+param $slax-arg-4;
+param $slax-arg-5;
+param $slax-arg-6;
+param $slax-arg-7;
+param $slax-arg-8;
+param $slax-arg-9;
+
 var $mine := {
     <a> "alpha";
     <b> "bravo";
@@ -8,6 +18,12 @@ var $mine := {
 }
 
 main <top> {
+    if ($slax-arg-1) {
+        <argv> {
+            call build-argv;
+        }
+    }
+
     <one> slax:join(":", $mine);
     <two> slax:join("%20", "this", "is", "the", "end", $mine/d);
     <three> slax:join(/nothing, $mine);
@@ -23,4 +39,11 @@ template answer {
     <fish> "blue";
 }
 
-    
+template build-argv {
+    for $i in 1 ... 9 {
+        var $a = slax:evaluate("$slax-arg-" _ $i);
+        if (!slax:is-empty($a)) {
+            <arg> $a;
+        }
+    }
+}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant