Coverage Report

Created: 2026-06-10 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/style.c
Line
Count
Source
1
/* $OpenBSD$ */
2
3
/*
4
 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5
 * Copyright (c) 2014 Tiago Cunha <tcunha@users.sourceforge.net>
6
 *
7
 * Permission to use, copy, modify, and distribute this software for any
8
 * purpose with or without fee is hereby granted, provided that the above
9
 * copyright notice and this permission notice appear in all copies.
10
 *
11
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
16
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
17
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
 */
19
20
#include <sys/types.h>
21
22
#include <ctype.h>
23
#include <stdlib.h>
24
#include <string.h>
25
26
#include "tmux.h"
27
28
/* Mask for bits not included in style. */
29
#define STYLE_ATTR_MASK (~0)
30
31
/* Default style. */
32
static struct style style_default = {
33
  { { { ' ' }, 0, 1, 1 }, 0, 0, 8, 8, 0, 0 },
34
  0,
35
36
  8,
37
  STYLE_ALIGN_DEFAULT,
38
  STYLE_LIST_OFF,
39
40
  STYLE_RANGE_NONE, 0, "",
41
42
  STYLE_WIDTH_DEFAULT, 0, STYLE_PAD_DEFAULT,
43
44
  STYLE_DEFAULT_BASE
45
};
46
47
/* Set range string. */
48
static void
49
style_set_range_string(struct style *sy, const char *s)
50
530
{
51
530
  strlcpy(sy->range_string, s, sizeof sy->range_string);
52
530
}
53
54
/*
55
 * Parse an embedded style of the form "fg=colour,bg=colour,bright,...".  Note
56
 * that this adds onto the given style, so it must have been initialized
57
 * already.
58
 */
