Annotation of src/usr.bin/tmux/options.c, Revision 1.29
1.29 ! nicm 1: /* $OpenBSD: options.c,v 1.28 2017/01/16 23:45:08 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.27 nicm 33: struct options_entry {
1.26 nicm 34: struct options *owner;
35:
36: const char *name;
37: const struct options_table_entry *tableentry;
38:
39: union {
40: char *string;
41: long long number;
42: struct grid_cell style;
43: struct {
44: const char **array;
45: u_int arraysize;
46: };
47: };
48:
1.27 nicm 49: RB_ENTRY(options_entry) entry;
1.26 nicm 50: };
51:
1.13 nicm 52: struct options {
1.27 nicm 53: RB_HEAD(options_tree, options_entry) tree;
1.26 nicm 54: struct options *parent;
1.13 nicm 55: };
56:
1.27 nicm 57: static struct options_entry *options_add(struct options *, const char *);
1.26 nicm 58:
59: #define OPTIONS_ARRAY_LIMIT 1000
60:
61: #define OPTIONS_IS_STRING(o) \
62: ((o)->tableentry == NULL || \
63: (o)->tableentry->type == OPTIONS_TABLE_STRING)
64: #define OPTIONS_IS_NUMBER(o) \
65: ((o)->tableentry != NULL && \
66: ((o)->tableentry->type == OPTIONS_TABLE_NUMBER || \
67: (o)->tableentry->type == OPTIONS_TABLE_KEY || \
68: (o)->tableentry->type == OPTIONS_TABLE_COLOUR || \
69: (o)->tableentry->type == OPTIONS_TABLE_ATTRIBUTES || \
70: (o)->tableentry->type == OPTIONS_TABLE_FLAG || \
71: (o)->tableentry->type == OPTIONS_TABLE_CHOICE))
72: #define OPTIONS_IS_STYLE(o) \
73: ((o)->tableentry != NULL && \
74: (o)->tableentry->type == OPTIONS_TABLE_STYLE)
75: #define OPTIONS_IS_ARRAY(o) \
76: ((o)->tableentry != NULL && \
77: (o)->tableentry->type == OPTIONS_TABLE_ARRAY)
78:
1.27 nicm 79: static int options_cmp(struct options_entry *, struct options_entry *);
80: RB_GENERATE_STATIC(options_tree, options_entry, entry, options_cmp);
1.1 nicm 81:
1.17 nicm 82: static int
1.27 nicm 83: options_cmp(struct options_entry *lhs, struct options_entry *rhs)
1.26 nicm 84: {
85: return (strcmp(lhs->name, rhs->name));
86: }
87:
88: static const struct options_table_entry *
89: options_parent_table_entry(struct options *oo, const char *s)
1.1 nicm 90: {
1.27 nicm 91: struct options_entry *o;
1.26 nicm 92:
93: if (oo->parent == NULL)
94: fatalx("no parent options for %s", s);
95: o = options_get_only(oo->parent, s);
96: if (o == NULL)
97: fatalx("%s not in parent options", s);
98: return (o->tableentry);
1.1 nicm 99: }
100:
1.13 nicm 101: struct options *
102: options_create(struct options *parent)
1.1 nicm 103: {
1.16 nicm 104: struct options *oo;
1.13 nicm 105:
106: oo = xcalloc(1, sizeof *oo);
1.7 nicm 107: RB_INIT(&oo->tree);
1.1 nicm 108: oo->parent = parent;
1.13 nicm 109: return (oo);
1.1 nicm 110: }
111:
112: void
113: options_free(struct options *oo)
114: {
1.27 nicm 115: struct options_entry *o, *tmp;
1.1 nicm 116:
1.26 nicm 117: RB_FOREACH_SAFE (o, options_tree, &oo->tree, tmp)
118: options_remove(o);
1.13 nicm 119: free(oo);
120: }
121:
1.27 nicm 122: struct options_entry *
1.13 nicm 123: options_first(struct options *oo)
124: {
125: return (RB_MIN(options_tree, &oo->tree));
126: }
127:
1.27 nicm 128: struct options_entry *
129: options_next(struct options_entry *o)
1.13 nicm 130: {
131: return (RB_NEXT(options_tree, &oo->tree, o));
1.1 nicm 132: }
133:
1.27 nicm 134: struct options_entry *
1.26 nicm 135: options_get_only(struct options *oo, const char *name)
1.1 nicm 136: {
1.27 nicm 137: struct options_entry o;
1.1 nicm 138:
1.26 nicm 139: o.name = name;
140: return (RB_FIND(options_tree, &oo->tree, &o));
1.1 nicm 141: }
142:
1.27 nicm 143: struct options_entry *
1.26 nicm 144: options_get(struct options *oo, const char *name)
1.1 nicm 145: {
1.27 nicm 146: struct options_entry *o;
1.1 nicm 147:
1.26 nicm 148: o = options_get_only(oo, name);
1.1 nicm 149: while (o == NULL) {
150: oo = oo->parent;
151: if (oo == NULL)
152: break;
1.26 nicm 153: o = options_get_only(oo, name);
154: }
155: return (o);
156: }
157:
1.27 nicm 158: struct options_entry *
1.26 nicm 159: options_empty(struct options *oo, const struct options_table_entry *oe)
160: {
1.27 nicm 161: struct options_entry *o;
1.26 nicm 162:
163: o = options_add(oo, oe->name);
164: o->tableentry = oe;
165:
166: return (o);
167: }
168:
1.27 nicm 169: struct options_entry *
1.26 nicm 170: options_default(struct options *oo, const struct options_table_entry *oe)
171: {
1.27 nicm 172: struct options_entry *o;
173: char *cp, *copy, *next;
174: u_int idx = 0;
1.26 nicm 175:
176: o = options_empty(oo, oe);
177:
178: if (oe->type == OPTIONS_TABLE_ARRAY) {
179: copy = cp = xstrdup(oe->default_str);
180: while ((next = strsep(&cp, ",")) != NULL) {
181: options_array_set(o, idx, next);
182: idx++;
183: }
184: free(copy);
185: return (o);
1.1 nicm 186: }
1.26 nicm 187:
188: if (oe->type == OPTIONS_TABLE_STRING)
189: o->string = xstrdup(oe->default_str);
190: else if (oe->type == OPTIONS_TABLE_STYLE) {
191: memcpy(&o->style, &grid_default_cell, sizeof o->style);
192: style_parse(&grid_default_cell, &o->style, oe->default_str);
193: } else
194: o->number = oe->default_num;
195: return (o);
196: }
197:
1.27 nicm 198: static struct options_entry *
1.26 nicm 199: options_add(struct options *oo, const char *name)
200: {
1.27 nicm 201: struct options_entry *o;
1.26 nicm 202:
203: o = options_get_only(oo, name);
204: if (o != NULL)
205: options_remove(o);
206:
207: o = xcalloc(1, sizeof *o);
208: o->owner = oo;
209: o->name = xstrdup(name);
210:
211: RB_INSERT(options_tree, &oo->tree, o);
1.1 nicm 212: return (o);
213: }
214:
1.2 nicm 215: void
1.27 nicm 216: options_remove(struct options_entry *o)
1.26 nicm 217: {
218: struct options *oo = o->owner;
219: u_int i;
220:
221: if (OPTIONS_IS_STRING(o))
222: free((void *)o->string);
223: else if (OPTIONS_IS_ARRAY(o)) {
224: for (i = 0; i < o->arraysize; i++)
225: free((void *)o->array[i]);
226: free(o->array);
227: }
228:
229: RB_REMOVE(options_tree, &oo->tree, o);
230: free(o);
231: }
232:
233: const char *
1.27 nicm 234: options_name(struct options_entry *o)
1.26 nicm 235: {
236: return (o->name);
237: }
238:
239: const struct options_table_entry *
1.27 nicm 240: options_table_entry(struct options_entry *o)
1.26 nicm 241: {
242: return (o->tableentry);
243: }
244:
245: const char *
1.27 nicm 246: options_array_get(struct options_entry *o, u_int idx)
1.26 nicm 247: {
248: if (!OPTIONS_IS_ARRAY(o))
249: return (NULL);
250: if (idx >= o->arraysize)
251: return (NULL);
252: return (o->array[idx]);
253: }
254:
255: int
1.27 nicm 256: options_array_set(struct options_entry *o, u_int idx, const char *value)
1.26 nicm 257: {
258: u_int i;
259:
260: if (!OPTIONS_IS_ARRAY(o))
261: return (-1);
262:
263: if (idx >= OPTIONS_ARRAY_LIMIT)
264: return (-1);
265: if (idx >= o->arraysize) {
266: o->array = xreallocarray(o->array, idx + 1, sizeof *o->array);
267: for (i = o->arraysize; i < idx + 1; i++)
268: o->array[i] = NULL;
269: o->arraysize = idx + 1;
270: }
271: if (o->array[idx] != NULL)
272: free((void *)o->array[idx]);
273: if (value != NULL)
274: o->array[idx] = xstrdup(value);
275: else
276: o->array[idx] = NULL;
277: return (0);
278: }
279:
280: int
1.27 nicm 281: options_array_size(struct options_entry *o, u_int *size)
1.26 nicm 282: {
283: if (!OPTIONS_IS_ARRAY(o))
284: return (-1);
285: if (size != NULL)
286: *size = o->arraysize;
287: return (0);
288: }
289:
290: int
1.27 nicm 291: options_isstring(struct options_entry *o)
1.26 nicm 292: {
293: if (o->tableentry == NULL)
294: return (1);
295: return (OPTIONS_IS_STRING(o) || OPTIONS_IS_ARRAY(o));
296: }
297:
298: const char *
1.27 nicm 299: options_tostring(struct options_entry *o, int idx)
1.26 nicm 300: {
301: static char s[1024];
302: const char *tmp;
303:
304: if (OPTIONS_IS_ARRAY(o)) {
305: if (idx == -1)
306: return (NULL);
307: if ((u_int)idx >= o->arraysize || o->array[idx] == NULL)
308: return ("");
309: return (o->array[idx]);
310: }
311: if (OPTIONS_IS_STYLE(o))
312: return (style_tostring(&o->style));
313: if (OPTIONS_IS_NUMBER(o)) {
314: tmp = NULL;
315: switch (o->tableentry->type) {
316: case OPTIONS_TABLE_NUMBER:
317: xsnprintf(s, sizeof s, "%lld", o->number);
318: break;
319: case OPTIONS_TABLE_KEY:
320: tmp = key_string_lookup_key(o->number);
321: break;
322: case OPTIONS_TABLE_COLOUR:
323: tmp = colour_tostring(o->number);
324: break;
325: case OPTIONS_TABLE_ATTRIBUTES:
326: tmp = attributes_tostring(o->number);
327: break;
328: case OPTIONS_TABLE_FLAG:
329: tmp = (o->number ? "on" : "off");
330: break;
331: case OPTIONS_TABLE_CHOICE:
332: tmp = o->tableentry->choices[o->number];
333: break;
334: case OPTIONS_TABLE_STRING:
335: case OPTIONS_TABLE_STYLE:
336: case OPTIONS_TABLE_ARRAY:
337: break;
338: }
339: if (tmp != NULL)
340: xsnprintf(s, sizeof s, "%s", tmp);
341: return (s);
342: }
343: if (OPTIONS_IS_STRING(o))
344: return (o->string);
345: return (NULL);
346: }
347:
348: char *
349: options_parse(const char *name, int *idx)
1.1 nicm 350: {
1.26 nicm 351: char *copy, *cp, *end;
1.1 nicm 352:
1.26 nicm 353: if (*name == '\0')
354: return (NULL);
355: copy = xstrdup(name);
356: if ((cp = strchr(copy, '[')) == NULL) {
357: *idx = -1;
358: return (copy);
359: }
360: end = strchr(cp + 1, ']');
361: if (end == NULL || end[1] != '\0' || !isdigit((u_char)end[-1])) {
362: free(copy);
363: return (NULL);
364: }
365: if (sscanf(cp, "[%d]", idx) != 1 || *idx < 0) {
366: free(copy);
367: return (NULL);
368: }
369: *cp = '\0';
370: return (copy);
1.1 nicm 371: }
372:
1.27 nicm 373: struct options_entry *
1.26 nicm 374: options_parse_get(struct options *oo, const char *s, int *idx, int only)
1.1 nicm 375: {
1.27 nicm 376: struct options_entry *o;
377: char *name;
1.1 nicm 378:
1.26 nicm 379: name = options_parse(s, idx);
380: if (name == NULL)
381: return (NULL);
382: if (only)
383: o = options_get_only(oo, name);
384: else
385: o = options_get(oo, name);
386: free(name);
387: if (o != NULL) {
388: if (OPTIONS_IS_ARRAY(o) && *idx == -1)
389: return (NULL);
390: if (!OPTIONS_IS_ARRAY(o) && *idx != -1)
391: return (NULL);
392: }
393: return (o);
394: }
1.1 nicm 395:
1.26 nicm 396: char *
397: options_match(const char *s, int *idx, int* ambiguous)
398: {
399: const struct options_table_entry *oe, *found;
400: char *name;
401: size_t namelen;
402:
403: name = options_parse(s, idx);
404: namelen = strlen(name);
1.29 ! nicm 405:
! 406: *idx = -1;
! 407: if (*name == '@') {
! 408: *ambiguous = 0;
! 409: return (xstrdup(name));
! 410: }
1.26 nicm 411:
412: found = NULL;
413: for (oe = options_table; oe->name != NULL; oe++) {
414: if (strcmp(oe->name, name) == 0) {
415: found = oe;
416: break;
417: }
418: if (strncmp(oe->name, name, namelen) == 0) {
419: if (found != NULL) {
420: *ambiguous = 1;
421: free(name);
422: return (NULL);
423: }
424: found = oe;
425: }
426: }
427: free(name);
428: if (found == NULL) {
429: *ambiguous = 0;
430: return (NULL);
1.22 nicm 431: }
1.26 nicm 432: return (xstrdup(found->name));
433: }
1.22 nicm 434:
1.27 nicm 435: struct options_entry *
1.26 nicm 436: options_match_get(struct options *oo, const char *s, int *idx, int only,
437: int* ambiguous)
438: {
1.27 nicm 439: char *name;
440: struct options_entry *o;
1.21 nicm 441:
1.26 nicm 442: name = options_match(s, idx, ambiguous);
443: if (name == NULL)
444: return (NULL);
445: *ambiguous = 0;
446: if (only)
447: o = options_get_only(oo, name);
448: else
449: o = options_get(oo, name);
450: free(name);
451: if (o != NULL) {
452: if (OPTIONS_IS_ARRAY(o) && *idx == -1)
453: return (NULL);
454: if (!OPTIONS_IS_ARRAY(o) && *idx != -1)
455: return (NULL);
456: }
1.4 nicm 457: return (o);
1.1 nicm 458: }
459:
1.26 nicm 460:
1.23 nicm 461: const char *
1.1 nicm 462: options_get_string(struct options *oo, const char *name)
463: {
1.27 nicm 464: struct options_entry *o;
1.26 nicm 465:
466: o = options_get(oo, name);
467: if (o == NULL)
468: fatalx("missing option %s", name);
469: if (!OPTIONS_IS_STRING(o))
470: fatalx("option %s is not a string", name);
471: return (o->string);
472: }
473:
474: long long
475: options_get_number(struct options *oo, const char *name)
476: {
1.27 nicm 477: struct options_entry *o;
1.1 nicm 478:
1.26 nicm 479: o = options_get(oo, name);
480: if (o == NULL)
1.15 nicm 481: fatalx("missing option %s", name);
1.26 nicm 482: if (!OPTIONS_IS_NUMBER(o))
483: fatalx("option %s is not a number", name);
484: return (o->number);
1.1 nicm 485: }
486:
1.26 nicm 487: const struct grid_cell *
488: options_get_style(struct options *oo, const char *name)
489: {
1.27 nicm 490: struct options_entry *o;
1.26 nicm 491:
492: o = options_get(oo, name);
493: if (o == NULL)
494: fatalx("missing option %s", name);
495: if (!OPTIONS_IS_STYLE(o))
496: fatalx("option %s is not a style", name);
497: return (&o->style);
498: }
499:
1.27 nicm 500: struct options_entry *
1.26 nicm 501: options_set_string(struct options *oo, const char *name, int append,
502: const char *fmt, ...)
1.1 nicm 503: {
1.27 nicm 504: struct options_entry *o;
505: va_list ap;
506: char *s, *value;
1.1 nicm 507:
1.26 nicm 508: va_start(ap, fmt);
509: xvasprintf(&s, fmt, ap);
510: va_end(ap);
511:
512: o = options_get_only(oo, name);
513: if (o != NULL && append && OPTIONS_IS_STRING(o)) {
514: xasprintf(&value, "%s%s", o->string, s);
515: free(s);
516: } else
517: value = s;
518: if (o == NULL && *name == '@')
519: o = options_add(oo, name);
520: else if (o == NULL) {
521: o = options_default(oo, options_parent_table_entry(oo, name));
522: if (o == NULL)
523: return (NULL);
524: }
1.21 nicm 525:
1.26 nicm 526: if (!OPTIONS_IS_STRING(o))
527: fatalx("option %s is not a string", name);
528: free(o->string);
529: o->string = value;
1.4 nicm 530: return (o);
1.1 nicm 531: }
532:
1.27 nicm 533: struct options_entry *
1.26 nicm 534: options_set_number(struct options *oo, const char *name, long long value)
1.1 nicm 535: {
1.27 nicm 536: struct options_entry *o;
1.26 nicm 537:
538: if (*name == '@')
539: fatalx("user option %s must be a string", name);
1.1 nicm 540:
1.26 nicm 541: o = options_get_only(oo, name);
542: if (o == NULL) {
543: o = options_default(oo, options_parent_table_entry(oo, name));
544: if (o == NULL)
545: return (NULL);
546: }
547:
548: if (!OPTIONS_IS_NUMBER(o))
549: fatalx("option %s is not a number", name);
550: o->number = value;
551: return (o);
1.10 nicm 552: }
553:
1.27 nicm 554: struct options_entry *
1.22 nicm 555: options_set_style(struct options *oo, const char *name, int append,
556: const char *value)
1.10 nicm 557: {
1.27 nicm 558: struct options_entry *o;
1.26 nicm 559: struct grid_cell gc;
560:
561: if (*name == '@')
562: fatalx("user option %s must be a string", name);
1.10 nicm 563:
1.26 nicm 564: o = options_get_only(oo, name);
565: if (o != NULL && append && OPTIONS_IS_STYLE(o))
566: memcpy(&gc, &o->style, sizeof gc);
1.12 nicm 567: else
1.26 nicm 568: memcpy(&gc, &grid_default_cell, sizeof gc);
569: if (style_parse(&grid_default_cell, &gc, value) == -1)
570: return (NULL);
571: if (o == NULL) {
572: o = options_default(oo, options_parent_table_entry(oo, name));
573: if (o == NULL)
574: return (NULL);
575: }
576:
577: if (!OPTIONS_IS_STYLE(o))
578: fatalx("option %s is not a style", name);
579: memcpy(&o->style, &gc, sizeof o->style);
580: return (o);
581: }
1.12 nicm 582:
1.26 nicm 583: enum options_table_scope
584: options_scope_from_flags(struct args *args, int window,
585: struct cmd_find_state *fs, struct options **oo, char **cause)
586: {
587: struct session *s = fs->s;
588: struct winlink *wl = fs->wl;
589: const char *target= args_get(args, 't');
590:
591: if (args_has(args, 's')) {
592: *oo = global_options;
593: return (OPTIONS_TABLE_SERVER);
594: }
1.12 nicm 595:
1.26 nicm 596: if (window || args_has(args, 'w')) {
597: if (args_has(args, 'g')) {
598: *oo = global_w_options;
599: return (OPTIONS_TABLE_WINDOW);
600: }
601: if (wl == NULL) {
602: if (target != NULL)
603: xasprintf(cause, "no such window: %s", target);
604: else
605: xasprintf(cause, "no current window");
606: return (OPTIONS_TABLE_NONE);
607: }
608: *oo = wl->window->options;
609: return (OPTIONS_TABLE_WINDOW);
610: } else {
611: if (args_has(args, 'g')) {
612: *oo = global_s_options;
613: return (OPTIONS_TABLE_SESSION);
614: }
615: if (s == NULL) {
616: if (target != NULL)
617: xasprintf(cause, "no such session: %s", target);
618: else
619: xasprintf(cause, "no current session");
620: return (OPTIONS_TABLE_NONE);
621: }
622: *oo = s->options;
623: return (OPTIONS_TABLE_SESSION);
624: }
625: }
1.21 nicm 626:
1.26 nicm 627: void
1.27 nicm 628: options_style_update_new(struct options *oo, struct options_entry *o)
1.26 nicm 629: {
1.27 nicm 630: const char *newname = o->tableentry->style;
631: struct options_entry *new;
1.26 nicm 632:
633: if (newname == NULL)
634: return;
635: new = options_get_only(oo, newname);
636: if (new == NULL)
637: new = options_set_style(oo, newname, 0, "default");
638:
639: if (strstr(o->name, "-bg") != NULL)
640: new->style.bg = o->number;
641: else if (strstr(o->name, "-fg") != NULL)
642: new->style.fg = o->number;
643: else if (strstr(o->name, "-attr") != NULL)
644: new->style.attr = o->number;
1.10 nicm 645: }
646:
1.26 nicm 647: void
1.27 nicm 648: options_style_update_old(struct options *oo, struct options_entry *o)
1.10 nicm 649: {
1.26 nicm 650: char newname[128];
651: int size;
1.10 nicm 652:
1.26 nicm 653: size = strrchr(o->name, '-') - o->name;
654:
655: xsnprintf(newname, sizeof newname, "%.*s-bg", size, o->name);
1.28 nicm 656: if (options_get(oo, newname) != NULL)
657: options_set_number(oo, newname, o->style.bg);
1.26 nicm 658:
659: xsnprintf(newname, sizeof newname, "%.*s-fg", size, o->name);
1.28 nicm 660: if (options_get(oo, newname) != NULL)
661: options_set_number(oo, newname, o->style.fg);
1.26 nicm 662:
663: xsnprintf(newname, sizeof newname, "%.*s-attr", size, o->name);
1.28 nicm 664: if (options_get(oo, newname) != NULL)
665: options_set_number(oo, newname, o->style.attr);
1.1 nicm 666: }