Accept multiple parameters to SM/RM/DECSET/DECRST, based on a diff from
[tmux-openbsd.git] / cmd-set-option.c
blob3b822d8b7ffa15328df008c38be8ad38cfe2acae
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 cmdq_error(cmdq,
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;
143 else {
144 s = cmd_find_session(cmdq, args_get(args, 't'), 0);
145 if (s == NULL) {
146 cmdq_error(cmdq,
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);
152 oo = &s->options;
154 } else {
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);
163 } else {
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)
177 continue;
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. */
186 recalculate_sizes();
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. */
197 enum cmd_retval
198 cmd_set_option_user(struct cmd *self, struct cmd_q *cmdq, const char* optstr,
199 const char *valstr)
201 struct args *args = self->args;
202 struct session *s;
203 struct winlink *wl;
204 struct options *oo;
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;
212 else {
213 wl = cmd_find_window(cmdq, args_get(args, 't'), NULL);
214 if (wl == NULL)
215 return (CMD_RETURN_ERROR);
216 oo = &wl->window->options;
218 } else {
219 if (args_has(self->args, 'g'))
220 oo = &global_s_options;
221 else {
222 s = cmd_find_session(cmdq, args_get(args, 't'), 0);
223 if (s == NULL)
224 return (CMD_RETURN_ERROR);
225 oo = &s->options;
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",
236 optstr);
237 return (CMD_RETURN_ERROR);
239 options_remove(oo, optstr);
240 } else {
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,
253 valstr);
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);
269 return (-1);
271 if (value != NULL) {
272 cmdq_error(cmdq, "value passed to unset option: %s", oe->name);
273 return (-1);
276 options_remove(oo, oe->name);
277 if (!args_has(args, 'q'))
278 cmdq_info(cmdq, "unset option: %s", oe->name);
279 return (0);
282 /* Set an option. */
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;
289 const char *s;
291 if (oe->type != OPTIONS_TABLE_FLAG && value == NULL) {
292 cmdq_error(cmdq, "empty value");
293 return (-1);
296 o = NULL;
297 switch (oe->type) {
298 case OPTIONS_TABLE_STRING:
299 o = cmd_set_option_string(self, cmdq, oe, oo, value);
300 break;
301 case OPTIONS_TABLE_NUMBER:
302 o = cmd_set_option_number(self, cmdq, oe, oo, value);
303 break;
304 case OPTIONS_TABLE_KEY:
305 o = cmd_set_option_key(self, cmdq, oe, oo, value);
306 break;
307 case OPTIONS_TABLE_COLOUR:
308 o = cmd_set_option_colour(self, cmdq, oe, oo, value);
309 break;
310 case OPTIONS_TABLE_ATTRIBUTES:
311 o = cmd_set_option_attributes(self, cmdq, oe, oo, value);
312 break;
313 case OPTIONS_TABLE_FLAG:
314 o = cmd_set_option_flag(self, cmdq, oe, oo, value);
315 break;
316 case OPTIONS_TABLE_CHOICE:
317 o = cmd_set_option_choice(self, cmdq, oe, oo, value);
318 break;
320 if (o == NULL)
321 return (-1);
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);
326 return (0);
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);
341 } else
342 newval = xstrdup(value);
344 o = options_set_string(oo, oe->name, "%s", newval);
346 free(newval);
347 return (o);
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)
355 long long ll;
356 const char *errstr;
358 ll = strtonum(value, oe->minimum, oe->maximum, &errstr);
359 if (errstr != NULL) {
360 cmdq_error(cmdq, "value is %s: %s", errstr, value);
361 return (NULL);
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)
372 int key;
374 if ((key = key_string_lookup_string(value)) == KEYC_NONE) {
375 cmdq_error(cmdq, "bad key: %s", value);
376 return (NULL);
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)
387 int colour;
389 if ((colour = colour_fromstring(value)) == -1) {
390 cmdq_error(cmdq, "bad colour: %s", value);
391 return (NULL);
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)
402 int attr;
404 if ((attr = attributes_fromstring(value)) == -1) {
405 cmdq_error(cmdq, "bad attributes: %s", value);
406 return (NULL);
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)
417 int flag;
419 if (value == NULL || *value == '\0')
420 flag = !options_get_number(oo, oe->name);
421 else {
422 if ((value[0] == '1' && value[1] == '\0') ||
423 strcasecmp(value, "on") == 0 ||
424 strcasecmp(value, "yes") == 0)
425 flag = 1;
426 else if ((value[0] == '0' && value[1] == '\0') ||
427 strcasecmp(value, "off") == 0 ||
428 strcasecmp(value, "no") == 0)
429 flag = 0;
430 else {
431 cmdq_error(cmdq, "bad value: %s", value);
432 return (NULL);
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,
443 const char *value)
445 const char **choicep;
446 int n, choice = -1;
448 n = 0;
449 for (choicep = oe->choices; *choicep != NULL; choicep++) {
450 n++;
451 if (strncmp(*choicep, value, strlen(value)) != 0)
452 continue;
454 if (choice != -1) {
455 cmdq_error(cmdq, "ambiguous value: %s", value);
456 return (NULL);
458 choice = n - 1;
460 if (choice == -1) {
461 cmdq_error(cmdq, "unknown value: %s", value);
462 return (NULL);
465 return (options_set_number(oo, oe->name, choice));