GDB (xrefs)
mi-console.c
Go to the documentation of this file.
1 /* MI Console code.
2 
3  Copyright (C) 2000-2015 Free Software Foundation, Inc.
4 
5  Contributed by Cygnus Solutions (a Red Hat company).
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
11  the Free Software Foundation; either version 3 of the License, or
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
20  along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 
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 
27 #include "defs.h"
28 #include "mi-console.h"
32 
34  {
35  int *magic;
36  struct ui_file *raw;
37  struct ui_file *buffer;
38  const char *prefix;
39  char quote;
40  };
41 
42 /* Use the address of this otherwise-unused global as a magic number
43  identifying this class of ui_file objects. */
45 
46 /* Create a console that wraps the given output stream RAW with the
47  string PREFIX and quoting it with QUOTE. */
48 
49 struct ui_file *
50 mi_console_file_new (struct ui_file *raw, const char *prefix, char quote)
51 {
52  struct ui_file *ui_file = ui_file_new ();
53  struct mi_console_file *mi_console = XNEW (struct mi_console_file);
54 
55  mi_console->magic = &mi_console_file_magic;
56  mi_console->raw = raw;
57  mi_console->buffer = mem_fileopen ();
58  mi_console->prefix = prefix;
59  mi_console->quote = quote;
62  set_ui_file_data (ui_file, mi_console, mi_console_file_delete);
63 
64  return ui_file;
65 }
66 
67 static void
69 {
70  struct mi_console_file *mi_console = ui_file_data (file);
71 
72  if (mi_console->magic != &mi_console_file_magic)
73  internal_error (__FILE__, __LINE__,
74  _("mi_console_file_delete: bad magic number"));
75 
76  xfree (mi_console);
77 }
78 
79 static void
80 mi_console_file_fputs (const char *buf, struct ui_file *file)
81 {
82  struct mi_console_file *mi_console = ui_file_data (file);
83 
84  if (mi_console->magic != &mi_console_file_magic)
85  internal_error (__FILE__, __LINE__,
86  "mi_console_file_fputs: bad magic number");
87 
88  /* Append the text to our internal buffer. */
89  fputs_unfiltered (buf, mi_console->buffer);
90  /* Flush when an embedded newline is present anywhere in the buffer. */
91  if (strchr (buf, '\n') != NULL)
92  gdb_flush (file);
93 }
94 
95 /* Transform a byte sequence into a console output packet. */
96 
97 static void
98 mi_console_raw_packet (void *data, const char *buf, long length_buf)
99 {
100  struct mi_console_file *mi_console = data;
101 
102  if (mi_console->magic != &mi_console_file_magic)
103  internal_error (__FILE__, __LINE__,
104  _("mi_console_raw_packet: bad magic number"));
105 
106  if (length_buf > 0)
107  {
108  fputs_unfiltered (mi_console->prefix, mi_console->raw);
109  if (mi_console->quote)
110  {
111  fputc_unfiltered (mi_console->quote, mi_console->raw);
112  fputstrn_unfiltered (buf, length_buf,
113  mi_console->quote, mi_console->raw);
114  fputc_unfiltered (mi_console->quote, mi_console->raw);
115  fputc_unfiltered ('\n', mi_console->raw);
116  }
117  else
118  {
119  fputstrn_unfiltered (buf, length_buf, 0, mi_console->raw);
120  fputc_unfiltered ('\n', mi_console->raw);
121  }
122  gdb_flush (mi_console->raw);
123  }
124 }
125 
126 static void
128 {
129  struct mi_console_file *mi_console = ui_file_data (file);
130 
131  if (mi_console->magic != &mi_console_file_magic)
132  internal_error (__FILE__, __LINE__,
133  _("mi_console_file_flush: bad magic number"));
134 
135  ui_file_put (mi_console->buffer, mi_console_raw_packet, mi_console);
136  ui_file_rewind (mi_console->buffer);
137 
138 }
139 
140 /* Change the underlying stream of the console directly; this is
141  useful as a minimum-impact way to reflect external changes like
142  logging enable/disable. */
143 
144 void
145 mi_console_set_raw (struct ui_file *file, struct ui_file *raw)
146 {
147  struct mi_console_file *mi_console = ui_file_data (file);
148 
149  if (mi_console->magic != &mi_console_file_magic)
150  internal_error (__FILE__, __LINE__,
151  _("mi_console_file_set_raw: bad magic number"));
152 
153  mi_console->raw = raw;
154 }
void( ui_file_fputs_ftype)(const char *, struct ui_file *stream)
Definition: ui-file.h:43
void fputs_unfiltered(const char *buf, struct ui_file *file)
Definition: ui-file.c:252
void xfree(void *)
Definition: common-utils.c:97
void mi_console_set_raw(struct ui_file *file, struct ui_file *raw)
Definition: mi-console.c:145
void( ui_file_delete_ftype)(struct ui_file *stream)
Definition: ui-file.h:78
struct ui_file * buffer
Definition: mi-console.c:37
static ui_file_delete_ftype mi_console_file_delete
Definition: mi-console.c:31
static ui_file_flush_ftype mi_console_file_flush
Definition: mi-console.c:30
void ui_file_put(struct ui_file *file, ui_file_put_method_ftype *write, void *dest)
Definition: ui-file.c:210
void internal_error(const char *file, int line, const char *fmt,...)
Definition: errors.c:50
static int mi_console_file_magic
Definition: mi-console.c:44
#define _(String)
Definition: gdb_locale.h:40
const char * prefix
Definition: mi-console.c:38
int fputc_unfiltered(int c, struct ui_file *stream)
Definition: utils.c:2169
void set_ui_file_flush(struct ui_file *file, ui_file_flush_ftype *flush_ptr)
Definition: ui-file.c:258
struct ui_file * ui_file_new(void)
Definition: ui-file.c:57
void( ui_file_flush_ftype)(struct ui_file *stream)
Definition: ui-file.h:32
void fputstrn_unfiltered(const char *str, int n, int quoter, struct ui_file *stream)
Definition: utils.c:1583
static void mi_console_raw_packet(void *data, const char *buf, long length_buf)
Definition: mi-console.c:98
struct ui_file * mem_fileopen(void)
Definition: ui-file.c:427
void set_ui_file_data(struct ui_file *file, void *data, ui_file_delete_ftype *delete_ptr)
Definition: ui-file.c:314
void set_ui_file_fputs(struct ui_file *file, ui_file_fputs_ftype *fputs_ptr)
Definition: ui-file.c:302
void ui_file_rewind(struct ui_file *file)
Definition: ui-file.c:204
void * ui_file_data(struct ui_file *file)
Definition: ui-file.c:183
static ui_file_fputs_ftype mi_console_file_fputs
Definition: mi-console.c:29
struct ui_file * raw
Definition: mi-console.c:36
void gdb_flush(struct ui_file *file)
Definition: ui-file.c:192
struct ui_file * mi_console_file_new(struct ui_file *raw, const char *prefix, char quote)
Definition: mi-console.c:50