Allow attach-session -t to accept a window and pane to select them on
[tmux-openbsd.git] / cmd-set-option.c
blob1b25fac034ae1d297fb85c8b7158196c9f30b786
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 cmd_set_option_exec
73 const struct cmd_entry cmd_set_window_option_entry = {
74 "set-window-option", "setw",
75 "agoqt:u", 1, 2,
76 "[-agoqu] " CMD_TARGET_WINDOW_USAGE " option [value]",
78 NULL,
79 cmd_set_option_exec
82 enum cmd_retval
83 cmd_set_option_exec(struct cmd *self, struct cmd_q *cmdq)
85 struct args *args = self->args;
86 const struct options_table_entry *table, *oe;
87 struct session *s;
88 struct winlink *wl;
89 struct client *c;
90 struct options *oo;
91 struct window *w;
92 const char *optstr, *valstr;
93 u_int i;
95 /* Get the option name and value. */
96 optstr = args->argv[0];
97 if (*optstr == '\0') {
98 cmdq_error(cmdq, "invalid option");
99 return (CMD_RETURN_ERROR);
101 if (args->argc < 2)
102 valstr = NULL;
103 else
104 valstr = args->argv[1];
106 /* Is this a user option? */
107 if (*optstr == '@')
108 return (cmd_set_option_user(self, cmdq, optstr, valstr));
110 /* Find the option entry, try each table. */
111 table = oe = NULL;
112 if (options_table_find(optstr, &table, &oe) != 0) {
113 cmdq_error(cmdq, "ambiguous option: %s", optstr);
114 return (CMD_RETURN_ERROR);
116 if (oe == NULL) {
117 cmdq_error(cmdq, "unknown option: %s", optstr);
118 return (CMD_RETURN_ERROR);
121 /* Work out the tree from the table. */
122 if (table == server_options_table)
123 oo = &global_options;
124 else if (table == window_options_table) {
125 if (args_has(self->args, 'g'))
126 oo = &global_w_options;
127 else {
128 wl = cmd_find_window(cmdq, args_get(args, 't'), NULL);
129 if (wl == NULL) {
130 cmdq_error(cmdq,
131 "couldn't set '%s'%s", optstr,
132 (!args_has(args, 't') && !args_has(args,
133 'g')) ? " need target window or -g" : "");
134 return (CMD_RETURN_ERROR);
136 oo = &wl->window->options;
138 } else if (table == session_options_table) {
139 if (args_has(self->args, 'g'))
140 oo = &global_s_options;
141 else {
142 s = cmd_find_session(cmdq, args_get(args, 't'), 0);
143 if (s == NULL) {
144 cmdq_error(cmdq,
145 "couldn't set '%s'%s", optstr,
146 (!args_has(args, 't') && !args_has(args,
147 'g')) ? " need target session or -g" : "");
148 return (CMD_RETURN_ERROR);
150 oo = &s->options;
152 } else {
153 cmdq_error(cmdq, "unknown table");
154 return (CMD_RETURN_ERROR);
157 /* Unset or set the option. */
158 if (args_has(args, 'u')) {
159 if (cmd_set_option_unset(self, cmdq, oe, oo, valstr) != 0)
160 return (CMD_RETURN_ERROR);
161 } else {
162 if (args_has(args, 'o') && options_find1(oo, optstr) != NULL) {
163 if (!args_has(args, 'q'))
164 cmdq_print(cmdq, "already set: %s", optstr);
165 return (CMD_RETURN_NORMAL);
167 if (cmd_set_option_set(self, cmdq, oe, oo, valstr) != 0)
168 return (CMD_RETURN_ERROR);
171 /* Start or stop timers when automatic-rename changed. */
172 if (strcmp (oe->name, "automatic-rename") == 0) {
173 for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
174 if ((w = ARRAY_ITEM(&windows, i)) == NULL)
175 continue;
176 if (options_get_number(&w->options, "automatic-rename"))
177 queue_window_name(w);
178 else if (event_initialized(&w->name_timer))
179 evtimer_del(&w->name_timer);
183 /* Update sizes and redraw. May not need it but meh. */
184 recalculate_sizes();
185 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
186 c = ARRAY_ITEM(&clients, i);
187 if (c != NULL && c->session != NULL)
188 server_redraw_client(c);
191 return (CMD_RETURN_NORMAL);
194 /* Set user option. */
195 enum cmd_retval
196 cmd_set_option_user(struct cmd *self, struct cmd_q *cmdq, const char* optstr,
197 const char *valstr)
199 struct args *args = self->args;
200 struct session *s;
201 struct winlink *wl;
202 struct options *oo;
204 if (args_has(args, 's'))
205 oo = &global_options;
206 else if (args_has(self->args, 'w') ||
207 self->entry == &cmd_set_window_option_entry) {
208 if (args_has(self->args, 'g'))
209 oo = &global_w_options;
210 else {
211 wl = cmd_find_window(cmdq, args_get(args, 't'), NULL);
212 if (wl == NULL)
213 return (CMD_RETURN_ERROR);
214 oo = &wl->window->options;
216 } else {
217 if (args_has(self->args, 'g'))
218 oo = &global_s_options;
219 else {
220 s = cmd_find_session(cmdq, args_get(args, 't'), 0);
221 if (s == NULL)
222 return (CMD_RETURN_ERROR);
223 oo = &s->options;
227 if (args_has(args, 'u')) {
228 if (options_find1(oo, optstr) == NULL) {
229 cmdq_error(cmdq, "unknown option: %s", optstr);
230 return (CMD_RETURN_ERROR);
232 if (valstr != NULL) {
233 cmdq_error(cmdq, "value passed to unset option: %s",
234 optstr);
235 return (CMD_RETURN_ERROR);
237 options_remove(oo, optstr);
238 } else {
239 if (valstr == NULL) {
240 cmdq_error(cmdq, "empty value");
241 return (CMD_RETURN_ERROR);
243 if (args_has(args, 'o') && options_find1(oo, optstr) != NULL) {
244 if (!args_has(args, 'q'))
245 cmdq_print(cmdq, "already set: %s", optstr);
246 return (CMD_RETURN_NORMAL);
248 options_set_string(oo, optstr, "%s", valstr);
249 if (!args_has(args, 'q')) {
250 cmdq_info(cmdq, "set option: %s -> %s", optstr,
251 valstr);
254 return (CMD_RETURN_NORMAL);
258 /* Unset an option. */
260 cmd_set_option_unset(struct cmd *self, struct cmd_q *cmdq,
261 const struct options_table_entry *oe, struct options *oo, const char *value)
263 struct args *args = self->args;
265 if (args_has(args, 'g')) {
266 cmdq_error(cmdq, "can't unset global option: %s", oe->name);
267 return (-1);
269 if (value != NULL) {
270 cmdq_error(cmdq, "value passed to unset option: %s", oe->name);
271 return (-1);
274 options_remove(oo, oe->name);
275 if (!args_has(args, 'q'))
276 cmdq_info(cmdq, "unset option: %s", oe->name);
277 return (0);
280 /* Set an option. */
282 cmd_set_option_set(struct cmd *self, struct cmd_q *cmdq,
283 const struct options_table_entry *oe, struct options *oo, const char *value)
285 struct args *args = self->args;
286 struct options_entry *o;
287 const char *s;
289 if (oe->type != OPTIONS_TABLE_FLAG && value == NULL) {
290 cmdq_error(cmdq, "empty value");
291 return (-1);
294 o = NULL;
295 switch (oe->type) {
296 case OPTIONS_TABLE_STRING:
297 o = cmd_set_option_string(self, cmdq, oe, oo, value);
298 break;
299 case OPTIONS_TABLE_NUMBER:
300 o = cmd_set_option_number(self, cmdq, oe, oo, value);
301 break;
302 case OPTIONS_TABLE_KEY:
303 o = cmd_set_option_key(self, cmdq, oe, oo, value);
304 break;
305 case OPTIONS_TABLE_COLOUR:
306 o = cmd_set_option_colour(self, cmdq, oe, oo, value);
307 break;
308 case OPTIONS_TABLE_ATTRIBUTES:
309 o = cmd_set_option_attributes(self, cmdq, oe, oo, value);
310 break;
311 case OPTIONS_TABLE_FLAG:
312 o = cmd_set_option_flag(self, cmdq, oe, oo, value);
313 break;
314 case OPTIONS_TABLE_CHOICE:
315 o = cmd_set_option_choice(self, cmdq, oe, oo, value);
316 break;
318 if (o == NULL)
319 return (-1);
321 s = options_table_print_entry(oe, o, 0);
322 if (!args_has(args, 'q'))
323 cmdq_info(cmdq, "set option: %s -> %s", oe->name, s);
324 return (0);
327 /* Set a string option. */
328 struct options_entry *
329 cmd_set_option_string(struct cmd *self, unused struct cmd_q *cmdq,
330 const struct options_table_entry *oe, struct options *oo, const char *value)
332 struct args *args = self->args;
333 struct options_entry *o;
334 char *oldval, *newval;
336 if (args_has(args, 'a')) {
337 oldval = options_get_string(oo, oe->name);
338 xasprintf(&newval, "%s%s", oldval, value);
339 } else
340 newval = xstrdup(value);
342 o = options_set_string(oo, oe->name, "%s", newval);
344 free(newval);
345 return (o);
348 /* Set a number option. */
349 struct options_entry *
350 cmd_set_option_number(unused struct cmd *self, struct cmd_q *cmdq,
351 const struct options_table_entry *oe, struct options *oo, const char *value)
353 long long ll;
354 const char *errstr;
356 ll = strtonum(value, oe->minimum, oe->maximum, &errstr);
357 if (errstr != NULL) {
358 cmdq_error(cmdq, "value is %s: %s", errstr, value);
359 return (NULL);
362 return (options_set_number(oo, oe->name, ll));
365 /* Set a key option. */
366 struct options_entry *
367 cmd_set_option_key(unused struct cmd *self, struct cmd_q *cmdq,
368 const struct options_table_entry *oe, struct options *oo, const char *value)
370 int key;
372 if ((key = key_string_lookup_string(value)) == KEYC_NONE) {
373 cmdq_error(cmdq, "bad key: %s", value);
374 return (NULL);
377 return (options_set_number(oo, oe->name, key));
380 /* Set a colour option. */
381 struct options_entry *
382 cmd_set_option_colour(unused struct cmd *self, struct cmd_q *cmdq,
383 const struct options_table_entry *oe, struct options *oo, const char *value)
385 int colour;
387 if ((colour = colour_fromstring(value)) == -1) {
388 cmdq_error(cmdq, "bad colour: %s", value);
389 return (NULL);
392 return (options_set_number(oo, oe->name, colour));
395 /* Set an attributes option. */
396 struct options_entry *
397 cmd_set_option_attributes(unused struct cmd *self, struct cmd_q *cmdq,
398 const struct options_table_entry *oe, struct options *oo, const char *value)
400 int attr;
402 if ((attr = attributes_fromstring(value)) == -1) {
403 cmdq_error(cmdq, "bad attributes: %s", value);
404 return (NULL);
407 return (options_set_number(oo, oe->name, attr));
410 /* Set a flag option. */
411 struct options_entry *
412 cmd_set_option_flag(unused struct cmd *self, struct cmd_q *cmdq,
413 const struct options_table_entry *oe, struct options *oo, const char *value)
415 int flag;
417 if (value == NULL || *value == '\0')
418 flag = !options_get_number(oo, oe->name);
419 else {
420 if ((value[0] == '1' && value[1] == '\0') ||
421 strcasecmp(value, "on") == 0 ||
422 strcasecmp(value, "yes") == 0)
423 flag = 1;
424 else if ((value[0] == '0' && value[1] == '\0') ||
425 strcasecmp(value, "off") == 0 ||
426 strcasecmp(value, "no") == 0)
427 flag = 0;
428 else {
429 cmdq_error(cmdq, "bad value: %s", value);
430 return (NULL);
434 return (options_set_number(oo, oe->name, flag));
437 /* Set a choice option. */
438 struct options_entry *
439 cmd_set_option_choice(unused struct cmd *self, struct cmd_q *cmdq,
440 const struct options_table_entry *oe, struct options *oo,
441 const char *value)
443 const char **choicep;
444 int n, choice = -1;
446 n = 0;
447 for (choicep = oe->choices; *choicep != NULL; choicep++) {
448 n++;
449 if (strncmp(*choicep, value, strlen(value)) != 0)
450 continue;
452 if (choice != -1) {
453 cmdq_error(cmdq, "ambiguous value: %s", value);
454 return (NULL);
456 choice = n - 1;
458 if (choice == -1) {
459 cmdq_error(cmdq, "unknown value: %s", value);
460 return (NULL);
463 return (options_set_number(oo, oe->name, choice));