feat/command-dispatch: introduce generic command dispatch #1

Merged
loic merged 5 commits from feat/command-dispatch into main 2026-06-27 15:12:11 +00:00
11 changed files with 27 additions and 29 deletions
Showing only changes of commit 66b0a2cb66 - Show all commits

View file

@ -5,19 +5,17 @@
#include <libft_ssl.h> #include <libft_ssl.h>
#include <stdint.h> #include <stdint.h>
#define SSL_MAX_STRINGS 64 #define DIGEST_MAX_STRINGS 64
struct ssl_config struct digest_config
{ {
const struct digest_algo *algo; const struct digest_algo *algo;
const char *cmd_name;
uint8_t flags; uint8_t flags;
const char *strings[SSL_MAX_STRINGS]; const char *strings[DIGEST_MAX_STRINGS];
int nb_strings; int nb_strings;
char **files; char **files;
int nb_files; int nb_files;
}; };
enum cli_code cli_parse_arguments(int argc, char **argv, struct ssl_config *config);
int ssl_run(const struct ssl_config *config);
#endif #endif

View file

@ -8,12 +8,12 @@
#define MAX_DIGEST_SIZE 64 #define MAX_DIGEST_SIZE 64
#define READ_BUFSIZE 4096 #define READ_BUFSIZE 4096
int run_string(const struct ssl_config *config, const char *s); int run_string(const struct digest_config *config, const char *s);
int run_file(const struct ssl_config *config, const char *path); int run_file(const struct digest_config *config, const char *path);
int run_stdin(const struct ssl_config *config); int run_stdin(const struct digest_config *config);
int digest_stream(const struct ssl_config *config, int fd, int digest_stream(const struct digest_config *config, int fd,
const char *label, int show_algo, int echo); const char *label, int show_algo, int echo);
void print_digest(const struct ssl_config *config, const uint8_t *digest, void print_digest(const struct digest_config *config, const uint8_t *digest,
const char *label, int show_algo); const char *label, int show_algo);
#endif #endif

View file

