From ba07fd79680717d0319e37cf37390ca3a311700c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexe=C3=AF=20KADIR?= Date: Fri, 16 Feb 2024 16:53:02 +0100 Subject: [PATCH] Added support for commands --- src/sandbox.c | 19 ++++++++++++++++++- src/sandbox.h | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/sandbox.c b/src/sandbox.c index e6b1cdd..4497949 100644 --- a/src/sandbox.c +++ b/src/sandbox.c @@ -8,4 +8,21 @@ int main(int argc, char* argv[]) { return 0; -} \ No newline at end of file +} + +Command* find_command(const char* name) { + Command* match = NULL; + + // Find all matching commands (starting with the given name) + size_t length = strlen(name); + for (size_t i = 0; i < sizeof(COMMANDS) / sizeof(COMMANDS[0]); i++) + if (strncmp(name, COMMANDS[i].name, length) == 0) { + // Check for multiple matches + if (match != NULL) + return NULL; + + match = &COMMANDS[i]; + } + + return match; +} diff --git a/src/sandbox.h b/src/sandbox.h index 6358d2f..9520cbe 100644 --- a/src/sandbox.h +++ b/src/sandbox.h @@ -1,3 +1,36 @@ #pragma once +#include + +typedef struct { + const char* name; + const char* args; + const char* description; +} Command; + +Command COMMANDS[] = { + {"help", "[command]", "Shows help for all or a specific command."}, + {"version", NULL, "Shows the version of the program."}, + {NULL, NULL, NULL}, + {"add-entry", " [--root|-r ] [--backing|-b ]", "Adds a new entry to the pool"}, + {"remove-entry", "", "Removes an entry from the pool"}, + {"list-entries", NULL, "Lists all entries in the pool"}, + {"clear-entries", NULL, "Clears all entries from the pool"}, + {NULL, NULL, NULL}, + {"add-backing", "", "Adds a backing disk to the pool from the given entry"}, + {"remove-backing", "", "Removes a backing disk from the pool"}, + {"list-backings", "[--tree|-t]", "Lists all backing disks in the pool"}, + {"clear-backings", NULL, "Clears all backing disks from the pool"}, + {NULL, NULL, NULL}, + {"start", " [--no-pci] [--vnc ] [--iso ]", "Starts the sandbox with the given entry"}, + {"power", NULL, "Sends a power signal to the sandbox"}, + {"stop", "[--force|-f] [--timeout|-t ]", "Stops the sandbox"}, + {"status", NULL, "Shows the status of the sandbox"}, +}; + int main(int argc, char* argv[]); + +/// @brief Finds the best matching command for the given command name. +/// @param name The name of the command to find. +/// @return The best matching command or NULL if no or multiple matches were found. No need to free the result. +Command* find_command(const char* name);