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