* tui-file.h (fputs_unfiltered_hook): Remove.
[deliverable/binutils-gdb.git] / gdb / tui / tui-file.c
1 /* UI_FILE - a generic STDIO like output stream.
2 Copyright 1999, 2000, 2001 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"
22 #include "ui-file.h"
23 #include "tui/tui-file.h"
24
25 #include "tui.h"
26
27 #include <string.h>
28
29 /* A ``struct ui_file'' that is compatible with all the legacy
30 code. */
31
32 /* new */
33 enum streamtype
34 {
35 afile,
36 astring
37 };
38
39 /* new */
40 struct tui_stream
41 {
42 int *ts_magic;
43 enum streamtype ts_streamtype;
44 FILE *ts_filestream;
45 char *ts_strbuf;
46 int ts_buflen;
47 };
48
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;
57
58 static struct ui_file *
59 tui_file_new (void)
60 {
61 struct tui_stream *tui = xmalloc (sizeof (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;
70 return file;
71 }
72
73 static void
74 tui_file_delete (struct ui_file *file)
75 {
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))
82 {
83 xfree (tmpstream->ts_strbuf);
84 }
85 xfree (tmpstream);
86 }
87
88 struct ui_file *
89 tui_fileopen (FILE *stream)
90 {
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;
97 return file;
98 }
99
100 struct ui_file *
101 tui_sfileopen (int n)
102 {
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;
107 if (n > 0)
108 {
109 tmpstream->ts_strbuf = xmalloc ((n + 1) * sizeof (char));
110 tmpstream->ts_strbuf[0] = '\0';
111 }
112 else
113 /* Do not allocate the buffer now. The first time something is printed
114 one will be allocated by tui_file_adjust_strbuf() */
115 tmpstream->ts_strbuf = NULL;
116 tmpstream->ts_buflen = n;
117 return file;
118 }
119
120 static int
121 tui_file_isatty (struct ui_file *file)
122 {
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)));
129 else
130 return 0;
131 }
132
133 static void
134 tui_file_rewind (struct ui_file *file)
135 {
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';
141 }
142
143 static void
144 tui_file_put (struct ui_file *file,
145 ui_file_put_method_ftype *write,
146 void *dest)
147 {
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));
154 }
155
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. */
162
163 /* FIXME: Should be broken up and moved to a TUI specific file. */
164
165 void
166 tui_file_fputs (const char *linebuffer, struct ui_file *file)
167 {
168 struct tui_stream *stream = ui_file_data (file);
169
170 if (stream->ts_streamtype == astring)
171 {
172 tui_file_adjust_strbuf (strlen (linebuffer), file);
173 strcat (stream->ts_strbuf, linebuffer);
174 }
175 else
176 {
177 tuiPuts_unfiltered (linebuffer, file);
178 }
179 }
180
181 char *
182 tui_file_get_strbuf (struct ui_file *file)
183 {
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);
189 }
190
191 /* adjust the length of the buffer by the amount necessary
192 to accomodate appending a string of length N to the buffer 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 }
This page took 0.043524 seconds and 5 git commands to generate.