]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/mi/mi-console.c
mi: Use the value in mi_console_file->quote as the quoting character
[thirdparty/binutils-gdb.git] / gdb / mi / mi-console.c
CommitLineData
fb40c209 1/* MI Console code.
349c5d5f 2
ecd75fc8 3 Copyright (C) 2000-2014 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 21
2b03b41d
SS
22/* An MI console is a kind of ui_file stream that sends output to
23 stdout, but encapsulated and prefixed with a distinctive string;
24 for instance, error output is normally identified by a leading
25 "&". */
26
fb40c209
AC
27#include "defs.h"
28#include "mi-console.h"
0e9f083f 29#include <string.h>
fb40c209 30
fb40c209
AC
31static ui_file_fputs_ftype mi_console_file_fputs;
32static ui_file_flush_ftype mi_console_file_flush;
33static ui_file_delete_ftype mi_console_file_delete;
34
35struct mi_console_file
36 {
37 int *magic;
38 struct ui_file *raw;
39 struct ui_file *buffer;
40 const char *prefix;
4389a95a 41 char quote;
fb40c209
AC
42 };
43
2b03b41d
SS
44/* Use the address of this otherwise-unused global as a magic number
45 identifying this class of ui_file objects. */
46static int mi_console_file_magic;
47
48/* Create a console that wraps the given output stream RAW with the
49 string PREFIX and quoting it with QUOTE. */
fb40c209
AC
50
51struct ui_file *
2b03b41d 52mi_console_file_new (struct ui_file *raw, const char *prefix, char quote)
fb40c209
AC
53{
54 struct ui_file *ui_file = ui_file_new ();
70ba0933 55 struct mi_console_file *mi_console = XNEW (struct mi_console_file);
102040f0 56
fb40c209
AC
57 mi_console->magic = &mi_console_file_magic;
58 mi_console->raw = raw;
59 mi_console->buffer = mem_fileopen ();
60 mi_console->prefix = prefix;
4389a95a 61 mi_console->quote = quote;
fb40c209
AC
62 set_ui_file_fputs (ui_file, mi_console_file_fputs);
63 set_ui_file_flush (ui_file, mi_console_file_flush);
64 set_ui_file_data (ui_file, mi_console, mi_console_file_delete);
2b03b41d 65
fb40c209
AC
66 return ui_file;
67}
68
69static void
70mi_console_file_delete (struct ui_file *file)
71{
72 struct mi_console_file *mi_console = ui_file_data (file);
102040f0 73
fb40c209 74 if (mi_console->magic != &mi_console_file_magic)
8e65ff28 75 internal_error (__FILE__, __LINE__,
e2e0b3e5 76 _("mi_console_file_delete: bad magic number"));
2b03b41d 77
b8c9b27d 78 xfree (mi_console);
fb40c209
AC
79}
80
81static void
2b03b41d 82mi_console_file_fputs (const char *buf, struct ui_file *file)
fb40c209
AC
83{
84 struct mi_console_file *mi_console = ui_file_data (file);
102040f0 85
fb40c209 86 if (mi_console->magic != &mi_console_file_magic)
8e65ff28
AC
87 internal_error (__FILE__, __LINE__,
88 "mi_console_file_fputs: bad magic number");
2b03b41d
SS
89
90 /* Append the text to our internal buffer. */
fb40c209 91 fputs_unfiltered (buf, mi_console->buffer);
2b03b41d 92 /* Flush when an embedded newline is present anywhere in the buffer. */
fb40c209
AC
93 if (strchr (buf, '\n') != NULL)
94 gdb_flush (file);
95}
96
2b03b41d
SS
97/* Transform a byte sequence into a console output packet. */
98
fb40c209 99static void
2b03b41d 100mi_console_raw_packet (void *data, const char *buf, long length_buf)
fb40c209
AC
101{
102 struct mi_console_file *mi_console = data;
102040f0 103
fb40c209 104 if (mi_console->magic != &mi_console_file_magic)
8e65ff28 105 internal_error (__FILE__, __LINE__,
2b03b41d 106 _("mi_console_raw_packet: bad magic number"));
fb40c209
AC
107
108 if (length_buf > 0)
109 {
110 fputs_unfiltered (mi_console->prefix, mi_console->raw);
4389a95a
AC
111 if (mi_console->quote)
112 {
303a33fa 113 fputc_unfiltered (mi_console->quote, mi_console->raw);
9a2b4c1b
MS
114 fputstrn_unfiltered (buf, length_buf,
115 mi_console->quote, mi_console->raw);
303a33fa
SM
116 fputc_unfiltered (mi_console->quote, mi_console->raw);
117 fputc_unfiltered ('\n', mi_console->raw);
4389a95a
AC
118 }
119 else
120 {
121 fputstrn_unfiltered (buf, length_buf, 0, mi_console->raw);
303a33fa 122 fputc_unfiltered ('\n', mi_console->raw);
4389a95a 123 }
fb40c209
AC
124 gdb_flush (mi_console->raw);
125 }
126}
127
128static void
129mi_console_file_flush (struct ui_file *file)
130{
131 struct mi_console_file *mi_console = ui_file_data (file);
102040f0 132
fb40c209 133 if (mi_console->magic != &mi_console_file_magic)
8e65ff28 134 internal_error (__FILE__, __LINE__,
e2e0b3e5 135 _("mi_console_file_flush: bad magic number"));
2b03b41d 136
fb40c209
AC
137 ui_file_put (mi_console->buffer, mi_console_raw_packet, mi_console);
138 ui_file_rewind (mi_console->buffer);
37ce89eb
SS
139
140}
141
142/* Change the underlying stream of the console directly; this is
143 useful as a minimum-impact way to reflect external changes like
144 logging enable/disable. */
145
146void
147mi_console_set_raw (struct ui_file *file, struct ui_file *raw)
148{
149 struct mi_console_file *mi_console = ui_file_data (file);
150
151 if (mi_console->magic != &mi_console_file_magic)
152 internal_error (__FILE__, __LINE__,
153 _("mi_console_file_set_raw: bad magic number"));
154
155 mi_console->raw = raw;
fb40c209 156}