Browse Source

Implement the interactive menu

Make the -i, --interactive option work by presenting a list
of all available supports and allowing the user to select
the supports to use.
master
Damien Goutte-Gattat 5 years ago
parent
commit
c5e54b113b
  1. 115
      src/gfsec-split.c

115
src/gfsec-split.c

@ -86,11 +86,122 @@ See the COPYING file or <http://www.gnu.org/licenses/gpl.html>.\n\
exit(EXIT_SUCCESS);
}
static char *
ask(const char *prompt)
{
char *buffer = NULL;
size_t len = 0;
ssize_t n;
printf("%s ", prompt);
if ( (n = getline(&buffer, &len, stdin)) == -1 )
err(EXIT_FAILURE, "Cannot read from standard input");
else if ( n > 0 && buffer[n - 1] == '\n' )
buffer[n - 1] = '\0';
return buffer;
}
static char *
ask_nonempty(const char *prompt)
{
char *answer;
int done = 0;
do {
answer = ask(prompt);
if ( ! (done = *answer != '\0') )
free(answer);
} while ( ! done );
return answer;
}
static int
ask_confirmation(const char *prompt)
{
int rc = 0;
char *answer;
answer = ask(prompt);
if ( (*answer == 'y' || *answer == 'Y') && *(answer + 1) == '\0' )
rc = 1;
free(answer);
return rc;
}
static int
prompt_for_share(gfsec_secret_t *cfg, gfsec_supports_list_t *list, unsigned number)
{
int i, rc;
char *answer, *path;
gfsec_share_t *share;
rc = -1;
printf("Select a support for share #%u:\n\n", number);
for ( i = 0; i < list->count; i++ )
printf(" (%d) %s\n", i+1, list->supports[i].description);
printf(" (x) Done\n\n");
while ( rc == -1 ) {
answer = ask ("Your choice?");
if ( (*answer == 'x' || *answer == 'X') && *(answer + 1) == '\0' )
rc = 1; /* We're done selecting supports. */
else {
int selected;
selected = strtol(answer, NULL, 10);
if ( selected > 0 && selected < list->count ) {
path = ask_nonempty("Specify a pathname on this support:");
share = gfsec_share_new();
gfsec_share_set_info(share, GFSEC_SHARE_NUMBER_AUTOASSIGN,
list->supports[selected - 1].scheme,
list->supports[selected - 1].authority,
path, NULL);
if ( (i = gfsec_secret_add_share(cfg, share)) != 0 )
errx(EXIT_FAILURE, "Cannot add share: %s", gfsec_error_string(i));
rc = 0; /* Done for this share. */
free(path);
}
}
free(answer);
}
return rc;
}
static void
interactive_loop(gfsec_secret_t *cfg)
{
errno = ENOSYS;
err(EXIT_FAILURE, "Cannot enter interactive loop");
gfsec_supports_list_t *list;
int i;
char buffer[255];
list = gfsec_scheme_module_get_supports();
i = 0;
while ( ! prompt_for_share(cfg, list, ++i) ) ;
gfsec_support_destroy_list(list);
printf("\nThe following shares will be created: %d\n", cfg->n_shares);
for ( i = 0; i < cfg->n_shares; i++ ) {
gfsec_share_get_uri(cfg->shares[i], buffer, sizeof(buffer));
printf(" %s\n", buffer);
}
putchar('\n');
if ( ! ask_confirmation("Proceed (y/N)?") )
errx(EXIT_FAILURE, "Split cancelled");
}
static unsigned

Loading…
Cancel
Save