Index: trunk/src/VBox/Additions/common/VBoxControl/VBoxControl.cpp
===================================================================
--- trunk/src/VBox/Additions/common/VBoxControl/VBoxControl.cpp	(revision 32200)
+++ trunk/src/VBox/Additions/common/VBoxControl/VBoxControl.cpp	(revision 32201)
@@ -24,4 +24,5 @@
 #include <iprt/initterm.h>
 #include <iprt/mem.h>
+#include <iprt/message.h>
 #include <iprt/path.h>
 #include <iprt/string.h>
@@ -513,10 +514,10 @@
 }
 
-int handleSetVideoMode(int argc, char *argv[])
+static RTEXITCODE handleSetVideoMode(int argc, char *argv[])
 {
     if (argc != 3 && argc != 4)
     {
         usage(SET_VIDEO_MODE);
-        return 1;
+        return RTEXITCODE_FAILURE;
     }
 
@@ -550,9 +551,11 @@
             RTPrintf("done.\n");
         }
-        else VBoxControlError("Error retrieving API for display change!");
-    }
-    else VBoxControlError("Error retrieving handle to user32.dll!");
-
-    return 0;
+        else
+            VBoxControlError("Error retrieving API for display change!");
+    }
+    else
+        VBoxControlError("Error retrieving handle to user32.dll!");
+
+    return RTEXITCODE_SUCCESS;
 }
 
@@ -593,5 +596,5 @@
 }
 
-int handleGetVideoAcceleration(int argc, char *argv[])
+static RTEXITCODE handleGetVideoAcceleration(int argc, char *argv[])
 {
     ULONG status;
@@ -611,8 +614,8 @@
         RegCloseKey(hkeyVideo);
     }
-    return 0;
-}
-
-int handleSetVideoAcceleration(int argc, char *argv[])
+    return RTEXITCODE_SUCCESS;
+}
+
+static RTEXITCODE handleSetVideoAcceleration(int argc, char *argv[])
 {
     ULONG status;
@@ -625,5 +628,5 @@
     {
         usage(SET_VIDEO_ACCEL);
-        return 1;
+        return RTEXITCODE_FAILURE;
     }
 
@@ -643,5 +646,5 @@
         RegCloseKey(hkeyVideo);
     }
-    return 0;
+    return RTEXITCODE_SUCCESS;
 }
 
@@ -755,10 +758,10 @@
 }
 
-int handleListCustomModes(int argc, char *argv[])
+static RTEXITCODE handleListCustomModes(int argc, char *argv[])
 {
     if (argc != 0)
     {
         usage(LIST_CUST_MODES);
-        return 1;
+        return RTEXITCODE_FAILURE;
     }
 
@@ -780,13 +783,13 @@
         RegCloseKey(hkeyVideo);
     }
-    return 0;
-}
-
-int handleAddCustomMode(int argc, char *argv[])
+    return RTEXITCODE_SUCCESS;
+}
+
+static RTEXITCODE handleAddCustomMode(int argc, char *argv[])
 {
     if (argc != 3)
     {
         usage(ADD_CUST_MODE);
-        return 1;
+        return RTEXITCODE_FAILURE;
     }
 
@@ -803,5 +806,5 @@
     {
         VBoxControlError("Error: invalid mode specified!\n");
-        return 1;
+        return RTEXITCODE_FAILURE;
     }
 
@@ -841,13 +844,13 @@
         RegCloseKey(hkeyVideo);
     }
-    return 0;
-}
-
-int handleRemoveCustomMode(int argc, char *argv[])
+    return RTEXITCODE_SUCCESS;
+}
+
+static RTEXITCODE handleRemoveCustomMode(int argc, char *argv[])
 {
     if (argc != 3)
     {
         usage(REMOVE_CUST_MODE);
-        return 1;
+        return RTEXITCODE_FAILURE;
     }
 
@@ -877,5 +880,5 @@
     }
 
-    return 0;
+    return RTEXITCODE_SUCCESS;
 }
 
@@ -887,8 +890,8 @@
  * This is accessed through the "VBoxGuestPropSvc" HGCM service.
  *
- * @returns 0 on success, 1 on failure
+ * @returns Command exit code.
  * @note see the command line API description for parameters
  */
