xref: /openbsd-src/usr.bin/tmux/attributes.c (revision 311827fb38084af6afc5eb49c90c13c87a94815b)
1 /* $OpenBSD: attributes.c,v 1.1 2009/06/01 22:58:49 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(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 	*(strrchr(buf, ',')) = '\0';
49 
50 	return (buf);
51 }
52 
53 int
54 attributes_fromstring(const char *str)
55 {
56 	const char	delimiters[] = " ,|";
57 	u_char		ch;
58 	size_t		end;
59 
60 	if (*str == '\0' || strcspn(str, delimiters) == 0)
61 		return (-1);
62 	if (strchr(delimiters, str[strlen(str) - 1]) != NULL)
63 		return (-1);
64 
65 	if (strcasecmp(str, "default") == 0)
66 		return (0);
67 
68 	ch = 0;
69 	do {
70 		end = strcspn(str, delimiters);
71 		if ((end == 6 && strncasecmp(str, "bright", end) == 0) ||
72 		    (end == 4 && strncasecmp(str, "bold", end) == 0))
73 			ch |= GRID_ATTR_BRIGHT;
74 		else if (end == 3 && strncasecmp(str, "dim", end) == 0)
75 			ch |= GRID_ATTR_DIM;
76 		else if (end == 10 && strncasecmp(str, "underscore", end) == 0)
77 			ch |= GRID_ATTR_UNDERSCORE;
78 		else if (end == 5 && strncasecmp(str, "blink", end) == 0)
79 			ch |= GRID_ATTR_BLINK;
80 		else if (end == 7 && strncasecmp(str, "reverse", end) == 0)
81 			ch |= GRID_ATTR_REVERSE;
82 		else if (end == 6 && strncasecmp(str, "hidden", end) == 0)
83 			ch |= GRID_ATTR_HIDDEN;
84 		else if (end == 7 && strncasecmp(str, "italics", end) == 0)
85 			ch |= GRID_ATTR_ITALICS;
86 		else
87 			return (-1);
88 		str += end + strspn(str + end, delimiters);
89 	} while (*str != '\0');
90 
91 	return (ch);
92 }
93