modfetch/modfetch.c

107 lines
3.2 KiB
C
Raw Normal View History

2024-02-11 22:11:57 +01:00
#include "config.h"
2024-02-11 19:37:41 +01:00
#include "mod.h"
2024-02-10 14:11:26 +01:00
#include "semver.h"
2024-02-11 22:11:57 +01:00
#include "util.h"
2024-02-09 12:05:06 +01:00
#include <ctype.h>
#include <dlfcn.h>
2024-02-09 15:31:30 +01:00
#include <errno.h>
2024-02-09 12:05:06 +01:00
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <wordexp.h>
static const char *PNAME = "modfetch";
2024-02-10 14:11:26 +01:00
static const semver VERSION = {
.major = 0,
.minor = 1,
.patch = 0,
};
2024-02-09 12:05:06 +01:00
int main(int argc, char *argv[]) {
char *config_path;
char *config_dir = getenv("XDG_CONFIG_HOME");
if (config_dir == NULL) {
char *home = getenv("$HOME");
if (asprintf(&config_dir, "%s/.config", home) < 0) {
fprintf(stderr, "error: failed formatting config dir (this shouldn't happen)");
exit(EXIT_FAILURE);
}
}
2024-02-09 12:05:06 +01:00
if (asprintf(&config_path, "%s/modfetch.conf", config_dir) < 0) {
2024-02-09 12:05:06 +01:00
fprintf(stderr, "error: failed formatting config path (this shouldn't happen)");
exit(EXIT_FAILURE);
}
for (size_t i = 1; i < (size_t)argc; ++i) {
if (strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--config") == 0) {
if (i == (size_t)argc - 1) {
fprintf(stderr, "error: no config path passed\n");
exit(EXIT_FAILURE);
}
i += 1;
2024-02-09 12:05:06 +01:00
config_path = argv[i];
continue;
}
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
2024-02-10 14:11:26 +01:00
fprintf(stderr, "%s: modular fetch [%s]\n", PNAME, svtoa(VERSION));
2024-02-09 12:05:06 +01:00
fprintf(stderr, "\n");
fprintf(stderr, "OPTIONS\n");
fprintf(stderr, "\t-h, --help\t\t\tdisplays this help text\n");
fprintf(stderr, "\t-c, --config /path/to/config\tchanges config path from the default ($XDG_CONFIG_HOME/%s.conf)\n", PNAME);
exit(EXIT_SUCCESS);
}
}
FILE *config_file = fopen(config_path, "r");
if (config_file == NULL) {
fprintf(stderr, "error: failed to open config at: %s\n", config_path);
exit(EXIT_FAILURE);
}
Config config = parse_config(config_file);
fclose(config_file);
2024-02-11 19:50:44 +01:00
free(config_path);
2024-02-09 12:05:06 +01:00
for (size_t i = 0; i < config.module_count; ++i) {
Module current_module = config.modules[i];
void *handle = dlopen(current_module.path, RTLD_NOW);
if (handle == NULL) {
fprintf(stderr, "error: failed opening module %s\n", current_module.path);
fprintf(stderr, "%s\n", dlerror());
exit(EXIT_FAILURE);
}
dlerror();
// stupid fucking c standard issue
// I'm not fixing this
// https://stackoverflow.com/questions/14134245/iso-c-void-and-function-pointers
2024-02-09 12:05:06 +01:00
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
2024-02-11 20:05:31 +01:00
semver (*mod_version)(void) = dlsym(handle, "version");
const char *(*mod_name)(void) = dlsym(handle, "name");
const char *(*mod_get)(void) = dlsym(handle, "get");
void (*mod_init)(semver, char**) = dlsym(handle, "init");
2024-02-09 12:05:06 +01:00
#pragma GCC diagnostic pop
2024-02-11 20:05:31 +01:00
mod_init(API_VERSION, current_module.config);
2024-02-09 12:05:06 +01:00
2024-02-11 20:05:31 +01:00
(void)mod_name;
(void)mod_version;
// printf("%s: %s\n", mod_name(), svtoa(mod_version()));
printf("%s\n", mod_get());
2024-02-09 12:05:06 +01:00
}
return 0;
}