Subject: Re: bug#15228: making open-process public

From: Amirouche <[email-masked]>
Date: Sun, 5 Mar 2017 21:41:56 +0100

Sorry I have an issue with my mail I can't find the mail where you asked 
for a patch.

Attached to this mail my very first patch.

Le 03/09/2016 à 09:33, Amirouche Boubekki a écrit :
> Wingo wrote:
>
>> We could just expose `open-process' from (ice-9 popen) to start with.
>
> AFAIK, that's what Mark wants.
>
> Here is an example use of `open-process' to wrap `html2text':
>
>
>    (use-modules (ice-9 popen))
>
>    (define open-process (@@ (ice-9 popen) open-process))
>
>    (define (html2text string)
>      (with-error-to-file "/dev/null"
>        (lambda ()
>          (call-with-values (lambda () (open-process OPEN_BOTH 
> "html2text"))
>            (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))))))
>
> IIUC to achieve this goal, I need to make `open-process' public
> in `ice-9 popen` module and add documentation for it?
>
> Is that correct?
>
>
>

>From 997e521e2368c771822b9741e47850665f7715f1 Mon Sep 17 00:00:00 2001
From: Amirouche <[email-masked]>
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

>From ae9587457b502234fc137644a12bbd754e6fdc0c Mon Sep 17 00:00:00 2001
From: Amirouche <[email-masked]>
Date: Sun, 5 Mar 2017 21:38:27 +0100
Subject: [PATCH 1/2] whitespace cleanup

---
 module/ice-9/popen.scm | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/module/ice-9/popen.scm b/module/ice-9/popen.scm
index b166e9d..26a7e0a 100644
--- a/module/ice-9/popen.scm
+++ b/module/ice-9/popen.scm
@@ -2,21 +2,21 @@
 
 ;;;; Copyright (C) 1998, 1999, 2000, 2001, 2003, 2006, 2010, 2011, 2012,
 ;;;;   2013 Free Software Foundation, Inc.
-;;;; 
+;;;;
 ;;;; This library is free software; you can redistribute it and/or
 ;;;; modify it under the terms of the GNU Lesser General Public
 ;;;; License as published by the Free Software Foundation; either
 ;;;; version 3 of the License, or (at your option) any later version.
-;;;; 
+;;;;
 ;;;; This library is distributed in the hope that it will be useful,
 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 ;;;; Lesser General Public License for more details.
-;;;; 
+;;;;
 ;;;; You should have received a copy of the GNU Lesser General Public
 ;;;; License along with this library; if not, write to the Free Software
 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-;;;; 
+;;;;
 
 (define-module (ice-9 popen)
   :use-module (ice-9 threads)
@@ -144,4 +144,3 @@ information on how to interpret this value."
 (define (open-input-output-pipe command)
   "Equivalent to @code{open-pipe} with mode @code{OPEN_BOTH}"
   (open-pipe command OPEN_BOTH))
-
-- 
2.9.3