guile-user
[Top][All Lists]
Advanced

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

How do you record output of both STDERR and STDIN to a string?


From: Bonface M. K.
Subject: How do you record output of both STDERR and STDIN to a string?
Date: Thu, 10 Sep 2020 04:14:57 +0300
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)

Hi all. Is there a way to "record" the output from both *stderr* and
*stdout* from a guile process if you wrap it around a "system" call to a
string? Here's something that works. It's not /exactly/ what I want
because "open-input-pipe" runs the command in a subprocess.

--8<---------------cut here---------------start------------->8---
(define (run-job job)
  (let* ((port (open-input-pipe job))
         (str (read-line port)))
    (close-pipe port)
    str))

(display (format #t "~s ~s ~s"
                 "padding"
                 (run-job "echo hello")
                 "testing"))
--8<---------------cut here---------------end--------------->8---

I've tried out creating a fork:

--8<---------------cut here---------------start------------->8---
(define (run-job thunk)
  (call-with-output-string
    (λ (port)
      (match (pipe)
        ((in . out)
         (match (primitive-fork)
           (0 ; child
            (close in)
            (with-error-to-port out thunk))
           ((= waitpid (pid . exit-code)) ;; parent
            (close out)
            (display (read-line in) port))))))))

;; Doesn't work:
(display (format #t "~s ~s ~s"
                 "padding"
                 (run-job (system "echo hello"))
                 "testing"))
--8<---------------cut here---------------end--------------->8---

Ideally for a correct output without errors, I'd like to have as output:

--8<---------------cut here---------------start------------->8---
 "padding" "hello" "testing"
--8<---------------cut here---------------end--------------->8---

and in the event I have an error, like say, by running (system "echoooo 
hello"), I get
the output:

--8<---------------cut here---------------start------------->8---
"padding" "sh: command not found" "testing""
--8<---------------cut here---------------end--------------->8---

PS: I'm new to Guile :)

-- 
Bonface M. K. (https://www.bonfacemunyoki.com)
One Divine Emacs To Rule Them All
GPG key = D4F09EB110177E03C28E2FE1F5BBAE1E0392253F

Attachment: signature.asc
Description: PGP signature


reply via email to

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