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