getoptargv.cpp revision e64031e20c39650a7bc902a3e1aba613b9415dee
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * IPRT - Command Line Parsing, Argument Vector.
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2010 Oracle Corporation
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * available from http://www.virtualbox.org. This file is free software;
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * you can redistribute it and/or modify it under the terms of the GNU
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * General Public License (GPL) as published by the Free Software
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * The contents of this file may alternatively be used under the terms
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * of the Common Development and Distribution License Version 1.0
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * VirtualBox OSE distribution, in which case the provisions of the
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * CDDL are applicable instead of those of the GPL.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * You may elect to license modified versions of this file under the
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * terms and conditions of either the GPL or the CDDL or both.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync/*******************************************************************************
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync* Header Files *
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync*******************************************************************************/
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync/*******************************************************************************
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync* Header Files *
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync*******************************************************************************/
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync * Array indexed by the quoting type and 7-bit ASCII character.
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync * We include some extra stuff here that the corresponding shell would normally
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync * require qouting of.
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsyncstatic uint8_t const g_abmQuoteChars[RTGETOPTARGV_CNV_QUOTE_MASK + 1][128/8] =
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync { 0xfe, 0xff, 0x0f, 0x00, 0x65, 0x00, 0x00, 0x50 },
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync { 0xfe, 0xff, 0x0f, 0x00, 0xd7, 0x07, 0x00, 0xd8 },
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync#if 0 /* To re-generate the bitmaps. */
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync for (size_t iType = 0; iType <= RTGETOPTARGV_CNV_QUOTE_MASK; iType++) \
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync } while (0)
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync ASMBitSet(&g_abmQuoteChars[RTGETOPTARGV_CNV_QUOTE_##ConstSuffix], (ch));
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync /* just flag all the control chars as in need of quoting. */
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync /* ... and space of course */
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync /* MS CRT / CMD.EXE: */
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync /* Bourne shell: */
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync for (size_t iType = 0; iType <= RTGETOPTARGV_CNV_QUOTE_MASK; iType++)
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync printf(iByte == 0 ? " 0x%02x" : ", 0x%02x", g_abmQuoteChars[iType][iByte]);
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync#endif /* To re-generate the bitmaps. */
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * Look for an unicode code point in the separator string.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * @returns true if it's a separator, false if it isn't.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * @param Cp The code point.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * @param pszSeparators The separators.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsyncstatic bool rtGetOptIsUniCpInString(RTUNICP Cp, const char *pszSeparators)
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync /* This could be done in a more optimal fashion. Probably worth a
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync separate RTStr function at some point. */
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync return true;
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync return false;
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * Look for an 7-bit ASCII character in the separator string.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * @returns true if it's a separator, false if it isn't.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * @param ch The character.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * @param pszSeparators The separators.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * @param cchSeparators The number of separators chars.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsyncDECLINLINE(bool) rtGetOptIsAsciiInSet(char ch, const char *pszSeparators, size_t cchSeparators)
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync return false;
0aa150e34ed49f14aaa37368c2e6999ec89e5f43vboxsync return memchr(pszSeparators, ch, cchSeparators) != NULL;
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * Checks if the character is in the set of separators
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * @returns true if it is, false if it isn't.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * @param Cp The code point.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * @param pszSeparators The separators.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * @param cchSeparators The length of @a pszSeparators.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsyncDECL_FORCE_INLINE(bool) rtGetOptIsCpInSet(RTUNICP Cp, const char *pszSeparators, size_t cchSeparators)
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync return rtGetOptIsAsciiInSet((char)Cp, pszSeparators, cchSeparators);
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * Skips any delimiters at the start of the string that is pointed to.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * @returns VINF_SUCCESS or RTStrGetCpEx status code.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * @param ppszSrc Where to get and return the string pointer.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * @param pszSeparators The separators.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * @param cchSeparators The length of @a pszSeparators.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsyncstatic int rtGetOptSkipDelimiters(const char **ppszSrc, const char *pszSeparators, size_t cchSeparators)
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync const char *pszRet;
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync || !rtGetOptIsCpInSet(Cp, pszSeparators, cchSeparators))
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsyncRTDECL(int) RTGetOptArgvFromString(char ***ppapszArgv, int *pcArgs, const char *pszCmdLine, const char *pszSeparators)
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * Some input validation.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync size_t const cchSeparators = strlen(pszSeparators);
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync AssertReturn(cchSeparators > 0, VERR_INVALID_PARAMETER);
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * Parse the command line and chop off it into argv individual argv strings.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync char *pszDup = (char *)RTMemAlloc(strlen(pszSrc) + 1);
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync unsigned iArg = 0;
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync /* Skip stuff */
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync rc = rtGetOptSkipDelimiters(&pszSrc, pszSeparators, cchSeparators);
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync /* Start a new entry. */
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync void *pvNew = RTMemRealloc(papszArgs, (iArg + 33) * sizeof(char *));
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync /* Parse and copy the string over. */
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync else if (rtGetOptIsCpInSet(Cp, pszSeparators, cchSeparators))
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * Terminate the array.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync * Check for empty string to make sure we've got an array.
b6cc4092c1e80655a5bc19dc125e772a8d2b870dvboxsync papszArgs = (char **)RTMemAlloc(1 * sizeof(char *));
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync * Checks if the argument needs quoting or not.
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync * @returns true if it needs, false if it don't.
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync * @param pszArg The argument.
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync * @param fFlags Quoting style.
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync * @param pcch Where to store the argument length when quoting
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync * is not required. (optimization)
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsyncDECLINLINE(bool) rtGetOpArgvRequiresQuoting(const char *pszArg, uint32_t fFlags, size_t *pcch)
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync unsigned char ch;
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync && ASMBitTest(&g_abmQuoteChars[fFlags & RTGETOPTARGV_CNV_QUOTE_MASK], ch))
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync return true;
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync return false;
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync * Grows the command line string buffer.
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync * @returns VINF_SUCCESS or VERR_NO_STR_MEMORY.
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync * @param ppszCmdLine Pointer to the command line string pointer.
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync * @param pcbCmdLineAlloc Pointer to the allocation length variable.
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync * @param cchMin The minimum size to grow with, kind of.
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsyncstatic int rtGetOptArgvToStringGrow(char **ppszCmdLine, size_t *pcbCmdLineAlloc, size_t cchMin)
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync * Checks if we have a sequence of DOS slashes followed by a double quote char.
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync * @returns true / false accordingly.
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync * @param psz The string.
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsyncDECLINLINE(bool) rtGetOptArgvMsCrtIsSlashQuote(const char *psz)
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsyncRTDECL(int) RTGetOptArgvToString(char **ppszCmdLine, const char * const *papszArgv, uint32_t fFlags)
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync AssertReturn(!(fFlags & ~RTGETOPTARGV_CNV_QUOTE_MASK), VERR_INVALID_PARAMETER);
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync rc = rtGetOptArgvToStringGrow(&pszCmdLine, &cbCmdLineAlloc, 1); \
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync if (RT_UNLIKELY(off + (cch) >= cbCmdLineAlloc)) { \
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync rc = rtGetOptArgvToStringGrow(&pszCmdLine, &cbCmdLineAlloc, (cch)); \
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync * Take the realloc approach, it requires less code and is probably more
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync * efficient than figuring out the size first.
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync /* does it need quoting? */
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync if (!rtGetOpArgvRequiresQuoting(pszArg, fFlags, &cchArg))
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync /* No quoting needed, just append the argument. */
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync else if ((fFlags & RTGETOPTARGV_CNV_QUOTE_MASK) == RTGETOPTARGV_CNV_QUOTE_MS_CRT)
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync * Microsoft CRT quoting. Quote the whole argument in double
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync * quotes to make it easier to read and code.
ddee36cc1075885bd13dabbd814f18e6090b157fvboxsync * Bourne Shell quoting. Quote the whole thing in single quotes
ddee36cc1075885bd13dabbd814f18e6090b157fvboxsync * and use double quotes for any single quote chars.
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsync /* Set return value / cleanup. */
e8d7104a0c14c78c147fedeb2dee7053a17354ebvboxsyncRTDECL(int) RTGetOptArgvToUtf16String(PRTUTF16 *ppwszCmdLine, const char * const *papszArgv, uint32_t fFlags)