--- madtty000.c 2010-10-08 12:06:05.000000000 -0400 +++ madtty.c 2010-12-06 18:09:57.000000000 -0500 @@ -104,8 +105,8 @@ /* geometry */ int rows, cols, maxcols; - unsigned curattrs; - short curfg, curbg; + unsigned curattrs, scurattrs; + short curfg, curbg, scurfg, scurbg; /* scrollback buffer */ struct t_row_t *scroll_buf; @@ -229,6 +230,33 @@ } } + static void save_curs(madtty_t *t) + { + t->curs_srow = t->curs_row - t->lines; + t->curs_scol = t->curs_col; + } + + static void restore_curs(madtty_t *t) + { + t->curs_row = t->lines + t->curs_srow; + t->curs_col = t->curs_scol; + clamp_cursor_to_bounds(t); + } + + static void save_attrs(madtty_t *t) + { + t->scurattrs = t->curattrs; + t->scurfg = t->curfg; + t->scurbg = t->curbg; + } + + static void restore_attrs(madtty_t *t) + { + t->curattrs = t->scurattrs; + t->curfg = t->scurfg; + t->curbg = t->scurbg; + } + static void clamp_cursor_to_bounds(madtty_t *t) { if (t->curs_row < t->lines) { @@ -393,6 +421,10 @@ { t_row_t *row, *start, *end; + save_attrs(t); + t->curattrs = A_NORMAL; + t->curfg = t->curbg = -1; + /* decide range */ if (pcount && param[0] == 2) { start = t->lines; @@ -412,6 +444,8 @@ for (row = start; row < end; row++) { t_row_set(row, 0, t->cols, t); } + + restore_attrs(t); } /* interprets a 'move cursor' (CUP) escape sequence */ @@ -687,14 +721,9 @@ case 'r': /* set scrolling region */ interpret_csi_DECSTBM(t, csiparam, param_count); break; case 's': /* save cursor location */ - t->curs_srow = t->curs_row - t->lines; - t->curs_scol = t->curs_col; - break; + save_curs(t); break; case 'u': /* restore cursor location */ - t->curs_row = t->lines + t->curs_srow; - t->curs_col = t->curs_scol; - clamp_cursor_to_bounds(t); - break; + restore_curs(t); break; default: break; } @@ -742,6 +771,18 @@ return; } break; + + case '7': /* save cursor and attrs */ + save_attrs(t); + save_curs(t); + cancel_escape_sequence(t); + return; + + case '8': /* restore cursor and attrs */ + restore_attrs(t); + restore_curs(t); + cancel_escape_sequence(t); + return; } if (t->elen + 1 >= (int)sizeof(t->ebuf)) { @@ -1224,7 +1265,7 @@ maxfd = sysconf(_SC_OPEN_MAX); for (fd = 3; fd < maxfd; fd++) - if (close(fd) == EBADF) + if (close(fd) == -1 && errno == EBADF) break; while (envp && envp[0]) {