Patchwork [U-Boot,04/14] Fix function readline in main.c

login
register
mail settings
Submitter Pali Rohár
Date Jan. 24, 2012, 2:28 p.m.
Message ID <1327415291-13260-5-git-send-email-pali.rohar@gmail.com>
Download mbox | patch
Permalink /patch/137561/
State Superseded
Headers show

Comments

Pali Rohár - Jan. 24, 2012, 2:28 p.m.
* Ignore ANSI escape codes for moving cursor, which are generated by keyboard

Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
---
Changes since original version:
   - Fixed commit message

 common/main.c |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 51 insertions(+), 0 deletions(-)
Marek Vasut - Jan. 25, 2012, 6:05 p.m.
>  * Ignore ANSI escape codes for moving cursor, which are generated by
> keyboard

I think WD should comment on this one.

> 
> Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
> ---
> Changes since original version:
>    - Fixed commit message
> 
>  common/main.c |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 files changed, 51 insertions(+), 0 deletions(-)
> 
> 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
>  			 */
Mike Frysinger - Feb. 27, 2012, 8:39 p.m.
On Wednesday 25 January 2012 13:05:31 Marek Vasut wrote:
> >  * Ignore ANSI escape codes for moving cursor, which are generated by
> > keyboard
> 
> I think WD should comment on this one.

no need ... see the previous thread on the previous version of this patch.  
Pali should be able to drop this.
-mike

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
 			 */