15796c8dcSSimon Schubert /* MI Console code.
25796c8dcSSimon Schubert
3*ef5ccd6cSJohn Marino Copyright (C) 2000-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert
55796c8dcSSimon Schubert Contributed by Cygnus Solutions (a Red Hat company).
65796c8dcSSimon Schubert
75796c8dcSSimon Schubert This file is part of GDB.
85796c8dcSSimon Schubert
95796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify
105796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by
115796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or
125796c8dcSSimon Schubert (at your option) any later version.
135796c8dcSSimon Schubert
145796c8dcSSimon Schubert This program is distributed in the hope that it will be useful,
155796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of
165796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
175796c8dcSSimon Schubert GNU General Public License for more details.
185796c8dcSSimon Schubert
195796c8dcSSimon Schubert You should have received a copy of the GNU General Public License
205796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */
215796c8dcSSimon Schubert
22*ef5ccd6cSJohn Marino /* An MI console is a kind of ui_file stream that sends output to
23*ef5ccd6cSJohn Marino stdout, but encapsulated and prefixed with a distinctive string;
24*ef5ccd6cSJohn Marino for instance, error output is normally identified by a leading
25*ef5ccd6cSJohn Marino "&". */
26*ef5ccd6cSJohn Marino
275796c8dcSSimon Schubert #include "defs.h"
285796c8dcSSimon Schubert #include "mi-console.h"
295796c8dcSSimon Schubert #include "gdb_string.h"
305796c8dcSSimon Schubert
315796c8dcSSimon Schubert static ui_file_fputs_ftype mi_console_file_fputs;
325796c8dcSSimon Schubert static ui_file_flush_ftype mi_console_file_flush;
335796c8dcSSimon Schubert static ui_file_delete_ftype mi_console_file_delete;
345796c8dcSSimon Schubert
355796c8dcSSimon Schubert struct mi_console_file
365796c8dcSSimon Schubert {
375796c8dcSSimon Schubert int *magic;
385796c8dcSSimon Schubert struct ui_file *raw;
395796c8dcSSimon Schubert struct ui_file *buffer;
405796c8dcSSimon Schubert const char *prefix;
415796c8dcSSimon Schubert char quote;
425796c8dcSSimon Schubert };
435796c8dcSSimon Schubert
44*ef5ccd6cSJohn Marino /* Use the address of this otherwise-unused global as a magic number
45*ef5ccd6cSJohn Marino identifying this class of ui_file objects. */
46*ef5ccd6cSJohn Marino static int mi_console_file_magic;
47*ef5ccd6cSJohn Marino
48*ef5ccd6cSJohn Marino /* Create a console that wraps the given output stream RAW with the
49*ef5ccd6cSJohn Marino string PREFIX and quoting it with QUOTE. */
505796c8dcSSimon Schubert
515796c8dcSSimon Schubert struct ui_file *
mi_console_file_new(struct ui_file * raw,const char * prefix,char quote)52*ef5ccd6cSJohn Marino mi_console_file_new (struct ui_file *raw, const char *prefix, char quote)
535796c8dcSSimon Schubert {
545796c8dcSSimon Schubert struct ui_file *ui_file = ui_file_new ();
555796c8dcSSimon Schubert struct mi_console_file *mi_console = XMALLOC (struct mi_console_file);
56cf7f2e2dSJohn Marino
575796c8dcSSimon Schubert mi_console->magic = &mi_console_file_magic;
585796c8dcSSimon Schubert mi_console->raw = raw;
595796c8dcSSimon Schubert mi_console->buffer = mem_fileopen ();
605796c8dcSSimon Schubert mi_console->prefix = prefix;
615796c8dcSSimon Schubert mi_console->quote = quote;
625796c8dcSSimon Schubert set_ui_file_fputs (ui_file, mi_console_file_fputs);
635796c8dcSSimon Schubert set_ui_file_flush (ui_file, mi_console_file_flush);
645796c8dcSSimon Schubert set_ui_file_data (ui_file, mi_console, mi_console_file_delete);
65*ef5ccd6cSJohn Marino
665796c8dcSSimon Schubert return ui_file;
675796c8dcSSimon Schubert }
685796c8dcSSimon Schubert
695796c8dcSSimon Schubert static void
mi_console_file_delete(struct ui_file * file)705796c8dcSSimon Schubert mi_console_file_delete (struct ui_file *file)
715796c8dcSSimon Schubert {
725796c8dcSSimon Schubert struct mi_console_file *mi_console = ui_file_data (file);
73cf7f2e2dSJohn Marino
745796c8dcSSimon Schubert if (mi_console->magic != &mi_console_file_magic)
755796c8dcSSimon Schubert internal_error (__FILE__, __LINE__,
765796c8dcSSimon Schubert _("mi_console_file_delete: bad magic number"));
77*ef5ccd6cSJohn Marino
785796c8dcSSimon Schubert xfree (mi_console);
795796c8dcSSimon Schubert }
805796c8dcSSimon Schubert
815796c8dcSSimon Schubert static void
mi_console_file_fputs(const char * buf,struct ui_file * file)82*ef5ccd6cSJohn Marino mi_console_file_fputs (const char *buf, struct ui_file *file)
835796c8dcSSimon Schubert {
845796c8dcSSimon Schubert struct mi_console_file *mi_console = ui_file_data (file);
85cf7f2e2dSJohn Marino
865796c8dcSSimon Schubert if (mi_console->magic != &mi_console_file_magic)
875796c8dcSSimon Schubert internal_error (__FILE__, __LINE__,
885796c8dcSSimon Schubert "mi_console_file_fputs: bad magic number");
89*ef5ccd6cSJohn Marino
90*ef5ccd6cSJohn Marino /* Append the text to our internal buffer. */
915796c8dcSSimon Schubert fputs_unfiltered (buf, mi_console->buffer);
92*ef5ccd6cSJohn Marino /* Flush when an embedded newline is present anywhere in the buffer. */
935796c8dcSSimon Schubert if (strchr (buf, '\n') != NULL)
945796c8dcSSimon Schubert gdb_flush (file);
955796c8dcSSimon Schubert }
965796c8dcSSimon Schubert
975796c8dcSSimon Schubert /* Transform a byte sequence into a console output packet. */
98*ef5ccd6cSJohn Marino
995796c8dcSSimon Schubert static void
mi_console_raw_packet(void * data,const char * buf,long length_buf)100*ef5ccd6cSJohn Marino mi_console_raw_packet (void *data, const char *buf, long length_buf)
1015796c8dcSSimon Schubert {
1025796c8dcSSimon Schubert struct mi_console_file *mi_console = data;
103cf7f2e2dSJohn Marino
1045796c8dcSSimon Schubert if (mi_console->magic != &mi_console_file_magic)
1055796c8dcSSimon Schubert internal_error (__FILE__, __LINE__,
106*ef5ccd6cSJohn Marino _("mi_console_raw_packet: bad magic number"));
1075796c8dcSSimon Schubert
1085796c8dcSSimon Schubert if (length_buf > 0)
1095796c8dcSSimon Schubert {
1105796c8dcSSimon Schubert fputs_unfiltered (mi_console->prefix, mi_console->raw);
1115796c8dcSSimon Schubert if (mi_console->quote)
1125796c8dcSSimon Schubert {
1135796c8dcSSimon Schubert fputs_unfiltered ("\"", mi_console->raw);
114c50c785cSJohn Marino fputstrn_unfiltered (buf, length_buf,
115c50c785cSJohn Marino mi_console->quote, mi_console->raw);
1165796c8dcSSimon Schubert fputs_unfiltered ("\"\n", mi_console->raw);
1175796c8dcSSimon Schubert }
1185796c8dcSSimon Schubert else
1195796c8dcSSimon Schubert {
1205796c8dcSSimon Schubert fputstrn_unfiltered (buf, length_buf, 0, mi_console->raw);
1215796c8dcSSimon Schubert fputs_unfiltered ("\n", mi_console->raw);
1225796c8dcSSimon Schubert }
1235796c8dcSSimon Schubert gdb_flush (mi_console->raw);
1245796c8dcSSimon Schubert }
1255796c8dcSSimon Schubert }
1265796c8dcSSimon Schubert
1275796c8dcSSimon Schubert static void
mi_console_file_flush(struct ui_file * file)1285796c8dcSSimon Schubert mi_console_file_flush (struct ui_file *file)
1295796c8dcSSimon Schubert {
1305796c8dcSSimon Schubert struct mi_console_file *mi_console = ui_file_data (file);
131cf7f2e2dSJohn Marino
1325796c8dcSSimon Schubert if (mi_console->magic != &mi_console_file_magic)
1335796c8dcSSimon Schubert internal_error (__FILE__, __LINE__,
1345796c8dcSSimon Schubert _("mi_console_file_flush: bad magic number"));
135*ef5ccd6cSJohn Marino
1365796c8dcSSimon Schubert ui_file_put (mi_console->buffer, mi_console_raw_packet, mi_console);
1375796c8dcSSimon Schubert ui_file_rewind (mi_console->buffer);
138*ef5ccd6cSJohn Marino
139*ef5ccd6cSJohn Marino }
140*ef5ccd6cSJohn Marino
141*ef5ccd6cSJohn Marino /* Change the underlying stream of the console directly; this is
142*ef5ccd6cSJohn Marino useful as a minimum-impact way to reflect external changes like
143*ef5ccd6cSJohn Marino logging enable/disable. */
144*ef5ccd6cSJohn Marino
145*ef5ccd6cSJohn Marino void
mi_console_set_raw(struct ui_file * file,struct ui_file * raw)146*ef5ccd6cSJohn Marino mi_console_set_raw (struct ui_file *file, struct ui_file *raw)
147*ef5ccd6cSJohn Marino {
148*ef5ccd6cSJohn Marino struct mi_console_file *mi_console = ui_file_data (file);
149*ef5ccd6cSJohn Marino
150*ef5ccd6cSJohn Marino if (mi_console->magic != &mi_console_file_magic)
151*ef5ccd6cSJohn Marino internal_error (__FILE__, __LINE__,
152*ef5ccd6cSJohn Marino _("mi_console_file_set_raw: bad magic number"));
153*ef5ccd6cSJohn Marino
154*ef5ccd6cSJohn Marino mi_console->raw = raw;
1555796c8dcSSimon Schubert }
156