1 /* UI_FILE - a generic STDIO like output stream.
2 Copyright (C) 1999, 2000, 2001, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "tui/tui-file.h"
23 #include "tui/tui-io.h"
27 #include "gdb_string.h"
29 /* A ``struct ui_file'' that is compatible with all the legacy
43 enum streamtype ts_streamtype
;
49 static ui_file_flush_ftype tui_file_flush
;
50 extern ui_file_fputs_ftype tui_file_fputs
;
51 static ui_file_isatty_ftype tui_file_isatty
;
52 static ui_file_rewind_ftype tui_file_rewind
;
53 static ui_file_put_ftype tui_file_put
;
54 static ui_file_delete_ftype tui_file_delete
;
55 static struct ui_file
*tui_file_new (void);
56 static int tui_file_magic
;
58 static struct ui_file
*
61 struct tui_stream
*tui
= XMALLOC (struct tui_stream
);
62 struct ui_file
*file
= ui_file_new ();
63 set_ui_file_data (file
, tui
, tui_file_delete
);
64 set_ui_file_flush (file
, tui_file_flush
);
65 set_ui_file_fputs (file
, tui_file_fputs
);
66 set_ui_file_isatty (file
, tui_file_isatty
);
67 set_ui_file_rewind (file
, tui_file_rewind
);
68 set_ui_file_put (file
, tui_file_put
);
69 tui
->ts_magic
= &tui_file_magic
;
74 tui_file_delete (struct ui_file
*file
)
76 struct tui_stream
*tmpstream
= ui_file_data (file
);
77 if (tmpstream
->ts_magic
!= &tui_file_magic
)
78 internal_error (__FILE__
, __LINE__
,
79 _("tui_file_delete: bad magic number"));
80 if ((tmpstream
->ts_streamtype
== astring
)
81 && (tmpstream
->ts_strbuf
!= NULL
))
83 xfree (tmpstream
->ts_strbuf
);
89 tui_fileopen (FILE *stream
)
91 struct ui_file
*file
= tui_file_new ();
92 struct tui_stream
*tmpstream
= ui_file_data (file
);
93 tmpstream
->ts_streamtype
= afile
;
94 tmpstream
->ts_filestream
= stream
;
95 tmpstream
->ts_strbuf
= NULL
;
96 tmpstream
->ts_buflen
= 0;
101 tui_sfileopen (int n
)
103 struct ui_file
*file
= tui_file_new ();
104 struct tui_stream
*tmpstream
= ui_file_data (file
);
105 tmpstream
->ts_streamtype
= astring
;
106 tmpstream
->ts_filestream
= NULL
;
109 tmpstream
->ts_strbuf
= xmalloc ((n
+ 1) * sizeof (char));
110 tmpstream
->ts_strbuf
[0] = '\0';
113 /* Do not allocate the buffer now. The first time something is
114 printed one will be allocated by tui_file_adjust_strbuf(). */
115 tmpstream
->ts_strbuf
= NULL
;
116 tmpstream
->ts_buflen
= n
;
121 tui_file_isatty (struct ui_file
*file
)
123 struct tui_stream
*stream
= ui_file_data (file
);
124 if (stream
->ts_magic
!= &tui_file_magic
)
125 internal_error (__FILE__
, __LINE__
,
126 _("tui_file_isatty: bad magic number"));
127 if (stream
->ts_streamtype
== afile
)
128 return (isatty (fileno (stream
->ts_filestream
)));
134 tui_file_rewind (struct ui_file
*file
)
136 struct tui_stream
*stream
= ui_file_data (file
);
137 if (stream
->ts_magic
!= &tui_file_magic
)
138 internal_error (__FILE__
, __LINE__
,
139 _("tui_file_rewind: bad magic number"));
140 stream
->ts_strbuf
[0] = '\0';
144 tui_file_put (struct ui_file
*file
,
145 ui_file_put_method_ftype
*write
,
148 struct tui_stream
*stream
= ui_file_data (file
);
149 if (stream
->ts_magic
!= &tui_file_magic
)
150 internal_error (__FILE__
, __LINE__
,
151 _("tui_file_put: bad magic number"));
152 if (stream
->ts_streamtype
== astring
)
153 write (dest
, stream
->ts_strbuf
, strlen (stream
->ts_strbuf
));
156 /* All TUI I/O sent to the *_filtered and *_unfiltered functions
157 eventually ends up here. The fputs_unfiltered_hook is primarily
158 used by GUIs to collect all output and send it to the GUI, instead
159 of the controlling terminal. Only output to gdb_stdout and
160 gdb_stderr are sent to the hook. Everything else is sent on to
161 fputs to allow file I/O to be handled appropriately. */
163 /* FIXME: Should be broken up and moved to a TUI specific file. */
166 tui_file_fputs (const char *linebuffer
, struct ui_file
*file
)
168 struct tui_stream
*stream
= ui_file_data (file
);
170 if (stream
->ts_streamtype
== astring
)
172 tui_file_adjust_strbuf (strlen (linebuffer
), file
);
173 strcat (stream
->ts_strbuf
, linebuffer
);
177 tui_puts (linebuffer
);
182 tui_file_get_strbuf (struct ui_file
*file
)
184 struct tui_stream
*stream
= ui_file_data (file
);
185 if (stream
->ts_magic
!= &tui_file_magic
)
186 internal_error (__FILE__
, __LINE__
,
187 _("tui_file_get_strbuf: bad magic number"));
188 return (stream
->ts_strbuf
);
191 /* Adjust the length of the buffer by the amount necessary to
192 accomodate appending a string of length N to the buffer
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
);