59
int
60
style_parse(struct style *sy, const struct grid_cell *base, const char *in)
61
3.55k
{
62
3.55k
  struct style  saved;
63
3.55k
  const char  delimiters[] = " ,\n", *errstr;
64
3.55k
  char    tmp[256], *found;
65
3.55k
  int   value;
66
3.55k
  size_t    end;
67
3.55k
  u_int   n;
68
69
3.55k
  if (*in == '\0')
70
1
    return (0);
71
3.55k
  style_copy(&saved, sy);
72
73
3.55k
  log_debug("%s: %s", __func__, in);
74
9.36k
  do {
75
9.97k
    while (*in != '\0' && strchr(delimiters, *in) != NULL)
76
610
      in++;
77
9.36k
    if (*in == '\0')
78
8
      break;
79
80
9.35k
    end = strcspn(in, delimiters);
81
9.35k
    if (end > (sizeof tmp) - 1)
82
2
      goto error;
83
9.35k
    memcpy(tmp, in, end);
84
9.35k
    tmp[end] = '\0';
85
86
9.35k
    log_debug("%s: %s", __func__, tmp);
87
9.35k
    if (strcasecmp(tmp, "default") == 0) {
88
124
      sy->gc.fg = base->fg;
89
124
      sy->gc.bg = base->bg;
90
124
      sy->gc.us = base->us;
91
124
      sy->gc.attr = base->attr;
92
124
      sy->gc.flags = base->flags;
93
9.23k
    } else if (strcasecmp(tmp, "ignore") == 0)
94
70
      sy->ignore = 1;
95
9.16k
    else if (strcasecmp(tmp, "noignore") == 0)
96
75
      sy->ignore = 0;
97
9.08k
    else if (strcasecmp(tmp, "push-default") == 0)
98
87
      sy->default_type = STYLE_DEFAULT_PUSH;
99
9.00k
    else if (strcasecmp(tmp, "pop-default") == 0)
100
73
      sy->default_type = STYLE_DEFAULT_POP;
101
8.92k
    else if (strcasecmp(tmp, "set-default") == 0)
102
85
      sy->default_type = STYLE_DEFAULT_SET;
103
8.84k
    else if (strcasecmp(tmp, "nolist") == 0)
104
93
      sy->list = STYLE_LIST_OFF;
105
8.74k
    else if (strncasecmp(tmp, "list=", 5) == 0) {
106
418
      if (strcasecmp(tmp + 5, "on") == 0)
107
70
        sy->list = STYLE_LIST_ON;
108
348
      else if (strcasecmp(tmp + 5, "focus") == 0)
109
70
        sy->list = STYLE_LIST_FOCUS;
110
278
      else if (strcasecmp(tmp + 5, "left-marker") == 0)
111
39
        sy->list = STYLE_LIST_LEFT_MARKER;
112
239
      else if (strcasecmp(tmp + 5, "right-marker") == 0)
113
42
        sy->list = STYLE_LIST_RIGHT_MARKER;
114
197
      else
115
197
        goto error;
116
8.33k
    } else if (strcasecmp(tmp, "norange") == 0) {
117
71
      sy->range_type = style_default.range_type;
118
71
      sy->range_argument = style_default.range_type;
119
71
      strlcpy(sy->range_string, style_default.range_string,
120
71
          sizeof sy->range_string);
121
8.26k
    } else if (end > 6 && strncasecmp(tmp, "range=", 6) == 0) {
122
1.11k
      found = strchr(tmp + 6, '|');
123
1.11k
      if (found != NULL) {
124
493
        *found++ = '\0';
125
493
        if (*found == '\0')
126
1
          goto error;
127
493
      }
128
1.11k
      if (strcasecmp(tmp + 6, "left") == 0) {
129
102
        if (found != NULL)
130
1
          goto error;
131
101
        sy->range_type = STYLE_RANGE_LEFT;
132
101
        sy->range_argument = 0;
133
101
        style_set_range_string(sy, "");
134
1.01k
      } else if (strcasecmp(tmp + 6, "right") == 0) {
135
112
        if (found != NULL)
136
1
          goto error;
137
111
        sy->range_type = STYLE_RANGE_RIGHT;
138
111
        sy->range_argument = 0;
139
111
        style_set_range_string(sy, "");
140
899
      } else if (strcasecmp(tmp + 6, "control") == 0) {
141
0
        if (found == NULL)
142
0
          goto error;
143
0
        n = strtonum(found, 0, 9, &errstr);
144
0
        if (errstr != NULL)
145
0
          goto error;
146
0
        sy->range_type = STYLE_RANGE_CONTROL;
147
0
        sy->range_argument = n;
148
0
        style_set_range_string(sy, "");
149
899
      } else if (strcasecmp(tmp + 6, "pane") == 0) {
150
94
        if (found == NULL)
151
1
          goto error;
152
93
        if (*found != '%' || found[1] == '\0')
153
12
          goto error;
154
81
        n = strtonum(found + 1, 0, UINT_MAX, &errstr);
155
81
        if (errstr != NULL)
156
9
          goto error;
157
72
        sy->range_type = STYLE_RANGE_PANE;
158
72
        sy->range_argument = n;
159
72
        style_set_range_string(sy, "");
160
805
      } else if (strcasecmp(tmp + 6, "window") == 0) {
161
84
        if (found == NULL)
162
1
          goto error;
163
83
        n = strtonum(found, 0, UINT_MAX, &errstr);
164
83
        if (errstr != NULL)
165
1
          goto error;
166
82
        sy->range_type = STYLE_RANGE_WINDOW;
167
82
        sy->range_argument = n;
168
82
        style_set_range_string(sy, "");
169
721
      } else if (strcasecmp(tmp + 6, "session") == 0) {
170
60
        if (found == NULL)
171
1
          goto error;
172
59
        if (*found != '$' || found[1] == '\0')
173
14
          goto error;
174
45
        n = strtonum(found + 1, 0, UINT_MAX, &errstr);
175
45
        if (errstr != NULL)
176
7
          goto error;
177
38
        sy->range_type = STYLE_RANGE_SESSION;
178
38
        sy->range_argument = n;
179
38
        style_set_range_string(sy, "");
180
661
      } else if (strcasecmp(tmp + 6, "user") == 0) {
181
127
        if (found == NULL)
182
1
          goto error;
183
126
        sy->range_type = STYLE_RANGE_USER;
184
126
        sy->range_argument = 0;
185
126
        style_set_range_string(sy, found);
186
126
      }
187
7.14k
    } else if (strcasecmp(tmp, "noalign") == 0)
188
104
      sy->align = style_default.align;
189
7.04k
    else if (end > 6 && strncasecmp(tmp, "align=", 6) == 0) {
190
489
      if (strcasecmp(tmp + 6, "left") == 0)
191
68
        sy->align = STYLE_ALIGN_LEFT;
192
421
      else if (strcasecmp(tmp + 6, "centre") == 0)
193
88
        sy->align = STYLE_ALIGN_CENTRE;
194
333
      else if (strcasecmp(tmp + 6, "right") == 0)
195
68
        sy->align = STYLE_ALIGN_RIGHT;
196
265
      else if (strcasecmp(tmp + 6, "absolute-centre") == 0)
197
44
        sy->align = STYLE_ALIGN_ABSOLUTE_CENTRE;
198
221
      else
199
221
        goto error;
200
6.55k
    } else if (end > 5 && strncasecmp(tmp, "fill=", 5) == 0) {
201
101
      if ((value = colour_fromstring(tmp + 5)) == -1)
202
9
        goto error;
203
92
      sy->fill = value;
204
6.45k
    } else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) {
205
2.95k
      if ((value = colour_fromstring(tmp + 3)) == -1)
206
664
        goto error;
207
2.28k
      if (*in == 'f' || *in == 'F') {
208
1.06k
        if (value != 8)
209
982
          sy->gc.fg = value;
210
78
        else
211
78
          sy->gc.fg = base->fg;
212
1.22k
      } else if (*in == 'b' || *in == 'B') {
213
1.17k
        if (value != 8)
214
1.10k
          sy->gc.bg = value;
215
67
        else
216
67
          sy->gc.bg = base->bg;
217
1.17k
      } else
218
54
        goto error;
219
3.50k
    } else if (end > 3 && strncasecmp(tmp, "us=", 3) == 0) {
220
1.21k
      if ((value = colour_fromstring(tmp + 3)) == -1)
221
234
        goto error;
222
979
      if (value != 8)
223
897
        sy->gc.us = value;
224
82
      else
225
82
        sy->gc.us = base->us;
226
2.28k
    } else if (strcasecmp(tmp, "none") == 0)
227
71
      sy->gc.attr = 0;
228
2.21k
    else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) {
229
470
      if (strcmp(tmp + 2, "attr") == 0)
230
72
        sy->gc.attr |= GRID_ATTR_NOATTR;
231
398
      else {
232
398
        value = attributes_fromstring(tmp + 2);
233
398
        if (value == -1)
234
182
          goto error;
235
216
        sy->gc.attr &= ~value;
236
216
      }
237
1.74k
    } else if (end > 6 && strncasecmp(tmp, "width=", 6) == 0) {
238
254
      if (end > 7 && tmp[end - 1] == '%') {
239
74
        tmp[end - 1] = '\0';
240
74
        n = strtonum(tmp + 6, 0, 100, &errstr);
241
74
        if (errstr != NULL)
242
8
          goto error;
243
66
        sy->width = (int)n;
244
66
        sy->width_percentage = 1;
245
180
      } else {
246
180
        n = strtonum(tmp + 6, 0, UINT_MAX, &errstr);
247
180
        if (errstr != NULL)
248
37
          goto error;
249
143
        sy->width = (int)n;
250
143
        sy->width_percentage = 0;
251
143
      }
252
1.49k
    } else if (end > 4 && strncasecmp(tmp, "pad=", 4) == 0) {
253
211
      n = strtonum(tmp + 4, 0, UINT_MAX, &errstr);
254
211
      if (errstr != NULL)
255
112
        goto error;
256
99
      sy->pad = (int)n;
257
1.28k
    } else {
258
1.28k
      if ((value = attributes_fromstring(tmp)) == -1)
259
863
        goto error;
260
420
      sy->gc.attr |= value;
261
420
    }
