Index: sdk/lib/crt/misc/getargs.c =================================================================== --- sdk/lib/crt/misc/getargs.c (revision 72199) +++ sdk/lib/crt/misc/getargs.c (working copy) @@ -181,16 +181,19 @@ */ void __getmainargs(int* argc, char*** argv, char*** env, int expand_wildcards, int* new_mode) { - int i, afterlastspace, ignorespace, doexpand; + int i, doexpand, slashesAdded, escapedQuote, inQuotes, bufferIndex; size_t len; char* aNewCmdln; + char* buffer; /* missing threading init */ i = 0; - afterlastspace = 0; - ignorespace = 0; doexpand = expand_wildcards; + escapedQuote = FALSE; + slashesAdded = 0; + inQuotes = 0; + bufferIndex = 0; if (__argv && _environ) { @@ -203,51 +206,87 @@ __argc = 0; len = strlen(_acmdln); + buffer = malloc(sizeof(char) * len); - /* Allocate a temporary buffer to be used instead of the original _acmdln parameter. */ - aNewCmdln = strndup(_acmdln, len); - - while (aNewCmdln[i]) + // Reference: https://msdn.microsoft.com/en-us/library/a1y7w461(v=vs.71).aspx + while (TRUE) { - if (aNewCmdln[i] == '"') - { - if(ignorespace) - { - ignorespace = 0; - } - else - { - ignorespace = 1; - doexpand = 0; - } - memmove(aNewCmdln + i, aNewCmdln + i + 1, len - i); - len--; - continue; - } + // Arguments are delimited by white space, which is either a space or a tab. + if (i >= len || (_acmdln[i] == ' ' || _acmdln[i] == '\t') && !inQuotes) + { + aexpand(strndup(buffer, bufferIndex), doexpand); + // Copy the last element from buffer and quit the loop + if (i >= len) + { + break; + } - if (aNewCmdln[i] == ' ' && !ignorespace) - { - aexpand(strndup(aNewCmdln + afterlastspace, i - afterlastspace), doexpand); - i++; - while (aNewCmdln[i] == ' ') - i++; - afterlastspace=i; - doexpand = expand_wildcards; - } - else - { - i++; - } - } + while (_acmdln[i] == ' ' || _acmdln[i] == '\t') + ++i; + bufferIndex = 0; + slashesAdded = 0; + escapedQuote = FALSE; + continue; + } - if (aNewCmdln[afterlastspace] != 0) - { - aexpand(strndup(aNewCmdln + afterlastspace, i - afterlastspace), doexpand); + if (_acmdln[i] == '\\') + { + buffer[bufferIndex++] = _acmdln[i]; + ++slashesAdded; + ++i; + escapedQuote = FALSE; + continue; + } + + if (_acmdln[i] == '\"') + { + if (slashesAdded > 0) + { + if (slashesAdded % 2 == 0) + { + // If an even number of backslashes is followed by a double quotation mark, then one backslash (\) + // is placed in the argv array for every pair of backslashes (\\), and the double quotation mark (") + // is interpreted as a string delimiter. + bufferIndex -= slashesAdded / 2; + } + else + { + // If an odd number of backslashes is followed by a double quotation mark, then one backslash (\) + // is placed in the argv array for every pair of backslashes (\\) and the double quotation mark is + // interpreted as an escape sequence by the remaining backslash, causing a literal double quotation mark (") + // to be placed in argv. + bufferIndex -= slashesAdded / 2 + 1; + buffer[bufferIndex++] = '\"'; + slashesAdded = 0; + escapedQuote = TRUE; + ++i; + continue; + } + slashesAdded = 0; + } + else if (!inQuotes && i > 0 && _acmdln[i - 1] == '\"' && !escapedQuote) + { + buffer[bufferIndex++] = '\"'; + ++i; + escapedQuote = TRUE; + continue; + } + slashesAdded = 0; + escapedQuote = FALSE; + inQuotes = !inQuotes; + doexpand = inQuotes ? FALSE : expand_wildcards; + ++i; + continue; + } + + buffer[bufferIndex++] = _acmdln[i]; + slashesAdded = 0; + escapedQuote = FALSE; + ++i; } /* Free the temporary buffer. */ - free(aNewCmdln); - + free(buffer); HeapValidate(GetProcessHeap(), 0, NULL); *argc = __argc; @@ -269,16 +308,18 @@ void __wgetmainargs(int* argc, wchar_t*** wargv, wchar_t*** wenv, int expand_wildcards, int* new_mode) { - int i, afterlastspace, ignorespace, doexpand; + int i, doexpand, slashesAdded, escapedQuote, inQuotes, bufferIndex; size_t len; - wchar_t* wNewCmdln; + wchar_t* buffer; /* missing threading init */ i = 0; - afterlastspace = 0; - ignorespace = 0; doexpand = expand_wildcards; + escapedQuote = FALSE; + slashesAdded = 0; + inQuotes = 0; + bufferIndex = 0; if (__wargv && __winitenv) { @@ -289,52 +330,88 @@ } __argc = 0; - len = wcslen(_wcmdln); + buffer = malloc(sizeof(wchar_t) * len); - /* Allocate a temporary buffer to be used instead of the original _wcmdln parameter. */ - wNewCmdln = wcsndup(_wcmdln, len); - - while (wNewCmdln[i]) + // Reference: https://msdn.microsoft.com/en-us/library/a1y7w461(v=vs.71).aspx + while (TRUE) { - if (wNewCmdln[i] == L'"') - { - if(ignorespace) - { - ignorespace = 0; - } - else - { - ignorespace = 1; - doexpand = 0; - } - memmove(wNewCmdln + i, wNewCmdln + i + 1, (len - i) * sizeof(wchar_t)); - len--; - continue; - } + // Arguments are delimited by white space, which is either a space or a tab. + if (i >= len || (_wcmdln[i] == ' ' || _wcmdln[i] == '\t') && !inQuotes) + { + wexpand(wcsndup(buffer, bufferIndex), doexpand); + // Copy the last element from buffer and quit the loop + if (i >= len) + { + break; + } - if (wNewCmdln[i] == L' ' && !ignorespace) - { - wexpand(wcsndup(wNewCmdln + afterlastspace, i - afterlastspace), doexpand); - i++; - while (wNewCmdln[i] == L' ') - i++; - afterlastspace=i; - doexpand = expand_wildcards; - } - else - { - i++; - } - } + while (_wcmdln[i] == ' ' || _wcmdln[i] == '\t') + ++i; + bufferIndex = 0; + slashesAdded = 0; + escapedQuote = FALSE; + continue; + } - if (wNewCmdln[afterlastspace] != 0) - { - wexpand(wcsndup(wNewCmdln + afterlastspace, i - afterlastspace), doexpand); + if (_wcmdln[i] == '\\') + { + buffer[bufferIndex++] = _wcmdln[i]; + ++slashesAdded; + ++i; + escapedQuote = FALSE; + continue; + } + + if (_wcmdln[i] == '\"') + { + if (slashesAdded > 0) + { + if (slashesAdded % 2 == 0) + { + // If an even number of backslashes is followed by a double quotation mark, then one backslash (\) + // is placed in the argv array for every pair of backslashes (\\), and the double quotation mark (") + // is interpreted as a string delimiter. + bufferIndex -= slashesAdded / 2; + } + else + { + // If an odd number of backslashes is followed by a double quotation mark, then one backslash (\) + // is placed in the argv array for every pair of backslashes (\\) and the double quotation mark is + // interpreted as an escape sequence by the remaining backslash, causing a literal double quotation mark (") + // to be placed in argv. + bufferIndex -= slashesAdded / 2 + 1; + buffer[bufferIndex++] = '\"'; + slashesAdded = 0; + escapedQuote = TRUE; + ++i; + continue; + } + slashesAdded = 0; + } + else if (!inQuotes && i > 0 && _wcmdln[i - 1] == '\"' && !escapedQuote) + { + buffer[bufferIndex++] = '\"'; + ++i; + escapedQuote = TRUE; + continue; + } + slashesAdded = 0; + escapedQuote = FALSE; + inQuotes = !inQuotes; + doexpand = inQuotes ? FALSE : expand_wildcards; + ++i; + continue; + } + + buffer[bufferIndex++] = _wcmdln[i]; + slashesAdded = 0; + escapedQuote = FALSE; + ++i; } /* Free the temporary buffer. */ - free(wNewCmdln); + free(buffer); HeapValidate(GetProcessHeap(), 0, NULL); Index: modules/rostests/apitests/crt/__getmainargs.c =================================================================== --- modules/rostests/apitests/crt/__getmainargs.c (nonexistent) +++ modules/rostests/apitests/crt/__getmainargs.c (working copy) @@ -0,0 +1,57 @@ +/* + * PROJECT: ReactOS api tests + * LICENSE: GPLv2+ - See COPYING in the top level directory + * PURPOSE: Test for __getmainargs + * PROGRAMMER: Yaroslav Veremenko + */ + +#include +#include +#include + +const char **__p__acmdln(void); +void __getmainargs(int* argc, char*** argv, char*** env, int expand_wildcards, int* new_mode); + +#define winetest_ok_str(x, y) \ + winetest_ok(strcmp(x, y) == 0, "Wrong string. Expected '%s', got '%s'\n", y, x) +#define ok_args (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : ok_args_imp + + +void +ok_args_imp(const char* input_args, /*char** argv,*/ const char* arg1, const char* arg2, const char* arg3) +{ + int argc = 0, mode = 0; + char** argv, **env; + + /* Remove cached argv, setup our input as program argument. */ + *__p___argv() = NULL; + *__p__acmdln() = input_args; + + /* Process the commandline stored in _acmdln */ + __getmainargs(&argc, &argv, &env, 0, &mode); + + winetest_ok(argc == 4, "Wrong value for argc, expected: 4, got: %d\n", argc); + if(argc != 4) + return; + + winetest_ok_str(argv[0], "test.exe"); + winetest_ok_str(argv[1], arg1); + winetest_ok_str(argv[2], arg2); + winetest_ok_str(argv[3], arg3); +} + +START_TEST(__getmainargs) +{ + ok_args("test.exe \"a b c\" d e", "a b c", "d", "e"); + ok_args("test.exe \"ab\\\"c\" \"\\\\\" d", "ab\"c", "\\", "d"); + ok_args("test.exe a\\\\\\b d\"e f\"g h", "a\\\\\\b", "de fg", "h"); + ok_args("test.exe a\\\\\\\"b c d", "a\\\"b", "c", "d"); + ok_args("test.exe a\\\\\\\\\"b c\" d e", "a\\\\b c", "d", "e"); + ok_args("test.exe a b \"\"", "a", "b", ""); + ok_args("test.exe a \"\" b", "a", "", "b"); + ok_args("test.exe a \"b\"\" c", "a", "b\"", "c"); + ok_args("test.exe a \"b\\\"\" c", "a", "b\"", "c"); + ok_args("test.exe a \" b\\ \"\" c", "a", " b\\ \"", "c"); + ok_args("test.exe a \"b\\ \"\"\" c\" d", "a", "b\\ \" c", "d"); + ok_args("test.exe a \"b\\ \"\"\" \"c \"\"\"\" d", "a", "b\\ \" c", "\" d"); +} Property changes on: rostests/apitests/crt/__getmainargs.c ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Index: rostests/apitests/crt/msvcrt_crt_apitest.cmake =================================================================== --- modules/rostests/apitests/crt/msvcrt_crt_apitest.cmake (revision 72199) +++ modules/rostests/apitests/crt/msvcrt_crt_apitest.cmake (working copy) @@ -66,7 +66,7 @@ # __doserrno.c # __fpecode.c # __get_app_type.c -# __getmainargs.c + __getmainargs.c # __initenv # __iob_func.c # __isascii.c Index: modules/rostests/apitests/crt/testlist.c =================================================================== --- modules/rostests/apitests/crt/testlist.c (revision 72199) +++ modules/rostests/apitests/crt/testlist.c (working copy) @@ -25,6 +25,7 @@ extern void func_wcsnlen(void); extern void func_wcstombs(void); extern void func_wcstoul(void); +extern void func___getmainargs(void); extern void func_static_construct(void); extern void func_static_init(void); @@ -50,6 +51,7 @@ #endif #if defined(TEST_STATIC_CRT) #elif defined(TEST_MSVCRT) + { "__getmainargs", func___getmainargs }, { "_vscprintf", func__vscprintf }, { "_vscwprintf", func__vscwprintf },