Re: call-with-input-pipe, call-with-output-pipe: allow list argument

Ivan Shmakov <[email protected]>
Newsgroups gmane.lisp.scheme.scheme48
Message-ID <[email protected]>
	The current implementation of call-with-input-pipe and
	call-with-output-pipe could be further extended to allow for
	filters to be run from Scheme48.  In particular, it'll allow for
	pipes to be constructed by pure Scheme48 means, like:

;; rough equivalent of Shell's cal_nl_output="$(cal | nl)"
foo> (define cal+nl-output
       (call-with-input-pipe '("cal")
         (lambda (cal-output)
           (call-with-input-pipe '("nl")
	     read-all/string
             cal-output))))	; NB: redirecting nl's stdin
; no values returned
foo> (display cal+nl-output)
     1	    October 2008    
     2	Su Mo Tu We Th Fr Sa
     3	          1  2  3  4
     4	 5  6  7  8  9 10 11
     5	12 13 14 15 16 17 18
     6	19 20 21 22 23 24 25
     7	26 27 28 29 30 31   
     8	                    
#{Unspecific}
foo> 

	The change is as follows.

Extend `call-with-mumble-pipe' to allow an optional `port' argument.
(call-with-mumble-pipe): Extended the function returned to accept an
optional port argument to allow for running programs in ``filter'' mode.
(call-with-input-pipe): Extended likewise.
(call-with-output-pipe): Likewise.

--- pipe.scm.~3~	2008-10-21 00:14:14.000000000 +0700
+++ pipe.scm	2008-10-22 21:48:16.000000000 +0700
@@ -7,7 +7,7 @@
 ; end of the pipe and COMMAND gets the output end.
 
 (define (call-with-mumble-pipe input?)
-  (lambda (command proc)
+  (lambda (command proc . may-be-port)
     (call-with-values open-pipe
       (lambda (input-pipe output-pipe)
 	(let ((pid (fork)))
@@ -27,12 +27,19 @@
 	       (lambda ()
 		 #f)
 	       (lambda ()
+		 (define port
+		   (and (not (null? may-be-port))
+			(car may-be-port)))
 		 (if input?
-		     (remap-file-descriptors! (current-input-port)
+		     (remap-file-descriptors! (or
+					       port
+					       (current-input-port))
 					      output-pipe
 					      (current-error-port))
 		     (remap-file-descriptors! input-pipe
-					      (current-output-port)
+					      (or
+					       port
+					       (current-output-port))
 					      (current-error-port)))
 		 (cond ((list? command)
 			(apply exec command))
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.