1 /* $OpenBSD: cmd-set-option.c,v 1.53 2012/02/25 12:57:42 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 int cmd_set_option_exec(struct cmd *, struct cmd_ctx *); 31 32 int cmd_set_option_unset(struct cmd *, struct cmd_ctx *, 33 const struct options_table_entry *, struct options *, 34 const char *); 35 int cmd_set_option_set(struct cmd *, struct cmd_ctx *, 36 const struct options_table_entry *, struct options *, 37 const char *); 38 39 struct options_entry *cmd_set_option_string(struct cmd *, struct cmd_ctx *, 40 const struct options_table_entry *, struct options *, 41 const char *); 42 struct options_entry *cmd_set_option_number(struct cmd *, struct cmd_ctx *, 43 const struct options_table_entry *, struct options *, 44 const char *); 45 struct options_entry *cmd_set_option_key(struct cmd *, struct cmd_ctx *, 46 const struct options_table_entry *, struct options *, 47 const char *); 48 struct options_entry *cmd_set_option_colour(struct cmd *, struct cmd_ctx *, 49 const struct options_table_entry *, struct options *, 50 const char *); 51 struct options_entry *cmd_set_option_attributes(struct cmd *, struct cmd_ctx *, 52 const struct options_table_entry *, struct options *, 53 const char *); 54 struct options_entry *cmd_set_option_flag(struct cmd *, struct cmd_ctx *, 55 const struct options_table_entry *, struct options *, 56 const char *); 57 struct options_entry *cmd_set_option_choice(struct cmd *, struct cmd_ctx *, 58 const struct options_table_entry *, struct options *, 59 const char *); 60 61 const struct cmd_entry cmd_set_option_entry = { 62 "set-option", "set", 63 "agst:uw", 1, 2, 64 "[-agsuw] [-t target-session|target-window] option [value]", 65 0, 66 NULL, 67 NULL, 68 cmd_set_option_exec 69 }; 70 71 const struct cmd_entry cmd_set_window_option_entry = { 72 "set-window-option", "setw", 73 "agt:u", 1, 2, 74 "[-agu] " CMD_TARGET_WINDOW_USAGE " option [value]", 75 0, 76 NULL, 77 NULL, 78 cmd_set_option_exec 79 }; 80 81 int 82 cmd_set_option_exec(struct cmd *self, struct cmd_ctx *ctx) 83 { 84 struct args *args = self->args; 85 const struct options_table_entry *table, *oe; 86 struct session *s; 87 struct winlink *wl; 88 struct client *c; 89 struct options *oo; 90 const char *optstr, *valstr; 91 u_int i; 92 93 /* Get the option name and value. */ 94 optstr = args->argv[0]; 95 if (*optstr == '\0') { 96 ctx->error(ctx, "invalid option"); 97 return (-1); 98 } 99 if (args->argc < 2) 100 valstr = NULL; 101 else 102 valstr = args->argv[1]; 103 104 /* Find the option entry, try each table. */ 105 table = oe = NULL; 106 if (options_table_find(optstr, &table, &oe) != 0) { 107 ctx->error(ctx, "ambiguous option: %s", optstr); 108 return (-1); 109 } 110 if (oe == NULL) { 111 ctx->error(ctx, "unknown option: %s", optstr); 112 return (-1); 113 } 114 115 /* Work out the tree from the table. */ 116 if (table == server_options_table) 117 oo = &global_options; 118 else if (table == window_options_table) { 119 if (args_has(self->args, 'g')) 120 oo = &global_w_options; 121 else { 122 wl = cmd_find_window(ctx, args_get(args, 't'), NULL); 123 if (wl == NULL) 124 return (-1); 125 oo = &wl->window->options; 126 } 127 } else if (table == session_options_table) { 128 if (args_has(self->args, 'g')) 129 oo = &global_s_options; 130 else { 131 s = cmd_find_session(ctx, args_get(args, 't'), 0); 132 if (s == NULL) 133 return (-1); 134 oo = &s->options; 135 } 136 } else { 137 ctx->error(ctx, "unknown table"); 138 return (-1); 139 } 140 141 /* Unset or set the option. */ 142 if (args_has(args, 'u')) { 143 if (cmd_set_option_unset(self, ctx, oe, oo, valstr) != 0) 144 return (-1); 145 } else { 146 if (cmd_set_option_set(self, ctx, oe, oo, valstr) != 0) 147 return (-1); 148 } 149 150 /* Update sizes and redraw. May not need it but meh. */ 151 recalculate_sizes(); 152 for (i = 0; i < ARRAY_LENGTH(&clients); i++) { 153 c = ARRAY_ITEM(&clients, i); 154 if (c != NULL && c->session != NULL) 155 server_redraw_client(c); 156 } 157 158 return (0); 159 } 160 161 /* Unset an option. */ 162 int 163 cmd_set_option_unset(struct cmd *self, struct cmd_ctx *ctx, 164 const struct options_table_entry *oe, struct options *oo, const char *value) 165 { 166 struct args *args = self->args; 167 168 if (args_has(args, 'g')) { 169 ctx->error(ctx, "can't unset global option: %s", oe->name); 170 return (-1); 171 } 172 if (value != NULL) { 173 ctx->error(ctx, "value passed to unset option: %s", oe->name); 174 return (-1); 175 } 176 177 options_remove(oo, oe->name); 178 ctx->info(ctx, "unset option: %s", oe->name); 179 return (0); 180 } 181 182 /* Set an option. */ 183 int 184 cmd_set_option_set(struct cmd *self, struct cmd_ctx *ctx, 185 const struct options_table_entry *oe, struct options *oo, const char *value) 186 { 187 struct options_entry *o; 188 const char *s; 189 190 if (oe->type != OPTIONS_TABLE_FLAG && value == NULL) { 191 ctx->error(ctx, "empty value"); 192 return (-1); 193 } 194 195 o = NULL; 196 switch (oe->type) { 197 case OPTIONS_TABLE_STRING: 198 o = cmd_set_option_string(self, ctx, oe, oo, value); 199 break; 200 case OPTIONS_TABLE_NUMBER: 201 o = cmd_set_option_number(self, ctx, oe, oo, value); 202 break; 203 case OPTIONS_TABLE_KEY: 204 o = cmd_set_option_key(self, ctx, oe, oo, value); 205 break; 206 case OPTIONS_TABLE_COLOUR: 207 o = cmd_set_option_colour(self, ctx, oe, oo, value); 208 break; 209 case OPTIONS_TABLE_ATTRIBUTES: 210 o = cmd_set_option_attributes(self, ctx, oe, oo, value); 211 break; 212 case OPTIONS_TABLE_FLAG: 213 o = cmd_set_option_flag(self, ctx, oe, oo, value); 214 break; 215 case OPTIONS_TABLE_CHOICE: 216 o = cmd_set_option_choice(self, ctx, oe, oo, value); 217 break; 218 } 219 if (o == NULL) 220 return (-1); 221 222 s = options_table_print_entry(oe, o); 223 ctx->info(ctx, "set option: %s -> %s", oe->name, s); 224 return (0); 225 } 226 227 /* Set a string option. */ 228 struct options_entry * 229 cmd_set_option_string(struct cmd *self, unused struct cmd_ctx *ctx, 230 const struct options_table_entry *oe, struct options *oo, const char *value) 231 { 232 struct args *args = self->args; 233 struct options_entry *o; 234 char *oldval, *newval; 235 236 if (args_has(args, 'a')) { 237 oldval = options_get_string(oo, oe->name); 238 xasprintf(&newval, "%s%s", oldval, value); 239 } else 240 newval = xstrdup(value); 241 242 o = options_set_string(oo, oe->name, "%s", newval); 243 244 xfree(newval); 245 return (o); 246 } 247 248 /* Set a number option. */ 249 struct options_entry * 250 cmd_set_option_number(unused struct cmd *self, struct cmd_ctx *ctx, 251 const struct options_table_entry *oe, struct options *oo, const char *value) 252 { 253 long long ll; 254 const char *errstr; 255 256 ll = strtonum(value, oe->minimum, oe->maximum, &errstr); 257 if (errstr != NULL) { 258 ctx->error(ctx, "value is %s: %s", errstr, value); 259 return (NULL); 260 } 261 262 return (options_set_number(oo, oe->name, ll)); 263 } 264 265 /* Set a key option. */ 266 struct options_entry * 267 cmd_set_option_key(unused struct cmd *self, struct cmd_ctx *ctx, 268 const struct options_table_entry *oe, struct options *oo, const char *value) 269 { 270 int key; 271 272 if ((key = key_string_lookup_string(value)) == KEYC_NONE) { 273 ctx->error(ctx, "bad key: %s", value); 274 return (NULL); 275 } 276 277 return (options_set_number(oo, oe->name, key)); 278 } 279 280 /* Set a colour option. */ 281 struct options_entry * 282 cmd_set_option_colour(unused struct cmd *self, struct cmd_ctx *ctx, 283 const struct options_table_entry *oe, struct options *oo, const char *value) 284 { 285 int colour; 286 287 if ((colour = colour_fromstring(value)) == -1) { 288 ctx->error(ctx, "bad colour: %s", value); 289 return (NULL); 290 } 291 292 return (options_set_number(oo, oe->name, colour)); 293 } 294 295 /* Set an attributes option. */ 296 struct options_entry * 297 cmd_set_option_attributes(unused struct cmd *self, struct cmd_ctx *ctx, 298 const struct options_table_entry *oe, struct options *oo, const char *value) 299 { 300 int attr; 301 302 if ((attr = attributes_fromstring(value)) == -1) { 303 ctx->error(ctx, "bad attributes: %s", value); 304 return (NULL); 305 } 306 307 return (options_set_number(oo, oe->name, attr)); 308 } 309 310 /* Set a flag option. */ 311 struct options_entry * 312 cmd_set_option_flag(unused struct cmd *self, struct cmd_ctx *ctx, 313 const struct options_table_entry *oe, struct options *oo, const char *value) 314 { 315 int flag; 316 317 if (value == NULL || *value == '\0') 318 flag = !options_get_number(oo, oe->name); 319 else { 320 if ((value[0] == '1' && value[1] == '\0') || 321 strcasecmp(value, "on") == 0 || 322 strcasecmp(value, "yes") == 0) 323 flag = 1; 324 else if ((value[0] == '0' && value[1] == '\0') || 325 strcasecmp(value, "off") == 0 || 326 strcasecmp(value, "no") == 0) 327 flag = 0; 328 else { 329 ctx->error(ctx, "bad value: %s", value); 330 return (NULL); 331 } 332 } 333 334 return (options_set_number(oo, oe->name, flag)); 335 } 336 337 /* Set a choice option. */ 338 struct options_entry * 339 cmd_set_option_choice(unused struct cmd *self, struct cmd_ctx *ctx, 340 const struct options_table_entry *oe, struct options *oo, const char *value) 341 { 342 const char **choicep; 343 int n, choice = -1; 344 345 n = 0; 346 for (choicep = oe->choices; *choicep != NULL; choicep++) { 347 n++; 348 if (strncmp(*choicep, value, strlen(value)) != 0) 349 continue; 350 351 if (choice != -1) { 352 ctx->error(ctx, "ambiguous value: %s", value); 353 return (NULL); 354 } 355 choice = n - 1; 356 } 357 if (choice == -1) { 358 ctx->error(ctx, "unknown value: %s", value); 359 return (NULL); 360 } 361 362 return (options_set_number(oo, oe->name, choice)); 363 } 364