Module: wine Branch: master Commit: e8d8df3c542548c329d3deca62d0352b531222d2 URL: http://source.winehq.org/git/wine.git/?a=commit;h=e8d8df3c542548c329d3deca62...
Author: Frédéric Delanoy frederic.delanoy@gmail.com Date: Fri Nov 11 19:27:43 2011 +0100
cmd: Trim whitespace in echo on/off.
---
programs/cmd/builtins.c | 54 +++++++++++++++++++++++------ programs/cmd/tests/test_builtins.cmd | 2 + programs/cmd/tests/test_builtins.cmd.exp | 2 + 3 files changed, 47 insertions(+), 11 deletions(-)
diff --git a/programs/cmd/builtins.c b/programs/cmd/builtins.c index 5272bfc..770a78a 100644 --- a/programs/cmd/builtins.c +++ b/programs/cmd/builtins.c @@ -854,6 +854,35 @@ BOOL WCMD_delete (WCHAR *command) { return foundAny; }
+/* + * WCMD_strtrim + * + * Returns a trimmed version of s with all leading and trailing whitespace removed + * Pre: s non NULL + * + */ +static WCHAR *WCMD_strtrim(const WCHAR *s) +{ + DWORD len = strlenW(s); + const WCHAR *start = s; + WCHAR* result; + + if (!(result = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR)))) + return NULL; + + while (isspaceW(*start)) start++; + if (*start) { + const WCHAR *end = s + len - 1; + while (end > start && isspaceW(*end)) end--; + memcpy(result, start, (end - start + 2) * sizeof(WCHAR)); + result[end - start + 1] = '\0'; + } else { + result[0] = '\0'; + } + + return result; +} + /**************************************************************************** * WCMD_echo * @@ -861,33 +890,36 @@ BOOL WCMD_delete (WCHAR *command) { * in DOS (try typing "ECHO ON AGAIN" for an example). */
-void WCMD_echo (const WCHAR *command) { - +void WCMD_echo (const WCHAR *command) +{ int count; const WCHAR *origcommand = command; + WCHAR *trimmed;
if ( command[0]==' ' || command[0]=='\t' || command[0]=='.' || command[0]==':' || command[0]==';') command++;
- count = strlenW(command); + trimmed = WCMD_strtrim(command); + if (!trimmed) return; + + count = strlenW(trimmed); if (count == 0 && origcommand[0]!='.' && origcommand[0]!=':' && origcommand[0]!=';') { if (echo_mode) WCMD_output (WCMD_LoadMessage(WCMD_ECHOPROMPT), onW); else WCMD_output (WCMD_LoadMessage(WCMD_ECHOPROMPT), offW); return; } - if (lstrcmpiW(command, onW) == 0) { + + if (lstrcmpiW(trimmed, onW) == 0) echo_mode = TRUE; - return; - } - if (lstrcmpiW(command, offW) == 0) { + else if (lstrcmpiW(trimmed, offW) == 0) echo_mode = FALSE; - return; + else { + WCMD_output_asis (command); + WCMD_output (newline); } - WCMD_output_asis (command); - WCMD_output (newline); - + HeapFree(GetProcessHeap(), 0, trimmed); }
/************************************************************************** diff --git a/programs/cmd/tests/test_builtins.cmd b/programs/cmd/tests/test_builtins.cmd index 13c9393..bcaccb2 100644 --- a/programs/cmd/tests/test_builtins.cmd +++ b/programs/cmd/tests/test_builtins.cmd @@ -26,8 +26,10 @@ echo@tab@word@tab@@space@ echo @tab@word echo @tab@word echo@tab@@tab@word +echo @tab@ on @space@
@echo off +echo off@tab@@space@ echo ------------ Testing 'echo' [OFF] -------------- echo word echo 'singlequotedword' diff --git a/programs/cmd/tests/test_builtins.cmd.exp b/programs/cmd/tests/test_builtins.cmd.exp index 7a0eec3..e35b710 100644 --- a/programs/cmd/tests/test_builtins.cmd.exp +++ b/programs/cmd/tests/test_builtins.cmd.exp @@ -74,6 +74,8 @@ word
@pwd@>echo@tab@@tab@word@space@ @tab@word + +@pwd@>echo @tab@ on @space@@space@ ------------ Testing 'echo' [OFF] -------------- word 'singlequotedword'