Coverage Report

Created: 2026-01-25 06:48

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, 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, "pane") == 0) {
141
0
        if (found == NULL)
142
0
          goto error;
143
0
        if (*found != '%' || found[1] == '\0')
144
0
          goto error;
145
0
        n = strtonum(found + 1, 0, UINT_MAX, &errstr);
146
0
        if (errstr != NULL)
147
0
          goto error;
148
0
        sy->range_type = STYLE_RANGE_PANE;
149
0
        sy->range_argument = n;
150
0
        style_set_range_string(sy, "");
151
0
      } else if (strcasecmp(tmp + 6, "window") == 0) {
152
0
        if (found == NULL)
153
0
          goto error;
154
0
        n = strtonum(found, 0, UINT_MAX, &errstr);
155
0
        if (errstr != NULL)
156
0
          goto error;
157
0
        sy->range_type = STYLE_RANGE_WINDOW;
158
0
        sy->range_argument = n;
159
0
        style_set_range_string(sy, "");
160
0
      } else if (strcasecmp(tmp + 6, "session") == 0) {
161
0
        if (found == NULL)
162
0
          goto error;
163
0
        if (*found != '$' || found[1] == '\0')
164
0
          goto error;
165
0
        n = strtonum(found + 1, 0, UINT_MAX, &errstr);
166
0
        if (errstr != NULL)
167
0
          goto error;
168
0
        sy->range_type = STYLE_RANGE_SESSION;
169
0
        sy->range_argument = n;
170
0
        style_set_range_string(sy, "");
171
0
      } else if (strcasecmp(tmp + 6, "user") == 0) {
172
0
        if (found == NULL)
173
0
          goto error;
174
0
        sy->range_type = STYLE_RANGE_USER;
175
0
        sy->range_argument = 0;
176
0
        style_set_range_string(sy, found);
177
0
      }
178
4
    } else if (strcasecmp(tmp, "noalign") == 0)
179
0
      sy->align = style_default.align;
180
4
    else if (end > 6 && strncasecmp(tmp, "align=", 6) == 0) {
181
0
      if (strcasecmp(tmp + 6, "left") == 0)
182
0
        sy->align = STYLE_ALIGN_LEFT;
183
0
      else if (strcasecmp(tmp + 6, "centre") == 0)
184
0
        sy->align = STYLE_ALIGN_CENTRE;
185
0
      else if (strcasecmp(tmp + 6, "right") == 0)
186
0
        sy->align = STYLE_ALIGN_RIGHT;
187
0
      else if (strcasecmp(tmp + 6, "absolute-centre") == 0)
188
0
        sy->align = STYLE_ALIGN_ABSOLUTE_CENTRE;
189
0
      else
190
0
        goto error;
191
4
    } else if (end > 5 && strncasecmp(tmp, "fill=", 5) == 0) {
192
0
      if ((value = colour_fromstring(tmp + 5)) == -1)
193
0
        goto error;
194
0
      sy->fill = value;
195
4
    } else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) {
196
2
      if ((value = colour_fromstring(tmp + 3)) == -1)
197
0
        goto error;
198
2
      if (*in == 'f' || *in == 'F') {
199
1
        if (value != 8)
200
1
          sy->gc.fg = value;
201
0
        else
202
0
          sy->gc.fg = base->fg;
203
1
      } else if (*in == 'b' || *in == 'B') {
204
1
        if (value != 8)
205
1
          sy->gc.bg = value;
206
0
        else
207
0
          sy->gc.bg = base->bg;
208
1
      } else
209
0
        goto error;
210
2
    } else if (end > 3 && strncasecmp(tmp, "us=", 3) == 0) {
211
0
      if ((value = colour_fromstring(tmp + 3)) == -1)
212
0
        goto error;
213
0
      if (value != 8)
214
0
        sy->gc.us = value;
215
0
      else
216
0
        sy->gc.us = base->us;
217
2
    } else if (strcasecmp(tmp, "none") == 0)
218
0
      sy->gc.attr = 0;
219
2
    else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) {
220
0
      if (strcmp(tmp + 2, "attr") == 0)
221
0
        sy->gc.attr |= GRID_ATTR_NOATTR;
222
0
      else {
223
0
        value = attributes_fromstring(tmp + 2);
224
0
        if (value == -1)
225
0
          goto error;
226
0
        sy->gc.attr &= ~value;
227
0
      }
228
2
    } else if (end > 6 && strncasecmp(tmp, "width=", 6) == 0) {
229
1
      n = strtonum(tmp + 6, 0, UINT_MAX, &errstr);
230
1
      if (errstr != NULL)
231
0
        goto error;
232
1
      sy->width = (int)n;
233
1
    } else if (end > 4 && strncasecmp(tmp, "pad=", 4) == 0) {
234
1
      n = strtonum(tmp + 4, 0, UINT_MAX, &errstr);
235
1
      if (errstr != NULL)
236
0
        goto error;
237
1
      sy->pad = (int)n;
238
1
    } else {
239
0
      if ((value = attributes_fromstring(tmp)) == -1)
240
0
        goto error;
241
0
      sy->gc.attr |= value;
242
0
    }
