[PATCH v2] cmd: Change parsing of the if command to avoid parsing the operators.

Bernhard Übelacker bernhardu at mailbox.org
Fri Jul 20 13:17:29 CDT 2018


Found while trying to look into #44236.
A batch script is executed containing a line like this:
  if (%1)==(p) start /W  " "  "%SFDIR%WSFplot" wr2300.t35 3

This returns an error like this:
  Syntax error
  Can't recognize 'p' as an internal or external command, or batch script.

It looks like native does handle the brackets differently when contained
inside the condition part of the if command.
Therefore this patch tries to skip the parsing in WCMD_ReadAndParseLine
of that part. Therefore it moves most of the content of WCMD_if into a
helper and calls that to estimate how long the condition part is.

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=44338
Signed-off-by: Bernhard Übelacker <bernhardu at mailbox.org>
---
History:
v0:
  Some comments in bug tracker by Vijay Kamuju.

v1:
  https://www.winehq.org/pipermail/wine-devel/2018-January/121764.html
  (Received no comments.)

v2:
* Rebased to wine 3.13.
* Removed --- in commit message making patchwatcher ignoring Signed-off-by line.
---
 programs/cmd/builtins.c                  | 79 ++++++++++++++----------
 programs/cmd/tests/test_builtins.cmd     | 20 ++++++
 programs/cmd/tests/test_builtins.cmd.exp |  4 ++
 programs/cmd/wcmd.h                      |  2 +
 programs/cmd/wcmdmain.c                  | 30 +++++++--
 5 files changed, 98 insertions(+), 37 deletions(-)

diff --git a/programs/cmd/builtins.c b/programs/cmd/builtins.c
index 088632f214..4dcf48fa3e 100644
--- a/programs/cmd/builtins.c
+++ b/programs/cmd/builtins.c
@@ -2824,25 +2824,9 @@ static int evaluate_if_comparison(const WCHAR *leftOperand, const WCHAR *operato
     return -1;
 }
 
