xref: /netbsd-src/external/bsd/tmux/dist/cmd-show-options.c (revision 46548964d21002ed96d2f9f7eeb6357c81c3425c)
15494e770Schristos /* $OpenBSD$ */
2698d5317Sjmmv 
3698d5317Sjmmv /*
4ed4e6cd4Schristos  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5698d5317Sjmmv  *
6698d5317Sjmmv  * Permission to use, copy, modify, and distribute this software for any
7698d5317Sjmmv  * purpose with or without fee is hereby granted, provided that the above
8698d5317Sjmmv  * copyright notice and this permission notice appear in all copies.
9698d5317Sjmmv  *
10698d5317Sjmmv  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11698d5317Sjmmv  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12698d5317Sjmmv  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13698d5317Sjmmv  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14698d5317Sjmmv  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15698d5317Sjmmv  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16698d5317Sjmmv  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17698d5317Sjmmv  */
18698d5317Sjmmv 
19698d5317Sjmmv #include <sys/types.h>
20698d5317Sjmmv 
21698d5317Sjmmv #include <stdlib.h>
22698d5317Sjmmv #include <string.h>
23698d5317Sjmmv 
24698d5317Sjmmv #include "tmux.h"
25698d5317Sjmmv 
26698d5317Sjmmv /*
27698d5317Sjmmv  * Show options.
28698d5317Sjmmv  */
29698d5317Sjmmv 
304e179ddaSchristos static enum cmd_retval	cmd_show_options_exec(struct cmd *, struct cmdq_item *);
31928fc495Schristos 
3230744affSchristos static void		cmd_show_options_print(struct cmd *, struct cmdq_item *,
3330744affSchristos 			    struct options_entry *, int, int);
344e179ddaSchristos static enum cmd_retval	cmd_show_options_all(struct cmd *, struct cmdq_item *,
3530744affSchristos 			    int, struct options *);
36698d5317Sjmmv 
37698d5317Sjmmv const struct cmd_entry cmd_show_options_entry = {
38ed4e6cd4Schristos 	.name = "show-options",
39ed4e6cd4Schristos 	.alias = "show",
40ed4e6cd4Schristos 
41*46548964Swiz 	.args = { "AgHpqst:vw", 0, 1, NULL },
4230744affSchristos 	.usage = "[-AgHpqsvw] " CMD_TARGET_PANE_USAGE " [option]",
43ed4e6cd4Schristos 
4430744affSchristos 	.target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
45ed4e6cd4Schristos 
464e179ddaSchristos 	.flags = CMD_AFTERHOOK,
47ed4e6cd4Schristos 	.exec = cmd_show_options_exec
48d530c4d0Sjmmv };
49d530c4d0Sjmmv 
50d530c4d0Sjmmv const struct cmd_entry cmd_show_window_options_entry = {
51ed4e6cd4Schristos 	.name = "show-window-options",
52ed4e6cd4Schristos 	.alias = "showw",
53ed4e6cd4Schristos 
54*46548964Swiz 	.args = { "gvt:", 0, 1, NULL },
55ed4e6cd4Schristos 	.usage = "[-gv] " CMD_TARGET_WINDOW_USAGE " [option]",
56ed4e6cd4Schristos 
57c9ad075bSchristos 	.target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
58ed4e6cd4Schristos 
594e179ddaSchristos 	.flags = CMD_AFTERHOOK,
60ed4e6cd4Schristos 	.exec = cmd_show_options_exec
61698d5317Sjmmv };
62698d5317Sjmmv 
6330744affSchristos const struct cmd_entry cmd_show_hooks_entry = {
6430744affSchristos 	.name = "show-hooks",
6530744affSchristos 	.alias = NULL,
6630744affSchristos 
67*46548964Swiz 	.args = { "gpt:w", 0, 1, NULL },
68e271dbb8Schristos 	.usage = "[-gpw] " CMD_TARGET_PANE_USAGE,
6930744affSchristos 
70e271dbb8Schristos 	.target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
7130744affSchristos 
7230744affSchristos 	.flags = CMD_AFTERHOOK,
7330744affSchristos 	.exec = cmd_show_options_exec
7430744affSchristos };
7530744affSchristos 
764e179ddaSchristos static enum cmd_retval
cmd_show_options_exec(struct cmd * self,struct cmdq_item * item)774e179ddaSchristos cmd_show_options_exec(struct cmd *self, struct cmdq_item *item)
78698d5317Sjmmv {
79e271dbb8Schristos 	struct args			*args = cmd_get_args(self);
80e271dbb8Schristos 	struct cmd_find_state		*target = cmdq_get_target(item);
81698d5317Sjmmv 	struct options			*oo;
8230744affSchristos 	char				*argument, *name = NULL, *cause;
8330744affSchristos 	int				 window, idx, ambiguous, parent, scope;
8430744affSchristos 	struct options_entry		*o;
85698d5317Sjmmv 
86e271dbb8Schristos 	window = (cmd_get_entry(self) == &cmd_show_window_options_entry);
8730744affSchristos 
88*46548964Swiz 	if (args_count(args) == 0) {
89e271dbb8Schristos 		scope = options_scope_from_flags(args, window, target, &oo,
90e271dbb8Schristos 		    &cause);
914e179ddaSchristos 		if (scope == OPTIONS_TABLE_NONE) {
9230744affSchristos 			if (args_has(args, 'q'))
9330744affSchristos 				return (CMD_RETURN_NORMAL);
944e179ddaSchristos 			cmdq_error(item, "%s", cause);
954e179ddaSchristos 			free(cause);
96928fc495Schristos 			return (CMD_RETURN_ERROR);
97698d5317Sjmmv 		}
9830744affSchristos 		return (cmd_show_options_all(self, item, scope, oo));
9930744affSchristos 	}
100*46548964Swiz 	argument = format_single_from_target(item, args_string(args, 0));
101698d5317Sjmmv 
10230744affSchristos 	name = options_match(argument, &idx, &ambiguous);
10330744affSchristos 	if (name == NULL) {
10430744affSchristos 		if (args_has(args, 'q'))
105*46548964Swiz 			goto out;
10630744affSchristos 		if (ambiguous)
10730744affSchristos 			cmdq_error(item, "ambiguous option: %s", argument);
108928fc495Schristos 		else
10930744affSchristos 			cmdq_error(item, "invalid option: %s", argument);
11030744affSchristos 		goto fail;
11130744affSchristos 	}
112e271dbb8Schristos 	scope = options_scope_from_name(args, window, name, target, &oo,
113e271dbb8Schristos 	    &cause);
11430744affSchristos 	if (scope == OPTIONS_TABLE_NONE) {
11530744affSchristos 		if (args_has(args, 'q'))
116*46548964Swiz 			goto out;
11730744affSchristos 		cmdq_error(item, "%s", cause);
11830744affSchristos 		free(cause);
11930744affSchristos 		goto fail;
12030744affSchristos 	}
12130744affSchristos 	o = options_get_only(oo, name);
12230744affSchristos 	if (args_has(args, 'A') && o == NULL) {
12330744affSchristos 		o = options_get(oo, name);
12430744affSchristos 		parent = 1;
12530744affSchristos 	} else
12630744affSchristos 		parent = 0;
12730744affSchristos 	if (o != NULL)
12830744affSchristos 		cmd_show_options_print(self, item, o, idx, parent);
129*46548964Swiz 	else if (*name == '@') {
130*46548964Swiz 		if (args_has(args, 'q'))
131*46548964Swiz 			goto out;
132*46548964Swiz 		cmdq_error(item, "invalid option: %s", argument);
133*46548964Swiz 		goto fail;
134*46548964Swiz 	}
13530744affSchristos 
136*46548964Swiz out:
13730744affSchristos 	free(name);
13830744affSchristos 	free(argument);
13930744affSchristos 	return (CMD_RETURN_NORMAL);
14030744affSchristos 
14130744affSchristos fail:
14230744affSchristos 	free(name);
14330744affSchristos 	free(argument);
14430744affSchristos 	return (CMD_RETURN_ERROR);
145928fc495Schristos }
146928fc495Schristos 
1474e179ddaSchristos static void
cmd_show_options_print(struct cmd * self,struct cmdq_item * item,struct options_entry * o,int idx,int parent)1484e179ddaSchristos cmd_show_options_print(struct cmd *self, struct cmdq_item *item,
14930744affSchristos     struct options_entry *o, int idx, int parent)
1504e179ddaSchristos {
151e271dbb8Schristos 	struct args			*args = cmd_get_args(self);
152ef36e747Schristos 	struct options_array_item	*a;
15330744affSchristos 	const char			*name = options_name(o);
15430744affSchristos 	char				*value, *tmp = NULL, *escaped;
1554e179ddaSchristos 
1564e179ddaSchristos 	if (idx != -1) {
15730744affSchristos 		xasprintf(&tmp, "%s[%d]", name, idx);
1584e179ddaSchristos 		name = tmp;
1594e179ddaSchristos 	} else {
160e271dbb8Schristos 		if (options_is_array(o)) {
161ef36e747Schristos 			a = options_array_first(o);
16230744affSchristos 			if (a == NULL) {
163e271dbb8Schristos 				if (!args_has(args, 'v'))
16430744affSchristos 					cmdq_print(item, "%s", name);
16530744affSchristos 				return;
16630744affSchristos 			}
167ef36e747Schristos 			while (a != NULL) {
168ef36e747Schristos 				idx = options_array_item_index(a);
16930744affSchristos 				cmd_show_options_print(self, item, o, idx,
17030744affSchristos 				    parent);
171ef36e747Schristos 				a = options_array_next(a);
1724e179ddaSchristos 			}
1734e179ddaSchristos 			return;
1744e179ddaSchristos 		}
1754e179ddaSchristos 	}
1764e179ddaSchristos 
177e271dbb8Schristos 	value = options_to_string(o, idx, 0);
178e271dbb8Schristos 	if (args_has(args, 'v'))
1794e179ddaSchristos 		cmdq_print(item, "%s", value);
180e271dbb8Schristos 	else if (options_is_string(o)) {
18130744affSchristos 		escaped = args_escape(value);
18230744affSchristos 		if (parent)
18330744affSchristos 			cmdq_print(item, "%s* %s", name, escaped);
18430744affSchristos 		else
18530744affSchristos 			cmdq_print(item, "%s %s", name, escaped);
1864e179ddaSchristos 		free(escaped);
18730744affSchristos 	} else {
18830744affSchristos 		if (parent)
18930744affSchristos 			cmdq_print(item, "%s* %s", name, value);
19030744affSchristos 		else
1914e179ddaSchristos 			cmdq_print(item, "%s %s", name, value);
19230744affSchristos 	}
19330744affSchristos 	free(value);
1944e179ddaSchristos 
1954e179ddaSchristos 	free(tmp);
1964e179ddaSchristos }
1974e179ddaSchristos 
1984e179ddaSchristos static enum cmd_retval
cmd_show_options_all(struct cmd * self,struct cmdq_item * item,int scope,struct options * oo)19930744affSchristos cmd_show_options_all(struct cmd *self, struct cmdq_item *item, int scope,
2004e179ddaSchristos     struct options *oo)
201928fc495Schristos {
202e271dbb8Schristos 	struct args				*args = cmd_get_args(self);
20330744affSchristos 	const struct options_table_entry	*oe;
204928fc495Schristos 	struct options_entry			*o;
205ef36e747Schristos 	struct options_array_item		*a;
20630744affSchristos 	const char				*name;
207ef36e747Schristos 	u_int					 idx;
20830744affSchristos 	int					 parent;
209928fc495Schristos 
210e271dbb8Schristos 	if (cmd_get_entry(self) != &cmd_show_hooks_entry) {
211ed4e6cd4Schristos 		o = options_first(oo);
212ed4e6cd4Schristos 		while (o != NULL) {
21330744affSchristos 			if (options_table_entry(o) == NULL)
21430744affSchristos 				cmd_show_options_print(self, item, o, -1, 0);
21530744affSchristos 			o = options_next(o);
21630744affSchristos 		}
217e271dbb8Schristos 	}
21830744affSchristos 	for (oe = options_table; oe->name != NULL; oe++) {
21930744affSchristos 		if (~oe->scope & scope)
22030744affSchristos 			continue;
22130744affSchristos 
222e271dbb8Schristos 		if ((cmd_get_entry(self) != &cmd_show_hooks_entry &&
223e271dbb8Schristos 		    !args_has(args, 'H') &&
22430744affSchristos 		    (oe->flags & OPTIONS_TABLE_IS_HOOK)) ||
225e271dbb8Schristos 		    (cmd_get_entry(self) == &cmd_show_hooks_entry &&
226e271dbb8Schristos 		    (~oe->flags & OPTIONS_TABLE_IS_HOOK)))
22730744affSchristos 			continue;
22830744affSchristos 
22930744affSchristos 		o = options_get_only(oo, oe->name);
23030744affSchristos 		if (o == NULL) {
231e271dbb8Schristos 			if (!args_has(args, 'A'))
23230744affSchristos 				continue;
23330744affSchristos 			o = options_get(oo, oe->name);
23430744affSchristos 			if (o == NULL)
23530744affSchristos 				continue;
23630744affSchristos 			parent = 1;
23730744affSchristos 		} else
23830744affSchristos 			parent = 0;
23930744affSchristos 
240e271dbb8Schristos 		if (!options_is_array(o))
24130744affSchristos 			cmd_show_options_print(self, item, o, -1, parent);
24230744affSchristos 		else if ((a = options_array_first(o)) == NULL) {
243e271dbb8Schristos 			if (!args_has(args, 'v')) {
24430744affSchristos 				name = options_name(o);
24530744affSchristos 				if (parent)
24630744affSchristos 					cmdq_print(item, "%s*", name);
24730744affSchristos 				else
24830744affSchristos 					cmdq_print(item, "%s", name);
24930744affSchristos 			}
25030744affSchristos 		} else {
251ef36e747Schristos 			while (a != NULL) {
252ef36e747Schristos 				idx = options_array_item_index(a);
25330744affSchristos 				cmd_show_options_print(self, item, o, idx,
25430744affSchristos 				    parent);
255ef36e747Schristos 				a = options_array_next(a);
2564e179ddaSchristos 			}
257928fc495Schristos 		}
258928fc495Schristos 	}
259928fc495Schristos 	return (CMD_RETURN_NORMAL);
260698d5317Sjmmv }
261