cmd: Add a space at the end of the first echo'ed batch line [try4]

Alexandre Goujon ale.goujon at gmail.com
Fri Oct 15 12:11:40 CDT 2010


I was looking for a forbidden char in batch files but just I found accentuated chars whereas I wanted a single (not wide) char.
Finally, I found it in the ASCII table.
The \x1a character name is SUBSTITUTE and is exactly what we need.
---
 programs/cmd/tests/batch.c               |   23 ++++++++++++++++-
 programs/cmd/tests/test_builtins.cmd     |   20 +++++++++++++-
 programs/cmd/tests/test_builtins.cmd.exp |   40 ++++++++++++++++++++++++++++-
 programs/cmd/wcmdmain.c                  |    3 ++
 4 files changed, 81 insertions(+), 5 deletions(-)

diff --git a/programs/cmd/tests/batch.c b/programs/cmd/tests/batch.c
index cc8c020..5514b8f 100644
--- a/programs/cmd/tests/batch.c
+++ b/programs/cmd/tests/batch.c
@@ -34,6 +34,8 @@ static BOOL run_cmd(const char *cmd_data, DWORD cmd_size)
     HANDLE file,fileerr;
     DWORD size;
     BOOL bres;
+    static const char escaped_space = '\x1A';
+    char *tmp, *data;
 
     file = CreateFileA("test.cmd", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
             FILE_ATTRIBUTE_NORMAL, NULL);
@@ -41,7 +43,17 @@ static BOOL run_cmd(const char *cmd_data, DWORD cmd_size)
     if(file == INVALID_HANDLE_VALUE)
         return FALSE;
 
-    bres = WriteFile(file, cmd_data, cmd_size, &size, NULL);
+    /* Substitute escaped spaces with real ones */
+    tmp = strdup(cmd_data);
+    data = tmp;
+    ok(tmp != NULL, "strdup failed\n");
+    if (!tmp)
+        return FALSE;
+
+    while( (tmp = strchr(tmp,escaped_space)) )
+        *tmp = ' ';
+
+    bres = WriteFile(file, data, cmd_size, &size, NULL);
     CloseHandle(file);
     ok(bres, "Could not write to file: %u\n", GetLastError());
     if(!bres)
@@ -75,6 +87,7 @@ static BOOL run_cmd(const char *cmd_data, DWORD cmd_size)
     CloseHandle(file);
     CloseHandle(fileerr);
     DeleteFileA("test.cmd");
+    free(data);
     return TRUE;
 }
 
@@ -113,6 +126,7 @@ static const char *compare_line(const char *out_line, const char *out_end, const
 
     static const char pwd_cmd[] = {'@','p','w','d','@'};
     static const char todo_space_cmd[] = {'@','t','o','d','o','_','s','p','a','c','e','@'};
+    static const char space_cmd[] = {'@','s','p','a','c','e','@'};
     static const char or_broken_cmd[] = {'@','o','r','_','b','r','o','k','e','n','@'};
 
     while(exp_ptr < exp_end) {
@@ -135,6 +149,13 @@ static const char *compare_line(const char *out_line, const char *out_end, const
                 if(out_ptr < out_end && *out_ptr == ' ')
                     out_ptr++;
                 continue;
+            }else if(exp_ptr+sizeof(space_cmd) <= exp_end
+                    && !memcmp(exp_ptr, space_cmd, sizeof(space_cmd))) {
+                exp_ptr += sizeof(space_cmd);
+                ok(*out_ptr == ' ', "expected space\n");
+                if(out_ptr < out_end && *out_ptr == ' ')
+                    out_ptr++;
+                continue;
             }else if(exp_ptr+sizeof(or_broken_cmd) <= exp_end
                      && !memcmp(exp_ptr, or_broken_cmd, sizeof(or_broken_cmd))) {
                 exp_ptr = exp_end;
diff --git a/programs/cmd/tests/test_builtins.cmd b/programs/cmd/tests/test_builtins.cmd
index d9f5cf7..6368965 100644
--- a/programs/cmd/tests/test_builtins.cmd
+++ b/programs/cmd/tests/test_builtins.cmd
@@ -1,7 +1,21 @@
 echo Tests for cmd's builtin commands
- at echo off
 
-echo ------------ Testing 'echo' --------------
+ at echo on
+echo ------------ Testing 'echo' [ON] --------------
+echo word
+echo 'singlequotedword'
+echo "doublequotedword"
+ at echo at-echoed-word
+echo "/?"
+echo.
+echo .
+echo.word
+echo .word
+echo word
+echo word
+
+ at echo off
+echo ------------ Testing 'echo' [OFF] --------------
 echo word
 echo 'singlequotedword'
 echo "doublequotedword"
@@ -11,6 +25,8 @@ echo.
 echo .
 echo.word
 echo .word
+echo word
+echo word
 
 echo ------------ Testing 'set' --------------
 echo set "FOO=bar" should not include the quotes in the variable value
diff --git a/programs/cmd/tests/test_builtins.cmd.exp b/programs/cmd/tests/test_builtins.cmd.exp
index 8942cb3..dee2c5b 100644
--- a/programs/cmd/tests/test_builtins.cmd.exp
+++ b/programs/cmd/tests/test_builtins.cmd.exp
@@ -1,7 +1,41 @@
 
- at pwd@>echo Tests for cmd's builtin commands at todo_space@
+ at pwd@>echo Tests for cmd's builtin commands at space@
 Tests for cmd's builtin commands
------------- Testing 'echo' --------------
+
+ at pwd@>echo ------------ Testing 'echo' [ON] -------------- at space@
+------------ Testing 'echo' [ON] --------------
+
+ at pwd@>echo word at space@
+word
+
+ at pwd@>echo 'singlequotedword'@space@
+'singlequotedword'
+
+ at pwd@>echo "doublequotedword"@space@
+"doublequotedword"
+at-echoed-word
+
+ at pwd@>echo "/?"@space@
+"/?"
+
+ at pwd@>echo.
+
+
+ at pwd@>echo . at space@
+.
+
+ at pwd@>echo.word
+word
+
+ at pwd@>echo .word at space@
+.word
+
+ at pwd@>echo word at space@@space@
+word at space@
+
+ at pwd@>echo word at space@@space@@space@
+word at space@@space@
+------------ Testing 'echo' [OFF] --------------
 word
 'singlequotedword'
 "doublequotedword"
@@ -11,6 +45,8 @@ at-echoed-word
 .
 word
 .word
+word at space@
+word at space@@space@
 ------------ Testing 'set' --------------
 set "FOO=bar" should not include the quotes in the variable value
 bar
diff --git a/programs/cmd/wcmdmain.c b/programs/cmd/wcmdmain.c
index b50512e..85f78b8 100644
--- a/programs/cmd/wcmdmain.c
+++ b/programs/cmd/wcmdmain.c
@@ -1779,8 +1779,11 @@ WCHAR *WCMD_ReadAndParseLine(WCHAR *optionalcmd, CMD_LIST **output, HANDLE readF
     if (context) handleExpansion(extraSpace, FALSE, NULL, NULL);
     /* Show prompt before batch line IF echo is on and in batch program */
     if (context && echo_mode && extraSpace[0] && (extraSpace[0] != '@')) {
+      const WCHAR spc[]={' ','\0'};
       WCMD_show_prompt();
       WCMD_output_asis(extraSpace);
+      /* I don't know why Windows puts a space here but it does */
+      WCMD_output_asis(spc);
       WCMD_output_asis(newline);
     }
 
-- 
1.7.0.4




More information about the wine-patches mailing list