aboutsummaryrefslogtreecommitdiff
path: root/terminal.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2021-07-18 14:15:18 +0000
committerDaniel Eklöf <daniel@ekloef.se>2021-07-18 14:15:18 +0000
commita577496f8f8ee55099bc47a58fc7c94d354d6602 (patch)
tree5daa9d66676181d886cf4e9adcca61fe21c66a7a /terminal.c
parentMerge branch 'refactor-shm-buffer-management' (diff)
parentchangelog: crash when \E[3J was executed with a selection in the scrollback (diff)
Merge branch 'crash-when-selection-is-in-scrollback-and-scrollback-is-erased'
Closes #633
Diffstat (limited to 'terminal.c')
-rw-r--r--terminal.c160
1 files changed, 160 insertions, 0 deletions
diff --git a/terminal.c b/terminal.c
index 72ab1f2b..b16cf19a 100644
--- a/terminal.c
+++ b/terminal.c
@@ -2011,6 +2011,166 @@ term_erase(struct terminal *term, const struct coord *start, const struct coord
sixel_overwrite_by_row(term, end->row, 0, end->col + 1);
}
+void
+term_erase_scrollback(struct terminal *term)
+{
+ const int mask = term->grid->num_rows - 1;
+ const int start = (term->grid->offset + term->rows) & mask;
+ const int end = (term->grid->offset - 1) & mask;
+ const int sel_start = term->selection.start.row;
+ const int sel_end = term->selection.end.row;
+
+ if (sel_end >= 0) {
+ /*
+ * Cancel selection if it touches any of the rows in the
+ * scrollback, since we can’t have the selection reference
+ * soon-to-be deleted rows.
+ *
+ * This is done by range checking the selection range against
+ * the scrollback range.
+ *
+ * To make this comparison simpler, the start/end absolute row
+ * numbers are “rebased” against the scrollback start, where
+ * row 0 is the *first* row in the scrollback. A high number
+ * thus means the row is further *down* in the scrollback,
+ * closer to the screen bottom.
+ */
+ int scrollback_start = term->grid->offset + term->rows;
+
+ int rel_sel_start = sel_start - scrollback_start + term->grid->num_rows;
+ int rel_sel_end = sel_end - scrollback_start + term->grid->num_rows;
+
+ int rel_start = start - scrollback_start + term->grid->num_rows;
+ int rel_end = end - scrollback_start + term->grid->num_rows;
+
+ rel_sel_start &= mask;
+ rel_sel_end &= mask;
+ rel_start &= mask;
+ rel_end &= mask;
+
+ if ((rel_sel_start <= rel_start && rel_sel_end >= rel_start) ||
+ (rel_sel_start <= rel_end && rel_sel_end >= rel_end) ||
+ (rel_sel_start >= rel_start && rel_sel_end <= rel_end))
+ {
+ selection_cancel(term);
+ }
+ }
+
+ for (int i = start;; i = (i + 1) & mask) {
+ struct row *row = term->grid->rows[i];
+ if (row != NULL) {
+ if (term->render.last_cursor.row == row)
+ term->render.last_cursor.row = NULL;
+
+ grid_row_free(row);
+ term->grid->rows[i] = NULL;
+ }
+
+ if (i == end)
+ break;
+ }
+
+ term->grid->view = term->grid->offset;
+ term_damage_view(term);
+}
+
+UNITTEST
+{
+ const int scrollback_rows = 16;
+ const int term_rows = 5;
+ const int cols = 5;
+
+ struct fdm *fdm = fdm_init();
+ xassert(fdm != NULL);
+
+ struct terminal term = {
+ .fdm = fdm,
+ .rows = term_rows,
+ .cols = cols,
+ .normal = {
+ .rows = xcalloc(scrollback_rows, sizeof(term.normal.rows[0])),
+ .num_rows = scrollback_rows,
+ .num_cols = cols,
+ },
+ .grid = &term.normal,
+ .selection = {
+ .start = {-1, -1},
+ .end = {-1, -1},
+ .kind = SELECTION_NONE,
+ .auto_scroll = {
+ .fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK),
+ },
+ },
+ };
+
+ xassert(term.selection.auto_scroll.fd >= 0);
+
+#define populate_scrollback() do { \
+ for (int i = 0; i < scrollback_rows; i++) { \
+ if (term.normal.rows[i] == NULL) { \
+ struct row *r = xcalloc(1, sizeof(*term.normal.rows[i])); \
+ r->cells = xcalloc(cols, sizeof(r->cells[0])); \
+ term.normal.rows[i] = r; \
+ } \
+ } \
+ } while (0)
+
+ /*
+ * Test case 1 - no selection, just verify all rows except those
+ * on screen have been deleted.
+ */
+
+ populate_scrollback();
+ term.normal.offset = 11;
+ term_erase_scrollback(&term);
+ for (int i = 0; i < scrollback_rows; i++) {
+ if (i >= term.normal.offset && i < term.normal.offset + term_rows)
+ xassert(term.normal.rows[i] != NULL);
+ else
+ xassert(term.normal.rows[i] == NULL);
+ }
+
+ /*
+ * Test case 2 - selection that touches the scrollback. Verify the
+ * selection is cancelled.
+ */
+
+ term.normal.offset = 14; /* Screen covers rows 14,15,0,1,2 */
+
+ /* Selection covers rows 15,0,1,2,3 */
+ term.selection.start = (struct coord){.row = 15};
+ term.selection.end = (struct coord){.row = 19};
+ term.selection.kind = SELECTION_CHAR_WISE;
+
+ populate_scrollback();
+ term_erase_scrollback(&term);
+ xassert(term.selection.start.row < 0);
+ xassert(term.selection.end.row < 0);
+ xassert(term.selection.kind == SELECTION_NONE);
+
+ /*
+ * Test case 3 - selection that does *not* touch the
+ * scrollback. Verify the selection is *not* cancelled.
+ */
+
+ /* Selection covers rows 15,0 */
+ term.selection.start = (struct coord){.row = 15};
+ term.selection.end = (struct coord){.row = 16};
+ term.selection.kind = SELECTION_CHAR_WISE;
+
+ populate_scrollback();
+ term_erase_scrollback(&term);
+ xassert(term.selection.start.row == 15);
+ xassert(term.selection.end.row == 16);
+ xassert(term.selection.kind == SELECTION_CHAR_WISE);
+
+ close(term.selection.auto_scroll.fd);
+ for (int i = 0; i < scrollback_rows; i++)
+ grid_row_free(term.normal.rows[i]);
+ free(term.normal.rows);
+ fdm_destroy(fdm);
+}
+
int
term_row_rel_to_abs(const struct terminal *term, int row)
{