diff mbox

[U-Boot,05/15] common/main.c: Fix function readline

Message ID 1324244051-25756-5-git-send-email-pali.rohar@gmail.com
State Superseded, archived
Headers show

Commit Message

Pali Rohár Dec. 18, 2011, 9:34 p.m. UTC
* Ignore ANSI escape codes for moving cursor, which are generated by keyboard

Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
---
 common/main.c |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 51 insertions(+), 0 deletions(-)

Comments

Mike Frysinger Jan. 6, 2012, 8:15 p.m. UTC | #1
On Sunday 18 December 2011 16:34:01 Pali Rohár wrote:
>  * Ignore ANSI escape codes for moving cursor, which are generated by
> keyboard

this probably should be behind an appropriate CONFIG knob.  i don't think this 
is relevant to serial users (which are the majority of boards).
-mike
Pali Rohár Jan. 7, 2012, 8:51 a.m. UTC | #2
On Friday 06 January 2012 15:15:29 Mike Frysinger wrote:
> On Sunday 18 December 2011 16:34:01 Pali Rohár wrote:
> >  * Ignore ANSI escape codes for moving cursor, which are generated by
> > 
> > keyboard
> 
> this probably should be behind an appropriate CONFIG knob.  i don't think
> this is relevant to serial users (which are the majority of boards).
> -mike

I think this is also relevant to serial console too. I tried serial console 
(over USB) with Nokia N900 on computer (ANSI terminal) and if I pressed cursor 
key (UP/DOWN/LEFT/RIGHT) it moved cursor. But in readline function buffer was 
stored ANSI sequence. So when I pressed ENTER, u-boot tried to execute that 
buffer and showed me error. But it showed error message something like unknown 
command <ansi_seq>. ansi_seq was recognized in ansi terminal on computer and 
it garbaged output. (for example if asn_seq was move up, error message 
continued on previous line...)

This patch ignore ansi codes which can be generated by curosor keys, so there 
will not be that bad error messages...
Mike Frysinger Feb. 27, 2012, 4:33 a.m. UTC | #3
On Saturday 07 January 2012 03:51:16 Pali Rohár wrote:
> On Friday 06 January 2012 15:15:29 Mike Frysinger wrote:
> > On Sunday 18 December 2011 16:34:01 Pali Rohár wrote:
> > >  * Ignore ANSI escape codes for moving cursor, which are generated by
> > > 
> > > keyboard
> > 
> > this probably should be behind an appropriate CONFIG knob.  i don't think
> > this is relevant to serial users (which are the majority of boards).
> 
> I think this is also relevant to serial console too. I tried serial console
> (over USB) with Nokia N900 on computer (ANSI terminal) and if I pressed
> cursor key (UP/DOWN/LEFT/RIGHT) it moved cursor. But in readline function
> buffer was stored ANSI sequence. So when I pressed ENTER, u-boot tried to
> execute that buffer and showed me error. But it showed error message
> something like unknown command <ansi_seq>. ansi_seq was recognized in ansi
> terminal on computer and it garbaged output. (for example if asn_seq was
> move up, error message continued on previous line...)
> 
> This patch ignore ansi codes which can be generated by curosor keys, so
> there will not be that bad error messages...

do you have CONFIG_CMDLINE_EDITING enabled ?  arrow keys work just fine when 
that's enabled, and if it isn't, then i think it's expected behavior that 
pressing the arrow keys would generate "junk" that u-boot wouldn't interpret.
-mike
Pali Rohár Feb. 27, 2012, 6:40 p.m. UTC | #4
On Sunday 26 February 2012 23:33:01 Mike Frysinger wrote:
> On Saturday 07 January 2012 03:51:16 Pali Rohár wrote:
> > On Friday 06 January 2012 15:15:29 Mike Frysinger wrote:
> > > On Sunday 18 December 2011 16:34:01 Pali Rohár wrote:
> > > >  * Ignore ANSI escape codes for moving cursor, which are generated by
> > > > 
> > > > keyboard
> > > 
> > > this probably should be behind an appropriate CONFIG knob.  i don't
> > > think
> > > this is relevant to serial users (which are the majority of boards).
> > 
> > I think this is also relevant to serial console too. I tried serial
> > console
> > (over USB) with Nokia N900 on computer (ANSI terminal) and if I pressed
> > cursor key (UP/DOWN/LEFT/RIGHT) it moved cursor. But in readline function
> > buffer was stored ANSI sequence. So when I pressed ENTER, u-boot tried to
> > execute that buffer and showed me error. But it showed error message
> > something like unknown command <ansi_seq>. ansi_seq was recognized in ansi
> > terminal on computer and it garbaged output. (for example if asn_seq was
> > move up, error message continued on previous line...)
> > 
> > This patch ignore ansi codes which can be generated by curosor keys, so
> > there will not be that bad error messages...
> 
> do you have CONFIG_CMDLINE_EDITING enabled ?  arrow keys work just fine when
> that's enabled, and if it isn't, then i think it's expected behavior that
> pressing the arrow keys would generate "junk" that u-boot wouldn't
> interpret. -mike

