2012-11-02 19:34:25 +05:30
|
|
|
/*-
|
2020-04-24 15:14:19 +05:30
|
|
|
* Licensed under the SPDX BSD-2-Clause identifier.
|
|
|
|
* Use is subject to license terms, as specified in the LICENSE file.
|
2012-11-02 19:34:25 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "defs.h"
|
|
|
|
|
2013-01-04 15:05:00 +05:30
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic ignored "-Wformat-nonliteral"
|
|
|
|
#endif
|
|
|
|
|
2012-11-02 19:34:25 +05:30
|
|
|
static bool
|
|
|
|
question(bool preset, const char *fmt, va_list ap)
|
|
|
|
{
|
2015-03-21 14:15:29 +05:30
|
|
|
int response;
|
2019-05-02 22:45:09 +05:30
|
|
|
bool rv = false;
|
2012-11-02 19:34:25 +05:30
|
|
|
|
|
|
|
vfprintf(stderr, fmt, ap);
|
2019-05-02 22:45:09 +05:30
|
|
|
if (preset)
|
2015-03-14 05:00:35 +05:30
|
|
|
fputs(" [Y/n] ", stderr);
|
2012-11-02 19:34:25 +05:30
|
|
|
else
|
2015-03-14 05:00:35 +05:30
|
|
|
fputs(" [y/N] ", stderr);
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2019-05-02 22:45:09 +05:30
|
|
|
response = fgetc(stdin);
|
|
|
|
if (response == '\n')
|
|
|
|
rv = preset;
|
|
|
|
else if (response == 'y' || response == 'Y')
|
|
|
|
rv = true;
|
|
|
|
else if (response == 'n' || response == 'N')
|
|
|
|
rv = false;
|
|
|
|
|
|
|
|
/* read the rest of the line */
|
|
|
|
while (response != EOF && response != '\n')
|
|
|
|
response = fgetc(stdin);
|
|
|
|
|
|
|
|
return rv;
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
yesno(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
bool res;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
res = question(1, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
noyes(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
bool res;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
res = question(0, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|