New command line option -norc. New type of command line option processing.

This commit is contained in:
jjgarcia 2003-11-04 16:12:46 +00:00
parent b828b62780
commit c229b350cf
3 changed files with 99 additions and 67 deletions

View file

@ -1670,6 +1670,13 @@ ECL 0.9d
- New manual page documents the scripting facilities of ECL
(contributed by Julian St.)
* Visible changes:
- New command line options -shell and -norc, which are useful for
scripting. New handling of command line options, is more robust
and allows combining the options -load, -eval, -shell in a number
of ways.
TODO:
=====

View file

@ -56,13 +56,20 @@ The Boehm-Weiser garbage collector.
interactive lisp.
.SH OPTIONS
.TP 1i
.B \-shell " file"
.BI \-shell " file"
Executes the given file and exits, without providing a read-eval-print loop.
If you want to use lisp as a scripting language, you can write
.BR "#!@bindir@/ecl -shell"
on the first line of the file to be executed, and then ECL will be
automatically invoked.
.TP
.B \-norc
Do not try to load the files
.I ~/.ecl
or
.I ~/.eclrc
at startup.
.TP
.B \-dir
Use
.I dir
@ -109,6 +116,15 @@ with .data as extension.
Produce a linkable object file. It cannot be loaded
with load, but it can be used to build libraries
or standalone executable programs.
.PP
The options
.B \-load,
.B \-shell,
and
.B \-eval
may appear any number of times, and they are combined and processed from left
to right.
.SH AUTHORS
The original version was developed by Giuseppe Attardi starting from the Kyoto
Common Lisp implementation by Taiichi Yuasa and Masami Hagiya. The current
@ -117,7 +133,9 @@ mailing list.
.SH FILES
.TP
.BR "~/.ecl, ~/.eclrc"
Default initialization files loaded at startup
Default initialization files loaded at startup unless the option
.BR \-norc
is provided.
(if they exist).
.SH SEE ALSO
.IP ""

View file

@ -340,70 +340,80 @@ value of this variable is non-NIL.")
`(locally (declare (notinline ,(car form)))
,form)))
(defun process-command-args ()
(do ((i 1 (1+ i))
(defun process-command-args (&aux (load-rc t) (commands nil) (quit nil))
#-ecl-min
(do ((load-rc t)
(commands nil)
(quit nil)
(i 1 (1+ i))
(argc (argc)))
((= i argc))
(cond
((string= "-dir" (argv i))
(incf i)
(if (= i argc)
(error "Missing directory")
((>= i argc)
(when load-rc
(dolist (file *lisp-init-file-list*)
(when (load file :if-does-not-exist nil :search-list nil :verbose nil)
(return))))
(eval `(progn ,@(nreverse commands)))
(when quit (quit))
)
(labels
((get-argument (n k default)
(do ((j (1+ n) (1+ j))
(argc (argc)))
((>= j argc) default)
(when (string= k (argv j))
(incf j)
(return
(or (= j argc)
(eql (schar (argv j) 0) #\-)
(let ((arg (argv j)))
(if (string-equal "nil" arg)
NIL
(or (string-equal "t" arg) arg))))))))
(help-message (stream)
(princ "Usage: ecl [-? | --help]
[-dir dir] [-load file] [-shell file] [-eval expr] [-norc]
[-compile file [-o ofile] [-c [cfile]] [-h [hfile]]
[-data [datafile]] [-s]]
"
stream))
(pop-arg (option)
(when (= (incf i) argc)
(format *error-output* "Missing argument to command line option ~A.~%" option)
(help-message *error-output*)
(quit 1))
(argv i)))
(let ((option (argv i)))
(cond
((string= "-dir" option)
(setf (logical-pathname-translations "SYS")
`(("SYS:*.*" ,(concatenate 'string (argv i) "*.*"))))))
((string= "-compile" (argv i))
(incf i)
(if (= i argc)
(error "Missing file name")
(flet ((get-argument (n k default)
(do ((j (1+ n) (1+ j))
(argc (argc)))
((= j argc) default)
(when (string= k (argv j))
(incf j)
(return
(or (= j argc)
(eql (schar (argv j) 0) #\-)
(let ((arg (argv j)))
(if (string-equal "nil" arg)
NIL
(or (string-equal "t" arg) arg)))))))))
(if (nth-value 3
(compile-file
(argv i)
:output-file (get-argument i "-o" T)
:c-file (get-argument i "-c" NIL)
:h-file (get-argument i "-h" NIL)
:data-file (get-argument i "-data" NIL)
:system-p (get-argument i "-s" NIL)))
`(("SYS:*.*" ,(concatenate 'string (pop-arg "-dir") "*.*")))))
((string= "-compile" option)
(if (nth-value 3
(compile-file
(pop-arg "-compile")
:output-file (get-argument i "-o" T)
:c-file (get-argument i "-c" NIL)
:h-file (get-argument i "-h" NIL)
:data-file (get-argument i "-data" NIL)
:system-p (get-argument i "-s" NIL)))
(quit 1)
(quit 0)))))
((string= "-load" (argv i))
(incf i)
(if (= i argc)
(error "Missing file name")
(load (argv i))))
((string= "-shell" (argv i))
(incf i)
#-ecl-min
(let ((*break-enable* nil))
(handler-case
(if (= i argc)
(format t "Missing file name")
(let ((*load-verbose* nil))
(load (argv i))))
(condition (c) (format t "~A" c))))
(quit))
((string= "-eval" (argv i))
(incf i)
(if (= i argc)
(error "Missing file name")
(eval (read-from-string (argv i)))))
(t (format t "Unknown flag ~A
Usage: ecl [-dir dir] [-load file] [-eval expr]
[-compile file [-o ofile] [-c [cfile]] [-h [hfile]] [-data [datafile]] [-s]]"
(argv i))
(quit 1)))))
(quit 0)))
((string= "-load" option)
(push `(load ,(pop-arg "-load") :verbose t) commands))
((string= "-shell" option)
(push `(load ,(pop-arg "-shell") :verbose nil) commands)
(setf quit t load-rc nil))
((string= "-eval" option)
(push (read-from-string (pop-arg "-eval")) commands))
((string= "-norc" option)
(setf load-rc nil))
((or (string= "-?" option) (string= "--help" option))
(help-message t)
(quit 0))
(t
(format *error-output* "Unknown command line option ~A.~%" option)
(help-message *error-output*)
(quit 1)))))))
(defvar *lisp-initialized* nil)
@ -417,12 +427,9 @@ file. When the saved image is invoked, it will start the redefined top-level."
(let* (+ ++ +++ - * ** *** / // ///)
(unless *lisp-initialized*
(catch *quit-tag*
(let ((*break-enable* nil))
(dolist (file *lisp-init-file-list*)
(when (load file :if-does-not-exist nil :search-list nil)
(return)))
;; process command arguments
(notinline (process-command-args))))