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 (sb!c::debug-info-source info)))
33 (cond ((and (eq (sb!c::debug-source-from source) :lisp)
34 (eq (sb!c::debug-source-function source) fun))
35 (values (svref (sb!c::debug-source-name source) 0)
38 ((legal-fun-name-p name)
39 (let ((exp (fun-name-inline-expansion name)))
40 (values exp (not exp) name)))
42 (values nil t name))))
43 (values nil t name))))
45 (defun closurep (object)
46 (= sb!vm:closure-header-widetag (widetag-of object)))
48 (defun %fun-fun (function)
49 (declare (function function))
50 (case (widetag-of function)
51 (#.sb!vm:simple-fun-header-widetag
53 (#.sb!vm:closure-header-widetag
54 (%closure-fun function))
55 (#.sb!vm:funcallable-instance-header-widetag
56 (%fun-fun (funcallable-instance-fun function)))))
58 (defun %closure-values (object)
59 (declare (function object))
60 (coerce (loop for index from 0 below (1- (get-closure-length object))
61 collect (%closure-index-ref object index))
64 (defun %fun-lambda-list (object)
65 (%simple-fun-arglist (%fun-fun object)))
67 ;;; a SETFable function to return the associated debug name for FUN
68 ;;; (i.e., the third value returned from CL:FUNCTION-LAMBDA-EXPRESSION),
69 ;;; or NIL if there's none
70 (defun %fun-name (function)
71 (%simple-fun-name (%fun-fun function)))
73 (defun %fun-type (function)
74 (%simple-fun-type (%fun-fun function)))
76 (defun (setf %fun-name) (new-name fun)
77 (aver nil) ; since this is unsafe 'til bug 137 is fixed
78 (let ((widetag (widetag-of fun)))
80 (#.sb!vm:simple-fun-header-widetag
81 ;; KLUDGE: The pun that %SIMPLE-FUN-NAME is used for closure
82 ;; functions is left over from CMU CL (modulo various renaming
83 ;; that's gone on since the fork).
84 (setf (%simple-fun-name fun) new-name))
85 (#.sb!vm:closure-header-widetag
86 ;; FIXME: It'd be nice to be able to set %FUN-NAME here on
87 ;; per-closure basis. Instead, we are still using the CMU CL
88 ;; approach of closures being named after their closure
89 ;; function, which doesn't work right e.g. for structure
90 ;; accessors, and might not be quite right for DEFUN
91 ;; in a non-null lexical environment either.
92 ;; When/if weak hash tables become supported
93 ;; again, it'll become easy to fix this, but for now there
94 ;; seems to be no easy way (short of the ugly way of adding a
95 ;; slot to every single closure header), so we don't.
97 ;; Meanwhile, users might encounter this problem by doing DEFUN
98 ;; in a non-null lexical environment, so we try to give a
99 ;; reasonably meaningful user-level "error" message (but only
100 ;; as a warning because this is optional debugging
101 ;; functionality anyway, not some hard ANSI requirement).
102 (warn "can't set name for closure, leaving name unchanged"))
104 ;; The other function subtype names are also un-settable
105 ;; but this problem seems less likely to be tickled by
106 ;; user-level code, so we can give a implementor-level
107 ;; "error" (warning) message.
108 (warn "can't set function name ((~S function)=~S), leaving it unchanged"
109 'widetag-of widetag))))
113 ;; FIXME: This business of going through %FUN-NAME and then globaldb
114 ;; is the way CMU CL did it, but it doesn't really seem right.
115 ;; When/if weak hash tables become supported again, using a weak
116 ;; hash table to maintain the object/documentation association would
117 ;; probably be better.
118 (let ((name (%fun-name x)))
119 (when (and name (typep name '(or symbol cons)))
120 (values (info :function :documentation name)))))
122 ;;; various environment inquiries
124 (defvar *features* '#.sb-cold:*shebang-features*
126 "a list of symbols that describe features provided by the
129 (defun machine-instance ()
131 "Return a string giving the name of the local machine."
132 (sb!unix:unix-gethostname))
134 (defvar *machine-version*)
136 (defun machine-version ()
138 "Return a string describing the version of the computer hardware we
139 are running on, or NIL if we can't find any useful information."
140 (unless (boundp '*machine-version*)
141 (setf *machine-version* (get-machine-version)))
144 ;;; FIXME: Don't forget to set these in a sample site-init file.
145 ;;; FIXME: Perhaps the functions could be SETFable instead of having the
146 ;;; interface be through special variables? As far as I can tell
147 ;;; from ANSI 11.1.2.1.1 "Constraints on the COMMON-LISP Package
148 ;;; for Conforming Implementations" it is kosher to add a SETF function for
149 ;;; a symbol in COMMON-LISP..
150 (defvar *short-site-name* nil
152 "The value of SHORT-SITE-NAME.")
153 (defvar *long-site-name* nil
154 #!+sb-doc "the value of LONG-SITE-NAME")
155 (defun short-site-name ()
157 "Return a string with the abbreviated site name, or NIL if not known."
159 (defun long-site-name ()
161 "Return a string with the long form of the site name, or NIL if not known."
165 (defvar *ed-functions* nil
166 "See function documentation for ED.")
168 (defun ed (&optional x)
169 "Starts the editor (on a file or a function if named). Functions
170 from the list *ED-FUNCTIONS* are called in order with X as an argument
171 until one of them returns non-NIL; these functions are responsible for
172 signalling a FILE-ERROR to indicate failure to perform an operation on
174 (dolist (fun *ed-functions*
175 (error 'extension-failure
176 :format-control "Don't know how to ~S ~A"
177 :format-arguments (list 'ed x)
178 :references (list '(:sbcl :variable *ed-functions*))))
179 (when (funcall fun x)
184 ;;; Each time we start dribbling to a new stream, we put it in
185 ;;; *DRIBBLE-STREAM*, and push a list of *DRIBBLE-STREAM*, *STANDARD-INPUT*,
186 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* in *PREVIOUS-DRIBBLE-STREAMS*.
187 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* is changed to a broadcast stream that
188 ;;; broadcasts to *DRIBBLE-STREAM* and to the old values of the variables.
189 ;;; *STANDARD-INPUT* is changed to an echo stream that echos input from the old
190 ;;; value of standard input to *DRIBBLE-STREAM*.
192 ;;; When dribble is called with no arguments, *DRIBBLE-STREAM* is closed,
193 ;;; and the values of *DRIBBLE-STREAM*, *STANDARD-INPUT*, and
194 ;;; *STANDARD-OUTPUT* are popped from *PREVIOUS-DRIBBLE-STREAMS*.
196 (defvar *previous-dribble-streams* nil)
197 (defvar *dribble-stream* nil)
199 (defun dribble (&optional pathname &key (if-exists :append))
201 "With a file name as an argument, dribble opens the file and sends a
202 record of further I/O to that file. Without an argument, it closes
203 the dribble file, and quits logging."
205 (let* ((new-dribble-stream
209 :if-does-not-exist :create))
211 (make-broadcast-stream *standard-output* new-dribble-stream))
213 (make-broadcast-stream *error-output* new-dribble-stream))
215 (make-echo-stream *standard-input* new-dribble-stream)))
216 (push (list *dribble-stream* *standard-input* *standard-output*
218 *previous-dribble-streams*)
219 (setf *dribble-stream* new-dribble-stream)
220 (setf *standard-input* new-standard-input)
221 (setf *standard-output* new-standard-output)
222 (setf *error-output* new-error-output)))
223 ((null *dribble-stream*)
224 (error "not currently dribbling"))
226 (let ((old-streams (pop *previous-dribble-streams*)))
227 (close *dribble-stream*)
228 (setf *dribble-stream* (first old-streams))
229 (setf *standard-input* (second old-streams))
230 (setf *standard-output* (third old-streams))
231 (setf *error-output* (fourth old-streams)))))
234 (defun %byte-blt (src src-start dst dst-start dst-end)
235 (%byte-blt src src-start dst dst-start dst-end))
237 ;;;; some *LOAD-FOO* variables
239 (defvar *load-print* nil
241 "the default for the :PRINT argument to LOAD")
243 (defvar *load-verbose* nil
244 ;; Note that CMU CL's default for this was T, and ANSI says it's
245 ;; implementation-dependent. We choose NIL on the theory that it's
246 ;; a nicer default behavior for Unix programs.
248 "the default for the :VERBOSE argument to LOAD")