Annotation of src/usr.bin/tmux/attributes.c, Revision 1.7
1.7 ! nicm 1: /* $OpenBSD: attributes.c,v 1.6 2017/02/16 10:53:25 nicm Exp $ */
1.1 nicm 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 *
1.7 ! nicm 26: attributes_tostring(int attr)
1.1 nicm 27: {
28: static char buf[128];
1.5 nicm 29: size_t len;
1.1 nicm 30:
1.3 nicm 31: if (attr == 0)
1.4 nicm 32: return ("none");
1.1 nicm 33:
1.7 ! nicm 34: len = xsnprintf(buf, sizeof buf, "%s%s%s%s%s%s%s%s",
1.6 nicm 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," : "",
1.7 ! nicm 41: (attr & GRID_ATTR_ITALICS) ? "italics," : "",
! 42: (attr & GRID_ATTR_STRIKETHROUGH) ? "strikethrough," : "");
1.5 nicm 43: if (len > 0)
44: buf[len - 1] = '\0';
1.1 nicm 45:
46: return (buf);
47: }
48:
49: int
50: attributes_fromstring(const char *str)
51: {
52: const char delimiters[] = " ,|";
1.7 ! nicm 53: int attr;
1.1 nicm 54: size_t end;
55:
56: if (*str == '\0' || strcspn(str, delimiters) == 0)
57: return (-1);
58: if (strchr(delimiters, str[strlen(str) - 1]) != NULL)
59: return (-1);
60:
1.4 nicm 61: if (strcasecmp(str, "default") == 0 || strcasecmp(str, "none") == 0)
1.1 nicm 62: return (0);
63:
1.3 nicm 64: attr = 0;
1.1 nicm 65: do {
66: end = strcspn(str, delimiters);
67: if ((end == 6 && strncasecmp(str, "bright", end) == 0) ||
68: (end == 4 && strncasecmp(str, "bold", end) == 0))
1.3 nicm 69: attr |= GRID_ATTR_BRIGHT;
1.1 nicm 70: else if (end == 3 && strncasecmp(str, "dim", end) == 0)
1.3 nicm 71: attr |= GRID_ATTR_DIM;
1.1 nicm 72: else if (end == 10 && strncasecmp(str, "underscore", end) == 0)
1.3 nicm 73: attr |= GRID_ATTR_UNDERSCORE;
1.1 nicm 74: else if (end == 5 && strncasecmp(str, "blink", end) == 0)
1.3 nicm 75: attr |= GRID_ATTR_BLINK;
1.1 nicm 76: else if (end == 7 && strncasecmp(str, "reverse", end) == 0)
1.3 nicm 77: attr |= GRID_ATTR_REVERSE;
1.1 nicm 78: else if (end == 6 && strncasecmp(str, "hidden", end) == 0)
1.3 nicm 79: attr |= GRID_ATTR_HIDDEN;
1.1 nicm 80: else if (end == 7 && strncasecmp(str, "italics", end) == 0)
1.3 nicm 81: attr |= GRID_ATTR_ITALICS;
1.7 ! nicm 82: else if (end == 13 && strncasecmp(str, "strikethrough", end) == 0)
! 83: attr |= GRID_ATTR_STRIKETHROUGH;
1.1 nicm 84: else
85: return (-1);
86: str += end + strspn(str + end, delimiters);
87: } while (*str != '\0');
88:
1.3 nicm 89: return (attr);
1.1 nicm 90: }