Annotation of src/usr.bin/tmux/options.c, Revision 1.67
1.67 ! nicm 1: /* $OpenBSD: options.c,v 1.66 2021/11/01 09:34:49 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.57 nicm 22: #include <fnmatch.h>
1.1 nicm 23: #include <stdarg.h>
1.8 nicm 24: #include <stdlib.h>
1.1 nicm 25: #include <string.h>
26:
27: #include "tmux.h"
28:
29: /*
30: * Option handling; each option has a name, type and value and is stored in
1.9 nicm 31: * a red-black tree.
1.1 nicm 32: */
33:
1.39 nicm 34: struct options_array_item {
35: u_int index;
1.41 nicm 36: union options_value value;
1.39 nicm 37: RB_ENTRY(options_array_item) entry;
38: };
39: static int
40: options_array_cmp(struct options_array_item *a1, struct options_array_item *a2)
41: {
42: if (a1->index < a2->index)
43: return (-1);
44: if (a1->index > a2->index)
45: return (1);
46: return (0);
47: }
48: RB_GENERATE_STATIC(options_array, options_array_item, entry, options_array_cmp);
49:
1.27 nicm 50: struct options_entry {
1.41 nicm 51: struct options *owner;
1.26 nicm 52:
1.41 nicm 53: const char *name;
54: const struct options_table_entry *tableentry;
55: union options_value value;
1.26 nicm 56:
1.56 nicm 57: int cached;
58: struct style style;
59:
1.41 nicm 60: RB_ENTRY(options_entry) entry;
1.26 nicm 61: };
62:
1.13 nicm 63: struct options {
1.27 nicm 64: RB_HEAD(options_tree, options_entry) tree;
1.26 nicm 65: struct options *parent;
1.13 nicm 66: };
67:
1.27 nicm 68: static struct options_entry *options_add(struct options *, const char *);
1.59 nicm 69: static void options_remove(struct options_entry *);
1.26 nicm 70:
71: #define OPTIONS_IS_STRING(o) \
72: ((o)->tableentry == NULL || \
73: (o)->tableentry->type == OPTIONS_TABLE_STRING)
74: #define OPTIONS_IS_NUMBER(o) \
75: ((o)->tableentry != NULL && \
76: ((o)->tableentry->type == OPTIONS_TABLE_NUMBER || \
77: (o)->tableentry->type == OPTIONS_TABLE_KEY || \
78: (o)->tableentry->type == OPTIONS_TABLE_COLOUR || \
79: (o)->tableentry->type == OPTIONS_TABLE_FLAG || \
80: (o)->tableentry->type == OPTIONS_TABLE_CHOICE))
1.43 nicm 81: #define OPTIONS_IS_COMMAND(o) \
82: ((o)->tableentry != NULL && \
83: (o)->tableentry->type == OPTIONS_TABLE_COMMAND)
1.41 nicm 84:
85: #define OPTIONS_IS_ARRAY(o) \
1.26 nicm 86: ((o)->tableentry != NULL && \
1.41 nicm 87: ((o)->tableentry->flags & OPTIONS_TABLE_IS_ARRAY))
1.26 nicm 88:
1.27 nicm 89: static int options_cmp(struct options_entry *, struct options_entry *);
90: RB_GENERATE_STATIC(options_tree, options_entry, entry, options_cmp);
1.1 nicm 91:
1.17 nicm 92: static int
1.27 nicm 93: options_cmp(struct options_entry *lhs, struct options_entry *rhs)
1.26 nicm 94: {
95: return (strcmp(lhs->name, rhs->name));
96: }
97:
1.60 nicm 98: static const char *
99: options_map_name(const char *name)
100: {
101: const struct options_name_map *map;
102:
103: for (map = options_other_names; map->from != NULL; map++) {
104: if (strcmp(map->from, name) == 0)
105: return (map->to);
106: }
107: return (name);
108: }
109:
1.26 nicm 110: static const struct options_table_entry *
111: options_parent_table_entry(struct options *oo, const char *s)
1.1 nicm 112: {
1.27 nicm 113: struct options_entry *o;
1.26 nicm 114:
115: if (oo->parent == NULL)
116: fatalx("no parent options for %s", s);
1.50 nicm 117: o = options_get(oo->parent, s);
1.26 nicm 118: if (o == NULL)
119: fatalx("%s not in parent options", s);
120: return (o->tableentry);
1.1 nicm 121: }
122:
1.41 nicm 123: static void
124: options_value_free(struct options_entry *o, union options_value *ov)
125: {
126: if (OPTIONS_IS_STRING(o))
127: free(ov->string);
1.43 nicm 128: if (OPTIONS_IS_COMMAND(o) && ov->cmdlist != NULL)
129: cmd_list_free(ov->cmdlist);
1.41 nicm 130: }
131:
1.42 nicm 132: static char *
1.57 nicm 133: options_value_to_string(struct options_entry *o, union options_value *ov,
1.41 nicm 134: int numeric)
135: {
1.42 nicm 136: char *s;
1.41 nicm 137:
1.43 nicm 138: if (OPTIONS_IS_COMMAND(o))
1.46 nicm 139: return (cmd_list_print(ov->cmdlist, 0));
1.41 nicm 140: if (OPTIONS_IS_NUMBER(o)) {
141: switch (o->tableentry->type) {
142: case OPTIONS_TABLE_NUMBER:
1.42 nicm 143: xasprintf(&s, "%lld", ov->number);
1.41 nicm 144: break;
145: case OPTIONS_TABLE_KEY:
1.58 nicm 146: s = xstrdup(key_string_lookup_key(ov->number, 0));
1.41 nicm 147: break;
148: case OPTIONS_TABLE_COLOUR:
1.42 nicm 149: s = xstrdup(colour_tostring(ov->number));
1.41 nicm 150: break;
151: case OPTIONS_TABLE_FLAG:
152: if (numeric)
1.42 nicm 153: xasprintf(&s, "%lld", ov->number);
1.41 nicm 154: else
1.42 nicm 155: s = xstrdup(ov->number ? "on" : "off");
1.41 nicm 156: break;
157: case OPTIONS_TABLE_CHOICE:
1.42 nicm 158: s = xstrdup(o->tableentry->choices[ov->number]);
1.41 nicm 159: break;
1.61 nicm 160: default:
1.42 nicm 161: fatalx("not a number option type");
1.41 nicm 162: }
163: return (s);
164: }
165: if (OPTIONS_IS_STRING(o))
1.42 nicm 166: return (xstrdup(ov->string));
167: return (xstrdup(""));
1.41 nicm 168: }
169:
1.13 nicm 170: struct options *
171: options_create(struct options *parent)
1.1 nicm 172: {
1.16 nicm 173: struct options *oo;
1.13 nicm 174:
175: oo = xcalloc(1, sizeof *oo);
1.7 nicm 176: RB_INIT(&oo->tree);
1.1 nicm 177: oo->parent = parent;
1.13 nicm 178: return (oo);
1.1 nicm 179: }
180:
181: void
182: options_free(struct options *oo)
183: {
1.27 nicm 184: struct options_entry *o, *tmp;
1.1 nicm 185:
1.35 nicm 186: RB_FOREACH_SAFE(o, options_tree, &oo->tree, tmp)
1.26 nicm 187: options_remove(o);
1.13 nicm 188: free(oo);
189: }
190:
1.57 nicm 191: struct options *
192: options_get_parent(struct options *oo)
193: {
194: return (oo->parent);
195: }
196:
1.50 nicm 197: void
198: options_set_parent(struct options *oo, struct options *parent)
199: {
200: oo->parent = parent;
201: }
202:
1.27 nicm 203: struct options_entry *
1.13 nicm 204: options_first(struct options *oo)
205: {
206: return (RB_MIN(options_tree, &oo->tree));
207: }
208:
1.27 nicm 209: struct options_entry *
210: options_next(struct options_entry *o)
1.13 nicm 211: {
212: return (RB_NEXT(options_tree, &oo->tree, o));
1.1 nicm 213: }
214:
1.27 nicm 215: struct options_entry *
1.26 nicm 216: options_get_only(struct options *oo, const char *name)
1.1 nicm 217: {
1.60 nicm 218: struct options_entry o = { .name = name }, *found;
1.1 nicm 219:
1.60 nicm 220: found = RB_FIND(options_tree, &oo->tree, &o);
221: if (found == NULL) {
222: o.name = options_map_name(name);
223: return (RB_FIND(options_tree, &oo->tree, &o));
224: }
225: return (found);
1.1 nicm 226: }
227:
1.27 nicm 228: struct options_entry *
1.26 nicm 229: options_get(struct options *oo, const char *name)
1.1 nicm 230: {
1.27 nicm 231: struct options_entry *o;
1.1 nicm 232:
1.26 nicm 233: o = options_get_only(oo, name);
1.1 nicm 234: while (o == NULL) {
235: oo = oo->parent;
236: if (oo == NULL)
237: break;
1.26 nicm 238: o = options_get_only(oo, name);
239: }
240: return (o);
241: }
242:
1.27 nicm 243: struct options_entry *
1.26 nicm 244: options_empty(struct options *oo, const struct options_table_entry *oe)
245: {
1.27 nicm 246: struct options_entry *o;
1.26 nicm 247:
248: o = options_add(oo, oe->name);
249: o->tableentry = oe;
250:
1.42 nicm 251: if (oe->flags & OPTIONS_TABLE_IS_ARRAY)
1.41 nicm 252: RB_INIT(&o->value.array);
1.39 nicm 253:
1.26 nicm 254: return (o);
255: }
256:
1.27 nicm 257: struct options_entry *
1.26 nicm 258: options_default(struct options *oo, const struct options_table_entry *oe)
259: {
1.41 nicm 260: struct options_entry *o;
261: union options_value *ov;
262: u_int i;
1.26 nicm 263:
264: o = options_empty(oo, oe);
1.41 nicm 265: ov = &o->value;
266:
267: if (oe->flags & OPTIONS_TABLE_IS_ARRAY) {
1.43 nicm 268: if (oe->default_arr == NULL) {
269: options_array_assign(o, oe->default_str, NULL);
270: return (o);
271: }
272: for (i = 0; oe->default_arr[i] != NULL; i++)
273: options_array_set(o, i, oe->default_arr[i], 0, NULL);
1.41 nicm 274: return (o);
275: }
276:
277: switch (oe->type) {
278: case OPTIONS_TABLE_STRING:
279: ov->string = xstrdup(oe->default_str);
280: break;
281: default:
282: ov->number = oe->default_num;
283: break;
284: }
1.26 nicm 285: return (o);
286: }
287:
1.57 nicm 288: char *
289: options_default_to_string(const struct options_table_entry *oe)
290: {
291: char *s;
292:
293: switch (oe->type) {
294: case OPTIONS_TABLE_STRING:
295: case OPTIONS_TABLE_COMMAND:
296: s = xstrdup(oe->default_str);
297: break;
298: case OPTIONS_TABLE_NUMBER:
299: xasprintf(&s, "%lld", oe->default_num);
300: break;
301: case OPTIONS_TABLE_KEY:
1.58 nicm 302: s = xstrdup(key_string_lookup_key(oe->default_num, 0));
1.57 nicm 303: break;
304: case OPTIONS_TABLE_COLOUR:
305: s = xstrdup(colour_tostring(oe->default_num));
306: break;
307: case OPTIONS_TABLE_FLAG:
308: s = xstrdup(oe->default_num ? "on" : "off");
309: break;
310: case OPTIONS_TABLE_CHOICE:
311: s = xstrdup(oe->choices[oe->default_num]);
312: break;
1.61 nicm 313: default:
314: fatalx("unknown option type");
1.57 nicm 315: }
316: return (s);
317: }
318:
1.27 nicm 319: static struct options_entry *
1.26 nicm 320: options_add(struct options *oo, const char *name)
321: {
1.27 nicm 322: struct options_entry *o;
1.26 nicm 323:
324: o = options_get_only(oo, name);
325: if (o != NULL)
326: options_remove(o);
327:
328: o = xcalloc(1, sizeof *o);
329: o->owner = oo;
330: o->name = xstrdup(name);
331:
332: RB_INSERT(options_tree, &oo->tree, o);
1.1 nicm 333: return (o);
334: }
335:
1.59 nicm 336: static void
1.27 nicm 337: options_remove(struct options_entry *o)
1.26 nicm 338: {
339: struct options *oo = o->owner;
340:
1.41 nicm 341: if (OPTIONS_IS_ARRAY(o))
1.39 nicm 342: options_array_clear(o);
1.41 nicm 343: else
344: options_value_free(o, &o->value);
1.26 nicm 345: RB_REMOVE(options_tree, &oo->tree, o);
1.53 nicm 346: free((void *)o->name);
1.26 nicm 347: free(o);
348: }
349:
350: const char *
1.27 nicm 351: options_name(struct options_entry *o)
1.26 nicm 352: {
353: return (o->name);
354: }
355:
1.57 nicm 356: struct options *
357: options_owner(struct options_entry *o)
358: {
359: return (o->owner);
360: }
361:
1.26 nicm 362: const struct options_table_entry *
1.27 nicm 363: options_table_entry(struct options_entry *o)
1.26 nicm 364: {
365: return (o->tableentry);
366: }
367:
1.39 nicm 368: static struct options_array_item *
369: options_array_item(struct options_entry *o, u_int idx)
370: {
371: struct options_array_item a;
372:
373: a.index = idx;
1.41 nicm 374: return (RB_FIND(options_array, &o->value.array, &a));
1.39 nicm 375: }
376:
1.54 nicm 377: static struct options_array_item *
378: options_array_new(struct options_entry *o, u_int idx)
379: {
380: struct options_array_item *a;
381:
382: a = xcalloc(1, sizeof *a);
383: a->index = idx;
384: RB_INSERT(options_array, &o->value.array, a);
385: return (a);
386: }
387:
1.39 nicm 388: static void
389: options_array_free(struct options_entry *o, struct options_array_item *a)
390: {
1.41 nicm 391: options_value_free(o, &a->value);
392: RB_REMOVE(options_array, &o->value.array, a);
1.39 nicm 393: free(a);
394: }
395:
1.31 nicm 396: void
397: options_array_clear(struct options_entry *o)
398: {
1.39 nicm 399: struct options_array_item *a, *a1;
400:
401: if (!OPTIONS_IS_ARRAY(o))
402: return;
403:
1.41 nicm 404: RB_FOREACH_SAFE(a, options_array, &o->value.array, a1)
1.63 nicm 405: options_array_free(o, a);
1.31 nicm 406: }
407:
1.41 nicm 408: union options_value *
1.27 nicm 409: options_array_get(struct options_entry *o, u_int idx)
1.26 nicm 410: {
1.39 nicm 411: struct options_array_item *a;
412:
1.26 nicm 413: if (!OPTIONS_IS_ARRAY(o))
414: return (NULL);
1.39 nicm 415: a = options_array_item(o, idx);
416: if (a == NULL)
1.26 nicm 417: return (NULL);
1.41 nicm 418: return (&a->value);
1.26 nicm 419: }
420:
421: int
1.31 nicm 422: options_array_set(struct options_entry *o, u_int idx, const char *value,
1.43 nicm 423: int append, char **cause)
1.26 nicm 424: {
1.39 nicm 425: struct options_array_item *a;
426: char *new;
1.45 nicm 427: struct cmd_parse_result *pr;
1.63 nicm 428: long long number;
1.26 nicm 429:
1.43 nicm 430: if (!OPTIONS_IS_ARRAY(o)) {
1.44 nicm 431: if (cause != NULL)
432: *cause = xstrdup("not an array");
1.26 nicm 433: return (-1);
1.43 nicm 434: }
435:
1.54 nicm 436: if (value == NULL) {
437: a = options_array_item(o, idx);
438: if (a != NULL)
439: options_array_free(o, a);
440: return (0);
441: }
442:
443: if (OPTIONS_IS_COMMAND(o)) {
1.45 nicm 444: pr = cmd_parse_from_string(value, NULL);
445: switch (pr->status) {
446: case CMD_PARSE_ERROR:
1.44 nicm 447: if (cause != NULL)
1.45 nicm 448: *cause = pr->error;
1.44 nicm 449: else
1.45 nicm 450: free(pr->error);
1.43 nicm 451: return (-1);
1.45 nicm 452: case CMD_PARSE_SUCCESS:
453: break;
1.44 nicm 454: }
1.26 nicm 455:
1.54 nicm 456: a = options_array_item(o, idx);
457: if (a == NULL)
458: a = options_array_new(o, idx);
459: else
460: options_value_free(o, &a->value);
461: a->value.cmdlist = pr->cmdlist;
1.39 nicm 462: return (0);
1.26 nicm 463: }
1.31 nicm 464:
1.43 nicm 465: if (OPTIONS_IS_STRING(o)) {
1.54 nicm 466: a = options_array_item(o, idx);
1.43 nicm 467: if (a != NULL && append)
468: xasprintf(&new, "%s%s", a->value.string, value);
469: else
470: new = xstrdup(value);
1.54 nicm 471: if (a == NULL)
472: a = options_array_new(o, idx);
473: else
474: options_value_free(o, &a->value);
475: a->value.string = new;
476: return (0);
1.43 nicm 477: }
478:
1.63 nicm 479: if (o->tableentry->type == OPTIONS_TABLE_COLOUR) {
480: if ((number = colour_fromstring(value)) == -1) {
481: xasprintf(cause, "bad colour: %s", value);
482: return (-1);
483: }
484: a = options_array_item(o, idx);
485: if (a == NULL)
486: a = options_array_new(o, idx);
487: else
488: options_value_free(o, &a->value);
489: a->value.number = number;
490: return (0);
491: }
492:
1.54 nicm 493: if (cause != NULL)
494: *cause = xstrdup("wrong array type");
495: return (-1);
1.26 nicm 496: }
497:
1.43 nicm 498: int
499: options_array_assign(struct options_entry *o, const char *s, char **cause)
1.31 nicm 500: {
501: const char *separator;
502: char *copy, *next, *string;
503: u_int i;
504:
505: separator = o->tableentry->separator;
506: if (separator == NULL)
507: separator = " ,";
1.43 nicm 508: if (*separator == '\0') {
509: if (*s == '\0')
510: return (0);
511: for (i = 0; i < UINT_MAX; i++) {
512: if (options_array_item(o, i) == NULL)
513: break;
514: }
515: return (options_array_set(o, i, s, 0, cause));
516: }
1.31 nicm 517:
1.43 nicm 518: if (*s == '\0')
519: return (0);
1.31 nicm 520: copy = string = xstrdup(s);
521: while ((next = strsep(&string, separator)) != NULL) {
522: if (*next == '\0')
523: continue;
1.39 nicm 524: for (i = 0; i < UINT_MAX; i++) {
525: if (options_array_item(o, i) == NULL)
1.31 nicm 526: break;
527: }
1.39 nicm 528: if (i == UINT_MAX)
1.31 nicm 529: break;
1.43 nicm 530: if (options_array_set(o, i, next, 0, cause) != 0) {
531: free(copy);
532: return (-1);
533: }
1.31 nicm 534: }
535: free(copy);
1.43 nicm 536: return (0);
1.31 nicm 537: }
538:
1.39 nicm 539: struct options_array_item *
540: options_array_first(struct options_entry *o)
541: {
542: if (!OPTIONS_IS_ARRAY(o))
543: return (NULL);
1.41 nicm 544: return (RB_MIN(options_array, &o->value.array));
1.39 nicm 545: }
546:
547: struct options_array_item *
548: options_array_next(struct options_array_item *a)
549: {
1.41 nicm 550: return (RB_NEXT(options_array, &o->value.array, a));
1.39 nicm 551: }
552:
553: u_int
554: options_array_item_index(struct options_array_item *a)
555: {
556: return (a->index);
557: }
558:
1.41 nicm 559: union options_value *
1.39 nicm 560: options_array_item_value(struct options_array_item *a)
561: {
1.41 nicm 562: return (&a->value);
1.39 nicm 563: }
564:
565: int
1.57 nicm 566: options_is_array(struct options_entry *o)
1.39 nicm 567: {
568: return (OPTIONS_IS_ARRAY(o));
569: }
570:
1.26 nicm 571: int
1.57 nicm 572: options_is_string(struct options_entry *o)
1.26 nicm 573: {
1.41 nicm 574: return (OPTIONS_IS_STRING(o));
1.26 nicm 575: }
576:
1.42 nicm 577: char *
1.57 nicm 578: options_to_string(struct options_entry *o, int idx, int numeric)
1.26 nicm 579: {
1.39 nicm 580: struct options_array_item *a;
1.26 nicm 581:
582: if (OPTIONS_IS_ARRAY(o)) {
583: if (idx == -1)
1.42 nicm 584: return (xstrdup(""));
1.39 nicm 585: a = options_array_item(o, idx);
586: if (a == NULL)
1.42 nicm 587: return (xstrdup(""));
1.57 nicm 588: return (options_value_to_string(o, &a->value, numeric));
1.26 nicm 589: }
1.57 nicm 590: return (options_value_to_string(o, &o->value, numeric));
1.26 nicm 591: }
592:
593: char *
594: options_parse(const char *name, int *idx)
1.1 nicm 595: {
1.26 nicm 596: char *copy, *cp, *end;
1.1 nicm 597:
1.26 nicm 598: if (*name == '\0')
599: return (NULL);
600: copy = xstrdup(name);
601: if ((cp = strchr(copy, '[')) == NULL) {
602: *idx = -1;
603: return (copy);
604: }
605: end = strchr(cp + 1, ']');
606: if (end == NULL || end[1] != '\0' || !isdigit((u_char)end[-1])) {
607: free(copy);
608: return (NULL);
609: }
610: if (sscanf(cp, "[%d]", idx) != 1 || *idx < 0) {
611: free(copy);
612: return (NULL);
613: }
614: *cp = '\0';
615: return (copy);
1.1 nicm 616: }
617:
1.27 nicm 618: struct options_entry *
1.26 nicm 619: options_parse_get(struct options *oo, const char *s, int *idx, int only)
1.1 nicm 620: {
1.27 nicm 621: struct options_entry *o;
622: char *name;
1.1 nicm 623:
1.26 nicm 624: name = options_parse(s, idx);
625: if (name == NULL)
626: return (NULL);
627: if (only)
628: o = options_get_only(oo, name);
629: else
630: o = options_get(oo, name);
631: free(name);
632: return (o);
633: }
1.1 nicm 634:
1.26 nicm 635: char *
1.50 nicm 636: options_match(const char *s, int *idx, int *ambiguous)
1.26 nicm 637: {
638: const struct options_table_entry *oe, *found;
1.60 nicm 639: char *parsed;
640: const char *name;
1.26 nicm 641: size_t namelen;
642:
1.60 nicm 643: parsed = options_parse(s, idx);
644: if (parsed == NULL)
1.33 nicm 645: return (NULL);
1.60 nicm 646: if (*parsed == '@') {
1.29 nicm 647: *ambiguous = 0;
1.60 nicm 648: return (parsed);
1.29 nicm 649: }
1.26 nicm 650:
1.60 nicm 651: name = options_map_name(parsed);
652: namelen = strlen(name);
653:
1.26 nicm 654: found = NULL;
655: for (oe = options_table; oe->name != NULL; oe++) {
656: if (strcmp(oe->name, name) == 0) {
657: found = oe;
658: break;
659: }
660: if (strncmp(oe->name, name, namelen) == 0) {
661: if (found != NULL) {
662: *ambiguous = 1;
1.60 nicm 663: free(parsed);
1.26 nicm 664: return (NULL);
665: }
666: found = oe;
667: }
668: }
1.60 nicm 669: free(parsed);
1.26 nicm 670: if (found == NULL) {
671: *ambiguous = 0;
672: return (NULL);
1.22 nicm 673: }
1.26 nicm 674: return (xstrdup(found->name));
675: }
1.22 nicm 676:
1.27 nicm 677: struct options_entry *
1.26 nicm 678: options_match_get(struct options *oo, const char *s, int *idx, int only,
1.55 nicm 679: int *ambiguous)
1.26 nicm 680: {
1.27 nicm 681: char *name;
682: struct options_entry *o;
1.21 nicm 683:
1.26 nicm 684: name = options_match(s, idx, ambiguous);
685: if (name == NULL)
686: return (NULL);
687: *ambiguous = 0;
688: if (only)
689: o = options_get_only(oo, name);
690: else
691: o = options_get(oo, name);
692: free(name);
1.4 nicm 693: return (o);
1.1 nicm 694: }
1.26 nicm 695:
1.23 nicm 696: const char *
1.1 nicm 697: options_get_string(struct options *oo, const char *name)
698: {
1.27 nicm 699: struct options_entry *o;
1.26 nicm 700:
701: o = options_get(oo, name);
702: if (o == NULL)
703: fatalx("missing option %s", name);
704: if (!OPTIONS_IS_STRING(o))
705: fatalx("option %s is not a string", name);
1.41 nicm 706: return (o->value.string);
1.26 nicm 707: }
708:
709: long long
710: options_get_number(struct options *oo, const char *name)
711: {
1.27 nicm 712: struct options_entry *o;
1.1 nicm 713:
1.26 nicm 714: o = options_get(oo, name);
715: if (o == NULL)
1.15 nicm 716: fatalx("missing option %s", name);
1.26 nicm 717: if (!OPTIONS_IS_NUMBER(o))
1.61 nicm 718: fatalx("option %s is not a number", name);
1.41 nicm 719: return (o->value.number);
1.1 nicm 720: }
721:
1.27 nicm 722: struct options_entry *
1.26 nicm 723: options_set_string(struct options *oo, const char *name, int append,
724: const char *fmt, ...)
1.1 nicm 725: {
1.27 nicm 726: struct options_entry *o;
727: va_list ap;
1.56 nicm 728: const char *separator = "";
1.27 nicm 729: char *s, *value;
1.1 nicm 730:
1.26 nicm 731: va_start(ap, fmt);
732: xvasprintf(&s, fmt, ap);
733: va_end(ap);
734:
735: o = options_get_only(oo, name);
736: if (o != NULL && append && OPTIONS_IS_STRING(o)) {
1.56 nicm 737: if (*name != '@') {
738: separator = o->tableentry->separator;
739: if (separator == NULL)
740: separator = "";
741: }
742: xasprintf(&value, "%s%s%s", o->value.string, separator, s);
1.26 nicm 743: free(s);
744: } else
745: value = s;
746: if (o == NULL && *name == '@')
747: o = options_add(oo, name);
748: else if (o == NULL) {
749: o = options_default(oo, options_parent_table_entry(oo, name));
750: if (o == NULL)
751: return (NULL);
752: }
1.21 nicm 753:
1.26 nicm 754: if (!OPTIONS_IS_STRING(o))
755: fatalx("option %s is not a string", name);
1.41 nicm 756: free(o->value.string);
757: o->value.string = value;
1.56 nicm 758: o->cached = 0;
1.4 nicm 759: return (o);
1.1 nicm 760: }
761:
1.27 nicm 762: struct options_entry *
1.26 nicm 763: options_set_number(struct options *oo, const char *name, long long value)
1.1 nicm 764: {
1.27 nicm 765: struct options_entry *o;
1.26 nicm 766:
767: if (*name == '@')
768: fatalx("user option %s must be a string", name);
1.1 nicm 769:
1.26 nicm 770: o = options_get_only(oo, name);
771: if (o == NULL) {
772: o = options_default(oo, options_parent_table_entry(oo, name));
773: if (o == NULL)
774: return (NULL);
775: }
776:
777: if (!OPTIONS_IS_NUMBER(o))
778: fatalx("option %s is not a number", name);
1.41 nicm 779: o->value.number = value;
1.26 nicm 780: return (o);
1.10 nicm 781: }
782:
1.50 nicm 783: int
1.49 nicm 784: options_scope_from_name(struct args *args, int window,
785: const char *name, struct cmd_find_state *fs, struct options **oo,
786: char **cause)
787: {
1.50 nicm 788: struct session *s = fs->s;
789: struct winlink *wl = fs->wl;
790: struct window_pane *wp = fs->wp;
791: const char *target = args_get(args, 't');
792: const struct options_table_entry *oe;
1.51 nicm 793: int scope = OPTIONS_TABLE_NONE;
1.49 nicm 794:
795: if (*name == '@')
796: return (options_scope_from_flags(args, window, fs, oo, cause));
797:
1.50 nicm 798: for (oe = options_table; oe->name != NULL; oe++) {
799: if (strcmp(oe->name, name) == 0)
800: break;
801: }
802: if (oe->name == NULL) {
1.49 nicm 803: xasprintf(cause, "unknown option: %s", name);
804: return (OPTIONS_TABLE_NONE);
805: }
1.51 nicm 806: switch (oe->scope) {
1.50 nicm 807: case OPTIONS_TABLE_SERVER:
1.49 nicm 808: *oo = global_options;
1.51 nicm 809: scope = OPTIONS_TABLE_SERVER;
1.50 nicm 810: break;
811: case OPTIONS_TABLE_SESSION:
1.51 nicm 812: if (args_has(args, 'g')) {
1.49 nicm 813: *oo = global_s_options;
1.51 nicm 814: scope = OPTIONS_TABLE_SESSION;
815: } else if (s == NULL && target != NULL)
1.50 nicm 816: xasprintf(cause, "no such session: %s", target);
817: else if (s == NULL)
818: xasprintf(cause, "no current session");
1.51 nicm 819: else {
1.50 nicm 820: *oo = s->options;
1.51 nicm 821: scope = OPTIONS_TABLE_SESSION;
822: }
1.50 nicm 823: break;
824: case OPTIONS_TABLE_WINDOW|OPTIONS_TABLE_PANE:
825: if (args_has(args, 'p')) {
826: if (wp == NULL && target != NULL)
827: xasprintf(cause, "no such pane: %s", target);
828: else if (wp == NULL)
829: xasprintf(cause, "no current pane");
1.51 nicm 830: else {
1.50 nicm 831: *oo = wp->options;
1.51 nicm 832: scope = OPTIONS_TABLE_PANE;
833: }
1.50 nicm 834: break;
835: }
836: /* FALLTHROUGH */
837: case OPTIONS_TABLE_WINDOW:
1.51 nicm 838: if (args_has(args, 'g')) {
1.49 nicm 839: *oo = global_w_options;
1.51 nicm 840: scope = OPTIONS_TABLE_WINDOW;
841: } else if (wl == NULL && target != NULL)
1.50 nicm 842: xasprintf(cause, "no such window: %s", target);
843: else if (wl == NULL)
844: xasprintf(cause, "no current window");
1.51 nicm 845: else {
1.49 nicm 846: *oo = wl->window->options;
1.51 nicm 847: scope = OPTIONS_TABLE_WINDOW;
848: }
1.50 nicm 849: break;
1.49 nicm 850: }
851: return (scope);
852: }
853:
1.50 nicm 854: int
1.26 nicm 855: options_scope_from_flags(struct args *args, int window,
856: struct cmd_find_state *fs, struct options **oo, char **cause)
857: {
1.50 nicm 858: struct session *s = fs->s;
859: struct winlink *wl = fs->wl;
860: struct window_pane *wp = fs->wp;
861: const char *target = args_get(args, 't');
1.26 nicm 862:
863: if (args_has(args, 's')) {
864: *oo = global_options;
865: return (OPTIONS_TABLE_SERVER);
866: }
1.12 nicm 867:
1.50 nicm 868: if (args_has(args, 'p')) {
869: if (wp == NULL) {
870: if (target != NULL)
871: xasprintf(cause, "no such pane: %s", target);
872: else
873: xasprintf(cause, "no current pane");
874: return (OPTIONS_TABLE_NONE);
875: }
876: *oo = wp->options;
877: return (OPTIONS_TABLE_PANE);
878: } else if (window || args_has(args, 'w')) {
1.26 nicm 879: if (args_has(args, 'g')) {
880: *oo = global_w_options;
881: return (OPTIONS_TABLE_WINDOW);
882: }
883: if (wl == NULL) {
884: if (target != NULL)
885: xasprintf(cause, "no such window: %s", target);
886: else
887: xasprintf(cause, "no current window");
888: return (OPTIONS_TABLE_NONE);
889: }
890: *oo = wl->window->options;
891: return (OPTIONS_TABLE_WINDOW);
892: } else {
893: if (args_has(args, 'g')) {
894: *oo = global_s_options;
895: return (OPTIONS_TABLE_SESSION);
896: }
897: if (s == NULL) {
898: if (target != NULL)
899: xasprintf(cause, "no such session: %s", target);
900: else
901: xasprintf(cause, "no current session");
902: return (OPTIONS_TABLE_NONE);
903: }
904: *oo = s->options;
905: return (OPTIONS_TABLE_SESSION);
906: }
1.56 nicm 907: }
908:
909: struct style *
910: options_string_to_style(struct options *oo, const char *name,
911: struct format_tree *ft)
912: {
913: struct options_entry *o;
914: const char *s;
915: char *expanded;
916:
917: o = options_get(oo, name);
918: if (o == NULL || !OPTIONS_IS_STRING(o))
919: return (NULL);
920:
921: if (o->cached)
922: return (&o->style);
923: s = o->value.string;
924: log_debug("%s: %s is '%s'", __func__, name, s);
925:
926: style_set(&o->style, &grid_default_cell);
927: o->cached = (strstr(s, "#{") == NULL);
928:
929: if (ft != NULL && !o->cached) {
930: expanded = format_expand(ft, s);
931: if (style_parse(&o->style, &grid_default_cell, expanded) != 0) {
932: free(expanded);
933: return (NULL);
934: }
935: free(expanded);
936: } else {
937: if (style_parse(&o->style, &grid_default_cell, s) != 0)
938: return (NULL);
939: }
940: return (&o->style);
1.57 nicm 941: }
942:
943: static int
944: options_from_string_check(const struct options_table_entry *oe,
945: const char *value, char **cause)
946: {
947: struct style sy;
948:
949: if (oe == NULL)
950: return (0);
951: if (strcmp(oe->name, "default-shell") == 0 && !checkshell(value)) {
952: xasprintf(cause, "not a suitable shell: %s", value);
953: return (-1);
954: }
955: if (oe->pattern != NULL && fnmatch(oe->pattern, value, 0) != 0) {
956: xasprintf(cause, "value is invalid: %s", value);
957: return (-1);
958: }
959: if ((oe->flags & OPTIONS_TABLE_IS_STYLE) &&
960: strstr(value, "#{") == NULL &&
961: style_parse(&sy, &grid_default_cell, value) != 0) {
962: xasprintf(cause, "invalid style: %s", value);
963: return (-1);
964: }
965: return (0);
966: }
967:
968: static int
969: options_from_string_flag(struct options *oo, const char *name,
970: const char *value, char **cause)
971: {
972: int flag;
973:
974: if (value == NULL || *value == '\0')
975: flag = !options_get_number(oo, name);
976: else if (strcmp(value, "1") == 0 ||
977: strcasecmp(value, "on") == 0 ||
978: strcasecmp(value, "yes") == 0)
979: flag = 1;
980: else if (strcmp(value, "0") == 0 ||
981: strcasecmp(value, "off") == 0 ||
982: strcasecmp(value, "no") == 0)
983: flag = 0;
984: else {
985: xasprintf(cause, "bad value: %s", value);
986: return (-1);
987: }
988: options_set_number(oo, name, flag);
989: return (0);
990: }
991:
1.65 nicm 992: int
993: options_find_choice(const struct options_table_entry *oe, const char *value,
994: char **cause)
995: {
996: const char **cp;
997: int n = 0, choice = -1;
998:
999: for (cp = oe->choices; *cp != NULL; cp++) {
1000: if (strcmp(*cp, value) == 0)
1001: choice = n;
1002: n++;
1003: }
1004: if (choice == -1) {
1005: xasprintf(cause, "unknown value: %s", value);
1006: return (-1);
1007: }
1008: return (choice);
1009: }
1010:
1.57 nicm 1011: static int
1012: options_from_string_choice(const struct options_table_entry *oe,
1013: struct options *oo, const char *name, const char *value, char **cause)
1014: {
1.65 nicm 1015: int choice = -1;
1.57 nicm 1016:
1017: if (value == NULL) {
1018: choice = options_get_number(oo, name);
1019: if (choice < 2)
1020: choice = !choice;
1021: } else {
1.65 nicm 1022: choice = options_find_choice(oe, value, cause);
1023: if (choice < 0)
1.57 nicm 1024: return (-1);
1025: }
1026: options_set_number(oo, name, choice);
1027: return (0);
1028: }
1029:
1030: int
1031: options_from_string(struct options *oo, const struct options_table_entry *oe,
1032: const char *name, const char *value, int append, char **cause)
1033: {
1034: enum options_table_type type;
1035: long long number;
1036: const char *errstr, *new;
1037: char *old;
1038: key_code key;
1039:
1040: if (oe != NULL) {
1041: if (value == NULL &&
1042: oe->type != OPTIONS_TABLE_FLAG &&
1043: oe->type != OPTIONS_TABLE_CHOICE) {
1044: xasprintf(cause, "empty value");
1045: return (-1);
1046: }
1047: type = oe->type;
1048: } else {
1049: if (*name != '@') {
1050: xasprintf(cause, "bad option name");
1051: return (-1);
1052: }
1053: type = OPTIONS_TABLE_STRING;
1054: }
1055:
1056: switch (type) {
1057: case OPTIONS_TABLE_STRING:
1058: old = xstrdup(options_get_string(oo, name));
1059: options_set_string(oo, name, append, "%s", value);
1060:
1061: new = options_get_string(oo, name);
1062: if (options_from_string_check(oe, new, cause) != 0) {
1063: options_set_string(oo, name, 0, "%s", old);
1064: free(old);
1065: return (-1);
1066: }
1067: free(old);
1068: return (0);
1069: case OPTIONS_TABLE_NUMBER:
1070: number = strtonum(value, oe->minimum, oe->maximum, &errstr);
1071: if (errstr != NULL) {
1072: xasprintf(cause, "value is %s: %s", errstr, value);
1073: return (-1);
1074: }
1075: options_set_number(oo, name, number);
1076: return (0);
1077: case OPTIONS_TABLE_KEY:
1078: key = key_string_lookup_string(value);
1079: if (key == KEYC_UNKNOWN) {
1080: xasprintf(cause, "bad key: %s", value);
1081: return (-1);
1082: }
1083: options_set_number(oo, name, key);
1084: return (0);
1085: case OPTIONS_TABLE_COLOUR:
1086: if ((number = colour_fromstring(value)) == -1) {
1087: xasprintf(cause, "bad colour: %s", value);
1088: return (-1);
1089: }
1090: options_set_number(oo, name, number);
1091: return (0);
1092: case OPTIONS_TABLE_FLAG:
1093: return (options_from_string_flag(oo, name, value, cause));
1094: case OPTIONS_TABLE_CHOICE:
1095: return (options_from_string_choice(oe, oo, name, value, cause));
1096: case OPTIONS_TABLE_COMMAND:
1097: break;
1098: }
1099: return (-1);
1100: }
1101:
1102: void
1103: options_push_changes(const char *name)
1104: {
1105: struct client *loop;
1106: struct session *s;
1107: struct window *w;
1108: struct window_pane *wp;
1.66 nicm 1109: int c;
1.57 nicm 1110:
1111: if (strcmp(name, "automatic-rename") == 0) {
1112: RB_FOREACH(w, windows, &windows) {
1113: if (w->active == NULL)
1114: continue;
1.66 nicm 1115: if (options_get_number(w->options, name))
1.57 nicm 1116: w->active->flags |= PANE_CHANGED;
1.66 nicm 1117: }
1118: }
1119: if (strcmp(name, "cursor-colour") == 0) {
1120: RB_FOREACH(wp, window_pane_tree, &all_window_panes) {
1121: c = options_get_number(wp->options, name);
1122: wp->screen->default_ccolour = c;
1.67 ! nicm 1123: }
! 1124: }
! 1125: if (strcmp(name, "cursor-style") == 0) {
! 1126: RB_FOREACH(wp, window_pane_tree, &all_window_panes) {
! 1127: wp->screen->default_mode = 0;
! 1128: screen_set_cursor_style(options_get_number(wp->options,
! 1129: name), &wp->screen->default_cstyle,
! 1130: &wp->screen->default_mode);
1.57 nicm 1131: }
1132: }
1133: if (strcmp(name, "key-table") == 0) {
1134: TAILQ_FOREACH(loop, &clients, entry)
1135: server_client_set_key_table(loop, NULL);
1136: }
1137: if (strcmp(name, "user-keys") == 0) {
1138: TAILQ_FOREACH(loop, &clients, entry) {
1139: if (loop->tty.flags & TTY_OPENED)
1140: tty_keys_build(&loop->tty);
1141: }
1142: }
1143: if (strcmp(name, "status") == 0 ||
1144: strcmp(name, "status-interval") == 0)
1145: status_timer_start_all();
1146: if (strcmp(name, "monitor-silence") == 0)
1147: alerts_reset_all();
1148: if (strcmp(name, "window-style") == 0 ||
1149: strcmp(name, "window-active-style") == 0) {
1150: RB_FOREACH(wp, window_pane_tree, &all_window_panes)
1151: wp->flags |= PANE_STYLECHANGED;
1.63 nicm 1152: }
1153: if (strcmp(name, "pane-colours") == 0) {
1154: RB_FOREACH(wp, window_pane_tree, &all_window_panes)
1155: colour_palette_from_option(&wp->palette, wp->options);
1.57 nicm 1156: }
1157: if (strcmp(name, "pane-border-status") == 0) {
1158: RB_FOREACH(w, windows, &windows)
1.62 nicm 1159: layout_fix_panes(w, NULL);
1.57 nicm 1160: }
1161: RB_FOREACH(s, sessions, &sessions)
1162: status_update_cache(s);
1163:
1164: recalculate_sizes();
1165: TAILQ_FOREACH(loop, &clients, entry) {
1166: if (loop->session != NULL)
1167: server_redraw_client(loop);
1168: }
1.59 nicm 1169: }
1170:
1171: int
1172: options_remove_or_default(struct options_entry *o, int idx, char **cause)
1173: {
1174: struct options *oo = o->owner;
1175:
1176: if (idx == -1) {
1177: if (o->tableentry != NULL &&
1178: (oo == global_options ||
1179: oo == global_s_options ||
1180: oo == global_w_options))
1181: options_default(oo, o->tableentry);
1182: else
1183: options_remove(o);
1184: } else if (options_array_set(o, idx, NULL, 0, cause) != 0)
1185: return (-1);
1186: return (0);
1.1 nicm 1187: }