Commit | Line | Data |
---|---|---|
d9fcf2fb | 1 | /* UI_FILE - a generic STDIO like output stream. |
c5a57081 | 2 | Copyright (C) 1999-2001, 2007-2012 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 | |
a9762ec7 | 8 | the Free Software Foundation; either version 3 of the License, or |
da59e081 JM |
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 | |
a9762ec7 | 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
da59e081 JM |
18 | |
19 | #include "defs.h" | |
d9fcf2fb | 20 | #include "ui-file.h" |
da59e081 | 21 | #include "tui/tui-file.h" |
d7b2e967 | 22 | #include "tui/tui-io.h" |
da59e081 | 23 | |
fbc75a32 | 24 | #include "tui.h" |
fbc75a32 | 25 | |
d02c80cd | 26 | #include "gdb_string.h" |
da59e081 | 27 | |
d9fcf2fb | 28 | /* A ``struct ui_file'' that is compatible with all the legacy |
1cc6d956 | 29 | code. */ |
da59e081 JM |
30 | |
31 | /* new */ | |
32 | enum streamtype | |
33 | { | |
34 | afile, | |
35 | astring | |
36 | }; | |
37 | ||
38 | /* new */ | |
39 | struct tui_stream | |
40 | { | |
41 | int *ts_magic; | |
42 | enum streamtype ts_streamtype; | |
43 | FILE *ts_filestream; | |
44 | char *ts_strbuf; | |
45 | int ts_buflen; | |
46 | }; | |
47 | ||
d9fcf2fb JM |
48 | static ui_file_flush_ftype tui_file_flush; |
49 | extern ui_file_fputs_ftype tui_file_fputs; | |
50 | static ui_file_isatty_ftype tui_file_isatty; | |
51 | static ui_file_rewind_ftype tui_file_rewind; | |
52 | static ui_file_put_ftype tui_file_put; | |
53 | static ui_file_delete_ftype tui_file_delete; | |
a14ed312 | 54 | static struct ui_file *tui_file_new (void); |
da59e081 JM |
55 | static int tui_file_magic; |
56 | ||
d9fcf2fb | 57 | static struct ui_file * |
fba45db2 | 58 | tui_file_new (void) |
da59e081 | 59 | { |
c0645fb5 | 60 | struct tui_stream *tui = XMALLOC (struct tui_stream); |
d9fcf2fb | 61 | struct ui_file *file = ui_file_new (); |
1c5313c5 | 62 | |
d9fcf2fb JM |
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); | |
da59e081 JM |
69 | tui->ts_magic = &tui_file_magic; |
70 | return file; | |
71 | } | |
72 | ||
73 | static void | |
fba45db2 | 74 | tui_file_delete (struct ui_file *file) |
da59e081 | 75 | { |
d9fcf2fb | 76 | struct tui_stream *tmpstream = ui_file_data (file); |
1c5313c5 | 77 | |
da59e081 | 78 | if (tmpstream->ts_magic != &tui_file_magic) |
8e65ff28 | 79 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 80 | _("tui_file_delete: bad magic number")); |
e5908723 MS |
81 | if ((tmpstream->ts_streamtype == astring) |
82 | && (tmpstream->ts_strbuf != NULL)) | |
da59e081 | 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); | |
1c5313c5 | 94 | |
da59e081 JM |
95 | tmpstream->ts_streamtype = afile; |
96 | tmpstream->ts_filestream = stream; | |
97 | tmpstream->ts_strbuf = NULL; | |
98 | tmpstream->ts_buflen = 0; | |
99 | return file; | |
100 | } | |
101 | ||
d9fcf2fb | 102 | struct ui_file * |
fba45db2 | 103 | tui_sfileopen (int n) |
da59e081 | 104 | { |
d9fcf2fb JM |
105 | struct ui_file *file = tui_file_new (); |
106 | struct tui_stream *tmpstream = ui_file_data (file); | |
1c5313c5 | 107 | |
da59e081 JM |
108 | tmpstream->ts_streamtype = astring; |
109 | tmpstream->ts_filestream = NULL; | |
110 | if (n > 0) | |
111 | { | |
112 | tmpstream->ts_strbuf = xmalloc ((n + 1) * sizeof (char)); | |
113 | tmpstream->ts_strbuf[0] = '\0'; | |
114 | } | |
115 | else | |
1cc6d956 MS |
116 | /* Do not allocate the buffer now. The first time something is |
117 | printed one will be allocated by tui_file_adjust_strbuf(). */ | |
da59e081 JM |
118 | tmpstream->ts_strbuf = NULL; |
119 | tmpstream->ts_buflen = n; | |
120 | return file; | |
121 | } | |
122 | ||
123 | static int | |
fba45db2 | 124 | tui_file_isatty (struct ui_file *file) |
da59e081 | 125 | { |
d9fcf2fb | 126 | struct tui_stream *stream = ui_file_data (file); |
1c5313c5 | 127 | |
da59e081 | 128 | if (stream->ts_magic != &tui_file_magic) |
8e65ff28 | 129 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 130 | _("tui_file_isatty: bad magic number")); |
da59e081 JM |
131 | if (stream->ts_streamtype == afile) |
132 | return (isatty (fileno (stream->ts_filestream))); | |
133 | else | |
134 | return 0; | |
135 | } | |
136 | ||
137 | static void | |
fba45db2 | 138 | tui_file_rewind (struct ui_file *file) |
da59e081 | 139 | { |
d9fcf2fb | 140 | struct tui_stream *stream = ui_file_data (file); |
1c5313c5 | 141 | |
da59e081 | 142 | if (stream->ts_magic != &tui_file_magic) |
8e65ff28 | 143 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 144 | _("tui_file_rewind: bad magic number")); |
da59e081 JM |
145 | stream->ts_strbuf[0] = '\0'; |
146 | } | |
147 | ||
148 | static void | |
d9fcf2fb JM |
149 | tui_file_put (struct ui_file *file, |
150 | ui_file_put_method_ftype *write, | |
da59e081 JM |
151 | void *dest) |
152 | { | |
d9fcf2fb | 153 | struct tui_stream *stream = ui_file_data (file); |
1c5313c5 | 154 | |
da59e081 | 155 | if (stream->ts_magic != &tui_file_magic) |
8e65ff28 | 156 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 157 | _("tui_file_put: bad magic number")); |
da59e081 JM |
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 | ||
1cc6d956 | 169 | /* FIXME: Should be broken up and moved to a TUI specific file. */ |
da59e081 JM |
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); |
e42acc6b SC |
175 | |
176 | if (stream->ts_streamtype == astring) | |
177 | { | |
178 | tui_file_adjust_strbuf (strlen (linebuffer), file); | |
179 | strcat (stream->ts_strbuf, linebuffer); | |
180 | } | |
da59e081 JM |
181 | else |
182 | { | |
174a4a09 | 183 | tui_puts (linebuffer); |
da59e081 JM |
184 | } |
185 | } | |
186 | ||
187 | char * | |
d9fcf2fb | 188 | tui_file_get_strbuf (struct ui_file *file) |
da59e081 | 189 | { |
d9fcf2fb | 190 | struct tui_stream *stream = ui_file_data (file); |
1c5313c5 | 191 | |
da59e081 | 192 | if (stream->ts_magic != &tui_file_magic) |
8e65ff28 | 193 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 194 | _("tui_file_get_strbuf: bad magic number")); |
da59e081 JM |
195 | return (stream->ts_strbuf); |
196 | } | |
197 | ||
1cc6d956 MS |
198 | /* Adjust the length of the buffer by the amount necessary to |
199 | accomodate appending a string of length N to the buffer | |
200 | contents. */ | |
da59e081 | 201 | void |
d9fcf2fb | 202 | tui_file_adjust_strbuf (int n, struct ui_file *file) |
da59e081 | 203 | { |
d9fcf2fb | 204 | struct tui_stream *stream = ui_file_data (file); |
da59e081 | 205 | int non_null_chars; |
1c5313c5 | 206 | |
da59e081 | 207 | if (stream->ts_magic != &tui_file_magic) |
8e65ff28 | 208 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 209 | _("tui_file_adjust_strbuf: bad magic number")); |
da59e081 JM |
210 | |
211 | if (stream->ts_streamtype != astring) | |
212 | return; | |
213 | ||
214 | if (stream->ts_strbuf) | |
215 | { | |
1cc6d956 | 216 | /* There is already a buffer allocated. */ |
da59e081 JM |
217 | non_null_chars = strlen (stream->ts_strbuf); |
218 | ||
219 | if (n > (stream->ts_buflen - non_null_chars - 1)) | |
220 | { | |
221 | stream->ts_buflen = n + non_null_chars + 1; | |
222 | stream->ts_strbuf = xrealloc (stream->ts_strbuf, stream->ts_buflen); | |
223 | } | |
224 | } | |
225 | else | |
1cc6d956 | 226 | /* No buffer yet, so allocate one of the desired size. */ |
da59e081 JM |
227 | stream->ts_strbuf = xmalloc ((n + 1) * sizeof (char)); |
228 | } | |
229 | ||
230 | static void | |
fba45db2 | 231 | tui_file_flush (struct ui_file *file) |
da59e081 | 232 | { |
d9fcf2fb | 233 | struct tui_stream *stream = ui_file_data (file); |
1c5313c5 | 234 | |
da59e081 | 235 | if (stream->ts_magic != &tui_file_magic) |
8e65ff28 | 236 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 237 | _("tui_file_flush: bad magic number")); |
da59e081 | 238 | |
da59e081 JM |
239 | switch (stream->ts_streamtype) |
240 | { | |
241 | case astring: | |
242 | break; | |
243 | case afile: | |
244 | fflush (stream->ts_filestream); | |
245 | break; | |
246 | } | |
247 | } |