summaryrefslogtreecommitdiff
path: root/debugger/question.ml
blob: ed294beacbccbc35665e1ab67592806b564d6cfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
(**************************************************************************)
(*                                                                        *)
(*                                 OCaml                                  *)
(*                                                                        *)
(*        Nicolas Pouillard, projet Gallium, INRIA Rocquencourt           *)
(*                                                                        *)
(*   Copyright 2006 Institut National de Recherche en Informatique et     *)
(*     en Automatique.                                                    *)
(*                                                                        *)
(*   All rights reserved.  This file is distributed under the terms of    *)
(*   the GNU Lesser General Public License version 2.1, with the          *)
(*   special exception on linking described in the file LICENSE.          *)
(*                                                                        *)
(**************************************************************************)

open Input_handling
open Primitives
module Lexer = Debugger_lexer

(* Ask user a yes or no question. *)
let yes_or_no message =
  if !interactif then
    let finally =
      let old_prompt = !current_prompt in
      fun () -> stop_user_input (); current_prompt := old_prompt
    in
      Fun.protect ~finally @@ fun () ->
        current_prompt := message ^ " ? (y or n) ";
        let answer =
          let rec ask () =
            resume_user_input ();
            let line =
              string_trim (Lexer.line (Lexing.from_function read_user_input))
            in
              match (if String.length line > 0 then line.[0] else ' ') with
                'y' -> true
              | 'n' -> false
              | _ ->
                stop_user_input ();
                print_string "Please answer y or n.";
                print_newline ();
                ask ()
          in
            ask ()
        in
          answer
  else
    false