debug(("term_size, old r,c,s (%d,%d,%d), new rcs (%d,%d,%d)\n", rows, cols, savelines, newrows, newcols, newsavelines));
void term_size(int newrows, int newcols, int newsavelines) {    tree234 *newsb, *newscreen, *newalt;    unsigned long *newdisp, *newwant, *oldline, *line;    int i, j, ccols;    int sblen;    int save_alt_which = alt_which;    if (newrows == rows && newcols == cols && newsavelines == savelines)	return;			       /* nothing to do */    deselect();    swap_screen(0);    alt_t = marg_t = 0;    alt_b = marg_b = newrows - 1;    debug(("term_size, old r,c,s (%d,%d,%d), new rcs (%d,%d,%d)\n",           rows, cols, savelines, newrows, newcols, newsavelines));    if (rows == -1) {	scrollback = newtree234(NULL);	screen = newtree234(NULL);	rows = 0;    }    /*     * Resize the screen and scrollback. We only need to shift     * lines around within our data structures, because lineptr()     * will take care of resizing each individual line if     * necessary. So:     *      *  - If the new screen and the old screen differ in length, we     *    must shunt some lines in from the scrollback or out to     *    the scrollback.     *      *  - If doing that fails to provide us with enough material to     *    fill the new screen (i.e. the number of rows needed in     *    the new screen exceeds the total number in the previous     *    screen+scrollback), we must invent some blank lines to     *    cover the gap.     *      *  - Then, if the new scrollback length is less than the     *    amount of scrollback we actually have, we must throw some     *    away.     */    sblen = count234(scrollback);    debug(("newrows=%d rows=%d sblen=%d\n", newrows, rows, sblen));    if (newrows > rows) {	for (i = rows; i < newrows; i++) {            debug(("increase screen: i=%d\n", i));	    if (sblen > 0) {                debug(("sblen=%d so use line from scrollback\n", sblen));		line = delpos234(scrollback, --sblen);	    } else {                debug(("sblen=%d so make up a new line\n", sblen));		line = smalloc(TSIZE * (newcols+2));		line[0] = newcols;		for (j = 0; j <= newcols; j++)		    line[j+1] = ERASE_CHAR;	    }            debug(("got new screen line %p\n", line));	    addpos234(screen, line, 0);	}    } else if (newrows < rows) {	for (i = newrows; i < rows; i++) {            debug(("decrease screen: i=%d\n", i));	    line = delpos234(screen, 0);            debug(("taken out line %p, count is now %d\n",                   line, count234(screen)));	    addpos234(scrollback, line, sblen++);            debug(("added to scrollback, sblen is now %d\n", sblen));	}    }    assert(count234(screen) == newrows);    while (sblen > newsavelines) {	line = delpos234(scrollback, 0);	sfree(line);	sblen--;    }    assert(count234(scrollback) <= newsavelines);    disptop = 0;    newdisp = smalloc (newrows*(newcols+1)*TSIZE);    for (i=0; i<newrows*(newcols+1); i++)	newdisp[i] = ATTR_INVALID;    sfree (disptext);    disptext = newdisp;    newwant = smalloc (newrows*(newcols+1)*TSIZE);    for (i=0; i<newrows*(newcols+1); i++)	newwant[i] = ATTR_INVALID;    sfree (wanttext);    wanttext = newwant;    newalt = newtree234(NULL);    for (i=0; i<newrows; i++) {	line = smalloc(TSIZE * (newcols+2));	line[0] = newcols;	for (j = 0; j <= newcols; j++)	    line[j+1] = erase_char;	addpos234(newalt, line, i);    }    if (alt_screen) {	while (NULL != (line = delpos234(alt_screen, 0)))	    sfree(line);	freetree234(alt_screen);    }    alt_screen = newalt;    sfree (selspace);    selspace = smalloc ( (newrows+newsavelines) * (newcols+sizeof(sel_nl)) );    tabs = srealloc (tabs, newcols*sizeof(*tabs));    {	int i;	for (i = (cols > 0 ? cols : 0); i < newcols; i++)	    tabs[i] = (i % 8 == 0 ? TRUE : FALSE);    }    if (rows > 0)	curs.y += newrows - rows;    if (curs.y < 0)	curs.y = 0;    if (curs.y >= newrows)	curs.y = newrows-1;    if (curs.x >= newcols)	curs.x = newcols-1;    alt_x = alt_y = 0;    wrapnext = alt_wnext = FALSE;    rows = newrows;    cols = newcols;    savelines = newsavelines;    fix_cpos;    swap_screen(save_alt_which);    update_sbar();    term_update();}