From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Zelphir Kaltstahl Newsgroups: gmane.lisp.guile.user Subject: Question about an error with ports Date: Thu, 10 Mar 2022 01:26:51 +0000 Message-ID: <3f938fe1-9af4-9076-8d50-d1f7b2ddda36@posteo.de> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="10049"; mail-complaints-to="usenet@ciao.gmane.io" To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Thu Mar 10 02:27:21 2022 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nS7aJ-0002Or-WF for guile-user@m.gmane-mx.org; Thu, 10 Mar 2022 02:27:20 +0100 Original-Received: from localhost ([::1]:48470 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nS7aI-0000hL-GU for guile-user@m.gmane-mx.org; Wed, 09 Mar 2022 20:27:18 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:57246) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nS7Zy-0000gw-OY for guile-user@gnu.org; Wed, 09 Mar 2022 20:26:59 -0500 Original-Received: from mout02.posteo.de ([185.67.36.66]:41709) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nS7Zw-0002fe-6f for guile-user@gnu.org; Wed, 09 Mar 2022 20:26:58 -0500 Original-Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id CCD47240101 for ; Thu, 10 Mar 2022 02:26:52 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1646875612; bh=MotHcceKoRCSz/pW0wJHCv34hqq8GRt6OlliuA555LA=; h=Date:To:From:Subject:From; b=ipFa6RPjJpDb6TUWAPY1hwhbNrZfyJbAKVzOeBZfAQ8Qd/wbPsknnuJmsx4dmvX+/ 4AOs+NZMXxu90vWZe4x5H+QdO7TQmuwv7KcHrOdGLqL87qMGt/70ZXdhz60wuOBEvw FzoBEsmCEjOPtAOO9td8VVDbXd+mwsDb9JTFB6Zce1vKq+lEmfb8aaCUVZnnHzH4m7 UFqeYonIur26PUM5ILLiC/BmgHHdhRVt34mFPqIoEtRcAacx9akYwDvEkc/fVlcYty 4I/Et1rByBV91SYcNZzZCucwO/VlbvnMfaCM+KYnPS56cmnMkMTnujbILMMB72ekPl PdAW7p5C4k6oQ== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4KDWcr0x1wz6tm6 for ; Thu, 10 Mar 2022 02:26:51 +0100 (CET) Content-Language: en-US Received-SPF: pass client-ip=185.67.36.66; envelope-from=zelphirkaltstahl@posteo.de; helo=mout02.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:18178 Archived-At: 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