Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does there exist standard way to run external program in Common Lisp?

In clisp, the following code works:

(defun hit-history () (shell "tail ssqHitNum.txt"))

However, in Clozure CL, the shell function is not supported!

like image 927
z_axis Avatar asked Oct 11 '11 03:10

z_axis


2 Answers

No, there is no standard way, but there are libraries which provide this functionality for the important implementations. For example, there's trivial-shell available in Quicklisp, which provides shell-command. (I didn't actually test it, but its among the recommended libraries on CLiki.) There is also external-program. Update: inferior-shell seems to be prefered these days, as Ehvince points out in a comment and his own answer.

You could also use read-time conditionals to make different implementations use their respective functionality to do this.

CCL has ccl:run-program, for example:

CL-USER> (run-program "whoami" '() :output *standard-output*)
foobar
#<EXTERNAL-PROCESS (whoami)[NIL] (EXITED : 0) #xC695EA6>
like image 170
danlei Avatar answered Oct 12 '22 12:10

danlei


Yes, with UIOP, part of ASDF, that should be included in all modern implementations.

  • synchronous commands: uiop:run-program
  • asynchronous commands: uiop:launch-program

So for example

(uiop:run-program (list "firefox" "http:url") :output t)

or

(defparameter *shell* (uiop:launch-program "bash" :input :stream :output :stream))

where you can send input and read output.

They are more explained here: https://lispcookbook.github.io/cl-cookbook/os.html#running-external-programs

trivial-shell is deprecated and replaced by inferior-shell, which internally uses the portable uiop's run-program (synchronous), so we can use just that.

like image 20
Ehvince Avatar answered Oct 12 '22 13:10

Ehvince