X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=ncurses%2Ftinfo%2Ftrim_sgr0.c;h=4cbcb6506772c21a9fdd67398b952422be7b19a0;hp=5f7b9b3383fec76e46e80f6f798100367787c343;hb=a8e3f06ac309504143cd56ac9ec55889bfdf4914;hpb=92e187a3459ab7ce1613a3684ca6642447c73620 diff --git a/ncurses/tinfo/trim_sgr0.c b/ncurses/tinfo/trim_sgr0.c index 5f7b9b33..4cbcb650 100644 --- a/ncurses/tinfo/trim_sgr0.c +++ b/ncurses/tinfo/trim_sgr0.c @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright (c) 2005-2007,2010 Free Software Foundation, Inc. * + * Copyright (c) 2005-2012,2017 Free Software Foundation, Inc. * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * @@ -36,7 +36,7 @@ #include -MODULE_ID("$Id: trim_sgr0.c,v 1.11 2010/05/01 19:33:31 tom Exp $") +MODULE_ID("$Id: trim_sgr0.c,v 1.16 2017/04/05 22:33:07 tom Exp $") #undef CUR #define CUR tp-> @@ -46,23 +46,30 @@ MODULE_ID("$Id: trim_sgr0.c,v 1.11 2010/05/01 19:33:31 tom Exp $") #define L_BRACK '[' static char * -set_attribute_9(TERMTYPE *tp, int flag) +set_attribute_9(TERMTYPE2 *tp, int flag) { - const char *result; + const char *value; + char *result; - if ((result = tparm(set_attributes, 0, 0, 0, 0, 0, 0, 0, 0, flag)) == 0) - result = ""; - return strdup(result); + value = tparm(set_attributes, 0, 0, 0, 0, 0, 0, 0, 0, flag); + if (PRESENT(value)) + result = strdup(value); + else + result = 0; + return result; } static int is_csi(const char *s) { - if (UChar(s[0]) == CSI) - return 1; - else if (s[0] == ESC && s[1] == L_BRACK) - return 2; - return 0; + int result = 0; + if (s != 0) { + if (UChar(s[0]) == CSI) + result = 1; + else if (s[0] == ESC && s[1] == L_BRACK) + result = 2; + } + return result; } static char * @@ -97,10 +104,10 @@ skip_delay(const char *s) static bool rewrite_sgr(char *s, char *attr) { - if (PRESENT(s)) { + if (s != 0) { if (PRESENT(attr)) { - unsigned len_s = strlen(s); - unsigned len_a = strlen(attr); + size_t len_s = strlen(s); + size_t len_a = strlen(attr); if (len_s > len_a && !strncmp(attr, s, len_a)) { unsigned n; @@ -108,7 +115,7 @@ rewrite_sgr(char *s, char *attr) for (n = 0; n < len_s - len_a; ++n) { s[n] = s[n + len_a]; } - strcpy(s + n, attr); + _nc_STRCPY(s + n, attr, strlen(s) + 1); TR(TRACE_DATABASE, ("to:\n\t%s", s)); } } @@ -121,33 +128,35 @@ static bool similar_sgr(char *a, char *b) { bool result = FALSE; - int csi_a = is_csi(a); - int csi_b = is_csi(b); - unsigned len_a; - unsigned len_b; + if (a != 0 && b != 0) { + int csi_a = is_csi(a); + int csi_b = is_csi(b); + size_t len_a; + size_t len_b; - TR(TRACE_DATABASE, ("similar_sgr:\n\t%s\n\t%s", - _nc_visbuf2(1, a), - _nc_visbuf2(2, b))); - if (csi_a != 0 && csi_b != 0 && csi_a == csi_b) { - a += csi_a; - b += csi_b; - if (*a != *b) { - a = skip_zero(a); - b = skip_zero(b); + TR(TRACE_DATABASE, ("similar_sgr:\n\t%s\n\t%s", + _nc_visbuf2(1, a), + _nc_visbuf2(2, b))); + if (csi_a != 0 && csi_b != 0 && csi_a == csi_b) { + a += csi_a; + b += csi_b; + if (*a != *b) { + a = skip_zero(a); + b = skip_zero(b); + } } + len_a = strlen(a); + len_b = strlen(b); + if (len_a && len_b) { + if (len_a > len_b) + result = (strncmp(a, b, len_b) == 0); + else + result = (strncmp(a, b, len_a) == 0); + } + TR(TRACE_DATABASE, ("...similar_sgr: %d\n\t%s\n\t%s", result, + _nc_visbuf2(1, a), + _nc_visbuf2(2, b))); } - len_a = strlen(a); - len_b = strlen(b); - if (len_a && len_b) { - if (len_a > len_b) - result = (strncmp(a, b, len_b) == 0); - else - result = (strncmp(a, b, len_a) == 0); - } - TR(TRACE_DATABASE, ("...similar_sgr: %d\n\t%s\n\t%s", result, - _nc_visbuf2(1, a), - _nc_visbuf2(2, b))); return result; } @@ -223,7 +232,7 @@ compare_part(const char *part, const char *full) * an error occurs, or the original sgr0 if no change is needed. */ NCURSES_EXPORT(char *) -_nc_trim_sgr0(TERMTYPE *tp) +_nc_trim_sgr0(TERMTYPE2 *tp) { char *result = exit_attribute_mode; @@ -264,7 +273,7 @@ _nc_trim_sgr0(TERMTYPE *tp) off + i); if (k2 != 0) { found = TRUE; - chop_out(off, i, i + k2); + chop_out(off, (unsigned) i, (unsigned) (i + k2)); break; } } @@ -285,7 +294,7 @@ _nc_trim_sgr0(TERMTYPE *tp) if (off[i - 1] == ';') i--; j = (size_t) (skip_zero(tmp + 1) - off); - (void) chop_out(off, i, j); + (void) chop_out(off, (unsigned) i, (unsigned) j); found = TRUE; } } @@ -296,7 +305,7 @@ _nc_trim_sgr0(TERMTYPE *tp) i = (size_t) (tmp - end); j = strlen(off); tmp = strdup(end); - chop_out(tmp, i, j); + chop_out(tmp, (unsigned) i, (unsigned) j); free(off); result = tmp; }