guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Questions about (open-input-output-pipe


From: Timothy Sample
Subject: Re: Questions about (open-input-output-pipe
Date: Wed, 11 Nov 2020 12:32:13 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)

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 via email to

[Prev in Thread] Current Thread [Next in Thread]