[vlc-commits] lua: cli: Fix non-ascii character input

2018-03-21 Thread Hugo Beauzée-Luyssen
vlc/vlc-3.0 | branch: master | Hugo Beauzée-Luyssen  | Mon Mar 
12 10:28:53 2018 +0100| [1ebfffd3dc467948fe5827725bb9889f2cb62997] | committer: 
Hugo Beauzée-Luyssen

lua: cli: Fix non-ascii character input

refs #19874

(cherry picked from commit 1f1291aedc994dc218bfc5b9ab3ba0a32c19909b)
Signed-off-by: Hugo Beauzée-Luyssen 
(cherry picked from commit 27852dd4c2a99c9fd2a890e1cb2941a448ea9bf6)
Signed-off-by: Hugo Beauzée-Luyssen 

> http://git.videolan.org/gitweb.cgi/vlc/vlc-3.0.git/?a=commit;h=1ebfffd3dc467948fe5827725bb9889f2cb62997
---

 modules/lua/libs/win.c | 80 ++
 1 file changed, 48 insertions(+), 32 deletions(-)

diff --git a/modules/lua/libs/win.c b/modules/lua/libs/win.c
index 7d0276331e..fc037bf65e 100644
--- a/modules/lua/libs/win.c
+++ b/modules/lua/libs/win.c
@@ -48,12 +48,15 @@ static HANDLE GetConsole( lua_State *L )
 
 #define MAX_LINE_LENGTH 1024
 
