Discussion:
[gambit-list] Process stdout redirection
C K Kashyap
2018-03-20 14:28:41 UTC
Permalink
Hello all,

I was hoping for this program to redirect the output of the forked process
into a file -

(define program
(list path: "ls" stdout-redirection: #f arguments: (list "-l")))

(with-output-to-file
(list path: "output.txt")
(lambda ()
(open-process program)))

But that does not seem to happen. I am not sure what I am missing here.

Regards,
Kashyap
Marc Feeley
2018-03-20 15:30:13 UTC
Permalink
When stdout-redirection: is #f, the output of the process will go to the POSIX stdout.

When it is #t, the output of the process will go to the process port, and you can read from that port to get the output.

For example:

(define program
(list path: "ls" stdout-redirection: #t arguments: (list "-l")))

(pp
(call-with-input-process
program
(lambda (port)
(read-line port #f))))

Marc
Post by C K Kashyap
Hello all,
I was hoping for this program to redirect the output of the forked process into a file -
(define program
(list path: "ls" stdout-redirection: #f arguments: (list "-l")))
(with-output-to-file
(list path: "output.txt")
(lambda ()
(open-process program)))
But that does not seem to happen. I am not sure what I am missing here.
Regards,
Kashyap
_______________________________________________
Gambit-list mailing list
https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
C K Kashyap
2018-03-20 17:54:33 UTC
Permalink
Got it - so if I have to redirect to a file, I could do this -
(with-output-to-file "output.txt"
(lambda ()
(display
(call-with-input-process
program
(lambda (port)
(read-line port #f))))))


Although, I'd like to understand why stdout-redirection: #f outputs to
stdout and not the current output port. Is that a problem?

Regards,
Kashyap
Post by Marc Feeley
When stdout-redirection: is #f, the output of the process will go to the POSIX stdout.
When it is #t, the output of the process will go to the process port, and
you can read from that port to get the output.
(define program
(list path: "ls" stdout-redirection: #t arguments: (list "-l")))
(pp
(call-with-input-process
program
(lambda (port)
(read-line port #f))))
Marc
Post by C K Kashyap
Hello all,
I was hoping for this program to redirect the output of the forked
process into a file -
Post by C K Kashyap
(define program
(list path: "ls" stdout-redirection: #f arguments: (list "-l")))
(with-output-to-file
(list path: "output.txt")
(lambda ()
(open-process program)))
But that does not seem to happen. I am not sure what I am missing here.
Regards,
Kashyap
_______________________________________________
Gambit-list mailing list
https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
Adam
2018-03-21 00:26:42 UTC
Permalink
Post by C K Kashyap
Got it - so if I have to redirect to a file, I could do this -
(with-output-to-file "output.txt"
(lambda ()
(display
(call-with-input-process
program
(lambda (port)
(read-line port #f))))))
Although, I'd like to understand why stdout-redirection: #f outputs to
stdout and not the current output port.
Stdout is the current output port of the host OS environment, so this
argument specifies that Gambit should leave it untouched.

Is that a problem?
That depends on you.
C K Kashyap
2018-03-21 01:03:22 UTC
Permalink
Thanks Adam,

I was wondering then, what would be a better way to do this -
(with-output-to-file "output.txt"
(lambda ()
(display
(call-with-input-process
program
(lambda (port)
(read-line port #f))))))

If I understand it right, this requires an extra read and write. If Gambit
lets the port be overridden by the surrounding with-output-to-file, perhaps
the whole thing could be achieved without the extra read and write. Hence
my question was more about whether there is a problem with this approach
that I am not able to see.

Regards,
Kashyap
Post by Adam
Post by C K Kashyap
Got it - so if I have to redirect to a file, I could do this -
(with-output-to-file "output.txt"
(lambda ()
(display
(call-with-input-process
program
(lambda (port)
(read-line port #f))))))
Although, I'd like to understand why stdout-redirection: #f outputs to
stdout and not the current output port.
Stdout is the current output port of the host OS environment, so this
argument specifies that Gambit should leave it untouched.
Is that a problem?
That depends on you.
Adam
2018-03-21 01:08:26 UTC
Permalink
Post by C K Kashyap
Thanks Adam,
I was wondering then, what would be a better way to do this -
(with-output-to-file "output.txt"
(lambda ()
(display
(call-with-input-process
program
(lambda (port)
(read-line port #f))))))
If I understand it right, this requires an extra read and write. If Gambit
lets the port be overridden by the surrounding with-output-to-file, perhaps
the whole thing could be achieved without the extra read and write. Hence
my question was more about whether there is a problem with this approach
that I am not able to see.
If you're trying to achieve "/bin/sh -c \"program > output.txt\"", then
first that's not what you're doing here, and second, you would benefit of
using read/write-subu8vector for the forwarding as it's faster. Or you
could ask the OS to do it using |shell-command|.

You need to clarify your problem specification and code example for anyone
to be able to give you more guidance.

Loading...