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"
|
|
|
|
|
2016-10-11 13:45:47 +00:00
|
|
|
char *cfg_file;
|
|
|
|
int cfg_finished;
|
|
|
|
static char **cfg_causes;
|
|
|
|
static u_int cfg_ncauses;
|
|
|
|
struct client *cfg_client;
|
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));
|
|
|
|
|
|
|
|
if (cfg_client != NULL)
|
|
|
|
server_client_unref(cfg_client);
|
|
|
|
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;
|
2015-09-01 10:01:56 +00:00
|
|
|
|
|
|
|
cfg_client = TAILQ_FIRST(&clients);
|
|
|
|
if (cfg_client != NULL)
|
|
|
|
cfg_client->references++;
|
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
load_cfg(TMUX_CONF, cfg_client, 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)
|
2016-10-16 17:55:14 +00:00
|
|
|
load_cfg(cfg_file, cfg_client, NULL, quiet);
|
2015-09-01 10:01:56 +00:00
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
cmdq_append(cfg_client, 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;
|
|
|
|
char delim[3] = { '\\', '\\', '\0' };
|
|
|
|
u_int found;
|
|
|
|
size_t line = 0;
|
|
|
|
char *buf, *cause1, *p;
|
|
|
|
struct cmd_list *cmdlist;
|
|
|
|
struct cmdq_item *new_item;
|
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
|
|
|
}
|
|
|
|
|
2014-06-25 19:17:27 +00:00
|
|
|
found = 0;
|
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
|
|
|
|
2012-12-13 15:36:16 +00:00
|
|
|
/* Skip empty lines. */
|
2014-06-25 19:17:27 +00:00
|
|
|
p = buf;
|
|
|
|
while (isspace((u_char) *p))
|
|
|
|
p++;
|
|
|
|
if (*p == '\0') {
|
|
|
|
free(buf);
|
2012-12-13 15:36:16 +00:00
|
|
|
continue;
|
2013-03-21 16:54:37 +00:00
|
|
|
}
|
2012-12-13 15:36:16 +00:00
|
|
|
|
2013-03-24 09:54:10 +00:00
|
|
|
/* Parse and run the command. */
|
2014-06-25 19:17:27 +00:00
|
|
|
if (cmd_string_parse(p, &cmdlist, path, line, &cause1) != 0) {
|
|
|
|
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
|
|
|
}
|