/*
   <p>Function to output a question prompt and return
   Y or N as the result.</p>

   <kbd>
   <b>environ - C programming support functions</b><br>
   Copyright (C) 2005  Martin C Gregorie
   
   This program is free software; you can redistribute it and/or
   modify it under the terms of the Lesser GNU General Public License.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the Lesser GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   </kbd>
  
   <p>Contact Martin Gregorie at <i>martin@gregorie.org</i></p>
*/

#include <stdio.h>
#include <ctype.h>
#include "environ.h"

#define BELL        0x07
#define BACKSPACE   0x08

/*
   Outputs <u>prompt</u> on <i>stdout</i> as "<i>prompt</i> (y/n) ? "
   and accepts the reply. If the answer is not one of 'YyNn' it emits a beep
   and a backspace and accepts input until the result is y,n,Y or N.
   It returns the validated input as an upper case letter (Y or N).
*/
int question(char *prompt)
{
    int     reply,
            found;
    
    printf("%s (y/n) ? ",prompt);
    found = 0;
    
    do
    {
        reply = toupper(getkey());
        if (reply == 'Y' || reply == 'N')
            found = 1;
        else
        {
            found = 0;
            printf("%c%c",BELL,BACKSPACE);
            fflush(stdout);
        }
    }
    while (found == 0);
    return(reply == 'Y');
}
