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>
|
2014-06-25 19:17:27 +00:00
|
|
|
#include <util.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2017-10-06 18:02:30 +00:00
|
|
|
/* Condition for %if, %elif, %else and %endif. */
|
|
|
|
struct cfg_cond {
|
|
|
|
size_t line; /* line number of %if */
|
|
|
|
int met; /* condition was met */
|
|
|
|
int skip; /* skip later %elif/%else */
|
|
|
|
int saw_else; /* saw a %else */
|
|
|
|
|
|
|
|
TAILQ_ENTRY(cfg_cond) entry;
|
|
|
|
};
|
|
|
|
TAILQ_HEAD(cfg_conds, cfg_cond);
|
|
|
|
|
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
|
|
|
|
2017-10-06 18:02:30 +00:00
|
|
|
static int
|
|
|
|
cfg_check_condition(const char *path, size_t line, const char *p, int *skip)
|
|
|
|
{
|
|
|
|
struct format_tree *ft;
|
|
|
|
char *s;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
while (isspace((u_char)*p))
|
|
|
|
p++;
|
|
|
|
if (p[0] == '\0') {
|
|
|
|
cfg_add_cause("%s:%zu: invalid condition", path, line);
|
|
|
|
*skip = 1;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
ft = format_create(NULL, NULL, FORMAT_NONE, FORMAT_NOJOBS);
|
|
|
|
s = format_expand(ft, p);
|
|
|
|
result = format_true(s);
|
|
|
|
free(s);
|
|
|
|
format_free(ft);
|
|
|
|
|
|
|
|
*skip = result;
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cfg_handle_if(const char *path, size_t line, struct cfg_conds *conds,
|
|
|
|
const char *p)
|
|
|
|
{
|
|
|
|
struct cfg_cond *cond;
|
|
|
|
struct cfg_cond *parent = TAILQ_FIRST(conds);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a new condition. If a previous condition exists and isn't
|
|
|
|
* currently met, this new one also can't be met.
|
|
|
|
*/
|
|
|
|
cond = xcalloc(1, sizeof *cond);
|
|
|
|
cond->line = line;
|
|
|
|
if (parent == NULL || parent->met)
|
|
|
|
cond->met = cfg_check_condition(path, line, p, &cond->skip);
|
|
|
|
else
|
|
|
|
cond->skip = 1;
|
|
|
|
cond->saw_else = 0;
|
|
|
|
TAILQ_INSERT_HEAD(conds, cond, entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cfg_handle_elif(const char *path, size_t line, struct cfg_conds *conds,
|
|
|
|
const char *p)
|
|
|
|
{
|
|
|
|
struct cfg_cond *cond = TAILQ_FIRST(conds);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If a previous condition exists and wasn't met, check this
|
|
|
|
* one instead and change the state.
|
|
|
|
*/
|
|
|
|
if (cond == NULL || cond->saw_else)
|
|
|
|
cfg_add_cause("%s:%zu: unexpected %%elif", path, line);
|
|
|
|
else if (!cond->skip)
|
|
|
|
cond->met = cfg_check_condition(path, line, p, &cond->skip);
|
|
|
|
else
|
|
|
|
cond->met = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cfg_handle_else(const char *path, size_t line, struct cfg_conds *conds)
|
|
|
|
{
|
|
|
|
struct cfg_cond *cond = TAILQ_FIRST(conds);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If a previous condition exists and wasn't met and wasn't already
|
|
|
|
* %else, use this one instead.
|
|
|
|
*/
|
|
|
|
if (cond == NULL || cond->saw_else) {
|
|
|
|
cfg_add_cause("%s:%zu: unexpected %%else", path, line);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cond->saw_else = 1;
|
|
|
|
cond->met = !cond->skip;
|
|
|
|
cond->skip = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cfg_handle_endif(const char *path, size_t line, struct cfg_conds *conds)
|
|
|
|
{
|
|
|
|
struct cfg_cond *cond = TAILQ_FIRST(conds);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove previous condition if one exists.
|
|
|
|
*/
|
|
|
|
if (cond == NULL) {
|
|
|
|
cfg_add_cause("%s:%zu: unexpected %%endif", path, line);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
TAILQ_REMOVE(conds, cond, entry);
|
|
|
|
free(cond);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cfg_handle_directive(const char *p, const char *path, size_t line,
|
|
|
|
struct cfg_conds *conds)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
while (p[n] != '\0' && !isspace((u_char)p[n]))
|
|
|
|
n++;
|
|
|
|
if (strncmp(p, "%if", n) == 0)
|
|
|
|
cfg_handle_if(path, line, conds, p + n);
|
|
|
|
else if (strncmp(p, "%elif", n) == 0)
|
|
|
|
cfg_handle_elif(path, line, conds, p + n);
|
|
|
|
else if (strcmp(p, "%else") == 0)
|
|
|
|
cfg_handle_else(path, line, conds);
|
|
|
|
else if (strcmp(p, "%endif") == 0)
|
|
|
|
cfg_handle_endif(path, line, conds);
|
|
|
|
else
|
|
|
|
cfg_add_cause("%s:%zu: invalid directive: %s", path, line, p);
|
|
|
|
}
|
|
|
|
|
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-10-06 18:02:30 +00:00
|
|
|
char *buf, *cause1, *p, *q;
|
2016-10-16 19:04:05 +00:00
|
|
|
struct cmd_list *cmdlist;
|
|
|
|
struct cmdq_item *new_item;
|
2017-10-06 18:02:30 +00:00
|
|
|
struct cfg_cond *cond, *cond1;
|
|
|
|
struct cfg_conds conds;
|
|
|
|
|
|
|
|
TAILQ_INIT(&conds);
|
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';
|
|
|
|
|
2017-10-06 18:02:30 +00:00
|
|
|
if (*p == '%') {
|
|
|
|
cfg_handle_directive(p, path, line, &conds);
|
2017-01-09 21:28:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-10-06 18:02:30 +00:00
|
|
|
cond = TAILQ_FIRST(&conds);
|
|
|
|
if (cond != NULL && !cond->met)
|
2017-01-09 21:28:56 +00:00
|
|
|
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);
|
|
|
|
|
2017-10-06 18:02:30 +00:00
|
|
|
TAILQ_FOREACH_REVERSE_SAFE(cond, &conds, cfg_conds, entry, cond1) {
|
|
|
|
cfg_add_cause("%s:%zu: unterminated %%if", path, cond->line);
|
|
|
|
TAILQ_REMOVE(&conds, cond, entry);
|
|
|
|
free(cond);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
Rewrite of choose mode, both to simplify and tidy the code and to add
some modern features.
Now the common code is in mode-tree.c, which provides an API used by the
three modes now separated into window-{buffer,client,tree}.c. Buffer
mode shows buffers, client mode clients and tree mode a tree of
sessions, windows and panes.
Each mode has a common set of key bindings plus a few that are specific
to the mode. Other changes are:
- each mode has a preview pane: for buffers this is the buffer content
(very useful), for others it is a preview of the pane;
- items may be sorted in different ways ('O' key);
- multiple items may be tagged and an operation applied to all of them
(for example, to delete multiple buffers at once);
- in tree mode a command may be run on the selected item (session,
window, pane) or on tagged items (key ':');
- displayed items may be filtered in tree mode by using a format (this
is used to implement find-window) (key 'f');
- the custom format (-F) for the display is no longer available;
- shortcut keys change from 0-9, a-z, A-Z which was always a bit weird
with keys used for other uses to 0-9, M-a to M-z.
Now that the code is simpler, other improvements will come later.
Primary key bindings for each mode are documented under the commands in
the man page (choose-buffer, choose-client, choose-tree).
Parts written by Thomas Adam.
2017-05-30 21:44:59 +00:00
|
|
|
window_pane_set_mode(wp, &window_copy_mode, NULL, NULL);
|
2012-11-19 10:38:06 +00:00
|
|
|
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
|
|
|
}
|