Processes - leftmike/foment GitHub Wiki

Processes

This API is a strict subset of the Processes API that is part of Racket. Specifically, process groups are not supported, there is no way to specify a different set of environment variables for the subprocess, 'exact command lines are not supported, and the shell-execute procedure is not supported.

(import (foment base)) to use these procedures.

procedure: (subprocess stdout stdin stderr command arg ...)

Create a new process to execute command with arguments, arg ....

If stdout is an output port, it is used as the standard output for the new process. Otherwise, stdout must be #f. In this case, a system pipe will be created with the output end used as the standard output for the new process and the input end returned by subprocess as an input port.

If stdin is an input port, it is used as the standard input for the new process. Otherwise, stdin must be #f. In this case, a system pipe will be created with the input end used as the standard input for the new process and the output end returned by subprocess as an output port.

If stderr is an output port, it is used as the standard error for the new process. If stderr is 'stdout, then the new process uses standard output as standard error. Otherwise, stderr must be #f. In this case, a system pipe will be created with the output end used as the standard error for the new process and the input end returned by subprocess as an input port.

Four values get returned by subprocess: the new subprocess; an input port if a system pipe was created for standard output, otherwise #f; an output port if a system pipe was created for standard input, otherwise #f; and an input port if a system pipe was created for standard error, otherwise #f.

All ports returned by subprocess must be explicitly closed.

procedure: (subprocess-wait subproc)

Wait until subproc terminates.

procedure: (subprocess-status subproc)

If subproc has terminated, return its exit code. Otherwise, return 'running.

procedure: (subprocess-kill subproc force?)

Mac OS and Unix: if force? is #f, send subproc an interrupt signal. Otherwise, kill subproc.

Windows: if force? is #f, do nothing. Otherwise, kill subproc.

procedure: (subprocess-pid subproc)

Return the process identifier for subproc.

procedure: (subprocess? obj)

Returns #t if obj is a subprocess and #f otherwise.

Simple Subprocesses

procedure: (system command)

Use the system shell to execute command, blocking until the command has terminated. Return #t if the command was success (exited with a status code of zero). Otherwise, return #f.

Standard output for the command will be (current-output-port), standard input will be (current-input-port), and standard error will be (current-error-port).

(define (with-output-to-string proc)
    (let ((port (open-output-string)))
        (parameterize ((current-output-port port)) (proc))
        (get-output-string port)))

(with-output-to-string (lambda () (system "ls -lh /")))

procedure: (system* command arg ...)

Execute command with arguments, arg ..., blocking until the command has terminated. Otherwise, system* is the same as system.

procedure: (system/exit-code command)

Return the status code from command after it terminates. Otherwise, system/exit-code is the same as system.

procedure: (system*/exit-code command arg ...)

Return the status code from command after it terminates. Otherwise, system*/exit-code is the same as system*.

procedure: (process command)

Use the system shell to execute command with system pipes connected to the standard input, output, and error of command. Five values will be returned: the input end, as an input port, of the system pipe connected to the standard output of command; an output port connected, via a system pipe, to the standard input of command; the process identifier of command; an input port connected, via a aystem pipe, to the standard error of command; and a control procedure.

The control procedure takes one argument and is used to interact with command. An argument of 'status will return the status of command as 'running, 'done-ok, or 'done-error. An argument of 'exit-code will return the exit code from command if it is terminated, otherwise #f if it is still running. An argument of 'wait will block until command is terminated. An argument of 'interrupt will send command an interrupt signal on Unix and Mac OS; on Windows it does nothing. An argument of 'kill will terminated command.

All ports returned by process must be explicitly closed.

procedure: (process* command arg ...)

Execute command with arguments, arg .... Otherwise, process* is the same as process.

All ports returned by process* must be explicitly closed.

procedure: (process/ports stdout stdin stderr command)

If stdout, stdin, and stderr are all #f, the process\ports is identifical to process. If stdout is an output port, then use it as the standard output for command; in this case, the corresponding value in the result list will be #f rather than an input port. If stdin is an input port, then use it as the standard iput for command; in this case, the corresponding value in the result list will be #f rather than an output port. If stderr is an output port, then use it as the standard error for command. If stderr is 'stdout, then command uses standard output as standard error. In both of these cases, the corresponding value in the result list will be #f rather than an input port.

All ports returned by process/ports must be explicitly closed.

procedure: (process*/ports out in err command arg ...)

Execute command with arguments, arg .... Otherwise, process*/ports is the same as process/ports.

All ports returned by process*/ports must be explicitly closed.

⚠️ **GitHub.com Fallback** ⚠️