dep

Package manager for embedded C libraries
git clone git://git.finwo.net/app/dep
Log | Files | Refs | README | LICENSE

commit 6f56d5bccfda5392198e75c3049a03cc93575daf
parent 09e6a294ef7bfdd3445d22b4c940bb43548c4b16
Author: finwo <finwo@pm.me>
Date:   Fri, 13 Mar 2026 12:15:57 +0100

Allow command aliasing

Diffstat:
Msrc/command/command.h | 4++--
Msrc/command/init/main.c | 6+++---
Msrc/command/license/main.c | 3++-
Msrc/main.c | 26+++++++++++++++++---------
4 files changed, 24 insertions(+), 15 deletions(-)

diff --git a/src/command/command.h b/src/command/command.h @@ -1,6 +1,6 @@ struct cmd_struct { - void *next; - const char *cmd; + void *next; + const char **name; int (*fn)(int, const char **); }; diff --git a/src/command/init/main.c b/src/command/init/main.c @@ -52,6 +52,7 @@ void __attribute__((constructor)) cmd_init_setup(void) { } cmd->next = commands; cmd->fn = cmd_init; - cmd->cmd = "init"; + static const char *init_names[] = { "init", NULL }; + cmd->name = init_names; commands = cmd; -} -\ No newline at end of file +} diff --git a/src/command/license/main.c b/src/command/license/main.c @@ -18,6 +18,7 @@ void __attribute__((constructor)) cmd_license_setup() { struct cmd_struct *cmd = calloc(1, sizeof(struct cmd_struct)); cmd->next = commands; cmd->fn = cmd_license; - cmd->cmd = "license"; + static const char *license_names[] = { "license", NULL }; + cmd->name = license_names; commands = cmd; } diff --git a/src/main.c b/src/main.c @@ -64,14 +64,22 @@ int main(int argc, const char **argv) { return -1; } - /* Try to run command with args provided. */ - struct cmd_struct *cmd = commands; - while (cmd) { - if (!strcmp(cmd->cmd, argv[0])) break; - cmd = cmd->next; - } - if (cmd) { - return cmd->fn(argc, argv); - } + /* Try to run command with args provided. */ + struct cmd_struct *cmd = commands; + while (cmd) { + const char **name = cmd->name; + while (*name) { + if (!strcmp(*name, argv[0])) { + goto found; + } + name++; + } + cmd = cmd->next; + } + cmd = NULL; +found: + if (cmd) { + return cmd->fn(argc, argv); + } return 0; }