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