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
);
133 "couldn't set '%s'%s", optstr
,
134 (!args_has(args
, 't') && !args_has(args
,
135 'g')) ? " need target window or -g" : "");
136 return (CMD_RETURN_ERROR
);
138 oo
= &wl
->window
->options
;
140 } else if (table
== session_options_table
) {
141 if (args_has(self
->args
, 'g'))
142 oo
= &global_s_options
;
144 s
= cmd_find_session(cmdq
, args_get(args
, 't'), 0);
147 "couldn't set '%s'%s", optstr
,
148 (!args_has(args
, 't') && !args_has(args
,
149 'g')) ? " need target session or -g" : "");
150 return (CMD_RETURN_ERROR
);
155 cmdq_error(cmdq
, "unknown table");
156 return (CMD_RETURN_ERROR
);
159 /* Unset or set the option. */
160 if (args_has(args
, 'u')) {
161 if (cmd_set_option_unset(self
, cmdq
, oe
, oo
, valstr
) != 0)
162 return (CMD_RETURN_ERROR
);
164 if (args_has(args
, 'o') && options_find1(oo
, optstr
) != NULL
) {
165 if (!args_has(args
, 'q'))
166 cmdq_print(cmdq
, "already set: %s", optstr
);
167 return (CMD_RETURN_NORMAL
);
169 if (cmd_set_option_set(self
, cmdq
, oe
, oo
, valstr
) != 0)
170 return (CMD_RETURN_ERROR
);
173 /* Start or stop timers when automatic-rename changed. */
174 if (strcmp (oe
->name
, "automatic-rename") == 0) {
175 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
176 if ((w
= ARRAY_ITEM(&windows
, i
)) == NULL
)
178 if (options_get_number(&w
->options
, "automatic-rename"))
179 queue_window_name(w
);
180 else if (event_initialized(&w
->name_timer
))
181 evtimer_del(&w
->name_timer
);
185 /* Update sizes and redraw. May not need it but meh. */
187 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
188 c
= ARRAY_ITEM(&clients
, i
);
189 if (c
!= NULL
&& c
->session
!= NULL
)
190 server_redraw_client(c
);
193 return (CMD_RETURN_NORMAL
);
196 /* Set user option. */
198 cmd_set_option_user(struct cmd
*self
, struct cmd_q
*cmdq
, const char* optstr
,
201 struct args
*args
= self
->args
;
206 if (args_has(args
, 's'))
207 oo
= &global_options
;
208 else if (args_has(self
->args
, 'w') ||
209 self
->entry
== &cmd_set_window_option_entry
) {
210 if (args_has(self
->args
, 'g'))
211 oo
= &global_w_options
;
213 wl
= cmd_find_window(cmdq
, args_get(args
, 't'), NULL
);
215 return (CMD_RETURN_ERROR
);
216 oo
= &wl
->window
->options
;
219 if (args_has(self
->args
, 'g'))
220 oo
= &global_s_options
;
222 s
= cmd_find_session(cmdq
, args_get(args
, 't'), 0);
224 return (CMD_RETURN_ERROR
);
229 if (args_has(args
, 'u')) {
230 if (options_find1(oo
, optstr
) == NULL
) {
231 cmdq_error(cmdq
, "unknown option: %s", optstr
);
232 return (CMD_RETURN_ERROR
);
234 if (valstr
!= NULL
) {
235 cmdq_error(cmdq
, "value passed to unset option: %s",
237 return (CMD_RETURN_ERROR
);
239 options_remove(oo
, optstr
);
241 if (valstr
== NULL
) {
242 cmdq_error(cmdq
, "empty value");
243 return (CMD_RETURN_ERROR
);
245 if (args_has(args
, 'o') && options_find1(oo
, optstr
) != NULL
) {
246 if (!args_has(args
, 'q'))
247 cmdq_print(cmdq
, "already set: %s", optstr
);
248 return (CMD_RETURN_NORMAL
);
250 options_set_string(oo
, optstr
, "%s", valstr
);
251 if (!args_has(args
, 'q')) {
252 cmdq_info(cmdq
, "set option: %s -> %s", optstr
,
256 return (CMD_RETURN_NORMAL
);
260 /* Unset an option. */
262 cmd_set_option_unset(struct cmd
*self
, struct cmd_q
*cmdq
,
263 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
265 struct args
*args
= self
->args
;
267 if (args_has(args
, 'g')) {
268 cmdq_error(cmdq
, "can't unset global option: %s", oe
->name
);
272 cmdq_error(cmdq
, "value passed to unset option: %s", oe
->name
);
276 options_remove(oo
, oe
->name
);
277 if (!args_has(args
, 'q'))
278 cmdq_info(cmdq
, "unset option: %s", oe
->name
);
284 cmd_set_option_set(struct cmd
*self
, struct cmd_q
*cmdq
,
285 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
287 struct args
*args
= self
->args
;
288 struct options_entry
*o
;
291 if (oe
->type
!= OPTIONS_TABLE_FLAG
&& value
== NULL
) {
292 cmdq_error(cmdq
, "empty value");
298 case OPTIONS_TABLE_STRING
:
299 o
= cmd_set_option_string(self
, cmdq
, oe
, oo
, value
);
301 case OPTIONS_TABLE_NUMBER
:
302 o
= cmd_set_option_number(self
, cmdq
, oe
, oo
, value
);
304 case OPTIONS_TABLE_KEY
:
305 o
= cmd_set_option_key(self
, cmdq
, oe
, oo
, value
);
307 case OPTIONS_TABLE_COLOUR
:
308 o
= cmd_set_option_colour(self
, cmdq
, oe
, oo
, value
);
310 case OPTIONS_TABLE_ATTRIBUTES
:
311 o
= cmd_set_option_attributes(self
, cmdq
, oe
, oo
, value
);
313 case OPTIONS_TABLE_FLAG
:
314 o
= cmd_set_option_flag(self
, cmdq
, oe
, oo
, value
);
316 case OPTIONS_TABLE_CHOICE
:
317 o
= cmd_set_option_choice(self
, cmdq
, oe
, oo
, value
);
323 s
= options_table_print_entry(oe
, o
, 0);
324 if (!args_has(args
, 'q'))
325 cmdq_info(cmdq
, "set option: %s -> %s", oe
->name
, s
);
329 /* Set a string option. */
330 struct options_entry
*
331 cmd_set_option_string(struct cmd
*self
, unused
struct cmd_q
*cmdq
,
332 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
334 struct args
*args
= self
->args
;
335 struct options_entry
*o
;
336 char *oldval
, *newval
;
338 if (args_has(args
, 'a')) {
339 oldval
= options_get_string(oo
, oe
->name
);
340 xasprintf(&newval
, "%s%s", oldval
, value
);
342 newval
= xstrdup(value
);
344 o
= options_set_string(oo
, oe
->name
, "%s", newval
);
350 /* Set a number option. */
351 struct options_entry
*
352 cmd_set_option_number(unused
struct cmd
*self
, struct cmd_q
*cmdq
,
353 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
358 ll
= strtonum(value
, oe
->minimum
, oe
->maximum
, &errstr
);
359 if (errstr
!= NULL
) {
360 cmdq_error(cmdq
, "value is %s: %s", errstr
, value
);
364 return (options_set_number(oo
, oe
->name
, ll
));
367 /* Set a key option. */
368 struct options_entry
*
369 cmd_set_option_key(unused
struct cmd
*self
, struct cmd_q
*cmdq
,
370 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
374 if ((key
= key_string_lookup_string(value
)) == KEYC_NONE
) {
375 cmdq_error(cmdq
, "bad key: %s", value
);
379 return (options_set_number(oo
, oe
->name
, key
));
382 /* Set a colour option. */
383 struct options_entry
*
384 cmd_set_option_colour(unused
struct cmd
*self
, struct cmd_q
*cmdq
,
385 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
389 if ((colour
= colour_fromstring(value
)) == -1) {
390 cmdq_error(cmdq
, "bad colour: %s", value
);
394 return (options_set_number(oo
, oe
->name
, colour
));
397 /* Set an attributes option. */
398 struct options_entry
*
399 cmd_set_option_attributes(unused
struct cmd
*self
, struct cmd_q
*cmdq
,
400 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
404 if ((attr
= attributes_fromstring(value
)) == -1) {
405 cmdq_error(cmdq
, "bad attributes: %s", value
);
409 return (options_set_number(oo
, oe
->name
, attr
));
412 /* Set a flag option. */
413 struct options_entry
*
414 cmd_set_option_flag(unused
struct cmd
*self
, struct cmd_q
*cmdq
,
415 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
419 if (value
== NULL
|| *value
== '\0')
420 flag
= !options_get_number(oo
, oe
->name
);
422 if ((value
[0] == '1' && value
[1] == '\0') ||
423 strcasecmp(value
, "on") == 0 ||
424 strcasecmp(value
, "yes") == 0)
426 else if ((value
[0] == '0' && value
[1] == '\0') ||
427 strcasecmp(value
, "off") == 0 ||
428 strcasecmp(value
, "no") == 0)
431 cmdq_error(cmdq
, "bad value: %s", value
);
436 return (options_set_number(oo
, oe
->name
, flag
));
439 /* Set a choice option. */
440 struct options_entry
*
441 cmd_set_option_choice(unused
struct cmd
*self
, struct cmd_q
*cmdq
,
442 const struct options_table_entry
*oe
, struct options
*oo
,
445 const char **choicep
;
449 for (choicep
= oe
->choices
; *choicep
!= NULL
; choicep
++) {
451 if (strncmp(*choicep
, value
, strlen(value
)) != 0)
455 cmdq_error(cmdq
, "ambiguous value: %s", value
);
461 cmdq_error(cmdq
, "unknown value: %s", value
);
465 return (options_set_number(oo
, oe
->name
, choice
));