Updated copyright notices for most files.
[deliverable/binutils-gdb.git] / gdb / tui / tui-file.c
CommitLineData
d9fcf2fb 1/* UI_FILE - a generic STDIO like output stream.
0fb0cc75
JB
2 Copyright (C) 1999, 2000, 2001, 2007, 2008, 2009
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
JM
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);
da59e081
JM
69 tui->ts_magic = &tui_file_magic;
70 return file;
71}
72
73static void
fba45db2 74tui_file_delete (struct ui_file *file)
da59e081 75{
d9fcf2fb 76 struct tui_stream *tmpstream = ui_file_data (file);
da59e081 77 if (tmpstream->ts_magic != &tui_file_magic)
8e65ff28 78 internal_error (__FILE__, __LINE__,
e2e0b3e5 79 _("tui_file_delete: bad magic number"));
e5908723
MS
80 if ((tmpstream->ts_streamtype == astring)
81 && (tmpstream->ts_strbuf != NULL))
da59e081 82 {
b8c9b27d 83 xfree (tmpstream->ts_strbuf);
da59e081 84 }
b8c9b27d 85 xfree (tmpstream);
da59e081
JM
86}
87
d9fcf2fb 88struct ui_file *
fba45db2 89tui_fileopen (FILE *stream)
da59e081 90{
d9fcf2fb
JM
91 struct ui_file *file = tui_file_new ();
92 struct tui_stream *tmpstream = ui_file_data (file);
da59e081
JM
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
d9fcf2fb 100struct ui_file *
fba45db2 101tui_sfileopen (int n)
da59e081 102{
d9fcf2fb
JM
103 struct ui_file *file = tui_file_new ();
104 struct tui_stream *tmpstream = ui_file_data (file);
da59e081
JM
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
1cc6d956
MS
113 /* Do not allocate the buffer now. The first time something is
114 printed one will be allocated by tui_file_adjust_strbuf(). */
da59e081
JM
115 tmpstream->ts_strbuf = NULL;
116 tmpstream->ts_buflen = n;
117 return file;
118}
119
120static int
fba45db2 121tui_file_isatty (struct ui_file *file)
da59e081 122{
d9fcf2fb 123 struct tui_stream *stream = ui_file_data (file);
da59e081 124 if (stream->ts_magic != &tui_file_magic)
8e65ff28 125 internal_error (__FILE__, __LINE__,
e2e0b3e5 126 _("tui_file_isatty: bad magic number"));
da59e081
JM
127 if (stream->ts_streamtype == afile)
128 return (isatty (fileno (stream->ts_filestream)));
129 else
130 return 0;
131}
132
133static void
fba45db2 134tui_file_rewind (struct ui_file *file)
da59e081 135{
d9fcf2fb 136 struct tui_stream *stream = ui_file_data (file);
da59e081 137 if (stream->ts_magic != &tui_file_magic)
8e65ff28 138 internal_error (__FILE__, __LINE__,
e2e0b3e5 139 _("tui_file_rewind: bad magic number"));
da59e081
JM
140 stream->ts_strbuf[0] = '\0';
141}
142
143static void
d9fcf2fb
JM
144tui_file_put (struct ui_file *file,
145 ui_file_put_method_ftype *write,
da59e081
JM
146 void *dest)
147{
d9fcf2fb 148 struct tui_stream *stream = ui_file_data (file);
da59e081 149 if (stream->ts_magic != &tui_file_magic)
8e65ff28 150 internal_error (__FILE__, __LINE__,
e2e0b3e5 151 _("tui_file_put: bad magic number"));
da59e081
JM
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
1cc6d956 163/* FIXME: Should be broken up and moved to a TUI specific file. */
da59e081
JM
164
165void
fba45db2 166tui_file_fputs (const char *linebuffer, struct ui_file *file)
da59e081 167{
d9fcf2fb 168 struct tui_stream *stream = ui_file_data (file);
e42acc6b
SC
169
170 if (stream->ts_streamtype == astring)
171 {
172 tui_file_adjust_strbuf (strlen (linebuffer), file);
173 strcat (stream->ts_strbuf, linebuffer);
174 }
da59e081
JM
175 else
176 {
174a4a09 177 tui_puts (linebuffer);
da59e081
JM
178 }
179}
180
181char *
d9fcf2fb 182tui_file_get_strbuf (struct ui_file *file)
da59e081 183{
d9fcf2fb 184 struct tui_stream *stream = ui_file_data (file);
da59e081 185 if (stream->ts_magic != &tui_file_magic)
8e65ff28 186 internal_error (__FILE__, __LINE__,
e2e0b3e5 187 _("tui_file_get_strbuf: bad magic number"));
da59e081
JM
188 return (stream->ts_strbuf);
189}
190
1cc6d956
MS
191/* Adjust the length of the buffer by the amount necessary to
192 accomodate appending a string of length N to the buffer
193 contents. */
da59e081 194void
d9fcf2fb 195tui_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 200 internal_error (__FILE__, __LINE__,
e2e0b3e5 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 {
1cc6d956 208 /* There is already a buffer allocated. */
da59e081
JM
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
1cc6d956 218 /* No buffer yet, so allocate one of the desired size. */
da59e081
JM
219 stream->ts_strbuf = xmalloc ((n + 1) * sizeof (char));
220}
221
222static void
fba45db2 223tui_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 227 internal_error (__FILE__, __LINE__,
e2e0b3e5 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}
This page took 0.802299 seconds and 4 git commands to generate.