GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/tmux/attributes.c Lines: 16 44 36.4 %
Date: 2017-11-07 Branches: 13 52 25.0 %

Line Branch Exec Source
1
/* $OpenBSD: attributes.c,v 1.7 2017/03/22 07:16:54 nicm Exp $ */
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
{
28
	static char	buf[128];
29
	size_t		len;
30
31
	if (attr == 0)
32
		return ("none");
33
34
	len = xsnprintf(buf, sizeof buf, "%s%s%s%s%s%s%s%s",
35
	    (attr & GRID_ATTR_BRIGHT) ? "bright," : "",
36
	    (attr & GRID_ATTR_DIM) ? "dim," : "",
37
	    (attr & GRID_ATTR_UNDERSCORE) ? "underscore," : "",
38
	    (attr & GRID_ATTR_BLINK)? "blink," : "",
39
	    (attr & GRID_ATTR_REVERSE) ? "reverse," : "",
40
	    (attr & GRID_ATTR_HIDDEN) ? "hidden," : "",
41
	    (attr & GRID_ATTR_ITALICS) ? "italics," : "",
42
	    (attr & GRID_ATTR_STRIKETHROUGH) ? "strikethrough," : "");
43
	if (len > 0)
44
		buf[len - 1] = '\0';
45
46
	return (buf);
47
}
48
49
int
50
attributes_fromstring(const char *str)
51
{
52
	const char	delimiters[] = " ,|";
53
	int		attr;
54
	size_t		end;
55
56

30
	if (*str == '\0' || strcspn(str, delimiters) == 0)
57
		return (-1);
58
10
	if (strchr(delimiters, str[strlen(str) - 1]) != NULL)
59
		return (-1);
60
61

20
	if (strcasecmp(str, "default") == 0 || strcasecmp(str, "none") == 0)
62
		return (0);
63
64
	attr = 0;
65
10
	do {
66
10
		end = strcspn(str, delimiters);
67

10
		if ((end == 6 && strncasecmp(str, "bright", end) == 0) ||
68
10
		    (end == 4 && strncasecmp(str, "bold", end) == 0))
69
			attr |= GRID_ATTR_BRIGHT;
70

10
		else if (end == 3 && strncasecmp(str, "dim", end) == 0)
71
			attr |= GRID_ATTR_DIM;
72

10
		else if (end == 10 && strncasecmp(str, "underscore", end) == 0)
73
			attr |= GRID_ATTR_UNDERSCORE;
74

10
		else if (end == 5 && strncasecmp(str, "blink", end) == 0)
75
			attr |= GRID_ATTR_BLINK;
76

20
		else if (end == 7 && strncasecmp(str, "reverse", end) == 0)
77
10
			attr |= GRID_ATTR_REVERSE;
78
		else if (end == 6 && strncasecmp(str, "hidden", end) == 0)
79
			attr |= GRID_ATTR_HIDDEN;
80
		else if (end == 7 && strncasecmp(str, "italics", end) == 0)
81
			attr |= GRID_ATTR_ITALICS;
82
		else if (end == 13 && strncasecmp(str, "strikethrough", end) == 0)
83
			attr |= GRID_ATTR_STRIKETHROUGH;
84
		else
85
			return (-1);
86
10
		str += end + strspn(str + end, delimiters);
87
10
	} while (*str != '\0');
88
89
10
	return (attr);
90
10
}