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