@ -6,7 +6,7 @@
int int
cli_handle_p(__unused const char *arg, void *config_void) cli_handle_p(__unused const char *arg, void *config_void)
{ {
struct ssl_config *config = (struct ssl_config *)config_void; struct digest_config *config = (struct digest_config *)config_void;
SET_FLAG(config->flags, FLAG_P); SET_FLAG(config->flags, FLAG_P);
return CLI_SUCCESS; return CLI_SUCCESS;

View file

@ -6,7 +6,7 @@
int int
cli_handle_q(__unused const char *arg, void *config_void) cli_handle_q(__unused const char *arg, void *config_void)
{ {
struct ssl_config *config = (struct ssl_config *)config_void; struct digest_config *config = (struct digest_config *)config_void;
SET_FLAG(config->flags, FLAG_Q); SET_FLAG(config->flags, FLAG_Q);
return CLI_SUCCESS; return CLI_SUCCESS;

View file

@ -6,7 +6,7 @@
int int
cli_handle_r(__unused const char *arg, void *config_void) cli_handle_r(__unused const char *arg, void *config_void)
{ {
struct ssl_config *config = (struct ssl_config *)config_void; struct digest_config *config = (struct digest_config *)config_void;
SET_FLAG(config->flags, FLAG_R); SET_FLAG(config->flags, FLAG_R);
return CLI_SUCCESS; return CLI_SUCCESS;

View file

@ -4,9 +4,9 @@
int int
cli_handle_s(const char *arg, void *config_void) cli_handle_s(const char *arg, void *config_void)
{ {
struct ssl_config *config = (struct ssl_config *)config_void; struct digest_config *config = (struct digest_config *)config_void;
if (config->nb_strings >= SSL_MAX_STRINGS) if (config->nb_strings >= DIGEST_MAX_STRINGS)
return CLI_ERROR; return CLI_ERROR;
config->strings[config->nb_strings++] = arg; config->strings[config->nb_strings++] = arg;
return CLI_SUCCESS; return CLI_SUCCESS;

View file

@ -7,12 +7,12 @@
#include "internal/ssl/ssl.h" #include "internal/ssl/ssl.h"
/* Forward declarations */ /* Forward declarations */
static void process_chunk(const struct ssl_config *config, union digest_ctx *ctx, static void process_chunk(const struct digest_config *config, union digest_ctx *ctx,
const uint8_t *buf, size_t n, int echo); const uint8_t *buf, size_t n, int echo);
/* ------------------- */ /* ------------------- */
int int
digest_stream(const struct ssl_config *config, int fd, digest_stream(const struct digest_config *config, int fd,
const char *label, int show_algo, int echo) const char *label, int show_algo, int echo)
{ {
union digest_ctx ctx; union digest_ctx ctx;
@ -31,7 +31,7 @@ digest_stream(const struct ssl_config *config, int fd,
} }
static void static void
process_chunk(const struct ssl_config *config, union digest_ctx *ctx, process_chunk(const struct digest_config *config, union digest_ctx *ctx,
const uint8_t *buf, size_t n, int echo) const uint8_t *buf, size_t n, int echo)
{ {
if (echo) if (echo)

View file

@ -20,7 +20,7 @@ typedef void (*t_formatter)(const char *hex, const char *name_upper,
const char *label); const char *label);
/* Forward declarations */ /* Forward declarations */
static enum fmt_mode resolve_fmt(const struct ssl_config *config, static enum fmt_mode resolve_fmt(const struct digest_config *config,
int show_algo); int show_algo);
static void build_hex(char *buf, const uint8_t *digest, size_t size); static void build_hex(char *buf, const uint8_t *digest, size_t size);
static void build_upper(char *buf, const char *src, size_t size); static void build_upper(char *buf, const char *src, size_t size);
@ -42,7 +42,7 @@ static const t_formatter s_formatters[] = {
}; };
void void
print_digest(const struct ssl_config *config, const uint8_t *digest, print_digest(const struct digest_config *config, const uint8_t *digest,
const char *label, int show_algo) const char *label, int show_algo)
{ {
char hex[MAX_DIGEST_SIZE * 2 + 1]; char hex[MAX_DIGEST_SIZE * 2 + 1];
@ -54,7 +54,7 @@ print_digest(const struct ssl_config *config, const uint8_t *digest,
} }
static enum fmt_mode static enum fmt_mode
resolve_fmt(const struct ssl_config *config, int show_algo) resolve_fmt(const struct digest_config *config, int show_algo)
{ {
if (HAS_FLAG(config->flags, FLAG_Q)) if (HAS_FLAG(config->flags, FLAG_Q))
return FMT_QUIET; return FMT_QUIET;

View file

@ -11,7 +11,7 @@
#include "version_gen.h" #include "version_gen.h"
int int
run_file(const struct ssl_config *config, const char *path) run_file(const struct digest_config *config, const char *path)
{ {
int fd; int fd;
int ret; int ret;

View file

@ -11,13 +11,13 @@
#include "internal/ssl/ssl.h" #include "internal/ssl/ssl.h"
/* Forward declarations */ /* Forward declarations */
static int run_stdin_p(const struct ssl_config *config); static int run_stdin_p(const struct digest_config *config);
static ssize_t read_and_hash(const struct ssl_config *config, union digest_ctx static ssize_t read_and_hash(const struct digest_config *config, union digest_ctx
*ctx, char *label, size_t cap); *ctx, char *label, size_t cap);
/* ------------------- */ /* ------------------- */
int int
run_stdin(const struct ssl_config *config) run_stdin(const struct digest_config *config)
{ {
if (HAS_FLAG(config->flags, FLAG_P) && !HAS_FLAG(config->flags, FLAG_Q)) if (HAS_FLAG(config->flags, FLAG_P) && !HAS_FLAG(config->flags, FLAG_Q))
return run_stdin_p(config); return run_stdin_p(config);
@ -26,7 +26,7 @@ run_stdin(const struct ssl_config *config)
} }
static ssize_t static ssize_t
read_and_hash(const struct ssl_config *config, union digest_ctx *ctx, read_and_hash(const struct digest_config *config, union digest_ctx *ctx,
char *label, size_t cap) char *label, size_t cap)
{ {
uint8_t buf[READ_BUFSIZE]; uint8_t buf[READ_BUFSIZE];
@ -47,7 +47,7 @@ read_and_hash(const struct ssl_config *config, union digest_ctx *ctx,
} }
static int static int
run_stdin_p(const struct ssl_config *config) run_stdin_p(const struct digest_config *config)
{ {
union digest_ctx ctx; union digest_ctx ctx;
uint8_t digest[MAX_DIGEST_SIZE]; uint8_t digest[MAX_DIGEST_SIZE];

View file

@ -8,7 +8,7 @@
#include "internal/ssl/ssl.h" #include "internal/ssl/ssl.h"
int int
run_string(const struct ssl_config *config, const char *s) run_string(const struct digest_config *config, const char *s)
{ {
union digest_ctx ctx; union digest_ctx ctx;
uint8_t digest[MAX_DIGEST_SIZE]; uint8_t digest[MAX_DIGEST_SIZE];