2024-02-10 14:11:26 +01:00
|
|
|
#include "semver.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
|
|
|
|
|
|
|
static const uint8_t MAX_MODULE_NAME_LENGTH = 128;
|
|
|
|
static const uint8_t MAX_MODULES = 128;
|
2024-02-09 15:10:13 +01:00
|
|
|
|
|
|
|
static const uint8_t INITIAL_CONFIG_SIZE = 8;
|
|
|
|
|
|
|
|
static const uint16_t MAX_PATH_LENGTH = 4096;
|
2024-02-09 12:05:06 +01:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
// path to the binary of this module
|
|
|
|
char *path;
|
|
|
|
// name=value strings for each value in config
|
|
|
|
char **config;
|
|
|
|
} Module;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
size_t module_count;
|
|
|
|
Module *modules;
|
|
|
|
} Config;
|
|
|
|
|
|
|
|
void parsing_error(size_t line) {
|
|
|
|
fprintf(stderr, "error: failed parsing config at line %zu", line);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *remove_whitespaces(const char *str) {
|
2024-02-09 15:10:13 +01:00
|
|
|
char tmp[strlen(str)];
|
2024-02-09 12:05:06 +01:00
|
|
|
size_t i = 0;
|
|
|
|
size_t j = 0;
|
|
|
|
while (str[i] != '\0') {
|
|
|
|
if (isspace(str[i])) {
|
2024-02-09 15:11:36 +01:00
|
|
|
i += 1;
|
2024-02-09 12:05:06 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
tmp[j] = str[i];
|
2024-02-09 15:11:36 +01:00
|
|
|
i += 1;
|
|
|
|
j += 1;
|
2024-02-09 12:05:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char *ret = malloc(j * sizeof(char));
|
|
|
|
strncpy(ret, tmp, j);
|
2024-02-09 22:07:49 +01:00
|
|
|
ret[j] = '\0';
|
2024-02-09 12:05:06 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *resolve_env_vars(const char *str) {
|
|
|
|
size_t len = strlen(str);
|
2024-02-09 15:10:13 +01:00
|
|
|
char *ret = malloc(MAX_PATH_LENGTH * sizeof(char));
|
2024-02-09 12:05:06 +01:00
|
|
|
size_t ret_offset = 0;
|
|
|
|
bool in_env_var = false;
|
2024-02-09 15:10:13 +01:00
|
|
|
char env_var[MAX_PATH_LENGTH];
|
|
|
|
memset(env_var, 0, sizeof(env_var));
|
2024-02-09 12:05:06 +01:00
|
|
|
size_t env_var_offset = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < len; ++i) {
|
|
|
|
if (str[i] == '~') {
|
|
|
|
char *home = getenv("HOME");
|
|
|
|
strcat(ret, home);
|
|
|
|
ret_offset += strlen(home);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (str[i] == '$') {
|
|
|
|
in_env_var = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-02-09 15:10:13 +01:00
|
|
|
// end of current env var
|
2024-02-09 12:05:06 +01:00
|
|
|
if (in_env_var && !(isalpha(str[i]) || isdigit(str[i]) || str[i] == '_')) {
|
|
|
|
in_env_var = false;
|
|
|
|
|
|
|
|
char *env = getenv(env_var);
|
|
|
|
strcat(ret, env);
|
|
|
|
ret_offset += strlen(env);
|
2024-02-09 15:10:13 +01:00
|
|
|
memset(env_var, 0, sizeof(env_var));
|
2024-02-09 12:05:06 +01:00
|
|
|
env_var_offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_env_var) {
|
|
|
|
env_var[env_var_offset] = str[i];
|
2024-02-09 15:11:36 +01:00
|
|
|
env_var_offset += 1;
|
2024-02-09 12:05:06 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret[ret_offset] = str[i];
|
2024-02-09 15:11:36 +01:00
|
|
|
ret_offset += 1;
|
2024-02-09 12:05:06 +01:00
|
|
|
}
|
2024-02-09 15:34:15 +01:00
|
|
|
ret[ret_offset] = '\0';
|
2024-02-09 12:05:06 +01:00
|
|
|
|
2024-02-09 15:10:13 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2024-02-09 12:23:17 +01:00
|
|
|
|
2024-02-09 15:10:13 +01:00
|
|
|
char *process_str(const char *str) {
|
|
|
|
char *whitespaceless = remove_whitespaces(str);
|
|
|
|
char *ret = resolve_env_vars(whitespaceless);
|
|
|
|
free(whitespaceless);
|
2024-02-09 12:05:06 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Config parse_config(FILE *config_file) {
|
|
|
|
size_t len = 0;
|
|
|
|
ssize_t read;
|
|
|
|
char *line = NULL;
|
|
|
|
size_t line_index = 0;
|
|
|
|
bool in_config = false;
|
|
|
|
|
2024-02-09 15:10:13 +01:00
|
|
|
char current_path[MAX_MODULE_NAME_LENGTH];
|
2024-02-09 12:05:06 +01:00
|
|
|
Module current_module = {
|
2024-02-09 15:10:13 +01:00
|
|
|
.path = NULL,
|
|
|
|
.config = NULL,
|
2024-02-09 12:05:06 +01:00
|
|
|
};
|
|
|
|
size_t config_index = 0;
|
2024-02-09 15:10:13 +01:00
|
|
|
size_t config_multiplier = 1;
|
2024-02-09 12:05:06 +01:00
|
|
|
|
|
|
|
Config config = {
|
|
|
|
.modules = malloc(MAX_MODULES * sizeof(Module)),
|
|
|
|
.module_count = 0,
|
|
|
|
};
|
|
|
|
|
2024-02-09 15:31:30 +01:00
|
|
|
// TODO handle errors of all the function inside the while
|
|
|
|
errno = 0;
|
|
|
|
|
2024-02-09 15:10:13 +01:00
|
|
|
// TODO rewrite this to read the config char by char
|
2024-02-09 12:05:06 +01:00
|
|
|
while ((read = getline(&line, &len, config_file)) != -1) {
|
2024-02-09 15:10:13 +01:00
|
|
|
if (read <= 1)
|
2024-02-09 13:54:29 +01:00
|
|
|
continue;
|
2024-02-09 15:10:13 +01:00
|
|
|
|
|
|
|
// if config is passed to this module
|
2024-02-09 12:05:06 +01:00
|
|
|
if (line[read - 2u] == '{') {
|
|
|
|
if (in_config) {
|
|
|
|
fclose(config_file);
|
|
|
|
parsing_error(line_index);
|
|
|
|
}
|
|
|
|
in_config = true;
|
2024-02-09 15:10:13 +01:00
|
|
|
// get rid of the '{'
|
2024-02-09 12:05:06 +01:00
|
|
|
strncpy(current_path, line, (size_t)read - 2);
|
2024-02-09 15:46:33 +01:00
|
|
|
current_path[read - 2] = '\0';
|
2024-02-09 15:10:13 +01:00
|
|
|
|
|
|
|
current_module.path = process_str(current_path);
|
|
|
|
current_module.config = malloc(INITIAL_CONFIG_SIZE * sizeof(char*));
|
2024-02-09 12:05:06 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// end of config for current module
|
|
|
|
if (line[read - 2u] == '}') {
|
|
|
|
if (!in_config) {
|
|
|
|
fclose(config_file);
|
|
|
|
parsing_error(line_index);
|
|
|
|
}
|
|
|
|
in_config = false;
|
2024-02-09 15:10:13 +01:00
|
|
|
|
|
|
|
if (config_index >= INITIAL_CONFIG_SIZE * config_multiplier) {
|
|
|
|
config_multiplier *= 2;
|
|
|
|
current_module.config = realloc(current_module.config, INITIAL_CONFIG_SIZE * config_multiplier);
|
|
|
|
}
|
2024-02-09 12:05:06 +01:00
|
|
|
current_module.config[config_index] = NULL;
|
2024-02-09 15:10:13 +01:00
|
|
|
config.modules[config.module_count] = current_module;
|
2024-02-09 15:11:36 +01:00
|
|
|
config.module_count += 1;
|
2024-02-09 15:10:13 +01:00
|
|
|
config_index = 0;
|
2024-02-09 12:05:06 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_config) {
|
2024-02-09 15:10:13 +01:00
|
|
|
if (config_index >= INITIAL_CONFIG_SIZE * config_multiplier) {
|
|
|
|
config_multiplier *= 2;
|
|
|
|
current_module.config = realloc(current_module.config, INITIAL_CONFIG_SIZE * config_multiplier);
|
|
|
|
}
|
|
|
|
current_module.config[config_index] = process_str(line);
|
2024-02-09 15:11:36 +01:00
|
|
|
config_index += 1;
|
2024-02-09 12:05:06 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-02-09 15:10:13 +01:00
|
|
|
// no config passed to this module
|
|
|
|
current_module.path = process_str(line);
|
|
|
|
|
2024-02-09 12:05:06 +01:00
|
|
|
current_module.config = malloc(sizeof(void*));
|
|
|
|
current_module.config[0] = NULL;
|
|
|
|
|
2024-02-09 15:10:13 +01:00
|
|
|
config.modules[config.module_count] = current_module;
|
2024-02-09 15:11:36 +01:00
|
|
|
config.module_count += 1;
|
2024-02-09 15:10:13 +01:00
|
|
|
|
|
|
|
free(line);
|
|
|
|
}
|
2024-02-09 15:31:30 +01:00
|
|
|
if (read == -1 && errno != 0)
|
|
|
|
free(line);
|
2024-02-09 12:05:06 +01:00
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
|
|
|
char *config_path;
|
|
|
|
|
|
|
|
if (asprintf(&config_path, "%s/modfetch.conf", getenv("XDG_CONFIG_HOME")) < 0) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-02-09 15:11:36 +01:00
|
|
|
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);
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2024-02-09 15:15:44 +01:00
|
|
|
// 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-10 14:11:26 +01:00
|
|
|
semver (*version)(void) = dlsym(handle, "version");
|
2024-02-10 14:51:50 +01:00
|
|
|
const char *(*name)(void) = dlsym(handle, "name");
|
2024-02-09 12:05:06 +01:00
|
|
|
const char *(*get)(void) = dlsym(handle, "get");
|
|
|
|
void (*init)(char**) = dlsym(handle, "init");
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
|
|
|
init(current_module.config);
|
|
|
|
|
2024-02-10 14:51:50 +01:00
|
|
|
(void)name;
|
2024-02-10 14:11:26 +01:00
|
|
|
(void)version;
|
2024-02-10 14:51:50 +01:00
|
|
|
// printf("%s: %s\n", name(), svtoa(version()));
|
2024-02-09 12:05:06 +01:00
|
|
|
printf("%s\n", get());
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|