[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.14.2 Process ports

Function: open-input-process-port command
Runs command via /bin/sh asynchronously. An input port is returned, which is connected to the stdout of command. The stdin and stderr of command is redirected from/to /dev/null. If you need to get an error output, you can use shell's redirection.
 
(define port (open-input-process-port "ls -l Makefile"))
(read-line port)
 => "-rw-r--r--   1 shiro    users        1013 Jun 22 21:09 Makefile"

(open-input-process-port "command 2>&1")
 => ;the port reads both stdout and stderr

(open-input-process-port "command 2>&1 1>/dev/null")
 => ;the port reads stderr
Metacharacters are interpreted by shell. You have to be careful if you pass the string provided from the outside world, for it may make a security flaw.

The exit status of subprocess is not automatically collected. It is the caller's responsibility to issue process-wait, or the subprocess remains in a zombie state. If it bothers you, you can use one of the following functions.

Function: call-with-input-process command proc
Runs command via /bin/sh and pipes its stdout to an input port, then call proc with the port as an argument. When proc returns, it collects its exit status, then returns the result proc returned. The cleanup is done even if proc raises an error.
 
(call-with-input-process "ls -l *"
  (lambda (p) (read-line p)))

Function: with-input-from-process command thunk
Runs command via /bin/sh, and calls thunk with its current input port connected to the command's stdout. The command is terminated and its exit status is collected when thunk returns, or it raises an error.
 
(with-input-from-process "ls -l *" read-line)

Function: open-output-process-port command
Runs command via /bin/sh, and returns an output port which is connected to the stdin of the command. The stdout and stderr of the command is redirected to /dev/null.

The exit status of the subprocess is not automatically collected. The caller should call process-wait on the subprocess at appropriate time.

Function: call-with-output-process command proc
Runs command via /bin/sh, and calls proc with an output port which is conected to the stdin of the command. The exit status of the command is collected after proc returns, or it raises an error.

 
(call-with-output-process "/usr/sbin/sendmail"
  (lambda (out) (display mail-body out)))

Function: with-output-to-process command thunk
Same as call-with-output-process, except that the output port which is connected to the stdin of the command is set to the current output port while executing thunk.

Function: call-with-process-io command proc
Runs command via /bin/sh, and calls proc with two arguments; the first argument is an input port which is connected to the command's stdout, and the second is an output port connected to the command's stdin. The error output from the command is redirected to /dev/null.

The exit status of the command is collected when proc returns or raises an error.

Function: process-output->string command
Function: process-output->string-list command
Runs command and collects its output (to stdout) to return them. process-output->string concatenates all the output from command to one string, replacing any sequence of whitespace characters to single space. The action is similar to "command substitution" in shell scripts. process-output->string-list collects the output from command line-by-line and returns the list of them. Newline characters are stripped.

Internally, command is run by call-with-input-process.

 
(process-output->string "uname -smp")
  => "Linux i686 unknown"

(process-output->string "ls")
  => "a.out foo.c foo.c~ foo.o"

(process-output->string-list "ls")
  => ("a.out" "foo.c" "foo.c~" "foo.o")


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Ken Dickey on November, 28 2002 using texi2html