From 31a9fc1fb60f8001f43186d36e3fbb5fce584d91 Mon Sep 17 00:00:00 2001 From: Alexander Orzechowski Date: Fri, 19 Aug 2022 09:59:30 -0400 Subject: [PATCH] util: Introduce env helpers --- include/util/env.h | 11 +++++++++++ util/env.c | 38 ++++++++++++++++++++++++++++++++++++++ util/meson.build | 1 + 3 files changed, 50 insertions(+) create mode 100644 include/util/env.h create mode 100644 util/env.c diff --git a/include/util/env.h b/include/util/env.h new file mode 100644 index 00000000..6720fa89 --- /dev/null +++ b/include/util/env.h @@ -0,0 +1,11 @@ +#ifndef UTIL_ENV_H +#define UTIL_ENV_H + +#include +#include + +bool env_parse_bool(const char *option); + +ssize_t env_parse_switch(const char *option, const char **switches); + +#endif diff --git a/util/env.c b/util/env.c new file mode 100644 index 00000000..b0a9efda --- /dev/null +++ b/util/env.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include "util/env.h" + +bool env_parse_bool(const char *option) { + const char *env = getenv(option); + if (env) { + wlr_log(WLR_INFO, "Loading %s option: %s", option, env); + } + + if (!env || strcmp(env, "0") == 0) { + return false; + } else if (strcmp(env, "1") == 0) { + return true; + } + + wlr_log(WLR_ERROR, "Unknown %s option: %s", option, env); + return false; +} + +ssize_t env_parse_switch(const char *option, const char **switches) { + const char *env = getenv(option); + if (env) { + wlr_log(WLR_INFO, "Loading %s option: %s", option, env); + } else { + return 0; + } + + for (ssize_t i = 0; switches[i]; i++) { + if (strcmp(env, switches[i]) == 0) { + return i; + } + } + + wlr_log(WLR_ERROR, "Unknown %s option: %s", option, env); + return 0; +} diff --git a/util/meson.build b/util/meson.build index 8933f2e7..34fc2e23 100644 --- a/util/meson.build +++ b/util/meson.build @@ -2,6 +2,7 @@ wlr_files += files( 'addon.c', 'array.c', 'box.c', + 'env.c', 'global.c', 'log.c', 'region.c',