1 /****************************************************************************
2 * Copyright (c) 2005-2006,2007 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Thomas Dickey *
31 ****************************************************************************/
33 #include <curses.priv.h>
38 #include <term_entry.h>
40 MODULE_ID("$Id: trim_sgr0.c,v 1.8 2007/04/07 17:14:11 tom Exp $")
46 #define ESC 033 /* ^[ */
50 set_attribute_9(TERMTYPE *tp, int flag)
54 if ((result = tparm(set_attributes, 0, 0, 0, 0, 0, 0, 0, 0, flag)) == 0)
56 return strdup(result);
62 if (UChar(s[0]) == CSI)
64 else if (s[0] == ESC && s[1] == L_BRACK)
75 else if (isalpha(UChar(s[1])))
82 skip_delay(const char *s)
84 if (s[0] == '$' && s[1] == '<') {
86 while (isdigit(UChar(*s)) || *s == '/')
95 * Improve similar_sgr a little by moving the attr-string from the beginning
96 * to the end of the s-string.
99 rewrite_sgr(char *s, char *attr)
103 unsigned len_s = strlen(s);
104 unsigned len_a = strlen(attr);
106 if (len_s > len_a && !strncmp(attr, s, len_a)) {
108 TR(TRACE_DATABASE, ("rewrite:\n\t%s", s));
109 for (n = 0; n < len_s - len_a; ++n) {
113 TR(TRACE_DATABASE, ("to:\n\t%s", s));
118 return FALSE; /* oops */
122 similar_sgr(char *a, char *b)
125 int csi_a = is_csi(a);
126 int csi_b = is_csi(b);
130 TR(TRACE_DATABASE, ("similar_sgr:\n\t%s\n\t%s",
133 if (csi_a != 0 && csi_b != 0 && csi_a == csi_b) {
143 if (len_a && len_b) {
145 result = (strncmp(a, b, len_b) == 0);
147 result = (strncmp(a, b, len_a) == 0);
149 TR(TRACE_DATABASE, ("...similar_sgr: %d\n\t%s\n\t%s", result,
156 chop_out(char *string, unsigned i, unsigned j)
158 TR(TRACE_DATABASE, ("chop_out %d..%d from %s", i, j, _nc_visbuf(string)));
159 while (string[j] != '\0') {
160 string[i++] = string[j++];
167 * Compare, ignoring delays. Some of the delay values are inconsistent, and
168 * we do not want to be stopped by that.
170 * Returns the number of chars from 'full' that we matched. If any mismatch
171 * occurs, return zero.
174 compare_part(const char *part, const char *full)
176 const char *next_part;
177 const char *next_full;
182 if (*part != *full) {
188 * Adjust the return-value to allow the rare case of
189 * string<delay>string
190 * to remove the whole piece. The most common case is a delay at the
191 * end of the string. The adjusted string will retain the delay, which
194 if (used_delay != 0) {
195 used_full += used_delay;
198 if (*part == '$' && *full == '$') {
199 next_part = skip_delay(part);
200 next_full = skip_delay(full);
201 if (next_part != part && next_full != full) {
202 used_delay += (next_full - full);
216 * While 'sgr0' is the "same" as termcap 'me', there is a compatibility issue.
217 * The sgr/sgr0 capabilities include setting/clearing alternate character set
218 * mode. A termcap application cannot use sgr, so sgr0 strings that reset
219 * alternate character set mode will be misinterpreted. Here, we remove those
220 * from the more common ISO/ANSI/VT100 entries, which have sgr0 agreeing with
223 * This function returns the modified sgr0 if it can be modified, a null if
224 * an error occurs, or the original sgr0 if no change is needed.
226 NCURSES_EXPORT(char *)
227 _nc_trim_sgr0(TERMTYPE *tp)
229 char *result = exit_attribute_mode;
231 T((T_CALLED("_nc_trim_sgr0()")));
233 if (PRESENT(exit_attribute_mode)
234 && PRESENT(set_attributes)) {
236 char *on = set_attribute_9(tp, 1);
237 char *off = set_attribute_9(tp, 0);
238 char *end = strdup(exit_attribute_mode);
242 TR(TRACE_DATABASE, ("checking if we can trim sgr0 based on sgr"));
243 TR(TRACE_DATABASE, ("sgr0 %s", _nc_visbuf(end)));
244 TR(TRACE_DATABASE, ("sgr(9:off) %s", _nc_visbuf(off)));
245 TR(TRACE_DATABASE, ("sgr(9:on) %s", _nc_visbuf(on)));
247 if (!rewrite_sgr(on, enter_alt_charset_mode)
248 || !rewrite_sgr(off, exit_alt_charset_mode)
249 || !rewrite_sgr(end, exit_alt_charset_mode)) {
251 } else if (similar_sgr(off, end)
252 && !similar_sgr(off, on)) {
253 TR(TRACE_DATABASE, ("adjusting sgr(9:off) : %s", _nc_visbuf(off)));
256 * If rmacs is a substring of sgr(0), remove that chunk.
258 if (exit_alt_charset_mode != 0) {
259 TR(TRACE_DATABASE, ("scan for rmacs %s", _nc_visbuf(exit_alt_charset_mode)));
261 k = strlen(exit_alt_charset_mode);
263 for (i = 0; i <= (j - k); ++i) {
264 int k2 = compare_part(exit_alt_charset_mode, off + i);
267 chop_out(off, i, i + k2);
274 * SGR 10 would reset to normal font.
277 if ((i = is_csi(off)) != 0
278 && off[strlen(off) - 1] == 'm') {
279 TR(TRACE_DATABASE, ("looking for SGR 10 in %s",
281 tmp = skip_zero(off + i);
283 && skip_zero(tmp + 1) != tmp + 1) {
285 if (off[i - 1] == ';')
287 j = skip_zero(tmp + 1) - off;
288 i = chop_out(off, i, j);
294 && (tmp = strstr(end, off)) != 0
295 && strcmp(end, off) != 0) {
303 TR(TRACE_DATABASE, ("...adjusted sgr0 : %s", _nc_visbuf(result)));
304 if (!strcmp(result, exit_attribute_mode)) {
305 TR(TRACE_DATABASE, ("...same result, discard"));
307 result = exit_attribute_mode;
311 * Either the sgr does not reference alternate character set,
312 * or it is incorrect. That's too hard to decide right now.
320 * Possibly some applications are confused if sgr0 contains rmacs,
321 * but that would be a different bug report -TD