Added a janky system for storing commands

This commit is contained in:
Alexei KADIR 2024-02-19 16:44:07 +01:00
parent 5cf1b33178
commit b77418f0ec
4 changed files with 64 additions and 5 deletions

View File

@ -7,6 +7,36 @@
#include <pwd.h> #include <pwd.h>
#include <unistd.h> #include <unistd.h>
#define ALIAS(...) \
(const char*[]) { __VA_ARGS__, NULL }
#define ALIASES(...) \
(const char**[]) { __VA_ARGS__, NULL }
#define ARGUMENTS(...) \
(const Argument[]) { \
__VA_ARGS__, {} \
}
#define OPTIONS(...) \
(const Option[]) { \
__VA_ARGS__, {} \
}
Command COMMANDS[] = {
// Help
{
.handler = command_help,
.description = "Display help information.",
.details = "Display help information about the available commands and their options.",
.aliases = ALIASES(ALIAS("help")),
.arguments = ARGUMENTS({.name = "command", .required = false, .description = "The command to display help information for."},
{.name = "test", .required = false, .description = "Test."}),
.options = OPTIONS({.aliases = ALIASES(ALIAS("--help"), ALIAS("-h")), .arguments = NULL, .description = "Display help information."},
{.aliases = ALIASES(ALIAS("--test")), .arguments = NULL, .description = "Test."}),
},
};
int main(int argc, char** argv) { int main(int argc, char** argv) {
// Ensure the sandbox user exists // Ensure the sandbox user exists
struct passwd* pw = getpwnam(SANDBOX_USER); struct passwd* pw = getpwnam(SANDBOX_USER);
@ -35,4 +65,8 @@ int main(int argc, char** argv) {
} }
// TODO: Parse commands from the command line // TODO: Parse commands from the command line
} }
int command_help(int argc, char* argv[]) {
return EXIT_SUCCESS;
}

View File

@ -5,4 +5,29 @@
#define SANDBOX_VERSION "0.1.4" #define SANDBOX_VERSION "0.1.4"
#define SANDBOX_USER "sandbox" #define SANDBOX_USER "sandbox"
typedef struct {
const char* name;
const bool required;
const char* description;
} Argument;
typedef struct {
const char*** aliases;
const Argument* arguments;
const char* description;
} Option;
typedef struct {
int (*const handler)(int argc, char* argv[]);
const char*** aliases;
const Argument* arguments;
const Option* options;
const char* description;
const char* details;
} Command;
extern Command COMMANDS[];
int main(int argc, char** argv); int main(int argc, char** argv);
int command_help(int argc, char* argv[]);

View File

@ -12,7 +12,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <openssl/md5.h> #include <openssl/md5.h>
char _error_buffer[ERROR_BUFFER_SIZE]; char ERROR_BUFFER[ERROR_BUFFER_SIZE];
result_t success(void) { result_t success(void) {
return EXIT_SUCCESS; return EXIT_SUCCESS;
@ -23,7 +23,7 @@ result_t failure(const char* format, ...) {
va_start(args, format); va_start(args, format);
// Print the error message into the error buffer // Print the error message into the error buffer
vsnprintf(_error_buffer, ERROR_BUFFER_SIZE, format, args); vsnprintf(ERROR_BUFFER, ERROR_BUFFER_SIZE, format, args);
va_end(args); va_end(args);
@ -31,7 +31,7 @@ result_t failure(const char* format, ...) {
} }
const char* error(void) { const char* error(void) {
return _error_buffer; return ERROR_BUFFER;
} }
result_t format(char** _str, const char* format, ...) { result_t format(char** _str, const char* format, ...) {

View File

@ -6,7 +6,7 @@
#define ERROR_BUFFER_SIZE 4096 #define ERROR_BUFFER_SIZE 4096
extern char _error_buffer[]; extern char ERROR_BUFFER[];
typedef int result_t; typedef int result_t;