How to write an execline helper in execline?

From: Olivier Brunel <jjk_at_jjacky.com>
Date: Tue, 11 Oct 2016 20:26:39 +0200

Hey,

So I find myself willing to write what I'll call execline helpers -
i.e. prog that do their work, then exec into (the rest of) their command
line, in typical execline fashion - in execline.

IOW an execline script that can be used as execline "command". But the
whole "exec into the (rest of the) command line" appears trickier than
I'd have thought it'd be.

If my helper takes no argument, it's easy enough:

#!/usr/bin/execlineb -S0
# work here
$_at_

Easy enough, right. (And no need to mess with the environment, which
is always nice.)

Now if it takes one (or more) arguments however, things fall apart. I
doubt I'm the first to want to do such a thing, so I may be missing out
something obvious/clever to do it, but I find myself stuck with this:

#!/usr/bin/execlineb
importas 1 1
# work w/ $1 here
shift -n 1
elgetpositionals
emptyenv -P
$_at_

That feels like an awful lot of "environment processing" just to get
"proper" substitution: execlineb needs to do its push, shift does the
shift, emptyenv does the pop, and of course there's the whole
importas/elgetpositionals as well. (Not to mention the boilerplate
involed...)

(Also, it's a weird error if there was no "rest of cmdline" to exec
into:
emptyenv: usage: emptyenv [ -p | -c | -o | -P ] prog...
Not the most obvious thing, though not that big a deal either I guess.)

I couldn't find simpler though, unless "cheating" by passing the args
in the environment, then I can do:

#!/usr/bin/execlineb -S0
importas -u ARG ARG
# work w/ $ARG here
$_at_

Which should work, but makes using the helper much more cumbersome.

So, please enlighten me if I'm missing a better solution, of course,
but in case I'm not, I was wondering how you'd feel about adding a new
option to execlineb, e.g. -s, that would require a number as arg (much
like -S) but would work as such:

- must have at least n+1 args, else error out
- substitute $1 ... $n and $_at_ in prog, only $@ would start from the
  n+1th arg
- exec in the new argv, w/out touching the environment :)

So back to my helper, I could do:

#!/usr/bin/execlineb -s1
# work w/ $1 here
$_at_

That's it. It might have other use as well, but at least to write
execline helpers in execline that will take one (or more) args, it
seems to me like a pretty useful thing.

What do you think?


Cheers,
Received on Tue Oct 11 2016 - 18:26:39 UTC

This archive was generated by hypermail 2.3.0 : Sun May 09 2021 - 19:38:49 UTC