From aafa120f92b2f41d1a753493676ee7ee0cb7a1ff Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 15 Jul 2020 11:19:18 +0200 Subject: selection: refactor: break out text extraction to a separate file --- extract.c | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 extract.c (limited to 'extract.c') diff --git a/extract.c b/extract.c new file mode 100644 index 00000000..b119bc3f --- /dev/null +++ b/extract.c @@ -0,0 +1,163 @@ +#include "extract.h" +#include + +#define LOG_MODULE "extract" +#define LOG_ENABLE_DBG 1 +#include "log.h" + +struct extraction_context { + wchar_t *buf; + size_t size; + size_t idx; + size_t empty_count; + const struct row *last_row; + const struct cell *last_cell; + enum selection_kind selection_kind; +}; + +struct extraction_context * +extract_begin(enum selection_kind kind) +{ + struct extraction_context *ctx = malloc(sizeof(*ctx)); + *ctx = (struct extraction_context){ + .selection_kind = kind, + }; + return ctx; +} + +bool +extract_finish(struct extraction_context *ctx, char **text, size_t *len) +{ + if (text == NULL) + return false; + + if (ctx->idx == 0) { + /* Selection of empty cells only */ + ctx->buf[ctx->idx] = L'\0'; + } else { + assert(ctx->idx > 0); + assert(ctx->idx < ctx->size); + if (ctx->buf[ctx->idx - 1] == L'\n') + ctx->buf[ctx->idx - 1] = L'\0'; + else + ctx->buf[ctx->idx] = L'\0'; + } + + bool ret = false; + + size_t _len = wcstombs(NULL, ctx->buf, 0); + if (_len == (size_t)-1) { + LOG_ERRNO("failed to convert selection to UTF-8"); + goto out; + } + + *text = malloc(_len + 1); + wcstombs(*text, ctx->buf, _len + 1); + + if (len != NULL) + *len = _len; + + ret = true; + +out: + free(ctx->buf); + free(ctx); + return ret; +} + +static bool +ensure_size(struct extraction_context *ctx, size_t additional_chars) +{ + while (ctx->size < ctx->idx + additional_chars) { + size_t new_size = ctx->size == 0 ? 512 : ctx->size * 2; + wchar_t *new_buf = realloc(ctx->buf, new_size * sizeof(wchar_t)); + + if (new_buf == NULL) + return false; + + ctx->buf = new_buf; + ctx->size = new_size; + } + + assert(ctx->size >= ctx->idx + additional_chars); + return true; +} + +bool +extract_one(struct terminal *term, struct row *row, struct cell *cell, + int col, void *context) +{ + struct extraction_context *ctx = context; + + if (cell->wc == CELL_MULT_COL_SPACER) + return true; + + if (ctx->last_row != NULL && row != ctx->last_row) { + /* New row - determine if we should insert a newline or not */ + + if (ctx->selection_kind == SELECTION_NONE || + ctx->selection_kind == SELECTION_NORMAL) + { + if (ctx->last_row->linebreak || + ctx->empty_count > 0 || + cell->wc == 0) + { + /* Row has a hard linebreak, or either last cell or + * current cell is empty */ + if (!ensure_size(ctx, 1)) + return false; + + ctx->buf[ctx->idx++] = L'\n'; + ctx->empty_count = 0; + } + } + + else if (ctx->selection_kind == SELECTION_BLOCK) { + /* Always insert a linebreak */ + if (!ensure_size(ctx, 1)) + return false; + + ctx->buf[ctx->idx++] = L'\n'; + ctx->empty_count = 0; + } + } + + if (cell->wc == 0) { + ctx->empty_count++; + ctx->last_row = row; + ctx->last_cell = cell; + return true; + } + + /* Replace empty cells with spaces when followed by non-empty cell */ + if (!ensure_size(ctx, ctx->empty_count)) + return false; + + for (size_t i = 0; i < ctx->empty_count; i++) + ctx->buf[ctx->idx++] = L' '; + ctx->empty_count = 0; + + if (cell->wc >= CELL_COMB_CHARS_LO && + cell->wc < (CELL_COMB_CHARS_LO + term->composed_count)) + { + const struct composed *composed + = &term->composed[cell->wc - CELL_COMB_CHARS_LO]; + + if (!ensure_size(ctx, 1 + composed->count)) + return false; + + ctx->buf[ctx->idx++] = composed->base; + for (size_t i = 0; i < composed->count; i++) + ctx->buf[ctx->idx++] = composed->combining[i]; + } + + else { + if (!ensure_size(ctx, 1)) + return false; + ctx->buf[ctx->idx++] = cell->wc; + } + + ctx->last_row = row; + ctx->last_cell = cell; + return true; +} -- cgit v1.3 From 2539e3cbb2fed78f446338ecd589eeb505ae9f45 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 15 Jul 2020 11:31:38 +0200 Subject: extract: extract_one: make arguments const --- extract.c | 4 ++-- extract.h | 4 ++-- terminal.h | 3 +++ 3 files changed, 7 insertions(+), 4 deletions(-) (limited to 'extract.c') diff --git a/extract.c b/extract.c index b119bc3f..663f94ef 100644 --- a/extract.c +++ b/extract.c @@ -84,8 +84,8 @@ ensure_size(struct extraction_context *ctx, size_t additional_chars) } bool -extract_one(struct terminal *term, struct row *row, struct cell *cell, - int col, void *context) +extract_one(const struct terminal *term, const struct row *row, + const struct cell *cell, int col, void *context) { struct extraction_context *ctx = context; diff --git a/extract.h b/extract.h index 2219780d..0b3693d2 100644 --- a/extract.h +++ b/extract.h @@ -10,8 +10,8 @@ struct extraction_context; struct extraction_context *extract_begin(enum selection_kind kind); bool extract_one( - struct terminal *term, struct row *row, struct cell *cell, int col, - void *context); + const struct terminal *term, const struct row *row, const struct cell *cell, + int col, void *context); bool extract_finish( struct extraction_context *context, char **text, size_t *len); diff --git a/terminal.h b/terminal.h index 4d5f717b..fece4b50 100644 --- a/terminal.h +++ b/terminal.h @@ -542,3 +542,6 @@ void term_disable_app_sync_updates(struct terminal *term); enum term_surface term_surface_kind( const struct terminal *term, const struct wl_surface *surface); + +bool term_scrollback_to_text(const struct terminal *term, wchar_t **text); +bool term_view_to_text(const struct terminal *term, wchar_t **text); -- cgit v1.3 From e6acafa11871af8992d5935f3f60d18c79e791b1 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 15 Jul 2020 11:32:40 +0200 Subject: extract: extract_one() sets a fail flag that extract_finish() reads This allows us to safely call extract_finish() when extract_one() failed, and we'll get the expected result; false, indicating extract_finish() failed. --- extract.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'extract.c') diff --git a/extract.c b/extract.c index 663f94ef..49cd198a 100644 --- a/extract.c +++ b/extract.c @@ -10,6 +10,7 @@ struct extraction_context { size_t size; size_t idx; size_t empty_count; + bool failed; const struct row *last_row; const struct cell *last_cell; enum selection_kind selection_kind; @@ -28,9 +29,18 @@ extract_begin(enum selection_kind kind) bool extract_finish(struct extraction_context *ctx, char **text, size_t *len) { + bool ret = false; + if (text == NULL) return false; + *text = NULL; + if (len != NULL) + *len = 0; + + if (ctx->failed) + goto out; + if (ctx->idx == 0) { /* Selection of empty cells only */ ctx->buf[ctx->idx] = L'\0'; @@ -43,8 +53,6 @@ extract_finish(struct extraction_context *ctx, char **text, size_t *len) ctx->buf[ctx->idx] = L'\0'; } - bool ret = false; - size_t _len = wcstombs(NULL, ctx->buf, 0); if (_len == (size_t)-1) { LOG_ERRNO("failed to convert selection to UTF-8"); @@ -105,7 +113,7 @@ extract_one(const struct terminal *term, const struct row *row, /* Row has a hard linebreak, or either last cell or * current cell is empty */ if (!ensure_size(ctx, 1)) - return false; + goto err; ctx->buf[ctx->idx++] = L'\n'; ctx->empty_count = 0; @@ -115,7 +123,7 @@ extract_one(const struct terminal *term, const struct row *row, else if (ctx->selection_kind == SELECTION_BLOCK) { /* Always insert a linebreak */ if (!ensure_size(ctx, 1)) - return false; + goto err; ctx->buf[ctx->idx++] = L'\n'; ctx->empty_count = 0; @@ -131,7 +139,7 @@ extract_one(const struct terminal *term, const struct row *row, /* Replace empty cells with spaces when followed by non-empty cell */ if (!ensure_size(ctx, ctx->empty_count)) - return false; + goto err; for (size_t i = 0; i < ctx->empty_count; i++) ctx->buf[ctx->idx++] = L' '; @@ -144,7 +152,7 @@ extract_one(const struct terminal *term, const struct row *row, = &term->composed[cell->wc - CELL_COMB_CHARS_LO]; if (!ensure_size(ctx, 1 + composed->count)) - return false; + goto err; ctx->buf[ctx->idx++] = composed->base; for (size_t i = 0; i < composed->count; i++) @@ -153,11 +161,15 @@ extract_one(const struct terminal *term, const struct row *row, else { if (!ensure_size(ctx, 1)) - return false; + goto err; ctx->buf[ctx->idx++] = cell->wc; } ctx->last_row = row; ctx->last_cell = cell; return true; + +err: + ctx->failed = true; + return false; } -- cgit v1.3 From e5401c845c058d53aff9586cf12e0074e74059aa Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 16 Jul 2020 17:46:02 +0200 Subject: extract: finish: allocate buffer before writing the terminator When all cells were empty, we'll have written 0 bytes to the buffer and hence it will still be un-allocated (i.e. NULL). --- extract.c | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) (limited to 'extract.c') diff --git a/extract.c b/extract.c index 49cd198a..b626d3e6 100644 --- a/extract.c +++ b/extract.c @@ -26,6 +26,23 @@ extract_begin(enum selection_kind kind) return ctx; } +static bool +ensure_size(struct extraction_context *ctx, size_t additional_chars) +{ + while (ctx->size < ctx->idx + additional_chars) { + size_t new_size = ctx->size == 0 ? 512 : ctx->size * 2; + wchar_t *new_buf = realloc(ctx->buf, new_size * sizeof(wchar_t)); + + if (new_buf == NULL) + return false; + + ctx->buf = new_buf; + ctx->size = new_size; + } + + assert(ctx->size >= ctx->idx + additional_chars); + return true; +} bool extract_finish(struct extraction_context *ctx, char **text, size_t *len) { @@ -43,6 +60,8 @@ extract_finish(struct extraction_context *ctx, char **text, size_t *len) if (ctx->idx == 0) { /* Selection of empty cells only */ + if (!ensure_size(ctx, 1)) + goto out; ctx->buf[ctx->idx] = L'\0'; } else { assert(ctx->idx > 0); @@ -73,24 +92,6 @@ out: return ret; } -static bool -ensure_size(struct extraction_context *ctx, size_t additional_chars) -{ - while (ctx->size < ctx->idx + additional_chars) { - size_t new_size = ctx->size == 0 ? 512 : ctx->size * 2; - wchar_t *new_buf = realloc(ctx->buf, new_size * sizeof(wchar_t)); - - if (new_buf == NULL) - return false; - - ctx->buf = new_buf; - ctx->size = new_size; - } - - assert(ctx->size >= ctx->idx + additional_chars); - return true; -} - bool extract_one(const struct terminal *term, const struct row *row, const struct cell *cell, int col, void *context) -- cgit v1.3