getoptargv.cpp revision c58f1213e628a545081c70e26c6b67a841cff880
/* $Id$ */
/** @file
* IPRT - Command Line Parsing, Argument Vector.
*/
/*
* Copyright (C) 2012 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
/*******************************************************************************
* Header Files *
*******************************************************************************/
/**
* Array indexed by the quoting type and 7-bit ASCII character.
*
* We include some extra stuff here that the corresponding shell would normally
* require quoting of.
*/
{
{ 0xfe, 0xff, 0x0f, 0x00, 0x65, 0x00, 0x00, 0x50 },
{ 0xfe, 0xff, 0x0f, 0x00, 0xd7, 0x07, 0x00, 0xd8 },
};
#if 0 /* To re-generate the bitmaps. */
#include <stdio.h>
int main()
{
do { \
} while (0)
/* just flag all the control chars as in need of quoting. */
/* ... and space of course */
SET_ALL(' ');
/* MS CRT / CMD.EXE: */
/* Bourne shell: */
{
printf(" {");
printf(" },\n");
}
return 0;
}
#endif /* To re-generate the bitmaps. */
/**
* Look for an unicode code point in the separator string.
*
* @returns true if it's a separator, false if it isn't.
* @param Cp The code point.
* @param pszSeparators The separators.
*/
{
/* This could be done in a more optimal fashion. Probably worth a
separate RTStr function at some point. */
for (;;)
{
AssertRCReturn(rc, false);
return true;
if (!CpSep)
return false;
}
}
/**
* Look for an 7-bit ASCII character in the separator string.
*
* @returns true if it's a separator, false if it isn't.
* @param ch The character.
* @param pszSeparators The separators.
* @param cchSeparators The number of separators chars.
*/
{
switch (cchSeparators)
{
return false;
default:
}
}
/**
* Checks if the character is in the set of separators
*
* @returns true if it is, false if it isn't.
*
* @param Cp The code point.
* @param pszSeparators The separators.
* @param cchSeparators The length of @a pszSeparators.
*/
DECL_FORCE_INLINE(bool) rtGetOptIsCpInSet(RTUNICP Cp, const char *pszSeparators, size_t cchSeparators)
{
}
/**
* Skips any delimiters at the start of the string that is pointed to.
*
* @returns VINF_SUCCESS or RTStrGetCpEx status code.
* @param ppszSrc Where to get and return the string pointer.
* @param pszSeparators The separators.
* @param cchSeparators The length of @a pszSeparators.
*/
static int rtGetOptSkipDelimiters(const char **ppszSrc, const char *pszSeparators, size_t cchSeparators)
{
const char *pszRet;
for (;;)
{
if (RT_FAILURE(rc))
{
return rc;
}
if ( !Cp
break;
}
return VINF_SUCCESS;
}
RTDECL(int) RTGetOptArgvFromString(char ***ppapszArgv, int *pcArgs, const char *pszCmdLine, const char *pszSeparators)
{
/*
* Some input validation.
*/
if (!pszSeparators)
pszSeparators = " \t\n\r";
else
/*
* Parse the command line and chop off it into argv individual argv strings.
*/
int rc = VINF_SUCCESS;
const char *pszSrc = pszCmdLine;
if (!pszDup)
return VERR_NO_STR_MEMORY;
unsigned iArg = 0;
while (*pszSrc)
{
/* Skip stuff */
if (RT_FAILURE(rc))
break;
if (!*pszSrc)
break;
/* Start a new entry. */
if ((iArg % 32) == 0)
{
if (!pvNew)
{
rc = VERR_NO_MEMORY;
break;
}
}
/* Parse and copy the string over. */
for (;;)
{
break;
if (!CpQuote)
{
break;
else
}
else
CpQuote = 0;
}
*pszDst++ = '\0';
break;
}
if (RT_FAILURE(rc))
{
return rc;
}
/*
* Terminate the array.
* Check for empty string to make sure we've got an array.
*/
if (iArg == 0)
{
if (!papszArgs)
return VERR_NO_MEMORY;
}
*ppapszArgv = papszArgs;
return VINF_SUCCESS;
}
{
if (papszArgv)
{
/*
* We've really only _two_ allocations here. Check the code in
* RTGetOptArgvFromString for the particulars.
*/
}
}
/**
* Checks if the argument needs quoting or not.
*
* @returns true if it needs, false if it don't.
* @param pszArg The argument.
* @param fFlags Quoting style.
* @param pcch Where to store the argument length when quoting
* is not required. (optimization)
*/
{
unsigned char ch;
{
if ( ch < 128
return true;
psz++;
}
return false;
}
/**
* Grows the command line string buffer.
*
* @returns VINF_SUCCESS or VERR_NO_STR_MEMORY.
* @param ppszCmdLine Pointer to the command line string pointer.
* @param pcbCmdLineAlloc Pointer to the allocation length variable.
* @param cchMin The minimum size to grow with, kind of.
*/
{
cb *= 2;
cb *= 2;
*pcbCmdLineAlloc = cb;
}
/**
* Checks if we have a sequence of DOS slashes followed by a double quote char.
*
* @returns true / false accordingly.
* @param psz The string.
*/
{
while (*psz == '\\')
psz++;
}
RTDECL(int) RTGetOptArgvToString(char **ppszCmdLine, const char * const *papszArgv, uint32_t fFlags)
{
if (RT_FAILURE(rc)) \
break; \
} \
if (RT_FAILURE(rc)) \
break; \
} \
/*
* Take the realloc approach, it requires less code and is probably more
* efficient than figuring out the size first.
*/
int rc = VINF_SUCCESS;
if (!pszCmdLine)
return VERR_NO_STR_MEMORY;
{
if (i > 0)
{
PUT_CH(' ');
}
/* does it need quoting? */
{
/* No quoting needed, just append the argument. */
}
{
/*
* Microsoft CRT quoting. Quote the whole argument in double
* quotes to make it easier to read and code.
*/
PUT_CH('"');
char ch;
{
if ( ch == '\\'
{
PUT_SZ("\\\\");
}
else if (ch == '"')
{
PUT_SZ("\\\"");
}
else
{
}
}
PUT_CH('"');
}
else
{
/*
* Bourne Shell quoting. Quote the whole thing in single quotes
* and use double quotes for any single quote chars.
*/
PUT_CH('\'');
char ch;
{
if (ch == '\'')
{
PUT_SZ("'\"'\"'");
}
else
{
}
}
PUT_CH('\'');
}
}
/* Set return value / cleanup. */
if (RT_SUCCESS(rc))
{
}
else
return rc;
}
RTDECL(int) RTGetOptArgvToUtf16String(PRTUTF16 *ppwszCmdLine, const char * const *papszArgv, uint32_t fFlags)
{
char *pszCmdLine;
if (RT_SUCCESS(rc))
{
}
return rc;
}