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