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