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