1 /* GDB_FILE - a generic STDIO like output stream.
2 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
23 #include "tui/tui-file.h"
27 /* Called instead of fputs for all TUI_FILE output. */
29 void (*fputs_unfiltered_hook
) (const char *linebuffer
, GDB_FILE
* stream
);
31 /* A ``struct gdb_file'' that is compatible with all the legacy
45 enum streamtype ts_streamtype
;
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
;
60 static struct gdb_file
*
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
;
76 tui_file_delete (file
)
77 struct gdb_file
*file
;
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
))
85 free (tmpstream
->ts_strbuf
);
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;
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
;
113 tmpstream
->ts_strbuf
= xmalloc ((n
+ 1) * sizeof (char));
114 tmpstream
->ts_strbuf
[0] = '\0';
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
;
125 tui_file_isatty (file
)
126 struct gdb_file
*file
;
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
)));
138 tui_file_rewind (file
)
139 struct gdb_file
*file
;
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';
148 tui_file_put (struct gdb_file
*file
,
149 gdb_file_put_method_ftype
*write
,
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
));
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. */
166 /* FIXME: Should be broken up and moved to a TUI specific file. */
169 tui_file_fputs (linebuffer
, file
)
170 const char *linebuffer
;
173 struct tui_stream
*stream
= gdb_file_data (file
);
175 extern int tui_owns_terminal
;
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
);
188 if (tui_version
&& tui_owns_terminal
)
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.
196 if (stream
->ts_streamtype
== astring
)
198 tui_file_adjust_strbuf (strlen (linebuffer
), stream
);
199 strcat (stream
->ts_strbuf
, linebuffer
);
203 tuiTermUnsetup (0, (tui_version
) ? cmdWin
->detail
.commandInfo
.curch
: 0);
204 fputs (linebuffer
, stream
->ts_filestream
);
206 if (linebuffer
[strlen (linebuffer
) - 1] == '\n')
207 tuiClearCommandCharCount ();
209 tuiIncrCommandCharCountBy (strlen (linebuffer
));
214 /* The normal case - just do a fputs() */
215 if (stream
->ts_streamtype
== astring
)
217 tui_file_adjust_strbuf (strlen (linebuffer
), stream
);
218 strcat (stream
->ts_strbuf
, linebuffer
);
221 fputs (linebuffer
, stream
->ts_filestream
);
226 if (stream
->ts_streamtype
== astring
)
228 tui_file_adjust_strbuf (strlen (linebuffer
), file
);
229 strcat (stream
->ts_strbuf
, linebuffer
);
232 fputs (linebuffer
, stream
->ts_filestream
);
238 tui_file_get_strbuf (struct gdb_file
*file
)
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
);
246 /* adjust the length of the buffer by the amount necessary
247 to accomodate appending a string of length N to the buffer contents */
249 tui_file_adjust_strbuf (int n
, struct gdb_file
*file
)
251 struct tui_stream
*stream
= gdb_file_data (file
);
253 if (stream
->ts_magic
!= &tui_file_magic
)
254 internal_error ("tui_file_adjust_strbuf: bad magic number");
256 if (stream
->ts_streamtype
!= astring
)
259 if (stream
->ts_strbuf
)
261 /* There is already a buffer allocated */
262 non_null_chars
= strlen (stream
->ts_strbuf
);
264 if (n
> (stream
->ts_buflen
- non_null_chars
- 1))
266 stream
->ts_buflen
= n
+ non_null_chars
+ 1;
267 stream
->ts_strbuf
= xrealloc (stream
->ts_strbuf
, stream
->ts_buflen
);
271 /* No buffer yet, so allocate one of the desired size */
272 stream
->ts_strbuf
= xmalloc ((n
+ 1) * sizeof (char));
276 tui_file_flush (file
)
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");
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
)
290 switch (stream
->ts_streamtype
)
295 fflush (stream
->ts_filestream
);