]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/tui/tui-file.c
More fixes for memory access violations triggered by running readelf on fuzzed binaries.
[thirdparty/binutils-gdb.git] / gdb / tui / tui-file.c
CommitLineData
d9fcf2fb 1/* UI_FILE - a generic STDIO like output stream.
32d0add0 2 Copyright (C) 1999-2015 Free Software Foundation, Inc.
da59e081
JM
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
a9762ec7 8 the Free Software Foundation; either version 3 of the License, or
da59e081
JM
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
a9762ec7 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
da59e081
JM
18
19#include "defs.h"
d9fcf2fb 20#include "ui-file.h"
da59e081 21#include "tui/tui-file.h"
d7b2e967 22#include "tui/tui-io.h"
da59e081 23
fbc75a32 24#include "tui.h"
fbc75a32 25
d9fcf2fb 26/* A ``struct ui_file'' that is compatible with all the legacy
1cc6d956 27 code. */
da59e081
JM
28
29/* new */
30enum streamtype
31{
32 afile,
33 astring
34};
35
36/* new */
37struct tui_stream
38{
39 int *ts_magic;
40 enum streamtype ts_streamtype;
41 FILE *ts_filestream;
42 char *ts_strbuf;
43 int ts_buflen;
44};
45
d9fcf2fb
JM
46static ui_file_flush_ftype tui_file_flush;
47extern ui_file_fputs_ftype tui_file_fputs;
48static ui_file_isatty_ftype tui_file_isatty;
49static ui_file_rewind_ftype tui_file_rewind;
50static ui_file_put_ftype tui_file_put;
51static ui_file_delete_ftype tui_file_delete;
a14ed312 52static struct ui_file *tui_file_new (void);
da59e081
JM
53static int tui_file_magic;
54
d9fcf2fb 55static struct ui_file *
fba45db2 56tui_file_new (void)
da59e081 57{
70ba0933 58 struct tui_stream *tui = XNEW (struct tui_stream);
d9fcf2fb 59 struct ui_file *file = ui_file_new ();
1c5313c5 60
d9fcf2fb
JM
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);
da59e081
JM
67 tui->ts_magic = &tui_file_magic;
68 return file;
69}
70
71static void
fba45db2 72tui_file_delete (struct ui_file *file)
da59e081 73{
d9fcf2fb 74 struct tui_stream *tmpstream = ui_file_data (file);
1c5313c5 75
da59e081 76 if (tmpstream->ts_magic != &tui_file_magic)
8e65ff28 77 internal_error (__FILE__, __LINE__,
e2e0b3e5 78 _("tui_file_delete: bad magic number"));
e5908723
MS
79 if ((tmpstream->ts_streamtype == astring)
80 && (tmpstream->ts_strbuf != NULL))
da59e081 81 {
b8c9b27d 82 xfree (tmpstream->ts_strbuf);
da59e081 83 }
b8c9b27d 84 xfree (tmpstream);
da59e081
JM
85}
86
d9fcf2fb 87struct ui_file *
fba45db2 88tui_fileopen (FILE *stream)
da59e081 89{
d9fcf2fb
JM
90 struct ui_file *file = tui_file_new ();
91 struct tui_stream *tmpstream = ui_file_data (file);
1c5313c5 92
da59e081
JM
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
d9fcf2fb 100struct ui_file *
fba45db2 101tui_sfileopen (int n)
da59e081 102{
d9fcf2fb
JM
103 struct ui_file *file = tui_file_new ();
104 struct tui_stream *tmpstream = ui_file_data (file);
1c5313c5 105
da59e081
JM
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
1cc6d956
MS
114 /* Do not allocate the buffer now. The first time something is
115 printed one will be allocated by tui_file_adjust_strbuf(). */
da59e081
JM
116 tmpstream->ts_strbuf = NULL;
117 tmpstream->ts_buflen = n;
118 return file;
119}
120
121static int
fba45db2 122tui_file_isatty (struct ui_file *file)
da59e081 123{
d9fcf2fb 124 struct tui_stream *stream = ui_file_data (file);
1c5313c5 125
da59e081 126 if (stream->ts_magic != &tui_file_magic)
8e65ff28 127 internal_error (__FILE__, __LINE__,
e2e0b3e5 128 _("tui_file_isatty: bad magic number"));
da59e081
JM
129 if (stream->ts_streamtype == afile)
130 return (isatty (fileno (stream->ts_filestream)));
131 else
132 return 0;
133}
134
135static void
fba45db2 136tui_file_rewind (struct ui_file *file)
da59e081 137{
d9fcf2fb 138 struct tui_stream *stream = ui_file_data (file);
1c5313c5 139
da59e081 140 if (stream->ts_magic != &tui_file_magic)
8e65ff28 141 internal_error (__FILE__, __LINE__,
e2e0b3e5 142 _("tui_file_rewind: bad magic number"));
da59e081
JM
143 stream->ts_strbuf[0] = '\0';
144}
145
146static void
d9fcf2fb
JM
147tui_file_put (struct ui_file *file,
148 ui_file_put_method_ftype *write,
da59e081
JM
149 void *dest)
150{
d9fcf2fb 151 struct tui_stream *stream = ui_file_data (file);
1c5313c5 152
da59e081 153 if (stream->ts_magic != &tui_file_magic)
8e65ff28 154 internal_error (__FILE__, __LINE__,
e2e0b3e5 155 _("tui_file_put: bad magic number"));
da59e081
JM
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
1cc6d956 167/* FIXME: Should be broken up and moved to a TUI specific file. */
da59e081
JM
168
169void
fba45db2 170tui_file_fputs (const char *linebuffer, struct ui_file *file)
da59e081 171{
d9fcf2fb 172 struct tui_stream *stream = ui_file_data (file);
e42acc6b
SC
173
174 if (stream->ts_streamtype == astring)
175 {
176 tui_file_adjust_strbuf (strlen (linebuffer), file);
177 strcat (stream->ts_strbuf, linebuffer);
178 }
da59e081
JM
179 else
180 {
174a4a09 181 tui_puts (linebuffer);
da59e081
JM
182 }
183}
184
185char *
d9fcf2fb 186tui_file_get_strbuf (struct ui_file *file)
da59e081 187{
d9fcf2fb 188 struct tui_stream *stream = ui_file_data (file);
1c5313c5 189
da59e081 190 if (stream->ts_magic != &tui_file_magic)
8e65ff28 191 internal_error (__FILE__, __LINE__,
e2e0b3e5 192 _("tui_file_get_strbuf: bad magic number"));
da59e081
JM
193 return (stream->ts_strbuf);
194}
195
1cc6d956
MS
196/* Adjust the length of the buffer by the amount necessary to
197 accomodate appending a string of length N to the buffer
198 contents. */
da59e081 199void
d9fcf2fb 200tui_file_adjust_strbuf (int n, struct ui_file *file)
da59e081 201{
d9fcf2fb 202 struct tui_stream *stream = ui_file_data (file);
da59e081 203 int non_null_chars;
1c5313c5 204
da59e081 205 if (stream->ts_magic != &tui_file_magic)
8e65ff28 206 internal_error (__FILE__, __LINE__,
e2e0b3e5 207 _("tui_file_adjust_strbuf: bad magic number"));
da59e081
JM
208
209 if (stream->ts_streamtype != astring)
210 return;
211
212 if (stream->ts_strbuf)
213 {
1cc6d956 214 /* There is already a buffer allocated. */
da59e081
JM
215 non_null_chars = strlen (stream->ts_strbuf);
216
217 if (n > (stream->ts_buflen - non_null_chars - 1))
218 {
219 stream->ts_buflen = n + non_null_chars + 1;
220 stream->ts_strbuf = xrealloc (stream->ts_strbuf, stream->ts_buflen);
221 }
222 }
223 else
1cc6d956 224 /* No buffer yet, so allocate one of the desired size. */
da59e081
JM
225 stream->ts_strbuf = xmalloc ((n + 1) * sizeof (char));
226}
227
228static void
fba45db2 229tui_file_flush (struct ui_file *file)
da59e081 230{
d9fcf2fb 231 struct tui_stream *stream = ui_file_data (file);
1c5313c5 232
da59e081 233 if (stream->ts_magic != &tui_file_magic)
8e65ff28 234 internal_error (__FILE__, __LINE__,
e2e0b3e5 235 _("tui_file_flush: bad magic number"));
da59e081 236
da59e081
JM
237 switch (stream->ts_streamtype)
238 {
239 case astring:
240 break;
241 case afile:
242 fflush (stream->ts_filestream);
243 break;
244 }
245}