Pipe program output to stream

Discussion of Common Lisp
Post Reply
makia
Posts: 25
Joined: Tue Jul 22, 2008 2:37 am

Pipe program output to stream

Post by makia » Thu Aug 28, 2008 12:05 pm

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

findinglisp
Posts: 447
Joined: Sat Jun 28, 2008 7:49 am
Location: Austin, TX
Contact:

Re: Pipe program output to stream

Post by findinglisp » Thu Aug 28, 2008 2:05 pm

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.
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/

makia
Posts: 25
Joined: Tue Jul 22, 2008 2:37 am

Re: Pipe program output to stream

Post by makia » Sat Aug 30, 2008 4:04 am

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

Wodin
Posts: 56
Joined: Sun Jun 29, 2008 8:16 am

Re: Pipe program output to stream

Post by Wodin » Sat Aug 30, 2008 9:27 am

I would have thought you would want :output :stream rather than :pty t.

makia
Posts: 25
Joined: Tue Jul 22, 2008 2:37 am

Re: Pipe program output to stream

Post by makia » Sat Aug 30, 2008 9:51 am

hmm, looks like process-output slot can be read only when process finishes ... so it does not do what i want

anton
Posts: 5
Joined: Tue Jul 15, 2008 1:45 am

Re: Pipe program output to stream

Post by anton » Sun Aug 31, 2008 1:22 pm

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"

Wodin
Posts: 56
Joined: Sun Jun 29, 2008 8:16 am

Re: Pipe program output to stream

Post by Wodin » Sun Aug 31, 2008 2:50 pm

:) 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
*

Post Reply