feat: interactive mode
This commit is contained in:
parent
17488fdee0
commit
95c902703e
4 changed files with 94 additions and 5 deletions
8
includes/internal/cli/interactive.h
Normal file
8
includes/internal/cli/interactive.h
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#ifndef FT_SSL_INTERACTIVE_H
|
||||
#define FT_SSL_INTERACTIVE_H
|
||||
|
||||
#include <cli.h>
|
||||
|
||||
enum cli_code run_interactive(void);
|
||||
|
||||
#endif
|
||||
|
|
@ -48,6 +48,7 @@ ft_ssl_SOURCES = \
|
|||
ssl/digest.c \
|
||||
ssl/output.c \
|
||||
cli/parse.c \
|
||||
cli/interactive.c \
|
||||
cli/handlers/option_map.c \
|
||||
cli/handlers/handle_help.c \
|
||||
cli/handlers/handle_version.c \
|
||||
|
|
|
|||
83
src/cli/interactive.c
Normal file
83
src/cli/interactive.c
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <cli.h>
|
||||
#include <libft_ssl.h>
|
||||
|
||||
#include "ft_ssl.h"
|
||||
#include "internal/cli/interactive.h"
|
||||
|
||||
#define INTERACTIVE_BUFSIZ 4096
|
||||
|
||||
static const struct digest_algo * const s_algos[] = {
|
||||
#define DIGEST_ALGO(lower, ds, bs) &g_##lower,
|
||||
#include <digest_algos.h>
|
||||
#undef DIGEST_ALGO
|
||||
NULL
|
||||
};
|
||||
|
||||
static const struct digest_algo *find_algo(const char *name);
|
||||
static void print_available(void);
|
||||
|
||||
enum cli_code
|
||||
run_interactive(void)
|
||||
{
|
||||
char buf[INTERACTIVE_BUFSIZ];
|
||||
char *sep;
|
||||
const struct digest_algo *algo;
|
||||
struct ssl_config config;
|
||||
|
||||
while (fgets(buf, (int)sizeof(buf), stdin))
|
||||
{
|
||||
buf[strcspn(buf, "\n")] = '\0';
|
||||
if (buf[0] == '\0')
|
||||
continue;
|
||||
sep = buf;
|
||||
while (*sep && *sep != ' ' && *sep != '\t')
|
||||
sep++;
|
||||
if (*sep)
|
||||
{
|
||||
*sep = '\0';
|
||||
sep++;
|
||||
while (*sep == ' ' || *sep == '\t')
|
||||
sep++;
|
||||
}
|
||||
algo = find_algo(buf);
|
||||
if (!algo)
|
||||
{
|
||||
fprintf(stderr, "ft_ssl: unknown command -- '%s'\n", buf);
|
||||
print_available();
|
||||
continue;
|
||||
}
|
||||
memset(&config, 0, sizeof(config));
|
||||
config.algo = algo;
|
||||
config.string = (*sep != '\0') ? sep : NULL;
|
||||
ssl_run(&config);
|
||||
}
|
||||
return CLI_EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
static const struct digest_algo *
|
||||
find_algo(const char *name)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; s_algos[i]; i++)
|
||||
{
|
||||
if (strcmp(s_algos[i]->name, name) == 0)
|
||||
return s_algos[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
print_available(void)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
fprintf(stderr, "available commands:");
|
||||
for (i = 0; s_algos[i]; i++)
|
||||
fprintf(stderr, " %s", s_algos[i]->name);
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
#include "ft_ssl.h"
|
||||
#include "internal/cli/cli_handlers.h"
|
||||
#include "internal/cli/interactive.h"
|
||||
#include "internal/cli/option.h"
|
||||
|
||||
/* Forward declarations */
|
||||
|
|
@ -43,11 +44,7 @@ parse_subcommand(int argc, char **argv, int first_arg,
|
|||
struct ssl_config *config)
|
||||
{
|
||||
if (first_arg >= argc)
|
||||
{
|
||||
fprintf(stderr, "%s: missing command\n", argv[0]);
|
||||
print_usage(argv[0]);
|
||||
return CLI_ERROR;
|
||||
}
|
||||
return run_interactive();
|
||||
config->algo = find_algo(argv[first_arg]);
|
||||
if (NULL == config->algo)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue