Annotation of src/usr.bin/tmux/cmd-set-option.c, Revision 1.124
1.124 ! nicm 1: /* $OpenBSD: cmd-set-option.c,v 1.123 2019/03/18 21:46:01 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:
1.120 nicm 21: #include <fnmatch.h>
1.1 nicm 22: #include <stdlib.h>
23: #include <string.h>
24:
25: #include "tmux.h"
26:
27: /*
28: * Set an option.
29: */
30:
1.101 nicm 31: static enum cmd_retval cmd_set_option_exec(struct cmd *, struct cmdq_item *);
1.59 nicm 32:
1.105 nicm 33: static int cmd_set_option_set(struct cmd *, struct cmdq_item *,
1.107 nicm 34: struct options *, struct options_entry *, const char *);
1.105 nicm 35: static int cmd_set_option_flag(struct cmdq_item *,
1.99 nicm 36: const struct options_table_entry *, struct options *,
37: const char *);
1.105 nicm 38: static int cmd_set_option_choice(struct cmdq_item *,
1.99 nicm 39: const struct options_table_entry *, struct options *,
40: const char *);
1.43 nicm 41:
1.1 nicm 42: const struct cmd_entry cmd_set_option_entry = {
1.91 nicm 43: .name = "set-option",
44: .alias = "set",
45:
1.115 nicm 46: .args = { "aFgoqst:uw", 1, 2 },
47: .usage = "[-aFgosquw] [-t target-window] option [value]",
1.91 nicm 48:
1.114 nicm 49: .target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
1.92 nicm 50:
1.100 nicm 51: .flags = CMD_AFTERHOOK,
1.91 nicm 52: .exec = cmd_set_option_exec
1.1 nicm 53: };
54:
1.46 nicm 55: const struct cmd_entry cmd_set_window_option_entry = {
1.91 nicm 56: .name = "set-window-option",
57: .alias = "setw",
58:
1.115 nicm 59: .args = { "aFgoqt:u", 1, 2 },
60: .usage = "[-aFgoqu] " CMD_TARGET_WINDOW_USAGE " option [value]",
1.91 nicm 61:
1.114 nicm 62: .target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
1.92 nicm 63:
1.100 nicm 64: .flags = CMD_AFTERHOOK,
1.91 nicm 65: .exec = cmd_set_option_exec
1.46 nicm 66: };
67:
1.99 nicm 68: static enum cmd_retval
1.101 nicm 69: cmd_set_option_exec(struct cmd *self, struct cmdq_item *item)
1.1 nicm 70: {
1.105 nicm 71: struct args *args = self->args;
1.109 nicm 72: int append = args_has(args, 'a');
1.114 nicm 73: struct cmd_find_state *fs = &item->target;
1.115 nicm 74: struct client *c, *loop;
1.105 nicm 75: struct session *s = fs->s;
76: struct winlink *wl = fs->wl;
1.112 nicm 77: struct window *w;
1.105 nicm 78: enum options_table_scope scope;
79: struct options *oo;
1.107 nicm 80: struct options_entry *parent, *o;
1.115 nicm 81: char *name, *argument, *value = NULL, *cause;
82: const char *target;
1.105 nicm 83: int window, idx, already, error, ambiguous;
1.123 nicm 84: struct style *sy;
1.115 nicm 85:
86: /* Expand argument. */
87: c = cmd_find_client(item, NULL, 1);
88: argument = format_single(item, args->argv[0], c, s, wl, NULL);
1.105 nicm 89:
90: /* Parse option name and index. */
1.115 nicm 91: name = options_match(argument, &idx, &ambiguous);
1.105 nicm 92: if (name == NULL) {
1.106 nicm 93: if (args_has(args, 'q'))
1.115 nicm 94: goto out;
1.105 nicm 95: if (ambiguous)
1.115 nicm 96: cmdq_error(item, "ambiguous option: %s", argument);
1.105 nicm 97: else
1.115 nicm 98: cmdq_error(item, "invalid option: %s", argument);
99: goto fail;
1.50 nicm 100: }
101: if (args->argc < 2)
1.105 nicm 102: value = NULL;
1.115 nicm 103: else if (args_has(args, 'F'))
104: value = format_single(item, args->argv[1], c, s, wl, NULL);
1.50 nicm 105: else
1.115 nicm 106: value = xstrdup(args->argv[1]);
1.50 nicm 107:
1.105 nicm 108: /*
109: * Figure out the scope: for user options it comes from the arguments,
110: * otherwise from the option name.
111: */
112: if (*name == '@') {
113: window = (self->entry == &cmd_set_window_option_entry);
114: scope = options_scope_from_flags(args, window, fs, &oo, &cause);
115: } else {
116: if (options_get_only(global_options, name) != NULL)
117: scope = OPTIONS_TABLE_SERVER;
118: else if (options_get_only(global_s_options, name) != NULL)
119: scope = OPTIONS_TABLE_SESSION;
120: else if (options_get_only(global_w_options, name) != NULL)
121: scope = OPTIONS_TABLE_WINDOW;
122: else {
123: scope = OPTIONS_TABLE_NONE;
1.115 nicm 124: xasprintf(&cause, "unknown option: %s", argument);
1.76 nicm 125: }
1.50 nicm 126: }
1.105 nicm 127: if (scope == OPTIONS_TABLE_NONE) {
1.106 nicm 128: if (args_has(args, 'q'))
1.115 nicm 129: goto out;
1.105 nicm 130: cmdq_error(item, "%s", cause);
131: free(cause);
1.113 nicm 132: goto fail;
1.50 nicm 133: }
134:
1.105 nicm 135: /* Which table should this option go into? */
136: if (scope == OPTIONS_TABLE_SERVER)
1.84 nicm 137: oo = global_options;
1.105 nicm 138: else if (scope == OPTIONS_TABLE_SESSION) {
139: if (args_has(self->args, 'g'))
140: oo = global_s_options;
141: else if (s == NULL) {
142: target = args_get(args, 't');
143: if (target != NULL)
144: cmdq_error(item, "no such session: %s", target);
145: else
146: cmdq_error(item, "no current session");
1.113 nicm 147: goto fail;
1.105 nicm 148: } else
149: oo = s->options;
150: } else if (scope == OPTIONS_TABLE_WINDOW) {
1.44 nicm 151: if (args_has(self->args, 'g'))
1.84 nicm 152: oo = global_w_options;
1.94 nicm 153: else if (wl == NULL) {
154: target = args_get(args, 't');
1.105 nicm 155: if (target != NULL)
156: cmdq_error(item, "no such window: %s", target);
157: else
1.101 nicm 158: cmdq_error(item, "no current window");
1.113 nicm 159: goto fail;
1.94 nicm 160: } else
1.84 nicm 161: oo = wl->window->options;
1.105 nicm 162: }
163: o = options_get_only(oo, name);
164: parent = options_get(oo, name);
165:
166: /* Check that array options and indexes match up. */
1.122 nicm 167: if (idx != -1 && (*name == '@' || !options_isarray(parent))) {
168: cmdq_error(item, "not an array: %s", argument);
169: goto fail;
1.105 nicm 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);
1.122 nicm 211: } else if (idx == -1 && !options_isarray(parent)) {
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.123 nicm 251: if (strcmp(name, "status-fg") == 0 || strcmp(name, "status-bg") == 0) {
252: sy = options_get_style(oo, "status-style");
253: sy->gc.fg = options_get_number(oo, "status-fg");
254: sy->gc.bg = options_get_number(oo, "status-bg");
255: }
256: if (strcmp(name, "status-style") == 0) {
257: sy = options_get_style(oo, "status-style");
258: options_set_number(oo, "status-fg", sy->gc.fg);
259: options_set_number(oo, "status-bg", sy->gc.bg);
260: }
1.105 nicm 261: if (strcmp(name, "status") == 0 ||
262: strcmp(name, "status-interval") == 0)
1.77 nicm 263: status_timer_start_all();
1.105 nicm 264: if (strcmp(name, "monitor-silence") == 0)
1.82 nicm 265: alerts_reset_all();
1.105 nicm 266: if (strcmp(name, "window-style") == 0 ||
267: strcmp(name, "window-active-style") == 0) {
1.96 nicm 268: RB_FOREACH(w, windows, &windows)
269: w->flags |= WINDOW_STYLECHANGED;
270: }
1.105 nicm 271: if (strcmp(name, "pane-border-status") == 0) {
1.95 nicm 272: RB_FOREACH(w, windows, &windows)
1.120 nicm 273: layout_fix_panes(w);
1.95 nicm 274: }
1.116 nicm 275: RB_FOREACH(s, sessions, &sessions)
1.121 nicm 276: status_update_cache(s);
1.1 nicm 277:
1.105 nicm 278: /*
279: * Update sizes and redraw. May not always be necessary but do it
280: * anyway.
281: */
1.1 nicm 282: recalculate_sizes();
1.115 nicm 283: TAILQ_FOREACH(loop, &clients, entry) {
284: if (loop->session != NULL)
285: server_redraw_client(loop);
1.1 nicm 286: }
287:
1.115 nicm 288: out:
289: free(argument);
290: free(value);
1.113 nicm 291: free(name);
1.57 nicm 292: return (CMD_RETURN_NORMAL);
1.113 nicm 293:
294: fail:
1.115 nicm 295: free(argument);
296: free(value);
1.113 nicm 297: free(name);
298: return (CMD_RETURN_ERROR);
1.27 nicm 299: }
1.59 nicm 300:
1.99 nicm 301: static int
1.105 nicm 302: cmd_set_option_set(struct cmd *self, struct cmdq_item *item, struct options *oo,
1.107 nicm 303: struct options_entry *parent, const char *value)
1.27 nicm 304: {
1.105 nicm 305: const struct options_table_entry *oe;
306: struct args *args = self->args;
307: int append = args_has(args, 'a');
1.107 nicm 308: struct options_entry *o;
1.105 nicm 309: long long number;
1.120 nicm 310: const char *errstr, *new;
311: char *old;
1.105 nicm 312: key_code key;
313:
314: oe = options_table_entry(parent);
315: if (value == NULL &&
316: oe->type != OPTIONS_TABLE_FLAG &&
317: oe->type != OPTIONS_TABLE_CHOICE) {
318: cmdq_error(item, "empty value");
1.43 nicm 319: return (-1);
1.27 nicm 320: }
1.43 nicm 321:
322: switch (oe->type) {
323: case OPTIONS_TABLE_STRING:
1.120 nicm 324: old = xstrdup(options_get_string(oo, oe->name));
1.105 nicm 325: options_set_string(oo, oe->name, append, "%s", value);
1.120 nicm 326: new = options_get_string(oo, oe->name);
327: if (oe->pattern != NULL && fnmatch(oe->pattern, new, 0) != 0) {
328: options_set_string(oo, oe->name, 0, "%s", old);
329: free(old);
330: cmdq_error(item, "value is invalid: %s", value);
331: return (-1);
332: }
333: free(old);
1.105 nicm 334: return (0);
1.43 nicm 335: case OPTIONS_TABLE_NUMBER:
1.105 nicm 336: number = strtonum(value, oe->minimum, oe->maximum, &errstr);
337: if (errstr != NULL) {
338: cmdq_error(item, "value is %s: %s", errstr, value);
339: return (-1);
340: }
341: options_set_number(oo, oe->name, number);
342: return (0);
1.52 nicm 343: case OPTIONS_TABLE_KEY:
1.105 nicm 344: key = key_string_lookup_string(value);
345: if (key == KEYC_UNKNOWN) {
346: cmdq_error(item, "bad key: %s", value);
347: return (-1);
348: }
349: options_set_number(oo, oe->name, key);
350: return (0);
1.43 nicm 351: case OPTIONS_TABLE_COLOUR:
1.105 nicm 352: if ((number = colour_fromstring(value)) == -1) {
353: cmdq_error(item, "bad colour: %s", value);
354: return (-1);
355: }
1.123 nicm 356: options_set_number(oo, oe->name, number);
1.105 nicm 357: return (0);
1.43 nicm 358: case OPTIONS_TABLE_FLAG:
1.105 nicm 359: return (cmd_set_option_flag(item, oe, oo, value));
1.43 nicm 360: case OPTIONS_TABLE_CHOICE:
1.105 nicm 361: return (cmd_set_option_choice(item, oe, oo, value));
1.64 nicm 362: case OPTIONS_TABLE_STYLE:
1.105 nicm 363: o = options_set_style(oo, oe->name, append, value);
364: if (o == NULL) {
365: cmdq_error(item, "bad style: %s", value);
366: return (-1);
367: }
368: return (0);
1.43 nicm 369: }
1.105 nicm 370: return (-1);
1.43 nicm 371: }
372:
1.105 nicm 373: static int
374: cmd_set_option_flag(struct cmdq_item *item,
1.69 nicm 375: const struct options_table_entry *oe, struct options *oo,
376: const char *value)
1.27 nicm 377: {
1.44 nicm 378: int flag;
1.27 nicm 379:
1.44 nicm 380: if (value == NULL || *value == '\0')
1.43 nicm 381: flag = !options_get_number(oo, oe->name);
1.105 nicm 382: else if (strcmp(value, "1") == 0 ||
383: strcasecmp(value, "on") == 0 ||
384: strcasecmp(value, "yes") == 0)
385: flag = 1;
386: else if (strcmp(value, "0") == 0 ||
387: strcasecmp(value, "off") == 0 ||
388: strcasecmp(value, "no") == 0)
389: flag = 0;
1.27 nicm 390: else {
1.105 nicm 391: cmdq_error(item, "bad value: %s", value);
392: return (-1);
1.27 nicm 393: }
1.105 nicm 394: options_set_number(oo, oe->name, flag);
395: return (0);
1.27 nicm 396: }
397:
1.105 nicm 398: static int
399: cmd_set_option_choice(struct cmdq_item *item,
1.60 nicm 400: const struct options_table_entry *oe, struct options *oo,
401: const char *value)
1.27 nicm 402: {
1.105 nicm 403: const char **cp;
1.44 nicm 404: int n, choice = -1;
1.27 nicm 405:
1.73 nicm 406: if (value == NULL) {
407: choice = options_get_number(oo, oe->name);
408: if (choice < 2)
409: choice = !choice;
410: } else {
411: n = 0;
1.105 nicm 412: for (cp = oe->choices; *cp != NULL; cp++) {
413: if (strcmp(*cp, value) == 0)
414: choice = n;
1.73 nicm 415: n++;
416: }
417: if (choice == -1) {
1.101 nicm 418: cmdq_error(item, "unknown value: %s", value);
1.105 nicm 419: return (-1);
1.27 nicm 420: }
421: }
1.105 nicm 422: options_set_number(oo, oe->name, choice);
423: return (0);
1.1 nicm 424: }