262
263
6.72k
    in += end + strspn(in + end, delimiters);
264
6.72k
  } while (*in != '\0');
265
266
925
  return (0);
267
268
2.63k
error:
269
2.63k
  style_copy(sy, &saved);
270
2.63k
  return (-1);
271
3.55k
}
272
273
/* Convert style to a string. */
274
const char *
275
style_tostring(struct style *sy)
276
0
{
277
0
  struct grid_cell  *gc = &sy->gc;
278
0
  int      off = 0;
279
0
  const char    *comma = "", *tmp = "";
280
0
  static char    s[256];
281
0
  char       b[21];
282
283
0
  *s = '\0';
284
285
0
  if (sy->list != STYLE_LIST_OFF) {
286
0
    if (sy->list == STYLE_LIST_ON)
287
0
      tmp = "on";
288
0
    else if (sy->list == STYLE_LIST_FOCUS)
289
0
      tmp = "focus";
290
0
    else if (sy->list == STYLE_LIST_LEFT_MARKER)
291
0
      tmp = "left-marker";
292
0
    else if (sy->list == STYLE_LIST_RIGHT_MARKER)
293
0
      tmp = "right-marker";
294
0
    off += xsnprintf(s + off, sizeof s - off, "%slist=%s", comma,
295
0
        tmp);
296
0
    comma = ",";
297
0
  }
298
0
  if (sy->range_type != STYLE_RANGE_NONE) {
299
0
    if (sy->range_type == STYLE_RANGE_LEFT)
300
0
      tmp = "left";
301
0
    else if (sy->range_type == STYLE_RANGE_RIGHT)
302
0
      tmp = "right";
303
0
    else if (sy->range_type == STYLE_RANGE_PANE) {
304
0
      snprintf(b, sizeof b, "pane|%%%u", sy->range_argument);
305
0
      tmp = b;
306
0
    } else if (sy->range_type == STYLE_RANGE_WINDOW) {
307
0
      snprintf(b, sizeof b, "window|%u", sy->range_argument);
308
0
      tmp = b;
309
0
    } else if (sy->range_type == STYLE_RANGE_SESSION) {
310
0
      snprintf(b, sizeof b, "session|$%u",
311
0
          sy->range_argument);
312
0
      tmp = b;
313
0
    } else if (sy->range_type == STYLE_RANGE_USER) {
314
0
      snprintf(b, sizeof b, "user|%s", sy->range_string);
315
0
      tmp = b;
316
0
    }
317
0
    off += xsnprintf(s + off, sizeof s - off, "%srange=%s", comma,
318
0
        tmp);
319
0
    comma = ",";
320
0
  }
321
0
  if (sy->align != STYLE_ALIGN_DEFAULT) {
322
0
    if (sy->align == STYLE_ALIGN_LEFT)
323
0
      tmp = "left";
324
0
    else if (sy->align == STYLE_ALIGN_CENTRE)
325
0
      tmp = "centre";
326
0
    else if (sy->align == STYLE_ALIGN_RIGHT)
327
0
      tmp = "right";
328
0
    else if (sy->align == STYLE_ALIGN_ABSOLUTE_CENTRE)
329
0
      tmp = "absolute-centre";
330
0
    off += xsnprintf(s + off, sizeof s - off, "%salign=%s", comma,
331
0
        tmp);
332
0
    comma = ",";
333
0
  }
334
0
  if (sy->default_type != STYLE_DEFAULT_BASE) {
335
0
    if (sy->default_type == STYLE_DEFAULT_PUSH)
336
0
      tmp = "push-default";
337
0
    else if (sy->default_type == STYLE_DEFAULT_POP)
338
0
      tmp = "pop-default";
339
0
    else if (sy->default_type == STYLE_DEFAULT_SET)
340
0
      tmp = "set-default";
341
0
    off += xsnprintf(s + off, sizeof s - off, "%s%s", comma, tmp);
342
0
    comma = ",";
343
0
  }
344
0
  if (sy->fill != 8) {
345
0
    off += xsnprintf(s + off, sizeof s - off, "%sfill=%s", comma,
346
0
        colour_tostring(sy->fill));
347
0
    comma = ",";
348
0
  }
349
0
  if (gc->fg != 8) {
350
0
    off += xsnprintf(s + off, sizeof s - off, "%sfg=%s", comma,
351
0
        colour_tostring(gc->fg));
352
0
    comma = ",";
353
0
  }
354
0
  if (gc->bg != 8) {
355
0
    off += xsnprintf(s + off, sizeof s - off, "%sbg=%s", comma,
356
0
        colour_tostring(gc->bg));
357
0
    comma = ",";
358
0
  }
359
0
  if (gc->us != 8) {
360
0
    off += xsnprintf(s + off, sizeof s - off, "%sus=%s", comma,
361
0
        colour_tostring(gc->us));
362
0
    comma = ",";
363
0
  }
364
0
  if (gc->attr != 0) {
365
0
    off += xsnprintf(s + off, sizeof s - off, "%s%s", comma,
366
0
        attributes_tostring(gc->attr));
367
0
    comma = ",";
368
0
  }
369
0
  if (sy->width >= 0) {
370
0
    if (sy->width_percentage)
371
0
      off += xsnprintf(s + off, sizeof s - off,
372
0
          "%swidth=%u%%", comma, sy->width);
373
0
    else
374
0
      off += xsnprintf(s + off, sizeof s - off,
375
0
          "%swidth=%u", comma, sy->width);
376
0
    comma = ",";
377
0
  }
378
0
  if (sy->pad >= 0) {
379
0
    xsnprintf(s + off, sizeof s - off, "%spad=%u", comma,
380
0
        sy->pad);
381
0
    comma = ",";
382
0
  }
383
0
  if (*s == '\0')
384
0
    return ("default");
385
0
  return (s);
386
0
}
387
388
/* Apply a style on top of the given style. */
389
void
390
style_add(struct grid_cell *gc, struct options *oo, const char *name,
391
    struct format_tree *ft)
