From e3bdb80e91fd1aea2e36945f09fb3bcc534c5404 Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Mon, 12 Feb 2024 00:49:52 +0100 Subject: [PATCH] initial mod subcommands work --- src/main.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index 23f5ed7..a46fcaf 100644 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -21,6 +22,14 @@ static const semver VERSION = { .patch = 0, }; +void print_mod_help(FILE *stream) { + fprintf(stream, "%s mod: module information\n", PNAME); + fprintf(stream, "\n"); + fprintf(stream, "SUBCOMMANDS\n"); + fprintf(stream, "\tlist\t\t\t\tlists all available modules\n"); + fprintf(stream, "\tinfo \t\t\tprints all info about module\n"); +} + int main(int argc, char *argv[]) { char *config_path = default_config_path(); @@ -37,8 +46,11 @@ int main(int argc, char *argv[]) { fclose(config_file); free(config_path); + bool hit_mod_cmd = false; + for (size_t i = 1; i < (size_t)argc; ++i) { if (strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--config") == 0) { + // no more args if (i == (size_t)argc - 1) { fprintf(stderr, "error: no config path passed\n"); exit(EXIT_FAILURE); @@ -52,9 +64,48 @@ int main(int argc, char *argv[]) { if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { printf("%s: modular fetch [%s]\n", PNAME, svtoa(VERSION)); printf("\n"); + printf("SUBCOMMANDS\n"); + printf("\tmod\t\t\t\tmodule information\n"); + printf("\n"); printf("OPTIONS\n"); printf("\t-h, --help\t\t\tdisplays this help text\n"); - printf("\t-c, --config /path/to/config\tchanges config path from the default ($XDG_CONFIG_HOME/%s.conf)\n", PNAME); + printf("\t-c, --config \tchanges config path from the default ($XDG_CONFIG_HOME/%s.conf)\n", PNAME); + exit(EXIT_SUCCESS); + } + + if (strncmp(argv[i], "mod", 3) == 0 || hit_mod_cmd) { + // no more args + if (i == (size_t)argc - 1) { + print_mod_help(stderr); + exit(EXIT_FAILURE); + } + + i += 1; + + if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { + print_mod_help(stdout); + exit(EXIT_SUCCESS); + } + + // some other -/-- option in between the mod and the specific mod command + if (argv[i][0] == '-') { + hit_mod_cmd = true; + continue; + } + + if (strcmp(argv[i], "list") == 0) { + for (size_t ii = 0; ii < config.module_count; ++ii) { + Module current_module = config.modules[ii]; + void *handle = dlopen(current_module.path, RTLD_NOW); +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wpedantic" + semver (*mod_version)(void) = dlsym(handle, "version"); + const char *(*mod_name)(void) = dlsym(handle, "name"); +#pragma GCC diagnostic pop + printf("%s-%s\n", mod_name(), svtoa(mod_version())); + } + } + exit(EXIT_SUCCESS); } }