1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License, Version 1.0 only
   6  * (the "License").  You may not use this file except in compliance
   7  * with the License.
   8  *
   9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 /*
  23  * ident        "%Z%%M% %I%     %E% SMI"
  24  *
  25  * Copyright (c) 2001 by Sun Microsystems, Inc.
  26  * All rights reserved.
  27  */
  28 
  29 package com.sun.dhcpmgr.cli.common;
  30 
  31 import java.io.BufferedReader;
  32 import java.io.InputStreamReader;
  33 import java.io.IOException;
  34 
  35 /**
  36  * This class provides convenient methods to read from System.in.
  37  */
  38 public class Console
  39 {
  40     /**
  41      * Prompt the user for a yes/no reply and read the reply.
  42      * @param prompt the prompt message
  43      * @param trueValue the value that represents an affirmative response
  44      * @param falseValue the value that represents a negative response
  45      * @param defaultReturn indicates whether affirmative or negative is
  46      *        the default (true means affirmative, false means negative)
  47      * @return true if affirmative response, false if negative
  48      */
  49     public static boolean promptUser(String prompt,
  50         String trueValue, String falseValue, boolean defaultReturn) {
  51 
  52         boolean done = false;
  53         boolean result = defaultReturn;
  54 
  55         StringBuffer buffer = new StringBuffer(prompt);
  56         buffer.append(" (");
  57         if (defaultReturn) {
  58             buffer.append('[');
  59             buffer.append(trueValue);
  60             buffer.append("]/");
  61             buffer.append(falseValue);
  62         } else {
  63             buffer.append(trueValue);
  64             buffer.append("/[");
  65             buffer.append(falseValue);
  66             buffer.append(']');
  67         }
  68         buffer.append("): ");
  69 
  70         while (!done) {
  71             System.out.print(buffer.toString());
  72             System.out.flush();
  73             String line = readLine();
  74             if (line == null || line.length() == 0) {
  75                 done = true;
  76             } else if (line.equalsIgnoreCase(trueValue)) {
  77                 result = true;
  78                 done = true;
  79             } else if (line.equalsIgnoreCase(falseValue)) {
  80                 result = false;
  81                 done = true;
  82             }
  83         }
  84 
  85         return (result);
  86 
  87     } // promptUser
  88 
  89     /**
  90      * Read a line from System.in.
  91      * @return the line or null in case of exception
  92      */
  93     public static String readLine() {
  94 
  95         String line = null;
  96 
  97         try {
  98             BufferedReader reader =
  99                 new BufferedReader(new InputStreamReader(System.in));
 100 
 101             line = reader.readLine();
 102         } catch (IOException e) {
 103             // ignore and return null
 104         }
 105 
 106         return (line);
 107     } // readLine
 108 }