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