Annotation of src/usr.bin/tmux/options.c, Revision 1.56
1.56 ! nicm 1: /* $OpenBSD: options.c,v 1.55 2019/12/03 10:47:22 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.56 ! nicm 56: int cached;
! 57: struct style style;
! 58:
1.41 nicm 59: RB_ENTRY(options_entry) entry;
1.26 nicm 60: };
61:
1.13 nicm 62: struct options {
1.27 nicm 63: RB_HEAD(options_tree, options_entry) tree;
1.26 nicm 64: struct options *parent;
1.13 nicm 65: };
66:
1.27 nicm 67: static struct options_entry *options_add(struct options *, const char *);
1.26 nicm 68:
69: #define OPTIONS_IS_STRING(o) \
70: ((o)->tableentry == NULL || \
71: (o)->tableentry->type == OPTIONS_TABLE_STRING)
72: #define OPTIONS_IS_NUMBER(o) \
73: ((o)->tableentry != NULL && \
74: ((o)->tableentry->type == OPTIONS_TABLE_NUMBER || \
75: (o)->tableentry->type == OPTIONS_TABLE_KEY || \
76: (o)->tableentry->type == OPTIONS_TABLE_COLOUR || \
77: (o)->tableentry->type == OPTIONS_TABLE_FLAG || \
78: (o)->tableentry->type == OPTIONS_TABLE_CHOICE))
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);
1.50 nicm 103: o = options_get(oo->parent, s);
1.26 nicm 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))
1.46 nicm 125: return (cmd_list_print(ov->cmdlist, 0));
1.41 nicm 126: if (OPTIONS_IS_NUMBER(o)) {
127: switch (o->tableentry->type) {
128: case OPTIONS_TABLE_NUMBER:
1.42 nicm 129: xasprintf(&s, "%lld", ov->number);
1.41 nicm 130: break;
131: case OPTIONS_TABLE_KEY:
1.42 nicm 132: s = xstrdup(key_string_lookup_key(ov->number));
1.41 nicm 133: break;
134: case OPTIONS_TABLE_COLOUR:
1.42 nicm 135: s = xstrdup(colour_tostring(ov->number));
1.41 nicm 136: break;
137: case OPTIONS_TABLE_FLAG:
138: if (numeric)
1.42 nicm 139: xasprintf(&s, "%lld", ov->number);
1.41 nicm 140: else
1.42 nicm 141: s = xstrdup(ov->number ? "on" : "off");
1.41 nicm 142: break;
143: case OPTIONS_TABLE_CHOICE:
1.42 nicm 144: s = xstrdup(o->tableentry->choices[ov->number]);
1.41 nicm 145: break;
146: case OPTIONS_TABLE_STRING:
1.43 nicm 147: case OPTIONS_TABLE_COMMAND:
1.42 nicm 148: fatalx("not a number option type");
1.41 nicm 149: }
150: return (s);
151: }
152: if (OPTIONS_IS_STRING(o))
1.42 nicm 153: return (xstrdup(ov->string));
154: return (xstrdup(""));
1.41 nicm 155: }
156:
1.13 nicm 157: struct options *
158: options_create(struct options *parent)
1.1 nicm 159: {
1.16 nicm 160: struct options *oo;
1.13 nicm 161:
162: oo = xcalloc(1, sizeof *oo);
1.7 nicm 163: RB_INIT(&oo->tree);
1.1 nicm 164: oo->parent = parent;
1.13 nicm 165: return (oo);
1.1 nicm 166: }
167:
168: void
169: options_free(struct options *oo)
170: {
1.27 nicm 171: struct options_entry *o, *tmp;
1.1 nicm 172:
1.35 nicm 173: RB_FOREACH_SAFE(o, options_tree, &oo->tree, tmp)
1.26 nicm 174: options_remove(o);
1.13 nicm 175: free(oo);
176: }
177:
1.50 nicm 178: void
179: options_set_parent(struct options *oo, struct options *parent)
180: {
181: oo->parent = parent;
182: }
183:
1.27 nicm 184: struct options_entry *
1.13 nicm 185: options_first(struct options *oo)
186: {
187: return (RB_MIN(options_tree, &oo->tree));
188: }
189:
1.27 nicm 190: struct options_entry *
191: options_next(struct options_entry *o)
1.13 nicm 192: {
193: return (RB_NEXT(options_tree, &oo->tree, o));
1.1 nicm 194: }
195:
1.27 nicm 196: struct options_entry *
1.26 nicm 197: options_get_only(struct options *oo, const char *name)
1.1 nicm 198: {
1.27 nicm 199: struct options_entry o;
1.1 nicm 200:
1.26 nicm 201: o.name = name;
202: return (RB_FIND(options_tree, &oo->tree, &o));
1.1 nicm 203: }
204:
1.27 nicm 205: struct options_entry *
1.26 nicm 206: options_get(struct options *oo, const char *name)
1.1 nicm 207: {
1.27 nicm 208: struct options_entry *o;
1.1 nicm 209:
1.26 nicm 210: o = options_get_only(oo, name);
1.1 nicm 211: while (o == NULL) {
212: oo = oo->parent;
213: if (oo == NULL)
214: break;
1.26 nicm 215: o = options_get_only(oo, name);
216: }
217: return (o);
218: }
219:
1.27 nicm 220: struct options_entry *
1.26 nicm 221: options_empty(struct options *oo, const struct options_table_entry *oe)
222: {
1.27 nicm 223: struct options_entry *o;
1.26 nicm 224:
225: o = options_add(oo, oe->name);
226: o->tableentry = oe;
227:
1.42 nicm 228: if (oe->flags & OPTIONS_TABLE_IS_ARRAY)
1.41 nicm 229: RB_INIT(&o->value.array);
1.39 nicm 230:
1.26 nicm 231: return (o);
232: }
233:
1.27 nicm 234: struct options_entry *
1.26 nicm 235: options_default(struct options *oo, const struct options_table_entry *oe)
236: {
1.41 nicm 237: struct options_entry *o;
238: union options_value *ov;
239: u_int i;
1.26 nicm 240:
241: o = options_empty(oo, oe);
1.41 nicm 242: ov = &o->value;
243:
244: if (oe->flags & OPTIONS_TABLE_IS_ARRAY) {
1.43 nicm 245: if (oe->default_arr == NULL) {
246: options_array_assign(o, oe->default_str, NULL);
247: return (o);
248: }
249: for (i = 0; oe->default_arr[i] != NULL; i++)
250: options_array_set(o, i, oe->default_arr[i], 0, NULL);
1.41 nicm 251: return (o);
252: }
253:
254: switch (oe->type) {
255: case OPTIONS_TABLE_STRING:
256: ov->string = xstrdup(oe->default_str);
257: break;
258: default:
259: ov->number = oe->default_num;
260: break;
261: }
1.26 nicm 262: return (o);
263: }
264:
1.27 nicm 265: static struct options_entry *
1.26 nicm 266: options_add(struct options *oo, const char *name)
267: {
1.27 nicm 268: struct options_entry *o;
1.26 nicm 269:
270: o = options_get_only(oo, name);
271: if (o != NULL)
272: options_remove(o);
273:
274: o = xcalloc(1, sizeof *o);
275: o->owner = oo;
276: o->name = xstrdup(name);
277:
278: RB_INSERT(options_tree, &oo->tree, o);
1.1 nicm 279: return (o);
280: }
281:
1.2 nicm 282: void
1.27 nicm 283: options_remove(struct options_entry *o)
1.26 nicm 284: {
285: struct options *oo = o->owner;
286:
1.41 nicm 287: if (OPTIONS_IS_ARRAY(o))
1.39 nicm 288: options_array_clear(o);
1.41 nicm 289: else
290: options_value_free(o, &o->value);
1.26 nicm 291: RB_REMOVE(options_tree, &oo->tree, o);
1.53 nicm 292: free((void *)o->name);
1.26 nicm 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:
1.54 nicm 317: static struct options_array_item *
318: options_array_new(struct options_entry *o, u_int idx)
319: {
320: struct options_array_item *a;
321:
322: a = xcalloc(1, sizeof *a);
323: a->index = idx;
324: RB_INSERT(options_array, &o->value.array, a);
325: return (a);
326: }
327:
1.39 nicm 328: static void
329: options_array_free(struct options_entry *o, struct options_array_item *a)
330: {
1.41 nicm 331: options_value_free(o, &a->value);
332: RB_REMOVE(options_array, &o->value.array, a);
1.39 nicm 333: free(a);
334: }
335:
1.31 nicm 336: void
337: options_array_clear(struct options_entry *o)
338: {
1.39 nicm 339: struct options_array_item *a, *a1;
340:
341: if (!OPTIONS_IS_ARRAY(o))
342: return;
343:
1.41 nicm 344: RB_FOREACH_SAFE(a, options_array, &o->value.array, a1)
1.39 nicm 345: options_array_free(o, a);
1.31 nicm 346: }
347:
1.41 nicm 348: union options_value *
1.27 nicm 349: options_array_get(struct options_entry *o, u_int idx)
1.26 nicm 350: {
1.39 nicm 351: struct options_array_item *a;
352:
1.26 nicm 353: if (!OPTIONS_IS_ARRAY(o))
354: return (NULL);
1.39 nicm 355: a = options_array_item(o, idx);
356: if (a == NULL)
1.26 nicm 357: return (NULL);
1.41 nicm 358: return (&a->value);
1.26 nicm 359: }
360:
361: int
1.31 nicm 362: options_array_set(struct options_entry *o, u_int idx, const char *value,
1.43 nicm 363: int append, char **cause)
1.26 nicm 364: {
1.39 nicm 365: struct options_array_item *a;
366: char *new;
1.45 nicm 367: struct cmd_parse_result *pr;
1.26 nicm 368:
1.43 nicm 369: if (!OPTIONS_IS_ARRAY(o)) {
1.44 nicm 370: if (cause != NULL)
371: *cause = xstrdup("not an array");
1.26 nicm 372: return (-1);
1.43 nicm 373: }
374:
1.54 nicm 375: if (value == NULL) {
376: a = options_array_item(o, idx);
377: if (a != NULL)
378: options_array_free(o, a);
379: return (0);
380: }
381:
382: if (OPTIONS_IS_COMMAND(o)) {
1.45 nicm 383: pr = cmd_parse_from_string(value, NULL);
384: switch (pr->status) {
385: case CMD_PARSE_EMPTY:
1.47 nicm 386: if (cause != NULL)
387: *cause = xstrdup("empty command");
1.45 nicm 388: return (-1);
389: case CMD_PARSE_ERROR:
1.44 nicm 390: if (cause != NULL)
1.45 nicm 391: *cause = pr->error;
1.44 nicm 392: else
1.45 nicm 393: free(pr->error);
1.43 nicm 394: return (-1);
1.45 nicm 395: case CMD_PARSE_SUCCESS:
396: break;
1.44 nicm 397: }
1.26 nicm 398:
1.54 nicm 399: a = options_array_item(o, idx);
400: if (a == NULL)
401: a = options_array_new(o, idx);
402: else
403: options_value_free(o, &a->value);
404: a->value.cmdlist = pr->cmdlist;
1.39 nicm 405: return (0);
1.26 nicm 406: }
1.31 nicm 407:
1.43 nicm 408: if (OPTIONS_IS_STRING(o)) {
1.54 nicm 409: a = options_array_item(o, idx);
1.43 nicm 410: if (a != NULL && append)
411: xasprintf(&new, "%s%s", a->value.string, value);
412: else
413: new = xstrdup(value);
1.54 nicm 414: if (a == NULL)
415: a = options_array_new(o, idx);
416: else
417: options_value_free(o, &a->value);
418: a->value.string = new;
419: return (0);
1.43 nicm 420: }
421:
1.54 nicm 422: if (cause != NULL)
423: *cause = xstrdup("wrong array type");
424: return (-1);
1.26 nicm 425: }
426:
1.43 nicm 427: int
428: options_array_assign(struct options_entry *o, const char *s, char **cause)
1.31 nicm 429: {
430: const char *separator;
431: char *copy, *next, *string;
432: u_int i;
433:
434: separator = o->tableentry->separator;
435: if (separator == NULL)
436: separator = " ,";
1.43 nicm 437: if (*separator == '\0') {
438: if (*s == '\0')
439: return (0);
440: for (i = 0; i < UINT_MAX; i++) {
441: if (options_array_item(o, i) == NULL)
442: break;
443: }
444: return (options_array_set(o, i, s, 0, cause));
445: }
1.31 nicm 446:
1.43 nicm 447: if (*s == '\0')
448: return (0);
1.31 nicm 449: copy = string = xstrdup(s);
450: while ((next = strsep(&string, separator)) != NULL) {
451: if (*next == '\0')
452: continue;
1.39 nicm 453: for (i = 0; i < UINT_MAX; i++) {
454: if (options_array_item(o, i) == NULL)
1.31 nicm 455: break;
456: }
1.39 nicm 457: if (i == UINT_MAX)
1.31 nicm 458: break;
1.43 nicm 459: if (options_array_set(o, i, next, 0, cause) != 0) {
460: free(copy);
461: return (-1);
462: }
1.31 nicm 463: }
464: free(copy);
1.43 nicm 465: return (0);
1.31 nicm 466: }
467:
1.39 nicm 468: struct options_array_item *
469: options_array_first(struct options_entry *o)
470: {
471: if (!OPTIONS_IS_ARRAY(o))
472: return (NULL);
1.41 nicm 473: return (RB_MIN(options_array, &o->value.array));
1.39 nicm 474: }
475:
476: struct options_array_item *
477: options_array_next(struct options_array_item *a)
478: {
1.41 nicm 479: return (RB_NEXT(options_array, &o->value.array, a));
1.39 nicm 480: }
481:
482: u_int
483: options_array_item_index(struct options_array_item *a)
484: {
485: return (a->index);
486: }
487:
1.41 nicm 488: union options_value *
1.39 nicm 489: options_array_item_value(struct options_array_item *a)
490: {
1.41 nicm 491: return (&a->value);
1.39 nicm 492: }
493:
494: int
495: options_isarray(struct options_entry *o)
496: {
497: return (OPTIONS_IS_ARRAY(o));
498: }
499:
1.26 nicm 500: int
1.27 nicm 501: options_isstring(struct options_entry *o)
1.26 nicm 502: {
1.41 nicm 503: return (OPTIONS_IS_STRING(o));
1.26 nicm 504: }
505:
1.42 nicm 506: char *
1.32 nicm 507: options_tostring(struct options_entry *o, int idx, int numeric)
1.26 nicm 508: {
1.39 nicm 509: struct options_array_item *a;
1.26 nicm 510:
511: if (OPTIONS_IS_ARRAY(o)) {
512: if (idx == -1)
1.42 nicm 513: return (xstrdup(""));
1.39 nicm 514: a = options_array_item(o, idx);
515: if (a == NULL)
1.42 nicm 516: return (xstrdup(""));
1.41 nicm 517: return (options_value_tostring(o, &a->value, numeric));
1.26 nicm 518: }
1.41 nicm 519: return (options_value_tostring(o, &o->value, numeric));
1.26 nicm 520: }
521:
522: char *
523: options_parse(const char *name, int *idx)
1.1 nicm 524: {
1.26 nicm 525: char *copy, *cp, *end;
1.1 nicm 526:
1.26 nicm 527: if (*name == '\0')
528: return (NULL);
529: copy = xstrdup(name);
530: if ((cp = strchr(copy, '[')) == NULL) {
531: *idx = -1;
532: return (copy);
533: }
534: end = strchr(cp + 1, ']');
535: if (end == NULL || end[1] != '\0' || !isdigit((u_char)end[-1])) {
536: free(copy);
537: return (NULL);
538: }
539: if (sscanf(cp, "[%d]", idx) != 1 || *idx < 0) {
540: free(copy);
541: return (NULL);
542: }
543: *cp = '\0';
544: return (copy);
1.1 nicm 545: }
546:
1.27 nicm 547: struct options_entry *
1.26 nicm 548: options_parse_get(struct options *oo, const char *s, int *idx, int only)
1.1 nicm 549: {
1.27 nicm 550: struct options_entry *o;
551: char *name;
1.1 nicm 552:
1.26 nicm 553: name = options_parse(s, idx);
554: if (name == NULL)
555: return (NULL);
556: if (only)
557: o = options_get_only(oo, name);
558: else
559: o = options_get(oo, name);
560: free(name);
561: return (o);
562: }
1.1 nicm 563:
1.26 nicm 564: char *
1.50 nicm 565: options_match(const char *s, int *idx, int *ambiguous)
1.26 nicm 566: {
567: const struct options_table_entry *oe, *found;
568: char *name;
569: size_t namelen;
570:
571: name = options_parse(s, idx);
1.33 nicm 572: if (name == NULL)
573: return (NULL);
1.26 nicm 574: namelen = strlen(name);
1.29 nicm 575:
576: if (*name == '@') {
577: *ambiguous = 0;
1.34 nicm 578: return (name);
1.29 nicm 579: }
1.26 nicm 580:
581: found = NULL;
582: for (oe = options_table; oe->name != NULL; oe++) {
583: if (strcmp(oe->name, name) == 0) {
584: found = oe;
585: break;
586: }
587: if (strncmp(oe->name, name, namelen) == 0) {
588: if (found != NULL) {
589: *ambiguous = 1;
590: free(name);
591: return (NULL);
592: }
593: found = oe;
594: }
595: }
596: free(name);
597: if (found == NULL) {
598: *ambiguous = 0;
599: return (NULL);
1.22 nicm 600: }
1.26 nicm 601: return (xstrdup(found->name));
602: }
1.22 nicm 603:
1.27 nicm 604: struct options_entry *
1.26 nicm 605: options_match_get(struct options *oo, const char *s, int *idx, int only,
1.55 nicm 606: int *ambiguous)
1.26 nicm 607: {
1.27 nicm 608: char *name;
609: struct options_entry *o;
1.21 nicm 610:
1.26 nicm 611: name = options_match(s, idx, ambiguous);
612: if (name == NULL)
613: return (NULL);
614: *ambiguous = 0;
615: if (only)
616: o = options_get_only(oo, name);
617: else
618: o = options_get(oo, name);
619: free(name);
1.4 nicm 620: return (o);
1.1 nicm 621: }
1.26 nicm 622:
1.23 nicm 623: const char *
1.1 nicm 624: options_get_string(struct options *oo, const char *name)
625: {
1.27 nicm 626: struct options_entry *o;
1.26 nicm 627:
628: o = options_get(oo, name);
629: if (o == NULL)
630: fatalx("missing option %s", name);
631: if (!OPTIONS_IS_STRING(o))
632: fatalx("option %s is not a string", name);
1.41 nicm 633: return (o->value.string);
1.26 nicm 634: }
635:
636: long long
637: options_get_number(struct options *oo, const char *name)
638: {
1.27 nicm 639: struct options_entry *o;
1.1 nicm 640:
1.26 nicm 641: o = options_get(oo, name);
642: if (o == NULL)
1.15 nicm 643: fatalx("missing option %s", name);
1.26 nicm 644: if (!OPTIONS_IS_NUMBER(o))
645: fatalx("option %s is not a number", name);
1.41 nicm 646: return (o->value.number);
1.1 nicm 647: }
648:
1.27 nicm 649: struct options_entry *
1.26 nicm 650: options_set_string(struct options *oo, const char *name, int append,
651: const char *fmt, ...)
1.1 nicm 652: {
1.27 nicm 653: struct options_entry *o;
654: va_list ap;
1.56 ! nicm 655: const char *separator = "";
1.27 nicm 656: char *s, *value;
1.1 nicm 657:
1.26 nicm 658: va_start(ap, fmt);
659: xvasprintf(&s, fmt, ap);
660: va_end(ap);
661:
662: o = options_get_only(oo, name);
663: if (o != NULL && append && OPTIONS_IS_STRING(o)) {
1.56 ! nicm 664: if (*name != '@') {
! 665: separator = o->tableentry->separator;
! 666: if (separator == NULL)
! 667: separator = "";
! 668: }
! 669: xasprintf(&value, "%s%s%s", o->value.string, separator, s);
1.26 nicm 670: free(s);
671: } else
672: value = s;
673: if (o == NULL && *name == '@')
674: o = options_add(oo, name);
675: else if (o == NULL) {
676: o = options_default(oo, options_parent_table_entry(oo, name));
677: if (o == NULL)
678: return (NULL);
679: }
1.21 nicm 680:
1.26 nicm 681: if (!OPTIONS_IS_STRING(o))
682: fatalx("option %s is not a string", name);
1.41 nicm 683: free(o->value.string);
684: o->value.string = value;
1.56 ! nicm 685: o->cached = 0;
1.4 nicm 686: return (o);
1.1 nicm 687: }
688:
1.27 nicm 689: struct options_entry *
1.26 nicm 690: options_set_number(struct options *oo, const char *name, long long value)
1.1 nicm 691: {
1.27 nicm 692: struct options_entry *o;
1.26 nicm 693:
694: if (*name == '@')
695: fatalx("user option %s must be a string", name);
1.1 nicm 696:
1.26 nicm 697: o = options_get_only(oo, name);
698: if (o == NULL) {
699: o = options_default(oo, options_parent_table_entry(oo, name));
700: if (o == NULL)
701: return (NULL);
702: }
703:
704: if (!OPTIONS_IS_NUMBER(o))
705: fatalx("option %s is not a number", name);
1.41 nicm 706: o->value.number = value;
1.26 nicm 707: return (o);
1.10 nicm 708: }
709:
1.50 nicm 710: int
1.49 nicm 711: options_scope_from_name(struct args *args, int window,
712: const char *name, struct cmd_find_state *fs, struct options **oo,
713: char **cause)
714: {
1.50 nicm 715: struct session *s = fs->s;
716: struct winlink *wl = fs->wl;
717: struct window_pane *wp = fs->wp;
718: const char *target = args_get(args, 't');
719: const struct options_table_entry *oe;
1.51 nicm 720: int scope = OPTIONS_TABLE_NONE;
1.49 nicm 721:
722: if (*name == '@')
723: return (options_scope_from_flags(args, window, fs, oo, cause));
724:
1.50 nicm 725: for (oe = options_table; oe->name != NULL; oe++) {
726: if (strcmp(oe->name, name) == 0)
727: break;
728: }
729: if (oe->name == NULL) {
1.49 nicm 730: xasprintf(cause, "unknown option: %s", name);
731: return (OPTIONS_TABLE_NONE);
732: }
1.51 nicm 733: switch (oe->scope) {
1.50 nicm 734: case OPTIONS_TABLE_SERVER:
1.49 nicm 735: *oo = global_options;
1.51 nicm 736: scope = OPTIONS_TABLE_SERVER;
1.50 nicm 737: break;
738: case OPTIONS_TABLE_SESSION:
1.51 nicm 739: if (args_has(args, 'g')) {
1.49 nicm 740: *oo = global_s_options;
1.51 nicm 741: scope = OPTIONS_TABLE_SESSION;
742: } else if (s == NULL && target != NULL)
1.50 nicm 743: xasprintf(cause, "no such session: %s", target);
744: else if (s == NULL)
745: xasprintf(cause, "no current session");
1.51 nicm 746: else {
1.50 nicm 747: *oo = s->options;
1.51 nicm 748: scope = OPTIONS_TABLE_SESSION;
749: }
1.50 nicm 750: break;
751: case OPTIONS_TABLE_WINDOW|OPTIONS_TABLE_PANE:
752: if (args_has(args, 'p')) {
753: if (wp == NULL && target != NULL)
754: xasprintf(cause, "no such pane: %s", target);
755: else if (wp == NULL)
756: xasprintf(cause, "no current pane");
1.51 nicm 757: else {
1.50 nicm 758: *oo = wp->options;
1.51 nicm 759: scope = OPTIONS_TABLE_PANE;
760: }
1.50 nicm 761: break;
762: }
763: /* FALLTHROUGH */
764: case OPTIONS_TABLE_WINDOW:
1.51 nicm 765: if (args_has(args, 'g')) {
1.49 nicm 766: *oo = global_w_options;
1.51 nicm 767: scope = OPTIONS_TABLE_WINDOW;
768: } else if (wl == NULL && target != NULL)
1.50 nicm 769: xasprintf(cause, "no such window: %s", target);
770: else if (wl == NULL)
771: xasprintf(cause, "no current window");
1.51 nicm 772: else {
1.49 nicm 773: *oo = wl->window->options;
1.51 nicm 774: scope = OPTIONS_TABLE_WINDOW;
775: }
1.50 nicm 776: break;
1.49 nicm 777: }
778: return (scope);
779: }
780:
1.50 nicm 781: int
1.26 nicm 782: options_scope_from_flags(struct args *args, int window,
783: struct cmd_find_state *fs, struct options **oo, char **cause)
784: {
1.50 nicm 785: struct session *s = fs->s;
786: struct winlink *wl = fs->wl;
787: struct window_pane *wp = fs->wp;
788: const char *target = args_get(args, 't');
1.26 nicm 789:
790: if (args_has(args, 's')) {
791: *oo = global_options;
792: return (OPTIONS_TABLE_SERVER);
793: }
1.12 nicm 794:
1.50 nicm 795: if (args_has(args, 'p')) {
796: if (wp == NULL) {
797: if (target != NULL)
798: xasprintf(cause, "no such pane: %s", target);
799: else
800: xasprintf(cause, "no current pane");
801: return (OPTIONS_TABLE_NONE);
802: }
803: *oo = wp->options;
804: return (OPTIONS_TABLE_PANE);
805: } else if (window || args_has(args, 'w')) {
1.26 nicm 806: if (args_has(args, 'g')) {
807: *oo = global_w_options;
808: return (OPTIONS_TABLE_WINDOW);
809: }
810: if (wl == NULL) {
811: if (target != NULL)
812: xasprintf(cause, "no such window: %s", target);
813: else
814: xasprintf(cause, "no current window");
815: return (OPTIONS_TABLE_NONE);
816: }
817: *oo = wl->window->options;
818: return (OPTIONS_TABLE_WINDOW);
819: } else {
820: if (args_has(args, 'g')) {
821: *oo = global_s_options;
822: return (OPTIONS_TABLE_SESSION);
823: }
824: if (s == NULL) {
825: if (target != NULL)
826: xasprintf(cause, "no such session: %s", target);
827: else
828: xasprintf(cause, "no current session");
829: return (OPTIONS_TABLE_NONE);
830: }
831: *oo = s->options;
832: return (OPTIONS_TABLE_SESSION);
833: }
1.56 ! nicm 834: }
! 835:
! 836: struct style *
! 837: options_string_to_style(struct options *oo, const char *name,
! 838: struct format_tree *ft)
! 839: {
! 840: struct options_entry *o;
! 841: const char *s;
! 842: char *expanded;
! 843:
! 844: o = options_get(oo, name);
! 845: if (o == NULL || !OPTIONS_IS_STRING(o))
! 846: return (NULL);
! 847:
! 848: if (o->cached)
! 849: return (&o->style);
! 850: s = o->value.string;
! 851: log_debug("%s: %s is '%s'", __func__, name, s);
! 852:
! 853: style_set(&o->style, &grid_default_cell);
! 854: o->cached = (strstr(s, "#{") == NULL);
! 855:
! 856: if (ft != NULL && !o->cached) {
! 857: expanded = format_expand(ft, s);
! 858: if (style_parse(&o->style, &grid_default_cell, expanded) != 0) {
! 859: free(expanded);
! 860: return (NULL);
! 861: }
! 862: free(expanded);
! 863: } else {
! 864: if (style_parse(&o->style, &grid_default_cell, s) != 0)
! 865: return (NULL);
! 866: }
! 867: return (&o->style);
1.1 nicm 868: }