Skip to content

Instantly share code, notes, and snippets.

@fukamachi
Created March 31, 2017 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fukamachi/46ac200115f1b6129a5f917668529264 to your computer and use it in GitHub Desktop.
Save fukamachi/46ac200115f1b6129a5f917668529264 to your computer and use it in GitHub Desktop.
external-program to stream
#+sbcl
(defun start (commands)
(check-type commands cons)
(multiple-value-bind (inputfd-shell outputfd-shell)
#+win32 (sb-win32::make-named-pipe) #-win32 (sb-posix:pipe)
(multiple-value-bind (inputfd-cl outputfd-cl)
#+win32 (sb-win32::make-named-pipe) #-win32 (sb-posix:pipe)
#+win32
(setf (sb-win32::inheritable-handle-p inputfd-cl) t
(sb-win32::inheritable-handle-p outputfd-cl) t)
(let ((input (sb-sys:make-fd-stream inputfd-cl :input t :serve-events t))
(output (sb-sys:make-fd-stream outputfd-shell :output t))
(shell-output (sb-sys:make-fd-stream outputfd-cl :output t)))
(values (make-two-way-stream input output)
(external-program:start (first commands) (rest commands)
:input (sb-sys:make-fd-stream inputfd-shell :input t)
:output shell-output
:error *error-output*
:status-hook
(lambda (process)
(when (eq (external-program:process-status process) :exited)
(close shell-output)
(close output)))))))))
#-sbcl (error "Supports only SBCL")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment