VirtualBox

Ignore:
Timestamp:
Nov 4, 2010 1:50:21 PM (14 years ago)
Author:
vboxsync
Message:

VBoxManage: Fixed incorrect return from errorGetOpt (VINF_SUCCESS instead of non-zero). Some rc cleanup in main().

Location:
trunk/src/VBox/Frontends/VBoxManage
Files:
4 edited

Legend:

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

    r33556 r33764  
    209209    RTR3Init();
    210210
     211    /*
     212     * Parse the global options
     213     */
    211214    bool fShowLogo = false;
    212215    bool fShowHelp = false;
     
    214217    int  iCmdArg;
    215218
    216     /* global options */
    217219    for (int i = 1; i < argc || argc <= iCmd; i++)
    218220    {
     
    275277
    276278
    277 #ifdef VBOX_ONLY_DOCS
    278     int rc = 0;
    279 #else /* !VBOX_ONLY_DOCS */
     279#ifndef VBOX_ONLY_DOCS
     280    /*
     281     * Initialize COM.
     282     */
    280283    using namespace com;
    281     HRESULT rc = 0;
    282 
    283     rc = com::Initialize();
    284     if (FAILED(rc))
    285     {
    286         RTMsgError("Failed to initialize COM!");
    287         return rc;
    288     }
     284    HRESULT hrc = com::Initialize();
     285    if (FAILED(hrc))
     286        return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to initialize COM!");
    289287
    290288    /*
    291      * The input is in the host OS'es codepage (NT guarantees ACP).
    292      * For VBox we use UTF-8 and convert to UCS-2 when calling (XP)COM APIs.
    293      * For simplicity, just convert the argv[] array here.
     289     * The input is ASSUMED to be in the current process codeset (NT guarantees
     290     * ACP, unixy systems doesn't guarantee anything).  This loop converts all
     291     * the argv[*] strings to UTF-8, which is a tad ugly but who cares.
     292     * (As a rule all strings in VirtualBox are UTF-8.)
    294293     */
    295294    for (int i = iCmdArg; i < argc; i++)
    296295    {
    297         char *converted;
    298         RTStrCurrentCPToUtf8(&converted, argv[i]);
    299         if (converted)
    300             argv[i] = converted;
     296        char *pszConverted;
     297        int rc = RTStrCurrentCPToUtf8(&pszConverted, argv[i]);
     298        if (RT_SUCCESS(rc))
     299            argv[i] = pszConverted;
    301300        else
    302301            /* Conversion was not possible,probably due to invalid characters.
     
    305304    }
    306305
     306    RTEXITCODE rcExit = RTEXITCODE_FAILURE;
    307307    do
    308308    {
     309    ///////////////////////////////////////////////////////////////////////////
    309310    // scopes all the stuff till shutdown
    310     ////////////////////////////////////////////////////////////////////////////
    311 
    312     /* convertfromraw: does not need a VirtualBox instantiation. */
    313     if (argc >= iCmdArg && (   !strcmp(argv[iCmd], "convertfromraw")
    314                             || !strcmp(argv[iCmd], "convertdd")))
    315     {
    316         rc = handleConvertFromRaw(argc - iCmdArg, argv + iCmdArg);
    317         break;
    318     }
    319 
    320     ComPtr<IVirtualBox> virtualBox;
    321     ComPtr<ISession> session;
    322 
    323     rc = virtualBox.createLocalObject(CLSID_VirtualBox);
    324     if (FAILED(rc))
    325         RTMsgError("Failed to create the VirtualBox object!");
    326     else
    327     {
    328         rc = session.createInprocObject(CLSID_Session);
    329         if (FAILED(rc))
    330             RTMsgError("Failed to create a session object!");
    331     }
    332 
    333     if (FAILED(rc))
    334     {
    335         com::ErrorInfo info;
    336         if (!info.isFullAvailable() && !info.isBasicAvailable())
    337         {
    338             com::GluePrintRCMessage(rc);
    339             RTMsgError("Most likely, the VirtualBox COM server is not running or failed to start.");
    340         }
     311        /*
     312         * convertfromraw: does not need a VirtualBox instantiation.
     313         */
     314        if (argc >= iCmdArg && (   !strcmp(argv[iCmd], "convertfromraw")
     315                                || !strcmp(argv[iCmd], "convertdd")))
     316        {
     317            rcExit = handleConvertFromRaw(argc - iCmdArg, argv + iCmdArg);
     318            break;
     319        }
     320
     321        /*
     322         * Get the remote VirtualBox object and create a local session object.
     323         */
     324        ComPtr<IVirtualBox> virtualBox;
     325        ComPtr<ISession> session;
     326
     327        hrc = virtualBox.createLocalObject(CLSID_VirtualBox);
     328        if (FAILED(hrc))
     329            RTMsgError("Failed to create the VirtualBox object!");
    341330        else
    342             com::GluePrintErrorInfo(info);
    343         break;
    344     }
    345 
    346     HandlerArg handlerArg = { 0, NULL, virtualBox, session };
    347 
    348     /*
    349      * All registered command handlers
    350      */
    351     static const struct
    352     {
    353         const char *command;
    354         USAGECATEGORY help;
    355         int (*handler)(HandlerArg *a);
    356     } s_commandHandlers[] =
    357     {
    358         { "internalcommands", 0,                       handleInternalCommands },
    359         { "list",             USAGE_LIST,              handleList },
    360         { "showvminfo",       USAGE_SHOWVMINFO,        handleShowVMInfo },
    361         { "registervm",       USAGE_REGISTERVM,        handleRegisterVM },
    362         { "unregistervm",     USAGE_UNREGISTERVM,      handleUnregisterVM },
    363         { "createhd",         USAGE_CREATEHD,          handleCreateHardDisk },
    364         { "createvdi",        USAGE_CREATEHD,          handleCreateHardDisk }, /* backward compatibility */
    365         { "modifyhd",         USAGE_MODIFYHD,          handleModifyHardDisk },
    366         { "modifyvdi",        USAGE_MODIFYHD,          handleModifyHardDisk }, /* backward compatibility */
    367         { "clonehd",          USAGE_CLONEHD,           handleCloneHardDisk },
    368         { "clonevdi",         USAGE_CLONEHD,           handleCloneHardDisk }, /* backward compatibility */
    369         { "addiscsidisk",     USAGE_ADDISCSIDISK,      handleAddiSCSIDisk },
    370         { "createvm",         USAGE_CREATEVM,          handleCreateVM },
    371         { "modifyvm",         USAGE_MODIFYVM,          handleModifyVM },
    372         { "startvm",          USAGE_STARTVM,           handleStartVM },
    373         { "controlvm",        USAGE_CONTROLVM,         handleControlVM },
    374         { "discardstate",     USAGE_DISCARDSTATE,      handleDiscardState },
    375         { "adoptstate",       USAGE_ADOPTSTATE,        handleAdoptState },
    376         { "snapshot",         USAGE_SNAPSHOT,          handleSnapshot },
    377         { "closemedium",      USAGE_CLOSEMEDIUM,       handleCloseMedium },
    378         { "storageattach",    USAGE_STORAGEATTACH,     handleStorageAttach },
    379         { "storagectl",       USAGE_STORAGECONTROLLER, handleStorageController },
    380         { "showhdinfo",       USAGE_SHOWHDINFO,        handleShowHardDiskInfo },
    381         { "showvdiinfo",      USAGE_SHOWHDINFO,        handleShowHardDiskInfo }, /* backward compatibility */
    382         { "getextradata",     USAGE_GETEXTRADATA,      handleGetExtraData },
    383         { "setextradata",     USAGE_SETEXTRADATA,      handleSetExtraData },
    384         { "setproperty",      USAGE_SETPROPERTY,       handleSetProperty },
    385         { "usbfilter",        USAGE_USBFILTER,         handleUSBFilter },
    386         { "sharedfolder",     USAGE_SHAREDFOLDER,      handleSharedFolder },
    387         { "vmstatistics",     USAGE_VM_STATISTICS,     handleVMStatistics },
     331        {
     332            hrc = session.createInprocObject(CLSID_Session);
     333            if (FAILED(hrc))
     334                RTMsgError("Failed to create a session object!");
     335        }
     336        if (FAILED(hrc))
     337        {
     338            com::ErrorInfo info;
     339            if (!info.isFullAvailable() && !info.isBasicAvailable())
     340            {
     341                com::GluePrintRCMessage(hrc);
     342                RTMsgError("Most likely, the VirtualBox COM server is not running or failed to start.");
     343            }
     344            else
     345                com::GluePrintErrorInfo(info);
     346            break;
     347        }
     348
     349        /*
     350         * All registered command handlers
     351         */
     352        static const struct
     353        {
     354            const char *command;
     355            USAGECATEGORY help;
     356            int (*handler)(HandlerArg *a);
     357        } s_commandHandlers[] =
     358        {
     359            { "internalcommands", 0,                       handleInternalCommands },
     360            { "list",             USAGE_LIST,              handleList },
     361            { "showvminfo",       USAGE_SHOWVMINFO,        handleShowVMInfo },
     362            { "registervm",       USAGE_REGISTERVM,        handleRegisterVM },
     363            { "unregistervm",     USAGE_UNREGISTERVM,      handleUnregisterVM },
     364            { "createhd",         USAGE_CREATEHD,          handleCreateHardDisk },
     365            { "createvdi",        USAGE_CREATEHD,          handleCreateHardDisk }, /* backward compatibility */
     366            { "modifyhd",         USAGE_MODIFYHD,          handleModifyHardDisk },
     367            { "modifyvdi",        USAGE_MODIFYHD,          handleModifyHardDisk }, /* backward compatibility */
     368            { "clonehd",          USAGE_CLONEHD,           handleCloneHardDisk },
     369            { "clonevdi",         USAGE_CLONEHD,           handleCloneHardDisk }, /* backward compatibility */
     370            { "addiscsidisk",     USAGE_ADDISCSIDISK,      handleAddiSCSIDisk },
     371            { "createvm",         USAGE_CREATEVM,          handleCreateVM },
     372            { "modifyvm",         USAGE_MODIFYVM,          handleModifyVM },
     373            { "startvm",          USAGE_STARTVM,           handleStartVM },
     374            { "controlvm",        USAGE_CONTROLVM,         handleControlVM },
     375            { "discardstate",     USAGE_DISCARDSTATE,      handleDiscardState },
     376            { "adoptstate",       USAGE_ADOPTSTATE,        handleAdoptState },
     377            { "snapshot",         USAGE_SNAPSHOT,          handleSnapshot },
     378            { "closemedium",      USAGE_CLOSEMEDIUM,       handleCloseMedium },
     379            { "storageattach",    USAGE_STORAGEATTACH,     handleStorageAttach },
     380            { "storagectl",       USAGE_STORAGECONTROLLER, handleStorageController },
     381            { "showhdinfo",       USAGE_SHOWHDINFO,        handleShowHardDiskInfo },
     382            { "showvdiinfo",      USAGE_SHOWHDINFO,        handleShowHardDiskInfo }, /* backward compatibility */
     383            { "getextradata",     USAGE_GETEXTRADATA,      handleGetExtraData },
     384            { "setextradata",     USAGE_SETEXTRADATA,      handleSetExtraData },
     385            { "setproperty",      USAGE_SETPROPERTY,       handleSetProperty },
     386            { "usbfilter",        USAGE_USBFILTER,         handleUSBFilter },
     387            { "sharedfolder",     USAGE_SHAREDFOLDER,      handleSharedFolder },
     388            { "vmstatistics",     USAGE_VM_STATISTICS,     handleVMStatistics },
    388389#ifdef VBOX_WITH_GUEST_PROPS
    389         { "guestproperty",    USAGE_GUESTPROPERTY,     handleGuestProperty },
     390            { "guestproperty",    USAGE_GUESTPROPERTY,     handleGuestProperty },
    390391#endif
    391392#ifdef VBOX_WITH_GUEST_CONTROL
    392         { "guestcontrol",     USAGE_GUESTCONTROL,      handleGuestControl },
     393            { "guestcontrol",     USAGE_GUESTCONTROL,      handleGuestControl },
    393394#endif
    394         { "metrics",          USAGE_METRICS,           handleMetrics },
    395         { "import",           USAGE_IMPORTAPPLIANCE,   handleImportAppliance },
    396         { "export",           USAGE_EXPORTAPPLIANCE,   handleExportAppliance },
     395            { "metrics",          USAGE_METRICS,           handleMetrics },
     396            { "import",           USAGE_IMPORTAPPLIANCE,   handleImportAppliance },
     397            { "export",           USAGE_EXPORTAPPLIANCE,   handleExportAppliance },
    397398#ifdef VBOX_WITH_NETFLT
    398         { "hostonlyif",       USAGE_HOSTONLYIFS,       handleHostonlyIf },
     399            { "hostonlyif",       USAGE_HOSTONLYIFS,       handleHostonlyIf },
    399400#endif
    400         { "dhcpserver",       USAGE_DHCPSERVER,        handleDHCPServer},
    401         { "vrde",             USAGE_VRDE,              handleVRDE},
    402         { NULL,               0,                       NULL }
    403     };
    404 
    405     int commandIndex;
    406     for (commandIndex = 0; s_commandHandlers[commandIndex].command != NULL; commandIndex++)
    407     {
    408         if (!strcmp(s_commandHandlers[commandIndex].command, argv[iCmd]))
    409         {
    410             handlerArg.argc = argc - iCmdArg;
    411             handlerArg.argv = &argv[iCmdArg];
    412 
    413             if (   fShowHelp
    414                 || (   argc - iCmdArg == 0
    415                     && s_commandHandlers[commandIndex].help))
    416             {
    417                 printUsage(s_commandHandlers[commandIndex].help, g_pStdOut);
    418                 rc = 1; /* error */
    419             }
    420             else
    421                 rc = s_commandHandlers[commandIndex].handler(&handlerArg);
    422             break;
    423         }
    424     }
    425     if (!s_commandHandlers[commandIndex].command)
    426         rc = errorSyntax(USAGE_ALL, "Invalid command '%s'", Utf8Str(argv[iCmd]).c_str());
    427 
    428     /* Although all handlers should always close the session if they open it,
    429      * we do it here just in case if some of the handlers contains a bug --
    430      * leaving the direct session not closed will turn the machine state to
    431      * Aborted which may have unwanted side effects like killing the saved
    432      * state file (if the machine was in the Saved state before). */
    433     session->UnlockMachine();
    434 
    435     EventQueue::getMainEventQueue()->processEventQueue(0);
     401            { "dhcpserver",       USAGE_DHCPSERVER,        handleDHCPServer},
     402            { "vrde",             USAGE_VRDE,              handleVRDE},
     403            { NULL,               0,                       NULL }
     404        };
     405
     406        HandlerArg  handlerArg = { 0, NULL, virtualBox, session };
     407        int         commandIndex;
     408        for (commandIndex = 0; s_commandHandlers[commandIndex].command != NULL; commandIndex++)
     409        {
     410            if (!strcmp(s_commandHandlers[commandIndex].command, argv[iCmd]))
     411            {
     412                handlerArg.argc = argc - iCmdArg;
     413                handlerArg.argv = &argv[iCmdArg];
     414
     415                if (   fShowHelp
     416                    || (   argc - iCmdArg == 0
     417                        && s_commandHandlers[commandIndex].help))
     418                {
     419                    printUsage(s_commandHandlers[commandIndex].help, g_pStdOut);
     420                    rcExit = RTEXITCODE_FAILURE; /* error */
     421                }
     422                else
     423                    rcExit = (RTEXITCODE)s_commandHandlers[commandIndex].handler(&handlerArg); /** @todo Change to return RTEXITCODE. */
     424                break;
     425            }
     426        }
     427        if (!s_commandHandlers[commandIndex].command)
     428            rcExit = errorSyntax(USAGE_ALL, "Invalid command '%s'", Utf8Str(argv[iCmd]).c_str());
     429
     430        /* Although all handlers should always close the session if they open it,
     431         * we do it here just in case if some of the handlers contains a bug --
     432         * leaving the direct session not closed will turn the machine state to
     433         * Aborted which may have unwanted side effects like killing the saved
     434         * state file (if the machine was in the Saved state before). */
     435        session->UnlockMachine();
     436
     437        EventQueue::getMainEventQueue()->processEventQueue(0);
     438
    436439    // end "all-stuff" scope
    437     ////////////////////////////////////////////////////////////////////////////
     440    ///////////////////////////////////////////////////////////////////////////
    438441    } while (0);
    439442
    440443    com::Shutdown();
    441 #endif /* !VBOX_ONLY_DOCS */
    442444
    443445    /*
     
    445447     */
    446448    for (int i = iCmdArg; i < argc; i++)
     449    {
    447450        RTStrFree(argv[i]);
    448 
    449     return rc != 0;
     451        argv[i] = NULL;
     452    }
     453
     454    return rcExit;
     455#else  /* VBOX_ONLY_DOCS */
     456    return RTEXITCODE_SUCCESS;
     457#endif /* VBOX_ONLY_DOCS */
    450458}
  • trunk/src/VBox/Frontends/VBoxManage/VBoxManage.h

    r33556 r33764  
    144144/* VBoxManageHelp.cpp */
    145145void printUsage(USAGECATEGORY u64Cmd, PRTSTREAM pStrm);
    146 int errorSyntax(USAGECATEGORY u64Cmd, const char *pszFormat, ...);
    147 int errorGetOpt(USAGECATEGORY u64Cmd, int rc, union RTGETOPTUNION const *pValueUnion);
    148 int errorArgument(const char *pszFormat, ...);
     146RTEXITCODE errorSyntax(USAGECATEGORY u64Cmd, const char *pszFormat, ...);
     147RTEXITCODE errorGetOpt(USAGECATEGORY u64Cmd, int rc, union RTGETOPTUNION const *pValueUnion);
     148RTEXITCODE errorArgument(const char *pszFormat, ...);
    149149
    150150void printUsageInternal(USAGECATEGORY u64Cmd, PRTSTREAM pStrm);
     
    211211int handleVMStatistics(HandlerArg *a);
    212212int handleVRDE(HandlerArg *a);
     213int handleExtPack(HandlerArg *a);
    213214
    214215/* VBoxManageDisk.cpp */
     
    216217int handleModifyHardDisk(HandlerArg *a);
    217218int handleCloneHardDisk(HandlerArg *a);
    218 int handleConvertFromRaw(int argc, char *argv[]);
     219RTEXITCODE handleConvertFromRaw(int argc, char *argv[]);
    219220int handleAddiSCSIDisk(HandlerArg *a);
    220221int handleShowHardDiskInfo(HandlerArg *a);
  • trunk/src/VBox/Frontends/VBoxManage/VBoxManageDisk.cpp

    r33567 r33764  
    785785};
    786786
    787 int handleConvertFromRaw(int argc, char *argv[])
     787RTEXITCODE handleConvertFromRaw(int argc, char *argv[])
    788788{
    789789    int rc = VINF_SUCCESS;
     
    952952        RTFileClose(File);
    953953
    954     return RT_FAILURE(rc);
     954    return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
    955955}
    956956
  • trunk/src/VBox/Frontends/VBoxManage/VBoxManageHelp.cpp

    r33590 r33764  
    731731/**
    732732 * Print a usage synopsis and the syntax error message.
     733 * @returns RTEXITCODE_SYNTAX.
    733734 */
    734 int errorSyntax(USAGECATEGORY u64Cmd, const char *pszFormat, ...)
     735RTEXITCODE errorSyntax(USAGECATEGORY u64Cmd, const char *pszFormat, ...)
    735736{
    736737    va_list args;
     
    745746    RTStrmPrintf(g_pStdErr, "\nSyntax error: %N\n", pszFormat, &args);
    746747    va_end(args);
    747     return 1;
     748    return RTEXITCODE_SYNTAX;
    748749}
    749750
     
    751752 * errorSyntax for RTGetOpt users.
    752753 *
    753  * @returns 1.
     754 * @returns RTEXITCODE_SYNTAX.
    754755 *
    755756 * @param   fUsageCategory  The usage category of the command.
     
    757758 * @param   pValueUnion     The value union.
    758759 */
    759 int errorGetOpt(USAGECATEGORY fUsageCategory, int rc, union RTGETOPTUNION const *pValueUnion)
     760RTEXITCODE errorGetOpt(USAGECATEGORY fUsageCategory, int rc, union RTGETOPTUNION const *pValueUnion)
    760761{
    761762    showLogo(g_pStdErr); // show logo even if suppressed
     
    768769
    769770    if (rc == VINF_GETOPT_NOT_OPTION)
    770         return RTMsgError("Invalid parameter '%s'", pValueUnion->psz);
     771        return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Invalid parameter '%s'", pValueUnion->psz);
    771772    if (rc > 0)
    772773    {
    773774        if (RT_C_IS_PRINT(rc))
    774             return RTMsgError("Invalid option -%c", rc);
    775         return RTMsgError("Invalid option case %i", rc);
     775            return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Invalid option -%c", rc);
     776        return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Invalid option case %i", rc);
    776777    }
    777778    if (rc == VERR_GETOPT_UNKNOWN_OPTION)
    778         return RTMsgError("Unknown option: %s", pValueUnion->psz);
     779        return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Unknown option: %s", pValueUnion->psz);
    779780    if (pValueUnion->pDef)
    780         return RTMsgError("%s: %Rrs", pValueUnion->pDef->pszLong, rc);
    781     return RTMsgError("%Rrs", rc);
     781        return RTMsgErrorExit(RTEXITCODE_SYNTAX, "%s: %Rrs", pValueUnion->pDef->pszLong, rc);
     782    return RTMsgErrorExit(RTEXITCODE_SYNTAX, "%Rrs", rc);
    782783}
    783784
    784785/**
    785786 * Print an error message without the syntax stuff.
     787 *
     788 * @returns RTEXITCODE_SYNTAX.
    786789 */
    787 int errorArgument(const char *pszFormat, ...)
     790RTEXITCODE errorArgument(const char *pszFormat, ...)
    788791{
    789792    va_list args;
     
    791794    RTMsgErrorV(pszFormat, args);
    792795    va_end(args);
    793     return 1;
     796    return RTEXITCODE_SYNTAX;
    794797}
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