summaryrefslogtreecommitdiffstats
path: root/stdlib/getput.tl
diff options
context:
space:
mode:
Diffstat (limited to 'stdlib/getput.tl')
-rw-r--r--stdlib/getput.tl46
1 files changed, 46 insertions, 0 deletions
diff --git a/stdlib/getput.tl b/stdlib/getput.tl
index aa28b56d..c01c73c5 100644
--- a/stdlib/getput.tl
+++ b/stdlib/getput.tl
@@ -46,6 +46,22 @@
(buf-set-length b (min (+ p p) bytes)))))
b))
+(defun sys:maproc-common (cmd-lambda put-expr get-expr)
+ (tree-bind (pipe-rd . pipe-wr) (pipe)
+ (with-stream (cmd-stdout (open-fileno pipe-wr "w"))
+ (with-stream (cmd-out (open-fileno pipe-rd "r"))
+ (match-case (fork)
+ (0 (close-stream cmd-out)
+ (with-stream (cmd-in (let ((*stdout* cmd-stdout))
+ [cmd-lambda]))
+ [put-expr cmd-in])
+ (exit* 0))
+ (nil (throwf 'process-error "~s: fork failed" %fun%))
+ (@pid (close-stream cmd-stdout)
+ (let ((out [get-expr cmd-out]))
+ (wait pid)
+ out)))))))
+
(defun get-jsons (: (s *stdin*))
(when (stringp s)
(set s (make-string-byte-input-stream s)))
@@ -205,6 +221,36 @@
(with-stream (s (open-command cmd `w@mopt`))
(put-jsons seq s flat-p)))
+(defun map-command-lines (command lines : mopt)
+ (sys:maproc-common (lambda () (open-command command `w@mopt`))
+ (lambda (strm) (put-lines lines strm))
+ (lambda (strm) (lcons-force (get-lines strm)))))
+
+(defun map-process-lines (program args lines : mopt)
+ (sys:maproc-common (lambda () (open-process program `w@mopt` args))
+ (lambda (strm) (put-lines lines strm))
+ (lambda (strm) (lcons-force (get-lines strm)))))
+
+(defun map-command-str (command str : mopt)
+ (sys:maproc-common (lambda () (open-command command `w@mopt`))
+ (lambda (strm) (put-string str strm))
+ (lambda (strm) (get-string strm))))
+
+(defun map-process-str (program args str : mopt)
+ (sys:maproc-common (lambda () (open-process program `w@mopt` args))
+ (lambda (strm) (put-string str strm))
+ (lambda (strm) (get-string strm))))
+
+(defun map-command-buf (command buf : (pos 0) bytes (skip 0) mopt)
+ (sys:maproc-common (lambda () (open-command command `w@mopt`))
+ (lambda (strm) (put-buf buf pos strm))
+ (lambda (strm) (sys:get-buf-common strm bytes skip))))
+
+(defun map-process-buf (program args buf : (pos 0) bytes (skip 0) mopt)
+ (sys:maproc-common (lambda () (open-process program `w@mopt` args))
+ (lambda (strm) (put-buf buf pos strm))
+ (lambda (strm) (sys:get-buf-common strm bytes skip))))
+
(defmacro close-lazy-streams (. body)
^(let ((sys:*lazy-streams*))
(unwind-protect