1*685b342aSnicm /* $OpenBSD: cmd-show-options.c,v 1.69 2022/02/14 09:10:48 nicm Exp $ */
2311827fbSnicm
3311827fbSnicm /*
498ca8272Snicm * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5311827fbSnicm *
6311827fbSnicm * Permission to use, copy, modify, and distribute this software for any
7311827fbSnicm * purpose with or without fee is hereby granted, provided that the above
8311827fbSnicm * copyright notice and this permission notice appear in all copies.
9311827fbSnicm *
10311827fbSnicm * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11311827fbSnicm * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12311827fbSnicm * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13311827fbSnicm * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14311827fbSnicm * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15311827fbSnicm * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16311827fbSnicm * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17311827fbSnicm */
18311827fbSnicm
19311827fbSnicm #include <sys/types.h>
20311827fbSnicm
21311827fbSnicm #include <stdlib.h>
22311827fbSnicm #include <string.h>
2358eb4b5dSnicm #include <vis.h>
24311827fbSnicm
25311827fbSnicm #include "tmux.h"
26311827fbSnicm
27311827fbSnicm /*
28311827fbSnicm * Show options.
29311827fbSnicm */
30311827fbSnicm
3168e0a7f2Snicm static enum cmd_retval cmd_show_options_exec(struct cmd *, struct cmdq_item *);
32311827fbSnicm
338aff522aSnicm static void cmd_show_options_print(struct cmd *, struct cmdq_item *,
34f8016429Snicm struct options_entry *, int, int);
3568e0a7f2Snicm static enum cmd_retval cmd_show_options_all(struct cmd *, struct cmdq_item *,
366e0f28f8Snicm int, struct options *);
379037b439Snicm
38311827fbSnicm const struct cmd_entry cmd_show_options_entry = {
39c057646bSnicm .name = "show-options",
40c057646bSnicm .alias = "show",
41c057646bSnicm
42a51dead1Snicm .args = { "AgHpqst:vw", 0, 1, NULL },
436e0f28f8Snicm .usage = "[-AgHpqsvw] " CMD_TARGET_PANE_USAGE " [option]",
44c057646bSnicm
456e0f28f8Snicm .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
468d471e80Snicm
477a61a8ddSnicm .flags = CMD_AFTERHOOK,
48c057646bSnicm .exec = cmd_show_options_exec
49311827fbSnicm };
50311827fbSnicm
5171730f13Snicm const struct cmd_entry cmd_show_window_options_entry = {
52c057646bSnicm .name = "show-window-options",
53c057646bSnicm .alias = "showw",
54c057646bSnicm
55a51dead1Snicm .args = { "gvt:", 0, 1, NULL },
56c057646bSnicm .usage = "[-gv] " CMD_TARGET_WINDOW_USAGE " [option]",
57c057646bSnicm
58bf0d297eSnicm .target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
598d471e80Snicm
607a61a8ddSnicm .flags = CMD_AFTERHOOK,
61c057646bSnicm .exec = cmd_show_options_exec
6271730f13Snicm };
6371730f13Snicm
64844b9093Snicm const struct cmd_entry cmd_show_hooks_entry = {
65844b9093Snicm .name = "show-hooks",
66844b9093Snicm .alias = NULL,
67844b9093Snicm
68a51dead1Snicm .args = { "gpt:w", 0, 1, NULL },
69b9cb9f01Snicm .usage = "[-gpw] " CMD_TARGET_PANE_USAGE,
70844b9093Snicm
71b9cb9f01Snicm .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
72844b9093Snicm
73844b9093Snicm .flags = CMD_AFTERHOOK,
74844b9093Snicm .exec = cmd_show_options_exec
75844b9093Snicm };
76844b9093Snicm
77dc1f0f5fSnicm static enum cmd_retval
cmd_show_options_exec(struct cmd * self,struct cmdq_item * item)7868e0a7f2Snicm cmd_show_options_exec(struct cmd *self, struct cmdq_item *item)
79311827fbSnicm {
8090d7ba38Snicm struct args *args = cmd_get_args(self);
81040343aeSnicm struct cmd_find_state *target = cmdq_get_target(item);
82311827fbSnicm struct options *oo;
838aff522aSnicm char *argument, *name = NULL, *cause;
846e0f28f8Snicm int window, idx, ambiguous, parent, scope;
858aff522aSnicm struct options_entry *o;
86311827fbSnicm
8790d7ba38Snicm window = (cmd_get_entry(self) == &cmd_show_window_options_entry);
88e8150bceSnicm
891693b10bSnicm if (args_count(args) == 0) {
90040343aeSnicm scope = options_scope_from_flags(args, window, target, &oo,
91040343aeSnicm &cause);
92d7b9896bSnicm if (scope == OPTIONS_TABLE_NONE) {
93d7b9896bSnicm if (args_has(args, 'q'))
94d7b9896bSnicm return (CMD_RETURN_NORMAL);
95d7b9896bSnicm cmdq_error(item, "%s", cause);
96d7b9896bSnicm free(cause);
97d7b9896bSnicm return (CMD_RETURN_ERROR);
98d7b9896bSnicm }
99f8016429Snicm return (cmd_show_options_all(self, item, scope, oo));
100d0dbe0f3Snicm }
1011693b10bSnicm argument = format_single_from_target(item, args_string(args, 0));
1028aff522aSnicm
1038aff522aSnicm name = options_match(argument, &idx, &ambiguous);
1048aff522aSnicm if (name == NULL) {
1058aff522aSnicm if (args_has(args, 'q'))
106*685b342aSnicm goto out;
1078aff522aSnicm if (ambiguous)
1088aff522aSnicm cmdq_error(item, "ambiguous option: %s", argument);
1098aff522aSnicm else
1108aff522aSnicm cmdq_error(item, "invalid option: %s", argument);
1118aff522aSnicm goto fail;
1128aff522aSnicm }
113040343aeSnicm scope = options_scope_from_name(args, window, name, target, &oo,
114040343aeSnicm &cause);
11558eb4b5dSnicm if (scope == OPTIONS_TABLE_NONE) {
1168aff522aSnicm if (args_has(args, 'q'))
117*685b342aSnicm goto out;
11858eb4b5dSnicm cmdq_error(item, "%s", cause);
11958eb4b5dSnicm free(cause);
1208aff522aSnicm goto fail;
121311827fbSnicm }
1228aff522aSnicm o = options_get_only(oo, name);
123f8016429Snicm if (args_has(args, 'A') && o == NULL) {
124f8016429Snicm o = options_get(oo, name);
125f8016429Snicm parent = 1;
126f8016429Snicm } else
127f8016429Snicm parent = 0;
1288aff522aSnicm if (o != NULL)
129f8016429Snicm cmd_show_options_print(self, item, o, idx, parent);
1307fb9f20dSnicm else if (*name == '@') {
1317fb9f20dSnicm if (args_has(args, 'q'))
132*685b342aSnicm goto out;
1337fb9f20dSnicm cmdq_error(item, "invalid option: %s", argument);
1347fb9f20dSnicm goto fail;
1357fb9f20dSnicm }
136311827fbSnicm
137*685b342aSnicm out:
1388aff522aSnicm free(name);
1398aff522aSnicm free(argument);
1408aff522aSnicm return (CMD_RETURN_NORMAL);
1418aff522aSnicm
1428aff522aSnicm fail:
1438aff522aSnicm free(name);
1448aff522aSnicm free(argument);
1458aff522aSnicm return (CMD_RETURN_ERROR);
14658eb4b5dSnicm }
14758eb4b5dSnicm
14858eb4b5dSnicm static void
cmd_show_options_print(struct cmd * self,struct cmdq_item * item,struct options_entry * o,int idx,int parent)14958eb4b5dSnicm cmd_show_options_print(struct cmd *self, struct cmdq_item *item,
150f8016429Snicm struct options_entry *o, int idx, int parent)
15158eb4b5dSnicm {
15290d7ba38Snicm struct args *args = cmd_get_args(self);
15339052edfSnicm struct options_array_item *a;
154844b9093Snicm const char *name = options_name(o);
155844b9093Snicm char *value, *tmp = NULL, *escaped;
15658eb4b5dSnicm
15758eb4b5dSnicm if (idx != -1) {
158844b9093Snicm xasprintf(&tmp, "%s[%d]", name, idx);
15958eb4b5dSnicm name = tmp;
16058eb4b5dSnicm } else {
16167c16a7cSnicm if (options_is_array(o)) {
16239052edfSnicm a = options_array_first(o);
163844b9093Snicm if (a == NULL) {
16490d7ba38Snicm if (!args_has(args, 'v'))
165844b9093Snicm cmdq_print(item, "%s", name);
166844b9093Snicm return;
167844b9093Snicm }
16839052edfSnicm while (a != NULL) {
16939052edfSnicm idx = options_array_item_index(a);
170f8016429Snicm cmd_show_options_print(self, item, o, idx,
171f8016429Snicm parent);
17239052edfSnicm a = options_array_next(a);
1732f0d274aSnicm }
1742f0d274aSnicm return;
1752f0d274aSnicm }
17658eb4b5dSnicm }
17758eb4b5dSnicm
17867c16a7cSnicm value = options_to_string(o, idx, 0);
17990d7ba38Snicm if (args_has(args, 'v'))
18058eb4b5dSnicm cmdq_print(item, "%s", value);
18167c16a7cSnicm else if (options_is_string(o)) {
1829d1bb104Snicm escaped = args_escape(value);
183f8016429Snicm if (parent)
184f8016429Snicm cmdq_print(item, "%s* %s", name, escaped);
185f8016429Snicm else
1869d1bb104Snicm cmdq_print(item, "%s %s", name, escaped);
18758eb4b5dSnicm free(escaped);
188f8016429Snicm } else {
189f8016429Snicm if (parent)
190f8016429Snicm cmdq_print(item, "%s* %s", name, value);
191f8016429Snicm else
19258eb4b5dSnicm cmdq_print(item, "%s %s", name, value);
193f8016429Snicm }
19411897870Snicm free(value);
19558eb4b5dSnicm
19658eb4b5dSnicm free(tmp);
1979037b439Snicm }
1989037b439Snicm
199dc1f0f5fSnicm static enum cmd_retval
cmd_show_options_all(struct cmd * self,struct cmdq_item * item,int scope,struct options * oo)2006e0f28f8Snicm cmd_show_options_all(struct cmd *self, struct cmdq_item *item, int scope,
2016e0f28f8Snicm struct options *oo)
2029037b439Snicm {
20390d7ba38Snicm struct args *args = cmd_get_args(self);
204f8016429Snicm const struct options_table_entry *oe;
20558900c88Snicm struct options_entry *o;
20639052edfSnicm struct options_array_item *a;
2076e0f28f8Snicm const char *name;
20839052edfSnicm u_int idx;
209f8016429Snicm int parent;
2109037b439Snicm
2113d708f80Snicm if (cmd_get_entry(self) != &cmd_show_hooks_entry) {
212c63816f1Snicm o = options_first(oo);
213c63816f1Snicm while (o != NULL) {
214c63816f1Snicm if (options_table_entry(o) == NULL)
215c63816f1Snicm cmd_show_options_print(self, item, o, -1, 0);
216c63816f1Snicm o = options_next(o);
217c63816f1Snicm }
2183d708f80Snicm }
219f8016429Snicm for (oe = options_table; oe->name != NULL; oe++) {
2206e0f28f8Snicm if (~oe->scope & scope)
221f8016429Snicm continue;
222f8016429Snicm
22390d7ba38Snicm if ((cmd_get_entry(self) != &cmd_show_hooks_entry &&
22490d7ba38Snicm !args_has(args, 'H') &&
22503097ba5Snicm (oe->flags & OPTIONS_TABLE_IS_HOOK)) ||
22690d7ba38Snicm (cmd_get_entry(self) == &cmd_show_hooks_entry &&
2277d64d0d8Snicm (~oe->flags & OPTIONS_TABLE_IS_HOOK)))
228844b9093Snicm continue;
229f8016429Snicm
230f8016429Snicm o = options_get_only(oo, oe->name);
231f8016429Snicm if (o == NULL) {
23290d7ba38Snicm if (!args_has(args, 'A'))
233f8016429Snicm continue;
234f8016429Snicm o = options_get(oo, oe->name);
235f8016429Snicm if (o == NULL)
236f8016429Snicm continue;
237f8016429Snicm parent = 1;
238f8016429Snicm } else
239f8016429Snicm parent = 0;
240f8016429Snicm
24167c16a7cSnicm if (!options_is_array(o))
242f8016429Snicm cmd_show_options_print(self, item, o, -1, parent);
243844b9093Snicm else if ((a = options_array_first(o)) == NULL) {
24490d7ba38Snicm if (!args_has(args, 'v')) {
2456e0f28f8Snicm name = options_name(o);
246f8016429Snicm if (parent)
2476e0f28f8Snicm cmdq_print(item, "%s*", name);
248f8016429Snicm else
2496e0f28f8Snicm cmdq_print(item, "%s", name);
250f8016429Snicm }
251844b9093Snicm } else {
25239052edfSnicm while (a != NULL) {
25339052edfSnicm idx = options_array_item_index(a);
2546e0f28f8Snicm cmd_show_options_print(self, item, o, idx,
2556e0f28f8Snicm parent);
25639052edfSnicm a = options_array_next(a);
25758eb4b5dSnicm }
2589037b439Snicm }
2599037b439Snicm }
260a224d0d3Snicm return (CMD_RETURN_NORMAL);
261311827fbSnicm }
262