1 /* $OpenBSD$ */ 2 3 /* 4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> 5 * Copyright (c) 2014 Tiago Cunha <tcunha@users.sourceforge.net> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 16 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 17 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #include <sys/types.h> 21 22 #include <ctype.h> 23 #include <stdlib.h> 24 #include <string.h> 25 26 #include "tmux.h" 27 28 /* Mask for bits not included in style. */ 29 #define STYLE_ATTR_MASK (~GRID_ATTR_CHARSET) 30 31 /* Default style. */ 32 static struct style style_default = { 33 { { { ' ' }, 0, 1, 1 }, 0, 0, 8, 8, 0 }, 34 35 8, 36 STYLE_ALIGN_DEFAULT, 37 STYLE_LIST_OFF, 38 39 STYLE_RANGE_NONE, 0, 40 41 STYLE_DEFAULT_BASE 42 }; 43 44 /* 45 * Parse an embedded style of the form "fg=colour,bg=colour,bright,...". Note 46 * that this adds onto the given style, so it must have been initialized 47 * already. 48 */ 49 int 50 style_parse(struct style *sy, const struct grid_cell *base, const char *in) 51 { 52 struct style saved; 53 const char delimiters[] = " ,", *cp; 54 char tmp[256], *found; 55 int value; 56 size_t end; 57 58 if (*in == '\0') 59 return (0); 60 style_copy(&saved, sy); 61 62 do { 63 while (*in != '\0' && strchr(delimiters, *in) != NULL) 64 in++; 65 if (*in == '\0') 66 break; 67 68 end = strcspn(in, delimiters); 69 if (end > (sizeof tmp) - 1) 70 goto error; 71 memcpy(tmp, in, end); 72 tmp[end] = '\0'; 73 74 if (strcasecmp(tmp, "default") == 0) { 75 sy->gc.fg = base->fg; 76 sy->gc.bg = base->bg; 77 sy->gc.attr = base->attr; 78 sy->gc.flags = base->flags; 79 } else if (strcasecmp(tmp, "push-default") == 0) 80 sy->default_type = STYLE_DEFAULT_PUSH; 81 else if (strcasecmp(tmp, "pop-default") == 0) 82 sy->default_type = STYLE_DEFAULT_POP; 83 else if (strcasecmp(tmp, "nolist") == 0) 84 sy->list = STYLE_LIST_OFF; 85 else if (strncasecmp(tmp, "list=", 5) == 0) { 86 if (strcasecmp(tmp + 5, "on") == 0) 87 sy->list = STYLE_LIST_ON; 88 else if (strcasecmp(tmp + 5, "focus") == 0) 89 sy->list = STYLE_LIST_FOCUS; 90 else if (strcasecmp(tmp + 5, "left-marker") == 0) 91 sy->list = STYLE_LIST_LEFT_MARKER; 92 else if (strcasecmp(tmp + 5, "right-marker") == 0) 93 sy->list = STYLE_LIST_RIGHT_MARKER; 94 else 95 goto error; 96 } else if (strcasecmp(tmp, "norange") == 0) { 97 sy->range_type = style_default.range_type; 98 sy->range_argument = style_default.range_type; 99 } else if (end > 6 && strncasecmp(tmp, "range=", 6) == 0) { 100 found = strchr(tmp + 6, '|'); 101 if (found != NULL) { 102 *found++ = '\0'; 103 if (*found == '\0') 104 goto error; 105 for (cp = found; *cp != '\0'; cp++) { 106 if (!isdigit((u_char)*cp)) 107 goto error; 108 } 109 } 110 if (strcasecmp(tmp + 6, "left") == 0) { 111 if (found != NULL) 112 goto error; 113 sy->range_type = STYLE_RANGE_LEFT; 114 sy->range_argument = 0; 115 } else if (strcasecmp(tmp + 6, "right") == 0) { 116 if (found != NULL) 117 goto error; 118 sy->range_type = STYLE_RANGE_RIGHT; 119 sy->range_argument = 0; 120 } else if (strcasecmp(tmp + 6, "window") == 0) { 121 if (found == NULL) 122 goto error; 123 sy->range_type = STYLE_RANGE_WINDOW; 124 sy->range_argument = atoi(found); 125 } 126 } else if (strcasecmp(tmp, "noalign") == 0) 127 sy->align = style_default.align; 128 else if (end > 6 && strncasecmp(tmp, "align=", 6) == 0) { 129 if (strcasecmp(tmp + 6, "left") == 0) 130 sy->align = STYLE_ALIGN_LEFT; 131 else if (strcasecmp(tmp + 6, "centre") == 0) 132 sy->align = STYLE_ALIGN_CENTRE; 133 else if (strcasecmp(tmp + 6, "right") == 0) 134 sy->align = STYLE_ALIGN_RIGHT; 135 else 136 goto error; 137 } else if (end > 5 && strncasecmp(tmp, "fill=", 5) == 0) { 138 if ((value = colour_fromstring(tmp + 5)) == -1) 139 goto error; 140 sy->fill = value; 141 } else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) { 142 if ((value = colour_fromstring(tmp + 3)) == -1) 143 goto error; 144 if (*in == 'f' || *in == 'F') { 145 if (value != 8) 146 sy->gc.fg = value; 147 else 148 sy->gc.fg = base->fg; 149 } else if (*in == 'b' || *in == 'B') { 150 if (value != 8) 151 sy->gc.bg = value; 152 else 153 sy->gc.bg = base->bg; 154 } else 155 goto error; 156 } else if (strcasecmp(tmp, "none") == 0) 157 sy->gc.attr = 0; 158 else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) { 159 if ((value = attributes_fromstring(tmp + 2)) == -1) 160 goto error; 161 sy->gc.attr &= ~value; 162 } else { 163 if ((value = attributes_fromstring(tmp)) == -1) 164 goto error; 165 sy->gc.attr |= value; 166 } 167 168 in += end + strspn(in + end, delimiters); 169 } while (*in != '\0'); 170 171 return (0); 172 173 error: 174 style_copy(sy, &saved); 175 return (-1); 176 } 177 178 /* Convert style to a string. */ 179 const char * 180 style_tostring(struct style *sy) 181 { 182 struct grid_cell *gc = &sy->gc; 183 int off = 0; 184 const char *comma = "", *tmp = ""; 185 static char s[256]; 186 char b[16]; 187 188 *s = '\0'; 189 190 if (sy->list != STYLE_LIST_OFF) { 191 if (sy->list == STYLE_LIST_ON) 192 tmp = "on"; 193 else if (sy->list == STYLE_LIST_FOCUS) 194 tmp = "focus"; 195 else if (sy->list == STYLE_LIST_LEFT_MARKER) 196 tmp = "left-marker"; 197 else if (sy->list == STYLE_LIST_RIGHT_MARKER) 198 tmp = "right-marker"; 199 else 200 abort(); // XXX: gcc 201 off += xsnprintf(s + off, sizeof s - off, "%slist=%s", comma, 202 tmp); 203 comma = ","; 204 } 205 if (sy->range_type != STYLE_RANGE_NONE) { 206 if (sy->range_type == STYLE_RANGE_LEFT) 207 tmp = "left"; 208 else if (sy->range_type == STYLE_RANGE_RIGHT) 209 tmp = "right"; 210 else if (sy->range_type == STYLE_RANGE_WINDOW) { 211 snprintf(b, sizeof b, "window|%u", sy->range_argument); 212 tmp = b; 213 } else 214 abort(); // XXX: gcc 215 off += xsnprintf(s + off, sizeof s - off, "%srange=%s", comma, 216 tmp); 217 comma = ","; 218 } 219 if (sy->align != STYLE_ALIGN_DEFAULT) { 220 if (sy->align == STYLE_ALIGN_LEFT) 221 tmp = "left"; 222 else if (sy->align == STYLE_ALIGN_CENTRE) 223 tmp = "centre"; 224 else if (sy->align == STYLE_ALIGN_RIGHT) 225 tmp = "right"; 226 else 227 abort(); // XXX: gcc 228 off += xsnprintf(s + off, sizeof s - off, "%salign=%s", comma, 229 tmp); 230 comma = ","; 231 } 232 if (sy->default_type != STYLE_DEFAULT_BASE) { 233 if (sy->default_type == STYLE_DEFAULT_PUSH) 234 tmp = "push-default"; 235 else if (sy->default_type == STYLE_DEFAULT_POP) 236 tmp = "pop-default"; 237 off += xsnprintf(s + off, sizeof s - off, "%s%s", comma, tmp); 238 comma = ","; 239 } 240 if (sy->fill != 8) { 241 off += xsnprintf(s + off, sizeof s - off, "%sfill=%s", comma, 242 colour_tostring(sy->fill)); 243 comma = ","; 244 } 245 if (gc->fg != 8) { 246 off += xsnprintf(s + off, sizeof s - off, "%sfg=%s", comma, 247 colour_tostring(gc->fg)); 248 comma = ","; 249 } 250 if (gc->bg != 8) { 251 off += xsnprintf(s + off, sizeof s - off, "%sbg=%s", comma, 252 colour_tostring(gc->bg)); 253 comma = ","; 254 } 255 if (gc->attr != 0 && gc->attr != GRID_ATTR_CHARSET) { 256 xsnprintf(s + off, sizeof s - off, "%s%s", comma, 257 attributes_tostring(gc->attr)); 258 comma = ","; 259 } 260 261 if (*s == '\0') 262 return ("default"); 263 return (s); 264 } 265 266 /* Apply a style. */ 267 void 268 style_apply(struct grid_cell *gc, struct options *oo, const char *name) 269 { 270 struct style *sy; 271 272 memcpy(gc, &grid_default_cell, sizeof *gc); 273 sy = options_get_style(oo, name); 274 gc->fg = sy->gc.fg; 275 gc->bg = sy->gc.bg; 276 gc->attr |= sy->gc.attr; 277 } 278 279 /* Initialize style from cell. */ 280 void 281 style_set(struct style *sy, const struct grid_cell *gc) 282 { 283 memcpy(sy, &style_default, sizeof *sy); 284 memcpy(&sy->gc, gc, sizeof sy->gc); 285 } 286 287 /* Copy style. */ 288 void 289 style_copy(struct style *dst, struct style *src) 290 { 291 memcpy(dst, src, sizeof *dst); 292 } 293 294 /* Check if two styles are (visibly) the same. */ 295 int 296 style_equal(struct style *sy1, struct style *sy2) 297 { 298 struct grid_cell *gc1 = &sy1->gc; 299 struct grid_cell *gc2 = &sy2->gc; 300 301 if (gc1->fg != gc2->fg) 302 return (0); 303 if (gc1->bg != gc2->bg) 304 return (0); 305 if ((gc1->attr & STYLE_ATTR_MASK) != (gc2->attr & STYLE_ATTR_MASK)) 306 return (0); 307 if (sy1->fill != sy2->fill) 308 return (0); 309 if (sy1->align != sy2->align) 310 return (0); 311 return (1); 312 } 313 314 /* Is this style default? */ 315 int 316 style_is_default(struct style *sy) 317 { 318 return (style_equal(sy, &style_default)); 319 } 320