Re: a small SBCL question



"TorvoSollazzo@xxxxxxxxxxxx" <matteo.pradella@xxxxxxxxx> writes:

Hi,
I have a small practical question for the SBCL experts: I need to call
an external program from my lisp code to excange data through files. At
shell, it is something like: "./the_program input.file output.file".
How can I do it using SBCL? I found sb-ext:run-program, but its
documentation appears a bit "sketchy" (at least to me ;)).

Btw, I found the following solutions for other Lisps (but I'd like to
use SBCL):
CLISP: (ext:shell "...")
ECL: (ext:system "...")
GCL: (system "...")

S/CL-USER[37]> (describe 'sb-ext:run-program)

SB-EXT:RUN-PROGRAM is an external symbol in #<PACKAGE "SB-EXT">.
Function: #<FUNCTION SB-EXT:RUN-PROGRAM>Its associated name (as in FUNCTION-LAMBDA-EXPRESSION) is SB-EXT:RUN-PROGRAM.
The function's arguments are: (PROGRAM ARGS &KEY (ENV NIL ENV-P)
(ENVIRONMENT
(IF ENV-P
(UNIX-ENVIRONMENT-SBCL-FROM-CMUCL ENV)
(POSIX-ENVIRON))
ENVIRONMENT-P)
(WAIT T) SEARCH PTY INPUT
IF-INPUT-DOES-NOT-EXIST OUTPUT
(IF-OUTPUT-EXISTS ERROR) (ERROR OUTPUT)
(IF-ERROR-EXISTS ERROR) STATUS-HOOK)
Function documentation:
RUN-PROGRAM creates a new Unix process running the Unix program
found in the file specified by the PROGRAM argument. ARGS are the
standard arguments that can be passed to a Unix program. For no
arguments, use NIL (which means that just the name of the program is
passed as arg 0).

RUN-PROGRAM will return a PROCESS structure. See the CMU Common Lisp
Users Manual for details about the PROCESS structure.

Notes about Unix environments (as in the :ENVIRONMENT and :ENV args):

- The SBCL implementation of RUN-PROGRAM, like Perl and many other
programs, but unlike the original CMU CL implementation, copies
the Unix environment by default.

- Running Unix programs from a setuid process, or in any other
situation where the Unix environment is under the control of someone
else, is a mother lode of security problems. If you are contemplating
doing this, read about it first. (The Perl community has a lot of good
documentation about this and other security issues in script-like
programs.)

The &KEY arguments have the following meanings:

:ENVIRONMENT
a list of SIMPLE-BASE-STRINGs describing the new Unix environment
(as in "man environ"). The default is to copy the environment of
the current process.
:ENV
an alternative lossy representation of the new Unix environment,
for compatibility with CMU CL
:SEARCH
Look for PROGRAM in each of the directories along the $PATH
environment variable. Otherwise an absolute pathname is required.
(See also FIND-EXECUTABLE-IN-SEARCH-PATH)
:WAIT
If non-NIL (default), wait until the created process finishes. If
NIL, continue running Lisp until the program finishes.
:PTY
Either T, NIL, or a stream. Unless NIL, the subprocess is established
under a PTY. If :pty is a stream, all output to this pty is sent to
this stream, otherwise the PROCESS-PTY slot is filled in with a stream
connected to pty that can read output and write input.
:INPUT
Either T, NIL, a pathname, a stream, or :STREAM. If T, the standard
input for the current process is inherited. If NIL, /dev/null
is used. If a pathname, the file so specified is used. If a stream,
all the input is read from that stream and send to the subprocess. If
:STREAM, the PROCESS-INPUT slot is filled in with a stream that sends
its output to the process. Defaults to NIL.
:IF-INPUT-DOES-NOT-EXIST (when :INPUT is the name of a file)
can be one of:
:ERROR to generate an error
:CREATE to create an empty file
NIL (the default) to return NIL from RUN-PROGRAM
:OUTPUT
Either T, NIL, a pathname, a stream, or :STREAM. If T, the standard
output for the current process is inherited. If NIL, /dev/null
is used. If a pathname, the file so specified is used. If a stream,
all the output from the process is written to this stream. If
:STREAM, the PROCESS-OUTPUT slot is filled in with a stream that can
be read to get the output. Defaults to NIL.
:IF-OUTPUT-EXISTS (when :OUTPUT is the name of a file)
can be one of:
:ERROR (the default) to generate an error
:SUPERSEDE to supersede the file with output from the program
:APPEND to append output from the program to the file
NIL to return NIL from RUN-PROGRAM, without doing anything
:ERROR and :IF-ERROR-EXISTS
Same as :OUTPUT and :IF-OUTPUT-EXISTS, except that :ERROR can also be
specified as :OUTPUT in which case all error output is routed to the
same place as normal output.
:STATUS-HOOK
This is a function the system calls whenever the status of the
process changes. The function takes the process as an argument.
Its defined argument types are:
(T T &KEY (:ENV T) (:ENVIRONMENT T) (:WAIT T) (:SEARCH T) (:PTY T) (:INPUT T) (:IF-INPUT-DOES-NOT-EXIST T) (:OUTPUT T) (:IF-OUTPUT-EXISTS T) (:ERROR T) (:IF-ERROR-EXISTS T) (:STATUS-HOOK T))
Its result type is:
(VALUES (OR SB-IMPL::PROCESS NULL) &OPTIONAL)
On Mon, Oct 30, 2006 03:06:13 PM [-1] it was compiled from:
SYS:SRC;CODE;RUN-PROGRAM.LISP.NEWEST Created: Friday, September 8, 2006 03:36:50 PM [-1]


;; Outch! How sketchy!

S/CL-USER[38]> (sb-ext:run-program "/bin/ls" '("-l" "/tmp/a.lisp"))

#<SB-IMPL::PROCESS :EXITED 1>

S/CL-USER[39]> (with-open-stream (files
(sb-ext:process-output
(sb-ext:run-program "/bin/ls" '("-l" "/tmp/a.lisp")
:output :stream)))
(loop :for line = (read-line files nil nil)
:while line :do (princ line) (terpri)))
-rw-r--r-- 1 pjb pjb 93 2006-12-13 21:21 /tmp/a.lisp
NIL

S/CL-USER[40]>

--
__Pascal Bourguignon__ http://www.informatimago.com/

CAUTION: The mass of this product contains the energy equivalent of
85 million tons of TNT per net ounce of weight.
.



Relevant Pages

  • Re: Displaying WMV from a stream with the WM ASF Reader Filter in a filter graph
    ... stream ). ... It uses DS Pack 2.3.4 and the DS filters from oggDS0995.exe The filter ... function TBCFileStream.SetPointer: HResult; ... inherited Create('File reader', nil, AStream, hr); ...
    (comp.lang.pascal.delphi.misc)
  • Re: Read-from-string
    ... for token = (read s nil eos nil) ... You can avoid the gensym by passing the stream itself. ... (loop for token = (read stream nil stream) ...
    (comp.lang.lisp)
  • Re: Changing reader syntax locally
    ... (w (read stream t nil t))) ... (copy-readtable *readtable* nil). ... SPECIFIER is either a symbol naming a function (available at read ...
    (comp.lang.lisp)
  • Re: Read-from-string
    ... for token = (read s nil eos nil) ... for token = (read stream nil stream) ... (loop for token = (read stream nil stream) ...
    (comp.lang.lisp)
  • Re: Oldest Biblical Text
    ... No. You're intentionally dodging the issue. ... nothing (zero, zip, nil) which demonstrates these claims ... which was preserved in the specific environment. ...
    (soc.history.ancient)