[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.

svn path=/trunk/; revision=67013
This commit is contained in:
Hermès Bélusca-Maïto 2015-04-03 00:11:42 +00:00
parent 313061d22a
commit 0b933fb6ad

View file

@ -137,14 +137,18 @@ VOID ConInString(LPTSTR lpInput, DWORD dwLength)
static VOID ConWrite(TCHAR *str, DWORD len, DWORD nStdHandle) static VOID ConWrite(TCHAR *str, DWORD len, DWORD nStdHandle)
{ {
DWORD dwWritten; DWORD dwNumBytes = 0;
HANDLE hOutput = GetStdHandle(nStdHandle); HANDLE hOutput = GetStdHandle(nStdHandle);
PVOID p; 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 */ /* Check whether we are writing to a console and if so, write to it */
if (IsConsoleHandle(hOutput)) if (IsConsoleHandle(hOutput))
{ {
if (WriteConsole(hOutput, str, len, &dwWritten, NULL)) WriteConsole(hOutput, str, len, &dwNumBytes, NULL);
return; return;
} }
@ -164,30 +168,38 @@ static VOID ConWrite(TCHAR *str, DWORD len, DWORD nStdHandle)
#endif #endif
/* /*
* Find any newline character in the buffer, * Find any newline character in the buffer,
* send the part BEFORE the newline, then send * write the part BEFORE the newline, then write
* a carriage-return + newline, and then send * a carriage-return + newline, and then write
* the remaining part of the buffer. * the remaining part of the buffer.
* *
* This fixes output in files and serial console. * This fixes output in files and serial console.
*/ */
while (str && *(PWCHAR)str && len > 0) while (len > 0)
{ {
p = wcspbrk((PWCHAR)str, L"\r\n"); /* Loop until we find a \r or \n character */
if (p) // 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; /* Advance one character */
WriteFile(hOutput, str, ((PWCHAR)p - (PWCHAR)str) * sizeof(WCHAR), &dwWritten, NULL); p = (PVOID)((PWCHAR)p + 1);
WriteFile(hOutput, L"\r\n", 2 * sizeof(WCHAR), &dwWritten, NULL); len--;
str = (PVOID)((PWCHAR)p + 1);
} }
else
/* 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')
{ {
WriteFile(hOutput, str, len * sizeof(WCHAR), &dwWritten, NULL); /* ... send a carriage-return + newline sequence and skip \r or \n */
break; WriteFile(hOutput, L"\r\n", 2 * sizeof(WCHAR), &dwNumBytes, NULL);
str = (PVOID)((PWCHAR)p + 1);
len--;
} }
} }
// WriteFile(hOutput, str, len * sizeof(WCHAR), &dwWritten, NULL);
#ifndef _UNICODE #ifndef _UNICODE
cmd_free(buffer); cmd_free(buffer);
#endif #endif
@ -206,30 +218,38 @@ static VOID ConWrite(TCHAR *str, DWORD len, DWORD nStdHandle)
#endif #endif
/* /*
* Find any newline character in the buffer, * Find any newline character in the buffer,
* send the part BEFORE the newline, then send * write the part BEFORE the newline, then write
* a carriage-return + newline, and then send * a carriage-return + newline, and then write
* the remaining part of the buffer. * the remaining part of the buffer.
* *
* This fixes output in files and serial console. * This fixes output in files and serial console.
*/ */
while (str && *(PCHAR)str && len > 0) while (len > 0)
{ {
p = strpbrk((PCHAR)str, "\r\n"); /* Loop until we find a \r or \n character */
if (p) // FIXME: What about the pair \r\n ?
p = str;
while (*(PCHAR)p != '\r' && *(PCHAR)p != '\n' && len > 0)
{ {
len -= ((PCHAR)p - (PCHAR)str) + 1; /* Advance one character */
WriteFile(hOutput, str, ((PCHAR)p - (PCHAR)str), &dwWritten, NULL); p = (PVOID)((PCHAR)p + 1);
WriteFile(hOutput, "\r\n", 2, &dwWritten, NULL); len--;
str = (PVOID)((PCHAR)p + 1);
} }
else
/* 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')
{ {
WriteFile(hOutput, str, len, &dwWritten, NULL); /* ... send a carriage-return + newline sequence and skip \r or \n */
break; WriteFile(hOutput, "\r\n", 2, &dwNumBytes, NULL);
str = (PVOID)((PCHAR)p + 1);
len--;
} }
} }
// WriteFile(hOutput, str, len, &dwWritten, NULL);
#ifdef _UNICODE #ifdef _UNICODE
cmd_free(buffer); cmd_free(buffer);
#endif #endif