2024-02-15 00:17:20 +01:00
|
|
|
#include "sandbox.h"
|
|
|
|
|
2024-02-18 18:09:53 +01:00
|
|
|
#include "utils.h"
|
2024-02-19 18:18:18 +01:00
|
|
|
#include "backing.h"
|
|
|
|
#include "container.h"
|
2024-02-15 00:17:20 +01:00
|
|
|
|
2024-02-19 16:01:53 +01:00
|
|
|
#include <stdio.h>
|
2024-02-19 02:06:12 +01:00
|
|
|
#include <stdbool.h>
|
2024-02-17 15:15:40 +01:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <unistd.h>
|
2024-02-23 18:32:03 +01:00
|
|
|
#include <string.h>
|
2024-02-17 23:59:38 +01:00
|
|
|
|
2024-02-23 18:32:03 +01:00
|
|
|
#define ALIAS(...) \
|
|
|
|
(const char*[]) { \
|
|
|
|
__VA_ARGS__, NULL \
|
|
|
|
}
|
2024-02-19 16:44:07 +01:00
|
|
|
|
2024-02-23 18:32:03 +01:00
|
|
|
#define ALIASES(...) \
|
|
|
|
(const char**[]) { \
|
|
|
|
__VA_ARGS__, NULL \
|
|
|
|
}
|
2024-02-19 16:44:07 +01:00
|
|
|
|
|
|
|
#define ARGUMENTS(...) \
|
|
|
|
(const Argument[]) { \
|
|
|
|
__VA_ARGS__, {} \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define OPTIONS(...) \
|
|
|
|
(const Option[]) { \
|
|
|
|
__VA_ARGS__, {} \
|
|
|
|
}
|
|
|
|
|
2024-02-23 18:32:03 +01:00
|
|
|
#define OPTION_ALIASES(...) \
|
|
|
|
(const char*[]) { \
|
|
|
|
__VA_ARGS__, NULL \
|
|
|
|
}
|
|
|
|
|
|
|
|
const Command COMMANDS[] = {
|
2024-02-19 16:44:07 +01:00
|
|
|
{
|
|
|
|
.handler = command_help,
|
2024-02-23 18:32:03 +01:00
|
|
|
.description = "Display help information",
|
|
|
|
.details = "Display help information for a specific command",
|
|
|
|
|
|
|
|
.aliases = ALIASES(ALIAS("h"), ALIAS("help", "me")),
|
|
|
|
.arguments = ARGUMENTS({
|
|
|
|
.name = "command",
|
|
|
|
.description = "The command to display help information for",
|
|
|
|
.optional = true,
|
|
|
|
}),
|
|
|
|
.options = OPTIONS({
|
|
|
|
.aliases = OPTION_ALIASES("verbose", "v"),
|
|
|
|
.description = "Display verbose help information",
|
|
|
|
.arguments = ARGUMENTS({
|
|
|
|
.name = "level",
|
|
|
|
.description = "The level of verbosity to display",
|
|
|
|
}),
|
|
|
|
}),
|
2024-02-19 16:44:07 +01:00
|
|
|
},
|
2024-02-23 18:32:03 +01:00
|
|
|
{}};
|
2024-02-19 16:44:07 +01:00
|
|
|
|
2024-02-18 18:09:53 +01:00
|
|
|
int main(int argc, char** argv) {
|
2024-02-23 18:32:03 +01:00
|
|
|
if (argc == 1) {
|
|
|
|
// Handle no command
|
|
|
|
fprintf(stderr, "No command specified\n");
|
2024-02-17 15:15:40 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2024-02-23 18:32:03 +01:00
|
|
|
// For each command
|
|
|
|
for (int i = 0; COMMANDS[i].handler != NULL; i++) {
|
|
|
|
const Command* command = &COMMANDS[i];
|
|
|
|
|
|
|
|
// For each alias
|
|
|
|
bool command_matched = false;
|
|
|
|
int k = 0;
|
|
|
|
for (int j = 0; command->aliases[j] != NULL; j++) {
|
|
|
|
const char** alias = command->aliases[j];
|
|
|
|
|
|
|
|
// For each string in the alias
|
|
|
|
bool alias_matched = true;
|
|
|
|
for (k = 0; alias[k] != NULL; k++)
|
|
|
|
if (argc <= 1 + k || strcmp(argv[1 + k], alias[k]) != 0) {
|
|
|
|
alias_matched = false;
|
|
|
|
break;
|
|
|
|
}
|
2024-02-17 15:15:40 +01:00
|
|
|
|
2024-02-23 18:32:03 +01:00
|
|
|
// Check if the alias matched
|
|
|
|
if (alias_matched) {
|
|
|
|
command_matched = true;
|
|
|
|
break;
|
|
|
|
}
|
2024-02-18 15:23:01 +01:00
|
|
|
}
|
|
|
|
|
2024-02-23 18:32:03 +01:00
|
|
|
// If the command matched
|
|
|
|
if (command_matched) {
|
|
|
|
// Call the command handler
|
|
|
|
return command->handler(argc - 1 - k, argv + 1 + k);
|
2024-02-18 14:43:38 +01:00
|
|
|
}
|
2024-02-18 15:23:01 +01:00
|
|
|
}
|
|
|
|
|
2024-02-23 18:32:03 +01:00
|
|
|
fprintf(stderr, "Unknown command: ");
|
|
|
|
for (int i = 1; i < argc; i++)
|
|
|
|
fprintf(stderr, "%s ", argv[i]);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
|
|
|
return EXIT_FAILURE;
|
2024-02-19 16:44:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int command_help(int argc, char* argv[]) {
|
2024-02-23 18:32:03 +01:00
|
|
|
// Print argv
|
|
|
|
printf("argc: %d\n", argc);
|
|
|
|
for (int i = 0; i < argc; i++)
|
|
|
|
printf("%s\n", argv[i]);
|
|
|
|
|
2024-02-19 16:44:07 +01:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|