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