Focus events can cause trouble if left on and they can't be turned off
[tmux-openbsd.git] / cmd-set-option.c
blob6c0fefbf9286e130bf515955706863c1b8718de4
1 /* $OpenBSD$ */
3 /*
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>
21 #include <stdlib.h>
22 #include <string.h>
24 #include "tmux.h"
27 * Set an option.
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 *,
37 const char *);
38 int cmd_set_option_set(struct cmd *, struct cmd_q *,
39 const struct options_table_entry *, struct options *,
40 const char *);
42 struct options_entry *cmd_set_option_string(struct cmd *, struct cmd_q *,
43 const struct options_table_entry *, struct options *,
44 const char *);
45 struct options_entry *cmd_set_option_number(struct cmd *, struct cmd_q *,
46 const struct options_table_entry *, struct options *,
47 const char *);
48 struct options_entry *cmd_set_option_key(struct cmd *, struct cmd_q *,
49 const struct options_table_entry *, struct options *,
50 const char *);
51 struct options_entry *cmd_set_option_colour(struct cmd *, struct cmd_q *,
52 const struct options_table_entry *, struct options *,
53 const char *);
54 struct options_entry *cmd_set_option_attributes(struct cmd *, struct cmd_q *,
55 const struct options_table_entry *, struct options *,
56 const char *);
57 struct options_entry *cmd_set_option_flag(struct cmd *, struct cmd_q *,
58 const struct options_table_entry *, struct options *,
59 const char *);
60 struct options_entry *cmd_set_option_choice(struct cmd *, struct cmd_q *,
61 const struct options_table_entry *, struct options *,
62 const char *);
64 const struct cmd_entry cmd_set_option_entry = {
65 "set-option", "set",
66 "agoqst:uw", 1, 2,
67 "[-agosquw] [-t target-session|target-window] option [value]",
69 NULL,
70 NULL,
71 cmd_set_option_exec
74 const struct cmd_entry cmd_set_window_option_entry = {
75 "set-window-option", "setw",
76 "agoqt:u", 1, 2,
77 "[-agoqu] " CMD_TARGET_WINDOW_USAGE " option [value]",
79 NULL,
80 NULL,
81 cmd_set_option_exec
84 enum cmd_retval
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;
89 struct session *s;
90 struct winlink *wl;
91 struct client *c;
92 struct options *oo;
93 struct window *w;
94 const char *optstr, *valstr;
95 u_int i;
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);
103 if (args->argc < 2)
104 valstr = NULL;
105 else
106 valstr = args->argv[1];
108 /* Is this a user option? */
109 if (*optstr == '@')
110 return (cmd_set_option_user(self, cmdq, optstr, valstr));
112 /* Find the option entry, try each table. */
113 table = oe = NULL;
114 if (options_table_find(optstr, &table, &oe) != 0) {
115 cmdq_error(cmdq, "ambiguous option: %s", optstr);
116 return (CMD_RETURN_ERROR);
118 if (oe == NULL) {
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;
129 else {
130 wl = cmd_find_window(cmdq, args_get(args, 't'), NULL);
131 if (wl == 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;
138 else {
139 s = cmd_find_session(cmdq, args_get(args, 't'), 0);
140 if (s == NULL)
141 return (CMD_RETURN_ERROR);
142 oo = &s->options;
144 } else {
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);
153 } else {
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)
167 continue;
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. */
176 recalculate_sizes();
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. */
187 enum cmd_retval
188 cmd_set_option_user(struct cmd *self, struct cmd_q *cmdq, const char* optstr,
189 const char *valstr)
191 struct args *args = self->args;
192 struct session *s;
193 struct winlink *wl;
194 struct options *oo;
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;
202 else {
203 wl = cmd_find_window(cmdq, args_get(args, 't'), NULL);
204 if (wl == NULL)
205 return (CMD_RETURN_ERROR);
206 oo = &wl->window->options;
208 } else {
209 if (args_has(self->args, 'g'))
210 oo = &global_s_options;
211 else {
212 s = cmd_find_session(cmdq, args_get(args, 't'), 0);
213 if (s == NULL)
214 return (CMD_RETURN_ERROR);
215 oo = &s->options;
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",
226 optstr);
227 return (CMD_RETURN_ERROR);
229 options_remove(oo, optstr);
230 } else {
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,
243 valstr);
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);
259 return (-1);
261 if (value != NULL) {
262 cmdq_error(cmdq, "value passed to unset option: %s", oe->name);
263 return (-1);
266 options_remove(oo, oe->name);
267 if (!args_has(args, 'q'))
268 cmdq_info(cmdq, "unset option: %s", oe->name);
269 return (0);
272 /* Set an option. */
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;
279 const char *s;
281 if (oe->type != OPTIONS_TABLE_FLAG && value == NULL) {
282 cmdq_error(cmdq, "empty value");
283 return (-1);
286 o = NULL;
287 switch (oe->type) {
288 case OPTIONS_TABLE_STRING:
289 o = cmd_set_option_string(self, cmdq, oe, oo, value);
290 break;
291 case OPTIONS_TABLE_NUMBER:
292 o = cmd_set_option_number(self, cmdq, oe, oo, value);
293 break;
294 case OPTIONS_TABLE_KEY:
295 o = cmd_set_option_key(self, cmdq, oe, oo, value);
296 break;
297 case OPTIONS_TABLE_COLOUR:
298 o = cmd_set_option_colour(self, cmdq, oe, oo, value);
299 break;
300 case OPTIONS_TABLE_ATTRIBUTES:
301 o = cmd_set_option_attributes(self, cmdq, oe, oo, value);
302 break;
303 case OPTIONS_TABLE_FLAG:
304 o = cmd_set_option_flag(self, cmdq, oe, oo, value);
305 break;
306 case OPTIONS_TABLE_CHOICE:
307 o = cmd_set_option_choice(self, cmdq, oe, oo, value);
308 break;
310 if (o == NULL)
311 return (-1);
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);
316 return (0);
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);
331 } else
332 newval = xstrdup(value);
334 o = options_set_string(oo, oe->name, "%s", newval);
336 free(newval);
337 return (o);
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)
345 long long ll;
346 const char *errstr;
348 ll = strtonum(value, oe->minimum, oe->maximum, &errstr);
349 if (errstr != NULL) {
350 cmdq_error(cmdq, "value is %s: %s", errstr, value);
351 return (NULL);
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)
362 int key;
364 if ((key = key_string_lookup_string(value)) == KEYC_NONE) {
365 cmdq_error(cmdq, "bad key: %s", value);
366 return (NULL);
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)
377 int colour;
379 if ((colour = colour_fromstring(value)) == -1) {
380 cmdq_error(cmdq, "bad colour: %s", value);
381 return (NULL);
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)
392 int attr;
394 if ((attr = attributes_fromstring(value)) == -1) {
395 cmdq_error(cmdq, "bad attributes: %s", value);
396 return (NULL);
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)
407 int flag;
409 if (value == NULL || *value == '\0')
410 flag = !options_get_number(oo, oe->name);
411 else {
412 if ((value[0] == '1' && value[1] == '\0') ||
413 strcasecmp(value, "on") == 0 ||
414 strcasecmp(value, "yes") == 0)
415 flag = 1;
416 else if ((value[0] == '0' && value[1] == '\0') ||
417 strcasecmp(value, "off") == 0 ||
418 strcasecmp(value, "no") == 0)
419 flag = 0;
420 else {
421 cmdq_error(cmdq, "bad value: %s", value);
422 return (NULL);
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,
433 const char *value)
435 const char **choicep;
436 int n, choice = -1;
438 n = 0;
439 for (choicep = oe->choices; *choicep != NULL; choicep++) {
440 n++;
441 if (strncmp(*choicep, value, strlen(value)) != 0)
442 continue;
444 if (choice != -1) {
445 cmdq_error(cmdq, "ambiguous value: %s", value);
446 return (NULL);
448 choice = n - 1;
450 if (choice == -1) {
451 cmdq_error(cmdq, "unknown value: %s", value);
452 return (NULL);
455 return (options_set_number(oo, oe->name, choice));