13
13
(in-package "SYSTEM")
15
(export '(*lisp-init-file-list* command-args process-command-args))
15
(export '(*lisp-init-file-list*
17
+default-command-arg-rules+
19
process-command-args))
17
21
(defvar *lisp-init-file-list* '("~/.ecl" "~/.eclrc")
18
22
"List of files automatically loaded when ECL is invoked.")
24
(defvar *help-message* "
25
Usage: ecl [-? | --help]
26
[-dir dir] [-load file] [-shell file] [-eval expr] [-rc | -norc] [-hp | -nohp]
27
[[-o ofile] [-c [cfile]] [-h [hfile]] [-data [datafile]] [-s] [-q]
31
"Prints a help message about command line arguments of ECL")
20
33
(defun command-args ()
21
34
"Returns the command line arguments as list"
22
35
(loop for i from 0 below (argc)
25
(defun help-message (stream)
26
"Prints a help message about command line arguments of ECL"
28
Usage: ecl [-? | --help]
29
[-dir dir] [-load file] [-shell file] [-eval expr] [-rc | -norc]
30
[[-o ofile] [-c [cfile]] [-h [hfile]] [-data [datafile]] [-s] [-q]
36
38
(defun command-arg-error (str &rest fmt-args)
37
39
;; Format an error message and quit
38
40
(declare (si::c-local))
39
41
(apply #'format *error-output* str fmt-args)
40
(help-message *error-output*)
42
(princ *help-message* *error-output*)
43
(defconstant +command-arg-rules+
44
'(("--help" 0 #0=(progn (help-message *standard-output*) (quit)) :noloadrc)
45
(defconstant +default-command-arg-rules+
46
'(("--help" 0 #0=(progn (princ *help-message* *standard-output*) (quit)) :noloadrc)
45
47
("-?" 0 #0# :noloadrc)
46
48
("-h" 0 #0# :noloadrc)
47
49
("-norc" 0 nil :noloadrc)
70
72
("-h" 1 (setq h-file 1))
71
73
("-data" 1 (setq data-file 1))
72
74
("-q" 0 (setq verbose nil))
75
("-hp" 0 (setf *relative-package-names* t))
76
("-nohp" 0 (setf *relative-package-names* nil))
73
77
("-s" 0 (setq system-p t))))
75
(defun produce-init-code (option-list rules error-on-unknown)
76
(declare (si::c-local))
79
(defun produce-init-code (option-list rules)
77
80
(do* ((commands '())
83
((or stop (null option-list))
80
84
(values `(let ((output-file t)
87
91
,@(nreverse commands)
88
92
(when quit (quit 0)))
90
95
(let* ((option (pop option-list))
91
96
(rule (assoc option rules :test #'string=)))
94
(command-arg-error "Unknown command line option ~A.~%" option)
95
(setf option-list nil))
96
(let ((pattern (copy-tree (third rule))))
98
(:noloadrc (setf loadrc nil))
99
(:loadrc (setf loadrc t))
100
(:stop (setf option-list nil)))
101
(let ((pattern (copy-tree (third rule))))
102
(unless (zerop (second rule))
103
(when (null option-list)
105
"Missing argument after command line option ~A.~%"
107
(nsubst (pop option-list) 1 pattern))
108
(push pattern commands)))))))
98
;; If there is a default rule, group all remaining arguments
99
;; including the unmatched one, and pass them to this rule.
100
(setf rule (assoc "*DEFAULT*" rules :test #'string=)
101
option-list `('(,option ,@option-list))
104
(command-arg-error "Unknown command line option ~A.~%" option)))
105
(let ((pattern (copy-tree (third rule))))
107
(:noloadrc (setf loadrc nil))
108
(:loadrc (setf loadrc t))
109
(:stop (setf option-list nil)))
110
(let ((pattern (copy-tree (third rule))))
111
(unless (zerop (second rule))
112
(when (null option-list)
114
"Missing argument after command line option ~A.~%"
116
(nsubst (pop option-list) 1 pattern))
117
(push pattern commands))))))
110
119
(defun process-command-args (&key
111
120
(args (rest (command-args)))
112
(rules +command-arg-rules+)
113
(error-on-unknown t))
121
(rules +default-command-arg-rules+))
114
122
"PROCESS-COMMAND-ARGS takes a list of arguments and processes according
115
123
to a set of rules. These rules are of the format
125
133
loaded before evaluating all forms.
127
135
An excerpt of the rules used by ECL:
128
'((\"--help\" 0 #0=(progn (help-message *standard-output*) (quit)) :noloadrc)
136
'((\"--help\" 0 #0=(progn (princ *help-message* *standard-output*) (quit))
129
138
(\"-?\" 0 #0# :noloadrc)
130
139
(\"-h\" 0 #0# :noloadrc)
131
140
(\"-norc\" 0 nil :noloadrc)