1.0.13.51: Fixups in mkstemp wrapper used in RUN-PROGRAM.
[sbcl/simd.git] / src / code / toplevel.lisp
blob4cc0b0c5810a5b10c4d007285cbd6b812f02cb97
1 ;;;; stuff related to the toplevel read-eval-print loop, plus some
2 ;;;; other miscellaneous functions that we don't have any better place
3 ;;;; for
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
14 (in-package "SB!IMPL")
16 ;;;; magic specials initialized by GENESIS
18 ;;; FIXME: The DEFVAR here is redundant with the (DECLAIM (SPECIAL ..))
19 ;;; of all static symbols in early-impl.lisp.
20 (progn
21 (defvar sb!vm::*current-catch-block*)
22 (defvar sb!vm::*current-unwind-protect-block*)
23 (defvar *free-interrupt-context-index*))
25 ;;; specials initialized by !COLD-INIT
27 ;;; FIXME: These could be converted to DEFVARs.
28 (declaim (special #!+(or x86 x86-64) *pseudo-atomic-bits*
29 *allow-with-interrupts*
30 *interrupts-enabled*
31 *interrupt-pending*
32 *type-system-initialized*))
34 (defvar *cold-init-complete-p*)
36 ;;; counts of nested errors (with internal errors double-counted)
37 (defvar *maximum-error-depth*)
38 (defvar *current-error-depth*)
40 ;;;; default initfiles
42 (defun sysinit-pathname ()
43 (or (let ((sbcl-homedir (sbcl-homedir-pathname)))
44 (when sbcl-homedir
45 (probe-file (merge-pathnames "sbclrc" sbcl-homedir))))
46 #!+win32
47 (merge-pathnames "sbcl\\sbclrc"
48 (sb!win32::get-folder-pathname
49 sb!win32::csidl_common_appdata))
50 #!-win32
51 "/etc/sbclrc"))
53 (defun userinit-pathname ()
54 (merge-pathnames ".sbclrc" (user-homedir-pathname)))
56 (defvar *sysinit-pathname-function* #'sysinit-pathname
57 #!+sb-doc
58 "Designator for a function of zero arguments called to obtain a pathname
59 designator for the default sysinit file, or NIL. If the function returns NIL,
60 no sysinit file is used unless one has been specified on the command-line.")
62 (defvar *userinit-pathname-function* #'userinit-pathname
63 #!+sb-doc
64 "Designator for a function of zero arguments called to obtain a pathname
65 designator or a stream for the default userinit file, or NIL. If the function
66 returns NIL, no userinit file is used unless one has been specified on the
67 command-line.")
70 ;;;; miscellaneous utilities for working with with TOPLEVEL
72 ;;; Execute BODY in a context where any %END-OF-THE-WORLD (thrown e.g.
73 ;;; by QUIT) is caught and any final processing and return codes are
74 ;;; handled appropriately.
75 (defmacro handling-end-of-the-world (&body body)
76 (with-unique-names (caught)
77 `(let ((,caught (catch '%end-of-the-world
78 (/show0 "inside CATCH '%END-OF-THE-WORLD")
79 (unwind-protect
80 (progn ,@body)
81 (call-hooks "exit" *exit-hooks*)))))
82 (/show0 "back from CATCH '%END-OF-THE-WORLD, flushing output")
83 (flush-standard-output-streams)
84 (sb!thread::terminate-session)
85 (/show0 "calling UNIX-EXIT")
86 (sb!unix:unix-exit ,caught))))
88 ;;;; working with *CURRENT-ERROR-DEPTH* and *MAXIMUM-ERROR-DEPTH*
90 ;;; INFINITE-ERROR-PROTECT is used by ERROR and friends to keep us out
91 ;;; of hyperspace.
92 (defmacro infinite-error-protect (&rest forms)
93 `(unless (infinite-error-protector)
94 (/show0 "back from INFINITE-ERROR-PROTECTOR")
95 (let ((*current-error-depth* (1+ *current-error-depth*)))
96 (/show0 "in INFINITE-ERROR-PROTECT, incremented error depth")
97 ;; arbitrary truncation
98 #!+sb-show (sb!debug:backtrace 8)
99 ,@forms)))
101 ;;; a helper function for INFINITE-ERROR-PROTECT
102 (defun infinite-error-protector ()
103 (/show0 "entering INFINITE-ERROR-PROTECTOR, *CURRENT-ERROR-DEPTH*=..")
104 (/hexstr *current-error-depth*)
105 (cond ((not *cold-init-complete-p*)
106 (%primitive print "Argh! error in cold init, halting")
107 (%primitive sb!c:halt))
108 ((or (not (boundp '*current-error-depth*))
109 (not (realp *current-error-depth*))
110 (not (boundp '*maximum-error-depth*))
111 (not (realp *maximum-error-depth*)))
112 (%primitive print "Argh! corrupted error depth, halting")
113 (%primitive sb!c:halt))
114 ((> *current-error-depth* *maximum-error-depth*)
115 (/show0 "*MAXIMUM-ERROR-DEPTH*=..")
116 (/hexstr *maximum-error-depth*)
117 (/show0 "in INFINITE-ERROR-PROTECTOR, calling ERROR-ERROR")
118 (error-error "Help! "
119 *current-error-depth*
120 " nested errors. "
121 "SB-KERNEL:*MAXIMUM-ERROR-DEPTH* exceeded.")
124 (/show0 "returning normally from INFINITE-ERROR-PROTECTOR")
125 nil)))
127 ;;; FIXME: I had a badly broken version of INFINITE-ERROR-PROTECTOR at
128 ;;; one point (shown below), and SBCL cross-compiled it without
129 ;;; warning about FORMS being undefined. Check whether that problem
130 ;;; (missing warning) is repeatable in the final system and if so, fix
131 ;;; it.
133 (defun infinite-error-protector ()
134 `(cond ((not *cold-init-complete-p*)
135 (%primitive print "Argh! error in cold init, halting")
136 (%primitive sb!c:halt))
137 ((or (not (boundp '*current-error-depth*))
138 (not (realp *current-error-depth*))
139 (not (boundp '*maximum-error-depth*))
140 (not (realp *maximum-error-depth*)))
141 (%primitive print "Argh! corrupted error depth, halting")
142 (%primitive sb!c:halt))
143 ((> *current-error-depth* *maximum-error-depth*)
144 (/show0 "in INFINITE-ERROR-PROTECTOR, calling ERROR-ERROR")
145 (error-error "Help! "
146 *current-error-depth*
147 " nested errors. "
148 "SB-KERNEL:*MAXIMUM-ERROR-DEPTH* exceeded.")
149 (progn ,@forms)
152 (/show0 "in INFINITE-ERROR-PROTECTOR, returning normally")
153 nil)))
156 ;;;; miscellaneous external functions
158 (defun sleep (n)
159 #!+sb-doc
160 "This function causes execution to be suspended for N seconds. N may
161 be any non-negative, non-complex number."
162 (when (or (not (realp n))
163 (minusp n))
164 (error 'simple-type-error
165 :format-control "invalid argument to SLEEP: ~S"
166 :format-arguments (list n)
167 :datum n
168 :expected-type '(real 0)))
169 #!-win32
170 (multiple-value-bind (sec nsec)
171 (if (integerp n)
172 (values n 0)
173 (multiple-value-bind (sec frac)
174 (truncate n)
175 (values sec (truncate frac 1e-9))))
176 (sb!unix:nanosleep sec nsec))
177 #!+win32
178 (sb!win32:millisleep (truncate (* n 1000)))
179 nil)
181 ;;;; SCRUB-CONTROL-STACK
183 (defconstant bytes-per-scrub-unit 2048)
185 ;;; Zero the unused portion of the control stack so that old objects
186 ;;; are not kept alive because of uninitialized stack variables.
188 ;;; "To summarize the problem, since not all allocated stack frame
189 ;;; slots are guaranteed to be written by the time you call an another
190 ;;; function or GC, there may be garbage pointers retained in your
191 ;;; dead stack locations. The stack scrubbing only affects the part
192 ;;; of the stack from the SP to the end of the allocated stack."
193 ;;; - ram, on cmucl-imp, Tue, 25 Sep 2001
195 ;;; So, as an (admittedly lame) workaround, from time to time we call
196 ;;; scrub-control-stack to zero out all the unused portion. This is
197 ;;; supposed to happen when the stack is mostly empty, so that we have
198 ;;; a chance of clearing more of it: callers are currently (2002.07.18)
199 ;;; REPL and SUB-GC
201 (defun scrub-control-stack ()
202 (declare (optimize (speed 3) (safety 0))
203 (values (unsigned-byte 20))) ; FIXME: DECLARE VALUES?
205 #!-stack-grows-downward-not-upward
206 (let* ((csp (sap-int (sb!c::control-stack-pointer-sap)))
207 (initial-offset (logand csp (1- bytes-per-scrub-unit)))
208 (end-of-stack
209 (- (sap-int (sb!di::descriptor-sap sb!vm:*control-stack-end*))
210 sb!c:*backend-page-size*)))
211 (labels
212 ((scrub (ptr offset count)
213 (declare (type system-area-pointer ptr)
214 (type (unsigned-byte 16) offset)
215 (type (unsigned-byte 20) count)
216 (values (unsigned-byte 20)))
217 (cond ((>= (sap-int ptr) end-of-stack) 0)
218 ((= offset bytes-per-scrub-unit)
219 (look (sap+ ptr bytes-per-scrub-unit) 0 count))
221 (setf (sap-ref-word ptr offset) 0)
222 (scrub ptr (+ offset sb!vm:n-word-bytes) count))))
223 (look (ptr offset count)
224 (declare (type system-area-pointer ptr)
225 (type (unsigned-byte 16) offset)
226 (type (unsigned-byte 20) count)
227 (values (unsigned-byte 20)))
228 (cond ((>= (sap-int ptr) end-of-stack) 0)
229 ((= offset bytes-per-scrub-unit)
230 count)
231 ((zerop (sap-ref-word ptr offset))
232 (look ptr (+ offset sb!vm:n-word-bytes) count))
234 (scrub ptr offset (+ count sb!vm:n-word-bytes))))))
235 (declare (type sb!vm::word csp))
236 (scrub (int-sap (- csp initial-offset))
237 (* (floor initial-offset sb!vm:n-word-bytes) sb!vm:n-word-bytes)
238 0)))
240 #!+stack-grows-downward-not-upward
241 (let* ((csp (sap-int (sb!c::control-stack-pointer-sap)))
242 (end-of-stack (+ (sap-int (sb!di::descriptor-sap sb!vm:*control-stack-start*))
243 sb!c:*backend-page-size*))
244 (initial-offset (logand csp (1- bytes-per-scrub-unit))))
245 (labels
246 ((scrub (ptr offset count)
247 (declare (type system-area-pointer ptr)
248 (type (unsigned-byte 16) offset)
249 (type (unsigned-byte 20) count)
250 (values (unsigned-byte 20)))
251 (let ((loc (int-sap (- (sap-int ptr) (+ offset sb!vm:n-word-bytes)))))
252 (cond ((< (sap-int loc) end-of-stack) 0)
253 ((= offset bytes-per-scrub-unit)
254 (look (int-sap (- (sap-int ptr) bytes-per-scrub-unit))
255 0 count))
256 (t ;; need to fix bug in %SET-STACK-REF
257 (setf (sap-ref-word loc 0) 0)
258 (scrub ptr (+ offset sb!vm:n-word-bytes) count)))))
259 (look (ptr offset count)
260 (declare (type system-area-pointer ptr)
261 (type (unsigned-byte 16) offset)
262 (type (unsigned-byte 20) count)
263 (values (unsigned-byte 20)))
264 (let ((loc (int-sap (- (sap-int ptr) offset))))
265 (cond ((< (sap-int loc) end-of-stack) 0)
266 ((= offset bytes-per-scrub-unit)
267 count)
268 ((zerop (sb!kernel::get-lisp-obj-address (stack-ref loc 0)))
269 (look ptr (+ offset sb!vm:n-word-bytes) count))
271 (scrub ptr offset (+ count sb!vm:n-word-bytes)))))))
272 (declare (type sb!vm::word csp))
273 (scrub (int-sap (+ csp initial-offset))
274 (* (floor initial-offset sb!vm:n-word-bytes) sb!vm:n-word-bytes)
275 0))))
277 ;;;; the default toplevel function
279 (defvar / nil
280 #!+sb-doc
281 "a list of all the values returned by the most recent top level EVAL")
282 (defvar // nil #!+sb-doc "the previous value of /")
283 (defvar /// nil #!+sb-doc "the previous value of //")
284 (defvar * nil #!+sb-doc "the value of the most recent top level EVAL")
285 (defvar ** nil #!+sb-doc "the previous value of *")
286 (defvar *** nil #!+sb-doc "the previous value of **")
287 (defvar + nil #!+sb-doc "the value of the most recent top level READ")
288 (defvar ++ nil #!+sb-doc "the previous value of +")
289 (defvar +++ nil #!+sb-doc "the previous value of ++")
290 (defvar - nil #!+sb-doc "the form currently being evaluated")
292 (defun interactive-eval (form)
293 #!+sb-doc
294 "Evaluate FORM, returning whatever it returns and adjusting ***, **, *,
295 +++, ++, +, ///, //, /, and -."
296 (setf - form)
297 (unwind-protect
298 (let ((results (multiple-value-list (eval form))))
299 (setf /// //
300 // /
301 / results
302 *** **
303 ** *
304 * (car results)))
305 (setf +++ ++
306 ++ +
307 + -))
308 (unless (boundp '*)
309 ;; The bogon returned an unbound marker.
310 ;; FIXME: It would be safer to check every one of the values in RESULTS,
311 ;; instead of just the first one.
312 (setf * nil)
313 (cerror "Go on with * set to NIL."
314 "EVAL returned an unbound marker."))
315 (values-list /))
317 ;;; Flush anything waiting on one of the ANSI Common Lisp standard
318 ;;; output streams before proceeding.
319 (defun flush-standard-output-streams ()
320 (dolist (name '(*debug-io*
321 *error-output*
322 *query-io*
323 *standard-output*
324 *trace-output*
325 *terminal-io*))
326 ;; FINISH-OUTPUT may block more easily than FORCE-OUTPUT
327 (force-output (symbol-value name)))
328 (values))
330 (defun process-init-file (specified-pathname default-function)
331 (restart-case
332 (let ((cookie (list)))
333 (flet ((process-stream (stream &optional pathname)
334 (loop
335 (restart-case
336 (handler-bind
337 ((error (lambda (e)
338 (error "Error during processing of ~
339 initialization file ~A:~%~% ~A"
340 (or pathname stream) e))))
341 (let ((form (read stream nil cookie)))
342 (if (eq cookie form)
343 (return-from process-init-file nil)
344 (eval form))))
345 (continue ()
346 :report "Ignore and continue processing.")))))
347 (if specified-pathname
348 (with-open-file (stream (parse-native-namestring specified-pathname)
349 :if-does-not-exist nil)
350 (if stream
351 (process-stream stream (pathname stream))
352 (error "The specified init file ~S was not found."
353 specified-pathname)))
354 (let ((default (funcall default-function)))
355 (when default
356 (with-open-file (stream (pathname default) :if-does-not-exist nil)
357 (when stream
358 (process-stream stream (pathname stream)))))))))
359 (abort ()
360 :report "Skip this initialization file.")))
362 (defun process-eval-options (eval-strings-or-forms)
363 (/show0 "handling --eval options")
364 (flet ((process-1 (string-or-form)
365 (etypecase string-or-form
366 (string
367 (multiple-value-bind (expr pos) (read-from-string string-or-form)
368 (unless (eq string-or-form
369 (read-from-string string-or-form nil string-or-form
370 :start pos))
371 (error "More than one expression in ~S" string-or-form))
372 (eval expr)
373 (flush-standard-output-streams)))
374 (cons (eval string-or-form) (flush-standard-output-streams)))))
375 (restart-case
376 (dolist (expr-as-string-or-form eval-strings-or-forms)
377 (/show0 "handling one --eval option")
378 (restart-case
379 (handler-bind
380 ((error (lambda (e)
381 (error "Error during processing of --eval ~
382 option ~S:~%~% ~A"
383 expr-as-string-or-form e))))
384 (process-1 expr-as-string-or-form))
385 (continue ()
386 :report "Ignore and continue with next --eval option.")))
387 (abort ()
388 :report "Skip rest of --eval options."))))
390 ;; Errors while processing the command line cause the system to QUIT,
391 ;; instead of trying to go into the Lisp debugger, because trying to
392 ;; go into the Lisp debugger would get into various annoying issues of
393 ;; where we should go after the user tries to return from the
394 ;; debugger.
395 (defun startup-error (control-string &rest args)
396 (format *error-output*
397 "fatal error before reaching READ-EVAL-PRINT loop: ~% ~?~%"
398 control-string
399 args)
400 (quit :unix-status 1))
402 ;;; the default system top level function
403 (defun toplevel-init ()
404 (/show0 "entering TOPLEVEL-INIT")
405 (let ( ;; value of --sysinit option
406 (sysinit nil)
407 ;; t if --no-sysinit option given
408 (no-sysinit nil)
409 ;; value of --userinit option
410 (userinit nil)
411 ;; t if --no-userinit option given
412 (no-userinit nil)
413 ;; values of --eval options, in reverse order; and also any
414 ;; other options (like --load) which're translated into --eval
416 ;; The values are stored as strings, so that they can be
417 ;; passed to READ only after their predecessors have been
418 ;; EVALed, so that things work when e.g. REQUIRE in one EVAL
419 ;; form creates a package referred to in the next EVAL form,
420 ;; except for forms transformed from syntactically-sugary
421 ;; switches like --load and --disable-debugger.
422 (reversed-evals nil)
423 ;; Has a --noprint option been seen?
424 (noprint nil)
425 ;; everything in *POSIX-ARGV* except for argv[0]=programname
426 (options (rest *posix-argv*)))
428 (declare (type list options))
430 (/show0 "done with outer LET in TOPLEVEL-INIT")
432 ;; FIXME: There are lots of ways for errors to happen around here
433 ;; (e.g. bad command line syntax, or READ-ERROR while trying to
434 ;; READ an --eval string). Make sure that they're handled
435 ;; reasonably.
437 ;; Process command line options.
438 (loop while options do
439 (/show0 "at head of LOOP WHILE OPTIONS DO in TOPLEVEL-INIT")
440 (let ((option (first options)))
441 (flet ((pop-option ()
442 (if options
443 (pop options)
444 (startup-error
445 "unexpected end of command line options"))))
446 (cond ((string= option "--sysinit")
447 (pop-option)
448 (if sysinit
449 (startup-error "multiple --sysinit options")
450 (setf sysinit (pop-option))))
451 ((string= option "--no-sysinit")
452 (pop-option)
453 (setf no-sysinit t))
454 ((string= option "--userinit")
455 (pop-option)
456 (if userinit
457 (startup-error "multiple --userinit options")
458 (setf userinit (pop-option))))
459 ((string= option "--no-userinit")
460 (pop-option)
461 (setf no-userinit t))
462 ((string= option "--eval")
463 (pop-option)
464 (push (pop-option) reversed-evals))
465 ((string= option "--load")
466 (pop-option)
467 (push
468 (list 'cl:load (native-pathname (pop-option)))
469 reversed-evals))
470 ((string= option "--noprint")
471 (pop-option)
472 (setf noprint t))
473 ((string= option "--disable-debugger")
474 (pop-option)
475 (push (list 'sb!ext:disable-debugger) reversed-evals))
476 ((string= option "--end-toplevel-options")
477 (pop-option)
478 (return))
480 ;; Anything we don't recognize as a toplevel
481 ;; option must be the start of user-level
482 ;; options.. except that if we encounter
483 ;; "--end-toplevel-options" after we gave up
484 ;; because we didn't recognize an option as a
485 ;; toplevel option, then the option we gave up on
486 ;; must have been an error. (E.g. in
487 ;; "sbcl --eval '(a)' --eval'(b)' --end-toplevel-options"
488 ;; this test will let us detect that the string
489 ;; "--eval(b)" is an error.)
490 (if (find "--end-toplevel-options" options
491 :test #'string=)
492 (startup-error "bad toplevel option: ~S"
493 (first options))
494 (return)))))))
495 (/show0 "done with LOOP WHILE OPTIONS DO in TOPLEVEL-INIT")
497 ;; Delete all the options that we processed, so that only
498 ;; user-level options are left visible to user code.
499 (setf (rest *posix-argv*) options)
501 ;; Handle initialization files.
502 (/show0 "handling initialization files in TOPLEVEL-INIT")
503 ;; This CATCH is needed for the debugger command TOPLEVEL to
504 ;; work.
505 (catch 'toplevel-catcher
506 ;; We wrap all the pre-REPL user/system customized startup
507 ;; code in a restart.
509 ;; (Why not wrap everything, even the stuff above, in this
510 ;; restart? Errors above here are basically command line
511 ;; or Unix environment errors, e.g. a missing file or a
512 ;; typo on the Unix command line, and you don't need to
513 ;; get into Lisp to debug them, you should just start over
514 ;; and do it right at the Unix level. Errors below here
515 ;; are generally errors in user Lisp code, and it might be
516 ;; helpful to let the user reach the REPL in order to help
517 ;; figure out what's going on.)
518 (restart-case
519 (progn
520 (unless no-sysinit
521 (process-init-file sysinit *sysinit-pathname-function*))
522 (unless no-userinit
523 (process-init-file userinit *userinit-pathname-function*))
524 (process-eval-options (nreverse reversed-evals)))
525 (abort ()
526 :report "Skip to toplevel READ/EVAL/PRINT loop."
527 (/show0 "CONTINUEing from pre-REPL RESTART-CASE")
528 (values)) ; (no-op, just fall through)
529 (quit ()
530 :report "Quit SBCL (calling #'QUIT, killing the process)."
531 (/show0 "falling through to QUIT from pre-REPL RESTART-CASE")
532 (quit :unix-status 1))))
534 ;; one more time for good measure, in case we fell out of the
535 ;; RESTART-CASE above before one of the flushes in the ordinary
536 ;; flow of control had a chance to operate
537 (flush-standard-output-streams)
539 (/show0 "falling into TOPLEVEL-REPL from TOPLEVEL-INIT")
540 (toplevel-repl noprint)
541 ;; (classic CMU CL error message: "You're certainly a clever child.":-)
542 (critically-unreachable "after TOPLEVEL-REPL")))
544 ;;; hooks to support customized toplevels like ACL-style toplevel from
545 ;;; KMR on sbcl-devel 2002-12-21. Altered by CSR 2003-11-16 for
546 ;;; threaded operation: altered *REPL-FUN* to *REPL-FUN-GENERATOR*.
547 (defvar *repl-read-form-fun* #'repl-read-form-fun
548 #!+sb-doc
549 "A function of two stream arguments IN and OUT for the toplevel REPL to
550 call: Return the next Lisp form to evaluate (possibly handling other magic --
551 like ACL-style keyword commands -- which precede the next Lisp form). The OUT
552 stream is there to support magic which requires issuing new prompts.")
553 (defvar *repl-prompt-fun* #'repl-prompt-fun
554 #!+sb-doc
555 "A function of one argument STREAM for the toplevel REPL to call: Prompt
556 the user for input.")
557 (defvar *repl-fun-generator* (constantly #'repl-fun)
558 #!+sb-doc
559 "A function of no arguments returning a function of one argument NOPRINT
560 that provides the REPL for the system. Assumes that *STANDARD-INPUT* and
561 *STANDARD-OUTPUT* are set up.")
563 ;;; read-eval-print loop for the default system toplevel
564 (defun toplevel-repl (noprint)
565 (/show0 "entering TOPLEVEL-REPL")
566 (let ((* nil) (** nil) (*** nil)
567 (- nil)
568 (+ nil) (++ nil) (+++ nil)
569 (/// nil) (// nil) (/ nil))
570 (/show0 "about to funcall *REPL-FUN-GENERATOR*")
571 (let ((repl-fun (funcall *repl-fun-generator*)))
572 ;; Each REPL in a multithreaded world should have bindings of
573 ;; most CL specials (most critically *PACKAGE*).
574 (with-rebound-io-syntax
575 (handler-bind ((step-condition 'invoke-stepper))
576 (loop
577 (/show0 "about to set up restarts in TOPLEVEL-REPL")
578 ;; CLHS recommends that there should always be an
579 ;; ABORT restart; we have this one here, and one per
580 ;; debugger level.
581 (with-simple-restart
582 (abort "~@<Exit debugger, returning to top level.~@:>")
583 (catch 'toplevel-catcher
584 #!-win32 (sb!unix::reset-signal-mask)
585 ;; In the event of a control-stack-exhausted-error, we
586 ;; should have unwound enough stack by the time we get
587 ;; here that this is now possible.
588 #!-win32
589 (sb!kernel::protect-control-stack-guard-page 1)
590 (funcall repl-fun noprint)
591 (critically-unreachable "after REPL")))))))))
593 ;;; Our default REPL prompt is the minimal traditional one.
594 (defun repl-prompt-fun (stream)
595 (fresh-line stream)
596 (write-string "* " stream)) ; arbitrary but customary REPL prompt
598 ;;; Our default form reader does relatively little magic, but does
599 ;;; handle the Unix-style EOF-is-end-of-process convention.
600 (defun repl-read-form-fun (in out)
601 (declare (type stream in out) (ignore out))
602 (let* ((eof-marker (cons nil nil))
603 (form (read in nil eof-marker)))
604 (if (eq form eof-marker)
605 (quit)
606 form)))
608 (defun repl-fun (noprint)
609 (/show0 "entering REPL")
610 (loop
611 (unwind-protect
612 (progn
613 ;; (See comment preceding the definition of SCRUB-CONTROL-STACK.)
614 (scrub-control-stack)
615 (sb!thread::get-foreground)
616 (unless noprint
617 (flush-standard-output-streams)
618 (funcall *repl-prompt-fun* *standard-output*)
619 ;; (Should *REPL-PROMPT-FUN* be responsible for doing its own
620 ;; FORCE-OUTPUT? I can't imagine a valid reason for it not to
621 ;; be done here, so leaving it up to *REPL-PROMPT-FUN* seems
622 ;; odd. But maybe there *is* a valid reason in some
623 ;; circumstances? perhaps some deadlock issue when being driven
624 ;; by another process or something...)
625 (force-output *standard-output*))
626 (let* ((form (funcall *repl-read-form-fun*
627 *standard-input*
628 *standard-output*))
629 (results (multiple-value-list (interactive-eval form))))
630 (unless noprint
631 (dolist (result results)
632 (fresh-line)
633 (prin1 result)))))
634 ;; If we started stepping in the debugger we want to stop now.
635 (disable-stepping))))
637 ;;; a convenient way to get into the assembly-level debugger
638 (defun %halt ()
639 (%primitive sb!c:halt))