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, which may or may not apply in its entirety at
7 ;;;; any given time, for this functionality is on the ACL website:
8 ;;;; <http://www.franz.com/support/documentation/6.2/doc/top-level.htm>.
10 (cl:defpackage :sb-aclrepl
12 (:export #:*prompt* #:*exit-on-eof* #:*max-history*
13 #:*use-short-package-name* #:*command-char*
16 (cl:in-package :sb-aclrepl)
19 (input nil) ; input, maybe a string or form
20 (func nil) ; cmd func entered, overloaded
21 ; (:eof :null-cmd :cmd-error :history-error)
22 (args nil) ; args for cmd func
23 (hnum nil)) ; history number
26 ;; numeric break level
28 ;; inspect data for a break level
30 ;; T when break initiated by an inspect
32 ;; restarts list for a break level
34 ;; T if break level is a continuable break
37 (eval-when (:compile-toplevel :load-toplevel :execute)
38 (defparameter *default-prompt* "~:[~2*~;[~:*~D~:[~;i~]~:[~;c~]] ~]~A(~D): "
39 "The default prompt."))
40 (defparameter *prompt* #.*default-prompt*
41 "The current prompt string or formatter function.")
42 (defparameter *use-short-package-name* t
43 "when T, use the shortnest package nickname in a prompt")
44 (defparameter *dir-stack* nil
45 "The top-level directory stack")
46 (defparameter *command-char* #\:
47 "Prefix character for a top-level command")
48 (defvar *max-history* 24
49 "Maximum number of history commands to remember")
50 (defvar *exit-on-eof* t
51 "If T, then exit when the EOF character is entered.")
52 (defparameter *history* nil
54 (defparameter *cmd-number* 1
55 "Number of the next command")
56 (defparameter *repl-output* nil
57 "The output stream for the repl")
58 (defparameter *repl-input* nil
59 "The input stream for the repl")
60 (defparameter *break-stack* (list (make-break-data :level 0))
61 "A stack of break data stored as a list of break-level structs")
63 (declaim (type list *history*))
65 (defvar *eof-marker* (cons :eof nil))
66 (defvar *eof-cmd* (make-user-cmd :func :eof))
67 (defvar *null-cmd* (make-user-cmd :func :null-cmd))
69 (defparameter *cmd-table-hash*
70 (make-hash-table :size 30 :test #'equal))
72 ;; Set up binding for multithreading
74 (let ((*prompt* #.*default-prompt*)
75 (*use-short-package-name* t)
84 (*break-stack* (list (make-break-data :level 0)))
87 (defun prompt-package-name ()
88 (if *use-short-package-name*
90 (package-nicknames cl:*package*)
91 (list (package-name cl:*package*)))
92 (lambda (a b) (< (length a) (length b)))))
93 (package-name cl:*package*)))
95 (defun read-cmd (input-stream)
96 ;; Reads a command from the user and returns a user-cmd object
97 (flet ((parse-args (parsing args-string)
100 (if (zerop (length args-string))
104 (let ((string-stream (make-string-input-stream args-string)))
105 (loop as arg = (read string-stream nil *eof-marker*)
106 until (eq arg *eof-marker*)
108 (let ((next-char (peek-char-non-whitespace input-stream)))
110 ((eql next-char *command-char*)
111 (let* ((line (string-trim-whitespace (read-line input-stream)))
112 (first-space-pos (position #\space line))
113 (cmd-string (subseq line 1 first-space-pos))
116 (string-trim-whitespace (subseq line first-space-pos))
118 (declare (string line))
119 (if (numberp (read-from-string cmd-string))
120 (let ((cmd (get-history (read-from-string cmd-string))))
121 (if (eq cmd *null-cmd*)
122 (make-user-cmd :func :history-error
123 :input (read-from-string cmd-string))
124 (make-user-cmd :func (user-cmd-func cmd)
125 :input (user-cmd-input cmd)
126 :args (user-cmd-args cmd)
127 :hnum *cmd-number*)))
128 (let ((cmd-entry (find-cmd cmd-string)))
130 (make-user-cmd :func (cmd-table-entry-func cmd-entry)
133 (cmd-table-entry-parsing cmd-entry)
136 (make-user-cmd :func :cmd-error
139 ((eql next-char #\newline)
140 (read-char input-stream)
143 (let ((form (read input-stream nil *eof-marker*)))
144 (if (eq form *eof-marker*)
146 (make-user-cmd :input form :func nil :hnum *cmd-number*))))))))
149 (defstruct cmd-table-entry
150 (name nil) ; name of command
151 (func nil) ; function handler
152 (desc nil) ; short description
153 (parsing nil) ; (:string :case-sensitive nil)
154 (group nil) ; command group (:cmd or :alias)
155 (abbr-len 0)) ; abbreviation length
157 (defun make-cte (name-param func desc parsing group abbr-len)
158 (let ((name (etypecase name-param
162 (string-downcase (write-to-string name-param))))))
163 (make-cmd-table-entry :name name :func func :desc desc
164 :parsing parsing :group group
165 :abbr-len (if abbr-len
169 (defun %add-entry (cmd &optional abbr-len)
170 (let* ((name (cmd-table-entry-name cmd))
174 (dotimes (i (length name))
175 (when (>= i (1- alen))
176 (setf (gethash (subseq name 0 (1+ i)) *cmd-table-hash*)
179 (defun add-cmd-table-entry (cmd-string abbr-len func-name desc parsing)
181 (make-cte cmd-string (symbol-function func-name) desc parsing :cmd abbr-len)
184 (defun find-cmd (cmdstr)
185 (gethash (string-downcase cmdstr) *cmd-table-hash*))
187 (defun user-cmd= (c1 c2)
188 "Returns T if two user commands are equal"
189 (and (eq (user-cmd-func c1) (user-cmd-func c2))
190 (equal (user-cmd-args c1) (user-cmd-args c2))
191 (equal (user-cmd-input c1) (user-cmd-input c2))))
193 (defun add-to-history (cmd)
194 (unless (and *history* (user-cmd= cmd (car *history*)))
195 (when (>= (length *history*) *max-history*)
196 (setq *history* (nbutlast *history* (+ (length *history*) *max-history* 1))))
198 (incf *cmd-number*)))
200 (defun get-history (n)
201 (let ((cmd (find n *history* :key #'user-cmd-hnum :test #'eql)))
206 (defun get-cmd-doc-list (&optional (group :cmd))
207 "Return list of all commands"
209 (maphash (lambda (k v)
211 (= (length k) (length (cmd-table-entry-name v)))
212 (eq (cmd-table-entry-group v) group))
214 (if (= (cmd-table-entry-abbr-len v)
217 (subseq k 0 (cmd-table-entry-abbr-len v)))
218 (cmd-table-entry-desc v)) cmds)))
220 (sort cmds #'string-lessp :key #'car)))
222 (defun cd-cmd (&optional string-dir)
224 ((or (zerop (length string-dir))
225 (string= string-dir "~"))
226 (setf cl:*default-pathname-defaults* (user-homedir-pathname)))
228 (let ((new (truename string-dir)))
229 (when (pathnamep new)
230 (setf cl:*default-pathname-defaults* new)))))
231 (format *repl-output* "~A~%" (namestring cl:*default-pathname-defaults*))
235 (format *repl-output* "Lisp's current working directory is ~s.~%"
236 (namestring cl:*default-pathname-defaults*))
239 (defun trace-cmd (&rest args)
241 (format *repl-output* "~A~%" (eval (sb-debug::expand-trace args)))
242 (format *repl-output* "~A~%" (sb-debug::%list-traced-funs)))
245 (defun untrace-cmd (&rest args)
247 (format *repl-output* "~A~%"
249 (sb-int:collect ((res))
250 (let ((current args))
252 (unless current (return))
253 (let ((name (pop current)))
254 (res (if (eq name :function)
255 `(sb-debug::untrace-1 ,(pop current))
256 `(sb-debug::untrace-1 ',name))))))
257 `(progn ,@(res) t))))
258 (format *repl-output* "~A~%" (eval (sb-debug::untrace-all))))
262 (defun thread-pids ()
263 "Return a list of the pids for all threads"
264 (let ((offset (* 4 sb-vm::thread-pid-slot)))
265 (sb-thread::mapcar-threads
266 #'(lambda (sap) (sb-sys:sap-ref-32 sap offset)))))
269 (defun other-thread-pids ()
270 "Returns a list of pids for all threads except the current process"
271 (delete (sb-thread:current-thread-id) (thread-pids) :test #'eql))
273 (defun exit-cmd (&optional (status 0))
275 (let ((other-pids (other-thread-pids)))
277 (format *repl-output* "There exists the following processes~%")
278 (format *repl-output* "~{~5d~%~}" other-pids)
279 (format *repl-output* "Do you want to exit lisp anyway [n]? ")
280 (force-output *repl-output*)
281 (let ((input (string-trim-whitespace (read-line *repl-input*))))
282 (if (and (plusp (length input))
283 (or (char= #\y (char input 0))
284 (char= #\Y (char input 0))))
285 ;; loop in case more threads get created while trying to exit
286 (do ((pids other-pids (other-thread-pids)))
288 (map nil #'sb-thread:destroy-thread pids)
290 (return-from exit-cmd)))))
291 (quit :unix-status status)
294 (defun package-cmd (&optional pkg)
297 (format *repl-output* "The ~A package is current.~%"
298 (package-name cl:*package*)))
299 ((null (find-package (write-to-string pkg)))
300 (format *repl-output* "Unknown package: ~A.~%" pkg))
302 (setf cl:*package* (find-package (write-to-string pkg)))))
305 (defun string-to-list-skip-spaces (str)
306 "Return a list of strings, delimited by spaces, skipping spaces."
308 (loop for i = 0 then (1+ j)
309 as j = (position #\space str :start i)
310 when (not (char= (char str i) #\space))
311 collect (subseq str i j) while j)))
313 (let ((last-files-loaded nil))
314 (defun ld-cmd (&optional string-files)
316 (setq last-files-loaded string-files)
317 (setq string-files last-files-loaded))
318 (dolist (arg (string-to-list-skip-spaces string-files))
319 (format *repl-output* "loading ~a~%" arg)
323 (defun cf-cmd (string-files)
325 (dolist (arg (string-to-list-skip-spaces string-files))
330 "Return if x and y are numbers, and x > y"
331 (and (numberp x) (numberp y) (> x y)))
333 (defun newer-file-p (file1 file2)
334 "Is file1 newer (written later than) file2?"
335 (>-num (if (probe-file file1) (file-write-date file1))
336 (if (probe-file file2) (file-write-date file2))))
338 (defun compile-file-as-needed (src-path)
339 "Compiles a file if needed, returns path."
340 (let ((dest-path (compile-file-pathname src-path)))
341 (when (or (not (probe-file dest-path))
342 (newer-file-p src-path dest-path))
343 (ensure-directories-exist dest-path)
344 (compile-file src-path :output-file dest-path))
347 ;;;; implementation of commands
349 (defun apropos-cmd (string)
350 (apropos (string-upcase string))
353 (let ((last-files-loaded nil))
354 (defun cload-cmd (&optional string-files)
356 (setq last-files-loaded string-files)
357 (setq string-files last-files-loaded))
358 (dolist (arg (string-to-list-skip-spaces string-files))
359 (format *repl-output* "loading ~a~%" arg)
360 (load (compile-file-as-needed arg)))
363 (defun inspect-cmd (arg)
364 (inspector arg nil *repl-output*)
367 (defun istep-cmd (&optional arg-string)
368 (istep arg-string *repl-output*)
371 (defun describe-cmd (&rest args)
373 (eval `(describe ,arg)))
376 (defun macroexpand-cmd (arg)
377 (pprint (macroexpand arg) *repl-output*)
380 (defun history-cmd ()
381 (let ((n (length *history*)))
385 (let ((hist (nth (- n i 1) *history*)))
386 (format *repl-output* "~3A " (user-cmd-hnum hist))
387 (if (stringp (user-cmd-input hist))
388 (format *repl-output* "~A~%" (user-cmd-input hist))
389 (format *repl-output* "~W~%" (user-cmd-input hist))))))
392 (defun help-cmd (&optional cmd)
395 (let ((cmd-entry (find-cmd cmd)))
397 (format *repl-output* "Documentation for ~A: ~A~%"
398 (cmd-table-entry-name cmd-entry)
399 (cmd-table-entry-desc cmd-entry)))))
401 (format *repl-output* "~11A ~4A ~A~%" "COMMAND" "ABBR" "DESCRIPTION")
402 (format *repl-output* "~11A ~4A ~A~%" "<n>" ""
403 "re-execute <n>th history command")
404 (dolist (doc-entry (get-cmd-doc-list :cmd))
405 (format *repl-output* "~11A ~4A ~A~%" (first doc-entry)
406 (second doc-entry) (third doc-entry)))))
410 (let ((doc-entries (get-cmd-doc-list :alias)))
411 (typecase doc-entries
413 (format *repl-output* "~11A ~A ~4A~%" "ALIAS" "ABBR" "DESCRIPTION")
414 (dolist (doc-entry doc-entries)
415 (format *repl-output* "~11A ~4A ~A~%" (first doc-entry) (second doc-entry) (third doc-entry))))
417 (format *repl-output* "No aliases are defined~%"))))
420 (defun shell-cmd (string-arg)
421 (sb-ext:run-program "/bin/sh" (list "-c" string-arg)
422 :input nil :output *repl-output*)
425 (defun pushd-cmd (string-arg)
426 (push string-arg *dir-stack*)
427 (cd-cmd *repl-output* string-arg)
432 (let ((dir (pop *dir-stack*)))
434 (format *repl-output* "No directory on stack to pop.~%"))
437 (defun pop-cmd (&optional (n 1))
438 (let ((new-level (- (length *break-stack*) n 1)))
439 (when (minusp new-level)
441 (dotimes (i (- (length *break-stack*) new-level 1))
442 (pop *break-stack*)))
444 (do* ((i (1- (length *break-stack*)) (1- i))
446 ((or found (minusp i)))
447 (let ((inspect (break-data-inspect (nth i *break-stack*))))
449 (set-current-inspect inspect)
453 (defun continue-cmd (n)
454 (let ((restarts (break-data-restarts (car *break-stack*))))
456 (if (< -1 n (length restarts))
458 (invoke-restart-interactively (nth n restarts))
460 (format *repl-output* "~&There is no such restart"))
461 (format *repl-output* "~&There are no restarts"))))
466 (defun current-cmd ()
472 (defun processes-cmd ()
474 (let ((pids (thread-pids))
475 (current-pid (sb-thread:current-thread-id)))
477 (format *repl-output* "~&~D" pid)
478 (when (= pid current-pid)
479 (format *repl-output* " [current listener]"))))
481 (format *repl-output* "~&Threads are not supported in this version of sbcl")
484 (defun kill-cmd (selected-pid)
486 (let ((pids (thread-pids)))
487 (if (find selected-pid pids :test #'eql)
489 (sb-thread:destroy-thread selected-pid)
490 (format *repl-output* "Thread ~D destroyed" selected-pid))
491 (format *repl-output* "No thread ~D exists" selected-pid)))
493 (declare (ignore selected-pid))
495 (format *repl-output* "~&Threads are not supported in this version of sbcl")
499 (setf *break-stack* (last *break-stack*))
503 (dolist (dir *dir-stack*)
504 (format *repl-output* "~a~%" dir))
508 ;;;; dispatch table for commands
511 '(("aliases" 3 alias-cmd "show aliases")
512 ("apropos" 2 apropos-cmd "show apropos" :parsing :string)
513 ("cd" 2 cd-cmd "change default diretory" :parsing :string)
514 ("ld" 2 ld-cmd "load a file" :parsing :string)
515 ("cf" 2 cf-cmd "compile file" :parsing :string)
516 ("cload" 2 cload-cmd "compile if needed and load file"
518 ;; ("current" 3 current-cmd "print the expression for the current stack frame")
519 ;; ("continue" 4 continue-cmd "continue from a continuable error")
520 ("describe" 2 describe-cmd "describe an object")
521 ("macroexpand" 2 macroexpand-cmd "macroexpand an expression")
522 ("package" 2 package-cmd "change current package")
523 ;; ("error" 3 error-cmd "print the last error message")
524 ("exit" 2 exit-cmd "exit sbcl")
525 ;; ("frame" 2 frame-cmd "print info about the current frame")
526 ("help" 2 help-cmd "print this help")
527 ("history" 3 history-cmd "print the recent history")
528 ("inspect" 2 inspect-cmd "inspect an object")
529 ("istep" 1 istep-cmd "navigate within inspection of a lisp object" :parsing :string)
530 ("kill" 2 kill-cmd "kill a process")
531 ("pwd" 3 pwd-cmd "print current directory")
532 ("pushd" 2 pushd-cmd "push directory on stack" :parsing :string)
533 ("pop" 3 pop-cmd "pop up `n' (default 1) break levels")
534 ("popd" 4 popd-cmd "pop directory from stack")
535 ("processes" 3 processes-cmd "list all processes")
536 ("trace" 2 trace-cmd "trace a function")
537 ("untrace" 4 untrace-cmd "untrace a function")
538 ("dirs" 2 dirs-cmd "show directory stack")
539 ("shell" 2 shell-cmd "execute a shell cmd" :parsing :string))))
540 (dolist (cmd cmd-table)
541 (destructuring-bind (cmd-string abbr-len func-name desc &key parsing) cmd
542 (add-cmd-table-entry cmd-string abbr-len func-name desc parsing))))
544 ;;;; machinery for aliases
546 (defsetf alias (name &key abbr-len description) (user-func)
549 (make-cte (quote ,name) ,user-func ,description nil :alias ,abbr-len))
552 (defmacro alias (name-param args &rest body)
556 (name (if (atom name-param)
559 (when (consp name-param)
560 (dolist (param (cdr name-param))
563 (eq param :case-sensitive)
565 (setq parsing param))
569 (setq abbr-index param)))))
572 (make-cte (quote ,name) (lambda ,args ,@body) ,desc ,parsing :alias (when ,abbr-index
578 (defun remove-alias (&rest aliases)
579 (declare (list aliases))
581 (remove-all (not (null (find :all aliases)))))
582 (unless remove-all ;; ensure all alias are strings
584 (loop for alias in aliases
590 (symbol-name alias))))))
593 (when (eq (cmd-table-entry-group cmd) :alias)
598 (let ((klen (length key)))
599 (and (>= (length alias) klen)
600 (string-equal (subseq alias 0 klen)
601 (subseq key 0 klen)))))
606 (remhash key *cmd-table-hash*))
609 ;;;; low-level reading/parsing functions
611 ;;; Skip white space (but not #\NEWLINE), and peek at the next
613 (defun peek-char-non-whitespace (&optional stream)
614 (do ((char (peek-char nil stream nil *eof-marker*)
615 (peek-char nil stream nil *eof-marker*)))
616 ((not (whitespace-char-not-newline-p char)) char)
619 (defun string-trim-whitespace (str)
620 (string-trim '(#\space #\tab #\return)
623 (defun whitespace-char-not-newline-p (x)
625 (or (char= x #\space)
627 (char= x #\return))))
630 ;;;; linking into SBCL hooks
632 (defun repl-prompt-fun (stream)
633 (let* ((break-data (car *break-stack*))
634 (break-level (break-data-level break-data)))
635 (when (zerop break-level)
636 (setq break-level nil))
637 (if (functionp *prompt*)
638 (write-string (funcall *prompt* break-level
639 (break-data-inspect-initiated break-data)
640 (break-data-continuable break-data)
641 (prompt-package-name) *cmd-number*)
644 (format nil *prompt* break-level
645 (break-data-inspect-initiated break-data)
646 (break-data-continuable break-data)
647 (prompt-package-name) *cmd-number*)
649 (format stream "~&Prompt error> "))
651 (format stream "~&~A" prompt))))))
653 (defun process-cmd (user-cmd input-stream output-stream)
654 ;; Processes a user command. Returns t if the user-cmd was a top-level
656 (cond ((eq user-cmd *eof-cmd*)
659 (format output-stream "EOF~%")
661 ((eq user-cmd *null-cmd*)
663 ((eq (user-cmd-func user-cmd) :cmd-error)
664 (format output-stream "Unknown top-level command: ~s.~%"
665 (user-cmd-input user-cmd))
666 (format output-stream "Type `:help' for the list of commands.~%")
668 ((eq (user-cmd-func user-cmd) :history-error)
669 (format output-stream "Input numbered ~d is not on the history list~%"
670 (user-cmd-input user-cmd))
672 ((functionp (user-cmd-func user-cmd))
673 (add-to-history user-cmd)
674 (let ((*repl-output* output-stream)
675 (*repl-input* input-stream))
676 (apply (user-cmd-func user-cmd) (user-cmd-args user-cmd)))
680 (add-to-history user-cmd)
681 nil))) ; nope, not in my job description
683 (defun repl-read-form-fun (input-stream output-stream)
684 ;; Pick off all the leading ACL magic commands, then return a normal
686 (loop for user-cmd = (read-cmd input-stream) do
687 (if (process-cmd user-cmd input-stream output-stream)
689 (repl-prompt-fun output-stream)
690 (force-output output-stream))
691 (return (user-cmd-input user-cmd)))))
694 (setf sb-int:*repl-prompt-fun* #'repl-prompt-fun
695 sb-int:*repl-read-form-fun* #'repl-read-form-fun)
697 ;;; Break level processing
699 ;; use an initial break-level to hold current inspect toplevel at
702 (defun new-break (&key restarts inspect continuable)
704 (make-break-data :level (length *break-stack*)
707 :inspect-initiated (when inspect t)
708 :continuable continuable)
711 (defun set-break-inspect (inspect)
712 "sets the inspect data for the current break level"
713 (setf (break-data-inspect (car *break-stack*)) inspect))
715 ) ;; close special variables bindings