Annotation of src/usr.bin/tmux/cmd-set-option.c, Revision 1.119
1.119 ! nicm 1: /* $OpenBSD: cmd-set-option.c,v 1.118 2017/09/07 13:18:44 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.101 nicm 30: static enum cmd_retval cmd_set_option_exec(struct cmd *, struct cmdq_item *);
1.59 nicm 31:
1.105 nicm 32: static int cmd_set_option_set(struct cmd *, struct cmdq_item *,
1.107 nicm 33: struct options *, struct options_entry *, const char *);
1.105 nicm 34: static int cmd_set_option_flag(struct cmdq_item *,
1.99 nicm 35: const struct options_table_entry *, struct options *,
36: const char *);
1.105 nicm 37: static int cmd_set_option_choice(struct cmdq_item *,
1.99 nicm 38: const struct options_table_entry *, struct options *,
39: const char *);
1.43 nicm 40:
1.1 nicm 41: const struct cmd_entry cmd_set_option_entry = {
1.91 nicm 42: .name = "set-option",
43: .alias = "set",
44:
1.115 nicm 45: .args = { "aFgoqst:uw", 1, 2 },
46: .usage = "[-aFgosquw] [-t target-window] option [value]",
1.91 nicm 47:
1.114 nicm 48: .target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
1.92 nicm 49:
1.100 nicm 50: .flags = CMD_AFTERHOOK,
1.91 nicm 51: .exec = cmd_set_option_exec
1.1 nicm 52: };
53:
1.46 nicm 54: const struct cmd_entry cmd_set_window_option_entry = {
1.91 nicm 55: .name = "set-window-option",
56: .alias = "setw",
57:
1.115 nicm 58: .args = { "aFgoqt:u", 1, 2 },
59: .usage = "[-aFgoqu] " CMD_TARGET_WINDOW_USAGE " option [value]",
1.91 nicm 60:
1.114 nicm 61: .target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
1.92 nicm 62:
1.100 nicm 63: .flags = CMD_AFTERHOOK,
1.91 nicm 64: .exec = cmd_set_option_exec
1.46 nicm 65: };
66:
1.99 nicm 67: static enum cmd_retval
1.101 nicm 68: cmd_set_option_exec(struct cmd *self, struct cmdq_item *item)
1.1 nicm 69: {
1.105 nicm 70: struct args *args = self->args;
1.109 nicm 71: int append = args_has(args, 'a');
1.114 nicm 72: struct cmd_find_state *fs = &item->target;
1.115 nicm 73: struct client *c, *loop;
1.105 nicm 74: struct session *s = fs->s;
75: struct winlink *wl = fs->wl;
1.112 nicm 76: struct window *w;
1.105 nicm 77: enum options_table_scope scope;
78: struct options *oo;
1.107 nicm 79: struct options_entry *parent, *o;
1.115 nicm 80: char *name, *argument, *value = NULL, *cause;
81: const char *target;
1.105 nicm 82: int window, idx, already, error, ambiguous;
1.115 nicm 83:
84: /* Expand argument. */
85: c = cmd_find_client(item, NULL, 1);
86: argument = format_single(item, args->argv[0], c, s, wl, NULL);
1.105 nicm 87:
88: /* Parse option name and index. */
1.115 nicm 89: name = options_match(argument, &idx, &ambiguous);
1.105 nicm 90: if (name == NULL) {
1.106 nicm 91: if (args_has(args, 'q'))
1.115 nicm 92: goto out;
1.105 nicm 93: if (ambiguous)
1.115 nicm 94: cmdq_error(item, "ambiguous option: %s", argument);
1.105 nicm 95: else
1.115 nicm 96: cmdq_error(item, "invalid option: %s", argument);
97: goto fail;
1.50 nicm 98: }
99: if (args->argc < 2)
1.105 nicm 100: value = NULL;
1.115 nicm 101: else if (args_has(args, 'F'))
102: value = format_single(item, args->argv[1], c, s, wl, NULL);
1.50 nicm 103: else
1.115 nicm 104: value = xstrdup(args->argv[1]);
1.50 nicm 105:
1.105 nicm 106: /*
107: * Figure out the scope: for user options it comes from the arguments,
108: * otherwise from the option name.
109: */
110: if (*name == '@') {
111: window = (self->entry == &cmd_set_window_option_entry);
112: scope = options_scope_from_flags(args, window, fs, &oo, &cause);
113: } else {
114: if (options_get_only(global_options, name) != NULL)
115: scope = OPTIONS_TABLE_SERVER;
116: else if (options_get_only(global_s_options, name) != NULL)
117: scope = OPTIONS_TABLE_SESSION;
118: else if (options_get_only(global_w_options, name) != NULL)
119: scope = OPTIONS_TABLE_WINDOW;
120: else {
121: scope = OPTIONS_TABLE_NONE;
1.115 nicm 122: xasprintf(&cause, "unknown option: %s", argument);
1.76 nicm 123: }
1.50 nicm 124: }
1.105 nicm 125: if (scope == OPTIONS_TABLE_NONE) {
1.106 nicm 126: if (args_has(args, 'q'))
1.115 nicm 127: goto out;
1.105 nicm 128: cmdq_error(item, "%s", cause);
129: free(cause);
1.113 nicm 130: goto fail;
1.50 nicm 131: }
132:
1.105 nicm 133: /* Which table should this option go into? */
134: if (scope == OPTIONS_TABLE_SERVER)
1.84 nicm 135: oo = global_options;
1.105 nicm 136: else if (scope == OPTIONS_TABLE_SESSION) {
137: if (args_has(self->args, 'g'))
138: oo = global_s_options;
139: else if (s == NULL) {
140: target = args_get(args, 't');
141: if (target != NULL)
142: cmdq_error(item, "no such session: %s", target);
143: else
144: cmdq_error(item, "no current session");
1.113 nicm 145: goto fail;
1.105 nicm 146: } else
147: oo = s->options;
148: } else if (scope == OPTIONS_TABLE_WINDOW) {
1.44 nicm 149: if (args_has(self->args, 'g'))
1.84 nicm 150: oo = global_w_options;
1.94 nicm 151: else if (wl == NULL) {
152: target = args_get(args, 't');
1.105 nicm 153: if (target != NULL)
154: cmdq_error(item, "no such window: %s", target);
155: else
1.101 nicm 156: cmdq_error(item, "no current window");
1.113 nicm 157: goto fail;
1.94 nicm 158: } else
1.84 nicm 159: oo = wl->window->options;
1.105 nicm 160: }
161: o = options_get_only(oo, name);
162: parent = options_get(oo, name);
163:
164: /* Check that array options and indexes match up. */
165: if (idx != -1) {
166: if (*name == '@' || options_array_size(parent, NULL) == -1) {
1.115 nicm 167: cmdq_error(item, "not an array: %s", argument);
1.113 nicm 168: goto fail;
1.105 nicm 169: }
170: }
171:
172: /* With -o, check this option is not already set. */
173: if (!args_has(args, 'u') && args_has(args, 'o')) {
174: if (idx == -1)
175: already = (o != NULL);
176: else {
177: if (o == NULL)
178: already = 0;
179: else
180: already = (options_array_get(o, idx) != NULL);
181: }
182: if (already) {
183: if (args_has(args, 'q'))
1.115 nicm 184: goto out;
185: cmdq_error(item, "already set: %s", argument);
1.113 nicm 186: goto fail;
1.105 nicm 187: }
1.1 nicm 188: }
189:
1.105 nicm 190: /* Change the option. */
1.44 nicm 191: if (args_has(args, 'u')) {
1.105 nicm 192: if (o == NULL)
1.118 nicm 193: goto out;
1.105 nicm 194: if (idx == -1) {
1.119 ! nicm 195: if (*name == '@')
! 196: options_remove(o);
! 197: else if (oo == global_options ||
1.105 nicm 198: oo == global_s_options ||
199: oo == global_w_options)
200: options_default(oo, options_table_entry(o));
201: else
202: options_remove(o);
203: } else
1.108 nicm 204: options_array_set(o, idx, NULL, 0);
1.109 nicm 205: } else if (*name == '@') {
206: if (value == NULL) {
207: cmdq_error(item, "empty value");
1.113 nicm 208: goto fail;
1.109 nicm 209: }
210: options_set_string(oo, name, append, "%s", value);
211: } else if (idx == -1 && options_array_size(parent, NULL) == -1) {
1.105 nicm 212: error = cmd_set_option_set(self, item, oo, parent, value);
213: if (error != 0)
1.113 nicm 214: goto fail;
1.43 nicm 215: } else {
1.109 nicm 216: if (value == NULL) {
217: cmdq_error(item, "empty value");
1.113 nicm 218: goto fail;
1.109 nicm 219: }
1.105 nicm 220: if (o == NULL)
221: o = options_empty(oo, options_table_entry(parent));
1.110 nicm 222: if (idx == -1) {
223: if (!append)
224: options_array_clear(o);
1.109 nicm 225: options_array_assign(o, value);
1.110 nicm 226: } else if (options_array_set(o, idx, value, append) != 0) {
1.115 nicm 227: cmdq_error(item, "invalid index: %s", argument);
1.113 nicm 228: goto fail;
1.61 nicm 229: }
1.55 nicm 230: }
231:
1.103 nicm 232: /* Update timers and so on for various options. */
1.105 nicm 233: if (strcmp(name, "automatic-rename") == 0) {
1.72 nicm 234: RB_FOREACH(w, windows, &windows) {
1.102 nicm 235: if (w->active == NULL)
236: continue;
1.84 nicm 237: if (options_get_number(w->options, "automatic-rename"))
1.81 nicm 238: w->active->flags |= PANE_CHANGED;
1.55 nicm 239: }
1.89 nicm 240: }
1.105 nicm 241: if (strcmp(name, "key-table") == 0) {
1.115 nicm 242: TAILQ_FOREACH(loop, &clients, entry)
243: server_client_set_key_table(loop, NULL);
1.117 nicm 244: }
245: if (strcmp(name, "user-keys") == 0) {
246: TAILQ_FOREACH(loop, &clients, entry) {
247: if (loop->tty.flags & TTY_OPENED)
248: tty_keys_build(&loop->tty);
249: }
1.1 nicm 250: }
1.105 nicm 251: if (strcmp(name, "status") == 0 ||
252: strcmp(name, "status-interval") == 0)
1.77 nicm 253: status_timer_start_all();
1.105 nicm 254: if (strcmp(name, "monitor-silence") == 0)
1.82 nicm 255: alerts_reset_all();
1.105 nicm 256: if (strcmp(name, "window-style") == 0 ||
257: strcmp(name, "window-active-style") == 0) {
1.96 nicm 258: RB_FOREACH(w, windows, &windows)
259: w->flags |= WINDOW_STYLECHANGED;
260: }
1.105 nicm 261: if (strcmp(name, "pane-border-status") == 0) {
1.95 nicm 262: RB_FOREACH(w, windows, &windows)
263: layout_fix_panes(w, w->sx, w->sy);
264: }
1.116 nicm 265: RB_FOREACH(s, sessions, &sessions)
1.111 nicm 266: status_update_saved(s);
1.1 nicm 267:
1.105 nicm 268: /*
269: * Update sizes and redraw. May not always be necessary but do it
270: * anyway.
271: */
1.1 nicm 272: recalculate_sizes();
1.115 nicm 273: TAILQ_FOREACH(loop, &clients, entry) {
274: if (loop->session != NULL)
275: server_redraw_client(loop);
1.1 nicm 276: }
277:
1.115 nicm 278: out:
279: free(argument);
280: free(value);
1.113 nicm 281: free(name);
1.57 nicm 282: return (CMD_RETURN_NORMAL);
1.113 nicm 283:
284: fail:
1.115 nicm 285: free(argument);
286: free(value);
1.113 nicm 287: free(name);
288: return (CMD_RETURN_ERROR);
1.27 nicm 289: }
1.59 nicm 290:
1.99 nicm 291: static int
1.105 nicm 292: cmd_set_option_set(struct cmd *self, struct cmdq_item *item, struct options *oo,
1.107 nicm 293: struct options_entry *parent, const char *value)
1.27 nicm 294: {
1.105 nicm 295: const struct options_table_entry *oe;
296: struct args *args = self->args;
297: int append = args_has(args, 'a');
1.107 nicm 298: struct options_entry *o;
1.105 nicm 299: long long number;
300: const char *errstr;
301: key_code key;
302:
303: oe = options_table_entry(parent);
304: if (value == NULL &&
305: oe->type != OPTIONS_TABLE_FLAG &&
306: oe->type != OPTIONS_TABLE_CHOICE) {
307: cmdq_error(item, "empty value");
1.43 nicm 308: return (-1);
1.27 nicm 309: }
1.43 nicm 310:
311: switch (oe->type) {
312: case OPTIONS_TABLE_STRING:
1.105 nicm 313: options_set_string(oo, oe->name, append, "%s", value);
314: return (0);
1.43 nicm 315: case OPTIONS_TABLE_NUMBER:
1.105 nicm 316: number = strtonum(value, oe->minimum, oe->maximum, &errstr);
317: if (errstr != NULL) {
318: cmdq_error(item, "value is %s: %s", errstr, value);
319: return (-1);
320: }
321: options_set_number(oo, oe->name, number);
322: return (0);
1.52 nicm 323: case OPTIONS_TABLE_KEY:
1.105 nicm 324: key = key_string_lookup_string(value);
325: if (key == KEYC_UNKNOWN) {
326: cmdq_error(item, "bad key: %s", value);
327: return (-1);
328: }
329: options_set_number(oo, oe->name, key);
330: return (0);
1.43 nicm 331: case OPTIONS_TABLE_COLOUR:
1.105 nicm 332: if ((number = colour_fromstring(value)) == -1) {
333: cmdq_error(item, "bad colour: %s", value);
334: return (-1);
335: }
336: o = options_set_number(oo, oe->name, number);
337: options_style_update_new(oo, o);
338: return (0);
1.43 nicm 339: case OPTIONS_TABLE_ATTRIBUTES:
1.105 nicm 340: if ((number = attributes_fromstring(value)) == -1) {
341: cmdq_error(item, "bad attributes: %s", value);
342: return (-1);
343: }
344: o = options_set_number(oo, oe->name, number);
345: options_style_update_new(oo, o);
346: return (0);
1.43 nicm 347: case OPTIONS_TABLE_FLAG:
1.105 nicm 348: return (cmd_set_option_flag(item, oe, oo, value));
1.43 nicm 349: case OPTIONS_TABLE_CHOICE:
1.105 nicm 350: return (cmd_set_option_choice(item, oe, oo, value));
1.64 nicm 351: case OPTIONS_TABLE_STYLE:
1.105 nicm 352: o = options_set_style(oo, oe->name, append, value);
353: if (o == NULL) {
354: cmdq_error(item, "bad style: %s", value);
355: return (-1);
356: }
357: options_style_update_old(oo, o);
358: return (0);
359: case OPTIONS_TABLE_ARRAY:
1.64 nicm 360: break;
1.43 nicm 361: }
1.105 nicm 362: return (-1);
1.43 nicm 363: }
364:
1.105 nicm 365: static int
366: cmd_set_option_flag(struct cmdq_item *item,
1.69 nicm 367: const struct options_table_entry *oe, struct options *oo,
368: const char *value)
1.27 nicm 369: {
1.44 nicm 370: int flag;
1.27 nicm 371:
1.44 nicm 372: if (value == NULL || *value == '\0')
1.43 nicm 373: flag = !options_get_number(oo, oe->name);
1.105 nicm 374: else if (strcmp(value, "1") == 0 ||
375: strcasecmp(value, "on") == 0 ||
376: strcasecmp(value, "yes") == 0)
377: flag = 1;
378: else if (strcmp(value, "0") == 0 ||
379: strcasecmp(value, "off") == 0 ||
380: strcasecmp(value, "no") == 0)
381: flag = 0;
1.27 nicm 382: else {
1.105 nicm 383: cmdq_error(item, "bad value: %s", value);
384: return (-1);
1.27 nicm 385: }
1.105 nicm 386: options_set_number(oo, oe->name, flag);
387: return (0);
1.27 nicm 388: }
389:
1.105 nicm 390: static int
391: cmd_set_option_choice(struct cmdq_item *item,
1.60 nicm 392: const struct options_table_entry *oe, struct options *oo,
393: const char *value)
1.27 nicm 394: {
1.105 nicm 395: const char **cp;
1.44 nicm 396: int n, choice = -1;
1.27 nicm 397:
1.73 nicm 398: if (value == NULL) {
399: choice = options_get_number(oo, oe->name);
400: if (choice < 2)
401: choice = !choice;
402: } else {
403: n = 0;
1.105 nicm 404: for (cp = oe->choices; *cp != NULL; cp++) {
405: if (strcmp(*cp, value) == 0)
406: choice = n;
1.73 nicm 407: n++;
408: }
409: if (choice == -1) {
1.101 nicm 410: cmdq_error(item, "unknown value: %s", value);
1.105 nicm 411: return (-1);
1.27 nicm 412: }
413: }
1.105 nicm 414: options_set_number(oo, oe->name, choice);
415: return (0);
1.1 nicm 416: }