Blame winpr/libwinpr/thread/argv.c

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