processor pasted by siiky on Tue Apr 13 01:54:30 2021
(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))))
)
Example use added by siiky on Tue Apr 13 01:55:56 2021
#;1> processor #<procedure (processor#processor cmd args . rest)> #;2> (processor "ls" '()) 1776616 4 5 8 ; 4 values #;3> (import chicken.file.posix) ; loading /home/siiky/.local/lib/chicken/11/chicken.file.posix.import.so ... #;4> (define stdout (open-input-file* 5)) #;5> (read stdout) processor.build.sh #;6> (read stdout) processor.egg #;7> (read stdout) processor.import.scm #;8> (read stdout) processor.import.so #;9> (read stdout) processor.install.sh #;10> (read stdout) processor.link #;11> (read stdout) processor.scm #;12> (read stdout) processor.so #;13> (read stdout) processor.static.o #;14> (read stdout) processor.types #;15> (read stdout) ^C *** user interrupt *** #;15> (import chicken.process) ; loading /home/siiky/.local/lib/chicken/11/chicken.process.import.so ... #;16> (process-wait 1776616) 1776616 #t 0 ; 3 values #;17> (read stdout) ^C *** user interrupt *** #;17>