392
7.44k
{
393
7.44k
  struct style    *sy;
394
7.44k
  struct format_tree  *ft0 = NULL;
395
396
7.44k
  if (ft == NULL)
397
0
    ft = ft0 = format_create(NULL, NULL, 0, FORMAT_NOJOBS);
398
399
7.44k
  sy = options_string_to_style(oo, name, ft);
400
7.44k
  if (sy == NULL)
401
0
    sy = &style_default;
402
7.44k
  if (sy->gc.fg != 8)
403
0
    gc->fg = sy->gc.fg;
404
7.44k
  if (sy->gc.bg != 8)
405
0
    gc->bg = sy->gc.bg;
406
7.44k
  if (sy->gc.us != 8)
407
0
    gc->us = sy->gc.us;
408
7.44k
  gc->attr |= sy->gc.attr;
409
410
7.44k
  if (ft0 != NULL)
411
0
    format_free(ft0);
412
7.44k
}
413
414
/* Apply a style on top of the default style. */
415
void
416
style_apply(struct grid_cell *gc, struct options *oo, const char *name,
417
    struct format_tree *ft)
418
0
{
419
0
  memcpy(gc, &grid_default_cell, sizeof *gc);
420
0
  style_add(gc, oo, name, ft);
421
0
}
422
423
/* Initialize style from cell. */
424
void
425
style_set(struct style *sy, const struct grid_cell *gc)
426
3.55k
{
427
3.55k
  memcpy(sy, &style_default, sizeof *sy);
428
3.55k
  memcpy(&sy->gc, gc, sizeof sy->gc);
429
3.55k
}
430
431
/* Copy style. */
432
void
433
style_copy(struct style *dst, struct style *src)
434
17.3k
{
435
17.3k
  memcpy(dst, src, sizeof *dst);
436
17.3k
}
437
438
/* Set scrollbar style from an option. */
439
void
440
style_set_scrollbar_style_from_option(struct style *sb_style,
441
    struct options *oo)
