xref: /freebsd/contrib/ncurses/ncurses/tinfo/trim_sgr0.c (revision 21817992b3314c908ab50f0bb88d2ee750b9c4ac)
1 /****************************************************************************
2  * Copyright 2020-2021,2023 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.22 2023/09/23 18:47:56 tom Exp $")
41 
42 #undef CUR
43 #define CUR tp->
44 
45 static char *
set_attribute_9(TERMTYPE2 * tp,int flag)46 set_attribute_9(TERMTYPE2 *tp, int flag)
47 {
48     const char *value;
49     char *result;
50 
51     value = TIPARM_9(set_attributes, 0, 0, 0, 0, 0, 0, 0, 0, flag);
52     if (PRESENT(value))
53 	result = strdup(value);
54     else
55 	result = 0;
56     return result;
57 }
58 
59 static int
is_csi(const char * s)60 is_csi(const char *s)
61 {
62     int result = 0;
63     if (s != 0) {
64 	if (UChar(s[0]) == CSI_CHR)
65 	    result = 1;
66 	else if (s[0] == ESC_CHR && s[1] == L_BLOCK)
67 	    result = 2;
68     }
69     return result;
70 }
71 
72 static char *
skip_zero(char * s)73 skip_zero(char *s)
74 {
75     if (s[0] == '0') {
76 	if (s[1] == ';')
77 	    s += 2;
78 	else if (isalpha(UChar(s[1])))
79 	    s += 1;
80     }
81     return s;
82 }
83 
84 static const char *
skip_delay(const char * s)85 skip_delay(const char *s)
86 {
87     if (s[0] == '$' && s[1] == '<') {
88 	s += 2;
89 	while (isdigit(UChar(*s)) || *s == '/')
90 	    ++s;
91 	if (*s == '>')
92 	    ++s;
93     }
94     return s;
95 }
96 
97 /*
98  * Improve similar_sgr a little by moving the attr-string from the beginning
99  * to the end of the s-string.
100  */
101 static bool
rewrite_sgr(char * s,char * attr)102 rewrite_sgr(char *s, char *attr)
103 {
104     if (s != 0) {
105 	if (PRESENT(attr)) {
106 	    size_t len_s = strlen(s);
107 	    size_t len_a = strlen(attr);
108 
109 	    if (len_s > len_a && !strncmp(attr, s, len_a)) {
110 		unsigned n;
111 		TR(TRACE_DATABASE, ("rewrite:\n\t%s", s));
112 		for (n = 0; n < len_s - len_a; ++n) {
113 		    s[n] = s[n + len_a];
114 		}
115 		_nc_STRCPY(s + n, attr, strlen(s) + 1);
116 		TR(TRACE_DATABASE, ("to:\n\t%s", s));
117 	    }
118 	}
119 	return TRUE;
120     }
121     return FALSE;		/* oops */
122 }
123 
124 static bool
similar_sgr(char * a,char * b)125 similar_sgr(char *a, char *b)
126 {
127     bool result = FALSE;
128     if (a != 0 && b != 0) {
129 	int csi_a = is_csi(a);
130 	int csi_b = is_csi(b);
131 	size_t len_a;
132 	size_t len_b;
133 
134 	TR(TRACE_DATABASE, ("similar_sgr:\n\t%s\n\t%s",
135 			    _nc_visbuf2(1, a),
136 			    _nc_visbuf2(2, b)));
137 	if (csi_a != 0 && csi_b != 0 && csi_a == csi_b) {
138 	    a += csi_a;
139 	    b += csi_b;
140 	    if (*a != *b) {
141 		a = skip_zero(a);
142 		b = skip_zero(b);
143 	    }
144 	}
145 	len_a = strlen(a);
146 	len_b = strlen(b);
147 	if (len_a && len_b) {
148 	    if (len_a > len_b)
149 		result = (strncmp(a, b, len_b) == 0);
150 	    else
151 		result = (strncmp(a, b, len_a) == 0);
152 	}
153 	TR(TRACE_DATABASE, ("...similar_sgr: %d\n\t%s\n\t%s", result,
154 			    _nc_visbuf2(1, a),
155 			    _nc_visbuf2(2, b)));
156     }
157     return result;
158 }
159 
160 static unsigned
chop_out(char * string,unsigned i,unsigned j)161 chop_out(char *string, unsigned i, unsigned j)
162 {
163     TR(TRACE_DATABASE, ("chop_out %d..%d from %s", i, j, _nc_visbuf(string)));
164     while (string[j] != '\0') {
165 	string[i++] = string[j++];
166     }
167     string[i] = '\0';
168     return i;
169 }
170 
171 /*
172  * Compare, ignoring delays.  Some of the delay values are inconsistent, and
173  * we do not want to be stopped by that.
174  *
175  * Returns the number of chars from 'full' that we matched.  If any mismatch
176  * occurs, return zero.
177  */
178 static unsigned
compare_part(const char * part,const char * full)179 compare_part(const char *part, const char *full)
180 {
181     const char *next_part;
182     const char *next_full;
183     unsigned used_full = 0;
184     unsigned used_delay = 0;
185 
186     while (*part != 0) {
187 	if (*part != *full) {
188 	    used_full = 0;
189 	    break;
190 	}
191 
192 	/*
193 	 * Adjust the return-value to allow the rare case of
194 	 *      string<delay>string
195 	 * to remove the whole piece.  The most common case is a delay at the
196 	 * end of the string.  The adjusted string will retain the delay, which
197 	 * is conservative.
198 	 */
199 	if (used_delay != 0) {
200 	    used_full += used_delay;
201 	    used_delay = 0;
202 	}
203 	if (*part == '$' && *full == '$') {
204 	    next_part = skip_delay(part);
205 	    next_full = skip_delay(full);
206 	    if (next_part != part && next_full != full) {
207 		used_delay += (unsigned) (next_full - full);
208 		full = next_full;
209 		part = next_part;
210 		continue;
211 	    }
212 	}
213 	++used_full;
214 	++part;
215 	++full;
216     }
217     return used_full;
218 }
219 
220 /*
221  * While 'sgr0' is the "same" as termcap 'me', there is a compatibility issue.
222  * The sgr/sgr0 capabilities include setting/clearing alternate character set
223  * mode.  A termcap application cannot use sgr, so sgr0 strings that reset
224  * alternate character set mode will be misinterpreted.  Here, we remove those
225  * from the more common ISO/ANSI/VT100 entries, which have sgr0 agreeing with
226  * sgr.
227  *
228  * This function returns the modified sgr0 if it can be modified, a null if
229  * an error occurs, or the original sgr0 if no change is needed.
230  */
231 NCURSES_EXPORT(char *)
_nc_trim_sgr0(TERMTYPE2 * tp)232 _nc_trim_sgr0(TERMTYPE2 *tp)
233 {
234     char *result = exit_attribute_mode;
235 
236     T((T_CALLED("_nc_trim_sgr0()")));
237 
238     if (PRESENT(exit_attribute_mode)
239 	&& PRESENT(set_attributes)) {
240 	bool found = FALSE;
241 	char *on = set_attribute_9(tp, 1);
242 	char *off = set_attribute_9(tp, 0);
243 	char *end = strdup(exit_attribute_mode);
244 	char *tmp;
245 	size_t i, j, k;
246 
247 	TR(TRACE_DATABASE, ("checking if we can trim sgr0 based on sgr"));
248 	TR(TRACE_DATABASE, ("sgr0       %s", _nc_visbuf(end)));
249 	TR(TRACE_DATABASE, ("sgr(9:off) %s", _nc_visbuf(off)));
250 	TR(TRACE_DATABASE, ("sgr(9:on)  %s", _nc_visbuf(on)));
251 
252 	if (!rewrite_sgr(on, enter_alt_charset_mode)
253 	    || !rewrite_sgr(off, exit_alt_charset_mode)
254 	    || !rewrite_sgr(end, exit_alt_charset_mode)) {
255 	    FreeIfNeeded(off);
256 	} else if (similar_sgr(off, end)
257 		   && !similar_sgr(off, on)) {
258 	    TR(TRACE_DATABASE, ("adjusting sgr(9:off) : %s", _nc_visbuf(off)));
259 	    result = off;
260 	    /*
261 	     * If rmacs is a substring of sgr(0), remove that chunk.
262 	     */
263 	    if (PRESENT(exit_alt_charset_mode)) {
264 		TR(TRACE_DATABASE, ("scan for rmacs %s", _nc_visbuf(exit_alt_charset_mode)));
265 		j = strlen(off);
266 		k = strlen(exit_alt_charset_mode);
267 		if (j > k) {
268 		    for (i = 0; i <= (j - k); ++i) {
269 			unsigned k2 = compare_part(exit_alt_charset_mode,
270 						   off + i);
271 			if (k2 != 0) {
272 			    found = TRUE;
273 			    chop_out(off, (unsigned) i, (unsigned) (i + k2));
274 			    break;
275 			}
276 		    }
277 		}
278 	    }
279 	    /*
280 	     * SGR 10 would reset to normal font.
281 	     */
282 	    if (!found) {
283 		if ((i = (size_t) is_csi(off)) != 0
284 		    && off[strlen(off) - 1] == 'm') {
285 		    TR(TRACE_DATABASE, ("looking for SGR 10 in %s",
286 					_nc_visbuf(off)));
287 		    tmp = skip_zero(off + i);
288 		    if (tmp[0] == '1'
289 			&& skip_zero(tmp + 1) != tmp + 1) {
290 			i = (size_t) (tmp - off);
291 			if (off[i - 1] == ';')
292 			    i--;
293 			j = (size_t) (skip_zero(tmp + 1) - off);
294 			(void) chop_out(off, (unsigned) i, (unsigned) j);
295 			found = TRUE;
296 		    }
297 		}
298 	    }
299 	    if (!found
300 		&& (tmp = strstr(end, off)) != 0
301 		&& strcmp(end, off) != 0) {
302 		i = (size_t) (tmp - end);
303 		j = strlen(off);
304 		tmp = strdup(end);
305 		chop_out(tmp, (unsigned) i, (unsigned) j);
306 		free(off);
307 		result = tmp;
308 	    }
309 	    TR(TRACE_DATABASE, ("...adjusted sgr0 : %s", _nc_visbuf(result)));
310 	    if (!strcmp(result, exit_attribute_mode)) {
311 		TR(TRACE_DATABASE, ("...same result, discard"));
312 		free(result);
313 		result = exit_attribute_mode;
314 	    }
315 	} else {
316 	    /*
317 	     * Either the sgr does not reference alternate character set,
318 	     * or it is incorrect.  That's too hard to decide right now.
319 	     */
320 	    free(off);
321 	}
322 	FreeIfNeeded(end);
323 	FreeIfNeeded(on);
324     } else {
325 	/*
326 	 * Possibly some applications are confused if sgr0 contains rmacs,
327 	 * but that would be a different bug report -TD
328 	 */
329     }
330 
331     returnPtr(result);
332 }
333