Commit | Line | Data |
---|---|---|
d9fcf2fb | 1 | /* UI_FILE - a generic STDIO like output stream. |
8e65ff28 | 2 | Copyright 1999, 2000, 2001 Free Software Foundation, Inc. |
da59e081 JM |
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 | 23 | #include "tui/tui-file.h" |
d7b2e967 | 24 | #include "tui/tui-io.h" |
da59e081 | 25 | |
fbc75a32 | 26 | #include "tui.h" |
fbc75a32 | 27 | |
d02c80cd | 28 | #include "gdb_string.h" |
da59e081 | 29 | |
d9fcf2fb | 30 | /* A ``struct ui_file'' that is compatible with all the legacy |
da59e081 JM |
31 | code. */ |
32 | ||
33 | /* new */ | |
34 | enum streamtype | |
35 | { | |
36 | afile, | |
37 | astring | |
38 | }; | |
39 | ||
40 | /* new */ | |
41 | struct tui_stream | |
42 | { | |
43 | int *ts_magic; | |
44 | enum streamtype ts_streamtype; | |
45 | FILE *ts_filestream; | |
46 | char *ts_strbuf; | |
47 | int ts_buflen; | |
48 | }; | |
49 | ||
d9fcf2fb JM |
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; | |
a14ed312 | 56 | static struct ui_file *tui_file_new (void); |
da59e081 JM |
57 | static int tui_file_magic; |
58 | ||
d9fcf2fb | 59 | static struct ui_file * |
fba45db2 | 60 | tui_file_new (void) |
da59e081 JM |
61 | { |
62 | struct tui_stream *tui = xmalloc (sizeof (struct tui_stream)); | |
d9fcf2fb JM |
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); | |
da59e081 JM |
70 | tui->ts_magic = &tui_file_magic; |
71 | return file; | |
72 | } | |
73 | ||
74 | static void | |
fba45db2 | 75 | tui_file_delete (struct ui_file *file) |
da59e081 | 76 | { |
d9fcf2fb | 77 | struct tui_stream *tmpstream = ui_file_data (file); |
da59e081 | 78 | if (tmpstream->ts_magic != &tui_file_magic) |
8e65ff28 AC |
79 | internal_error (__FILE__, __LINE__, |
80 | "tui_file_delete: bad magic number"); | |
da59e081 JM |
81 | if ((tmpstream->ts_streamtype == astring) && |
82 | (tmpstream->ts_strbuf != NULL)) | |
83 | { | |
b8c9b27d | 84 | xfree (tmpstream->ts_strbuf); |
da59e081 | 85 | } |
b8c9b27d | 86 | xfree (tmpstream); |
da59e081 JM |
87 | } |
88 | ||
d9fcf2fb | 89 | struct ui_file * |
fba45db2 | 90 | tui_fileopen (FILE *stream) |
da59e081 | 91 | { |
d9fcf2fb JM |
92 | struct ui_file *file = tui_file_new (); |
93 | struct tui_stream *tmpstream = ui_file_data (file); | |
da59e081 JM |
94 | tmpstream->ts_streamtype = afile; |
95 | tmpstream->ts_filestream = stream; | |
96 | tmpstream->ts_strbuf = NULL; | |
97 | tmpstream->ts_buflen = 0; | |
98 | return file; | |
99 | } | |
100 | ||
d9fcf2fb | 101 | struct ui_file * |
fba45db2 | 102 | tui_sfileopen (int n) |
da59e081 | 103 | { |
d9fcf2fb JM |
104 | struct ui_file *file = tui_file_new (); |
105 | struct tui_stream *tmpstream = ui_file_data (file); | |
da59e081 JM |
106 | tmpstream->ts_streamtype = astring; |
107 | tmpstream->ts_filestream = NULL; | |
108 | if (n > 0) | |
109 | { | |
110 | tmpstream->ts_strbuf = xmalloc ((n + 1) * sizeof (char)); | |
111 | tmpstream->ts_strbuf[0] = '\0'; | |
112 | } | |
113 | else | |
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; | |
118 | return file; | |
119 | } | |
120 | ||
121 | static int | |
fba45db2 | 122 | tui_file_isatty (struct ui_file *file) |
da59e081 | 123 | { |
d9fcf2fb | 124 | struct tui_stream *stream = ui_file_data (file); |
da59e081 | 125 | if (stream->ts_magic != &tui_file_magic) |
8e65ff28 AC |
126 | internal_error (__FILE__, __LINE__, |
127 | "tui_file_isatty: bad magic number"); | |
da59e081 JM |
128 | if (stream->ts_streamtype == afile) |
129 | return (isatty (fileno (stream->ts_filestream))); | |
130 | else | |
131 | return 0; | |
132 | } | |
133 | ||
134 | static void | |
fba45db2 | 135 | tui_file_rewind (struct ui_file *file) |
da59e081 | 136 | { |
d9fcf2fb | 137 | struct tui_stream *stream = ui_file_data (file); |
da59e081 | 138 | if (stream->ts_magic != &tui_file_magic) |
8e65ff28 AC |
139 | internal_error (__FILE__, __LINE__, |
140 | "tui_file_rewind: bad magic number"); | |
da59e081 JM |
141 | stream->ts_strbuf[0] = '\0'; |
142 | } | |
143 | ||
144 | static void | |
d9fcf2fb JM |
145 | tui_file_put (struct ui_file *file, |
146 | ui_file_put_method_ftype *write, | |
da59e081 JM |
147 | void *dest) |
148 | { | |
d9fcf2fb | 149 | struct tui_stream *stream = ui_file_data (file); |
da59e081 | 150 | if (stream->ts_magic != &tui_file_magic) |
8e65ff28 AC |
151 | internal_error (__FILE__, __LINE__, |
152 | "tui_file_put: bad magic number"); | |
da59e081 JM |
153 | if (stream->ts_streamtype == astring) |
154 | write (dest, stream->ts_strbuf, strlen (stream->ts_strbuf)); | |
155 | } | |
156 | ||
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. */ | |
163 | ||
164 | /* FIXME: Should be broken up and moved to a TUI specific file. */ | |
165 | ||
166 | void | |
fba45db2 | 167 | tui_file_fputs (const char *linebuffer, struct ui_file *file) |
da59e081 | 168 | { |
d9fcf2fb | 169 | struct tui_stream *stream = ui_file_data (file); |
e42acc6b SC |
170 | |
171 | if (stream->ts_streamtype == astring) | |
172 | { | |
173 | tui_file_adjust_strbuf (strlen (linebuffer), file); | |
174 | strcat (stream->ts_strbuf, linebuffer); | |
175 | } | |
da59e081 JM |
176 | else |
177 | { | |
174a4a09 | 178 | tui_puts (linebuffer); |
da59e081 JM |
179 | } |
180 | } | |
181 | ||
182 | char * | |
d9fcf2fb | 183 | tui_file_get_strbuf (struct ui_file *file) |
da59e081 | 184 | { |
d9fcf2fb | 185 | struct tui_stream *stream = ui_file_data (file); |
da59e081 | 186 | if (stream->ts_magic != &tui_file_magic) |
8e65ff28 AC |
187 | internal_error (__FILE__, __LINE__, |
188 | "tui_file_get_strbuf: bad magic number"); | |
da59e081 JM |
189 | return (stream->ts_strbuf); |
190 | } | |
191 | ||
192 | /* adjust the length of the buffer by the amount necessary | |
193 | to accomodate appending a string of length N to the buffer contents */ | |
194 | void | |
d9fcf2fb | 195 | tui_file_adjust_strbuf (int n, struct ui_file *file) |
da59e081 | 196 | { |
d9fcf2fb | 197 | struct tui_stream *stream = ui_file_data (file); |
da59e081 JM |
198 | int non_null_chars; |
199 | if (stream->ts_magic != &tui_file_magic) | |
8e65ff28 AC |
200 | internal_error (__FILE__, __LINE__, |
201 | "tui_file_adjust_strbuf: bad magic number"); | |
da59e081 JM |
202 | |
203 | if (stream->ts_streamtype != astring) | |
204 | return; | |
205 | ||
206 | if (stream->ts_strbuf) | |
207 | { | |
208 | /* There is already a buffer allocated */ | |
209 | non_null_chars = strlen (stream->ts_strbuf); | |
210 | ||
211 | if (n > (stream->ts_buflen - non_null_chars - 1)) | |
212 | { | |
213 | stream->ts_buflen = n + non_null_chars + 1; | |
214 | stream->ts_strbuf = xrealloc (stream->ts_strbuf, stream->ts_buflen); | |
215 | } | |
216 | } | |
217 | else | |
218 | /* No buffer yet, so allocate one of the desired size */ | |
219 | stream->ts_strbuf = xmalloc ((n + 1) * sizeof (char)); | |
220 | } | |
221 | ||
222 | static void | |
fba45db2 | 223 | tui_file_flush (struct ui_file *file) |
da59e081 | 224 | { |
d9fcf2fb | 225 | struct tui_stream *stream = ui_file_data (file); |
da59e081 | 226 | if (stream->ts_magic != &tui_file_magic) |
8e65ff28 AC |
227 | internal_error (__FILE__, __LINE__, |
228 | "tui_file_flush: bad magic number"); | |
da59e081 | 229 | |
da59e081 JM |
230 | switch (stream->ts_streamtype) |
231 | { | |
232 | case astring: | |
233 | break; | |
234 | case afile: | |
235 | fflush (stream->ts_filestream); | |
236 | break; | |
237 | } | |
238 | } |