Coverage Report

Created: 2024-09-11 06:44

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