1.0.47.4: Fix slot-documentation for SB-POSIX:TERMIOS slot CC.
[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 ;;; Note: this function gets called by the compiler (as of 1.0.17.x,
21 ;;; in MAYBE-INLINE-SYNTACTIC-CLOSURE), and so although ANSI says
22 ;;; we're allowed to return NIL here freely, it seems plausible that
23 ;;; small changes to the circumstances under which this function
24 ;;; returns non-NIL might have subtle consequences on the compiler.
25 ;;; So it might be desirable to have the compiler not rely on this
26 ;;; function, eventually.
27 (defun function-lambda-expression (fun)
28   "Return (VALUES DEFINING-LAMBDA-EXPRESSION CLOSURE-P NAME), where
29   DEFINING-LAMBDA-EXPRESSION is NIL if unknown, or a suitable argument
30   to COMPILE otherwise, CLOSURE-P is non-NIL if the function's definition
31   might have been enclosed in some non-null lexical environment, and
32   NAME is some name (for debugging only) or NIL if there is no name."
33   (declare (type function fun))
34   (etypecase fun
35     #!+sb-eval
36     (sb!eval:interpreted-function
37      (let ((name (sb!eval:interpreted-function-name fun))
38            (lambda-list (sb!eval:interpreted-function-lambda-list fun))
39            (declarations (sb!eval:interpreted-function-declarations fun))
40            (body (sb!eval:interpreted-function-body fun)))
41        (values `(lambda ,lambda-list
42                   ,@(when declarations `((declare ,@declarations)))
43                   ,@body)
44                t name)))
45     (function
46      (let* ((fun (%simple-fun-self (%fun-fun fun)))
47             (name (%fun-name fun))
48             (code (sb!di::fun-code-header fun))
49             (info (sb!kernel:%code-debug-info code)))
50        (if info
51            (let ((source (sb!c::debug-info-source info)))
52              (cond ((and (sb!c::debug-source-form source)
53                          (eq (sb!c::debug-source-function source) fun))
54                     (values (sb!c::debug-source-form source)
55                             nil
56                             name))
57                    ((legal-fun-name-p name)
58                     (let ((exp (fun-name-inline-expansion name)))
59                       (values exp (not exp) name)))
60                    (t
61                     (values nil t name))))
62            (values nil t name))))))
63
64 ;;;; Generalizing over SIMPLE-FUN, CLOSURE, and FUNCALLABLE-INSTANCEs
65
66 ;;; Underlying SIMPLE-FUN
67 (defun %fun-fun (function)
68   (declare (function function))
69   (typecase function
70     (simple-fun
71      function)
72     (closure
73      (%closure-fun function))
74     (funcallable-instance
75      (%fun-fun (funcallable-instance-fun function)))))
76
77 (defun %fun-lambda-list (function)
78   (typecase function
79     #!+sb-eval
80     (sb!eval:interpreted-function
81      (sb!eval:interpreted-function-debug-lambda-list function))
82     (t
83      (%simple-fun-arglist (%fun-fun function)))))
84
85 (defun (setf %fun-lambda-list) (new-value function)
86   (typecase function
87     #!+sb-eval
88     (sb!eval:interpreted-function
89      (setf (sb!eval:interpreted-function-debug-lambda-list function) new-value))
90     ;; FIXME: Eliding general funcallable-instances for now.
91     ((or simple-fun closure)
92      (setf (%simple-fun-arglist (%fun-fun function)) new-value)))
93   new-value)
94
95 (defun %fun-type (function)
96   (%simple-fun-type (%fun-fun function)))
97
98 ;;; a SETFable function to return the associated debug name for FUN
99 ;;; (i.e., the third value returned from CL:FUNCTION-LAMBDA-EXPRESSION),
100 ;;; or NIL if there's none
101 (defun %fun-name (function)
102   (typecase function
103     #!+sb-eval
104     (sb!eval:interpreted-function
105      (sb!eval:interpreted-function-debug-name function))
106     (t
107      (%simple-fun-name (%fun-fun function)))))
108
109 (defun (setf %fun-name) (new-value function)
110   (typecase function
111     #!+sb-eval
112     (sb!eval:interpreted-function
113      (setf (sb!eval:interpreted-function-debug-name function) new-value))
114     ;; FIXME: Eliding general funcallable-instances for now.
115     ((or simple-fun closure)
116      (setf (%simple-fun-name (%fun-fun function)) new-value)))
117   new-value)
118
119 (defun %fun-doc (function)
120   (typecase function
121     #!+sb-eval
122     (sb!eval:interpreted-function
123      (sb!eval:interpreted-function-documentation function))
124     (t
125      (%simple-fun-doc (%fun-fun function)))))
126
127 (defun (setf %fun-doc) (new-value function)
128   (declare (type (or null string) new-value))
129   (typecase function
130     #!+sb-eval
131     (sb!eval:interpreted-function
132      (setf (sb!eval:interpreted-function-documentation function) new-value))
133     ((or simple-fun closure)
134      (setf (%simple-fun-doc (%fun-fun function)) new-value)))
135   new-value)
136 \f
137 ;;; various environment inquiries
138
139 (defvar *features* '#.sb-cold:*shebang-features*
140   #!+sb-doc
141   "a list of symbols that describe features provided by the
142    implementation")
143
144 (defun machine-instance ()
145   #!+sb-doc
146   "Return a string giving the name of the local machine."
147   #!+win32 (sb!win32::get-computer-name)
148   #!-win32 (sb!unix:unix-gethostname))
149
150 (defvar *machine-version*)
151
152 (defun machine-version ()
153   #!+sb-doc
154   "Return a string describing the version of the computer hardware we
155 are running on, or NIL if we can't find any useful information."
156   (unless (boundp '*machine-version*)
157     (setf *machine-version* (get-machine-version)))
158   *machine-version*)
159
160 ;;; FIXME: Don't forget to set these in a sample site-init file.
161 ;;; FIXME: Perhaps the functions could be SETFable instead of having the
162 ;;; interface be through special variables? As far as I can tell
163 ;;; from ANSI 11.1.2.1.1 "Constraints on the COMMON-LISP Package
164 ;;; for Conforming Implementations" it is kosher to add a SETF function for
165 ;;; a symbol in COMMON-LISP..
166 (defvar *short-site-name* nil
167   #!+sb-doc
168   "The value of SHORT-SITE-NAME.")
169 (defvar *long-site-name* nil
170   #!+sb-doc "the value of LONG-SITE-NAME")
171 (defun short-site-name ()
172   #!+sb-doc
173   "Return a string with the abbreviated site name, or NIL if not known."
174   *short-site-name*)
175 (defun long-site-name ()
176   #!+sb-doc
177   "Return a string with the long form of the site name, or NIL if not known."
178   *long-site-name*)
179 \f
180 ;;;; ED
181 (defvar *ed-functions* nil
182   "See function documentation for ED.")
183
184 (defun ed (&optional x)
185   "Starts the editor (on a file or a function if named).  Functions
186 from the list *ED-FUNCTIONS* are called in order with X as an argument
187 until one of them returns non-NIL; these functions are responsible for
188 signalling a FILE-ERROR to indicate failure to perform an operation on
189 the file system."
190   (dolist (fun *ed-functions*
191            (error 'extension-failure
192                   :format-control "Don't know how to ~S ~A"
193                   :format-arguments (list 'ed x)
194                   :references (list '(:sbcl :variable *ed-functions*))))
195     (when (funcall fun x)
196       (return t))))
197 \f
198 ;;;; dribble stuff
199
200 ;;; Each time we start dribbling to a new stream, we put it in
201 ;;; *DRIBBLE-STREAM*, and push a list of *DRIBBLE-STREAM*, *STANDARD-INPUT*,
202 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* in *PREVIOUS-DRIBBLE-STREAMS*.
203 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* is changed to a broadcast stream that
204 ;;; broadcasts to *DRIBBLE-STREAM* and to the old values of the variables.
205 ;;; *STANDARD-INPUT* is changed to an echo stream that echos input from the old
206 ;;; value of standard input to *DRIBBLE-STREAM*.
207 ;;;
208 ;;; When dribble is called with no arguments, *DRIBBLE-STREAM* is closed,
209 ;;; and the values of *DRIBBLE-STREAM*, *STANDARD-INPUT*, and
210 ;;; *STANDARD-OUTPUT* are popped from *PREVIOUS-DRIBBLE-STREAMS*.
211
212 (defvar *previous-dribble-streams* nil)
213 (defvar *dribble-stream* nil)
214
215 (defun dribble (&optional pathname &key (if-exists :append))
216   #!+sb-doc
217   "With a file name as an argument, dribble opens the file and sends a
218   record of further I/O to that file. Without an argument, it closes
219   the dribble file, and quits logging."
220   (cond (pathname
221          (let* ((new-dribble-stream
222                  (open pathname
223                        :direction :output
224                        :if-exists if-exists
225                        :if-does-not-exist :create))
226                 (new-standard-output
227                  (make-broadcast-stream *standard-output* new-dribble-stream))
228                 (new-error-output
229                  (make-broadcast-stream *error-output* new-dribble-stream))
230                 (new-standard-input
231                  (make-echo-stream *standard-input* new-dribble-stream)))
232            (push (list *dribble-stream* *standard-input* *standard-output*
233                        *error-output*)
234                  *previous-dribble-streams*)
235            (setf *dribble-stream* new-dribble-stream)
236            (setf *standard-input* new-standard-input)
237            (setf *standard-output* new-standard-output)
238            (setf *error-output* new-error-output)))
239         ((null *dribble-stream*)
240          (error "not currently dribbling"))
241         (t
242          (let ((old-streams (pop *previous-dribble-streams*)))
243            (close *dribble-stream*)
244            (setf *dribble-stream* (first old-streams))
245            (setf *standard-input* (second old-streams))
246            (setf *standard-output* (third old-streams))
247            (setf *error-output* (fourth old-streams)))))
248   (values))
249
250 (defun %byte-blt (src src-start dst dst-start dst-end)
251   (%byte-blt src src-start dst dst-start dst-end))
252
253 ;;;; some *LOAD-FOO* variables
254
255 (defvar *load-print* nil
256   #!+sb-doc
257   "the default for the :PRINT argument to LOAD")
258
259 (defvar *load-verbose* nil
260   ;; Note that CMU CL's default for this was T, and ANSI says it's
261   ;; implementation-dependent. We choose NIL on the theory that it's
262   ;; a nicer default behavior for Unix programs.
263   #!+sb-doc
264   "the default for the :VERBOSE argument to LOAD")