Commit | Line | Data |
---|---|---|
fb40c209 | 1 | /* MI Console code. |
349c5d5f | 2 | |
7b6bb8da | 3 | Copyright (C) 2000, 2001, 2002, 2007, 2008, 2009, 2010, 2011 |
0fb0cc75 | 4 | Free Software Foundation, Inc. |
349c5d5f | 5 | |
ab91fdd5 | 6 | Contributed by Cygnus Solutions (a Red Hat company). |
fb40c209 AC |
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 | |
a9762ec7 | 12 | the Free Software Foundation; either version 3 of the License, or |
fb40c209 AC |
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 | |
a9762ec7 | 21 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
fb40c209 AC |
22 | |
23 | #include "defs.h" | |
24 | #include "mi-console.h" | |
27b82ed2 | 25 | #include "gdb_string.h" |
fb40c209 | 26 | |
fb40c209 AC |
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; | |
4389a95a | 39 | char quote; |
fb40c209 AC |
40 | }; |
41 | ||
42 | int mi_console_file_magic; | |
43 | ||
44 | struct ui_file * | |
45 | mi_console_file_new (struct ui_file *raw, | |
4389a95a | 46 | const char *prefix, char quote) |
fb40c209 AC |
47 | { |
48 | struct ui_file *ui_file = ui_file_new (); | |
49 | struct mi_console_file *mi_console = XMALLOC (struct mi_console_file); | |
102040f0 | 50 | |
fb40c209 AC |
51 | mi_console->magic = &mi_console_file_magic; |
52 | mi_console->raw = raw; | |
53 | mi_console->buffer = mem_fileopen (); | |
54 | mi_console->prefix = prefix; | |
4389a95a | 55 | mi_console->quote = quote; |
fb40c209 AC |
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); | |
102040f0 | 66 | |
fb40c209 | 67 | if (mi_console->magic != &mi_console_file_magic) |
8e65ff28 | 68 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 69 | _("mi_console_file_delete: bad magic number")); |
b8c9b27d | 70 | xfree (mi_console); |
fb40c209 AC |
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); | |
102040f0 | 78 | |
fb40c209 | 79 | if (mi_console->magic != &mi_console_file_magic) |
8e65ff28 AC |
80 | internal_error (__FILE__, __LINE__, |
81 | "mi_console_file_fputs: bad magic number"); | |
fb40c209 AC |
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; | |
102040f0 | 96 | |
fb40c209 | 97 | if (mi_console->magic != &mi_console_file_magic) |
8e65ff28 | 98 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 99 | _("mi_console_file_transform: bad magic number")); |
fb40c209 AC |
100 | |
101 | if (length_buf > 0) | |
102 | { | |
103 | fputs_unfiltered (mi_console->prefix, mi_console->raw); | |
4389a95a AC |
104 | if (mi_console->quote) |
105 | { | |
106 | fputs_unfiltered ("\"", mi_console->raw); | |
9a2b4c1b MS |
107 | fputstrn_unfiltered (buf, length_buf, |
108 | mi_console->quote, mi_console->raw); | |
4389a95a AC |
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 | } | |
fb40c209 AC |
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); | |
102040f0 | 124 | |
fb40c209 | 125 | if (mi_console->magic != &mi_console_file_magic) |
8e65ff28 | 126 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 127 | _("mi_console_file_flush: bad magic number")); |
fb40c209 AC |
128 | ui_file_put (mi_console->buffer, mi_console_raw_packet, mi_console); |
129 | ui_file_rewind (mi_console->buffer); | |
130 | } |