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>
30 static enum cmd_retval
cmd_show_options_exec(struct cmd
*, struct cmdq_item
*);
32 static void cmd_show_options_print(struct cmd
*, struct cmdq_item
*,
33 struct options_entry
*, int, int);
34 static enum cmd_retval
cmd_show_options_all(struct cmd
*, struct cmdq_item
*,
35 int, struct options
*);
37 const struct cmd_entry cmd_show_options_entry
= {
38 .name
= "show-options",
41 .args
= { "AgHpqst:vw", 0, 1, NULL
},
42 .usage
= "[-AgHpqsvw] " CMD_TARGET_PANE_USAGE
" [option]",
44 .target
= { 't', CMD_FIND_PANE
, CMD_FIND_CANFAIL
},
46 .flags
= CMD_AFTERHOOK
,
47 .exec
= cmd_show_options_exec
50 const struct cmd_entry cmd_show_window_options_entry
= {
51 .name
= "show-window-options",
54 .args
= { "gvt:", 0, 1, NULL
},
55 .usage
= "[-gv] " CMD_TARGET_WINDOW_USAGE
" [option]",
57 .target
= { 't', CMD_FIND_WINDOW
, CMD_FIND_CANFAIL
},
59 .flags
= CMD_AFTERHOOK
,
60 .exec
= cmd_show_options_exec
63 const struct cmd_entry cmd_show_hooks_entry
= {
67 .args
= { "gpt:w", 0, 1, NULL
},
68 .usage
= "[-gpw] " CMD_TARGET_PANE_USAGE
,
70 .target
= { 't', CMD_FIND_PANE
, CMD_FIND_CANFAIL
},
72 .flags
= CMD_AFTERHOOK
,
73 .exec
= cmd_show_options_exec
76 static enum cmd_retval
77 cmd_show_options_exec(struct cmd
*self
, struct cmdq_item
*item
)
79 struct args
*args
= cmd_get_args(self
);
80 struct cmd_find_state
*target
= cmdq_get_target(item
);
82 char *argument
, *name
= NULL
, *cause
;
83 int window
, idx
, ambiguous
, parent
, scope
;
84 struct options_entry
*o
;
86 window
= (cmd_get_entry(self
) == &cmd_show_window_options_entry
);
88 if (args_count(args
) == 0) {
89 scope
= options_scope_from_flags(args
, window
, target
, &oo
,
91 if (scope
== OPTIONS_TABLE_NONE
) {
92 if (args_has(args
, 'q'))
93 return (CMD_RETURN_NORMAL
);
94 cmdq_error(item
, "%s", cause
);
96 return (CMD_RETURN_ERROR
);
98 return (cmd_show_options_all(self
, item
, scope
, oo
));
100 argument
= format_single_from_target(item
, args_string(args
, 0));
102 name
= options_match(argument
, &idx
, &ambiguous
);
104 if (args_has(args
, 'q'))
107 cmdq_error(item
, "ambiguous option: %s", argument
);
109 cmdq_error(item
, "invalid option: %s", argument
);
112 scope
= options_scope_from_name(args
, window
, name
, target
, &oo
,
114 if (scope
== OPTIONS_TABLE_NONE
) {
115 if (args_has(args
, 'q'))
117 cmdq_error(item
, "%s", cause
);
121 o
= options_get_only(oo
, name
);
122 if (args_has(args
, 'A') && o
== NULL
) {
123 o
= options_get(oo
, name
);
128 cmd_show_options_print(self
, item
, o
, idx
, parent
);
129 else if (*name
== '@') {
130 if (args_has(args
, 'q'))
132 cmdq_error(item
, "invalid option: %s", argument
);
139 return (CMD_RETURN_NORMAL
);
144 return (CMD_RETURN_ERROR
);
148 cmd_show_options_print(struct cmd
*self
, struct cmdq_item
*item
,
149 struct options_entry
*o
, int idx
, int parent
)
151 struct args
*args
= cmd_get_args(self
);
152 struct options_array_item
*a
;
153 const char *name
= options_name(o
);
154 char *value
, *tmp
= NULL
, *escaped
;
157 xasprintf(&tmp
, "%s[%d]", name
, idx
);
160 if (options_is_array(o
)) {
161 a
= options_array_first(o
);
163 if (!args_has(args
, 'v'))
164 cmdq_print(item
, "%s", name
);
168 idx
= options_array_item_index(a
);
169 cmd_show_options_print(self
, item
, o
, idx
,
171 a
= options_array_next(a
);
177 value
= options_to_string(o
, idx
, 0);
178 if (args_has(args
, 'v'))
179 cmdq_print(item
, "%s", value
);
180 else if (options_is_string(o
)) {
181 escaped
= args_escape(value
);
183 cmdq_print(item
, "%s* %s", name
, escaped
);
185 cmdq_print(item
, "%s %s", name
, escaped
);
189 cmdq_print(item
, "%s* %s", name
, value
);
191 cmdq_print(item
, "%s %s", name
, value
);
198 static enum cmd_retval
199 cmd_show_options_all(struct cmd
*self
, struct cmdq_item
*item
, int scope
,
202 struct args
*args
= cmd_get_args(self
);
203 const struct options_table_entry
*oe
;
204 struct options_entry
*o
;
205 struct options_array_item
*a
;
210 if (cmd_get_entry(self
) != &cmd_show_hooks_entry
) {
211 o
= options_first(oo
);
213 if (options_table_entry(o
) == NULL
)
214 cmd_show_options_print(self
, item
, o
, -1, 0);
218 for (oe
= options_table
; oe
->name
!= NULL
; oe
++) {
219 if (~oe
->scope
& scope
)
222 if ((cmd_get_entry(self
) != &cmd_show_hooks_entry
&&
223 !args_has(args
, 'H') &&
224 (oe
->flags
& OPTIONS_TABLE_IS_HOOK
)) ||
225 (cmd_get_entry(self
) == &cmd_show_hooks_entry
&&
226 (~oe
->flags
& OPTIONS_TABLE_IS_HOOK
)))
229 o
= options_get_only(oo
, oe
->name
);
231 if (!args_has(args
, 'A'))
233 o
= options_get(oo
, oe
->name
);
240 if (!options_is_array(o
))
241 cmd_show_options_print(self
, item
, o
, -1, parent
);
242 else if ((a
= options_array_first(o
)) == NULL
) {
243 if (!args_has(args
, 'v')) {
244 name
= options_name(o
);
246 cmdq_print(item
, "%s*", name
);
248 cmdq_print(item
, "%s", name
);
252 idx
= options_array_item_index(a
);
253 cmd_show_options_print(self
, item
, o
, idx
,
255 a
= options_array_next(a
);
259 return (CMD_RETURN_NORMAL
);