1 /****************************************************************************
2 * Copyright 2020 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.20 2020/05/27 23:54:31 tom Exp $")
46 #define ESC 033 /* ^[ */
50 set_attribute_9(TERMTYPE2 *tp, int flag)
55 value = TIPARM_9(set_attributes, 0, 0, 0, 0, 0, 0, 0, 0, flag);
57 result = strdup(value);
68 if (UChar(s[0]) == CSI)
70 else if (s[0] == ESC && s[1] == L_BRACK)
82 else if (isalpha(UChar(s[1])))
89 skip_delay(const char *s)
91 if (s[0] == '$' && s[1] == '<') {
93 while (isdigit(UChar(*s)) || *s == '/')
102 * Improve similar_sgr a little by moving the attr-string from the beginning
103 * to the end of the s-string.
106 rewrite_sgr(char *s, char *attr)
110 size_t len_s = strlen(s);
111 size_t len_a = strlen(attr);
113 if (len_s > len_a && !strncmp(attr, s, len_a)) {
115 TR(TRACE_DATABASE, ("rewrite:\n\t%s", s));
116 for (n = 0; n < len_s - len_a; ++n) {
119 _nc_STRCPY(s + n, attr, strlen(s) + 1);
120 TR(TRACE_DATABASE, ("to:\n\t%s", s));
125 return FALSE; /* oops */
129 similar_sgr(char *a, char *b)
132 if (a != 0 && b != 0) {
133 int csi_a = is_csi(a);
134 int csi_b = is_csi(b);
138 TR(TRACE_DATABASE, ("similar_sgr:\n\t%s\n\t%s",
141 if (csi_a != 0 && csi_b != 0 && csi_a == csi_b) {
151 if (len_a && len_b) {
153 result = (strncmp(a, b, len_b) == 0);
155 result = (strncmp(a, b, len_a) == 0);
157 TR(TRACE_DATABASE, ("...similar_sgr: %d\n\t%s\n\t%s", result,
165 chop_out(char *string, unsigned i, unsigned j)
167 TR(TRACE_DATABASE, ("chop_out %d..%d from %s", i, j, _nc_visbuf(string)));
168 while (string[j] != '\0') {
169 string[i++] = string[j++];
176 * Compare, ignoring delays. Some of the delay values are inconsistent, and
177 * we do not want to be stopped by that.
179 * Returns the number of chars from 'full' that we matched. If any mismatch
180 * occurs, return zero.
183 compare_part(const char *part, const char *full)
185 const char *next_part;
186 const char *next_full;
187 unsigned used_full = 0;
188 unsigned used_delay = 0;
191 if (*part != *full) {
197 * Adjust the return-value to allow the rare case of
198 * string<delay>string
199 * to remove the whole piece. The most common case is a delay at the
200 * end of the string. The adjusted string will retain the delay, which
203 if (used_delay != 0) {
204 used_full += used_delay;
207 if (*part == '$' && *full == '$') {
208 next_part = skip_delay(part);
209 next_full = skip_delay(full);
210 if (next_part != part && next_full != full) {
211 used_delay += (unsigned) (next_full - full);
225 * While 'sgr0' is the "same" as termcap 'me', there is a compatibility issue.
226 * The sgr/sgr0 capabilities include setting/clearing alternate character set
227 * mode. A termcap application cannot use sgr, so sgr0 strings that reset
228 * alternate character set mode will be misinterpreted. Here, we remove those
229 * from the more common ISO/ANSI/VT100 entries, which have sgr0 agreeing with
232 * This function returns the modified sgr0 if it can be modified, a null if
233 * an error occurs, or the original sgr0 if no change is needed.
235 NCURSES_EXPORT(char *)
236 _nc_trim_sgr0(TERMTYPE2 *tp)
238 char *result = exit_attribute_mode;
240 T((T_CALLED("_nc_trim_sgr0()")));
242 if (PRESENT(exit_attribute_mode)
243 && PRESENT(set_attributes)) {
245 char *on = set_attribute_9(tp, 1);
246 char *off = set_attribute_9(tp, 0);
247 char *end = strdup(exit_attribute_mode);
251 TR(TRACE_DATABASE, ("checking if we can trim sgr0 based on sgr"));
252 TR(TRACE_DATABASE, ("sgr0 %s", _nc_visbuf(end)));
253 TR(TRACE_DATABASE, ("sgr(9:off) %s", _nc_visbuf(off)));
254 TR(TRACE_DATABASE, ("sgr(9:on) %s", _nc_visbuf(on)));
256 if (!rewrite_sgr(on, enter_alt_charset_mode)
257 || !rewrite_sgr(off, exit_alt_charset_mode)
258 || !rewrite_sgr(end, exit_alt_charset_mode)) {
260 } else if (similar_sgr(off, end)
261 && !similar_sgr(off, on)) {
262 TR(TRACE_DATABASE, ("adjusting sgr(9:off) : %s", _nc_visbuf(off)));
265 * If rmacs is a substring of sgr(0), remove that chunk.
267 if (PRESENT(exit_alt_charset_mode)) {
268 TR(TRACE_DATABASE, ("scan for rmacs %s", _nc_visbuf(exit_alt_charset_mode)));
270 k = strlen(exit_alt_charset_mode);
272 for (i = 0; i <= (j - k); ++i) {
273 unsigned k2 = compare_part(exit_alt_charset_mode,
277 chop_out(off, (unsigned) i, (unsigned) (i + k2));
284 * SGR 10 would reset to normal font.
287 if ((i = (size_t) is_csi(off)) != 0
288 && off[strlen(off) - 1] == 'm') {
289 TR(TRACE_DATABASE, ("looking for SGR 10 in %s",
291 tmp = skip_zero(off + i);
293 && skip_zero(tmp + 1) != tmp + 1) {
294 i = (size_t) (tmp - off);
295 if (off[i - 1] == ';')
297 j = (size_t) (skip_zero(tmp + 1) - off);
298 (void) chop_out(off, (unsigned) i, (unsigned) j);
304 && (tmp = strstr(end, off)) != 0
305 && strcmp(end, off) != 0) {
306 i = (size_t) (tmp - end);
309 chop_out(tmp, (unsigned) i, (unsigned) j);
313 TR(TRACE_DATABASE, ("...adjusted sgr0 : %s", _nc_visbuf(result)));
314 if (!strcmp(result, exit_attribute_mode)) {
315 TR(TRACE_DATABASE, ("...same result, discard"));
317 result = exit_attribute_mode;
321 * Either the sgr does not reference alternate character set,
322 * or it is incorrect. That's too hard to decide right now.
330 * Possibly some applications are confused if sgr0 contains rmacs,
331 * but that would be a different bug report -TD