refactor: replace interactive REPL with command dispatch

This commit is contained in:
lohhiiccc 2026-06-27 17:06:12 +02:00
parent 2721fdb48b
commit afcd1d6d05
4 changed files with 33 additions and 93 deletions

View file

@ -1,8 +0,0 @@
#ifndef FT_SSL_INTERACTIVE_H
#define FT_SSL_INTERACTIVE_H
#include <cli.h>
enum cli_code run_interactive(void);
#endif

View file

@ -41,15 +41,14 @@ FORCE:
ft_ssl_SOURCES = \ ft_ssl_SOURCES = \
main.c \ main.c \
ssl_run.c \ cmd/command_table.c \
cmd/digest_run.c \
ssl/run_stdin.c \ ssl/run_stdin.c \
ssl/run_string.c \ ssl/run_string.c \
ssl/run_file.c \ ssl/run_file.c \
ssl/digest.c \ ssl/digest.c \
ssl/output.c \ ssl/output.c \
cli/parse.c \
cli/version.c \ cli/version.c \
cli/interactive.c \
cli/handlers/option_map.c \ cli/handlers/option_map.c \
cli/handlers/handle_help.c \ cli/handlers/handle_help.c \
cli/handlers/handle_version.c \ cli/handlers/handle_version.c \

View file

@ -1,72 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.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];
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;
algo = find_algo(buf);
if (NULL == algo)
{
fprintf(stderr, "ft_ssl: unknown command -- '%s'\n", buf);
print_available();
continue;
}
memset(&config, 0, sizeof(config));
config.algo = algo;
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 (0 == strcmp(s_algos[i]->name, name))
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");
}

View file

@ -1,24 +1,45 @@
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <cli.h> #include <cli.h>
#include "ft_ssl.h" #include "internal/cmd/command.h"
#include "version_gen.h"
char *g_prog_name = NULL; char *g_prog_name = NULL;
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
struct ssl_config config = {0}; const struct command_descriptor *cmd;
int ret = CLI_ERROR; int ret;
g_prog_name = argv[0]; g_prog_name = argv[0];
cli_set_prog_name(g_prog_name); cli_set_prog_name(g_prog_name);
ret = cli_parse_arguments(argc, argv, &config); if (argc < 2)
if (CLI_SUCCESS != ret) {
goto cleanup; print_commands(stderr);
ret = ssl_run(&config); return 2;
}
cleanup: if (0 == strcmp(argv[1], "-h") || 0 == strcmp(argv[1], "--help"))
return (CLI_ERROR == ret) ? 2 : EXIT_SUCCESS; {
print_commands(stdout);
return EXIT_SUCCESS;
}
cmd = find_command(argv[1]);
if (NULL == cmd)
{
fprintf(stderr, "%s: Error: %s is an invalid command.\n",
argv[0], argv[1]);
print_commands(stderr);
return 2;
}
if (NULL == cmd->run)
{
fprintf(stderr, "%s: %s: not yet implemented\n", argv[0], argv[1]);
return 2;
}
ret = cmd->run(argc - 1, argv + 1, cmd);
return ((int)CLI_ERROR == ret) ? 2 : EXIT_SUCCESS;
} }