From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Ricardo Wurmus Newsgroups: gmane.lisp.guile.bugs Subject: bug#43364: with-output-to-port works with file ports Date: Fri, 08 Sep 2023 21:06:11 +0200 Message-ID: <877cp05vqp.fsf@elephly.net> References: <20200912225923.5fa9504e@airmail.cc> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="40245"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.10.5; emacs 28.2 Cc: 43364@debbugs.gnu.org, pinoaffe@airmail.cc To: Felix Lechner Original-X-From: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Fri Sep 08 21:09:19 2023 Return-path: Envelope-to: guile-bugs@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 1qegqz-000ABq-Ry for guile-bugs@m.gmane-mx.org; Fri, 08 Sep 2023 21:09:19 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qegql-0007aq-0m; Fri, 08 Sep 2023 15:09:03 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qegqi-0007a5-9Z for bug-guile@gnu.org; Fri, 08 Sep 2023 15:09:00 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qegqi-0000lB-0b for bug-guile@gnu.org; Fri, 08 Sep 2023 15:09:00 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qegqk-0002Ym-Ci for bug-guile@gnu.org; Fri, 08 Sep 2023 15:09:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Ricardo Wurmus Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Fri, 08 Sep 2023 19:09:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 43364 X-GNU-PR-Package: guile X-Debbugs-Original-Cc: bug-guile@gnu.org, pinoaffe , 43364@debbugs.gnu.org Original-Received: via spool by 43364-submit@debbugs.gnu.org id=B43364.16942001259796 (code B ref 43364); Fri, 08 Sep 2023 19:09:02 +0000 Original-Received: (at 43364) by debbugs.gnu.org; 8 Sep 2023 19:08:45 +0000 Original-Received: from localhost ([127.0.0.1]:45630 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qegqT-0002Xw-8R for submit@debbugs.gnu.org; Fri, 08 Sep 2023 15:08:45 -0400 Original-Received: from sender3-of-o57.zoho.com ([136.143.184.57]:21784) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qegqQ-0002Xl-4L for 43364@debbugs.gnu.org; Fri, 08 Sep 2023 15:08:43 -0400 ARC-Seal: i=1; a=rsa-sha256; t=1694200084; cv=none; d=zohomail.com; s=zohoarc; b=Mz4MGjFQbIA32gRKsZqmz59ldR9GA7DVd6yQMCu515uwym6higLmrrR/uL5uFtrYwX9ntYQi4p2H5Ic5g8yQVpqF+hDh0pPRMIKtYImfOsH7ewysjbgNMnIEXGPHOApq8Pdsg6iBdlRZIaX7MZOpEGtk/T24WKV70vEbhc/W7PQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1694200084; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=WNfGH6wNa8v7n8Ksqe3uL59SKjJHJaCantlv8eRkswI=; b=NZaki3LekS4G1GwHUcwIoKzT9lGvCY82Hl6uGw9UpN+XHvJAkPcNGiCr3A9WAQsSNQXgO/BzBTz5kuL/S3DvUKI/wkGRx4e3a6AmsvEAqnUOX1BG0OEfJq7/jBjfnNATw6I08P841unKCC2PnpAktVUZEvbQWWZy36kpjOtcr7g= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=elephly.net; spf=pass smtp.mailfrom=rekado@elephly.net; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1694200084; s=zoho; d=elephly.net; i=rekado@elephly.net; h=References:From:From:To:To:Cc:Cc:Subject:Subject:Date:Date:In-reply-to:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Reply-To; bh=WNfGH6wNa8v7n8Ksqe3uL59SKjJHJaCantlv8eRkswI=; b=GK/SkiZcKOq/ABqA0TLQKBHRYqnKUOaPt0qF8PAwQyUR0qHyudEEtstuTU07CxLI /Kmu2rtBjHKNl1V8RMn0eWUpEPVJJQJpOMpy+O3WYODkkhHgH8NMyrkLrzMiA9xCAGV lwj6bFhKlz0ZPI/jXF2zcVun4PClivDXf2dGCZLw= Original-Received: from localhost (133-122-142-46.pool.kielnet.net [46.142.122.133]) by mx.zohomail.com with SMTPS id 1694200082434876.6715577442903; Fri, 8 Sep 2023 12:08:02 -0700 (PDT) In-reply-to: X-ZohoMailClient: External X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Original-Sender: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.bugs:10659 Archived-At: Felix Lechner via "Bug reports for GUILE, GNU's Ubiquitous Extension Langua= ge" writes: > Hi, > > In an interesting (or perhaps maddening) inconsistency, > 'with-output-to-port' captures stdout from system* here > > (call-with-output-file "/tmp/test.log" > (lambda (port) > (with-output-to-port > port > (lambda () > (system* "mktemp" "-d"))))) > > but 'with-output-to-string' does not do so here > > (with-output-to-string > (lambda () > (system* "mktemp" "-d"))) > > According to lloda on #guile, system* handles the redirection only > when the current ports are file ports. Thanks for that pointer! That=E2=80=99s correct. I=E2=80=99ve been using the following monstrosity = to capture and process output. Perhaps someone finds a prettier way? --8<---------------cut here---------------start------------->8--- (define* (call-with-output-processor command proc #:optional capture-stderr= ?) "Silently execute COMMAND, a list of strings representing an executable with its arguments, and apply PROC to every line printed to standard output and, optionally when CAPTURE-STDERR? is #T, standard error. Return the exit status of COMMAND." ;; We can only capture a program's standard error by parameterizing ;; current-error-port to a *file* port before using system* or ;; open-pipe*. The process will write its standard error stream to ;; the provided file descriptor. Meanwhile we read from the file ;; descriptor (blocking) for new lines until the process exits. (match (socketpair PF_UNIX SOCK_STREAM 0) ((in . out) (let ((err (if capture-stderr? (dup out) (%make-void-port "w")))) (catch #true (lambda () (let ((thread (parameterize ((current-error-port err) (current-output-port out)) (call-with-new-thread (lambda () (let ((status (status:exit-val (apply system* command)))) (close-port err) (close-port out) status)))))) (let loop () (match (read-line in 'concat) ((? eof-object?) (for-each (lambda (port) (false-if-exception (close-port port))) (list err out in)) (join-thread thread)) (line (proc line) (loop)))))) (lambda (key . args) (for-each (lambda (port) (false-if-exception (close-port port))) (list err out in)) (apply throw key args))))))) --8<---------------cut here---------------end--------------->8--- --=20 Ricardo