1 /* $OpenBSD: style.c,v 1.19 2019/04/05 20:32:31 nicm Exp $ */ 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, 0, 8, 8, { { ' ' }, 0, 1, 1 } }, 34 35 STYLE_ALIGN_DEFAULT, 36 STYLE_LIST_OFF, 37 38 STYLE_RANGE_NONE, 0 39 }; 40 41 /* 42 * Parse an embedded style of the form "fg=colour,bg=colour,bright,...". 43 * Note that this adds onto the given style, so it must have been initialized 44 * alredy. 45 */ 46 int 47 style_parse(struct style *sy, const struct grid_cell *base, const char *in) 48 { 49 struct style saved; 50 const char delimiters[] = " ,", *cp; 51 char tmp[256], *found; 52 int value; 53 size_t end; 54 55 if (*in == '\0') 56 return (0); 57 style_copy(&saved, sy); 58 59 do { 60 while (*in != '\0' && strchr(delimiters, *in) != NULL) { 61 in++; 62 end--; 63 } 64 if (*in == '\0') 65 break; 66 67 end = strcspn(in, delimiters); 68 if (end > (sizeof tmp) - 1) 69 goto error; 70 memcpy(tmp, in, end); 71 tmp[end] = '\0'; 72 73 if (strcasecmp(tmp, "default") == 0) { 74 sy->gc.fg = base->fg; 75 sy->gc.bg = base->bg; 76 sy->gc.attr = base->attr; 77 sy->gc.flags = base->flags; 78 } else if (strcasecmp(tmp, "nolist") == 0) 79 sy->list = STYLE_LIST_OFF; 80 else if (strncasecmp(tmp, "list=", 5) == 0) { 81 if (strcasecmp(tmp + 5, "on") == 0) 82 sy->list = STYLE_LIST_ON; 83 else if (strcasecmp(tmp + 5, "focus") == 0) 84 sy->list = STYLE_LIST_FOCUS; 85 else if (strcasecmp(tmp + 5, "left-marker") == 0) 86 sy->list = STYLE_LIST_LEFT_MARKER; 87 else if (strcasecmp(tmp + 5, "right-marker") == 0) 88 sy->list = STYLE_LIST_RIGHT_MARKER; 89 else 90 goto error; 91 } else if (strcasecmp(tmp, "norange") == 0) { 92 sy->range_type = style_default.range_type; 93 sy->range_argument = style_default.range_type; 94 } else if (end > 6 && strncasecmp(tmp, "range=", 6) == 0) { 95 found = strchr(tmp + 6, '|'); 96 if (found != NULL) { 97 *found++ = '\0'; 98 if (*found == '\0') 99 goto error; 100 for (cp = found; *cp != '\0'; cp++) { 101 if (!isdigit((u_char)*cp)) 102 goto error; 103 } 104 } 105 if (strcasecmp(tmp + 6, "left") == 0) { 106 if (found != NULL) 107 goto error; 108 sy->range_type = STYLE_RANGE_LEFT; 109 sy->range_argument = 0; 110 } else if (strcasecmp(tmp + 6, "right") == 0) { 111 if (found != NULL) 112 goto error; 113 sy->range_type = STYLE_RANGE_RIGHT; 114 sy->range_argument = 0; 115 } else if (strcasecmp(tmp + 6, "window") == 0) { 116 if (found == NULL) 117 goto error; 118 sy->range_type = STYLE_RANGE_WINDOW; 119 sy->range_argument = atoi(found); 120 } 121 } else if (strcasecmp(tmp, "noalign") == 0) 122 sy->align = style_default.align; 123 else if (end > 6 && strncasecmp(tmp, "align=", 6) == 0) { 124 if (strcasecmp(tmp + 6, "left") == 0) 125 sy->align = STYLE_ALIGN_LEFT; 126 else if (strcasecmp(tmp + 6, "centre") == 0) 127 sy->align = STYLE_ALIGN_CENTRE; 128 else if (strcasecmp(tmp + 6, "right") == 0) 129 sy->align = STYLE_ALIGN_RIGHT; 130 else 131 goto error; 132 } else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) { 133 if ((value = colour_fromstring(tmp + 3)) == -1) 134 goto error; 135 if (*in == 'f' || *in == 'F') { 136 if (value != 8) 137 sy->gc.fg = value; 138 else 139 sy->gc.fg = base->fg; 140 } else if (*in == 'b' || *in == 'B') { 141 if (value != 8) 142 sy->gc.bg = value; 143 else 144 sy->gc.bg = base->bg; 145 } else 146 goto error; 147 } else if (strcasecmp(tmp, "none") == 0) 148 sy->gc.attr = 0; 149 else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) { 150 if ((value = attributes_fromstring(tmp + 2)) == -1) 151 goto error; 152 sy->gc.attr &= ~value; 153 } else { 154 if ((value = attributes_fromstring(tmp)) == -1) 155 goto error; 156 sy->gc.attr |= value; 157 } 158 159 in += end + strspn(in + end, delimiters); 160 } while (*in != '\0'); 161 162 return (0); 163 164 error: 165 style_copy(sy, &saved); 166 return (-1); 167 } 168 169 /* Convert style to a string. */ 170 const char * 171 style_tostring(struct style *sy) 172 { 173 struct grid_cell *gc = &sy->gc; 174 int off = 0; 175 const char *comma = "", *tmp = ""; 176 static char s[256]; 177 char b[16]; 178 179 *s = '\0'; 180 181 if (sy->list != STYLE_LIST_OFF) { 182 if (sy->list == STYLE_LIST_ON) 183 tmp = "on"; 184 else if (sy->list == STYLE_LIST_FOCUS) 185 tmp = "focus"; 186 else if (sy->list == STYLE_LIST_LEFT_MARKER) 187 tmp = "left-marker"; 188 else if (sy->list == STYLE_LIST_RIGHT_MARKER) 189 tmp = "right-marker"; 190 off += xsnprintf(s + off, sizeof s - off, "%slist=%s", comma, 191 tmp); 192 comma = ","; 193 } 194 if (sy->range_type != STYLE_RANGE_NONE) { 195 if (sy->range_type == STYLE_RANGE_LEFT) 196 tmp = "left"; 197 else if (sy->range_type == STYLE_RANGE_RIGHT) 198 tmp = "right"; 199 else if (sy->range_type == STYLE_RANGE_WINDOW) { 200 snprintf(b, sizeof b, "window|%u", sy->range_argument); 201 tmp = b; 202 } 203 off += xsnprintf(s + off, sizeof s - off, "%srange=%s", comma, 204 tmp); 205 comma = ","; 206 } 207 if (sy->align != STYLE_ALIGN_DEFAULT) { 208 if (sy->align == STYLE_ALIGN_LEFT) 209 tmp = "left"; 210 else if (sy->align == STYLE_ALIGN_CENTRE) 211 tmp = "centre"; 212 else if (sy->align == STYLE_ALIGN_RIGHT) 213 tmp = "right"; 214 off += xsnprintf(s + off, sizeof s - off, "%salign=%s", comma, 215 tmp); 216 comma = ","; 217 } 218 if (gc->fg != 8) { 219 off += xsnprintf(s + off, sizeof s - off, "%sfg=%s", comma, 220 colour_tostring(gc->fg)); 221 comma = ","; 222 } 223 if (gc->bg != 8) { 224 off += xsnprintf(s + off, sizeof s - off, "%sbg=%s", comma, 225 colour_tostring(gc->bg)); 226 comma = ","; 227 } 228 if (gc->attr != 0 && gc->attr != GRID_ATTR_CHARSET) { 229 xsnprintf(s + off, sizeof s - off, "%s%s", comma, 230 attributes_tostring(gc->attr)); 231 comma = ","; 232 } 233 234 if (*s == '\0') 235 return ("default"); 236 return (s); 237 } 238 239 /* Apply a style. */ 240 void 241 style_apply(struct grid_cell *gc, struct options *oo, const char *name) 242 { 243 struct style *sy; 244 245 memcpy(gc, &grid_default_cell, sizeof *gc); 246 sy = options_get_style(oo, name); 247 gc->fg = sy->gc.fg; 248 gc->bg = sy->gc.bg; 249 gc->attr |= sy->gc.attr; 250 } 251 252 /* Apply a style, updating if default. */ 253 void 254 style_apply_update(struct grid_cell *gc, struct options *oo, const char *name) 255 { 256 struct style *sy; 257 258 sy = options_get_style(oo, name); 259 if (sy->gc.fg != 8) 260 gc->fg = sy->gc.fg; 261 if (sy->gc.bg != 8) 262 gc->bg = sy->gc.bg; 263 if (sy->gc.attr != 0) 264 gc->attr |= sy->gc.attr; 265 } 266 267 /* Initialize style from cell. */ 268 void 269 style_set(struct style *sy, const struct grid_cell *gc) 270 { 271 memcpy(sy, &style_default, sizeof *sy); 272 memcpy(&sy->gc, gc, sizeof sy->gc); 273 } 274 275 /* Copy style. */ 276 void 277 style_copy(struct style *dst, struct style *src) 278 { 279 memcpy(dst, src, sizeof *dst); 280 } 281 282 /* Check if two styles are (visibly) the same. */ 283 int 284 style_equal(struct style *sy1, struct style *sy2) 285 { 286 struct grid_cell *gc1 = &sy1->gc; 287 struct grid_cell *gc2 = &sy2->gc; 288 289 if (gc1->fg != gc2->fg) 290 return (0); 291 if (gc1->bg != gc2->bg) 292 return (0); 293 if ((gc1->attr & STYLE_ATTR_MASK) != (gc2->attr & STYLE_ATTR_MASK)) 294 return (0); 295 if (sy1->align != sy2->align) 296 return (0); 297 return (1); 298 } 299 300 /* Is this style default? */ 301 int 302 style_is_default(struct style *sy) 303 { 304 return (style_equal(sy, &style_default)); 305 } 306