[PATCH 5/5] [programs/cmd] Correct handling of %~0 for batch call

Jason Edmeades us at edmeades.me.uk
Sun Aug 5 17:15:30 CDT 2018


Fixes bug#44369

When a batch label is called, %0 and %~0 should be the label being called, and if you start adding
modifiers to it (eg %~d0) then you get details of the batch program containing the label.

Signed-off-by: Jason Edmeades <us at edmeades.me.uk>
---
 programs/cmd/batch.c                     | 23 +++++++++++++----------
 programs/cmd/tests/test_builtins.cmd     |  9 +++++++++
 programs/cmd/tests/test_builtins.cmd.exp |  3 +++
 3 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/programs/cmd/batch.c b/programs/cmd/batch.c
index e10baf852c..75f377cf50 100644
--- a/programs/cmd/batch.c
+++ b/programs/cmd/batch.c
@@ -460,10 +460,19 @@ void WCMD_HandleTildaModifiers(WCHAR **start, BOOL atExecute)
   }
   if (lastModifier == firstModifier) return; /* Invalid syntax */
 
-  /* Extract the parameter to play with */
-  if (*lastModifier == '0') {
+  /* So now, firstModifier points to beginning of modifiers, lastModifier
+     points to the variable just after the modifiers. Process modifiers
+     in a specific order, remembering there could be duplicates           */
+  modifierLen = lastModifier - firstModifier;
+  finaloutput[0] = 0x00;
+
+  /* Extract the parameter to play with
+     Special case param 0 - With %~0 you get the batch label which was called
+     whereas if you start applying other modifiers to it, you get the filename
+     the batch label is in                                                     */
+  if (*lastModifier == '0' && modifierLen > 1) {
     strcpyW(outputparam, context->batchfileW);
-  } else if ((*lastModifier >= '1' && *lastModifier <= '9')) {
+  } else if ((*lastModifier >= '0' && *lastModifier <= '9')) {
     strcpyW(outputparam,
             WCMD_parameter (context -> command,
                             *lastModifier-'0' + context -> shift_count[*lastModifier-'0'],
@@ -473,12 +482,6 @@ void WCMD_HandleTildaModifiers(WCHAR **start, BOOL atExecute)
     strcpyW(outputparam, forloopcontext.variable[foridx]);
   }
 
-  /* So now, firstModifier points to beginning of modifiers, lastModifier
-     points to the variable just after the modifiers. Process modifiers
-     in a specific order, remembering there could be duplicates           */
-  modifierLen = lastModifier - firstModifier;
-  finaloutput[0] = 0x00;
-
   /* 1. Handle '~' : Strip surrounding quotes */
   if (outputparam[0]=='"' &&
       memchrW(firstModifier, '~', modifierLen) != NULL) {
@@ -728,7 +731,7 @@ void WCMD_call (WCHAR *command) {
       li.QuadPart = 0;
       li.u.LowPart = SetFilePointer(context -> h, li.u.LowPart,
                      &li.u.HighPart, FILE_CURRENT);
-      WCMD_batch (param1, command, TRUE, gotoLabel, context->h);
+      WCMD_batch (context->batchfileW, command, TRUE, gotoLabel, context->h);
       SetFilePointer(context -> h, li.u.LowPart,
                      &li.u.HighPart, FILE_BEGIN);
 
diff --git a/programs/cmd/tests/test_builtins.cmd b/programs/cmd/tests/test_builtins.cmd
index d9338003c9..5cff817c4f 100644
--- a/programs/cmd/tests/test_builtins.cmd
+++ b/programs/cmd/tests/test_builtins.cmd
@@ -670,6 +670,15 @@ echo '%~xs1'
 goto :eof
 :endEchoFuns
 
+echo ------------ Testing parameter zero ------------
+call :func parm1 parm2
+goto :endParm0
+:func
+echo %~0 %~1
+echo [%0] [%~d0] [%~p0] [%~n0] [%~x0] [%~s0]
+goto :EOF
+:endParm0
+
 echo ------------ Testing variable delayed expansion ------------
 rem NT4 doesn't support this
 echo --- default mode (load-time expansion)
diff --git a/programs/cmd/tests/test_builtins.cmd.exp b/programs/cmd/tests/test_builtins.cmd.exp
index 539014ce21..62e47b3b5e 100644
--- a/programs/cmd/tests/test_builtins.cmd.exp
+++ b/programs/cmd/tests/test_builtins.cmd.exp
@@ -575,6 +575,9 @@ N
 @drive@
 ''
 '.eh'@or_broken@''
+------------ Testing parameter zero ------------
+:func parm1
+[:func] [@drive@] [@path@] [test] [.cmd] [@drive@@shortpath at test.cmd]
 ------------ Testing variable delayed expansion ------------
 --- default mode (load-time expansion)
 foo
-- 
2.17.1




More information about the wine-devel mailing list