Skip to contentSkip to conversation

readline: make ctrl-u to work like linux console

PATCH Superseded [email protected] 2 participants 2 messages Oct 16, 2019 Library code

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 readline: make ctrl-u to work like linux console

From: DU HUANPENG <[email protected]>

currtly, the ctrl-u discards the whole line, in most linux boxes, ctrl-u just erase characters before cursor to the begginning of the line. this patch make ctrl-u to do this.

---
 lib/readline.c | 34 +++++++++++++++++++++++++++++++---
 1 file changed, 31 insertions(+), 3 deletions(-)
lib/readline.c +31 -3
index d026af1..c0b194c 100644
@@ -188,9 +188,10 @@ int readline(const char *prompt, char *buf, int len)
 	unsigned wlen;
 	int ichar;
 	int insert = 1;
+	int i;
 #ifdef CONFIG_AUTO_COMPLETE
 	char tmp;
-	int reprint, i;
+	int reprint;
 	char *completestr;
 
 	complete_reset();
@@ -291,8 +292,35 @@ int readline(const char *prompt, char *buf, int len)
 			break;
 		case BB_KEY_ERASE_LINE:
 		case CTL_CH('u'):
-			BEGINNING_OF_LINE();
-			ERASE_TO_EOL();
+			if(num >= eol_num) {
+				BEGINNING_OF_LINE();
+				ERASE_TO_EOL();
+			} else {
+				for(i=num; i<eol_num; i++) {
+					buf[i-num] = buf[i];
+				}
+				buf[i] = '\0';
+
+				for(i=0; i<num; i++) {
+					getcmd_putch(CTL_BACKSPACE);
+				}
+				for(i=0; i<eol_num; i++) {
+					getcmd_putch(' ');
+				}
+				for(i=0; i<eol_num; i++) {
+					getcmd_putch(CTL_BACKSPACE);
+				}
+
+				eol_num -= num;
+				num = 0;
+
+				for(i=0; i<eol_num; i++) {
+					getcmd_putch(buf[i]);
+				}
+				for(i=0; i<eol_num; i++) {
+					getcmd_putch(CTL_BACKSPACE);
+				}
+			}
 			break;
 		case DEL:
 		case BB_KEY_DEL7:
Signature · 7 lines
2.7.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

Hi,

Am 16.10.19 um 07:34 schrieb [email protected]:

Quoted 5 lines

From: DU HUANPENG <[email protected]>

currtly, the ctrl-u discards the whole line, in most linux boxes, ctrl-u just erase characters before cursor to the begginning of the line. this patch make ctrl-u to do this.

Cool, didnÄt know i can use ctrl-u like this :)

Quoted 23 lines
---
 lib/readline.c | 34 +++++++++++++++++++++++++++++++---
 1 file changed, 31 insertions(+), 3 deletions(-)
lib/readline.c +2 -1
index d026af1..c0b194c 100644
@@ -188,9 +188,10 @@ int readline(const char *prompt, char *buf, int len)
 	unsigned wlen;
 	int ichar;
 	int insert = 1;
+	int i;
 #ifdef CONFIG_AUTO_COMPLETE
 	char tmp;
-	int reprint, i;
+	int reprint;
 	char *completestr;
 
 	complete_reset();

lib/readline.c int readline(const char *prompt, char *buf, int len) · line 297

Quoted 8 lines
lib/readline.c +3 -2
@@ -291,8 +292,35 @@ int readline(const char *prompt, char *buf, int len)
 			break;
 		case BB_KEY_ERASE_LINE:
 		case CTL_CH('u'):
-			BEGINNING_OF_LINE();
-			ERASE_TO_EOL();
+			if(num >= eol_num) {
+				BEGINNING_OF_LINE();
+				ERASE_TO_EOL();

Please use kernel/barebox coding style. ./scripts/checkpatch.pl in barebox repository can help you.

Quoted 20 lines
lib/readline.c +19 -0
+			} else {
+				for(i=num; i<eol_num; i++) {
+					buf[i-num] = buf[i];
+				}
+				buf[i] = '\0';
+
+				for(i=0; i<num; i++) {
+					getcmd_putch(CTL_BACKSPACE);
+				}
+				for(i=0; i<eol_num; i++) {
+					getcmd_putch(' ');
+				}
+				for(i=0; i<eol_num; i++) {
+					getcmd_putch(CTL_BACKSPACE);
+				}
+
+				eol_num -= num;
+				num = 0;
+

lib/readline.c int readline(const char *prompt, char *buf, int len) · line 326

Quoted 10 lines
lib/readline.c +7 -0
+				for(i=0; i<eol_num; i++) {
+					getcmd_putch(buf[i]);
+				}
+				for(i=0; i<eol_num; i++) {
+					getcmd_putch(CTL_BACKSPACE);
+				}
+			}
 			break;
 		case DEL:
 		case BB_KEY_DEL7:

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox