[Openmcl-devel] How to use lisp program in unix pipeline

Gary Byers gb at clozure.com
Tue May 13 05:29:05 PDT 2003



On Tue, 13 May 2003, Frank Sonnemans wrote:

> Gary,
>
> Thanks for the exhaustive reply. From what you write I have another scenario
> in mind. Instead of having many multi megabyte images I can create one
> specialized image which does nothing but load a fasl (or lisp) file which is
> the first commandline parameter, call a sort of main function which gets the
> rest of the commandline options as a parameter and exit. This way openmcl
> would behave like a shell executing a shell script on steroids.
>
> Regards,
>
> Frank

I think so, though as soon as I sent my long message yesterday and saw
Sven Van Caekenber's reply, I wondered if it might be simpler in many
ways to use shell scripts.  Let's see.

Here's an attempt to define an application class that behaves as you
suggest.  One might realistically want a -little- more error handling
than is shown here.

;---- 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
;;; LISP-DEVELOPMENT-SYSTEM.
(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))
   nil)

;;; 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))
  ;; The CAR of CCL::*COMMAND-LINE-ARGUMENT-LIST* is the
  ;; 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

And here's a familiar-looking "application":

;---- file "adder-app.lisp"

(in-package "CL-USER")

;;; Read numbers from *standard-input* until EOF.
;;; Print the result of adding our first argument (default: 1) to
;;; each number.  Don't try to handle errors: this is just a silly
;;; example.
(defun main (&optional (num-string "1") &rest ignore)
  (declare (ignore ignore))
  (let* ((addend (read-from-string num-string))
         (eof (cons nil nil)))
    (check-type addend number)
    (loop
      (format t "~&number: ")
      (let* ((num (read t nil eof)))
        (if (eq num eof) (return))
        (format t "~& sum of ~d and ~d is ~d." num addend (+ num addend))))))

;---- end of "adder-app.lisp"

If we save an image using the new application class (this happens to be
0.14, but I believe this stuff works the same way in 0.13):

;-----
Welcome to OpenMCL Version (Alpha: Darwin) 0.14-030513!
? (load "home:filter-app.lisp")
#P"/Users/gb/filter-app.lisp"
? (save-application "home:filter-app.image" :application-class 'ccl::filter-app)
;-----

We can then invoke the image from the shell, specifying a "main" application
to run and additional arguments:

[~] gb at dervish> openmcl -I filter-app.image adder-app.lisp
number: 3
 sum of 3 and 1 is 4.
number: 4
 sum of 4 and 1 is 5.
number: 0.5
 sum of 0.5 and 1 is 1.5.
number: -1
 sum of -1 and 1 is 0.
number: not-a-number
Fatal error: value NOT-A-NUMBER is not of the expected type NUMBER.
[~] gb at dervish> echo '#c(3.0 2.0)'  | openmcl -I filter-app.image adder-app.lisp 2
number:
 sum of #c(3.0 2.0) and 2 is #c(5.0 2.0).
number:
;-----

I'm not sure whether *ERROR-OUTPUT* is really that distinct from
*STANDARD-OUTPUT*, and for filter-like programs it might be nice to
distinguish them better.

You may also find that it's necessary to FORCE-OUTPUT a bit more
often than you would when writing  "normal" lisp code; this is probably
more true in 0.14 than 0.13.x.

_______________________________________________
Openmcl-devel mailing list
Openmcl-devel at clozure.com
http://clozure.com/cgi-bin/mailman/listinfo/openmcl-devel



More information about the Openmcl-devel mailing list