+#define IS_SPACE(c) ((c) == ' ' || (c) == '\t')
+
+static void
+parse_conf (const char *conf_file, struct conf *config)
+{
+ unsigned int cnt = 0;
+ char line[256 + 1];
+ FILE *conf;
+
+ conf = open_file (conf_file, "r");
+
+ while (fgets (line, sizeof (line), conf))
+ {
+ char *cfg, *val;
+ char *assign, *comment, *opt, *value;
+ char *p;
+
+ cnt++;
+ if ((p = strchr (line, '\r')) && *(p + 1) != '\n')
+ vfprintf_fail ("%s: CR ending of line %u is not supported, switch to CRLF/LF instead", conf_file, cnt);
+ if (strlen (line) > (sizeof (line) - 2))
+ vfprintf_fail ("%s: line %u exceeds maximum of %u characters", conf_file, cnt, (unsigned int)(sizeof (line) - 2));
+ if ((p = strpbrk (line, "\n\r")))
+ *p = '\0';
+/* NAME PARSING (start) */
+ p = line;
+ /* skip leading spaces and tabs for name */
+ while (IS_SPACE (*p))
+ p++;
+ /* skip line if a) string end, b) comment, [cd]) newline */
+ if (*p == '\0' || *p == '#' || *p == '\n' || *p == '\r')
+ continue;
+ opt = p;
+ if (!(assign = strchr (opt, '='))) /* check for = */
+ {
+ char *s;
+ if ((s = strpbrk (opt, "# ")))
+ *s = '\0';
+ vfprintf_fail (formats[FMT_CONF], conf_file, opt, "not followed by =");
+ }
+ p = assign;
+ /* skip trailing spaces and tabs for name */
+ while (IS_SPACE (*(p - 1)))
+ p--;
+ *p = '\0';
+/* NAME PARSING (end) */
+/* NAME VALIDATION (start) */
+ for (p = opt; *p; p++)
+ if (!isalnum (*p) && *p != '-')
+ vfprintf_fail (formats[FMT_CONF], conf_file, opt, "cannot be made of non-option characters");
+/* NAME VALIDATION (end) */
+/* VALUE PARSING (start) */
+ p = assign + 1;
+ /* skip leading spaces and tabs for value */
+ while (IS_SPACE (*p))
+ p++;
+ /* skip line if comment */
+ if (*p == '#')
+ continue;
+ value = p;
+ if ((comment = strchr (p, '#')))
+ p = comment;
+ else
+ p += strlen (p);
+ /* skip trailing spaces and tabs for value */
+ while (IS_SPACE (*(p - 1)))
+ p--;
+ *p = '\0';
+/* VALUE PARSING (end) */
+
+ /* save option name */
+ cfg = xstrdup (opt);
+ /* save option value (allow empty ones) */
+ val = strlen (value) ? xstrdup (value) : NULL;
+
+ assign_conf (conf_file, config, cfg, val);
+ free (cfg);
+ }
+
+ fclose (conf);
+}
+
+#define ASSIGN_CONF(str,val) do { \
+ free (str); \
+ str = val; \
+} while (false)
+
+static void
+assign_conf (const char *conf_file, struct conf *config, const char *cfg, char *val)
+{
+ if (streq (cfg, "attr"))
+ ASSIGN_CONF (config->attr, val);
+ else if (streq (cfg, "color"))
+ ASSIGN_CONF (config->color, val);
+ else if (streq (cfg, "exclude-random"))
+ ASSIGN_CONF (config->exclude_random, val);
+ else if (streq (cfg, "omit-color-empty"))
+ ASSIGN_CONF (config->omit_color_empty, val);
+ else
+ vfprintf_fail (formats[FMT_CONF], conf_file, cfg, "not recognized");
+}
+
+static void
+init_conf_vars (const struct conf *config)
+{
+ if (config->attr)
+ process_opt_attr (config->attr, false);
+ if (config->exclude_random)
+ process_opt_exclude_random (config->exclude_random, false);
+ if (config->omit_color_empty)
+ {
+ if (streq (config->omit_color_empty, "yes"))
+ omit_color_empty = true;
+ else if (streq (config->omit_color_empty, "no"))
+ omit_color_empty = false;
+ else
+ vfprintf_fail (formats[FMT_GENERIC], "omit-color-empty conf option is not valid");
+ }