diff --git a/include/config.h b/include/config.h index 92b6d8b..a84d010 100644 --- a/include/config.h +++ b/include/config.h @@ -12,6 +12,11 @@ void parse_config_file(char *path); */ void setup_led_formula(void); +/** + * @brief ensure that the config paths are valid + */ +void check_config(); + extern char *battery_status_path; extern char *battery_power_source; diff --git a/src/config.c b/src/config.c index 16b3b6e..a1c8ead 100644 --- a/src/config.c +++ b/src/config.c @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -13,7 +14,7 @@ if (!v) { \ log_warn("missing [%s]", vn); \ } \ - } while (0); + } while (0) #define set_str_conf(op, tt, v) do { \ toml_datum_t val; \ @@ -21,7 +22,7 @@ if (val.ok) { \ op = val.u.s; \ } \ - } while (0); + } while (0) #define set_int_conf(op, tt, v) do { \ toml_datum_t val; \ @@ -29,7 +30,15 @@ if (val.ok) { \ op = val.u.i; \ } \ - } while (0); + } while (0) + +#define check_path(path) do { \ + struct stat sb; \ + if (stat(path, &sb) == -1) { \ + log_fatal("path does not exist: %s", path); \ + exit(1); \ + } \ + } while (0) static te_expr *expr; static double percentage; @@ -105,5 +114,16 @@ parse_config_file(char *path) set_str_conf(led_blink_timing_formula, led_blink, "timing_formula"); set_str_conf(acpi_daemon_socket_path, acpi_daemon, "socket_path"); + check_config(); + toml_free(config); } + +void +check_config() +{ + check_path(battery_status_path); + check_path(battery_power_source); + check_path(led_brightness_path); + check_path(acpi_daemon_socket_path); +} diff --git a/src/main.c b/src/main.c index 8188486..2cff9b6 100644 --- a/src/main.c +++ b/src/main.c @@ -86,7 +86,7 @@ int main(int argc, char *argv[]) { cag_option_context context; - bool plugged = plugged_in(battery_power_source); + bool plugged; char buffer[MAX_BUFLEN], *out[4], *config_path, *percent; int sock_fd; bool debug = false; @@ -113,6 +113,10 @@ main(int argc, char *argv[]) break; } } + check_config(); + + /* get current plugged in state */ + plugged = plugged_in(battery_power_source); /* configure the led blinking formula */ setup_led_formula();