2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2009-06-01 22:58:49 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2012-12-13 15:36:16 +00:00
|
|
|
#include <ctype.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
2012-07-10 11:53:01 +00:00
|
|
|
#include <stdlib.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
#include <string.h>
|
2015-09-01 10:01:56 +00:00
|
|
|
#include <unistd.h>
|
2014-06-25 19:17:27 +00:00
|
|
|
#include <util.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2017-04-25 14:46:23 +00:00
|
|
|
static char *cfg_file;
|
|
|
|
int cfg_finished;
|
|
|
|
static char **cfg_causes;
|
|
|
|
static u_int cfg_ncauses;
|
|
|
|
static struct cmdq_item *cfg_item;
|
|
|
|
|
|
|
|
static enum cmd_retval
|
|
|
|
cfg_client_done(__unused struct cmdq_item *item, __unused void *data)
|
|
|
|
{
|
|
|
|
if (!cfg_finished)
|
|
|
|
return (CMD_RETURN_WAIT);
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
2015-09-01 10:01:56 +00:00
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cfg_done(__unused struct cmdq_item *item, __unused void *data)
|
2016-10-16 17:55:14 +00:00
|
|
|
{
|
|
|
|
if (cfg_finished)
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
cfg_finished = 1;
|
|
|
|
|
|
|
|
if (!RB_EMPTY(&sessions))
|
|
|
|
cfg_show_causes(RB_MIN(sessions, &sessions));
|
|
|
|
|
2017-04-25 14:46:23 +00:00
|
|
|
if (cfg_item != NULL)
|
|
|
|
cfg_item->flags &= ~CMDQ_WAITING;
|
|
|
|
|
2017-04-21 14:04:54 +00:00
|
|
|
status_prompt_load_history();
|
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
2015-09-01 10:01:56 +00:00
|
|
|
|
2015-09-01 10:10:59 +00:00
|
|
|
void
|
|
|
|
set_cfg_file(const char *path)
|
|
|
|
{
|
|
|
|
free(cfg_file);
|
|
|
|
cfg_file = xstrdup(path);
|
|
|
|
}
|
|
|
|
|
2015-09-01 10:01:56 +00:00
|
|
|
void
|
|
|
|
start_cfg(void)
|
|
|
|
{
|
2015-09-01 10:10:59 +00:00
|
|
|
const char *home;
|
2016-05-12 16:05:33 +00:00
|
|
|
int quiet = 0;
|
2017-04-25 14:46:23 +00:00
|
|
|
struct client *c;
|
2015-09-01 10:01:56 +00:00
|
|
|
|
2017-04-21 13:15:43 +00:00
|
|
|
/*
|
2017-04-25 14:46:23 +00:00
|
|
|
* Configuration files are loaded without a client, so NULL is passed
|
|
|
|
* into load_cfg() and commands run in the global queue with
|
|
|
|
* item->client NULL.
|
|
|
|
*
|
|
|
|
* However, we must block the initial client (but just the initial
|
|
|
|
* client) so that its command runs after the configuration is loaded.
|
|
|
|
* Because start_cfg() is called so early, we can be sure the client's
|
|
|
|
* command queue is currently empty and our callback will be at the
|
|
|
|
* front - we need to get in before MSG_COMMAND.
|
2017-04-21 13:15:43 +00:00
|
|
|
*/
|
2017-04-25 14:46:23 +00:00
|
|
|
c = TAILQ_FIRST(&clients);
|
|
|
|
if (c != NULL) {
|
|
|
|
cfg_item = cmdq_get_callback(cfg_client_done, NULL);
|
|
|
|
cmdq_append(c, cfg_item);
|
|
|
|
}
|
2015-09-01 10:01:56 +00:00
|
|
|
|
2017-04-21 13:15:43 +00:00
|
|
|
load_cfg(TMUX_CONF, NULL, NULL, 1);
|
2015-09-01 10:01:56 +00:00
|
|
|
|
2015-09-01 10:10:59 +00:00
|
|
|
if (cfg_file == NULL && (home = find_home()) != NULL) {
|
|
|
|
xasprintf(&cfg_file, "%s/.tmux.conf", home);
|
2016-05-12 16:05:33 +00:00
|
|
|
quiet = 1;
|
2015-09-01 10:10:59 +00:00
|
|
|
}
|
2016-05-12 16:05:33 +00:00
|
|
|
if (cfg_file != NULL)
|
2017-04-21 13:15:43 +00:00
|
|
|
load_cfg(cfg_file, NULL, NULL, quiet);
|
2015-09-01 10:01:56 +00:00
|
|
|
|
2017-04-21 13:15:43 +00:00
|
|
|
cmdq_append(NULL, cmdq_get_callback(cfg_done, NULL));
|
2015-09-01 10:01:56 +00:00
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2013-03-24 09:54:10 +00:00
|
|
|
int
|
2016-10-16 19:04:05 +00:00
|
|
|
load_cfg(const char *path, struct client *c, struct cmdq_item *item, int quiet)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2016-10-16 19:04:05 +00:00
|
|
|
FILE *f;
|
2017-01-09 21:28:56 +00:00
|
|
|
const char delim[3] = { '\\', '\\', '\0' };
|
|
|
|
u_int found = 0;
|
2016-10-16 19:04:05 +00:00
|
|
|
size_t line = 0;
|
2017-01-09 21:28:56 +00:00
|
|
|
char *buf, *cause1, *p, *q, *s;
|
2016-10-16 19:04:05 +00:00
|
|
|
struct cmd_list *cmdlist;
|
|
|
|
struct cmdq_item *new_item;
|
2017-01-09 21:28:56 +00:00
|
|
|
int condition = 0;
|
|
|
|
struct format_tree *ft;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2013-03-25 10:06:13 +00:00
|
|
|
log_debug("loading %s", path);
|
2009-06-01 22:58:49 +00:00
|
|
|
if ((f = fopen(path, "rb")) == NULL) {
|
2016-05-12 16:05:33 +00:00
|
|
|
if (errno == ENOENT && quiet)
|
|
|
|
return (0);
|
|
|
|
cfg_add_cause("%s: %s", path, strerror(errno));
|
2013-03-24 09:54:10 +00:00
|
|
|
return (-1);
|
2013-03-22 15:50:13 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 12:09:21 +00:00
|
|
|
while ((buf = fparseln(f, NULL, &line, delim, 0)) != NULL) {
|
2014-06-25 19:17:27 +00:00
|
|
|
log_debug("%s: %s", path, buf);
|
2011-08-24 10:46:01 +00:00
|
|
|
|
2014-06-25 19:17:27 +00:00
|
|
|
p = buf;
|
2017-01-09 21:28:56 +00:00
|
|
|
while (isspace((u_char)*p))
|
2014-06-25 19:17:27 +00:00
|
|
|
p++;
|
|
|
|
if (*p == '\0') {
|
|
|
|
free(buf);
|
2012-12-13 15:36:16 +00:00
|
|
|
continue;
|
2013-03-21 16:54:37 +00:00
|
|
|
}
|
2017-01-09 21:28:56 +00:00
|
|
|
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;
|
|
|
|
}
|
2017-05-01 12:20:55 +00:00
|
|
|
ft = format_create(NULL, NULL, FORMAT_NONE,
|
|
|
|
FORMAT_NOJOBS);
|
2017-01-09 21:28:56 +00:00
|
|
|
|
|
|
|
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;
|
2012-12-13 15:36:16 +00:00
|
|
|
|
2017-01-15 22:00:56 +00:00
|
|
|
cmdlist = cmd_string_parse(p, path, line, &cause1);
|
|
|
|
if (cmdlist == NULL) {
|
2014-06-25 19:17:27 +00:00
|
|
|
free(buf);
|
2013-03-24 09:54:10 +00:00
|
|
|
if (cause1 == NULL)
|
2009-06-01 22:58:49 +00:00
|
|
|
continue;
|
2014-10-27 22:23:47 +00:00
|
|
|
cfg_add_cause("%s:%zu: %s", path, line, cause1);
|
2013-03-24 09:54:10 +00:00
|
|
|
free(cause1);
|
2010-02-06 17:15:33 +00:00
|
|
|
continue;
|
2012-12-06 13:06:05 +00:00
|
|
|
}
|
2014-06-25 19:17:27 +00:00
|
|
|
free(buf);
|
2013-03-24 09:54:10 +00:00
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
if (cmdlist == NULL)
|
|
|
|
continue;
|
2016-10-16 19:04:05 +00:00
|
|
|
new_item = cmdq_get_command(cmdlist, NULL, NULL, 0);
|
|
|
|
if (item != NULL)
|
|
|
|
cmdq_insert_after(item, new_item);
|
2016-10-16 17:55:14 +00:00
|
|
|
else
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_append(c, new_item);
|
2009-06-01 22:58:49 +00:00
|
|
|
cmd_list_free(cmdlist);
|
2016-10-16 17:55:14 +00:00
|
|
|
|
2013-03-24 09:54:10 +00:00
|
|
|
found++;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
|
2013-03-24 09:54:10 +00:00
|
|
|
return (found);
|
|
|
|
}
|
|
|
|
|
2014-10-27 22:23:47 +00:00
|
|
|
void
|
2015-04-25 18:49:01 +00:00
|
|
|
cfg_add_cause(const char *fmt, ...)
|
2014-10-27 22:23:47 +00:00
|
|
|
{
|
2015-04-25 18:49:01 +00:00
|
|
|
va_list ap;
|
|
|
|
char *msg;
|
2014-10-27 22:23:47 +00:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
xvasprintf(&msg, fmt, ap);
|
2015-05-07 11:42:56 +00:00
|
|
|
va_end(ap);
|
2014-10-27 22:23:47 +00:00
|
|
|
|
2015-04-25 18:47:01 +00:00
|
|
|
cfg_ncauses++;
|
|
|
|
cfg_causes = xreallocarray(cfg_causes, cfg_ncauses, sizeof *cfg_causes);
|
|
|
|
cfg_causes[cfg_ncauses - 1] = msg;
|
2014-10-27 22:23:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-10-16 19:04:05 +00:00
|
|
|
cfg_print_causes(struct cmdq_item *item)
|
2014-10-27 22:23:47 +00:00
|
|
|
{
|
|
|
|
u_int i;
|
|
|
|
|
2015-04-25 18:47:01 +00:00
|
|
|
for (i = 0; i < cfg_ncauses; i++) {
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_print(item, "%s", cfg_causes[i]);
|
2015-04-25 18:47:01 +00:00
|
|
|
free(cfg_causes[i]);
|
2014-10-27 22:23:47 +00:00
|
|
|
}
|
2015-04-25 18:47:01 +00:00
|
|
|
|
|
|
|
free(cfg_causes);
|
|
|
|
cfg_causes = NULL;
|
2015-04-27 22:50:35 +00:00
|
|
|
cfg_ncauses = 0;
|
2014-10-27 22:23:47 +00:00
|
|
|
}
|
|
|
|
|
2012-11-19 10:38:06 +00:00
|
|
|
void
|
2013-03-24 09:54:10 +00:00
|
|
|
cfg_show_causes(struct session *s)
|
2012-11-19 10:38:06 +00:00
|
|
|
{
|
|
|
|
struct window_pane *wp;
|
|
|
|
u_int i;
|
|
|
|
|
2015-04-25 18:47:01 +00:00
|
|
|
if (s == NULL || cfg_ncauses == 0)
|
2012-11-19 10:38:06 +00:00
|
|
|
return;
|
|
|
|
wp = s->curw->window->active;
|
|
|
|
|
|
|
|
window_pane_set_mode(wp, &window_copy_mode);
|
|
|
|
window_copy_init_for_output(wp);
|
2015-04-25 18:47:01 +00:00
|
|
|
for (i = 0; i < cfg_ncauses; i++) {
|
|
|
|
window_copy_add(wp, "%s", cfg_causes[i]);
|
|
|
|
free(cfg_causes[i]);
|
2012-11-19 10:38:06 +00:00
|
|
|
}
|
2015-04-25 18:47:01 +00:00
|
|
|
|
|
|
|
free(cfg_causes);
|
|
|
|
cfg_causes = NULL;
|
2015-04-27 22:50:35 +00:00
|
|
|
cfg_ncauses = 0;
|
2012-11-19 10:38:06 +00:00
|
|
|
}
|