refactor: rework command dispacher

This commit is contained in:
lohhiiccc 2026-07-10 22:57:29 +02:00 committed by lohhiiccc
parent 27797d1ea0
commit f7878a0246
31 changed files with 307 additions and 383 deletions

View file

@ -3,11 +3,12 @@
#include <cli.h>
int cli_handle_help(const char *arg, void *config);
int cli_handle_help(const char *arg, void *config_void);
int cli_handle_version(const char *arg, void *config);
int cli_handle_p(const char *arg, void *config);
int cli_handle_q(const char *arg, void *config);
int cli_handle_r(const char *arg, void *config);
int cli_handle_s(const char *arg, void *config);
int digest_handle_help(const char *arg, void *config);
int digest_handle_p(const char *arg, void *config);
int digest_handle_q(const char *arg, void *config);
int digest_handle_r(const char *arg, void *config);
int digest_handle_s(const char *arg, void *config);
#endif

View file

@ -5,32 +5,14 @@
#include <cli.h>
#include "internal/cli/cli_handlers.h"
#include "internal/cmd/command.h"
#include "internal/cli/command.h"
#define DIGEST_OPTION_LIST \
X( \
'h', \
"help", \
no_argument, \
cli_handle_help, \
OPT_ARG_NONE, \
"Display this help and exit", \
0 \
) \
X( \
'V', \
"version", \
no_argument, \
cli_handle_version, \
OPT_ARG_NONE, \
"Display version information and exit", \
0 \
) \
X( \
'p', \
"stdin", \
no_argument, \
cli_handle_p, \
digest_handle_p, \
OPT_ARG_NONE, \
"Echo stdin to stdout and append checksum to output", \
0 \
@ -39,7 +21,7 @@
'q', \
"quiet", \
no_argument, \
cli_handle_q, \
digest_handle_q, \
OPT_ARG_NONE, \
"Quiet mode: only print the digest", \
0 \
@ -48,7 +30,7 @@
'r', \
"reverse", \
no_argument, \
cli_handle_r, \
digest_handle_r, \
OPT_ARG_NONE, \
"Reverse the output format (digest then filename)", \
0 \
@ -57,11 +39,29 @@
's', \
"string", \
required_argument, \
cli_handle_s, \
digest_handle_s, \
OPT_ARG_STRING, \
"Hash a string", \
CLI_OPT_F_REPEATABLE \
) \
X( \
'h', \
"help", \
no_argument, \
digest_handle_help, \
OPT_ARG_NONE, \
"Display this help and exit", \
0 \
)
// X( \
// 'V', \
// "version", \
// no_argument, \
// digest_handle_version, \
// OPT_ARG_NONE, \
// "Display version information and exit", \
// 0 \
// )
#undef X
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc, flags) + 1

View file

@ -0,0 +1,6 @@
#ifndef FT_SSL_CLI_DISPATCH_H
#define FT_SSL_CLI_DISPATCH_H
int ft_ssl_dispatch(int argc, char **argv);
#endif

View file

@ -0,0 +1,45 @@
#ifndef FT_SSL_CMD_GLOBAL_H
#define FT_SSL_CMD_GLOBAL_H
#include <getopt.h>
#include <cli.h>
#include "internal/cli/cli_handlers.h"
#define GLOBAL_OPTION_LIST \
X( \
'h', \
"help", \
no_argument, \
cli_handle_help, \
OPT_ARG_NONE, \
"Display this help and exit", \
0 \
) \
X( \
'v', \
"version", \
no_argument, \
cli_handle_version, \
OPT_ARG_NONE, \
"Display version information and exit", \
0 \
)
#undef X
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc, flags) + 1
enum { GLOBAL_OPT_LEN = (0 GLOBAL_OPTION_LIST) };
#undef X
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc, flags) \
+ (1 * (has_arg == no_argument) \
+ (2 * (has_arg == required_argument) \
+ (3 * (has_arg == optional_argument))))
enum { GLOBAL_OPTSTR_LEN = (2 GLOBAL_OPTION_LIST) };
/* +2 for ':' to disable getopt error messages and \0 */
#undef X
extern const struct option_descriptor g_global_opts[];
#endif

View file

