Author: hbelusca
Date: Fri Apr  3 00:11:42 2015
New Revision: 67013

URL: http://svn.reactos.org/svn/reactos?rev=67013&view=rev
Log:
[CMD]: Fix the ConWrite newline support I added in r59411 (strpbrk only applies 
on NULL-terminated strings, whereas here I manipulate char-counted strings). 
Spotted by Thomas. As a result should fix almost all of the non-NULL-terminated 
strings that show up in the failed cmd wine test.

Modified:
    trunk/reactos/base/shell/cmd/console.c

Modified: trunk/reactos/base/shell/cmd/console.c
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/base/shell/cmd/console.c?rev=67013&r1=67012&r2=67013&view=diff
==============================================================================
--- trunk/reactos/base/shell/cmd/console.c      [iso-8859-1] (original)
+++ trunk/reactos/base/shell/cmd/console.c      [iso-8859-1] Fri Apr  3 
00:11:42 2015
@@ -137,15 +137,19 @@
 
 static VOID ConWrite(TCHAR *str, DWORD len, DWORD nStdHandle)
 {
-    DWORD dwWritten;
+    DWORD dwNumBytes = 0;
     HANDLE hOutput = GetStdHandle(nStdHandle);
     PVOID p;
 
+    /* If we don't write anything, just return */
+    if (!str || len == 0)
+        return;
+
     /* Check whether we are writing to a console and if so, write to it */
     if (IsConsoleHandle(hOutput))
     {
-        if (WriteConsole(hOutput, str, len, &dwWritten, NULL))
-            return;
+        WriteConsole(hOutput, str, len, &dwNumBytes, NULL);
+        return;
     }
 
     /* We're writing to a file or pipe instead of the console. Convert the
@@ -164,30 +168,38 @@
 #endif
         /*
          * Find any newline character in the buffer,
-         * send the part BEFORE the newline, then send
-         * a carriage-return + newline, and then send
+         * write the part BEFORE the newline, then write
+         * a carriage-return + newline, and then write
          * the remaining part of the buffer.
          *
          * This fixes output in files and serial console.
          */
-        while (str && *(PWCHAR)str && len > 0)
+        while (len > 0)
         {
-            p = wcspbrk((PWCHAR)str, L"\r\n");
-            if (p)
+            /* Loop until we find a \r or \n character */
+            // FIXME: What about the pair \r\n ?
+            p = str;
+            while (*(PWCHAR)p != L'\r' && *(PWCHAR)p != L'\n' && len > 0)
             {
-                len -= ((PWCHAR)p - (PWCHAR)str) + 1;
-                WriteFile(hOutput, str, ((PWCHAR)p - (PWCHAR)str) * 
sizeof(WCHAR), &dwWritten, NULL);
-                WriteFile(hOutput, L"\r\n", 2 * sizeof(WCHAR), &dwWritten, 
NULL);
+                /* Advance one character */
+                p = (PVOID)((PWCHAR)p + 1);
+                len--;
+            }
+
+            /* Write everything up to \r or \n */
+            dwNumBytes = ((PWCHAR)p - (PWCHAR)str) * sizeof(WCHAR);
+            WriteFile(hOutput, str, dwNumBytes, &dwNumBytes, NULL);
+
+            /* If we hit \r or \n ... */
+            if (*(PWCHAR)p == L'\r' || *(PWCHAR)p == L'\n')
+            {
+                /* ... send a carriage-return + newline sequence and skip \r 
or \n */
+                WriteFile(hOutput, L"\r\n", 2 * sizeof(WCHAR), &dwNumBytes, 
NULL);
                 str = (PVOID)((PWCHAR)p + 1);
-            }
-            else
-            {
-                WriteFile(hOutput, str, len * sizeof(WCHAR), &dwWritten, NULL);
-                break;
+                len--;
             }
         }
 
-        // WriteFile(hOutput, str, len * sizeof(WCHAR), &dwWritten, NULL);
 #ifndef _UNICODE
         cmd_free(buffer);
 #endif
@@ -206,30 +218,38 @@
 #endif
         /*
          * Find any newline character in the buffer,
-         * send the part BEFORE the newline, then send
-         * a carriage-return + newline, and then send
+         * write the part BEFORE the newline, then write
+         * a carriage-return + newline, and then write
          * the remaining part of the buffer.
          *
          * This fixes output in files and serial console.
          */
-        while (str && *(PCHAR)str && len > 0)
+        while (len > 0)
         {
-            p = strpbrk((PCHAR)str, "\r\n");
-            if (p)
+            /* Loop until we find a \r or \n character */
+            // FIXME: What about the pair \r\n ?
+            p = str;
+            while (*(PCHAR)p != '\r' && *(PCHAR)p != '\n' && len > 0)
             {
-                len -= ((PCHAR)p - (PCHAR)str) + 1;
-                WriteFile(hOutput, str, ((PCHAR)p - (PCHAR)str), &dwWritten, 
NULL);
-                WriteFile(hOutput, "\r\n", 2, &dwWritten, NULL);
+                /* Advance one character */
+                p = (PVOID)((PCHAR)p + 1);
+                len--;
+            }
+
+            /* Write everything up to \r or \n */
+            dwNumBytes = ((PCHAR)p - (PCHAR)str) * sizeof(CHAR);
+            WriteFile(hOutput, str, dwNumBytes, &dwNumBytes, NULL);
+
+            /* If we hit \r or \n ... */
+            if (*(PCHAR)p == '\r' || *(PCHAR)p == '\n')
+            {
+                /* ... send a carriage-return + newline sequence and skip \r 
or \n */
+                WriteFile(hOutput, "\r\n", 2, &dwNumBytes, NULL);
                 str = (PVOID)((PCHAR)p + 1);
-            }
-            else
-            {
-                WriteFile(hOutput, str, len, &dwWritten, NULL);
-                break;
+                len--;
             }
         }
 
-        // WriteFile(hOutput, str, len, &dwWritten, NULL);
 #ifdef _UNICODE
         cmd_free(buffer);
 #endif


Reply via email to