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