Add __FILE__ and __LINE__ parameter to internal_error() /
[deliverable/binutils-gdb.git] / gdb / tui / tui-file.c
CommitLineData
d9fcf2fb 1/* UI_FILE - a generic STDIO like output stream.
8e65ff28 2 Copyright 1999, 2000, 2001 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
8 the Free Software Foundation; either version 2 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, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21#include "defs.h"
d9fcf2fb 22#include "ui-file.h"
da59e081
JM
23#include "tui/tui-file.h"
24
fbc75a32
AC
25#ifdef TUI
26#include "tui.h"
27#include "tuiData.h"
28#include "tuiIO.h"
29#include "tuiCommand.h"
30#endif
31
da59e081
JM
32#include <string.h>
33
34/* Called instead of fputs for all TUI_FILE output. */
35
d9fcf2fb
JM
36void (*fputs_unfiltered_hook) (const char *linebuffer,
37 struct ui_file * stream);
da59e081 38
d9fcf2fb 39/* A ``struct ui_file'' that is compatible with all the legacy
da59e081
JM
40 code. */
41
42/* new */
43enum streamtype
44{
45 afile,
46 astring
47};
48
49/* new */
50struct tui_stream
51{
52 int *ts_magic;
53 enum streamtype ts_streamtype;
54 FILE *ts_filestream;
55 char *ts_strbuf;
56 int ts_buflen;
57};
58
d9fcf2fb
JM
59static ui_file_flush_ftype tui_file_flush;
60extern ui_file_fputs_ftype tui_file_fputs;
61static ui_file_isatty_ftype tui_file_isatty;
62static ui_file_rewind_ftype tui_file_rewind;
63static ui_file_put_ftype tui_file_put;
64static ui_file_delete_ftype tui_file_delete;
a14ed312 65static struct ui_file *tui_file_new (void);
da59e081
JM
66static int tui_file_magic;
67
d9fcf2fb 68static struct ui_file *
fba45db2 69tui_file_new (void)
da59e081
JM
70{
71 struct tui_stream *tui = xmalloc (sizeof (struct tui_stream));
d9fcf2fb
JM
72 struct ui_file *file = ui_file_new ();
73 set_ui_file_data (file, tui, tui_file_delete);
74 set_ui_file_flush (file, tui_file_flush);
75 set_ui_file_fputs (file, tui_file_fputs);
76 set_ui_file_isatty (file, tui_file_isatty);
77 set_ui_file_rewind (file, tui_file_rewind);
78 set_ui_file_put (file, tui_file_put);
da59e081
JM
79 tui->ts_magic = &tui_file_magic;
80 return file;
81}
82
83static void
fba45db2 84tui_file_delete (struct ui_file *file)
da59e081 85{
d9fcf2fb 86 struct tui_stream *tmpstream = ui_file_data (file);
da59e081 87 if (tmpstream->ts_magic != &tui_file_magic)
8e65ff28
AC
88 internal_error (__FILE__, __LINE__,
89 "tui_file_delete: bad magic number");
da59e081
JM
90 if ((tmpstream->ts_streamtype == astring) &&
91 (tmpstream->ts_strbuf != NULL))
92 {
b8c9b27d 93 xfree (tmpstream->ts_strbuf);
da59e081 94 }
b8c9b27d 95 xfree (tmpstream);
da59e081
JM
96}
97
d9fcf2fb 98struct ui_file *
fba45db2 99tui_fileopen (FILE *stream)
da59e081 100{
d9fcf2fb
JM
101 struct ui_file *file = tui_file_new ();
102 struct tui_stream *tmpstream = ui_file_data (file);
da59e081
JM
103 tmpstream->ts_streamtype = afile;
104 tmpstream->ts_filestream = stream;
105 tmpstream->ts_strbuf = NULL;
106 tmpstream->ts_buflen = 0;
107 return file;
108}
109
d9fcf2fb 110struct ui_file *
fba45db2 111tui_sfileopen (int n)
da59e081 112{
d9fcf2fb
JM
113 struct ui_file *file = tui_file_new ();
114 struct tui_stream *tmpstream = ui_file_data (file);
da59e081
JM
115 tmpstream->ts_streamtype = astring;
116 tmpstream->ts_filestream = NULL;
117 if (n > 0)
118 {
119 tmpstream->ts_strbuf = xmalloc ((n + 1) * sizeof (char));
120 tmpstream->ts_strbuf[0] = '\0';
121 }
122 else
123 /* Do not allocate the buffer now. The first time something is printed
124 one will be allocated by tui_file_adjust_strbuf() */
125 tmpstream->ts_strbuf = NULL;
126 tmpstream->ts_buflen = n;
127 return file;
128}
129
130static int
fba45db2 131tui_file_isatty (struct ui_file *file)
da59e081 132{
d9fcf2fb 133 struct tui_stream *stream = ui_file_data (file);
da59e081 134 if (stream->ts_magic != &tui_file_magic)
8e65ff28
AC
135 internal_error (__FILE__, __LINE__,
136 "tui_file_isatty: bad magic number");
da59e081
JM
137 if (stream->ts_streamtype == afile)
138 return (isatty (fileno (stream->ts_filestream)));
139 else
140 return 0;
141}
142
143static void
fba45db2 144tui_file_rewind (struct ui_file *file)
da59e081 145{
d9fcf2fb 146 struct tui_stream *stream = ui_file_data (file);
da59e081 147 if (stream->ts_magic != &tui_file_magic)
8e65ff28
AC
148 internal_error (__FILE__, __LINE__,
149 "tui_file_rewind: bad magic number");
da59e081
JM
150 stream->ts_strbuf[0] = '\0';
151}
152
153static void
d9fcf2fb
JM
154tui_file_put (struct ui_file *file,
155 ui_file_put_method_ftype *write,
da59e081
JM
156 void *dest)
157{
d9fcf2fb 158 struct tui_stream *stream = ui_file_data (file);
da59e081 159 if (stream->ts_magic != &tui_file_magic)
8e65ff28
AC
160 internal_error (__FILE__, __LINE__,
161 "tui_file_put: bad magic number");
da59e081
JM
162 if (stream->ts_streamtype == astring)
163 write (dest, stream->ts_strbuf, strlen (stream->ts_strbuf));
164}
165
166/* All TUI I/O sent to the *_filtered and *_unfiltered functions
167 eventually ends up here. The fputs_unfiltered_hook is primarily
168 used by GUIs to collect all output and send it to the GUI, instead
169 of the controlling terminal. Only output to gdb_stdout and
170 gdb_stderr are sent to the hook. Everything else is sent on to
171 fputs to allow file I/O to be handled appropriately. */
172
173/* FIXME: Should be broken up and moved to a TUI specific file. */
174
175void
fba45db2 176tui_file_fputs (const char *linebuffer, struct ui_file *file)
da59e081 177{
d9fcf2fb 178 struct tui_stream *stream = ui_file_data (file);
da59e081
JM
179#if defined(TUI)
180 extern int tui_owns_terminal;
181#endif
182 /* NOTE: cagney/1999-10-13: The use of fputs_unfiltered_hook is
183 seriously discouraged. Those wanting to hook output should
d9fcf2fb 184 instead implement their own ui_file object and install that. See
da59e081
JM
185 also tui_file_flush(). */
186 if (fputs_unfiltered_hook
187 && (file == gdb_stdout
188 || file == gdb_stderr))
189 fputs_unfiltered_hook (linebuffer, file);
190 else
191 {
192#if defined(TUI)
193 if (tui_version && tui_owns_terminal)
194 {
195 /* If we get here somehow while updating the TUI (from
196 * within a tuiDo(), then we need to temporarily
197 * set up the terminal for GDB output. This probably just
198 * happens on error output.
199 */
200
201 if (stream->ts_streamtype == astring)
202 {
fbc75a32 203 tui_file_adjust_strbuf (strlen (linebuffer), file);
da59e081
JM
204 strcat (stream->ts_strbuf, linebuffer);
205 }
206 else
207 {
208 tuiTermUnsetup (0, (tui_version) ? cmdWin->detail.commandInfo.curch : 0);
209 fputs (linebuffer, stream->ts_filestream);
210 tuiTermSetup (0);
211 if (linebuffer[strlen (linebuffer) - 1] == '\n')
212 tuiClearCommandCharCount ();
213 else
214 tuiIncrCommandCharCountBy (strlen (linebuffer));
215 }
216 }
217 else
218 {
219 /* The normal case - just do a fputs() */
220 if (stream->ts_streamtype == astring)
221 {
fbc75a32 222 tui_file_adjust_strbuf (strlen (linebuffer), file);
da59e081
JM
223 strcat (stream->ts_strbuf, linebuffer);
224 }
225 else
226 fputs (linebuffer, stream->ts_filestream);
227 }
228
229
230#else
231 if (stream->ts_streamtype == astring)
232 {
233 tui_file_adjust_strbuf (strlen (linebuffer), file);
234 strcat (stream->ts_strbuf, linebuffer);
235 }
236 else
237 fputs (linebuffer, stream->ts_filestream);
238#endif
239 }
240}
241
242char *
d9fcf2fb 243tui_file_get_strbuf (struct ui_file *file)
da59e081 244{
d9fcf2fb 245 struct tui_stream *stream = ui_file_data (file);
da59e081 246 if (stream->ts_magic != &tui_file_magic)
8e65ff28
AC
247 internal_error (__FILE__, __LINE__,
248 "tui_file_get_strbuf: bad magic number");
da59e081
JM
249 return (stream->ts_strbuf);
250}
251
252/* adjust the length of the buffer by the amount necessary
253 to accomodate appending a string of length N to the buffer contents */
254void
d9fcf2fb 255tui_file_adjust_strbuf (int n, struct ui_file *file)
da59e081 256{
d9fcf2fb 257 struct tui_stream *stream = ui_file_data (file);
da59e081
JM
258 int non_null_chars;
259 if (stream->ts_magic != &tui_file_magic)
8e65ff28
AC
260 internal_error (__FILE__, __LINE__,
261 "tui_file_adjust_strbuf: bad magic number");
da59e081
JM
262
263 if (stream->ts_streamtype != astring)
264 return;
265
266 if (stream->ts_strbuf)
267 {
268 /* There is already a buffer allocated */
269 non_null_chars = strlen (stream->ts_strbuf);
270
271 if (n > (stream->ts_buflen - non_null_chars - 1))
272 {
273 stream->ts_buflen = n + non_null_chars + 1;
274 stream->ts_strbuf = xrealloc (stream->ts_strbuf, stream->ts_buflen);
275 }
276 }
277 else
278 /* No buffer yet, so allocate one of the desired size */
279 stream->ts_strbuf = xmalloc ((n + 1) * sizeof (char));
280}
281
282static void
fba45db2 283tui_file_flush (struct ui_file *file)
da59e081 284{
d9fcf2fb 285 struct tui_stream *stream = ui_file_data (file);
da59e081 286 if (stream->ts_magic != &tui_file_magic)
8e65ff28
AC
287 internal_error (__FILE__, __LINE__,
288 "tui_file_flush: bad magic number");
da59e081
JM
289
290 /* NOTE: cagney/1999-10-12: If we've been linked with code that uses
291 fputs_unfiltered_hook then we assume that it doesn't need to know
292 about flushes. Code that does need to know about flushes can
d9fcf2fb 293 implement a proper ui_file object. */
da59e081
JM
294 if (fputs_unfiltered_hook)
295 return;
296
297 switch (stream->ts_streamtype)
298 {
299 case astring:
300 break;
301 case afile:
302 fflush (stream->ts_filestream);
303 break;
304 }
305}
This page took 0.104288 seconds and 4 git commands to generate.