66 lines
1010 B
C
66 lines
1010 B
C
|
#include <libcbd.h>
|
||
|
|
||
|
#include <cbdutil.h>
|
||
|
|
||
|
uint verbose_level;
|
||
|
|
||
|
void __attribute__((noreturn))
|
||
|
error(const char* fmt, ...)
|
||
|
{
|
||
|
va_list ap;
|
||
|
|
||
|
va_start(ap, fmt);
|
||
|
vfprintf(stderr, fmt, ap);
|
||
|
exit(EXIT_FAILURE);
|
||
|
}
|
||
|
|
||
|
int
|
||
|
verbose(uint level, const char* fmt, ...)
|
||
|
{
|
||
|
int ret = 0;
|
||
|
va_list ap;
|
||
|
|
||
|
if (verbose_level >= level) {
|
||
|
va_start(ap, fmt);
|
||
|
vprintf(fmt, ap);
|
||
|
}
|
||
|
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
bool
|
||
|
ask_user_bool(const char* fmt, ...)
|
||
|
{
|
||
|
va_list ap;
|
||
|
char* line = NULL;
|
||
|
size_t len = 0;
|
||
|
int ret;
|
||
|
bool answer;
|
||
|
|
||
|
va_start(ap, fmt);
|
||
|
again:
|
||
|
vprintf(fmt, ap);
|
||
|
printf(" [y/n]? ");
|
||
|
getline(&line, &len, stdin);
|
||
|
if (ret > 0) {
|
||
|
switch (line[0]) {
|
||
|
case 'y':
|
||
|
case 'Y':
|
||
|
answer = true;
|
||
|
break;
|
||
|
case 'n':
|
||
|
case 'N':
|
||
|
answer = false;
|
||
|
break;
|
||
|
default:
|
||
|
ret = -1;
|
||
|
}
|
||
|
}
|
||
|
free(line);
|
||
|
if (ret <= 0) {
|
||
|
goto again;
|
||
|
}
|
||
|
|
||
|
return answer;
|
||
|
}
|