run copyright.sh for 2011.
[deliverable/binutils-gdb.git] / gdb / tui / tui-file.c
CommitLineData
d9fcf2fb 1/* UI_FILE - a generic STDIO like output stream.
7b6bb8da 2 Copyright (C) 1999, 2000, 2001, 2007, 2008, 2009, 2010, 2011
0fb0cc75 3 Free Software Foundation, Inc.
da59e081
JM
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
da59e081
JM
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
da59e081
JM
19
20#include "defs.h"
d9fcf2fb 21#include "ui-file.h"
da59e081 22#include "tui/tui-file.h"
d7b2e967 23#include "tui/tui-io.h"
da59e081 24
fbc75a32 25#include "tui.h"
fbc75a32 26
d02c80cd 27#include "gdb_string.h"
da59e081 28
d9fcf2fb 29/* A ``struct ui_file'' that is compatible with all the legacy
1cc6d956 30 code. */
da59e081
JM
31
32/* new */
33enum streamtype
34{
35 afile,
36 astring
37};
38
39/* new */
40struct 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
d9fcf2fb
JM
49static ui_file_flush_ftype tui_file_flush;
50extern ui_file_fputs_ftype tui_file_fputs;
51static ui_file_isatty_ftype tui_file_isatty;
52static ui_file_rewind_ftype tui_file_rewind;
53static ui_file_put_ftype tui_file_put;
54static ui_file_delete_ftype tui_file_delete;
a14ed312 55static struct ui_file *tui_file_new (void);
da59e081
JM
56static int tui_file_magic;
57
d9fcf2fb 58static struct ui_file *
fba45db2 59tui_file_new (void)
da59e081 60{
c0645fb5 61 struct tui_stream *tui = XMALLOC (struct tui_stream);
d9fcf2fb 62 struct ui_file *file = ui_file_new ();
1c5313c5 63
d9fcf2fb
JM
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
74static void
fba45db2 75tui_file_delete (struct ui_file *file)
da59e081 76{
d9fcf2fb 77 struct tui_stream *tmpstream = ui_file_data (file);
1c5313c5 78
da59e081 79 if (tmpstream->ts_magic != &tui_file_magic)
8e65ff28 80 internal_error (__FILE__, __LINE__,
e2e0b3e5 81 _("tui_file_delete: bad magic number"));
e5908723
MS
82 if ((tmpstream->ts_streamtype == astring)
83 && (tmpstream->ts_strbuf != NULL))
da59e081 84 {
b8c9b27d 85 xfree (tmpstream->ts_strbuf);
da59e081 86 }
b8c9b27d 87 xfree (tmpstream);
da59e081
JM
88}
89
d9fcf2fb 90struct ui_file *
fba45db2 91tui_fileopen (FILE *stream)
da59e081 92{
d9fcf2fb
JM
93 struct ui_file *file = tui_file_new ();
94 struct tui_stream *tmpstream = ui_file_data (file);
1c5313c5 95
da59e081
JM
96 tmpstream->ts_streamtype = afile;
97 tmpstream->ts_filestream = stream;
98 tmpstream->ts_strbuf = NULL;
99 tmpstream->ts_buflen = 0;
100 return file;
101}
102
d9fcf2fb 103struct ui_file *
fba45db2 104tui_sfileopen (int n)
da59e081 105{
d9fcf2fb
JM
106 struct ui_file *file = tui_file_new ();
107 struct tui_stream *tmpstream = ui_file_data (file);
1c5313c5 108
da59e081
JM
109 tmpstream->ts_streamtype = astring;
110 tmpstream->ts_filestream = NULL;
111 if (n > 0)
112 {
113 tmpstream->ts_strbuf = xmalloc ((n + 1) * sizeof (char));
114 tmpstream->ts_strbuf[0] = '\0';
115 }
116 else
1cc6d956
MS
117 /* Do not allocate the buffer now. The first time something is
118 printed one will be allocated by tui_file_adjust_strbuf(). */
da59e081
JM
119 tmpstream->ts_strbuf = NULL;
120 tmpstream->ts_buflen = n;
121 return file;
122}
123
124static int
fba45db2 125tui_file_isatty (struct ui_file *file)
da59e081 126{
d9fcf2fb 127 struct tui_stream *stream = ui_file_data (file);
1c5313c5 128
da59e081 129 if (stream->ts_magic != &tui_file_magic)
8e65ff28 130 internal_error (__FILE__, __LINE__,
e2e0b3e5 131 _("tui_file_isatty: bad magic number"));
da59e081
JM
132 if (stream->ts_streamtype == afile)
133 return (isatty (fileno (stream->ts_filestream)));
134 else
135 return 0;
136}
137
138static void
fba45db2 139tui_file_rewind (struct ui_file *file)
da59e081 140{
d9fcf2fb 141 struct tui_stream *stream = ui_file_data (file);
1c5313c5 142
da59e081 143 if (stream->ts_magic != &tui_file_magic)
8e65ff28 144 internal_error (__FILE__, __LINE__,
e2e0b3e5 145 _("tui_file_rewind: bad magic number"));
da59e081
JM
146 stream->ts_strbuf[0] = '\0';
147}
148
149static void
d9fcf2fb
JM
150tui_file_put (struct ui_file *file,
151 ui_file_put_method_ftype *write,
da59e081
JM
152 void *dest)
153{
d9fcf2fb 154 struct tui_stream *stream = ui_file_data (file);
1c5313c5 155
da59e081 156 if (stream->ts_magic != &tui_file_magic)
8e65ff28 157 internal_error (__FILE__, __LINE__,
e2e0b3e5 158 _("tui_file_put: bad magic number"));
da59e081
JM
159 if (stream->ts_streamtype == astring)
160 write (dest, stream->ts_strbuf, strlen (stream->ts_strbuf));
161}
162
163/* All TUI I/O sent to the *_filtered and *_unfiltered functions
164 eventually ends up here. The fputs_unfiltered_hook is primarily
165 used by GUIs to collect all output and send it to the GUI, instead
166 of the controlling terminal. Only output to gdb_stdout and
167 gdb_stderr are sent to the hook. Everything else is sent on to
168 fputs to allow file I/O to be handled appropriately. */
169
1cc6d956 170/* FIXME: Should be broken up and moved to a TUI specific file. */
da59e081
JM
171
172void
fba45db2 173tui_file_fputs (const char *linebuffer, struct ui_file *file)
da59e081 174{
d9fcf2fb 175 struct tui_stream *stream = ui_file_data (file);
e42acc6b
SC
176
177 if (stream->ts_streamtype == astring)
178 {
179 tui_file_adjust_strbuf (strlen (linebuffer), file);
180 strcat (stream->ts_strbuf, linebuffer);
181 }
da59e081
JM
182 else
183 {
174a4a09 184 tui_puts (linebuffer);
da59e081
JM
185 }
186}
187
188char *
d9fcf2fb 189tui_file_get_strbuf (struct ui_file *file)
da59e081 190{
d9fcf2fb 191 struct tui_stream *stream = ui_file_data (file);
1c5313c5 192
da59e081 193 if (stream->ts_magic != &tui_file_magic)
8e65ff28 194 internal_error (__FILE__, __LINE__,
e2e0b3e5 195 _("tui_file_get_strbuf: bad magic number"));
da59e081
JM
196 return (stream->ts_strbuf);
197}
198
1cc6d956
MS
199/* Adjust the length of the buffer by the amount necessary to
200 accomodate appending a string of length N to the buffer
201 contents. */
da59e081 202void
d9fcf2fb 203tui_file_adjust_strbuf (int n, struct ui_file *file)
da59e081 204{
d9fcf2fb 205 struct tui_stream *stream = ui_file_data (file);
da59e081 206 int non_null_chars;
1c5313c5 207
da59e081 208 if (stream->ts_magic != &tui_file_magic)
8e65ff28 209 internal_error (__FILE__, __LINE__,
e2e0b3e5 210 _("tui_file_adjust_strbuf: bad magic number"));
da59e081
JM
211
212 if (stream->ts_streamtype != astring)
213 return;
214
215 if (stream->ts_strbuf)
216 {
1cc6d956 217 /* There is already a buffer allocated. */
da59e081
JM
218 non_null_chars = strlen (stream->ts_strbuf);
219
220 if (n > (stream->ts_buflen - non_null_chars - 1))
221 {
222 stream->ts_buflen = n + non_null_chars + 1;
223 stream->ts_strbuf = xrealloc (stream->ts_strbuf, stream->ts_buflen);
224 }
225 }
226 else
1cc6d956 227 /* No buffer yet, so allocate one of the desired size. */
da59e081
JM
228 stream->ts_strbuf = xmalloc ((n + 1) * sizeof (char));
229}
230
231static void
fba45db2 232tui_file_flush (struct ui_file *file)
da59e081 233{
d9fcf2fb 234 struct tui_stream *stream = ui_file_data (file);
1c5313c5 235
da59e081 236 if (stream->ts_magic != &tui_file_magic)
8e65ff28 237 internal_error (__FILE__, __LINE__,
e2e0b3e5 238 _("tui_file_flush: bad magic number"));
da59e081 239
da59e081
JM
240 switch (stream->ts_streamtype)
241 {
242 case astring:
243 break;
244 case afile:
245 fflush (stream->ts_filestream);
246 break;
247 }
248}
This page took 1.094841 seconds and 4 git commands to generate.