VBoxControl.cpp revision 84c5015eccf5ba071c02e51da99b4d7593f690fe
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * VBoxControl - Guest Additions Command Line Management Interface
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * Copyright (C) 2007 Sun Microsystems, Inc.
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * available from http://www.virtualbox.org. This file is free software;
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * you can redistribute it and/or modify it under the terms of the GNU
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * General Public License (GPL) as published by the Free Software
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * additional information or have any questions.
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync/*******************************************************************************
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync* Header Files *
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync*******************************************************************************/
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync/*******************************************************************************
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync* Global Variables *
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync*******************************************************************************/
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync/** The program name (derived from argv[0]). */
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync/** The current verbosity level. */
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * Displays the program usage message.
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * @param u64Which
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync/** Helper function */
c785dbab313731d1f4662b4684c0808cc14703dbvboxsyncstatic void doUsage(char const *line, char const *name = "", char const *command = "")
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync RTPrintf("%s %-*s%s", name, 30 - strlen(name), command, line);
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync/** Enumerate the different parts of the usage we might want to print out */
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync RTPrintf("%s [-v|--version] print version number and exit\n", g_pszProgName);
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync RTPrintf("%s --nologo ... suppress the logo\n\n", g_pszProgName);
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync if ((GET_GUEST_PROP == eWhich) || (USAGE_ALL == eWhich))
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync doUsage("<key>\n", g_pszProgName, "getguestproperty");
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync if ((SET_GUEST_PROP == eWhich) || (USAGE_ALL == eWhich))
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync doUsage("<key> [<value>] (no value deletes key)\n", g_pszProgName, "setguestproperty");
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * Displays an error message.
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * @param pszFormat The message text.
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * @param ... Format arguments.
c785dbab313731d1f4662b4684c0808cc14703dbvboxsyncstatic void VBoxControlError(const char *pszFormat, ...)
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync // RTStrmPrintf(g_pStdErr, "%s: error: ", g_pszProgName);
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * Retrieves a value from the host/guest configuration registry.
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * This is accessed through the "VBoxSharedInfoSvc" HGCM service.
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * @returns 0 on success, 1 on failure
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * @param key (string) the key which the value is stored under.
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync using namespace svcInfo;
12767477bc2dbc7815e4784576a15c990f5590d3vboxsync VBoxControlError("Failed to convert the key name to Utf8, error %Rrc\n", rc);
12767477bc2dbc7815e4784576a15c990f5590d3vboxsync VBoxControlError("Failed to connect to the guest property service, error %Rrc\n", rc);
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync rc = VbglR3InfoSvcReadKey(u32ClientID, argv[0], szValue, sizeof(szValue), NULL);
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync VBoxControlError("Failed to retrieve the property value, error %Rrc\n", rc);
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * Writes a value to the host/guest configuration registry.
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * This is accessed through the "VBoxSharedInfoSvc" HGCM service.
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * @returns 0 on success, 1 on failure
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * @param key (string) the key which the value is stored under.
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * @param value (string) the value to write. If empty, the key will be
12767477bc2dbc7815e4784576a15c990f5590d3vboxsync VBoxControlError("Failed to convert the key name to Utf8, error %Rrc\n", rc);
12767477bc2dbc7815e4784576a15c990f5590d3vboxsync VBoxControlError("Failed to convert the key value to Utf8, error %Rrc\n", rc);
12767477bc2dbc7815e4784576a15c990f5590d3vboxsync VBoxControlError("Failed to connect to the host/guest registry service, error %Rrc\n", rc);
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync rc = VbglR3InfoSvcWriteKey(u32ClientID, argv[0], pszValue);
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync VBoxControlError("Failed to store the property value, error %Rrc\n", rc);
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync/** command handler type */
c785dbab313731d1f4662b4684c0808cc14703dbvboxsynctypedef DECLCALLBACK(int) FNHANDLER(int argc, char *argv[]);
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync/** The table of all registered command handlers. */
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync const char *command;
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync/** Main function */
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync /** The application's global return code */
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync /** An IPRT return code for local use */
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync /** The index of the command line argument we are currently processing */
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync /** Should we show the logo text? */
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync bool showlogo = true;
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync /** Should we print the usage after the logo? For the --help switch. */
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync bool dohelp = false;
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync /** Will we be executing a command or just printing information? */
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync bool onlyinfo = false;
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * Start by handling command line switches
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync /** Are we finished with handling switches? */
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync bool done = false;
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync /* Print version number, and do nothing else. */
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync RTPrintf("%sr%d\n", VBOX_VERSION_STRING, VBoxSVNRev ());
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync /* We have found an argument which isn't a switch. Exit to the
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * command processing bit. */
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * Find the application name, show our logo if the user hasn't suppressed it,
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * and show the usage if the user asked us to
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync RTPrintf("VirtualBox Guest Additions Command Line Management Interface Version "
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync "(C) 2008 Sun Microsystems, Inc.\n"
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync "All rights reserved\n\n");
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * Do global initialisation for the programme if we will be handling a command
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync VBoxControlError("Failed to initialise the VirtualBox runtime - error %Rrc\n", rrc);
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync VBoxControlError("Could not contact the host system. Make sure that you are running this\n"
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync "application inside a VirtualBox guest system, and that you have sufficient\n"
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync "user permissions.\n");
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * Now look for an actual command in the argument list and handle it.
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync /** Is next parameter a known command? */
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync bool found = false;
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync /** And if so, what is its position in the table? */
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync unsigned index = 0;
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync if (0 == strcmp(argv[iArg], g_commandHandlers[index].command))
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync rc = g_commandHandlers[index].handler(argc - iArg - 1, argv + iArg + 1);
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync /* The user didn't specify a command. */
c785dbab313731d1f4662b4684c0808cc14703dbvboxsync * And exit, returning the status