Patchwork ANSI escape characters support for Windows console

login
register
mail settings
Submitter Pavel Dovgaluk
Date May 29, 2012, 12:29 p.m.
Message ID <002f01cd3d96$a2378a30$e6a69e90$@Dovgaluk@ispras.ru>
Download mbox | patch
Permalink /patch/161739/
State New
Headers show

Comments

Pavel Dovgaluk - May 29, 2012, 12:29 p.m.
This patch adds support of ANSI escape characters used in readline module
to impelementation of stdio character device for Windows.

Signed-off-by: Pavel Dovgalyuk <pavel.dovgaluk@gmail.com>
---
 qemu-char.c |   48 ++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 38 insertions(+), 10 deletions(-)

 static void win_stdio_wait_func(void *opaque)
@@ -1948,7 +1970,9 @@ static CharDriverState *qemu_chr_open_win_stdio(QemuOpts *opts)
 {
     CharDriverState   *chr;
     WinStdioCharState *stdio;
+    HANDLE             hStdOut;
     DWORD              dwMode;
+    DWORD              dwOutMode;
     int                is_console = 0;
 
     if (stdio_nb_clients >= STDIO_MAX_CLIENTS
@@ -1960,12 +1984,14 @@ static CharDriverState *qemu_chr_open_win_stdio(QemuOpts *opts)
     stdio = g_malloc0(sizeof(WinStdioCharState));
 
     stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
-    if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
+    if (stdio->hStdIn == INVALID_HANDLE_VALUE
+        || hStdOut == INVALID_HANDLE_VALUE) {
         fprintf(stderr, "cannot open stdio: invalid handle\n");
         exit(1);
     }
 
     is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
+    GetConsoleMode(hStdOut, &dwOutMode);
 
     chr->opaque    = stdio;
     chr->chr_write = win_stdio_write;
@@ -2005,9 +2031,11 @@ static CharDriverState *qemu_chr_open_win_stdio(QemuOpts *opts)
         /* set the terminal in raw mode */
         /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
         dwMode |= ENABLE_PROCESSED_INPUT;
+        dwOutMode |= ENABLE_PROCESSED_OUTPUT;
     }
 
     SetConsoleMode(stdio->hStdIn, dwMode);
+    SetConsoleMode(hStdOut, dwOutMode);
 
     chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
     qemu_chr_fe_set_echo(chr, false);

Patch

diff --git a/qemu-char.c b/qemu-char.c
index fe1126f..6035b79 100644
--- a/qemu-char.c
+++ b/qemu-char.c
@@ -1810,19 +1810,41 @@  static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int
len)
 {
     HANDLE  hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
     DWORD   dwSize;
-    int     len1;
+    int     ret = 0;
+    CONSOLE_SCREEN_BUFFER_INFO info;
 
-    len1 = len;
-
-    while (len1 > 0) {
-        if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
-            break;
+    while (len > 0) {
+        if (len > 2 && buf[0] == '\033' && buf[1] == '[') {
+            switch (buf[2]) {
+            case 'D':
+                if (!WriteFile(hStdOut, "\b", 1, &dwSize, NULL)) {
+                    return ret;
+                }
+                break;
+            case 'K':
+                if (!GetConsoleScreenBufferInfo(hStdOut, &info)) {
+                    return ret;
+                }
+                if (!FillConsoleOutputCharacter(hStdOut, ' ',
+                    info.dwSize.X - info.dwCursorPosition.X,
+                    info.dwCursorPosition, &dwSize)) {
+                    return ret;
+                }
+                break;
+            }
+            dwSize = 3;
+        } else {
+            if (!WriteFile(hStdOut, buf, 1, &dwSize, NULL)) {
+                return ret;
+            }
         }
-        buf  += dwSize;
-        len1 -= dwSize;
+
+        buf += dwSize;
+        len -= dwSize;
+        ret += dwSize;
     }
 
-    return len - len1;
+    return ret;
 }