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_set_option_exec(struct cmd
*, struct cmd_q
*);
32 enum cmd_retval
cmd_set_option_user(struct cmd
*, struct cmd_q
*,
33 const char *, const char *);
35 int cmd_set_option_unset(struct cmd
*, struct cmd_q
*,
36 const struct options_table_entry
*, struct options
*,
38 int cmd_set_option_set(struct cmd
*, struct cmd_q
*,
39 const struct options_table_entry
*, struct options
*,
42 struct options_entry
*cmd_set_option_string(struct cmd
*, struct cmd_q
*,
43 const struct options_table_entry
*, struct options
*,
45 struct options_entry
*cmd_set_option_number(struct cmd
*, struct cmd_q
*,
46 const struct options_table_entry
*, struct options
*,
48 struct options_entry
*cmd_set_option_key(struct cmd
*, struct cmd_q
*,
49 const struct options_table_entry
*, struct options
*,
51 struct options_entry
*cmd_set_option_colour(struct cmd
*, struct cmd_q
*,
52 const struct options_table_entry
*, struct options
*,
54 struct options_entry
*cmd_set_option_attributes(struct cmd
*, struct cmd_q
*,
55 const struct options_table_entry
*, struct options
*,
57 struct options_entry
*cmd_set_option_flag(struct cmd
*, struct cmd_q
*,
58 const struct options_table_entry
*, struct options
*,
60 struct options_entry
*cmd_set_option_choice(struct cmd
*, struct cmd_q
*,
61 const struct options_table_entry
*, struct options
*,
64 const struct cmd_entry cmd_set_option_entry
= {
67 "[-agosquw] [-t target-session|target-window] option [value]",
74 const struct cmd_entry cmd_set_window_option_entry
= {
75 "set-window-option", "setw",
77 "[-agoqu] " CMD_TARGET_WINDOW_USAGE
" option [value]",
85 cmd_set_option_exec(struct cmd
*self
, struct cmd_q
*cmdq
)
87 struct args
*args
= self
->args
;
88 const struct options_table_entry
*table
, *oe
;
94 const char *optstr
, *valstr
;
97 /* Get the option name and value. */
98 optstr
= args
->argv
[0];
99 if (*optstr
== '\0') {
100 cmdq_error(cmdq
, "invalid option");
101 return (CMD_RETURN_ERROR
);
106 valstr
= args
->argv
[1];
108 /* Is this a user option? */
110 return (cmd_set_option_user(self
, cmdq
, optstr
, valstr
));
112 /* Find the option entry, try each table. */
114 if (options_table_find(optstr
, &table
, &oe
) != 0) {
115 cmdq_error(cmdq
, "ambiguous option: %s", optstr
);
116 return (CMD_RETURN_ERROR
);
119 cmdq_error(cmdq
, "unknown option: %s", optstr
);
120 return (CMD_RETURN_ERROR
);
123 /* Work out the tree from the table. */
124 if (table
== server_options_table
)
125 oo
= &global_options
;
126 else if (table
== window_options_table
) {
127 if (args_has(self
->args
, 'g'))
128 oo
= &global_w_options
;
130 wl
= cmd_find_window(cmdq
, args_get(args
, 't'), NULL
);
132 return (CMD_RETURN_ERROR
);
133 oo
= &wl
->window
->options
;
135 } else if (table
== session_options_table
) {
136 if (args_has(self
->args
, 'g'))
137 oo
= &global_s_options
;
139 s
= cmd_find_session(cmdq
, args_get(args
, 't'), 0);
141 return (CMD_RETURN_ERROR
);
145 cmdq_error(cmdq
, "unknown table");
146 return (CMD_RETURN_ERROR
);
149 /* Unset or set the option. */
150 if (args_has(args
, 'u')) {
151 if (cmd_set_option_unset(self
, cmdq
, oe
, oo
, valstr
) != 0)
152 return (CMD_RETURN_ERROR
);
154 if (args_has(args
, 'o') && options_find1(oo
, optstr
) != NULL
) {
155 if (!args_has(args
, 'q'))
156 cmdq_print(cmdq
, "already set: %s", optstr
);
157 return (CMD_RETURN_NORMAL
);
159 if (cmd_set_option_set(self
, cmdq
, oe
, oo
, valstr
) != 0)
160 return (CMD_RETURN_ERROR
);
163 /* Start or stop timers when automatic-rename changed. */
164 if (strcmp (oe
->name
, "automatic-rename") == 0) {
165 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
166 if ((w
= ARRAY_ITEM(&windows
, i
)) == NULL
)
168 if (options_get_number(&w
->options
, "automatic-rename"))
169 queue_window_name(w
);
170 else if (event_initialized(&w
->name_timer
))
171 evtimer_del(&w
->name_timer
);
175 /* Update sizes and redraw. May not need it but meh. */
177 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
178 c
= ARRAY_ITEM(&clients
, i
);
179 if (c
!= NULL
&& c
->session
!= NULL
)
180 server_redraw_client(c
);
183 return (CMD_RETURN_NORMAL
);
186 /* Set user option. */
188 cmd_set_option_user(struct cmd
*self
, struct cmd_q
*cmdq
, const char* optstr
,
191 struct args
*args
= self
->args
;
196 if (args_has(args
, 's'))
197 oo
= &global_options
;
198 else if (args_has(self
->args
, 'w') ||
199 self
->entry
== &cmd_set_window_option_entry
) {
200 if (args_has(self
->args
, 'g'))
201 oo
= &global_w_options
;
203 wl
= cmd_find_window(cmdq
, args_get(args
, 't'), NULL
);
205 return (CMD_RETURN_ERROR
);
206 oo
= &wl
->window
->options
;
209 if (args_has(self
->args
, 'g'))
210 oo
= &global_s_options
;
212 s
= cmd_find_session(cmdq
, args_get(args
, 't'), 0);
214 return (CMD_RETURN_ERROR
);
219 if (args_has(args
, 'u')) {
220 if (options_find1(oo
, optstr
) == NULL
) {
221 cmdq_error(cmdq
, "unknown option: %s", optstr
);
222 return (CMD_RETURN_ERROR
);
224 if (valstr
!= NULL
) {
225 cmdq_error(cmdq
, "value passed to unset option: %s",
227 return (CMD_RETURN_ERROR
);
229 options_remove(oo
, optstr
);
231 if (valstr
== NULL
) {
232 cmdq_error(cmdq
, "empty value");
233 return (CMD_RETURN_ERROR
);
235 if (args_has(args
, 'o') && options_find1(oo
, optstr
) != NULL
) {
236 if (!args_has(args
, 'q'))
237 cmdq_print(cmdq
, "already set: %s", optstr
);
238 return (CMD_RETURN_NORMAL
);
240 options_set_string(oo
, optstr
, "%s", valstr
);
241 if (!args_has(args
, 'q')) {
242 cmdq_info(cmdq
, "set option: %s -> %s", optstr
,
246 return (CMD_RETURN_NORMAL
);
250 /* Unset an option. */
252 cmd_set_option_unset(struct cmd
*self
, struct cmd_q
*cmdq
,
253 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
255 struct args
*args
= self
->args
;
257 if (args_has(args
, 'g')) {
258 cmdq_error(cmdq
, "can't unset global option: %s", oe
->name
);
262 cmdq_error(cmdq
, "value passed to unset option: %s", oe
->name
);
266 options_remove(oo
, oe
->name
);
267 if (!args_has(args
, 'q'))
268 cmdq_info(cmdq
, "unset option: %s", oe
->name
);
274 cmd_set_option_set(struct cmd
*self
, struct cmd_q
*cmdq
,
275 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
277 struct args
*args
= self
->args
;
278 struct options_entry
*o
;
281 if (oe
->type
!= OPTIONS_TABLE_FLAG
&& value
== NULL
) {
282 cmdq_error(cmdq
, "empty value");
288 case OPTIONS_TABLE_STRING
:
289 o
= cmd_set_option_string(self
, cmdq
, oe
, oo
, value
);
291 case OPTIONS_TABLE_NUMBER
:
292 o
= cmd_set_option_number(self
, cmdq
, oe
, oo
, value
);
294 case OPTIONS_TABLE_KEY
:
295 o
= cmd_set_option_key(self
, cmdq
, oe
, oo
, value
);
297 case OPTIONS_TABLE_COLOUR
:
298 o
= cmd_set_option_colour(self
, cmdq
, oe
, oo
, value
);
300 case OPTIONS_TABLE_ATTRIBUTES
:
301 o
= cmd_set_option_attributes(self
, cmdq
, oe
, oo
, value
);
303 case OPTIONS_TABLE_FLAG
:
304 o
= cmd_set_option_flag(self
, cmdq
, oe
, oo
, value
);
306 case OPTIONS_TABLE_CHOICE
:
307 o
= cmd_set_option_choice(self
, cmdq
, oe
, oo
, value
);
313 s
= options_table_print_entry(oe
, o
, 0);
314 if (!args_has(args
, 'q'))
315 cmdq_info(cmdq
, "set option: %s -> %s", oe
->name
, s
);
319 /* Set a string option. */
320 struct options_entry
*
321 cmd_set_option_string(struct cmd
*self
, unused
struct cmd_q
*cmdq
,
322 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
324 struct args
*args
= self
->args
;
325 struct options_entry
*o
;
326 char *oldval
, *newval
;
328 if (args_has(args
, 'a')) {
329 oldval
= options_get_string(oo
, oe
->name
);
330 xasprintf(&newval
, "%s%s", oldval
, value
);
332 newval
= xstrdup(value
);
334 o
= options_set_string(oo
, oe
->name
, "%s", newval
);
340 /* Set a number option. */
341 struct options_entry
*
342 cmd_set_option_number(unused
struct cmd
*self
, struct cmd_q
*cmdq
,
343 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
348 ll
= strtonum(value
, oe
->minimum
, oe
->maximum
, &errstr
);
349 if (errstr
!= NULL
) {
350 cmdq_error(cmdq
, "value is %s: %s", errstr
, value
);
354 return (options_set_number(oo
, oe
->name
, ll
));
357 /* Set a key option. */
358 struct options_entry
*
359 cmd_set_option_key(unused
struct cmd
*self
, struct cmd_q
*cmdq
,
360 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
364 if ((key
= key_string_lookup_string(value
)) == KEYC_NONE
) {
365 cmdq_error(cmdq
, "bad key: %s", value
);
369 return (options_set_number(oo
, oe
->name
, key
));
372 /* Set a colour option. */
373 struct options_entry
*
374 cmd_set_option_colour(unused
struct cmd
*self
, struct cmd_q
*cmdq
,
375 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
379 if ((colour
= colour_fromstring(value
)) == -1) {
380 cmdq_error(cmdq
, "bad colour: %s", value
);
384 return (options_set_number(oo
, oe
->name
, colour
));
387 /* Set an attributes option. */
388 struct options_entry
*
389 cmd_set_option_attributes(unused
struct cmd
*self
, struct cmd_q
*cmdq
,
390 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
394 if ((attr
= attributes_fromstring(value
)) == -1) {
395 cmdq_error(cmdq
, "bad attributes: %s", value
);
399 return (options_set_number(oo
, oe
->name
, attr
));
402 /* Set a flag option. */
403 struct options_entry
*
404 cmd_set_option_flag(unused
struct cmd
*self
, struct cmd_q
*cmdq
,
405 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
409 if (value
== NULL
|| *value
== '\0')
410 flag
= !options_get_number(oo
, oe
->name
);
412 if ((value
[0] == '1' && value
[1] == '\0') ||
413 strcasecmp(value
, "on") == 0 ||
414 strcasecmp(value
, "yes") == 0)
416 else if ((value
[0] == '0' && value
[1] == '\0') ||
417 strcasecmp(value
, "off") == 0 ||
418 strcasecmp(value
, "no") == 0)
421 cmdq_error(cmdq
, "bad value: %s", value
);
426 return (options_set_number(oo
, oe
->name
, flag
));
429 /* Set a choice option. */
430 struct options_entry
*
431 cmd_set_option_choice(unused
struct cmd
*self
, struct cmd_q
*cmdq
,
432 const struct options_table_entry
*oe
, struct options
*oo
,
435 const char **choicep
;
439 for (choicep
= oe
->choices
; *choicep
!= NULL
; choicep
++) {
441 if (strncmp(*choicep
, value
, strlen(value
)) != 0)
445 cmdq_error(cmdq
, "ambiguous value: %s", value
);
451 cmdq_error(cmdq
, "unknown value: %s", value
);
455 return (options_set_number(oo
, oe
->name
, choice
));