Coverage Report

Created: 2026-06-12 06:50

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
0
{
51
0
  strlcpy(sy->range_string, s, sizeof sy->range_string);
52
0
}
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
{
62
3
  struct style  saved;
63
3
  const char  delimiters[] = " ,\n", *errstr;
64
3
  char    tmp[256], *found;
65
3
  int   value;
66
3
  size_t    end;
67
3
  u_int   n;
68
69
3
  if (*in == '\0')
70
0
    return (0);
71
3
  style_copy(&saved, sy);
72
73
3
  log_debug("%s: %s", __func__, in);
74
6
  do {
75
6
    while (*in != '\0' && strchr(delimiters, *in) != NULL)
76
0
      in++;
77
6
    if (*in == '\0')
78
0
      break;
79
80
6
    end = strcspn(in, delimiters);
81
6
    if (end > (sizeof tmp) - 1)
82
0
      goto error;
83
6
    memcpy(tmp, in, end);
84
6
    tmp[end] = '\0';
85
86
6
    log_debug("%s: %s", __func__, tmp);
87
6
    if (strcasecmp(tmp, "default") == 0) {
88
2
      sy->gc.fg = base->fg;
89
2
      sy->gc.bg = base->bg;
90
2
      sy->gc.us = base->us;
91
2
      sy->gc.attr = base->attr;
92
2
      sy->gc.flags = base->flags;
93
4
    } else if (strcasecmp(tmp, "ignore") == 0)
94
0
      sy->ignore = 1;
95
4
    else if (strcasecmp(tmp, "noignore") == 0)
96
0
      sy->ignore = 0;
97
4
    else if (strcasecmp(tmp, "push-default") == 0)
98
0
      sy->default_type = STYLE_DEFAULT_PUSH;
99
4
    else if (strcasecmp(tmp, "pop-default") == 0)
100
0
      sy->default_type = STYLE_DEFAULT_POP;
101
4
    else if (strcasecmp(tmp, "set-default") == 0)
102
0
      sy->default_type = STYLE_DEFAULT_SET;
103
4
    else if (strcasecmp(tmp, "nolist") == 0)
104
0
      sy->list = STYLE_LIST_OFF;
105
4
    else if (strncasecmp(tmp, "list=", 5) == 0) {
106
0
      if (strcasecmp(tmp + 5, "on") == 0)
107
0
        sy->list = STYLE_LIST_ON;
108
0
      else if (strcasecmp(tmp + 5, "focus") == 0)
109
0
        sy->list = STYLE_LIST_FOCUS;
110
0
      else if (strcasecmp(tmp + 5, "left-marker") == 0)
111
0
        sy->list = STYLE_LIST_LEFT_MARKER;
112
0
      else if (strcasecmp(tmp + 5, "right-marker") == 0)
113
0
        sy->list = STYLE_LIST_RIGHT_MARKER;
114
0
      else
115
0
        goto error;
116
4
    } else if (strcasecmp(tmp, "norange") == 0) {
117
0
      sy->range_type = style_default.range_type;
118
0
      sy->range_argument = style_default.range_type;
119
0
      strlcpy(sy->range_string, style_default.range_string,
120
0
          sizeof sy->range_string);
121
4
    } else if (end > 6 && strncasecmp(tmp, "range=", 6) == 0) {
122
0
      found = strchr(tmp + 6, '|');
123
0
      if (found != NULL) {
124
0
        *found++ = '\0';
125
0
        if (*found == '\0')
126
0
          goto error;
127
0
      }
128
0
      if (strcasecmp(tmp + 6, "left") == 0) {
129
0
        if (found != NULL)
130
0
          goto error;
131
0
        sy->range_type = STYLE_RANGE_LEFT;
132
0
        sy->range_argument = 0;
133
0
        style_set_range_string(sy, "");
134
0
      } else if (strcasecmp(tmp + 6, "right") == 0) {
135
0
        if (found != NULL)
136
0
          goto error;
137
0
        sy->range_type = STYLE_RANGE_RIGHT;
138
0
        sy->range_argument = 0;
139
0
        style_set_range_string(sy, "");
140
0
      } 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
0
      } else if (strcasecmp(tmp + 6, "pane") == 0) {
150
0
        if (found == NULL)
151
0
          goto error;
152
0
        if (*found != '%' || found[1] == '\0')
153
0
          goto error;
154
0
        n = strtonum(found + 1, 0, UINT_MAX, &errstr);
155
0
        if (errstr != NULL)
156
0
          goto error;
157
0
        sy->range_type = STYLE_RANGE_PANE;
158
0
        sy->range_argument = n;
159
0
        style_set_range_string(sy, "");
160
0
      } else if (strcasecmp(tmp + 6, "window") == 0) {
161
0
        if (found == NULL)
162
0
          goto error;
163
0
        n = strtonum(found, 0, UINT_MAX, &errstr);
164
0
        if (errstr != NULL)
165
0
          goto error;
166
0
        sy->range_type = STYLE_RANGE_WINDOW;
167
0
        sy->range_argument = n;
168
0
        style_set_range_string(sy, "");
169
0
      } else if (strcasecmp(tmp + 6, "session") == 0) {
170
0
        if (found == NULL)
171
0
          goto error;
172
0
        if (*found != '$' || found[1] == '\0')
173
0
          goto error;
174
0
        n = strtonum(found + 1, 0, UINT_MAX, &errstr);
175
0
        if (errstr != NULL)
176
0
          goto error;
177
0
        sy->range_type = STYLE_RANGE_SESSION;
178
0
        sy->range_argument = n;
179
0
        style_set_range_string(sy, "");
180
0
      } else if (strcasecmp(tmp + 6, "user") == 0) {
181
0
        if (found == NULL)
182
0
          goto error;
183
0
        sy->range_type = STYLE_RANGE_USER;
184
0
        sy->range_argument = 0;
185
0
        style_set_range_string(sy, found);
186
0
      }
187
4
    } else if (strcasecmp(tmp, "noalign") == 0)
188
0
      sy->align = style_default.align;
189
4
    else if (end > 6 && strncasecmp(tmp, "align=", 6) == 0) {
190
0
      if (strcasecmp(tmp + 6, "left") == 0)
191
0
        sy->align = STYLE_ALIGN_LEFT;
192
0
      else if (strcasecmp(tmp + 6, "centre") == 0)
193
0
        sy->align = STYLE_ALIGN_CENTRE;
194
0
      else if (strcasecmp(tmp + 6, "right") == 0)
195
0
        sy->align = STYLE_ALIGN_RIGHT;
196
0
      else if (strcasecmp(tmp + 6, "absolute-centre") == 0)
197
0
        sy->align = STYLE_ALIGN_ABSOLUTE_CENTRE;
198
0
      else
199
0
        goto error;
200
4
    } else if (end > 5 && strncasecmp(tmp, "fill=", 5) == 0) {
201
0
      if ((value = colour_fromstring(tmp + 5)) == -1)
202
0
        goto error;
203
0
      sy->fill = value;
204
4
    } else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) {
205
2
      if ((value = colour_fromstring(tmp + 3)) == -1)
206
0
        goto error;
207
2
      if (*in == 'f' || *in == 'F') {
208
1
        if (value != 8)
209
1
          sy->gc.fg = value;
210
0
        else
211
0
          sy->gc.fg = base->fg;
212
1
      } else if (*in == 'b' || *in == 'B') {
213
1
        if (value != 8)
214
1
          sy->gc.bg = value;
215
0
        else
216
0
          sy->gc.bg = base->bg;
217
1
      } else
218
0
        goto error;
219
2
    } else if (end > 3 && strncasecmp(tmp, "us=", 3) == 0) {
220
0
      if ((value = colour_fromstring(tmp + 3)) == -1)
221
0
        goto error;
222
0
      if (value != 8)
223
0
        sy->gc.us = value;
224
0
      else
225
0
        sy->gc.us = base->us;
226
2
    } else if (strcasecmp(tmp, "none") == 0)
