1 /* UI_FILE - a generic STDIO like output stream.
2 Copyright (C) 1999-2014 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
21 #include "tui/tui-file.h"
22 #include "tui/tui-io.h"
26 /* A ``struct ui_file'' that is compatible with all the legacy
40 enum streamtype ts_streamtype
;
46 static ui_file_flush_ftype tui_file_flush
;
47 extern ui_file_fputs_ftype tui_file_fputs
;
48 static ui_file_isatty_ftype tui_file_isatty
;
49 static ui_file_rewind_ftype tui_file_rewind
;
50 static ui_file_put_ftype tui_file_put
;
51 static ui_file_delete_ftype tui_file_delete
;
52 static struct ui_file
*tui_file_new (void);
53 static int tui_file_magic
;
55 static struct ui_file
*
58 struct tui_stream
*tui
= XNEW (struct tui_stream
);
59 struct ui_file
*file
= ui_file_new ();
61 set_ui_file_data (file
, tui
, tui_file_delete
);
62 set_ui_file_flush (file
, tui_file_flush
);
63 set_ui_file_fputs (file
, tui_file_fputs
);
64 set_ui_file_isatty (file
, tui_file_isatty
);
65 set_ui_file_rewind (file
, tui_file_rewind
);
66 set_ui_file_put (file
, tui_file_put
);
67 tui
->ts_magic
= &tui_file_magic
;
72 tui_file_delete (struct ui_file
*file
)
74 struct tui_stream
*tmpstream
= ui_file_data (file
);
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
))
82 xfree (tmpstream
->ts_strbuf
);
88 tui_fileopen (FILE *stream
)
90 struct ui_file
*file
= tui_file_new ();
91 struct tui_stream
*tmpstream
= ui_file_data (file
);
93 tmpstream
->ts_streamtype
= afile
;
94 tmpstream
->ts_filestream
= stream
;
95 tmpstream
->ts_strbuf
= NULL
;
96 tmpstream
->ts_buflen
= 0;
101 tui_sfileopen (int n
)
103 struct ui_file
*file
= tui_file_new ();
104 struct tui_stream
*tmpstream
= ui_file_data (file
);
106 tmpstream
->ts_streamtype
= astring
;
107 tmpstream
->ts_filestream
= NULL
;
110 tmpstream
->ts_strbuf
= xmalloc ((n
+ 1) * sizeof (char));
111 tmpstream
->ts_strbuf
[0] = '\0';
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
;
122 tui_file_isatty (struct ui_file
*file
)
124 struct tui_stream
*stream
= ui_file_data (file
);
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
)));
136 tui_file_rewind (struct ui_file
*file
)
138 struct tui_stream
*stream
= ui_file_data (file
);
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';
147 tui_file_put (struct ui_file
*file
,
148 ui_file_put_method_ftype
*write
,
151 struct tui_stream
*stream
= ui_file_data (file
);
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
));
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. */
167 /* FIXME: Should be broken up and moved to a TUI specific file. */
170 tui_file_fputs (const char *linebuffer
, struct ui_file
*file
)
172 struct tui_stream
*stream
= ui_file_data (file
);
174 if (stream
->ts_streamtype
== astring
)
176 tui_file_adjust_strbuf (strlen (linebuffer
), file
);
177 strcat (stream
->ts_strbuf
, linebuffer
);
181 tui_puts (linebuffer
);
186 tui_file_get_strbuf (struct ui_file
*file
)
188 struct tui_stream
*stream
= ui_file_data (file
);
190 if (stream
->ts_magic
!= &tui_file_magic
)
191 internal_error (__FILE__
, __LINE__
,
192 _("tui_file_get_strbuf: bad magic number"));
193 return (stream
->ts_strbuf
);
196 /* Adjust the length of the buffer by the amount necessary to
197 accomodate appending a string of length N to the buffer
200 tui_file_adjust_strbuf (int n
, struct ui_file
*file
)
202 struct tui_stream
*stream
= ui_file_data (file
);
205 if (stream
->ts_magic
!= &tui_file_magic
)
206 internal_error (__FILE__
, __LINE__
,
207 _("tui_file_adjust_strbuf: bad magic number"));
209 if (stream
->ts_streamtype
!= astring
)
212 if (stream
->ts_strbuf
)
214 /* There is already a buffer allocated. */
215 non_null_chars
= strlen (stream
->ts_strbuf
);
217 if (n
> (stream
->ts_buflen
- non_null_chars
- 1))
219 stream
->ts_buflen
= n
+ non_null_chars
+ 1;
220 stream
->ts_strbuf
= xrealloc (stream
->ts_strbuf
, stream
->ts_buflen
);
224 /* No buffer yet, so allocate one of the desired size. */
225 stream
->ts_strbuf
= xmalloc ((n
+ 1) * sizeof (char));
229 tui_file_flush (struct ui_file
*file
)
231 struct tui_stream
*stream
= ui_file_data (file
);
233 if (stream
->ts_magic
!= &tui_file_magic
)
234 internal_error (__FILE__
, __LINE__
,
235 _("tui_file_flush: bad magic number"));
237 switch (stream
->ts_streamtype
)
242 fflush (stream
->ts_filestream
);