1 /****************************************************************************
2 * Copyright 2020-2023,2024 Thomas E. Dickey *
3 * Copyright 2005-2012,2017 Free Software Foundation, Inc. *
4 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
29
30 /****************************************************************************
31 * Author: Thomas Dickey *
32 ****************************************************************************/
33
34 #include <curses.priv.h>
35
36 #include <ctype.h>
37
38 #include <tic.h>
39
40 MODULE_ID("$Id: trim_sgr0.c,v 1.27 2024/12/21 20:15:26 tom Exp $")
41
42 #undef CUR
43 #define CUR tp->
44
45 /*
46 * Skip a padding token, e.g., "<5>", "<5.1>", "<5/>", "<5*>", or "<5/>".
47 * If the pattern does not match, return null.
48 */
49 static char *
skip_padding(char * value)50 skip_padding(char *value)
51 {
52 char *result = NULL;
53 if (*value++ == '$' && *value++ == '<') {
54 int ch;
55 int state = 0; /* 1=integer, 2=decimal, 3=fraction */
56 while ((ch = UChar(*value++)) != '\0') {
57 if (ch == '*' || ch == '/') {
58 if (!state)
59 break;
60 } else if (ch == '>') {
61 if (state)
62 result = value;
63 break;
64 } else if (ch == '.') {
65 if (state < 2) {
66 state = 2;
67 } else {
68 break; /* a single decimal point is allowed */
69 }
70 } else if (isdigit(ch)) {
71 if (state < 2) {
72 state = 1;
73 } else if (state == 2) {
74 state = 3;
75 } else {
76 break; /* only a single digit after decimal point */
77 }
78 } else {
79 break;
80 }
81 }
82 }
83 return result;
84 }
85
86 static void
strip_padding(char * value)87 strip_padding(char *value)
88 {
89 char *s = value;
90 char ch;
91
92 while ((ch = *s) != '\0') {
93 if (ch == '\\') {
94 if (*++s == '\0')
95 break;
96 ++s;
97 } else {
98 char *d = NULL;
99 if (ch == '$')
100 d = skip_padding(s);
101 if (d != NULL) {
102 char *t = s;
103 while ((*t++ = *d++) != '\0') ;
104 } else {
105 ++s;
106 }
107 }
108 }
109 }
110
111 static char *
set_attribute_9(const TERMTYPE2 * tp,int flag)112 set_attribute_9(const TERMTYPE2 *tp, int flag)
113 {
114 const char *value;
115 char *result;
116
117 value = TIPARM_9(set_attributes, 0, 0, 0, 0, 0, 0, 0, 0, flag);
118 if (PRESENT(value)) {
119 result = strdup(value);
120 if (result != NULL)
121 strip_padding(result);
122 } else {
123 result = NULL;
124 }
125 return result;
126 }
127
128 static int
is_csi(const char * s)129 is_csi(const char *s)
130 {
131 int result = 0;
132 if (s != NULL) {
133 if (UChar(s[0]) == CSI_CHR)
134 result = 1;
135 else if (s[0] == ESC_CHR && s[1] == L_BLOCK)
136 result = 2;
137 }
138 return result;
139 }
140
141 static char *
skip_zero(char * s)142 skip_zero(char *s)
143 {
144 if (s[0] == '0') {
145 if (s[1] == ';')
146 s += 2;
147 else if (isalpha(UChar(s[1])))
148 s += 1;
149 }
150 return s;
151 }
152
153 static const char *
skip_delay(const char * s)154 skip_delay(const char *s)
155 {
156 if (s[0] == '$' && s[1] == '<') {
157 s += 2;
158 while (isdigit(UChar(*s)) || *s == '/')
159 ++s;
160 if (*s == '>')
161 ++s;
162 }
163 return s;
164 }
165
166 /*
167 * Improve similar_sgr a little by moving the attr-string from the beginning
168 * to the end of the s-string.
169 */
170 static bool
rewrite_sgr(char * s,const char * attr)171 rewrite_sgr(char *s, const char *attr)
172 {
173 if (s != NULL) {
174 if (PRESENT(attr)) {
175 size_t len_s = strlen(s);
176 size_t len_a = strlen(attr);
177
178 if (len_s > len_a && !strncmp(attr, s, len_a)) {
179 unsigned n;
180 TR(TRACE_DATABASE, ("rewrite:\n\t%s", s));
181 for (n = 0; n < len_s - len_a; ++n) {
182 s[n] = s[n + len_a];
183 }
184 _nc_STRCPY(s + n, attr, strlen(s) + 1);
185 TR(TRACE_DATABASE, ("to:\n\t%s", s));
186 }
187 }
188 return TRUE;
189 }
190 return FALSE; /* oops */
191 }
192
193 static bool
similar_sgr(char * a,char * b)194 similar_sgr(char *a, char *b)
195 {
196 bool result = FALSE;
197 if (a != NULL && b != NULL) {
198 int csi_a = is_csi(a);
199 int csi_b = is_csi(b);
200 size_t len_a;
201 size_t len_b;
202
203 TR(TRACE_DATABASE, ("similar_sgr:\n\t%s\n\t%s",
204 _nc_visbuf2(1, a),
205 _nc_visbuf2(2, b)));
206 if (csi_a != 0 && csi_b != 0 && csi_a == csi_b) {
207 a += csi_a;
208 b += csi_b;
209 if (*a != *b) {
210 a = skip_zero(a);
211 b = skip_zero(b);
212 }
213 }
214 len_a = strlen(a);
215 len_b = strlen(b);
216 if (len_a && len_b) {
217 if (len_a > len_b)
218 result = (strncmp(a, b, len_b) == 0);
219 else
220 result = (strncmp(a, b, len_a) == 0);
221 }
222 TR(TRACE_DATABASE, ("...similar_sgr: %d\n\t%s\n\t%s", result,
223 _nc_visbuf2(1, a),
224 _nc_visbuf2(2, b)));
225 }
226 return result;
227 }
228
229 static unsigned
chop_out(char * string,unsigned i,unsigned j)230 chop_out(char *string, unsigned i, unsigned j)
231 {
232 TR(TRACE_DATABASE, ("chop_out %d..%d from %s", i, j, _nc_visbuf(string)));
233 while (string[j] != '\0') {
234 string[i++] = string[j++];
235 }
236 string[i] = '\0';
237 return i;
238 }
239
240 /*
241 * Compare, ignoring delays. Some of the delay values are inconsistent, and
242 * we do not want to be stopped by that.
243 *
244 * Returns the number of chars from 'full' that we matched. If any mismatch
245 * occurs, return zero.
246 */
247 static unsigned
compare_part(const char * part,const char * full)248 compare_part(const char *part, const char *full)
249 {
250 const char *next_part;
251 const char *next_full;
252 unsigned used_full = 0;
253 unsigned used_delay = 0;
254
255 while (*part != 0) {
256 if (*part != *full) {
257 used_full = 0;
258 break;
259 }
260
261 /*
262 * Adjust the return-value to allow the rare case of
263 * string<delay>string
264 * to remove the whole piece. The most common case is a delay at the
265 * end of the string. The adjusted string will retain the delay, which
266 * is conservative.
267 */
268 if (used_delay != 0) {
269 used_full += used_delay;
270 used_delay = 0;
271 }
272 if (*part == '$' && *full == '$') {
273 next_part = skip_delay(part);
274 next_full = skip_delay(full);
275 if (next_part != part && next_full != full) {
276 used_delay += (unsigned) (next_full - full);
277 full = next_full;
278 part = next_part;
279 continue;
280 }
281 }
282 ++used_full;
283 ++part;
284 ++full;
285 }
286 return used_full;
287 }
288
289 /*
290 * While 'sgr0' is the "same" as termcap 'me', there is a compatibility issue.
291 * The sgr/sgr0 capabilities include setting/clearing alternate character set
292 * mode. A termcap application cannot use sgr, so sgr0 strings that reset
293 * alternate character set mode will be misinterpreted. Here, we remove those
294 * from the more common ISO/ANSI/VT100 entries, which have sgr0 agreeing with
295 * sgr.
296 *
297 * This function returns the modified sgr0 if it can be modified, a null if
298 * an error occurs, or the original sgr0 if no change is needed.
299 */
300 NCURSES_EXPORT(char *)
_nc_trim_sgr0(TERMTYPE2 * tp)301 _nc_trim_sgr0(TERMTYPE2 *tp)
302 {
303 char *result = exit_attribute_mode;
304
305 T((T_CALLED("_nc_trim_sgr0()")));
306
307 if (PRESENT(exit_attribute_mode)
308 && PRESENT(set_attributes)) {
309 char *on = set_attribute_9(tp, 1);
310 char *off = set_attribute_9(tp, 0);
311 char *end = strdup(exit_attribute_mode);
312 char *tmp;
313
314 TR(TRACE_DATABASE, ("checking if we can trim sgr0 based on sgr"));
315 TR(TRACE_DATABASE, ("sgr0 %s", _nc_visbuf(end)));
316 TR(TRACE_DATABASE, ("sgr(9:off) %s", _nc_visbuf(off)));
317 TR(TRACE_DATABASE, ("sgr(9:on) %s", _nc_visbuf(on)));
318
319 if (!rewrite_sgr(on, enter_alt_charset_mode)
320 || !rewrite_sgr(off, exit_alt_charset_mode)
321 || !rewrite_sgr(end, exit_alt_charset_mode)) {
322 FreeIfNeeded(off);
323 } else if (similar_sgr(off, end)
324 && !similar_sgr(off, on)) {
325 bool found = FALSE;
326 size_t i, j;
327
328 TR(TRACE_DATABASE, ("adjusting sgr(9:off) : %s", _nc_visbuf(off)));
329 result = off;
330 /*
331 * If rmacs is a substring of sgr(0), remove that chunk.
332 */
333 if (PRESENT(exit_alt_charset_mode)) {
334 size_t k;
335
336 TR(TRACE_DATABASE, ("scan for rmacs %s", _nc_visbuf(exit_alt_charset_mode)));
337 j = strlen(off);
338 k = strlen(exit_alt_charset_mode);
339 if (j > k) {
340 for (i = 0; i <= (j - k); ++i) {
341 unsigned k2 = compare_part(exit_alt_charset_mode,
342 off + i);
343 if (k2 != 0) {
344 found = TRUE;
345 chop_out(off, (unsigned) i, (unsigned) (i + k2));
346 break;
347 }
348 }
349 }
350 }
351 /*
352 * SGR 10 would reset to normal font.
353 */
354 if (!found) {
355 if ((i = (size_t) is_csi(off)) != 0
356 && off[strlen(off) - 1] == 'm') {
357 TR(TRACE_DATABASE, ("looking for SGR 10 in %s",
358 _nc_visbuf(off)));
359 tmp = skip_zero(off + i);
360 if (tmp[0] == '1'
361 && skip_zero(tmp + 1) != tmp + 1) {
362 i = (size_t) (tmp - off);
363 if (off[i - 1] == ';')
364 i--;
365 j = (size_t) (skip_zero(tmp + 1) - off);
366 (void) chop_out(off, (unsigned) i, (unsigned) j);
367 found = TRUE;
368 }
369 }
370 }
371 if (!found
372 && (tmp = strstr(end, off)) != NULL
373 && strcmp(end, off) != 0) {
374 i = (size_t) (tmp - end);
375 j = strlen(off);
376 tmp = strdup(end);
377 chop_out(tmp, (unsigned) i, (unsigned) j);
378 free(off);
379 result = tmp;
380 }
381 TR(TRACE_DATABASE, ("...adjusted sgr0 : %s", _nc_visbuf(result)));
382 if (!strcmp(result, exit_attribute_mode)) {
383 TR(TRACE_DATABASE, ("...same result, discard"));
384 free(result);
385 result = exit_attribute_mode;
386 }
387 } else {
388 /*
389 * Either the sgr does not reference alternate character set,
390 * or it is incorrect. That's too hard to decide right now.
391 */
392 free(off);
393 }
394 FreeIfNeeded(end);
395 FreeIfNeeded(on);
396 } else {
397 /*
398 * Possibly some applications are confused if sgr0 contains rmacs,
399 * but that would be a different bug report -TD
400 */
401 }
402
403 returnPtr(result);
404 }
405