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