do config :)

This commit is contained in:
2024-07-14 21:44:08 +02:00
parent f9bfc48a9f
commit 0955b115e6
12 changed files with 303 additions and 135 deletions

138
src/config.c Normal file
View File

@@ -0,0 +1,138 @@
#include "util.h"
#include <string.h>
#include <sys/stat.h>
#include <config.h>
bool line_parser(FILE *fd, ssize_t *blksize,
u_char (*cb)(char *key, char *value)) {
size_t opt_size = 4096;
if (blksize != NULL)
opt_size = *blksize;
while (true) {
size_t alloc_size = opt_size;
char *buf = malloc(alloc_size);
if (buf == NULL)
return false;
ssize_t read_size = getline(&buf, &alloc_size, fd);
if (read_size == -1) {
free(buf);
break;
}
uint read;
char *key = malloc(read_size);
if (key == NULL) {
free(buf);
return false;
}
if ((read = sscanf(buf, "%[^ ] = %[^\n]\n", key, buf)) != 0) {
u_char ret = cb(key, buf);
if (ret & 0b0100)
free(key);
if (ret & 0b0010)
free(buf);
if (ret & 0b1000)
return false;
if (ret & 0b0001)
break;
}
}
return true;
}
struct config *__config;
u_char config_line_handler(char *k, char *v) {
if(strcmp(k, "colors.bg") == 0)
__config->theme.colors.bg = v;
else if(strcmp(k, "colors.fg") == 0)
__config->theme.colors.fg = v;
else if(strcmp(k, "colors.err") == 0)
__config->theme.colors.err = v;
else if(strcmp(k, "colors.s_wl") == 0)
__config->theme.colors.s_wl = v;
else if(strcmp(k, "colors.s_xorg") == 0)
__config->theme.colors.s_xorg = v;
else if(strcmp(k, "colors.s_shell") == 0)
__config->theme.colors.s_shell = v;
else if(strcmp(k, "colors.f_other") == 0)
__config->theme.colors.f_other = v;
else if(strcmp(k, "colors.e_hostname") == 0)
__config->theme.colors.e_hostname = v;
else if(strcmp(k, "colors.e_date") == 0)
__config->theme.colors.e_date = v;
else if(strcmp(k, "colors.e_box") == 0)
__config->theme.colors.e_box = v;
else if(strcmp(k, "colors.e_header") == 0)
__config->theme.colors.e_header = v;
else if(strcmp(k, "colors.e_user") == 0)
__config->theme.colors.e_user = v;
else if(strcmp(k, "colors.e_passwd") == 0)
__config->theme.colors.e_passwd = v;
else if(strcmp(k, "colors.e_badpasswd") == 0)
__config->theme.colors.e_badpasswd = v;
else if(strcmp(k, "colors.e_key") == 0)
__config->theme.colors.e_key = v;
else if(strcmp(k, "chars.hb") == 0)
__config->theme.chars.hb = v;
else if(strcmp(k, "chars.vb") == 0)
__config->theme.chars.vb = v;
else if(strcmp(k, "chars.ctl") == 0)
__config->theme.chars.ctl = v;
else if(strcmp(k, "chars.ctr") == 0)
__config->theme.chars.ctr = v;
else if(strcmp(k, "chars.cbl") == 0)
__config->theme.chars.cbl = v;
else if(strcmp(k, "chars.cbr") == 0)
__config->theme.chars.cbr = v;
else if(strcmp(k, "functions.poweroff") == 0)
__config->functions.poweroff = find_keyname(v);
else if(strcmp(k, "functions.reboot") == 0)
__config->functions.reboot = find_keyname(v);
else if(strcmp(k, "functions.refresh") == 0)
__config->functions.refresh = find_keyname(v);
else if(strcmp(k, "strings.f_poweroff") == 0)
__config->strings.f_poweroff= v;
else if(strcmp(k, "strings.f_reboot") == 0)
__config->strings.f_reboot= v;
else if(strcmp(k, "strings.f_refresh") == 0)
__config->strings.f_refresh= v;
else if(strcmp(k, "strings.e_user") == 0)
__config->strings.e_user= v;
else if(strcmp(k, "strings.e_passwd") == 0)
__config->strings.e_passwd= v;
else if(strcmp(k, "strings.s_xorg") == 0)
__config->strings.s_xorg= v;
else if(strcmp(k, "strings.s_wayland") == 0)
__config->strings.s_wayland= v;
else if(strcmp(k, "strings.s_shell") == 0)
__config->strings.s_shell= v;
else if(strcmp(k, "behavior.include_defshell") == 0)
__config->behavior.include_defshell= strcmp(v, "true") == 0;
else
return 0b1111;
return 0b100;
}
struct config *parse_config(char *path) {
struct stat sb;
FILE *fd = fopen(path, "r");
if (fd == NULL || (stat(path, &sb) == -1)) {
perror("fopen");
return NULL;
}
__config = malloc(sizeof(struct config));
if (__config == NULL)
return NULL;
bool ret = line_parser(fd, &sb.st_blksize, config_line_handler);
if (!ret) {
free(__config);
return NULL;
}
return __config;
}

View File

@@ -4,71 +4,18 @@
#include <sys/types.h>
#include <unistd.h>
#include <config.h>
#include <sessions.h>
#include <ui.h>
#include <users.h>
static const struct config default_config() {
struct theme_colors __colors;
__colors.bg = "48;2;38;28;28";
__colors.fg = "22;24;38;2;245;245;245";
__colors.err = "1;31";
__colors.s_wl = "38;2;255;174;66";
__colors.s_xorg = "38;2;37;175;255";
__colors.s_shell = "38;2;34;140;34";
__colors.f_other = "38;2;255;64;64";
__colors.e_hostname = "38;2;255;64;64";
__colors.e_date = "38;2;144;144;144";
__colors.e_box = "38;2;122;122;122";
__colors.e_header = "4;38;2;0;255;0";
__colors.e_user = "36";
__colors.e_passwd = "4;38;2;245;245;205";
__colors.e_badpasswd = "3;4;31";
__colors.e_key = "38;2;255;174;66";
struct theme_chars __chars;
__chars.hb = "";
__chars.vb = "";
__chars.ctl = "";
__chars.ctr = "";
__chars.cbl = "";
__chars.cbr = "";
struct theme __theme;
__theme.colors = __colors;
__theme.chars = __chars;
struct functions __functions;
__functions.poweroff = F1;
__functions.reboot = F2;
__functions.refresh = F5;
struct strings __strings;
__strings.f_poweroff = "powewoff";
__strings.f_reboot = "rewoot";
__strings.f_refresh = "rewresh";
__strings.e_user = "wuser";
__strings.e_passwd = "passwd";
__strings.s_xorg = "xworg";
__strings.s_wayland = "waywand";
__strings.s_shell = "swell";
struct behavior __behavior;
__behavior.include_defshell = true;
struct config __config;
__config.theme = __theme;
__config.functions = __functions;
__config.strings = __strings;
__config.behavior = __behavior;
return __config;
}
#include <stdio.h>
#include <auth.h>
int main(int argc, char *argv[]) {
setup(default_config());
struct config* config = parse_config("/etc/lidm.ini");
if(config == NULL) {
fprintf(stderr, "error parsing config\n");
return 1;
}
setup(*config);
struct users_list *users = get_human_users();
struct sessions_list *sessions = get_avaliable_sessions();

View File

@@ -92,6 +92,8 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) {
break;
}
fclose(fd);
// just add this to the list
if (name_buf != NULL && exec_buf != NULL) {
if (used_size >= alloc_size) {

View File

@@ -15,6 +15,16 @@ void strcln(char **dest, const char *source) {
strcpy(*dest, source);
}
enum keys find_keyname(char* name) {
for (size_t i = 0; i < sizeof(key_mappings) / sizeof(key_mappings[0]); i++) {
if(strcmp(key_names[i], name) == 0)
return (enum keys)i;
}
perror("key not found");
exit(1);
}
enum keys find_ansi(char *seq) {
for (size_t i = 0; i < sizeof(key_mappings) / sizeof(key_mappings[0]); i++) {
struct key_mapping mapping = key_mappings[i];