Cleanup stuff for the UI build

1. Move DNS server registration entirely to the core.
2. Move -memcheck option initialization entirely to the core.
3. Move qemu_help routine (used by both, Core and UI) to common qemu-help.c file.

Change-Id: I5e47e94bdc375f0e037ccc6bd88daeb5fa06d1cf
diff --git a/vl-android.c b/vl-android.c
index 12627fd..8e38b7f 100644
--- a/vl-android.c
+++ b/vl-android.c
@@ -351,6 +351,9 @@
 /* Memory checker options. */
 char* android_op_memcheck = NULL;
 
+/* -dns-server option value. */
+char* android_op_dns_server = NULL;
+
 extern int android_display_width;
 extern int android_display_height;
 extern int android_display_bpp;
@@ -4546,7 +4549,7 @@
     pause_all_vcpus();
 }
 
-static void version(void)
+void version(void)
 {
     printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
 }
@@ -4975,6 +4978,47 @@
     return buf;
 }
 
+static int
+add_dns_server( const char*  server_name )
+{
+    SockAddress   addr;
+
+    if (sock_address_init_resolve( &addr, server_name, 55, 0 ) < 0) {
+        fprintf(stdout,
+                "### WARNING: can't resolve DNS server name '%s'\n",
+                server_name );
+        return -1;
+    }
+
+    fprintf(stderr,
+            "DNS server name '%s' resolved to %s\n", server_name, sock_address_to_string(&addr) );
+
+    if ( slirp_add_dns_server( &addr ) < 0 ) {
+        fprintf(stderr,
+                "### WARNING: could not add DNS server '%s' to the network stack\n", server_name);
+        return -1;
+    }
+    return 0;
+}
+
+/* Appends a parameter to a string of parameters separated with space.
+ * Pararm:
+ *  param_str String containing parameters separated with space.
+ *  param Parameter to append to the string.
+ *  size - Size (in characters) of the buffer addressed by param_str.
+ */
+static void
+append_param(char* param_str, const char* arg, int size)
+{
+    if (*param_str) {
+        strncat(param_str, " ", size);
+        strncat(param_str, arg, size);
+    } else {
+        strncpy(param_str, arg, size);
+        param_str[size - 1] = '\0';
+    }
+}
+
 int main(int argc, char **argv, char **envp)
 {
     const char *gdbstub_dev = NULL;
@@ -5024,6 +5068,14 @@
 #ifdef CONFIG_STANDALONE_CORE
     IniFile*  hw_ini = NULL;
 #endif  // CONFIG_STANDALONE_CORE
+    /* Container for the kernel initialization parameters collected in this
+     * routine. */
+    char kernel_cmdline_append[1024];
+    /* Combines kernel initialization parameters passed from the UI with
+     * the parameters collected in this routine. */
+    char kernel_cmdline_full[1024];
+    char tmp_str[1024];
+    int    dns_count = 0;
 
     init_clocks();
 
@@ -5068,6 +5120,8 @@
     snapshot = 0;
     kernel_filename = NULL;
     kernel_cmdline = "";
+    kernel_cmdline_append[0] = '\0';
+    kernel_cmdline_full[0] = '\0';
     cyls = heads = secs = 0;
     translation = BIOS_ATA_TRANSLATION_AUTO;
     monitor_device = "vc:80Cx24C";
@@ -5820,9 +5874,19 @@
             case QEMU_OPTION_android_hw:
                 android_op_hwini = (char*)optarg;
                 break;
+            case QEMU_OPTION_dns_server:
+                android_op_dns_server = (char*)optarg;
+                break;
+
 #ifdef CONFIG_MEMCHECK
             case QEMU_OPTION_android_memcheck:
                 android_op_memcheck = (char*)optarg;
+                snprintf(tmp_str, sizeof(tmp_str), "memcheck=%s",
+                         android_op_memcheck);
+                tmp_str[sizeof(tmp_str) - 1] = '\0';
+                /* This will set ro.kernel.memcheck system property
+                 * to memcheck's tracing flags. */
+                append_param(kernel_cmdline_append, tmp_str, sizeof(kernel_cmdline_append));
                 break;
 #endif // CONFIG_MEMCHECK
             }
@@ -5868,6 +5932,44 @@
     iniFile_free(hw_ini);
 #endif  // CONFIG_STANDALONE_CORE
 
+    if (android_op_dns_server) {
+        char*  x = strchr(android_op_dns_server, ',');
+        dns_count = 0;
+        if (x == NULL)
+        {
+            if ( add_dns_server( android_op_dns_server ) == 0 )
+                dns_count = 1;
+        }
+        else
+        {
+            x = android_op_dns_server;
+            while (*x) {
+                char*  y = strchr(x, ',');
+
+                if (y != NULL) {
+                    *y = 0;
+                    y++;
+                } else {
+                    y = x + strlen(x);
+                }
+
+                if (y > x && add_dns_server( x ) == 0) {
+                    dns_count += 1;
+                }
+                x = y;
+            }
+        }
+        if (dns_count == 0)
+            fprintf( stdout, "### WARNING: will use system default DNS server\n" );
+    }
+
+    if (dns_count == 0)
+        dns_count = slirp_get_system_dns_servers();
+    if (dns_count) {
+        snprintf(tmp_str, sizeof(tmp_str), "android.ndns=%d", dns_count);
+        append_param(kernel_cmdline_append, tmp_str, sizeof(kernel_cmdline_append));
+    }
+
 #ifdef CONFIG_MEMCHECK
     if (android_op_memcheck) {
         memcheck_init(android_op_memcheck);
@@ -6235,8 +6337,22 @@
     }
 #endif
 
+    /* Combine kernel command line passed from the UI with parameters
+     * collected during initialization. */
+    if (*kernel_cmdline) {
+        if (kernel_cmdline_append[0]) {
+            snprintf(kernel_cmdline_full, sizeof(kernel_cmdline_full), "%s %s",
+                     kernel_cmdline, kernel_cmdline_append);
+        } else {
+            strncpy(kernel_cmdline_full, kernel_cmdline, sizeof(kernel_cmdline_full));
+            kernel_cmdline_full[sizeof(kernel_cmdline_full) - 1] = '\0';
+        }
+    } else if (kernel_cmdline_append[0]) {
+        strncpy(kernel_cmdline_full, kernel_cmdline_append, sizeof(kernel_cmdline_full));
+    }
+
     machine->init(ram_size, boot_devices,
-                  kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
+                  kernel_filename, kernel_cmdline_full, initrd_filename, cpu_model);
 
 
     for (env = first_cpu; env != NULL; env = env->next_cpu) {