feat/command-dispatch: introduce generic command dispatch #1
4 changed files with 33 additions and 93 deletions
|
|
@ -1,8 +0,0 @@
|
|||
#ifndef FT_SSL_INTERACTIVE_H
|
||||
#define FT_SSL_INTERACTIVE_H
|
||||
|
||||
#include <cli.h>
|
||||
|
||||
enum cli_code run_interactive(void);
|
||||
|
||||
#endif
|
||||
|
|
@ -41,15 +41,14 @@ FORCE:
|
|||
|
||||
ft_ssl_SOURCES = \
|
||||
main.c \
|
||||
ssl_run.c \
|
||||
cmd/command_table.c \
|
||||
cmd/digest_run.c \
|
||||
ssl/run_stdin.c \
|
||||
ssl/run_string.c \
|
||||
ssl/run_file.c \
|
||||
ssl/digest.c \
|
||||
ssl/output.c \
|
||||
cli/parse.c \
|
||||
cli/version.c \
|
||||
cli/interactive.c \
|
||||
cli/handlers/option_map.c \
|
||||
cli/handlers/handle_help.c \
|
||||
cli/handlers/handle_version.c \
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
}
|
||||
41
src/main.c
41
src/main.c
|
|
@ -1,24 +1,45 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <cli.h>
|
||||
|
||||
#include "ft_ssl.h"
|
||||
#include "internal/cmd/command.h"
|
||||
#include "version_gen.h"
|
||||
|
||||
char *g_prog_name = NULL;
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
struct ssl_config config = {0};
|
||||
int ret = CLI_ERROR;
|
||||
const struct command_descriptor *cmd;
|
||||
int ret;
|
||||
|
||||
g_prog_name = argv[0];
|
||||
cli_set_prog_name(g_prog_name);
|
||||
ret = cli_parse_arguments(argc, argv, &config);
|
||||
if (CLI_SUCCESS != ret)
|
||||
goto cleanup;
|
||||
ret = ssl_run(&config);
|
||||
|
||||
cleanup:
|
||||
return (CLI_ERROR == ret) ? 2 : EXIT_SUCCESS;
|
||||
if (argc < 2)
|
||||
{
|
||||
print_commands(stderr);
|
||||
return 2;
|
||||
}
|
||||
if (0 == strcmp(argv[1], "-h") || 0 == strcmp(argv[1], "--help"))
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue