commit 9b363f7d13d380b443a21058202836633f75a8ca from: Caleb Stein date: Thu May 14 19:07:47 2026 UTC move config file read to separate file commit - 5e62c7dbd699ebb47288a586a1aa9824bc924b62 commit + 9b363f7d13d380b443a21058202836633f75a8ca blob - 04c623ad642c67d9bd68e517c8ff8ffcc55cf0a4 blob + 76f7b9f674d767119fa8b8c7f798804fb1618862 --- include/statbar.h +++ include/statbar.h @@ -19,6 +19,15 @@ #define WIFI "\uf1eb" #define ETHERNET "\uef09" +/* Config */ +extern bool weather_loc_valid; +extern bool mail_path_valid; + +extern void read_config(struct timespec *clock_interval, + struct timespec *battery_interval, + struct timespec *cputemp_interval, + struct timespec *fanspeed_interval); + /* Clock */ extern char clock_string[26]; blob - ca9705aa4409a9541ff6410ae3efeb7b0b488ea1 blob + ac72b49cfe8e5e6cf713b4071ecc9589d4860ccc --- src/Makefile +++ src/Makefile @@ -1,5 +1,5 @@ PROG= statbar -SRCS= main.c +SRCS= main.c config.c MAN= CFLAGS += -I../include -I/usr/X11R6/include -I/usr/local/include blob - 1c4546a812004569752d41c3e74f859f621e5cb9 blob + 3236c9badb06bae51b0362a9da8e4c4bf26061ca --- src/main.c +++ src/main.c @@ -2,7 +2,6 @@ #include #include #include -#include #include #include @@ -35,178 +34,12 @@ static volatile sig_atomic_t reload_config = 0; static volatile sig_atomic_t reload_mail = 0; static volatile sig_atomic_t reload_battery = 0; static volatile sig_atomic_t reload_weather = 0; -static bool weather_loc_valid = false; -static bool mail_path_valid = false; +bool weather_loc_valid = false; +bool mail_path_valid = false; int weather_pipe[2]; void -read_config( - struct timespec *clock_interval, - struct timespec *battery_interval, - struct timespec *cputemp_interval, - struct timespec *fanspeed_interval -){ - char *config_full_path; - char *path_sep; - time_t clock_ms; - time_t battery_ms; - time_t cputemp_ms; - char **weather_location; - uid_t uid; - struct passwd *pw; - int mkdir_ret; - FILE *file; - char *line = NULL; - char *delim; - char *nline; - int val; - int i = 0; - size_t n = 0; - - puts("Reading config..."); - - uid = getuid(); - pw = getpwuid(uid); - - if (pw == NULL) - { - perror("getpwuid"); - - return; - } - - if (asprintf(&config_full_path, "%s/.config/statbar/statbar.conf", pw->pw_dir) == -1) - { - perror("asprintf"); - - return; - } - path_sep = config_full_path + 1; - path_sep = strchr(path_sep, '/'); - while (path_sep) - { - *path_sep = '\0'; - mkdir_ret = mkdir(config_full_path, 0700); - if (mkdir_ret != 0 && errno != EEXIST) - { - perror("mkdir"); - free(config_full_path); - - return; - } - *path_sep = '/'; - path_sep++; - path_sep = strchr(path_sep, '/'); - } - file = fopen(config_full_path, "r"); - if (file == NULL) - { - free(config_full_path); - - return; - } - free(config_full_path); - - while (getline(&line, &n, file) > 0) - { - i++; - delim = strchr(line, ':'); - if (delim == NULL || *(delim + 1) == '\n' || *(delim + 1) == '\0') - goto read_err; - - nline = strchr(delim, '\n'); - if (nline != NULL) *nline = '\0'; - - if (strncmp(line, "weather location", strlen("weather location")) == 0) - { - weather_location = get_weather_location_ptr(); - if (weather_loc_valid && *weather_location != NULL) close_weather(); - delim++; - while (*delim == ' ') delim++; - *weather_location = strdup(delim); - if (*weather_location == NULL) - perror("strdup"); - else - weather_loc_valid = true; - - continue; - } - if (strncmp(line, "inbox directory", strlen("inbox directory")) == 0) - { - if (mail_path_valid && *get_mail_path_ptr() != NULL) close_mail(); - delim++; - while (*delim == ' ') delim++; - if (asprintf(get_mail_path_ptr(), "%s/new", delim) == -1) - perror("asprintf"); - else - mail_path_valid = true; - - continue; - } - if (strncmp(line, "ethernet device", strlen("ethernet device")) == 0) - { - delim++; - while (*delim == ' ') delim++; - set_ethernet_device(delim); - continue; - } - if (strncmp(line, "wifi device", strlen("wifi device")) == 0) - { - delim++; - while (*delim == ' ') delim++; - set_wifi_device(delim); - continue; - } - - /* 10 minute max is arbitrary */ - delim++; - val = (int)strtonum(delim, 1, 600000, NULL); - if (val == 0) - goto read_err; - - if (strncmp(line, "clock interval", strlen("clock interval")) == 0) - { - clock_interval->tv_sec = val / 1000; - clock_interval->tv_nsec = (val % 1000) * 1000000; - } - else if (strncmp(line, "battery interval", strlen("battery interval")) == 0) - { - battery_interval->tv_sec = val / 1000; - battery_interval->tv_nsec = (val % 1000) * 1000000; - } - else if (strncmp(line, "cputemp interval", strlen("cputemp interval")) == 0) - { - cputemp_interval->tv_sec = val / 1000; - cputemp_interval->tv_nsec = (val % 1000) * 1000000; - } - else if (strncmp(line, "fanspeed interval", strlen("fanspeed interval")) == 0) - { - fanspeed_interval->tv_sec = val / 1000; - fanspeed_interval->tv_nsec = (val % 1000) * 1000000; - } - else - { - goto read_err; - } - - n = 0; - continue; - - read_err: - (void)fprintf(stderr, "Config syntax error line %d: %s\n", i, line); - free(line); - (void)fclose(file); - - return; - } - free(line); - if (ferror(file)) - perror("getline"); - (void)fclose(file); -} - -void sig_handler(int sig) { int saved_errno = errno; blob - /dev/null blob + 46fc5109abd7af01513817c45f41be69b07ca3b3 (mode 644) --- /dev/null +++ src/config.c @@ -0,0 +1,176 @@ +#include +#include + +#include +#include +#include +#include +#include + +#include "statbar.h" + +void +read_config( + struct timespec *clock_interval, + struct timespec *battery_interval, + struct timespec *cputemp_interval, + struct timespec *fanspeed_interval +){ + char *config_full_path; + char *path_sep; + time_t clock_ms; + time_t battery_ms; + time_t cputemp_ms; + char **weather_location; + uid_t uid; + struct passwd *pw; + int mkdir_ret; + FILE *file; + char *line = NULL; + char *delim; + char *nline; + int val; + int i = 0; + size_t n = 0; + + puts("Reading config..."); + + uid = getuid(); + pw = getpwuid(uid); + + if (pw == NULL) + { + perror("getpwuid"); + + return; + } + + if (asprintf(&config_full_path, "%s/.config/statbar/statbar.conf", pw->pw_dir) == -1) + { + perror("asprintf"); + + return; + } + path_sep = config_full_path + 1; + path_sep = strchr(path_sep, '/'); + while (path_sep) + { + *path_sep = '\0'; + mkdir_ret = mkdir(config_full_path, 0700); + if (mkdir_ret != 0 && errno != EEXIST) + { + perror("mkdir"); + free(config_full_path); + + return; + } + *path_sep = '/'; + path_sep++; + path_sep = strchr(path_sep, '/'); + } + file = fopen(config_full_path, "r"); + if (file == NULL) + { + free(config_full_path); + + return; + } + free(config_full_path); + + while (getline(&line, &n, file) > 0) + { + i++; + delim = strchr(line, ':'); + if (delim == NULL || *(delim + 1) == '\n' || *(delim + 1) == '\0') + goto read_err; + + nline = strchr(delim, '\n'); + if (nline != NULL) *nline = '\0'; + + if (strncmp(line, "weather location", strlen("weather location")) == 0) + { + weather_location = get_weather_location_ptr(); + if (weather_loc_valid && *weather_location != NULL) close_weather(); + delim++; + while (*delim == ' ') delim++; + *weather_location = strdup(delim); + if (*weather_location == NULL) + perror("strdup"); + else + weather_loc_valid = true; + + continue; + } + if (strncmp(line, "inbox directory", strlen("inbox directory")) == 0) + { + if (mail_path_valid && *get_mail_path_ptr() != NULL) close_mail(); + delim++; + while (*delim == ' ') delim++; + if (asprintf(get_mail_path_ptr(), "%s/new", delim) == -1) + perror("asprintf"); + else + mail_path_valid = true; + + continue; + } + if (strncmp(line, "ethernet device", strlen("ethernet device")) == 0) + { + delim++; + while (*delim == ' ') delim++; + set_ethernet_device(delim); + continue; + } + if (strncmp(line, "wifi device", strlen("wifi device")) == 0) + { + delim++; + while (*delim == ' ') delim++; + set_wifi_device(delim); + continue; + } + + /* 10 minute max is arbitrary */ + delim++; + val = (int)strtonum(delim, 1, 600000, NULL); + if (val == 0) + goto read_err; + + if (strncmp(line, "clock interval", strlen("clock interval")) == 0) + { + clock_interval->tv_sec = val / 1000; + clock_interval->tv_nsec = (val % 1000) * 1000000; + } + else if (strncmp(line, "battery interval", strlen("battery interval")) == 0) + { + battery_interval->tv_sec = val / 1000; + battery_interval->tv_nsec = (val % 1000) * 1000000; + } + else if (strncmp(line, "cputemp interval", strlen("cputemp interval")) == 0) + { + cputemp_interval->tv_sec = val / 1000; + cputemp_interval->tv_nsec = (val % 1000) * 1000000; + } + else if (strncmp(line, "fanspeed interval", strlen("fanspeed interval")) == 0) + { + fanspeed_interval->tv_sec = val / 1000; + fanspeed_interval->tv_nsec = (val % 1000) * 1000000; + } + else + { + goto read_err; + } + + n = 0; + continue; + + read_err: + (void)fprintf(stderr, "Config syntax error line %d: %s\n", i, line); + free(line); + (void)fclose(file); + + return; + } + free(line); + if (ferror(file)) + perror("getline"); + (void)fclose(file); +}