From 68e443b62a845d541e81909c5684fb36b6264974 Mon Sep 17 00:00:00 2001 From: lohhiiccc <96543753+lohhiiccc@users.noreply.github.com> Date: Sat, 27 Jun 2026 17:05:48 +0200 Subject: [PATCH] feat(cmd): add command_descriptor table and dispatcher --- includes/internal/cmd/command.h | 26 ++++++++++++++++ src/cmd/command_table.c | 54 +++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 includes/internal/cmd/command.h create mode 100644 src/cmd/command_table.c diff --git a/includes/internal/cmd/command.h b/includes/internal/cmd/command.h new file mode 100644 index 0000000..68d9923 --- /dev/null +++ b/includes/internal/cmd/command.h @@ -0,0 +1,26 @@ +#ifndef FT_SSL_CMD_COMMAND_H +#define FT_SSL_CMD_COMMAND_H + +#include +#include + +#include + +enum command_category { + CMD_DIGEST, + CMD_CIPHER +}; + +struct command_descriptor { + const char *name; + enum command_category category; + const struct option_descriptor *opts; + size_t nb_opts; + const void *impl; + int (*run)(int argc, char **argv, const struct command_descriptor *self); +}; + +const struct command_descriptor *find_command(const char *name); +void print_commands(FILE *out); + +#endif diff --git a/src/cmd/command_table.c b/src/cmd/command_table.c new file mode 100644 index 0000000..85d6757 --- /dev/null +++ b/src/cmd/command_table.c @@ -0,0 +1,54 @@ +#include +#include + +#include + +#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 +#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 +#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); +}