CommonDoc / common-doc

A framework for representing and manipulating documents.
http://commondoc.github.io
40 stars 4 forks source link

uiop:run-program with string input #19

Open epipping opened 7 years ago

epipping commented 7 years ago

Hi,

I was looking at how different projects on quicklisp use uiop:run-program and came across this snippet in common-doc:

(defmethod expand-macro ((plot gnuplot))
  "Take the gnuplot source code from the children and the image name, render it
with gnuplot into an image."
  (let* ((pathname (absolute-path (image-path plot)))
         ;; The gnuplot commands
         (text (text (first (children plot))))
         ;; The gnuplot commands to set output format, file etc.
         (input (format nil "set term pngcairo; set output ~S; ~A~%"
                        (namestring pathname)
                        text))
         ;; The gnuplot command
         (command "gnuplot"))
    ;; Run
    (handler-case
        (progn
          (with-input-from-string (stream input)
            (uiop:run-program command :input stream))
          (make-image (namestring (relativize-pathname pathname))))
      (t ()
        (make-text "gnuplot error.")))))

I wanted to let you know that passing input to uiop:run-program through a stream, especially one that isn't a file stream, is not very portable. But you can also pass strings directly (that's far more portable because it internally uses a file). So e.g.

(uiop:run-program "cat" :input '("hello, world") :output :interactive)

will print "hello, world".