-int getGuestProperty(int argc, char **argv)
+static RTEXITCODE getGuestProperty(int argc, char **argv)
 {
     using namespace guestProp;
@@ -903,5 +906,5 @@
     {
         usage(GUEST_PROP);
-        return 1;
+        return RTEXITCODE_FAILURE;
     }
 
@@ -976,5 +979,5 @@
         VbglR3GuestPropDisconnect(u32ClientId);
     RTMemFree(pvBuf);
-    return RT_SUCCESS(rc) ? 0 : 1;
+    return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
 }
 
@@ -984,8 +987,8 @@
  * This is accessed through the "VBoxGuestPropSvc" HGCM service.
  *
- * @returns 0 on success, 1 on failure
+ * @returns Command exit code.
  * @note see the command line API description for parameters
  */
-static int setGuestProperty(int argc, char *argv[])
+static RTEXITCODE setGuestProperty(int argc, char *argv[])
 {
     /*
@@ -1016,5 +1019,5 @@
     {
         usage(GUEST_PROP);
-        return 1;
+        return RTEXITCODE_FAILURE;
     }
     /* This is always needed. */
@@ -1041,5 +1044,5 @@
     if (u32ClientId != 0)
         VbglR3GuestPropDisconnect(u32ClientId);
-    return RT_SUCCESS(rc) ? 0 : 1;
+    return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
 }
 
@@ -1049,8 +1052,8 @@
  * This is accessed through the "VBoxGuestPropSvc" HGCM service.
  *
- * @returns 0 on success, 1 on failure
+ * @returns Command exit code.
  * @note see the command line API description for parameters
  */
-static int enumGuestProperty(int argc, char *argv[])
+static RTEXITCODE enumGuestProperty(int argc, char *argv[])
 {
     /*
@@ -1070,5 +1073,5 @@
     {
         usage(GUEST_PROP);
-        return 1;
+        return RTEXITCODE_FAILURE;
     }
 
@@ -1108,5 +1111,5 @@
     else
         VBoxControlError("Failed to connect to the guest property service! Error: %Rrc\n", rc);
-    return RT_SUCCESS(rc) ? 0 : 1;
+    return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
 }
 
@@ -1116,8 +1119,8 @@
  * This is accessed through the "VBoxGuestPropSvc" HGCM service.
  *
- * @returns 0 on success, 1 on failure
+ * @returns Command exit code.
  * @note see the command line API description for parameters
  */
-int waitGuestProperty(int argc, char **argv)
+static RTEXITCODE waitGuestProperty(int argc, char **argv)
 {
     using namespace guestProp;
@@ -1163,5 +1166,5 @@
     {
         usage(GUEST_PROP);
-        return 1;
+        return RTEXITCODE_FAILURE;
     }
 
@@ -1243,5 +1246,5 @@
         VbglR3GuestPropDisconnect(u32ClientId);
     RTMemFree(pvBuf);
-    return RT_SUCCESS(rc) ? 0 : 1;
+    return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
 }
 
@@ -1254,22 +1257,22 @@
  * @note see the command line API description for parameters
  */
-static int handleGuestProperty(int argc, char *argv[])
+static RTEXITCODE handleGuestProperty(int argc, char *argv[])
 {
     if (0 == argc)
     {
         usage(GUEST_PROP);
-        return 1;
-    }
-    if (0 == strcmp(argv[0], "get"))
+        return RTEXITCODE_FAILURE;
+    }
+    if (!strcmp(argv[0], "get"))
         return getGuestProperty(argc - 1, argv + 1);
-    else if (0 == strcmp(argv[0], "set"))
+    else if (!strcmp(argv[0], "set"))
         return setGuestProperty(argc - 1, argv + 1);
-    else if (0 == strcmp(argv[0], "enumerate"))
+    else if (!strcmp(argv[0], "enumerate"))
         return enumGuestProperty(argc - 1, argv + 1);
-    else if (0 == strcmp(argv[0], "wait"))
+    else if (!strcmp(argv[0], "wait"))
         return waitGuestProperty(argc - 1, argv + 1);
     /* else */
     usage(GUEST_PROP);
-    return 1;
+    return RTEXITCODE_FAILURE;
 }
 #endif
@@ -1279,5 +1282,5 @@
  * Lists the Shared Folders provided by the host.
  */
