1 ;;;; Replicate much of the ACL toplevel functionality in SBCL. Mostly
2 ;;;; this is portable code, but fundamentally it all hangs from a few
3 ;;;; SBCL-specific hooks like SB-INT:*REPL-READ-FUN* and
4 ;;;; SB-INT:*REPL-PROMPT-FUN*.
6 ;;;; The documentation for this functionality is on the ACL website,
7 ;;;; <http://www.franz.com/support/documentation/6.2/doc/top-level.htm>.
9 (cl:defpackage :sb-aclrepl
12 ;; (what else should we be exporting?)
15 (cl:in-package :sb-aclrepl)
17 (eval-when (:compile-toplevel :load-toplevel :execute)
18 (defparameter *default-prompt* "~A(~d): "
19 "The default prompt."))
20 (defparameter *prompt* #.*default-prompt*
21 "The current prompt string or formatter function.")
22 (defparameter *use-short-package-name* t
23 "when T, use the shortnest package nickname in a prompt")
24 (defparameter *dir-stack* nil
25 "The top-level directory stack")
26 (defparameter *cmd-char* #\:
27 "Prefix character for a top-level command")
28 (defvar *max-history* 24
29 "Maximum number of history commands to remember")
31 "If T, then exit when the EOF character is entered.")
32 (defparameter *history* nil
34 (defparameter *cmd-number* 0
35 "Number of the current command")
38 (input nil) ; input, maybe a string or form
39 (func nil) ; cmd func entered, overloaded (:eof :null-cmd))
40 (args nil) ; args for cmd func
41 (hnum nil)) ; history number
43 (defvar *eof-marker* (cons :eof nil))
44 (defvar *eof-cmd* (make-user-cmd :func :eof))
45 (defvar *null-cmd* (make-user-cmd :func :null-cmd))
47 (defun prompt-package-name ()
48 (if *use-short-package-name*
50 (package-nicknames cl:*package*)
51 (list (package-name cl:*package*)))
53 (package-name cl:*package*)))
55 (defun read-cmd (input-stream)
56 (flet ((parse-args (parsing args-string)
59 (if (zerop (length args-string))
63 (let ((string-stream (make-string-input-stream args-string)))
64 (loop as arg = (read string-stream nil *eof-marker*)
65 until (eq arg *eof-marker*)
67 (let ((next-char (peek-char-non-whitespace input-stream)))
69 ((eql next-char *cmd-char*)
70 (let* ((line (string-trim-whitespace (read-line input-stream)))
71 (first-space-pos (position #\space line))
72 (cmd-string (subseq line 1 first-space-pos))
75 (string-trim-whitespace (subseq line first-space-pos))
77 (if (numberp (read-from-string cmd-string))
78 (get-history (read-from-string cmd-string))
79 (let ((cmd-entry (find-cmd cmd-string)))
81 (make-user-cmd :func (cmd-table-entry-func cmd-entry)
84 (cmd-table-entry-parsing cmd-entry)
88 (format t "Unknown top-level command: ~s.~%" cmd-string)
89 (format t "Type `:help' for the list of commands.~%")
92 ((eql next-char #\newline)
93 (read-char input-stream)
96 (let ((form (read input-stream nil *eof-marker*)))
97 (if (eq form *eof-marker*)
99 (make-user-cmd :input form :func nil :hnum *cmd-number*))))))))
101 (defparameter *cmd-table-hash*
102 (make-hash-table :size 30 :test #'equal))
105 (defstruct cmd-table-entry
106 (name nil) ; name of command
107 (func nil) ; function handler
108 (desc nil) ; short description
109 (parsing nil) ; (:string :case-sensitive nil)
110 (group nil)) ; command group (:cmd or :alias)
112 (defun make-cte (name-param func desc parsing group)
113 (let ((name (etypecase name-param
117 (string-downcase (write-to-string name-param))))))
118 (make-cmd-table-entry :name name :func func :desc desc
119 :parsing parsing :group group)))
121 (defun %add-entry (cmd &optional abbr-len)
122 (let* ((name (cmd-table-entry-name cmd))
126 (dotimes (i (length name))
127 (when (>= i (1- alen))
128 (setf (gethash (subseq name 0 (1+ i)) *cmd-table-hash*)
131 (defun add-cmd-table-entry (cmd-string abbr-len func-name desc parsing)
133 (make-cte cmd-string (symbol-function func-name) desc parsing :cmd)
136 (defun find-cmd (cmdstr)
137 (gethash (string-downcase cmdstr) *cmd-table-hash*))
139 (defun user-cmd= (c1 c2)
140 "Returns T if two user commands are equal"
141 (if (or (not (user-cmd-p c1)) (not (user-cmd-p c2)))
143 (format t "Error: ~s or ~s is not a user-cmd" c1 c2)
145 (and (eq (user-cmd-func c1) (user-cmd-func c2))
146 (equal (user-cmd-args c1) (user-cmd-args c2))
147 (equal (user-cmd-input c1) (user-cmd-input c2)))))
149 (defun add-to-history (cmd)
150 (unless (and *history* (user-cmd= cmd (car *history*)))
151 (when (>= (length *history*) *max-history*)
152 (setq *history* (nbutlast *history* (+ (length *history*) *max-history* 1))))
153 (push cmd *history*)))
155 (defun get-history (n)
156 (let ((cmd (find n *history* :key #'user-cmd-hnum :test #'eql)))
160 (format t "Input numbered %d is not on the history list.." n)
163 (defun get-cmd-doc-list (&optional (group :cmd))
164 "Return list of all commands"
166 (maphash (lambda (k v)
168 (eql (length k) (length (cmd-table-entry-name v)))
169 (eq (cmd-table-entry-group v) group))
170 (push (list k (cmd-table-entry-desc v)) cmds)))
172 (sort cmds #'string-lessp :key #'car)))
174 (defun cd-cmd (&optional string-dir)
176 ((or (zerop (length string-dir))
177 (string= string-dir "~"))
178 (setf cl:*default-pathname-defaults* (user-homedir-pathname)))
180 (let ((new (truename string-dir)))
181 (when (pathnamep new)
182 (setf cl:*default-pathname-defaults* new)))))
183 (format t "~A~%" (namestring cl:*default-pathname-defaults*))
187 (format t "Lisp's current working directory is ~s.~%"
188 (namestring cl:*default-pathname-defaults*))
191 (defun trace-cmd (&rest args)
193 (format t "~A~%" (eval (apply #'sb-debug::expand-trace args)))
194 (format t "~A~%" (sb-debug::%list-traced-funs)))
197 (defun untrace-cmd (&rest args)
201 (sb-int:collect ((res))
202 (let ((current args))
204 (unless current (return))
205 (let ((name (pop current)))
206 (res (if (eq name :function)
207 `(sb-debug::untrace-1 ,(pop current))
208 `(sb-debug::untrace-1 ',name))))))
209 `(progn ,@(res) t))))
210 (format t "~A~%" (eval (sb-debug::untrace-all))))
213 (defun exit-cmd (&optional (status 0))
214 (quit :unix-status status)
217 (defun package-cmd (&optional pkg)
220 (format t "The ~A package is current.~%" (package-name cl:*package*)))
221 ((null (find-package (write-to-string pkg)))
222 (format t "Unknown package: ~A.~%" pkg))
224 (setf cl:*package* (find-package (write-to-string pkg)))))
227 (defun string-to-list-skip-spaces (str)
228 "Return a list of strings, delimited by spaces, skipping spaces."
229 (loop for i = 0 then (1+ j)
230 as j = (position #\space str :start i)
231 when (not (char= (char str i) #\space))
232 collect (subseq str i j) while j))
234 (defun ld-cmd (string-files)
235 (dolist (arg (string-to-list-skip-spaces string-files))
236 (format t "loading ~a~%" arg)
240 (defun cf-cmd (string-files)
241 (dolist (arg (string-to-list-skip-spaces string-files))
246 "Return if x and y are numbers, and x > y"
247 (and (numberp x) (numberp y) (> x y)))
249 (defun newer-file-p (file1 file2)
250 "Is file1 newer (written later than) file2?"
251 (>-num (if (probe-file file1) (file-write-date file1))
252 (if (probe-file file2) (file-write-date file2))))
254 (defun compile-file-as-needed (src-path)
255 "Compiles a file if needed, returns path."
256 (let ((dest-path (compile-file-pathname src-path)))
257 (when (or (not (probe-file dest-path))
258 (newer-file-p src-path dest-path))
259 (ensure-directories-exist dest-path)
260 (compile-file src-path :output-file dest-path))
263 ;;;; implementation of commands
265 (defun cload-cmd (string-files)
266 (dolist (arg (string-to-list-skip-spaces string-files))
267 (load (compile-file-as-needed arg)))
270 (defun inspect-cmd (arg)
271 (eval `(inspect ,arg))
274 (defun describe-cmd (&rest args)
276 (eval `(describe ,arg)))
279 (defun macroexpand-cmd (arg)
280 (pprint (macroexpand arg))
283 (defun history-cmd ()
284 (let ((n (length *history*)))
288 (let ((hist (nth (- n i 1) *history*)))
289 (format t "~3A ~A~%" (user-cmd-hnum hist) (user-cmd-input hist)))))
292 (defun help-cmd (&optional cmd)
295 (let ((cmd-entry (find-cmd cmd)))
297 (format t "Documentation for ~A: ~A~%"
298 (cmd-table-entry-name cmd-entry)
299 (cmd-table-entry-desc cmd-entry)))))
301 (format t "~13A ~a~%" "Command" "Description")
302 (format t "------------- -------------~%")
303 (dolist (doc-entry (get-cmd-doc-list :cmd))
304 (format t "~13A ~A~%" (car doc-entry) (cadr doc-entry)))))
308 (let ((doc-entries (get-cmd-doc-list :alias)))
309 (typecase doc-entries
311 (format t "~13A ~a~%" "Alias" "Description")
312 (format t "------------- -------------~%")
313 (dolist (doc-entry doc-entries)
314 (format t "~13A ~A~%" (car doc-entry) (cadr doc-entry))))
316 (format t "No aliases are defined~%"))))
319 (defun shell-cmd (string-arg)
320 (sb-ext:run-program "/bin/sh" (list "-c" string-arg)
321 :input nil :output *trace-output*)
324 (defun pushd-cmd (string-arg)
325 (push string-arg *dir-stack*)
331 (let ((dir (pop *dir-stack*)))
333 (format t "No directory on stack to pop.~%"))
337 (dolist (dir *dir-stack*)
338 (format t "~a~%" dir))
341 ;;;; dispatch table for commands
344 '(("aliases" 3 alias-cmd "show aliases")
345 ("cd" 2 cd-cmd "change default diretory" :parsing :string)
346 ("ld" 2 ld-cmd "load a file" :parsing :string)
347 ("cf" 2 cf-cmd "compile file" :parsing :string)
348 ("cload" 2 cload-cmd "compile if needed and load file"
350 ("describe" 2 describe-cmd "describe an object")
351 ("macroexpand" 2 macroexpand-cmd "macroexpand an expression")
352 ("package" 2 package-cmd "change current package")
353 ("exit" 2 exit-cmd "exit sbcl")
354 ("help" 2 help-cmd "print this help")
355 ("history" 3 history-cmd "print the recent history")
356 ("inspect" 2 inspect-cmd "inspect an object")
357 ("pwd" 3 pwd-cmd "print current directory")
358 ("pushd" 2 pushd-cmd "push directory on stack" :parsing :string)
359 ("popd" 2 popd-cmd "pop directory from stack")
360 ("trace" 2 trace-cmd "trace a function")
361 ("untrace" 4 untrace-cmd "untrace a function")
362 ("dirs" 2 dirs-cmd "show directory stack")
363 ("shell" 2 shell-cmd "execute a shell cmd" :parsing :string))))
364 (dolist (cmd cmd-table)
365 (destructuring-bind (cmd-string abbr-len func-name desc &key parsing) cmd
366 (add-cmd-table-entry cmd-string abbr-len func-name desc parsing))))
368 ;;;; machinery for aliases
370 (defsetf alias (name) (user-func)
373 (make-cte (quote ,name) ,user-func "" nil :alias))
376 (defmacro alias (name-param args &rest body)
380 (name (if (atom name-param)
383 (when (consp name-param)
384 (dolist (param (cdr name-param))
387 (eq param :case-sensitive)
389 (setq parsing param))
393 (setq abbr-index param)))))
396 (make-cte (quote ,name) (lambda ,args ,@body) ,desc ,parsing :alias)
401 (defun remove-alias (&rest aliases)
403 (remove-all (not (null (find :all aliases)))))
404 (unless remove-all ;; ensure all alias are strings
406 (loop for alias in aliases
412 (symbol-name alias))))))
415 (when (eq (cmd-table-entry-group cmd) :alias)
420 (let ((klen (length key)))
421 (and (>= (length alias) klen)
422 (string-equal (subseq alias 0 klen)
423 (subseq key 0 klen)))))
428 (remhash key *cmd-table-hash*))
431 ;;;; low-level reading/parsing functions
433 ;;; Skip white space (but not #\NEWLINE), and peek at the next
435 (defun peek-char-non-whitespace (&optional stream)
436 (do ((char (peek-char nil stream nil *eof-marker*)
437 (peek-char nil stream nil *eof-marker*)))
438 ((not (whitespace-char-not-newline-p char)) char)
441 (defun string-trim-whitespace (str)
442 (string-trim '(#\space #\tab #\return)
445 (defun whitespace-char-not-newline-p (x)
447 (or (char= x #\space)
449 (char= x #\return))))
451 ;;;; linking into SBCL hooks
453 (defun repl-prompt-fun (stream)
456 (if (functionp *prompt*)
457 (write-string (funcall *prompt* (prompt-package-name) *cmd-number*)
459 (format stream *prompt* (prompt-package-name) *cmd-number*)))
461 ;;; If USER-CMD is to be processed as something magical (not an
462 ;;; ordinary eval-and-print-me form) then do so and return non-NIL.
463 (defun execute-as-acl-magic (user-cmd input-stream output-stream)
464 ;; kludgity kludge kludge kludge ("and then a miracle occurs")
466 ;; This is a really sloppy job of smashing KMR's code (what he
467 ;; called DEFUN REP-ONE-CMD) onto DB's hook ideas, not even doing
468 ;; the basics like passing INPUT-STREAM and OUTPUT-STREAM into the
469 ;; KMR code. A real implementation might want to do rather better.
470 (cond ((eq user-cmd *eof-cmd*)
475 t) ; Yup, we knew how to handle that.
476 ((eq user-cmd *null-cmd*)
479 ((functionp (user-cmd-func user-cmd))
480 (apply (user-cmd-func user-cmd) (user-cmd-args user-cmd))
481 (add-to-history user-cmd)
485 nil))) ; nope, not in my job description
487 (defun repl-read-form-fun (input-stream output-stream)
488 ;; Pick off all the leading ACL magic commands, then return a normal
490 (loop for user-cmd = (read-cmd input-stream) do
491 (if (execute-as-acl-magic user-cmd input-stream output-stream)
493 (repl-prompt-fun output-stream)
494 (force-output output-stream))
495 (return (user-cmd-input user-cmd)))))
497 (setf sb-int:*repl-prompt-fun* #'repl-prompt-fun
498 sb-int:*repl-read-form-fun* #'repl-read-form-fun)