blob: 177dcd87225e5e950f8d7e8782e6aee52d161e3b [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2020-2021,2023 Thomas E. Dickey *
3 * Copyright 2005-2012,2017 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05304 * *
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>
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053039
micky3879b9f5e72025-07-08 18:04:53 -040040MODULE_ID("$Id: trim_sgr0.c,v 1.22 2023/09/23 18:47:56 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053041
42#undef CUR
43#define CUR tp->
44
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053045static char *
micky3879b9f5e72025-07-08 18:04:53 -040046set_attribute_9(TERMTYPE2 *tp, int flag)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053047{
Steve Kondikae271bc2015-11-15 02:50:53 +010048 const char *value;
49 char *result;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053050
micky3879b9f5e72025-07-08 18:04:53 -040051 value = TIPARM_9(set_attributes, 0, 0, 0, 0, 0, 0, 0, 0, flag);
Steve Kondikae271bc2015-11-15 02:50:53 +010052 if (PRESENT(value))
53 result = strdup(value);
54 else
55 result = 0;
56 return result;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053057}
58
59static int
60is_csi(const char *s)
61{
Steve Kondikae271bc2015-11-15 02:50:53 +010062 int result = 0;
63 if (s != 0) {
micky3879b9f5e72025-07-08 18:04:53 -040064 if (UChar(s[0]) == CSI_CHR)
Steve Kondikae271bc2015-11-15 02:50:53 +010065 result = 1;
micky3879b9f5e72025-07-08 18:04:53 -040066 else if (s[0] == ESC_CHR && s[1] == L_BLOCK)
Steve Kondikae271bc2015-11-15 02:50:53 +010067 result = 2;
68 }
69 return result;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053070}
71
72static char *
73skip_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
84static const char *
85skip_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 */
101static bool
102rewrite_sgr(char *s, char *attr)
103{
Steve Kondikae271bc2015-11-15 02:50:53 +0100104 if (s != 0) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530105 if (PRESENT(attr)) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100106 size_t len_s = strlen(s);
107 size_t len_a = strlen(attr);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530108
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 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100115 _nc_STRCPY(s + n, attr, strlen(s) + 1);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530116 TR(TRACE_DATABASE, ("to:\n\t%s", s));
117 }
118 }
119 return TRUE;
120 }
121 return FALSE; /* oops */
122}
123
124static bool
125similar_sgr(char *a, char *b)
126{
127 bool result = FALSE;
Steve Kondikae271bc2015-11-15 02:50:53 +0100128 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;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530133
Steve Kondikae271bc2015-11-15 02:50:53 +0100134 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 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530144 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100145 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)));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530156 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530157 return result;
158}
159
160static unsigned
161chop_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 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100178static unsigned
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530179compare_part(const char *part, const char *full)
180{
181 const char *next_part;
182 const char *next_full;
Steve Kondikae271bc2015-11-15 02:50:53 +0100183 unsigned used_full = 0;
184 unsigned used_delay = 0;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530185
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) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100207 used_delay += (unsigned) (next_full - full);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530208 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/*
micky3879b9f5e72025-07-08 18:04:53 -0400221 * While 'sgr0' is the "same" as termcap 'me', there is a compatibility issue.
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530222 * 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 */
231NCURSES_EXPORT(char *)
micky3879b9f5e72025-07-08 18:04:53 -0400232_nc_trim_sgr0(TERMTYPE2 *tp)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530233{
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 */
micky3879b9f5e72025-07-08 18:04:53 -0400263 if (PRESENT(exit_alt_charset_mode)) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530264 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) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100269 unsigned k2 = compare_part(exit_alt_charset_mode,
270 off + i);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530271 if (k2 != 0) {
272 found = TRUE;
Steve Kondikae271bc2015-11-15 02:50:53 +0100273 chop_out(off, (unsigned) i, (unsigned) (i + k2));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530274 break;
275 }
276 }
277 }
278 }
279 /*
280 * SGR 10 would reset to normal font.
281 */
282 if (!found) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100283 if ((i = (size_t) is_csi(off)) != 0
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530284 && 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) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100290 i = (size_t) (tmp - off);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530291 if (off[i - 1] == ';')
292 i--;
Steve Kondikae271bc2015-11-15 02:50:53 +0100293 j = (size_t) (skip_zero(tmp + 1) - off);
294 (void) chop_out(off, (unsigned) i, (unsigned) j);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530295 found = TRUE;
296 }
297 }
298 }
299 if (!found
300 && (tmp = strstr(end, off)) != 0
301 && strcmp(end, off) != 0) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100302 i = (size_t) (tmp - end);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530303 j = strlen(off);
304 tmp = strdup(end);
Steve Kondikae271bc2015-11-15 02:50:53 +0100305 chop_out(tmp, (unsigned) i, (unsigned) j);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530306 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}