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_find(const char *, const struct options_table_entry
**,
33 const struct options_table_entry
**);
35 int cmd_set_option_unset(struct cmd
*, struct cmd_ctx
*,
36 const struct options_table_entry
*, struct options
*,
38 int cmd_set_option_set(struct cmd
*, struct cmd_ctx
*,
39 const struct options_table_entry
*, struct options
*,
42 struct options_entry
*cmd_set_option_string(struct cmd
*, struct cmd_ctx
*,
43 const struct options_table_entry
*, struct options
*,
45 struct options_entry
*cmd_set_option_number(struct cmd
*, struct cmd_ctx
*,
46 const struct options_table_entry
*, struct options
*,
48 struct options_entry
*cmd_set_option_keys(struct cmd
*, struct cmd_ctx
*,
49 const struct options_table_entry
*, struct options
*,
51 struct options_entry
*cmd_set_option_colour(struct cmd
*, struct cmd_ctx
*,
52 const struct options_table_entry
*, struct options
*,
54 struct options_entry
*cmd_set_option_attributes(struct cmd
*, struct cmd_ctx
*,
55 const struct options_table_entry
*, struct options
*,
57 struct options_entry
*cmd_set_option_flag(struct cmd
*, struct cmd_ctx
*,
58 const struct options_table_entry
*, struct options
*,
60 struct options_entry
*cmd_set_option_choice(struct cmd
*, struct cmd_ctx
*,
61 const struct options_table_entry
*, struct options
*,
64 const struct cmd_entry cmd_set_option_entry
= {
67 "[-agsuw] [-t target-session|target-window] option [value]",
74 const struct cmd_entry cmd_set_window_option_entry
= {
75 "set-window-option", "setw",
77 "[-agu] " CMD_TARGET_WINDOW_USAGE
" option [value]",
84 /* Look for an option in all three tables. */
87 const char *optstr
, const struct options_table_entry
**table
,
88 const struct options_table_entry
**oe
)
90 static const struct options_table_entry
*tables
[] = {
95 const struct options_table_entry
*oe_loop
;
98 for (i
= 0; i
< nitems(tables
); i
++) {
99 for (oe_loop
= tables
[i
]; oe_loop
->name
!= NULL
; oe_loop
++) {
100 if (strncmp(oe_loop
->name
, optstr
, strlen(optstr
)) != 0)
103 /* If already found, ambiguous. */
109 /* Bail now if an exact match. */
110 if (strcmp((*oe
)->name
, optstr
) == 0)
118 cmd_set_option_exec(struct cmd
*self
, struct cmd_ctx
*ctx
)
120 struct args
*args
= self
->args
;
121 const struct options_table_entry
*table
, *oe
;
126 const char *optstr
, *valstr
;
129 /* Get the option name and value. */
130 optstr
= args
->argv
[0];
131 if (*optstr
== '\0') {
132 ctx
->error(ctx
, "invalid option");
138 valstr
= args
->argv
[1];
140 /* Find the option entry, try each table. */
142 if (cmd_set_option_find(optstr
, &table
, &oe
) != 0) {
143 ctx
->error(ctx
, "ambiguous option: %s", optstr
);
147 ctx
->error(ctx
, "unknown option: %s", optstr
);
151 /* Work out the tree from the table. */
152 if (table
== server_options_table
)
153 oo
= &global_options
;
154 else if (table
== window_options_table
) {
155 if (args_has(self
->args
, 'g'))
156 oo
= &global_w_options
;
158 wl
= cmd_find_window(ctx
, args_get(args
, 't'), NULL
);
161 oo
= &wl
->window
->options
;
163 } else if (table
== session_options_table
) {
164 if (args_has(self
->args
, 'g'))
165 oo
= &global_s_options
;
167 s
= cmd_find_session(ctx
, args_get(args
, 't'), 0);
173 ctx
->error(ctx
, "unknown table");
177 /* Unset or set the option. */
178 if (args_has(args
, 'u')) {
179 if (cmd_set_option_unset(self
, ctx
, oe
, oo
, valstr
) != 0)
182 if (cmd_set_option_set(self
, ctx
, oe
, oo
, valstr
) != 0)
186 /* Update sizes and redraw. May not need it but meh. */
188 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
189 c
= ARRAY_ITEM(&clients
, i
);
190 if (c
!= NULL
&& c
->session
!= NULL
)
191 server_redraw_client(c
);
197 /* Unset an option. */
199 cmd_set_option_unset(struct cmd
*self
, struct cmd_ctx
*ctx
,
200 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
202 struct args
*args
= self
->args
;
204 if (args_has(args
, 'g')) {
205 ctx
->error(ctx
, "can't unset global option: %s", oe
->name
);
209 ctx
->error(ctx
, "value passed to unset option: %s", oe
->name
);
213 options_remove(oo
, oe
->name
);
214 ctx
->info(ctx
, "unset option: %s", oe
->name
);
220 cmd_set_option_set(struct cmd
*self
, struct cmd_ctx
*ctx
,
221 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
223 struct options_entry
*o
;
226 if (oe
->type
!= OPTIONS_TABLE_FLAG
&& value
== NULL
) {
227 ctx
->error(ctx
, "empty value");
233 case OPTIONS_TABLE_STRING
:
234 o
= cmd_set_option_string(self
, ctx
, oe
, oo
, value
);
236 case OPTIONS_TABLE_NUMBER
:
237 o
= cmd_set_option_number(self
, ctx
, oe
, oo
, value
);
239 case OPTIONS_TABLE_KEYS
:
240 o
= cmd_set_option_keys(self
, ctx
, oe
, oo
, value
);
242 case OPTIONS_TABLE_COLOUR
:
243 o
= cmd_set_option_colour(self
, ctx
, oe
, oo
, value
);
245 case OPTIONS_TABLE_ATTRIBUTES
:
246 o
= cmd_set_option_attributes(self
, ctx
, oe
, oo
, value
);
248 case OPTIONS_TABLE_FLAG
:
249 o
= cmd_set_option_flag(self
, ctx
, oe
, oo
, value
);
251 case OPTIONS_TABLE_CHOICE
:
252 o
= cmd_set_option_choice(self
, ctx
, oe
, oo
, value
);
258 s
= options_table_print_entry(oe
, o
);
259 ctx
->info(ctx
, "set option: %s -> %s", oe
->name
, s
);
263 /* Set a string option. */
264 struct options_entry
*
265 cmd_set_option_string(struct cmd
*self
, unused
struct cmd_ctx
*ctx
,
266 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
268 struct args
*args
= self
->args
;
269 struct options_entry
*o
;
270 char *oldval
, *newval
;
272 if (args_has(args
, 'a')) {
273 oldval
= options_get_string(oo
, oe
->name
);
274 xasprintf(&newval
, "%s%s", oldval
, value
);
276 newval
= xstrdup(value
);
278 o
= options_set_string(oo
, oe
->name
, "%s", newval
);
284 /* Set a number option. */
285 struct options_entry
*
286 cmd_set_option_number(unused
struct cmd
*self
, struct cmd_ctx
*ctx
,
287 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
292 ll
= strtonum(value
, oe
->minimum
, oe
->maximum
, &errstr
);
293 if (errstr
!= NULL
) {
294 ctx
->error(ctx
, "value is %s: %s", errstr
, value
);
298 return (options_set_number(oo
, oe
->name
, ll
));
301 /* Set a keys option. */
302 struct options_entry
*
303 cmd_set_option_keys(unused
struct cmd
*self
, struct cmd_ctx
*ctx
,
304 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
306 struct keylist
*keylist
;
307 char *copy
, *ptr
, *s
;
310 keylist
= xmalloc(sizeof *keylist
);
313 ptr
= copy
= xstrdup(value
);
314 while ((s
= strsep(&ptr
, ",")) != NULL
) {
315 if ((key
= key_string_lookup_string(s
)) == KEYC_NONE
) {
316 ctx
->error(ctx
, "unknown key: %s", s
);
321 ARRAY_ADD(keylist
, key
);
325 return (options_set_data(oo
, oe
->name
, keylist
, xfree
));
328 /* Set a colour option. */
329 struct options_entry
*
330 cmd_set_option_colour(unused
struct cmd
*self
, struct cmd_ctx
*ctx
,
331 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
335 if ((colour
= colour_fromstring(value
)) == -1) {
336 ctx
->error(ctx
, "bad colour: %s", value
);
340 return (options_set_number(oo
, oe
->name
, colour
));
343 /* Set an attributes option. */
344 struct options_entry
*
345 cmd_set_option_attributes(unused
struct cmd
*self
, struct cmd_ctx
*ctx
,
346 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
350 if ((attr
= attributes_fromstring(value
)) == -1) {
351 ctx
->error(ctx
, "bad attributes: %s", value
);
355 return (options_set_number(oo
, oe
->name
, attr
));
358 /* Set a flag option. */
359 struct options_entry
*
360 cmd_set_option_flag(unused
struct cmd
*self
, struct cmd_ctx
*ctx
,
361 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
365 if (value
== NULL
|| *value
== '\0')
366 flag
= !options_get_number(oo
, oe
->name
);
368 if ((value
[0] == '1' && value
[1] == '\0') ||
369 strcasecmp(value
, "on") == 0 ||
370 strcasecmp(value
, "yes") == 0)
372 else if ((value
[0] == '0' && value
[1] == '\0') ||
373 strcasecmp(value
, "off") == 0 ||
374 strcasecmp(value
, "no") == 0)
377 ctx
->error(ctx
, "bad value: %s", value
);
382 return (options_set_number(oo
, oe
->name
, flag
));
385 /* Set a choice option. */
386 struct options_entry
*
387 cmd_set_option_choice(unused
struct cmd
*self
, struct cmd_ctx
*ctx
,
388 const struct options_table_entry
*oe
, struct options
*oo
, const char *value
)
390 const char **choicep
;
394 for (choicep
= oe
->choices
; *choicep
!= NULL
; choicep
++) {
396 if (strncmp(*choicep
, value
, strlen(value
)) != 0)
400 ctx
->error(ctx
, "ambiguous value: %s", value
);
406 ctx
->error(ctx
, "unknown value: %s", value
);
410 return (options_set_number(oo
, oe
->name
, choice
));