From a9e1bec854f6c0343dfb2cb32879cdb31d84f209 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Tue, 2 Feb 2016 16:54:39 -0800 Subject: Adding --eargs mechanism. * txr.c (txr_main): Implement --eargs. * txr.1: Documented. --- txr.1 | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- txr.c | 23 ++++++++++++++++++- 2 files changed, 103 insertions(+), 2 deletions(-) diff --git a/txr.1 b/txr.1 index aaec5228..672aca3d 100644 --- a/txr.1 +++ b/txr.1 @@ -716,6 +716,20 @@ argument, which is useful on some systems which have limitations in their implementation of the "hash bang" mechanism. For details about its special syntax, See Hash Bang Support below. +.coIP --eargs +The +.code --eargs +option (extended +.codn --args ) +is like +.code --args +but must be followed by an argument. The argument is substituted +in place of occurrences of +.code {} +in the +.code --eargs +syntax. + .coIP --lisp This option influences the treatment of query files which do not have a suffix indicating their type: they are treated as \*(TL source. @@ -994,7 +1008,9 @@ not work. .cble To support systems like this, \*(TX supports the special argument -.codn --args . +.codn --args , +as well as as an extended version, +.codn --eargs . With .codn --args , it is possible to encode multiple arguments @@ -1026,6 +1042,70 @@ of that argument, is split into the two arguments .codn "-B -f" . +The +.code --eargs +mechanism allows an additional flexibility. An +.code --eargs +argument must be followed by one more argument. +Occurrences of the two-character sequence +.code {} +in the encoded argument string are replaced with that +following argument. This replacement occurs after +the argument splitting. + +Example: + +.cblk + #!/usr/bin/txr --eargs:-B:{}:--foo:42 +.cble + +This has an effect which cannot be replicated in any known +implementation of the hash bang mechanism. Suppose +that this hash bang line is placed in a script called +.codn script.txr . +When this script is invoked with arguments, as in: + +.cblk + script.txr a b c +.cble + +then \*(TX is invoked similarly to: + +.cblk + /usr/bin/txr --eargs:-B:{}:--foo:42 script.txr a b c +.cble + +Then, when +.code --eargs +processing takes place, firstly the argument sequence + +.code + -B {} --foo 42 +.code + +is produced. Then, all occurrences of +.code {} +are replaced with +.codn script.txr , +resulting in: + +.code + -B script.txr --foo 42 +.code + +The resulting \*(TX invocation is + +.code + /usr/bin/txr -B script.txr --foo 42 a b c +.code + +Thus, +.code --eargs +allows some arguments to be encoded into the interpreter script, such that +script name is inserted anywhere among them, possibly multiple times. Arguments +for the interpreter can be encoded, as well as arguments to be processed by the +script. + .SS* Whitespace Outside of directives, whitespace is significant in \*(TX queries, and represents a pattern match for whitespace in the input. An extent of text consisting of diff --git a/txr.c b/txr.c index 476362ae..d9c2b166 100644 --- a/txr.c +++ b/txr.c @@ -474,7 +474,28 @@ int txr_main(int argc, char **argv) continue; } - /* Odd case 2: -Dfoo=bar syntax. */ + /* Odd case 2: --eargs is followed by an arbitrary delimiting + * character, not necessarily = */ + if (match_str(arg, lit("--eargs"), zero) && ge(length(arg), num(8))) { + val sep = sub_str(arg, num(7), num(8)); + val arg2; + if (!arg_list) { + format(std_error, + lit("~a: --eargs=[...] must be followed by an argument\n"), + prog_string, nao); + return EXIT_FAILURE; + } + arg = sub_str(arg, num(8), nil); + arg2 = upop(&arg_list, &arg_undo); + arg_list = append2(mapcar(curry_123_3(func_n3(regsub), + regex_compile(lit("{}"), nil), + arg2), + split_str(arg, sep)), + arg_list); + continue; + } + + /* Odd case 3: -Dfoo=bar syntax. */ if (equal(sub(arg, zero, two), lit("-D"))) { val dopt_arg = sub(arg, two, t); cons_bind(var, def, split_str(dopt_arg, lit("="))); -- cgit v1.2.3