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