@ -2,74 +2,31 @@
#define FT_SSL_OPT_H
#include <cli.h>
#include <libft_ssl.h>
#define FT_SSL_OPTION_LIST \
X( \
'h', \
"help", \
no_argument, \
cli_handle_help, \
OPT_ARG_NONE, \
"Display this help and exit", \
0 \
) \
X( \
'V', \
"version", \
no_argument, \
cli_handle_version, \
OPT_ARG_NONE, \
"Display version information and exit", \
0 \
) \
X( \
'p', \
"stdin", \
no_argument, \
cli_handle_p, \
OPT_ARG_NONE, \
"Echo stdin to stdout and append checksum to output", \
0 \
) \
X( \
'q', \
"quiet", \
no_argument, \
cli_handle_q, \
OPT_ARG_NONE, \
"Quiet mode: only print the digest", \
0 \
) \
X( \
'r', \
"reverse", \
no_argument, \
cli_handle_r, \
OPT_ARG_NONE, \
"Reverse the output format (digest then filename)", \
0 \
) \
X( \
's', \
"string", \
required_argument, \
cli_handle_s, \
OPT_ARG_STRING, \
"Hash a string", \
CLI_OPT_F_REPEATABLE \
)
#include "internal/cli/command.h"
#include "internal/cli/digest_cmd.h"
#undef X
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc, flags) + 1
enum { FT_SSL_OPT_LEN = (0 FT_SSL_OPTION_LIST ) };
#undef X
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc, flags) \
+ (1 * (has_arg == no_argument) \
+ (2 * (has_arg == required_argument) \
+ (3 * (has_arg == optional_argument))))
enum { FT_SSL_OPTSTR_LEN = (2 FT_SSL_OPTION_LIST) };
/* +2 for ':' to disable getopt error messages and \0 */
/* Forward-declare all digest algo globals */
#define DIGEST_ALGO(lower, ds, bs) extern const struct digest_algo g_##lower;
#include <digest_algos.h>
#undef DIGEST_ALGO
static const struct command_descriptor g_commands[] = {
/* Digest commands */
#define DIGEST_ALGO(lower, ds, bs) \
{ #lower, CMD_DIGEST, g_digest_opts, DIGEST_OPT_LEN, &g_##lower, digest_run },
#include <digest_algos.h>
#undef DIGEST_ALGO
/* Cipher commands (not yet implemented) */
{ "base64", CMD_CIPHER, NULL, 0, NULL, NULL },
{ "des", CMD_CIPHER, NULL, 0, NULL, NULL },
{ "des-ecb", CMD_CIPHER, NULL, 0, NULL, NULL },
{ "des-cbc", CMD_CIPHER, NULL, 0, NULL, NULL },
/* Sentinel */
{ NULL, 0, NULL, 0, NULL, NULL }
};
void print_usage(const char *prog);

View file

@ -1,5 +1,5 @@
#ifndef FT_SSL_SSL_H
#define FT_SSL_SSL_H
#ifndef FT_SSL_CMD_DIGEST_ENGINE_H
#define FT_SSL_CMD_DIGEST_ENGINE_H
#include <stdint.h>
@ -8,9 +8,9 @@
#define MAX_DIGEST_SIZE 64
#define READ_BUFSIZE 4096
int run_string(const struct digest_config *config, const char *s);
int run_file(const struct digest_config *config, const char *path);
int run_stdin(const struct digest_config *config);
int digest_run_string(const struct digest_config *config, const char *s);
int digest_run_file(const struct digest_config *config, const char *path);
int digest_run_stdin(const struct digest_config *config);
int digest_stream(const struct digest_config *config, int fd,
const char *label, int show_algo, int echo);
void print_digest(const struct digest_config *config, const uint8_t *digest,

View file

@ -41,21 +41,25 @@ FORCE:
ft_ssl_SOURCES = \
main.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/version.c \
cli/handlers/option_map.c \
cli/handlers/handle_help.c \
cli/dispatch.c \
cli/command_table.c \
cli/print_version.c \
cli/print_help.c \
cli/handlers/handle_version.c \
cli/handlers/handle_p.c \
cli/handlers/handle_q.c \
cli/handlers/handle_r.c \
cli/handlers/handle_s.c
cli/handlers/handle_help.c \
cli/handlers/global/option_map.c \
cli/handlers/digest/option_map.c \
cli/handlers/digest/handle_help.c \
cli/handlers/digest/handle_p.c \
cli/handlers/digest/handle_q.c \
cli/handlers/digest/handle_r.c \
cli/handlers/digest/handle_s.c \
cmd/digest/run.c \
cmd/digest/run_stdin.c \
cmd/digest/run_string.c \
cmd/digest/run_file.c \
cmd/digest/digest.c \
cmd/digest/output.c
ft_ssl_CPPFLAGS = \
-I $(top_srcdir)/includes \

16
src/cli/command_table.c Normal file
View file

@ -0,0 +1,16 @@
#include <string.h>
#include "internal/cli/option.h"
const struct command_descriptor *
find_command(const char *name)
{
const struct command_descriptor *cmd;
for (cmd = g_commands; NULL != cmd->name; cmd++)
if (0 == strcmp(cmd->name, name))
return cmd;
return NULL;
}

79
src/cli/dispatch.c Normal file
View file

@ -0,0 +1,79 @@
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <cli.h>
#include "internal/cli/command.h"
#include "internal/cli/dispatch.h"
#include "internal/cli/global_cmd.h"
#include "version_gen.h"
/* Forward declarations */
static int find_subcommand_index(int argc, char **argv);
static enum cli_code parse_global_options(char **argv, int sub_idx);
static int dispatch_command(int argc, char **argv, int sub_idx);
/* ------------------- */
int
ft_ssl_dispatch(int argc, char **argv)
{
int sub_idx;
enum cli_code ret;
sub_idx = find_subcommand_index(argc, argv);
ret = parse_global_options(argv, sub_idx);
if (CLI_SUCCESS != ret)
return ((int)CLI_ERROR == ret) ? 2 : EXIT_SUCCESS;
return dispatch_command(argc, argv, sub_idx);
}
static int
find_subcommand_index(int argc, char **argv)
{
int i;
for (i = 1; i < argc; i++)
if ('-' != argv[i][0])
return i;
return argc;
}
static enum cli_code
parse_global_options(char **argv, int sub_idx)
{
char opt_str[GLOBAL_OPTSTR_LEN + 1];
struct option long_opts[GLOBAL_OPT_LEN + 1];
return cli_parse(sub_idx, argv, NULL, g_global_opts, GLOBAL_OPT_LEN,
opt_str, long_opts);
}
static int
dispatch_command(int argc, char **argv, int sub_idx)
{
const struct command_descriptor *cmd;
int ret;
if (sub_idx >= argc)
{
print_commands(stderr);
return 2;
}
cmd = find_command(argv[sub_idx]);
if (NULL == cmd)
{
fprintf(stderr, "%s: Error: %s is an invalid command.\n",
g_prog_name, argv[sub_idx]);
print_commands(stderr);
return 2;
}
if (NULL == cmd->run)
{
fprintf(stderr, "%s: %s: not yet implemented\n",
g_prog_name, argv[sub_idx]);
return 2;
}
ret = cmd->run(argc - sub_idx, argv + sub_idx, cmd);
return ((int)CLI_ERROR == ret) ? 2 : EXIT_SUCCESS;
}

View file

@ -0,0 +1,17 @@
#include <cli.h>
#include <stdio.h>
#include "compiler.h"
#include "ft_ssl.h"
#include "internal/cli/digest_cmd.h"
#include "version_gen.h"
int
digest_handle_help(__unused const char *arg, void *config_void)
{
struct digest_config *config = (struct digest_config *)config_void;
printf("Usage: %s %s [OPTIONS] [FILE...]\n\n", g_prog_name, config->cmd_name);
cli_print_options(g_digest_opts, DIGEST_OPT_LEN, cli_arg_type_to_str);
return CLI_EXIT_SUCCESS;
}

View file

@ -4,7 +4,7 @@
#include "internal/cli/cli_handlers.h"
int
cli_handle_p(__unused const char *arg, void *config_void)
digest_handle_p(__unused const char *arg, void *config_void)
{
struct digest_config *config = (struct digest_config *)config_void;

View file

@ -4,7 +4,7 @@
#include "internal/cli/cli_handlers.h"
int
cli_handle_q(__unused const char *arg, void *config_void)
digest_handle_q(__unused const char *arg, void *config_void)
{
struct digest_config *config = (struct digest_config *)config_void;

View file

@ -4,7 +4,7 @@
#include "internal/cli/cli_handlers.h"
int
cli_handle_r(__unused const char *arg, void *config_void)
digest_handle_r(__unused const char *arg, void *config_void)
{
struct digest_config *config = (struct digest_config *)config_void;

View file

@ -2,7 +2,7 @@
#include "internal/cli/cli_handlers.h"
int
cli_handle_s(const char *arg, void *config_void)
digest_handle_s(const char *arg, void *config_void)
{
struct digest_config *config = (struct digest_config *)config_void;

View file

@ -1,4 +1,4 @@
#include "internal/cmd/digest_cmd.h"
#include "internal/cli/digest_cmd.h"
#undef X
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc, flags) \

View file

@ -0,0 +1,12 @@
#include "internal/cli/global_cmd.h"
#undef X
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc, flags) \
{ short_opt, long_opt, has_arg, handler, arg_type, desc, flags },
const struct option_descriptor g_global_opts[] = {
GLOBAL_OPTION_LIST
{0, NULL, 0, NULL, OPT_ARG_NONE, NULL, 0}
};
#undef X

View file

@ -1,17 +1,18 @@
#include <cli.h>
#include <stdio.h>
#include <libft_ssl.h>
#include <cli.h>
#include "internal/cli/global_cmd.h"
#include "internal/cli/option.h"
#include "compiler.h"
#include "ft_ssl.h"
#include "internal/cmd/digest_cmd.h"
#include "version_gen.h"
int
cli_handle_help(__unused const char *arg, void *config_void)
cli_handle_help(__unused const char *arg, __unused void *config_void)
{
struct digest_config *config = (struct digest_config *)config_void;
printf("Usage: %s %s [OPTIONS] [FILE...]\n\n", g_prog_name, config->cmd_name);
cli_print_options(g_digest_opts, DIGEST_OPT_LEN, cli_arg_type_to_str);
printf("Usage: %s [OPTIONS] <command> [args...]\n\n", g_prog_name);
cli_print_options(g_global_opts, GLOBAL_OPT_LEN, cli_arg_type_to_str);
print_commands(stdout);
return CLI_EXIT_SUCCESS;
}

View file

@ -1,5 +1,3 @@
#include <stdio.h>
#include <cli.h>
#include "compiler.h"

View file

@ -1,91 +0,0 @@
#include <getopt.h>
#include <stdio.h>
#include <string.h>
#include <cli.h>
#include <libft_ssl.h>
#include "ft_ssl.h"
#include "internal/cli/cli_handlers.h"
#include "internal/cli/interactive.h"
#include "internal/cli/option.h"
/* Forward declarations */
static void init_config(struct ssl_config *config);
static const struct digest_algo *find_algo(const char *name);
static enum cli_code parse_subcommand(int argc, char **argv, int first_arg,
struct ssl_config *config);
/* ------------------- */
static const struct digest_algo * const s_algos[] = {
#define DIGEST_ALGO(lower, ds, bs) &g_##lower,
#include <digest_algos.h>
#undef DIGEST_ALGO
NULL
};
enum cli_code
cli_parse_arguments(int argc, char **argv, struct ssl_config *config)
{
char opt_str[FT_SSL_OPTSTR_LEN];
struct option long_opts[FT_SSL_OPT_LEN + 1];
enum cli_code ret;
init_config(config);
ret = cli_parse(argc, argv, config, g_options, FT_SSL_OPT_LEN, opt_str, long_opts);
if (CLI_SUCCESS != ret)
return ret;
return parse_subcommand(argc, argv, optind, config);
}
static enum cli_code
parse_subcommand(int argc, char **argv, int first_arg,
struct ssl_config *config)
{
if (first_arg >= argc)
return run_interactive();
config->algo = find_algo(argv[first_arg]);
if (NULL == config->algo)
{
fprintf(stderr, "%s: unknown command -- '%s'\n", argv[0], argv[first_arg]);
print_usage(argv[0]);
return CLI_ERROR;
}
config->files = argv + first_arg + 1;
config->nb_files = argc - first_arg - 1;
return CLI_SUCCESS;
}
static const struct digest_algo *
find_algo(const char *name)
{
size_t i;
for (i = 0; NULL != s_algos[i]; i++)
{
if (0 == strcmp(s_algos[i]->name, name))
return s_algos[i];
}
return NULL;
}
void
print_usage(const char *prog)
{
size_t i;
fprintf(stderr, "usage: %s {", prog);
for (i = 0; NULL != s_algos[i]; i++)
{
if (0 != i)
fprintf(stderr, "|");
fprintf(stderr, "%s", s_algos[i]->name);
}
fprintf(stderr, "} [-pqr] [-s string] [file...]\n");
}
static void
init_config(struct ssl_config *config)
{
memset(config, 0, sizeof(struct ssl_config));
}

20
src/cli/print_help.c Normal file
View file

@ -0,0 +1,20 @@
#include <stdio.h>
#include <libft_ssl.h>
#include "internal/cli/option.h"
void
print_commands(FILE *out)
{
const struct command_descriptor *cmd;
fprintf(out, "\nMessage Digest commands:\n");
for (cmd = g_commands; NULL != cmd->name; cmd++)
if (CMD_DIGEST == cmd->category)
fprintf(out, " %s\n", cmd->name);
fprintf(out, "\nCipher commands:\n");
for (cmd = g_commands; NULL != cmd->name; cmd++)
if (CMD_CIPHER == cmd->category)
fprintf(out, " %s\n", cmd->name);
}

View file

@ -1,54 +0,0 @@
#include <stdio.h>
#include <string.h>
#include <libft_ssl.h>
#include "internal/cmd/command.h"
#include "internal/cmd/digest_cmd.h"
/* Forward-declare all digest algo globals */
#define DIGEST_ALGO(lower, ds, bs) extern const struct digest_algo g_##lower;
#include <digest_algos.h>
#undef DIGEST_ALGO
static const struct command_descriptor s_commands[] = {
/* Digest commands */
#define DIGEST_ALGO(lower, ds, bs) \
{ #lower, CMD_DIGEST, g_digest_opts, DIGEST_OPT_LEN, &g_##lower, digest_run },
#include <digest_algos.h>
#undef DIGEST_ALGO
/* Cipher commands (not yet implemented) */
{ "base64", CMD_CIPHER, NULL, 0, NULL, NULL },
{ "des", CMD_CIPHER, NULL, 0, NULL, NULL },
{ "des-ecb", CMD_CIPHER, NULL, 0, NULL, NULL },
{ "des-cbc", CMD_CIPHER, NULL, 0, NULL, NULL },
/* Sentinel */
{ NULL, 0, NULL, 0, NULL, NULL }
};
const struct command_descriptor *
find_command(const char *name)
{
const struct command_descriptor *cmd;
for (cmd = s_commands; NULL != cmd->name; cmd++)
if (0 == strcmp(cmd->name, name))
return cmd;
return NULL;
}
void
print_commands(FILE *out)
{
const struct command_descriptor *cmd;
fprintf(out, "\nStandard commands:\n");
fprintf(out, "\nMessage Digest commands:\n");
for (cmd = s_commands; NULL != cmd->name; cmd++)
if (CMD_DIGEST == cmd->category)
fprintf(out, " %s\n", cmd->name);
fprintf(out, "\nCipher commands:\n");
for (cmd = s_commands; NULL != cmd->name; cmd++)
if (CMD_CIPHER == cmd->category)
fprintf(out, " %s\n", cmd->name);
}

View file

@ -4,7 +4,7 @@
#include <libft_ssl.h>
#include "ft_ssl.h"
#include "internal/ssl/ssl.h"
#include "internal/cmd/digest.h"
/* Forward declarations */
static void process_chunk(const struct digest_config *config, union digest_ctx *ctx,

View file

@ -6,7 +6,7 @@
#include "compiler.h"
#include "ft_ssl.h"
#include "ft_ssl_flags.h"
#include "internal/ssl/ssl.h"
#include "internal/cmd/digest.h"
enum fmt_mode
{

View file

@ -7,9 +7,9 @@
#include "compiler.h"
#include "ft_ssl.h"
#include "ft_ssl_flags.h"
#include "internal/cmd/command.h"
#include "internal/cmd/digest_cmd.h"
#include "internal/ssl/ssl.h"
#include "internal/cli/command.h"
#include "internal/cli/digest_cmd.h"
#include "internal/cmd/digest.h"
#include "version_gen.h"
/* Forward declarations */
@ -45,11 +45,11 @@ digest_run(int argc, char **argv, const struct command_descriptor *self)
ret_val = (int)CLI_SUCCESS;
has_input = (0 < config.nb_strings || 0 < config.nb_files);
if (HAS_FLAG(config.flags, FLAG_P) || !has_input)
update_ret(&ret_val, run_stdin(&config));
update_ret(&ret_val, digest_run_stdin(&config));
for (i = 0; i < config.nb_strings; i++)
update_ret(&ret_val, run_string(&config, config.strings[i]));
update_ret(&ret_val, digest_run_string(&config, config.strings[i]));
for (i = 0; i < config.nb_files; i++)
update_ret(&ret_val, run_file(&config, config.files[i]));
update_ret(&ret_val, digest_run_file(&config, config.files[i]));
return ret_val;
}

View file

@ -7,11 +7,11 @@
#include <cli.h>
#include "ft_ssl.h"
#include "internal/ssl/ssl.h"
#include "internal/cmd/digest.h"
#include "version_gen.h"
int
run_file(const struct digest_config *config, const char *path)
digest_run_file(const struct digest_config *config, const char *path)
{
int fd;
int ret;

View file

@ -8,7 +8,7 @@
#include "compiler.h"
#include "ft_ssl.h"
#include "ft_ssl_flags.h"
#include "internal/ssl/ssl.h"
#include "internal/cmd/digest.h"
/* Forward declarations */
static int run_stdin_p(const struct digest_config *config);
@ -17,7 +17,7 @@ static ssize_t read_and_hash(const struct digest_config *config, union digest_ct
/* ------------------- */
int
run_stdin(const struct digest_config *config)
digest_run_stdin(const struct digest_config *config)
{
if (HAS_FLAG(config->flags, FLAG_P) && !HAS_FLAG(config->flags, FLAG_Q))
return run_stdin_p(config);

View file

@ -5,10 +5,10 @@
#include <libft_ssl.h>
#include "ft_ssl.h"
#include "internal/ssl/ssl.h"
#include "internal/cmd/digest.h"
int
run_string(const struct digest_config *config, const char *s)
digest_run_string(const struct digest_config *config, const char *s)
{
union digest_ctx ctx;
uint8_t digest[MAX_DIGEST_SIZE];

View file

@ -1,10 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cli.h>
#include "internal/cmd/command.h"
#include "internal/cli/dispatch.h"
#include "version_gen.h"
char *g_prog_name = NULL;
@ -12,34 +8,7 @@ char *g_prog_name = NULL;
int
main(int argc, char **argv)
{
const struct command_descriptor *cmd;
int ret;
g_prog_name = argv[0];
cli_set_prog_name(g_prog_name);
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;
return ft_ssl_dispatch(argc, argv);
}

View file

@ -1,56 +0,0 @@
#include <stdio.h>
#include <cli.h>
#include <libft_ssl.h>
#include "compiler.h"
#include "ft_ssl.h"
#include "ft_ssl_flags.h"
#include "internal/ssl/ssl.h"
#include "version_gen.h"
/* Forward declarations */
static int check_algo(const struct ssl_config *config);
static inline void update_ret(int *ret, int result);
/* ------------------- */
int
ssl_run(const struct ssl_config *config)
{
int ret;
int has_input;
int i;
if (CLI_SUCCESS != check_algo(config))
return CLI_ERROR;
ret = CLI_SUCCESS;
has_input = (0 < config->nb_strings || 0 < config->nb_files);
if (HAS_FLAG(config->flags, FLAG_P) || !has_input)
update_ret(&ret, run_stdin(config));
for (i = 0; i < config->nb_strings; i++)
update_ret(&ret, run_string(config, config->strings[i]));
for (i = 0; i < config->nb_files; i++)
update_ret(&ret, run_file(config, config->files[i]));
return ret;
}
static int
check_algo(const struct ssl_config *config)
{
if (NULL == config->algo->init
|| NULL == config->algo->update
|| NULL == config->algo->final)
{
fprintf(stderr, "%s: %s: not yet implemented\n",
g_prog_name, config->algo->name);
return CLI_ERROR;
}
return CLI_SUCCESS;
}
static inline void
update_ret(int *ret, int result)
{
if (CLI_ERROR == result)
*ret = CLI_ERROR;
}