1 /* $OpenBSD$ */ 2 3 /* 4 * Copyright (c) 2009 Joshua Elsasser <josh (at) 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%s%s%s", 35 (attr & GRID_ATTR_CHARSET) ? "acs," : "", 36 (attr & GRID_ATTR_BRIGHT) ? "bright," : "", 37 (attr & GRID_ATTR_DIM) ? "dim," : "", 38 (attr & GRID_ATTR_UNDERSCORE) ? "underscore," : "", 39 (attr & GRID_ATTR_BLINK)? "blink," : "", 40 (attr & GRID_ATTR_REVERSE) ? "reverse," : "", 41 (attr & GRID_ATTR_HIDDEN) ? "hidden," : "", 42 (attr & GRID_ATTR_ITALICS) ? "italics," : "", 43 (attr & GRID_ATTR_STRIKETHROUGH) ? "strikethrough," : "", 44 (attr & GRID_ATTR_UNDERSCORE_2) ? "double-underscore," : "", 45 (attr & GRID_ATTR_UNDERSCORE_3) ? "curly-underscore," : "", 46 (attr & GRID_ATTR_UNDERSCORE_4) ? "dotted-underscore," : "", 47 (attr & GRID_ATTR_UNDERSCORE_5) ? "dashed-underscore," : "", 48 (attr & GRID_ATTR_OVERLINE) ? "overline," : "", 49 (attr & GRID_ATTR_NOATTR) ? "noattr," : ""); 50 if (len > 0) 51 buf[len - 1] = '\0'; 52 53 return (buf); 54 } 55 56 int 57 attributes_fromstring(const char *str) 58 { 59 const char delimiters[] = " ,|"; 60 int attr; 61 size_t end; 62 u_int i; 63 struct { 64 const char *name; 65 int attr; 66 } table[] = { 67 { "acs", GRID_ATTR_CHARSET }, 68 { "bright", GRID_ATTR_BRIGHT }, 69 { "bold", GRID_ATTR_BRIGHT }, 70 { "dim", GRID_ATTR_DIM }, 71 { "underscore", GRID_ATTR_UNDERSCORE }, 72 { "blink", GRID_ATTR_BLINK }, 73 { "reverse", GRID_ATTR_REVERSE }, 74 { "hidden", GRID_ATTR_HIDDEN }, 75 { "italics", GRID_ATTR_ITALICS }, 76 { "strikethrough", GRID_ATTR_STRIKETHROUGH }, 77 { "double-underscore", GRID_ATTR_UNDERSCORE_2 }, 78 { "curly-underscore", GRID_ATTR_UNDERSCORE_3 }, 79 { "dotted-underscore", GRID_ATTR_UNDERSCORE_4 }, 80 { "dashed-underscore", GRID_ATTR_UNDERSCORE_5 }, 81 { "overline", GRID_ATTR_OVERLINE } 82 }; 83 84 if (*str == '\0' || strcspn(str, delimiters) == 0) 85 return (-1); 86 if (strchr(delimiters, str[strlen(str) - 1]) != NULL) 87 return (-1); 88 89 if (strcasecmp(str, "default") == 0 || strcasecmp(str, "none") == 0) 90 return (0); 91 92 attr = 0; 93 do { 94 end = strcspn(str, delimiters); 95 for (i = 0; i < nitems(table); i++) { 96 if (end != strlen(table[i].name)) 97 continue; 98 if (strncasecmp(str, table[i].name, end) == 0) { 99 attr |= table[i].attr; 100 break; 101 } 102 } 103 if (i == nitems(table)) 104 return (-1); 105 str += end + strspn(str + end, delimiters); 106 } while (*str != '\0'); 107 108 return (attr); 109 } 110