From 997e521e2368c771822b9741e47850665f7715f1 Mon Sep 17 00:00:00 2001 From: Amirouche Date: Sun, 5 Mar 2017 21:37:09 +0100 Subject: [PATCH 2/2] make open-process public in (ice-9 popen) --- doc/ref/posix.texi | 60 ++++++++++++++++++++++++++++++++++++++++++-------- module/ice-9/popen.scm | 2 +- 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/doc/ref/posix.texi b/doc/ref/posix.texi index 4afe6bf..53f2230 100644 --- a/doc/ref/posix.texi +++ b/doc/ref/posix.texi @@ -2161,12 +2161,12 @@ expiry will be signalled. A real-time timer, counting down elapsed real time. At zero it raises @code{SIGALRM}. This is like @code{alarm} above, but with a higher resolution period. -@end defvar +@end defvar @defvar ITIMER_VIRTUAL A virtual-time timer, counting down while the current process is actually using CPU. At zero it raises @code{SIGVTALRM}. -@end defvar +@end defvar @defvar ITIMER_PROF A profiling timer, counting down while the process is running (like @@ -2175,7 +2175,7 @@ process's behalf. At zero it raises a @code{SIGPROF}. This timer is intended for profiling where a program is spending its time (by looking where it is when the timer goes off). -@end defvar +@end defvar @code{getitimer} returns the restart timer value and its current value, as a list containing two pairs. Each pair is a time in seconds and @@ -2260,6 +2260,48 @@ module@footnote{This module is only available on systems where the (use-modules (ice-9 popen)) @end lisp +@findex open-process +@deffn {Scheme Procedure} open-process mode command [args...] + +Execute @code{command} in a subprocess passing @code{args} as arguments. + +@code{open-process} is the procedure used by and @code{open-pipe} +and else to communicate with a subprocess using pipes. The notable +difference with @code{open-pipe} API is that @code{open-process} +does return multiple values. + +It returns @code{read-port}, @code{write-port} and @code{pid}. Depending +on the @code{mode} you open the subprocess you can access +@code{read-port} and @code{write-port} using regular port procedures. +In particular you can close the @code{read-port} without closing the +@code{write-port}. Don't forget to call @code{waitpid} on @code{pid}. + +Here is an example program that calls @code{tr} that request to replace +@code{qsdf} with @code{asdf}: + +@lisp +(use-modules (ice-9 rdelim)) +(use-modules (ice-9 popen)) + + +(define open-process (@@ (ice-9 popen) open-process)) + +(define (qsdf->asdf string) + (call-with-values (lambda () (open-process OPEN_BOTH "tr" "qsdf" "asdf")) + (lambda (read-port write-port pid) + (display string write-port) + (close-port write-port) + (let ((str (read-string read-port))) + (close-port read-port) + (waitpid pid) + str)))) + + +(pk (qsdf->asdf "qsdf is fun!")) +@result{} "asdf is fun!" +@end lisp +@end deffn + @findex popen @deffn {Scheme Procedure} open-pipe command mode @deffnx {Scheme Procedure} open-pipe* mode prog [args...] @@ -2356,11 +2398,11 @@ the garbage collector pick them up at some later time. @cindex network @menu -* Network Address Conversion:: -* Network Databases:: -* Network Socket Address:: -* Network Sockets and Communication:: -* Internet Socket Examples:: +* Network Address Conversion:: +* Network Databases:: +* Network Socket Address:: +* Network Sockets and Communication:: +* Internet Socket Examples:: @end menu @node Network Address Conversion @@ -3167,7 +3209,7 @@ effect but the value in Guile is always a pair. @c Note that we refer only to ``man ip'' here. On GNU/Linux it's @c ``man 7 ip'' but on NetBSD it's ``man 4 ip''. -@c +@c For IP level (@code{IPPROTO_IP}) the following @var{optname}s are defined (when provided by the system). See @command{man ip} for what they mean. diff --git a/module/ice-9/popen.scm b/module/ice-9/popen.scm index 26a7e0a..37ea068 100644 --- a/module/ice-9/popen.scm +++ b/module/ice-9/popen.scm @@ -22,7 +22,7 @@ :use-module (ice-9 threads) :use-module (srfi srfi-9) :export (port/pid-table open-pipe* open-pipe close-pipe open-input-pipe - open-output-pipe open-input-output-pipe)) + open-output-pipe open-input-output-pipe open-process)) (eval-when (expand load eval) (load-extension (string-append "libguile-" (effective-version)) -- 2.9.3