20ea6138760c92fb2365d8cd8886a4d3d1f95808
[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
17 ;;; cobbled from stuff in describe.lisp.
18 (defun function-doc (x)
19   (let ((name
20          (case (get-type x)
21            (#.sb!vm:closure-header-type
22             (%function-name (%closure-function x)))
23            ((#.sb!vm:function-header-type #.sb!vm:closure-function-header-type)
24             (%function-name x))
25            (#.sb!vm:funcallable-instance-header-type
26             (typecase x
27               (byte-function
28                (sb!c::byte-function-name x))
29               (byte-closure
30                (sb!c::byte-function-name (byte-closure-function x)))
31               (sb!eval:interpreted-function
32                (multiple-value-bind (exp closure-p dname)
33                    (sb!eval:interpreted-function-lambda-expression x)
34                  (declare (ignore exp closure-p))
35                  dname))
36               (t ;; funcallable-instance
37                (%function-name
38                 (funcallable-instance-function x))))))))
39     (when (and name (typep name '(or symbol cons)))
40       (values (info :function :documentation name)))))
41
42 (defvar *features* '#.sb-cold:*shebang-features*
43   #!+sb-doc
44   "a list of symbols that describe features provided by the
45    implementation")
46 \f
47 ;;; various environment inquiries
48
49 (defun machine-instance ()
50   #!+sb-doc
51   "Return a string giving the name of the local machine."
52   (sb!unix:unix-gethostname))
53
54 ;;; FIXME: Don't forget to set these in a sample site-init file.
55 ;;; FIXME: Perhaps the functions could be SETFable instead of having the
56 ;;; interface be through special variables? As far as I can tell
57 ;;; from ANSI 11.1.2.1.1 "Constraints on the COMMON-LISP Package
58 ;;; for Conforming Implementations" it is kosher to add a SETF function for
59 ;;; a symbol in COMMON-LISP..
60 (defvar *short-site-name* nil
61   #!+sb-doc
62   "The value of SHORT-SITE-NAME.")
63 (defvar *long-site-name* nil
64   #!+sb-doc "the value of LONG-SITE-NAME")
65 (defun short-site-name ()
66   #!+sb-doc
67   "Returns a string with the abbreviated site name, or NIL if not known."
68   *short-site-name*)
69 (defun long-site-name ()
70   #!+sb-doc
71   "Returns a string with the long form of the site name, or NIL if not known."
72   *long-site-name*)
73 \f
74 ;;;; dribble stuff
75
76 ;;; Each time we start dribbling to a new stream, we put it in
77 ;;; *DRIBBLE-STREAM*, and push a list of *DRIBBLE-STREAM*, *STANDARD-INPUT*,
78 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* in *PREVIOUS-DRIBBLE-STREAMS*.
79 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* is changed to a broadcast stream that
80 ;;; broadcasts to *DRIBBLE-STREAM* and to the old values of the variables.
81 ;;; *STANDARD-INPUT* is changed to an echo stream that echos input from the old
82 ;;; value of standard input to *DRIBBLE-STREAM*.
83 ;;;
84 ;;; When dribble is called with no arguments, *DRIBBLE-STREAM* is closed,
85 ;;; and the values of *DRIBBLE-STREAM*, *STANDARD-INPUT*, and
86 ;;; *STANDARD-OUTPUT* are popped from *PREVIOUS-DRIBBLE-STREAMS*.
87
88 (defvar *previous-dribble-streams* nil)
89 (defvar *dribble-stream* nil)
90
91 (defun dribble (&optional pathname &key (if-exists :append))
92   #!+sb-doc
93   "With a file name as an argument, dribble opens the file and sends a
94   record of further I/O to that file. Without an argument, it closes
95   the dribble file, and quits logging."
96   (cond (pathname
97          (let* ((new-dribble-stream
98                  (open pathname
99                        :direction :output
100                        :if-exists if-exists
101                        :if-does-not-exist :create))
102                 (new-standard-output
103                  (make-broadcast-stream *standard-output* new-dribble-stream))
104                 (new-error-output
105                  (make-broadcast-stream *error-output* new-dribble-stream))
106                 (new-standard-input
107                  (make-echo-stream *standard-input* new-dribble-stream)))
108            (push (list *dribble-stream* *standard-input* *standard-output*
109                        *error-output*)
110                  *previous-dribble-streams*)
111            (setf *dribble-stream* new-dribble-stream)
112            (setf *standard-input* new-standard-input)
113            (setf *standard-output* new-standard-output)
114            (setf *error-output* new-error-output)))
115         ((null *dribble-stream*)
116          (error "not currently dribbling"))
117         (t
118          (let ((old-streams (pop *previous-dribble-streams*)))
119            (close *dribble-stream*)
120            (setf *dribble-stream* (first old-streams))
121            (setf *standard-input* (second old-streams))
122            (setf *standard-output* (third old-streams))
123            (setf *error-output* (fourth old-streams)))))
124   (values))
125
126 (defun %byte-blt (src src-start dst dst-start dst-end)
127   (%byte-blt src src-start dst dst-start dst-end))