1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-26 13:10:22 +02:00

Allows modification of shell used by open-pipe

open-pipe executes a shell command in a subprocess. This commit adds
the ability to modify the shell used for executing commands.
The default "/bin/sh -c" can be inspected and modified by the
new procedure-with-setter 'pipe-shell-command-transformer'.

This useful in MinGW since its "sh" is not in "/bin".

* module/ice-9/popen.scm (%command-transformer): new procedure
  (pipe-shell-command-transformer): new procedure-with-setter
  (open-pipe): use new command transformer
* doc/ref/posix.texi (open-pipe): mention pipe-shell-command-transformer
  (pipe-shell-command-transformer): document new procedure
* test-suite/tests/popen.test ("pipe-shell-command-transformer"): new tests
  Also, modify open-pipe shell for MinGW
* NEWS: updated
* test-suite/tests/ports.test (mingw?): new variable
    Also, modify open-pipe shell for MinGW
This commit is contained in:
Michael Gran 2023-06-25 07:57:18 -07:00
parent 9d625278f7
commit 1174e1eb9d
5 changed files with 83 additions and 9 deletions

View file

@ -1,30 +1,32 @@
;; popen emulation, for non-stdio based ports.
;;;; Copyright (C) 1998-2001, 2003, 2006, 2010-2013, 2019
;;;; Copyright (C) 1998-2001, 2003, 2006, 2010-2013, 2019, 2025
;;;; 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 binary-ports)
#:use-module (ice-9 threads)
#:use-module (ice-9 receive)
#:use-module (srfi srfi-1)
#: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 pipeline))
open-output-pipe open-input-output-pipe pipeline
pipe-shell-command-transformer))
(eval-when (expand load eval)
(load-extension (string-append "libguile-" (effective-version))
@ -74,6 +76,15 @@
rw-port)
;; A procedurde that changes the command received by open-pipe into the
;; shell command to be run by open-pipe*.
(define (%command-transformer cmd)
`("/bin/sh" "-c" ,cmd))
(define pipe-shell-command-transformer
(make-procedure-with-setter
(lambda () %command-transformer)
(lambda (p) (set! %command-transformer p))))
;; a guardian to ensure the cleanup is done correctly when
;; an open pipe is gc'd or a close-port is used.
(define pipe-guardian (make-guardian))
@ -156,7 +167,7 @@ A port to the process (based on pipes) is created and returned.
@var{mode} specifies whether an input, an output or an input-output
port to the process is created: it should be the value of
@code{OPEN_READ}, @code{OPEN_WRITE} or @code{OPEN_BOTH}."
(open-pipe* mode "/bin/sh" "-c" command))
(apply open-pipe* mode (%command-transformer command)))
(define (fetch-pipe-info port)
(%port-property port 'popen-pipe-info))