summaryrefslogtreecommitdiffstats
path: root/cfg.c
diff options
context:
space:
mode:
authorThomas Adam <thomas@xteddy.org>2017-01-09 22:01:12 +0000
committerThomas Adam <thomas@xteddy.org>2017-01-09 22:01:12 +0000
commit2c862b04af15d125b6fb82f1db1ec0a59b1ffff2 (patch)
tree778c41073382825c9a352f8ba654e0a9181bef69 /cfg.c
parent36619607ba79f15a793d9c04d9cb98a98562dc30 (diff)
parentd2d984647ee0149c6fc9ec4bcd8d109679208a27 (diff)
Merge branch 'obsd-master'
Diffstat (limited to 'cfg.c')
-rw-r--r--cfg.c44
1 files changed, 37 insertions, 7 deletions
diff --git a/cfg.c b/cfg.c
index d425b748..ce80c142 100644
--- a/cfg.c
+++ b/cfg.c
@@ -81,12 +81,14 @@ int
load_cfg(const char *path, struct client *c, struct cmdq_item *item, int quiet)
{
FILE *f;
- char delim[3] = { '\\', '\\', '\0' };
- u_int found;
+ const char delim[3] = { '\\', '\\', '\0' };
+ u_int found = 0;
size_t line = 0;
- char *buf, *cause1, *p;
+ char *buf, *cause1, *p, *q, *s;
struct cmd_list *cmdlist;
struct cmdq_item *new_item;
+ int condition = 0;
+ struct format_tree *ft;
log_debug("loading %s", path);
if ((f = fopen(path, "rb")) == NULL) {
@@ -96,20 +98,48 @@ load_cfg(const char *path, struct client *c, struct cmdq_item *item, int quiet)
return (-1);
}
- found = 0;
while ((buf = fparseln(f, NULL, &line, delim, 0)) != NULL) {
log_debug("%s: %s", path, buf);
- /* Skip empty lines. */
p = buf;
- while (isspace((u_char) *p))
+ while (isspace((u_char)*p))
p++;
if (*p == '\0') {
free(buf);
continue;
}
+ q = p + strlen(p) - 1;
+ while (q != p && isspace((u_char)*q))
+ *q-- = '\0';
+
+ if (condition != 0 && strcmp(p, "%endif") == 0) {
+ condition = 0;
+ continue;
+ }
+ if (strncmp(p, "%if ", 4) == 0) {
+ if (condition != 0) {
+ cfg_add_cause("%s:%zu: nested %%if", path,
+ line);
+ continue;
+ }
+ ft = format_create(NULL, FORMAT_NOJOBS);
+
+ s = p + 3;
+ while (isspace((u_char)*s))
+ s++;
+ s = format_expand(ft, s);
+ if (*s != '\0' && (s[0] != '0' || s[1] != '\0'))
+ condition = 1;
+ else
+ condition = -1;
+ free(s);
+
+ format_free(ft);
+ continue;
+ }
+ if (condition == -1)
+ continue;
- /* Parse and run the command. */
if (cmd_string_parse(p, &cmdlist, path, line, &cause1) != 0) {
free(buf);
if (cause1 == NULL)