2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2007 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>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Show options.
|
|
|
|
*/
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
static enum cmd_retval cmd_show_options_exec(struct cmd *, struct cmdq_item *);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2019-04-18 12:22:07 +00:00
|
|
|
static void cmd_show_options_print(struct cmd *, struct cmdq_item *,
|
|
|
|
struct options_entry *, int);
|
2016-10-16 19:04:05 +00:00
|
|
|
static enum cmd_retval cmd_show_options_all(struct cmd *, struct cmdq_item *,
|
2017-01-15 20:48:41 +00:00
|
|
|
struct options *);
|
2013-03-21 16:17:01 +00:00
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
const struct cmd_entry cmd_show_options_entry = {
|
2015-12-13 21:53:57 +00:00
|
|
|
.name = "show-options",
|
|
|
|
.alias = "show",
|
|
|
|
|
2019-04-26 11:38:51 +00:00
|
|
|
.args = { "gHqst:vw", 0, 1 },
|
|
|
|
.usage = "[-gHqsvw] [-t target-session|target-window] [option]",
|
2015-12-13 21:53:57 +00:00
|
|
|
|
2017-04-22 10:22:39 +00:00
|
|
|
.target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
|
2015-12-14 00:31:54 +00:00
|
|
|
|
2016-10-14 22:14:22 +00:00
|
|
|
.flags = CMD_AFTERHOOK,
|
2015-12-13 21:53:57 +00:00
|
|
|
.exec = cmd_show_options_exec
|
2009-06-01 22:58:49 +00:00
|
|
|
};
|
|
|
|
|
2011-01-04 02:03:41 +00:00
|
|
|
const struct cmd_entry cmd_show_window_options_entry = {
|
2015-12-13 21:53:57 +00:00
|
|
|
.name = "show-window-options",
|
|
|
|
.alias = "showw",
|
|
|
|
|
|
|
|
.args = { "gvt:", 0, 1 },
|
|
|
|
.usage = "[-gv] " CMD_TARGET_WINDOW_USAGE " [option]",
|
|
|
|
|
2017-04-22 10:22:39 +00:00
|
|
|
.target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
|
2015-12-14 00:31:54 +00:00
|
|
|
|
2016-10-14 22:14:22 +00:00
|
|
|
.flags = CMD_AFTERHOOK,
|
2015-12-13 21:53:57 +00:00
|
|
|
.exec = cmd_show_options_exec
|
2011-01-04 02:03:41 +00:00
|
|
|
};
|
|
|
|
|
2019-04-26 11:38:51 +00:00
|
|
|
const struct cmd_entry cmd_show_hooks_entry = {
|
|
|
|
.name = "show-hooks",
|
|
|
|
.alias = NULL,
|
|
|
|
|
|
|
|
.args = { "gt:", 0, 1 },
|
|
|
|
.usage = "[-g] " CMD_TARGET_SESSION_USAGE,
|
|
|
|
|
|
|
|
.target = { 't', CMD_FIND_SESSION, 0 },
|
|
|
|
|
|
|
|
.flags = CMD_AFTERHOOK,
|
|
|
|
.exec = cmd_show_options_exec
|
|
|
|
};
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_show_options_exec(struct cmd *self, struct cmdq_item *item)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2016-03-03 14:15:22 +00:00
|
|
|
struct args *args = self->args;
|
2017-04-22 10:22:39 +00:00
|
|
|
struct cmd_find_state *fs = &item->target;
|
2019-04-18 12:22:07 +00:00
|
|
|
struct client *c = cmd_find_client(item, NULL, 1);
|
|
|
|
struct session *s = item->target.s;
|
|
|
|
struct winlink *wl = item->target.wl;
|
2016-03-03 14:15:22 +00:00
|
|
|
struct options *oo;
|
|
|
|
enum options_table_scope scope;
|
2019-04-18 12:22:07 +00:00
|
|
|
char *argument, *name = NULL, *cause;
|
|
|
|
const char *target;
|
|
|
|
int window, idx, ambiguous;
|
|
|
|
struct options_entry *o;
|
2017-01-15 20:48:41 +00:00
|
|
|
|
|
|
|
window = (self->entry == &cmd_show_window_options_entry);
|
2019-04-18 12:22:07 +00:00
|
|
|
if (args->argc == 0) {
|
2019-05-22 18:58:31 +00:00
|
|
|
scope = options_scope_from_flags(args, window, fs, &oo, &cause);
|
|
|
|
if (scope == OPTIONS_TABLE_NONE) {
|
|
|
|
if (args_has(args, 'q'))
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
cmdq_error(item, "%s", cause);
|
|
|
|
free(cause);
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
2019-04-18 12:22:07 +00:00
|
|
|
return (cmd_show_options_all(self, item, oo));
|
|
|
|
}
|
|
|
|
argument = format_single(item, args->argv[0], c, s, wl, NULL);
|
|
|
|
|
|
|
|
name = options_match(argument, &idx, &ambiguous);
|
|
|
|
if (name == NULL) {
|
|
|
|
if (args_has(args, 'q'))
|
|
|
|
goto fail;
|
|
|
|
if (ambiguous)
|
|
|
|
cmdq_error(item, "ambiguous option: %s", argument);
|
|
|
|
else
|
|
|
|
cmdq_error(item, "invalid option: %s", argument);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (*name == '@')
|
|
|
|
scope = options_scope_from_flags(args, window, fs, &oo, &cause);
|
|
|
|
else {
|
|
|
|
if (options_get_only(global_options, name) != NULL)
|
|
|
|
scope = OPTIONS_TABLE_SERVER;
|
|
|
|
else if (options_get_only(global_s_options, name) != NULL)
|
|
|
|
scope = OPTIONS_TABLE_SESSION;
|
|
|
|
else if (options_get_only(global_w_options, name) != NULL)
|
|
|
|
scope = OPTIONS_TABLE_WINDOW;
|
|
|
|
else {
|
|
|
|
scope = OPTIONS_TABLE_NONE;
|
|
|
|
xasprintf(&cause, "unknown option: %s", argument);
|
|
|
|
}
|
|
|
|
if (scope == OPTIONS_TABLE_SERVER)
|
|
|
|
oo = global_options;
|
|
|
|
else if (scope == OPTIONS_TABLE_SESSION) {
|
|
|
|
if (args_has(self->args, 'g'))
|
|
|
|
oo = global_s_options;
|
|
|
|
else if (s == NULL) {
|
|
|
|
target = args_get(args, 't');
|
|
|
|
if (target != NULL) {
|
|
|
|
cmdq_error(item, "no such session: %s",
|
|
|
|
target);
|
|
|
|
} else
|
|
|
|
cmdq_error(item, "no current session");
|
|
|
|
goto fail;
|
|
|
|
} else
|
|
|
|
oo = s->options;
|
|
|
|
} else if (scope == OPTIONS_TABLE_WINDOW) {
|
|
|
|
if (args_has(self->args, 'g'))
|
|
|
|
oo = global_w_options;
|
|
|
|
else if (wl == NULL) {
|
|
|
|
target = args_get(args, 't');
|
|
|
|
if (target != NULL) {
|
|
|
|
cmdq_error(item, "no such window: %s",
|
|
|
|
target);
|
|
|
|
} else
|
|
|
|
cmdq_error(item, "no current window");
|
|
|
|
goto fail;
|
|
|
|
} else
|
|
|
|
oo = wl->window->options;
|
|
|
|
}
|
|
|
|
}
|
2017-01-15 20:48:41 +00:00
|
|
|
if (scope == OPTIONS_TABLE_NONE) {
|
2019-04-18 12:22:07 +00:00
|
|
|
if (args_has(args, 'q'))
|
|
|
|
goto fail;
|
2017-01-15 20:48:41 +00:00
|
|
|
cmdq_error(item, "%s", cause);
|
|
|
|
free(cause);
|
2019-04-18 12:22:07 +00:00
|
|
|
goto fail;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
2019-04-18 12:22:07 +00:00
|
|
|
o = options_get_only(oo, name);
|
|
|
|
if (o != NULL)
|
|
|
|
cmd_show_options_print(self, item, o, idx);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2019-04-18 12:22:07 +00:00
|
|
|
free(name);
|
|
|
|
free(argument);
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
free(name);
|
|
|
|
free(argument);
|
|
|
|
return (CMD_RETURN_ERROR);
|
2017-01-15 20:48:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cmd_show_options_print(struct cmd *self, struct cmdq_item *item,
|
2017-01-16 14:49:14 +00:00
|
|
|
struct options_entry *o, int idx)
|
2017-01-15 20:48:41 +00:00
|
|
|
{
|
2019-03-18 11:58:40 +00:00
|
|
|
struct options_array_item *a;
|
2019-04-26 11:38:51 +00:00
|
|
|
const char *name = options_name(o);
|
|
|
|
char *value, *tmp = NULL, *escaped;
|
2017-01-15 20:48:41 +00:00
|
|
|
|
|
|
|
if (idx != -1) {
|
2019-04-26 11:38:51 +00:00
|
|
|
xasprintf(&tmp, "%s[%d]", name, idx);
|
2017-01-15 20:48:41 +00:00
|
|
|
name = tmp;
|
|
|
|
} else {
|
2019-03-18 11:58:40 +00:00
|
|
|
if (options_isarray(o)) {
|
|
|
|
a = options_array_first(o);
|
2019-04-26 11:38:51 +00:00
|
|
|
if (a == NULL) {
|
|
|
|
if (!args_has(self->args, 'v'))
|
|
|
|
cmdq_print(item, "%s", name);
|
|
|
|
return;
|
|
|
|
}
|
2019-03-18 11:58:40 +00:00
|
|
|
while (a != NULL) {
|
|
|
|
idx = options_array_item_index(a);
|
|
|
|
cmd_show_options_print(self, item, o, idx);
|
|
|
|
a = options_array_next(a);
|
2017-01-24 19:11:46 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2017-01-15 20:48:41 +00:00
|
|
|
}
|
|
|
|
|
2017-01-30 21:41:17 +00:00
|
|
|
value = options_tostring(o, idx, 0);
|
2017-01-15 20:48:41 +00:00
|
|
|
if (args_has(self->args, 'v'))
|
|
|
|
cmdq_print(item, "%s", value);
|
|
|
|
else if (options_isstring(o)) {
|
2019-05-23 18:33:53 +00:00
|
|
|
escaped = args_escape(value);
|
|
|
|
cmdq_print(item, "%s %s", name, escaped);
|
2017-01-15 20:48:41 +00:00
|
|
|
free(escaped);
|
|
|
|
} else
|
|
|
|
cmdq_print(item, "%s %s", name, value);
|
2019-04-25 18:18:55 +00:00
|
|
|
free(value);
|
2017-01-15 20:48:41 +00:00
|
|
|
|
|
|
|
free(tmp);
|
2013-03-21 16:17:01 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_show_options_all(struct cmd *self, struct cmdq_item *item,
|
2017-01-15 20:48:41 +00:00
|
|
|
struct options *oo)
|
2013-03-21 16:17:01 +00:00
|
|
|
{
|
2019-04-30 18:02:03 +00:00
|
|
|
struct options_entry *o;
|
|
|
|
struct options_array_item *a;
|
|
|
|
u_int idx;
|
|
|
|
const struct options_table_entry *oe;
|
2013-03-21 16:17:01 +00:00
|
|
|
|
2015-10-27 15:58:42 +00:00
|
|
|
o = options_first(oo);
|
|
|
|
while (o != NULL) {
|
2019-04-30 18:02:03 +00:00
|
|
|
oe = options_table_entry(o);
|
2019-04-26 11:38:51 +00:00
|
|
|
if ((self->entry != &cmd_show_hooks_entry &&
|
|
|
|
!args_has(self->args, 'H') &&
|
2019-04-30 18:02:03 +00:00
|
|
|
oe != NULL &&
|
|
|
|
(oe->flags & OPTIONS_TABLE_IS_HOOK)) ||
|
2019-04-26 11:38:51 +00:00
|
|
|
(self->entry == &cmd_show_hooks_entry &&
|
2019-04-30 18:02:03 +00:00
|
|
|
(oe == NULL ||
|
|
|
|
(~oe->flags & OPTIONS_TABLE_IS_HOOK)))) {
|
2019-04-26 11:38:51 +00:00
|
|
|
o = options_next(o);
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-18 11:58:40 +00:00
|
|
|
if (!options_isarray(o))
|
2017-01-15 20:48:41 +00:00
|
|
|
cmd_show_options_print(self, item, o, -1);
|
2019-04-26 11:38:51 +00:00
|
|
|
else if ((a = options_array_first(o)) == NULL) {
|
|
|
|
if (!args_has(self->args, 'v'))
|
|
|
|
cmdq_print(item, "%s", options_name(o));
|
|
|
|
} else {
|
2019-03-18 11:58:40 +00:00
|
|
|
while (a != NULL) {
|
|
|
|
idx = options_array_item_index(a);
|
2017-01-15 20:48:41 +00:00
|
|
|
cmd_show_options_print(self, item, o, idx);
|
2019-03-18 11:58:40 +00:00
|
|
|
a = options_array_next(a);
|
2017-01-15 20:48:41 +00:00
|
|
|
}
|
2012-02-25 12:57:42 +00:00
|
|
|
}
|
2015-10-27 15:58:42 +00:00
|
|
|
o = options_next(o);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
2012-07-11 07:10:15 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|