cmd: Handle very odd delimiter support for command line.

oldstable
Jason Edmeades 2012-10-16 23:35:33 +01:00 committed by Alexandre Julliard
parent f9105db050
commit dccccfc273
6 changed files with 63 additions and 50 deletions

View File

@ -128,6 +128,9 @@ void WCMD_batch (WCHAR *file, WCHAR *command, BOOL called, WCHAR *startLabel, HA
* end [O] Optional. Pointer to the last char of param n in s
* raw [I] True to return the parameter in raw format (quotes maintained)
* False returns the parameter with quotes stripped
* wholecmdline [I] True to indicate this routine is being used to parse the
* command line, and special logic for arg0->1 transition
* needs to be applied.
*
* RETURNS
* Success: The nth delimited parameter found in s
@ -143,7 +146,8 @@ void WCMD_batch (WCHAR *file, WCHAR *command, BOOL called, WCHAR *startLabel, HA
* other API calls, e.g. c:\"a b"\c is returned as c:\a b\c. However, some commands
* need to preserve the exact syntax (echo, for, etc) hence the raw option.
*/
WCHAR *WCMD_parameter (WCHAR *s, int n, WCHAR **start, WCHAR **end, BOOL raw)
WCHAR *WCMD_parameter (WCHAR *s, int n, WCHAR **start, WCHAR **end, BOOL raw,
BOOL wholecmdline)
{
static const WCHAR defaultDelims[] = { ' ', '\t', ',', '=', ';', '\0' };
int curParamNb = 0;
@ -173,6 +177,12 @@ WCHAR *WCMD_parameter (WCHAR *s, int n, WCHAR **start, WCHAR **end, BOOL raw)
/* Once we have found a delimiter, break */
if (strchrW(defaultDelims, *p) != NULL) break;
/* Very odd special case - Seems as if a ( acts as a delimiter which is
not swallowed but is effective only when it comes between the program
name and the parameters. Need to avoid this triggering when used
to walk parameters generally. */
if (wholecmdline && curParamNb == 0 && *p=='(') break;
/* If we find a quote, copy until we get the end quote */
if (*p == '"') {
p++;
@ -418,8 +428,9 @@ void WCMD_HandleTildaModifiers(WCHAR **start, const WCHAR *forVariable,
strcpyW(outputparam, context->batchfileW);
} else if ((*lastModifier >= '1' && *lastModifier <= '9')) {
strcpyW(outputparam,
WCMD_parameter (context -> command, *lastModifier-'0' + context -> shift_count[*lastModifier-'0'],
NULL, NULL, FALSE));
WCMD_parameter (context -> command,
*lastModifier-'0' + context -> shift_count[*lastModifier-'0'],
NULL, NULL, FALSE, TRUE));
} else {
strcpyW(outputparam, forValue);
}

View File

@ -541,7 +541,7 @@ void WCMD_copy(WCHAR * command) {
opt_d = opt_v = opt_n = opt_z = opt_y = opt_noty = FALSE;
/* Walk through all args, building up a list of files to process */
thisparam = WCMD_parameter(command, argno++, &rawarg, NULL, TRUE);
thisparam = WCMD_parameter(command, argno++, &rawarg, NULL, TRUE, FALSE);
while (*(thisparam)) {
WCHAR *pos1, *pos2;
BOOL inquotes;
@ -599,7 +599,7 @@ void WCMD_copy(WCHAR * command) {
}
/* This parameter was purely switches, get the next one */
thisparam = WCMD_parameter(command, argno++, &rawarg, NULL, TRUE);
thisparam = WCMD_parameter(command, argno++, &rawarg, NULL, TRUE, FALSE);
continue;
}
@ -623,7 +623,8 @@ void WCMD_copy(WCHAR * command) {
/* Move to next thing to process */
thisparam++;
if (*thisparam == 0x00) thisparam = WCMD_parameter(command, argno++, &rawarg, NULL, TRUE);
if (*thisparam == 0x00)
thisparam = WCMD_parameter(command, argno++, &rawarg, NULL, TRUE, FALSE);
continue;
}
@ -680,7 +681,7 @@ void WCMD_copy(WCHAR * command) {
thisparam = pos1;
continue;
} else {
thisparam = WCMD_parameter(command, argno++, &rawarg, NULL, TRUE);
thisparam = WCMD_parameter(command, argno++, &rawarg, NULL, TRUE, FALSE);
}
}
@ -1018,7 +1019,7 @@ void WCMD_create_dir (WCHAR *command) {
}
/* Loop through all args */
while (TRUE) {
WCHAR *thisArg = WCMD_parameter(command, argno++, &argN, NULL, FALSE);
WCHAR *thisArg = WCMD_parameter(command, argno++, &argN, NULL, FALSE, FALSE);
if (!argN) break;
if (!create_full_path(thisArg)) {
WCMD_print_error ();
@ -1323,7 +1324,7 @@ BOOL WCMD_delete (WCHAR *command) {
WCHAR *thisArg;
argN = NULL;
thisArg = WCMD_parameter (command, argno, &argN, NULL, FALSE);
thisArg = WCMD_parameter (command, argno, &argN, NULL, FALSE, FALSE);
if (!argN)
break; /* no more parameters */
if (argN[0] == '/')
@ -1547,7 +1548,7 @@ void WCMD_for (WCHAR *p, CMD_LIST **cmdList) {
int parameterNo = 0;
/* Handle optional qualifiers (multiple are allowed) */
WCHAR *thisArg = WCMD_parameter(p, parameterNo++, NULL, NULL, FALSE);
WCHAR *thisArg = WCMD_parameter(p, parameterNo++, NULL, NULL, FALSE, FALSE);
optionsRoot[0] = 0;
while (thisArg && *thisArg == '/') {
@ -1571,7 +1572,7 @@ void WCMD_for (WCHAR *p, CMD_LIST **cmdList) {
/* Retrieve next parameter to see if is root/options (raw form required
with for /f, or unquoted in for /r) */
thisArg = WCMD_parameter(p, parameterNo, NULL, NULL, doFileset);
thisArg = WCMD_parameter(p, parameterNo, NULL, NULL, doFileset, FALSE);
/* Next parm is either qualifier, path/options or variable -
only care about it if it is the path/options */
@ -1590,7 +1591,7 @@ void WCMD_for (WCHAR *p, CMD_LIST **cmdList) {
}
/* Step to next token */
thisArg = WCMD_parameter(p, parameterNo++, NULL, NULL, FALSE);
thisArg = WCMD_parameter(p, parameterNo++, NULL, NULL, FALSE, FALSE);
}
/* Ensure line continues with variable */
@ -1615,7 +1616,7 @@ void WCMD_for (WCHAR *p, CMD_LIST **cmdList) {
WINE_TRACE("Variable identified as %s\n", wine_dbgstr_w(variable));
/* Ensure line continues with IN */
thisArg = WCMD_parameter(p, parameterNo++, NULL, NULL, FALSE);
thisArg = WCMD_parameter(p, parameterNo++, NULL, NULL, FALSE, FALSE);
if (!thisArg
|| !(CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
thisArg, sizeof(inW)/sizeof(inW[0]), inW,
@ -1714,7 +1715,7 @@ void WCMD_for (WCHAR *p, CMD_LIST **cmdList) {
WINE_TRACE("Processing for set %p\n", thisSet);
i = 0;
while (*(item = WCMD_parameter (thisSet->command, i, &itemStart, NULL, TRUE))) {
while (*(item = WCMD_parameter (thisSet->command, i, &itemStart, NULL, TRUE, FALSE))) {
/*
* If the parameter within the set has a wildcard then search for matching files
@ -1835,7 +1836,7 @@ void WCMD_for (WCHAR *p, CMD_LIST **cmdList) {
while (WCMD_fgets(buffer, sizeof(buffer)/sizeof(WCHAR), input)) {
/* Skip blank lines*/
parm = WCMD_parameter (buffer, 0, &where, NULL, FALSE);
parm = WCMD_parameter (buffer, 0, &where, NULL, FALSE, FALSE);
WINE_TRACE("Parsed parameter: %s from %s\n", wine_dbgstr_w(parm),
wine_dbgstr_w(buffer));
@ -1866,7 +1867,7 @@ void WCMD_for (WCHAR *p, CMD_LIST **cmdList) {
/* Skip blank lines, and re-extract parameter now string has quotes removed */
strcpyW(buffer, item);
parm = WCMD_parameter (buffer, 0, &where, NULL, FALSE);
parm = WCMD_parameter (buffer, 0, &where, NULL, FALSE, FALSE);
WINE_TRACE("Parsed parameter: %s from %s\n", wine_dbgstr_w(parm),
wine_dbgstr_w(buffer));
@ -2138,7 +2139,7 @@ void WCMD_if (WCHAR *p, CMD_LIST **cmdList) {
WINE_TRACE("Condition: %s\n", wine_dbgstr_w(condition));
if (!lstrcmpiW (condition, errlvlW)) {
WCHAR *param = WCMD_parameter(p, 1+negate, NULL, NULL, FALSE);
WCHAR *param = WCMD_parameter(p, 1+negate, NULL, NULL, FALSE, FALSE);
WCHAR *endptr;
long int param_int = strtolW(param, &endptr, 10);
if (*endptr) {
@ -2146,24 +2147,24 @@ void WCMD_if (WCHAR *p, CMD_LIST **cmdList) {
return;
}
test = ((long int)errorlevel >= param_int);
WCMD_parameter(p, 2+negate, &command, NULL, FALSE);
WCMD_parameter(p, 2+negate, &command, NULL, FALSE, FALSE);
}
else if (!lstrcmpiW (condition, existW)) {
test = (GetFileAttributesW(WCMD_parameter(p, 1+negate, NULL, NULL, FALSE))
test = (GetFileAttributesW(WCMD_parameter(p, 1+negate, NULL, NULL, FALSE, FALSE))
!= INVALID_FILE_ATTRIBUTES);
WCMD_parameter(p, 2+negate, &command, NULL, FALSE);
WCMD_parameter(p, 2+negate, &command, NULL, FALSE, FALSE);
}
else if (!lstrcmpiW (condition, defdW)) {
test = (GetEnvironmentVariableW(WCMD_parameter(p, 1+negate, NULL, NULL, FALSE),
test = (GetEnvironmentVariableW(WCMD_parameter(p, 1+negate, NULL, NULL, FALSE, FALSE),
NULL, 0) > 0);
WCMD_parameter(p, 2+negate, &command, NULL, FALSE);
WCMD_parameter(p, 2+negate, &command, NULL, FALSE, FALSE);
}
else if ((s = strstrW (p, eqeqW))) {
/* We need to get potential surrounding double quotes, so param1/2 can't be used */
WCHAR *leftPart, *leftPartEnd, *rightPart, *rightPartEnd;
s += 2;
WCMD_parameter(p, 0+negate+caseInsensitive, &leftPart, &leftPartEnd, FALSE);
WCMD_parameter(p, 1+negate+caseInsensitive, &rightPart, &rightPartEnd, FALSE);
WCMD_parameter(p, 0+negate+caseInsensitive, &leftPart, &leftPartEnd, FALSE, FALSE);
WCMD_parameter(p, 1+negate+caseInsensitive, &rightPart, &rightPartEnd, FALSE, FALSE);
test = caseInsensitive
? (CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
leftPart, leftPartEnd-leftPart+1,
@ -2171,7 +2172,7 @@ void WCMD_if (WCHAR *p, CMD_LIST **cmdList) {
: (CompareStringW(LOCALE_SYSTEM_DEFAULT, 0,
leftPart, leftPartEnd-leftPart+1,
rightPart, rightPartEnd-rightPart+1) == CSTR_EQUAL);
WCMD_parameter(s, 1, &command, NULL, FALSE);
WCMD_parameter(s, 1, &command, NULL, FALSE, FALSE);
}
else {
WCMD_output_stderr(WCMD_LoadMessage(WCMD_SYNTAXERR));
@ -2346,7 +2347,7 @@ void WCMD_remove_dir (WCHAR *command) {
/* Loop through all args */
while (argN) {
WCHAR *thisArg = WCMD_parameter (command, argno++, &argN, NULL, FALSE);
WCHAR *thisArg = WCMD_parameter (command, argno++, &argN, NULL, FALSE, FALSE);
if (argN && argN[0] != '/') {
WINE_TRACE("rd: Processing arg %s (quals:%s)\n", wine_dbgstr_w(thisArg),
wine_dbgstr_w(quals));
@ -3118,7 +3119,7 @@ void WCMD_type (WCHAR *command) {
/* Loop through all args */
errorlevel = 0;
while (argN) {
WCHAR *thisArg = WCMD_parameter (command, argno++, &argN, NULL, FALSE);
WCHAR *thisArg = WCMD_parameter (command, argno++, &argN, NULL, FALSE, FALSE);
HANDLE h;
WCHAR buffer[512];
@ -3212,7 +3213,7 @@ void WCMD_more (WCHAR *command) {
WCMD_enter_paged_mode(moreStrPage);
while (argN) {
WCHAR *thisArg = WCMD_parameter (command, argno++, &argN, NULL, FALSE);
WCHAR *thisArg = WCMD_parameter (command, argno++, &argN, NULL, FALSE, FALSE);
HANDLE h;
if (!argN) break;

View File

@ -803,7 +803,7 @@ void WCMD_directory (WCHAR *cmd)
prevEntry = NULL;
while (argN) {
WCHAR fullname[MAXSTRING];
WCHAR *thisArg = WCMD_parameter(cmd, argno++, &argN, NULL, FALSE);
WCHAR *thisArg = WCMD_parameter(cmd, argno++, &argN, NULL, FALSE, FALSE);
if (argN && argN[0] != '/') {
WINE_TRACE("Found parm '%s'\n", wine_dbgstr_w(thisArg));

View File

@ -57,7 +57,7 @@ var=33@space@
0@space@
3@space@
3@space@
@todo_wine@4@space@
4@space@
------ Testing invocation with CMD /C -------------
0@space@
1@space@
@ -69,7 +69,7 @@ var=33@space@
2@space@
0@space@
3@space@
@todo_wine@4@space@
4@space@
---------- Testing CMD /C quoting -----------------
"hi"
1@space@
@ -93,7 +93,7 @@ THIS FAILS: cmd "/c"say one
THIS FAILS: cmd ignoreme/c say one
--------- Testing special characters --------------
0@space@
@todo_wine@0@space@
0@space@
)@space@
[@space@
]@space@
@ -113,10 +113,10 @@ THIS FAILS: cmd ignoreme/c say one
1:1,2:@space@
1:(1),2:@space@
1:1(2),2:@space@
@todo_wine@1:(1),2:@space@
@todo_wine@1:((1)),2:@space@
@todo_wine@1:(1)(2),2:@space@
@todo_wine@1:(1),2:(2)@space@
1:(1),2:@space@
1:((1)),2:@space@
1:(1)(2),2:@space@
1:(1),2:(2)@space@
1:1,2:2@space@
1:1,2:2@space@
1:1,2:2@space@
@ -126,7 +126,7 @@ THIS FAILS: cmd ignoreme/c say one
0:tell,1:1,2:2,All:'1 2'@or_broken@0:tell,1:1,2:2,All:' 1 2'
0:tell,1:1,2:2,All:'1 2'@or_broken@0:tell,1:1,2:2,All:' 1 2'
0:tell,1:1,2:2,All:'==1==2'
@todo_wine@0:tell,1:(1234),2:,All:'(1234)'
@todo_wine@0:tell,1:(12(34),2:,All:'(12(34)'
@todo_wine@0:tell,1:(12,2:34),All:'(12;34)'
@todo_wine@--------- Finished --------------
0:tell,1:(1234),2:,All:'(1234)'
0:tell,1:(12(34),2:,All:'(12(34)'
0:tell,1:(12,2:34),All:'(12;34)'
--------- Finished --------------

View File

@ -107,7 +107,7 @@ static inline BOOL WCMD_is_console_handle(HANDLE h)
return (((DWORD_PTR)h) & 3) == 3;
}
WCHAR *WCMD_fgets (WCHAR *buf, DWORD n, HANDLE stream);
WCHAR *WCMD_parameter (WCHAR *s, int n, WCHAR **start, WCHAR **end, BOOL raw);
WCHAR *WCMD_parameter (WCHAR *s, int n, WCHAR **start, WCHAR **end, BOOL raw, BOOL wholecmdline);
WCHAR *WCMD_skip_leading_spaces (WCHAR *string);
BOOL WCMD_keyword_ws_found(const WCHAR *keyword, int len, const WCHAR *ptr);
void WCMD_HandleTildaModifiers(WCHAR **start, const WCHAR *forVariable, const WCHAR *forValue, BOOL justFors);

View File

@ -837,13 +837,14 @@ static void handleExpansion(WCHAR *cmd, BOOL justFors,
/* Replace use of %0...%9 if in batch program*/
} else if (!justFors && context && (i >= 0) && (i <= 9)) {
t = WCMD_parameter(context -> command, i + context -> shift_count[i], NULL, NULL, TRUE);
t = WCMD_parameter(context -> command, i + context -> shift_count[i],
NULL, NULL, TRUE, TRUE);
WCMD_strsubstW(p, p+2, t, -1);
/* Replace use of %* if in batch program*/
} else if (!justFors && context && *(p+1)=='*') {
WCHAR *startOfParms = NULL;
WCMD_parameter(context -> command, 0, NULL, &startOfParms, TRUE);
WCMD_parameter(context -> command, 0, NULL, &startOfParms, TRUE, TRUE);
if (startOfParms != NULL) {
startOfParms++; /* Skip to first delimiter then skip whitespace */
while (*startOfParms==' ' || *startOfParms == '\t') startOfParms++;
@ -1021,7 +1022,7 @@ void WCMD_run_program (WCHAR *command, BOOL called)
/* Quick way to get the filename is to extract the first argument. */
WINE_TRACE("Running '%s' (%d)\n", wine_dbgstr_w(command), called);
firstParam = WCMD_parameter(command, 0, NULL, NULL, FALSE);
firstParam = WCMD_parameter(command, 0, NULL, NULL, FALSE, TRUE);
if (!firstParam) return;
/* Calculate the search path and stem to search for */
@ -1359,7 +1360,7 @@ void WCMD_execute (const WCHAR *command, const WCHAR *redirects,
/* Otherwise STDIN could come from a '<' redirect */
} else if ((p = strchrW(new_redir,'<')) != NULL) {
h = CreateFileW(WCMD_parameter(++p, 0, NULL, NULL, FALSE), GENERIC_READ, FILE_SHARE_READ,
h = CreateFileW(WCMD_parameter(++p, 0, NULL, NULL, FALSE, FALSE), GENERIC_READ, FILE_SHARE_READ,
&sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (h == INVALID_HANDLE_VALUE) {
WCMD_print_error ();
@ -1404,7 +1405,7 @@ void WCMD_execute (const WCHAR *command, const WCHAR *redirects,
WINE_TRACE("Redirect %d (%p) to %d (%p)\n", handle, GetStdHandle(idx_stdhandles[idx]), idx, h);
} else {
WCHAR *param = WCMD_parameter(p, 0, NULL, NULL, FALSE);
WCHAR *param = WCMD_parameter(p, 0, NULL, NULL, FALSE, FALSE);
h = CreateFileW(param, GENERIC_WRITE, 0, &sa, creationDisposition,
FILE_ATTRIBUTE_NORMAL, NULL);
if (h == INVALID_HANDLE_VALUE) {
@ -2330,14 +2331,14 @@ int wmain (int argc, WCHAR *argvW[])
args = 1; /* start at first arg, skipping cmd.exe itself */
opt_c = opt_k = opt_q = opt_s = FALSE;
WCMD_parameter(cmdLine, args, &argPos, NULL, TRUE);
WCMD_parameter(cmdLine, args, &argPos, NULL, TRUE, TRUE);
while (argPos && argPos[0] != 0x00)
{
WCHAR c;
WINE_TRACE("Command line parm: '%s'\n", wine_dbgstr_w(argPos));
if (argPos[0]!='/' || argPos[1]=='\0') {
args++;
WCMD_parameter(cmdLine, args, &argPos, NULL, TRUE);
WCMD_parameter(cmdLine, args, &argPos, NULL, TRUE, TRUE);
continue;
}
@ -2362,7 +2363,7 @@ int wmain (int argc, WCHAR *argvW[])
if (argPos[2]==0 || argPos[2]==' ' || argPos[2]=='\t') {
args++;
WCMD_parameter(cmdLine, args, &argPos, NULL, TRUE);
WCMD_parameter(cmdLine, args, &argPos, NULL, TRUE, TRUE);
}
else /* handle `cmd /cnotepad.exe` and `cmd /x/c ...` */
{
@ -2443,7 +2444,7 @@ int wmain (int argc, WCHAR *argvW[])
/* Finally, we only stay in new mode IF the first parameter is quoted and
is a valid executable, ie must exist, otherwise drop back to old mode */
if (!opt_s) {
WCHAR *thisArg = WCMD_parameter(cmd, 0, NULL, NULL, FALSE);
WCHAR *thisArg = WCMD_parameter(cmd, 0, NULL, NULL, FALSE, TRUE);
WCHAR pathext[MAXSTRING];
BOOL found = FALSE;