2019-07-16 11:52:22 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdbool.h>
|
2019-07-17 10:12:14 +02:00
|
|
|
#include <ctype.h>
|
2019-07-16 11:52:22 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <pwd.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
|
|
#define LOG_MODULE "config"
|
|
|
|
|
#define LOG_ENABLE_DBG 0
|
|
|
|
|
#include "log.h"
|
|
|
|
|
|
2019-07-17 09:29:56 +02:00
|
|
|
static char *
|
|
|
|
|
get_shell(void)
|
|
|
|
|
{
|
|
|
|
|
struct passwd *passwd = getpwuid(getuid());
|
|
|
|
|
if (passwd == NULL) {
|
|
|
|
|
LOG_ERRNO("failed to lookup user");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *shell = passwd->pw_shell;
|
|
|
|
|
LOG_DBG("user's shell: %s", shell);
|
|
|
|
|
|
|
|
|
|
return strdup(shell);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-16 11:52:22 +02:00
|
|
|
static char *
|
|
|
|
|
get_config_path_user_config(void)
|
|
|
|
|
{
|
|
|
|
|
struct passwd *passwd = getpwuid(getuid());
|
|
|
|
|
if (passwd == NULL) {
|
|
|
|
|
LOG_ERRNO("failed to lookup user");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *home_dir = passwd->pw_dir;
|
|
|
|
|
LOG_DBG("user's home directory: %s", home_dir);
|
|
|
|
|
|
|
|
|
|
int len = snprintf(NULL, 0, "%s/.config/footrc", home_dir);
|
|
|
|
|
char *path = malloc(len + 1);
|
|
|
|
|
snprintf(path, len + 1, "%s/.config/footrc", home_dir);
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
|
get_config_path_xdg(void)
|
|
|
|
|
{
|
|
|
|
|
const char *xdg_config_home = getenv("XDG_CONFIG_HOME");
|
|
|
|
|
if (xdg_config_home == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
int len = snprintf(NULL, 0, "%s/footrc", xdg_config_home);
|
|
|
|
|
char *path = malloc(len + 1);
|
|
|
|
|
snprintf(path, len + 1, "%s/footrc", xdg_config_home);
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
|
get_config_path(void)
|
|
|
|
|
{
|
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
|
|
char *config = get_config_path_xdg();
|
|
|
|
|
if (config != NULL && stat(config, &st) == 0 && S_ISREG(st.st_mode))
|
|
|
|
|
return config;
|
|
|
|
|
free(config);
|
|
|
|
|
|
|
|
|
|
/* 'Default' XDG_CONFIG_HOME */
|
|
|
|
|
config = get_config_path_user_config();
|
|
|
|
|
if (config != NULL && stat(config, &st) == 0 && S_ISREG(st.st_mode))
|
|
|
|
|
return config;
|
|
|
|
|
free(config);
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
2019-07-17 10:12:14 +02:00
|
|
|
parse_section_main(const char *key, const char *value, struct config *conf,
|
|
|
|
|
const char *path, unsigned lineno)
|
2019-07-16 11:52:22 +02:00
|
|
|
{
|
2019-07-18 14:29:40 +02:00
|
|
|
if (strcmp(key, "term") == 0) {
|
|
|
|
|
free(conf->term);
|
|
|
|
|
conf->term = strdup(value);
|
2019-07-16 11:52:22 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-17 09:40:58 +02:00
|
|
|
else if (strcmp(key, "shell") == 0) {
|
|
|
|
|
free(conf->shell);
|
|
|
|
|
conf->shell = strdup(value);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 14:29:40 +02:00
|
|
|
else if (strcmp(key, "font") == 0) {
|
|
|
|
|
free(conf->font);
|
|
|
|
|
conf->font = strdup(value);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-16 11:52:22 +02:00
|
|
|
else {
|
2019-07-17 10:12:14 +02:00
|
|
|
LOG_WARN("%s:%u: invalid key: %s", path, lineno, key);
|
2019-07-16 11:52:22 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
parse_config_file(FILE *f, struct config *conf, const char *path)
|
|
|
|
|
{
|
|
|
|
|
enum section {
|
|
|
|
|
SECTION_MAIN,
|
|
|
|
|
} section = SECTION_MAIN;
|
|
|
|
|
|
|
|
|
|
/* Function pointer, called for each key/value line */
|
|
|
|
|
typedef bool (*parser_fun_t)(
|
2019-07-17 10:12:14 +02:00
|
|
|
const char *key, const char *value, struct config *conf,
|
|
|
|
|
const char *path, unsigned lineno);
|
2019-07-16 11:52:22 +02:00
|
|
|
|
|
|
|
|
/* Maps sections to line parser functions */
|
|
|
|
|
static const parser_fun_t section_parser_map[] = {
|
|
|
|
|
[SECTION_MAIN] = &parse_section_main,
|
|
|
|
|
};
|
|
|
|
|
|
2019-07-17 10:32:22 +02:00
|
|
|
#if defined(_DEBUG) && defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG
|
2019-07-17 10:12:14 +02:00
|
|
|
static const char *const section_names[] = {
|
|
|
|
|
[SECTION_MAIN] = "main",
|
|
|
|
|
};
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-07-16 11:52:22 +02:00
|
|
|
unsigned lineno = 0;
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
errno = 0;
|
|
|
|
|
lineno++;
|
|
|
|
|
|
|
|
|
|
char *line = NULL;
|
|
|
|
|
size_t count = 0;
|
|
|
|
|
ssize_t ret = getline(&line, &count, f);
|
|
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
free(line);
|
|
|
|
|
if (errno != 0) {
|
|
|
|
|
LOG_ERRNO("failed to read from configuration");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* No sections yet */
|
|
|
|
|
if (line[0] == '[' && line[strlen(line) - 1] == ']') {
|
|
|
|
|
assert(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-17 10:12:14 +02:00
|
|
|
char *key = strtok(line, "=");
|
|
|
|
|
char *value = strtok(NULL, "\n");
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
while (isspace(*key))
|
|
|
|
|
key++;
|
|
|
|
|
char *end = key + strlen(key) - 1;
|
|
|
|
|
while (isspace(*end))
|
|
|
|
|
end--;
|
|
|
|
|
*(end + 1) = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (value == NULL) {
|
|
|
|
|
if (key != NULL && strlen(key) > 0 && key[0] != '#') {
|
|
|
|
|
LOG_ERR("%s:%d: syntax error: %s", path, lineno, line);
|
|
|
|
|
free(line);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(line);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
while (isspace(*value))
|
|
|
|
|
value++;
|
|
|
|
|
char *end = value + strlen(value) - 1;
|
|
|
|
|
while (isspace(*end))
|
|
|
|
|
end--;
|
|
|
|
|
*(end + 1) = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (key[0] == '#') {
|
|
|
|
|
free(line);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOG_DBG("section=%s, key='%s', value='%s'",
|
|
|
|
|
section_names[section], key, value);
|
|
|
|
|
|
2019-07-16 11:52:22 +02:00
|
|
|
parser_fun_t section_parser = section_parser_map[section];
|
|
|
|
|
assert(section_parser != NULL);
|
|
|
|
|
|
2019-07-17 10:12:14 +02:00
|
|
|
if (!section_parser(key, value, conf, path, lineno)) {
|
2019-07-16 11:52:22 +02:00
|
|
|
free(line);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(line);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-17 10:12:14 +02:00
|
|
|
bool
|
|
|
|
|
config_load(struct config *conf)
|
2019-07-16 11:52:22 +02:00
|
|
|
{
|
2019-07-17 10:12:14 +02:00
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
|
|
*conf = (struct config) {
|
2019-07-18 14:29:40 +02:00
|
|
|
.term = strdup("foot"),
|
2019-07-17 09:29:56 +02:00
|
|
|
.shell = get_shell(),
|
2019-07-16 11:52:22 +02:00
|
|
|
.font = strdup("monospace"),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
char *path = get_config_path();
|
|
|
|
|
LOG_INFO("loading configuration from %s", path);
|
|
|
|
|
|
|
|
|
|
if (path == NULL) {
|
|
|
|
|
/* Default conf */
|
|
|
|
|
LOG_WARN("no configuration found, using defaults");
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FILE *f = fopen(path, "r");
|
|
|
|
|
if (f == NULL) {
|
|
|
|
|
LOG_ERR("%s: failed to open", path);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-17 10:12:14 +02:00
|
|
|
ret = parse_config_file(f, conf, path);
|
2019-07-16 11:52:22 +02:00
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
free(path);
|
2019-07-17 10:12:14 +02:00
|
|
|
return ret;
|
2019-07-16 11:52:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
config_free(struct config conf)
|
|
|
|
|
{
|
2019-07-17 09:29:56 +02:00
|
|
|
free(conf.shell);
|
2019-07-16 11:52:22 +02:00
|
|
|
free(conf.font);
|
|
|
|
|
}
|