COMMON LISP JP > Archives > 2013/05/30

2013/05/30 02:31:45 UTCrayfill
#
(eval-when (:compile-toplevel :load-toplevel :execute)
  (ql:quickload :flexi-streams)
  (ql:quickload :babel))

(defun stdin-bound-run-program (output program &rest args)
  (multiple-value-bind (out in)
      (sb-posix:pipe)
    (unwind-protect
	 (let ((stdin (sb-sys:make-fd-stream out :input t :element-type '(unsigned-byte 8)))
	       (input (sb-sys:make-fd-stream in :output t :element-type '(unsigned-byte 8))))
	   (values (sb-ext:run-program program args :input stdin :output output :wait nil)
		   input))
      (sb-unix:unix-close out))))

(multiple-value-bind (program stream)
    (stdin-bound-run-program t "/usr/bin/xargs" "echo")
  (with-open-stream (s stream)
    (write-sequence (babel:string-to-octets "foo bar baz" :encoding :utf-8) s)
    program))
#
sbcl限定ですが一応こんな感じで動きました
#
flexi-streamsの方でもmake-in-memory-input-streamを直接使って試してみたんですがrun-program内でstreamのelement-typeがflexi-streams:octetなんて知らねーよ、と怒られました。transformer設定で動くのかしら?
2013/05/30 05:14:18 UTCg000001
#
ありがとうございます:)