Skip to content

Commit

Permalink
ssh/popen: Change API to match "open-pipe*"
Browse files Browse the repository at this point in the history
Change (ssh popen) procedures to make their behavior match the behavior of
"open-pipe*" procedure.

Reported by graywolf in
<#39>

* modules/ssh/popen.scm (string-replace-substring): New procedure.
(shell-quote): New procedure.
(open-remote-pipe*, open-remote-input-pipe*, open-remote-output-pipe*): Use
"shell-quote" to quote arguments to make procedures behavior match the
"open-pipe*" behavior.
* tests/common.scm (start-server/exec): Update.
* NEWS: Update.
  • Loading branch information
artyom-poptsov committed Aug 23, 2024
1 parent a3bc3a9 commit 7a6c86f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
12 changes: 12 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ Copyright (C) Artyom V. Poptsov <[email protected]>
notice and this notice are preserved.

* Unreleased
** Change =(ssh popen)= procedures behavior :API_CHANGE:
Now the following procedures handle the program arguments the same way as
=open-pipe*= procedure. This also makes the their behavior match the
description from the Guile-SSH documentation.

This change affects the following procedures from =(ssh popen)=:
- =open-remote-pipe*=
- =open-remote-input-pipe*=
- =open-remote-output-pipe*=

Reported by graywolf in
<https://github.com/artyom-poptsov/guile-ssh/issues/39>
** New simplified version of the project logo
Thanks to Darya Sev. <[email protected]> for very helpful design advices for the
new simplified version of the project logo.
Expand Down
54 changes: 50 additions & 4 deletions modules/ssh/popen.scm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
;;; popen.scm -- Remote popen emulation.

;; Copyright (C) 2015, 2016 Artyom V. Poptsov <[email protected]>
;; Copyright (C) 2015-2024 Artyom V. Poptsov <[email protected]>
;;
;; This file is a part of Guile-SSH.
;;
Expand Down Expand Up @@ -55,6 +55,43 @@

(define OPEN_PTY "t")


;; This procedure is taken from GNU Guile 3.0.0.
;;
;; Original comment:
;;
;; string-replace-substring By A. Wingo in
;; https://lists.gnu.org/archive/html/guile-devel/2014-03/msg00058.html
;; also in string-replace-substring guix:guix/utils.scm.

(define (string-replace-substring str substring replacement)
"Return a new string where every instance of @var{substring} in string
@var{str} has been replaced by @var{replacement}. For example:

@lisp
(string-replace-substring \"a ring of strings\" \"ring\" \"rut\")
@result{} \"a rut of struts\"
@end lisp
"
(let ((sublen (string-length substring)))
(with-output-to-string
(lambda ()
(let lp ((start 0))
(cond
((string-contains str substring start)
=> (lambda (end)
(display (substring/shared str start end))
(display replacement)
(lp (+ end sublen))))
(else
(display (substring/shared str start)))))))))



(define (shell-quote s)
"Quote string S for sh-compatible shells."
(string-append "'" (string-replace-substring s "'" "'\\''") "'"))

(define (open-remote-pipe session command mode)
"Execute a COMMAND on the remote host using a SESSION with a pipe to it.
Returns newly created channel port with the specified MODE."
Expand All @@ -70,7 +107,10 @@ Returns newly created channel port with the specified MODE."
(define (open-remote-pipe* session mode prog . args)
"Execute a PROG with optional ARGS on the remote host using a SESSION with a
pipe to it. Returns newly created channel port with the specified MODE."
(open-remote-pipe session (string-join (cons prog args)) mode))
(open-remote-pipe session
(string-join (cons (shell-quote prog)
(map shell-quote args)))
mode))


(define (open-remote-input-pipe session command)
Expand All @@ -81,7 +121,10 @@ Returns newly created input channel port."
(define (open-remote-input-pipe* session prog . args)
"Execute a PROG with optional ARGS on the remote host using a SESSION with
an input pipe to it. Returns newly created input channel port."
(open-remote-pipe session (string-join (cons prog args)) OPEN_READ))
(open-remote-pipe session
(string-join (cons (shell-quote prog)
(map shell-quote args)))
OPEN_READ))


(define (open-remote-output-pipe session command)
Expand All @@ -92,6 +135,9 @@ Returns newly created input channel port."
(define (open-remote-output-pipe* session prog . args)
"Execute a PROG with optional ARGS on the remote host using a SESSION with
an output pipe to it. Returns newly created output channel port."
(open-remote-pipe session (string-join (cons prog args)) OPEN_WRITE))
(open-remote-pipe session
(string-join (cons (shell-quote prog)
(map shell-quote args)))
OPEN_WRITE))

;;; popen.scm ends here.
11 changes: 6 additions & 5 deletions tests/common.scm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
;;; common.scm -- Heper procedures and macros for tests.

;; Copyright (C) 2015-2021 Artyom V. Poptsov <[email protected]>
;; Copyright (C) 2015-2024 Artyom V. Poptsov <[email protected]>
;;
;; This file is a part of Guile-SSH.
;;
Expand Down Expand Up @@ -427,9 +427,9 @@ scheme@(guile-user)> ")
(define (state:message-handle-exec message session)
(format-log/scm 'nolog "start-server/exec" "state:message-handle-exec: message: ~A" message)
(let ((command (exec-req:cmd (message-get-req message))))
(format-log/scm 'nolog "start-server/exec" "command: ~A" command)
(format-log/scm 'nolog "start-server/exec" "command: ~S" command)
(cond
((string=? command "ping")
((or (string=? command "'ping'") (string=? command "ping"))
(message-reply-success message)
(channel-request-send-exit-status *channel* 0)
(write-line "pong" *channel*)
Expand All @@ -438,7 +438,8 @@ scheme@(guile-user)> ")
(channel-send-eof *channel*))
(lambda args
(format-log/scm 'nolog "start-server/exec" "ERROR: ~a" args))))
((string=? command "uname") ; For exit status testing
((or (string=? command "'uname'") (string=? command "uname"))
;; For exit status testing
(body session message *channel*))
((string=? command "exit status") ; For exit status testing
(message-reply-success message)
Expand Down Expand Up @@ -466,7 +467,7 @@ scheme@(guile-user)> ")
(channel-request-send-exit-status *channel* 0)
(channel-send-eof *channel*))

((string=? command "guile -q")
((or (string=? command "'guile -q'") (string=? command "'guile' '-q'"))
(message-reply-success message)
(display %guile-version-string *channel*)
(body session message *channel*))
Expand Down

0 comments on commit 7a6c86f

Please sign in to comment.