Is there a way to run an external program with args computed at
runtime? Using run-program (in openmcl) specifies that the args be
"simple strings". What this seems to mean is that anything other than
a "quoted string" is verboten. If I try to put a variable or function
call in, it complains.
For example:
(run-program "tail" "-3" "foo") works.
(run-program "tail" (format nil "~A" -3) "foo") fails.
Neil
In article
<····································@i29g2000prf.googlegroups.com>,
Neil Baylis <···········@gmail.com> wrote:
> Is there a way to run an external program with args computed at
> runtime? Using run-program (in openmcl) specifies that the args be
> "simple strings". What this seems to mean is that anything other than
> a "quoted string" is verboten. If I try to put a variable or function
> call in, it complains.
>
> For example:
>
> (run-program "tail" "-3" "foo") works.
>
> (run-program "tail" (format nil "~A" -3) "foo") fails.
>
> Neil
Clozure CL 1.1 (aka OpenMCL) on a Mac:
? (documentation 'run-program 'function)
"Invoke an external program as an OS subprocess of lisp."
? (arglist 'run-program)
(CCL::PROGRAM CCL::ARGS &KEY :WAIT :PTY :INPUT :IF-INPUT-DOES-NOT-EXIST :OUTPUT :IF-OUTPUT-EXISTS :ERROR :IF-ERROR-EXISTS :STATUS-HOOK :ELEMENT-TYPE :ENV)
:ANALYSIS
You have to pass args as a list. Your call above is not correct.
You need to call it like this :
? (run-program "tail" (list "-n" (format nil "~a" 3) "foo.lisp"))
or even to get some output:
? (run-program "tail" (list "-n" (format nil "~a" 3) "foo.lisp") :output *standard-output*)
Since RUN-PROGRAM is a function, it does not matter how you compute the args list. You
can give a literal list of strings or compute a list of computed strings as the value for
args.
Thanks, Rainer.
Your example made my problem obvious, even though the code in my post
was incorrect:
What had been working for me was the following:
(run-program "tail" '("-3" "foo") :output t)
But when I changed it to the following, it didn't work:
(run-program "tail" '((format nil "~a" -3) "foo") :output t)
Which is now obvious to me.. of course it didn't evaluate the format,
because the expression was quoted. I need to use the (list...) form
you provided. Thanks. I'm learning gradually and still getting tripped
up by things like this.
Interestingly (or not..) the following fails:
? (run-program "tail" '("-3 foo") :output t)
tail: illegal option -- -3 foo
I guess there's something I don't understand about how these strings
get passed to the other process.
Neil
> You should try the same thing in a shell:
>
> $ "tail" "-3 foo"
> tail: illegal option -- -3 foo
> $ "tail" "-3" "foo"
> 27.356
> 43.923
> 67.172
> $
>
> Capische?
>
> -Rob
Duh. The "-3 foo" is ending up in argv[1].
OK, I can deal with that ;)
Thanks,
Neil
From: Robert Maas, see http://tinyurl.com/uh3t
Subject: Re: Openmcl run-program args
Date:
Message-ID: <rem-2008feb21-007@yahoo.com>
> > You should try the same thing in a shell:
> > =A0 =A0 $ "tail" "-3 foo"
> > =A0 =A0 tail: illegal option -- -3 foo
> > =A0 =A0 $ "tail" "-3" "foo"
> > =A0 =A0 27.356
> > =A0 =A0 43.923
> > =A0 =A0 67.172
> > =A0 =A0 $
> From: Neil Baylis <···········@gmail.com>
> Duh. The "-3 foo" is ending up in argv[1].
IMO that's actually one of the *advantages* of using run-program
from Lisp rather than typing directly at the Unix/Linux shell: You
can force exactly what you want into each argument to the called
program, without needing to deal with Unix's weird rules for what's
semi-quoted and what's totally quoted and what's substituted etc.
When talking to a program that has its own weird ways to quote some
characters differently from others, such as grep, it can be
virtually impossible to figure out how to get the effect you want
if the regular expression contains several special characters that
are verbatim and also several special characters that are to be
treated as special regular-expression notation for various things
such as alternatives or first-of-line or last-of-line or
number-of-repetitions etc.
Neil Baylis <···········@gmail.com> wrote:
> Is there a way to run an external program with args computed at
> runtime? Using run-program (in openmcl) specifies that the args be
> "simple strings". What this seems to mean is that anything other than
> a "quoted string" is verboten. If I try to put a variable or function
> call in, it complains.
>
> For example:
>
> (run-program "tail" "-3" "foo") works.
>
> (run-program "tail" (format nil "~A" -3) "foo") fails.
>
> Neil
I wonder which wresion you are using. I am using current snapshot
on Linux and
(run-program "tail" "-3" "foo")
gives me:
Welcome to Clozure Common Lisp Version 1.2-r (LinuxX8664)!
? (run-program "tail" "-3" "foo")
> Error: Incorrect keyword arguments in ("foo") .
> While executing: CCL::TOPLEVEL-EVAL, in process listener(1).
> Type :POP to abort, :R for a list of available restarts.
> Type :? for other options.
1 >
while:
(run-program "tail" (list (format nil "~A" -3) "foo"))
works OK. AFAICS with current snapshot you should give arguments
as a list.
--
Waldek Hebisch
·······@math.uni.wroc.pl