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