1 ;;;; querying the user: Y-OR-N-P, YES-OR-NO-P
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
17 (defun query-readline ()
18 (force-output *query-io*)
19 (string-trim " " (read-line *query-io*)))
21 ;;; FIXME: The ANSI documentation for these says that they
22 ;;; prompt with strings a la "(Y or N)" or "(Yes or No)", but
23 ;;; these implementations don't.
25 (defun y-or-n-p (&optional format-string &rest arguments)
27 "Y-OR-N-P prints the message, if any, and reads characters from *QUERY-IO*
28 until the user enters y or Y as an affirmative, or either n or N as a
29 negative answer. It ignores preceding whitespace and asks again if you
30 enter any other characters."
32 (fresh-line *query-io*)
33 (apply #'format *query-io* format-string arguments))
35 (let* ((line (query-readline))
36 (ans (if (string= line "")
37 #\? ;Force CASE below to issue instruction.
39 (unless (sb!impl::whitespacep ans)
41 ((#\y #\Y) (return t))
42 ((#\n #\N) (return nil))
44 (write-line "Please type \"y\" for yes or \"n\" for no. "
47 (apply #'format *query-io* format-string arguments))
48 (force-output *query-io*)))))))
50 ;;; This is similar to Y-OR-N-P, but it clears the input buffer, beeps, and
51 ;;; uses READ-LINE to get "YES" or "NO".
52 (defun yes-or-no-p (&optional format-string &rest arguments)
54 "YES-OR-NO-P is similar to Y-OR-N-P, except that it clears the
55 input buffer, beeps, and uses READ-LINE to get the strings
57 (clear-input *query-io*)
60 (fresh-line *query-io*)
61 (apply #'format *query-io* format-string arguments))
62 (do ((ans (query-readline) (query-readline)))
64 (cond ((string-equal ans "YES") (return t))
65 ((string-equal ans "NO") (return nil))
67 (write-line "Please type \"yes\" for yes or \"no\" for no. "
70 (apply #'format *query-io* format-string arguments))))))