442
11.1k
{
443
11.1k
  struct style  *sy;
444
445
11.1k
  sy = options_string_to_style(oo, "pane-scrollbars-style", NULL);
446
11.1k
  if (sy == NULL) {
447
0
    style_set(sb_style, &grid_default_cell);
448
0
    sb_style->width = PANE_SCROLLBARS_DEFAULT_WIDTH;
449
0
    sb_style->pad = PANE_SCROLLBARS_DEFAULT_PADDING;
450
0
    utf8_set(&sb_style->gc.data, PANE_SCROLLBARS_CHARACTER);
451
11.1k
  } else {
452
11.1k
    style_copy(sb_style, sy);
453
11.1k
    if (sb_style->width < 1)
454
0
      sb_style->width = PANE_SCROLLBARS_DEFAULT_WIDTH;
455
11.1k
    if (sb_style->pad < 0)
456
0
      sb_style->pad = PANE_SCROLLBARS_DEFAULT_PADDING;
457
11.1k
    utf8_set(&sb_style->gc.data, PANE_SCROLLBARS_CHARACTER);
458
11.1k
  }
459
11.1k
}
460
461
/* Initialize style ranges. */
462
void
463
style_ranges_init(struct style_ranges *srs)
464
11.1k
{
465
11.1k
  TAILQ_INIT(srs);
466
11.1k
}
467
468
/* Free style ranges. */
469
void
470
style_ranges_free(struct style_ranges *srs)
471
11.1k
{
472
11.1k
  struct style_range  *sr, *sr1;
473
474
11.1k
  TAILQ_FOREACH_SAFE(sr, srs, entry, sr1) {
475
0
    TAILQ_REMOVE(srs, sr, entry);
476
0
    free(sr);
477
0
  }
478
11.1k
}
479
480
/* Get range for position from style ranges. */
481
struct style_range *
482
style_ranges_get_range(struct style_ranges *srs, u_int x)
483
0
{
484
0
  struct style_range  *sr;
485
486
0
  if (srs == NULL)
487
0
    return (NULL);
488
0
  TAILQ_FOREACH(sr, srs, entry) {
489
0
    if (x >= sr->start && x < sr->end)
490
0
      return (sr);
491
0
  }
492
0
  return (NULL);
493
0
}