VirtualBox

Changeset 27968 in vbox


Ignore:
Timestamp:
Apr 2, 2010 8:36:35 PM (14 years ago)
Author:
vboxsync
Message:

RTGetOptArgvToString: Implemented bourne shell style quoting.

Location:
trunk/src/VBox/Runtime
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/misc/getoptargv.cpp

    r27386 r27968  
    390390{
    391391    AssertReturn(!(fFlags & ~RTGETOPTARGV_CNV_QUOTE_MASK), VERR_INVALID_PARAMETER);
    392     AssertReturn((fFlags & RTGETOPTARGV_CNV_QUOTE_MASK) == RTGETOPTARGV_CNV_QUOTE_MS_CRT, VERR_NOT_IMPLEMENTED);
    393392
    394393#define PUT_CH(ch) \
     
    462461            PUT_CH('"');
    463462        }
    464         else /* bourne shell */
    465         {
    466             AssertFailed(/*later*/);
     463        else
     464        {
     465            /*
     466             * Bourne Shell quoting.  Quote the whole thing in single quotes
     467             * and use double quotes for any single quote chars.
     468             */
     469            PUT_CH('\'');
     470            char ch;
     471            while ((ch = *pszArg++))
     472            {
     473                if (ch == '\'')
     474                {
     475                    PUT_SZ("'\"'\"'");
     476                }
     477                else
     478                {
     479                    PUT_CH(ch);
     480                }
     481            }
     482            PUT_CH('\'');
    467483        }
    468484    }
  • trunk/src/VBox/Runtime/testcase/tstRTGetOptArgv.cpp

    r27797 r27968  
    4949        const char * const      apszArgs[5];
    5050        const char             *pszCmdLine;
    51     } s_aTests[] =
     51    } s_aMscCrtTests[] =
    5252    {
    5353        {
     
    6969    };
    7070
    71     for (size_t i = 0; i < RT_ELEMENTS(s_aTests); i++)
     71    for (size_t i = 0; i < RT_ELEMENTS(s_aMscCrtTests); i++)
    7272    {
    7373        char *pszCmdLine = NULL;
    74         int rc = RTGetOptArgvToString(&pszCmdLine, s_aTests[i].apszArgs, RTGETOPTARGV_CNV_QUOTE_MS_CRT);
    75         RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
    76         if (strcmp(s_aTests[i].pszCmdLine, pszCmdLine))
     74        int rc = RTGetOptArgvToString(&pszCmdLine, s_aMscCrtTests[i].apszArgs, RTGETOPTARGV_CNV_QUOTE_MS_CRT);
     75        RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
     76        if (strcmp(s_aMscCrtTests[i].pszCmdLine, pszCmdLine))
    7777            RTTestIFailed("g_aTest[%i] failed:\n"
    7878                          " got      '%s'\n"
    7979                          " expected '%s'\n",
    80                           i, pszCmdLine, s_aTests[i].pszCmdLine);
     80                          i, pszCmdLine, s_aMscCrtTests[i].pszCmdLine);
     81        RTStrFree(pszCmdLine);
     82    }
     83
     84
     85    RTTestISub("RTGetOptArgvToString / BOURNE_SH");
     86
     87    static const struct
     88    {
     89        const char * const      apszArgs[5];
     90        const char             *pszCmdLine;
     91    } s_aBournShTests[] =
     92    {
     93        {
     94            { "abcd", "a ", " b", " c ", NULL },
     95            "abcd 'a ' ' b' ' c '"
     96        },
     97        {
     98            { "a\n\\b", "de'fg", "h", "'", NULL },
     99            "'a\n\\b' 'de'\"'\"'fg' h ''\"'\"''"
     100        }
     101    };
     102
     103    for (size_t i = 0; i < RT_ELEMENTS(s_aBournShTests); i++)
     104    {
     105        char *pszCmdLine = NULL;
     106        int rc = RTGetOptArgvToString(&pszCmdLine, s_aBournShTests[i].apszArgs, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH);
     107        RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
     108        if (strcmp(s_aBournShTests[i].pszCmdLine, pszCmdLine))
     109            RTTestIFailed("g_aTest[%i] failed:\n"
     110                          " got      |%s|\n"
     111                          " expected |%s|\n",
     112                          i, pszCmdLine, s_aBournShTests[i].pszCmdLine);
     113        RTStrFree(pszCmdLine);
     114    }
     115
     116
     117    RTTestISub("RTGetOptArgvToString <-> RTGetOptArgvFromString");
     118
     119    for (size_t i = 0; i < RT_ELEMENTS(s_aBournShTests); i++)
     120    {
     121        char *pszCmdLine = NULL;
     122        int rc = RTGetOptArgvToString(&pszCmdLine, s_aBournShTests[i].apszArgs, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH);
     123        RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
     124
     125        char  **papszArgs;
     126        int     cArgs;
     127        rc = RTGetOptArgvFromString(&papszArgs, &cArgs, pszCmdLine, NULL);
     128        RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
     129
     130        size_t j = 0;
     131        while (papszArgs[j] && s_aBournShTests[i].apszArgs[j])
     132        {
     133            if (strcmp(papszArgs[j], s_aBournShTests[i].apszArgs[j]))
     134                RTTestIFailed("Test #%u, argument #%u mismatch:\n"
     135                              " FromString: |%s| (got)\n"
     136                              " ToString:   |%s| (expected)\n",
     137                              i, j, papszArgs[j], s_aBournShTests[i].apszArgs[j]);
     138
     139            /* next */
     140            j++;
     141        }
     142        RTTESTI_CHECK(papszArgs[j] == NULL);
     143        RTTESTI_CHECK(s_aBournShTests[i].apszArgs[j] == NULL);
     144
     145        RTGetOptArgvFree(papszArgs);
    81146        RTStrFree(pszCmdLine);
    82147    }
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette