linuxinstall/src/sandbox.c

116 lines
2.4 KiB
C
Raw Normal View History

#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-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>
#include <string.h>
2024-02-17 23:59:38 +01:00
#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__, {} \
}
#define OPTION_ALIASES(...) \
(const char*[]) { \
__VA_ARGS__, NULL \
}
const Command COMMANDS[] = {
{
.handler = command_help,
.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-18 18:09:53 +01:00
int main(int argc, char** argv) {
if (argc == 1) {
// Handle no command
fprintf(stderr, "No command specified\n");
2024-02-17 15:15:40 +01:00
return EXIT_FAILURE;
}
// 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
// Check if the alias matched
if (alias_matched) {
command_matched = true;
break;
}
}
// 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
}
}
fprintf(stderr, "Unknown command: ");
for (int i = 1; i < argc; i++)
fprintf(stderr, "%s ", argv[i]);
fprintf(stderr, "\n");
return EXIT_FAILURE;
}
int command_help(int argc, char* argv[]) {
// Print argv
printf("argc: %d\n", argc);
for (int i = 0; i < argc; i++)
printf("%s\n", argv[i]);
return EXIT_SUCCESS;
}