Annotation of src/usr.bin/tmux/options.c, Revision 1.43
1.43 ! nicm 1: /* $OpenBSD: options.c,v 1.42 2019/04/25 18:18:55 nicm Exp $ */
1.1 nicm 2:
3: /*
1.18 nicm 4: * Copyright (c) 2008 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:
1.26 nicm 21: #include <ctype.h>
1.1 nicm 22: #include <stdarg.h>
1.8 nicm 23: #include <stdlib.h>
1.1 nicm 24: #include <string.h>
25:
26: #include "tmux.h"
27:
28: /*
29: * Option handling; each option has a name, type and value and is stored in
1.9 nicm 30: * a red-black tree.
1.1 nicm 31: */
32:
1.39 nicm 33: struct options_array_item {
34: u_int index;
1.41 nicm 35: union options_value value;
1.39 nicm 36: RB_ENTRY(options_array_item) entry;
37: };
38: static int
39: options_array_cmp(struct options_array_item *a1, struct options_array_item *a2)
40: {
41: if (a1->index < a2->index)
42: return (-1);
43: if (a1->index > a2->index)
44: return (1);
45: return (0);
46: }
47: RB_GENERATE_STATIC(options_array, options_array_item, entry, options_array_cmp);
48:
1.27 nicm 49: struct options_entry {
1.41 nicm 50: struct options *owner;
1.26 nicm 51:
1.41 nicm 52: const char *name;
53: const struct options_table_entry *tableentry;
54: union options_value value;
1.26 nicm 55:
1.41 nicm 56: RB_ENTRY(options_entry) entry;
1.26 nicm 57: };
58:
1.13 nicm 59: struct options {
1.27 nicm 60: RB_HEAD(options_tree, options_entry) tree;
1.26 nicm 61: struct options *parent;
1.13 nicm 62: };
63:
1.27 nicm 64: static struct options_entry *options_add(struct options *, const char *);
1.26 nicm 65:
66: #define OPTIONS_IS_STRING(o) \
67: ((o)->tableentry == NULL || \
68: (o)->tableentry->type == OPTIONS_TABLE_STRING)
69: #define OPTIONS_IS_NUMBER(o) \
70: ((o)->tableentry != NULL && \
71: ((o)->tableentry->type == OPTIONS_TABLE_NUMBER || \
72: (o)->tableentry->type == OPTIONS_TABLE_KEY || \
73: (o)->tableentry->type == OPTIONS_TABLE_COLOUR || \
74: (o)->tableentry->type == OPTIONS_TABLE_FLAG || \
75: (o)->tableentry->type == OPTIONS_TABLE_CHOICE))
76: #define OPTIONS_IS_STYLE(o) \
77: ((o)->tableentry != NULL && \
78: (o)->tableentry->type == OPTIONS_TABLE_STYLE)
1.43 ! nicm 79: #define OPTIONS_IS_COMMAND(o) \
! 80: ((o)->tableentry != NULL && \
! 81: (o)->tableentry->type == OPTIONS_TABLE_COMMAND)
1.41 nicm 82:
83: #define OPTIONS_IS_ARRAY(o) \
1.26 nicm 84: ((o)->tableentry != NULL && \
1.41 nicm 85: ((o)->tableentry->flags & OPTIONS_TABLE_IS_ARRAY))
1.26 nicm 86:
1.27 nicm 87: static int options_cmp(struct options_entry *, struct options_entry *);
88: RB_GENERATE_STATIC(options_tree, options_entry, entry, options_cmp);
1.1 nicm 89:
1.17 nicm 90: static int
1.27 nicm 91: options_cmp(struct options_entry *lhs, struct options_entry *rhs)
1.26 nicm 92: {
93: return (strcmp(lhs->name, rhs->name));
94: }
95:
96: static const struct options_table_entry *
97: options_parent_table_entry(struct options *oo, const char *s)
1.1 nicm 98: {
1.27 nicm 99: struct options_entry *o;
1.26 nicm 100:
101: if (oo->parent == NULL)
102: fatalx("no parent options for %s", s);
103: o = options_get_only(oo->parent, s);
104: if (o == NULL)
105: fatalx("%s not in parent options", s);
106: return (o->tableentry);
1.1 nicm 107: }
108:
1.41 nicm 109: static void
110: options_value_free(struct options_entry *o, union options_value *ov)
111: {
112: if (OPTIONS_IS_STRING(o))
113: free(ov->string);
1.43 ! nicm 114: if (OPTIONS_IS_COMMAND(o) && ov->cmdlist != NULL)
! 115: cmd_list_free(ov->cmdlist);
1.41 nicm 116: }
117:
1.42 nicm 118: static char *
1.41 nicm 119: options_value_tostring(struct options_entry *o, union options_value *ov,
120: int numeric)
121: {
1.42 nicm 122: char *s;
1.41 nicm 123:
1.43 ! nicm 124: if (OPTIONS_IS_COMMAND(o))
! 125: return (cmd_list_print(ov->cmdlist));
1.41 nicm 126: if (OPTIONS_IS_STYLE(o))
1.42 nicm 127: return (xstrdup(style_tostring(&ov->style)));
1.41 nicm 128: if (OPTIONS_IS_NUMBER(o)) {
129: switch (o->tableentry->type) {
130: case OPTIONS_TABLE_NUMBER:
1.42 nicm 131: xasprintf(&s, "%lld", ov->number);
1.41 nicm 132: break;
133: case OPTIONS_TABLE_KEY:
1.42 nicm 134: s = xstrdup(key_string_lookup_key(ov->number));
1.41 nicm 135: break;
136: case OPTIONS_TABLE_COLOUR:
1.42 nicm 137: s = xstrdup(colour_tostring(ov->number));
1.41 nicm 138: break;
139: case OPTIONS_TABLE_FLAG:
140: if (numeric)
1.42 nicm 141: xasprintf(&s, "%lld", ov->number);
1.41 nicm 142: else
1.42 nicm 143: s = xstrdup(ov->number ? "on" : "off");
1.41 nicm 144: break;
145: case OPTIONS_TABLE_CHOICE:
1.42 nicm 146: s = xstrdup(o->tableentry->choices[ov->number]);
1.41 nicm 147: break;
148: case OPTIONS_TABLE_STRING:
149: case OPTIONS_TABLE_STYLE:
1.43 ! nicm 150: case OPTIONS_TABLE_COMMAND:
1.42 nicm 151: fatalx("not a number option type");
1.41 nicm 152: }
153: return (s);
154: }
155: if (OPTIONS_IS_STRING(o))
1.42 nicm 156: return (xstrdup(ov->string));
157: return (xstrdup(""));
1.41 nicm 158: }
159:
1.13 nicm 160: struct options *
161: options_create(struct options *parent)
1.1 nicm 162: {
1.16 nicm 163: struct options *oo;
1.13 nicm 164:
165: oo = xcalloc(1, sizeof *oo);
1.7 nicm 166: RB_INIT(&oo->tree);
1.1 nicm 167: oo->parent = parent;
1.13 nicm 168: return (oo);
1.1 nicm 169: }
170:
171: void
172: options_free(struct options *oo)
173: {
1.27 nicm 174: struct options_entry *o, *tmp;
1.1 nicm 175:
1.35 nicm 176: RB_FOREACH_SAFE(o, options_tree, &oo->tree, tmp)
1.26 nicm 177: options_remove(o);
1.13 nicm 178: free(oo);
179: }
180:
1.27 nicm 181: struct options_entry *
1.13 nicm 182: options_first(struct options *oo)
183: {
184: return (RB_MIN(options_tree, &oo->tree));
185: }
186:
1.27 nicm 187: struct options_entry *
188: options_next(struct options_entry *o)
1.13 nicm 189: {
190: return (RB_NEXT(options_tree, &oo->tree, o));
1.1 nicm 191: }
192:
1.27 nicm 193: struct options_entry *
1.26 nicm 194: options_get_only(struct options *oo, const char *name)
1.1 nicm 195: {
1.27 nicm 196: struct options_entry o;
1.1 nicm 197:
1.26 nicm 198: o.name = name;
199: return (RB_FIND(options_tree, &oo->tree, &o));
1.1 nicm 200: }
201:
1.27 nicm 202: struct options_entry *
1.26 nicm 203: options_get(struct options *oo, const char *name)
1.1 nicm 204: {
1.27 nicm 205: struct options_entry *o;
1.1 nicm 206:
1.26 nicm 207: o = options_get_only(oo, name);
1.1 nicm 208: while (o == NULL) {
209: oo = oo->parent;
210: if (oo == NULL)
211: break;
1.26 nicm 212: o = options_get_only(oo, name);
213: }
214: return (o);
215: }
216:
1.27 nicm 217: struct options_entry *
1.26 nicm 218: options_empty(struct options *oo, const struct options_table_entry *oe)
219: {
1.27 nicm 220: struct options_entry *o;
1.26 nicm 221:
222: o = options_add(oo, oe->name);
223: o->tableentry = oe;
224:
1.42 nicm 225: if (oe->flags & OPTIONS_TABLE_IS_ARRAY)
1.41 nicm 226: RB_INIT(&o->value.array);
1.39 nicm 227:
1.26 nicm 228: return (o);
229: }
230:
1.27 nicm 231: struct options_entry *
1.26 nicm 232: options_default(struct options *oo, const struct options_table_entry *oe)
233: {
1.41 nicm 234: struct options_entry *o;
235: union options_value *ov;
236: u_int i;
1.26 nicm 237:
238: o = options_empty(oo, oe);
1.41 nicm 239: ov = &o->value;
240:
241: if (oe->flags & OPTIONS_TABLE_IS_ARRAY) {
1.43 ! nicm 242: if (oe->default_arr == NULL) {
! 243: options_array_assign(o, oe->default_str, NULL);
! 244: return (o);
! 245: }
! 246: for (i = 0; oe->default_arr[i] != NULL; i++)
! 247: options_array_set(o, i, oe->default_arr[i], 0, NULL);
1.41 nicm 248: return (o);
249: }
250:
251: switch (oe->type) {
252: case OPTIONS_TABLE_STRING:
253: ov->string = xstrdup(oe->default_str);
254: break;
255: case OPTIONS_TABLE_STYLE:
256: style_set(&ov->style, &grid_default_cell);
257: style_parse(&ov->style, &grid_default_cell, oe->default_str);
258: break;
259: default:
260: ov->number = oe->default_num;
261: break;
262: }
1.26 nicm 263: return (o);
264: }
265:
1.27 nicm 266: static struct options_entry *
1.26 nicm 267: options_add(struct options *oo, const char *name)
268: {
1.27 nicm 269: struct options_entry *o;
1.26 nicm 270:
271: o = options_get_only(oo, name);
272: if (o != NULL)
273: options_remove(o);
274:
275: o = xcalloc(1, sizeof *o);
276: o->owner = oo;
277: o->name = xstrdup(name);
278:
279: RB_INSERT(options_tree, &oo->tree, o);
1.1 nicm 280: return (o);
281: }
282:
1.2 nicm 283: void
1.27 nicm 284: options_remove(struct options_entry *o)
1.26 nicm 285: {
286: struct options *oo = o->owner;
287:
1.41 nicm 288: if (OPTIONS_IS_ARRAY(o))
1.39 nicm 289: options_array_clear(o);
1.41 nicm 290: else
291: options_value_free(o, &o->value);
1.26 nicm 292: RB_REMOVE(options_tree, &oo->tree, o);
293: free(o);
294: }
295:
296: const char *
1.27 nicm 297: options_name(struct options_entry *o)
1.26 nicm 298: {
299: return (o->name);
300: }
301:
302: const struct options_table_entry *
1.27 nicm 303: options_table_entry(struct options_entry *o)
1.26 nicm 304: {
305: return (o->tableentry);
306: }
307:
1.39 nicm 308: static struct options_array_item *
309: options_array_item(struct options_entry *o, u_int idx)
310: {
311: struct options_array_item a;
312:
313: a.index = idx;
1.41 nicm 314: return (RB_FIND(options_array, &o->value.array, &a));
1.39 nicm 315: }
316:
317: static void
318: options_array_free(struct options_entry *o, struct options_array_item *a)
319: {
1.41 nicm 320: options_value_free(o, &a->value);
321: RB_REMOVE(options_array, &o->value.array, a);
1.39 nicm 322: free(a);
323: }
324:
1.31 nicm 325: void
326: options_array_clear(struct options_entry *o)
327: {
1.39 nicm 328: struct options_array_item *a, *a1;
329:
330: if (!OPTIONS_IS_ARRAY(o))
331: return;
332:
1.41 nicm 333: RB_FOREACH_SAFE(a, options_array, &o->value.array, a1)
1.39 nicm 334: options_array_free(o, a);
1.31 nicm 335: }
336:
1.41 nicm 337: union options_value *
1.27 nicm 338: options_array_get(struct options_entry *o, u_int idx)
1.26 nicm 339: {
1.39 nicm 340: struct options_array_item *a;
341:
1.26 nicm 342: if (!OPTIONS_IS_ARRAY(o))
343: return (NULL);
1.39 nicm 344: a = options_array_item(o, idx);
345: if (a == NULL)
1.26 nicm 346: return (NULL);
1.41 nicm 347: return (&a->value);
1.26 nicm 348: }
349:
350: int
1.31 nicm 351: options_array_set(struct options_entry *o, u_int idx, const char *value,
1.43 ! nicm 352: int append, char **cause)
1.26 nicm 353: {
1.39 nicm 354: struct options_array_item *a;
355: char *new;
1.43 ! nicm 356: struct cmd_list *cmdlist;
1.26 nicm 357:
1.43 ! nicm 358: if (!OPTIONS_IS_ARRAY(o)) {
! 359: *cause = xstrdup("not an array");
1.26 nicm 360: return (-1);
1.43 ! nicm 361: }
! 362:
! 363: if (OPTIONS_IS_COMMAND(o)) {
! 364: cmdlist = cmd_string_parse(value, NULL, 0, cause);
! 365: if (cmdlist == NULL && *cause != NULL)
! 366: return (-1);
! 367: }
1.26 nicm 368:
1.39 nicm 369: a = options_array_item(o, idx);
370: if (value == NULL) {
371: if (a != NULL)
372: options_array_free(o, a);
373: return (0);
1.26 nicm 374: }
1.31 nicm 375:
1.43 ! nicm 376: if (OPTIONS_IS_STRING(o)) {
! 377: if (a != NULL && append)
! 378: xasprintf(&new, "%s%s", a->value.string, value);
! 379: else
! 380: new = xstrdup(value);
! 381: }
! 382:
1.39 nicm 383: if (a == NULL) {
384: a = xcalloc(1, sizeof *a);
385: a->index = idx;
1.41 nicm 386: RB_INSERT(options_array, &o->value.array, a);
1.43 ! nicm 387: } else
1.41 nicm 388: options_value_free(o, &a->value);
1.43 ! nicm 389:
! 390: if (OPTIONS_IS_STRING(o))
1.41 nicm 391: a->value.string = new;
1.43 ! nicm 392: else if (OPTIONS_IS_COMMAND(o))
! 393: a->value.cmdlist = cmdlist;
1.26 nicm 394: return (0);
395: }
396:
1.43 ! nicm 397: int
! 398: options_array_assign(struct options_entry *o, const char *s, char **cause)
1.31 nicm 399: {
400: const char *separator;
401: char *copy, *next, *string;
402: u_int i;
403:
404: separator = o->tableentry->separator;
405: if (separator == NULL)
406: separator = " ,";
1.43 ! nicm 407: if (*separator == '\0') {
! 408: if (*s == '\0')
! 409: return (0);
! 410: for (i = 0; i < UINT_MAX; i++) {
! 411: if (options_array_item(o, i) == NULL)
! 412: break;
! 413: }
! 414: return (options_array_set(o, i, s, 0, cause));
! 415: }
1.31 nicm 416:
1.43 ! nicm 417: if (*s == '\0')
! 418: return (0);
1.31 nicm 419: copy = string = xstrdup(s);
420: while ((next = strsep(&string, separator)) != NULL) {
421: if (*next == '\0')
422: continue;
1.39 nicm 423: for (i = 0; i < UINT_MAX; i++) {
424: if (options_array_item(o, i) == NULL)
1.31 nicm 425: break;
426: }
1.39 nicm 427: if (i == UINT_MAX)
1.31 nicm 428: break;
1.43 ! nicm 429: if (options_array_set(o, i, next, 0, cause) != 0) {
! 430: free(copy);
! 431: return (-1);
! 432: }
1.31 nicm 433: }
434: free(copy);
1.43 ! nicm 435: return (0);
1.31 nicm 436: }
437:
1.39 nicm 438: struct options_array_item *
439: options_array_first(struct options_entry *o)
440: {
441: if (!OPTIONS_IS_ARRAY(o))
442: return (NULL);
1.41 nicm 443: return (RB_MIN(options_array, &o->value.array));
1.39 nicm 444: }
445:
446: struct options_array_item *
447: options_array_next(struct options_array_item *a)
448: {
1.41 nicm 449: return (RB_NEXT(options_array, &o->value.array, a));
1.39 nicm 450: }
451:
452: u_int
453: options_array_item_index(struct options_array_item *a)
454: {
455: return (a->index);
456: }
457:
1.41 nicm 458: union options_value *
1.39 nicm 459: options_array_item_value(struct options_array_item *a)
460: {
1.41 nicm 461: return (&a->value);
1.39 nicm 462: }
463:
464: int
465: options_isarray(struct options_entry *o)
466: {
467: return (OPTIONS_IS_ARRAY(o));
468: }
469:
1.26 nicm 470: int
1.27 nicm 471: options_isstring(struct options_entry *o)
1.26 nicm 472: {
1.41 nicm 473: return (OPTIONS_IS_STRING(o));
1.26 nicm 474: }
475:
1.42 nicm 476: char *
1.32 nicm 477: options_tostring(struct options_entry *o, int idx, int numeric)
1.26 nicm 478: {
1.39 nicm 479: struct options_array_item *a;
1.26 nicm 480:
481: if (OPTIONS_IS_ARRAY(o)) {
482: if (idx == -1)
1.42 nicm 483: return (xstrdup(""));
1.39 nicm 484: a = options_array_item(o, idx);
485: if (a == NULL)
1.42 nicm 486: return (xstrdup(""));
1.41 nicm 487: return (options_value_tostring(o, &a->value, numeric));
1.26 nicm 488: }
1.41 nicm 489: return (options_value_tostring(o, &o->value, numeric));
1.26 nicm 490: }
491:
492: char *
493: options_parse(const char *name, int *idx)
1.1 nicm 494: {
1.26 nicm 495: char *copy, *cp, *end;
1.1 nicm 496:
1.26 nicm 497: if (*name == '\0')
498: return (NULL);
499: copy = xstrdup(name);
500: if ((cp = strchr(copy, '[')) == NULL) {
501: *idx = -1;
502: return (copy);
503: }
504: end = strchr(cp + 1, ']');
505: if (end == NULL || end[1] != '\0' || !isdigit((u_char)end[-1])) {
506: free(copy);
507: return (NULL);
508: }
509: if (sscanf(cp, "[%d]", idx) != 1 || *idx < 0) {
510: free(copy);
511: return (NULL);
512: }
513: *cp = '\0';
514: return (copy);
1.1 nicm 515: }
516:
1.27 nicm 517: struct options_entry *
1.26 nicm 518: options_parse_get(struct options *oo, const char *s, int *idx, int only)
1.1 nicm 519: {
1.27 nicm 520: struct options_entry *o;
521: char *name;
1.1 nicm 522:
1.26 nicm 523: name = options_parse(s, idx);
524: if (name == NULL)
525: return (NULL);
526: if (only)
527: o = options_get_only(oo, name);
528: else
529: o = options_get(oo, name);
530: free(name);
531: return (o);
532: }
1.1 nicm 533:
1.26 nicm 534: char *
535: options_match(const char *s, int *idx, int* ambiguous)
536: {
537: const struct options_table_entry *oe, *found;
538: char *name;
539: size_t namelen;
540:
541: name = options_parse(s, idx);
1.33 nicm 542: if (name == NULL)
543: return (NULL);
1.26 nicm 544: namelen = strlen(name);
1.29 nicm 545:
546: if (*name == '@') {
547: *ambiguous = 0;
1.34 nicm 548: return (name);
1.29 nicm 549: }
1.26 nicm 550:
551: found = NULL;
552: for (oe = options_table; oe->name != NULL; oe++) {
553: if (strcmp(oe->name, name) == 0) {
554: found = oe;
555: break;
556: }
557: if (strncmp(oe->name, name, namelen) == 0) {
558: if (found != NULL) {
559: *ambiguous = 1;
560: free(name);
561: return (NULL);
562: }
563: found = oe;
564: }
565: }
566: free(name);
567: if (found == NULL) {
568: *ambiguous = 0;
569: return (NULL);
1.22 nicm 570: }
1.26 nicm 571: return (xstrdup(found->name));
572: }
1.22 nicm 573:
1.27 nicm 574: struct options_entry *
1.26 nicm 575: options_match_get(struct options *oo, const char *s, int *idx, int only,
576: int* ambiguous)
577: {
1.27 nicm 578: char *name;
579: struct options_entry *o;
1.21 nicm 580:
1.26 nicm 581: name = options_match(s, idx, ambiguous);
582: if (name == NULL)
583: return (NULL);
584: *ambiguous = 0;
585: if (only)
586: o = options_get_only(oo, name);
587: else
588: o = options_get(oo, name);
589: free(name);
1.4 nicm 590: return (o);
1.1 nicm 591: }
1.26 nicm 592:
1.23 nicm 593: const char *
1.1 nicm 594: options_get_string(struct options *oo, const char *name)
595: {
1.27 nicm 596: struct options_entry *o;
1.26 nicm 597:
598: o = options_get(oo, name);
599: if (o == NULL)
600: fatalx("missing option %s", name);
601: if (!OPTIONS_IS_STRING(o))
602: fatalx("option %s is not a string", name);
1.41 nicm 603: return (o->value.string);
1.26 nicm 604: }
605:
606: long long
607: options_get_number(struct options *oo, const char *name)
608: {
1.27 nicm 609: struct options_entry *o;
1.1 nicm 610:
1.26 nicm 611: o = options_get(oo, name);
612: if (o == NULL)
1.15 nicm 613: fatalx("missing option %s", name);
1.26 nicm 614: if (!OPTIONS_IS_NUMBER(o))
615: fatalx("option %s is not a number", name);
1.41 nicm 616: return (o->value.number);
1.1 nicm 617: }
618:
1.37 nicm 619: struct style *
1.26 nicm 620: options_get_style(struct options *oo, const char *name)
621: {
1.27 nicm 622: struct options_entry *o;
1.26 nicm 623:
624: o = options_get(oo, name);
625: if (o == NULL)
626: fatalx("missing option %s", name);
627: if (!OPTIONS_IS_STYLE(o))
628: fatalx("option %s is not a style", name);
1.41 nicm 629: return (&o->value.style);
1.26 nicm 630: }
631:
1.27 nicm 632: struct options_entry *
1.26 nicm 633: options_set_string(struct options *oo, const char *name, int append,
634: const char *fmt, ...)
1.1 nicm 635: {
1.27 nicm 636: struct options_entry *o;
637: va_list ap;
638: char *s, *value;
1.1 nicm 639:
1.26 nicm 640: va_start(ap, fmt);
641: xvasprintf(&s, fmt, ap);
642: va_end(ap);
643:
644: o = options_get_only(oo, name);
645: if (o != NULL && append && OPTIONS_IS_STRING(o)) {
1.41 nicm 646: xasprintf(&value, "%s%s", o->value.string, s);
1.26 nicm 647: free(s);
648: } else
649: value = s;
650: if (o == NULL && *name == '@')
651: o = options_add(oo, name);
652: else if (o == NULL) {
653: o = options_default(oo, options_parent_table_entry(oo, name));
654: if (o == NULL)
655: return (NULL);
656: }
1.21 nicm 657:
1.26 nicm 658: if (!OPTIONS_IS_STRING(o))
659: fatalx("option %s is not a string", name);
1.41 nicm 660: free(o->value.string);
661: o->value.string = value;
1.4 nicm 662: return (o);
1.1 nicm 663: }
664:
1.27 nicm 665: struct options_entry *
1.26 nicm 666: options_set_number(struct options *oo, const char *name, long long value)
1.1 nicm 667: {
1.27 nicm 668: struct options_entry *o;
1.26 nicm 669:
670: if (*name == '@')
671: fatalx("user option %s must be a string", name);
1.1 nicm 672:
1.26 nicm 673: o = options_get_only(oo, name);
674: if (o == NULL) {
675: o = options_default(oo, options_parent_table_entry(oo, name));
676: if (o == NULL)
677: return (NULL);
678: }
679:
680: if (!OPTIONS_IS_NUMBER(o))
681: fatalx("option %s is not a number", name);
1.41 nicm 682: o->value.number = value;
1.26 nicm 683: return (o);
1.10 nicm 684: }
685:
1.27 nicm 686: struct options_entry *
1.22 nicm 687: options_set_style(struct options *oo, const char *name, int append,
688: const char *value)
1.10 nicm 689: {
1.27 nicm 690: struct options_entry *o;
1.37 nicm 691: struct style sy;
1.26 nicm 692:
693: if (*name == '@')
694: fatalx("user option %s must be a string", name);
1.10 nicm 695:
1.26 nicm 696: o = options_get_only(oo, name);
697: if (o != NULL && append && OPTIONS_IS_STYLE(o))
1.41 nicm 698: style_copy(&sy, &o->value.style);
1.12 nicm 699: else
1.37 nicm 700: style_set(&sy, &grid_default_cell);
701: if (style_parse(&sy, &grid_default_cell, value) == -1)
1.26 nicm 702: return (NULL);
703: if (o == NULL) {
704: o = options_default(oo, options_parent_table_entry(oo, name));
705: if (o == NULL)
706: return (NULL);
707: }
708:
709: if (!OPTIONS_IS_STYLE(o))
710: fatalx("option %s is not a style", name);
1.41 nicm 711: style_copy(&o->value.style, &sy);
1.26 nicm 712: return (o);
713: }
1.12 nicm 714:
1.26 nicm 715: enum options_table_scope
716: options_scope_from_flags(struct args *args, int window,
717: struct cmd_find_state *fs, struct options **oo, char **cause)
718: {
719: struct session *s = fs->s;
720: struct winlink *wl = fs->wl;
721: const char *target= args_get(args, 't');
722:
723: if (args_has(args, 's')) {
724: *oo = global_options;
725: return (OPTIONS_TABLE_SERVER);
726: }
1.12 nicm 727:
1.26 nicm 728: if (window || args_has(args, 'w')) {
729: if (args_has(args, 'g')) {
730: *oo = global_w_options;
731: return (OPTIONS_TABLE_WINDOW);
732: }
733: if (wl == NULL) {
734: if (target != NULL)
735: xasprintf(cause, "no such window: %s", target);
736: else
737: xasprintf(cause, "no current window");
738: return (OPTIONS_TABLE_NONE);
739: }
740: *oo = wl->window->options;
741: return (OPTIONS_TABLE_WINDOW);
742: } else {
743: if (args_has(args, 'g')) {
744: *oo = global_s_options;
745: return (OPTIONS_TABLE_SESSION);
746: }
747: if (s == NULL) {
748: if (target != NULL)
749: xasprintf(cause, "no such session: %s", target);
750: else
751: xasprintf(cause, "no current session");
752: return (OPTIONS_TABLE_NONE);
753: }
754: *oo = s->options;
755: return (OPTIONS_TABLE_SESSION);
756: }
1.1 nicm 757: }