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