for (i = term->rows; i < newrows; i++) { if (sblen > 0) {
assert(term->rows == count234(term->screen)); while (term->rows < newrows) { if (term->tempsblines > 0) {  assert(sblen >= term->tempsblines);
void term_size(Terminal *term, int newrows, int newcols, int newsavelines){    tree234 *newalt;    unsigned long *newdisp, *line;    int i, j;    int sblen;    int save_alt_which = term->alt_which;    if (newrows == term->rows && newcols == term->cols &&	newsavelines == term->savelines)	return;			       /* nothing to do */    deselect(term);    swap_screen(term, 0, FALSE, FALSE);    term->alt_t = term->marg_t = 0;    term->alt_b = term->marg_b = newrows - 1;    if (term->rows == -1) {	term->scrollback = newtree234(NULL);	term->screen = newtree234(NULL);	term->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(term->scrollback);    /* Do this loop to expand the screen if newrows > rows */    for (i = term->rows; i < newrows; i++) {	if (sblen > 0) {	    line = delpos234(term->scrollback, --sblen);	} else {	    line = smalloc(TSIZE * (newcols + 2));	    line[0] = newcols;	    for (j = 0; j < newcols; j++)		line[j + 1] = ERASE_CHAR;            line[newcols + 1] = LATTR_NORM;	}	addpos234(term->screen, line, 0);    }    /* Do this loop to shrink the screen if newrows < rows */    for (i = newrows; i < term->rows; i++) {	line = delpos234(term->screen, 0);	addpos234(term->scrollback, line, sblen++);    }    assert(count234(term->screen) == newrows);    while (sblen > newsavelines) {	line = delpos234(term->scrollback, 0);	sfree(line);	sblen--;    }    assert(count234(term->scrollback) <= newsavelines);    term->disptop = 0;    newdisp = smalloc(newrows * (newcols + 1) * TSIZE);    for (i = 0; i < newrows * (newcols + 1); i++)	newdisp[i] = ATTR_INVALID;    sfree(term->disptext);    term->disptext = newdisp;    term->dispcurs = NULL;    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] = term->erase_char;        line[newcols + 1] = LATTR_NORM;	addpos234(newalt, line, i);    }    if (term->alt_screen) {	while (NULL != (line = delpos234(term->alt_screen, 0)))	    sfree(line);	freetree234(term->alt_screen);    }    term->alt_screen = newalt;    term->alt_sblines = 0;    term->tabs = srealloc(term->tabs, newcols * sizeof(*term->tabs));    {	int i;	for (i = (term->cols > 0 ? term->cols : 0); i < newcols; i++)	    term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);    }    if (term->rows > 0)	term->curs.y += newrows - term->rows;    if (term->curs.y < 0)	term->curs.y = 0;    if (term->curs.y >= newrows)	term->curs.y = newrows - 1;    if (term->curs.x >= newcols)	term->curs.x = newcols - 1;    term->alt_x = term->alt_y = 0;    term->wrapnext = term->alt_wnext = FALSE;    term->rows = newrows;    term->cols = newcols;    term->savelines = newsavelines;    fix_cpos;    swap_screen(term, save_alt_which, FALSE, FALSE);    update_sbar(term);    term_update(term);    if (term->resize_fn)	term->resize_fn(term->resize_ctx, term->cols, term->rows);}