unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Questions about (open-input-output-pipe
@ 2020-11-10  8:29 luhux
  2020-11-10 14:27 ` Neil Jerram
  0 siblings, 1 reply; 3+ messages in thread
From: luhux @ 2020-11-10  8:29 UTC (permalink / raw)
  To: guile-user

Hello everyone

I am a newbie to guile and I am learning to use guile to write scripts

I want to implement this shell command:


wg pubkey < private > public



This is a command to generate a key,
It inputs private key and EOF from stdin, then it outputs the public key and exits.

I implemented it using the following code in guile:



(use-modules (ice-9 popen)
             (ice-9 rdelim))

(define (wg-gen-private-key)
  (let* ((port (open-input-pipe "wg genkey"))
	(result (read-line port)))
    (close-pipe port)
    result))

(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)))


Then I executed the code to get the public key




scheme@(guile-user)> (wg-gen-public-key (wg-gen-private-key))




but the code got stuck in this place:




    (let ((result (read-line port)))




How should I do?

There are too few Guile information on the Internet. Sorry for asking such a basic question.

luhux



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Questions about (open-input-output-pipe
  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
  0 siblings, 1 reply; 3+ messages in thread
From: Neil Jerram @ 2020-11-10 14:27 UTC (permalink / raw)
  To: luhux; +Cc: guile-user

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.

Alternatively, it might be buffering, i.e. the private key hasn't yet
reached "wg pubkey".  In that case (force-output port) might help.

Best wishes,
    Neil


On Tue, 10 Nov 2020 at 08:51, luhux <luhux@outlook.com> wrote:

> Hello everyone
>
> I am a newbie to guile and I am learning to use guile to write scripts
>
> I want to implement this shell command:
>
>
> wg pubkey < private > public
>
>
>
> This is a command to generate a key,
> It inputs private key and EOF from stdin, then it outputs the public key
> and exits.
>
> I implemented it using the following code in guile:
>
>
>
> (use-modules (ice-9 popen)
>              (ice-9 rdelim))
>
> (define (wg-gen-private-key)
>   (let* ((port (open-input-pipe "wg genkey"))
>         (result (read-line port)))
>     (close-pipe port)
>     result))
>
> (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)))
>
>
> Then I executed the code to get the public key
>
>
>
>
> scheme@(guile-user)> (wg-gen-public-key (wg-gen-private-key))
>
>
>
>
> but the code got stuck in this place:
>
>
>
>
>     (let ((result (read-line port)))
>
>
>
>
> How should I do?
>
> There are too few Guile information on the Internet. Sorry for asking such
> a basic question.
>
> luhux
>
>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Questions about (open-input-output-pipe
  2020-11-10 14:27 ` Neil Jerram
@ 2020-11-11 17:32   ` Timothy Sample
  0 siblings, 0 replies; 3+ messages in thread
From: Timothy Sample @ 2020-11-11 17:32 UTC (permalink / raw)
  To: luhux; +Cc: guile-user

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



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-11-11 17:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 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).