GDB (xrefs)
tui-file.c
Go to the documentation of this file.
1 /* UI_FILE - a generic STDIO like output stream.
2  Copyright (C) 1999-2015 Free Software Foundation, Inc.
3 
4  This file is part of GDB.
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 
19 #include "defs.h"
20 #include "ui-file.h"
21 #include "tui/tui-file.h"
22 #include "tui/tui-io.h"
23 #include "tui/tui-command.h"
24 #include "tui.h"
25 
26 /* A ``struct ui_file'' that is compatible with all the legacy
27  code. */
28 
29 /* new */
31 {
34 };
35 
36 /* new */
37 struct tui_stream
38 {
39  int *ts_magic;
42  char *ts_strbuf;
43  int ts_buflen;
44 };
45 
52 static struct ui_file *tui_file_new (void);
53 static int tui_file_magic;
54 
55 static struct ui_file *
57 {
58  struct tui_stream *tui = XNEW (struct tui_stream);
59  struct ui_file *file = ui_file_new ();
60 
61  set_ui_file_data (file, tui, tui_file_delete);
67  tui->ts_magic = &tui_file_magic;
68  return file;
69 }
70 
71 static void
72 tui_file_delete (struct ui_file *file)
73 {
74  struct tui_stream *tmpstream = ui_file_data (file);
75 
76  if (tmpstream->ts_magic != &tui_file_magic)
77  internal_error (__FILE__, __LINE__,
78  _("tui_file_delete: bad magic number"));
79  if ((tmpstream->ts_streamtype == astring)
80  && (tmpstream->ts_strbuf != NULL))
81  {
82  xfree (tmpstream->ts_strbuf);
83  }
84  xfree (tmpstream);
85 }
86 
87 struct ui_file *
88 tui_fileopen (FILE *stream)
89 {
90  struct ui_file *file = tui_file_new ();
91  struct tui_stream *tmpstream = ui_file_data (file);
92 
93  tmpstream->ts_streamtype = afile;
94  tmpstream->ts_filestream = stream;
95  tmpstream->ts_strbuf = NULL;
96  tmpstream->ts_buflen = 0;
97  return file;
98 }
99 
100 struct ui_file *
102 {
103  struct ui_file *file = tui_file_new ();
104  struct tui_stream *tmpstream = ui_file_data (file);
105 
106  tmpstream->ts_streamtype = astring;
107  tmpstream->ts_filestream = NULL;
108  if (n > 0)
109  {
110  tmpstream->ts_strbuf = xmalloc ((n + 1) * sizeof (char));
111  tmpstream->ts_strbuf[0] = '\0';
112  }
113  else
114  /* Do not allocate the buffer now. The first time something is
115  printed one will be allocated by tui_file_adjust_strbuf(). */
116  tmpstream->ts_strbuf = NULL;
117  tmpstream->ts_buflen = n;
118  return file;
119 }
120 
121 static int
122 tui_file_isatty (struct ui_file *file)
123 {
124  struct tui_stream *stream = ui_file_data (file);
125 
126  if (stream->ts_magic != &tui_file_magic)
127  internal_error (__FILE__, __LINE__,
128  _("tui_file_isatty: bad magic number"));
129  if (stream->ts_streamtype == afile)
130  return (isatty (fileno (stream->ts_filestream)));
131  else
132  return 0;
133 }
134 
135 static void
136 tui_file_rewind (struct ui_file *file)
137 {
138  struct tui_stream *stream = ui_file_data (file);
139 
140  if (stream->ts_magic != &tui_file_magic)
141  internal_error (__FILE__, __LINE__,
142  _("tui_file_rewind: bad magic number"));
143  stream->ts_strbuf[0] = '\0';
144 }
145 
146 static void
147 tui_file_put (struct ui_file *file,
149  void *dest)
150 {
151  struct tui_stream *stream = ui_file_data (file);
152 
153  if (stream->ts_magic != &tui_file_magic)
154  internal_error (__FILE__, __LINE__,
155  _("tui_file_put: bad magic number"));
156  if (stream->ts_streamtype == astring)
157  write (dest, stream->ts_strbuf, strlen (stream->ts_strbuf));
158 }
159 
160 /* All TUI I/O sent to the *_filtered and *_unfiltered functions
161  eventually ends up here. The fputs_unfiltered_hook is primarily
162  used by GUIs to collect all output and send it to the GUI, instead
163  of the controlling terminal. Only output to gdb_stdout and
164  gdb_stderr are sent to the hook. Everything else is sent on to
165  fputs to allow file I/O to be handled appropriately. */
166 
167 /* FIXME: Should be broken up and moved to a TUI specific file. */
168 
169 void
170 tui_file_fputs (const char *linebuffer, struct ui_file *file)
171 {
172  struct tui_stream *stream = ui_file_data (file);
173 
174  if (stream->ts_streamtype == astring)
175  {
176  tui_file_adjust_strbuf (strlen (linebuffer), file);
177  strcat (stream->ts_strbuf, linebuffer);
178  }
179  else
180  {
181  tui_puts (linebuffer);
182  /* gdb_stdout is buffered, and the caller must gdb_flush it at
183  appropriate times. Other streams are not so buffered. */
184  if (file != gdb_stdout)
186  }
187 }
188 
189 char *
191 {
192  struct tui_stream *stream = ui_file_data (file);
193 
194  if (stream->ts_magic != &tui_file_magic)
195  internal_error (__FILE__, __LINE__,
196  _("tui_file_get_strbuf: bad magic number"));
197  return (stream->ts_strbuf);
198 }
199 
200 /* Adjust the length of the buffer by the amount necessary to
201  accomodate appending a string of length N to the buffer
202  contents. */
203 void
204 tui_file_adjust_strbuf (int n, struct ui_file *file)
205 {
206  struct tui_stream *stream = ui_file_data (file);
207  int non_null_chars;
208 
209  if (stream->ts_magic != &tui_file_magic)
210  internal_error (__FILE__, __LINE__,
211  _("tui_file_adjust_strbuf: bad magic number"));
212 
213  if (stream->ts_streamtype != astring)
214  return;
215 
216  if (stream->ts_strbuf)
217  {
218  /* There is already a buffer allocated. */
219  non_null_chars = strlen (stream->ts_strbuf);
220 
221  if (n > (stream->ts_buflen - non_null_chars - 1))
222  {
223  stream->ts_buflen = n + non_null_chars + 1;
224  stream->ts_strbuf = xrealloc (stream->ts_strbuf, stream->ts_buflen);
225  }
226  }
227  else
228  /* No buffer yet, so allocate one of the desired size. */
229  stream->ts_strbuf = xmalloc ((n + 1) * sizeof (char));
230 }
231 
232 static void
233 tui_file_flush (struct ui_file *file)
234 {
235  struct tui_stream *stream = ui_file_data (file);
236 
237  if (stream->ts_magic != &tui_file_magic)
238  internal_error (__FILE__, __LINE__,
239  _("tui_file_flush: bad magic number"));
240 
241  switch (stream->ts_streamtype)
242  {
243  case astring:
244  break;
245  case afile:
246  /* gdb_stdout is buffered. Other files are always flushed on
247  every write. */
248  if (file == gdb_stdout)
250  fflush (stream->ts_filestream);
251  break;
252  }
253 }
char * tui_file_get_strbuf(struct ui_file *file)
Definition: tui-file.c:190
FILE * ts_filestream
Definition: tui-file.c:41
void set_ui_file_isatty(struct ui_file *file, ui_file_isatty_ftype *isatty_ptr)
Definition: ui-file.c:264
static ui_file_isatty_ftype tui_file_isatty
Definition: tui-file.c:48
void( ui_file_fputs_ftype)(const char *, struct ui_file *stream)
Definition: ui-file.h:43
void xfree(void *)
Definition: common-utils.c:97
void( ui_file_delete_ftype)(struct ui_file *stream)
Definition: ui-file.h:78
void tui_refresh_cmd_win(void)
Definition: tui-command.c:95
struct ui_file * gdb_stdout
Definition: main.c:71
void internal_error(const char *file, int line, const char *fmt,...)
Definition: errors.c:50
static ui_file_flush_ftype tui_file_flush
Definition: tui-file.c:46
#define _(String)
Definition: gdb_locale.h:40
char * ts_strbuf
Definition: tui-file.c:42
void tui_puts(const char *string)
Definition: tui-io.c:154
void set_ui_file_rewind(struct ui_file *file, ui_file_rewind_ftype *rewind_ptr)
Definition: ui-file.c:270
static struct ui_file * tui_file_new(void)
Definition: tui-file.c:56
void set_ui_file_flush(struct ui_file *file, ui_file_flush_ftype *flush_ptr)
Definition: ui-file.c:258
ui_file_fputs_ftype tui_file_fputs
struct ui_file * ui_file_new(void)
Definition: ui-file.c:57
struct ui_file * tui_sfileopen(int n)
Definition: tui-file.c:101
static int tui_file_magic
Definition: tui-file.c:53
enum streamtype ts_streamtype
Definition: tui-file.c:40
int * ts_magic
Definition: tui-file.c:39
void( ui_file_flush_ftype)(struct ui_file *stream)
Definition: ui-file.h:32
streamtype
Definition: tui-file.c:30
Definition: tui-file.c:32
static ui_file_put_ftype tui_file_put
Definition: tui-file.c:50
void * xmalloc(YYSIZE_T)
int ts_buflen
Definition: tui-file.c:43
void( ui_file_put_ftype)(struct ui_file *stream, ui_file_put_method_ftype *method, void *context)
Definition: ui-file.h:73
PTR xrealloc(PTR ptr, size_t size)
Definition: common-utils.c:51
int( ui_file_isatty_ftype)(struct ui_file *stream)
Definition: ui-file.h:63
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
struct ui_file * tui_fileopen(FILE *stream)
Definition: tui-file.c:88
static ui_file_delete_ftype tui_file_delete
Definition: tui-file.c:51
void * ui_file_data(struct ui_file *file)
Definition: ui-file.c:183
void tui_file_adjust_strbuf(int n, struct ui_file *file)
Definition: tui-file.c:204
void( ui_file_rewind_ftype)(struct ui_file *stream)
Definition: ui-file.h:67
void set_ui_file_put(struct ui_file *file, ui_file_put_ftype *put_ptr)
Definition: ui-file.c:276
static ui_file_rewind_ftype tui_file_rewind
Definition: tui-file.c:49
void( ui_file_put_method_ftype)(void *object, const char *buffer, long length_buffer)
Definition: ui-file.h:71