git: 5fe0a82501cb - main - vt: Fix contents of paste buffer for newcons.

From: Hans Petter Selasky <hselasky_at_FreeBSD.org>
Date: Mon, 27 Jun 2022 08:18:07 UTC
The branch main has been updated by hselasky:

URL: https://cgit.FreeBSD.org/src/commit/?id=5fe0a82501cb86278b9aa426c4bf11d992e95aaa

commit 5fe0a82501cb86278b9aa426c4bf11d992e95aaa
Author:     Hans Petter Selasky <hselasky@FreeBSD.org>
AuthorDate: 2022-06-22 16:38:47 +0000
Commit:     Hans Petter Selasky <hselasky@FreeBSD.org>
CommitDate: 2022-06-27 08:17:16 +0000

    vt: Fix contents of paste buffer for newcons.
    
    Trim all word separators from end of line, except for last line and
    only use '\r' to terminate the pasted lines as expected by TTY.
    
    Submitted by:   Ivan Quitschal <tezeka@hotmail.com>
    Differential Revision:  https://reviews.freebsd.org/D35552
    PR:             263084
    MFC after:      1 week
    Sponsored by:   NVIDIA Networking
---
 sys/dev/vt/vt_buf.c  | 27 ++++++++++++++++++++-------
 sys/dev/vt/vt_core.c |  5 ++---
 2 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/sys/dev/vt/vt_buf.c b/sys/dev/vt/vt_buf.c
index 041c8193ca93..fa6c7c8fec5f 100644
--- a/sys/dev/vt/vt_buf.c
+++ b/sys/dev/vt/vt_buf.c
@@ -741,8 +741,8 @@ vtbuf_get_marked_len(struct vt_buf *vb)
 	si = s.tp_row * vb->vb_scr_size.tp_col + s.tp_col;
 	ei = e.tp_row * vb->vb_scr_size.tp_col + e.tp_col;
 
-	/* Number symbols and number of rows to inject \n */
-	sz = ei - si + ((e.tp_row - s.tp_row) * 2);
+	/* Number symbols and number of rows to inject \r */
+	sz = ei - si + (e.tp_row - s.tp_row);
 
 	return (sz * sizeof(term_char_t));
 }
@@ -750,7 +750,7 @@ vtbuf_get_marked_len(struct vt_buf *vb)
 void
 vtbuf_extract_marked(struct vt_buf *vb, term_char_t *buf, int sz)
 {
-	int i, r, c, cs, ce;
+	int i, j, r, c, cs, ce;
 	term_pos_t s, e;
 
 	/* Swap according to window coordinates. */
@@ -769,15 +769,28 @@ vtbuf_extract_marked(struct vt_buf *vb, term_char_t *buf, int sz)
 	for (r = s.tp_row; r <= e.tp_row; r++) {
 		cs = (r == s.tp_row)?s.tp_col:0;
 		ce = (r == e.tp_row)?e.tp_col:vb->vb_scr_size.tp_col;
-		for (c = cs; c < ce; c++) {
+
+		/* Copy characters from terminal window. */
+		j = i;
+		for (c = cs; c < ce; c++)
 			buf[i++] = vb->vb_rows[r][c];
-		}
-		/* Add new line for all rows, but not for last one. */
+
+		/* For all rows, but the last one. */
 		if (r != e.tp_row) {
+			/* Trim trailing word separators, if any. */
+			for (; i != j; i--) {
+				if (TCHAR_CHARACTER(buf[i - 1]) != ' ')
+					break;
+			}
+			/* Add newline character as expected by TTY. */
 			buf[i++] = '\r';
-			buf[i++] = '\n';
 		}
 	}
+	/* Zero rest of expected buffer size, if any. */
+	while ((i * sizeof(buf[0])) < sz)
+		buf[i++] = '\0';
+
+	MPASS((i * sizeof(buf[0])) == sz);
 }
 
 int
diff --git a/sys/dev/vt/vt_core.c b/sys/dev/vt/vt_core.c
index a623f50c9c06..588ebbcbc4e9 100644
--- a/sys/dev/vt/vt_core.c
+++ b/sys/dev/vt/vt_core.c
@@ -2135,7 +2135,7 @@ vt_mouse_paste()
 	buf = VD_PASTEBUF(main_vd);
 	len /= sizeof(term_char_t);
 	for (i = 0; i < len; i++) {
-		if (buf[i] == '\0')
+		if (TCHAR_CHARACTER(buf[i]) == '\0')
 			continue;
 		terminal_input_char(main_vd->vd_curwindow->vw_terminal,
 		    buf[i]);
@@ -2287,8 +2287,7 @@ vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
 			VD_PASTEBUFSZ(vd) = len;
 		}
 		/* Request copy/paste buffer data, no more than `len' */
-		vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd),
-		    VD_PASTEBUFSZ(vd));
+		vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd), len);
 
 		VD_PASTEBUFLEN(vd) = len;