Coverage Report

Created: 2026-04-27 07:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/attributes.c
Line
Count
Source
1
/* $OpenBSD$ */
2
3
/*
4
 * Copyright (c) 2009 Joshua Elsasser <josh@elsasser.org>
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 <string.h>
22
23
#include "tmux.h"
24
25
const char *
26
attributes_tostring(int attr)
27
0
{
28
0
  static char buf[512];
29
0
  size_t    len;
30
31
0
  if (attr == 0)
32
0
    return ("none");
33
34
0
  len = xsnprintf(buf, sizeof buf, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
35
0
      (attr & GRID_ATTR_CHARSET) ? "acs," : "",
36
0
      (attr & GRID_ATTR_BRIGHT) ? "bright," : "",
37
0
      (attr & GRID_ATTR_DIM) ? "dim," : "",
38
0
      (attr & GRID_ATTR_UNDERSCORE) ? "underscore," : "",
39
0
      (attr & GRID_ATTR_BLINK)? "blink," : "",
40
0
      (attr & GRID_ATTR_REVERSE) ? "reverse," : "",
41
0
      (attr & GRID_ATTR_HIDDEN) ? "hidden," : "",
42
0
      (attr & GRID_ATTR_ITALICS) ? "italics," : "",
43
0
      (attr & GRID_ATTR_STRIKETHROUGH) ? "strikethrough," : "",
44
0
      (attr & GRID_ATTR_UNDERSCORE_2) ? "double-underscore," : "",
45
0
      (attr & GRID_ATTR_UNDERSCORE_3) ? "curly-underscore," : "",
46
0
      (attr & GRID_ATTR_UNDERSCORE_4) ? "dotted-underscore," : "",
47
0
      (attr & GRID_ATTR_UNDERSCORE_5) ? "dashed-underscore," : "",
48
0
        (attr & GRID_ATTR_OVERLINE) ? "overline," : "",
49
0
      (attr & GRID_ATTR_NOATTR) ? "noattr," : "");
50
0
  if (len > 0)
51
0
    buf[len - 1] = '\0';
52
53
0
  return (buf);
54
0
}
55
56
int
57
attributes_fromstring(const char *str)
58
1.68k
{
59
1.68k
  const char  delimiters[] = " ,|";
60
1.68k
  int   attr;
61
1.68k
  size_t    end;
62
1.68k
  u_int   i;
63
1.68k
  struct {
64
1.68k
    const char  *name;
65
1.68k
    int    attr;
66
1.68k
  } table[] = {
67
1.68k
    { "acs", GRID_ATTR_CHARSET },
68
1.68k
    { "bright", GRID_ATTR_BRIGHT },
69
1.68k
    { "bold", GRID_ATTR_BRIGHT },
70
1.68k
    { "dim", GRID_ATTR_DIM },
71
1.68k
    { "underscore", GRID_ATTR_UNDERSCORE },
72
1.68k
    { "blink", GRID_ATTR_BLINK },
73
1.68k
    { "reverse", GRID_ATTR_REVERSE },
74
1.68k
    { "hidden", GRID_ATTR_HIDDEN },
75
1.68k
    { "italics", GRID_ATTR_ITALICS },
76
1.68k
    { "strikethrough", GRID_ATTR_STRIKETHROUGH },
77
1.68k
    { "double-underscore", GRID_ATTR_UNDERSCORE_2 },
78
1.68k
    { "curly-underscore", GRID_ATTR_UNDERSCORE_3 },
79
1.68k
    { "dotted-underscore", GRID_ATTR_UNDERSCORE_4 },
80
1.68k
    { "dashed-underscore", GRID_ATTR_UNDERSCORE_5 },
81
1.68k
    { "overline", GRID_ATTR_OVERLINE }
82
1.68k
  };
83
84
1.68k
  if (*str == '\0' || strcspn(str, delimiters) == 0)
85
1
    return (-1);
86
1.68k
  if (strchr(delimiters, str[strlen(str) - 1]) != NULL)
87
9
    return (-1);
88
89
1.67k
  if (strcasecmp(str, "default") == 0 || strcasecmp(str, "none") == 0)
90
152
    return (0);
91
92
1.51k
  attr = 0;
93
1.67k
  do {
94
1.67k
    end = strcspn(str, delimiters);
95
20.1k
    for (i = 0; i < nitems(table); i++) {
96
19.0k
      if (end != strlen(table[i].name))
97
16.7k
        continue;
98
2.36k
      if (strncasecmp(str, table[i].name, end) == 0) {
99
644
        attr |= table[i].attr;
100
644
        break;
101
644
      }
102
2.36k
    }
103
1.67k
    if (i == nitems(table))
104
1.03k
      return (-1);
105
644
    str += end + strspn(str + end, delimiters);
106
644
  } while (*str != '\0');
107
108
484
  return (attr);
109
1.51k
}