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

unofficial mirror of guile-user@gnu.org 

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://yhetil.org/guile-user/0 guile-user/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 guile-user guile-user/ https://yhetil.org/guile-user \
		guile-user@gnu.org
	public-inbox-index guile-user

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.yhetil.org/yhetil.lisp.guile.user
	nntp://news.gmane.io/gmane.lisp.guile.user


AGPL code for this site: git clone http://ou63pmih66umazou.onion/public-inbox.git