diff options
author | Nicholas Marriott <nicm@cvs.openbsd.org> | 2017-01-09 21:28:57 +0000 |
---|---|---|
committer | Nicholas Marriott <nicm@cvs.openbsd.org> | 2017-01-09 21:28:57 +0000 |
commit | 410e989f02ceb1a6e7c988ca91c3cfc4a174e6a1 (patch) | |
tree | 9db3b3abcaa44e91a3866ddb16dee21f612a2de6 /usr.bin | |
parent | 6b544ed0aef6350d522dda46bce4c74d5877eb48 (diff) |
Add %if/%endif for conditionals when parsing configuration files, the
argument is a format (the new == and != are useful).
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/tmux/cfg.c | 46 | ||||
-rw-r--r-- | usr.bin/tmux/tmux.1 | 26 |
2 files changed, 63 insertions, 9 deletions
diff --git a/usr.bin/tmux/cfg.c b/usr.bin/tmux/cfg.c index 288929e4999..e670da9c6b0 100644 --- a/usr.bin/tmux/cfg.c +++ b/usr.bin/tmux/cfg.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cfg.c,v 1.51 2017/01/09 19:27:00 nicm Exp $ */ +/* $OpenBSD: cfg.c,v 1.52 2017/01/09 21:28:56 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com> @@ -82,12 +82,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) { @@ -97,20 +99,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) diff --git a/usr.bin/tmux/tmux.1 b/usr.bin/tmux/tmux.1 index b319537efa4..693b1483a8b 100644 --- a/usr.bin/tmux/tmux.1 +++ b/usr.bin/tmux/tmux.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: tmux.1,v 1.520 2017/01/09 21:03:25 nicm Exp $ +.\" $OpenBSD: tmux.1,v 1.521 2017/01/09 21:28:56 nicm Exp $ .\" .\" Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> .\" @@ -930,6 +930,30 @@ If is given, no error will be returned if .Ar path does not exist. +.Pp +Within a configuration file, commands may be made conditional by surrounding +them with +.Em %if +and +.Em %endif +lines. +The argument to +.Em %if +is expanded as a format and if it evaluates to false +.Ns ( Ql 0 +or empty), subsequent lines are ignored until +.Em %endif . +For example: +.Bd -literal -offset indent +%if #{==:#{host},myhost} +set -g status-style bg=red +%endif +.Ed +.Pp +Will change the status line to red if running on +.Ql myhost . +.Em %if +may not be nested. .It Ic start-server .D1 (alias: Ic start ) Start the |