Coverage Report

Created: 2024-07-27 06:19

/src/tmux/attributes.c
Line
Count
Source (jump to first uncovered line)
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",
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
  if (len > 0)
50
0
    buf[len - 1] = '\0';
51
52
0
  return (buf);
53
0
}
54
55
int
56
attributes_fromstring(const char *str)
57
0
{
58
0
  const char  delimiters[] = " ,|";
59
0
  int   attr;
60
0
  size_t    end;
61
0
  u_int   i;
62
0
  struct {
63
0
    const char  *name;
64
0
    int    attr;
65
0
  } table[] = {
66
0
    { "acs", GRID_ATTR_CHARSET },
67
0
    { "bright", GRID_ATTR_BRIGHT },
68
0
    { "bold", GRID_ATTR_BRIGHT },
69
0
    { "dim", GRID_ATTR_DIM },
70
0
    { "underscore", GRID_ATTR_UNDERSCORE },
71
0
    { "blink", GRID_ATTR_BLINK },
72
0
    { "reverse", GRID_ATTR_REVERSE },
73
0
    { "hidden", GRID_ATTR_HIDDEN },
74
0
    { "italics", GRID_ATTR_ITALICS },
75
0
    { "strikethrough", GRID_ATTR_STRIKETHROUGH },
76
0
    { "double-underscore", GRID_ATTR_UNDERSCORE_2 },
77
0
    { "curly-underscore", GRID_ATTR_UNDERSCORE_3 },
78
0
    { "dotted-underscore", GRID_ATTR_UNDERSCORE_4 },
79
0
    { "dashed-underscore", GRID_ATTR_UNDERSCORE_5 },
80
0
    { "overline", GRID_ATTR_OVERLINE }
81
0
  };
82
83
0
  if (*str == '\0' || strcspn(str, delimiters) == 0)
84
0
    return (-1);
85
0
  if (strchr(delimiters, str[strlen(str) - 1]) != NULL)
86
0
    return (-1);
87
88
0
  if (strcasecmp(str, "default") == 0 || strcasecmp(str, "none") == 0)
89
0
    return (0);
90
91
0
  attr = 0;
92
0
  do {
93
0
    end = strcspn(str, delimiters);
94
0
    for (i = 0; i < nitems(table); i++) {
95
0
      if (end != strlen(table[i].name))
96
0
        continue;
97
0
      if (strncasecmp(str, table[i].name, end) == 0) {
98
0
        attr |= table[i].attr;
99
0
        break;
100
0
      }
101
0
    }
102
0
    if (i == nitems(table))
103
0
      return (-1);
104
0
    str += end + strspn(str + end, delimiters);
105
0
  } while (*str != '\0');
106
107
0
  return (attr);
108
0
}