+void fatalsig(int signum) {
+ exit_upon_signal = true;
+}
+
+/*
+ * Do nothing upon SIGUSR1. Running this signal handler will nevertheless
+ * interrupt nanosleep() so that i3status immediately generates new output.
+ *
+ */
+void sigusr1(int signum) {
+}
+
+/*
+ * Checks if the given path exists by calling stat().
+ *
+ */
+static bool path_exists(const char *path) {
+ struct stat buf;
+ return (stat(path, &buf) == 0);
+}
+
+static void *scalloc(size_t size) {
+ void *result = calloc(size, 1);
+ exit_if_null(result, "Error: out of memory (calloc(%zd))\n", size);
+ return result;
+}
+
+static char *sstrdup(const char *str) {
+ char *result = strdup(str);
+ exit_if_null(result, "Error: out of memory (strdup())\n");
+ return result;
+}
+
+/*
+ * Parses the "align" module option (to validate input).
+ */
+static int parse_align(cfg_t *context, cfg_opt_t *option, const char *value, void *result) {
+ if (strcasecmp(value, "left") != 0 && strcasecmp(value,"right") != 0 && strcasecmp(value, "center") != 0)
+ die("Invalid alignment attribute found in section %s, line %d: \"%s\"\n"
+ "Valid attributes are: left, center, right\n", context->name, context->line, value);
+
+ char **cresult = result;
+ *cresult = sstrdup(value);
+
+ return 0;
+}
+
+/*
+ * Parses the "min_width" module option whose value can either be a string or an integer.
+ */
+static int parse_min_width(cfg_t *context, cfg_opt_t *option, const char *value, void *result) {
+ char *end;
+ long num = strtol(value, &end, 10);
+
+ if (num < 0)
+ die("Invalid min_width attribute found in section %s, line %d: %d\n"
+ "Expected positive integer or string\n", context->name, context->line, num);
+ else if (num == LONG_MIN || num == LONG_MAX || (end && *end != '\0'))
+ num = 0;
+
+ if (strlen(value) == 0)
+ die("Empty min_width attribute found in section %s, line %d\n"
+ "Expected positive integer or non-empty string\n", context->name, context->line);
+
+ if (strcmp(value, "0") == 0)
+ die("Invalid min_width attribute found in section %s, line %d: \"%s\"\n"
+ "Expected positive integer or string\n", context->name, context->line, value);
+
+ struct min_width *parsed = scalloc(sizeof(struct min_width));
+ parsed->num = num;
+
+ /* num is preferred, but if it’s 0 (i.e. not valid), store and use
+ * the raw string value */
+ if (num == 0)
+ parsed->str = sstrdup(value);
+
+ struct min_width **cresult = result;
+ *cresult = parsed;
+
+ return 0;
+}
+
+/*
+ * Validates a color in "#RRGGBB" format
+ *
+ */
+static int valid_color(const char *value)
+{
+ if (strlen(value) != 7) return 0;
+ if (value[0] != '#') return 0;
+ for (int i = 1; i < 7; ++i) {
+ if (value[i] >= '0' && value[i] <= '9') continue;
+ if (value[i] >= 'a' && value[i] <= 'f') continue;
+ if (value[i] >= 'A' && value[i] <= 'F') continue;
+ return 0;
+ }
+ return 1;
+}
+
+/*
+ * This function resolves ~ in pathnames.
+ * It may resolve wildcards in the first part of the path, but if no match
+ * or multiple matches are found, it just returns a copy of path as given.
+ *
+ */
+static char *resolve_tilde(const char *path) {
+ static glob_t globbuf;
+ char *head, *tail, *result = NULL;
+
+ tail = strchr(path, '/');
+ head = strndup(path, tail ? (size_t)(tail - path) : strlen(path));
+
+ int res = glob(head, GLOB_TILDE, NULL, &globbuf);
+ free(head);
+ /* no match, or many wildcard matches are bad */
+ if (res == GLOB_NOMATCH || globbuf.gl_pathc != 1)
+ result = sstrdup(path);
+ else if (res != 0) {
+ die("glob() failed");
+ } else {
+ head = globbuf.gl_pathv[0];
+ result = scalloc(strlen(head) + (tail ? strlen(tail) : 0) + 1);
+ strncpy(result, head, strlen(head));
+ if (tail)
+ strncat(result, tail, strlen(tail));
+ }
+ globfree(&globbuf);
+
+ return result;
+}
+
+static char *get_config_path(void) {
+ char *xdg_config_home, *xdg_config_dirs, *config_path;
+
+ /* 1: check the traditional path under the home directory */
+ config_path = resolve_tilde("~/.i3status.conf");
+ if (path_exists(config_path))
+ return config_path;
+
+ /* 2: check for $XDG_CONFIG_HOME/i3status/config */
+ if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
+ xdg_config_home = "~/.config";
+
+ xdg_config_home = resolve_tilde(xdg_config_home);
+ if (asprintf(&config_path, "%s/i3status/config", xdg_config_home) == -1)
+ die("asprintf() failed");
+ free(xdg_config_home);
+
+ if (path_exists(config_path))
+ return config_path;
+ free(config_path);
+
+ /* 3: check the traditional path under /etc */
+ config_path = SYSCONFDIR "/i3status.conf";
+ if (path_exists(config_path))
+ return sstrdup(config_path);
+
+ /* 4: check for $XDG_CONFIG_DIRS/i3status/config */
+ if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
+ xdg_config_dirs = "/etc/xdg";
+
+ char *buf = strdup(xdg_config_dirs);
+ char *tok = strtok(buf, ":");
+ while (tok != NULL) {
+ tok = resolve_tilde(tok);
+ if (asprintf(&config_path, "%s/i3status/config", tok) == -1)
+ die("asprintf() failed");
+ free(tok);
+ if (path_exists(config_path)) {
+ free(buf);
+ return config_path;
+ }
+ free(config_path);
+ tok = strtok(NULL, ":");
+ }
+ free(buf);
+
+ die("Unable to find the configuration file (looked at "
+ "~/.i3status.conf, $XDG_CONFIG_HOME/i3status/config, "
+ "/etc/i3status.conf and $XDG_CONFIG_DIRS/i3status/config)");
+ return NULL;
+}
+
+/*
+ * Returns the default separator to use if no custom separator has been specified.
+ */
+static char *get_default_separator() {
+ if (output_format == O_DZEN2)
+ return "^p(5;-2)^ro(2)^p()^p(5)";
+ if (output_format == O_I3BAR)
+ // anything besides the empty string indicates that the default separator should be used
+ return "default";
+ return " | ";