unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Timothy Sample <samplet@ngyro.com>
To: luhux <luhux@outlook.com>
Cc: guile-user <guile-user@gnu.org>
Subject: Re: Questions about (open-input-output-pipe
Date: Wed, 11 Nov 2020 12:32:13 -0500	[thread overview]
Message-ID: <87k0urre8y.fsf@ngyro.com> (raw)
In-Reply-To: <CAKuG=vuNj49S_9xnv6jtRayVFPPB07VAbSkw9+yZZqnkAvKEZg@mail.gmail.com> (Neil Jerram's message of "Tue, 10 Nov 2020 14:27:34 +0000")

Hi luhux,

Neil Jerram <neiljerram@gmail.com> writes:

> On Tue, 10 Nov 2020 at 08:51, luhux <luhux@outlook.com> wrote:
>
>> I want to implement this shell command:
>>
>> wg pubkey < private > public
>>
>> [...]
>>
>> I implemented it using the following code in guile:
>>
>> [...]
>>
>> (define (wg-gen-public-key private-key)
>>   (let ((port (open-input-output-pipe "wg pubkey")))
>>     (display private-key port)
>>     (let ((result (read-line port)))
>>       (close-port port)
>>       result)))
>>
>> but the code [gets] stuck [...]
>>
>> How should I do?
>
> I guess it's because "wg pubkey" has not yet seen EOF on its input, i.e. it
> doesn't know that the input is complete.
>
> If that's right, I'm afraid I don't know how to fix it.  Presumably you
> need a call that closes half of the port, but still allows reading the "wg
> pubkey" output from it.

You can use the new “pipeline” interface:

    (define (wg-gen-public-key private-key)
      (call-with-values (lambda () (pipeline '(("wg" "pubkey"))))
        (lambda (from to pids)
          (display private-key to)
          (close-port to)
          (let ((result (read-line from)))
            (close-port from)
            ;; Reap the process and check its status.
            (match-let* (((pid) pids)
                         ((_ . status) (waitpid pid)))
              (unless (zero? (status:exit-val status))
                (error "could not generate public key")))
            result))))

It gives you two ports, “from” and “to”, as if you called “pipe” and
then forked and exec’ed.  Hence, you can close the “to” port to let “wg”
know there’s no more input coming.  (BTW, if you use this code directly,
don’t forget to use the “(ice-9 match)” module for “match-let*”.)

As far as I know, there is no way to close half of the “open-pipe” port.
I think that “OPEN_BOTH” is useful if you have a “co-process” that
implements some protocol.  That way, it knows it needs to respond
immediately whenever it finishes reading a protocol-defined message
(after every S-expression, for example).

Hope that helps!


-- Tim



      reply	other threads:[~2020-11-11 17:32 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-10  8:29 Questions about (open-input-output-pipe luhux
2020-11-10 14:27 ` Neil Jerram
2020-11-11 17:32   ` Timothy Sample [this message]

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=87k0urre8y.fsf@ngyro.com \
    --to=samplet@ngyro.com \
    --cc=guile-user@gnu.org \
    --cc=luhux@outlook.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).