I had CONFIG_CMDLINE_EDITING disabled - I did not know this option. Anyway, 
when I enabled it arrow keys does not worked with cfb video driver and rx51 
platform code.

I will try to fix that problem and I then will delete this patch.
Mike Frysinger Feb. 27, 2012, 8:20 p.m. UTC | #5
On Monday 27 February 2012 13:40:07 Pali Rohár wrote:
> On Sunday 26 February 2012 23:33:01 Mike Frysinger wrote:
> > On Saturday 07 January 2012 03:51:16 Pali Rohár wrote:
> > > On Friday 06 January 2012 15:15:29 Mike Frysinger wrote:
> > > > On Sunday 18 December 2011 16:34:01 Pali Rohár wrote:
> > > > >  * Ignore ANSI escape codes for moving cursor, which are generated
> > > > >  by
> > > > > 
> > > > > keyboard
> > > > 
> > > > this probably should be behind an appropriate CONFIG knob.  i don't
> > > > think
> > > > this is relevant to serial users (which are the majority of boards).
> > > 
> > > I think this is also relevant to serial console too. I tried serial
> > > console
> > > (over USB) with Nokia N900 on computer (ANSI terminal) and if I pressed
> > > cursor key (UP/DOWN/LEFT/RIGHT) it moved cursor. But in readline
> > > function buffer was stored ANSI sequence. So when I pressed ENTER,
> > > u-boot tried to execute that buffer and showed me error. But it showed
> > > error message something like unknown command <ansi_seq>. ansi_seq was
> > > recognized in ansi terminal on computer and it garbaged output. (for
> > > example if asn_seq was move up, error message continued on previous
> > > line...)
> > > 
> > > This patch ignore ansi codes which can be generated by curosor keys, so
> > > there will not be that bad error messages...
> > 
> > do you have CONFIG_CMDLINE_EDITING enabled ?  arrow keys work just fine
> > when that's enabled, and if it isn't, then i think it's expected
> > behavior that pressing the arrow keys would generate "junk" that u-boot
> > wouldn't interpret.
> 
> I had CONFIG_CMDLINE_EDITING disabled - I did not know this option. Anyway,
> when I enabled it arrow keys does not worked with cfb video driver and rx51
> platform code.
> 
> I will try to fix that problem and I then will delete this patch.

sounds good.  not sure if this something we could really document in a way 
that'd help people :/.
-mike
diff mbox

Patch

diff --git a/common/main.c b/common/main.c
index e96c95a..e7b5516 100644
--- a/common/main.c
+++ b/common/main.c
@@ -958,6 +958,7 @@  int readline_into_buffer (const char *const prompt, char * buffer)
 	int	n = 0;				/* buffer index		*/
 	int	plen = 0;			/* prompt length	*/
 	int	col;				/* output column cnt	*/
+	int	esc = 0;			/* ansi escape char	*/
 	char	c;
 
 	/* print prompt */
@@ -1024,7 +1025,57 @@  int readline_into_buffer (const char *const prompt, char * buffer)
 			p=delete_char(p_buf, p, &col, &n, plen);
 			continue;
 
+		case '\e':			/* ANSI escape char	*/
+			esc = 1;
+			continue;
+
 		default:
+
+			/*
+			 * Check for ANSI escape chars
+			 */
+			if (esc == 0 && c == '\e') {
+				esc = 1;
+				continue;
+			} else if (esc == 1) {
+				if (c == '[') {
+					esc = 2;
+					continue;
+				}
+				if (n < CONFIG_SYS_CBSIZE-2) {
+					++n;
+					*p++ = '\e';
+					putc('\e');
+				}
+				esc = 0;
+			} else if (esc == 2 || esc == 3) {
+				if (esc == 2 && c == '1') {
+					esc = 3;
+					continue;
+				}
+				/* Ignore ANSI escape sequences */
+				/* generated by keyboard */
+				/* \e [ 1 A-D and \e [ A-D */
+				if (c >= 'A' && c <= 'D') {
+					esc = 0;
+					continue;
+				}
+				if (esc == 2 && n < CONFIG_SYS_CBSIZE-3) {
+					n += 2;
+					*p++ = '\e';
+					*p++ = '[';
+					puts("\e[");
+				} else if (esc == 3 &&
+						n < CONFIG_SYS_CBSIZE-4) {
+					n += 3;
+					*p++ = '\e';
+					*p++ = '[';
+					*p++ = '1';
+					puts("\e[1");
+				}
+				esc = 0;
+			}
+
 			/*
 			 * Must be a normal character then
 			 */