1 /* $OpenBSD: cmd-set-option.c,v 1.124 2019/04/23 20:36:55 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> 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 <fnmatch.h> 22 #include <stdlib.h> 23 #include <string.h> 24 25 #include "tmux.h" 26 27 /* 28 * Set an option. 29 */ 30 31 static enum cmd_retval cmd_set_option_exec(struct cmd *, struct cmdq_item *); 32 33 static int cmd_set_option_set(struct cmd *, struct cmdq_item *, 34 struct options *, struct options_entry *, const char *); 35 static int cmd_set_option_flag(struct cmdq_item *, 36 const struct options_table_entry *, struct options *, 37 const char *); 38 static int cmd_set_option_choice(struct cmdq_item *, 39 const struct options_table_entry *, struct options *, 40 const char *); 41 42 const struct cmd_entry cmd_set_option_entry = { 43 .name = "set-option", 44 .alias = "set", 45 46 .args = { "aFgoqst:uw", 1, 2 }, 47 .usage = "[-aFgosquw] [-t target-window] option [value]", 48 49 .target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL }, 50 51 .flags = CMD_AFTERHOOK, 52 .exec = cmd_set_option_exec 53 }; 54 55 const struct cmd_entry cmd_set_window_option_entry = { 56 .name = "set-window-option", 57 .alias = "setw", 58 59 .args = { "aFgoqt:u", 1, 2 }, 60 .usage = "[-aFgoqu] " CMD_TARGET_WINDOW_USAGE " option [value]", 61 62 .target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL }, 63 64 .flags = CMD_AFTERHOOK, 65 .exec = cmd_set_option_exec 66 }; 67 68 static enum cmd_retval 69 cmd_set_option_exec(struct cmd *self, struct cmdq_item *item) 70 { 71 struct args *args = self->args; 72 int append = args_has(args, 'a'); 73 struct cmd_find_state *fs = &item->target; 74 struct client *c, *loop; 75 struct session *s = fs->s; 76 struct winlink *wl = fs->wl; 77 struct window *w; 78 enum options_table_scope scope; 79 struct options *oo; 80 struct options_entry *parent, *o; 81 char *name, *argument, *value = NULL, *cause; 82 const char *target; 83 int window, idx, already, error, ambiguous; 84 struct style *sy; 85 86 /* Expand argument. */ 87 c = cmd_find_client(item, NULL, 1); 88 argument = format_single(item, args->argv[0], c, s, wl, NULL); 89 90 /* Parse option name and index. */ 91 name = options_match(argument, &idx, &ambiguous); 92 if (name == NULL) { 93 if (args_has(args, 'q')) 94 goto out; 95 if (ambiguous) 96 cmdq_error(item, "ambiguous option: %s", argument); 97 else 98 cmdq_error(item, "invalid option: %s", argument); 99 goto fail; 100 } 101 if (args->argc < 2) 102 value = NULL; 103 else if (args_has(args, 'F')) 104 value = format_single(item, args->argv[1], c, s, wl, NULL); 105 else 106 value = xstrdup(args->argv[1]); 107 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; 124 xasprintf(&cause, "unknown option: %s", argument); 125 } 126 } 127 if (scope == OPTIONS_TABLE_NONE) { 128 if (args_has(args, 'q')) 129 goto out; 130 cmdq_error(item, "%s", cause); 131 free(cause); 132 goto fail; 133 } 134 135 /* Which table should this option go into? */ 136 if (scope == OPTIONS_TABLE_SERVER) 137 oo = global_options; 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"); 147 goto fail; 148 } else 149 oo = s->options; 150 } else if (scope == OPTIONS_TABLE_WINDOW) { 151 if (args_has(self->args, 'g')) 152 oo = global_w_options; 153 else if (wl == NULL) { 154 target = args_get(args, 't'); 155 if (target != NULL) 156 cmdq_error(item, "no such window: %s", target); 157 else 158 cmdq_error(item, "no current window"); 159 goto fail; 160 } else 161 oo = wl->window->options; 162 } 163 o = options_get_only(oo, name); 164 parent = options_get(oo, name); 165 166 /* Check that array options and indexes match up. */ 167 if (idx != -1 && (*name == '@' || !options_isarray(parent))) { 168 cmdq_error(item, "not an array: %s", argument); 169 goto fail; 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')) 184 goto out; 185 cmdq_error(item, "already set: %s", argument); 186 goto fail; 187 } 188 } 189 190 /* Change the option. */ 191 if (args_has(args, 'u')) { 192 if (o == NULL) 193 goto out; 194 if (idx == -1) { 195 if (*name == '@') 196 options_remove(o); 197 else if (oo == global_options || 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 204 options_array_set(o, idx, NULL, 0); 205 } else if (*name == '@') { 206 if (value == NULL) { 207 cmdq_error(item, "empty value"); 208 goto fail; 209 } 210 options_set_string(oo, name, append, "%s", value); 211 } else if (idx == -1 && !options_isarray(parent)) { 212 error = cmd_set_option_set(self, item, oo, parent, value); 213 if (error != 0) 214 goto fail; 215 } else { 216 if (value == NULL) { 217 cmdq_error(item, "empty value"); 218 goto fail; 219 } 220 if (o == NULL) 221 o = options_empty(oo, options_table_entry(parent)); 222 if (idx == -1) { 223 if (!append) 224 options_array_clear(o); 225 options_array_assign(o, value); 226 } else if (options_array_set(o, idx, value, append) != 0) { 227 cmdq_error(item, "invalid index: %s", argument); 228 goto fail; 229 } 230 } 231 232 /* Update timers and so on for various options. */ 233 if (strcmp(name, "automatic-rename") == 0) { 234 RB_FOREACH(w, windows, &windows) { 235 if (w->active == NULL) 236 continue; 237 if (options_get_number(w->options, "automatic-rename")) 238 w->active->flags |= PANE_CHANGED; 239 } 240 } 241 if (strcmp(name, "key-table") == 0) { 242 TAILQ_FOREACH(loop, &clients, entry) 243 server_client_set_key_table(loop, NULL); 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 } 250 } 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 } 261 if (strcmp(name, "status") == 0 || 262 strcmp(name, "status-interval") == 0) 263 status_timer_start_all(); 264 if (strcmp(name, "monitor-silence") == 0) 265 alerts_reset_all(); 266 if (strcmp(name, "window-style") == 0 || 267 strcmp(name, "window-active-style") == 0) { 268 RB_FOREACH(w, windows, &windows) 269 w->flags |= WINDOW_STYLECHANGED; 270 } 271 if (strcmp(name, "pane-border-status") == 0) { 272 RB_FOREACH(w, windows, &windows) 273 layout_fix_panes(w); 274 } 275 RB_FOREACH(s, sessions, &sessions) 276 status_update_cache(s); 277 278 /* 279 * Update sizes and redraw. May not always be necessary but do it 280 * anyway. 281 */ 282 recalculate_sizes(); 283 TAILQ_FOREACH(loop, &clients, entry) { 284 if (loop->session != NULL) 285 server_redraw_client(loop); 286 } 287 288 out: 289 free(argument); 290 free(value); 291 free(name); 292 return (CMD_RETURN_NORMAL); 293 294 fail: 295 free(argument); 296 free(value); 297 free(name); 298 return (CMD_RETURN_ERROR); 299 } 300 301 static int 302 cmd_set_option_set(struct cmd *self, struct cmdq_item *item, struct options *oo, 303 struct options_entry *parent, const char *value) 304 { 305 const struct options_table_entry *oe; 306 struct args *args = self->args; 307 int append = args_has(args, 'a'); 308 struct options_entry *o; 309 long long number; 310 const char *errstr, *new; 311 char *old; 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"); 319 return (-1); 320 } 321 322 switch (oe->type) { 323 case OPTIONS_TABLE_STRING: 324 old = xstrdup(options_get_string(oo, oe->name)); 325 options_set_string(oo, oe->name, append, "%s", value); 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); 334 return (0); 335 case OPTIONS_TABLE_NUMBER: 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); 343 case OPTIONS_TABLE_KEY: 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); 351 case OPTIONS_TABLE_COLOUR: 352 if ((number = colour_fromstring(value)) == -1) { 353 cmdq_error(item, "bad colour: %s", value); 354 return (-1); 355 } 356 options_set_number(oo, oe->name, number); 357 return (0); 358 case OPTIONS_TABLE_FLAG: 359 return (cmd_set_option_flag(item, oe, oo, value)); 360 case OPTIONS_TABLE_CHOICE: 361 return (cmd_set_option_choice(item, oe, oo, value)); 362 case OPTIONS_TABLE_STYLE: 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); 369 } 370 return (-1); 371 } 372 373 static int 374 cmd_set_option_flag(struct cmdq_item *item, 375 const struct options_table_entry *oe, struct options *oo, 376 const char *value) 377 { 378 int flag; 379 380 if (value == NULL || *value == '\0') 381 flag = !options_get_number(oo, oe->name); 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; 390 else { 391 cmdq_error(item, "bad value: %s", value); 392 return (-1); 393 } 394 options_set_number(oo, oe->name, flag); 395 return (0); 396 } 397 398 static int 399 cmd_set_option_choice(struct cmdq_item *item, 400 const struct options_table_entry *oe, struct options *oo, 401 const char *value) 402 { 403 const char **cp; 404 int n, choice = -1; 405 406 if (value == NULL) { 407 choice = options_get_number(oo, oe->name); 408 if (choice < 2) 409 choice = !choice; 410 } else { 411 n = 0; 412 for (cp = oe->choices; *cp != NULL; cp++) { 413 if (strcmp(*cp, value) == 0) 414 choice = n; 415 n++; 416 } 417 if (choice == -1) { 418 cmdq_error(item, "unknown value: %s", value); 419 return (-1); 420 } 421 } 422 options_set_number(oo, oe->name, choice); 423 return (0); 424 } 425