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