]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/tui/tui-file.c
PARAMS removal.
[thirdparty/binutils-gdb.git] / gdb / tui / tui-file.c
CommitLineData
d9fcf2fb 1/* UI_FILE - a generic STDIO like output stream.
da59e081
JM
2 Copyright (C) 1999, 2000 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 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 *
da59e081
JM
69tui_file_new ()
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
84tui_file_delete (file)
d9fcf2fb 85 struct ui_file *file;
da59e081 86{
d9fcf2fb 87 struct tui_stream *tmpstream = ui_file_data (file);
da59e081
JM
88 if (tmpstream->ts_magic != &tui_file_magic)
89 internal_error ("tui_file_delete: bad magic number");
90 if ((tmpstream->ts_streamtype == astring) &&
91 (tmpstream->ts_strbuf != NULL))
92 {
93 free (tmpstream->ts_strbuf);
94 }
95 free (tmpstream);
96}
97
d9fcf2fb 98struct ui_file *
da59e081
JM
99tui_fileopen (stream)
100 FILE *stream;
101{
d9fcf2fb
JM
102 struct ui_file *file = tui_file_new ();
103 struct tui_stream *tmpstream = ui_file_data (file);
da59e081
JM
104 tmpstream->ts_streamtype = afile;
105 tmpstream->ts_filestream = stream;
106 tmpstream->ts_strbuf = NULL;
107 tmpstream->ts_buflen = 0;
108 return file;
109}
110
d9fcf2fb 111struct ui_file *
da59e081
JM
112tui_sfileopen (n)
113 int n;
114{
d9fcf2fb
JM
115 struct ui_file *file = tui_file_new ();
116 struct tui_stream *tmpstream = ui_file_data (file);
da59e081
JM
117 tmpstream->ts_streamtype = astring;
118 tmpstream->ts_filestream = NULL;
119 if (n > 0)
120 {
121 tmpstream->ts_strbuf = xmalloc ((n + 1) * sizeof (char));
122 tmpstream->ts_strbuf[0] = '\0';
123 }
124 else
125 /* Do not allocate the buffer now. The first time something is printed
126 one will be allocated by tui_file_adjust_strbuf() */
127 tmpstream->ts_strbuf = NULL;
128 tmpstream->ts_buflen = n;
129 return file;
130}
131
132static int
133tui_file_isatty (file)
d9fcf2fb 134 struct ui_file *file;
da59e081 135{
d9fcf2fb 136 struct tui_stream *stream = ui_file_data (file);
da59e081
JM
137 if (stream->ts_magic != &tui_file_magic)
138 internal_error ("tui_file_isatty: bad magic number");
139 if (stream->ts_streamtype == afile)
140 return (isatty (fileno (stream->ts_filestream)));
141 else
142 return 0;
143}
144
145static void
146tui_file_rewind (file)
d9fcf2fb 147 struct ui_file *file;
da59e081 148{
d9fcf2fb 149 struct tui_stream *stream = ui_file_data (file);
da59e081
JM
150 if (stream->ts_magic != &tui_file_magic)
151 internal_error ("tui_file_rewind: bad magic number");
152 stream->ts_strbuf[0] = '\0';
153}
154
155static void
d9fcf2fb
JM
156tui_file_put (struct ui_file *file,
157 ui_file_put_method_ftype *write,
da59e081
JM
158 void *dest)
159{
d9fcf2fb 160 struct tui_stream *stream = ui_file_data (file);
da59e081
JM
161 if (stream->ts_magic != &tui_file_magic)
162 internal_error ("tui_file_put: bad magic number");
163 if (stream->ts_streamtype == astring)
164 write (dest, stream->ts_strbuf, strlen (stream->ts_strbuf));
165}
166
167/* All TUI I/O sent to the *_filtered and *_unfiltered functions
168 eventually ends up here. The fputs_unfiltered_hook is primarily
169 used by GUIs to collect all output and send it to the GUI, instead
170 of the controlling terminal. Only output to gdb_stdout and
171 gdb_stderr are sent to the hook. Everything else is sent on to
172 fputs to allow file I/O to be handled appropriately. */
173
174/* FIXME: Should be broken up and moved to a TUI specific file. */
175
176void
177tui_file_fputs (linebuffer, file)
178 const char *linebuffer;
d9fcf2fb 179 struct ui_file *file;
da59e081 180{
d9fcf2fb 181 struct tui_stream *stream = ui_file_data (file);
da59e081
JM
182#if defined(TUI)
183 extern int tui_owns_terminal;
184#endif
185 /* NOTE: cagney/1999-10-13: The use of fputs_unfiltered_hook is
186 seriously discouraged. Those wanting to hook output should
d9fcf2fb 187 instead implement their own ui_file object and install that. See
da59e081
JM
188 also tui_file_flush(). */
189 if (fputs_unfiltered_hook
190 && (file == gdb_stdout
191 || file == gdb_stderr))
192 fputs_unfiltered_hook (linebuffer, file);
193 else
194 {
195#if defined(TUI)
196 if (tui_version && tui_owns_terminal)
197 {
198 /* If we get here somehow while updating the TUI (from
199 * within a tuiDo(), then we need to temporarily
200 * set up the terminal for GDB output. This probably just
201 * happens on error output.
202 */
203
204 if (stream->ts_streamtype == astring)
205 {
fbc75a32 206 tui_file_adjust_strbuf (strlen (linebuffer), file);
da59e081
JM
207 strcat (stream->ts_strbuf, linebuffer);
208 }
209 else
210 {
211 tuiTermUnsetup (0, (tui_version) ? cmdWin->detail.commandInfo.curch : 0);
212 fputs (linebuffer, stream->ts_filestream);
213 tuiTermSetup (0);
214 if (linebuffer[strlen (linebuffer) - 1] == '\n')
215 tuiClearCommandCharCount ();
216 else
217 tuiIncrCommandCharCountBy (strlen (linebuffer));
218 }
219 }
220 else
221 {
222 /* The normal case - just do a fputs() */
223 if (stream->ts_streamtype == astring)
224 {
fbc75a32 225 tui_file_adjust_strbuf (strlen (linebuffer), file);
da59e081
JM
226 strcat (stream->ts_strbuf, linebuffer);
227 }
228 else
229 fputs (linebuffer, stream->ts_filestream);
230 }
231
232
233#else
234 if (stream->ts_streamtype == astring)
235 {
236 tui_file_adjust_strbuf (strlen (linebuffer), file);
237 strcat (stream->ts_strbuf, linebuffer);
238 }
239 else
240 fputs (linebuffer, stream->ts_filestream);
241#endif
242 }
243}
244
245char *
d9fcf2fb 246tui_file_get_strbuf (struct ui_file *file)
da59e081 247{
d9fcf2fb 248 struct tui_stream *stream = ui_file_data (file);
da59e081
JM
249 if (stream->ts_magic != &tui_file_magic)
250 internal_error ("tui_file_get_strbuf: bad magic number");
251 return (stream->ts_strbuf);
252}
253
254/* adjust the length of the buffer by the amount necessary
255 to accomodate appending a string of length N to the buffer contents */
256void
d9fcf2fb 257tui_file_adjust_strbuf (int n, struct ui_file *file)
da59e081 258{
d9fcf2fb 259 struct tui_stream *stream = ui_file_data (file);
da59e081
JM
260 int non_null_chars;
261 if (stream->ts_magic != &tui_file_magic)
262 internal_error ("tui_file_adjust_strbuf: bad magic number");
263
264 if (stream->ts_streamtype != astring)
265 return;
266
267 if (stream->ts_strbuf)
268 {
269 /* There is already a buffer allocated */
270 non_null_chars = strlen (stream->ts_strbuf);
271
272 if (n > (stream->ts_buflen - non_null_chars - 1))
273 {
274 stream->ts_buflen = n + non_null_chars + 1;
275 stream->ts_strbuf = xrealloc (stream->ts_strbuf, stream->ts_buflen);
276 }
277 }
278 else
279 /* No buffer yet, so allocate one of the desired size */
280 stream->ts_strbuf = xmalloc ((n + 1) * sizeof (char));
281}
282
283static void
284tui_file_flush (file)
d9fcf2fb 285 struct ui_file *file;
da59e081 286{
d9fcf2fb 287 struct tui_stream *stream = ui_file_data (file);
da59e081
JM
288 if (stream->ts_magic != &tui_file_magic)
289 internal_error ("tui_file_flush: bad magic number");
290
291 /* NOTE: cagney/1999-10-12: If we've been linked with code that uses
292 fputs_unfiltered_hook then we assume that it doesn't need to know
293 about flushes. Code that does need to know about flushes can
d9fcf2fb 294 implement a proper ui_file object. */
da59e081
JM
295 if (fputs_unfiltered_hook)
296 return;
297
298 switch (stream->ts_streamtype)
299 {
300 case astring:
301 break;
302 case afile:
303 fflush (stream->ts_filestream);
304 break;
305 }
306}