-static bool ReadWin32( HANDLE *hConsoleIn, char *p_buffer, int *pi_size )
+static bool ReadWin32( HANDLE *hConsoleIn, unsigned char *p_buffer, int 
*pi_size )
 {
 INPUT_RECORD input_record;
 DWORD i_dw;
 
-while( *pi_size < MAX_LINE_LENGTH &&
+// Prefer to fail early when there's not enough space to store a 4 bytes
+// UTF8 character. The function will be immediatly called again and we 
won't
+// lose an input
+while( *pi_size < MAX_LINE_LENGTH - 4 &&
ReadConsoleInput( hConsoleIn, &input_record, 1, &i_dw ) )
 {
 if( input_record.EventType != KEY_EVENT ||
@@ -66,47 +69,60 @@ static bool ReadWin32( HANDLE *hConsoleIn, char *p_buffer, 
int *pi_size )
 /* nothing interesting */
 continue;
 }
-
-p_buffer[ *pi_size ] = input_record.Event.KeyEvent.uChar.AsciiChar;
-
-/* Echo out the command */
-putc( p_buffer[ *pi_size ], stdout );
-
-/* Handle special keys */
-if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
+if( input_record.Event.KeyEvent.uChar.AsciiChar == '\n' ||
+input_record.Event.KeyEvent.uChar.AsciiChar == '\r' )
 {
-if ( p_buffer[ *pi_size ] == '\r' )
-p_buffer[ *pi_size ] = '\n';
-(*pi_size)++; /* We want the \n to be in the output string */
 putc( '\n', stdout );
+p_buffer[*pi_size] = '\n';
+(*pi_size)++;
 break;
 }
-switch( p_buffer[ *pi_size ] )
+switch( input_record.Event.KeyEvent.uChar.AsciiChar )
 {
 case '\b':
-if( *pi_size )
+if ( *pi_size == 0 )
+break;
+if ( *pi_size > 1 && (p_buffer[*pi_size - 1] & 0xC0) == 0x80 )
 {
-*pi_size -= 2;
-putc( ' ', stdout );
-putc( '\b', stdout );
+// pi_size currently points to the character to be written, so
+// we need to roll back from 2 bytes to start erasing the 
previous
+// character
+(*pi_size) -= 2;
+unsigned int nbBytes = 1;
+while( *pi_size > 0 && (p_buffer[*pi_size] & 0xC0) == 0x80 )
+{
+(*pi_size)--;
+nbBytes++;
+}
+assert( clz( (unsigned char)~(p_buffer[*pi_size]) ) == nbBytes 
+ 1 );
+// The first utf8 byte will be overriden by a \0
 }
+else
+(*pi_size)--;
+p_buffer[*pi_size] = 0;
+
+fputs( "\b \b", stdout );
 break;
-//case '\r':
-//(*pi_size) --;
-//break;
+default:
+{
+WCHAR psz_winput[] = { 
input_record.Event.KeyEvent.uChar.UnicodeChar, L'\0' };
+char* psz_input = FromWide( psz_winput );
+int input_size = strlen(psz_input);
+if ( *pi_size + input_size > MAX_LINE_LENGTH )
+{
+p_buffer[ *pi_size ] = 0;
+return false;
+}
+strcpy( (char*)&p_buffer[*pi_size], psz_input );
+utf8_fprintf( stdout, "%s", psz_input );
+free(psz_input);
+*pi_size += input_size;
+}
 }
-
-(*pi_size)++;
-}
-
-if( *pi_size == MAX_LINE_LENGTH )
-  // p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
-{
-p_buffer[ *pi_size ] = 0;
-return true;
 }
 
-return false;
+p_buffer[ *pi_size ] = 0;
+return true;
 }
 
 static int vlclua_console_init( lua_State *L )
@@ -135,7 +151,7 @@ static int vlclua_console_read( lua_State *L )
 {
 char psz_buffer[MAX_LINE_LENGTH+1];
 int i_size = 0;
-ReadWin32( GetConsole( L ), psz_buffer, &i_size );
+ReadWin32( GetConsole( L ), (unsigned char*)psz_buffer, &i_size );
 lua_pushlstring( L, psz_buffer, i_size );
 
 return 1;

___

[vlc-commits] lua: cli: Fix non-ascii character input

2018-03-14 Thread Hugo Beauzée-Luyssen
vlc | branch: master | Hugo Beauzée-Luyssen  | Mon Mar 12 
10:28:53 2018 +0100| [27852dd4c2a99c9fd2a890e1cb2941a448ea9bf6] | committer: 
Hugo Beauzée-Luyssen

lua: cli: Fix non-ascii character input

refs #19874

(cherry picked from commit 1f1291aedc994dc218bfc5b9ab3ba0a32c19909b)
Signed-off-by: Hugo Beauzée-Luyssen 

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=27852dd4c2a99c9fd2a890e1cb2941a448ea9bf6
---

 modules/lua/libs/win.c | 80 ++
 1 file changed, 48 insertions(+), 32 deletions(-)

diff --git a/modules/lua/libs/win.c b/modules/lua/libs/win.c
index 7d0276331e..fc037bf65e 100644
--- a/modules/lua/libs/win.c
+++ b/modules/lua/libs/win.c
@@ -48,12 +48,15 @@ static HANDLE GetConsole( lua_State *L )
 
 #define MAX_LINE_LENGTH 1024
 
-static bool ReadWin32( HANDLE *hConsoleIn, char *p_buffer, int *pi_size )
+static bool ReadWin32( HANDLE *hConsoleIn, unsigned char *p_buffer, int 
*pi_size )
 {
 INPUT_RECORD input_record;
 DWORD i_dw;
 
-while( *pi_size < MAX_LINE_LENGTH &&
+// Prefer to fail early when there's not enough space to store a 4 bytes
+// UTF8 character. The function will be immediatly called again and we 
won't
+// lose an input
+while( *pi_size < MAX_LINE_LENGTH - 4 &&
ReadConsoleInput( hConsoleIn, &input_record, 1, &i_dw ) )
 {
 if( input_record.EventType != KEY_EVENT ||
@@ -66,47 +69,60 @@ static bool ReadWin32( HANDLE *hConsoleIn, char *p_buffer, 
int *pi_size )
 /* nothing interesting */
 continue;
 }
-
-p_buffer[ *pi_size ] = input_record.Event.KeyEvent.uChar.AsciiChar;
-
-/* Echo out the command */
-putc( p_buffer[ *pi_size ], stdout );
-
-/* Handle special keys */
-if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
+if( input_record.Event.KeyEvent.uChar.AsciiChar == '\n' ||
+input_record.Event.KeyEvent.uChar.AsciiChar == '\r' )
 {
-if ( p_buffer[ *pi_size ] == '\r' )
-p_buffer[ *pi_size ] = '\n';
-(*pi_size)++; /* We want the \n to be in the output string */
 putc( '\n', stdout );
+p_buffer[*pi_size] = '\n';
+(*pi_size)++;
 break;
 }
-switch( p_buffer[ *pi_size ] )
+switch( input_record.Event.KeyEvent.uChar.AsciiChar )
 {
 case '\b':
-if( *pi_size )
+if ( *pi_size == 0 )
+break;
+if ( *pi_size > 1 && (p_buffer[*pi_size - 1] & 0xC0) == 0x80 )
 {
-*pi_size -= 2;
-putc( ' ', stdout );
-putc( '\b', stdout );
+// pi_size currently points to the character to be written, so
+// we need to roll back from 2 bytes to start erasing the 
previous
+// character
+(*pi_size) -= 2;
+unsigned int nbBytes = 1;
+while( *pi_size > 0 && (p_buffer[*pi_size] & 0xC0) == 0x80 )
+{
+(*pi_size)--;
+nbBytes++;
+}
+assert( clz( (unsigned char)~(p_buffer[*pi_size]) ) == nbBytes 
+ 1 );
+// The first utf8 byte will be overriden by a \0
 }
+else
+(*pi_size)--;
+p_buffer[*pi_size] = 0;
+
+fputs( "\b \b", stdout );
 break;
-//case '\r':
-//(*pi_size) --;
-//break;
+default:
+{
+WCHAR psz_winput[] = { 
input_record.Event.KeyEvent.uChar.UnicodeChar, L'\0' };
+char* psz_input = FromWide( psz_winput );
+int input_size = strlen(psz_input);
+if ( *pi_size + input_size > MAX_LINE_LENGTH )
+{
+p_buffer[ *pi_size ] = 0;
+return false;
+}
+strcpy( (char*)&p_buffer[*pi_size], psz_input );
+utf8_fprintf( stdout, "%s", psz_input );
+free(psz_input);
+*pi_size += input_size;
+}
 }
-
-(*pi_size)++;
-}
-
-if( *pi_size == MAX_LINE_LENGTH )
-  // p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
-{
-p_buffer[ *pi_size ] = 0;
-return true;
 }
 
-return false;
+p_buffer[ *pi_size ] = 0;
+return true;
 }
 
 static int vlclua_console_init( lua_State *L )
@@ -135,7 +151,7 @@ static int vlclua_console_read( lua_State *L )
 {
 char psz_buffer[MAX_LINE_LENGTH+1];
 int i_size = 0;
-ReadWin32( GetConsole( L ), psz_buffer, &i_size );
+ReadWin32( GetConsole( L ), (unsigned char*)psz_buffer, &i_size );
 lua_pushlstring( L, psz_buffer, i_size );
 
 return 1;

___
vlc-commits mailing list
vlc-commits@videolan.org
https://mailman.videolan.org/listinfo/vlc-commits