Import gdb 7.3 into vendor branch
[dragonfly.git] / contrib / gdb-7 / gdb / mi / mi-console.c
1 /* MI Console code.
2
3    Copyright (C) 2000, 2001, 2002, 2007, 2008, 2009, 2010, 2011
4    Free Software Foundation, Inc.
5
6    Contributed by Cygnus Solutions (a Red Hat company).
7
8    This file is part of GDB.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22
23 #include "defs.h"
24 #include "mi-console.h"
25 #include "gdb_string.h"
26
27 /* MI-console: send output to std-out but correcty encapsulated */
28
29 static ui_file_fputs_ftype mi_console_file_fputs;
30 static ui_file_flush_ftype mi_console_file_flush;
31 static ui_file_delete_ftype mi_console_file_delete;
32
33 struct mi_console_file
34   {
35     int *magic;
36     struct ui_file *raw;
37     struct ui_file *buffer;
38     const char *prefix;
39     char quote;
40   };
41
42 int mi_console_file_magic;
43
44 struct ui_file *
45 mi_console_file_new (struct ui_file *raw,
46                      const char *prefix, char quote)
47 {
48   struct ui_file *ui_file = ui_file_new ();
49   struct mi_console_file *mi_console = XMALLOC (struct mi_console_file);
50
51   mi_console->magic = &mi_console_file_magic;
52   mi_console->raw = raw;
53   mi_console->buffer = mem_fileopen ();
54   mi_console->prefix = prefix;
55   mi_console->quote = quote;
56   set_ui_file_fputs (ui_file, mi_console_file_fputs);
57   set_ui_file_flush (ui_file, mi_console_file_flush);
58   set_ui_file_data (ui_file, mi_console, mi_console_file_delete);
59   return ui_file;
60 }
61
62 static void
63 mi_console_file_delete (struct ui_file *file)
64 {
65   struct mi_console_file *mi_console = ui_file_data (file);
66
67   if (mi_console->magic != &mi_console_file_magic)
68     internal_error (__FILE__, __LINE__,
69                     _("mi_console_file_delete: bad magic number"));
70   xfree (mi_console);
71 }
72
73 static void
74 mi_console_file_fputs (const char *buf,
75                        struct ui_file *file)
76 {
77   struct mi_console_file *mi_console = ui_file_data (file);
78
79   if (mi_console->magic != &mi_console_file_magic)
80     internal_error (__FILE__, __LINE__,
81                     "mi_console_file_fputs: bad magic number");
82   /* Append the text to our internal buffer */
83   fputs_unfiltered (buf, mi_console->buffer);
84   /* Flush when an embedded \n */
85   if (strchr (buf, '\n') != NULL)
86     gdb_flush (file);
87 }
88
89 /* Transform a byte sequence into a console output packet. */
90 static void
91 mi_console_raw_packet (void *data,
92                        const char *buf,
93                        long length_buf)
94 {
95   struct mi_console_file *mi_console = data;
96
97   if (mi_console->magic != &mi_console_file_magic)
98     internal_error (__FILE__, __LINE__,
99                     _("mi_console_file_transform: bad magic number"));
100
101   if (length_buf > 0)
102     {
103       fputs_unfiltered (mi_console->prefix, mi_console->raw);
104       if (mi_console->quote)
105         {
106           fputs_unfiltered ("\"", mi_console->raw);
107           fputstrn_unfiltered (buf, length_buf,
108                                mi_console->quote, mi_console->raw);
109           fputs_unfiltered ("\"\n", mi_console->raw);
110         }
111       else
112         {
113           fputstrn_unfiltered (buf, length_buf, 0, mi_console->raw);
114           fputs_unfiltered ("\n", mi_console->raw);
115         }
116       gdb_flush (mi_console->raw);
117     }
118 }
119
120 static void
121 mi_console_file_flush (struct ui_file *file)
122 {
123   struct mi_console_file *mi_console = ui_file_data (file);
124
125   if (mi_console->magic != &mi_console_file_magic)
126     internal_error (__FILE__, __LINE__,
127                     _("mi_console_file_flush: bad magic number"));
128   ui_file_put (mi_console->buffer, mi_console_raw_packet, mi_console);
129   ui_file_rewind (mi_console->buffer);
130 }