Annotation of src/usr.bin/tmux/options.c, Revision 1.44
1.44 ! nicm 1: /* $OpenBSD: options.c,v 1.43 2019/04/26 11:38:51 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.44 ! nicm 357: char *error;
1.26 nicm 358:
1.43 nicm 359: if (!OPTIONS_IS_ARRAY(o)) {
1.44 ! nicm 360: if (cause != NULL)
! 361: *cause = xstrdup("not an array");
1.26 nicm 362: return (-1);
1.43 nicm 363: }
364:
365: if (OPTIONS_IS_COMMAND(o)) {
1.44 ! nicm 366: cmdlist = cmd_string_parse(value, NULL, 0, &error);
! 367: if (cmdlist == NULL && error != NULL) {
! 368: if (cause != NULL)
! 369: *cause = error;
! 370: else
! 371: free(error);
1.43 nicm 372: return (-1);
1.44 ! nicm 373: }
1.43 nicm 374: }
1.26 nicm 375:
1.39 nicm 376: a = options_array_item(o, idx);
377: if (value == NULL) {
378: if (a != NULL)
379: options_array_free(o, a);
380: return (0);
1.26 nicm 381: }
1.31 nicm 382:
1.43 nicm 383: if (OPTIONS_IS_STRING(o)) {
384: if (a != NULL && append)
385: xasprintf(&new, "%s%s", a->value.string, value);
386: else
387: new = xstrdup(value);
388: }
389:
1.39 nicm 390: if (a == NULL) {
391: a = xcalloc(1, sizeof *a);
392: a->index = idx;
1.41 nicm 393: RB_INSERT(options_array, &o->value.array, a);
1.43 nicm 394: } else
1.41 nicm 395: options_value_free(o, &a->value);
1.43 nicm 396:
397: if (OPTIONS_IS_STRING(o))
1.41 nicm 398: a->value.string = new;
1.43 nicm 399: else if (OPTIONS_IS_COMMAND(o))
400: a->value.cmdlist = cmdlist;
1.26 nicm 401: return (0);
402: }
403:
1.43 nicm 404: int
405: options_array_assign(struct options_entry *o, const char *s, char **cause)
1.31 nicm 406: {
407: const char *separator;
408: char *copy, *next, *string;
409: u_int i;
410:
411: separator = o->tableentry->separator;
412: if (separator == NULL)
413: separator = " ,";
1.43 nicm 414: if (*separator == '\0') {
415: if (*s == '\0')
416: return (0);
417: for (i = 0; i < UINT_MAX; i++) {
418: if (options_array_item(o, i) == NULL)
419: break;
420: }
421: return (options_array_set(o, i, s, 0, cause));
422: }
1.31 nicm 423:
1.43 nicm 424: if (*s == '\0')
425: return (0);
1.31 nicm 426: copy = string = xstrdup(s);
427: while ((next = strsep(&string, separator)) != NULL) {
428: if (*next == '\0')
429: continue;
1.39 nicm 430: for (i = 0; i < UINT_MAX; i++) {
431: if (options_array_item(o, i) == NULL)
1.31 nicm 432: break;
433: }
1.39 nicm 434: if (i == UINT_MAX)
1.31 nicm 435: break;
1.43 nicm 436: if (options_array_set(o, i, next, 0, cause) != 0) {
437: free(copy);
438: return (-1);
439: }
1.31 nicm 440: }
441: free(copy);
1.43 nicm 442: return (0);
1.31 nicm 443: }
444:
1.39 nicm 445: struct options_array_item *
446: options_array_first(struct options_entry *o)
447: {
448: if (!OPTIONS_IS_ARRAY(o))
449: return (NULL);
1.41 nicm 450: return (RB_MIN(options_array, &o->value.array));
1.39 nicm 451: }
452:
453: struct options_array_item *
454: options_array_next(struct options_array_item *a)
455: {
1.41 nicm 456: return (RB_NEXT(options_array, &o->value.array, a));
1.39 nicm 457: }
458:
459: u_int
460: options_array_item_index(struct options_array_item *a)
461: {
462: return (a->index);
463: }
464:
1.41 nicm 465: union options_value *
1.39 nicm 466: options_array_item_value(struct options_array_item *a)
467: {
1.41 nicm 468: return (&a->value);
1.39 nicm 469: }
470:
471: int
472: options_isarray(struct options_entry *o)
473: {
474: return (OPTIONS_IS_ARRAY(o));
475: }
476:
1.26 nicm 477: int
1.27 nicm 478: options_isstring(struct options_entry *o)
1.26 nicm 479: {
1.41 nicm 480: return (OPTIONS_IS_STRING(o));
1.26 nicm 481: }
482:
1.42 nicm 483: char *
1.32 nicm 484: options_tostring(struct options_entry *o, int idx, int numeric)
1.26 nicm 485: {
1.39 nicm 486: struct options_array_item *a;
1.26 nicm 487:
488: if (OPTIONS_IS_ARRAY(o)) {
489: if (idx == -1)
1.42 nicm 490: return (xstrdup(""));
1.39 nicm 491: a = options_array_item(o, idx);
492: if (a == NULL)
1.42 nicm 493: return (xstrdup(""));
1.41 nicm 494: return (options_value_tostring(o, &a->value, numeric));
1.26 nicm 495: }
1.41 nicm 496: return (options_value_tostring(o, &o->value, numeric));
1.26 nicm 497: }
498:
499: char *
500: options_parse(const char *name, int *idx)
1.1 nicm 501: {
1.26 nicm 502: char *copy, *cp, *end;
1.1 nicm 503:
1.26 nicm 504: if (*name == '\0')
505: return (NULL);
506: copy = xstrdup(name);
507: if ((cp = strchr(copy, '[')) == NULL) {
508: *idx = -1;
509: return (copy);
510: }
511: end = strchr(cp + 1, ']');
512: if (end == NULL || end[1] != '\0' || !isdigit((u_char)end[-1])) {
513: free(copy);
514: return (NULL);
515: }
516: if (sscanf(cp, "[%d]", idx) != 1 || *idx < 0) {
517: free(copy);
518: return (NULL);
519: }
520: *cp = '\0';
521: return (copy);
1.1 nicm 522: }
523:
1.27 nicm 524: struct options_entry *
1.26 nicm 525: options_parse_get(struct options *oo, const char *s, int *idx, int only)
1.1 nicm 526: {
1.27 nicm 527: struct options_entry *o;
528: char *name;
1.1 nicm 529:
1.26 nicm 530: name = options_parse(s, idx);
531: if (name == NULL)
532: return (NULL);
533: if (only)
534: o = options_get_only(oo, name);
535: else
536: o = options_get(oo, name);
537: free(name);
538: return (o);
539: }
1.1 nicm 540:
1.26 nicm 541: char *
542: options_match(const char *s, int *idx, int* ambiguous)
543: {
544: const struct options_table_entry *oe, *found;
545: char *name;
546: size_t namelen;
547:
548: name = options_parse(s, idx);
1.33 nicm 549: if (name == NULL)
550: return (NULL);
1.26 nicm 551: namelen = strlen(name);
1.29 nicm 552:
553: if (*name == '@') {
554: *ambiguous = 0;
1.34 nicm 555: return (name);
1.29 nicm 556: }
1.26 nicm 557:
558: found = NULL;
559: for (oe = options_table; oe->name != NULL; oe++) {
560: if (strcmp(oe->name, name) == 0) {
561: found = oe;
562: break;
563: }
564: if (strncmp(oe->name, name, namelen) == 0) {
565: if (found != NULL) {
566: *ambiguous = 1;
567: free(name);
568: return (NULL);
569: }
570: found = oe;
571: }
572: }
573: free(name);
574: if (found == NULL) {
575: *ambiguous = 0;
576: return (NULL);
1.22 nicm 577: }
1.26 nicm 578: return (xstrdup(found->name));
579: }
1.22 nicm 580:
1.27 nicm 581: struct options_entry *
1.26 nicm 582: options_match_get(struct options *oo, const char *s, int *idx, int only,
583: int* ambiguous)
584: {
1.27 nicm 585: char *name;
586: struct options_entry *o;
1.21 nicm 587:
1.26 nicm 588: name = options_match(s, idx, ambiguous);
589: if (name == NULL)
590: return (NULL);
591: *ambiguous = 0;
592: if (only)
593: o = options_get_only(oo, name);
594: else
595: o = options_get(oo, name);
596: free(name);
1.4 nicm 597: return (o);
1.1 nicm 598: }
1.26 nicm 599:
1.23 nicm 600: const char *
1.1 nicm 601: options_get_string(struct options *oo, const char *name)
602: {
1.27 nicm 603: struct options_entry *o;
1.26 nicm 604:
605: o = options_get(oo, name);
606: if (o == NULL)
607: fatalx("missing option %s", name);
608: if (!OPTIONS_IS_STRING(o))
609: fatalx("option %s is not a string", name);
1.41 nicm 610: return (o->value.string);
1.26 nicm 611: }
612:
613: long long
614: options_get_number(struct options *oo, const char *name)
615: {
1.27 nicm 616: struct options_entry *o;
1.1 nicm 617:
1.26 nicm 618: o = options_get(oo, name);
619: if (o == NULL)
1.15 nicm 620: fatalx("missing option %s", name);
1.26 nicm 621: if (!OPTIONS_IS_NUMBER(o))
622: fatalx("option %s is not a number", name);
1.41 nicm 623: return (o->value.number);
1.1 nicm 624: }
625:
1.37 nicm 626: struct style *
1.26 nicm 627: options_get_style(struct options *oo, const char *name)
628: {
1.27 nicm 629: struct options_entry *o;
1.26 nicm 630:
631: o = options_get(oo, name);
632: if (o == NULL)
633: fatalx("missing option %s", name);
634: if (!OPTIONS_IS_STYLE(o))
635: fatalx("option %s is not a style", name);
1.41 nicm 636: return (&o->value.style);
1.26 nicm 637: }
638:
1.27 nicm 639: struct options_entry *
1.26 nicm 640: options_set_string(struct options *oo, const char *name, int append,
641: const char *fmt, ...)
1.1 nicm 642: {
1.27 nicm 643: struct options_entry *o;
644: va_list ap;
645: char *s, *value;
1.1 nicm 646:
1.26 nicm 647: va_start(ap, fmt);
648: xvasprintf(&s, fmt, ap);
649: va_end(ap);
650:
651: o = options_get_only(oo, name);
652: if (o != NULL && append && OPTIONS_IS_STRING(o)) {
1.41 nicm 653: xasprintf(&value, "%s%s", o->value.string, s);
1.26 nicm 654: free(s);
655: } else
656: value = s;
657: if (o == NULL && *name == '@')
658: o = options_add(oo, name);
659: else if (o == NULL) {
660: o = options_default(oo, options_parent_table_entry(oo, name));
661: if (o == NULL)
662: return (NULL);
663: }
1.21 nicm 664:
1.26 nicm 665: if (!OPTIONS_IS_STRING(o))
666: fatalx("option %s is not a string", name);
1.41 nicm 667: free(o->value.string);
668: o->value.string = value;
1.4 nicm 669: return (o);
1.1 nicm 670: }
671:
1.27 nicm 672: struct options_entry *
1.26 nicm 673: options_set_number(struct options *oo, const char *name, long long value)
1.1 nicm 674: {
1.27 nicm 675: struct options_entry *o;
1.26 nicm 676:
677: if (*name == '@')
678: fatalx("user option %s must be a string", name);
1.1 nicm 679:
1.26 nicm 680: o = options_get_only(oo, name);
681: if (o == NULL) {
682: o = options_default(oo, options_parent_table_entry(oo, name));
683: if (o == NULL)
684: return (NULL);
685: }
686:
687: if (!OPTIONS_IS_NUMBER(o))
688: fatalx("option %s is not a number", name);
1.41 nicm 689: o->value.number = value;
1.26 nicm 690: return (o);
1.10 nicm 691: }
692:
1.27 nicm 693: struct options_entry *
1.22 nicm 694: options_set_style(struct options *oo, const char *name, int append,
695: const char *value)
1.10 nicm 696: {
1.27 nicm 697: struct options_entry *o;
1.37 nicm 698: struct style sy;
1.26 nicm 699:
700: if (*name == '@')
701: fatalx("user option %s must be a string", name);
1.10 nicm 702:
1.26 nicm 703: o = options_get_only(oo, name);
704: if (o != NULL && append && OPTIONS_IS_STYLE(o))
1.41 nicm 705: style_copy(&sy, &o->value.style);
1.12 nicm 706: else
1.37 nicm 707: style_set(&sy, &grid_default_cell);
708: if (style_parse(&sy, &grid_default_cell, value) == -1)
1.26 nicm 709: return (NULL);
710: if (o == NULL) {
711: o = options_default(oo, options_parent_table_entry(oo, name));
712: if (o == NULL)
713: return (NULL);
714: }
715:
716: if (!OPTIONS_IS_STYLE(o))
717: fatalx("option %s is not a style", name);
1.41 nicm 718: style_copy(&o->value.style, &sy);
1.26 nicm 719: return (o);
720: }
1.12 nicm 721:
1.26 nicm 722: enum options_table_scope
723: options_scope_from_flags(struct args *args, int window,
724: struct cmd_find_state *fs, struct options **oo, char **cause)
725: {
726: struct session *s = fs->s;
727: struct winlink *wl = fs->wl;
728: const char *target= args_get(args, 't');
729:
730: if (args_has(args, 's')) {
731: *oo = global_options;
732: return (OPTIONS_TABLE_SERVER);
733: }
1.12 nicm 734:
1.26 nicm 735: if (window || args_has(args, 'w')) {
736: if (args_has(args, 'g')) {
737: *oo = global_w_options;
738: return (OPTIONS_TABLE_WINDOW);
739: }
740: if (wl == NULL) {
741: if (target != NULL)
742: xasprintf(cause, "no such window: %s", target);
743: else
744: xasprintf(cause, "no current window");
745: return (OPTIONS_TABLE_NONE);
746: }
747: *oo = wl->window->options;
748: return (OPTIONS_TABLE_WINDOW);
749: } else {
750: if (args_has(args, 'g')) {
751: *oo = global_s_options;
752: return (OPTIONS_TABLE_SESSION);
753: }
754: if (s == NULL) {
755: if (target != NULL)
756: xasprintf(cause, "no such session: %s", target);
757: else
758: xasprintf(cause, "no current session");
759: return (OPTIONS_TABLE_NONE);
760: }
761: *oo = s->options;
762: return (OPTIONS_TABLE_SESSION);
763: }
1.1 nicm 764: }