xref: /openbsd-src/usr.bin/tmux/attributes.c (revision 43003dfe3ad45d1698bed8a37f2b0f5b14f20d4f)
1 /* $OpenBSD: attributes.c,v 1.2 2009/06/02 11:14:08 ray 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(u_char ch)
27 {
28 	static char	buf[128];
29 
30 	if (ch == 0)
31 		return ("default");
32 
33 	buf[0] = '\0';
34 	if (ch & GRID_ATTR_BRIGHT)
35 		strlcat(buf, "bright,", sizeof (buf));
36 	if (ch & GRID_ATTR_DIM)
37 		strlcat(buf, "dim,", sizeof (buf));
38 	if (ch & GRID_ATTR_UNDERSCORE)
39 		strlcat(buf, "underscore,", sizeof (buf));
40 	if (ch & GRID_ATTR_BLINK)
41 		strlcat(buf, "blink,", sizeof (buf));
42 	if (ch & GRID_ATTR_REVERSE)
43 		strlcat(buf, "reverse,", sizeof (buf));
44 	if (ch & GRID_ATTR_HIDDEN)
45 		strlcat(buf, "hidden,", sizeof (buf));
46 	if (ch & GRID_ATTR_ITALICS)
47 		strlcat(buf, "italics,", sizeof (buf));
48 	if (*buf)
49 		*(strrchr(buf, ',')) = '\0';
50 
51 	return (buf);
52 }
53 
54 int
55 attributes_fromstring(const char *str)
56 {
57 	const char	delimiters[] = " ,|";
58 	u_char		ch;
59 	size_t		end;
60 
61 	if (*str == '\0' || strcspn(str, delimiters) == 0)
62 		return (-1);
63 	if (strchr(delimiters, str[strlen(str) - 1]) != NULL)
64 		return (-1);
65 
66 	if (strcasecmp(str, "default") == 0)
67 		return (0);
68 
69 	ch = 0;
70 	do {
71 		end = strcspn(str, delimiters);
72 		if ((end == 6 && strncasecmp(str, "bright", end) == 0) ||
73 		    (end == 4 && strncasecmp(str, "bold", end) == 0))
74 			ch |= GRID_ATTR_BRIGHT;
75 		else if (end == 3 && strncasecmp(str, "dim", end) == 0)
76 			ch |= GRID_ATTR_DIM;
77 		else if (end == 10 && strncasecmp(str, "underscore", end) == 0)
78 			ch |= GRID_ATTR_UNDERSCORE;
79 		else if (end == 5 && strncasecmp(str, "blink", end) == 0)
80 			ch |= GRID_ATTR_BLINK;
81 		else if (end == 7 && strncasecmp(str, "reverse", end) == 0)
82 			ch |= GRID_ATTR_REVERSE;
83 		else if (end == 6 && strncasecmp(str, "hidden", end) == 0)
84 			ch |= GRID_ATTR_HIDDEN;
85 		else if (end == 7 && strncasecmp(str, "italics", end) == 0)
86 			ch |= GRID_ATTR_ITALICS;
87 		else
88 			return (-1);
89 		str += end + strspn(str + end, delimiters);
90 	} while (*str != '\0');
91 
92 	return (ch);
93 }
94