2013-09-22 01:16:49 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Process Argument Vector Functions
|
|
|
|
*
|
|
|
|
* Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
2013-09-25 23:47:18 +04:00
|
|
|
#include <winpr/heap.h>
|
2013-09-22 01:16:49 +04:00
|
|
|
#include <winpr/handle.h>
|
|
|
|
|
|
|
|
#include <winpr/thread.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2014-08-18 19:22:22 +04:00
|
|
|
#include "../log.h"
|
2014-08-18 20:57:08 +04:00
|
|
|
#define TAG WINPR_TAG("thread")
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2013-09-22 01:16:49 +04:00
|
|
|
/**
|
|
|
|
* CommandLineToArgvW function:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/windows/desktop/bb776391/
|
|
|
|
*
|
|
|
|
* CommandLineToArgvW has a special interpretation of backslash characters
|
|
|
|
* when they are followed by a quotation mark character ("), as follows:
|
|
|
|
*
|
|
|
|
* 2n backslashes followed by a quotation mark produce n backslashes followed by a quotation mark.
|
2019-11-06 17:24:51 +03:00
|
|
|
* (2n) + 1 backslashes followed by a quotation mark again produce n backslashes followed by a
|
|
|
|
* quotation mark. n backslashes not followed by a quotation mark simply produce n backslashes.
|
2013-09-22 01:16:49 +04:00
|
|
|
*
|
2019-11-06 17:24:51 +03:00
|
|
|
* The address returned by CommandLineToArgvW is the address of the first element in an array of
|
|
|
|
* LPWSTR values; the number of pointers in this array is indicated by pNumArgs. Each pointer to a
|
|
|
|
* null-terminated Unicode string represents an individual argument found on the command line.
|
2013-09-22 01:16:49 +04:00
|
|
|
*
|
|
|
|
* CommandLineToArgvW allocates a block of contiguous memory for pointers to the argument strings,
|
|
|
|
* and for the argument strings themselves; the calling application must free the memory used by the
|
2019-11-06 17:24:51 +03:00
|
|
|
* argument list when it is no longer needed. To free the memory, use a single call to the LocalFree
|
|
|
|
* function.
|
2013-09-22 01:16:49 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parsing C++ Command-Line Arguments:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft
|
|
|
|
*
|
|
|
|
* Microsoft C/C++ startup code uses the following rules when
|
|
|
|
* interpreting arguments given on the operating system command line:
|
|
|
|
*
|
|
|
|
* Arguments are delimited by white space, which is either a space or a tab.
|
|
|
|
*
|
|
|
|
* The caret character (^) is not recognized as an escape character or delimiter.
|
|
|
|
* The character is handled completely by the command-line parser in the operating
|
|
|
|
* system before being passed to the argv array in the program.
|
|
|
|
*
|
|
|
|
* A string surrounded by double quotation marks ("string") is interpreted as a
|
|
|
|
* single argument, regardless of white space contained within. A quoted string
|
|
|
|
* can be embedded in an argument.
|
|
|
|
*
|
|
|
|
* A double quotation mark preceded by a backslash (\") is interpreted as a
|
|
|
|
* literal double quotation mark character (").
|
|
|
|
*
|
|
|
|
* Backslashes are interpreted literally, unless they immediately
|
|
|
|
* precede a double quotation mark.
|
|
|
|
*
|
|
|
|
* If an even number of backslashes is followed by a double quotation mark,
|
|
|
|
* one backslash is placed in the argv array for every pair of backslashes,
|
|
|
|
* and the double quotation mark is interpreted as a string delimiter.
|
|
|
|
*
|
|
|
|
* If an odd number of backslashes is followed by a double quotation mark,
|
|
|
|
* one backslash is placed in the argv array for every pair of backslashes,
|
|
|
|
* and the double quotation mark is "escaped" by the remaining backslash,
|
|
|
|
* causing a literal double quotation mark (") to be placed in argv.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
LPSTR* CommandLineToArgvA(LPCSTR lpCmdLine, int* pNumArgs)
|
2013-09-22 01:16:49 +04:00
|
|
|
{
|
2021-08-02 13:13:34 +03:00
|
|
|
const char* p;
|
2019-10-29 12:18:09 +03:00
|
|
|
size_t length;
|
2021-08-26 12:23:32 +03:00
|
|
|
const char* pBeg;
|
|
|
|
const char* pEnd;
|
2014-08-18 21:34:47 +04:00
|
|
|
char* buffer;
|
|
|
|
char* pOutput;
|
2017-11-14 15:55:24 +03:00
|
|
|
int numArgs = 0;
|
2014-08-18 21:34:47 +04:00
|
|
|
LPSTR* pArgs;
|
2019-10-29 12:18:09 +03:00
|
|
|
size_t maxNumArgs;
|
|
|
|
size_t maxBufferSize;
|
|
|
|
size_t cmdLineLength;
|
2014-08-18 21:34:47 +04:00
|
|
|
BOOL* lpEscapedChars;
|
2013-09-22 03:49:00 +04:00
|
|
|
LPSTR lpEscapedCmdLine;
|
2013-09-22 01:16:49 +04:00
|
|
|
|
|
|
|
if (!lpCmdLine)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!pNumArgs)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
pArgs = NULL;
|
2013-09-22 03:49:00 +04:00
|
|
|
lpEscapedCmdLine = NULL;
|
2019-10-29 12:18:09 +03:00
|
|
|
cmdLineLength = strlen(lpCmdLine);
|
2019-11-06 17:24:51 +03:00
|
|
|
lpEscapedChars = (BOOL*)calloc(cmdLineLength + 1, sizeof(BOOL));
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!lpEscapedChars)
|
|
|
|
return NULL;
|
2013-09-22 03:49:00 +04:00
|
|
|
|
|
|
|
if (strstr(lpCmdLine, "\\\""))
|
2013-09-22 01:16:49 +04:00
|
|
|
{
|
2019-10-29 12:18:09 +03:00
|
|
|
size_t i;
|
|
|
|
size_t n;
|
2021-08-02 13:13:34 +03:00
|
|
|
const char* pLastEnd = NULL;
|
2019-11-06 17:24:51 +03:00
|
|
|
lpEscapedCmdLine = (char*)calloc(cmdLineLength + 1, sizeof(char));
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!lpEscapedCmdLine)
|
|
|
|
{
|
|
|
|
free(lpEscapedChars);
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2021-08-02 13:13:34 +03:00
|
|
|
p = (const char*)lpCmdLine;
|
|
|
|
pLastEnd = (const char*)lpCmdLine;
|
2019-11-06 17:24:51 +03:00
|
|
|
pOutput = (char*)lpEscapedCmdLine;
|
2013-09-22 01:16:49 +04:00
|
|
|
|
2013-09-22 04:05:14 +04:00
|
|
|
while (p < &lpCmdLine[cmdLineLength])
|
2013-09-22 01:16:49 +04:00
|
|
|
{
|
2013-09-22 04:05:14 +04:00
|
|
|
pBeg = strstr(p, "\\\"");
|
|
|
|
|
|
|
|
if (!pBeg)
|
2013-09-22 03:49:00 +04:00
|
|
|
{
|
2019-10-29 12:18:09 +03:00
|
|
|
length = strlen(p);
|
2013-09-22 04:05:14 +04:00
|
|
|
CopyMemory(pOutput, p, length);
|
|
|
|
pOutput += length;
|
2013-09-22 03:49:00 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-09-22 04:05:14 +04:00
|
|
|
pEnd = pBeg + 2;
|
2013-09-22 01:16:49 +04:00
|
|
|
|
2013-09-22 04:05:14 +04:00
|
|
|
while (pBeg >= lpCmdLine)
|
|
|
|
{
|
|
|
|
if (*pBeg != '\\')
|
|
|
|
{
|
|
|
|
pBeg++;
|
|
|
|
break;
|
|
|
|
}
|
2013-09-22 01:16:49 +04:00
|
|
|
|
2013-09-22 04:05:14 +04:00
|
|
|
pBeg--;
|
|
|
|
}
|
2013-09-22 03:49:00 +04:00
|
|
|
|
2019-10-29 12:18:09 +03:00
|
|
|
n = ((pEnd - pBeg) - 1);
|
|
|
|
length = (pBeg - pLastEnd);
|
2013-09-22 04:05:14 +04:00
|
|
|
CopyMemory(pOutput, p, length);
|
|
|
|
pOutput += length;
|
|
|
|
p += length;
|
2013-09-22 01:16:49 +04:00
|
|
|
|
2013-09-22 04:05:14 +04:00
|
|
|
for (i = 0; i < (n / 2); i++)
|
2015-11-05 20:10:05 +03:00
|
|
|
*pOutput++ = '\\';
|
2013-09-22 01:16:49 +04:00
|
|
|
|
2013-09-22 04:05:14 +04:00
|
|
|
p += n + 1;
|
|
|
|
|
|
|
|
if ((n % 2) != 0)
|
2013-09-23 01:23:00 +04:00
|
|
|
lpEscapedChars[pOutput - lpEscapedCmdLine] = TRUE;
|
2013-09-22 01:16:49 +04:00
|
|
|
|
2015-11-05 20:10:05 +03:00
|
|
|
*pOutput++ = '"';
|
2013-09-22 04:05:14 +04:00
|
|
|
pLastEnd = p;
|
|
|
|
}
|
2013-09-22 01:16:49 +04:00
|
|
|
|
2015-11-05 20:10:05 +03:00
|
|
|
*pOutput++ = '\0';
|
2019-11-06 17:24:51 +03:00
|
|
|
lpCmdLine = (LPCSTR)lpEscapedCmdLine;
|
2019-10-29 12:18:09 +03:00
|
|
|
cmdLineLength = strlen(lpCmdLine);
|
2013-09-22 03:49:00 +04:00
|
|
|
}
|
2013-09-22 01:16:49 +04:00
|
|
|
|
2013-09-23 01:23:00 +04:00
|
|
|
maxNumArgs = 2;
|
2021-08-02 13:13:34 +03:00
|
|
|
p = (const char*)lpCmdLine;
|
2013-09-22 01:16:49 +04:00
|
|
|
|
2015-11-05 20:10:05 +03:00
|
|
|
while (p < lpCmdLine + cmdLineLength)
|
2013-09-22 03:49:00 +04:00
|
|
|
{
|
2015-11-05 20:10:05 +03:00
|
|
|
p += strcspn(p, " \t");
|
|
|
|
p += strspn(p, " \t");
|
2013-09-22 03:49:00 +04:00
|
|
|
maxNumArgs++;
|
|
|
|
}
|
2013-09-22 01:16:49 +04:00
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
maxBufferSize = (maxNumArgs * (sizeof(char*))) + (cmdLineLength + 1);
|
2019-11-06 17:24:51 +03:00
|
|
|
buffer = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, maxBufferSize);
|
2013-09-22 03:49:00 +04:00
|
|
|
|
|
|
|
if (!buffer)
|
2017-01-25 15:09:25 +03:00
|
|
|
{
|
2018-08-24 13:15:06 +03:00
|
|
|
free(lpEscapedCmdLine);
|
2017-01-25 15:09:25 +03:00
|
|
|
free(lpEscapedChars);
|
2013-09-22 03:49:00 +04:00
|
|
|
return NULL;
|
2017-01-25 15:09:25 +03:00
|
|
|
}
|
2013-09-22 03:49:00 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
pArgs = (LPSTR*)buffer;
|
|
|
|
pOutput = (char*)&buffer[maxNumArgs * (sizeof(char*))];
|
2021-08-02 13:13:34 +03:00
|
|
|
p = (const char*)lpCmdLine;
|
2013-09-22 03:49:00 +04:00
|
|
|
|
2015-11-05 20:10:05 +03:00
|
|
|
while (p < lpCmdLine + cmdLineLength)
|
2013-09-22 03:49:00 +04:00
|
|
|
{
|
2015-11-05 20:10:05 +03:00
|
|
|
pBeg = p;
|
2013-09-22 03:49:00 +04:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2015-11-05 20:10:05 +03:00
|
|
|
p += strcspn(p, " \t\"\0");
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-11-05 20:10:05 +03:00
|
|
|
if ((*p != '"') || !lpEscapedChars[p - lpCmdLine])
|
|
|
|
break;
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-11-05 20:10:05 +03:00
|
|
|
p++;
|
2013-09-22 03:49:00 +04:00
|
|
|
}
|
2013-09-22 01:16:49 +04:00
|
|
|
|
2015-11-05 20:10:05 +03:00
|
|
|
if (*p != '"')
|
2013-09-22 03:49:00 +04:00
|
|
|
{
|
|
|
|
/* no whitespace escaped with double quotes */
|
2019-10-29 12:18:09 +03:00
|
|
|
length = (p - pBeg);
|
2013-09-22 03:49:00 +04:00
|
|
|
CopyMemory(pOutput, pBeg, length);
|
|
|
|
pOutput[length] = '\0';
|
|
|
|
pArgs[numArgs++] = pOutput;
|
|
|
|
pOutput += (length + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-05 20:10:05 +03:00
|
|
|
p++;
|
2013-09-22 03:49:00 +04:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2015-11-05 20:10:05 +03:00
|
|
|
p += strcspn(p, "\"\0");
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-11-05 20:10:05 +03:00
|
|
|
if ((*p != '"') || !lpEscapedChars[p - lpCmdLine])
|
|
|
|
break;
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-11-05 20:10:05 +03:00
|
|
|
p++;
|
2013-09-22 03:49:00 +04:00
|
|
|
}
|
|
|
|
|
2015-11-05 20:10:05 +03:00
|
|
|
if (*p != '"')
|
2014-08-18 19:22:22 +04:00
|
|
|
WLog_ERR(TAG, "parsing error: uneven number of unescaped double quotes!");
|
2013-09-22 01:16:49 +04:00
|
|
|
|
2015-11-05 20:10:05 +03:00
|
|
|
if (*p && *(++p))
|
|
|
|
p += strcspn(p, " \t\0");
|
2013-09-22 03:49:00 +04:00
|
|
|
|
|
|
|
pArgs[numArgs++] = pOutput;
|
|
|
|
|
2015-11-05 20:10:05 +03:00
|
|
|
while (pBeg < p)
|
2013-09-22 03:49:00 +04:00
|
|
|
{
|
|
|
|
if (*pBeg != '"')
|
2015-11-05 20:10:05 +03:00
|
|
|
*pOutput++ = *pBeg;
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2013-09-22 03:49:00 +04:00
|
|
|
pBeg++;
|
2013-09-22 01:16:49 +04:00
|
|
|
}
|
|
|
|
|
2015-11-05 20:10:05 +03:00
|
|
|
*pOutput++ = '\0';
|
2013-09-22 01:16:49 +04:00
|
|
|
}
|
2013-09-22 03:49:00 +04:00
|
|
|
|
2015-11-05 20:10:05 +03:00
|
|
|
p += strspn(p, " \t");
|
2013-09-22 01:16:49 +04:00
|
|
|
}
|
2013-09-22 03:49:00 +04:00
|
|
|
|
2015-05-11 10:07:39 +03:00
|
|
|
free(lpEscapedCmdLine);
|
|
|
|
free(lpEscapedChars);
|
2013-09-22 01:16:49 +04:00
|
|
|
*pNumArgs = numArgs;
|
|
|
|
return pArgs;
|
|
|
|
}
|
|
|
|
|
2013-09-25 23:47:18 +04:00
|
|
|
#ifndef _WIN32
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
LPWSTR* CommandLineToArgvW(LPCWSTR lpCmdLine, int* pNumArgs)
|
2013-09-22 01:16:49 +04:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|