+ /*
+ * While 'sgr0' is the "same" as termcap 'me', there is a compatibility
+ * issue. The sgr/sgr0 capabilities include setting/clearing alternate
+ * character set mode. A termcap application cannot use sgr, so sgr0
+ * strings that reset alternate character set mode will be
+ * misinterpreted. Here, we remove those from the more common
+ * ISO/ANSI/VT100 entries, which have sgr0 agreeing with sgr.
+ */
+ if (exit_attribute_mode != 0
+ && set_attributes != 0) {
+ char *on = set_attribute_9(1);
+ char *off = set_attribute_9(0);
+ char *tmp;
+ size_t i, j, k;
+
+ if (similar_sgr(off, exit_attribute_mode)
+ && !similar_sgr(off, on)) {
+ TR(TRACE_DATABASE, ("adjusting sgr0 : %s", _nc_visbuf(off)));
+ FreeIfNeeded(fix_me);
+ fix_me = off;
+ for (i = 0; off[i] != '\0'; ++i) {
+ if (on[i] != off[i]) {
+ j = strlen(off);
+ k = strlen(on);
+ while (j != 0
+ && k != 0
+ && off[j - 1] == on[k - 1]) {
+ --j, --k;
+ }
+ while (off[j] != '\0') {
+ off[i++] = off[j++];
+ }
+ off[i] = '\0';
+ break;
+ }
+ }
+ /* SGR 10 would reset to normal font */
+ if ((i = is_csi(off)) != 0
+ && off[strlen(off) - 1] == 'm') {
+ tmp = skip_zero(off + i);
+ if (tmp[0] == '1'
+ && skip_zero(tmp + 1) != tmp + 1) {
+ i = tmp - off;
+ if (off[i - 1] == ';')
+ i--;
+ j = skip_zero(tmp + 1) - off;
+ while (off[j] != '\0') {
+ off[i++] = off[j++];
+ }
+ off[i] = '\0';
+ }
+ }
+ TR(TRACE_DATABASE, ("...adjusted me : %s", _nc_visbuf(fix_me)));
+ if (!strcmp(fix_me, exit_attribute_mode)) {
+ TR(TRACE_DATABASE, ("...same result, discard"));
+ free(fix_me);
+ fix_me = 0;
+ }
+ }
+ free(on);
+ }
+