(module processor (processor) (import scheme chicken.base chicken.file.posix chicken.process) ;; @brief Call the command @a cmd with arguments @a args in a subprocess. ;; ;; @param cmd The command to run; @see chicken.process.process-execute's ;; PATHNAME. ;; @param args The arguments to call @a cmd with; @see ;; chicken.process.process-execute's ARGUMENT-LIST. ;; @param stdin The file descriptor to use for the subprocess' stdin. ;; @param stdout The file descriptor to use for the subprocess' stdout. ;; @param stderr The file descriptor to use for the subprocess' stderr. ;; @param env An environment alist; @see chicken.process.process-execute's ;; ENVIRONMENT-ALIST. ;; ;; @returns 4 values: the subprocess' PID, a write file descriptor for stdin, a read ;; file descriptor for stdout, and a read file descriptor for ;; stderr. ;; ;; @see chicken.process.process-execute ;; ;; TODO: How to make it easier to wait for the subprocess and get its `wstatus`? (define (processor cmd args #!key (stdin #f) (stdout #f) (stderr #f) (env #f)) (let-values (((stdin-r stdin-w) (if stdin (values stdin #f) (create-pipe))) ((stdout-r stdout-w) (if stdout (values #f stdout) (create-pipe))) ((stderr-r stderr-w) (if stderr (values #f stderr) (create-pipe)))) (let ((pid (process-fork (lambda () (file-close stdin-w) (file-close stdout-r) (file-close stderr-r) (duplicate-fileno stdin-r fileno/stdin) (duplicate-fileno stdout-w fileno/stdout) (duplicate-fileno stderr-w fileno/stderr) ; TODO: Is there a way for `process-execute` to call ; `execvpe` instead of `execve`? (process-execute cmd args env))))) (values pid stdin-w stdout-r stderr-w)))) )