4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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.
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.
19 #include <sys/types.h>
31 static enum cmd_retval
cmd_show_options_exec(struct cmd
*, struct cmdq_item
*);
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
*);
38 const struct cmd_entry cmd_show_options_entry
= {
39 .name
= "show-options",
42 .args
= { "AgHpqst:vw", 0, 1, NULL
},
43 .usage
= "[-AgHpqsvw] " CMD_TARGET_PANE_USAGE
" [option]",
45 .target
= { 't', CMD_FIND_PANE
, CMD_FIND_CANFAIL
},
47 .flags
= CMD_AFTERHOOK
,
48 .exec
= cmd_show_options_exec
51 const struct cmd_entry cmd_show_window_options_entry
= {
52 .name
= "show-window-options",
55 .args
= { "gvt:", 0, 1, NULL
},
56 .usage
= "[-gv] " CMD_TARGET_WINDOW_USAGE
" [option]",
58 .target
= { 't', CMD_FIND_WINDOW
, CMD_FIND_CANFAIL
},
60 .flags
= CMD_AFTERHOOK
,
61 .exec
= cmd_show_options_exec
64 const struct cmd_entry cmd_show_hooks_entry
= {
68 .args
= { "gpt:w", 0, 1, NULL
},
69 .usage
= "[-gpw] " CMD_TARGET_PANE_USAGE
,
71 .target
= { 't', CMD_FIND_PANE
, CMD_FIND_CANFAIL
},
73 .flags
= CMD_AFTERHOOK
,
74 .exec
= cmd_show_options_exec
77 static enum cmd_retval
78 cmd_show_options_exec(struct cmd
*self
, struct cmdq_item
*item
)
80 struct args
*args
= cmd_get_args(self
);
81 struct cmd_find_state
*target
= cmdq_get_target(item
);
83 char *argument
, *name
= NULL
, *cause
;
84 int window
, idx
, ambiguous
, parent
, scope
;
85 struct options_entry
*o
;
87 window
= (cmd_get_entry(self
) == &cmd_show_window_options_entry
);
89 if (args_count(args
) == 0) {
90 scope
= options_scope_from_flags(args
, window
, target
, &oo
,
92 if (scope
== OPTIONS_TABLE_NONE
) {
93 if (args_has(args
, 'q'))
94 return (CMD_RETURN_NORMAL
);
95 cmdq_error(item
, "%s", cause
);
97 return (CMD_RETURN_ERROR
);
99 return (cmd_show_options_all(self
, item
, scope
, oo
));
101 argument
= format_single_from_target(item
, args_string(args
, 0));
103 name
= options_match(argument
, &idx
, &ambiguous
);
105 if (args_has(args
, 'q'))
108 cmdq_error(item
, "ambiguous option: %s", argument
);
110 cmdq_error(item
, "invalid option: %s", argument
);
113 scope
= options_scope_from_name(args
, window
, name
, target
, &oo
,
115 if (scope
== OPTIONS_TABLE_NONE
) {
116 if (args_has(args
, 'q'))
118 cmdq_error(item
, "%s", cause
);
122 o
= options_get_only(oo
, name
);
123 if (args_has(args
, 'A') && o
== NULL
) {
124 o
= options_get(oo
, name
);
129 cmd_show_options_print(self
, item
, o
, idx
, parent
);
133 return (CMD_RETURN_NORMAL
);
138 return (CMD_RETURN_ERROR
);
142 cmd_show_options_print(struct cmd
*self
, struct cmdq_item
*item
,
143 struct options_entry
*o
, int idx
, int parent
)
145 struct args
*args
= cmd_get_args(self
);
146 struct options_array_item
*a
;
147 const char *name
= options_name(o
);
148 char *value
, *tmp
= NULL
, *escaped
;
151 xasprintf(&tmp
, "%s[%d]", name
, idx
);
154 if (options_is_array(o
)) {
155 a
= options_array_first(o
);
157 if (!args_has(args
, 'v'))
158 cmdq_print(item
, "%s", name
);
162 idx
= options_array_item_index(a
);
163 cmd_show_options_print(self
, item
, o
, idx
,
165 a
= options_array_next(a
);
171 value
= options_to_string(o
, idx
, 0);
172 if (args_has(args
, 'v'))
173 cmdq_print(item
, "%s", value
);
174 else if (options_is_string(o
)) {
175 escaped
= args_escape(value
);
177 cmdq_print(item
, "%s* %s", name
, escaped
);
179 cmdq_print(item
, "%s %s", name
, escaped
);
183 cmdq_print(item
, "%s* %s", name
, value
);
185 cmdq_print(item
, "%s %s", name
, value
);
192 static enum cmd_retval
193 cmd_show_options_all(struct cmd
*self
, struct cmdq_item
*item
, int scope
,
196 struct args
*args
= cmd_get_args(self
);
197 const struct options_table_entry
*oe
;
198 struct options_entry
*o
;
199 struct options_array_item
*a
;
204 if (cmd_get_entry(self
) != &cmd_show_hooks_entry
) {
205 o
= options_first(oo
);
207 if (options_table_entry(o
) == NULL
)
208 cmd_show_options_print(self
, item
, o
, -1, 0);
212 for (oe
= options_table
; oe
->name
!= NULL
; oe
++) {
213 if (~oe
->scope
& scope
)
216 if ((cmd_get_entry(self
) != &cmd_show_hooks_entry
&&
217 !args_has(args
, 'H') &&
218 (oe
->flags
& OPTIONS_TABLE_IS_HOOK
)) ||
219 (cmd_get_entry(self
) == &cmd_show_hooks_entry
&&
220 (~oe
->flags
& OPTIONS_TABLE_IS_HOOK
)))
223 o
= options_get_only(oo
, oe
->name
);
225 if (!args_has(args
, 'A'))
227 o
= options_get(oo
, oe
->name
);
234 if (!options_is_array(o
))
235 cmd_show_options_print(self
, item
, o
, -1, parent
);
236 else if ((a
= options_array_first(o
)) == NULL
) {
237 if (!args_has(args
, 'v')) {
238 name
= options_name(o
);
240 cmdq_print(item
, "%s*", name
);
242 cmdq_print(item
, "%s", name
);
246 idx
= options_array_item_index(a
);
247 cmd_show_options_print(self
, item
, o
, idx
,
249 a
= options_array_next(a
);
253 return (CMD_RETURN_NORMAL
);