Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running shell commands with gnu clisp

Tags:

stream

lisp

clisp

I'm trying to create a "system" command for clisp that works like this

(setq result (system "pwd"))

;;now result is equal to /my/path/here

I have something like this:

(defun system (cmd)
 (ext:run-program :output :stream))

But, I am not sure how to transform a stream into a string. I've reviewed the hyperspec and google more than a few times.

edit: working with Ranier's command and using with-output-to-stream,

(defun system (cmd)
  (with-output-to-string (stream)
    (ext:run-program cmd :output stream)))

And then trying to run grep, which is in my path...

[11]> (system "grep")

*** - STRING: argument #<OUTPUT STRING-OUTPUT-STREAM> should be a string, a
      symbol or a character
The following restarts are available:
USE-VALUE      :R1      Input a value to be used instead.
ABORT          :R2      Abort main loop
Break 1 [12]> :r2
like image 270
Paul Nathan Avatar asked Dec 29 '22 14:12

Paul Nathan


1 Answers

Something like this?

Version 2:

(defun copy-stream (in out)
   (loop for line = (read-line in nil nil)
         while line
         do (write-line line out)))

(defun system (cmd)
  (with-open-stream (s1 (ext:run-program cmd :output :stream))
    (with-output-to-string (out)
      (copy-stream s1 out))))


[6]> (system "ls")
"#.emacs#
Applications
..."
like image 167
Rainer Joswig Avatar answered Dec 31 '22 04:12

Rainer Joswig