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