Blame SOURCES/0007-Fix-backspace-when-editing-a-multiline-cmdline.patch

87e39f
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
87e39f
From: Robbie Harwood <rharwood@redhat.com>
87e39f
Date: Thu, 21 Oct 2021 15:15:33 -0400
87e39f
Subject: [PATCH] Fix backspace when editing a multiline cmdline
87e39f
87e39f
Once the cmdline had passed the width of the screen, adding additional
87e39f
characters introduced a spurious newline, and another newline at the
87e39f
width of input.  Furthermore, hitting backspace would not start
87e39f
redrawing at the end of input, but rather at the beginning of the
87e39f
current line - resulting in extra duplicate lines scrolling the console.
87e39f
87e39f
First, fix the assumption that the length of cmdline is the width - it
87e39f
needs to include the length of the prompt (i.e., length of input and
87e39f
space).
87e39f
87e39f
Second, fix the behavior of single-line redraw (i.e., redraw == 1) to
87e39f
move the cursor to the row the line begins at.
87e39f
87e39f
Third, don't scroll the cursor down when a line wrap would occur - it's
87e39f
not necessary since line wrap is enabled, and results in the extra blank
87e39f
line.
87e39f
87e39f
Finally, comment all used escape sequences so that I don't need to look
87e39f
them up again.
87e39f
87e39f
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
87e39f
---
87e39f
 com32/elflink/ldlinux/cli.c | 33 ++++++++++++++++++---------------
87e39f
 1 file changed, 18 insertions(+), 15 deletions(-)
87e39f
87e39f
diff --git a/com32/elflink/ldlinux/cli.c b/com32/elflink/ldlinux/cli.c
87e39f
index 3119b11f..4913f038 100644
87e39f
--- a/com32/elflink/ldlinux/cli.c
87e39f
+++ b/com32/elflink/ldlinux/cli.c
87e39f
@@ -135,6 +135,7 @@ const char *edit_cmdline(const char *input, int top /*, int width */ ,
87e39f
     struct cli_command *comm_counter = NULL;
87e39f
     clock_t kbd_to = kbdtimeout;
87e39f
     clock_t tto = totaltimeout;
87e39f
+    int prompt_len = 1 + strlen(input);
87e39f
 
87e39f
     if (!width) {
87e39f
 	int height;
87e39f
@@ -144,7 +145,7 @@ const char *edit_cmdline(const char *input, int top /*, int width */ ,
87e39f
 
87e39f
     len = cursor = 0;
87e39f
     prev_len = 0;
87e39f
-    x = y = 0;
87e39f
+    y = 0;
87e39f
 
87e39f
     /*
87e39f
      * Before we start messing with the x,y coordinates print 'input'
87e39f
@@ -152,6 +153,7 @@ const char *edit_cmdline(const char *input, int top /*, int width */ ,
87e39f
      * previously.
87e39f
      */
87e39f
     printf("%s ", input);
87e39f
+    x = prompt_len;
87e39f
 
87e39f
     while (!done) {
87e39f
 	if (redraw > 1) {
87e39f
@@ -162,8 +164,7 @@ const char *edit_cmdline(const char *input, int top /*, int width */ ,
87e39f
 	    if (pDraw_Menu)
87e39f
 		    (*pDraw_Menu) (-1, top, 1);
87e39f
 	    prev_len = 0;
87e39f
-	    printf("\033[2J\033[H");
87e39f
-	    // printf("\033[0m\033[2J\033[H");
87e39f
+	    printf("\033[2J\033[H"); /* Clear entire screen; move to 0, 0. */
87e39f
 	}
87e39f
 
87e39f
 	if (redraw > 0) {
87e39f
@@ -172,10 +173,14 @@ const char *edit_cmdline(const char *input, int top /*, int width */ ,
87e39f
 	    prev_len = max(len, prev_len);
87e39f
 
87e39f
 	    /* Redraw the command line */
87e39f
-	    printf("\033[?25l");
87e39f
-	    printf("\033[1G%s ", input);
87e39f
+	    printf("\033[?25l"); /* Hide cursor. */
87e39f
+	    printf("\033[1G"); /* Column 1. */
87e39f
+	    if (y > 0)
87e39f
+		printf("\033[%dA", y); /* Directly up. */
87e39f
 
87e39f
-	    x = strlen(input);
87e39f
+	    printf("%s ", input);
87e39f
+
87e39f
+	    x = prompt_len;
87e39f
 	    y = 0;
87e39f
 	    at = 0;
87e39f
 	    while (at < prev_len) {
87e39f
@@ -183,23 +188,22 @@ const char *edit_cmdline(const char *input, int top /*, int width */ ,
87e39f
 		at++;
87e39f
 		x++;
87e39f
 		if (x >= width) {
87e39f
-		    printf("\r\n");
87e39f
 		    x = 0;
87e39f
 		    y++;
87e39f
 		}
87e39f
 	    }
87e39f
-	    printf("\033[K\r");
87e39f
+	    printf("\033[K\r"); /* Clear to end of line; go to beginning. */
87e39f
 
87e39f
-	    dy = y - (cursor + strlen(input) + 1) / width;
87e39f
-	    x = (cursor + strlen(input) + 1) % width;
87e39f
+	    dy = y - (cursor + prompt_len) / width;
87e39f
+	    x = (cursor + prompt_len) % width;
87e39f
 
87e39f
 	    if (dy) {
87e39f
-		printf("\033[%dA", dy);
87e39f
+		printf("\033[%dA", dy); /* Cursor directly up. */
87e39f
 		y -= dy;
87e39f
 	    }
87e39f
 	    if (x)
87e39f
-		printf("\033[%dC", x);
87e39f
-	    printf("\033[?25h");
87e39f
+		printf("\033[%dC", x); /* Cursor forward. */
87e39f
+	    printf("\033[?25h"); /* Show cursor. */
87e39f
 	    prev_len = len;
87e39f
 	    redraw = 0;
87e39f
 	}
87e39f
@@ -439,7 +443,6 @@ const char *edit_cmdline(const char *input, int top /*, int width */ ,
87e39f
 		    cursor++;
87e39f
 		    x++;
87e39f
 		    if (x >= width) {
87e39f
-			printf("\r\n\033[K");
87e39f
 			y++;
87e39f
 			x = 0;
87e39f
 		    }
87e39f
@@ -459,7 +462,7 @@ const char *edit_cmdline(const char *input, int top /*, int width */ ,
87e39f
 	}
87e39f
     }
87e39f
 
87e39f
-    printf("\033[?7h");
87e39f
+    printf("\033[?7h"); /* Enable line wrap. */
87e39f
 
87e39f
     /* Add the command to the history if its length is larger than 0 */
87e39f
     len = strlen(ret);