-/****************************************************************************
- * WCMD_if
- *
- * Batch file conditional.
- *
- * On entry, cmdlist will point to command containing the IF, and optionally
- *   the first command to execute (if brackets not found)
- *   If &&'s were found, this may be followed by a record flagged as isAmpersand
- *   If ('s were found, execute all within that bracket
- *   Command may optionally be followed by an ELSE - need to skip instructions
- *   in the else using the same logic
- *
- * FIXME: Much more syntax checking needed!
- */
-void WCMD_if (WCHAR *p, CMD_LIST **cmdList)
+int evaluate_if_condition(WCHAR *p, WCHAR **command, int *test, int *negate)
 {
-  int negate; /* Negate condition */
-  int test;   /* Condition evaluation result */
-  WCHAR condition[MAX_PATH], *command;
+  WCHAR condition[MAX_PATH];
   static const WCHAR notW[]    = {'n','o','t','\0'};
   static const WCHAR errlvlW[] = {'e','r','r','o','r','l','e','v','e','l','\0'};
   static const WCHAR existW[]  = {'e','x','i','s','t','\0'};
@@ -2850,36 +2834,36 @@ void WCMD_if (WCHAR *p, CMD_LIST **cmdList)
   static const WCHAR parmI[]   = {'/','I','\0'};
   int caseInsensitive = (strstrW(quals, parmI) != NULL);
 
-  negate = !lstrcmpiW(param1,notW);
-  strcpyW(condition, (negate ? param2 : param1));
+  *negate = !lstrcmpiW(param1,notW);
+  strcpyW(condition, (*negate ? param2 : param1));
   WINE_TRACE("Condition: %s\n", wine_dbgstr_w(condition));
 
   if (!lstrcmpiW (condition, errlvlW)) {
-    WCHAR *param = WCMD_parameter(p, 1+negate, NULL, FALSE, FALSE);
+    WCHAR *param = WCMD_parameter(p, 1+(*negate), NULL, FALSE, FALSE);
     WCHAR *endptr;
     long int param_int = strtolW(param, &endptr, 10);
     if (*endptr) goto syntax_err;
-    test = ((long int)errorlevel >= param_int);
-    WCMD_parameter(p, 2+negate, &command, FALSE, FALSE);
+    *test = ((long int)errorlevel >= param_int);
+    WCMD_parameter(p, 2+(*negate), command, FALSE, FALSE);
   }
   else if (!lstrcmpiW (condition, existW)) {
     WIN32_FIND_DATAW fd;
-    HANDLE hff = FindFirstFileW(WCMD_parameter(p, 1+negate, NULL, FALSE, FALSE), &fd);
-    test = (hff != INVALID_HANDLE_VALUE );
-    if (test) FindClose(hff);
+    HANDLE hff = FindFirstFileW(WCMD_parameter(p, 1+(*negate), NULL, FALSE, FALSE), &fd);
+    *test = (hff != INVALID_HANDLE_VALUE );
+    if (*test) FindClose(hff);
 
-    WCMD_parameter(p, 2+negate, &command, FALSE, FALSE);
+    WCMD_parameter(p, 2+(*negate), command, FALSE, FALSE);
   }
   else if (!lstrcmpiW (condition, defdW)) {
-    test = (GetEnvironmentVariableW(WCMD_parameter(p, 1+negate, NULL, FALSE, FALSE),
+    *test = (GetEnvironmentVariableW(WCMD_parameter(p, 1+(*negate), NULL, FALSE, FALSE),
                                     NULL, 0) > 0);
-    WCMD_parameter(p, 2+negate, &command, FALSE, FALSE);
+    WCMD_parameter(p, 2+(*negate), command, FALSE, FALSE);
   }
   else { /* comparison operation */
     WCHAR leftOperand[MAXSTRING], rightOperand[MAXSTRING], operator[MAXSTRING];
     WCHAR *paramStart;
 
-    strcpyW(leftOperand, WCMD_parameter(p, negate+caseInsensitive, &paramStart, TRUE, FALSE));
+    strcpyW(leftOperand, WCMD_parameter(p, (*negate)+caseInsensitive, &paramStart, TRUE, FALSE));
     if (!*leftOperand)
       goto syntax_err;
 
@@ -2900,14 +2884,43 @@ void WCMD_if (WCHAR *p, CMD_LIST **cmdList)
     if (!*rightOperand)
       goto syntax_err;
 
-    test = evaluate_if_comparison(leftOperand, operator, rightOperand, caseInsensitive);
-    if (test == -1)
+    *test = evaluate_if_comparison(leftOperand, operator, rightOperand, caseInsensitive);
+    if (*test == -1)
       goto syntax_err;
 
     p = paramStart + strlenW(rightOperand);
-    WCMD_parameter(p, 0, &command, FALSE, FALSE);
+    WCMD_parameter(p, 0, command, FALSE, FALSE);
   }
 
+  return 1;
+
+syntax_err:
+  return -1;
+}
+
+/****************************************************************************
+ * WCMD_if
+ *
+ * Batch file conditional.
+ *
+ * On entry, cmdlist will point to command containing the IF, and optionally
+ *   the first command to execute (if brackets not found)
+ *   If &&'s were found, this may be followed by a record flagged as isAmpersand
+ *   If ('s were found, execute all within that bracket
+ *   Command may optionally be followed by an ELSE - need to skip instructions
+ *   in the else using the same logic
+ *
+ * FIXME: Much more syntax checking needed!
+ */
+void WCMD_if (WCHAR *p, CMD_LIST **cmdList)
+{
+  int negate; /* Negate condition */
+  int test;   /* Condition evaluation result */
+  WCHAR *command;
+
+  if (evaluate_if_condition(p, &command, &test, &negate) == -1)
+      goto syntax_err;
+
   /* Process rest of IF statement which is on the same line
      Note: This may process all or some of the cmdList (eg a GOTO) */
   WCMD_part_execute(cmdList, command, TRUE, (test != negate));
diff --git a/programs/cmd/tests/test_builtins.cmd b/programs/cmd/tests/test_builtins.cmd
index b9e9b259a9..2b6e3f16d7 100644
--- a/programs/cmd/tests/test_builtins.cmd
+++ b/programs/cmd/tests/test_builtins.cmd
@@ -908,6 +908,26 @@ if %elseIF% == 1 (
 ) else (
   echo else if seems to be broken
 )
+if ()==() (
+  echo comparison operators surrounded by brackets seem to work
+) else (
+  echo comparison operators surrounded by brackets seem to be broken
+)
+if 1(==1( (
+  echo comparison operators surrounded by brackets seem to work
+) else (
+  echo comparison operators surrounded by brackets seem to be broken
+)
+if )==) (
+  echo comparison operators surrounded by brackets seem to work
+) else (
+  echo comparison operators surrounded by brackets seem to be broken
+)
+if /i not (a)==(b) (
+  echo comparison operators surrounded by brackets seem to work
+) else (
+  echo comparison operators surrounded by brackets seem to be broken
+)
 echo --- case sensitivity with and without /i option
 if bar==BAR echo if does not default to case sensitivity
 if not bar==BAR echo if seems to default to case sensitivity
diff --git a/programs/cmd/tests/test_builtins.cmd.exp b/programs/cmd/tests/test_builtins.cmd.exp
index 3118359265..70dc81631c 100644
--- a/programs/cmd/tests/test_builtins.cmd.exp
+++ b/programs/cmd/tests/test_builtins.cmd.exp
@@ -658,6 +658,10 @@ if seems not to detect /c as parameter
 else if seems to work
 else if seems to work
 else if seems to work
+comparison operators surrounded by brackets seem to work
+comparison operators surrounded by brackets seem to work
+comparison operators surrounded by brackets seem to work
+comparison operators surrounded by brackets seem to work
 --- case sensitivity with and without /i option
 if seems to default to case sensitivity
 if /i seems to work
diff --git a/programs/cmd/wcmd.h b/programs/cmd/wcmd.h
index 8d6eb6b48e..619f65df92 100644
--- a/programs/cmd/wcmd.h
+++ b/programs/cmd/wcmd.h
@@ -154,6 +154,8 @@ static inline BOOL ends_with_backslash( const WCHAR *path )
     return path[0] && path[strlenW(path) - 1] == '\\';
 }
 
+int evaluate_if_condition(WCHAR *p, WCHAR **command, int *test, int *negate);
+
 /* Data structure to hold context when executing batch files */
 
 typedef struct _BATCH_CONTEXT {
diff --git a/programs/cmd/wcmdmain.c b/programs/cmd/wcmdmain.c
index 8fe2d574e5..d137b152d5 100644
--- a/programs/cmd/wcmdmain.c
+++ b/programs/cmd/wcmdmain.c
@@ -1921,13 +1921,35 @@ WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, HANDLE
 
         /* If command starts with 'if ' or 'else ', handle ('s mid line. We should ensure this
            is only true in the command portion of the IF statement, but this
-           should suffice for now
-            FIXME: Silly syntax like "if 1(==1( (
-                                        echo they equal
-                                      )" will be parsed wrong */
+           should suffice for now.
+           To be able to handle ('s in the condition part take as much as evaluate_if_condition
+           would take and skip parsing it here. */
         } else if (WCMD_keyword_ws_found(ifCmd, sizeof(ifCmd)/sizeof(ifCmd[0]), curPos)) {
+          static const WCHAR parmI[]   = {'/','I','\0'};
+          static const WCHAR notW[]    = {'n','o','t','\0'};
+          int negate; /* Negate condition */
+          int test;   /* Condition evaluation result */
+          WCHAR *p, *command;
+
           inIf = TRUE;
 
+          p = curPos+(sizeof(ifCmd)/sizeof(*ifCmd));
+          while (*p == ' ' || *p == '\t') {
+            p++;
+            if (lstrcmpiW(WCMD_parameter(p, 0, NULL, TRUE, FALSE), notW) == 0)
+              p += strlenW(notW);
+            if (lstrcmpiW(WCMD_parameter(p, 0, NULL, TRUE, FALSE), parmI) == 0)
+              p += strlenW(parmI);
+          }
+
+          if (evaluate_if_condition(p, &command, &test, &negate) != -1)
+          {
+              int if_condition_len = command - curPos;
+              memcpy(&curCopyTo[*curLen], curPos, if_condition_len*sizeof(WCHAR));
+              (*curLen)+=if_condition_len;
+              curPos+=if_condition_len;
+          }
+
         } else if (WCMD_keyword_ws_found(ifElse, sizeof(ifElse)/sizeof(ifElse[0]), curPos)) {
           const int keyw_len = sizeof(ifElse)/sizeof(ifElse[0]) + 1;
           inElse = TRUE;
-- 
2.18.0




More information about the wine-devel mailing list