1 ;;;; Environment query functions, DOCUMENTATION and DRIBBLE.
3 ;;;; FIXME: If there are exactly three things in here, it could be
4 ;;;; exactly three files named e.g. equery.lisp, doc.lisp, and dribble.lisp.
6 ;;;; This software is part of the SBCL system. See the README file for
9 ;;;; This software is derived from the CMU CL system, which was
10 ;;;; written at Carnegie Mellon University and released into the
11 ;;;; public domain. The software is in the public domain and is
12 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
13 ;;;; files for more information.
15 (in-package "SB!IMPL")
17 ;;;; function names and documentation
19 ;;;; the ANSI interface to function names (and to other stuff too)
20 (defun function-lambda-expression (fun)
21 "Return (VALUES DEFINING-LAMBDA-EXPRESSION CLOSURE-P NAME), where
22 DEFINING-LAMBDA-EXPRESSION is NIL if unknown, or a suitable argument
23 to COMPILE otherwise, CLOSURE-P is non-NIL if the function's definition
24 might have been enclosed in some non-null lexical environment, and
25 NAME is some name (for debugging only) or NIL if there is no name."
26 (declare (type function fun))
27 (let* ((fun (%simple-fun-self fun))
28 (name (%fun-name fun))
29 (code (sb!di::fun-code-header fun))
30 (info (sb!kernel:%code-debug-info code)))
32 (let ((source (first (sb!c::compiled-debug-info-source info))))
33 (cond ((and (eq (sb!c::debug-source-from source) :lisp)
34 (eq (sb!c::debug-source-info source) fun))
35 (values (svref (sb!c::debug-source-name source) 0)
38 ;; FIXME: shouldn't these two clauses be the other way
39 ;; round? Using VALID-FUNCTION-NAME-P to see if we
40 ;; want to find an inline-expansion?
44 (let ((exp (fun-name-inline-expansion name)))
47 (values nil t name))))))
48 (values nil t name))))
50 ;;; a SETFable function to return the associated debug name for FUN
51 ;;; (i.e., the third value returned from CL:FUNCTION-LAMBDA-EXPRESSION),
52 ;;; or NIL if there's none
53 (defun %fun-name (fun)
54 (case (widetag-of fun)
55 (#.sb!vm:closure-header-widetag
56 (%simple-fun-name (%closure-fun fun)))
57 (#.sb!vm:simple-fun-header-widetag
58 ;; KLUDGE: The pun that %SIMPLE-FUN-NAME is used for closure
59 ;; functions is left over from CMU CL (modulo various renaming
60 ;; that's gone on since the fork).
61 (%simple-fun-name fun))
62 (#.sb!vm:funcallable-instance-header-widetag
64 (funcallable-instance-fun fun)))))
66 (defun (setf %fun-name) (new-name fun)
67 (aver nil) ; since this is unsafe 'til bug 137 is fixed
68 (let ((widetag (widetag-of fun)))
70 (#.sb!vm:simple-fun-header-widetag
71 ;; KLUDGE: The pun that %SIMPLE-FUN-NAME is used for closure
72 ;; functions is left over from CMU CL (modulo various renaming
73 ;; that's gone on since the fork).
74 (setf (%simple-fun-name fun) new-name))
75 (#.sb!vm:closure-header-widetag
76 ;; FIXME: It'd be nice to be able to set %FUN-NAME here on
77 ;; per-closure basis. Instead, we are still using the CMU CL
78 ;; approach of closures being named after their closure
79 ;; function, which doesn't work right e.g. for structure
80 ;; accessors, and might not be quite right for DEFUN
81 ;; in a non-null lexical environment either.
82 ;; When/if weak hash tables become supported
83 ;; again, it'll become easy to fix this, but for now there
84 ;; seems to be no easy way (short of the ugly way of adding a
85 ;; slot to every single closure header), so we don't.
87 ;; Meanwhile, users might encounter this problem by doing DEFUN
88 ;; in a non-null lexical environment, so we try to give a
89 ;; reasonably meaningful user-level "error" message (but only
90 ;; as a warning because this is optional debugging
91 ;; functionality anyway, not some hard ANSI requirement).
92 (warn "can't set name for closure, leaving name unchanged"))
94 ;; The other function subtype names are also un-settable
95 ;; but this problem seems less likely to be tickled by
96 ;; user-level code, so we can give a implementor-level
97 ;; "error" (warning) message.
98 (warn "can't set function name ((~S function)=~S), leaving it unchanged"
99 'widetag-of widetag))))
103 ;; FIXME: This business of going through %FUN-NAME and then globaldb
104 ;; is the way CMU CL did it, but it doesn't really seem right.
105 ;; When/if weak hash tables become supported again, using a weak
106 ;; hash table to maintain the object/documentation association would
107 ;; probably be better.
108 (let ((name (%fun-name x)))
109 (when (and name (typep name '(or symbol cons)))
110 (values (info :function :documentation name)))))
112 ;;; various environment inquiries
114 (defvar *features* '#.sb-cold:*shebang-features*
116 "a list of symbols that describe features provided by the
119 (defun machine-instance ()
121 "Return a string giving the name of the local machine."
122 (sb!unix:unix-gethostname))
124 (defvar *machine-version*)
126 (defun machine-version ()
128 "Return a string describing the version of the computer hardware we
129 are running on, or NIL if we can't find any useful information."
130 (unless (boundp '*machine-version*)
131 (setf *machine-version* (get-machine-version)))
134 ;;; FIXME: Don't forget to set these in a sample site-init file.
135 ;;; FIXME: Perhaps the functions could be SETFable instead of having the
136 ;;; interface be through special variables? As far as I can tell
137 ;;; from ANSI 11.1.2.1.1 "Constraints on the COMMON-LISP Package
138 ;;; for Conforming Implementations" it is kosher to add a SETF function for
139 ;;; a symbol in COMMON-LISP..
140 (defvar *short-site-name* nil
142 "The value of SHORT-SITE-NAME.")
143 (defvar *long-site-name* nil
144 #!+sb-doc "the value of LONG-SITE-NAME")
145 (defun short-site-name ()
147 "Return a string with the abbreviated site name, or NIL if not known."
149 (defun long-site-name ()
151 "Return a string with the long form of the site name, or NIL if not known."
155 (defvar *ed-functions* nil
156 "See function documentation for ED.")
158 (defun ed (&optional x)
159 "Starts the editor (on a file or a function if named). Functions
160 from the list *ED-FUNCTIONS* are called in order with X as an argument
161 until one of them returns non-NIL; these functions are responsible for
162 signalling a FILE-ERROR to indicate failure to perform an operation on
164 (dolist (fun *ed-functions*
165 (error 'extension-failure
166 :format-control "Don't know how to ~S ~A"
167 :format-arguments (list 'ed x)
168 :references (list '(:sbcl :variable *ed-functions*))))
169 (when (funcall fun x)
174 ;;; Each time we start dribbling to a new stream, we put it in
175 ;;; *DRIBBLE-STREAM*, and push a list of *DRIBBLE-STREAM*, *STANDARD-INPUT*,
176 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* in *PREVIOUS-DRIBBLE-STREAMS*.
177 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* is changed to a broadcast stream that
178 ;;; broadcasts to *DRIBBLE-STREAM* and to the old values of the variables.
179 ;;; *STANDARD-INPUT* is changed to an echo stream that echos input from the old
180 ;;; value of standard input to *DRIBBLE-STREAM*.
182 ;;; When dribble is called with no arguments, *DRIBBLE-STREAM* is closed,
183 ;;; and the values of *DRIBBLE-STREAM*, *STANDARD-INPUT*, and
184 ;;; *STANDARD-OUTPUT* are popped from *PREVIOUS-DRIBBLE-STREAMS*.
186 (defvar *previous-dribble-streams* nil)
187 (defvar *dribble-stream* nil)
189 (defun dribble (&optional pathname &key (if-exists :append))
191 "With a file name as an argument, dribble opens the file and sends a
192 record of further I/O to that file. Without an argument, it closes
193 the dribble file, and quits logging."
195 (let* ((new-dribble-stream
199 :if-does-not-exist :create))
201 (make-broadcast-stream *standard-output* new-dribble-stream))
203 (make-broadcast-stream *error-output* new-dribble-stream))
205 (make-echo-stream *standard-input* new-dribble-stream)))
206 (push (list *dribble-stream* *standard-input* *standard-output*
208 *previous-dribble-streams*)
209 (setf *dribble-stream* new-dribble-stream)
210 (setf *standard-input* new-standard-input)
211 (setf *standard-output* new-standard-output)
212 (setf *error-output* new-error-output)))
213 ((null *dribble-stream*)
214 (error "not currently dribbling"))
216 (let ((old-streams (pop *previous-dribble-streams*)))
217 (close *dribble-stream*)
218 (setf *dribble-stream* (first old-streams))
219 (setf *standard-input* (second old-streams))
220 (setf *standard-output* (third old-streams))
221 (setf *error-output* (fourth old-streams)))))
224 (defun %byte-blt (src src-start dst dst-start dst-end)
225 (%byte-blt src src-start dst dst-start dst-end))
227 ;;;; some *LOAD-FOO* variables
229 (defvar *load-print* nil
231 "the default for the :PRINT argument to LOAD")
233 (defvar *load-verbose* nil
234 ;; Note that CMU CL's default for this was T, and ANSI says it's
235 ;; implementation-dependent. We choose NIL on the theory that it's
236 ;; a nicer default behavior for Unix programs.
238 "the default for the :VERBOSE argument to LOAD")