| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: SYSTEM -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; |
| 5 |
;;;; cmdline.lsp -- command line processing |
| 6 |
;;;; |
| 7 |
;;;; Copyright (c) 2005, Juan Jose Garcia-Ripoll |
| 8 |
;;;; |
| 9 |
;;;; See file 'LICENSE' for the copyright details. |
| 10 |
;;;; |
| 11 |
|
| 12 |
(in-package "SYSTEM") |
| 13 |
|
| 14 |
(defparameter *command-break-enable* nil) |
| 15 |
|
| 16 |
(defparameter *lisp-init-file-list* '("~/.ecl" "~/.eclrc") |
| 17 |
"List of files automatically loaded when ECL is invoked.") |
| 18 |
|
| 19 |
(defparameter *help-message* " |
| 20 |
Usage: ecl [-? | --help] [-v | --version] [--debug | --nodebug] |
| 21 |
[--dir dir] [--load file] [--shell file] [--eval expr] [--rc | --norc] |
| 22 |
[--c-stack size] [--lisp-stack size] [--heap-size size] [--frame-stack size] |
| 23 |
[[-o ofile] [-c [cfile]] [-h [hfile]] [--data [datafile]] [-s] [-q] |
| 24 |
--compile file] |
| 25 |
[[-o ofile] --link file+] |
| 26 |
[--input-encoding external-format] [--output-encoding external-format] |
| 27 |
[--error-encoding external-format] [--encoding external-format] |
| 28 |
[--trap-fpe | --no-trap-fpe] |
| 29 |
" |
| 30 |
"Prints a help message about command line arguments of ECL") |
| 31 |
|
| 32 |
(defun command-args () |
| 33 |
"Returns the original command line arguments as list. First argument is |
| 34 |
the ECL program itself." |
| 35 |
(loop for i from 0 below (argc) |
| 36 |
collect (argv i))) |
| 37 |
|
| 38 |
(defparameter *command-args* |
| 39 |
(command-args)) |
| 40 |
|
| 41 |
(defparameter *unprocessed-ecl-command-args* |
| 42 |
nil |
| 43 |
"As part of ECL's command line rules, this variable stores all arguments which |
| 44 |
appeared after a '--'.") |
| 45 |
|
| 46 |
(defun command-arg-error (str &rest fmt-args) |
| 47 |
;; Format an error message and quit |
| 48 |
(declare (si::c-local)) |
| 49 |
(apply #'format *error-output* str fmt-args) |
| 50 |
(princ *help-message* *error-output*) |
| 51 |
(quit 1)) |
| 52 |
|
| 53 |
(defconstant +default-command-arg-rules+ |
| 54 |
'((("-?" "--help") 0 |
| 55 |
(progn (princ *help-message* *standard-output*) |
| 56 |
(quit)) |
| 57 |
:noloadrc) |
| 58 |
(("-norc" "--norc") 0 nil :noloadrc) |
| 59 |
(("-v" "--version") 0 |
| 60 |
(progn (setf quit 0) |
| 61 |
(format *standard-output* "ECL ~A~%" (lisp-implementation-version))) |
| 62 |
:noloadrc) |
| 63 |
(("-debug" "--debug") 0 (setf *command-break-enable* t)) |
| 64 |
(("-nodebug" "--nodebug") 0 (setf *command-break-enable* nil)) |
| 65 |
(("-eval" "--eval") 1 (eval (read-from-string 1))) |
| 66 |
(("-shell" "--shell") 1 |
| 67 |
(progn (setq quit 0) |
| 68 |
(setq ext:*unprocessed-ecl-command-args* (rest 1)) |
| 69 |
(load (first (rest 1)) :verbose nil)) |
| 70 |
:stop) |
| 71 |
(("-load" "--load") 1 (load 1 :verbose verbose)) |
| 72 |
(("-dir" "--dir") 1 (setf (logical-pathname-translations "SYS") |
| 73 |
`(("**;*.*" ,(merge-pathnames "**/*.*" (truename 1)))))) |
| 74 |
("--heap-size" 1 (ext:set-limit 'ext:heap-size (read-from-string 1))) |
| 75 |
("--lisp-stack" 1 (ext:set-limit 'ext:lisp-stack (read-from-string 1))) |
| 76 |
("--frame-stack" 1 (ext:set-limit 'ext:frame-stack (read-from-string 1))) |
| 77 |
("--c-stack" 1 (ext:set-limit 'ext:c-stack (read-from-string 1))) |
| 78 |
("--trap-fpe" 0 (si::trap-fpe t t)) |
| 79 |
("--no-trap-fpe" 0 (si::trap-fpe t nil)) |
| 80 |
("--encoding" 1 (let ((enc (read-from-string 1))) |
| 81 |
(setf ext::*default-external-format* enc) |
| 82 |
(dolist (i (list *standard-input* *standard-output* |
| 83 |
*error-output* *trace-output*)) |
| 84 |
(setf (stream-external-format i) enc)))) |
| 85 |
("--input-encoding" 1 |
| 86 |
(setf (stream-external-format *standard-input*) (read-from-string 1))) |
| 87 |
("--output-encoding" 1 |
| 88 |
(setf (stream-external-format *standard-output*) (read-from-string 1))) |
| 89 |
("--error-encoding" 1 |
| 90 |
(setf (stream-external-format *error-output*) (read-from-string 1))) |
| 91 |
(("-compile" "--compile") 1 |
| 92 |
(progn |
| 93 |
(setq quit |
| 94 |
(if (nth-value |
| 95 |
3 (compile-file |
| 96 |
1 :output-file output-file :c-file c-file |
| 97 |
:h-file h-file :data-file data-file |
| 98 |
:verbose verbose :system-p system-p)) |
| 99 |
1 |
| 100 |
0) |
| 101 |
output-file t |
| 102 |
c-file nil |
| 103 |
h-file nil |
| 104 |
data-file nil |
| 105 |
system-p nil))) |
| 106 |
(("-link" "--link") &rest |
| 107 |
(progn |
| 108 |
(require 'cmp) |
| 109 |
(funcall (read-from-string "c::build-program") |
| 110 |
(or output-file "lisp.exe") :lisp-files '&rest) |
| 111 |
(setq output-file t quit t))) |
| 112 |
("-o" &optional (setq output-file 1)) |
| 113 |
("-c" &optional (setq c-file 1)) |
| 114 |
("-h" &optional (setq h-file 1)) |
| 115 |
(("-data" "--data") 1 (setq data-file 1)) |
| 116 |
(("-q" "--quiet") 0 (setq verbose nil)) |
| 117 |
("-s" 0 (setq system-p t)) |
| 118 |
("--" 1 (setf ext:*unprocessed-ecl-command-args* (rest 1)) :stop))) |
| 119 |
|
| 120 |
(defun produce-init-code (option-list rules) |
| 121 |
(do* ((match-option (lambda (k r) |
| 122 |
(if (listp r) |
| 123 |
(member k r :test #'string=) |
| 124 |
(string= k r)))) |
| 125 |
(commands '()) |
| 126 |
(stop nil) |
| 127 |
(loadrc t)) |
| 128 |
((or stop (null option-list)) |
| 129 |
(values `(let ((output-file t) |
| 130 |
(c-file nil) |
| 131 |
(h-file nil) |
| 132 |
(data-file nil) |
| 133 |
(verbose t) |
| 134 |
(system-p nil) |
| 135 |
(quit nil) |
| 136 |
(*command-break-enable* nil)) |
| 137 |
,@(nreverse commands) |
| 138 |
(when quit (quit 0))) |
| 139 |
loadrc |
| 140 |
option-list)) |
| 141 |
(let* ((option (pop option-list)) |
| 142 |
(rule (assoc option rules :test match-option))) |
| 143 |
(unless rule |
| 144 |
;; If there is a default rule, group all remaining arguments |
| 145 |
;; including the unmatched one, and pass them to this rule. |
| 146 |
(setf rule (assoc "*DEFAULT*" rules :test match-option) |
| 147 |
stop t) |
| 148 |
(unless rule |
| 149 |
(command-arg-error "Unknown command line option ~A.~%" option))) |
| 150 |
(case (fourth rule) |
| 151 |
(:noloadrc (setf loadrc nil)) |
| 152 |
(:loadrc (setf loadrc t)) |
| 153 |
(:stop (setf option-list `('(,option ,@option-list)) |
| 154 |
stop t))) |
| 155 |
(let ((pattern (copy-tree (third rule))) |
| 156 |
(noptions (second rule))) |
| 157 |
(cond ((equal noptions 0) |
| 158 |
;; No extra arguments |
| 159 |
) |
| 160 |
((and (equal noptions '&optional) |
| 161 |
(or (null option-list) |
| 162 |
(assoc (first option-list) rules :test match-option))) |
| 163 |
;; The argument is optional and the next command line option is |
| 164 |
;; either absent or it is a valid command line option |
| 165 |
(nsubst t 1 pattern)) |
| 166 |
((null option-list) |
| 167 |
(command-arg-error |
| 168 |
"Missing argument after command line option ~A.~%" |
| 169 |
option)) |
| 170 |
((or (eq noptions 'rest) (eq noptions '&rest)) |
| 171 |
(nsubst option-list noptions pattern) |
| 172 |
(setf option-list nil)) |
| 173 |
(t |
| 174 |
(nsubst (pop option-list) 1 pattern))) |
| 175 |
(push pattern commands))))) |
| 176 |
|
| 177 |
(defun process-command-args (&key |
| 178 |
(args (rest *command-args*)) |
| 179 |
(rules +default-command-arg-rules+)) |
| 180 |
"PROCESS-COMMAND-ARGS takes a list of arguments and processes according |
| 181 |
to a set of rules. These rules are of the format |
| 182 |
|
| 183 |
(option-name nargs template [ :stop | :noloadrc | :loadrc ] ) |
| 184 |
|
| 185 |
OPTION-NAME is a string containing the command line option. NARGS is |
| 186 |
the number of arguments that this option takes. TEMPLATE is a lisp |
| 187 |
form where numbers from 0 to NARGS will be substituted by the |
| 188 |
arguments, and which will be evaluated afterwards. The flags :STOP, |
| 189 |
:NOLOADRC and :LOADRC denote whether to stop processing the command |
| 190 |
line after this option and whether the initialization file will be |
| 191 |
loaded before evaluating all forms. |
| 192 |
|
| 193 |
An excerpt of the rules used by ECL: |
| 194 |
'((\"--help\" 0 #0=(progn (princ *help-message* *standard-output*) (quit)) |
| 195 |
:noloadrc) |
| 196 |
(\"-?\" 0 #0# :noloadrc) |
| 197 |
(\"-h\" 0 #0# :noloadrc) |
| 198 |
(\"-norc\" 0 nil :noloadrc) |
| 199 |
(\"--\" 0 nil :stop) |
| 200 |
(\"-eval\" 1 (eval (read-from-string 1)))) |
| 201 |
" |
| 202 |
(multiple-value-bind (commands loadrc unprocessed-options) |
| 203 |
(produce-init-code args rules) |
| 204 |
(declare (ignore unprocessed-options)) |
| 205 |
(restart-case |
| 206 |
(handler-bind ((error |
| 207 |
#'(lambda (c) |
| 208 |
(if *command-break-enable* |
| 209 |
(invoke-debugger c) |
| 210 |
(progn |
| 211 |
(format *error-output* |
| 212 |
"An error occurred during initialization:~%~A.~%" |
| 213 |
c) |
| 214 |
(quit 1)))))) |
| 215 |
(progn |
| 216 |
(when loadrc |
| 217 |
(dolist (file *lisp-init-file-list*) |
| 218 |
(when (load file :if-does-not-exist nil :search-list nil :verbose nil) |
| 219 |
(return)))) |
| 220 |
(eval commands))) |
| 221 |
(continue () |
| 222 |
:report "Ignore initialization errors and continue.") |
| 223 |
(abort () |
| 224 |
:report "Quit ECL unsafely, ignoring all existing threads." |
| 225 |
(si::quit -1 nil))))) |