243
244
6
    in += end + strspn(in + end, delimiters);
245
6
  } while (*in != '\0');
246
247
3
  return (0);
248
249
0
error:
250
0
  style_copy(sy, &saved);
251
0
  return (-1);
252
3
}
253
254
/* Convert style to a string. */
255
const char *
256
style_tostring(struct style *sy)
257
0
{
258
0
  struct grid_cell  *gc = &sy->gc;
259
0
  int      off = 0;
260
0
  const char    *comma = "", *tmp = "";
261
0
  static char    s[256];
262
0
  char       b[21];
263
264
0
  *s = '\0';
265
266
0
  if (sy->list != STYLE_LIST_OFF) {
267
0
    if (sy->list == STYLE_LIST_ON)
268
0
      tmp = "on";
269
0
    else if (sy->list == STYLE_LIST_FOCUS)
270
0
      tmp = "focus";
271
0
    else if (sy->list == STYLE_LIST_LEFT_MARKER)
272
0
      tmp = "left-marker";
273
0
    else if (sy->list == STYLE_LIST_RIGHT_MARKER)
274
0
      tmp = "right-marker";
275
0
    off += xsnprintf(s + off, sizeof s - off, "%slist=%s", comma,
276
0
        tmp);
277
0
    comma = ",";
278
0
  }
279
0
  if (sy->range_type != STYLE_RANGE_NONE) {
280
0
    if (sy->range_type == STYLE_RANGE_LEFT)
281
0
      tmp = "left";
282
0
    else if (sy->range_type == STYLE_RANGE_RIGHT)
283
0
      tmp = "right";
284
0
    else if (sy->range_type == STYLE_RANGE_PANE) {
285
0
      snprintf(b, sizeof b, "pane|%%%u", sy->range_argument);
286
0
      tmp = b;
287
0
    } else if (sy->range_type == STYLE_RANGE_WINDOW) {
288
0
      snprintf(b, sizeof b, "window|%u", sy->range_argument);
289
0
      tmp = b;
290
0
    } else if (sy->range_type == STYLE_RANGE_SESSION) {
291
0
      snprintf(b, sizeof b, "session|$%u",
292
0
          sy->range_argument);
293
0
      tmp = b;
294
0
    } else if (sy->range_type == STYLE_RANGE_USER) {
295
0
      snprintf(b, sizeof b, "user|%s", sy->range_string);
296
0
      tmp = b;
297
0
    }
298
0
    off += xsnprintf(s + off, sizeof s - off, "%srange=%s", comma,
299
0
        tmp);
300
0
    comma = ",";
301
0
  }
302
0
  if (sy->align != STYLE_ALIGN_DEFAULT) {
303
0
    if (sy->align == STYLE_ALIGN_LEFT)
304
0
      tmp = "left";
305
0
    else if (sy->align == STYLE_ALIGN_CENTRE)
306
0
      tmp = "centre";
307
0
    else if (sy->align == STYLE_ALIGN_RIGHT)
308
0
      tmp = "right";
309
0
    else if (sy->align == STYLE_ALIGN_ABSOLUTE_CENTRE)
310
0
      tmp = "absolute-centre";
311
0
    off += xsnprintf(s + off, sizeof s - off, "%salign=%s", comma,
312
0
        tmp);
313
0
    comma = ",";
314
0
  }
315
0
  if (sy->default_type != STYLE_DEFAULT_BASE) {
316
0
    if (sy->default_type == STYLE_DEFAULT_PUSH)
317
0
      tmp = "push-default";
318
0
    else if (sy->default_type == STYLE_DEFAULT_POP)
319
0
      tmp = "pop-default";
320
0
    else if (sy->default_type == STYLE_DEFAULT_SET)
321
0
      tmp = "set-default";
322
0
    off += xsnprintf(s + off, sizeof s - off, "%s%s", comma, tmp);
323
0
    comma = ",";
324
0
  }
325
0
  if (sy->fill != 8) {
326
0
    off += xsnprintf(s + off, sizeof s - off, "%sfill=%s", comma,
327
0
        colour_tostring(sy->fill));
328
0
    comma = ",";
329
0
  }
330
0
  if (gc->fg != 8) {
331
0
    off += xsnprintf(s + off, sizeof s - off, "%sfg=%s", comma,
332
0
        colour_tostring(gc->fg));
333
0
    comma = ",";
334
0
  }
335
0
  if (gc->bg != 8) {
336
0
    off += xsnprintf(s + off, sizeof s - off, "%sbg=%s", comma,
337
0
        colour_tostring(gc->bg));
338
0
    comma = ",";
339
0
  }
340
0
  if (gc->us != 8) {
341
0
    off += xsnprintf(s + off, sizeof s - off, "%sus=%s", comma,
342
0
        colour_tostring(gc->us));
343
0
    comma = ",";
344
0
  }
345
0
  if (gc->attr != 0) {
346
0
    xsnprintf(s + off, sizeof s - off, "%s%s", comma,
347
0
        attributes_tostring(gc->attr));
348
0
    comma = ",";
349
0
  }
350
0
  if (sy->width >= 0) {
351
0
    xsnprintf(s + off, sizeof s - off, "%swidth=%u", comma,
352
0
        sy->width);
353
0
    comma = ",";
354
0
  }
355
0
  if (sy->pad >= 0) {
356
0
    xsnprintf(s + off, sizeof s - off, "%spad=%u", comma,
357
0
        sy->pad);
358
0
    comma = ",";
359
0
  }
360
0
  if (*s == '\0')
361
0
    return ("default");
362
0
  return (s);
363
0
}
364
365
/* Apply a style on top of the given style. */
366
void
367
style_add(struct grid_cell *gc, struct options *oo, const char *name,
368
    struct format_tree *ft)
