1 /****************************************************************************
2 * Copyright 2020-2021,2023 Thomas E. Dickey *
3 * Copyright 2005-2012,2017 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Thomas Dickey *
32 ****************************************************************************/
34 #include <curses.priv.h>
40 MODULE_ID("$Id: trim_sgr0.c,v 1.22 2023/09/23 18:47:56 tom Exp $")
46 set_attribute_9(TERMTYPE2 *tp, int flag)
51 value = TIPARM_9(set_attributes, 0, 0, 0, 0, 0, 0, 0, 0, flag);
53 result = strdup(value);
64 if (UChar(s[0]) == CSI_CHR)
66 else if (s[0] == ESC_CHR && s[1] == L_BLOCK)
78 else if (isalpha(UChar(s[1])))
85 skip_delay(const char *s)
87 if (s[0] == '$' && s[1] == '<') {
89 while (isdigit(UChar(*s)) || *s == '/')
98 * Improve similar_sgr a little by moving the attr-string from the beginning
99 * to the end of the s-string.
102 rewrite_sgr(char *s, char *attr)
106 size_t len_s = strlen(s);
107 size_t len_a = strlen(attr);
109 if (len_s > len_a && !strncmp(attr, s, len_a)) {
111 TR(TRACE_DATABASE, ("rewrite:\n\t%s", s));
112 for (n = 0; n < len_s - len_a; ++n) {
115 _nc_STRCPY(s + n, attr, strlen(s) + 1);
116 TR(TRACE_DATABASE, ("to:\n\t%s", s));
121 return FALSE; /* oops */
125 similar_sgr(char *a, char *b)
128 if (a != 0 && b != 0) {
129 int csi_a = is_csi(a);
130 int csi_b = is_csi(b);
134 TR(TRACE_DATABASE, ("similar_sgr:\n\t%s\n\t%s",
137 if (csi_a != 0 && csi_b != 0 && csi_a == csi_b) {
147 if (len_a && len_b) {
149 result = (strncmp(a, b, len_b) == 0);
151 result = (strncmp(a, b, len_a) == 0);
153 TR(TRACE_DATABASE, ("...similar_sgr: %d\n\t%s\n\t%s", result,
161 chop_out(char *string, unsigned i, unsigned j)
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++];
172 * Compare, ignoring delays. Some of the delay values are inconsistent, and
173 * we do not want to be stopped by that.
175 * Returns the number of chars from 'full' that we matched. If any mismatch
176 * occurs, return zero.
179 compare_part(const char *part, const char *full)
181 const char *next_part;
182 const char *next_full;
183 unsigned used_full = 0;
184 unsigned used_delay = 0;
187 if (*part != *full) {
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
199 if (used_delay != 0) {
200 used_full += used_delay;
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);
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
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.
231 NCURSES_EXPORT(char *)
232 _nc_trim_sgr0(TERMTYPE2 *tp)
234 char *result = exit_attribute_mode;
236 T((T_CALLED("_nc_trim_sgr0()")));
238 if (PRESENT(exit_attribute_mode)
239 && PRESENT(set_attributes)) {
241 char *on = set_attribute_9(tp, 1);
242 char *off = set_attribute_9(tp, 0);
243 char *end = strdup(exit_attribute_mode);
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)));
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)) {
256 } else if (similar_sgr(off, end)
257 && !similar_sgr(off, on)) {
258 TR(TRACE_DATABASE, ("adjusting sgr(9:off) : %s", _nc_visbuf(off)));
261 * If rmacs is a substring of sgr(0), remove that chunk.
263 if (PRESENT(exit_alt_charset_mode)) {
264 TR(TRACE_DATABASE, ("scan for rmacs %s", _nc_visbuf(exit_alt_charset_mode)));
266 k = strlen(exit_alt_charset_mode);
268 for (i = 0; i <= (j - k); ++i) {
269 unsigned k2 = compare_part(exit_alt_charset_mode,
273 chop_out(off, (unsigned) i, (unsigned) (i + k2));
280 * SGR 10 would reset to normal font.
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",
287 tmp = skip_zero(off + i);
289 && skip_zero(tmp + 1) != tmp + 1) {
290 i = (size_t) (tmp - off);
291 if (off[i - 1] == ';')
293 j = (size_t) (skip_zero(tmp + 1) - off);
294 (void) chop_out(off, (unsigned) i, (unsigned) j);
300 && (tmp = strstr(end, off)) != 0
301 && strcmp(end, off) != 0) {
302 i = (size_t) (tmp - end);
305 chop_out(tmp, (unsigned) i, (unsigned) j);
309 TR(TRACE_DATABASE, ("...adjusted sgr0 : %s", _nc_visbuf(result)));
310 if (!strcmp(result, exit_attribute_mode)) {
311 TR(TRACE_DATABASE, ("...same result, discard"));
313 result = exit_attribute_mode;
317 * Either the sgr does not reference alternate character set,
318 * or it is incorrect. That's too hard to decide right now.
326 * Possibly some applications are confused if sgr0 contains rmacs,
327 * but that would be a different bug report -TD