]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/tui/tui-file.c
This commit was manufactured by cvs2svn to create branch
[thirdparty/binutils-gdb.git] / gdb / tui / tui-file.c
1 /* UI_FILE - a generic STDIO like output stream.
2 Copyright (C) 1999, 2000, 2001, 2007, 2008 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 3 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, see <http://www.gnu.org/licenses/>. */
18
19 #include "defs.h"
20 #include "ui-file.h"
21 #include "tui/tui-file.h"
22 #include "tui/tui-io.h"
23
24 #include "tui.h"
25
26 #include "gdb_string.h"
27
28 /* A ``struct ui_file'' that is compatible with all the legacy
29 code. */
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
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;
54 static struct ui_file *tui_file_new (void);
55 static int tui_file_magic;
56
57 static struct ui_file *
58 tui_file_new (void)
59 {
60 struct tui_stream *tui = XMALLOC (struct tui_stream);
61 struct ui_file *file = ui_file_new ();
62 set_ui_file_data (file, tui, tui_file_delete);
63 set_ui_file_flush (file, tui_file_flush);
64 set_ui_file_fputs (file, tui_file_fputs);
65 set_ui_file_isatty (file, tui_file_isatty);
66 set_ui_file_rewind (file, tui_file_rewind);
67 set_ui_file_put (file, tui_file_put);
68 tui->ts_magic = &tui_file_magic;
69 return file;
70 }
71
72 static void
73 tui_file_delete (struct ui_file *file)
74 {
75 struct tui_stream *tmpstream = ui_file_data (file);
76 if (tmpstream->ts_magic != &tui_file_magic)
77 internal_error (__FILE__, __LINE__,
78 _("tui_file_delete: bad magic number"));
79 if ((tmpstream->ts_streamtype == astring)
80 && (tmpstream->ts_strbuf != NULL))
81 {
82 xfree (tmpstream->ts_strbuf);
83 }
84 xfree (tmpstream);
85 }
86
87 struct ui_file *
88 tui_fileopen (FILE *stream)
89 {
90 struct ui_file *file = tui_file_new ();
91 struct tui_stream *tmpstream = ui_file_data (file);
92 tmpstream->ts_streamtype = afile;
93 tmpstream->ts_filestream = stream;
94 tmpstream->ts_strbuf = NULL;
95 tmpstream->ts_buflen = 0;
96 return file;
97 }
98
99 struct ui_file *
100 tui_sfileopen (int n)
101 {
102 struct ui_file *file = tui_file_new ();
103 struct tui_stream *tmpstream = ui_file_data (file);
104 tmpstream->ts_streamtype = astring;
105 tmpstream->ts_filestream = NULL;
106 if (n > 0)
107 {
108 tmpstream->ts_strbuf = xmalloc ((n + 1) * sizeof (char));
109 tmpstream->ts_strbuf[0] = '\0';
110 }
111 else
112 /* Do not allocate the buffer now. The first time something is
113 printed one will be allocated by tui_file_adjust_strbuf(). */
114 tmpstream->ts_strbuf = NULL;
115 tmpstream->ts_buflen = n;
116 return file;
117 }
118
119 static int
120 tui_file_isatty (struct ui_file *file)
121 {
122 struct tui_stream *stream = ui_file_data (file);
123 if (stream->ts_magic != &tui_file_magic)
124 internal_error (__FILE__, __LINE__,
125 _("tui_file_isatty: bad magic number"));
126 if (stream->ts_streamtype == afile)
127 return (isatty (fileno (stream->ts_filestream)));
128 else
129 return 0;
130 }
131
132 static void
133 tui_file_rewind (struct ui_file *file)
134 {
135 struct tui_stream *stream = ui_file_data (file);
136 if (stream->ts_magic != &tui_file_magic)
137 internal_error (__FILE__, __LINE__,
138 _("tui_file_rewind: bad magic number"));
139 stream->ts_strbuf[0] = '\0';
140 }
141
142 static void
143 tui_file_put (struct ui_file *file,
144 ui_file_put_method_ftype *write,
145 void *dest)
146 {
147 struct tui_stream *stream = ui_file_data (file);
148 if (stream->ts_magic != &tui_file_magic)
149 internal_error (__FILE__, __LINE__,
150 _("tui_file_put: bad magic number"));
151 if (stream->ts_streamtype == astring)
152 write (dest, stream->ts_strbuf, strlen (stream->ts_strbuf));
153 }
154
155 /* All TUI I/O sent to the *_filtered and *_unfiltered functions
156 eventually ends up here. The fputs_unfiltered_hook is primarily
157 used by GUIs to collect all output and send it to the GUI, instead
158 of the controlling terminal. Only output to gdb_stdout and
159 gdb_stderr are sent to the hook. Everything else is sent on to
160 fputs to allow file I/O to be handled appropriately. */
161
162 /* FIXME: Should be broken up and moved to a TUI specific file. */
163
164 void
165 tui_file_fputs (const char *linebuffer, struct ui_file *file)
166 {
167 struct tui_stream *stream = ui_file_data (file);
168
169 if (stream->ts_streamtype == astring)
170 {
171 tui_file_adjust_strbuf (strlen (linebuffer), file);
172 strcat (stream->ts_strbuf, linebuffer);
173 }
174 else
175 {
176 tui_puts (linebuffer);
177 }
178 }
179
180 char *
181 tui_file_get_strbuf (struct ui_file *file)
182 {
183 struct tui_stream *stream = ui_file_data (file);
184 if (stream->ts_magic != &tui_file_magic)
185 internal_error (__FILE__, __LINE__,
186 _("tui_file_get_strbuf: bad magic number"));
187 return (stream->ts_strbuf);
188 }
189
190 /* Adjust the length of the buffer by the amount necessary to
191 accomodate appending a string of length N to the buffer
192 contents. */
193 void
194 tui_file_adjust_strbuf (int n, struct ui_file *file)
195 {
196 struct tui_stream *stream = ui_file_data (file);
197 int non_null_chars;
198 if (stream->ts_magic != &tui_file_magic)
199 internal_error (__FILE__, __LINE__,
200 _("tui_file_adjust_strbuf: bad magic number"));
201
202 if (stream->ts_streamtype != astring)
203 return;
204
205 if (stream->ts_strbuf)
206 {
207 /* There is already a buffer allocated. */
208 non_null_chars = strlen (stream->ts_strbuf);
209
210 if (n > (stream->ts_buflen - non_null_chars - 1))
211 {
212 stream->ts_buflen = n + non_null_chars + 1;
213 stream->ts_strbuf = xrealloc (stream->ts_strbuf, stream->ts_buflen);
214 }
215 }
216 else
217 /* No buffer yet, so allocate one of the desired size. */
218 stream->ts_strbuf = xmalloc ((n + 1) * sizeof (char));
219 }
220
221 static void
222 tui_file_flush (struct ui_file *file)
223 {
224 struct tui_stream *stream = ui_file_data (file);
225 if (stream->ts_magic != &tui_file_magic)
226 internal_error (__FILE__, __LINE__,
227 _("tui_file_flush: bad magic number"));
228
229 switch (stream->ts_streamtype)
230 {
231 case astring:
232 break;
233 case afile:
234 fflush (stream->ts_filestream);
235 break;
236 }
237 }