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