xref: /openbsd-src/usr.bin/tmux/attributes.c (revision c90a81c56dcebd6a1b73fe4aff9b03385b8e63b3)
1 /* $OpenBSD: attributes.c,v 1.8 2018/10/18 07:57:57 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[512];
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%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 	    (attr & GRID_ATTR_UNDERSCORE_2) ? "double-underscore," : "",
44 	    (attr & GRID_ATTR_UNDERSCORE_3) ? "curly-underscore," : "",
45 	    (attr & GRID_ATTR_UNDERSCORE_4) ? "dotted-underscore," : "",
46 	    (attr & GRID_ATTR_UNDERSCORE_5) ? "dashed-underscore," : "");
47 	if (len > 0)
48 		buf[len - 1] = '\0';
49 
50 	return (buf);
51 }
52 
53 int
54 attributes_fromstring(const char *str)
55 {
56 	const char	delimiters[] = " ,|";
57 	int		attr;
58 	size_t		end;
59 	u_int		i;
60 	struct {
61 		const char*	name;
62 		int		attr;
63 	} table[] = {
64 		{ "bright", GRID_ATTR_BRIGHT },
65 		{ "bold", GRID_ATTR_BRIGHT },
66 		{ "dim", GRID_ATTR_DIM },
67 		{ "underscore", GRID_ATTR_UNDERSCORE },
68 		{ "blink", GRID_ATTR_BLINK },
69 		{ "reverse", GRID_ATTR_REVERSE },
70 		{ "hidden", GRID_ATTR_HIDDEN },
71 		{ "italics", GRID_ATTR_ITALICS },
72 		{ "strikethrough", GRID_ATTR_STRIKETHROUGH },
73 		{ "double-underscore", GRID_ATTR_UNDERSCORE_2 },
74 		{ "curly-underscore", GRID_ATTR_UNDERSCORE_3 },
75 		{ "dotted-underscore", GRID_ATTR_UNDERSCORE_4 },
76 		{ "dashed-underscore", GRID_ATTR_UNDERSCORE_5 }
77 	};
78 
79 	if (*str == '\0' || strcspn(str, delimiters) == 0)
80 		return (-1);
81 	if (strchr(delimiters, str[strlen(str) - 1]) != NULL)
82 		return (-1);
83 
84 	if (strcasecmp(str, "default") == 0 || strcasecmp(str, "none") == 0)
85 		return (0);
86 
87 	attr = 0;
88 	do {
89 		end = strcspn(str, delimiters);
90 		for (i = 0; i < nitems(table); i++) {
91 			if (end != strlen(table[i].name))
92 				continue;
93 			if (strncasecmp(str, table[i].name, end) == 0) {
94 				attr |= table[i].attr;
95 				break;
96 			}
97 		}
98 		if (i == nitems(table))
99 			return (-1);
100 		str += end + strspn(str + end, delimiters);
101 	} while (*str != '\0');
102 
103 	return (attr);
104 }
105