VirtualBox

Changeset 68184 in vbox for trunk/src


Ignore:
Timestamp:
Jul 31, 2017 8:28:23 AM (7 years ago)
Author:
vboxsync
Message:

VBoxManage: Added simple response file loading code. The response file must come before any command and be prefixed by '@'.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VBoxManage/VBoxManage.cpp

    r68140 r68184  
    3838#include <iprt/buildconfig.h>
    3939#include <iprt/ctype.h>
     40#include <iprt/file.h>
     41#include <iprt/getopt.h>
    4042#include <iprt/initterm.h>
    4143#include <iprt/path.h>
     
    460462     * the support driver.
    461463     */
    462     RTR3InitExe(argc, &argv, 0);
     464    int vrc = RTR3InitExe(argc, &argv, 0);
     465    if (RT_FAILURE(vrc))
     466        return RTMsgInitFailure(vrc);
    463467#if defined(RT_OS_WINDOWS) && !defined(VBOX_ONLY_DOCS)
    464468    ATL::CComModule _Module; /* Required internally by ATL (constructor records instance in global variable). */
     
    474478    const char *pszSettingsPw = NULL;
    475479    const char *pszSettingsPwFile = NULL;
     480#ifndef VBOX_ONLY_DOCS
     481    int         cResponseFileArgs     = 0;
     482    char      **papszResponseFileArgs = NULL;
     483    char      **papszNewArgv          = NULL;
     484#endif
    476485
    477486    for (int i = 1; i < argc || argc <= iCmd; i++)
     
    548557            iCmd += 2;
    549558        }
     559#ifndef VBOX_ONLY_DOCS
     560        else if (argv[i][0] == '@')
     561        {
     562            if (papszResponseFileArgs)
     563                return RTMsgErrorExitFailure("Only one response file allowed");
     564
     565            /* Load response file, making sure it's valid UTF-8. */
     566            char  *pszResponseFile;
     567            size_t cbResponseFile;
     568            vrc = RTFileReadAllEx(&argv[i][1], 0, RTFOFF_MAX, RTFILE_RDALL_O_DENY_NONE | RTFILE_RDALL_F_TRAILING_ZERO_BYTE,
     569                                  (void **)&pszResponseFile, &cbResponseFile);
     570            if (RT_FAILURE(vrc))
     571                return RTMsgErrorExitFailure("Error reading response file '%s': %Rrc", &argv[i][1], vrc);
     572            vrc = RTStrValidateEncoding(pszResponseFile);
     573            if (RT_FAILURE(vrc))
     574            {
     575                RTFileReadAllFree(pszResponseFile, cbResponseFile);
     576                return RTMsgErrorExitFailure("Invalid response file ('%s') encoding: %Rrc", &argv[i][1], vrc);
     577            }
     578
     579            /* Parse it. */
     580            vrc = RTGetOptArgvFromString(&papszResponseFileArgs, &cResponseFileArgs, pszResponseFile,
     581                                         RTGETOPTARGV_CNV_QUOTE_BOURNE_SH, NULL);
     582            RTFileReadAllFree(pszResponseFile, cbResponseFile);
     583            if (RT_FAILURE(vrc))
     584                return RTMsgErrorExitFailure("Failed to parse response file '%s' (bourne shell style): %Rrc", &argv[i][1], vrc);
     585
     586            /* Construct new argv+argc with the response file arguments inserted. */
     587            int cNewArgs = argc + cResponseFileArgs;
     588            papszNewArgv = (char **)RTMemAllocZ((cNewArgs + 2) * sizeof(papszNewArgv[0]));
     589            if (!papszNewArgv)
     590                return RTMsgErrorExitFailure("out of memory");
     591            memcpy(&papszNewArgv[0], &argv[0], sizeof(argv[0]) * (i + 1));
     592            memcpy(&papszNewArgv[i + 1], papszResponseFileArgs, sizeof(argv[0]) * cResponseFileArgs);
     593            memcpy(&papszNewArgv[i + 1 + cResponseFileArgs], &argv[i + 1], sizeof(argv[0]) * (argc - i - 1 + 1));
     594            argv = papszNewArgv;
     595            argc = argc + cResponseFileArgs;
     596
     597            iCmd++;
     598        }
     599#endif
    550600        else
    551601            break;
     
    697747    }
    698748
     749    if (papszResponseFileArgs)
     750    {
     751        RTGetOptArgvFree(papszResponseFileArgs);
     752        RTMemFree(papszNewArgv);
     753    }
     754
    699755    return rcExit;
    700756#else  /* VBOX_ONLY_DOCS */
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