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