[shell32.dll]

- rewrite the parser to handle command line argument parsing
- include basic description of what the rules for parsing are
- some minor formatting here and there (couldn't be helped)

svn path=/branches/shell32_new-bringup/; revision=53533
This commit is contained in:
Claudiu Mihail 2011-09-02 19:29:30 +00:00
parent fc902e6f58
commit 18d876cbf2

View file

@ -32,6 +32,125 @@ static const WCHAR wszEmpty[] = {0};
#define SEE_MASK_CLASSALL (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY)
static void ParseNoTildeEffect(PWSTR &res, LPCWSTR &args, DWORD &len, DWORD &used, int argNum)
{
bool firstCharQuote = false;
bool quotes_opened = false;
bool backslash_encountered = false;
for (int curArg=0; curArg<=argNum && *args; ++curArg)
{
firstCharQuote = false;
if (*args == '"')
{
quotes_opened = true;
firstCharQuote = true;
args++;
}
while(*args)
{
if (*args == '\\')
{
// if we found a backslash then flip the variable
backslash_encountered = !backslash_encountered;
}
else if (*args == '"')
{
if (quotes_opened)
{
if (*(args+1) != '"')
{
quotes_opened = false;
args++;
break;
}
else
{
args++;
}
}
else
{
quotes_opened = true;
}
backslash_encountered = false;
}
else
{
backslash_encountered = false;
if (*args == ' ' && !firstCharQuote)
break;
}
if (curArg == argNum)
{
used++;
if (used < len)
*res++ = *args;
}
args++;
}
while(*args == ' ')
++args;
}
}
static void ParseTildeEffect(PWSTR &res, LPCWSTR &args, DWORD &len, DWORD &used, int argNum)
{
bool quotes_opened = false;
bool backslash_encountered = false;
for (int curArg=0; curArg<=argNum && *args; ++curArg)
{
while(*args)
{
if (*args == '\\')
{
// if we found a backslash then flip the variable
backslash_encountered = !backslash_encountered;
}
else if (*args == '"')
{
if (quotes_opened)
{
if (*(args+1) != '"')
{
quotes_opened = false;
}
else
{
args++;
}
}
else
{
quotes_opened = true;
}
backslash_encountered = false;
}
else
{
backslash_encountered = false;
if (*args == ' ' && !quotes_opened && curArg!=argNum)
break;
}
if (curArg == argNum)
{
used++;
if (used < len)
*res++ = *args;
}
args++;
}
}
}
/***********************************************************************
* SHELL_ArgifyW [Internal]
@ -48,7 +167,18 @@ static const WCHAR wszEmpty[] = {0};
* %S ???
* %* all following parameters (see batfile)
*
*/
* The way we parse the command line arguments was determined through extensive
* testing and can be summed up by the following rules"
*
* - %2
* - if first letter is " break on first non literal " and include any white spaces
* - if first letter is NOT " break on first " or white space
* - if " is opened any pair of consecutive " results in ONE literal "
*
* - %~2
* - use rules from here http://www.autohotkey.net/~deleyd/parameters/parameters.htm
*/
static BOOL SHELL_ArgifyW(WCHAR* out, DWORD len, const WCHAR* fmt, const WCHAR* lpFile, LPITEMIDLIST pidl, LPCWSTR args, DWORD* out_len)
{
WCHAR xlpFile[1024];
@ -57,10 +187,14 @@ static BOOL SHELL_ArgifyW(WCHAR* out, DWORD len, const WCHAR* fmt, const WCHAR*
PWSTR res = out;
PCWSTR cmd;
DWORD used = 0;
bool tildeEffect = false;
TRACE("%p, %d, %s, %s, %p, %p\n", out, len, debugstr_w(fmt),
debugstr_w(lpFile), pidl, args);
DbgPrint("[shell32, SHELL_ArgifyW] Processing %ws\n", args);
DbgPrint("[shell32, SHELL_ArgifyW] fmt = %ws\n", fmt);
while (*fmt)
{
if (*fmt == '%')
@ -69,28 +203,19 @@ static BOOL SHELL_ArgifyW(WCHAR* out, DWORD len, const WCHAR* fmt, const WCHAR*
{
case '\0':
case '%':
{
used++;
if (used < len)
*res++ = '%';
break;
}; break;
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
case '*':
{
if (args)
{
if (*fmt == '*')
{
used++;
if (used < len)
*res++ = '"';
while(*args)
{
used++;
@ -100,30 +225,43 @@ static BOOL SHELL_ArgifyW(WCHAR* out, DWORD len, const WCHAR* fmt, const WCHAR*
args++;
}
used++;
if (used < len)
*res++ = '"';
break;
}
else
}
}; break;
case '~':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
//case '0':
{
while(*args && !isspace(*args))
if (*fmt == '~')
{
used++;
if (used < len)
*res++ = *args++;
else
args++;
fmt++;
tildeEffect = true;
}
while(isspace(*args))
++args;
}
break;
if (args)
{
if (tildeEffect)
{
ParseTildeEffect(res, args, len, used, *fmt - '2');
tildeEffect = false;
}
else
{
break;
ParseNoTildeEffect(res, args, len, used, *fmt - '2');
}
}
}; break;
case '1':
if (!done || (*fmt == '1'))
{
@ -150,7 +288,8 @@ static BOOL SHELL_ArgifyW(WCHAR* out, DWORD len, const WCHAR* fmt, const WCHAR*
*/
case 'l':
case 'L':
if (lpFile) {
if (lpFile)
{
used += wcslen(lpFile);
if (used < len)
{
@ -163,7 +302,8 @@ static BOOL SHELL_ArgifyW(WCHAR* out, DWORD len, const WCHAR* fmt, const WCHAR*
case 'i':
case 'I':
if (pidl) {
if (pidl)
{
DWORD chars = 0;
/* %p should not exceed 8, maybe 16 when looking forward to 64bit.
* allowing a buffer of 100 should more than exceed all needs */
@ -172,9 +312,12 @@ static BOOL SHELL_ArgifyW(WCHAR* out, DWORD len, const WCHAR* fmt, const WCHAR*
HGLOBAL hmem = SHAllocShared(pidl, ILGetSize(pidl), 0);
pv = SHLockShared(hmem, 0);
chars = swprintf(buf, wszILPtr, pv);
if (chars >= sizeof(buf)/sizeof(WCHAR))
ERR("pidl format buffer too small!\n");
used += chars;
if (used < len)
{
wcscpy(res,buf);
@ -248,6 +391,8 @@ static BOOL SHELL_ArgifyW(WCHAR* out, DWORD len, const WCHAR* fmt, const WCHAR*
if (out_len)
*out_len = used;
DbgPrint("[shell32, SHELL_ArgifyW] Done result = %ws\n", out);
return found_p1;
}
@ -258,7 +403,8 @@ static HRESULT SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl, LPWSTR psz
HRESULT hr = SHGetDesktopFolder(&desktop);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr))
{
hr = desktop->GetDisplayNameOf(pidl, SHGDN_FORPARSING, &strret);
if (SUCCEEDED(hr))
@ -298,17 +444,23 @@ static UINT_PTR SHELL_ExecuteW(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
/* ShellExecute specifies the command from psei->lpDirectory
* if present. Not from the current dir as CreateProcess does */
if( lpDirectory )
if( ( gcdret = GetCurrentDirectoryW( MAX_PATH, curdir)))
if( !SetCurrentDirectoryW( lpDirectory))
if ( lpDirectory )
if ( ( gcdret = GetCurrentDirectoryW( MAX_PATH, curdir)))
if ( !SetCurrentDirectoryW( lpDirectory))
ERR("cannot set directory %s\n", debugstr_w(lpDirectory));
ZeroMemory(&startup,sizeof(STARTUPINFOW));
startup.cb = sizeof(STARTUPINFOW);
startup.dwFlags = STARTF_USESHOWWINDOW;
startup.wShowWindow = psei->nShow;
dwCreationFlags = CREATE_UNICODE_ENVIRONMENT;
if (psei->fMask & SEE_MASK_NO_CONSOLE)
dwCreationFlags |= CREATE_NEW_CONSOLE;
//DbgPrint("[shell32, SHELL_ExecuteW] CreateProcessW cmd = %ws\n", (LPWSTR)lpCmd);
//DbgBreakPoint();
if (CreateProcessW(NULL, (LPWSTR)lpCmd, NULL, NULL, FALSE, dwCreationFlags, env,
lpDirectory, &startup, &info))
{
@ -318,6 +470,7 @@ static UINT_PTR SHELL_ExecuteW(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
if (WaitForInputIdle( info.hProcess, 30000 ) == WAIT_FAILED)
WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
retval = 33;
if (psei->fMask & SEE_MASK_NOCLOSEPROCESS)
psei_out->hProcess = info.hProcess;
else
@ -333,6 +486,7 @@ static UINT_PTR SHELL_ExecuteW(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
TRACE("returning %lu\n", retval);
psei_out->hInstApp = (HINSTANCE)retval;
if( gcdret )
if( !SetCurrentDirectoryW( curdir))
ERR("cannot return to directory %s\n", debugstr_w(curdir));
@ -628,6 +782,7 @@ static UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOpera
filetypelen /= sizeof(WCHAR);
if (filetypelen == sizeof(filetype)/sizeof(WCHAR))
filetypelen--;
filetype[filetypelen] = '\0';
TRACE("File type: %s\n", debugstr_w(filetype));
}
@ -650,7 +805,7 @@ static UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOpera
SHELL_ArgifyW(lpResult, resultLen, command, xlpFile, pidl, args, &finishedLen);
if (finishedLen > resultLen)
ERR("Argify buffer not large enough.. truncated\n");
DbgPrint("[shell32, SHELL_FindExecutable] Remove double quotation marks and command line arguments\n");
/* Remove double quotation marks and command line arguments */
if (*lpResult == '"')
{
@ -1370,11 +1525,14 @@ static UINT_PTR SHELL_quote_and_execute( LPCWSTR wcmd, LPCWSTR wszParameters, LP
strcpyW(wszQuotedCmd, wQuote);
strcatW(wszQuotedCmd, wcmd);
strcatW(wszQuotedCmd, wQuote);
if (wszParameters[0]) {
if (wszParameters[0])
{
strcatW(wszQuotedCmd, wSpace);
strcatW(wszQuotedCmd, wszParameters);
}
TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName), debugstr_w(psei->lpVerb), debugstr_w(wszQuotedCmd), debugstr_w(lpstrProtocol));
if (*lpstrProtocol)
retval = execute_from_key(lpstrProtocol, wszApplicationName, env, psei->lpParameters, wcmd, execfunc, psei, psei_out);
else
@ -1498,14 +1656,20 @@ BOOL SHELL_execute( LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc )
else if (*sei_tmp.lpFile == '\"')
{
DWORD l = strlenW(sei_tmp.lpFile+1);
if(l >= dwApplicationNameLen) dwApplicationNameLen = l+1;
if(l >= dwApplicationNameLen)
dwApplicationNameLen = l+1;
wszApplicationName = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen*sizeof(WCHAR));
memcpy(wszApplicationName, sei_tmp.lpFile+1, (l+1)*sizeof(WCHAR));
if (wszApplicationName[l-1] == '\"')
wszApplicationName[l-1] = '\0';
appKnownSingular = TRUE;
TRACE("wszApplicationName=%s\n",debugstr_w(wszApplicationName));
} else {
}
else
{
DWORD l = strlenW(sei_tmp.lpFile)+1;
if(l > dwApplicationNameLen) dwApplicationNameLen = l+1;
wszApplicationName = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen*sizeof(WCHAR));
@ -1563,7 +1727,8 @@ BOOL SHELL_execute( LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc )
pSEH->Release();
if (hr == S_OK) {
if (hr == S_OK)
{
HeapFree(GetProcessHeap(), 0, wszApplicationName);
if (wszParameters != parametersBuffer)
HeapFree(GetProcessHeap(), 0, wszParameters);
@ -1677,9 +1842,11 @@ BOOL SHELL_execute( LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc )
TRACE("execute:%s,%s,%s\n", debugstr_w(wszApplicationName), debugstr_w(wszParameters), debugstr_w(wszDir));
/* separate out command line arguments from executable file name */
if (!*sei_tmp.lpParameters && !appKnownSingular) {
if (!*sei_tmp.lpParameters && !appKnownSingular)
{
/* If the executable path is quoted, handle the rest of the command line as parameters. */
if (sei_tmp.lpFile[0] == '"') {
if (sei_tmp.lpFile[0] == '"')
{
LPWSTR src = wszApplicationName/*sei_tmp.lpFile*/ + 1;
LPWSTR dst = wfileName;
LPWSTR end;
@ -1690,12 +1857,14 @@ BOOL SHELL_execute( LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc )
*dst = '\0';
if (*src == '"') {
if (*src == '"')
{
end = ++src;
while(isspace(*src))
++src;
} else
}
else
end = src;
/* copy the parameter string to 'wszParameters' */
@ -1712,7 +1881,8 @@ BOOL SHELL_execute( LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc )
LPWSTR space, s;
LPWSTR beg = wszApplicationName/*sei_tmp.lpFile*/;
for(s=beg; (space= const_cast<LPWSTR>(strchrW(s, ' '))); s=space+1) {
for(s=beg; (space= const_cast<LPWSTR>(strchrW(s, ' '))); s=space+1)
{
int idx = space-sei_tmp.lpFile;
memcpy(buffer, sei_tmp.lpFile, idx * sizeof(WCHAR));
buffer[idx] = '\0';
@ -1735,7 +1905,8 @@ BOOL SHELL_execute( LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc )
lstrcpynW(wfileName, sei_tmp.lpFile,sizeof(wfileName)/sizeof(WCHAR));
}
} else
}
else
lstrcpynW(wfileName, sei_tmp.lpFile,sizeof(wfileName)/sizeof(WCHAR));
lpFile = wfileName;
@ -1750,13 +1921,15 @@ BOOL SHELL_execute( LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc )
wcmdLen = len;
}
strcpyW(wcmd, wszApplicationName);
if (sei_tmp.lpParameters[0]) {
if (sei_tmp.lpParameters[0])
{
strcatW(wcmd, wSpace);
strcatW(wcmd, wszParameters);
}
retval = execfunc(wcmd, NULL, FALSE, &sei_tmp, sei);
if (retval > 32) {
if (retval > 32)
{
HeapFree(GetProcessHeap(), 0, wszApplicationName);
if (wszParameters != parametersBuffer)
HeapFree(GetProcessHeap(), 0, wszParameters);