unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Bug in system?
@ 2012-11-07  2:49 Keith Wright
  2012-11-07  4:30 ` Daniel Hartwig
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Keith Wright @ 2012-11-07  2:49 UTC (permalink / raw)
  To: guile-user


 > (define x (with-output-to-string (lambda()(display  "joy"))))
 > x
  $1 = "joy"

This is as expected.

 > (define y (with-output-to-string (lambda()(system "date"))))
  Tue Nov  6 21:42:41 EST 2012
 > y
  $2 = ""

The stdout of the system call does not go into the string,
why not?

-- 
     -- Keith Wright  <kwright@free-comp-shop.com>

Programmer in Chief, Free Computer Shop <http://www.free-comp-shop.com>
         ---  Food, Shelter, Source code.  ---



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

* Re: Bug in system?
  2012-11-07  2:49 Bug in system? Keith Wright
@ 2012-11-07  4:30 ` Daniel Hartwig
  2012-11-07  4:34   ` Daniel Hartwig
  2012-11-07 15:06 ` Ludovic Courtès
  2012-11-07 18:15 ` Alexei Matveev
  2 siblings, 1 reply; 5+ messages in thread
From: Daniel Hartwig @ 2012-11-07  4:30 UTC (permalink / raw)
  To: Keith Wright; +Cc: guile-user

On 7 November 2012 10:49, Keith Wright <kwright@keithdiane.us> wrote:
>  > (define y (with-output-to-string (lambda()(system "date"))))
>   Tue Nov  6 21:42:41 EST 2012
>  > y
>   $2 = ""
>
> The stdout of the system call does not go into the string,
> why not?

with-output-to-string can not capture stdout, only output sent to real
scheme ports (via "display", for example).



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

* Re: Bug in system?
  2012-11-07  4:30 ` Daniel Hartwig
@ 2012-11-07  4:34   ` Daniel Hartwig
  0 siblings, 0 replies; 5+ messages in thread
From: Daniel Hartwig @ 2012-11-07  4:34 UTC (permalink / raw)
  To: Keith Wright; +Cc: guile-user

On 7 November 2012 12:30, Daniel Hartwig <mandyke@gmail.com> wrote:
> On 7 November 2012 10:49, Keith Wright <kwright@keithdiane.us> wrote:
>>  > (define y (with-output-to-string (lambda()(system "date"))))
>>   Tue Nov  6 21:42:41 EST 2012
>>  > y
>>   $2 = ""
>>
>> The stdout of the system call does not go into the string,
>> why not?
>
> with-output-to-string can not capture stdout, only output sent to real
> scheme ports (via "display", for example).

Discussed once or twice within the last few months.  See
<http://permalink.gmane.org/gmane.lisp.guile.user/9507>.



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

* Re: Bug in system?
  2012-11-07  2:49 Bug in system? Keith Wright
  2012-11-07  4:30 ` Daniel Hartwig
@ 2012-11-07 15:06 ` Ludovic Courtès
  2012-11-07 18:15 ` Alexei Matveev
  2 siblings, 0 replies; 5+ messages in thread
From: Ludovic Courtès @ 2012-11-07 15:06 UTC (permalink / raw)
  To: guile-user

Hi,

Keith Wright <kwright@keithdiane.us> skribis:

>  > (define y (with-output-to-string (lambda()(system "date"))))
>   Tue Nov  6 21:42:41 EST 2012
>  > y
>   $2 = ""
>
> The stdout of the system call does not go into the string,
> why not?

As Daniel said, this doesn’t work currently.

Instead, you would use the (ice-9 popen) module for that:

  scheme@(guile-user)> (use-modules (ice-9 popen) (ice-9 rdelim))
  scheme@(guile-user)> (define p (open-input-pipe "date"))
  scheme@(guile-user)> (read-line p)
  $7 = "Wed Nov  7 16:05:02 CET 2012"
  scheme@(guile-user)> (close-pipe p)
  $8 = 0

Ludo’.




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

* Re: Bug in system?
  2012-11-07  2:49 Bug in system? Keith Wright
  2012-11-07  4:30 ` Daniel Hartwig
  2012-11-07 15:06 ` Ludovic Courtès
@ 2012-11-07 18:15 ` Alexei Matveev
  2 siblings, 0 replies; 5+ messages in thread
From: Alexei Matveev @ 2012-11-07 18:15 UTC (permalink / raw)
  To: Keith Wright, guile-user

[-- Attachment #1: Type: text/plain, Size: 1080 bytes --]

On 7 November 2012 03:49, Keith Wright <kwright@keithdiane.us> wrote:

>
>
>  > (define y (with-output-to-string (lambda()(system "date"))))
>   Tue Nov  6 21:42:41 EST 2012
>  > y
>   $2 = ""
>
> The stdout of the system call does not go into the string,
> why not?
>

For the same reason you cannot capture stdout of, say,
printf() in the native code to a file using

  with-output-to-file

I am not qualified to judge if these are valid reasons, though.
I used the code below to achieve that.

Alexei

;;;
;;; Beware that writing to the  same file from multiple workers is not
;;; going to end good:
;;;
(define (with-stdout-to-file file proc)
  (with-output-to-file file
    (lambda ()
      (let ((dup-1 (dup 1))         ; make a dup of the current stdout
            (fd (fileno (current-output-port))))
        (dup2 fd 1)     ; close stdout and redirect it to fd
        (proc)          ; execute thunk with stdout redirected to file
        (dup2 dup-1 1)  ; close file, stdout to original destination
        (close dup-1)))))               ; dont leak file descriptors

[-- Attachment #2: Type: text/html, Size: 1481 bytes --]

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

end of thread, other threads:[~2012-11-07 18:15 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-11-07  2:49 Bug in system? Keith Wright
2012-11-07  4:30 ` Daniel Hartwig
2012-11-07  4:34   ` Daniel Hartwig
2012-11-07 15:06 ` Ludovic Courtès
2012-11-07 18:15 ` Alexei Matveev

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