1/* $OpenBSD: style.c,v 1.34 2024/01/22 16:34:46 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 (~0) 30 31/* Default style. */ 32static struct style style_default = { 33 { { { ' ' }, 0, 1, 1 }, 0, 0, 8, 8, 0, 0 }, 34 0, 35 36 8, 37 STYLE_ALIGN_DEFAULT, 38 STYLE_LIST_OFF, 39 40 STYLE_RANGE_NONE, 0, "", 41 42 STYLE_DEFAULT_BASE 43}; 44 45/* Set range string. */ 46static void 47style_set_range_string(struct style *sy, const char *s) 48{ 49 strlcpy(sy->range_string, s, sizeof sy->range_string); 50} 51 52/* 53 * Parse an embedded style of the form "fg=colour,bg=colour,bright,...". Note 54 * that this adds onto the given style, so it must have been initialized 55 * already. 56 */ 57int 58style_parse(struct style *sy, const struct grid_cell *base, const char *in) 59{ 60 struct style saved; 61 const char delimiters[] = " ,\n", *cp; 62 char tmp[256], *found; 63 int value; 64 size_t end; 65 66 if (*in == '\0') 67 return (0); 68 style_copy(&saved, sy); 69 70 log_debug("%s: %s", __func__, in); 71 do { 72 while (*in != '\0' && strchr(delimiters, *in) != NULL) 73 in++; 74 if (*in == '\0') 75 break; 76 77 end = strcspn(in, delimiters); 78 if (end > (sizeof tmp) - 1) 79 goto error; 80 memcpy(tmp, in, end); 81 tmp[end] = '\0'; 82 83 log_debug("%s: %s", __func__, tmp); 84 if (strcasecmp(tmp, "default") == 0) { 85 sy->gc.fg = base->fg; 86 sy->gc.bg = base->bg; 87 sy->gc.us = base->us; 88 sy->gc.attr = base->attr; 89 sy->gc.flags = base->flags; 90 } else if (strcasecmp(tmp, "ignore") == 0) 91 sy->ignore = 1; 92 else if (strcasecmp(tmp, "noignore") == 0) 93 sy->ignore = 0; 94 else if (strcasecmp(tmp, "push-default") == 0) 95 sy->default_type = STYLE_DEFAULT_PUSH; 96 else if (strcasecmp(tmp, "pop-default") == 0) 97 sy->default_type = STYLE_DEFAULT_POP; 98 else if (strcasecmp(tmp, "nolist") == 0) 99 sy->list = STYLE_LIST_OFF; 100 else if (strncasecmp(tmp, "list=", 5) == 0) { 101 if (strcasecmp(tmp + 5, "on") == 0) 102 sy->list = STYLE_LIST_ON; 103 else if (strcasecmp(tmp + 5, "focus") == 0) 104 sy->list = STYLE_LIST_FOCUS; 105 else if (strcasecmp(tmp + 5, "left-marker") == 0) 106 sy->list = STYLE_LIST_LEFT_MARKER; 107 else if (strcasecmp(tmp + 5, "right-marker") == 0) 108 sy->list = STYLE_LIST_RIGHT_MARKER; 109 else 110 goto error; 111 } else if (strcasecmp(tmp, "norange") == 0) { 112 sy->range_type = style_default.range_type; 113 sy->range_argument = style_default.range_type; 114 strlcpy(sy->range_string, style_default.range_string, 115 sizeof sy->range_string); 116 } else if (end > 6 && strncasecmp(tmp, "range=", 6) == 0) { 117 found = strchr(tmp + 6, '|'); 118 if (found != NULL) { 119 *found++ = '\0'; 120 if (*found == '\0') 121 goto error; 122 } 123 if (strcasecmp(tmp + 6, "left") == 0) { 124 if (found != NULL) 125 goto error; 126 sy->range_type = STYLE_RANGE_LEFT; 127 sy->range_argument = 0; 128 style_set_range_string(sy, ""); 129 } else if (strcasecmp(tmp + 6, "right") == 0) { 130 if (found != NULL) 131 goto error; 132 sy->range_type = STYLE_RANGE_RIGHT; 133 sy->range_argument = 0; 134 style_set_range_string(sy, ""); 135 } else if (strcasecmp(tmp + 6, "pane") == 0) { 136 if (found == NULL) 137 goto error; 138 if (*found != '%' || found[1] == '\0') 139 goto error; 140 for (cp = found + 1; *cp != '\0'; cp++) { 141 if (!isdigit((u_char)*cp)) 142 goto error; 143 } 144 sy->range_type = STYLE_RANGE_PANE; 145 sy->range_argument = atoi(found + 1); 146 style_set_range_string(sy, ""); 147 } else if (strcasecmp(tmp + 6, "window") == 0) { 148 if (found == NULL) 149 goto error; 150 for (cp = found; *cp != '\0'; cp++) { 151 if (!isdigit((u_char)*cp)) 152 goto error; 153 } 154 sy->range_type = STYLE_RANGE_WINDOW; 155 sy->range_argument = atoi(found); 156 style_set_range_string(sy, ""); 157 } else if (strcasecmp(tmp + 6, "session") == 0) { 158 if (found == NULL) 159 goto error; 160 if (*found != '$' || found[1] == '\0') 161 goto error; 162 for (cp = found + 1; *cp != '\0'; cp++) { 163 if (!isdigit((u_char)*cp)) 164 goto error; 165 } 166 sy->range_type = STYLE_RANGE_SESSION; 167 sy->range_argument = atoi(found + 1); 168 style_set_range_string(sy, ""); 169 } else if (strcasecmp(tmp + 6, "user") == 0) { 170 if (found == NULL) 171 goto error; 172 sy->range_type = STYLE_RANGE_USER; 173 sy->range_argument = 0; 174 style_set_range_string(sy, found); 175 } 176 } else if (strcasecmp(tmp, "noalign") == 0) 177 sy->align = style_default.align; 178 else if (end > 6 && strncasecmp(tmp, "align=", 6) == 0) { 179 if (strcasecmp(tmp + 6, "left") == 0) 180 sy->align = STYLE_ALIGN_LEFT; 181 else if (strcasecmp(tmp + 6, "centre") == 0) 182 sy->align = STYLE_ALIGN_CENTRE; 183 else if (strcasecmp(tmp + 6, "right") == 0) 184 sy->align = STYLE_ALIGN_RIGHT; 185 else if (strcasecmp(tmp + 6, "absolute-centre") == 0) 186 sy->align = STYLE_ALIGN_ABSOLUTE_CENTRE; 187 else 188 goto error; 189 } else if (end > 5 && strncasecmp(tmp, "fill=", 5) == 0) { 190 if ((value = colour_fromstring(tmp + 5)) == -1) 191 goto error; 192 sy->fill = value; 193 } else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) { 194 if ((value = colour_fromstring(tmp + 3)) == -1) 195 goto error; 196 if (*in == 'f' || *in == 'F') { 197 if (value != 8) 198 sy->gc.fg = value; 199 else 200 sy->gc.fg = base->fg; 201 } else if (*in == 'b' || *in == 'B') { 202 if (value != 8) 203 sy->gc.bg = value; 204 else 205 sy->gc.bg = base->bg; 206 } else 207 goto error; 208 } else if (end > 3 && strncasecmp(tmp, "us=", 3) == 0) { 209 if ((value = colour_fromstring(tmp + 3)) == -1) 210 goto error; 211 if (value != 8) 212 sy->gc.us = value; 213 else 214 sy->gc.us = base->us; 215 } else if (strcasecmp(tmp, "none") == 0) 216 sy->gc.attr = 0; 217 else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) { 218 if ((value = attributes_fromstring(tmp + 2)) == -1) 219 goto error; 220 sy->gc.attr &= ~value; 221 } else { 222 if ((value = attributes_fromstring(tmp)) == -1) 223 goto error; 224 sy->gc.attr |= value; 225 } 226 227 in += end + strspn(in + end, delimiters); 228 } while (*in != '\0'); 229 230 return (0); 231 232error: 233 style_copy(sy, &saved); 234 return (-1); 235} 236 237/* Convert style to a string. */ 238const char * 239style_tostring(struct style *sy) 240{ 241 struct grid_cell *gc = &sy->gc; 242 int off = 0; 243 const char *comma = "", *tmp = ""; 244 static char s[256]; 245 char b[21]; 246 247 *s = '\0'; 248 249 if (sy->list != STYLE_LIST_OFF) { 250 if (sy->list == STYLE_LIST_ON) 251 tmp = "on"; 252 else if (sy->list == STYLE_LIST_FOCUS) 253 tmp = "focus"; 254 else if (sy->list == STYLE_LIST_LEFT_MARKER) 255 tmp = "left-marker"; 256 else if (sy->list == STYLE_LIST_RIGHT_MARKER) 257 tmp = "right-marker"; 258 off += xsnprintf(s + off, sizeof s - off, "%slist=%s", comma, 259 tmp); 260 comma = ","; 261 } 262 if (sy->range_type != STYLE_RANGE_NONE) { 263 if (sy->range_type == STYLE_RANGE_LEFT) 264 tmp = "left"; 265 else if (sy->range_type == STYLE_RANGE_RIGHT) 266 tmp = "right"; 267 else if (sy->range_type == STYLE_RANGE_PANE) { 268 snprintf(b, sizeof b, "pane|%%%u", sy->range_argument); 269 tmp = b; 270 } else if (sy->range_type == STYLE_RANGE_WINDOW) { 271 snprintf(b, sizeof b, "window|%u", sy->range_argument); 272 tmp = b; 273 } else if (sy->range_type == STYLE_RANGE_SESSION) { 274 snprintf(b, sizeof b, "session|$%u", 275 sy->range_argument); 276 tmp = b; 277 } else if (sy->range_type == STYLE_RANGE_USER) { 278 snprintf(b, sizeof b, "user|%s", sy->range_string); 279 tmp = b; 280 } 281 off += xsnprintf(s + off, sizeof s - off, "%srange=%s", comma, 282 tmp); 283 comma = ","; 284 } 285 if (sy->align != STYLE_ALIGN_DEFAULT) { 286 if (sy->align == STYLE_ALIGN_LEFT) 287 tmp = "left"; 288 else if (sy->align == STYLE_ALIGN_CENTRE) 289 tmp = "centre"; 290 else if (sy->align == STYLE_ALIGN_RIGHT) 291 tmp = "right"; 292 else if (sy->align == STYLE_ALIGN_ABSOLUTE_CENTRE) 293 tmp = "absolute-centre"; 294 off += xsnprintf(s + off, sizeof s - off, "%salign=%s", comma, 295 tmp); 296 comma = ","; 297 } 298 if (sy->default_type != STYLE_DEFAULT_BASE) { 299 if (sy->default_type == STYLE_DEFAULT_PUSH) 300 tmp = "push-default"; 301 else if (sy->default_type == STYLE_DEFAULT_POP) 302 tmp = "pop-default"; 303 off += xsnprintf(s + off, sizeof s - off, "%s%s", comma, tmp); 304 comma = ","; 305 } 306 if (sy->fill != 8) { 307 off += xsnprintf(s + off, sizeof s - off, "%sfill=%s", comma, 308 colour_tostring(sy->fill)); 309 comma = ","; 310 } 311 if (gc->fg != 8) { 312 off += xsnprintf(s + off, sizeof s - off, "%sfg=%s", comma, 313 colour_tostring(gc->fg)); 314 comma = ","; 315 } 316 if (gc->bg != 8) { 317 off += xsnprintf(s + off, sizeof s - off, "%sbg=%s", comma, 318 colour_tostring(gc->bg)); 319 comma = ","; 320 } 321 if (gc->us != 8) { 322 off += xsnprintf(s + off, sizeof s - off, "%sus=%s", comma, 323 colour_tostring(gc->us)); 324 comma = ","; 325 } 326 if (gc->attr != 0) { 327 xsnprintf(s + off, sizeof s - off, "%s%s", comma, 328 attributes_tostring(gc->attr)); 329 comma = ","; 330 } 331 332 if (*s == '\0') 333 return ("default"); 334 return (s); 335} 336 337/* Apply a style on top of the given style. */ 338void 339style_add(struct grid_cell *gc, struct options *oo, const char *name, 340 struct format_tree *ft) 341{ 342 struct style *sy; 343 struct format_tree *ft0 = NULL; 344 345 if (ft == NULL) 346 ft = ft0 = format_create(NULL, NULL, 0, FORMAT_NOJOBS); 347 348 sy = options_string_to_style(oo, name, ft); 349 if (sy == NULL) 350 sy = &style_default; 351 if (sy->gc.fg != 8) 352 gc->fg = sy->gc.fg; 353 if (sy->gc.bg != 8) 354 gc->bg = sy->gc.bg; 355 if (sy->gc.us != 8) 356 gc->us = sy->gc.us; 357 gc->attr |= sy->gc.attr; 358 359 if (ft0 != NULL) 360 format_free(ft0); 361} 362 363/* Apply a style on top of the default style. */ 364void 365style_apply(struct grid_cell *gc, struct options *oo, const char *name, 366 struct format_tree *ft) 367{ 368 memcpy(gc, &grid_default_cell, sizeof *gc); 369 style_add(gc, oo, name, ft); 370} 371 372/* Initialize style from cell. */ 373void 374style_set(struct style *sy, const struct grid_cell *gc) 375{ 376 memcpy(sy, &style_default, sizeof *sy); 377 memcpy(&sy->gc, gc, sizeof sy->gc); 378} 379 380/* Copy style. */ 381void 382style_copy(struct style *dst, struct style *src) 383{ 384 memcpy(dst, src, sizeof *dst); 385} 386