-int listSharedFolders(int argc, char **argv)
+static RTEXITCODE listSharedFolders(int argc, char **argv)
 {
     bool usageOK = true;
@@ -1285,10 +1288,7 @@
     if (argc == 1)
     {
-        if (   strcmp(argv[0], "-automount")  == 0
-            || strcmp(argv[0], "--automount") == 0
-           )
-        {
+        if (   !strcmp(argv[0], "-automount")
+            || !strcmp(argv[0], "--automount"))
             fOnlyShowAutoMount = true;
-        }
         else
             usageOK = false;
@@ -1300,5 +1300,5 @@
     {
         usage(GUEST_SHAREDFOLDERS);
-        return 1;
+        return RTEXITCODE_FAILURE;
     }
 
@@ -1341,5 +1341,5 @@
         VbglR3SharedFolderDisconnect(u32ClientId);
     }
-    return RT_SUCCESS(rc) ? 0 : 1;
+    return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
 }
 
@@ -1350,21 +1350,21 @@
  * @note see the command line API description for parameters
  */
-static int handleSharedFolder(int argc, char *argv[])
+static RTEXITCODE handleSharedFolder(int argc, char *argv[])
 {
     if (0 == argc)
     {
         usage(GUEST_SHAREDFOLDERS);
-        return 1;
-    }
-    if (0 == strcmp(argv[0], "list"))
+        return RTEXITCODE_FAILURE;
+    }
+    if (!strcmp(argv[0], "list"))
         return listSharedFolders(argc - 1, argv + 1);
     /* else */
     usage(GUEST_SHAREDFOLDERS);
-    return 1;
+    return RTEXITCODE_FAILURE;
 }
 #endif
 
 /** command handler type */
-typedef DECLCALLBACK(int) FNHANDLER(int argc, char *argv[]);
+typedef DECLCALLBACK(RTEXITCODE) FNHANDLER(int argc, char *argv[]);
 typedef FNHANDLER *PFNHANDLER;
 
@@ -1372,23 +1372,23 @@
 struct COMMANDHANDLER
 {
-    const char *command;
-    PFNHANDLER handler;
-} g_commandHandlers[] =
+    const char *pszCommand;
+    PFNHANDLER pfnHandler;
+} g_aCommandHandlers[] =
 {
 #if defined(RT_OS_WINDOWS) && !defined(VBOX_CONTROL_TEST)
-    { "getvideoacceleration", handleGetVideoAcceleration },
-    { "setvideoacceleration", handleSetVideoAcceleration },
-    { "listcustommodes", handleListCustomModes },
-    { "addcustommode", handleAddCustomMode },
-    { "removecustommode", handleRemoveCustomMode },
-    { "setvideomode", handleSetVideoMode },
+    { "getvideoacceleration",   handleGetVideoAcceleration },
+    { "setvideoacceleration",   handleSetVideoAcceleration },
+    { "listcustommodes",        handleListCustomModes },
+    { "addcustommode",          handleAddCustomMode },
+    { "removecustommode",       handleRemoveCustomMode },
+    { "setvideomode",           handleSetVideoMode },
 #endif
 #ifdef VBOX_WITH_GUEST_PROPS
-    { "guestproperty", handleGuestProperty },
+    { "guestproperty",          handleGuestProperty },
 #endif
 #ifdef VBOX_WITH_SHARED_FOLDERS
-    { "sharedfolder", handleSharedFolder },
+    { "sharedfolder",           handleSharedFolder },
 #endif
-    { NULL, NULL }  /* terminator */
+    { NULL,                     NULL }  /* unnecessary terminator entry */
 };
 
