xref: /openbsd-src/usr.bin/tmux/cmd-show-options.c (revision 685b342a2af10dd5a23e00fc88fe08cdca1bbb56)
1 /* $OpenBSD: cmd-show-options.c,v 1.69 2022/02/14 09:10:48 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <stdlib.h>
22 #include <string.h>
23 #include <vis.h>
24 
25 #include "tmux.h"
26 
27 /*
28  * Show options.
29  */
30 
31 static enum cmd_retval	cmd_show_options_exec(struct cmd *, struct cmdq_item *);
32 
33 static void		cmd_show_options_print(struct cmd *, struct cmdq_item *,
34 			    struct options_entry *, int, int);
35 static enum cmd_retval	cmd_show_options_all(struct cmd *, struct cmdq_item *,
36 			    int, struct options *);
37 
38 const struct cmd_entry cmd_show_options_entry = {
39 	.name = "show-options",
40 	.alias = "show",
41 
42 	.args = { "AgHpqst:vw", 0, 1, NULL },
43 	.usage = "[-AgHpqsvw] " CMD_TARGET_PANE_USAGE " [option]",
44 
45 	.target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
46 
47 	.flags = CMD_AFTERHOOK,
48 	.exec = cmd_show_options_exec
49 };
50 
51 const struct cmd_entry cmd_show_window_options_entry = {
52 	.name = "show-window-options",
53 	.alias = "showw",
54 
55 	.args = { "gvt:", 0, 1, NULL },
56 	.usage = "[-gv] " CMD_TARGET_WINDOW_USAGE " [option]",
57 
58 	.target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
59 
60 	.flags = CMD_AFTERHOOK,
61 	.exec = cmd_show_options_exec
62 };
63 
64 const struct cmd_entry cmd_show_hooks_entry = {
65 	.name = "show-hooks",
66 	.alias = NULL,
67 
68 	.args = { "gpt:w", 0, 1, NULL },
69 	.usage = "[-gpw] " CMD_TARGET_PANE_USAGE,
70 
71 	.target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
72 
73 	.flags = CMD_AFTERHOOK,
74 	.exec = cmd_show_options_exec
75 };
76 
77 static enum cmd_retval
cmd_show_options_exec(struct cmd * self,struct cmdq_item * item)78 cmd_show_options_exec(struct cmd *self, struct cmdq_item *item)
79 {
80 	struct args			*args = cmd_get_args(self);
81 	struct cmd_find_state		*target = cmdq_get_target(item);
82 	struct options			*oo;
83 	char				*argument, *name = NULL, *cause;
84 	int				 window, idx, ambiguous, parent, scope;
85 	struct options_entry		*o;
86 
87 	window = (cmd_get_entry(self) == &cmd_show_window_options_entry);
88 
89 	if (args_count(args) == 0) {
90 		scope = options_scope_from_flags(args, window, target, &oo,
91 		    &cause);
92 		if (scope == OPTIONS_TABLE_NONE) {
93 			if (args_has(args, 'q'))
94 				return (CMD_RETURN_NORMAL);
95 			cmdq_error(item, "%s", cause);
96 			free(cause);
97 			return (CMD_RETURN_ERROR);
98 		}
99 		return (cmd_show_options_all(self, item, scope, oo));
100 	}
101 	argument = format_single_from_target(item, args_string(args, 0));
102 
103 	name = options_match(argument, &idx, &ambiguous);
104 	if (name == NULL) {
105 		if (args_has(args, 'q'))
106 			goto out;
107 		if (ambiguous)
108 			cmdq_error(item, "ambiguous option: %s", argument);
109 		else
110 			cmdq_error(item, "invalid option: %s", argument);
111 		goto fail;
112 	}
113 	scope = options_scope_from_name(args, window, name, target, &oo,
114 	    &cause);
115 	if (scope == OPTIONS_TABLE_NONE) {
116 		if (args_has(args, 'q'))
117 			goto out;
118 		cmdq_error(item, "%s", cause);
119 		free(cause);
120 		goto fail;
121 	}
122 	o = options_get_only(oo, name);
123 	if (args_has(args, 'A') && o == NULL) {
124 		o = options_get(oo, name);
125 		parent = 1;
126 	} else
127 		parent = 0;
128 	if (o != NULL)
129 		cmd_show_options_print(self, item, o, idx, parent);
130 	else if (*name == '@') {
131 		if (args_has(args, 'q'))
132 			goto out;
133 		cmdq_error(item, "invalid option: %s", argument);
134 		goto fail;
135 	}
136 
137 out:
138 	free(name);
139 	free(argument);
140 	return (CMD_RETURN_NORMAL);
141 
142 fail:
143 	free(name);
144 	free(argument);
145 	return (CMD_RETURN_ERROR);
146 }
147 
148 static void
cmd_show_options_print(struct cmd * self,struct cmdq_item * item,struct options_entry * o,int idx,int parent)149 cmd_show_options_print(struct cmd *self, struct cmdq_item *item,
150     struct options_entry *o, int idx, int parent)
151 {
152 	struct args			*args = cmd_get_args(self);
153 	struct options_array_item	*a;
154 	const char			*name = options_name(o);
155 	char				*value, *tmp = NULL, *escaped;
156 
157 	if (idx != -1) {
158 		xasprintf(&tmp, "%s[%d]", name, idx);
159 		name = tmp;
160 	} else {
161 		if (options_is_array(o)) {
162 			a = options_array_first(o);
163 			if (a == NULL) {
164 				if (!args_has(args, 'v'))
165 					cmdq_print(item, "%s", name);
166 				return;
167 			}
168 			while (a != NULL) {
169 				idx = options_array_item_index(a);
170 				cmd_show_options_print(self, item, o, idx,
171 				    parent);
172 				a = options_array_next(a);
173 			}
174 			return;
175 		}
176 	}
177 
178 	value = options_to_string(o, idx, 0);
179 	if (args_has(args, 'v'))
180 		cmdq_print(item, "%s", value);
181 	else if (options_is_string(o)) {
182 		escaped = args_escape(value);
183 		if (parent)
184 			cmdq_print(item, "%s* %s", name, escaped);
185 		else
186 			cmdq_print(item, "%s %s", name, escaped);
187 		free(escaped);
188 	} else {
189 		if (parent)
190 			cmdq_print(item, "%s* %s", name, value);
191 		else
192 			cmdq_print(item, "%s %s", name, value);
193 	}
194 	free(value);
195 
196 	free(tmp);
197 }
198 
199 static enum cmd_retval
cmd_show_options_all(struct cmd * self,struct cmdq_item * item,int scope,struct options * oo)200 cmd_show_options_all(struct cmd *self, struct cmdq_item *item, int scope,
201     struct options *oo)
202 {
203 	struct args				*args = cmd_get_args(self);
204 	const struct options_table_entry	*oe;
205 	struct options_entry			*o;
206 	struct options_array_item		*a;
207 	const char				*name;
208 	u_int					 idx;
209 	int					 parent;
210 
211 	if (cmd_get_entry(self) != &cmd_show_hooks_entry) {
212 		o = options_first(oo);
213 		while (o != NULL) {
214 			if (options_table_entry(o) == NULL)
215 				cmd_show_options_print(self, item, o, -1, 0);
216 			o = options_next(o);
217 		}
218 	}
219 	for (oe = options_table; oe->name != NULL; oe++) {
220 		if (~oe->scope & scope)
221 			continue;
222 
223 		if ((cmd_get_entry(self) != &cmd_show_hooks_entry &&
224 		    !args_has(args, 'H') &&
225 		    (oe->flags & OPTIONS_TABLE_IS_HOOK)) ||
226 		    (cmd_get_entry(self) == &cmd_show_hooks_entry &&
227 		    (~oe->flags & OPTIONS_TABLE_IS_HOOK)))
228 			continue;
229 
230 		o = options_get_only(oo, oe->name);
231 		if (o == NULL) {
232 			if (!args_has(args, 'A'))
233 				continue;
234 			o = options_get(oo, oe->name);
235 			if (o == NULL)
236 				continue;
237 			parent = 1;
238 		} else
239 			parent = 0;
240 
241 		if (!options_is_array(o))
242 			cmd_show_options_print(self, item, o, -1, parent);
243 		else if ((a = options_array_first(o)) == NULL) {
244 			if (!args_has(args, 'v')) {
245 				name = options_name(o);
246 				if (parent)
247 					cmdq_print(item, "%s*", name);
248 				else
249 					cmdq_print(item, "%s", name);
250 			}
251 		} else {
252 			while (a != NULL) {
253 				idx = options_array_item_index(a);
254 				cmd_show_options_print(self, item, o, idx,
255 				    parent);
256 				a = options_array_next(a);
257 			}
258 		}
259 	}
260 	return (CMD_RETURN_NORMAL);
261 }
262