Pass the whole commandline.
[gdbmicli.git] / main.c
diff --git a/main.c b/main.c
index 54360b3..2980031 100644 (file)
--- a/main.c
+++ b/main.c
@@ -12,6 +12,7 @@
 #define fatal(fmt...) fatal_func (__FILE__, __LINE__, __PRETTY_FUNCTION__, fmt)
 #define gdb_assert assert
 #define gdb_assert_not_reached(msg) fatal ("%s", (msg))
+#define _(x) x
 
 static ATTRIBUTE_NORETURN void
 fatal_func (const char *file, int line, const char *func, const char *fmt, ...)
@@ -348,35 +349,42 @@ completion_entry_function (const char *text, int matches)
     gdb_assert_not_reached ("too many matches");
 }
 
+extern char **mi_gdb_argv;
+extern void (*mi_gdb_start_hook) (mi_h *h);
+
+static void
+start_hook (mi_h *h)
+{
+  on_exit (h_disconnect, h);
+  mi_set_console_cb (h, console_cb, NULL);
+//  mi_set_to_gdb_cb (h, to_gdb_cb, NULL);
+//  mi_set_from_gdb_cb (h, from_gdb_cb, NULL);
+}
+
 int
 main (int argc, char **argv)
 {
   mi_h *h;
-  char *cmd;
-  const char *ex[argc];
-  unsigned ex_count = 0, ex_used = 0;
+  mi_output *res;
 
   setbuf (stdout, NULL);
 
-  while (*++argv != NULL)
-    {
-      if (strcmp (*argv, "-ex") == 0 && argv[1] != NULL)
-       ex[ex_count++] = *++argv;
-      else
-       fatal ("Unknown parameter: %s", *argv);
-    }
+  mi_gdb_argv = xmalloc ((argc + 2) * sizeof (*mi_gdb_argv));
+  memcpy (&mi_gdb_argv[2], &argv[1], argc * sizeof (*mi_gdb_argv));
+  mi_gdb_argv[0] = "gdb";
+  mi_gdb_argv[1] = "--interpreter=mi";
 
-  mi_set_workaround (MI_PSYM_SEARCH, 0);
+  mi_gdb_start_hook = start_hook;
 
   h = mi_connect_local ();
   if (h == NULL)
     fatal ("Cannot connect to GDB");
 
-  on_exit (h_disconnect, h);
-
-  mi_set_console_cb (h, console_cb, NULL);
-//  mi_set_to_gdb_cb (h, to_gdb_cb, NULL);
-//  mi_set_from_gdb_cb (h, from_gdb_cb, NULL);
+  /* First eat the prompt.  Then run empty command so that additional results
+     from -ex or -x during mi_connect_local are flushed.  */
+  res = mi_get_response_blk (h);
+  gdb_assert (res == NULL);
+  default_command (h, "echo");
 
   completion_entry_function_h = h;
   rl_completion_entry_function = completion_entry_function;
@@ -396,19 +404,13 @@ main (int argc, char **argv)
 
   for (;;)
     {
-      if (ex_used < ex_count)
-       {
-         cmd = xstrdup (ex[ex_used++]);
-         printf ("(gdb) %s\n", cmd);
-       }
+      char *cmd = readline ("(gdb) ");
+
+      if (cmd == NULL)
+       cmd = xstrdup ("quit");
       else
-       {
-         cmd = readline ("(gdb) ");
-         if (cmd == NULL)
-           cmd = xstrdup ("quit");
-         else
-           add_history (cmd);
-       }
+       add_history (cmd);
+
       executecommand (h, cmd);
       xfree (cmd);
     }