/[cmucl]/src/code/commandline.lisp
ViewVC logotype

Contents of /src/code/commandline.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.17.16.1 - (show annotations)
Thu Feb 25 20:34:48 2010 UTC (4 years, 1 month ago) by rtoy
Branch: intl-2-branch
Changes since 1.17: +16 -13 lines
Restart internalization work.  This new branch starts with code from
the intl-branch on date 2010-02-12 18:00:00+0500.  This version works
and

LANG=en@piglatin bin/lisp

works (once the piglatin translation is added).
1 ;;; -*- Mode: Lisp; Package: Extensions; Log: code.log -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the CMU Common Lisp project at
5 ;;; Carnegie Mellon University, and has been placed in the public domain.
6 ;;;
7 (ext:file-comment
8 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/commandline.lisp,v 1.17.16.1 2010/02/25 20:34:48 rtoy Exp $")
9 ;;;
10 ;;; **********************************************************************
11 ;;;
12 ;;; Stuff to eat the command line passed to us from the shell.
13 ;;; Written by Bill Chiles.
14 ;;;
15
16 (in-package "EXTENSIONS")
17
18 (intl:textdomain "cmucl")
19
20 (export '(*command-line-application-arguments* *command-line-words* *command-line-switches*
21 *command-switch-demons* *command-line-utility-name*
22 *command-line-strings* *batch-mode*
23 cmd-switch-string command-line-switch-p
24 cmd-switch-name cmd-switch-value cmd-switch-words command-line-switch
25 defswitch cmd-switch-arg get-command-line-switch))
26
27 (defvar *command-line-application-arguments* ()
28 _N"A list of all the command line arguments after --")
29
30 (defvar *command-line-switches* ()
31 _N"A list of cmd-switch's representing the arguments used to invoke
32 this process.")
33
34 (defvar *command-line-utility-name* ""
35 _N"The string name that was used to invoke this process.")
36
37 (defvar *command-line-words* ()
38 _N"A list of words between the utility name and the first switch.")
39
40 (defvar *command-line-strings* ()
41 _N"A list of strings obtained from the command line that invoked this process.")
42
43 (defvar *command-switch-demons* ()
44 _N"An Alist of (\"argument-name\" . demon-function)")
45
46 (defvar *batch-mode* nil
47 _N"When True runs lisp with its input coming from standard-input.
48 If an error is detected returns error code 1, otherwise 0.")
49
50 (defstruct (command-line-switch (:conc-name cmd-switch-)
51 (:constructor make-cmd-switch
52 (name value words))
53 (:print-function print-command-line-switch))
54 name ;the name of the switch
55 value ;the value of that switch
56 words ;random words dangling between switches assigned to the
57 ;preceeding switch
58 )
59
60 (defun print-command-line-switch (object stream n)
61 (declare (ignore n))
62 (write-string "#<Command Line Switch " stream)
63 (prin1 (cmd-switch-name object) stream)
64 (let ((value (cmd-switch-value object))
65 (words (cmd-switch-words object)))
66 (when (or value words) (write-string " -- " stream)
67 (when value (prin1 value stream))
68 (when words (prin1 words stream))))
69 (write-string ">" stream))
70
71
72
73 ;;;; Processing the command strings.
74
75 (defun process-command-strings ()
76 (setq *command-line-words* nil)
77 (setq *command-line-switches* nil)
78 (let ((cmd-strings lisp::lisp-command-line-list)
79 str)
80 (declare (special lisp::lisp-command-line-list))
81 ;; Set some initial variables.
82 ;;
83 (setf *command-line-strings* (copy-list lisp::lisp-command-line-list))
84 (setf *command-line-utility-name* (pop cmd-strings))
85 (setq str (pop cmd-strings))
86 ;; Set initial command line words.
87 ;;
88 (loop
89 (unless str (return nil))
90 (unless (zerop (length (the simple-string str)))
91 (when (char= (schar str 0) #\-)
92 (setq *command-line-words* (reverse *command-line-words*))
93 (return nil))
94 (push str *command-line-words*))
95 (setq str (pop cmd-strings)))
96 ;; Set command line switches.
97 ;;
98 (loop
99 (unless str
100 (return (setf *command-line-switches*
101 (nreverse *command-line-switches*))))
102 (let* ((position (position #\= (the simple-string str) :test #'char=))
103 (switch (subseq (the simple-string str) 1 position))
104 (value (if position
105 (subseq (the simple-string str) (1+ position)
106 (length (the simple-string str))))))
107 (setq str (pop cmd-strings))
108 ;; Set this switch's words until the next switch.
109 ;;
110 (let (word-list)
111 (loop
112 (unless str
113 (push (make-cmd-switch switch value (nreverse word-list))
114 *command-line-switches*)
115 (return nil))
116
117 (unless (zerop (length (the simple-string str)))
118 (when (char= #\- (schar str 0))
119 (push (make-cmd-switch switch value (nreverse word-list))
120 *command-line-switches*)
121 (when (and (= (length str) 2)
122 (char= #\- (schar str 1)))
123 ;; Gather up everything after --, and exit.
124 (setf *command-line-application-arguments* cmd-strings)
125 (setf str nil))
126 (return nil))
127 (push str word-list))
128 (setq str (pop cmd-strings))))))))
129
130 (defun get-command-line-switch (sname)
131 _N"Accepts the name of a switch as a string and returns the value of the
132 switch. If no value was specified, then any following words are returned.
133 If there are no following words, then t is returned. If the switch was not
134 specified, then nil is returned."
135 (let* ((name (if (char= (schar sname 0) #\-) (subseq sname 1) sname))
136 (switch (find name *command-line-switches*
137 :test #'string-equal
138 :key #'cmd-switch-name)))
139 (when switch
140 (or (cmd-switch-value switch)
141 (cmd-switch-words switch)
142 T))))
143
144
145
146 ;;;; Defining Switches and invoking demons.
147
148 (defvar *complain-about-illegal-switches* t
149 _N"When set, invoking switch demons complains about illegal switches that have
150 not been defined with DEFSWITCH.")
151
152 ;;; This is a list of legal switch names. DEFSWITCH sets this, and
153 ;;; INVOKE-SWITCH-DEMONS makes sure all the switches it sees are on this
154 ;;; list.
155 ;;;
156 (defvar *legal-cmd-line-switches* nil)
157
158 ;;; INVOKE-SWITCH-DEMONS cdrs down the list of *command-line-switches*. For
159 ;;; each switch, it checks to see if there is a switch demon with the same
160 ;;; name. If there is, then that demon is called as a function on the switch.
161 ;;;
162 (defun invoke-switch-demons (&optional (switches *command-line-switches*)
163 (demons *command-switch-demons*))
164 (dolist (switch switches t)
165 (let* ((name (cmd-switch-name switch))
166 (demon (cdr (assoc name demons :test #'string-equal))))
167 (cond (demon (funcall demon switch))
168 ((or (member name *legal-cmd-line-switches* :test #'string-equal)
169 (not *complain-about-illegal-switches*)))
170 (t (warn _"~S is an illegal switch" switch)))
171 (lisp::finish-standard-output-streams))))
172
173 (defmacro defswitch (name &optional function)
174 _N"Associates function with the switch name in *command-switch-demons*. Name
175 is a simple-string that does not begin with a hyphen, unless the switch name
176 really does begin with one. Function is optional, but defining the switch
177 is necessary to keep invoking switch demons from complaining about illegal
178 switches. This can be inhibited with *complain-about-illegal-switches*."
179 (let ((gname (gensym))
180 (gfunction (gensym)))
181 `(let ((,gname ,name)
182 (,gfunction ,function))
183 (check-type ,gname simple-string)
184 (check-type ,gfunction (or symbol function) _"a symbol or function")
185 (push ,gname *legal-cmd-line-switches*)
186 (when ,gfunction
187 (push (cons ,gname ,gfunction) *command-switch-demons*)))))
188
189
190 (defun eval-switch-demon (switch)
191 (let ((cmds (cmd-switch-arg switch)))
192 (do ((length (length cmds))
193 (start 0))
194 ((>= start length))
195 (multiple-value-bind (form next)
196 (read-from-string cmds nil nil :start start)
197 (eval form)
198 (lisp::finish-standard-output-streams)
199 (setf start next)))))
200 (defswitch "eval" #'eval-switch-demon)
201
202 (defun load-switch-demon (switch)
203 (load (cmd-switch-arg switch)))
204 (defswitch "load" #'load-switch-demon)
205
206 (defun cmd-switch-arg (switch)
207 (or (cmd-switch-value switch)
208 (car (cmd-switch-words switch))
209 (car *command-line-words*)))
210
211 (defswitch "core")
212 (defswitch "init")
213 (defswitch "noinit")
214 (defswitch "nositeinit")
215 (defswitch "hinit")
216 (defswitch "batch")
217 (defswitch "dynamic-space-size")
218 (defswitch "lib")
219 (defswitch "quiet")
220 (defswitch "debug-lisp-search")
221 #+x86
222 (defswitch "fpu")

  ViewVC Help
Powered by ViewVC 1.1.5