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