unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: tantalum <sph@posteo.eu>
To: samuel.barreto8@gmail.com
Cc: guile-user@gnu.org
Subject: re: how to pipe the output of one process to another using guile ?
Date: Wed, 06 Sep 2017 21:09:53 +0000	[thread overview]
Message-ID: <dc6c7cc942558df945caf13424e395c4@posteo.de> (raw)

hi

(ice-9 popen) 
https://www.gnu.org/software/guile/manual/html_node/Pipes.html
is the only module that can create sub-processes with pipe input/output 
and does not use shell. it takes a program name and runs it as a 
sub-process with a pipe connected to its input/output. but its limited 
in that it can not set up multiple processes to be linked directly by 
pipes.
that means a solution using (ice-9 popen) would look like this:
* two open-pipe calls, one for a pipe for reading from the first program 
and another open-pipe call with a pipe for writing to the second program
* then copying the data with procedures like get-bytevector and 
put-bytevector
this means all data goes through the guile process as well as two pipes 
and not from sub-process to sub-process via one pipe

as far as i know, linking two processes, like in your example, is not 
currently cleanly possible without installing a guile library or 
extension.
there is (os process) 
http://www.nongnu.org/guile-lib/doc/ref/os.process/ in guile-lib which 
offers bindings that seem to do exactly what your example implies:

     (tail-call-pipeline ("ls" "/etc") ("grep" "passwd"))

maybe that will work for you.
i am not sure what limitations this has, since i am under the impression 
that there is no other reliable way to create sub-processes in guile 
(without core extensions) other than with (ice-9 popen).

alternatively you can use shell ("system" uses "sh")

     (system "cat my-toy-file.txt | wc -l")

and if you need the result as a string:

     (import (ice-9 popen) (rnrs io ports))

     (define (shell-eval->string command-str)
         (let* ((port (open-pipe command-str OPEN_READ)) (result 
(get-string-all port))) (close-pipe port)
         result))

     (display (shell-eval->string "cat my-toy-file.txt | wc -l"))

lastly, there is a third option, a scheme library and guile extension 
that is part of sph-lib https://github.com/sph-mn/sph-lib .
the name of the relevant library is (sph process create) and it is 
specifically for creating sub-processes and process chains.
to my knowledge it works reliably (i use it frequently), but it requires 
the compilation and installation of a guile extension (a shared 
library).
and it currently lacks proper online documentation. but here is the 
source code with docstrings anyway: 
https://github.com/sph-mn/sph-lib/blob/master/modules/sph/process/create.scm
here is how the example would be implemented using it:

     (import (sph process create))

     (process-chain-finish
         (process-chain #f (current-output-port) (list (list "cat" 
"my-toy-file.txt") (list "wc" "-l"))))



             reply	other threads:[~2017-09-06 21:09 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-06 21:09 tantalum [this message]
2017-09-07  6:54 ` how to pipe the output of one process to another using guile ? Samuel Barreto
  -- strict thread matches above, loose matches on Subject: below --
2017-09-04 11:03 Samuel Barreto

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=dc6c7cc942558df945caf13424e395c4@posteo.de \
    --to=sph@posteo.eu \
    --cc=guile-user@gnu.org \
    --cc=samuel.barreto8@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).