4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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 enum cmd_retval
cmd_show_options_exec(struct cmd
*, struct cmd_q
*);
32 enum cmd_retval
cmd_show_options_one(struct cmd
*, struct cmd_q
*,
33 struct options
*, int);
34 enum cmd_retval
cmd_show_options_all(struct cmd
*, struct cmd_q
*,
35 const struct options_table_entry
*, struct options
*);
37 const struct cmd_entry cmd_show_options_entry
= {
38 "show-options", "show",
40 "[-gqsvw] [-t target-session|target-window] [option]",
46 const struct cmd_entry cmd_show_window_options_entry
= {
47 "show-window-options", "showw",
49 "[-gv] " CMD_TARGET_WINDOW_USAGE
" [option]",
56 cmd_show_options_exec(struct cmd
*self
, struct cmd_q
*cmdq
)
58 struct args
*args
= self
->args
;
61 const struct options_table_entry
*table
;
65 if (args_has(self
->args
, 's')) {
67 table
= server_options_table
;
68 } else if (args_has(self
->args
, 'w') ||
69 self
->entry
== &cmd_show_window_options_entry
) {
70 table
= window_options_table
;
71 if (args_has(self
->args
, 'g'))
72 oo
= &global_w_options
;
74 wl
= cmd_find_window(cmdq
, args_get(args
, 't'), NULL
);
76 return (CMD_RETURN_ERROR
);
77 oo
= &wl
->window
->options
;
80 table
= session_options_table
;
81 if (args_has(self
->args
, 'g'))
82 oo
= &global_s_options
;
84 s
= cmd_find_session(cmdq
, args_get(args
, 't'), 0);
86 return (CMD_RETURN_ERROR
);
91 quiet
= args_has(self
->args
, 'q');
93 return (cmd_show_options_all(self
, cmdq
, table
, oo
));
95 return (cmd_show_options_one(self
, cmdq
, oo
, quiet
));
99 cmd_show_options_one(struct cmd
*self
, struct cmd_q
*cmdq
,
100 struct options
*oo
, int quiet
)
102 struct args
*args
= self
->args
;
103 const struct options_table_entry
*table
, *oe
;
104 struct options_entry
*o
;
107 if (*args
->argv
[0] == '@') {
108 if ((o
= options_find1(oo
, args
->argv
[0])) == NULL
) {
110 return (CMD_RETURN_NORMAL
);
111 cmdq_error(cmdq
, "unknown option: %s", args
->argv
[0]);
112 return (CMD_RETURN_ERROR
);
114 if (args_has(self
->args
, 'v'))
115 cmdq_print(cmdq
, "%s", o
->str
);
117 cmdq_print(cmdq
, "%s \"%s\"", o
->name
, o
->str
);
118 return (CMD_RETURN_NORMAL
);
122 if (options_table_find(args
->argv
[0], &table
, &oe
) != 0) {
123 cmdq_error(cmdq
, "ambiguous option: %s", args
->argv
[0]);
124 return (CMD_RETURN_ERROR
);
128 return (CMD_RETURN_NORMAL
);
129 cmdq_error(cmdq
, "unknown option: %s", args
->argv
[0]);
130 return (CMD_RETURN_ERROR
);
132 if ((o
= options_find1(oo
, oe
->name
)) == NULL
)
133 return (CMD_RETURN_NORMAL
);
134 optval
= options_table_print_entry(oe
, o
, args_has(self
->args
, 'v'));
135 if (args_has(self
->args
, 'v'))
136 cmdq_print(cmdq
, "%s", optval
);
138 cmdq_print(cmdq
, "%s %s", oe
->name
, optval
);
139 return (CMD_RETURN_NORMAL
);
143 cmd_show_options_all(struct cmd
*self
, struct cmd_q
*cmdq
,
144 const struct options_table_entry
*table
, struct options
*oo
)
146 const struct options_table_entry
*oe
;
147 struct options_entry
*o
;
150 RB_FOREACH(o
, options_tree
, &oo
->tree
) {
151 if (*o
->name
== '@') {
152 if (args_has(self
->args
, 'v'))
153 cmdq_print(cmdq
, "%s", o
->str
);
155 cmdq_print(cmdq
, "%s \"%s\"", o
->name
, o
->str
);
159 for (oe
= table
; oe
->name
!= NULL
; oe
++) {
160 if ((o
= options_find1(oo
, oe
->name
)) == NULL
)
162 optval
= options_table_print_entry(oe
, o
,
163 args_has(self
->args
, 'v'));
164 if (args_has(self
->args
, 'v'))
165 cmdq_print(cmdq
, "%s", optval
);
167 cmdq_print(cmdq
, "%s %s", oe
->name
, optval
);
170 return (CMD_RETURN_NORMAL
);