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>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2019-02-16 11:42:08 +00:00
|
|
|
struct client *cfg_client;
|
2017-04-25 14:46:23 +00:00
|
|
|
int cfg_finished;
|
|
|
|
static char **cfg_causes;
|
|
|
|
static u_int cfg_ncauses;
|
|
|
|
static struct cmdq_item *cfg_item;
|
|
|
|
|
2021-02-22 08:18:13 +00:00
|
|
|
int cfg_quiet = 1;
|
|
|
|
char **cfg_files;
|
|
|
|
u_int cfg_nfiles;
|
|
|
|
|
2017-04-25 14:46:23 +00:00
|
|
|
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;
|
|
|
|
|
2022-06-20 07:59:37 +00:00
|
|
|
cfg_show_causes(NULL);
|
2016-10-16 17:55:14 +00:00
|
|
|
|
2017-04-25 14:46:23 +00:00
|
|
|
if (cfg_item != NULL)
|
2019-06-18 11:08:42 +00:00
|
|
|
cmdq_continue(cfg_item);
|
2017-04-25 14:46:23 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
void
|
|
|
|
start_cfg(void)
|
|
|
|
{
|
2020-04-23 16:56:45 +00:00
|
|
|
struct client *c;
|
2021-02-22 08:18:13 +00:00
|
|
|
u_int i;
|
2023-09-15 06:31:49 +00:00
|
|
|
int flags = 0;
|
2015-09-01 10:01:56 +00:00
|
|
|
|
2017-04-21 13:15:43 +00:00
|
|
|
/*
|
2019-05-23 11:13:30 +00:00
|
|
|
* Configuration files are loaded without a client, so commands are run
|
|
|
|
* in the global queue with item->client NULL.
|
2017-04-25 14:46:23 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
*/
|
2019-02-16 11:42:08 +00:00
|
|
|
cfg_client = c = TAILQ_FIRST(&clients);
|
2017-04-25 14:46:23 +00:00
|
|
|
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
|
|
|
|
2023-09-15 06:31:49 +00:00
|
|
|
if (cfg_quiet)
|
|
|
|
flags = CMD_PARSE_QUIET;
|
|
|
|
for (i = 0; i < cfg_nfiles; i++)
|
|
|
|
load_cfg(cfg_files[i], c, NULL, NULL, flags, NULL);
|
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
|
2023-09-15 06:31:49 +00:00
|
|
|
load_cfg(const char *path, struct client *c, struct cmdq_item *item,
|
|
|
|
struct cmd_find_state *current, int flags, struct cmdq_item **new_item)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2016-10-16 19:04:05 +00:00
|
|
|
FILE *f;
|
2019-05-23 11:13:30 +00:00
|
|
|
struct cmd_parse_input pi;
|
|
|
|
struct cmd_parse_result *pr;
|
2019-05-20 13:23:32 +00:00
|
|
|
struct cmdq_item *new_item0;
|
2021-04-07 12:50:12 +00:00
|
|
|
struct cmdq_state *state;
|
2017-10-06 18:02:30 +00:00
|
|
|
|
2019-05-23 11:13:30 +00:00
|
|
|
if (new_item != NULL)
|
|
|
|
*new_item = NULL;
|
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) {
|
2019-05-23 11:13:30 +00:00
|
|
|
if (errno == ENOENT && (flags & CMD_PARSE_QUIET))
|
2016-05-12 16:05:33 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-05-23 11:13:30 +00:00
|
|
|
memset(&pi, 0, sizeof pi);
|
|
|
|
pi.flags = flags;
|
|
|
|
pi.file = path;
|
2019-05-23 18:39:00 +00:00
|
|
|
pi.line = 1;
|
2019-06-05 20:00:53 +00:00
|
|
|
pi.item = item;
|
2019-06-20 06:51:36 +00:00
|
|
|
pi.c = c;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2019-05-23 11:13:30 +00:00
|
|
|
pr = cmd_parse_from_file(f, &pi);
|
|
|
|
fclose(f);
|
|
|
|
if (pr->status == CMD_PARSE_ERROR) {
|
|
|
|
cfg_add_cause("%s", pr->error);
|
|
|
|
free(pr->error);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if (flags & CMD_PARSE_PARSEONLY) {
|
|
|
|
cmd_list_free(pr->cmdlist);
|
|
|
|
return (0);
|
2017-10-06 18:02:30 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 12:50:12 +00:00
|
|
|
if (item != NULL)
|
2023-09-15 06:31:49 +00:00
|
|
|
state = cmdq_copy_state(cmdq_get_state(item), current);
|
2021-04-07 12:50:12 +00:00
|
|
|
else
|
|
|
|
state = cmdq_new_state(NULL, NULL, 0);
|
|
|
|
cmdq_add_format(state, "current_file", "%s", pi.file);
|
|
|
|
|
|
|
|
new_item0 = cmdq_get_command(pr->cmdlist, state);
|
2019-05-23 11:13:30 +00:00
|
|
|
if (item != NULL)
|
2019-12-19 09:22:33 +00:00
|
|
|
new_item0 = cmdq_insert_after(item, new_item0);
|
2019-05-23 11:13:30 +00:00
|
|
|
else
|
2019-12-19 09:22:33 +00:00
|
|
|
new_item0 = cmdq_append(NULL, new_item0);
|
2019-05-23 11:13:30 +00:00
|
|
|
cmd_list_free(pr->cmdlist);
|
2021-04-07 12:50:12 +00:00
|
|
|
cmdq_free_state(state);
|
2019-05-23 11:13:30 +00:00
|
|
|
|
2019-12-12 12:49:36 +00:00
|
|
|
if (new_item != NULL)
|
|
|
|
*new_item = new_item0;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
load_cfg_from_buffer(const void *buf, size_t len, const char *path,
|
2023-09-15 06:31:49 +00:00
|
|
|
struct client *c, struct cmdq_item *item, struct cmd_find_state *current,
|
|
|
|
int flags, struct cmdq_item **new_item)
|
2019-12-12 12:49:36 +00:00
|
|
|
{
|
|
|
|
struct cmd_parse_input pi;
|
|
|
|
struct cmd_parse_result *pr;
|
|
|
|
struct cmdq_item *new_item0;
|
2021-04-07 12:50:12 +00:00
|
|
|
struct cmdq_state *state;
|
2019-12-12 12:49:36 +00:00
|
|
|
|
|
|
|
if (new_item != NULL)
|
|
|
|
*new_item = NULL;
|
|
|
|
|
|
|
|
log_debug("loading %s", path);
|
|
|
|
|
|
|
|
memset(&pi, 0, sizeof pi);
|
|
|
|
pi.flags = flags;
|
|
|
|
pi.file = path;
|
|
|
|
pi.line = 1;
|
|
|
|
pi.item = item;
|
|
|
|
pi.c = c;
|
|
|
|
|
|
|
|
pr = cmd_parse_from_buffer(buf, len, &pi);
|
|
|
|
if (pr->status == CMD_PARSE_ERROR) {
|
|
|
|
cfg_add_cause("%s", pr->error);
|
|
|
|
free(pr->error);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if (flags & CMD_PARSE_PARSEONLY) {
|
|
|
|
cmd_list_free(pr->cmdlist);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2021-04-07 12:50:12 +00:00
|
|
|
if (item != NULL)
|
2023-09-15 06:31:49 +00:00
|
|
|
state = cmdq_copy_state(cmdq_get_state(item), current);
|
2021-04-07 12:50:12 +00:00
|
|
|
else
|
|
|
|
state = cmdq_new_state(NULL, NULL, 0);
|
|
|
|
cmdq_add_format(state, "current_file", "%s", pi.file);
|
|
|
|
|
|
|
|
new_item0 = cmdq_get_command(pr->cmdlist, state);
|
2019-12-12 12:49:36 +00:00
|
|
|
if (item != NULL)
|
2019-12-19 09:22:33 +00:00
|
|
|
new_item0 = cmdq_insert_after(item, new_item0);
|
2019-12-12 12:49:36 +00:00
|
|
|
else
|
2019-12-19 09:22:33 +00:00
|
|
|
new_item0 = cmdq_append(NULL, new_item0);
|
2019-12-12 12:49:36 +00:00
|
|
|
cmd_list_free(pr->cmdlist);
|
2021-04-07 12:50:12 +00:00
|
|
|
cmdq_free_state(state);
|
2019-12-12 12:49:36 +00:00
|
|
|
|
2019-05-20 13:23:32 +00:00
|
|
|
if (new_item != NULL)
|
2019-05-23 11:13:30 +00:00
|
|
|
*new_item = new_item0;
|
|
|
|
return (0);
|
2013-03-24 09:54:10 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2022-06-20 07:59:37 +00:00
|
|
|
struct client *c = TAILQ_FIRST(&clients);
|
2019-03-12 11:16:49 +00:00
|
|
|
struct window_pane *wp;
|
|
|
|
struct window_mode_entry *wme;
|
|
|
|
u_int i;
|
2012-11-19 10:38:06 +00:00
|
|
|
|
2022-06-20 07:59:37 +00:00
|
|
|
if (cfg_ncauses == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (c != NULL && (c->flags & CLIENT_CONTROL)) {
|
|
|
|
for (i = 0; i < cfg_ncauses; i++) {
|
|
|
|
control_write(c, "%%config-error %s", cfg_causes[i]);
|
|
|
|
free(cfg_causes[i]);
|
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s == NULL) {
|
|
|
|
if (c != NULL && c->session != NULL)
|
|
|
|
s = c->session;
|
|
|
|
else
|
|
|
|
s = RB_MIN(sessions, &sessions);
|
|
|
|
}
|
|
|
|
if (s == NULL || s->attached == 0) /* wait for an attached session */
|
2012-11-19 10:38:06 +00:00
|
|
|
return;
|
|
|
|
wp = s->curw->window->active;
|
|
|
|
|
2019-03-12 11:16:49 +00:00
|
|
|
wme = TAILQ_FIRST(&wp->modes);
|
|
|
|
if (wme == NULL || wme->mode != &window_view_mode)
|
2020-04-10 07:44:26 +00:00
|
|
|
window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL);
|
2015-04-25 18:47:01 +00:00
|
|
|
for (i = 0; i < cfg_ncauses; i++) {
|
2022-04-18 10:47:14 +00:00
|
|
|
window_copy_add(wp, 0, "%s", cfg_causes[i]);
|
2015-04-25 18:47:01 +00:00
|
|
|
free(cfg_causes[i]);
|
2012-11-19 10:38:06 +00:00
|
|
|
}
|
2015-04-25 18:47:01 +00:00
|
|
|
|
2022-06-20 07:59:37 +00:00
|
|
|
out:
|
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
|
|
|
}
|