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