227
0
      sy->gc.attr = 0;
228
2
    else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) {
229
0
      if (strcmp(tmp + 2, "attr") == 0)
230
0
        sy->gc.attr |= GRID_ATTR_NOATTR;
231
0
      else {
232
0
        value = attributes_fromstring(tmp + 2);
233
0
        if (value == -1)
234
0
          goto error;
235
0
        sy->gc.attr &= ~value;
236
0
      }
237
2
    } else if (end > 6 && strncasecmp(tmp, "width=", 6) == 0) {
238
1
      if (end > 7 && tmp[end - 1] == '%') {
239
0
        tmp[end - 1] = '\0';
240
0
        n = strtonum(tmp + 6, 0, 100, &errstr);
241
0
        if (errstr != NULL)
242
0
          goto error;
243
0
        sy->width = (int)n;
244
0
        sy->width_percentage = 1;
245
1
      } else {
246
1
        n = strtonum(tmp + 6, 0, UINT_MAX, &errstr);
247
1
        if (errstr != NULL)
248
0
          goto error;
249
1
        sy->width = (int)n;
250
1
        sy->width_percentage = 0;
251
1
      }
252
1
    } else if (end > 4 && strncasecmp(tmp, "pad=", 4) == 0) {
253
1
      n = strtonum(tmp + 4, 0, UINT_MAX, &errstr);
254
1
      if (errstr != NULL)
255
0
        goto error;
256
1
      sy->pad = (int)n;
257
1
    } else {
258
0
      if ((value = attributes_fromstring(tmp)) == -1)
259
0
        goto error;
260
0
      sy->gc.attr |= value;
261
0
    }
262
263
6
    in += end + strspn(in + end, delimiters);
264
6
  } while (*in != '\0');
265
266
3
  return (0);
267
268
0
error:
269
0
  style_copy(sy, &saved);
270
0
  return (-1);
271
3
}
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
{
427
3
  memcpy(sy, &style_default, sizeof *sy);
428
3
  memcpy(&sy->gc, gc, sizeof sy->gc);
429
3
}
430
431
/* Copy style. */
432
void
433
style_copy(struct style *dst, struct style *src)
434
11.1k
{
435
11.1k
  memcpy(dst, src, sizeof *dst);
436
11.1k
}
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
}