369
7.52k
{
370
7.52k
  struct style    *sy;
371
7.52k
  struct format_tree  *ft0 = NULL;
372
373
7.52k
  if (ft == NULL)
374
0
    ft = ft0 = format_create(NULL, NULL, 0, FORMAT_NOJOBS);
375
376
7.52k
  sy = options_string_to_style(oo, name, ft);
377
7.52k
  if (sy == NULL)
378
0
    sy = &style_default;
379
7.52k
  if (sy->gc.fg != 8)
380
0
    gc->fg = sy->gc.fg;
381
7.52k
  if (sy->gc.bg != 8)
382
0
    gc->bg = sy->gc.bg;
383
7.52k
  if (sy->gc.us != 8)
384
0
    gc->us = sy->gc.us;
385
7.52k
  gc->attr |= sy->gc.attr;
386
387
7.52k
  if (ft0 != NULL)
388
0
    format_free(ft0);
389
7.52k
}
390
391
/* Apply a style on top of the default style. */
392
void
393
style_apply(struct grid_cell *gc, struct options *oo, const char *name,
394
    struct format_tree *ft)
395
0
{
396
0
  memcpy(gc, &grid_default_cell, sizeof *gc);
397
0
  style_add(gc, oo, name, ft);
398
0
}
399
400
/* Initialize style from cell. */
401
void
402
style_set(struct style *sy, const struct grid_cell *gc)
403
3
{
404
3
  memcpy(sy, &style_default, sizeof *sy);
405
3
  memcpy(&sy->gc, gc, sizeof sy->gc);
406
3
}
407
408
/* Copy style. */
409
void
410
style_copy(struct style *dst, struct style *src)
411
10.9k
{
412
10.9k
  memcpy(dst, src, sizeof *dst);
413
10.9k
}
414
415
void
416
style_set_scrollbar_style_from_option(struct style *sb_style, struct options *oo)
417
10.9k
{
418
10.9k
  struct style  *sy;
419
420
10.9k
  sy = options_string_to_style(oo, "pane-scrollbars-style", NULL);
421
10.9k
  if (sy == NULL) {
422
0
    style_set(sb_style, &grid_default_cell);
423
0
    sb_style->width = PANE_SCROLLBARS_DEFAULT_WIDTH;
424
0
    sb_style->pad = PANE_SCROLLBARS_DEFAULT_PADDING;
425
0
    utf8_set(&sb_style->gc.data, PANE_SCROLLBARS_CHARACTER);
426
10.9k
  } else {
427
10.9k
    style_copy(sb_style, sy);
428
10.9k
    if (sb_style->width < 1)
429
0
      sb_style->width = PANE_SCROLLBARS_DEFAULT_WIDTH;
430
10.9k
    if (sb_style->pad < 0)
431
0
      sb_style->pad = PANE_SCROLLBARS_DEFAULT_PADDING;
432
10.9k
    utf8_set(&sb_style->gc.data, PANE_SCROLLBARS_CHARACTER);
433
10.9k
  }
434
10.9k
}