unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Question about an error with ports
@ 2022-03-10  1:26 Zelphir Kaltstahl
  2022-03-10 15:05 ` Olivier Dion via General Guile related discussions
  0 siblings, 1 reply; 15+ messages in thread
From: Zelphir Kaltstahl @ 2022-03-10  1:26 UTC (permalink / raw)
  To: guile-user

Hello Guile Users!

I have some code at 
https://notabug.org/ZelphirKaltstahl/guile-examples/src/63af5250aa5a45d68bc6bd2d6193d2a6fb127f24/shell/example-04-using-popen-get-out-and-error-with-ports.scm:

~~~~
(import (ice-9 popen)
         (ice-9 textual-ports)
         (ice-9 binary-ports)
         (ice-9 exceptions)
         (ice-9 receive)
         (ice-9 match))


;; Is this useful at all? Or is there a better way? Maybe
;; anything using the file descriptors directly?
(define read-from-write-to
   (lambda* (in-port out-port #:key (bytes-count 1024))
     "Read from an IN-PORT and write to OUT-PORT,
     BYTES-COUNT bytes at a time."
     (let loop ([bv (get-bytevector-n in-port bytes-count)])
       (cond
        [(eof-object? bv)
         (close-port out-port)]
        [else
         (put-bytevector out-port bv)]))))


;; Trying to allow the user to give output port and error
;; port to the function. But how to elegantly call it then?
(define run-command
   (lambda* (cmd
             #:key
             (cmd-out-port (current-output-port))
             (err-out-port (current-error-port)))
     (match-let ([(err-read . err-write) (pipe)]
                 [stderr (current-error-port)])
       (with-error-to-port err-write
         (λ ()
           (let* (;; Run the actual command. If an error
                  ;; happens, it should write to the
                  ;; err-write port. Output of the command
                  ;; should be written to an output port,
                  ;; which corresponds to the input-port,
                  ;; which is returned by open-input-pipe.
                  [in-port (open-input-pipe cmd)]
                  ;; Read in block mode.
                  [_ignored (setvbuf in-port 'block)])
             ;; Write to caller given command output port.
             (read-from-write-to in-port cmd-out-port)
             ;; Close the port, to which the child process
             ;; was to write errors, as the child process has
             ;; finished (either successfully or
             ;; unsuccessfully, but definitely
             ;; finished). Close the error output port,
             ;; before reading from the corresponding error
             ;; read port.
             (close-port err-write)
             ;; Write to caller given error output port.
             (read-from-write-to err-read err-out-port)
             ;; Get the exit code of the command.
             (close-pipe in-port)))))))

;; Why does this exit? I guess because of writing something
;; to the current error port and that means "an error happened"?
;; But why do I get an error and what does the error tell me?
(run-command "echo 'bong' 1>&2")
~~~~

I have the following questions:

(1) Is the read-from-write-to procedure useful at all, or is there a better way 
to get all stuff from an input port and output it to an output port, avoiding 
possibly large string values?

(2) Why do I get an error and what does the error mean? And why does it quit the 
REPL entirely, instead of giving me that [1] thingy, indicating, that an error 
happened, but letting me continue with the REPL session?

(3) How would I elegantly call a function, which allows to optionally specify an 
output port and an error port, specifying both?

Best regards,
Zelphir

-- 
repositories: https://notabug.org/ZelphirKaltstahl




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

end of thread, other threads:[~2022-03-12  0:27 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-10  1:26 Question about an error with ports Zelphir Kaltstahl
2022-03-10 15:05 ` Olivier Dion via General Guile related discussions
2022-03-10 22:32   ` Zelphir Kaltstahl
2022-03-10 23:46     ` Olivier Dion via General Guile related discussions
2022-03-11 12:05       ` Chris Vine
2022-03-11 14:58         ` Olivier Dion via General Guile related discussions
2022-03-11 17:26           ` Chris Vine
2022-03-11 17:47             ` Olivier Dion via General Guile related discussions
2022-03-11 18:13             ` Chris Vine
2022-03-11 19:48               ` Maxime Devos
2022-03-11 19:49               ` Maxime Devos
2022-03-12  0:10                 ` Chris Vine
2022-03-12  0:27                   ` Chris Vine
2022-03-11 12:11       ` Maxime Devos
2022-03-11 15:06         ` Olivier Dion via General Guile related discussions

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