draft
[gdbmicli.git] / main.c
1 #include <stdio.h>
2 #include <stdarg.h>
3 #include <readline/readline.h>
4 #include "ansidecl.h"
5 #include "mi_gdb.h"
6
7 static ATTRIBUTE_NORETURN void
8 fatal (const char *fmt, ...)
9 {
10   va_list ap;
11
12   va_start (ap, fmt);
13   vfprintf (stderr, fmt, ap);
14   va_end (ap);
15   exit (EXIT_FAILURE);
16 }
17
18 static void
19 console_cb (const char *str, void *data)
20 {
21   fputs (str, stdout);
22 }
23
24 int
25 main (int argc, char **argv)
26 {
27   mi_h *h;
28   char *cmd;
29   const char *ex[argc];
30   unsigned ex_count = 0, ex_used = 0;
31
32   setbuf (stdout, NULL);
33
34   while (*++argv != NULL)
35     {
36       if (strcmp (*argv, "-ex") == 0 && argv[1] != NULL)
37         ex[ex_count++] = *++argv;
38       else
39         fatal ("Unknown parameter: %s", *argv);
40     }
41
42   mi_set_workaround (MI_PSYM_SEARCH, 0);
43
44   h = mi_connect_local ();
45   if (h == NULL)
46     fatal ("Cannot connect to GDB");
47
48   mi_set_console_cb (h, console_cb, NULL);
49
50   for (;;)
51     {
52       mi_output *rec, *res;
53
54       if (ex_used < ex_count)
55         {
56           cmd = strdup (ex[ex_used++]);
57           printf ("(gdb) %s\n", cmd);
58         }
59       else
60         {
61           cmd = readline ("(gdb) ");
62           if (cmd == NULL)
63             cmd = strdup ("quit");
64         }
65
66       mi_send (h, "-interpreter-exec console \"%s\"\n", cmd);
67       free (cmd);
68
69       res = mi_get_response_blk (h);
70       if (res == NULL)
71         fatal ("mi_get_response_blk == NULL");
72
73       rec = mi_get_rrecord (res);
74       if (rec == NULL)
75         fatal ("mi_get_rrecord == NULL");
76       if (rec->tclass != MI_CL_DONE)
77         fatal ("mi_get_rrecord != MI_CL_DONE");
78
79       mi_free_output (res);
80     }
81 }