Debian Codegraph @

src/lsp/iolib.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: SYSTEM -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya.
6 ;;;; Copyright (c) 1990, Giuseppe Attardi.
7 ;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll.
8 ;;;;
9 ;;;; See file 'LICENSE' for the copyright details.
10
11 ;;;; The IO library.
12
13 (in-package "SYSTEM")
14
15 (defmacro with-open-stream ((var stream) &rest body)
16 "Syntax: (with-open-stream (var stream-form) {decl}* {form}*)
17 Evaluates FORMs with VAR bound to the value of STREAM-FORM. The stream is
18 automatically closed on exit."
19 (multiple-value-bind (ds b)
20 (find-declarations body)
21 `(LET ((,var ,stream))
22 ,@ds
23 (UNWIND-PROTECT
24 (PROGN ,@b)
25 (CLOSE ,var)))))
26
27 (defmacro with-input-from-string ((var string &key index (start 0) end) &rest body)
28 "Syntax: (with-input-from-string (var string-form {keyword value}*)
29 {decl}* {form}*)
30 Evaluates FORMs with VAR bound to a string input stream from the string that
31 is the value of STRING-FORM. The stream is automatically closed on exit.
32 Possible keywords are :INDEX, :START, and :END."
33 (multiple-value-bind (ds b)
34 (find-declarations body)
35 `(let ((,var (make-string-input-stream ,string ,start ,end)))
36 ,@ds
37 (unwind-protect
38 ,(if index
39 `(multiple-value-prog1
40 (progn ,@b)
41 (setf ,index (file-position ,var)))
42 `(progn ,@b))
43 (close ,var)))))
44
45 (defmacro with-output-to-string ((var &optional string &rest r &key element-type) &rest body)
46 "Syntax: (with-output-to-string (var [string-form]) {decl}* {form}*)
47 Evaluates FORMs with VAR bound to a string output stream to the string that is
48 the value of STRING-FORM. If STRING-FORM is not given, a new string is used.
49 The stream is automatically closed on exit and the string is returned."
50 (multiple-value-bind (decls body)
51 (find-declarations body)
52 (if string
53 (with-gensyms (elt-type-var)
54 `(let ((,var (make-string-output-stream-from-string ,string))
55 ;; We must evaluate element-type for side effects.
56 (,elt-type-var ,element-type))
57 (declare (ignore ,elt-type-var))
58 ,@decls
59 (unwind-protect (progn ,@body)
60 (close ,var))))
61 `(let ((,var (make-string-output-stream ,@r)))
62 ,@decls
63 (unwind-protect (progn
64 ,@body
65 (get-output-stream-string ,var))
66 (close ,var))))))
67
68 (defun read-from-string (string
69 &optional (eof-error-p t) eof-value
70 &key (start 0) (end (length string))
71 preserve-whitespace)
72 "Args: (string &optional (eof-error-p t) (eof-value nil)
73 &key (start 0) (end (length string)) (preserve-whitespace nil))
74 Reads an object from STRING and returns the object. As the second value,
75 returns the index to the character next to the object's representation.
76 PRESERVE-WHITESPACE specifies whether to leave the character next to the
77 object's representation."
78 (let ((stream (make-string-input-stream string start end)))
79 (if preserve-whitespace
80 (values (read-preserving-whitespace stream eof-error-p eof-value)
81 (file-position stream))
82 (values (read stream eof-error-p eof-value)
83 (file-position stream)))))
84
85 (defun si::string-to-object (string &optional (err-value nil err-value-p))
86 (if err-value-p
87 (si::safe-eval `(read-from-string ,string) nil err-value)
88 (si::safe-eval `(read-from-string ,string) nil)))
89
90 (defun write-to-string (object &rest rest
91 &aux (stream (make-string-output-stream)))
92 "Args: (object &key (escape *print-escape*) (radix *print-radix*)
93 (base *print-base*) (circle *print-circle*)
94 (pretty *print-pretty*) (level *print-level*)
95 (length *print-length*) (case *print-case*)
96 (array *print-array*) (gensym *print-gensym*))
97 Returns as a string the printed representation of OBJECT in the specified
98 mode. See the variable docs of *PRINT-...* for the mode."
99 (apply #'write object :stream stream rest)
100 (get-output-stream-string stream))
101
102 (defun prin1-to-string (object
103 &aux (stream (make-string-output-stream)))
104 "Args: (object)
105 PRIN1s OBJECT to a new string and returns the result. Equivalent to
106 (WRITE-TO-STRING OBJECT :ESCAPE T)."
107 (prin1 object stream)
108 (get-output-stream-string stream))
109
110 (defun princ-to-string (object
111 &aux (stream (make-string-output-stream)))
112 "Args: (object)
113 PRINCs OBJECT to a new string and returns the result. Equivalent to
114 (WRITE-TO-STRING OBJECT :ESCAPE NIL)."
115 (princ object stream)
116 (get-output-stream-string stream))
117
118 (defmacro with-open-file ((stream . filespec) &rest body)
119 "Syntax: (with-open-file (var filespec-form {options}*) {decl}* {form}*)
120 Opens the specified file using OPTIONs, and evaluates FORMs with VAR bound to
121 a stream to/from the file. The file is automatically closed on exit. See
122 OPEN for the options."
123 (multiple-value-bind (ds b)
124 (find-declarations body)
125 `(LET ((,stream (OPEN ,@filespec)))
126 ,@ds
127 (UNWIND-PROTECT
128 (MULTIPLE-VALUE-PROG1 (PROGN ,@b) (WHEN ,stream (CLOSE ,stream)))
129 (WHEN ,stream (CLOSE ,stream :ABORT T))))))
130
131 (defun y-or-n-p (&optional string &rest args)
132 "Args: (&optional format-string &rest args)
133 Asks the user a Y-or-N question. Does FRESH-LINE, prints a message as if
134 FORMAT-STRING and ARGs were given to FORMAT, and then prints \"(Y or N)\" is
135 printed. If FORMAT-STRING is NIL, however, no prompt will appear."
136 (do ((reply))
137 (nil)
138 (when string (format *query-io* "~&~? (Y or N) " string args))
139 (setq reply (read *query-io*))
140 (cond ((string-equal (symbol-name reply) "Y")
141 (return-from y-or-n-p t))
142 ((string-equal (symbol-name reply) "N")
143 (return-from y-or-n-p nil)))))
144
145 (defun yes-or-no-p (&optional string &rest args)
146 "Args: (&optional format-string &rest args)
147 Asks the user an YES-or-NO question. Does FRESH-LINE, prints a message as if
148 FORMAT-STRING and ARGs were given to FORMAT, and then prints \"(Y or N)\" is
149 printed. If FORMAT-STRING is NIL, however, no prompt will appear."
150 (do ((reply))
151 (nil)
152 (when string (format *query-io* "~&~? (Yes or No) " string args))
153 (setq reply (read *query-io*))
154 (cond ((string-equal (symbol-name reply) "YES")
155 (return-from yes-or-no-p t))
156 ((string-equal (symbol-name reply) "NO")
157 (return-from yes-or-no-p nil)))))
158
159 (defun sharp-a-reader (stream subchar arg)
160 (declare (ignore subchar))
161 (let ((initial-contents (read stream nil nil t)))
162 (cond
163 (*read-suppress* nil)
164 ((null arg)
165 ;; readably-pretty-printed array: #A(type dims initial-contents)
166 (let ((elt-type (car initial-contents))
167 (dims (cadr initial-contents))
168 (initial-contents (caddr initial-contents)))
169 (make-array dims :element-type elt-type :initial-contents initial-contents)))
170 (t
171 (do* ((i 0 (1+ i))
172 (d nil (cons (length ic) d))
173 (ic initial-contents (if (zerop (length ic)) ic (elt ic 0))))
174 ((>= i arg)
175 (make-array (nreverse d) :initial-contents initial-contents))
176 (declare (fixnum i)))))))
177
178 (set-dispatch-macro-character #\# #\a 'sharp-a-reader)
179 (set-dispatch-macro-character #\# #\A 'sharp-a-reader)
180
181 (defun sharp-s-reader (stream subchar arg)
182 (declare (ignore subchar))
183 (when (and arg (null *read-suppress*))
184 (error "~S is an extra argument for the #s readmacro." arg))
185 (let ((l (read stream t nil t)))
186 (when *read-suppress*
187 (return-from sharp-s-reader nil))
188 (unless (get-sysprop (car l) 'is-a-structure)
189 (error "~S is not a structure." (car l)))
190 ;; Intern keywords in the keyword package.
191 (do ((ll (cdr l) (cddr ll)))
192 ((endp ll)
193 ;; Find an appropriate construtor.
194 (do ((cs (get-sysprop (car l) 'structure-constructors) (cdr cs)))
195 ((endp cs)
196 (error "The structure ~S has no structure constructor."
197 (car l)))
198 (when (symbolp (car cs))
199 (return (apply (car cs) (cdr l))))))
200 (rplaca ll (intern (string (car ll)) 'keyword)))))
201
202 (set-dispatch-macro-character #\# #\s 'sharp-s-reader)
203 (set-dispatch-macro-character #\# #\S 'sharp-s-reader)
204
205 (defparameter *dribble-closure* nil)
206
207 (defun dribble (&optional (pathname "DRIBBLE.LOG" psp))
208 "Args: (&optional filespec)
209 If FILESPEC is given, starts recording the interaction to the specified file.
210 FILESPEC may be a symbol, a string, a pathname, or a file stream. If FILESPEC
211 is not given, ends the recording."
212 (cond (*dribble-closure*
213 (funcall *dribble-closure* psp))
214 ((null psp)
215 (error "Not in dribble."))
216 (t
217 (let* ((namestring (namestring pathname))
218 (stream (open pathname :direction :output
219 :if-exists :supersede
220 :if-does-not-exist :create))
221 (dribble-stream (make-two-way-stream
222 (make-echo-stream *terminal-io* stream)
223 (make-broadcast-stream *terminal-io* stream)))
224 (standard-input *standard-input*)
225 (standard-output *standard-output*)
226 (closure #'(lambda (pathname-p)
227 (when pathname-p
228 (error "Already in dribble (to ~A)" namestring))
229 (unless (and (eq dribble-stream *standard-input*)
230 (eq dribble-stream *standard-output*))
231 (warn "Stream variables rebound while DRIBBLE is on.~%Some output may be lost."))
232 (format stream "~&Finished dribbling to ~A." namestring)
233 (close stream)
234 (setq *standard-input* standard-input
235 *standard-output* standard-output
236 *dribble-closure* nil))))
237 (multiple-value-bind (sec min hour day month year)
238 (get-decoded-time)
239 (format dribble-stream "~&Starts dribbling to ~A (~d/~d/~d, ~2,'0d:~2,'0d:~2,'0d)."
240 namestring year month day hour min sec)
241 (setq *standard-input* dribble-stream
242 *standard-output* dribble-stream
243 *dribble-closure* closure)))))
244 (values))
245
246 ;(provide 'iolib)
247
248 (defmacro with-standard-io-syntax (&body body)
249 "Syntax: ({forms}*)
250 The forms of the body are executed in a print environment that corresponds to
251 the one defined in the ANSI standard. *print-base* is 10, *print-array* is t,
252 *package* is \"CL-USER\", etc."
253 (with-clean-symbols (%progv-list)
254 `(let ((%progv-list +io-syntax-progv-list+))
255 (progv (si:cons-car %progv-list)
256 (si:cons-cdr %progv-list)
257 ,@body))))
258
259 (defmacro with-ecl-io-syntax (&body body)
260 "Syntax: ({forms}*)
261 The forms of the body are executed in a print environment that corresponds to
262 the one used internally by ECL compiled files."
263 (with-clean-symbols (%progv-list)
264 `(let ((%progv-list +ecl-syntax-progv-list+))
265 (progv (si:cons-car %progv-list)
266 (si:cons-cdr %progv-list)
267 ,@body))))
268
269 #-formatter
270 (defmacro formatter (control-string)
271 `#'(lambda (*standard-output* &rest args)
272 (si::formatter-aux *standard-output* ,control-string args)))
273
274 (defmacro print-unreadable-object
275 ((object stream &key type identity) &body body)
276 (if body
277 `(flet ((.print-unreadable-object-body. () ,@body))
278 (print-unreadable-object-function
279 ,object ,stream ,type ,identity #'.print-unreadable-object-body.))
280 `(print-unreadable-object-function ,object ,stream ,type ,identity nil)))
281
282 (let* ((basic-encodings
283 #+unicode
284 '(:UTF-8 :UCS-2 :UCS-2BE :UCS-2LE :UCS-4 :UCS-4BE :UCS-4LE
285 :ISO-8859-1 :LATIN-1 :US-ASCII :DEFAULT)
286 #-unicode
287 '(:DEFAULT))
288 (all-encodings nil))
289 (defun ext:all-encodings ()
290 (or all-encodings
291 (progn
292 (setf all-encodings basic-encodings)
293 #+unicode
294 (dolist (i (directory "sys:encodings;*"))
295 (push (intern (string-upcase (pathname-name i)) "KEYWORD") all-encodings))
296 all-encodings))))
297
298 (defun ext:load-encoding (name)
299 #-unicode
300 (warn "EXT:LOAD-ENCODING not available when ECL is built without support for Unicode")
301 #+unicode
302 (let ((ext:*default-external-format* t) ; circularity: processing filenames needs encodings itself
303 (filename (make-pathname :name (symbol-name name) :defaults "sys:encodings;")))
304 (cond ((probe-file filename)
305 (load filename :verbose nil)
306 name)
307 ((probe-file (setf filename (make-pathname :type "BIN" :defaults filename)))
308 (with-open-file (in filename :element-type '(unsigned-byte 16)
309 :external-format :big-endian)
310 (let* ((l (read-byte in))
311 (s (make-array l :element-type '(unsigned-byte 16) :initial-element 0)))
312 (read-sequence s in)
313 s)))
314 (t
315 (error "Unable to find mapping file ~A for encoding ~A" filename name)))))
316
317 (defun ext:make-encoding (mapping)
318 #-unicode
319 (error "Not a valid external format ~A" mapping)
320 #+unicode
321 (cond
322 ((symbolp mapping)
323 (let ((var (intern (symbol-name mapping) (find-package "EXT"))))
324 (unless (boundp var)
325 (setf (symbol-value var) (ext::make-encoding (load-encoding mapping))))
326 (symbol-value var)))
327 ((consp mapping)
328 (let ((output (make-hash-table :size 512 :test 'eq)))
329 (dolist (record mapping output)
330 (let* ((byte (car record))
331 (unicode (cdr record))
332 (unicode-char (code-char unicode)))
333 (when (> byte #xFF)
334 (setf (gethash (ash byte -8) output) t))
335 (setf (gethash byte output) unicode-char)
336 (setf (gethash unicode-char output) byte)))))
337 ((arrayp mapping)
338 (do* ((l (array-total-size mapping))
339 (output (make-hash-table :size (floor (* 1.5 l)) :test 'eq))
340 (i 0 (+ 2 i)))
341 ((>= i l) output)
342 (let* ((byte (aref mapping i))
343 (unicode (aref mapping (1+ i)))
344 (unicode-char (code-char unicode)))
345 (when (> byte #xFF)
346 (setf (gethash (ash byte -8) output) t))
347 (setf (gethash byte output) unicode-char)
348 (setf (gethash unicode-char output) byte))))
349 (t
350 (error "Not a valid external format ~A" mapping))))