#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cx/string.h>
#include <cx/utils.h>
#include <libidav/utils.h>
#include "assistant.h"
char* assistant_getcfg(
const char *cfgname) {
cxmutstr line;
char *value =
NULL;
while(!value) {
printf(
"%s: ", cfgname);
fflush(stdout);
line = util_readline(stdin);
if(line.length ==
0 || !line.ptr) {
fprintf(stderr,
"%s must be not empty\n", cfgname);
continue;
}
value = line.ptr;
break;
}
return value;
}
char* assistant_getoptcfg(
const char *cfgname) {
cxmutstr line;
char *value =
NULL;
while(!value) {
printf(
"%s (optional): ", cfgname);
fflush(stdout);
line = util_readline(stdin);
if(line.length ==
0 || !line.ptr) {
if(line.ptr) {
free(line.ptr);
}
break;
}
value = line.ptr;
break;
}
return value;
}
char* assistant_gethiddenoptcfg(
const char *cfgname) {
printf(
"%s (optional): ", cfgname);
fflush(stdout);
char *pw = util_password_input(
"");
if(pw[
0] ==
0) {
free(pw);
pw =
NULL;
}
return pw;
}
char* assistant_getdefcfg(
const char *cfgname,
const char *defval) {
cxmutstr line;
char *value =
NULL;
while(!value) {
printf(
"%s (default: %s): ", cfgname, defval);
fflush(stdout);
line = util_readline(stdin);
if(line.length ==
0 || !line.ptr) {
if(line.ptr) {
free(line.ptr);
}
break;
}
value = line.ptr;
break;
}
return value ? value : strdup(defval);
}