Submit merged

Squashed commit of the following:

commit aeefab810c6331e2f96e81f20e4408b39dd3a2ca
Author: Vladimir Chtchetkine <vchtchetkine@google.com>
Date:   Thu Dec 2 07:40:34 2010 -0800

    Implement -attach-core UI option

    Change-Id: I4168e2d707cab1b4873ee16d86d5126c1a316abf

Change-Id: I2da1ef5d53641f3c60d83d8d5ddf3aff34b0c6c7
diff --git a/android/console.c b/android/console.c
index 42addba..5e37060 100644
--- a/android/console.c
+++ b/android/console.c
@@ -112,6 +112,8 @@
 
 } ControlGlobalRec;
 
+/* UI client currently attached to the core. */
+ControlClient attached_ui_client = NULL;
 
 static int
 control_global_add_redir( ControlGlobal  global,
@@ -212,6 +214,10 @@
 
     D(( "destroying control client %p\n", client ));
 
+    if (client == attached_ui_client) {
+        attached_ui_client = NULL;
+    }
+
     sock = control_client_detach( client );
     if (sock >= 0)
         socket_close(sock);
@@ -392,8 +398,9 @@
         CommandDef  subcmd;
 
         if (cmd->handler) {
-            if ( !cmd->handler( client, args ) )
+            if ( !cmd->handler( client, args ) ) {
                 control_write( client, "OK\r\n" );
+            }
             break;
         }
 
@@ -503,8 +510,8 @@
     size = socket_recv( client->sock, buf, sizeof(buf) );
     if (size < 0) {
         D(( "size < 0, exiting with %d: %s\n", errno, errno_str ));
-		if (errno != EWOULDBLOCK && errno != EAGAIN && errno != EINTR)
-			control_client_destroy( client );
+        if (errno != EWOULDBLOCK && errno != EAGAIN)
+            control_client_destroy( client );
         return;
     }
 
@@ -2364,12 +2371,43 @@
     return 0;
 }
 
+/* UI settings, passed to the core via -ui-settings command line parameter. */
+extern char* android_op_ui_settings;
+
+static int
+do_attach_ui( ControlClient client, char* args )
+{
+    // Make sure that there are no UI already attached to this console.
+    if (attached_ui_client != NULL) {
+        control_write( client, "KO: Another UI is attached to this core!\r\n" );
+        control_client_destroy(client);
+        return -1;
+    }
+
+    attached_ui_client = client;
+
+    if (android_op_ui_settings != NULL) {
+        // Reply "OK" with the saved -ui-settings property.
+        char reply_buf[4096];
+        snprintf(reply_buf, sizeof(reply_buf), "OK: %s\r\n", android_op_ui_settings);
+        control_write( client, reply_buf);
+    } else {
+        control_write( client, "OK\r\n");
+    }
+
+    return 0;
+}
+
 static const CommandDefRec  qemu_commands[] =
 {
     { "monitor", "enter QEMU monitor",
     "Enter the QEMU virtual machine monitor\r\n",
     NULL, do_qemu_monitor, NULL },
 
+    { "attach UI", "attach UI to the core",
+    "Attach UI to the core\r\n",
+    NULL, do_attach_ui, NULL },
+
     { NULL, NULL, NULL, NULL, NULL, NULL }
 };
 
@@ -2385,8 +2423,8 @@
 static int
 do_kill( ControlClient  client, char*  args )
 {
-	control_write( client, "OK: killing emulator, bye bye\r\n" );
-	exit(0);
+    control_write( client, "OK: killing emulator, bye bye\r\n" );
+    exit(0);
 }
 
 static const CommandDefRec   main_commands[] =
@@ -2410,7 +2448,7 @@
       NULL, cdma_commands },
 
     { "kill", "kill the emulator instance", NULL, NULL,
-	  do_kill, NULL },
+      do_kill, NULL },
 
     { "network", "manage network settings",
       "allows you to manage the settings related to the network data connection of the\r\n"