=?UTF-8?Q?Fr=C3=A9d=C3=A9ric=20Delanoy=20?=: cmd: Trim whitespace in echo on/off.

Alexandre Julliard julliard at winehq.org
Tue Nov 15 13:17:31 CST 2011


Module: wine
Branch: master
Commit: e8d8df3c542548c329d3deca62d0352b531222d2
URL:    http://source.winehq.org/git/wine.git/?a=commit;h=e8d8df3c542548c329d3deca62d0352b531222d2

Author: Frédéric Delanoy <frederic.delanoy at 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 at tab@word at tab@@space@
 echo @tab at word
 echo  @tab at word
 echo at tab@@tab at word
+echo @tab@ on @space@
 
 @echo off
+echo off at 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 at tab@@tab at word@space@
 @tab at word
+
+ at pwd@>echo @tab@ on @space@@space@
 ------------ Testing 'echo' [OFF] --------------
 word
 'singlequotedword'




More information about the wine-cvs mailing list