Annotation of src/usr.bin/tmux/cmd-set-option.c, Revision 1.134
1.134 ! nicm 1: /* $OpenBSD: cmd-set-option.c,v 1.133 2020/04/13 20:54:15 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.127 nicm 46: .args = { "aFgopqst:uw", 1, 2 },
47: .usage = "[-aFgopqsuw] " CMD_TARGET_PANE_USAGE " option [value]",
1.91 nicm 48:
1.127 nicm 49: .target = { 't', CMD_FIND_PANE, 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.125 nicm 68: const struct cmd_entry cmd_set_hook_entry = {
69: .name = "set-hook",
70: .alias = NULL,
71:
1.129 nicm 72: .args = { "agpRt:uw", 1, 2 },
73: .usage = "[-agpRuw] " CMD_TARGET_PANE_USAGE " hook [command]",
1.125 nicm 74:
1.129 nicm 75: .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
1.125 nicm 76:
77: .flags = CMD_AFTERHOOK,
78: .exec = cmd_set_option_exec
79: };
80:
1.99 nicm 81: static enum cmd_retval
1.101 nicm 82: cmd_set_option_exec(struct cmd *self, struct cmdq_item *item)
1.1 nicm 83: {
1.130 nicm 84: struct args *args = cmd_get_args(self);
1.109 nicm 85: int append = args_has(args, 'a');
1.131 nicm 86: struct cmd_find_state *target = cmdq_get_target(item);
1.132 nicm 87: struct client *loop;
1.131 nicm 88: struct session *s = target->s;
1.112 nicm 89: struct window *w;
1.127 nicm 90: struct window_pane *wp;
1.105 nicm 91: struct options *oo;
1.107 nicm 92: struct options_entry *parent, *o;
1.115 nicm 93: char *name, *argument, *value = NULL, *cause;
1.105 nicm 94: int window, idx, already, error, ambiguous;
1.127 nicm 95: int scope;
1.115 nicm 96:
1.130 nicm 97: window = (cmd_get_entry(self) == &cmd_set_window_option_entry);
1.126 nicm 98:
1.115 nicm 99: /* Expand argument. */
1.132 nicm 100: argument = format_single_from_target(item, args->argv[0]);
1.105 nicm 101:
1.126 nicm 102: /* If set-hook -R, fire the hook straight away. */
1.130 nicm 103: if (cmd_get_entry(self) == &cmd_set_hook_entry && args_has(args, 'R')) {
1.125 nicm 104: notify_hook(item, argument);
1.126 nicm 105: free(argument);
1.125 nicm 106: return (CMD_RETURN_NORMAL);
107: }
108:
1.105 nicm 109: /* Parse option name and index. */
1.115 nicm 110: name = options_match(argument, &idx, &ambiguous);
1.105 nicm 111: if (name == NULL) {
1.106 nicm 112: if (args_has(args, 'q'))
1.115 nicm 113: goto out;
1.105 nicm 114: if (ambiguous)
1.115 nicm 115: cmdq_error(item, "ambiguous option: %s", argument);
1.105 nicm 116: else
1.115 nicm 117: cmdq_error(item, "invalid option: %s", argument);
118: goto fail;
1.50 nicm 119: }
120: if (args->argc < 2)
1.105 nicm 121: value = NULL;
1.115 nicm 122: else if (args_has(args, 'F'))
1.132 nicm 123: value = format_single_from_target(item, args->argv[1]);
1.50 nicm 124: else
1.115 nicm 125: value = xstrdup(args->argv[1]);
1.50 nicm 126:
1.126 nicm 127: /* Get the scope and table for the option .*/
1.131 nicm 128: scope = options_scope_from_name(args, window, name, target, &oo,
129: &cause);
1.105 nicm 130: if (scope == OPTIONS_TABLE_NONE) {
1.106 nicm 131: if (args_has(args, 'q'))
1.115 nicm 132: goto out;
1.105 nicm 133: cmdq_error(item, "%s", cause);
134: free(cause);
1.113 nicm 135: goto fail;
1.105 nicm 136: }
137: o = options_get_only(oo, name);
138: parent = options_get(oo, name);
139:
140: /* Check that array options and indexes match up. */
1.122 nicm 141: if (idx != -1 && (*name == '@' || !options_isarray(parent))) {
142: cmdq_error(item, "not an array: %s", argument);
143: goto fail;
1.105 nicm 144: }
145:
146: /* With -o, check this option is not already set. */
147: if (!args_has(args, 'u') && args_has(args, 'o')) {
148: if (idx == -1)
149: already = (o != NULL);
150: else {
151: if (o == NULL)
152: already = 0;
153: else
154: already = (options_array_get(o, idx) != NULL);
155: }
156: if (already) {
157: if (args_has(args, 'q'))
1.115 nicm 158: goto out;
159: cmdq_error(item, "already set: %s", argument);
1.113 nicm 160: goto fail;
1.105 nicm 161: }
1.1 nicm 162: }
163:
1.105 nicm 164: /* Change the option. */
1.44 nicm 165: if (args_has(args, 'u')) {
1.105 nicm 166: if (o == NULL)
1.118 nicm 167: goto out;
1.105 nicm 168: if (idx == -1) {
1.119 nicm 169: if (*name == '@')
170: options_remove(o);
171: else if (oo == global_options ||
1.105 nicm 172: oo == global_s_options ||
173: oo == global_w_options)
174: options_default(oo, options_table_entry(o));
175: else
176: options_remove(o);
1.125 nicm 177: } else if (options_array_set(o, idx, NULL, 0, &cause) != 0) {
178: cmdq_error(item, "%s", cause);
179: free(cause);
180: goto fail;
181: }
1.109 nicm 182: } else if (*name == '@') {
183: if (value == NULL) {
184: cmdq_error(item, "empty value");
1.113 nicm 185: goto fail;
1.109 nicm 186: }
187: options_set_string(oo, name, append, "%s", value);
1.122 nicm 188: } else if (idx == -1 && !options_isarray(parent)) {
1.105 nicm 189: error = cmd_set_option_set(self, item, oo, parent, value);
190: if (error != 0)
1.113 nicm 191: goto fail;
1.43 nicm 192: } else {
1.109 nicm 193: if (value == NULL) {
194: cmdq_error(item, "empty value");
1.113 nicm 195: goto fail;
1.109 nicm 196: }
1.105 nicm 197: if (o == NULL)
198: o = options_empty(oo, options_table_entry(parent));
1.110 nicm 199: if (idx == -1) {
200: if (!append)
201: options_array_clear(o);
1.125 nicm 202: if (options_array_assign(o, value, &cause) != 0) {
203: cmdq_error(item, "%s", cause);
204: free(cause);
205: goto fail;
206: }
207: } else if (options_array_set(o, idx, value, append,
208: &cause) != 0) {
209: cmdq_error(item, "%s", cause);
210: free(cause);
1.113 nicm 211: goto fail;
1.61 nicm 212: }
1.55 nicm 213: }
214:
1.103 nicm 215: /* Update timers and so on for various options. */
1.105 nicm 216: if (strcmp(name, "automatic-rename") == 0) {
1.72 nicm 217: RB_FOREACH(w, windows, &windows) {
1.102 nicm 218: if (w->active == NULL)
219: continue;
1.84 nicm 220: if (options_get_number(w->options, "automatic-rename"))
1.81 nicm 221: w->active->flags |= PANE_CHANGED;
1.55 nicm 222: }
1.89 nicm 223: }
1.105 nicm 224: if (strcmp(name, "key-table") == 0) {
1.115 nicm 225: TAILQ_FOREACH(loop, &clients, entry)
226: server_client_set_key_table(loop, NULL);
1.117 nicm 227: }
228: if (strcmp(name, "user-keys") == 0) {
229: TAILQ_FOREACH(loop, &clients, entry) {
230: if (loop->tty.flags & TTY_OPENED)
231: tty_keys_build(&loop->tty);
232: }
1.1 nicm 233: }
1.105 nicm 234: if (strcmp(name, "status") == 0 ||
235: strcmp(name, "status-interval") == 0)
1.77 nicm 236: status_timer_start_all();
1.105 nicm 237: if (strcmp(name, "monitor-silence") == 0)
1.82 nicm 238: alerts_reset_all();
1.105 nicm 239: if (strcmp(name, "window-style") == 0 ||
240: strcmp(name, "window-active-style") == 0) {
1.127 nicm 241: RB_FOREACH(wp, window_pane_tree, &all_window_panes)
242: wp->flags |= PANE_STYLECHANGED;
1.96 nicm 243: }
1.105 nicm 244: if (strcmp(name, "pane-border-status") == 0) {
1.95 nicm 245: RB_FOREACH(w, windows, &windows)
1.120 nicm 246: layout_fix_panes(w);
1.95 nicm 247: }
1.116 nicm 248: RB_FOREACH(s, sessions, &sessions)
1.121 nicm 249: status_update_cache(s);
1.1 nicm 250:
1.105 nicm 251: /*
252: * Update sizes and redraw. May not always be necessary but do it
253: * anyway.
254: */
1.1 nicm 255: recalculate_sizes();
1.115 nicm 256: TAILQ_FOREACH(loop, &clients, entry) {
257: if (loop->session != NULL)
258: server_redraw_client(loop);
1.1 nicm 259: }
260:
1.115 nicm 261: out:
262: free(argument);
263: free(value);
1.113 nicm 264: free(name);
1.57 nicm 265: return (CMD_RETURN_NORMAL);
1.113 nicm 266:
267: fail:
1.115 nicm 268: free(argument);
269: free(value);
1.113 nicm 270: free(name);
271: return (CMD_RETURN_ERROR);
1.27 nicm 272: }
1.59 nicm 273:
1.99 nicm 274: static int
1.134 ! nicm 275: cmd_set_option_check_string(const struct options_table_entry *oe,
! 276: const char *value, char **cause)
! 277: {
! 278: struct style sy;
! 279:
! 280: if (strcmp(oe->name, "default-shell") == 0 && !checkshell(value)) {
! 281: xasprintf(cause, "not a suitable shell: %s", value);
! 282: return (-1);
! 283: }
! 284: if (oe->pattern != NULL && fnmatch(oe->pattern, value, 0) != 0) {
! 285: xasprintf(cause, "value is invalid: %s", value);
! 286: return (-1);
! 287: }
! 288: if ((oe->flags & OPTIONS_TABLE_IS_STYLE) &&
! 289: strstr(value, "#{") == NULL &&
! 290: style_parse(&sy, &grid_default_cell, value) != 0) {
! 291: xasprintf(cause, "invalid style: %s", value);
! 292: return (-1);
! 293: }
! 294: return (0);
! 295: }
! 296:
! 297: static int
1.105 nicm 298: cmd_set_option_set(struct cmd *self, struct cmdq_item *item, struct options *oo,
1.107 nicm 299: struct options_entry *parent, const char *value)
1.27 nicm 300: {
1.105 nicm 301: const struct options_table_entry *oe;
1.130 nicm 302: struct args *args = cmd_get_args(self);
1.105 nicm 303: int append = args_has(args, 'a');
304: long long number;
1.120 nicm 305: const char *errstr, *new;
1.134 ! nicm 306: char *old, *cause;
1.105 nicm 307: key_code key;
308:
309: oe = options_table_entry(parent);
310: if (value == NULL &&
311: oe->type != OPTIONS_TABLE_FLAG &&
312: oe->type != OPTIONS_TABLE_CHOICE) {
313: cmdq_error(item, "empty value");
1.43 nicm 314: return (-1);
1.27 nicm 315: }
1.43 nicm 316:
317: switch (oe->type) {
318: case OPTIONS_TABLE_STRING:
1.120 nicm 319: old = xstrdup(options_get_string(oo, oe->name));
1.105 nicm 320: options_set_string(oo, oe->name, append, "%s", value);
1.120 nicm 321: new = options_get_string(oo, oe->name);
1.134 ! nicm 322: if (cmd_set_option_check_string(oe, new, &cause) != 0) {
! 323: cmdq_error(item, "%s", cause);
! 324: free(cause);
! 325:
1.128 nicm 326: options_set_string(oo, oe->name, 0, "%s", old);
327: free(old);
1.120 nicm 328: return (-1);
329: }
330: free(old);
1.105 nicm 331: return (0);
1.43 nicm 332: case OPTIONS_TABLE_NUMBER:
1.105 nicm 333: number = strtonum(value, oe->minimum, oe->maximum, &errstr);
334: if (errstr != NULL) {
335: cmdq_error(item, "value is %s: %s", errstr, value);
336: return (-1);
337: }
338: options_set_number(oo, oe->name, number);
339: return (0);
1.52 nicm 340: case OPTIONS_TABLE_KEY:
1.105 nicm 341: key = key_string_lookup_string(value);
342: if (key == KEYC_UNKNOWN) {
343: cmdq_error(item, "bad key: %s", value);
344: return (-1);
345: }
346: options_set_number(oo, oe->name, key);
347: return (0);
1.43 nicm 348: case OPTIONS_TABLE_COLOUR:
1.105 nicm 349: if ((number = colour_fromstring(value)) == -1) {
350: cmdq_error(item, "bad colour: %s", value);
351: return (-1);
352: }
1.123 nicm 353: options_set_number(oo, oe->name, number);
1.105 nicm 354: return (0);
1.43 nicm 355: case OPTIONS_TABLE_FLAG:
1.105 nicm 356: return (cmd_set_option_flag(item, oe, oo, value));
1.43 nicm 357: case OPTIONS_TABLE_CHOICE:
1.105 nicm 358: return (cmd_set_option_choice(item, oe, oo, value));
1.125 nicm 359: case OPTIONS_TABLE_COMMAND:
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: }