1 | /* $Id: VBoxManageHelp.cpp 94211 2022-03-13 20:40:25Z 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 | #ifndef VBOX_ONLY_DOCS
|
---|
52 | static enum HELP_CMD_VBOXMANAGE g_enmCurCommand = HELP_CMD_VBOXMANAGE_INVALID;
|
---|
53 | /** The scope mask for the current subcommand. */
|
---|
54 | static uint64_t g_fCurSubcommandScope = RTMSGREFENTRYSTR_SCOPE_GLOBAL;
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Sets the current command.
|
---|
58 | *
|
---|
59 | * This affects future calls to error and help functions.
|
---|
60 | *
|
---|
61 | * @param enmCommand The command.
|
---|
62 | */
|
---|
63 | void setCurrentCommand(enum HELP_CMD_VBOXMANAGE enmCommand)
|
---|
64 | {
|
---|
65 | Assert(g_enmCurCommand == HELP_CMD_VBOXMANAGE_INVALID);
|
---|
66 | g_enmCurCommand = enmCommand;
|
---|
67 | g_fCurSubcommandScope = RTMSGREFENTRYSTR_SCOPE_GLOBAL;
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Sets the current subcommand.
|
---|
73 | *
|
---|
74 | * This affects future calls to error and help functions.
|
---|
75 | *
|
---|
76 | * @param fSubcommandScope The subcommand scope.
|
---|
77 | */
|
---|
78 | void setCurrentSubcommand(uint64_t fSubcommandScope)
|
---|
79 | {
|
---|
80 | g_fCurSubcommandScope = fSubcommandScope;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Takes first char and make it uppercase.
|
---|
86 | *
|
---|
87 | * @returns pointer to string starting from next char.
|
---|
88 | * @param pszSrc Source string.
|
---|
89 | * @param pszDst Pointer to buffer to place first char uppercase.
|
---|
90 | */
|
---|
91 | static const char *captialize(const char *pszSrc, char *pszDst)
|
---|
92 | {
|
---|
93 | *RTStrPutCp(pszDst, RTUniCpToUpper(RTStrGetCp(pszSrc))) = '\0';
|
---|
94 | return RTStrNextCp(pszSrc);
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Prints brief help for a command or subcommand.
|
---|
100 | *
|
---|
101 | * @returns Number of lines written.
|
---|
102 | * @param enmCommand The command.
|
---|
103 | * @param fSubcommandScope The subcommand scope, REFENTRYSTR_SCOPE_GLOBAL
|
---|
104 | * for all.
|
---|
105 | * @param pStrm The output stream.
|
---|
106 | */
|
---|
107 | static uint32_t printBriefCommandOrSubcommandHelp(enum HELP_CMD_VBOXMANAGE enmCommand, uint64_t fSubcommandScope, PRTSTREAM pStrm)
|
---|
108 | {
|
---|
109 | /*
|
---|
110 | * Try to find translated, falling back untranslated.
|
---|
111 | */
|
---|
112 | uint32_t cLinesWritten = 0;
|
---|
113 | uint32_t cPendingBlankLines = 0;
|
---|
114 | uint32_t cFound = 0;
|
---|
115 | PCHELP_LANG_ENTRY_T const apHelpLangEntries[] =
|
---|
116 | {
|
---|
117 | ASMAtomicUoReadPtrT(&g_pHelpLangEntry, PCHELP_LANG_ENTRY_T),
|
---|
118 | #ifdef VBOX_WITH_VBOXMANAGE_NLS
|
---|
119 | &g_aHelpLangEntries[0]
|
---|
120 | #endif
|
---|
121 | };
|
---|
122 | for (uint32_t k = 0; k < RT_ELEMENTS(apHelpLangEntries) && cFound == 0; k++)
|
---|
123 | {
|
---|
124 | /* skip if english is used */
|
---|
125 | if (k > 0 && apHelpLangEntries[k] == apHelpLangEntries[0])
|
---|
126 | break;
|
---|
127 | uint32_t const cHelpEntries = *apHelpLangEntries[k]->pcHelpEntries;
|
---|
128 | for (uint32_t i = 0; i < cHelpEntries; i++)
|
---|
129 | {
|
---|
130 | PCRTMSGREFENTRY pHelp = apHelpLangEntries[k]->papHelpEntries[i];
|
---|
131 | if (pHelp->idInternal == (int64_t)enmCommand)
|
---|
132 | {
|
---|
133 | cFound++;
|
---|
134 | if (cFound == 1)
|
---|
135 | {
|
---|
136 | if (fSubcommandScope == RTMSGREFENTRYSTR_SCOPE_GLOBAL)
|
---|
137 | {
|
---|
138 | char szFirstChar[8];
|
---|
139 | RTStrmPrintf(pStrm, Help::tr("Usage - %s%s:\n"), szFirstChar, captialize(pHelp->pszBrief, szFirstChar));
|
---|
140 | }
|
---|
141 | else
|
---|
142 | RTStrmPrintf(pStrm, Help::tr("Usage:\n"));
|
---|
143 | }
|
---|
144 | RTMsgRefEntryPrintStringTable(pStrm, &pHelp->Synopsis, fSubcommandScope, &cPendingBlankLines, &cLinesWritten);
|
---|
145 | if (!cPendingBlankLines)
|
---|
146 | cPendingBlankLines = 1;
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|
150 | Assert(cFound > 0);
|
---|
151 | return cLinesWritten;
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Prints the brief usage information for the current (sub)command.
|
---|
157 | *
|
---|
158 | * @param pStrm The output stream.
|
---|
159 | */
|
---|
160 | void printUsage(PRTSTREAM pStrm)
|
---|
161 | {
|
---|
162 | printBriefCommandOrSubcommandHelp(g_enmCurCommand, g_fCurSubcommandScope, pStrm);
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Prints full help for a command or subcommand.
|
---|
168 | *
|
---|
169 | * @param enmCommand The command.
|
---|
170 | * @param fSubcommandScope The subcommand scope, REFENTRYSTR_SCOPE_GLOBAL
|
---|
171 | * for all.
|
---|
172 | * @param pStrm The output stream.
|
---|
173 | */
|
---|
174 | static void printFullCommandOrSubcommandHelp(enum HELP_CMD_VBOXMANAGE enmCommand, uint64_t fSubcommandScope, PRTSTREAM pStrm)
|
---|
175 | {
|
---|
176 | /* Try to find translated, then untranslated */
|
---|
177 | uint32_t cPendingBlankLines = 0;
|
---|
178 | uint32_t cFound = 0;
|
---|
179 | PCHELP_LANG_ENTRY_T const apHelpLangEntries[] =
|
---|
180 | {
|
---|
181 | ASMAtomicUoReadPtrT(&g_pHelpLangEntry, PCHELP_LANG_ENTRY_T),
|
---|
182 | #ifdef VBOX_WITH_VBOXMANAGE_NLS
|
---|
183 | &g_aHelpLangEntries[0]
|
---|
184 | #endif
|
---|
185 | };
|
---|
186 | for (uint32_t k = 0; k < RT_ELEMENTS(apHelpLangEntries) && cFound == 0; k++)
|
---|
187 | {
|
---|
188 | /* skip if english is used */
|
---|
189 | if (k > 0 && apHelpLangEntries[k] == apHelpLangEntries[0])
|
---|
190 | break;
|
---|
191 | uint32_t const cHelpEntries = *apHelpLangEntries[k]->pcHelpEntries;
|
---|
192 | for (uint32_t i = 0; i < cHelpEntries; i++)
|
---|
193 | {
|
---|
194 | PCRTMSGREFENTRY pHelp = apHelpLangEntries[k]->papHelpEntries[i];
|
---|
195 |
|
---|
196 | if ( pHelp->idInternal == (int64_t)enmCommand
|
---|
197 | || enmCommand == HELP_CMD_VBOXMANAGE_INVALID)
|
---|
198 | {
|
---|
199 | cFound++;
|
---|
200 | RTMsgRefEntryPrintStringTable(pStrm, &pHelp->Help, fSubcommandScope, &cPendingBlankLines, NULL /*pcLinesWritten*/);
|
---|
201 | if (cPendingBlankLines < 2)
|
---|
202 | cPendingBlankLines = 2;
|
---|
203 | }
|
---|
204 | }
|
---|
205 | }
|
---|
206 | Assert(cFound > 0);
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * Prints the full help for the current (sub)command.
|
---|
212 | *
|
---|
213 | * @param pStrm The output stream.
|
---|
214 | */
|
---|
215 | void printHelp(PRTSTREAM pStrm)
|
---|
216 | {
|
---|
217 | printFullCommandOrSubcommandHelp(g_enmCurCommand, g_fCurSubcommandScope, pStrm);
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * Display no subcommand error message and current command usage.
|
---|
223 | *
|
---|
224 | * @returns RTEXITCODE_SYNTAX.
|
---|
225 | */
|
---|
226 | RTEXITCODE errorNoSubcommand(void)
|
---|
227 | {
|
---|
228 | Assert(g_enmCurCommand != HELP_CMD_VBOXMANAGE_INVALID);
|
---|
229 | Assert(g_fCurSubcommandScope == RTMSGREFENTRYSTR_SCOPE_GLOBAL);
|
---|
230 |
|
---|
231 | return errorSyntax(Help::tr("No subcommand specified"));
|
---|
232 | }
|
---|
233 |
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Display unknown subcommand error message and current command usage.
|
---|
237 | *
|
---|
238 | * May show full command help instead if the subcommand is a common help option.
|
---|
239 | *
|
---|
240 | * @returns RTEXITCODE_SYNTAX, or RTEXITCODE_SUCCESS if common help option.
|
---|
241 | * @param pszSubcommand The name of the alleged subcommand.
|
---|
242 | */
|
---|
243 | RTEXITCODE errorUnknownSubcommand(const char *pszSubcommand)
|
---|
244 | {
|
---|
245 | Assert(g_enmCurCommand != HELP_CMD_VBOXMANAGE_INVALID);
|
---|
246 | Assert(g_fCurSubcommandScope == RTMSGREFENTRYSTR_SCOPE_GLOBAL);
|
---|
247 |
|
---|
248 | /* check if help was requested. */
|
---|
249 | if ( strcmp(pszSubcommand, "--help") == 0
|
---|
250 | || strcmp(pszSubcommand, "-h") == 0
|
---|
251 | || strcmp(pszSubcommand, "-?") == 0)
|
---|
252 | {
|
---|
253 | printFullCommandOrSubcommandHelp(g_enmCurCommand, g_fCurSubcommandScope, g_pStdOut);
|
---|
254 | return RTEXITCODE_SUCCESS;
|
---|
255 | }
|
---|
256 |
|
---|
257 | return errorSyntax(Help::tr("Unknown subcommand: %s"), pszSubcommand);
|
---|
258 | }
|
---|
259 |
|
---|
260 |
|
---|
261 | /**
|
---|
262 | * Display too many parameters error message and current command usage.
|
---|
263 | *
|
---|
264 | * May show full command help instead if the subcommand is a common help option.
|
---|
265 | *
|
---|
266 | * @returns RTEXITCODE_SYNTAX, or RTEXITCODE_SUCCESS if common help option.
|
---|
267 | * @param papszArgs The first unwanted parameter. Terminated by
|
---|
268 | * NULL entry.
|
---|
269 | */
|
---|
270 | RTEXITCODE errorTooManyParameters(char **papszArgs)
|
---|
271 | {
|
---|
272 | Assert(g_enmCurCommand != HELP_CMD_VBOXMANAGE_INVALID);
|
---|
273 | Assert(g_fCurSubcommandScope != RTMSGREFENTRYSTR_SCOPE_GLOBAL);
|
---|
274 |
|
---|
275 | /* check if help was requested. */
|
---|
276 | if (papszArgs)
|
---|
277 | {
|
---|
278 | for (uint32_t i = 0; papszArgs[i]; i++)
|
---|
279 | if ( strcmp(papszArgs[i], "--help") == 0
|
---|
280 | || strcmp(papszArgs[i], "-h") == 0
|
---|
281 | || strcmp(papszArgs[i], "-?") == 0)
|
---|
282 | {
|
---|
283 | printFullCommandOrSubcommandHelp(g_enmCurCommand, g_fCurSubcommandScope, g_pStdOut);
|
---|
284 | return RTEXITCODE_SUCCESS;
|
---|
285 | }
|
---|
286 | else if (!strcmp(papszArgs[i], "--"))
|
---|
287 | break;
|
---|
288 | }
|
---|
289 |
|
---|
290 | return errorSyntax(Help::tr("Too many parameters"));
|
---|
291 | }
|
---|
292 |
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * Display current (sub)command usage and the custom error message.
|
---|
296 | *
|
---|
297 | * @returns RTEXITCODE_SYNTAX.
|
---|
298 | * @param pszFormat Custom error message format string.
|
---|
299 | * @param va Format arguments.
|
---|
300 | */
|
---|
301 | RTEXITCODE errorSyntaxV(const char *pszFormat, va_list va)
|
---|
302 | {
|
---|
303 | Assert(g_enmCurCommand != HELP_CMD_VBOXMANAGE_INVALID);
|
---|
304 |
|
---|
305 | showLogo(g_pStdErr);
|
---|
306 |
|
---|
307 | va_list vaCopy;
|
---|
308 | va_copy(vaCopy, va);
|
---|
309 | RTMsgErrorV(pszFormat, vaCopy);
|
---|
310 | va_end(vaCopy);
|
---|
311 |
|
---|
312 | RTStrmPutCh(g_pStdErr, '\n');
|
---|
313 | if ( printBriefCommandOrSubcommandHelp(g_enmCurCommand, g_fCurSubcommandScope, g_pStdErr)
|
---|
314 | >= ERROR_REPEAT_AFTER_USAGE_LENGTH)
|
---|
315 | {
|
---|
316 | /* Usage was very long, repeat the error message. */
|
---|
317 | RTStrmPutCh(g_pStdErr, '\n');
|
---|
318 | RTMsgErrorV(pszFormat, va);
|
---|
319 | }
|
---|
320 | return RTEXITCODE_SYNTAX;
|
---|
321 | }
|
---|
322 |
|
---|
323 |
|
---|
324 | /**
|
---|
325 | * Display current (sub)command usage and the custom error message.
|
---|
326 | *
|
---|
327 | * @returns RTEXITCODE_SYNTAX.
|
---|
328 | * @param pszFormat Custom error message format string.
|
---|
329 | * @param ... Format arguments.
|
---|
330 | */
|
---|
331 | RTEXITCODE errorSyntax(const char *pszFormat, ...)
|
---|
332 | {
|
---|
333 | va_list va;
|
---|
334 | va_start(va, pszFormat);
|
---|
335 | RTEXITCODE rcExit = errorSyntaxV(pszFormat, va);
|
---|
336 | va_end(va);
|
---|
337 | return rcExit;
|
---|
338 | }
|
---|
339 |
|
---|
340 |
|
---|
341 | /**
|
---|
342 | * Display current (sub)command usage and the custom error message.
|
---|
343 | *
|
---|
344 | * @returns E_INVALIDARG
|
---|
345 | * @param pszFormat Custom error message format string.
|
---|
346 | * @param ... Format arguments.
|
---|
347 | */
|
---|
348 | HRESULT errorSyntaxHr(const char *pszFormat, ...)
|
---|
349 | {
|
---|
350 | va_list va;
|
---|
351 | va_start(va, pszFormat);
|
---|
352 | errorSyntaxV(pszFormat, va);
|
---|
353 | va_end(va);
|
---|
354 | return E_INVALIDARG;
|
---|
355 | }
|
---|
356 |
|
---|
357 |
|
---|
358 | /**
|
---|
359 | * Print an error message without the syntax stuff.
|
---|
360 | *
|
---|
361 | * @returns RTEXITCODE_SYNTAX.
|
---|
362 | */
|
---|
363 | RTEXITCODE errorArgument(const char *pszFormat, ...)
|
---|
364 | {
|
---|
365 | va_list args;
|
---|
366 | va_start(args, pszFormat);
|
---|
367 | RTMsgErrorV(pszFormat, args);
|
---|
368 | va_end(args);
|
---|
369 | return RTEXITCODE_SYNTAX;
|
---|
370 | }
|
---|
371 |
|
---|
372 |
|
---|
373 | /**
|
---|
374 | * Print an error message without the syntax stuff.
|
---|
375 | *
|
---|
376 | * @returns E_INVALIDARG.
|
---|
377 | */
|
---|
378 | HRESULT errorArgumentHr(const char *pszFormat, ...)
|
---|
379 | {
|
---|
380 | va_list args;
|
---|
381 | va_start(args, pszFormat);
|
---|
382 | RTMsgErrorV(pszFormat, args);
|
---|
383 | va_end(args);
|
---|
384 | return E_INVALIDARG;
|
---|
385 | }
|
---|
386 |
|
---|
387 |
|
---|
388 | /**
|
---|
389 | * Worker for errorGetOpt.
|
---|
390 | *
|
---|
391 | * @param rcGetOpt The RTGetOpt return value.
|
---|
392 | * @param pValueUnion The value union returned by RTGetOpt.
|
---|
393 | */
|
---|
394 | static void errorGetOptWorker(int rcGetOpt, union RTGETOPTUNION const *pValueUnion)
|
---|
395 | {
|
---|
396 | if (rcGetOpt == VINF_GETOPT_NOT_OPTION)
|
---|
397 | RTMsgError(Help::tr("Invalid parameter '%s'"), pValueUnion->psz);
|
---|
398 | else if (rcGetOpt > 0)
|
---|
399 | {
|
---|
400 | if (RT_C_IS_PRINT(rcGetOpt))
|
---|
401 | RTMsgError(Help::tr("Invalid option -%c"), rcGetOpt);
|
---|
402 | else
|
---|
403 | RTMsgError(Help::tr("Invalid option case %i"), rcGetOpt);
|
---|
404 | }
|
---|
405 | else if (rcGetOpt == VERR_GETOPT_UNKNOWN_OPTION)
|
---|
406 | RTMsgError(Help::tr("Unknown option: %s"), pValueUnion->psz);
|
---|
407 | else if (rcGetOpt == VERR_GETOPT_INVALID_ARGUMENT_FORMAT)
|
---|
408 | RTMsgError(Help::tr("Invalid argument format: %s"), pValueUnion->psz);
|
---|
409 | else if (pValueUnion->pDef)
|
---|
410 | RTMsgError("%s: %Rrs", pValueUnion->pDef->pszLong, rcGetOpt);
|
---|
411 | else
|
---|
412 | RTMsgError("%Rrs", rcGetOpt);
|
---|
413 | }
|
---|
414 |
|
---|
415 |
|
---|
416 | /**
|
---|
417 | * For use to deal with RTGetOptFetchValue failures.
|
---|
418 | *
|
---|
419 | * @retval RTEXITCODE_SYNTAX
|
---|
420 | * @param iValueNo The value number being fetched, counting the
|
---|
421 | * RTGetOpt value as zero and the first
|
---|
422 | * RTGetOptFetchValue call as one.
|
---|
423 | * @param pszOption The option being parsed.
|
---|
424 | * @param rcGetOptFetchValue The status returned by RTGetOptFetchValue.
|
---|
425 | * @param pValueUnion The value union returned by the fetch.
|
---|
426 | */
|
---|
427 | RTEXITCODE errorFetchValue(int iValueNo, const char *pszOption, int rcGetOptFetchValue, union RTGETOPTUNION const *pValueUnion)
|
---|
428 | {
|
---|
429 | Assert(g_enmCurCommand != HELP_CMD_VBOXMANAGE_INVALID);
|
---|
430 | showLogo(g_pStdErr);
|
---|
431 | if (rcGetOptFetchValue == VERR_GETOPT_REQUIRED_ARGUMENT_MISSING)
|
---|
432 | RTMsgError(Help::tr("Missing the %u%s value for option %s"),
|
---|
433 | iValueNo,
|
---|
434 | iValueNo == 1 ? Help::tr("st")
|
---|
435 | : iValueNo == 2 ? Help::tr("nd")
|
---|
436 | : iValueNo == 3 ? Help::tr("rd")
|
---|
437 | : Help::tr("th"),
|
---|
438 | pszOption);
|
---|
439 | else
|
---|
440 | errorGetOptWorker(rcGetOptFetchValue, pValueUnion);
|
---|
441 | return RTEXITCODE_SYNTAX;
|
---|
442 |
|
---|
443 | }
|
---|
444 |
|
---|
445 |
|
---|
446 | /**
|
---|
447 | * Handled an RTGetOpt error or common option.
|
---|
448 | *
|
---|
449 | * This implements the 'V' and 'h' cases. It reports appropriate syntax errors
|
---|
450 | * for other @a rcGetOpt values.
|
---|
451 | *
|
---|
452 | * @retval RTEXITCODE_SUCCESS if help or version request.
|
---|
453 | * @retval RTEXITCODE_SYNTAX if not help or version request.
|
---|
454 | * @param rcGetOpt The RTGetOpt return value.
|
---|
455 | * @param pValueUnion The value union returned by RTGetOpt.
|
---|
456 | */
|
---|
457 | RTEXITCODE errorGetOpt(int rcGetOpt, union RTGETOPTUNION const *pValueUnion)
|
---|
458 | {
|
---|
459 | Assert(g_enmCurCommand != HELP_CMD_VBOXMANAGE_INVALID);
|
---|
460 |
|
---|
461 | /*
|
---|
462 | * Check if it is an unhandled standard option.
|
---|
463 | */
|
---|
464 | if (rcGetOpt == 'V')
|
---|
465 | {
|
---|
466 | RTPrintf("%sr%d\n", VBOX_VERSION_STRING, RTBldCfgRevision());
|
---|
467 | return RTEXITCODE_SUCCESS;
|
---|
468 | }
|
---|
469 |
|
---|
470 | if (rcGetOpt == 'h')
|
---|
471 | {
|
---|
472 | printFullCommandOrSubcommandHelp(g_enmCurCommand, g_fCurSubcommandScope, g_pStdOut);
|
---|
473 | return RTEXITCODE_SUCCESS;
|
---|
474 | }
|
---|
475 |
|
---|
476 | /*
|
---|
477 | * We failed.
|
---|
478 | */
|
---|
479 | showLogo(g_pStdErr);
|
---|
480 | errorGetOptWorker(rcGetOpt, pValueUnion);
|
---|
481 | if ( printBriefCommandOrSubcommandHelp(g_enmCurCommand, g_fCurSubcommandScope, g_pStdErr)
|
---|
482 | >= ERROR_REPEAT_AFTER_USAGE_LENGTH)
|
---|
483 | {
|
---|
484 | /* Usage was very long, repeat the error message. */
|
---|
485 | RTStrmPutCh(g_pStdErr, '\n');
|
---|
486 | errorGetOptWorker(rcGetOpt, pValueUnion);
|
---|
487 | }
|
---|
488 | return RTEXITCODE_SYNTAX;
|
---|
489 | }
|
---|
490 |
|
---|
491 | #endif /* !VBOX_ONLY_DOCS */
|
---|
492 |
|
---|
493 |
|
---|
494 |
|
---|
495 | void showLogo(PRTSTREAM pStrm)
|
---|
496 | {
|
---|
497 | static bool s_fShown; /* show only once */
|
---|
498 |
|
---|
499 | if (!s_fShown)
|
---|
500 | {
|
---|
501 | RTStrmPrintf(pStrm, VBOX_PRODUCT " Command Line Management Interface Version "
|
---|
502 | VBOX_VERSION_STRING "\n"
|
---|
503 | "(C) 2005-" VBOX_C_YEAR " " VBOX_VENDOR "\n"
|
---|
504 | "All rights reserved.\n"
|
---|
505 | "\n");
|
---|
506 | s_fShown = true;
|
---|
507 | }
|
---|
508 | }
|
---|
509 |
|
---|
510 |
|
---|
511 |
|
---|
512 |
|
---|
513 | void printUsage(USAGECATEGORY enmCommand, uint64_t fSubcommandScope, PRTSTREAM pStrm)
|
---|
514 | {
|
---|
515 | bool fDumpOpts = false;
|
---|
516 | #ifdef RT_OS_LINUX
|
---|
517 | bool fLinux = true;
|
---|
518 | #else
|
---|
519 | bool fLinux = false;
|
---|
520 | #endif
|
---|
521 | #ifdef RT_OS_WINDOWS
|
---|
522 | bool fWin = true;
|
---|
523 | #else
|
---|
524 | bool fWin = false;
|
---|
525 | #endif
|
---|
526 | #ifdef RT_OS_SOLARIS
|
---|
527 | bool fSolaris = true;
|
---|
528 | #else
|
---|
529 | bool fSolaris = false;
|
---|
530 | #endif
|
---|
531 | #ifdef RT_OS_FREEBSD
|
---|
532 | bool fFreeBSD = true;
|
---|
533 | #else
|
---|
534 | bool fFreeBSD = false;
|
---|
535 | #endif
|
---|
536 | #ifdef RT_OS_DARWIN
|
---|
537 | bool fDarwin = true;
|
---|
538 | #else
|
---|
539 | bool fDarwin = false;
|
---|
540 | #endif
|
---|
541 | #ifdef VBOX_WITH_VBOXSDL
|
---|
542 | bool fVBoxSDL = true;
|
---|
543 | #else
|
---|
544 | bool fVBoxSDL = false;
|
---|
545 | #endif
|
---|
546 |
|
---|
547 | RT_NOREF(fSubcommandScope);
|
---|
548 |
|
---|
549 | Assert(enmCommand != USAGE_INVALID);
|
---|
550 | Assert(enmCommand != USAGE_S_NEWCMD);
|
---|
551 |
|
---|
552 | if (enmCommand == USAGE_S_DUMPOPTS)
|
---|
553 | {
|
---|
554 | fDumpOpts = true;
|
---|
555 | fLinux = true;
|
---|
556 | fWin = true;
|
---|
557 | fSolaris = true;
|
---|
558 | fFreeBSD = true;
|
---|
559 | fDarwin = true;
|
---|
560 | fVBoxSDL = true;
|
---|
561 | enmCommand = USAGE_S_ALL;
|
---|
562 | }
|
---|
563 |
|
---|
564 | RTStrmPrintf(pStrm,
|
---|
565 | Help::tr("Usage:\n"
|
---|
566 | "\n"));
|
---|
567 |
|
---|
568 | if (enmCommand == USAGE_S_ALL)
|
---|
569 | RTStrmPrintf(pStrm,
|
---|
570 | " VBoxManage [<general option>] <command>\n"
|
---|
571 | "\n"
|
---|
572 | "\n"
|
---|
573 | "General Options:\n"
|
---|
574 | "\n"
|
---|
575 | " [-V|--version] print version number and exit\n"
|
---|
576 | " [--dump-build-type] print build type and exit\n"
|
---|
577 | " [-q|--nologo] suppress the logo\n"
|
---|
578 | " [--settingspw <pw>] provide the settings password\n"
|
---|
579 | " [--settingspwfile <file>] provide a file containing the settings password\n"
|
---|
580 | " [@<response-file>] load arguments from the given response file (bourne style)\n"
|
---|
581 | "\n"
|
---|
582 | "\n"
|
---|
583 | "Commands:\n"
|
---|
584 | "\n");
|
---|
585 |
|
---|
586 | const char *pcszSep1 = " ";
|
---|
587 | const char *pcszSep2 = " ";
|
---|
588 | if (enmCommand != USAGE_S_ALL)
|
---|
589 | {
|
---|
590 | pcszSep1 = "VBoxManage";
|
---|
591 | pcszSep2 = "";
|
---|
592 | }
|
---|
593 |
|
---|
594 | #define SEP pcszSep1, pcszSep2
|
---|
595 |
|
---|
596 | if (enmCommand == USAGE_STORAGEATTACH || enmCommand == USAGE_S_ALL)
|
---|
597 | RTStrmPrintf(pStrm,
|
---|
598 | "%s storageattach %s <uuid|vmname>\n"
|
---|
599 | " --storagectl <name>\n"
|
---|
600 | " [--port <number>]\n"
|
---|
601 | " [--device <number>]\n"
|
---|
602 | " [--type dvddrive|hdd|fdd]\n"
|
---|
603 | " [--medium none|emptydrive|additions|\n"
|
---|
604 | " <uuid|filename>|host:<drive>|iscsi]\n"
|
---|
605 | " [--mtype normal|writethrough|immutable|shareable|\n"
|
---|
606 | " readonly|multiattach]\n"
|
---|
607 | " [--comment <text>]\n"
|
---|
608 | " [--setuuid <uuid>]\n"
|
---|
609 | " [--setparentuuid <uuid>]\n"
|
---|
610 | " [--passthrough on|off]\n"
|
---|
611 | " [--tempeject on|off]\n"
|
---|
612 | " [--nonrotational on|off]\n"
|
---|
613 | " [--discard on|off]\n"
|
---|
614 | " [--hotpluggable on|off]\n"
|
---|
615 | " [--bandwidthgroup <name>]\n"
|
---|
616 | " [--forceunmount]\n"
|
---|
617 | " [--server <name>|<ip>]\n"
|
---|
618 | " [--target <target>]\n"
|
---|
619 | " [--tport <port>]\n"
|
---|
620 | " [--lun <lun>]\n"
|
---|
621 | " [--encodedlun <lun>]\n"
|
---|
622 | " [--username <username>]\n"
|
---|
623 | " [--password <password>]\n"
|
---|
624 | " [--passwordfile <file>]\n"
|
---|
625 | " [--initiator <initiator>]\n"
|
---|
626 | " [--intnet]\n"
|
---|
627 | "\n", SEP);
|
---|
628 |
|
---|
629 | if (enmCommand == USAGE_MODIFYMEDIUM || enmCommand == USAGE_S_ALL)
|
---|
630 | RTStrmPrintf(pStrm,
|
---|
631 | "%s modifymedium %s [disk|dvd|floppy] <uuid|filename>\n"
|
---|
632 | " [--type normal|writethrough|immutable|shareable|\n"
|
---|
633 | " readonly|multiattach]\n"
|
---|
634 | " [--autoreset on|off]\n"
|
---|
635 | " [--property <name=[value]>]\n"
|
---|
636 | " [--compact]\n"
|
---|
637 | " [--resize <megabytes>|--resizebyte <bytes>]\n"
|
---|
638 | " [--move <path>]\n"
|
---|
639 | " [--setlocation <path>]\n"
|
---|
640 | " [--description <description string>]"
|
---|
641 | "\n", SEP);
|
---|
642 |
|
---|
643 | #if defined(VBOX_WITH_NETFLT)
|
---|
644 | if (enmCommand == USAGE_HOSTONLYIFS || enmCommand == USAGE_S_ALL)
|
---|
645 | {
|
---|
646 | RTStrmPrintf(pStrm,
|
---|
647 | "%s hostonlyif %s ipconfig <name>\n"
|
---|
648 | " [--dhcp |\n"
|
---|
649 | " --ip <ipv4> [--netmask <ipv4> (def:255.255.255.0)]|\n"
|
---|
650 | " --ipv6 <ipv6> [--netmasklengthv6 <N> (def:64)]]", SEP);
|
---|
651 | # if !defined(RT_OS_SOLARIS) || defined(VBOX_ONLY_DOCS)
|
---|
652 | RTStrmPrintf(pStrm,
|
---|
653 | " |\n"
|
---|
654 | " create |\n"
|
---|
655 | " remove <name>\n");
|
---|
656 | # else
|
---|
657 | RTStrmPrintf(pStrm,
|
---|
658 | "\n");
|
---|
659 | # endif
|
---|
660 | RTStrmPrintf(pStrm,
|
---|
661 | "\n");
|
---|
662 | }
|
---|
663 | #endif
|
---|
664 |
|
---|
665 | if (enmCommand == USAGE_USBDEVSOURCE || enmCommand == USAGE_S_ALL)
|
---|
666 | {
|
---|
667 | RTStrmPrintf(pStrm,
|
---|
668 | "%s usbdevsource %s add <source name>\n"
|
---|
669 | " --backend <backend>\n"
|
---|
670 | " --address <address>\n"
|
---|
671 | "%s usbdevsource %s remove <source name>\n"
|
---|
672 | "\n", SEP, SEP);
|
---|
673 | }
|
---|
674 |
|
---|
675 | #ifndef VBOX_ONLY_DOCS /* Converted to man page, not needed. */
|
---|
676 | if (enmCommand == USAGE_S_ALL)
|
---|
677 | {
|
---|
678 | uint32_t cPendingBlankLines = 0;
|
---|
679 | PCHELP_LANG_ENTRY_T pHelpLangEntry = ASMAtomicUoReadPtrT(&g_pHelpLangEntry, PCHELP_LANG_ENTRY_T);
|
---|
680 | uint32_t const cHelpEntries = *pHelpLangEntry->pcHelpEntries;
|
---|
681 | for (uint32_t i = 0; i < cHelpEntries; i++)
|
---|
682 | {
|
---|
683 | PCRTMSGREFENTRY pHelp = pHelpLangEntry->papHelpEntries[i];
|
---|
684 |
|
---|
685 | while (cPendingBlankLines-- > 0)
|
---|
686 | RTStrmPutCh(pStrm, '\n');
|
---|
687 |
|
---|
688 | char szFirstChar[8];
|
---|
689 | RTStrmPrintf(pStrm, " %s%s:\n", szFirstChar, captialize(pHelp->pszBrief, szFirstChar));
|
---|
690 |
|
---|
691 | cPendingBlankLines = 0;
|
---|
692 | RTMsgRefEntryPrintStringTable(pStrm, &pHelp->Synopsis, RTMSGREFENTRYSTR_SCOPE_GLOBAL,
|
---|
693 | &cPendingBlankLines, NULL /*pcLinesWritten*/);
|
---|
694 | cPendingBlankLines = RT_MAX(cPendingBlankLines, 1);
|
---|
695 | }
|
---|
696 | }
|
---|
697 | #endif
|
---|
698 | }
|
---|
699 |
|
---|
700 | /**
|
---|
701 | * Print a usage synopsis and the syntax error message.
|
---|
702 | * @returns RTEXITCODE_SYNTAX.
|
---|
703 | */
|
---|
704 | RTEXITCODE errorSyntax(USAGECATEGORY enmCommand, const char *pszFormat, ...)
|
---|
705 | {
|
---|
706 | va_list args;
|
---|
707 | showLogo(g_pStdErr); // show logo even if suppressed
|
---|
708 | #ifndef VBOX_ONLY_DOCS
|
---|
709 | if (g_fInternalMode)
|
---|
710 | printUsageInternal(enmCommand, g_pStdErr);
|
---|
711 | else if (g_enmCurCommand == HELP_CMD_VBOXMANAGE_INVALID)
|
---|
712 | printUsage(enmCommand, RTMSGREFENTRYSTR_SCOPE_GLOBAL, g_pStdErr);
|
---|
713 | else
|
---|
714 | printUsage(g_pStdErr);
|
---|
715 | #else
|
---|
716 | RT_NOREF_PV(enmCommand);
|
---|
717 | #endif
|
---|
718 | va_start(args, pszFormat);
|
---|
719 | RTStrmPrintf(g_pStdErr, Help::tr("\nSyntax error: %N\n"), pszFormat, &args);
|
---|
720 | va_end(args);
|
---|
721 | return RTEXITCODE_SYNTAX;
|
---|
722 | }
|
---|
723 |
|
---|
724 | /**
|
---|
725 | * Print a usage synopsis and the syntax error message.
|
---|
726 | * @returns RTEXITCODE_SYNTAX.
|
---|
727 | */
|
---|
728 | RTEXITCODE errorSyntaxEx(USAGECATEGORY enmCommand, uint64_t fSubcommandScope, const char *pszFormat, ...)
|
---|
729 | {
|
---|
730 | va_list args;
|
---|
731 | showLogo(g_pStdErr); // show logo even if suppressed
|
---|
732 | #ifndef VBOX_ONLY_DOCS
|
---|
733 | if (g_fInternalMode)
|
---|
734 | printUsageInternal(enmCommand, g_pStdErr);
|
---|
735 | else if (g_enmCurCommand == HELP_CMD_VBOXMANAGE_INVALID)
|
---|
736 | printUsage(enmCommand, fSubcommandScope, g_pStdErr);
|
---|
737 | else
|
---|
738 | printUsage(g_pStdErr);
|
---|
739 | #else
|
---|
740 | RT_NOREF2(enmCommand, fSubcommandScope);
|
---|
741 | #endif
|
---|
742 | va_start(args, pszFormat);
|
---|
743 | RTStrmPrintf(g_pStdErr, Help::tr("\nSyntax error: %N\n"), pszFormat, &args);
|
---|
744 | va_end(args);
|
---|
745 | return RTEXITCODE_SYNTAX;
|
---|
746 | }
|
---|
747 |
|
---|
748 | /**
|
---|
749 | * errorSyntax for RTGetOpt users.
|
---|
750 | *
|
---|
751 | * @returns RTEXITCODE_SYNTAX.
|
---|
752 | *
|
---|
753 | * @param enmCommand The command.
|
---|
754 | * @param fSubcommandScope The subcommand scope, REFENTRYSTR_SCOPE_GLOBAL
|
---|
755 | * for all.
|
---|
756 | * @param rc The RTGetOpt return code.
|
---|
757 | * @param pValueUnion The value union.
|
---|
758 | */
|
---|
759 | RTEXITCODE errorGetOptEx(USAGECATEGORY enmCommand, uint64_t fSubcommandScope, int rc, union RTGETOPTUNION const *pValueUnion)
|
---|
760 | {
|
---|
761 | /*
|
---|
762 | * Check if it is an unhandled standard option.
|
---|
763 | */
|
---|
764 | #ifndef VBOX_ONLY_DOCS
|
---|
765 | if (rc == 'V')
|
---|
766 | {
|
---|
767 | RTPrintf("%sr%d\n", VBOX_VERSION_STRING, RTBldCfgRevision());
|
---|
768 | return RTEXITCODE_SUCCESS;
|
---|
769 | }
|
---|
770 | #endif
|
---|
771 |
|
---|
772 | if (rc == 'h')
|
---|
773 | {
|
---|
774 | showLogo(g_pStdErr);
|
---|
775 | #ifndef VBOX_ONLY_DOCS
|
---|
776 | if (g_fInternalMode)
|
---|
777 | printUsageInternal(enmCommand, g_pStdOut);
|
---|
778 | else if (g_enmCurCommand == HELP_CMD_VBOXMANAGE_INVALID)
|
---|
779 | printUsage(enmCommand, fSubcommandScope, g_pStdOut);
|
---|
780 | else
|
---|
781 | printUsage(g_pStdErr);
|
---|
782 | #endif
|
---|
783 | return RTEXITCODE_SUCCESS;
|
---|
784 | }
|
---|
785 |
|
---|
786 | /*
|
---|
787 | * General failure.
|
---|
788 | */
|
---|
789 | showLogo(g_pStdErr); // show logo even if suppressed
|
---|
790 | #ifndef VBOX_ONLY_DOCS
|
---|
791 | if (g_fInternalMode)
|
---|
792 | printUsageInternal(enmCommand, g_pStdErr);
|
---|
793 | else if (g_enmCurCommand == HELP_CMD_VBOXMANAGE_INVALID)
|
---|
794 | printUsage(enmCommand, fSubcommandScope, g_pStdErr);
|
---|
795 | else
|
---|
796 | printUsage(g_pStdErr);
|
---|
797 | #else
|
---|
798 | RT_NOREF2(enmCommand, fSubcommandScope);
|
---|
799 | #endif
|
---|
800 |
|
---|
801 | if (rc == VINF_GETOPT_NOT_OPTION)
|
---|
802 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, Help::tr("Invalid parameter '%s'"), pValueUnion->psz);
|
---|
803 | if (rc > 0)
|
---|
804 | {
|
---|
805 | if (RT_C_IS_PRINT(rc))
|
---|
806 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, Help::tr("Invalid option -%c"), rc);
|
---|
807 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, Help::tr("Invalid option case %i"), rc);
|
---|
808 | }
|
---|
809 | if (rc == VERR_GETOPT_UNKNOWN_OPTION)
|
---|
810 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, Help::tr("Unknown option: %s"), pValueUnion->psz);
|
---|
811 | if (rc == VERR_GETOPT_INVALID_ARGUMENT_FORMAT)
|
---|
812 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, Help::tr("Invalid argument format: %s"), pValueUnion->psz);
|
---|
813 | if (pValueUnion->pDef)
|
---|
814 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "%s: %Rrs", pValueUnion->pDef->pszLong, rc);
|
---|
815 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "%Rrs", rc);
|
---|
816 | }
|
---|
817 |
|
---|
818 |
|
---|
819 | /**
|
---|
820 | * errorSyntax for RTGetOpt users.
|
---|
821 | *
|
---|
822 | * @returns RTEXITCODE_SYNTAX.
|
---|
823 | *
|
---|
824 | * @param enmCommand The command.
|
---|
825 | * @param rc The RTGetOpt return code.
|
---|
826 | * @param pValueUnion The value union.
|
---|
827 | */
|
---|
828 | RTEXITCODE errorGetOpt(USAGECATEGORY enmCommand, int rc, union RTGETOPTUNION const *pValueUnion)
|
---|
829 | {
|
---|
830 | return errorGetOptEx(enmCommand, RTMSGREFENTRYSTR_SCOPE_GLOBAL, rc, pValueUnion);
|
---|
831 | }
|
---|
832 |
|
---|