1 | /* $Id: VBoxManageHelp.cpp 94234 2022-03-15 09:19:29Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxManage - help and other message output.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #include <VBox/version.h>
|
---|
23 |
|
---|
24 | #include <iprt/asm.h>
|
---|
25 | #include <iprt/buildconfig.h>
|
---|
26 | #include <iprt/ctype.h>
|
---|
27 | #include <iprt/assert.h>
|
---|
28 | #include <iprt/env.h>
|
---|
29 | #include <iprt/err.h>
|
---|
30 | #include <iprt/getopt.h>
|
---|
31 | #include <iprt/stream.h>
|
---|
32 | #include <iprt/message.h>
|
---|
33 | #include <iprt/uni.h>
|
---|
34 |
|
---|
35 | #include "VBoxManage.h"
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Defined Constants And Macros *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | /** If the usage is the given number of length long or longer, the error is
|
---|
42 | * repeated so the user can actually see it. */
|
---|
43 | #define ERROR_REPEAT_AFTER_USAGE_LENGTH 16
|
---|
44 |
|
---|
45 |
|
---|
46 | /*********************************************************************************************************************************
|
---|
47 | * Global Variables *
|
---|
48 | *********************************************************************************************************************************/
|
---|
49 | DECLARE_TRANSLATION_CONTEXT(Help);
|
---|
50 |
|
---|
51 | static enum HELP_CMD_VBOXMANAGE g_enmCurCommand = HELP_CMD_VBOXMANAGE_INVALID;
|
---|
52 | /** The scope mask for the current subcommand. */
|
---|
53 | static uint64_t g_fCurSubcommandScope = RTMSGREFENTRYSTR_SCOPE_GLOBAL;
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Sets the current command.
|
---|
57 | *
|
---|
58 | * This affects future calls to error and help functions.
|
---|
59 | *
|
---|
60 | * @param enmCommand The command.
|
---|
61 | */
|
---|
62 | void setCurrentCommand(enum HELP_CMD_VBOXMANAGE enmCommand)
|
---|
63 | {
|
---|
64 | Assert(g_enmCurCommand == HELP_CMD_VBOXMANAGE_INVALID);
|
---|
65 | g_enmCurCommand = enmCommand;
|
---|
66 | g_fCurSubcommandScope = RTMSGREFENTRYSTR_SCOPE_GLOBAL;
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Sets the current subcommand.
|
---|
72 | *
|
---|
73 | * This affects future calls to error and help functions.
|
---|
74 | *
|
---|
75 | * @param fSubcommandScope The subcommand scope.
|
---|
76 | */
|
---|
77 | void setCurrentSubcommand(uint64_t fSubcommandScope)
|
---|
78 | {
|
---|
79 | g_fCurSubcommandScope = fSubcommandScope;
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Takes first char and make it uppercase.
|
---|
85 | *
|
---|
86 | * @returns pointer to string starting from next char.
|
---|
87 | * @param pszSrc Source string.
|
---|
88 | * @param pszDst Pointer to buffer to place first char uppercase.
|
---|
89 | */
|
---|
90 | static const char *captialize(const char *pszSrc, char *pszDst)
|
---|
91 | {
|
---|
92 | *RTStrPutCp(pszDst, RTUniCpToUpper(RTStrGetCp(pszSrc))) = '\0';
|
---|
93 | return RTStrNextCp(pszSrc);
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Prints brief help for a command or subcommand.
|
---|
99 | *
|
---|
100 | * @returns Number of lines written.
|
---|
101 | * @param enmCommand The command.
|
---|
102 | * @param fSubcommandScope The subcommand scope, REFENTRYSTR_SCOPE_GLOBAL
|
---|
103 | * for all.
|
---|
104 | * @param pStrm The output stream.
|
---|
105 | */
|
---|
106 | static uint32_t printBriefCommandOrSubcommandHelp(enum HELP_CMD_VBOXMANAGE enmCommand, uint64_t fSubcommandScope, PRTSTREAM pStrm)
|
---|
107 | {
|
---|
108 | /*
|
---|
109 | * Try to find translated, falling back untranslated.
|
---|
110 | */
|
---|
111 | uint32_t cLinesWritten = 0;
|
---|
112 | uint32_t cPendingBlankLines = 0;
|
---|
113 | uint32_t cFound = 0;
|
---|
114 | PCHELP_LANG_ENTRY_T const apHelpLangEntries[] =
|
---|
115 | {
|
---|
116 | ASMAtomicUoReadPtrT(&g_pHelpLangEntry, PCHELP_LANG_ENTRY_T),
|
---|
117 | #ifdef VBOX_WITH_VBOXMANAGE_NLS
|
---|
118 | &g_aHelpLangEntries[0]
|
---|
119 | #endif
|
---|
120 | };
|
---|
121 | for (uint32_t k = 0; k < RT_ELEMENTS(apHelpLangEntries) && cFound == 0; k++)
|
---|
122 | {
|
---|
123 | /* skip if english is used */
|
---|
124 | if (k > 0 && apHelpLangEntries[k] == apHelpLangEntries[0])
|
---|
125 | break;
|
---|
126 | uint32_t const cHelpEntries = *apHelpLangEntries[k]->pcHelpEntries;
|
---|
127 | for (uint32_t i = 0; i < cHelpEntries; i++)
|
---|
128 | {
|
---|
129 | PCRTMSGREFENTRY pHelp = apHelpLangEntries[k]->papHelpEntries[i];
|
---|
130 | if (pHelp->idInternal == (int64_t)enmCommand)
|
---|
131 | {
|
---|
132 | cFound++;
|
---|
133 | if (cFound == 1)
|
---|
134 | {
|
---|
135 | if (fSubcommandScope == RTMSGREFENTRYSTR_SCOPE_GLOBAL)
|
---|
136 | {
|
---|
137 | char szFirstChar[8];
|
---|
138 | RTStrmPrintf(pStrm, Help::tr("Usage - %s%s:\n"), szFirstChar, captialize(pHelp->pszBrief, szFirstChar));
|
---|
139 | }
|
---|
140 | else
|
---|
141 | RTStrmPrintf(pStrm, Help::tr("Usage:\n"));
|
---|
142 | }
|
---|
143 | RTMsgRefEntryPrintStringTable(pStrm, &pHelp->Synopsis, fSubcommandScope, &cPendingBlankLines, &cLinesWritten);
|
---|
144 | if (!cPendingBlankLines)
|
---|
145 | cPendingBlankLines = 1;
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 | Assert(cFound > 0);
|
---|
150 | return cLinesWritten;
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Prints the brief usage information for the current (sub)command.
|
---|
156 | *
|
---|
157 | * @param pStrm The output stream.
|
---|
158 | */
|
---|
159 | void printUsage(PRTSTREAM pStrm)
|
---|
160 | {
|
---|
161 | printBriefCommandOrSubcommandHelp(g_enmCurCommand, g_fCurSubcommandScope, pStrm);
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Prints full help for a command or subcommand.
|
---|
167 | *
|
---|
168 | * @param enmCommand The command.
|
---|
169 | * @param fSubcommandScope The subcommand scope, REFENTRYSTR_SCOPE_GLOBAL
|
---|
170 | * for all.
|
---|
171 | * @param pStrm The output stream.
|
---|
172 | */
|
---|
173 | static void printFullCommandOrSubcommandHelp(enum HELP_CMD_VBOXMANAGE enmCommand, uint64_t fSubcommandScope, PRTSTREAM pStrm)
|
---|
174 | {
|
---|
175 | /* Try to find translated, then untranslated */
|
---|
176 | uint32_t cPendingBlankLines = 0;
|
---|
177 | uint32_t cFound = 0;
|
---|
178 | PCHELP_LANG_ENTRY_T const apHelpLangEntries[] =
|
---|
179 | {
|
---|
180 | ASMAtomicUoReadPtrT(&g_pHelpLangEntry, PCHELP_LANG_ENTRY_T),
|
---|
181 | #ifdef VBOX_WITH_VBOXMANAGE_NLS
|
---|
182 | &g_aHelpLangEntries[0]
|
---|
183 | #endif
|
---|
184 | };
|
---|
185 | for (uint32_t k = 0; k < RT_ELEMENTS(apHelpLangEntries) && cFound == 0; k++)
|
---|
186 | {
|
---|
187 | /* skip if english is used */
|
---|
188 | if (k > 0 && apHelpLangEntries[k] == apHelpLangEntries[0])
|
---|
189 | break;
|
---|
190 | uint32_t const cHelpEntries = *apHelpLangEntries[k]->pcHelpEntries;
|
---|
191 | for (uint32_t i = 0; i < cHelpEntries; i++)
|
---|
192 | {
|
---|
193 | PCRTMSGREFENTRY pHelp = apHelpLangEntries[k]->papHelpEntries[i];
|
---|
194 |
|
---|
195 | if ( pHelp->idInternal == (int64_t)enmCommand
|
---|
196 | || enmCommand == HELP_CMD_VBOXMANAGE_INVALID)
|
---|
197 | {
|
---|
198 | cFound++;
|
---|
199 | RTMsgRefEntryPrintStringTable(pStrm, &pHelp->Help, fSubcommandScope, &cPendingBlankLines, NULL /*pcLinesWritten*/);
|
---|
200 | if (cPendingBlankLines < 2)
|
---|
201 | cPendingBlankLines = 2;
|
---|
202 | }
|
---|
203 | }
|
---|
204 | }
|
---|
205 | Assert(cFound > 0);
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Prints the full help for the current (sub)command.
|
---|
211 | *
|
---|
212 | * @param pStrm The output stream.
|
---|
213 | */
|
---|
214 | void printHelp(PRTSTREAM pStrm)
|
---|
215 | {
|
---|
216 | printFullCommandOrSubcommandHelp(g_enmCurCommand, g_fCurSubcommandScope, pStrm);
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Display no subcommand error message and current command usage.
|
---|
222 | *
|
---|
223 | * @returns RTEXITCODE_SYNTAX.
|
---|
224 | */
|
---|
225 | RTEXITCODE errorNoSubcommand(void)
|
---|
226 | {
|
---|
227 | Assert(g_enmCurCommand != HELP_CMD_VBOXMANAGE_INVALID);
|
---|
228 | Assert(g_fCurSubcommandScope == RTMSGREFENTRYSTR_SCOPE_GLOBAL);
|
---|
229 |
|
---|
230 | return errorSyntax(Help::tr("No subcommand specified"));
|
---|
231 | }
|
---|
232 |
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Display unknown subcommand error message and current command usage.
|
---|
236 | *
|
---|
237 | * May show full command help instead if the subcommand is a common help option.
|
---|
238 | *
|
---|
239 | * @returns RTEXITCODE_SYNTAX, or RTEXITCODE_SUCCESS if common help option.
|
---|
240 | * @param pszSubcommand The name of the alleged subcommand.
|
---|
241 | */
|
---|
242 | RTEXITCODE errorUnknownSubcommand(const char *pszSubcommand)
|
---|
243 | {
|
---|
244 | Assert(g_enmCurCommand != HELP_CMD_VBOXMANAGE_INVALID);
|
---|
245 | Assert(g_fCurSubcommandScope == RTMSGREFENTRYSTR_SCOPE_GLOBAL);
|
---|
246 |
|
---|
247 | /* check if help was requested. */
|
---|
248 | if ( strcmp(pszSubcommand, "--help") == 0
|
---|
249 | || strcmp(pszSubcommand, "-h") == 0
|
---|
250 | || strcmp(pszSubcommand, "-?") == 0)
|
---|
251 | {
|
---|
252 | printFullCommandOrSubcommandHelp(g_enmCurCommand, g_fCurSubcommandScope, g_pStdOut);
|
---|
253 | return RTEXITCODE_SUCCESS;
|
---|
254 | }
|
---|
255 |
|
---|
256 | return errorSyntax(Help::tr("Unknown subcommand: %s"), pszSubcommand);
|
---|
257 | }
|
---|
258 |
|
---|
259 |
|
---|
260 | /**
|
---|
261 | * Display too many parameters error message and current command usage.
|
---|
262 | *
|
---|
263 | * May show full command help instead if the subcommand is a common help option.
|
---|
264 | *
|
---|
265 | * @returns RTEXITCODE_SYNTAX, or RTEXITCODE_SUCCESS if common help option.
|
---|
266 | * @param papszArgs The first unwanted parameter. Terminated by
|
---|
267 | * NULL entry.
|
---|
268 | */
|
---|
269 | RTEXITCODE errorTooManyParameters(char **papszArgs)
|
---|
270 | {
|
---|
271 | Assert(g_enmCurCommand != HELP_CMD_VBOXMANAGE_INVALID);
|
---|
272 | Assert(g_fCurSubcommandScope != RTMSGREFENTRYSTR_SCOPE_GLOBAL);
|
---|
273 |
|
---|
274 | /* check if help was requested. */
|
---|
275 | if (papszArgs)
|
---|
276 | {
|
---|
277 | for (uint32_t i = 0; papszArgs[i]; i++)
|
---|
278 | if ( strcmp(papszArgs[i], "--help") == 0
|
---|
279 | || strcmp(papszArgs[i], "-h") == 0
|
---|
280 | || strcmp(papszArgs[i], "-?") == 0)
|
---|
281 | {
|
---|
282 | printFullCommandOrSubcommandHelp(g_enmCurCommand, g_fCurSubcommandScope, g_pStdOut);
|
---|
283 | return RTEXITCODE_SUCCESS;
|
---|
284 | }
|
---|
285 | else if (!strcmp(papszArgs[i], "--"))
|
---|
286 | break;
|
---|
287 | }
|
---|
288 |
|
---|
289 | return errorSyntax(Help::tr("Too many parameters"));
|
---|
290 | }
|
---|
291 |
|
---|
292 |
|
---|
293 | /**
|
---|
294 | * Display current (sub)command usage and the custom error message.
|
---|
295 | *
|
---|
296 | * @returns RTEXITCODE_SYNTAX.
|
---|
297 | * @param pszFormat Custom error message format string.
|
---|
298 | * @param va Format arguments.
|
---|
299 | */
|
---|
300 | RTEXITCODE errorSyntaxV(const char *pszFormat, va_list va)
|
---|
301 | {
|
---|
302 | Assert(g_enmCurCommand != HELP_CMD_VBOXMANAGE_INVALID);
|
---|
303 |
|
---|
304 | showLogo(g_pStdErr);
|
---|
305 |
|
---|
306 | va_list vaCopy;
|
---|
307 | va_copy(vaCopy, va);
|
---|
308 | RTMsgErrorV(pszFormat, vaCopy);
|
---|
309 | va_end(vaCopy);
|
---|
310 |
|
---|
311 | RTStrmPutCh(g_pStdErr, '\n');
|
---|
312 | if ( printBriefCommandOrSubcommandHelp(g_enmCurCommand, g_fCurSubcommandScope, g_pStdErr)
|
---|
313 | >= ERROR_REPEAT_AFTER_USAGE_LENGTH)
|
---|
314 | {
|
---|
315 | /* Usage was very long, repeat the error message. */
|
---|
316 | RTStrmPutCh(g_pStdErr, '\n');
|
---|
317 | RTMsgErrorV(pszFormat, va);
|
---|
318 | }
|
---|
319 | return RTEXITCODE_SYNTAX;
|
---|
320 | }
|
---|
321 |
|
---|
322 |
|
---|
323 | /**
|
---|
324 | * Display current (sub)command usage and the custom error message.
|
---|
325 | *
|
---|
326 | * @returns RTEXITCODE_SYNTAX.
|
---|
327 | * @param pszFormat Custom error message format string.
|
---|
328 | * @param ... Format arguments.
|
---|
329 | */
|
---|
330 | RTEXITCODE errorSyntax(const char *pszFormat, ...)
|
---|
331 | {
|
---|
332 | va_list va;
|
---|
333 | va_start(va, pszFormat);
|
---|
334 | RTEXITCODE rcExit = errorSyntaxV(pszFormat, va);
|
---|
335 | va_end(va);
|
---|
336 | return rcExit;
|
---|
337 | }
|
---|
338 |
|
---|
339 |
|
---|
340 | /**
|
---|
341 | * Display current (sub)command usage and the custom error message.
|
---|
342 | *
|
---|
343 | * @returns E_INVALIDARG
|
---|
344 | * @param pszFormat Custom error message format string.
|
---|
345 | * @param ... Format arguments.
|
---|
346 | */
|
---|
347 | HRESULT errorSyntaxHr(const char *pszFormat, ...)
|
---|
348 | {
|
---|
349 | va_list va;
|
---|
350 | va_start(va, pszFormat);
|
---|
351 | errorSyntaxV(pszFormat, va);
|
---|
352 | va_end(va);
|
---|
353 | return E_INVALIDARG;
|
---|
354 | }
|
---|
355 |
|
---|
356 |
|
---|
357 | /**
|
---|
358 | * Print an error message without the syntax stuff.
|
---|
359 | *
|
---|
360 | * @returns RTEXITCODE_SYNTAX.
|
---|
361 | */
|
---|
362 | RTEXITCODE errorArgument(const char *pszFormat, ...)
|
---|
363 | {
|
---|
364 | va_list args;
|
---|
365 | va_start(args, pszFormat);
|
---|
366 | RTMsgErrorV(pszFormat, args);
|
---|
367 | va_end(args);
|
---|
368 | return RTEXITCODE_SYNTAX;
|
---|
369 | }
|
---|
370 |
|
---|
371 |
|
---|
372 | /**
|
---|
373 | * Print an error message without the syntax stuff.
|
---|
374 | *
|
---|
375 | * @returns E_INVALIDARG.
|
---|
376 | */
|
---|
377 | HRESULT errorArgumentHr(const char *pszFormat, ...)
|
---|
378 | {
|
---|
379 | va_list args;
|
---|
380 | va_start(args, pszFormat);
|
---|
381 | RTMsgErrorV(pszFormat, args);
|
---|
382 | va_end(args);
|
---|
383 | return E_INVALIDARG;
|
---|
384 | }
|
---|
385 |
|
---|
386 |
|
---|
387 | /**
|
---|
388 | * Worker for errorGetOpt.
|
---|
389 | *
|
---|
390 | * @param rcGetOpt The RTGetOpt return value.
|
---|
391 | * @param pValueUnion The value union returned by RTGetOpt.
|
---|
392 | */
|
---|
393 | static void errorGetOptWorker(int rcGetOpt, union RTGETOPTUNION const *pValueUnion)
|
---|
394 | {
|
---|
395 | if (rcGetOpt == VINF_GETOPT_NOT_OPTION)
|
---|
396 | RTMsgError(Help::tr("Invalid parameter '%s'"), pValueUnion->psz);
|
---|
397 | else if (rcGetOpt > 0)
|
---|
398 | {
|
---|
399 | if (RT_C_IS_PRINT(rcGetOpt))
|
---|
400 | RTMsgError(Help::tr("Invalid option -%c"), rcGetOpt);
|
---|
401 | else
|
---|
402 | RTMsgError(Help::tr("Invalid option case %i"), rcGetOpt);
|
---|
403 | }
|
---|
404 | else if (rcGetOpt == VERR_GETOPT_UNKNOWN_OPTION)
|
---|
405 | RTMsgError(Help::tr("Unknown option: %s"), pValueUnion->psz);
|
---|
406 | else if (rcGetOpt == VERR_GETOPT_INVALID_ARGUMENT_FORMAT)
|
---|
407 | RTMsgError(Help::tr("Invalid argument format: %s"), pValueUnion->psz);
|
---|
408 | else if (pValueUnion->pDef)
|
---|
409 | RTMsgError("%s: %Rrs", pValueUnion->pDef->pszLong, rcGetOpt);
|
---|
410 | else
|
---|
411 | RTMsgError("%Rrs", rcGetOpt);
|
---|
412 | }
|
---|
413 |
|
---|
414 |
|
---|
415 | /**
|
---|
416 | * For use to deal with RTGetOptFetchValue failures.
|
---|
417 | *
|
---|
418 | * @retval RTEXITCODE_SYNTAX
|
---|
419 | * @param iValueNo The value number being fetched, counting the
|
---|
420 | * RTGetOpt value as zero and the first
|
---|
421 | * RTGetOptFetchValue call as one.
|
---|
422 | * @param pszOption The option being parsed.
|
---|
423 | * @param rcGetOptFetchValue The status returned by RTGetOptFetchValue.
|
---|
424 | * @param pValueUnion The value union returned by the fetch.
|
---|
425 | */
|
---|
426 | RTEXITCODE errorFetchValue(int iValueNo, const char *pszOption, int rcGetOptFetchValue, union RTGETOPTUNION const *pValueUnion)
|
---|
427 | {
|
---|
428 | Assert(g_enmCurCommand != HELP_CMD_VBOXMANAGE_INVALID);
|
---|
429 | showLogo(g_pStdErr);
|
---|
430 | if (rcGetOptFetchValue == VERR_GETOPT_REQUIRED_ARGUMENT_MISSING)
|
---|
431 | RTMsgError(Help::tr("Missing the %u%s value for option %s"),
|
---|
432 | iValueNo,
|
---|
433 | iValueNo == 1 ? Help::tr("st")
|
---|
434 | : iValueNo == 2 ? Help::tr("nd")
|
---|
435 | : iValueNo == 3 ? Help::tr("rd")
|
---|
436 | : Help::tr("th"),
|
---|
437 | pszOption);
|
---|
438 | else
|
---|
439 | errorGetOptWorker(rcGetOptFetchValue, pValueUnion);
|
---|
440 | return RTEXITCODE_SYNTAX;
|
---|
441 |
|
---|
442 | }
|
---|
443 |
|
---|
444 |
|
---|
445 | /**
|
---|
446 | * Handled an RTGetOpt error or common option.
|
---|
447 | *
|
---|
448 | * This implements the 'V' and 'h' cases. It reports appropriate syntax errors
|
---|
449 | * for other @a rcGetOpt values.
|
---|
450 | *
|
---|
451 | * @retval RTEXITCODE_SUCCESS if help or version request.
|
---|
452 | * @retval RTEXITCODE_SYNTAX if not help or version request.
|
---|
453 | * @param rcGetOpt The RTGetOpt return value.
|
---|
454 | * @param pValueUnion The value union returned by RTGetOpt.
|
---|
455 | */
|
---|
456 | RTEXITCODE errorGetOpt(int rcGetOpt, union RTGETOPTUNION const *pValueUnion)
|
---|
457 | {
|
---|
458 | Assert(g_enmCurCommand != HELP_CMD_VBOXMANAGE_INVALID);
|
---|
459 |
|
---|
460 | /*
|
---|
461 | * Check if it is an unhandled standard option.
|
---|
462 | */
|
---|
463 | if (rcGetOpt == 'V')
|
---|
464 | {
|
---|
465 | RTPrintf("%sr%d\n", VBOX_VERSION_STRING, RTBldCfgRevision());
|
---|
466 | return RTEXITCODE_SUCCESS;
|
---|
467 | }
|
---|
468 |
|
---|
469 | if (rcGetOpt == 'h')
|
---|
470 | {
|
---|
471 | printFullCommandOrSubcommandHelp(g_enmCurCommand, g_fCurSubcommandScope, g_pStdOut);
|
---|
472 | return RTEXITCODE_SUCCESS;
|
---|
473 | }
|
---|
474 |
|
---|
475 | /*
|
---|
476 | * We failed.
|
---|
477 | */
|
---|
478 | showLogo(g_pStdErr);
|
---|
479 | errorGetOptWorker(rcGetOpt, pValueUnion);
|
---|
480 | if ( printBriefCommandOrSubcommandHelp(g_enmCurCommand, g_fCurSubcommandScope, g_pStdErr)
|
---|
481 | >= ERROR_REPEAT_AFTER_USAGE_LENGTH)
|
---|
482 | {
|
---|
483 | /* Usage was very long, repeat the error message. */
|
---|
484 | RTStrmPutCh(g_pStdErr, '\n');
|
---|
485 | errorGetOptWorker(rcGetOpt, pValueUnion);
|
---|
486 | }
|
---|
487 | return RTEXITCODE_SYNTAX;
|
---|
488 | }
|
---|
489 |
|
---|
490 |
|
---|
491 | void showLogo(PRTSTREAM pStrm)
|
---|
492 | {
|
---|
493 | static bool s_fShown; /* show only once */
|
---|
494 |
|
---|
495 | if (!s_fShown)
|
---|
496 | {
|
---|
497 | RTStrmPrintf(pStrm, VBOX_PRODUCT " Command Line Management Interface Version "
|
---|
498 | VBOX_VERSION_STRING "\n"
|
---|
499 | "(C) 2005-" VBOX_C_YEAR " " VBOX_VENDOR "\n"
|
---|
500 | "All rights reserved.\n"
|
---|
501 | "\n");
|
---|
502 | s_fShown = true;
|
---|
503 | }
|
---|
504 | }
|
---|
505 |
|
---|
506 |
|
---|
507 |
|
---|
508 |
|
---|
509 | void printUsage(USAGECATEGORY enmCommand, uint64_t fSubcommandScope, PRTSTREAM pStrm)
|
---|
510 | {
|
---|
511 | bool fDumpOpts = false;
|
---|
512 | #ifdef RT_OS_LINUX
|
---|
513 | bool fLinux = true;
|
---|
514 | #else
|
---|
515 | bool fLinux = false;
|
---|
516 | #endif
|
---|
517 | #ifdef RT_OS_WINDOWS
|
---|
518 | bool fWin = true;
|
---|
519 | #else
|
---|
520 | bool fWin = false;
|
---|
521 | #endif
|
---|
522 | #ifdef RT_OS_SOLARIS
|
---|
523 | bool fSolaris = true;
|
---|
524 | #else
|
---|
525 | bool fSolaris = false;
|
---|
526 | #endif
|
---|
527 | #ifdef RT_OS_FREEBSD
|
---|
528 | bool fFreeBSD = true;
|
---|
529 | #else
|
---|
530 | bool fFreeBSD = false;
|
---|
531 | #endif
|
---|
532 | #ifdef RT_OS_DARWIN
|
---|
533 | bool fDarwin = true;
|
---|
534 | #else
|
---|
535 | bool fDarwin = false;
|
---|
536 | #endif
|
---|
537 | #ifdef VBOX_WITH_VBOXSDL
|
---|
538 | bool fVBoxSDL = true;
|
---|
539 | #else
|
---|
540 | bool fVBoxSDL = false;
|
---|
541 | #endif
|
---|
542 |
|
---|
543 | RT_NOREF(fSubcommandScope);
|
---|
544 |
|
---|
545 | Assert(enmCommand != USAGE_INVALID);
|
---|
546 | Assert(enmCommand != USAGE_S_NEWCMD);
|
---|
547 |
|
---|
548 | if (enmCommand == USAGE_S_DUMPOPTS)
|
---|
549 | {
|
---|
550 | fDumpOpts = true;
|
---|
551 | fLinux = true;
|
---|
552 | fWin = true;
|
---|
553 | fSolaris = true;
|
---|
554 | fFreeBSD = true;
|
---|
555 | fDarwin = true;
|
---|
556 | fVBoxSDL = true;
|
---|
557 | enmCommand = USAGE_S_ALL;
|
---|
558 | }
|
---|
559 |
|
---|
560 | RTStrmPrintf(pStrm,
|
---|
561 | Help::tr("Usage:\n"
|
---|
562 | "\n"));
|
---|
563 |
|
---|
564 | if (enmCommand == USAGE_S_ALL)
|
---|
565 | RTStrmPrintf(pStrm,
|
---|
566 | " VBoxManage [<general option>] <command>\n"
|
---|
567 | "\n"
|
---|
568 | "\n"
|
---|
569 | "General Options:\n"
|
---|
570 | "\n"
|
---|
571 | " [-V|--version] print version number and exit\n"
|
---|
572 | " [--dump-build-type] print build type and exit\n"
|
---|
573 | " [-q|--nologo] suppress the logo\n"
|
---|
574 | " [--settingspw <pw>] provide the settings password\n"
|
---|
575 | " [--settingspwfile <file>] provide a file containing the settings password\n"
|
---|
576 | " [@<response-file>] load arguments from the given response file (bourne style)\n"
|
---|
577 | "\n"
|
---|
578 | "\n"
|
---|
579 | "Commands:\n"
|
---|
580 | "\n");
|
---|
581 |
|
---|
582 | if (enmCommand == USAGE_S_ALL)
|
---|
583 | {
|
---|
584 | uint32_t cPendingBlankLines = 0;
|
---|
585 | PCHELP_LANG_ENTRY_T pHelpLangEntry = ASMAtomicUoReadPtrT(&g_pHelpLangEntry, PCHELP_LANG_ENTRY_T);
|
---|
586 | uint32_t const cHelpEntries = *pHelpLangEntry->pcHelpEntries;
|
---|
587 | for (uint32_t i = 0; i < cHelpEntries; i++)
|
---|
588 | {
|
---|
589 | PCRTMSGREFENTRY pHelp = pHelpLangEntry->papHelpEntries[i];
|
---|
590 |
|
---|
591 | while (cPendingBlankLines-- > 0)
|
---|
592 | RTStrmPutCh(pStrm, '\n');
|
---|
593 |
|
---|
594 | char szFirstChar[8];
|
---|
595 | RTStrmPrintf(pStrm, " %s%s:\n", szFirstChar, captialize(pHelp->pszBrief, szFirstChar));
|
---|
596 |
|
---|
597 | cPendingBlankLines = 0;
|
---|
598 | RTMsgRefEntryPrintStringTable(pStrm, &pHelp->Synopsis, RTMSGREFENTRYSTR_SCOPE_GLOBAL,
|
---|
599 | &cPendingBlankLines, NULL /*pcLinesWritten*/);
|
---|
600 | cPendingBlankLines = RT_MAX(cPendingBlankLines, 1);
|
---|
601 | }
|
---|
602 | }
|
---|
603 | }
|
---|
604 |
|
---|
605 | /**
|
---|
606 | * Print a usage synopsis and the syntax error message.
|
---|
607 | * @returns RTEXITCODE_SYNTAX.
|
---|
608 | */
|
---|
609 | RTEXITCODE errorSyntax(USAGECATEGORY enmCommand, const char *pszFormat, ...)
|
---|
610 | {
|
---|
611 | va_list args;
|
---|
612 | showLogo(g_pStdErr); // show logo even if suppressed
|
---|
613 |
|
---|
614 | if (g_fInternalMode)
|
---|
615 | printUsageInternal(enmCommand, g_pStdErr);
|
---|
616 | else if (g_enmCurCommand == HELP_CMD_VBOXMANAGE_INVALID)
|
---|
617 | printUsage(enmCommand, RTMSGREFENTRYSTR_SCOPE_GLOBAL, g_pStdErr);
|
---|
618 | else
|
---|
619 | printUsage(g_pStdErr);
|
---|
620 |
|
---|
621 | va_start(args, pszFormat);
|
---|
622 | RTStrmPrintf(g_pStdErr, Help::tr("\nSyntax error: %N\n"), pszFormat, &args);
|
---|
623 | va_end(args);
|
---|
624 | return RTEXITCODE_SYNTAX;
|
---|
625 | }
|
---|
626 |
|
---|
627 | /**
|
---|
628 | * Print a usage synopsis and the syntax error message.
|
---|
629 | * @returns RTEXITCODE_SYNTAX.
|
---|
630 | */
|
---|
631 | RTEXITCODE errorSyntaxEx(USAGECATEGORY enmCommand, uint64_t fSubcommandScope, const char *pszFormat, ...)
|
---|
632 | {
|
---|
633 | va_list args;
|
---|
634 | showLogo(g_pStdErr); // show logo even if suppressed
|
---|
635 |
|
---|
636 | if (g_fInternalMode)
|
---|
637 | printUsageInternal(enmCommand, g_pStdErr);
|
---|
638 | else if (g_enmCurCommand == HELP_CMD_VBOXMANAGE_INVALID)
|
---|
639 | printUsage(enmCommand, fSubcommandScope, g_pStdErr);
|
---|
640 | else
|
---|
641 | printUsage(g_pStdErr);
|
---|
642 |
|
---|
643 | va_start(args, pszFormat);
|
---|
644 | RTStrmPrintf(g_pStdErr, Help::tr("\nSyntax error: %N\n"), pszFormat, &args);
|
---|
645 | va_end(args);
|
---|
646 | return RTEXITCODE_SYNTAX;
|
---|
647 | }
|
---|
648 |
|
---|
649 | /**
|
---|
650 | * errorSyntax for RTGetOpt users.
|
---|
651 | *
|
---|
652 | * @returns RTEXITCODE_SYNTAX.
|
---|
653 | *
|
---|
654 | * @param enmCommand The command.
|
---|
655 | * @param fSubcommandScope The subcommand scope, REFENTRYSTR_SCOPE_GLOBAL
|
---|
656 | * for all.
|
---|
657 | * @param rc The RTGetOpt return code.
|
---|
658 | * @param pValueUnion The value union.
|
---|
659 | */
|
---|
660 | RTEXITCODE errorGetOptEx(USAGECATEGORY enmCommand, uint64_t fSubcommandScope, int rc, union RTGETOPTUNION const *pValueUnion)
|
---|
661 | {
|
---|
662 | /*
|
---|
663 | * Check if it is an unhandled standard option.
|
---|
664 | */
|
---|
665 | if (rc == 'V')
|
---|
666 | {
|
---|
667 | RTPrintf("%sr%d\n", VBOX_VERSION_STRING, RTBldCfgRevision());
|
---|
668 | return RTEXITCODE_SUCCESS;
|
---|
669 | }
|
---|
670 |
|
---|
671 | if (rc == 'h')
|
---|
672 | {
|
---|
673 | showLogo(g_pStdErr);
|
---|
674 |
|
---|
675 | if (g_fInternalMode)
|
---|
676 | printUsageInternal(enmCommand, g_pStdOut);
|
---|
677 | else if (g_enmCurCommand == HELP_CMD_VBOXMANAGE_INVALID)
|
---|
678 | printUsage(enmCommand, fSubcommandScope, g_pStdOut);
|
---|
679 | else
|
---|
680 | printUsage(g_pStdErr);
|
---|
681 |
|
---|
682 | return RTEXITCODE_SUCCESS;
|
---|
683 | }
|
---|
684 |
|
---|
685 | /*
|
---|
686 | * General failure.
|
---|
687 | */
|
---|
688 | showLogo(g_pStdErr); // show logo even if suppressed
|
---|
689 |
|
---|
690 | if (g_fInternalMode)
|
---|
691 | printUsageInternal(enmCommand, g_pStdErr);
|
---|
692 | else if (g_enmCurCommand == HELP_CMD_VBOXMANAGE_INVALID)
|
---|
693 | printUsage(enmCommand, fSubcommandScope, g_pStdErr);
|
---|
694 | else
|
---|
695 | printUsage(g_pStdErr);
|
---|
696 |
|
---|
697 | if (rc == VINF_GETOPT_NOT_OPTION)
|
---|
698 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, Help::tr("Invalid parameter '%s'"), pValueUnion->psz);
|
---|
699 | if (rc > 0)
|
---|
700 | {
|
---|
701 | if (RT_C_IS_PRINT(rc))
|
---|
702 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, Help::tr("Invalid option -%c"), rc);
|
---|
703 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, Help::tr("Invalid option case %i"), rc);
|
---|
704 | }
|
---|
705 | if (rc == VERR_GETOPT_UNKNOWN_OPTION)
|
---|
706 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, Help::tr("Unknown option: %s"), pValueUnion->psz);
|
---|
707 | if (rc == VERR_GETOPT_INVALID_ARGUMENT_FORMAT)
|
---|
708 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, Help::tr("Invalid argument format: %s"), pValueUnion->psz);
|
---|
709 | if (pValueUnion->pDef)
|
---|
710 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "%s: %Rrs", pValueUnion->pDef->pszLong, rc);
|
---|
711 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "%Rrs", rc);
|
---|
712 | }
|
---|
713 |
|
---|
714 |
|
---|
715 | /**
|
---|
716 | * errorSyntax for RTGetOpt users.
|
---|
717 | *
|
---|
718 | * @returns RTEXITCODE_SYNTAX.
|
---|
719 | *
|
---|
720 | * @param enmCommand The command.
|
---|
721 | * @param rc The RTGetOpt return code.
|
---|
722 | * @param pValueUnion The value union.
|
---|
723 | */
|
---|
724 | RTEXITCODE errorGetOpt(USAGECATEGORY enmCommand, int rc, union RTGETOPTUNION const *pValueUnion)
|
---|
725 | {
|
---|
726 | return errorGetOptEx(enmCommand, RTMSGREFENTRYSTR_SCOPE_GLOBAL, rc, pValueUnion);
|
---|
727 | }
|
---|
728 |
|
---|