From eda33f22ff86486e0d22eb47fa941e156506627c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Delanoy?= Date: Thu, 25 Aug 2011 00:48:22 +0200 Subject: [PATCH] cmd: Fix if/else tab handling. --- programs/cmd/builtins.c | 11 ++++++----- programs/cmd/tests/test_builtins.cmd | 7 +++++++ programs/cmd/tests/test_builtins.cmd.exp | 7 +++++++ programs/cmd/wcmdmain.c | 19 +++++++++---------- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/programs/cmd/builtins.c b/programs/cmd/builtins.c index 7cc65201fe9..5e405d580f0 100644 --- a/programs/cmd/builtins.c +++ b/programs/cmd/builtins.c @@ -1250,7 +1250,7 @@ static void WCMD_part_execute(CMD_LIST **cmdList, const WCHAR *firstcmd, if (isIF) processThese = conditionTRUE; while (*cmdList) { - const WCHAR ifElse[] = {'e','l','s','e',' ','\0'}; + const WCHAR ifElse[] = {'e','l','s','e'}; /* execute all appropriate commands */ curPosition = *cmdList; @@ -1280,16 +1280,17 @@ static void WCMD_part_execute(CMD_LIST **cmdList, const WCHAR *firstcmd, /* End of the command - does 'ELSE ' follow as the next command? */ } else { - if (isIF && CompareStringW(LOCALE_USER_DEFAULT, - NORM_IGNORECASE | SORT_STRINGSORT, - (*cmdList)->command, 5, ifElse, -1) == CSTR_EQUAL) { + if (isIF + && WCMD_keyword_ws_found(ifElse, sizeof(ifElse)/sizeof(ifElse[0]), + (*cmdList)->command)) { /* Swap between if and else processing */ processThese = !processThese; /* Process the ELSE part */ if (processThese) { - WCHAR *cmd = ((*cmdList)->command) + strlenW(ifElse); + const int keyw_len = sizeof(ifElse)/sizeof(ifElse[0]) + 1; + WCHAR *cmd = ((*cmdList)->command) + keyw_len; /* Skip leading whitespace between condition and the command */ while (*cmd && (*cmd==' ' || *cmd=='\t')) cmd++; diff --git a/programs/cmd/tests/test_builtins.cmd b/programs/cmd/tests/test_builtins.cmd index cd46dbd838a..cc083d2845d 100644 --- a/programs/cmd/tests/test_builtins.cmd +++ b/programs/cmd/tests/test_builtins.cmd @@ -391,6 +391,13 @@ if /i foo==FOO echo if /i seems to work if /i not foo==FOO echo if /i seems to be broken if /I foo==FOO echo if /I seems to work if /I not foo==FOO echo if /I seems to be broken +if@tab@1==1 echo doom +if @tab@1==1 echo doom +if 1==1 (echo doom) else@tab@echo quake +if@tab@not @tab@1==@tab@0 @tab@echo lol +if 1==0@tab@(echo doom) else echo quake +if 1==0 (echo doom)@tab@else echo quake +if 1==0 (echo doom) else@tab@echo quake echo -----------Testing for ----------- echo ...plain FOR diff --git a/programs/cmd/tests/test_builtins.cmd.exp b/programs/cmd/tests/test_builtins.cmd.exp index dafb6a3c66e..535ef44e5a9 100644 --- a/programs/cmd/tests/test_builtins.cmd.exp +++ b/programs/cmd/tests/test_builtins.cmd.exp @@ -271,6 +271,13 @@ Testing case sensitivity with and without /i option if seems to default to case sensitivity if /i seems to work if /I seems to work +doom +doom +doom +lol +quake +quake +quake -----------Testing for ----------- ...plain FOR A diff --git a/programs/cmd/wcmdmain.c b/programs/cmd/wcmdmain.c index 3efabfa72d8..63e8c164ee0 100644 --- a/programs/cmd/wcmdmain.c +++ b/programs/cmd/wcmdmain.c @@ -1755,8 +1755,8 @@ WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, HANDLE static WCHAR *extraSpace = NULL; /* Deliberately never freed */ const WCHAR remCmd[] = {'r','e','m'}; const WCHAR forCmd[] = {'f','o','r'}; - const WCHAR ifCmd[] = {'i','f',' ','\0'}; - const WCHAR ifElse[] = {'e','l','s','e',' ','\0'}; + const WCHAR ifCmd[] = {'i','f'}; + const WCHAR ifElse[] = {'e','l','s','e'}; BOOL inRem = FALSE; BOOL inFor = FALSE; BOOL inIn = FALSE; @@ -1847,24 +1847,23 @@ WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, HANDLE } else if (WCMD_keyword_ws_found(forCmd, sizeof(forCmd)/sizeof(forCmd[0]), curPos)) { inFor = TRUE; - /* If command starts with 'if' or 'else', handle ('s mid line. We should ensure this + /* 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 */ - } else if (CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT, - curPos, 3, ifCmd, -1) == CSTR_EQUAL) { + } else if (WCMD_keyword_ws_found(ifCmd, sizeof(ifCmd)/sizeof(ifCmd[0]), curPos)) { inIf = TRUE; - } else if (CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT, - curPos, 5, ifElse, -1) == CSTR_EQUAL) { + } 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; lastWasElse = TRUE; onlyWhiteSpace = TRUE; - memcpy(&curCopyTo[*curLen], curPos, 5*sizeof(WCHAR)); - (*curLen)+=5; - curPos+=5; + memcpy(&curCopyTo[*curLen], curPos, keyw_len*sizeof(WCHAR)); + (*curLen)+=keyw_len; + curPos+=keyw_len; continue; /* In a for loop, the DO command will follow a close bracket followed by