1 /* UI_FILE - a generic STDIO like output stream.
2 Copyright (C) 1999, 2000, 2001 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., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
23 #include "tui/tui-file.h"
24 #include "tui/tui-io.h"
28 #include "gdb_string.h"
30 /* A ``struct ui_file'' that is compatible with all the legacy
44 enum streamtype ts_streamtype
;
50 static ui_file_flush_ftype tui_file_flush
;
51 extern ui_file_fputs_ftype tui_file_fputs
;
52 static ui_file_isatty_ftype tui_file_isatty
;
53 static ui_file_rewind_ftype tui_file_rewind
;
54 static ui_file_put_ftype tui_file_put
;
55 static ui_file_delete_ftype tui_file_delete
;
56 static struct ui_file
*tui_file_new (void);
57 static int tui_file_magic
;
59 static struct ui_file
*
62 struct tui_stream
*tui
= xmalloc (sizeof (struct tui_stream
));
63 struct ui_file
*file
= ui_file_new ();
64 set_ui_file_data (file
, tui
, tui_file_delete
);
65 set_ui_file_flush (file
, tui_file_flush
);
66 set_ui_file_fputs (file
, tui_file_fputs
);
67 set_ui_file_isatty (file
, tui_file_isatty
);
68 set_ui_file_rewind (file
, tui_file_rewind
);
69 set_ui_file_put (file
, tui_file_put
);
70 tui
->ts_magic
= &tui_file_magic
;
75 tui_file_delete (struct ui_file
*file
)
77 struct tui_stream
*tmpstream
= ui_file_data (file
);
78 if (tmpstream
->ts_magic
!= &tui_file_magic
)
79 internal_error (__FILE__
, __LINE__
,
80 _("tui_file_delete: bad magic number"));
81 if ((tmpstream
->ts_streamtype
== astring
) &&
82 (tmpstream
->ts_strbuf
!= NULL
))
84 xfree (tmpstream
->ts_strbuf
);
90 tui_fileopen (FILE *stream
)
92 struct ui_file
*file
= tui_file_new ();
93 struct tui_stream
*tmpstream
= ui_file_data (file
);
94 tmpstream
->ts_streamtype
= afile
;
95 tmpstream
->ts_filestream
= stream
;
96 tmpstream
->ts_strbuf
= NULL
;
97 tmpstream
->ts_buflen
= 0;
102 tui_sfileopen (int n
)
104 struct ui_file
*file
= tui_file_new ();
105 struct tui_stream
*tmpstream
= ui_file_data (file
);
106 tmpstream
->ts_streamtype
= astring
;
107 tmpstream
->ts_filestream
= NULL
;
110 tmpstream
->ts_strbuf
= xmalloc ((n
+ 1) * sizeof (char));
111 tmpstream
->ts_strbuf
[0] = '\0';
114 /* Do not allocate the buffer now. The first time something is printed
115 one will be allocated by tui_file_adjust_strbuf() */
116 tmpstream
->ts_strbuf
= NULL
;
117 tmpstream
->ts_buflen
= n
;
122 tui_file_isatty (struct ui_file
*file
)
124 struct tui_stream
*stream
= ui_file_data (file
);
125 if (stream
->ts_magic
!= &tui_file_magic
)
126 internal_error (__FILE__
, __LINE__
,
127 _("tui_file_isatty: bad magic number"));
128 if (stream
->ts_streamtype
== afile
)
129 return (isatty (fileno (stream
->ts_filestream
)));
135 tui_file_rewind (struct ui_file
*file
)
137 struct tui_stream
*stream
= ui_file_data (file
);
138 if (stream
->ts_magic
!= &tui_file_magic
)
139 internal_error (__FILE__
, __LINE__
,
140 _("tui_file_rewind: bad magic number"));
141 stream
->ts_strbuf
[0] = '\0';
145 tui_file_put (struct ui_file
*file
,
146 ui_file_put_method_ftype
*write
,
149 struct tui_stream
*stream
= ui_file_data (file
);
150 if (stream
->ts_magic
!= &tui_file_magic
)
151 internal_error (__FILE__
, __LINE__
,
152 _("tui_file_put: bad magic number"));
153 if (stream
->ts_streamtype
== astring
)
154 write (dest
, stream
->ts_strbuf
, strlen (stream
->ts_strbuf
));
157 /* All TUI I/O sent to the *_filtered and *_unfiltered functions
158 eventually ends up here. The fputs_unfiltered_hook is primarily
159 used by GUIs to collect all output and send it to the GUI, instead
160 of the controlling terminal. Only output to gdb_stdout and
161 gdb_stderr are sent to the hook. Everything else is sent on to
162 fputs to allow file I/O to be handled appropriately. */
164 /* FIXME: Should be broken up and moved to a TUI specific file. */
167 tui_file_fputs (const char *linebuffer
, struct ui_file
*file
)
169 struct tui_stream
*stream
= ui_file_data (file
);
171 if (stream
->ts_streamtype
== astring
)
173 tui_file_adjust_strbuf (strlen (linebuffer
), file
);
174 strcat (stream
->ts_strbuf
, linebuffer
);
178 tui_puts (linebuffer
);
183 tui_file_get_strbuf (struct ui_file
*file
)
185 struct tui_stream
*stream
= ui_file_data (file
);
186 if (stream
->ts_magic
!= &tui_file_magic
)
187 internal_error (__FILE__
, __LINE__
,
188 _("tui_file_get_strbuf: bad magic number"));
189 return (stream
->ts_strbuf
);
192 /* adjust the length of the buffer by the amount necessary
193 to accomodate appending a string of length N to the buffer contents */
195 tui_file_adjust_strbuf (int n
, struct ui_file
*file
)
197 struct tui_stream
*stream
= ui_file_data (file
);
199 if (stream
->ts_magic
!= &tui_file_magic
)
200 internal_error (__FILE__
, __LINE__
,
201 _("tui_file_adjust_strbuf: bad magic number"));
203 if (stream
->ts_streamtype
!= astring
)
206 if (stream
->ts_strbuf
)
208 /* There is already a buffer allocated */
209 non_null_chars
= strlen (stream
->ts_strbuf
);
211 if (n
> (stream
->ts_buflen
- non_null_chars
- 1))
213 stream
->ts_buflen
= n
+ non_null_chars
+ 1;
214 stream
->ts_strbuf
= xrealloc (stream
->ts_strbuf
, stream
->ts_buflen
);
218 /* No buffer yet, so allocate one of the desired size */
219 stream
->ts_strbuf
= xmalloc ((n
+ 1) * sizeof (char));
223 tui_file_flush (struct ui_file
*file
)
225 struct tui_stream
*stream
= ui_file_data (file
);
226 if (stream
->ts_magic
!= &tui_file_magic
)
227 internal_error (__FILE__
, __LINE__
,
228 _("tui_file_flush: bad magic number"));
230 switch (stream
->ts_streamtype
)
235 fflush (stream
->ts_filestream
);