LispWorks User Guide and Reference Manual > 40 The SYSTEM Package

NextPrevUpTopContentsIndex

open-pipe

Function
Summary

Runs a command in a subshell.

Package

system

Signature

open-pipe command &key direction element-type interrupt-off shell-type => stream

Arguments

command

A string, a list of strings, a simple-vector of strings, or nil .

direction

:input , :output or :io .

element-type

A type specifier.

interrupt-off

A boolean. Not implemented on Microsoft Windows.

shell-type

A shell type.

Values

stream

A pipe stream.

Description

On Unix/Linux/Mac OS X the behavior of open-pipe is analogous to that of popen in the UNIX library. It creates a pipe to/from a subprocess and returns a stream. The stream can be read from or written to as appropriate.

On Microsoft Windows open-pipe calls CreateProcess and CreatePipe and returns a bidirectional stream.

If command is a string then it is passed to the shell as the command to run without any arguments. If command is a list, then its first element is the command to run directly and the other elements are passed as arguments on the command line (that is, element 0 has its name in argv[0] in C, and so on). If command is a simple-vector of strings, the element at index 0 is the command to run and the other elements are the complete set of arguments seen by the command (that is, element 1 becomes argv[0] in C, and so on). If command is nil , then the shell is run.

direction is a keyword for the stream direction. The default value is :input . Bidirectional (I/O) pipes may be created by pasing :io . See the example below. This argument is ignored on Microsoft Windows.

element-type specifies the type of the stream as with open. The default value is base-char . This argument is ignored on Microsoft Windows.

interrupt-off , if t , ensures that Ctrl+C (SIGINT) to the LispWorks image is ignored by the subprocess. This argument is not implemented on Microsoft Windows.

shell-type specifies the type of shell to run. On UNIX/Linux/Mac OS X/FreeBSD the default value is "/bin/sh" . On Microsoft Windows the default value is "cmd" . Note that on Windows ME/98/95 you will need to pass "command" .

stream supports mixed character and binary I/O in the same way as file streams constructed by open.

Examples

Example on Unix:

CL-USER 1 > (setf *ls* (sys:open-pipe "ls"))
Warning: Setting unbound variable *LS*
#<SYSTEM::PIPE-STREAM "ls">
 
CL-USER 2 > (loop while 
                  (print (read-line *ls* nil nil)))
 
"hello" 
"othello" 
NIL 
NIL
 
CL-USER 3 > (close *ls*)
T

The following example shows you how to use bidirectional pipes.

CL-USER 1 >  (with-open-stream
                   (s (sys:open-pipe "/bin/csh"
                                     :direction :io))
                 (write-line "whereis ls" s) 
                 (force-output s)
                 (read-line s))
"ls: /sbin/ls /usr/bin/ls /usr/share/man/man1.Z/ls.1"
NIL

Example on Microsoft Windows

CL-USER 40 > (setf *ls* (sys:open-pipe "dir"))
#<WIN32::TWO-WAY-PIPE-STREAM 205F03F4>
 
CL-USER 41 > (loop while 
                   (print (read-line *ls* nil nil)))
 
" Volume in drive Z is lispsrc" 
" Volume Serial Number is 82E3-1342" 
"" 
" Directory of Z:\\v42\\delivery-tests" 
"" 
"20/02/02  11:57a        <DIR>          ." 
"20/02/02  11:57a        <DIR>          .." 
"14/02/02  07:04p             6,815,772 othello.exe" 
"14/02/02  07:07p             6,553,628 hello.exe" 
"               4 File(s)     13,369,400 bytes" 
"                          3,974,103,040 bytes free" 
NIL 
NIL
 
CL-USER 42 > (close *ls*)
T
See also

call-system
call-system-showing-output


LispWorks User Guide and Reference Manual - 22 Dec 2009

NextPrevUpTopContentsIndex