2009-06-02 00:58:49 +02:00
|
|
|
/* $OpenBSD$ */
|
2008-06-02 20:08:17 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* 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 16:36:16 +01:00
|
|
|
#include <ctype.h>
|
2008-06-02 20:08:17 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
2012-07-11 21:34:16 +02:00
|
|
|
#include <stdlib.h>
|
2008-06-02 20:08:17 +02:00
|
|
|
#include <string.h>
|
2015-09-01 12:01:56 +02:00
|
|
|
#include <unistd.h>
|
2008-06-02 20:08:17 +02:00
|
|
|
|
|
|
|
#include "tmux.h"
|
2015-01-30 18:08:13 +01:00
|
|
|
#include "tmate.h"
|
2008-06-02 20:08:17 +02:00
|
|
|
|
2015-09-01 12:10:59 +02:00
|
|
|
char *cfg_file;
|
2015-12-23 11:41:41 +01:00
|
|
|
#ifdef TMATE
|
|
|
|
char *tmate_cfg_file;
|
|
|
|
#endif
|
2015-09-01 12:01:56 +02:00
|
|
|
struct cmd_q *cfg_cmd_q;
|
|
|
|
int cfg_finished;
|
|
|
|
int cfg_references;
|
|
|
|
char **cfg_causes;
|
|
|
|
u_int cfg_ncauses;
|
|
|
|
struct client *cfg_client;
|
|
|
|
|
|
|
|
void cfg_default_done(struct cmd_q *);
|
|
|
|
|
2015-09-01 12:10:59 +02:00
|
|
|
void
|
|
|
|
set_cfg_file(const char *path)
|
|
|
|
{
|
|
|
|
free(cfg_file);
|
|
|
|
cfg_file = xstrdup(path);
|
|
|
|
}
|
|
|
|
|
2015-09-01 12:01:56 +02:00
|
|
|
void
|
|
|
|
start_cfg(void)
|
|
|
|
{
|
2015-09-01 12:10:59 +02:00
|
|
|
char *cause = NULL;
|
|
|
|
const char *home;
|
2015-09-01 12:01:56 +02:00
|
|
|
|
|
|
|
cfg_cmd_q = cmdq_new(NULL);
|
|
|
|
cfg_cmd_q->emptyfn = cfg_default_done;
|
|
|
|
|
|
|
|
cfg_finished = 0;
|
|
|
|
cfg_references = 1;
|
|
|
|
|
|
|
|
cfg_client = TAILQ_FIRST(&clients);
|
|
|
|
if (cfg_client != NULL)
|
|
|
|
cfg_client->references++;
|
|
|
|
|
|
|
|
if (access(TMUX_CONF, R_OK) == 0) {
|
|
|
|
if (load_cfg(TMUX_CONF, cfg_cmd_q, &cause) == -1)
|
|
|
|
cfg_add_cause("%s: %s", TMUX_CONF, cause);
|
|
|
|
} else if (errno != ENOENT)
|
|
|
|
cfg_add_cause("%s: %s", TMUX_CONF, strerror(errno));
|
|
|
|
|
2015-09-01 12:10:59 +02:00
|
|
|
if (cfg_file == NULL && (home = find_home()) != NULL) {
|
|
|
|
xasprintf(&cfg_file, "%s/.tmux.conf", home);
|
|
|
|
if (access(cfg_file, R_OK) != 0 && errno == ENOENT) {
|
|
|
|
free(cfg_file);
|
|
|
|
cfg_file = NULL;
|
|
|
|
}
|
|
|
|
}
|
2015-09-01 12:01:56 +02:00
|
|
|
if (cfg_file != NULL && load_cfg(cfg_file, cfg_cmd_q, &cause) == -1)
|
|
|
|
cfg_add_cause("%s: %s", cfg_file, cause);
|
|
|
|
free(cause);
|
|
|
|
|
2015-12-23 11:41:41 +01:00
|
|
|
#ifdef TMATE
|
|
|
|
cause = NULL;
|
|
|
|
if ((home = find_home()) != NULL) {
|
|
|
|
xasprintf(&tmate_cfg_file, "%s/.tmate.conf", home);
|
|
|
|
if (access(tmate_cfg_file, R_OK) != 0 && errno == ENOENT) {
|
|
|
|
free(tmate_cfg_file);
|
|
|
|
tmate_cfg_file = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tmate_cfg_file != NULL && load_cfg(tmate_cfg_file, cfg_cmd_q, &cause) == -1)
|
|
|
|
cfg_add_cause("%s: %s", cfg_file, cause);
|
|
|
|
free(cause);
|
|
|
|
#endif
|
|
|
|
|
2015-09-01 12:01:56 +02:00
|
|
|
cmdq_continue(cfg_cmd_q);
|
|
|
|
}
|
2008-06-02 20:55:53 +02:00
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
int
|
|
|
|
load_cfg(const char *path, struct cmd_q *cmdq, char **cause)
|
2008-06-02 20:08:17 +02:00
|
|
|
{
|
2009-10-29 00:12:38 +01:00
|
|
|
FILE *f;
|
2014-06-25 21:17:27 +02:00
|
|
|
char delim[3] = { '\\', '\\', '\0' };
|
|
|
|
u_int found;
|
|
|
|
size_t line = 0;
|
2014-10-27 23:23:47 +01:00
|
|
|
char *buf, *cause1, *p;
|
2009-01-18 15:40:48 +01:00
|
|
|
struct cmd_list *cmdlist;
|
2008-06-02 20:08:17 +02:00
|
|
|
|
2013-03-05 18:17:59 +01:00
|
|
|
log_debug("loading %s", path);
|
2008-06-02 20:08:17 +02:00
|
|
|
if ((f = fopen(path, "rb")) == NULL) {
|
2013-02-23 23:25:58 +01:00
|
|
|
xasprintf(cause, "%s: %s", path, strerror(errno));
|
|
|
|
return (-1);
|
2013-02-19 00:20:21 +01:00
|
|
|
}
|
|
|
|
|
2014-06-25 21:17:27 +02:00
|
|
|
found = 0;
|
2015-09-09 14:09:21 +02:00
|
|
|
while ((buf = fparseln(f, NULL, &line, delim, 0)) != NULL) {
|
2014-06-25 21:17:27 +02:00
|
|
|
log_debug("%s: %s", path, buf);
|
2011-08-25 23:14:23 +02:00
|
|
|
|
2012-12-13 16:36:16 +01:00
|
|
|
/* Skip empty lines. */
|
2014-06-25 21:17:27 +02:00
|
|
|
p = buf;
|
|
|
|
while (isspace((u_char) *p))
|
|
|
|
p++;
|
|
|
|
if (*p == '\0') {
|
|
|
|
free(buf);
|
2012-12-13 16:36:16 +01:00
|
|
|
continue;
|
2013-02-16 20:35:49 +01:00
|
|
|
}
|
2012-12-13 16:36:16 +01:00
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
/* Parse and run the command. */
|
2014-06-25 21:17:27 +02:00
|
|
|
if (cmd_string_parse(p, &cmdlist, path, line, &cause1) != 0) {
|
|
|
|
free(buf);
|
2013-02-23 23:25:58 +01:00
|
|
|
if (cause1 == NULL)
|
2008-06-19 23:20:27 +02:00
|
|
|
continue;
|
2014-10-27 23:23:47 +01:00
|
|
|
cfg_add_cause("%s:%zu: %s", path, line, cause1);
|
2013-02-23 23:25:58 +01:00
|
|
|
free(cause1);
|
2010-02-08 19:10:07 +01:00
|
|
|
continue;
|
2012-12-06 14:06:05 +01:00
|
|
|
}
|
2014-06-25 21:17:27 +02:00
|
|
|
free(buf);
|
2013-02-23 23:25:58 +01:00
|
|
|
|
2009-01-18 15:40:48 +01:00
|
|
|
if (cmdlist == NULL)
|
2008-07-25 19:20:40 +02:00
|
|
|
continue;
|
2015-04-19 23:34:21 +02:00
|
|
|
cmdq_append(cmdq, cmdlist, NULL);
|
2009-01-18 15:40:48 +01:00
|
|
|
cmd_list_free(cmdlist);
|
2013-02-23 23:25:58 +01:00
|
|
|
found++;
|
2008-06-19 23:13:56 +02:00
|
|
|
}
|
2009-04-01 00:23:43 +02:00
|
|
|
fclose(f);
|
2008-06-02 20:08:17 +02:00
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
return (found);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-11-18 15:27:44 +01:00
|
|
|
cfg_default_done(__unused struct cmd_q *cmdq)
|
2013-02-23 23:25:58 +01:00
|
|
|
{
|
|
|
|
if (--cfg_references != 0)
|
|
|
|
return;
|
|
|
|
cfg_finished = 1;
|
2013-02-19 00:20:21 +01:00
|
|
|
|
2015-12-23 11:41:41 +01:00
|
|
|
#ifdef TMATE
|
2015-01-30 18:08:13 +01:00
|
|
|
tmate_session_start();
|
2015-12-23 11:41:41 +01:00
|
|
|
#endif
|
2015-01-30 18:08:13 +01:00
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
if (!RB_EMPTY(&sessions))
|
|
|
|
cfg_show_causes(RB_MIN(sessions, &sessions));
|
2012-11-27 17:12:29 +01:00
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
cmdq_free(cfg_cmd_q);
|
|
|
|
cfg_cmd_q = NULL;
|
2013-10-20 19:28:43 +02:00
|
|
|
|
|
|
|
if (cfg_client != NULL) {
|
|
|
|
/*
|
|
|
|
* The client command queue starts with client_exit set to 1 so
|
|
|
|
* only continue if not empty (that is, we have been delayed
|
|
|
|
* during configuration parsing for long enough that the
|
|
|
|
* MSG_COMMAND has arrived), else the client will exit before
|
|
|
|
* the MSG_COMMAND which might tell it not to.
|
|
|
|
*/
|
|
|
|
if (!TAILQ_EMPTY(&cfg_client->cmdq->queue))
|
|
|
|
cmdq_continue(cfg_client->cmdq);
|
2015-06-05 20:06:30 +02:00
|
|
|
server_client_unref(cfg_client);
|
2013-10-20 19:28:43 +02:00
|
|
|
cfg_client = NULL;
|
|
|
|
}
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
2012-11-19 11:38:06 +01:00
|
|
|
|
2014-10-27 23:23:47 +01:00
|
|
|
void
|
2015-04-25 20:49:01 +02:00
|
|
|
cfg_add_cause(const char *fmt, ...)
|
2014-10-27 23:23:47 +01:00
|
|
|
{
|
2015-04-25 20:49:01 +02:00
|
|
|
va_list ap;
|
|
|
|
char *msg;
|
2014-10-27 23:23:47 +01:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
xvasprintf(&msg, fmt, ap);
|
2015-05-07 13:42:56 +02:00
|
|
|
va_end(ap);
|
2014-10-27 23:23:47 +01:00
|
|
|
|
2015-04-25 20:47:01 +02:00
|
|
|
cfg_ncauses++;
|
|
|
|
cfg_causes = xreallocarray(cfg_causes, cfg_ncauses, sizeof *cfg_causes);
|
|
|
|
cfg_causes[cfg_ncauses - 1] = msg;
|
2014-10-27 23:23:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cfg_print_causes(struct cmd_q *cmdq)
|
|
|
|
{
|
|
|
|
u_int i;
|
|
|
|
|
2015-04-25 20:47:01 +02:00
|
|
|
for (i = 0; i < cfg_ncauses; i++) {
|
|
|
|
cmdq_print(cmdq, "%s", cfg_causes[i]);
|
|
|
|
free(cfg_causes[i]);
|
2014-10-27 23:23:47 +01:00
|
|
|
}
|
2015-04-25 20:47:01 +02:00
|
|
|
|
|
|
|
free(cfg_causes);
|
|
|
|
cfg_causes = NULL;
|
2015-04-28 00:50:35 +02:00
|
|
|
cfg_ncauses = 0;
|
2008-06-02 20:08:17 +02:00
|
|
|
}
|
2012-11-19 11:38:06 +01:00
|
|
|
|
|
|
|
void
|
2013-02-23 23:25:58 +01:00
|
|
|
cfg_show_causes(struct session *s)
|
2012-11-19 11:38:06 +01:00
|
|
|
{
|
|
|
|
struct window_pane *wp;
|
|
|
|
u_int i;
|
|
|
|
|
2015-04-25 20:47:01 +02:00
|
|
|
if (s == NULL || cfg_ncauses == 0)
|
2012-11-19 11:38:06 +01:00
|
|
|
return;
|
|
|
|
wp = s->curw->window->active;
|
|
|
|
|
|
|
|
window_pane_set_mode(wp, &window_copy_mode);
|
|
|
|
window_copy_init_for_output(wp);
|
2015-04-25 20:47:01 +02:00
|
|
|
for (i = 0; i < cfg_ncauses; i++) {
|
|
|
|
window_copy_add(wp, "%s", cfg_causes[i]);
|
|
|
|
free(cfg_causes[i]);
|
2012-11-19 11:38:06 +01:00
|
|
|
}
|
2015-04-25 20:47:01 +02:00
|
|
|
|
|
|
|
free(cfg_causes);
|
|
|
|
cfg_causes = NULL;
|
2015-04-28 00:50:35 +02:00
|
|
|
cfg_ncauses = 0;
|
2012-11-19 11:38:06 +01:00
|
|
|
}
|