Annotation of src/usr.bin/tmux/cmd-set-option.c, Revision 1.95
1.95 ! nicm 1: /* $OpenBSD: cmd-set-option.c,v 1.94 2016/03/03 14:15:22 nicm Exp $ */
1.1 nicm 2:
3: /*
1.93 nicm 4: * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 5: *
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.
9: *
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.
17: */
18:
19: #include <sys/types.h>
20:
21: #include <stdlib.h>
22: #include <string.h>
23:
24: #include "tmux.h"
25:
26: /*
27: * Set an option.
28: */
29:
1.60 nicm 30: enum cmd_retval cmd_set_option_exec(struct cmd *, struct cmd_q *);
1.59 nicm 31:
1.60 nicm 32: enum cmd_retval cmd_set_option_user(struct cmd *, struct cmd_q *,
1.61 nicm 33: const char *, const char *);
1.1 nicm 34:
1.60 nicm 35: int cmd_set_option_unset(struct cmd *, struct cmd_q *,
1.44 nicm 36: const struct options_table_entry *, struct options *,
37: const char *);
1.60 nicm 38: int cmd_set_option_set(struct cmd *, struct cmd_q *,
1.44 nicm 39: const struct options_table_entry *, struct options *,
40: const char *);
1.43 nicm 41:
1.60 nicm 42: struct options_entry *cmd_set_option_string(struct cmd *, struct cmd_q *,
1.44 nicm 43: const struct options_table_entry *, struct options *,
44: const char *);
1.60 nicm 45: struct options_entry *cmd_set_option_number(struct cmd *, struct cmd_q *,
1.44 nicm 46: const struct options_table_entry *, struct options *,
47: const char *);
1.60 nicm 48: struct options_entry *cmd_set_option_key(struct cmd *, struct cmd_q *,
1.44 nicm 49: const struct options_table_entry *, struct options *,
50: const char *);
1.60 nicm 51: struct options_entry *cmd_set_option_colour(struct cmd *, struct cmd_q *,
1.44 nicm 52: const struct options_table_entry *, struct options *,
53: const char *);
1.60 nicm 54: struct options_entry *cmd_set_option_attributes(struct cmd *, struct cmd_q *,
1.44 nicm 55: const struct options_table_entry *, struct options *,
56: const char *);
1.60 nicm 57: struct options_entry *cmd_set_option_flag(struct cmd *, struct cmd_q *,
1.44 nicm 58: const struct options_table_entry *, struct options *,
59: const char *);
1.60 nicm 60: struct options_entry *cmd_set_option_choice(struct cmd *, struct cmd_q *,
1.44 nicm 61: const struct options_table_entry *, struct options *,
62: const char *);
1.64 nicm 63: struct options_entry *cmd_set_option_style(struct cmd *, struct cmd_q *,
64: const struct options_table_entry *, struct options *,
65: const char *);
1.27 nicm 66:
1.1 nicm 67: const struct cmd_entry cmd_set_option_entry = {
1.91 nicm 68: .name = "set-option",
69: .alias = "set",
70:
71: .args = { "agoqst:uw", 1, 2 },
72: .usage = "[-agosquw] [-t target-window] option [value]",
73:
1.92 nicm 74: .tflag = CMD_WINDOW_CANFAIL,
75:
76: .flags = 0,
1.91 nicm 77: .exec = cmd_set_option_exec
1.1 nicm 78: };
79:
1.46 nicm 80: const struct cmd_entry cmd_set_window_option_entry = {
1.91 nicm 81: .name = "set-window-option",
82: .alias = "setw",
83:
84: .args = { "agoqt:u", 1, 2 },
85: .usage = "[-agoqu] " CMD_TARGET_WINDOW_USAGE " option [value]",
86:
1.92 nicm 87: .tflag = CMD_WINDOW_CANFAIL,
88:
89: .flags = 0,
1.91 nicm 90: .exec = cmd_set_option_exec
1.46 nicm 91: };
92:
1.57 nicm 93: enum cmd_retval
1.60 nicm 94: cmd_set_option_exec(struct cmd *self, struct cmd_q *cmdq)
1.1 nicm 95: {
1.44 nicm 96: struct args *args = self->args;
1.90 nicm 97: struct session *s = cmdq->state.tflag.s;
98: struct winlink *wl = cmdq->state.tflag.wl;
99: struct window *w;
100: struct client *c;
1.87 nicm 101: const struct options_table_entry *oe;
1.43 nicm 102: struct options *oo;
1.94 nicm 103: const char *optstr, *valstr, *target;
1.1 nicm 104:
1.50 nicm 105: /* Get the option name and value. */
106: optstr = args->argv[0];
107: if (*optstr == '\0') {
1.60 nicm 108: cmdq_error(cmdq, "invalid option");
1.57 nicm 109: return (CMD_RETURN_ERROR);
1.50 nicm 110: }
111: if (args->argc < 2)
112: valstr = NULL;
113: else
114: valstr = args->argv[1];
115:
1.59 nicm 116: /* Is this a user option? */
117: if (*optstr == '@')
1.60 nicm 118: return (cmd_set_option_user(self, cmdq, optstr, valstr));
1.59 nicm 119:
1.50 nicm 120: /* Find the option entry, try each table. */
1.87 nicm 121: oe = NULL;
122: if (options_table_find(optstr, &oe) != 0) {
1.76 nicm 123: if (!args_has(args, 'q')) {
124: cmdq_error(cmdq, "ambiguous option: %s", optstr);
125: return (CMD_RETURN_ERROR);
126: }
127: return (CMD_RETURN_NORMAL);
1.50 nicm 128: }
129: if (oe == NULL) {
1.67 nicm 130: if (!args_has(args, 'q')) {
131: cmdq_error(cmdq, "unknown option: %s", optstr);
132: return (CMD_RETURN_ERROR);
133: }
134: return (CMD_RETURN_NORMAL);
1.50 nicm 135: }
136:
1.87 nicm 137: /* Work out the tree from the scope of the option. */
138: if (oe->scope == OPTIONS_TABLE_SERVER)
1.84 nicm 139: oo = global_options;
1.87 nicm 140: else if (oe->scope == OPTIONS_TABLE_WINDOW) {
1.44 nicm 141: if (args_has(self->args, 'g'))
1.84 nicm 142: oo = global_w_options;
1.94 nicm 143: else if (wl == NULL) {
144: target = args_get(args, 't');
145: if (target != NULL) {
146: cmdq_error(cmdq, "no such window: %s",
147: target);
148: } else
149: cmdq_error(cmdq, "no current window");
150: return (CMD_RETURN_ERROR);
151: } else
1.84 nicm 152: oo = wl->window->options;
1.87 nicm 153: } else if (oe->scope == OPTIONS_TABLE_SESSION) {
1.44 nicm 154: if (args_has(self->args, 'g'))
1.84 nicm 155: oo = global_s_options;
1.94 nicm 156: else if (s == NULL) {
157: target = args_get(args, 't');
158: if (target != NULL) {
159: cmdq_error(cmdq, "no such session: %s",
160: target);
161: } else
162: cmdq_error(cmdq, "no current session");
163: return (CMD_RETURN_ERROR);
164: } else
1.84 nicm 165: oo = s->options;
1.50 nicm 166: } else {
1.60 nicm 167: cmdq_error(cmdq, "unknown table");
1.57 nicm 168: return (CMD_RETURN_ERROR);
1.1 nicm 169: }
170:
1.43 nicm 171: /* Unset or set the option. */
1.44 nicm 172: if (args_has(args, 'u')) {
1.60 nicm 173: if (cmd_set_option_unset(self, cmdq, oe, oo, valstr) != 0)
1.57 nicm 174: return (CMD_RETURN_ERROR);
1.43 nicm 175: } else {
1.61 nicm 176: if (args_has(args, 'o') && options_find1(oo, optstr) != NULL) {
1.67 nicm 177: if (!args_has(args, 'q')) {
178: cmdq_error(cmdq, "already set: %s", optstr);
179: return (CMD_RETURN_ERROR);
180: }
1.61 nicm 181: return (CMD_RETURN_NORMAL);
182: }
1.60 nicm 183: if (cmd_set_option_set(self, cmdq, oe, oo, valstr) != 0)
1.57 nicm 184: return (CMD_RETURN_ERROR);
1.55 nicm 185: }
186:
1.78 nicm 187: /* Start or stop timers if necessary. */
1.65 nicm 188: if (strcmp(oe->name, "automatic-rename") == 0) {
1.72 nicm 189: RB_FOREACH(w, windows, &windows) {
1.84 nicm 190: if (options_get_number(w->options, "automatic-rename"))
1.81 nicm 191: w->active->flags |= PANE_CHANGED;
1.55 nicm 192: }
1.89 nicm 193: }
194: if (strcmp(oe->name, "key-table") == 0) {
195: TAILQ_FOREACH(c, &clients, entry)
196: server_client_set_key_table(c, NULL);
1.1 nicm 197: }
1.77 nicm 198: if (strcmp(oe->name, "status") == 0 ||
199: strcmp(oe->name, "status-interval") == 0)
200: status_timer_start_all();
1.82 nicm 201: if (strcmp(oe->name, "monitor-silence") == 0)
202: alerts_reset_all();
1.95 ! nicm 203:
! 204: /* When the pane-border-status option has been changed, resize panes. */
! 205: if (strcmp(oe->name, "pane-border-status") == 0) {
! 206: RB_FOREACH(w, windows, &windows)
! 207: layout_fix_panes(w, w->sx, w->sy);
! 208: }
1.1 nicm 209:
1.43 nicm 210: /* Update sizes and redraw. May not need it but meh. */
1.1 nicm 211: recalculate_sizes();
1.74 nicm 212: TAILQ_FOREACH(c, &clients, entry) {
213: if (c->session != NULL)
1.27 nicm 214: server_redraw_client(c);
1.1 nicm 215: }
216:
1.57 nicm 217: return (CMD_RETURN_NORMAL);
1.27 nicm 218: }
1.59 nicm 219:
220: /* Set user option. */
221: enum cmd_retval
1.70 nicm 222: cmd_set_option_user(struct cmd *self, struct cmd_q *cmdq, const char *optstr,
1.59 nicm 223: const char *valstr)
224: {
225: struct args *args = self->args;
1.90 nicm 226: struct session *s = cmdq->state.tflag.s;
227: struct winlink *wl = cmdq->state.tflag.wl;
1.59 nicm 228: struct options *oo;
229:
230: if (args_has(args, 's'))
1.84 nicm 231: oo = global_options;
1.59 nicm 232: else if (args_has(self->args, 'w') ||
233: self->entry == &cmd_set_window_option_entry) {
234: if (args_has(self->args, 'g'))
1.84 nicm 235: oo = global_w_options;
1.90 nicm 236: else
1.84 nicm 237: oo = wl->window->options;
1.59 nicm 238: } else {
239: if (args_has(self->args, 'g'))
1.84 nicm 240: oo = global_s_options;
1.90 nicm 241: else
1.84 nicm 242: oo = s->options;
1.59 nicm 243: }
244:
245: if (args_has(args, 'u')) {
246: if (options_find1(oo, optstr) == NULL) {
1.67 nicm 247: if (!args_has(args, 'q')) {
248: cmdq_error(cmdq, "unknown option: %s", optstr);
249: return (CMD_RETURN_ERROR);
250: }
251: return (CMD_RETURN_NORMAL);
1.59 nicm 252: }
253: if (valstr != NULL) {
1.60 nicm 254: cmdq_error(cmdq, "value passed to unset option: %s",
1.59 nicm 255: optstr);
256: return (CMD_RETURN_ERROR);
257: }
258: options_remove(oo, optstr);
259: } else {
260: if (valstr == NULL) {
1.60 nicm 261: cmdq_error(cmdq, "empty value");
1.59 nicm 262: return (CMD_RETURN_ERROR);
1.61 nicm 263: }
264: if (args_has(args, 'o') && options_find1(oo, optstr) != NULL) {
1.67 nicm 265: if (!args_has(args, 'q')) {
266: cmdq_error(cmdq, "already set: %s", optstr);
1.70 nicm 267: return (CMD_RETURN_ERROR);
1.67 nicm 268: }
1.61 nicm 269: return (CMD_RETURN_NORMAL);
1.59 nicm 270: }
271: options_set_string(oo, optstr, "%s", valstr);
272: }
273: return (CMD_RETURN_NORMAL);
274: }
1.27 nicm 275:
1.43 nicm 276: /* Unset an option. */
277: int
1.60 nicm 278: cmd_set_option_unset(struct cmd *self, struct cmd_q *cmdq,
1.69 nicm 279: const struct options_table_entry *oe, struct options *oo,
280: const char *value)
1.27 nicm 281: {
1.44 nicm 282: struct args *args = self->args;
1.43 nicm 283:
1.44 nicm 284: if (value != NULL) {
1.60 nicm 285: cmdq_error(cmdq, "value passed to unset option: %s", oe->name);
1.43 nicm 286: return (-1);
1.27 nicm 287: }
1.43 nicm 288:
1.84 nicm 289: if (args_has(args, 'g') || oo == global_options) {
1.75 nicm 290: switch (oe->type) {
291: case OPTIONS_TABLE_STRING:
292: options_set_string(oo, oe->name, "%s", oe->default_str);
293: break;
294: case OPTIONS_TABLE_STYLE:
295: options_set_style(oo, oe->name, oe->default_str, 0);
296: break;
297: default:
298: options_set_number(oo, oe->name, oe->default_num);
299: break;
300: }
301: } else
302: options_remove(oo, oe->name);
1.43 nicm 303: return (0);
1.27 nicm 304: }
305:
1.43 nicm 306: /* Set an option. */
307: int
1.60 nicm 308: cmd_set_option_set(struct cmd *self, struct cmd_q *cmdq,
1.69 nicm 309: const struct options_table_entry *oe, struct options *oo,
310: const char *value)
1.27 nicm 311: {
312: struct options_entry *o;
313:
1.73 nicm 314: switch (oe->type) {
315: case OPTIONS_TABLE_FLAG:
316: case OPTIONS_TABLE_CHOICE:
317: break;
318: default:
319: if (value == NULL) {
320: cmdq_error(cmdq, "empty value");
321: return (-1);
322: }
1.27 nicm 323: }
324:
1.43 nicm 325: o = NULL;
326: switch (oe->type) {
327: case OPTIONS_TABLE_STRING:
1.60 nicm 328: o = cmd_set_option_string(self, cmdq, oe, oo, value);
1.43 nicm 329: break;
330: case OPTIONS_TABLE_NUMBER:
1.60 nicm 331: o = cmd_set_option_number(self, cmdq, oe, oo, value);
1.43 nicm 332: break;
1.52 nicm 333: case OPTIONS_TABLE_KEY:
1.60 nicm 334: o = cmd_set_option_key(self, cmdq, oe, oo, value);
1.43 nicm 335: break;
336: case OPTIONS_TABLE_COLOUR:
1.60 nicm 337: o = cmd_set_option_colour(self, cmdq, oe, oo, value);
1.66 nicm 338: if (o != NULL)
339: style_update_new(oo, o->name, oe->style);
1.43 nicm 340: break;
341: case OPTIONS_TABLE_ATTRIBUTES:
1.60 nicm 342: o = cmd_set_option_attributes(self, cmdq, oe, oo, value);
1.66 nicm 343: if (o != NULL)
344: style_update_new(oo, o->name, oe->style);
1.43 nicm 345: break;
346: case OPTIONS_TABLE_FLAG:
1.60 nicm 347: o = cmd_set_option_flag(self, cmdq, oe, oo, value);
1.43 nicm 348: break;
349: case OPTIONS_TABLE_CHOICE:
1.60 nicm 350: o = cmd_set_option_choice(self, cmdq, oe, oo, value);
1.43 nicm 351: break;
1.64 nicm 352: case OPTIONS_TABLE_STYLE:
353: o = cmd_set_option_style(self, cmdq, oe, oo, value);
354: break;
1.43 nicm 355: }
356: if (o == NULL)
357: return (-1);
358: return (0);
359: }
360:
361: /* Set a string option. */
362: struct options_entry *
1.86 nicm 363: cmd_set_option_string(struct cmd *self, __unused struct cmd_q *cmdq,
1.69 nicm 364: const struct options_table_entry *oe, struct options *oo,
365: const char *value)
1.43 nicm 366: {
1.54 nicm 367: struct args *args = self->args;
1.43 nicm 368: struct options_entry *o;
369: char *oldval, *newval;
370:
1.44 nicm 371: if (args_has(args, 'a')) {
1.43 nicm 372: oldval = options_get_string(oo, oe->name);
1.44 nicm 373: xasprintf(&newval, "%s%s", oldval, value);
1.27 nicm 374: } else
1.44 nicm 375: newval = xstrdup(value);
1.28 nicm 376:
1.43 nicm 377: o = options_set_string(oo, oe->name, "%s", newval);
1.27 nicm 378:
1.56 nicm 379: free(newval);
1.43 nicm 380: return (o);
1.27 nicm 381: }
382:
1.43 nicm 383: /* Set a number option. */
384: struct options_entry *
1.86 nicm 385: cmd_set_option_number(__unused struct cmd *self, struct cmd_q *cmdq,
1.69 nicm 386: const struct options_table_entry *oe, struct options *oo,
387: const char *value)
1.27 nicm 388: {
1.44 nicm 389: long long ll;
390: const char *errstr;
1.27 nicm 391:
1.44 nicm 392: ll = strtonum(value, oe->minimum, oe->maximum, &errstr);
1.27 nicm 393: if (errstr != NULL) {
1.60 nicm 394: cmdq_error(cmdq, "value is %s: %s", errstr, value);
1.43 nicm 395: return (NULL);
1.27 nicm 396: }
397:
1.43 nicm 398: return (options_set_number(oo, oe->name, ll));
1.27 nicm 399: }
400:
1.52 nicm 401: /* Set a key option. */
1.43 nicm 402: struct options_entry *
1.86 nicm 403: cmd_set_option_key(__unused struct cmd *self, struct cmd_q *cmdq,
1.69 nicm 404: const struct options_table_entry *oe, struct options *oo,
405: const char *value)
1.27 nicm 406: {
1.85 nicm 407: key_code key;
1.52 nicm 408:
1.88 nicm 409: key = key_string_lookup_string(value);
410: if (key == KEYC_UNKNOWN) {
1.60 nicm 411: cmdq_error(cmdq, "bad key: %s", value);
1.52 nicm 412: return (NULL);
1.27 nicm 413: }
414:
1.52 nicm 415: return (options_set_number(oo, oe->name, key));
1.27 nicm 416: }
417:
1.43 nicm 418: /* Set a colour option. */
419: struct options_entry *
1.86 nicm 420: cmd_set_option_colour(__unused struct cmd *self, struct cmd_q *cmdq,
1.69 nicm 421: const struct options_table_entry *oe, struct options *oo,
422: const char *value)
1.27 nicm 423: {
1.44 nicm 424: int colour;
1.27 nicm 425:
1.44 nicm 426: if ((colour = colour_fromstring(value)) == -1) {
1.60 nicm 427: cmdq_error(cmdq, "bad colour: %s", value);
1.43 nicm 428: return (NULL);
1.27 nicm 429: }
430:
1.43 nicm 431: return (options_set_number(oo, oe->name, colour));
1.27 nicm 432: }
433:
1.43 nicm 434: /* Set an attributes option. */
435: struct options_entry *
1.86 nicm 436: cmd_set_option_attributes(__unused struct cmd *self, struct cmd_q *cmdq,
1.69 nicm 437: const struct options_table_entry *oe, struct options *oo,
438: const char *value)
1.27 nicm 439: {
1.44 nicm 440: int attr;
1.27 nicm 441:
1.44 nicm 442: if ((attr = attributes_fromstring(value)) == -1) {
1.60 nicm 443: cmdq_error(cmdq, "bad attributes: %s", value);
1.43 nicm 444: return (NULL);
1.27 nicm 445: }
446:
1.43 nicm 447: return (options_set_number(oo, oe->name, attr));
1.27 nicm 448: }
449:
1.43 nicm 450: /* Set a flag option. */
451: struct options_entry *
1.86 nicm 452: cmd_set_option_flag(__unused struct cmd *self, struct cmd_q *cmdq,
1.69 nicm 453: const struct options_table_entry *oe, struct options *oo,
454: const char *value)
1.27 nicm 455: {
1.44 nicm 456: int flag;
1.27 nicm 457:
1.44 nicm 458: if (value == NULL || *value == '\0')
1.43 nicm 459: flag = !options_get_number(oo, oe->name);
1.27 nicm 460: else {
1.44 nicm 461: if ((value[0] == '1' && value[1] == '\0') ||
462: strcasecmp(value, "on") == 0 ||
463: strcasecmp(value, "yes") == 0)
1.27 nicm 464: flag = 1;
1.44 nicm 465: else if ((value[0] == '0' && value[1] == '\0') ||
466: strcasecmp(value, "off") == 0 ||
467: strcasecmp(value, "no") == 0)
1.27 nicm 468: flag = 0;
469: else {
1.60 nicm 470: cmdq_error(cmdq, "bad value: %s", value);
1.43 nicm 471: return (NULL);
1.27 nicm 472: }
473: }
474:
1.43 nicm 475: return (options_set_number(oo, oe->name, flag));
1.27 nicm 476: }
477:
1.43 nicm 478: /* Set a choice option. */
479: struct options_entry *
1.86 nicm 480: cmd_set_option_choice(__unused struct cmd *self, struct cmd_q *cmdq,
1.60 nicm 481: const struct options_table_entry *oe, struct options *oo,
482: const char *value)
1.27 nicm 483: {
1.44 nicm 484: const char **choicep;
485: int n, choice = -1;
1.27 nicm 486:
1.73 nicm 487: if (value == NULL) {
488: choice = options_get_number(oo, oe->name);
489: if (choice < 2)
490: choice = !choice;
491: } else {
492: n = 0;
493: for (choicep = oe->choices; *choicep != NULL; choicep++) {
494: n++;
495: if (strncmp(*choicep, value, strlen(value)) != 0)
496: continue;
497:
498: if (choice != -1) {
499: cmdq_error(cmdq, "ambiguous value: %s", value);
500: return (NULL);
501: }
502: choice = n - 1;
503: }
504: if (choice == -1) {
505: cmdq_error(cmdq, "unknown value: %s", value);
1.43 nicm 506: return (NULL);
1.27 nicm 507: }
508: }
509:
1.43 nicm 510: return (options_set_number(oo, oe->name, choice));
1.64 nicm 511: }
512:
513: /* Set a style option. */
514: struct options_entry *
515: cmd_set_option_style(struct cmd *self, struct cmd_q *cmdq,
516: const struct options_table_entry *oe, struct options *oo,
517: const char *value)
518: {
519: struct args *args = self->args;
520: struct options_entry *o;
521: int append;
522:
523: append = args_has(args, 'a');
524: if ((o = options_set_style(oo, oe->name, value, append)) == NULL) {
525: cmdq_error(cmdq, "bad style: %s", value);
526: return (NULL);
527: }
528:
529: style_update_old(oo, oe->name, &o->style);
530: return (o);
1.1 nicm 531: }