1.0.13.19: Odds and ends (OpenBSD NEWS, minor bug in PROBE-FILE, mkstemp())
[sbcl.git] / src / code / target-extensions.lisp
1 ;;;; This file contains things for the extensions packages (SB-EXT and
2 ;;;; also "internal extensions" SB-INT) which can't be built at
3 ;;;; cross-compile time, and perhaps also some things which might as
4 ;;;; well not be built at cross-compile time because they're not
5 ;;;; needed then. Things which can't be built at cross-compile time
6 ;;;; (e.g. because they need machinery which only exists inside SBCL's
7 ;;;; implementation of the LISP package) do not belong in this file.
8
9 ;;;; This software is part of the SBCL system. See the README file for
10 ;;;; more information.
11 ;;;;
12 ;;;; This software is derived from the CMU CL system, which was
13 ;;;; written at Carnegie Mellon University and released into the
14 ;;;; public domain. The software is in the public domain and is
15 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
16 ;;;; files for more information.
17
18 (in-package "SB!IMPL")
19 \f
20 ;;;; variables initialization and shutdown sequences
21
22 ;; (Most of the save-a-core functionality is defined later, in its
23 ;; own file, but we'd like to have these symbols declared special
24 ;; and initialized ASAP.)
25 (defvar *save-hooks* nil
26   #!+sb-doc
27   "This is a list of functions which are called in an unspecified
28 order before creating a saved core image. Unused by SBCL itself:
29 reserved for user and applications.")
30
31 (defvar *init-hooks* nil
32   #!+sb-doc
33   "This is a list of functions which are called in an unspecified
34 order when a saved core image starts up, after the system itself has
35 been initialized. Unused by SBCL itself: reserved for user and
36 applications.")
37
38 \f
39 ;;; Binary search for simple vectors
40 (defun binary-search (value seq &key (key #'identity))
41   (declare (simple-vector seq))
42   (labels ((recurse (start end)
43              (when (< start end)
44                (let* ((i (+ start (truncate (- end start) 2)))
45                       (elt (svref seq i))
46                       (key-value (funcall key elt)))
47                  (cond ((< value key-value)
48                         (recurse start i))
49                        ((> value key-value)
50                         (recurse (1+ i) end))
51                        (t
52                         elt))))))
53     (recurse 0 (length seq))))
54
55 \f
56 ;;; like LISTEN, but any whitespace in the input stream will be flushed
57 (defun listen-skip-whitespace (&optional (stream *standard-input*))
58   (do ((char (read-char-no-hang stream nil nil nil)
59              (read-char-no-hang stream nil nil nil)))
60       ((null char) nil)
61     (cond ((not (whitespace[1]p char))
62            (unread-char char stream)
63            (return t)))))
64 \f
65 ;;;; helpers for C library calls
66
67 ;;; Signal a SIMPLE-CONDITION/ERROR condition associated with an ANSI C
68 ;;; errno problem, arranging for the condition's print representation
69 ;;; to be similar to the ANSI C perror(3) style.
70 (defun simple-perror (prefix-string
71                       &key
72                       (errno (get-errno))
73                       (simple-error 'simple-error)
74                       other-condition-args)
75   (declare (type symbol simple-error))
76   (aver (subtypep simple-error 'simple-condition))
77   (aver (subtypep simple-error 'error))
78   (apply #'error
79          simple-error
80          :format-control "~@<~A: ~2I~_~A~:>"
81          :format-arguments (list prefix-string (strerror errno))
82          other-condition-args))
83
84 ;;; Constructing shortish strings one character at a time. More efficient then
85 ;;; a string-stream, as can directly use simple-base-strings when applicable,
86 ;;; and if the maximum size is know doesn't need to copy the result at all --
87 ;;; but if the result is going to be HUGE, string-streams will win.
88 (defmacro with-push-char ((&key (element-type 'character) (initial-size 28)) &body body)
89   (with-unique-names (string size pointer)
90     `(let* ((,size ,initial-size)
91             (,string (make-array ,size :element-type ',element-type))
92             (,pointer 0))
93        (declare (type (integer 0 ,sb!xc:array-dimension-limit) ,size)
94                 (type (integer 0 ,(1- sb!xc:array-dimension-limit)) ,pointer)
95                 (type (simple-array ,element-type (*)) ,string))
96        (flet ((push-char (char)
97                 (declare (optimize (sb!c::insert-array-bounds-checks 0)))
98                 (when (= ,pointer ,size)
99                   (let ((old ,string))
100                     (setf ,size (* 2 (+ ,size 2))
101                           ,string (make-array ,size :element-type ',element-type))
102                     (replace ,string old)))
103                 (setf (char ,string ,pointer) char)
104                 (incf ,pointer))
105               (get-pushed-string ()
106                 (let ((string ,string)
107                       (size ,pointer))
108                   (setf ,size 0
109                         ,pointer 0
110                         ,string ,(coerce "" `(simple-array ,element-type (*))))
111                   ;; This is really local, so we can be destructive!
112                   (%shrink-vector string size)
113                   string)))
114          ,@body))))