X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=ncurses%2Fbase%2Fwresize.c;h=93276ef5c42b201006cfd8b53943f1f865db87f9;hp=a61742a57c6d00e05946919d7a0588c7f8d8e5e8;hb=c976a90788f3e50afc773670ff74c5270ecb48df;hpb=46722468f47c2b77b3987729b4bcf2321cccfd01 diff --git a/ncurses/base/wresize.c b/ncurses/base/wresize.c index a61742a5..93276ef5 100644 --- a/ncurses/base/wresize.c +++ b/ncurses/base/wresize.c @@ -1,5 +1,6 @@ /**************************************************************************** - * Copyright (c) 1998-2001,2002 Free Software Foundation, Inc. * + * Copyright 2019,2020 Thomas E. Dickey * + * Copyright 1998-2010,2011 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 * @@ -27,18 +28,19 @@ ****************************************************************************/ /**************************************************************************** - * Author: Thomas E. Dickey 1996-2002 * + * Author: Thomas E. Dickey 1996-on * + * and: Juergen Pfeifer * ****************************************************************************/ #include -MODULE_ID("$Id: wresize.c,v 1.23 2002/09/28 15:15:51 tom Exp $") +MODULE_ID("$Id: wresize.c,v 1.39 2020/02/02 23:34:34 tom Exp $") static int cleanup_lines(struct ldat *data, int length) { while (--length >= 0) - free(data->text); + FreeAndNull(data[length].text); free(data); return ERR; } @@ -53,21 +55,40 @@ repair_subwindows(WINDOW *cmp) WINDOWLIST *wp; struct ldat *pline = cmp->_line; int row; +#ifdef USE_SP_WINDOWLIST + SCREEN *sp = _nc_screen_of(cmp); +#endif + + _nc_lock_global(curses); - for (wp = _nc_windows; wp != 0; wp = wp->next) { + for (each_window(SP_PARM, wp)) { WINDOW *tst = &(wp->win); if (tst->_parent == cmp) { - if (tst->_pary > cmp->_maxy) - tst->_pary = cmp->_maxy; - if (tst->_parx > cmp->_maxx) - tst->_parx = cmp->_maxx; +#define REPAIR1(field, limit) \ + if (tst->field > cmp->limit) \ + tst->field = cmp->limit + + REPAIR1(_pary, _maxy); + REPAIR1(_parx, _maxx); + +#define REPAIR2(field, limit) \ + if (tst->limit + tst->field > cmp->limit) \ + tst->limit = (NCURSES_SIZE_T) (cmp->limit - tst->field) + + REPAIR2(_pary, _maxy); + REPAIR2(_parx, _maxx); + +#define REPAIR3(field, limit) \ + if (tst->field > tst->limit) \ + tst->field = tst->limit - if (tst->_maxy + tst->_pary > cmp->_maxy) - tst->_maxy = cmp->_maxy - tst->_pary; - if (tst->_maxx + tst->_parx > cmp->_maxx) - tst->_maxx = cmp->_maxx - tst->_parx; + REPAIR3(_cury, _maxy); + REPAIR3(_curx, _maxx); + + REPAIR3(_regtop, _maxy); + REPAIR3(_regbottom, _maxy); for (row = 0; row <= tst->_maxy; ++row) { tst->_line[row].text = &pline[tst->_pary + row].text[tst->_parx]; @@ -75,6 +96,7 @@ repair_subwindows(WINDOW *cmp) repair_subwindows(tst); } } + _nc_unlock_global(curses); } /* @@ -90,14 +112,16 @@ wresize(WINDOW *win, int ToLines, int ToCols) struct ldat *new_lines = 0; #ifdef TRACE - T((T_CALLED("wresize(%p,%d,%d)"), win, ToLines, ToCols)); + T((T_CALLED("wresize(%p,%d,%d)"), (void *) win, ToLines, ToCols)); if (win) { - TR(TRACE_UPDATE, ("...beg (%d, %d), max(%d,%d), reg(%d,%d)", - win->_begy, win->_begx, - win->_maxy, win->_maxx, - win->_regtop, win->_regbottom)); - if (_nc_tracing & TRACE_UPDATE) + TR(TRACE_UPDATE, ("...beg (%ld, %ld), max(%ld,%ld), reg(%ld,%ld)", + (long) win->_begy, (long) win->_begx, + (long) win->_maxy, (long) win->_maxx, + (long) win->_regtop, (long) win->_regbottom)); + if (USE_TRACEF(TRACE_UPDATE)) { _tracedump("...before", win); + _nc_unlock_global(tracef); + } } #endif @@ -148,7 +172,8 @@ wresize(WINDOW *win, int ToLines, int ToCols) if (!(win->_flags & _SUBWIN)) { if (row <= size_y) { if (ToCols != size_x) { - if ((s = typeMalloc(NCURSES_CH_T, ToCols + 1)) == 0) + s = typeMalloc(NCURSES_CH_T, (unsigned) ToCols + 1); + if (s == 0) returnCode(cleanup_lines(new_lines, row)); for (col = 0; col <= ToCols; ++col) { s[col] = (col <= size_x @@ -159,13 +184,16 @@ wresize(WINDOW *win, int ToLines, int ToCols) s = win->_line[row].text; } } else { - if ((s = typeMalloc(NCURSES_CH_T, ToCols + 1)) == 0) + s = typeMalloc(NCURSES_CH_T, (unsigned) ToCols + 1); + if (s == 0) returnCode(cleanup_lines(new_lines, row)); for (col = 0; col <= ToCols; ++col) s[col] = win->_nc_bkgd; } - } else { + } else if (pline != 0 && pline[win->_pary + row].text != 0) { s = &pline[win->_pary + row].text[win->_parx]; + } else { + s = 0; } if_USE_SCROLL_HINTS(new_lines[row].oldindex = row); @@ -176,11 +204,11 @@ wresize(WINDOW *win, int ToLines, int ToCols) if ((ToCols != size_x) || (row > size_y)) { if (end >= begin) { /* growing */ if (new_lines[row].firstchar < begin) - new_lines[row].firstchar = begin; + new_lines[row].firstchar = (NCURSES_SIZE_T) begin; } else { /* shrinking */ new_lines[row].firstchar = 0; } - new_lines[row].lastchar = ToCols; + new_lines[row].lastchar = (NCURSES_SIZE_T) ToCols; } new_lines[row].text = s; } @@ -191,24 +219,24 @@ wresize(WINDOW *win, int ToLines, int ToCols) if (!(win->_flags & _SUBWIN)) { if (ToCols == size_x) { for (row = ToLines + 1; row <= size_y; row++) { - free(win->_line[row].text); + FreeAndNull(win->_line[row].text); } } else { for (row = 0; row <= size_y; row++) { - free(win->_line[row].text); + FreeAndNull(win->_line[row].text); } } } - free(win->_line); + FreeAndNull(win->_line); win->_line = new_lines; /* * Finally, adjust the parameters showing screen size and cursor * position: */ - win->_maxx = ToCols; - win->_maxy = ToLines; + win->_maxx = (NCURSES_SIZE_T) ToCols; + win->_maxy = (NCURSES_SIZE_T) ToLines; if (win->_regtop > win->_maxy) win->_regtop = win->_maxy; @@ -228,12 +256,14 @@ wresize(WINDOW *win, int ToLines, int ToCols) repair_subwindows(win); #ifdef TRACE - TR(TRACE_UPDATE, ("...beg (%d, %d), max(%d,%d), reg(%d,%d)", - win->_begy, win->_begx, - win->_maxy, win->_maxx, - win->_regtop, win->_regbottom)); - if (_nc_tracing & TRACE_UPDATE) + TR(TRACE_UPDATE, ("...beg (%ld, %ld), max(%ld,%ld), reg(%ld,%ld)", + (long) win->_begy, (long) win->_begx, + (long) win->_maxy, (long) win->_maxx, + (long) win->_regtop, (long) win->_regbottom)); + if (USE_TRACEF(TRACE_UPDATE)) { _tracedump("...after:", win); + _nc_unlock_global(tracef); + } #endif returnCode(OK); }