@@ -1397,5 +1397,5 @@
 {
     /** The application's global return code */
-    int rc = 0;
+    RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
     /** An IPRT return code for local use */
     int rrc = VINF_SUCCESS;
@@ -1409,17 +1409,20 @@
     bool fOnlyInfo = false;
 
+    rrc = RTR3Init();
+    if (RT_FAILURE(rrc))
+        return RTMsgInitFailure(rrc);
+
     /*
      * Start by handling command line switches
      */
-
-    /** Are we finished with handling switches? */
-    bool done = false;
+    /** @todo RTGetOpt conversion of the whole file. */
+    bool done = false;      /**< Are we finished with handling switches? */
     while (!done && (iArg < argc))
     {
-        if (   0 == strcmp(argv[iArg], "-V")
-            || 0 == strcmp(argv[iArg], "-v")
-            || 0 == strcmp(argv[iArg], "--version")
-            || 0 == strcmp(argv[iArg], "-version")
-            || 0 == strcmp(argv[iArg], "getversion")
+        if (   !strcmp(argv[iArg], "-V")
+            || !strcmp(argv[iArg], "-v")
+            || !strcmp(argv[iArg], "--version")
+            || !strcmp(argv[iArg], "-version")
+            || !strcmp(argv[iArg], "getversion")
            )
             {
@@ -1430,9 +1433,9 @@
                 done = true;
             }
-        else if (   0 == strcmp(argv[iArg], "-nologo")
-                 || 0 == strcmp(argv[iArg], "--nologo"))
+        else if (   !strcmp(argv[iArg], "-nologo")
+                 || !strcmp(argv[iArg], "--nologo"))
             fShowLogo = false;
-        else if (   0 == strcmp(argv[iArg], "-help")
-                 || 0 == strcmp(argv[iArg], "--help"))
+        else if (   !strcmp(argv[iArg], "-help")
+                 || !strcmp(argv[iArg], "--help"))
         {
             fOnlyInfo = true;
@@ -1452,5 +1455,4 @@
      * and show the usage if the user asked us to
      */
-
     g_pszProgName = RTPathFilename(argv[0]);
     if (fShowLogo)
@@ -1465,22 +1467,13 @@
      * Do global initialisation for the programme if we will be handling a command
      */
-
     if (!fOnlyInfo)
     {
-        rrc = RTR3Init(); /** @todo r=bird: This ALWAYS goes first, the only exception is when you have to parse args to figure out which to call! */
-        if (!RT_SUCCESS(rrc))
-        {
-            VBoxControlError("Failed to initialise the VirtualBox runtime - error %Rrc\n", rrc);
-            rc = 1;
-        }
-        if (0 == rc)
-        {
-            if (!RT_SUCCESS(VbglR3Init()))
-            {
-                VBoxControlError("Could not contact the host system.  Make sure that you are running this\n"
-                                 "application inside a VirtualBox guest system, and that you have sufficient\n"
-                                 "user permissions.\n");
-                rc = 1;
-            }
+        rrc = VbglR3Init();
+        if (RT_FAILURE(rrc))
+        {
+            VBoxControlError("Could not contact the host system.  Make sure that you are running this\n"
+                             "application inside a VirtualBox guest system, and that you have sufficient\n"
+                             "user permissions.\n");
+            rcExit = RTEXITCODE_FAILURE;
         }
     }
@@ -1490,5 +1483,5 @@
      */
 
-    if (!fOnlyInfo && (0 == rc))
+    if (!fOnlyInfo && rcExit == RTEXITCODE_SUCCESS)
     {
         /*
@@ -1497,9 +1490,11 @@
          * here.
          */
+        /** @todo this must be done before we start checking for --help and
+         *        stuff above. */
         for (int i = iArg; i < argc; i++)
         {
-            char *converted;
-            RTStrCurrentCPToUtf8(&converted, argv[i]);
-            argv[i] = converted;
+            char *pszConverted;
+            RTStrCurrentCPToUtf8(&pszConverted, argv[i]);
+            argv[i] = pszConverted;
         }
 
@@ -1510,9 +1505,9 @@
             /** And if so, what is its position in the table? */
             unsigned index = 0;
-            while (   index < RT_ELEMENTS(g_commandHandlers)
+            while (   index < RT_ELEMENTS(g_aCommandHandlers)
                    && !found
-                   && (g_commandHandlers[index].command != NULL))
+                   && (g_aCommandHandlers[index].pszCommand != NULL))
             {
-                if (0 == strcmp(argv[iArg], g_commandHandlers[index].command))
+                if (!strcmp(argv[iArg], g_aCommandHandlers[index].pszCommand))
                     found = true;
                 else
@@ -1520,8 +1515,8 @@
             }
             if (found)
-                rc = g_commandHandlers[index].handler(argc - iArg - 1, argv + iArg + 1);
+                rcExit = g_aCommandHandlers[index].pfnHandler(argc - iArg - 1, argv + iArg + 1);
             else
             {
-                rc = 1;
+                rcExit = RTEXITCODE_FAILURE;
                 usage();
             }
@@ -1530,5 +1525,5 @@
         {
             /* The user didn't specify a command. */
-            rc = 1;
+            rcExit = RTEXITCODE_FAILURE;
             usage();
         }
@@ -1539,5 +1534,4 @@
         for (int i = iArg; i < argc; i++)
             RTStrFree(argv[i]);
-
     }
 
@@ -1545,6 +1539,5 @@
      * And exit, returning the status
      */
-
-    return rc;
-}
-
+    return rcExit;
+}
+
