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