This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Pipe program output to stream

7 posts · 8463 views

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

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/

Re: Pipe program output to stream

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

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

Re: Pipe program output to stream

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

You do something wrong. This works for me:
(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

:) Cool. Of course, if you try this on Windows you get the following:
[...]
* (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
*