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]",
47 const struct cmd_entry cmd_show_window_options_entry
= {
48 "show-window-options", "showw",
50 "[-gv] " CMD_TARGET_WINDOW_USAGE
" [option]",
58 cmd_show_options_exec(struct cmd
*self
, struct cmd_q
*cmdq
)
60 struct args
*args
= self
->args
;
63 const struct options_table_entry
*table
;
67 if (args_has(self
->args
, 's')) {
69 table
= server_options_table
;
70 } else if (args_has(self
->args
, 'w') ||
71 self
->entry
== &cmd_show_window_options_entry
) {
72 table
= window_options_table
;
73 if (args_has(self
->args
, 'g'))
74 oo
= &global_w_options
;
76 wl
= cmd_find_window(cmdq
, args_get(args
, 't'), NULL
);
78 return (CMD_RETURN_ERROR
);
79 oo
= &wl
->window
->options
;
82 table
= session_options_table
;
83 if (args_has(self
->args
, 'g'))
84 oo
= &global_s_options
;
86 s
= cmd_find_session(cmdq
, args_get(args
, 't'), 0);
88 return (CMD_RETURN_ERROR
);
93 quiet
= args_has(self
->args
, 'q');
95 return (cmd_show_options_all(self
, cmdq
, table
, oo
));
97 return (cmd_show_options_one(self
, cmdq
, oo
, quiet
));
101 cmd_show_options_one(struct cmd
*self
, struct cmd_q
*cmdq
,
102 struct options
*oo
, int quiet
)
104 struct args
*args
= self
->args
;
105 const struct options_table_entry
*table
, *oe
;
106 struct options_entry
*o
;
109 if (*args
->argv
[0] == '@') {
110 if ((o
= options_find1(oo
, args
->argv
[0])) == NULL
) {
112 return (CMD_RETURN_NORMAL
);
113 cmdq_error(cmdq
, "unknown option: %s", args
->argv
[0]);
114 return (CMD_RETURN_ERROR
);
116 if (args_has(self
->args
, 'v'))
117 cmdq_print(cmdq
, "%s", o
->str
);
119 cmdq_print(cmdq
, "%s \"%s\"", o
->name
, o
->str
);
120 return (CMD_RETURN_NORMAL
);
124 if (options_table_find(args
->argv
[0], &table
, &oe
) != 0) {
125 cmdq_error(cmdq
, "ambiguous option: %s", args
->argv
[0]);
126 return (CMD_RETURN_ERROR
);
130 return (CMD_RETURN_NORMAL
);
131 cmdq_error(cmdq
, "unknown option: %s", args
->argv
[0]);
132 return (CMD_RETURN_ERROR
);
134 if ((o
= options_find1(oo
, oe
->name
)) == NULL
)
135 return (CMD_RETURN_NORMAL
);
136 optval
= options_table_print_entry(oe
, o
, args_has(self
->args
, 'v'));
137 if (args_has(self
->args
, 'v'))
138 cmdq_print(cmdq
, "%s", optval
);
140 cmdq_print(cmdq
, "%s %s", oe
->name
, optval
);
141 return (CMD_RETURN_NORMAL
);
145 cmd_show_options_all(struct cmd
*self
, struct cmd_q
*cmdq
,
146 const struct options_table_entry
*table
, struct options
*oo
)
148 const struct options_table_entry
*oe
;
149 struct options_entry
*o
;
152 RB_FOREACH(o
, options_tree
, &oo
->tree
) {
153 if (*o
->name
== '@') {
154 if (args_has(self
->args
, 'v'))
155 cmdq_print(cmdq
, "%s", o
->str
);
157 cmdq_print(cmdq
, "%s \"%s\"", o
->name
, o
->str
);
161 for (oe
= table
; oe
->name
!= NULL
; oe
++) {
162 if ((o
= options_find1(oo
, oe
->name
)) == NULL
)
164 optval
= options_table_print_entry(oe
, o
,
165 args_has(self
->args
, 'v'));
166 if (args_has(self
->args
, 'v'))
167 cmdq_print(cmdq
, "%s", optval
);
169 cmdq_print(cmdq
, "%s %s", oe
->name
, optval
);
172 return (CMD_RETURN_NORMAL
);