/src/tmux/cmd-show-options.c
Line | Count | Source |
1 | | /* $OpenBSD: cmd-show-options.c,v 1.77 2026/10/09 13:12:14 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 | | |
24 | | #include "tmux.h" |
25 | | |
26 | | /* |
27 | | * Show options. |
28 | | */ |
29 | | |
30 | | #define SHOW_OPTIONS_TEMPLATE \ |
31 | 0 | "#{?option_value_only," \ |
32 | 0 | "#{option_value}," \ |
33 | 0 | "#{option_name}#{?option_has_array_key," \ |
34 | 0 | "[#{option_array_key}],}" \ |
35 | 0 | "#{?option_is_parent,*,}" \ |
36 | 0 | "#{?option_has_value, " \ |
37 | 0 | "#{?option_is_string,#{q/a:option_value},#{option_value}},}}" |
38 | | #define SHOW_HOOKS_MONITOR_TEMPLATE \ |
39 | 0 | "#{option_name}:#{hook_monitor_target}:#{hook_monitor_format}" |
40 | | |
41 | | static enum cmd_retval cmd_show_options_exec(struct cmd *, struct cmdq_item *); |
42 | | |
43 | | static void cmd_show_options_print(struct cmd *, struct cmdq_item *, |
44 | | struct options_entry *, const char *, int); |
45 | | static void cmd_show_hooks_print_monitor(struct cmd *, |
46 | | struct cmdq_item *, struct options_entry *); |
47 | | static enum cmd_retval cmd_show_options_all(struct cmd *, struct cmdq_item *, |
48 | | int, struct options *); |
49 | | |
50 | | const struct cmd_entry cmd_show_options_entry = { |
51 | | .name = "show-options", |
52 | | .description = "Show server, session, or pane options.", |
53 | | .alias = "show", |
54 | | |
55 | | .args = { "AgF:Hpqst:vw", 0, 1, NULL }, |
56 | | .usage = "[-AgHpqsvw] [-F format] " CMD_TARGET_PANE_USAGE " [option]", |
57 | | |
58 | | .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL }, |
59 | | |
60 | | .flags = CMD_AFTERHOOK, |
61 | | .exec = cmd_show_options_exec |
62 | | }; |
63 | | |
64 | | const struct cmd_entry cmd_show_window_options_entry = { |
65 | | .name = "show-window-options", |
66 | | .description = "Show window options.", |
67 | | .alias = "showw", |
68 | | |
69 | | .args = { "F:gvt:", 0, 1, NULL }, |
70 | | .usage = "[-gv] [-F format] " CMD_TARGET_WINDOW_USAGE " [option]", |
71 | | |
72 | | .target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL }, |
73 | | |
74 | | .flags = CMD_AFTERHOOK, |
75 | | .exec = cmd_show_options_exec |
76 | | }; |
77 | | |
78 | | const struct cmd_entry cmd_show_hooks_entry = { |
79 | | .name = "show-hooks", |
80 | | .description = "Show hooks.", |
81 | | .alias = NULL, |
82 | | |
83 | | .args = { "BF:gpt:w", 0, 1, NULL }, |
84 | | .usage = "[-Bgpw] [-F format] " CMD_TARGET_PANE_USAGE " [hook]", |
85 | | |
86 | | .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL }, |
87 | | |
88 | | .flags = CMD_AFTERHOOK, |
89 | | .exec = cmd_show_options_exec |
90 | | }; |
91 | | |
92 | | static enum cmd_retval |
93 | | cmd_show_options_exec(struct cmd *self, struct cmdq_item *item) |
94 | 0 | { |
95 | 0 | struct args *args = cmd_get_args(self); |
96 | 0 | struct cmd_find_state *target = cmdq_get_target(item); |
97 | 0 | struct options *oo; |
98 | 0 | char *argument, *name = NULL, *cause; |
99 | 0 | char *array_key = NULL; |
100 | 0 | int window, ambiguous, parent, print_parent, scope; |
101 | 0 | struct options_entry *o; |
102 | |
|
103 | 0 | window = (cmd_get_entry(self) == &cmd_show_window_options_entry); |
104 | |
|
105 | 0 | if (args_count(args) == 0) { |
106 | 0 | scope = options_scope_from_flags(args, window, target, &oo, |
107 | 0 | &cause); |
108 | 0 | if (scope == OPTIONS_TABLE_NONE) { |
109 | 0 | if (args_has(args, 'q')) |
110 | 0 | return (CMD_RETURN_NORMAL); |
111 | 0 | cmdq_error(item, "%s", cause); |
112 | 0 | free(cause); |
113 | 0 | return (CMD_RETURN_ERROR); |
114 | 0 | } |
115 | 0 | if (cmd_get_entry(self) == &cmd_show_hooks_entry && |
116 | 0 | args_has(args, 'B')) { |
117 | 0 | o = options_first(oo); |
118 | 0 | while (o != NULL) { |
119 | 0 | cmd_show_hooks_print_monitor(self, item, o); |
120 | 0 | o = options_next(o); |
121 | 0 | } |
122 | 0 | return (CMD_RETURN_NORMAL); |
123 | 0 | } |
124 | 0 | return (cmd_show_options_all(self, item, scope, oo)); |
125 | 0 | } |
126 | 0 | argument = format_single_from_target(item, args_string(args, 0)); |
127 | |
|
128 | 0 | name = options_match(argument, &array_key, &ambiguous); |
129 | 0 | if (name == NULL) { |
130 | 0 | if (args_has(args, 'q')) |
131 | 0 | goto out; |
132 | 0 | if (ambiguous) |
133 | 0 | cmdq_error(item, "ambiguous option: %s", argument); |
134 | 0 | else |
135 | 0 | cmdq_error(item, "invalid option: %s", argument); |
136 | 0 | goto fail; |
137 | 0 | } |
138 | 0 | scope = options_scope_from_name(args, window, name, target, &oo, |
139 | 0 | &cause); |
140 | 0 | if (scope == OPTIONS_TABLE_NONE) { |
141 | 0 | if (args_has(args, 'q')) |
142 | 0 | goto out; |
143 | 0 | cmdq_error(item, "%s", cause); |
144 | 0 | free(cause); |
145 | 0 | goto fail; |
146 | 0 | } |
147 | 0 | o = options_get_only(oo, name); |
148 | 0 | if (args_has(args, 'A') && o == NULL) { |
149 | 0 | o = options_get(oo, name); |
150 | 0 | parent = 1; |
151 | 0 | } else |
152 | 0 | parent = 0; |
153 | 0 | if (o != NULL) { |
154 | 0 | if (cmd_get_entry(self) == &cmd_show_hooks_entry && |
155 | 0 | args_has(args, 'B')) |
156 | 0 | cmd_show_hooks_print_monitor(self, item, o); |
157 | 0 | else { |
158 | 0 | print_parent = parent; |
159 | 0 | if (array_key == NULL && options_is_array(o) && |
160 | 0 | options_array_first(o) == NULL) { |
161 | 0 | print_parent = 0; |
162 | 0 | } |
163 | 0 | cmd_show_options_print(self, item, o, array_key, |
164 | 0 | print_parent); |
165 | 0 | } |
166 | 0 | } else if (*name == '@') { |
167 | 0 | if (args_has(args, 'q')) |
168 | 0 | goto out; |
169 | 0 | cmdq_error(item, "invalid option: %s", argument); |
170 | 0 | goto fail; |
171 | 0 | } |
172 | | |
173 | 0 | out: |
174 | 0 | free(name); |
175 | 0 | free(array_key); |
176 | 0 | free(argument); |
177 | 0 | return (CMD_RETURN_NORMAL); |
178 | | |
179 | 0 | fail: |
180 | 0 | free(name); |
181 | 0 | free(array_key); |
182 | 0 | free(argument); |
183 | 0 | return (CMD_RETURN_ERROR); |
184 | 0 | } |
185 | | |
186 | | static void |
187 | | cmd_show_options_print(struct cmd *self, struct cmdq_item *item, |
188 | | struct options_entry *o, const char *array_key, int parent) |
189 | 0 | { |
190 | 0 | struct args *args = cmd_get_args(self); |
191 | 0 | struct options_array_item *a; |
192 | 0 | struct format_tree *ft; |
193 | 0 | const char *name = options_name(o); |
194 | 0 | const char *template = args_get(args, 'F'); |
195 | 0 | char *value, *line; |
196 | 0 | struct timeval tv = { 0 }; |
197 | 0 | u_int fire_count; |
198 | 0 | time_t fire_time; |
199 | 0 | int is_hook = 0, is_user = 0; |
200 | 0 | int has_value = 1; |
201 | 0 | const struct options_table_entry *oe = options_table_entry(o); |
202 | |
|
203 | 0 | if (array_key != NULL) |
204 | 0 | value = options_to_string(o, array_key, 0); |
205 | 0 | else if (options_is_array(o)) { |
206 | 0 | a = options_array_first(o); |
207 | 0 | if (a != NULL) { |
208 | 0 | while (a != NULL) { |
209 | 0 | array_key = options_array_item_key(a); |
210 | 0 | cmd_show_options_print(self, item, o, array_key, |
211 | 0 | parent); |
212 | 0 | a = options_array_next(a); |
213 | 0 | } |
214 | 0 | return; |
215 | 0 | } |
216 | 0 | if (template == NULL && args_has(args, 'v')) |
217 | 0 | return; |
218 | 0 | value = xstrdup(""); |
219 | 0 | has_value = 0; |
220 | 0 | } else |
221 | 0 | value = options_to_string(o, NULL, 0); |
222 | | |
223 | 0 | if (template == NULL) |
224 | 0 | template = SHOW_OPTIONS_TEMPLATE; |
225 | |
|
226 | 0 | if (oe != NULL && (oe->flags & OPTIONS_TABLE_IS_HOOK)) |
227 | 0 | is_hook = 1; |
228 | 0 | else if (oe == NULL) |
229 | 0 | is_user = 1; |
230 | |
|
231 | 0 | ft = format_create_from_target(item); |
232 | 0 | format_add(ft, "option_name", "%s", name); |
233 | 0 | format_add(ft, "option_value", "%s", value); |
234 | 0 | format_add(ft, "option_value_only", "%d", args_has(args, 'v')); |
235 | 0 | format_add(ft, "option_is_parent", "%d", parent); |
236 | 0 | format_add(ft, "option_is_array", "%d", options_is_array(o)); |
237 | 0 | format_add(ft, "option_is_string", "%d", options_is_string(o)); |
238 | 0 | format_add(ft, "option_is_hook", "%d", is_hook); |
239 | 0 | format_add(ft, "option_is_user", "%d", is_user); |
240 | 0 | format_add(ft, "option_has_value", "%d", has_value); |
241 | 0 | if (cmd_get_entry(self) == &cmd_show_hooks_entry) { |
242 | 0 | fire_count = options_get_fire_count(o); |
243 | 0 | format_add(ft, "hook_fire_count", "%u", fire_count); |
244 | |
|
245 | 0 | fire_time = options_get_fire_time(o); |
246 | 0 | if (fire_time != 0) { |
247 | 0 | tv.tv_sec = fire_time; |
248 | 0 | format_add_tv(ft, "hook_fire_time", &tv); |
249 | 0 | } |
250 | 0 | } |
251 | 0 | if (array_key != NULL) { |
252 | 0 | format_add(ft, "option_array_key", "%s", array_key); |
253 | 0 | format_add(ft, "option_has_array_key", "1"); |
254 | 0 | } else { |
255 | 0 | format_add(ft, "option_array_key", "%s", ""); |
256 | 0 | format_add(ft, "option_has_array_key", "0"); |
257 | 0 | } |
258 | 0 | line = format_expand(ft, template); |
259 | 0 | format_free(ft); |
260 | |
|
261 | 0 | cmdq_print(item, "%s", line); |
262 | 0 | free(line); |
263 | 0 | free(value); |
264 | 0 | } |
265 | | |
266 | | static void |
267 | | cmd_show_hooks_print_monitor(struct cmd *self, struct cmdq_item *item, |
268 | | struct options_entry *o) |
269 | 0 | { |
270 | 0 | struct args *args = cmd_get_args(self); |
271 | 0 | struct format_tree *ft; |
272 | 0 | enum monitor_type type; |
273 | 0 | const char *template = args_get(args, 'F'), *format; |
274 | 0 | char *value, *target, *line; |
275 | 0 | struct timeval tv = { 0 }; |
276 | 0 | u_int fire_count; |
277 | 0 | time_t fire_time; |
278 | 0 | int id; |
279 | |
|
280 | 0 | value = hooks_monitor_to_string(o); |
281 | 0 | if (value == NULL) |
282 | 0 | return; |
283 | 0 | if (!hooks_monitor_get(o, &type, &id, &format)) { |
284 | 0 | free(value); |
285 | 0 | return; |
286 | 0 | } |
287 | 0 | if (template == NULL) |
288 | 0 | template = SHOW_HOOKS_MONITOR_TEMPLATE; |
289 | |
|
290 | 0 | switch (type) { |
291 | 0 | case MONITOR_SESSION: |
292 | 0 | target = xstrdup(""); |
293 | 0 | break; |
294 | 0 | case MONITOR_PANE: |
295 | 0 | xasprintf(&target, "%%%d", id); |
296 | 0 | break; |
297 | 0 | case MONITOR_ALL_PANES: |
298 | 0 | target = xstrdup("%*"); |
299 | 0 | break; |
300 | 0 | case MONITOR_WINDOW: |
301 | 0 | xasprintf(&target, "@%d", id); |
302 | 0 | break; |
303 | 0 | case MONITOR_ALL_WINDOWS: |
304 | 0 | target = xstrdup("@*"); |
305 | 0 | break; |
306 | 0 | } |
307 | | |
308 | 0 | ft = format_create_from_target(item); |
309 | 0 | format_add(ft, "option_name", "%s", options_name(o)); |
310 | 0 | format_add(ft, "option_value", "%s", value); |
311 | 0 | format_add(ft, "option_value_only", "%d", 0); |
312 | 0 | format_add(ft, "option_is_parent", "%d", 0); |
313 | 0 | format_add(ft, "option_is_array", "%d", 0); |
314 | 0 | format_add(ft, "option_is_string", "%d", 1); |
315 | 0 | format_add(ft, "option_is_hook", "%d", 1); |
316 | 0 | format_add(ft, "option_is_user", "%d", 1); |
317 | 0 | format_add(ft, "option_has_value", "%d", 1); |
318 | 0 | format_add(ft, "option_array_key", "%s", ""); |
319 | 0 | format_add(ft, "option_has_array_key", "0"); |
320 | |
|
321 | 0 | format_add(ft, "hook_monitor_target", "%s", target); |
322 | 0 | format_add(ft, "hook_monitor_format", "%s", format); |
323 | |
|
324 | 0 | fire_count = hooks_monitor_get_fire_count(o); |
325 | 0 | format_add(ft, "hook_fire_count", "%u", fire_count); |
326 | |
|
327 | 0 | fire_time = hooks_monitor_get_fire_time(o); |
328 | 0 | if (fire_time != 0) { |
329 | 0 | tv.tv_sec = fire_time; |
330 | 0 | format_add_tv(ft, "hook_fire_time", &tv); |
331 | 0 | } |
332 | |
|
333 | 0 | line = format_expand(ft, template); |
334 | 0 | format_free(ft); |
335 | |
|
336 | 0 | cmdq_print(item, "%s", line); |
337 | 0 | free(line); |
338 | 0 | free(target); |
339 | 0 | free(value); |
340 | 0 | } |
341 | | |
342 | | static enum cmd_retval |
343 | | cmd_show_options_all(struct cmd *self, struct cmdq_item *item, int scope, |
344 | | struct options *oo) |
345 | 0 | { |
346 | 0 | struct args *args = cmd_get_args(self); |
347 | 0 | const struct options_table_entry *oe; |
348 | 0 | struct options_entry *o; |
349 | 0 | const char *name; |
350 | 0 | int parent, is_user_hook; |
351 | |
|
352 | 0 | o = options_first(oo); |
353 | 0 | while (o != NULL) { |
354 | 0 | if (options_table_entry(o) == NULL) { |
355 | 0 | name = options_name(o); |
356 | 0 | is_user_hook = 0; |
357 | 0 | if (*name == '@') { |
358 | 0 | if (hooks_is_event(name) || |
359 | 0 | options_get_monitor_data(o) != NULL) { |
360 | 0 | is_user_hook = 1; |
361 | 0 | } |
362 | 0 | } |
363 | 0 | if (cmd_get_entry(self) != &cmd_show_hooks_entry) { |
364 | 0 | if (!is_user_hook || args_has(args, 'H')) { |
365 | 0 | cmd_show_options_print(self, item, o, |
366 | 0 | NULL, 0); |
367 | 0 | } |
368 | 0 | } else if (is_user_hook) |
369 | 0 | cmd_show_options_print(self, item, o, NULL, 0); |
370 | 0 | } |
371 | 0 | o = options_next(o); |
372 | 0 | } |
373 | 0 | for (oe = options_table; oe->name != NULL; oe++) { |
374 | 0 | if (~oe->scope & scope) |
375 | 0 | continue; |
376 | | |
377 | 0 | if ((cmd_get_entry(self) != &cmd_show_hooks_entry && |
378 | 0 | !args_has(args, 'H') && |
379 | 0 | (oe->flags & OPTIONS_TABLE_IS_HOOK)) || |
380 | 0 | (cmd_get_entry(self) == &cmd_show_hooks_entry && |
381 | 0 | (~oe->flags & OPTIONS_TABLE_IS_HOOK))) |
382 | 0 | continue; |
383 | | |
384 | 0 | o = options_get_only(oo, oe->name); |
385 | 0 | if (o == NULL) { |
386 | 0 | if (!args_has(args, 'A')) |
387 | 0 | continue; |
388 | 0 | o = options_get(oo, oe->name); |
389 | 0 | if (o == NULL) |
390 | 0 | continue; |
391 | 0 | parent = 1; |
392 | 0 | } else |
393 | 0 | parent = 0; |
394 | | |
395 | 0 | cmd_show_options_print(self, item, o, NULL, parent); |
396 | 0 | } |
397 | 0 | return (CMD_RETURN_NORMAL); |
398 | 0 | } |