Changeset 33764 in vbox for trunk/src/VBox/Frontends/VBoxManage
- Timestamp:
- Nov 4, 2010 1:50:21 PM (14 years ago)
- Location:
- trunk/src/VBox/Frontends/VBoxManage
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VBoxManage/VBoxManage.cpp
r33556 r33764 209 209 RTR3Init(); 210 210 211 /* 212 * Parse the global options 213 */ 211 214 bool fShowLogo = false; 212 215 bool fShowHelp = false; … … 214 217 int iCmdArg; 215 218 216 /* global options */217 219 for (int i = 1; i < argc || argc <= iCmd; i++) 218 220 { … … 275 277 276 278 277 #ifdef VBOX_ONLY_DOCS 278 int rc = 0; 279 #else /* !VBOX_ONLY_DOCS */ 279 #ifndef VBOX_ONLY_DOCS 280 /* 281 * Initialize COM. 282 */ 280 283 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!"); 289 287 290 288 /* 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.) 294 293 */ 295 294 for (int i = iCmdArg; i < argc; i++) 296 295 { 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; 301 300 else 302 301 /* Conversion was not possible,probably due to invalid characters. … … 305 304 } 306 305 306 RTEXITCODE rcExit = RTEXITCODE_FAILURE; 307 307 do 308 308 { 309 /////////////////////////////////////////////////////////////////////////// 309 310 // 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!"); 341 330 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 }, 388 389 #ifdef VBOX_WITH_GUEST_PROPS 389 { "guestproperty", USAGE_GUESTPROPERTY, handleGuestProperty },390 { "guestproperty", USAGE_GUESTPROPERTY, handleGuestProperty }, 390 391 #endif 391 392 #ifdef VBOX_WITH_GUEST_CONTROL 392 { "guestcontrol", USAGE_GUESTCONTROL, handleGuestControl },393 { "guestcontrol", USAGE_GUESTCONTROL, handleGuestControl }, 393 394 #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 }, 397 398 #ifdef VBOX_WITH_NETFLT 398 { "hostonlyif", USAGE_HOSTONLYIFS, handleHostonlyIf },399 { "hostonlyif", USAGE_HOSTONLYIFS, handleHostonlyIf }, 399 400 #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 436 439 // end "all-stuff" scope 437 /////////////////////////////////////////////////////////////////////////// /440 /////////////////////////////////////////////////////////////////////////// 438 441 } while (0); 439 442 440 443 com::Shutdown(); 441 #endif /* !VBOX_ONLY_DOCS */442 444 443 445 /* … … 445 447 */ 446 448 for (int i = iCmdArg; i < argc; i++) 449 { 447 450 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 */ 450 458 } -
trunk/src/VBox/Frontends/VBoxManage/VBoxManage.h
r33556 r33764 144 144 /* VBoxManageHelp.cpp */ 145 145 void printUsage(USAGECATEGORY u64Cmd, PRTSTREAM pStrm); 146 interrorSyntax(USAGECATEGORY u64Cmd, const char *pszFormat, ...);147 interrorGetOpt(USAGECATEGORY u64Cmd, int rc, union RTGETOPTUNION const *pValueUnion);148 interrorArgument(const char *pszFormat, ...);146 RTEXITCODE errorSyntax(USAGECATEGORY u64Cmd, const char *pszFormat, ...); 147 RTEXITCODE errorGetOpt(USAGECATEGORY u64Cmd, int rc, union RTGETOPTUNION const *pValueUnion); 148 RTEXITCODE errorArgument(const char *pszFormat, ...); 149 149 150 150 void printUsageInternal(USAGECATEGORY u64Cmd, PRTSTREAM pStrm); … … 211 211 int handleVMStatistics(HandlerArg *a); 212 212 int handleVRDE(HandlerArg *a); 213 int handleExtPack(HandlerArg *a); 213 214 214 215 /* VBoxManageDisk.cpp */ … … 216 217 int handleModifyHardDisk(HandlerArg *a); 217 218 int handleCloneHardDisk(HandlerArg *a); 218 inthandleConvertFromRaw(int argc, char *argv[]);219 RTEXITCODE handleConvertFromRaw(int argc, char *argv[]); 219 220 int handleAddiSCSIDisk(HandlerArg *a); 220 221 int handleShowHardDiskInfo(HandlerArg *a); -
trunk/src/VBox/Frontends/VBoxManage/VBoxManageDisk.cpp
r33567 r33764 785 785 }; 786 786 787 inthandleConvertFromRaw(int argc, char *argv[])787 RTEXITCODE handleConvertFromRaw(int argc, char *argv[]) 788 788 { 789 789 int rc = VINF_SUCCESS; … … 952 952 RTFileClose(File); 953 953 954 return RT_ FAILURE(rc);954 return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE; 955 955 } 956 956 -
trunk/src/VBox/Frontends/VBoxManage/VBoxManageHelp.cpp
r33590 r33764 731 731 /** 732 732 * Print a usage synopsis and the syntax error message. 733 * @returns RTEXITCODE_SYNTAX. 733 734 */ 734 interrorSyntax(USAGECATEGORY u64Cmd, const char *pszFormat, ...)735 RTEXITCODE errorSyntax(USAGECATEGORY u64Cmd, const char *pszFormat, ...) 735 736 { 736 737 va_list args; … … 745 746 RTStrmPrintf(g_pStdErr, "\nSyntax error: %N\n", pszFormat, &args); 746 747 va_end(args); 747 return 1;748 return RTEXITCODE_SYNTAX; 748 749 } 749 750 … … 751 752 * errorSyntax for RTGetOpt users. 752 753 * 753 * @returns 1.754 * @returns RTEXITCODE_SYNTAX. 754 755 * 755 756 * @param fUsageCategory The usage category of the command. … … 757 758 * @param pValueUnion The value union. 758 759 */ 759 interrorGetOpt(USAGECATEGORY fUsageCategory, int rc, union RTGETOPTUNION const *pValueUnion)760 RTEXITCODE errorGetOpt(USAGECATEGORY fUsageCategory, int rc, union RTGETOPTUNION const *pValueUnion) 760 761 { 761 762 showLogo(g_pStdErr); // show logo even if suppressed … … 768 769 769 770 if (rc == VINF_GETOPT_NOT_OPTION) 770 return RTMsgError ("Invalid parameter '%s'", pValueUnion->psz);771 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Invalid parameter '%s'", pValueUnion->psz); 771 772 if (rc > 0) 772 773 { 773 774 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); 776 777 } 777 778 if (rc == VERR_GETOPT_UNKNOWN_OPTION) 778 return RTMsgError ("Unknown option: %s", pValueUnion->psz);779 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Unknown option: %s", pValueUnion->psz); 779 780 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); 782 783 } 783 784 784 785 /** 785 786 * Print an error message without the syntax stuff. 787 * 788 * @returns RTEXITCODE_SYNTAX. 786 789 */ 787 interrorArgument(const char *pszFormat, ...)790 RTEXITCODE errorArgument(const char *pszFormat, ...) 788 791 { 789 792 va_list args; … … 791 794 RTMsgErrorV(pszFormat, args); 792 795 va_end(args); 793 return 1;796 return RTEXITCODE_SYNTAX; 794 797 }
Note:
See TracChangeset
for help on using the changeset viewer.