Page 1 of 1

Pipe program output to stream

Posted: Thu Aug 28, 2008 12:05 pm
by makia
Something like perl -> open STREAM, "program |"
How can i do something like this in common lisp
btw. i can collect output in string and work on it but after program finishes, i need to read output while program is running
Is there something portable to do this ? If not then sbcl specific.
Thanks

Re: Pipe program output to stream

Posted: Thu Aug 28, 2008 2:05 pm
by findinglisp
There is nothing standard that would allow you to do what you want. Most operating-system sorts of things are handled by libraries and extensions outside of the Lisp standard.

You should read the SBCL manual, though. For example, this should do what you want:
http://www.sbcl.org/manual/Running-exte ... l-programs

There are similar extensions for CLISP, etc.

Re: Pipe program output to stream

Posted: Sat Aug 30, 2008 4:04 am
by makia
Yes, something like this works
Thanks

(let ((pty (process-pty (run-program "external-program" '("arg1") :search t :wait nil :pty t))))
(loop for line = (read-line pty nil nil)
while line do
(print line)))

Re: Pipe program output to stream

Posted: Sat Aug 30, 2008 9:27 am
by Wodin
I would have thought you would want :output :stream rather than :pty t.

Re: Pipe program output to stream

Posted: Sat Aug 30, 2008 9:51 am
by makia
hmm, looks like process-output slot can be read only when process finishes ... so it does not do what i want

Re: Pipe program output to stream

Posted: Sun Aug 31, 2008 1:22 pm
by anton
You do something wrong. This works for me:

Code: Select all


(setq proc (sb-ext:run-program #+win32"C:\\Windows\\System32\\cmd.exe"
                               #-win32 "/bin/bash"
                               nil :input :stream
                               :output :stream :wait nil))

(princ "echo hello" (sb-ext:process-input proc))
(princ #\Newline (sb-ext:process-input proc))
(finish-output (sb-ext:process-input proc))

(read-line (sb-ext:process-output proc))
   => "hello"

(princ "echo hello again" (sb-ext:process-input proc))
(princ #\Newline (sb-ext:process-input proc))
(finish-output (sb-ext:process-input proc))

(read-line (sb-ext:process-output proc))
  => "hello again"

Re: Pipe program output to stream

Posted: Sun Aug 31, 2008 2:50 pm
by Wodin
:) Cool. Of course, if you try this on Windows you get the following:

Code: Select all

[...]
* (read-line (sb-ext:process-output proc))

"Microsoft Windows XP [Version 5.1.2600]
NIL
* (read-line (sb-ext:process-output proc))

"(C) Copyright 1985-2001 Microsoft Corp.
NIL
* (read-line (sb-ext:process-output proc))

"
NIL
* (read-line (sb-ext:process-output proc))

"C:\\>echo hello"
NIL
* (read-line (sb-ext:process-output proc))

"hello
NIL
* (read-line (sb-ext:process-output proc))

"
NIL
*