1 /* $OpenBSD: cmd-set-option.c,v 1.61 2013/03/24 09:55:02 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> 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 30 enum cmd_retval cmd_set_option_exec(struct cmd *, struct cmd_q *); 31 32 enum cmd_retval cmd_set_option_user(struct cmd *, struct cmd_q *, 33 const char *, const char *); 34 35 int cmd_set_option_unset(struct cmd *, struct cmd_q *, 36 const struct options_table_entry *, struct options *, 37 const char *); 38 int cmd_set_option_set(struct cmd *, struct cmd_q *, 39 const struct options_table_entry *, struct options *, 40 const char *); 41 42 struct options_entry *cmd_set_option_string(struct cmd *, struct cmd_q *, 43 const struct options_table_entry *, struct options *, 44 const char *); 45 struct options_entry *cmd_set_option_number(struct cmd *, struct cmd_q *, 46 const struct options_table_entry *, struct options *, 47 const char *); 48 struct options_entry *cmd_set_option_key(struct cmd *, struct cmd_q *, 49 const struct options_table_entry *, struct options *, 50 const char *); 51 struct options_entry *cmd_set_option_colour(struct cmd *, struct cmd_q *, 52 const struct options_table_entry *, struct options *, 53 const char *); 54 struct options_entry *cmd_set_option_attributes(struct cmd *, struct cmd_q *, 55 const struct options_table_entry *, struct options *, 56 const char *); 57 struct options_entry *cmd_set_option_flag(struct cmd *, struct cmd_q *, 58 const struct options_table_entry *, struct options *, 59 const char *); 60 struct options_entry *cmd_set_option_choice(struct cmd *, struct cmd_q *, 61 const struct options_table_entry *, struct options *, 62 const char *); 63 64 const struct cmd_entry cmd_set_option_entry = { 65 "set-option", "set", 66 "agoqst:uw", 1, 2, 67 "[-agosquw] [-t target-session|target-window] option [value]", 68 0, 69 NULL, 70 NULL, 71 cmd_set_option_exec 72 }; 73 74 const struct cmd_entry cmd_set_window_option_entry = { 75 "set-window-option", "setw", 76 "agoqt:u", 1, 2, 77 "[-agoqu] " CMD_TARGET_WINDOW_USAGE " option [value]", 78 0, 79 NULL, 80 NULL, 81 cmd_set_option_exec 82 }; 83 84 enum cmd_retval 85 cmd_set_option_exec(struct cmd *self, struct cmd_q *cmdq) 86 { 87 struct args *args = self->args; 88 const struct options_table_entry *table, *oe; 89 struct session *s; 90 struct winlink *wl; 91 struct client *c; 92 struct options *oo; 93 struct window *w; 94 const char *optstr, *valstr; 95 u_int i; 96 97 /* Get the option name and value. */ 98 optstr = args->argv[0]; 99 if (*optstr == '\0') { 100 cmdq_error(cmdq, "invalid option"); 101 return (CMD_RETURN_ERROR); 102 } 103 if (args->argc < 2) 104 valstr = NULL; 105 else 106 valstr = args->argv[1]; 107 108 /* Is this a user option? */ 109 if (*optstr == '@') 110 return (cmd_set_option_user(self, cmdq, optstr, valstr)); 111 112 /* Find the option entry, try each table. */ 113 table = oe = NULL; 114 if (options_table_find(optstr, &table, &oe) != 0) { 115 cmdq_error(cmdq, "ambiguous option: %s", optstr); 116 return (CMD_RETURN_ERROR); 117 } 118 if (oe == NULL) { 119 cmdq_error(cmdq, "unknown option: %s", optstr); 120 return (CMD_RETURN_ERROR); 121 } 122 123 /* Work out the tree from the table. */ 124 if (table == server_options_table) 125 oo = &global_options; 126 else if (table == window_options_table) { 127 if (args_has(self->args, 'g')) 128 oo = &global_w_options; 129 else { 130 wl = cmd_find_window(cmdq, args_get(args, 't'), NULL); 131 if (wl == NULL) 132 return (CMD_RETURN_ERROR); 133 oo = &wl->window->options; 134 } 135 } else if (table == session_options_table) { 136 if (args_has(self->args, 'g')) 137 oo = &global_s_options; 138 else { 139 s = cmd_find_session(cmdq, args_get(args, 't'), 0); 140 if (s == NULL) 141 return (CMD_RETURN_ERROR); 142 oo = &s->options; 143 } 144 } else { 145 cmdq_error(cmdq, "unknown table"); 146 return (CMD_RETURN_ERROR); 147 } 148 149 /* Unset or set the option. */ 150 if (args_has(args, 'u')) { 151 if (cmd_set_option_unset(self, cmdq, oe, oo, valstr) != 0) 152 return (CMD_RETURN_ERROR); 153 } else { 154 if (args_has(args, 'o') && options_find1(oo, optstr) != NULL) { 155 if (!args_has(args, 'q')) 156 cmdq_print(cmdq, "already set: %s", optstr); 157 return (CMD_RETURN_NORMAL); 158 } 159 if (cmd_set_option_set(self, cmdq, oe, oo, valstr) != 0) 160 return (CMD_RETURN_ERROR); 161 } 162 163 /* Start or stop timers when automatic-rename changed. */ 164 if (strcmp (oe->name, "automatic-rename") == 0) { 165 for (i = 0; i < ARRAY_LENGTH(&windows); i++) { 166 if ((w = ARRAY_ITEM(&windows, i)) == NULL) 167 continue; 168 if (options_get_number(&w->options, "automatic-rename")) 169 queue_window_name(w); 170 else if (event_initialized(&w->name_timer)) 171 evtimer_del(&w->name_timer); 172 } 173 } 174 175 /* Update sizes and redraw. May not need it but meh. */ 176 recalculate_sizes(); 177 for (i = 0; i < ARRAY_LENGTH(&clients); i++) { 178 c = ARRAY_ITEM(&clients, i); 179 if (c != NULL && c->session != NULL) 180 server_redraw_client(c); 181 } 182 183 return (CMD_RETURN_NORMAL); 184 } 185 186 /* Set user option. */ 187 enum cmd_retval 188 cmd_set_option_user(struct cmd *self, struct cmd_q *cmdq, const char* optstr, 189 const char *valstr) 190 { 191 struct args *args = self->args; 192 struct session *s; 193 struct winlink *wl; 194 struct options *oo; 195 196 if (args_has(args, 's')) 197 oo = &global_options; 198 else if (args_has(self->args, 'w') || 199 self->entry == &cmd_set_window_option_entry) { 200 if (args_has(self->args, 'g')) 201 oo = &global_w_options; 202 else { 203 wl = cmd_find_window(cmdq, args_get(args, 't'), NULL); 204 if (wl == NULL) 205 return (CMD_RETURN_ERROR); 206 oo = &wl->window->options; 207 } 208 } else { 209 if (args_has(self->args, 'g')) 210 oo = &global_s_options; 211 else { 212 s = cmd_find_session(cmdq, args_get(args, 't'), 0); 213 if (s == NULL) 214 return (CMD_RETURN_ERROR); 215 oo = &s->options; 216 } 217 } 218 219 if (args_has(args, 'u')) { 220 if (options_find1(oo, optstr) == NULL) { 221 cmdq_error(cmdq, "unknown option: %s", optstr); 222 return (CMD_RETURN_ERROR); 223 } 224 if (valstr != NULL) { 225 cmdq_error(cmdq, "value passed to unset option: %s", 226 optstr); 227 return (CMD_RETURN_ERROR); 228 } 229 options_remove(oo, optstr); 230 } else { 231 if (valstr == NULL) { 232 cmdq_error(cmdq, "empty value"); 233 return (CMD_RETURN_ERROR); 234 } 235 if (args_has(args, 'o') && options_find1(oo, optstr) != NULL) { 236 if (!args_has(args, 'q')) 237 cmdq_print(cmdq, "already set: %s", optstr); 238 return (CMD_RETURN_NORMAL); 239 } 240 options_set_string(oo, optstr, "%s", valstr); 241 if (!args_has(args, 'q')) { 242 cmdq_info(cmdq, "set option: %s -> %s", optstr, 243 valstr); 244 } 245 } 246 return (CMD_RETURN_NORMAL); 247 } 248 249 250 /* Unset an option. */ 251 int 252 cmd_set_option_unset(struct cmd *self, struct cmd_q *cmdq, 253 const struct options_table_entry *oe, struct options *oo, const char *value) 254 { 255 struct args *args = self->args; 256 257 if (args_has(args, 'g')) { 258 cmdq_error(cmdq, "can't unset global option: %s", oe->name); 259 return (-1); 260 } 261 if (value != NULL) { 262 cmdq_error(cmdq, "value passed to unset option: %s", oe->name); 263 return (-1); 264 } 265 266 options_remove(oo, oe->name); 267 if (!args_has(args, 'q')) 268 cmdq_info(cmdq, "unset option: %s", oe->name); 269 return (0); 270 } 271 272 /* Set an option. */ 273 int 274 cmd_set_option_set(struct cmd *self, struct cmd_q *cmdq, 275 const struct options_table_entry *oe, struct options *oo, const char *value) 276 { 277 struct args *args = self->args; 278 struct options_entry *o; 279 const char *s; 280 281 if (oe->type != OPTIONS_TABLE_FLAG && value == NULL) { 282 cmdq_error(cmdq, "empty value"); 283 return (-1); 284 } 285 286 o = NULL; 287 switch (oe->type) { 288 case OPTIONS_TABLE_STRING: 289 o = cmd_set_option_string(self, cmdq, oe, oo, value); 290 break; 291 case OPTIONS_TABLE_NUMBER: 292 o = cmd_set_option_number(self, cmdq, oe, oo, value); 293 break; 294 case OPTIONS_TABLE_KEY: 295 o = cmd_set_option_key(self, cmdq, oe, oo, value); 296 break; 297 case OPTIONS_TABLE_COLOUR: 298 o = cmd_set_option_colour(self, cmdq, oe, oo, value); 299 break; 300 case OPTIONS_TABLE_ATTRIBUTES: 301 o = cmd_set_option_attributes(self, cmdq, oe, oo, value); 302 break; 303 case OPTIONS_TABLE_FLAG: 304 o = cmd_set_option_flag(self, cmdq, oe, oo, value); 305 break; 306 case OPTIONS_TABLE_CHOICE: 307 o = cmd_set_option_choice(self, cmdq, oe, oo, value); 308 break; 309 } 310 if (o == NULL) 311 return (-1); 312 313 s = options_table_print_entry(oe, o, 0); 314 if (!args_has(args, 'q')) 315 cmdq_info(cmdq, "set option: %s -> %s", oe->name, s); 316 return (0); 317 } 318 319 /* Set a string option. */ 320 struct options_entry * 321 cmd_set_option_string(struct cmd *self, unused struct cmd_q *cmdq, 322 const struct options_table_entry *oe, struct options *oo, const char *value) 323 { 324 struct args *args = self->args; 325 struct options_entry *o; 326 char *oldval, *newval; 327 328 if (args_has(args, 'a')) { 329 oldval = options_get_string(oo, oe->name); 330 xasprintf(&newval, "%s%s", oldval, value); 331 } else 332 newval = xstrdup(value); 333 334 o = options_set_string(oo, oe->name, "%s", newval); 335 336 free(newval); 337 return (o); 338 } 339 340 /* Set a number option. */ 341 struct options_entry * 342 cmd_set_option_number(unused struct cmd *self, struct cmd_q *cmdq, 343 const struct options_table_entry *oe, struct options *oo, const char *value) 344 { 345 long long ll; 346 const char *errstr; 347 348 ll = strtonum(value, oe->minimum, oe->maximum, &errstr); 349 if (errstr != NULL) { 350 cmdq_error(cmdq, "value is %s: %s", errstr, value); 351 return (NULL); 352 } 353 354 return (options_set_number(oo, oe->name, ll)); 355 } 356 357 /* Set a key option. */ 358 struct options_entry * 359 cmd_set_option_key(unused struct cmd *self, struct cmd_q *cmdq, 360 const struct options_table_entry *oe, struct options *oo, const char *value) 361 { 362 int key; 363 364 if ((key = key_string_lookup_string(value)) == KEYC_NONE) { 365 cmdq_error(cmdq, "bad key: %s", value); 366 return (NULL); 367 } 368 369 return (options_set_number(oo, oe->name, key)); 370 } 371 372 /* Set a colour option. */ 373 struct options_entry * 374 cmd_set_option_colour(unused struct cmd *self, struct cmd_q *cmdq, 375 const struct options_table_entry *oe, struct options *oo, const char *value) 376 { 377 int colour; 378 379 if ((colour = colour_fromstring(value)) == -1) { 380 cmdq_error(cmdq, "bad colour: %s", value); 381 return (NULL); 382 } 383 384 return (options_set_number(oo, oe->name, colour)); 385 } 386 387 /* Set an attributes option. */ 388 struct options_entry * 389 cmd_set_option_attributes(unused struct cmd *self, struct cmd_q *cmdq, 390 const struct options_table_entry *oe, struct options *oo, const char *value) 391 { 392 int attr; 393 394 if ((attr = attributes_fromstring(value)) == -1) { 395 cmdq_error(cmdq, "bad attributes: %s", value); 396 return (NULL); 397 } 398 399 return (options_set_number(oo, oe->name, attr)); 400 } 401 402 /* Set a flag option. */ 403 struct options_entry * 404 cmd_set_option_flag(unused struct cmd *self, struct cmd_q *cmdq, 405 const struct options_table_entry *oe, struct options *oo, const char *value) 406 { 407 int flag; 408 409 if (value == NULL || *value == '\0') 410 flag = !options_get_number(oo, oe->name); 411 else { 412 if ((value[0] == '1' && value[1] == '\0') || 413 strcasecmp(value, "on") == 0 || 414 strcasecmp(value, "yes") == 0) 415 flag = 1; 416 else if ((value[0] == '0' && value[1] == '\0') || 417 strcasecmp(value, "off") == 0 || 418 strcasecmp(value, "no") == 0) 419 flag = 0; 420 else { 421 cmdq_error(cmdq, "bad value: %s", value); 422 return (NULL); 423 } 424 } 425 426 return (options_set_number(oo, oe->name, flag)); 427 } 428 429 /* Set a choice option. */ 430 struct options_entry * 431 cmd_set_option_choice(unused struct cmd *self, struct cmd_q *cmdq, 432 const struct options_table_entry *oe, struct options *oo, 433 const char *value) 434 { 435 const char **choicep; 436 int n, choice = -1; 437 438 n = 0; 439 for (choicep = oe->choices; *choicep != NULL; choicep++) { 440 n++; 441 if (strncmp(*choicep, value, strlen(value)) != 0) 442 continue; 443 444 if (choice != -1) { 445 cmdq_error(cmdq, "ambiguous value: %s", value); 446 return (NULL); 447 } 448 choice = n - 1; 449 } 450 if (choice == -1) { 451 cmdq_error(cmdq, "unknown value: %s", value); 452 return (NULL); 453 } 454 455 return (options_set_number(oo, oe->name, choice)); 456 } 457