feat(cmd): add command_descriptor table and dispatcher

This commit is contained in:
lohhiiccc 2026-06-27 17:05:48 +02:00
parent 66b0a2cb66
commit 68e443b62a
2 changed files with 80 additions and 0 deletions

View file

@ -0,0 +1,26 @@
#ifndef FT_SSL_CMD_COMMAND_H
#define FT_SSL_CMD_COMMAND_H
#include <stddef.h>
#include <stdio.h>
#include <cli.h>
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

54
src/cmd/command_table.c Normal file
View file

@ -0,0 +1,54 @@
#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);
}