avoid recursive errors from broken standard streams on debugger entry
[sbcl.git] / src / code / toplevel.lisp
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
4
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.
13
14 (in-package "SB!IMPL")
15 \f
16 ;;;; magic specials initialized by GENESIS
17
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   #!+hpux (defvar sb!vm::*c-lra*)
24   (defvar *free-interrupt-context-index*))
25 \f
26 ;;; specials initialized by !COLD-INIT
27
28 ;;; FIXME: These could be converted to DEFVARs.
29 (declaim (special #!+(or x86 x86-64) *pseudo-atomic-bits*
30                   *allow-with-interrupts*
31                   *interrupts-enabled*
32                   *interrupt-pending*
33                   *type-system-initialized*))
34
35 (defvar *cold-init-complete-p*)
36
37 ;;; counts of nested errors (with internal errors double-counted)
38 (defvar *maximum-error-depth*)
39 (defvar *current-error-depth*)
40
41 ;;;; default initfiles
42
43 (defun sysinit-pathname ()
44   (or (let ((sbcl-homedir (sbcl-homedir-pathname)))
45         (when sbcl-homedir
46           (probe-file (merge-pathnames "sbclrc" sbcl-homedir))))
47       #!+win32
48       (merge-pathnames "sbcl\\sbclrc"
49                        (sb!win32::get-folder-pathname
50                         sb!win32::csidl_common_appdata))
51       #!-win32
52       "/etc/sbclrc"))
53
54 (defun userinit-pathname ()
55   (merge-pathnames ".sbclrc" (user-homedir-pathname)))
56
57 (defvar *sysinit-pathname-function* #'sysinit-pathname
58   #!+sb-doc
59   "Designator for a function of zero arguments called to obtain a
60 pathname designator for the default sysinit file, or NIL. If the
61 function returns NIL, no sysinit file is used unless one has been
62 specified on the command-line.")
63
64 (defvar *userinit-pathname-function* #'userinit-pathname
65   #!+sb-doc
66   "Designator for a function of zero arguments called to obtain a
67 pathname designator or a stream for the default userinit file, or NIL.
68 If the function returns NIL, no userinit file is used unless one has
69 been specified on the command-line.")
70
71 \f
72 ;;;; miscellaneous utilities for working with with TOPLEVEL
73
74 ;;; Execute BODY in a context where any %END-OF-THE-WORLD (thrown e.g.
75 ;;; by QUIT) is caught and any final processing and return codes are
76 ;;; handled appropriately.
77 (defmacro handling-end-of-the-world (&body body)
78   (with-unique-names (caught)
79     `(without-interrupts
80        (let ((,caught
81                (catch '%end-of-the-world
82                  (unwind-protect
83                       (with-local-interrupts ,@body (quit))
84                    (handler-case
85                        (with-local-interrupts
86                          (call-hooks "exit" *exit-hooks* :on-error :warn))
87                      (serious-condition ()
88                        1))))))
89          ;; If user called QUIT and exit hooks were OK, the status is what it
90          ;; is -- even eg. streams cannot be flushed anymore. Even if
91          ;; something goes wrong now, we still report what was asked. We still
92          ;; want to have %END-OF-THE-WORLD visible, though.
93          (catch '%end-of-the-world
94            (handler-case
95                (unwind-protect
96                     (progn
97                       (flush-standard-output-streams)
98                       (sb!thread::terminate-session))
99                  (sb!unix:unix-exit ,caught))
100              (serious-condition ())))))))
101 \f
102 ;;;; working with *CURRENT-ERROR-DEPTH* and *MAXIMUM-ERROR-DEPTH*
103
104 ;;; INFINITE-ERROR-PROTECT is used by ERROR and friends to keep us out
105 ;;; of hyperspace.
106 (defmacro infinite-error-protect (&rest forms)
107   `(unless (infinite-error-protector)
108      (/show0 "back from INFINITE-ERROR-PROTECTOR")
109      (let ((*current-error-depth* (1+ *current-error-depth*)))
110        (/show0 "in INFINITE-ERROR-PROTECT, incremented error depth")
111        ;; arbitrary truncation
112        #!+sb-show (sb!debug:backtrace 8)
113        ,@forms)))
114
115 ;;; a helper function for INFINITE-ERROR-PROTECT
116 (defun infinite-error-protector ()
117   (/show0 "entering INFINITE-ERROR-PROTECTOR, *CURRENT-ERROR-DEPTH*=..")
118   (/hexstr *current-error-depth*)
119   (cond ((not *cold-init-complete-p*)
120          (%primitive print "Argh! error in cold init, halting")
121          (%primitive sb!c:halt))
122         ((or (not (boundp '*current-error-depth*))
123              (not (realp   *current-error-depth*))
124              (not (boundp '*maximum-error-depth*))
125              (not (realp   *maximum-error-depth*)))
126          (%primitive print "Argh! corrupted error depth, halting")
127          (%primitive sb!c:halt))
128         ((> *current-error-depth* *maximum-error-depth*)
129          (/show0 "*MAXIMUM-ERROR-DEPTH*=..")
130          (/hexstr *maximum-error-depth*)
131          (/show0 "in INFINITE-ERROR-PROTECTOR, calling ERROR-ERROR")
132          (error-error "Help! "
133                       *current-error-depth*
134                       " nested errors. "
135                       "SB-KERNEL:*MAXIMUM-ERROR-DEPTH* exceeded.")
136          t)
137         (t
138          (/show0 "returning normally from INFINITE-ERROR-PROTECTOR")
139          nil)))
140
141 ;;; FIXME: I had a badly broken version of INFINITE-ERROR-PROTECTOR at
142 ;;; one point (shown below), and SBCL cross-compiled it without
143 ;;; warning about FORMS being undefined. Check whether that problem
144 ;;; (missing warning) is repeatable in the final system and if so, fix
145 ;;; it.
146 #|
147 (defun infinite-error-protector ()
148   `(cond ((not *cold-init-complete-p*)
149           (%primitive print "Argh! error in cold init, halting")
150           (%primitive sb!c:halt))
151          ((or (not (boundp '*current-error-depth*))
152               (not (realp   *current-error-depth*))
153               (not (boundp '*maximum-error-depth*))
154               (not (realp   *maximum-error-depth*)))
155           (%primitive print "Argh! corrupted error depth, halting")
156           (%primitive sb!c:halt))
157          ((> *current-error-depth* *maximum-error-depth*)
158           (/show0 "in INFINITE-ERROR-PROTECTOR, calling ERROR-ERROR")
159           (error-error "Help! "
160                        *current-error-depth*
161                        " nested errors. "
162                        "SB-KERNEL:*MAXIMUM-ERROR-DEPTH* exceeded.")
163           (progn ,@forms)
164           t)
165          (t
166           (/show0 "in INFINITE-ERROR-PROTECTOR, returning normally")
167           nil)))
168 |#
169 \f
170 ;;;; miscellaneous external functions
171
172 (defun sleep (seconds)
173   #!+sb-doc
174   "This function causes execution to be suspended for SECONDS. SECONDS may be
175 any non-negative real number."
176   (when (or (not (realp seconds))
177             (minusp seconds))
178     (error 'simple-type-error
179            :format-control "invalid argument to SLEEP: ~S"
180            :format-arguments (list seconds)
181            :datum seconds
182            :expected-type '(real 0)))
183   #!-win32
184   (multiple-value-bind (sec nsec)
185       (if (integerp seconds)
186           (values seconds 0)
187           (multiple-value-bind (sec frac)
188               (truncate seconds)
189             (values sec (truncate frac 1e-9))))
190     ;; nanosleep() accepts time_t as the first argument, but on some platforms
191     ;; it is restricted to 100 million seconds. Maybe someone can actually
192     ;; have a reason to sleep for over 3 years?
193     (loop while (> sec (expt 10 8))
194           do (decf sec (expt 10 8))
195              (sb!unix:nanosleep (expt 10 8) 0))
196     (sb!unix:nanosleep sec nsec))
197   #!+win32
198   (sb!win32:millisleep (truncate (* seconds 1000)))
199   nil)
200 \f
201 ;;;; the default toplevel function
202
203 (defvar / nil
204   #!+sb-doc
205   "a list of all the values returned by the most recent top level EVAL")
206 (defvar //  nil #!+sb-doc "the previous value of /")
207 (defvar /// nil #!+sb-doc "the previous value of //")
208 (defvar *   nil #!+sb-doc "the value of the most recent top level EVAL")
209 (defvar **  nil #!+sb-doc "the previous value of *")
210 (defvar *** nil #!+sb-doc "the previous value of **")
211 (defvar +   nil #!+sb-doc "the value of the most recent top level READ")
212 (defvar ++  nil #!+sb-doc "the previous value of +")
213 (defvar +++ nil #!+sb-doc "the previous value of ++")
214 (defvar -   nil #!+sb-doc "the form currently being evaluated")
215
216 (defun interactive-eval (form &key (eval #'eval))
217   #!+sb-doc
218   "Evaluate FORM, returning whatever it returns and adjusting ***, **, *,
219 +++, ++, +, ///, //, /, and -."
220   (setf - form)
221   (unwind-protect
222        (let ((results (multiple-value-list (funcall eval form))))
223          (setf /// //
224                // /
225                / results
226                *** **
227                ** *
228                * (car results)))
229     (setf +++ ++
230           ++ +
231           + -))
232   (unless (boundp '*)
233     ;; The bogon returned an unbound marker.
234     ;; FIXME: It would be safer to check every one of the values in RESULTS,
235     ;; instead of just the first one.
236     (setf * nil)
237     (cerror "Go on with * set to NIL."
238             "EVAL returned an unbound marker."))
239   (values-list /))
240
241 ;;; Flush anything waiting on one of the ANSI Common Lisp standard
242 ;;; output streams before proceeding.
243 (defun flush-standard-output-streams ()
244   (let ((null (make-broadcast-stream)))
245     (dolist (name '(*debug-io*
246                     *error-output*
247                     *query-io*
248                     *standard-output*
249                     *trace-output*
250                     *terminal-io*))
251       ;; 0. Pull out the underlying stream, so we know what it is.
252       ;; 1. Handle errors on it. We're doing this on entry to
253       ;;    debugger, so we don't want recursive errors here.
254       ;; 2. Rebind the stream symbol in case some poor sod sees
255       ;;    a broken stream here while running with *BREAK-ON-ERRORS*.
256       (let ((stream (stream-output-stream (symbol-value name))))
257         (progv (list name) (list null)
258           (handler-bind ((stream-error
259                            (lambda (c)
260                              (when (eq stream (stream-error-stream c))
261                                (go :next)))))
262             (force-output stream))))
263       :next))
264   (values))
265
266 (defun stream-output-stream (stream)
267   (typecase stream
268     (fd-stream
269      stream)
270     (synonym-stream
271      (stream-output-stream
272       (symbol-value (synonym-stream-symbol stream))))
273     (two-way-stream
274      (stream-output-stream
275       (two-way-stream-output-stream stream)))
276     (t
277      stream)))
278
279 (defun process-init-file (specified-pathname kind)
280   (multiple-value-bind (context default-function)
281       (ecase kind
282         (:system
283          (values "sysinit" *sysinit-pathname-function*))
284         (:user
285          (values "userinit" *userinit-pathname-function*)))
286     (if specified-pathname
287         (with-open-file (stream (parse-native-namestring specified-pathname)
288                                 :if-does-not-exist nil)
289           (if stream
290               (load-as-source stream :context context)
291               (cerror "Ignore missing init file"
292                       "The specified ~A file ~A was not found."
293                       context specified-pathname)))
294         (let ((default (funcall default-function)))
295           (when default
296             (with-open-file (stream (pathname default) :if-does-not-exist nil)
297               (when stream
298                 (load-as-source stream :context context))))))))
299
300 (defun process-eval/load-options (options)
301   (/show0 "handling --eval and --load options")
302   (flet ((process-1 (cons)
303            (destructuring-bind (opt . value) cons
304              (ecase opt
305                (:eval
306                 (with-simple-restart (continue "Ignore runtime option --eval ~S."
307                                                value)
308                   (multiple-value-bind (expr pos) (read-from-string value)
309                     (if (eq value (read-from-string value nil value :start pos))
310                         (eval expr)
311                         (error "Multiple expressions in --eval option: ~S"
312                                value)))))
313                (:load
314                 (with-simple-restart (continue "Ignore runtime option --load ~S."
315                                                value)
316                   (load (native-pathname value))))
317                (:quit
318                 (quit))))
319            (flush-standard-output-streams)))
320     (with-simple-restart (abort "Skip rest of --eval and --load options.")
321       (dolist (option options)
322         (process-1 option)))))
323
324 (defun process-script (script)
325   (flet ((load-script (stream)
326            ;; Scripts don't need to be stylish or fast, but silence is usually a
327            ;; desirable quality...
328            (handler-bind (((or style-warning compiler-note) #'muffle-warning)
329                           (stream-error (lambda (e)
330                                           ;; Shell-style.
331                                           (when (member (stream-error-stream e)
332                                                         (list *stdout* *stdin* *stderr*))
333                                             (quit)))))
334              ;; Let's not use the *TTY* for scripts, ok? Also, normally we use
335              ;; synonym streams, but in order to have the broken pipe/eof error
336              ;; handling right we want to bind them for scripts.
337              (let ((*terminal-io* (make-two-way-stream *stdin* *stdout*))
338                    (*debug-io* (make-two-way-stream *stdin* *stderr*))
339                    (*standard-input* *stdin*)
340                    (*standard-output* *stdout*)
341                    (*error-output* *stderr*))
342                (load stream :verbose nil :print nil)))))
343     (handling-end-of-the-world
344       (if (eq t script)
345           (load-script *stdin*)
346           (with-open-file (f (native-pathname script) :element-type :default)
347             (sb!fasl::maybe-skip-shebang-line f)
348             (load-script f))))))
349
350 ;; Errors while processing the command line cause the system to QUIT,
351 ;; instead of trying to go into the Lisp debugger, because trying to
352 ;; go into the Lisp debugger would get into various annoying issues of
353 ;; where we should go after the user tries to return from the
354 ;; debugger.
355 (defun startup-error (control-string &rest args)
356   (format *error-output*
357           "fatal error before reaching READ-EVAL-PRINT loop: ~%  ~?~%"
358           control-string
359           args)
360   (quit :unix-status 1))
361
362 ;;; the default system top level function
363 (defun toplevel-init ()
364   (/show0 "entering TOPLEVEL-INIT")
365   (let ( ;; value of --sysinit option
366         (sysinit nil)
367         ;; t if --no-sysinit option given
368         (no-sysinit nil)
369         ;; value of --userinit option
370         (userinit nil)
371         ;; t if --no-userinit option given
372         (no-userinit nil)
373         ;; t if --disable-debugger option given
374         (disable-debugger nil)
375         ;; list of (<kind> . <string>) conses representing --eval and --load
376         ;; options. options. --eval options are stored as strings, so that
377         ;; they can be passed to READ only after their predecessors have been
378         ;; EVALed, so that things work when e.g. REQUIRE in one EVAL form
379         ;; creates a package referred to in the next EVAL form. Storing the
380         ;; original string also makes for easier debugging.
381         (reversed-options nil)
382         ;; Has a --noprint option been seen?
383         (noprint nil)
384         ;; Has a --script option been seen?
385         (script nil)
386         ;; Quit after processing other options?
387         (finally-quit nil)
388         ;; everything in *POSIX-ARGV* except for argv[0]=programname
389         (options (rest *posix-argv*)))
390
391     (declare (type list options))
392
393     (/show0 "done with outer LET in TOPLEVEL-INIT")
394
395     ;; FIXME: There are lots of ways for errors to happen around here
396     ;; (e.g. bad command line syntax, or READ-ERROR while trying to
397     ;; READ an --eval string). Make sure that they're handled
398     ;; reasonably.
399
400     ;; Process command line options.
401     (loop while options do
402          (/show0 "at head of LOOP WHILE OPTIONS DO in TOPLEVEL-INIT")
403          (let ((option (first options)))
404            (flet ((pop-option ()
405                     (if options
406                         (pop options)
407                         (startup-error
408                          "unexpected end of command line options"))))
409              (cond ((string= option "--script")
410                     (pop-option)
411                     (setf disable-debugger t
412                           no-userinit t
413                           no-sysinit t
414                           script (if options (pop-option) t))
415                     (return))
416                    ((string= option "--sysinit")
417                     (pop-option)
418                     (if sysinit
419                         (startup-error "multiple --sysinit options")
420                         (setf sysinit (pop-option))))
421                    ((string= option "--no-sysinit")
422                     (pop-option)
423                     (setf no-sysinit t))
424                    ((string= option "--userinit")
425                     (pop-option)
426                     (if userinit
427                         (startup-error "multiple --userinit options")
428                         (setf userinit (pop-option))))
429                    ((string= option "--no-userinit")
430                     (pop-option)
431                     (setf no-userinit t))
432                    ((string= option "--eval")
433                     (pop-option)
434                     (push (cons :eval (pop-option)) reversed-options))
435                    ((string= option "--load")
436                     (pop-option)
437                     (push (cons :load (pop-option)) reversed-options))
438                    ((string= option "--noprint")
439                     (pop-option)
440                     (setf noprint t))
441                    ((string= option "--disable-debugger")
442                     (pop-option)
443                     (setf disable-debugger t))
444                    ((string= option "--quit")
445                     (pop-option)
446                     (setf finally-quit t))
447                    ((string= option "--non-interactive")
448                     ;; This option is short for --quit and --disable-debugger,
449                     ;; which are needed in combination for reliable non-
450                     ;; interactive startup.
451                     (pop-option)
452                     (setf finally-quit t)
453                     (setf disable-debugger t))
454                    ((string= option "--end-toplevel-options")
455                     (pop-option)
456                     (return))
457                    (t
458                     ;; Anything we don't recognize as a toplevel
459                     ;; option must be the start of user-level
460                     ;; options.. except that if we encounter
461                     ;; "--end-toplevel-options" after we gave up
462                     ;; because we didn't recognize an option as a
463                     ;; toplevel option, then the option we gave up on
464                     ;; must have been an error. (E.g. in
465                     ;;  "sbcl --eval '(a)' --eval'(b)' --end-toplevel-options"
466                     ;; this test will let us detect that the string
467                     ;; "--eval(b)" is an error.)
468                     (if (find "--end-toplevel-options" options
469                               :test #'string=)
470                         (startup-error "bad toplevel option: ~S"
471                                        (first options))
472                         (return)))))))
473     (/show0 "done with LOOP WHILE OPTIONS DO in TOPLEVEL-INIT")
474
475     ;; Delete all the options that we processed, so that only
476     ;; user-level options are left visible to user code.
477     (setf (rest *posix-argv*) options)
478
479     ;; Disable debugger before processing initialization files & co.
480     (when disable-debugger
481       (sb!ext:disable-debugger))
482
483     ;; Handle initialization files.
484     (/show0 "handling initialization files in TOPLEVEL-INIT")
485     ;; This CATCH is needed for the debugger command TOPLEVEL to
486     ;; work.
487     (catch 'toplevel-catcher
488       ;; We wrap all the pre-REPL user/system customized startup
489       ;; code in a restart.
490       ;;
491       ;; (Why not wrap everything, even the stuff above, in this
492       ;; restart? Errors above here are basically command line
493       ;; or Unix environment errors, e.g. a missing file or a
494       ;; typo on the Unix command line, and you don't need to
495       ;; get into Lisp to debug them, you should just start over
496       ;; and do it right at the Unix level. Errors below here
497       ;; are generally errors in user Lisp code, and it might be
498       ;; helpful to let the user reach the REPL in order to help
499       ;; figure out what's going on.)
500       (restart-case
501           (progn
502             (unless no-sysinit
503               (process-init-file sysinit :system))
504             (unless no-userinit
505               (process-init-file userinit :user))
506             (when finally-quit
507               (push (list :quit) reversed-options))
508             (process-eval/load-options (nreverse reversed-options))
509             (when script
510               (process-script script)
511               (bug "PROCESS-SCRIPT returned")))
512         (abort ()
513           :report (lambda (s)
514                     (write-string
515                      (if script
516                          ;; In case script calls (enable-debugger)!
517                          "Abort script, exiting lisp."
518                          "Skip to toplevel READ/EVAL/PRINT loop.")
519                      s))
520           (/show0 "CONTINUEing from pre-REPL RESTART-CASE")
521           (values))                     ; (no-op, just fall through)
522         (quit ()
523           :report "Quit SBCL (calling #'QUIT, killing the process)."
524           :test (lambda (c) (declare (ignore c)) (not script))
525           (/show0 "falling through to QUIT from pre-REPL RESTART-CASE")
526           (quit :unix-status 1))))
527
528     ;; one more time for good measure, in case we fell out of the
529     ;; RESTART-CASE above before one of the flushes in the ordinary
530     ;; flow of control had a chance to operate
531     (flush-standard-output-streams)
532
533     (/show0 "falling into TOPLEVEL-REPL from TOPLEVEL-INIT")
534     (toplevel-repl noprint)
535     ;; (classic CMU CL error message: "You're certainly a clever child.":-)
536     (critically-unreachable "after TOPLEVEL-REPL")))
537
538 ;;; hooks to support customized toplevels like ACL-style toplevel from
539 ;;; KMR on sbcl-devel 2002-12-21.  Altered by CSR 2003-11-16 for
540 ;;; threaded operation: altered *REPL-FUN* to *REPL-FUN-GENERATOR*.
541 (defvar *repl-read-form-fun* #'repl-read-form-fun
542   #!+sb-doc
543   "A function of two stream arguments IN and OUT for the toplevel REPL to
544 call: Return the next Lisp form to evaluate (possibly handling other magic --
545 like ACL-style keyword commands -- which precede the next Lisp form). The OUT
546 stream is there to support magic which requires issuing new prompts.")
547 (defvar *repl-prompt-fun* #'repl-prompt-fun
548   #!+sb-doc
549   "A function of one argument STREAM for the toplevel REPL to call: Prompt
550 the user for input.")
551 (defvar *repl-fun-generator* (constantly #'repl-fun)
552   #!+sb-doc
553   "A function of no arguments returning a function of one argument NOPRINT
554 that provides the REPL for the system. Assumes that *STANDARD-INPUT* and
555 *STANDARD-OUTPUT* are set up.")
556
557 ;;; read-eval-print loop for the default system toplevel
558 (defun toplevel-repl (noprint)
559   (/show0 "entering TOPLEVEL-REPL")
560   (let ((* nil) (** nil) (*** nil)
561         (- nil)
562         (+ nil) (++ nil) (+++ nil)
563         (/// nil) (// nil) (/ nil))
564     (/show0 "about to funcall *REPL-FUN-GENERATOR*")
565     (let ((repl-fun (funcall *repl-fun-generator*)))
566       ;; Each REPL in a multithreaded world should have bindings of
567       ;; most CL specials (most critically *PACKAGE*).
568       (with-rebound-io-syntax
569           (handler-bind ((step-condition 'invoke-stepper))
570             (loop
571                (/show0 "about to set up restarts in TOPLEVEL-REPL")
572                ;; CLHS recommends that there should always be an
573                ;; ABORT restart; we have this one here, and one per
574                ;; debugger level.
575                (with-simple-restart
576                    (abort "~@<Exit debugger, returning to top level.~@:>")
577                  (catch 'toplevel-catcher
578                    ;; In the event of a control-stack-exhausted-error, we
579                    ;; should have unwound enough stack by the time we get
580                    ;; here that this is now possible.
581                    #!-win32
582                    (sb!kernel::reset-control-stack-guard-page)
583                    (funcall repl-fun noprint)
584                    (critically-unreachable "after REPL")))))))))
585
586 ;;; Our default REPL prompt is the minimal traditional one.
587 (defun repl-prompt-fun (stream)
588   (fresh-line stream)
589   (write-string "* " stream)) ; arbitrary but customary REPL prompt
590
591 ;;; Our default form reader does relatively little magic, but does
592 ;;; handle the Unix-style EOF-is-end-of-process convention.
593 (defun repl-read-form-fun (in out)
594   (declare (type stream in out) (ignore out))
595   ;; KLUDGE: *READ-SUPPRESS* makes the REPL useless, and cannot be
596   ;; recovered from -- flip it here.
597   (when *read-suppress*
598     (warn "Setting *READ-SUPPRESS* to NIL to restore toplevel usability.")
599     (setf *read-suppress* nil))
600   (let* ((eof-marker (cons nil nil))
601          (form (read in nil eof-marker)))
602     (if (eq form eof-marker)
603         (quit)
604         form)))
605
606 (defun repl-fun (noprint)
607   (/show0 "entering REPL")
608   (loop
609    (unwind-protect
610         (progn
611           ;; (See comment preceding the definition of SCRUB-CONTROL-STACK.)
612           (scrub-control-stack)
613           (sb!thread::get-foreground)
614           (unless noprint
615             (flush-standard-output-streams)
616             (funcall *repl-prompt-fun* *standard-output*)
617             ;; (Should *REPL-PROMPT-FUN* be responsible for doing its own
618             ;; FORCE-OUTPUT? I can't imagine a valid reason for it not to
619             ;; be done here, so leaving it up to *REPL-PROMPT-FUN* seems
620             ;; odd. But maybe there *is* a valid reason in some
621             ;; circumstances? perhaps some deadlock issue when being driven
622             ;; by another process or something...)
623             (force-output *standard-output*))
624           (let* ((form (funcall *repl-read-form-fun*
625                                 *standard-input*
626                                 *standard-output*))
627                  (results (multiple-value-list (interactive-eval form))))
628             (unless noprint
629               (dolist (result results)
630                 (fresh-line)
631                 (prin1 result)))))
632      ;; If we started stepping in the debugger we want to stop now.
633      (disable-stepping))))
634 \f
635 ;;; a convenient way to get into the assembly-level debugger
636 (defun %halt ()
637   (%primitive sb!c:halt))