[Openmcl-devel] Using OpenMCL as a shell scripting interpreter

Frank Sonnemans frank.sonnemans at unitekeapro.com
Wed Mar 31 08:06:41 PST 2004

I had a similar request to use openmcl for shell scripts several
months ago. The idea was to use something like:

openmcl scriptfile args

In which openmcl would call a main function like in C. Gary send me the
following code which I dumped into an image.

;---- file "filter-app.lisp"
(in-package "CCL")  ; Too many of these things are internal symbols in CCL.

;;; A FILTER-APP inherits from APPLICATION, but not from
(defclass filter-app (application) ())

;;; We'll handle the argument list by hand.  It may not even be necessary
;;; to specialize this method, but it can't hurt.
(defmethod parse-application-arguments ((app filter-app))
  (values nil nil nil))

;;; We don't have any sort of specialized init file.
(defmethod application-init-file ((app filter-app))

;;; All unhandled errors are fatal: we don't want to see a break loop.
(defmethod application-error ((app filter-app) cond frame)
  (declare (ignore frame))
  (format t "~&Fatal error: ~a~&" cond)
  (force-output)                        ; ensure user sees the error
  (quit -1))

;;; Define the application class's toplevel function.
(defmethod toplevel-function ((app filter-app) init-file)
  (declare (ignore init-file))
  ;; pathname of the kernel.
  ;; Assume that the CADR is the name of a loadable file
  ;; which defines CL-USER::MAIN.  After loading the
  ;; file, try to call CL-USER::MAIN on the CDDR of the
  ;; argument-list.
  (%set-toplevel nil)
  (fmakunbound 'cl-user::main)
  (load (cadr ccl::*command-line-argument-list*))
  (apply #'cl-user::main (cddr ccl::*command-line-argument-list*)))

;---- end of filter-app.lisp

I call openmcl with the appropriate image using a script:

openmcl -I ~/bin/filter-app.image "$@"

# end of script

And a simple test file:

(in-package "CL-USER")

(defun print-arguments (lst)
  (when (consp lst)
    (format t "~%~a" (car lst))
    (print-arguments (rest lst))))

(defun main (&rest arguments)
  (format t "Testing filter mode openmcl")
  (print-arguments arguments)
  (format t "~%All done~%~%")

Works very well.



More information about the Openmcl-devel mailing list