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 int cmd_set_option_exec(struct cmd
*, struct cmd_ctx
*);
32 int cmd_set_option_unset(struct cmd
*, struct cmd_ctx
*,
33 const struct options_table_entry
*, struct options
*,
35 int cmd_set_option_set(struct cmd
*, struct cmd_ctx
*,
36 const struct options_table_entry
*, struct options
*,
39 struct options_entry
*cmd_set_option_string(struct cmd
*, struct cmd_ctx
*,
40 const struct options_table_entry
*, struct options
*,
42 struct options_entry
*cmd_set_option_number(struct cmd
*, struct cmd_ctx
*,
43 const struct options_table_entry
*, struct options
*,
45 struct options_entry
*cmd_set_option_keys(struct cmd
*, struct cmd_ctx
*,
46 const struct options_table_entry
*, struct options
*,
48 struct options_entry
*cmd_set_option_colour(struct cmd
*, struct cmd_ctx
*,
49 const struct options_table_entry
*, struct options
*,
51 struct options_entry
*cmd_set_option_attributes(struct cmd
*, struct cmd_ctx
*,
52 const struct options_table_entry
*, struct options
*,
54 struct options_entry
*cmd_set_option_flag(struct cmd
*, struct cmd_ctx
*,
55 const struct options_table_entry
*, struct options
*,
57 struct options_entry
*cmd_set_option_choice(struct cmd
*, struct cmd_ctx
*,
58 const struct options_table_entry
*, struct options
*,
61 const struct cmd_entry cmd_set_option_entry
= {
64 "[-agsuw] [-t target-session|target-window] option [value]",
71 const struct cmd_entry cmd_set_window_option_entry
= {
72 "set-window-option", "setw",
74 "[-agu] " CMD_TARGET_WINDOW_USAGE
" option [value]",
82 cmd_set_option_exec(struct cmd
*self
, struct cmd_ctx
*ctx
)
84 struct args
*args
= self
->args
;
85 const struct options_table_entry
*table
, *oe
, *oe_loop
;
90 const char *optstr
, *valstr
;
93 /* Work out the options tree and table to use. */
94 if (args_has(self
->args
, 's')) {
96 table
= server_options_table
;
97 } else if (args_has(self
->args
, 'w') ||
98 self
->entry
== &cmd_set_window_option_entry
) {
99 table
= window_options_table
;
100 if (args_has(self
->args
, 'g'))
101 oo
= &global_w_options
;
103 wl
= cmd_find_window(ctx
, args_get(args
, 't'), NULL
);
106 oo
= &wl
->window
->options
;
109 table
= session_options_table
;
110 if (args_has(self
->args
, 'g'))
111 oo
= &global_s_options
;
113 s
= cmd_find_session(ctx
, args_get(args
, 't'));
120 /* Get the option name and value. */
121 optstr
= args
->argv
[0];
122 if (*optstr
== '\0') {
123 ctx
->error(ctx
, "invalid option");
129 valstr
= args
->argv
[1];
131 /* Find the option table entry. */
133 for (oe_loop
= table
; oe_loop
->name
!= NULL
; oe_loop
++) {
134 if (strncmp(oe_loop
->name
, optstr
, strlen(optstr
)) != 0)
138 ctx
->error(ctx
, "ambiguous option: %s", optstr
);
143 /* Bail now if an exact match. */
144 if (strcmp(oe
->name
, optstr
) == 0)
148 ctx
->error(ctx
, "unknown option: %s", optstr
);
152 /* Unset or set the option. */
153 if (args_has(args
, 'u')) {
154 if (cmd_set_option_unset(self
, ctx
, oe
, oo
, valstr
) != 0)
157 if (cmd_set_option_set(self
, ctx
, oe
, oo
, valstr
) != 0)
161 /* Update sizes and redraw. May not need it but meh. */
163 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
164 c
= ARRAY_ITEM(&clients
, i
);
165 if (c
!= NULL
&& c
->session
!= NULL
)
166 server_redraw_client(c
);
170 * Special-case: kill all persistent jobs if status-left, status-right
171 * or set-titles-string have changed. Persistent jobs are only used by
172 * the status line at the moment so this works XXX.
174 if (strcmp(oe
->name
, "status-left") == 0 ||
175 strcmp(oe
->name
, "status-right") == 0 ||
176 strcmp(oe
->name
, "status") == 0 ||
177 strcmp(oe
->name
, "set-titles-string") == 0 ||
178 strcmp(oe
->name
, "window-status-format") == 0) {
179 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
180 c
= ARRAY_ITEM(&clients
, i
);
181 if (c
!= NULL
&& c
->session
!= NULL
)
182 server_redraw_client(c
);
189 /* Unset an option. */
191 cmd_set_option_unset(struct cmd
*self
, struct cmd_ctx
*ctx
,
192 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
194 struct args
*args
= self
->args
;
196 if (args_has(args
, 'g')) {
197 ctx
->error(ctx
, "can't unset global option: %s", oe
->name
);
201 ctx
->error(ctx
, "value passed to unset option: %s", oe
->name
);
205 options_remove(oo
, oe
->name
);
206 ctx
->info(ctx
, "unset option: %s", oe
->name
);
212 cmd_set_option_set(struct cmd
*self
, struct cmd_ctx
*ctx
,
213 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
215 struct options_entry
*o
;
218 if (oe
->type
!= OPTIONS_TABLE_FLAG
&& value
== NULL
) {
219 ctx
->error(ctx
, "empty value");
225 case OPTIONS_TABLE_STRING
:
226 o
= cmd_set_option_string(self
, ctx
, oe
, oo
, value
);
228 case OPTIONS_TABLE_NUMBER
:
229 o
= cmd_set_option_number(self
, ctx
, oe
, oo
, value
);
231 case OPTIONS_TABLE_KEYS
:
232 o
= cmd_set_option_keys(self
, ctx
, oe
, oo
, value
);
234 case OPTIONS_TABLE_COLOUR
:
235 o
= cmd_set_option_colour(self
, ctx
, oe
, oo
, value
);
237 case OPTIONS_TABLE_ATTRIBUTES
:
238 o
= cmd_set_option_attributes(self
, ctx
, oe
, oo
, value
);
240 case OPTIONS_TABLE_FLAG
:
241 o
= cmd_set_option_flag(self
, ctx
, oe
, oo
, value
);
243 case OPTIONS_TABLE_CHOICE
:
244 o
= cmd_set_option_choice(self
, ctx
, oe
, oo
, value
);
250 s
= options_table_print_entry(oe
, o
);
251 ctx
->info(ctx
, "set option: %s -> %s", oe
->name
, s
);
255 /* Set a string option. */
256 struct options_entry
*
257 cmd_set_option_string(struct cmd
*self
, unused
struct cmd_ctx
*ctx
,
258 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
260 struct args
*args
= self
->args
;
261 struct options_entry
*o
;
262 char *oldval
, *newval
;
264 if (args_has(args
, 'a')) {
265 oldval
= options_get_string(oo
, oe
->name
);
266 xasprintf(&newval
, "%s%s", oldval
, value
);
268 newval
= xstrdup(value
);
270 o
= options_set_string(oo
, oe
->name
, "%s", newval
);
276 /* Set a number option. */
277 struct options_entry
*
278 cmd_set_option_number(unused
struct cmd
*self
, struct cmd_ctx
*ctx
,
279 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
284 ll
= strtonum(value
, oe
->minimum
, oe
->maximum
, &errstr
);
285 if (errstr
!= NULL
) {
286 ctx
->error(ctx
, "value is %s: %s", errstr
, value
);
290 return (options_set_number(oo
, oe
->name
, ll
));
293 /* Set a keys option. */
294 struct options_entry
*
295 cmd_set_option_keys(unused
struct cmd
*self
, struct cmd_ctx
*ctx
,
296 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
298 struct keylist
*keylist
;
299 char *copy
, *ptr
, *s
;
302 keylist
= xmalloc(sizeof *keylist
);
305 ptr
= copy
= xstrdup(value
);
306 while ((s
= strsep(&ptr
, ",")) != NULL
) {
307 if ((key
= key_string_lookup_string(s
)) == KEYC_NONE
) {
308 ctx
->error(ctx
, "unknown key: %s", s
);
313 ARRAY_ADD(keylist
, key
);
317 return (options_set_data(oo
, oe
->name
, keylist
, xfree
));
320 /* Set a colour option. */
321 struct options_entry
*
322 cmd_set_option_colour(unused
struct cmd
*self
, struct cmd_ctx
*ctx
,
323 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
327 if ((colour
= colour_fromstring(value
)) == -1) {
328 ctx
->error(ctx
, "bad colour: %s", value
);
332 return (options_set_number(oo
, oe
->name
, colour
));
335 /* Set an attributes option. */
336 struct options_entry
*
337 cmd_set_option_attributes(unused
struct cmd
*self
, struct cmd_ctx
*ctx
,
338 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
342 if ((attr
= attributes_fromstring(value
)) == -1) {
343 ctx
->error(ctx
, "bad attributes: %s", value
);
347 return (options_set_number(oo
, oe
->name
, attr
));
350 /* Set a flag option. */
351 struct options_entry
*
352 cmd_set_option_flag(unused
struct cmd
*self
, struct cmd_ctx
*ctx
,
353 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
357 if (value
== NULL
|| *value
== '\0')
358 flag
= !options_get_number(oo
, oe
->name
);
360 if ((value
[0] == '1' && value
[1] == '\0') ||
361 strcasecmp(value
, "on") == 0 ||
362 strcasecmp(value
, "yes") == 0)
364 else if ((value
[0] == '0' && value
[1] == '\0') ||
365 strcasecmp(value
, "off") == 0 ||
366 strcasecmp(value
, "no") == 0)
369 ctx
->error(ctx
, "bad value: %s", value
);
374 return (options_set_number(oo
, oe
->name
, flag
));
377 /* Set a choice option. */
378 struct options_entry
*
379 cmd_set_option_choice(unused
struct cmd
*self
, struct cmd_ctx
*ctx
,
380 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
382 const char **choicep
;
386 for (choicep
= oe
->choices
; *choicep
!= NULL
; choicep
++) {
388 if (strncmp(*choicep
, value
, strlen(value
)) != 0)
392 ctx
->error(ctx
, "ambiguous value: %s", value
);
398 ctx
->error(ctx
, "unknown value: %s", value
);
402 return (options_set_number(oo
, oe
->name
, choice
));