=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/tmux/cfg.c,v retrieving revision 1.75 retrieving revision 1.76 diff -u -r1.75 -r1.76 --- src/usr.bin/tmux/cfg.c 2019/06/20 06:51:36 1.75 +++ src/usr.bin/tmux/cfg.c 2019/12/10 14:22:15 1.76 @@ -1,4 +1,4 @@ -/* $OpenBSD: cfg.c,v 1.75 2019/06/20 06:51:36 nicm Exp $ */ +/* $OpenBSD: cfg.c,v 1.76 2019/12/10 14:22:15 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott @@ -67,12 +67,45 @@ cfg_file = xstrdup(path); } +static char * +expand_cfg_file(const char *path, const char *home) +{ + char *expanded, *name; + const char *end; + struct environ_entry *value; + + if (strncmp(path, "~/", 2) == 0) { + if (home == NULL) + return (NULL); + xasprintf(&expanded, "%s%s", home, path + 1); + return (expanded); + } + + if (*path == '$') { + end = strchr(path, '/'); + if (end == NULL) + name = xstrdup(path + 1); + else + name = xstrndup(path + 1, end - path - 1); + value = environ_find(global_environ, name); + free(name); + if (value == NULL) + return (NULL); + if (end == NULL) + end = ""; + xasprintf(&expanded, "%s%s", value->value, end); + return (expanded); + } + + return (xstrdup(path)); +} + void start_cfg(void) { - const char *home; - int flags = 0; + const char *home = find_home(); struct client *c; + char *path, *copy, *next, *expanded; /* * Configuration files are loaded without a client, so commands are run @@ -90,15 +123,21 @@ cmdq_append(c, cfg_item); } - if (cfg_file == NULL) - load_cfg(TMUX_CONF, c, NULL, CMD_PARSE_QUIET, NULL); - - if (cfg_file == NULL && (home = find_home()) != NULL) { - xasprintf(&cfg_file, "%s/.tmux.conf", home); - flags = CMD_PARSE_QUIET; - } - if (cfg_file != NULL) - load_cfg(cfg_file, c, NULL, flags, NULL); + if (cfg_file == NULL) { + path = copy = xstrdup(TMUX_CONF); + while ((next = strsep(&path, ":")) != NULL) { + expanded = expand_cfg_file(next, home); + if (expanded == NULL) { + log_debug("couldn't expand %s", next); + continue; + } + log_debug("expanded %s to %s", next, expanded); + load_cfg(expanded, c, NULL, CMD_PARSE_QUIET, NULL); + free(expanded); + } + free(copy); + } else + load_cfg(cfg_file, c, NULL, 0, NULL); cmdq_append(NULL, cmdq_get_callback(cfg_done, NULL)); }