VirtualBox

Changeset 94240 in vbox for trunk/src/VBox/Frontends


Ignore:
Timestamp:
Mar 15, 2022 10:10:24 AM (3 years ago)
Author:
vboxsync
Message:

FE/VBoxManage: Move stuff to VBoxInternalManage.cpp because it is only used there now, ​bugref:9186

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

Legend:

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

    r93115 r94240  
    3636#include <VBox/sup.h>
    3737#include <VBox/log.h>
    38 
     38#include <VBox/version.h>
     39
     40#include <iprt/buildconfig.h>
    3941#include <iprt/ctype.h>
    4042#include <iprt/file.h>
     
    131133} HOSTPARTITIONS, *PHOSTPARTITIONS;
    132134
    133 /** flag whether we're in internal mode */
    134 bool g_fInternalMode;
    135135
    136136/**
    137137 * Print the usage info.
    138138 */
    139 void printUsageInternal(USAGECATEGORY enmCommand, PRTSTREAM pStrm)
     139static void printUsageInternal(USAGECATEGORY enmCommand, PRTSTREAM pStrm)
    140140{
    141141    Assert(enmCommand != USAGE_INVALID);
     
    288288}
    289289
     290
     291/**
     292 * Print a usage synopsis and the syntax error message.
     293 * @returns RTEXITCODE_SYNTAX.
     294 */
     295static RTEXITCODE errorSyntaxInternal(USAGECATEGORY enmCommand, const char *pszFormat, ...)
     296{
     297    va_list args;
     298    showLogo(g_pStdErr); // show logo even if suppressed
     299
     300    printUsageInternal(enmCommand, g_pStdErr);
     301
     302    va_start(args, pszFormat);
     303    RTStrmPrintf(g_pStdErr, Internal::tr("\nSyntax error: %N\n"), pszFormat, &args);
     304    va_end(args);
     305    return RTEXITCODE_SYNTAX;
     306}
     307
     308
     309/**
     310 * errorSyntaxInternal for RTGetOpt users.
     311 *
     312 * @returns RTEXITCODE_SYNTAX.
     313 *
     314 * @param   enmCommand      The command.
     315 * @param   rc              The RTGetOpt return code.
     316 * @param   pValueUnion     The value union.
     317 */
     318static RTEXITCODE errorGetOptInternal(USAGECATEGORY enmCommand, int rc, union RTGETOPTUNION const *pValueUnion)
     319{
     320    /*
     321     * Check if it is an unhandled standard option.
     322     */
     323    if (rc == 'V')
     324    {
     325        RTPrintf("%sr%d\n", VBOX_VERSION_STRING, RTBldCfgRevision());
     326        return RTEXITCODE_SUCCESS;
     327    }
     328
     329    if (rc == 'h')
     330    {
     331        showLogo(g_pStdErr);
     332        printUsageInternal(enmCommand, g_pStdOut);
     333        return RTEXITCODE_SUCCESS;
     334    }
     335
     336    /*
     337     * General failure.
     338     */
     339    showLogo(g_pStdErr); // show logo even if suppressed
     340
     341    printUsageInternal(enmCommand, g_pStdErr);
     342
     343    if (rc == VINF_GETOPT_NOT_OPTION)
     344        return RTMsgErrorExit(RTEXITCODE_SYNTAX, Internal::tr("Invalid parameter '%s'"), pValueUnion->psz);
     345    if (rc > 0)
     346    {
     347        if (RT_C_IS_PRINT(rc))
     348            return RTMsgErrorExit(RTEXITCODE_SYNTAX, Internal::tr("Invalid option -%c"), rc);
     349        return RTMsgErrorExit(RTEXITCODE_SYNTAX, Internal::tr("Invalid option case %i"), rc);
     350    }
     351    if (rc == VERR_GETOPT_UNKNOWN_OPTION)
     352        return RTMsgErrorExit(RTEXITCODE_SYNTAX, Internal::tr("Unknown option: %s"), pValueUnion->psz);
     353    if (rc == VERR_GETOPT_INVALID_ARGUMENT_FORMAT)
     354        return RTMsgErrorExit(RTEXITCODE_SYNTAX, Internal::tr("Invalid argument format: %s"), pValueUnion->psz);
     355    if (pValueUnion->pDef)
     356        return RTMsgErrorExit(RTEXITCODE_SYNTAX, "%s: %Rrs", pValueUnion->pDef->pszLong, rc);
     357    return RTMsgErrorExit(RTEXITCODE_SYNTAX, "%Rrs", rc);
     358}
     359
     360
    290361/** @todo this is no longer necessary, we can enumerate extra data */
    291362/**
     
    650721        uuidType = HDUUID;
    651722        if (argc != 3 && argc != 2)
    652             return errorSyntax(USAGE_I_SETHDUUID, Internal::tr("Not enough parameters"));
     723            return errorSyntaxInternal(USAGE_I_SETHDUUID, Internal::tr("Not enough parameters"));
    653724        /* if specified, take UUID, otherwise generate a new one */
    654725        if (argc == 3)
    655726        {
    656727            if (RT_FAILURE(RTUuidFromStr(&rtuuid, argv[2])))
    657                 return errorSyntax(USAGE_I_SETHDUUID, Internal::tr("Invalid UUID parameter"));
     728                return errorSyntaxInternal(USAGE_I_SETHDUUID, Internal::tr("Invalid UUID parameter"));
    658729            uuid = argv[2];
    659730        } else
     
    664735        uuidType = HDPARENTUUID;
    665736        if (argc != 3)
    666             return errorSyntax(USAGE_I_SETHDPARENTUUID, Internal::tr("Not enough parameters"));
     737            return errorSyntaxInternal(USAGE_I_SETHDPARENTUUID, Internal::tr("Not enough parameters"));
    667738        if (RT_FAILURE(RTUuidFromStr(&rtuuid, argv[2])))
    668             return errorSyntax(USAGE_I_SETHDPARENTUUID, Internal::tr("Invalid UUID parameter"));
     739            return errorSyntaxInternal(USAGE_I_SETHDPARENTUUID, Internal::tr("Invalid UUID parameter"));
    669740        uuid = argv[2];
    670741    }
    671742    else
    672         return errorSyntax(USAGE_I_SETHDUUID, Internal::tr("Invalid invocation"));
     743        return errorSyntaxInternal(USAGE_I_SETHDUUID, Internal::tr("Invalid invocation"));
    673744
    674745    /* just try it */
     
    722793    if (argc != 1)
    723794    {
    724         return errorSyntax(USAGE_I_DUMPHDINFO, Internal::tr("Not enough parameters"));
     795        return errorSyntaxInternal(USAGE_I_DUMPHDINFO, Internal::tr("Not enough parameters"));
    725796    }
    726797
     
    11161187        else
    11171188        {
    1118             return errorSyntax(USAGE_I_LISTPARTITIONS, Internal::tr("Invalid parameter '%s'"), argv[i]);
     1189            return errorSyntaxInternal(USAGE_I_LISTPARTITIONS, Internal::tr("Invalid parameter '%s'"), argv[i]);
    11191190        }
    11201191    }
    11211192
    11221193    if (rawdisk.isEmpty())
    1123         return errorSyntax(USAGE_I_LISTPARTITIONS, Internal::tr("Mandatory parameter -rawdisk missing"));
     1194        return errorSyntaxInternal(USAGE_I_LISTPARTITIONS, Internal::tr("Mandatory parameter -rawdisk missing"));
    11241195
    11251196    RTFILE hRawFile;
     
    12331304#endif /* RT_OS_LINUX || RT_OS_FREEBSD */
    12341305        else
    1235             return errorSyntax(USAGE_I_CREATERAWVMDK, Internal::tr("Invalid parameter '%s'"), argv[i]);
     1306            return errorSyntaxInternal(USAGE_I_CREATERAWVMDK, Internal::tr("Invalid parameter '%s'"), argv[i]);
    12361307    }
    12371308
    12381309    if (filename.isEmpty())
    1239         return errorSyntax(USAGE_I_CREATERAWVMDK, Internal::tr("Mandatory parameter -filename missing"));
     1310        return errorSyntaxInternal(USAGE_I_CREATERAWVMDK, Internal::tr("Mandatory parameter -filename missing"));
    12401311    if (rawdisk.isEmpty())
    1241         return errorSyntax(USAGE_I_CREATERAWVMDK, Internal::tr("Mandatory parameter -rawdisk missing"));
     1312        return errorSyntaxInternal(USAGE_I_CREATERAWVMDK, Internal::tr("Mandatory parameter -rawdisk missing"));
    12421313    if (!pszPartitions && pszMBRFilename)
    1243         return errorSyntax(USAGE_I_CREATERAWVMDK,
     1314        return errorSyntaxInternal(USAGE_I_CREATERAWVMDK,
    12441315                           Internal::tr("The parameter -mbr is only valid when the parameter -partitions is also present"));
    12451316
     
    19241995        else
    19251996        {
    1926             return errorSyntax(USAGE_I_RENAMEVMDK, Internal::tr("Invalid parameter '%s'"), argv[i]);
     1997            return errorSyntaxInternal(USAGE_I_RENAMEVMDK, Internal::tr("Invalid parameter '%s'"), argv[i]);
    19271998        }
    19281999    }
    19292000
    19302001    if (src.isEmpty())
    1931         return errorSyntax(USAGE_I_RENAMEVMDK, Internal::tr("Mandatory parameter -from missing"));
     2002        return errorSyntaxInternal(USAGE_I_RENAMEVMDK, Internal::tr("Mandatory parameter -from missing"));
    19322003    if (dst.isEmpty())
    1933         return errorSyntax(USAGE_I_RENAMEVMDK, Internal::tr("Mandatory parameter -to missing"));
     2004        return errorSyntaxInternal(USAGE_I_RENAMEVMDK, Internal::tr("Mandatory parameter -to missing"));
    19342005
    19352006    PVDISK pDisk = NULL;
     
    19972068        else
    19982069        {
    1999             return errorSyntax(USAGE_I_CONVERTTORAW, Internal::tr("Invalid parameter '%s'"), argv[i]);
     2070            return errorSyntaxInternal(USAGE_I_CONVERTTORAW, Internal::tr("Invalid parameter '%s'"), argv[i]);
    20002071        }
    20012072    }
    20022073
    20032074    if (src.isEmpty())
    2004         return errorSyntax(USAGE_I_CONVERTTORAW, Internal::tr("Mandatory filename parameter missing"));
     2075        return errorSyntaxInternal(USAGE_I_CONVERTTORAW, Internal::tr("Mandatory filename parameter missing"));
    20052076    if (dst.isEmpty())
    2006         return errorSyntax(USAGE_I_CONVERTTORAW, Internal::tr("Mandatory outputfile parameter missing"));
     2077        return errorSyntaxInternal(USAGE_I_CONVERTTORAW, Internal::tr("Mandatory outputfile parameter missing"));
    20072078
    20082079    PVDISK pDisk = NULL;
     
    21652236        else
    21662237        {
    2167             return errorSyntax(USAGE_I_CONVERTHD, Internal::tr("Invalid parameter '%s'"), argv[i]);
     2238            return errorSyntaxInternal(USAGE_I_CONVERTHD, Internal::tr("Invalid parameter '%s'"), argv[i]);
    21682239        }
    21692240    }
    21702241
    21712242    if (src.isEmpty())
    2172         return errorSyntax(USAGE_I_CONVERTHD, Internal::tr("Mandatory input image parameter missing"));
     2243        return errorSyntaxInternal(USAGE_I_CONVERTHD, Internal::tr("Mandatory input image parameter missing"));
    21732244    if (dst.isEmpty())
    2174         return errorSyntax(USAGE_I_CONVERTHD, Internal::tr("Mandatory output image parameter missing"));
     2245        return errorSyntaxInternal(USAGE_I_CONVERTHD, Internal::tr("Mandatory output image parameter missing"));
    21752246
    21762247
     
    22862357        else
    22872358        {
    2288             return errorSyntax(USAGE_I_REPAIRHD, Internal::tr("Invalid parameter '%s'"), argv[i]);
     2359            return errorSyntaxInternal(USAGE_I_REPAIRHD, Internal::tr("Invalid parameter '%s'"), argv[i]);
    22892360        }
    22902361    }
    22912362
    22922363    if (image.isEmpty())
    2293         return errorSyntax(USAGE_I_REPAIRHD, Internal::tr("Mandatory input image parameter missing"));
     2364        return errorSyntaxInternal(USAGE_I_REPAIRHD, Internal::tr("Mandatory input image parameter missing"));
    22942365
    22952366    PVDINTERFACE     pVDIfs = NULL;
     
    23662437     */
    23672438    if (argc < 1)
    2368         return errorSyntax(USAGE_I_DEBUGLOG, Internal::tr("Missing VM name/UUID"));
     2439        return errorSyntaxInternal(USAGE_I_DEBUGLOG, Internal::tr("Missing VM name/UUID"));
    23692440
    23702441    ComPtr<IMachine> ptrMachine;
     
    24542525
    24552526            default:
    2456                 return errorGetOpt(USAGE_I_DEBUGLOG, ch, &ValueUnion);
     2527                return errorGetOptInternal(USAGE_I_DEBUGLOG, ch, &ValueUnion);
    24572528        }
    24582529    }
     
    24822553    /* one parameter, the password to hash */
    24832554    if (argc != 1)
    2484         return errorSyntax(USAGE_I_PASSWORDHASH, Internal::tr("password to hash required"));
     2555        return errorSyntaxInternal(USAGE_I_PASSWORDHASH, Internal::tr("password to hash required"));
    24852556
    24862557    uint8_t abDigest[RTSHA256_HASH_SIZE];
     
    25012572    /* one parameter, guest name */
    25022573    if (argc < 1)
    2503         return errorSyntax(USAGE_I_GUESTSTATS, Internal::tr("Missing VM name/UUID"));
     2574        return errorSyntaxInternal(USAGE_I_GUESTSTATS, Internal::tr("Missing VM name/UUID"));
    25042575
    25052576    /*
     
    25262597
    25272598            default:
    2528                 return errorGetOpt(USAGE_I_GUESTSTATS, ch, &ValueUnion);
     2599                return errorGetOptInternal(USAGE_I_GUESTSTATS, ch, &ValueUnion);
    25292600        }
    25302601    }
    25312602
    25322603    if (argc > 1 && aUpdateInterval == 0)
    2533         return errorSyntax(USAGE_I_GUESTSTATS, Internal::tr("Invalid update interval specified"));
     2604        return errorSyntaxInternal(USAGE_I_GUESTSTATS, Internal::tr("Invalid update interval specified"));
    25342605
    25352606    RTPrintf(Internal::tr("argc=%d interval=%u\n"), argc, aUpdateInterval);
     
    25822653RTEXITCODE handleInternalCommands(HandlerArg *a)
    25832654{
    2584     g_fInternalMode = true;
    2585 
    25862655    /* at least a command is required */
    25872656    if (a->argc < 1)
    2588         return errorSyntax(USAGE_S_ALL, Internal::tr("Command missing"));
     2657        return errorSyntaxInternal(USAGE_S_ALL, Internal::tr("Command missing"));
    25892658
    25902659    /*
     
    26262695
    26272696    /* default: */
    2628     return errorSyntax(USAGE_S_ALL, Internal::tr("Invalid command '%s'"), a->argv[0]);
    2629 }
     2697    return errorSyntaxInternal(USAGE_S_ALL, Internal::tr("Invalid command '%s'"), a->argv[0]);
     2698}
  • trunk/src/VBox/Frontends/VBoxManage/VBoxManage.cpp

    r94234 r94240  
    758758            return RTEXITCODE_SUCCESS;
    759759        }
    760         return errorSyntax(USAGE_S_ALL, VBoxManage::tr("Invalid command '%s'"), argv[iCmd]);
     760        return errorSyntax(VBoxManage::tr("Invalid command '%s'"), argv[iCmd]);
    761761    }
    762762
  • trunk/src/VBox/Frontends/VBoxManage/VBoxManage.h

    r94234 r94240  
    130130};
    131131
    132 /** flag whether we're in internal mode */
    133 extern bool g_fInternalMode;
    134132
    135133/** showVMInfo details */
     
    160158
    161159/* VBoxManageHelp.cpp */
    162 
    163160/* Legacy help infrastructure, to be replaced by new one using generated help. */
    164161void printUsage(USAGECATEGORY enmCommand, uint64_t fSubcommandScope, PRTSTREAM pStrm);
    165 RTEXITCODE errorSyntax(USAGECATEGORY enmCommand, const char *pszFormat, ...);
    166 RTEXITCODE errorSyntaxEx(USAGECATEGORY enmCommand, uint64_t fSubcommandScope, const char *pszFormat, ...);
    167 RTEXITCODE errorGetOpt(USAGECATEGORY enmCommand, int rc, union RTGETOPTUNION const *pValueUnion);
    168 RTEXITCODE errorGetOptEx(USAGECATEGORY enmCommand, uint64_t fSubcommandScope, int rc, union RTGETOPTUNION const *pValueUnion);
    169 
    170 void printUsageInternal(USAGECATEGORY enmCommand, PRTSTREAM pStrm);
    171162
    172163void        setCurrentCommand(enum HELP_CMD_VBOXMANAGE enmCommand);
  • trunk/src/VBox/Frontends/VBoxManage/VBoxManageHelp.cpp

    r94235 r94240  
    561561    }
    562562}
    563 
    564 /**
    565  * Print a usage synopsis and the syntax error message.
    566  * @returns RTEXITCODE_SYNTAX.
    567  */
    568 RTEXITCODE errorSyntax(USAGECATEGORY enmCommand, const char *pszFormat, ...)
    569 {
    570     va_list args;
    571     showLogo(g_pStdErr); // show logo even if suppressed
    572 
    573     if (g_fInternalMode)
    574         printUsageInternal(enmCommand, g_pStdErr);
    575     else if (g_enmCurCommand == HELP_CMD_VBOXMANAGE_INVALID)
    576         printUsage(enmCommand, RTMSGREFENTRYSTR_SCOPE_GLOBAL, g_pStdErr);
    577     else
    578         printUsage(g_pStdErr);
    579 
    580     va_start(args, pszFormat);
    581     RTStrmPrintf(g_pStdErr, Help::tr("\nSyntax error: %N\n"), pszFormat, &args);
    582     va_end(args);
    583     return RTEXITCODE_SYNTAX;
    584 }
    585 
    586 /**
    587  * Print a usage synopsis and the syntax error message.
    588  * @returns RTEXITCODE_SYNTAX.
    589  */
    590 RTEXITCODE errorSyntaxEx(USAGECATEGORY enmCommand, uint64_t fSubcommandScope, const char *pszFormat, ...)
    591 {
    592     va_list args;
    593     showLogo(g_pStdErr); // show logo even if suppressed
    594 
    595     if (g_fInternalMode)
    596         printUsageInternal(enmCommand, g_pStdErr);
    597     else if (g_enmCurCommand == HELP_CMD_VBOXMANAGE_INVALID)
    598         printUsage(enmCommand, fSubcommandScope, g_pStdErr);
    599     else
    600         printUsage(g_pStdErr);
    601 
    602     va_start(args, pszFormat);
    603     RTStrmPrintf(g_pStdErr, Help::tr("\nSyntax error: %N\n"), pszFormat, &args);
    604     va_end(args);
    605     return RTEXITCODE_SYNTAX;
    606 }
    607 
    608 /**
    609  * errorSyntax for RTGetOpt users.
    610  *
    611  * @returns RTEXITCODE_SYNTAX.
    612  *
    613  * @param   enmCommand          The command.
    614  * @param   fSubcommandScope    The subcommand scope, REFENTRYSTR_SCOPE_GLOBAL
    615  *                              for all.
    616  * @param   rc                  The RTGetOpt return code.
    617  * @param   pValueUnion         The value union.
    618  */
    619 RTEXITCODE errorGetOptEx(USAGECATEGORY enmCommand, uint64_t fSubcommandScope, int rc, union RTGETOPTUNION const *pValueUnion)
    620 {
    621     /*
    622      * Check if it is an unhandled standard option.
    623      */
    624     if (rc == 'V')
    625     {
    626         RTPrintf("%sr%d\n", VBOX_VERSION_STRING, RTBldCfgRevision());
    627         return RTEXITCODE_SUCCESS;
    628     }
    629 
    630     if (rc == 'h')
    631     {
    632         showLogo(g_pStdErr);
    633 
    634         if (g_fInternalMode)
    635             printUsageInternal(enmCommand, g_pStdOut);
    636         else if (g_enmCurCommand == HELP_CMD_VBOXMANAGE_INVALID)
    637             printUsage(enmCommand, fSubcommandScope, g_pStdOut);
    638         else
    639             printUsage(g_pStdErr);
    640 
    641         return RTEXITCODE_SUCCESS;
    642     }
    643 
    644     /*
    645      * General failure.
    646      */
    647     showLogo(g_pStdErr); // show logo even if suppressed
    648 
    649     if (g_fInternalMode)
    650         printUsageInternal(enmCommand, g_pStdErr);
    651     else if (g_enmCurCommand == HELP_CMD_VBOXMANAGE_INVALID)
    652         printUsage(enmCommand, fSubcommandScope, g_pStdErr);
    653     else
    654         printUsage(g_pStdErr);
    655 
    656     if (rc == VINF_GETOPT_NOT_OPTION)
    657         return RTMsgErrorExit(RTEXITCODE_SYNTAX, Help::tr("Invalid parameter '%s'"), pValueUnion->psz);
    658     if (rc > 0)
    659     {
    660         if (RT_C_IS_PRINT(rc))
    661             return RTMsgErrorExit(RTEXITCODE_SYNTAX, Help::tr("Invalid option -%c"), rc);
    662         return RTMsgErrorExit(RTEXITCODE_SYNTAX, Help::tr("Invalid option case %i"), rc);
    663     }
    664     if (rc == VERR_GETOPT_UNKNOWN_OPTION)
    665         return RTMsgErrorExit(RTEXITCODE_SYNTAX, Help::tr("Unknown option: %s"), pValueUnion->psz);
    666     if (rc == VERR_GETOPT_INVALID_ARGUMENT_FORMAT)
    667         return RTMsgErrorExit(RTEXITCODE_SYNTAX, Help::tr("Invalid argument format: %s"), pValueUnion->psz);
    668     if (pValueUnion->pDef)
    669         return RTMsgErrorExit(RTEXITCODE_SYNTAX, "%s: %Rrs", pValueUnion->pDef->pszLong, rc);
    670     return RTMsgErrorExit(RTEXITCODE_SYNTAX, "%Rrs", rc);
    671 }
    672 
    673 
    674 /**
    675  * errorSyntax for RTGetOpt users.
    676  *
    677  * @returns RTEXITCODE_SYNTAX.
    678  *
    679  * @param   enmCommand      The command.
    680  * @param   rc              The RTGetOpt return code.
    681  * @param   pValueUnion     The value union.
    682  */
    683 RTEXITCODE errorGetOpt(USAGECATEGORY enmCommand, int rc, union RTGETOPTUNION const *pValueUnion)
    684 {
    685     return errorGetOptEx(enmCommand, RTMSGREFENTRYSTR_SCOPE_GLOBAL, rc, pValueUnion);
    686 }
    687 
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