Commit | Line | Data |
---|---|---|
d9fcf2fb | 1 | /* UI_FILE - a generic STDIO like output stream. |
da59e081 JM |
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" | |
d9fcf2fb | 22 | #include "ui-file.h" |
da59e081 JM |
23 | #include "tui/tui-file.h" |
24 | ||
fbc75a32 AC |
25 | #ifdef TUI |
26 | #include "tui.h" | |
27 | #include "tuiData.h" | |
28 | #include "tuiIO.h" | |
29 | #include "tuiCommand.h" | |
30 | #endif | |
31 | ||
da59e081 JM |
32 | #include <string.h> |
33 | ||
34 | /* Called instead of fputs for all TUI_FILE output. */ | |
35 | ||
d9fcf2fb JM |
36 | void (*fputs_unfiltered_hook) (const char *linebuffer, |
37 | struct ui_file * stream); | |
da59e081 | 38 | |
d9fcf2fb | 39 | /* A ``struct ui_file'' that is compatible with all the legacy |
da59e081 JM |
40 | code. */ |
41 | ||
42 | /* new */ | |
43 | enum streamtype | |
44 | { | |
45 | afile, | |
46 | astring | |
47 | }; | |
48 | ||
49 | /* new */ | |
50 | struct tui_stream | |
51 | { | |
52 | int *ts_magic; | |
53 | enum streamtype ts_streamtype; | |
54 | FILE *ts_filestream; | |
55 | char *ts_strbuf; | |
56 | int ts_buflen; | |
57 | }; | |
58 | ||
d9fcf2fb JM |
59 | static ui_file_flush_ftype tui_file_flush; |
60 | extern ui_file_fputs_ftype tui_file_fputs; | |
61 | static ui_file_isatty_ftype tui_file_isatty; | |
62 | static ui_file_rewind_ftype tui_file_rewind; | |
63 | static ui_file_put_ftype tui_file_put; | |
64 | static ui_file_delete_ftype tui_file_delete; | |
a14ed312 | 65 | static struct ui_file *tui_file_new (void); |
da59e081 JM |
66 | static int tui_file_magic; |
67 | ||
d9fcf2fb | 68 | static struct ui_file * |
fba45db2 | 69 | tui_file_new (void) |
da59e081 JM |
70 | { |
71 | struct tui_stream *tui = xmalloc (sizeof (struct tui_stream)); | |
d9fcf2fb JM |
72 | struct ui_file *file = ui_file_new (); |
73 | set_ui_file_data (file, tui, tui_file_delete); | |
74 | set_ui_file_flush (file, tui_file_flush); | |
75 | set_ui_file_fputs (file, tui_file_fputs); | |
76 | set_ui_file_isatty (file, tui_file_isatty); | |
77 | set_ui_file_rewind (file, tui_file_rewind); | |
78 | set_ui_file_put (file, tui_file_put); | |
da59e081 JM |
79 | tui->ts_magic = &tui_file_magic; |
80 | return file; | |
81 | } | |
82 | ||
83 | static void | |
fba45db2 | 84 | tui_file_delete (struct ui_file *file) |
da59e081 | 85 | { |
d9fcf2fb | 86 | struct tui_stream *tmpstream = ui_file_data (file); |
da59e081 JM |
87 | if (tmpstream->ts_magic != &tui_file_magic) |
88 | internal_error ("tui_file_delete: bad magic number"); | |
89 | if ((tmpstream->ts_streamtype == astring) && | |
90 | (tmpstream->ts_strbuf != NULL)) | |
91 | { | |
92 | free (tmpstream->ts_strbuf); | |
93 | } | |
94 | free (tmpstream); | |
95 | } | |
96 | ||
d9fcf2fb | 97 | struct ui_file * |
fba45db2 | 98 | tui_fileopen (FILE *stream) |
da59e081 | 99 | { |
d9fcf2fb JM |
100 | struct ui_file *file = tui_file_new (); |
101 | struct tui_stream *tmpstream = ui_file_data (file); | |
da59e081 JM |
102 | tmpstream->ts_streamtype = afile; |
103 | tmpstream->ts_filestream = stream; | |
104 | tmpstream->ts_strbuf = NULL; | |
105 | tmpstream->ts_buflen = 0; | |
106 | return file; | |
107 | } | |
108 | ||
d9fcf2fb | 109 | struct ui_file * |
fba45db2 | 110 | tui_sfileopen (int n) |
da59e081 | 111 | { |
d9fcf2fb JM |
112 | struct ui_file *file = tui_file_new (); |
113 | struct tui_stream *tmpstream = ui_file_data (file); | |
da59e081 JM |
114 | tmpstream->ts_streamtype = astring; |
115 | tmpstream->ts_filestream = NULL; | |
116 | if (n > 0) | |
117 | { | |
118 | tmpstream->ts_strbuf = xmalloc ((n + 1) * sizeof (char)); | |
119 | tmpstream->ts_strbuf[0] = '\0'; | |
120 | } | |
121 | else | |
122 | /* Do not allocate the buffer now. The first time something is printed | |
123 | one will be allocated by tui_file_adjust_strbuf() */ | |
124 | tmpstream->ts_strbuf = NULL; | |
125 | tmpstream->ts_buflen = n; | |
126 | return file; | |
127 | } | |
128 | ||
129 | static int | |
fba45db2 | 130 | tui_file_isatty (struct ui_file *file) |
da59e081 | 131 | { |
d9fcf2fb | 132 | struct tui_stream *stream = ui_file_data (file); |
da59e081 JM |
133 | if (stream->ts_magic != &tui_file_magic) |
134 | internal_error ("tui_file_isatty: bad magic number"); | |
135 | if (stream->ts_streamtype == afile) | |
136 | return (isatty (fileno (stream->ts_filestream))); | |
137 | else | |
138 | return 0; | |
139 | } | |
140 | ||
141 | static void | |
fba45db2 | 142 | tui_file_rewind (struct ui_file *file) |
da59e081 | 143 | { |
d9fcf2fb | 144 | struct tui_stream *stream = ui_file_data (file); |
da59e081 JM |
145 | if (stream->ts_magic != &tui_file_magic) |
146 | internal_error ("tui_file_rewind: bad magic number"); | |
147 | stream->ts_strbuf[0] = '\0'; | |
148 | } | |
149 | ||
150 | static void | |
d9fcf2fb JM |
151 | tui_file_put (struct ui_file *file, |
152 | ui_file_put_method_ftype *write, | |
da59e081 JM |
153 | void *dest) |
154 | { | |
d9fcf2fb | 155 | struct tui_stream *stream = ui_file_data (file); |
da59e081 JM |
156 | if (stream->ts_magic != &tui_file_magic) |
157 | internal_error ("tui_file_put: bad magic number"); | |
158 | if (stream->ts_streamtype == astring) | |
159 | write (dest, stream->ts_strbuf, strlen (stream->ts_strbuf)); | |
160 | } | |
161 | ||
162 | /* All TUI I/O sent to the *_filtered and *_unfiltered functions | |
163 | eventually ends up here. The fputs_unfiltered_hook is primarily | |
164 | used by GUIs to collect all output and send it to the GUI, instead | |
165 | of the controlling terminal. Only output to gdb_stdout and | |
166 | gdb_stderr are sent to the hook. Everything else is sent on to | |
167 | fputs to allow file I/O to be handled appropriately. */ | |
168 | ||
169 | /* FIXME: Should be broken up and moved to a TUI specific file. */ | |
170 | ||
171 | void | |
fba45db2 | 172 | tui_file_fputs (const char *linebuffer, struct ui_file *file) |
da59e081 | 173 | { |
d9fcf2fb | 174 | struct tui_stream *stream = ui_file_data (file); |
da59e081 JM |
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 | |
d9fcf2fb | 180 | instead implement their own ui_file object and install that. See |
da59e081 JM |
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 | { | |
fbc75a32 | 199 | tui_file_adjust_strbuf (strlen (linebuffer), file); |
da59e081 JM |
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 | { | |
fbc75a32 | 218 | tui_file_adjust_strbuf (strlen (linebuffer), file); |
da59e081 JM |
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 * | |
d9fcf2fb | 239 | tui_file_get_strbuf (struct ui_file *file) |
da59e081 | 240 | { |
d9fcf2fb | 241 | struct tui_stream *stream = ui_file_data (file); |
da59e081 JM |
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 | |
d9fcf2fb | 250 | tui_file_adjust_strbuf (int n, struct ui_file *file) |
da59e081 | 251 | { |
d9fcf2fb | 252 | struct tui_stream *stream = ui_file_data (file); |
da59e081 JM |
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 | |
fba45db2 | 277 | tui_file_flush (struct ui_file *file) |
da59e081 | 278 | { |
d9fcf2fb | 279 | struct tui_stream *stream = ui_file_data (file); |
da59e081 JM |
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 | |
d9fcf2fb | 286 | implement a proper ui_file object. */ |
da59e081 JM |
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 | } |