0.8.11.2:
[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 (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)
36                          nil
37                          name))
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?
41                 ((stringp name)
42                  (values nil t name))
43                 (t
44                  (let ((exp (fun-name-inline-expansion name)))
45                    (if exp
46                        (values exp nil name)
47                        (values nil t name))))))
48         (values nil t name))))
49
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
63      (%simple-fun-name
64       (funcallable-instance-fun fun)))))
65
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)))
69     (case widetag
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. 
86        ;;
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"))
93       (t
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))))
100   new-name)
101
102 (defun %fun-doc (x)
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)))))
111 \f
112 ;;; various environment inquiries
113
114 (defvar *features* '#.sb-cold:*shebang-features*
115   #!+sb-doc
116   "a list of symbols that describe features provided by the
117    implementation")
118
119 (defun machine-instance ()
120   #!+sb-doc
121   "Return a string giving the name of the local machine."
122   (sb!unix:unix-gethostname))
123
124 (defvar *machine-version*)
125
126 (defun machine-version ()
127   #!+sb-doc
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)))
132   *machine-version*)
133   
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
141   #!+sb-doc
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 ()
146   #!+sb-doc
147   "Return a string with the abbreviated site name, or NIL if not known."
148   *short-site-name*)
149 (defun long-site-name ()
150   #!+sb-doc
151   "Return a string with the long form of the site name, or NIL if not known."
152   *long-site-name*)
153 \f
154 ;;;; ED
155 (defvar *ed-functions* nil
156   "See function documentation for ED.")
157
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
163 the file system."
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)
170       (return t))))
171 \f
172 ;;;; dribble stuff
173
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*.
181 ;;;
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*.
185
186 (defvar *previous-dribble-streams* nil)
187 (defvar *dribble-stream* nil)
188
189 (defun dribble (&optional pathname &key (if-exists :append))
190   #!+sb-doc
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."
194   (cond (pathname
195          (let* ((new-dribble-stream
196                  (open pathname
197                        :direction :output
198                        :if-exists if-exists
199                        :if-does-not-exist :create))
200                 (new-standard-output
201                  (make-broadcast-stream *standard-output* new-dribble-stream))
202                 (new-error-output
203                  (make-broadcast-stream *error-output* new-dribble-stream))
204                 (new-standard-input
205                  (make-echo-stream *standard-input* new-dribble-stream)))
206            (push (list *dribble-stream* *standard-input* *standard-output*
207                        *error-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"))
215         (t
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)))))
222   (values))
223
224 (defun %byte-blt (src src-start dst dst-start dst-end)
225   (%byte-blt src src-start dst dst-start dst-end))