0.9.8.7:
[sbcl.git] / src / code / target-misc.lisp
1 ;;;; Environment query functions, DOCUMENTATION and DRIBBLE.
2 ;;;;
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.
5
6 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; more information.
8 ;;;;
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.
14
15 (in-package "SB!IMPL")
16 \f
17 ;;;; function names and documentation
18
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)))
31       (if info
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)
36                          nil
37                          name))
38                 ((legal-fun-name-p name)
39                  (let ((exp (fun-name-inline-expansion name)))
40                    (values exp (not exp) name)))
41                 (t
42                  (values nil t name))))
43         (values nil t name))))
44
45 (defun closurep (object)
46   (= sb!vm:closure-header-widetag (widetag-of object)))
47
48 (defun %fun-fun (function)
49   (declare (function function))
50   (case (widetag-of function)
51     (#.sb!vm:simple-fun-header-widetag
52      function)
53     (#.sb!vm:closure-header-widetag
54      (%closure-fun function))
55     (#.sb!vm:funcallable-instance-header-widetag
56      (%fun-fun (funcallable-instance-fun function)))))
57
58 (defun %closure-values (object)
59   (declare (function object))
60   (loop for index from 0
61      below (- (get-closure-length object) (1- sb!vm:closure-info-offset))
62      collect (%closure-index-ref object index)))
63
64 (defun %fun-lambda-list (object)
65   (%simple-fun-arglist (%fun-fun object)))
66
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)))
72
73 (defun %fun-type (function)
74   (%simple-fun-type (%fun-fun function)))
75
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)))
79     (case widetag
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.
96        ;;
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"))
103       (t
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))))
110   new-name)
111
112 (defun %fun-doc (x)
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)))))
121 \f
122 ;;; various environment inquiries
123
124 (defvar *features* '#.sb-cold:*shebang-features*
125   #!+sb-doc
126   "a list of symbols that describe features provided by the
127    implementation")
128
129 (defun machine-instance ()
130   #!+sb-doc
131   "Return a string giving the name of the local machine."
132   #!+win32 "some-random-windows-box"
133   #!-win32 (sb!unix:unix-gethostname))
134
135 (defvar *machine-version*)
136
137 (defun machine-version ()
138   #!+sb-doc
139   "Return a string describing the version of the computer hardware we
140 are running on, or NIL if we can't find any useful information."
141   (unless (boundp '*machine-version*)
142     (setf *machine-version* (get-machine-version)))
143   *machine-version*)
144
145 ;;; FIXME: Don't forget to set these in a sample site-init file.
146 ;;; FIXME: Perhaps the functions could be SETFable instead of having the
147 ;;; interface be through special variables? As far as I can tell
148 ;;; from ANSI 11.1.2.1.1 "Constraints on the COMMON-LISP Package
149 ;;; for Conforming Implementations" it is kosher to add a SETF function for
150 ;;; a symbol in COMMON-LISP..
151 (defvar *short-site-name* nil
152   #!+sb-doc
153   "The value of SHORT-SITE-NAME.")
154 (defvar *long-site-name* nil
155   #!+sb-doc "the value of LONG-SITE-NAME")
156 (defun short-site-name ()
157   #!+sb-doc
158   "Return a string with the abbreviated site name, or NIL if not known."
159   *short-site-name*)
160 (defun long-site-name ()
161   #!+sb-doc
162   "Return a string with the long form of the site name, or NIL if not known."
163   *long-site-name*)
164 \f
165 ;;;; ED
166 (defvar *ed-functions* nil
167   "See function documentation for ED.")
168
169 (defun ed (&optional x)
170   "Starts the editor (on a file or a function if named).  Functions
171 from the list *ED-FUNCTIONS* are called in order with X as an argument
172 until one of them returns non-NIL; these functions are responsible for
173 signalling a FILE-ERROR to indicate failure to perform an operation on
174 the file system."
175   (dolist (fun *ed-functions*
176            (error 'extension-failure
177                   :format-control "Don't know how to ~S ~A"
178                   :format-arguments (list 'ed x)
179                   :references (list '(:sbcl :variable *ed-functions*))))
180     (when (funcall fun x)
181       (return t))))
182 \f
183 ;;;; dribble stuff
184
185 ;;; Each time we start dribbling to a new stream, we put it in
186 ;;; *DRIBBLE-STREAM*, and push a list of *DRIBBLE-STREAM*, *STANDARD-INPUT*,
187 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* in *PREVIOUS-DRIBBLE-STREAMS*.
188 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* is changed to a broadcast stream that
189 ;;; broadcasts to *DRIBBLE-STREAM* and to the old values of the variables.
190 ;;; *STANDARD-INPUT* is changed to an echo stream that echos input from the old
191 ;;; value of standard input to *DRIBBLE-STREAM*.
192 ;;;
193 ;;; When dribble is called with no arguments, *DRIBBLE-STREAM* is closed,
194 ;;; and the values of *DRIBBLE-STREAM*, *STANDARD-INPUT*, and
195 ;;; *STANDARD-OUTPUT* are popped from *PREVIOUS-DRIBBLE-STREAMS*.
196
197 (defvar *previous-dribble-streams* nil)
198 (defvar *dribble-stream* nil)
199
200 (defun dribble (&optional pathname &key (if-exists :append))
201   #!+sb-doc
202   "With a file name as an argument, dribble opens the file and sends a
203   record of further I/O to that file. Without an argument, it closes
204   the dribble file, and quits logging."
205   (cond (pathname
206          (let* ((new-dribble-stream
207                  (open pathname
208                        :direction :output
209                        :if-exists if-exists
210                        :if-does-not-exist :create))
211                 (new-standard-output
212                  (make-broadcast-stream *standard-output* new-dribble-stream))
213                 (new-error-output
214                  (make-broadcast-stream *error-output* new-dribble-stream))
215                 (new-standard-input
216                  (make-echo-stream *standard-input* new-dribble-stream)))
217            (push (list *dribble-stream* *standard-input* *standard-output*
218                        *error-output*)
219                  *previous-dribble-streams*)
220            (setf *dribble-stream* new-dribble-stream)
221            (setf *standard-input* new-standard-input)
222            (setf *standard-output* new-standard-output)
223            (setf *error-output* new-error-output)))
224         ((null *dribble-stream*)
225          (error "not currently dribbling"))
226         (t
227          (let ((old-streams (pop *previous-dribble-streams*)))
228            (close *dribble-stream*)
229            (setf *dribble-stream* (first old-streams))
230            (setf *standard-input* (second old-streams))
231            (setf *standard-output* (third old-streams))
232            (setf *error-output* (fourth old-streams)))))
233   (values))
234
235 (defun %byte-blt (src src-start dst dst-start dst-end)
236   (%byte-blt src src-start dst dst-start dst-end))
237
238 ;;;; some *LOAD-FOO* variables
239
240 (defvar *load-print* nil
241   #!+sb-doc
242   "the default for the :PRINT argument to LOAD")
243
244 (defvar *load-verbose* nil
245   ;; Note that CMU CL's default for this was T, and ANSI says it's
246   ;; implementation-dependent. We choose NIL on the theory that it's
247   ;; a nicer default behavior for Unix programs.
248   #!+sb-doc
249   "the default for the :VERBOSE argument to LOAD")