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