X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=ncurses%2Fbase%2Fnew_pair.c;h=92131232e8695edf52b51f8902fac008176ef61c;hp=f13d16f4575421c1cac5c9345b8c6ab06dfc257c;hb=1f7a36fe16b11bb4aab51be89bbc1b0f75936b78;hpb=a20e6eb464be80b9cd8cae7ce925d27fe9c209ed;ds=inline diff --git a/ncurses/base/new_pair.c b/ncurses/base/new_pair.c index f13d16f4..92131232 100644 --- a/ncurses/base/new_pair.c +++ b/ncurses/base/new_pair.c @@ -1,5 +1,6 @@ /**************************************************************************** - * Copyright (c) 2017 Free Software Foundation, Inc. * + * Copyright 2018-2019,2020 Thomas E. Dickey * + * Copyright 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 * @@ -48,7 +49,7 @@ #define MaxColors max_colors #endif -#if USE_NEW_PAIR +#if NCURSES_EXT_COLORS /* fix redefinition versys tic.h */ #undef entry @@ -60,9 +61,9 @@ #endif -MODULE_ID("$Id: new_pair.c,v 1.14 2017/08/11 18:20:22 tom Exp $") +MODULE_ID("$Id: new_pair.c,v 1.20 2020/04/11 16:06:56 tom Exp $") -#if USE_NEW_PAIR +#if NCURSES_EXT_COLORS #ifdef NEW_PAIR_DEBUG @@ -102,13 +103,17 @@ dumpit(SCREEN *sp, int pair, const char *tag) char bigbuf[256 * 20]; char *p = bigbuf; int n; - sprintf(p, "%s", tag); - p += strlen(p); + size_t have = sizeof(bigbuf); + + _nc_STRCPY(p, tag, have); for (n = 0; n < sp->_pair_limit; ++n) { if (list[n].mode != cpFREE) { - sprintf(p, " %d%c(%d,%d)", - n, n == pair ? '@' : ':', list[n].next, list[n].prev); p += strlen(p); + if ((size_t) (p - bigbuf) + 50 > have) + break; + _nc_SPRINTF(p, _nc_SLIMIT(have - (p - bigbuf)) + " %d%c(%d,%d)", + n, n == pair ? '@' : ':', list[n].next, list[n].prev); } } T(("(%d/%d) %ld - %s", @@ -139,17 +144,16 @@ static int _nc_find_color_pair(SCREEN *sp, int fg, int bg) { colorpair_t find; - int result; + int result = -1; void *pp; find.fg = fg; find.bg = bg; - if (sp != 0 && - (pp = tfind(&find, &sp->_ordered_pairs, compare_data)) != 0) { - colorpair_t *temp = *(colorpair_t **) pp; - result = (int) (temp - sp->_color_pairs); - } else { - result = -1; + if (sp != 0) { + if ((pp = tfind(&find, &sp->_ordered_pairs, compare_data)) != 0) { + colorpair_t *temp = *(colorpair_t **) pp; + result = (int) (temp - sp->_color_pairs); + } } return result; } @@ -192,13 +196,22 @@ NCURSES_EXPORT(void) _nc_reset_color_pair(SCREEN *sp, int pair, colorpair_t * next) { colorpair_t *last; + if (ValidPair(sp, pair)) { - last = _nc_reserve_pairs(sp, pair); + bool used; + + ReservePairs(sp, pair); + last = &(sp->_color_pairs[pair]); delink_color_pair(sp, pair); if (last->mode > cpFREE && (last->fg != next->fg || last->bg != next->bg)) { /* remove the old entry from fast index */ tdelete(last, &sp->_ordered_pairs, compare_data); + used = FALSE; + } else { + used = (last->mode != cpFREE); + } + if (!used) { /* create a new entry in fast index */ *last = *next; tsearch(last, &sp->_ordered_pairs, compare_data); @@ -277,7 +290,8 @@ NCURSES_SP_NAME(alloc_pair) (NCURSES_SP_DCLx int fg, int bg) } if (!found && (SP_PARM->_pair_alloc < SP_PARM->_pair_limit)) { pair = SP_PARM->_pair_alloc; - if (_nc_reserve_pairs(sp, pair) == 0) { + ReservePairs(SP_PARM, pair); + if (SP_PARM->_color_pairs == 0) { pair = -1; } else { found = TRUE; @@ -376,4 +390,4 @@ void _nc_new_pair(void) { } -#endif /* USE_NEW_PAIR */ +#endif /* NCURSES_EXT_COLORS */