* dwarf2read.c (read_tag_const_type, read_tag_volatile_type):
[deliverable/binutils-gdb.git] / gdb / ui-file.c
CommitLineData
d9fcf2fb
JM
1/* UI_FILE - a generic STDIO like output stream.
2 Copyright (C) 1999, 2000 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/* Implement the ``struct ui_file'' object. */
22
23#include "defs.h"
24#include "ui-file.h"
1d1358b6 25#include "gdb_string.h"
d9fcf2fb
JM
26
27#undef XMALLOC
28#define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
29
30static ui_file_isatty_ftype null_file_isatty;
31static ui_file_write_ftype null_file_write;
32static ui_file_fputs_ftype null_file_fputs;
33static ui_file_flush_ftype null_file_flush;
34static ui_file_delete_ftype null_file_delete;
35static ui_file_rewind_ftype null_file_rewind;
36static ui_file_put_ftype null_file_put;
37
38struct ui_file
39 {
40 int *magic;
41 ui_file_flush_ftype *to_flush;
42 ui_file_write_ftype *to_write;
43 ui_file_fputs_ftype *to_fputs;
44 ui_file_delete_ftype *to_delete;
45 ui_file_isatty_ftype *to_isatty;
46 ui_file_rewind_ftype *to_rewind;
47 ui_file_put_ftype *to_put;
48 void *to_data;
49 };
50int ui_file_magic;
51
52struct ui_file *
fba45db2 53ui_file_new (void)
d9fcf2fb
JM
54{
55 struct ui_file *file = xmalloc (sizeof (struct ui_file));
56 file->magic = &ui_file_magic;
57 set_ui_file_data (file, NULL, null_file_delete);
58 set_ui_file_flush (file, null_file_flush);
59 set_ui_file_write (file, null_file_write);
60 set_ui_file_fputs (file, null_file_fputs);
61 set_ui_file_isatty (file, null_file_isatty);
62 set_ui_file_rewind (file, null_file_rewind);
63 set_ui_file_put (file, null_file_put);
64 return file;
65}
66
67void
fba45db2 68ui_file_delete (struct ui_file *file)
d9fcf2fb
JM
69{
70 file->to_delete (file);
b8c9b27d 71 xfree (file);
d9fcf2fb
JM
72}
73
74static int
fba45db2 75null_file_isatty (struct ui_file *file)
d9fcf2fb
JM
76{
77 return 0;
78}
79
80static void
fba45db2 81null_file_rewind (struct ui_file *file)
d9fcf2fb
JM
82{
83 return;
84}
85
86static void
87null_file_put (struct ui_file *file,
88 ui_file_put_method_ftype *write,
89 void *dest)
90{
91 return;
92}
93
94static void
fba45db2 95null_file_flush (struct ui_file *file)
d9fcf2fb
JM
96{
97 return;
98}
99
100static void
101null_file_write (struct ui_file *file,
102 const char *buf,
103 long sizeof_buf)
104{
105 if (file->to_fputs == null_file_fputs)
106 /* Both the write and fputs methods are null. Discard the
107 request. */
108 return;
109 else
110 {
111 /* The fputs method isn't null, slowly pass the write request
112 onto that. FYI, this isn't as bad as it may look - the
113 current (as of 1999-11-07) printf_* function calls fputc and
114 fputc does exactly the below. By having a write function it
115 is possible to clean up that code. */
116 int i;
117 char b[2];
118 b[1] = '\0';
119 for (i = 0; i < sizeof_buf; i++)
120 {
121 b[0] = buf[i];
122 file->to_fputs (b, file);
123 }
124 return;
125 }
126}
127
128static void
fba45db2 129null_file_fputs (const char *buf, struct ui_file *file)
d9fcf2fb
JM
130{
131 if (file->to_write == null_file_write)
132 /* Both the write and fputs methods are null. Discard the
133 request. */
134 return;
135 else
136 {
137 /* The write method was implemented, use that. */
138 file->to_write (file, buf, strlen (buf));
139 }
140}
141
142static void
fba45db2 143null_file_delete (struct ui_file *file)
d9fcf2fb
JM
144{
145 return;
146}
147
148void *
fba45db2 149ui_file_data (struct ui_file *file)
d9fcf2fb
JM
150{
151 if (file->magic != &ui_file_magic)
152 internal_error ("ui_file_data: bad magic number");
153 return file->to_data;
154}
155
156void
fba45db2 157gdb_flush (struct ui_file *file)
d9fcf2fb
JM
158{
159 file->to_flush (file);
160}
161
162int
fba45db2 163ui_file_isatty (struct ui_file *file)
d9fcf2fb
JM
164{
165 return file->to_isatty (file);
166}
167
168void
fba45db2 169ui_file_rewind (struct ui_file *file)
d9fcf2fb
JM
170{
171 file->to_rewind (file);
172}
173
174void
175ui_file_put (struct ui_file *file,
176 ui_file_put_method_ftype *write,
177 void *dest)
178{
179 file->to_put (file, write, dest);
180}
181
182void
183ui_file_write (struct ui_file *file,
184 const char *buf,
185 long length_buf)
186{
187 file->to_write (file, buf, length_buf);
188}
189
190void
fba45db2 191fputs_unfiltered (const char *buf, struct ui_file *file)
d9fcf2fb
JM
192{
193 file->to_fputs (buf, file);
194}
195
196void
fba45db2 197set_ui_file_flush (struct ui_file *file, ui_file_flush_ftype *flush)
d9fcf2fb
JM
198{
199 file->to_flush = flush;
200}
201
202void
fba45db2 203set_ui_file_isatty (struct ui_file *file, ui_file_isatty_ftype *isatty)
d9fcf2fb
JM
204{
205 file->to_isatty = isatty;
206}
207
208void
fba45db2 209set_ui_file_rewind (struct ui_file *file, ui_file_rewind_ftype *rewind)
d9fcf2fb
JM
210{
211 file->to_rewind = rewind;
212}
213
214void
fba45db2 215set_ui_file_put (struct ui_file *file, ui_file_put_ftype *put)
d9fcf2fb
JM
216{
217 file->to_put = put;
218}
219
220void
221set_ui_file_write (struct ui_file *file,
222 ui_file_write_ftype *write)
223{
224 file->to_write = write;
225}
226
227void
fba45db2 228set_ui_file_fputs (struct ui_file *file, ui_file_fputs_ftype *fputs)
d9fcf2fb
JM
229{
230 file->to_fputs = fputs;
231}
232
233void
fba45db2
KB
234set_ui_file_data (struct ui_file *file, void *data,
235 ui_file_delete_ftype *delete)
d9fcf2fb
JM
236{
237 file->to_data = data;
238 file->to_delete = delete;
239}
240
241/* ui_file utility function for converting a ``struct ui_file'' into
242 a memory buffer''. */
243
244struct accumulated_ui_file
245{
246 char *buffer;
247 long length;
248};
249
250static void
251do_ui_file_xstrdup (void *context, const char *buffer, long length)
252{
253 struct accumulated_ui_file *acc = context;
254 if (acc->buffer == NULL)
255 acc->buffer = xmalloc (length + 1);
256 else
257 acc->buffer = xrealloc (acc->buffer, acc->length + length + 1);
258 memcpy (acc->buffer + acc->length, buffer, length);
259 acc->length += length;
260 acc->buffer[acc->length] = '\0';
261}
262
263char *
264ui_file_xstrdup (struct ui_file *file,
265 long *length)
266{
267 struct accumulated_ui_file acc;
268 acc.buffer = NULL;
269 acc.length = 0;
270 ui_file_put (file, do_ui_file_xstrdup, &acc);
271 if (acc.buffer == NULL)
272 acc.buffer = xstrdup ("");
273 *length = acc.length;
274 return acc.buffer;
275}
276\f
277/* A pure memory based ``struct ui_file'' that can be used an output
278 buffer. The buffers accumulated contents are available via
279 ui_file_put(). */
280
281struct mem_file
282 {
283 int *magic;
284 char *buffer;
285 int sizeof_buffer;
286 int length_buffer;
287 };
288
289static ui_file_rewind_ftype mem_file_rewind;
290static ui_file_put_ftype mem_file_put;
291static ui_file_write_ftype mem_file_write;
292static ui_file_delete_ftype mem_file_delete;
a14ed312 293static struct ui_file *mem_file_new (void);
d9fcf2fb
JM
294static int mem_file_magic;
295
296static struct ui_file *
297mem_file_new (void)
298{
299 struct mem_file *stream = XMALLOC (struct mem_file);
300 struct ui_file *file = ui_file_new ();
301 set_ui_file_data (file, stream, mem_file_delete);
302 set_ui_file_rewind (file, mem_file_rewind);
303 set_ui_file_put (file, mem_file_put);
304 set_ui_file_write (file, mem_file_write);
305 stream->magic = &mem_file_magic;
306 stream->buffer = NULL;
307 stream->sizeof_buffer = 0;
308 stream->length_buffer = 0;
309 return file;
310}
311
312static void
313mem_file_delete (struct ui_file *file)
314{
315 struct mem_file *stream = ui_file_data (file);
316 if (stream->magic != &mem_file_magic)
317 internal_error ("mem_file_delete: bad magic number");
318 if (stream->buffer != NULL)
b8c9b27d
KB
319 xfree (stream->buffer);
320 xfree (stream);
d9fcf2fb
JM
321}
322
323struct ui_file *
324mem_fileopen (void)
325{
326 return mem_file_new ();
327}
328
329static void
330mem_file_rewind (struct ui_file *file)
331{
332 struct mem_file *stream = ui_file_data (file);
333 if (stream->magic != &mem_file_magic)
334 internal_error ("mem_file_rewind: bad magic number");
335 stream->length_buffer = 0;
336}
337
338static void
339mem_file_put (struct ui_file *file,
340 ui_file_put_method_ftype *write,
341 void *dest)
342{
343 struct mem_file *stream = ui_file_data (file);
344 if (stream->magic != &mem_file_magic)
345 internal_error ("mem_file_put: bad magic number");
346 if (stream->length_buffer > 0)
347 write (dest, stream->buffer, stream->length_buffer);
348}
349
350void
351mem_file_write (struct ui_file *file,
352 const char *buffer,
353 long length_buffer)
354{
355 struct mem_file *stream = ui_file_data (file);
356 if (stream->magic != &mem_file_magic)
357 internal_error ("mem_file_write: bad magic number");
358 if (stream->buffer == NULL)
359 {
360 stream->length_buffer = length_buffer;
361 stream->sizeof_buffer = length_buffer;
362 stream->buffer = xmalloc (stream->sizeof_buffer);
363 memcpy (stream->buffer, buffer, length_buffer);
364 }
365 else
366 {
367 int new_length = stream->length_buffer + length_buffer;
368 if (new_length >= stream->sizeof_buffer)
369 {
370 stream->sizeof_buffer = new_length;
371 stream->buffer = xrealloc (stream->buffer, stream->sizeof_buffer);
372 }
373 memcpy (stream->buffer + stream->length_buffer, buffer, length_buffer);
374 stream->length_buffer = new_length;
375 }
376}
377\f
378/* ``struct ui_file'' implementation that maps directly onto
379 <stdio.h>'s FILE. */
380
381static ui_file_write_ftype stdio_file_write;
382static ui_file_fputs_ftype stdio_file_fputs;
383static ui_file_isatty_ftype stdio_file_isatty;
384static ui_file_delete_ftype stdio_file_delete;
a14ed312 385static struct ui_file *stdio_file_new (FILE * file, int close_p);
d9fcf2fb
JM
386static ui_file_flush_ftype stdio_file_flush;
387
388static int stdio_file_magic;
389
390struct stdio_file
391 {
392 int *magic;
393 FILE *file;
394 int close_p;
395 };
396
397static struct ui_file *
fba45db2 398stdio_file_new (FILE *file, int close_p)
d9fcf2fb
JM
399{
400 struct ui_file *ui_file = ui_file_new ();
401 struct stdio_file *stdio = xmalloc (sizeof (struct stdio_file));
402 stdio->magic = &stdio_file_magic;
403 stdio->file = file;
404 stdio->close_p = close_p;
405 set_ui_file_data (ui_file, stdio, stdio_file_delete);
406 set_ui_file_flush (ui_file, stdio_file_flush);
407 set_ui_file_write (ui_file, stdio_file_write);
408 set_ui_file_fputs (ui_file, stdio_file_fputs);
409 set_ui_file_isatty (ui_file, stdio_file_isatty);
410 return ui_file;
411}
412
413static void
fba45db2 414stdio_file_delete (struct ui_file *file)
d9fcf2fb
JM
415{
416 struct stdio_file *stdio = ui_file_data (file);
417 if (stdio->magic != &stdio_file_magic)
418 internal_error ("stdio_file_delete: bad magic number");
419 if (stdio->close_p)
420 {
421 fclose (stdio->file);
422 }
b8c9b27d 423 xfree (stdio);
d9fcf2fb
JM
424}
425
426static void
fba45db2 427stdio_file_flush (struct ui_file *file)
d9fcf2fb
JM
428{
429 struct stdio_file *stdio = ui_file_data (file);
430 if (stdio->magic != &stdio_file_magic)
431 internal_error ("stdio_file_flush: bad magic number");
432 fflush (stdio->file);
433}
434
435static void
436stdio_file_write (struct ui_file *file, const char *buf, long length_buf)
437{
438 struct stdio_file *stdio = ui_file_data (file);
439 if (stdio->magic != &stdio_file_magic)
440 internal_error ("stdio_file_write: bad magic number");
441 fwrite (buf, length_buf, 1, stdio->file);
442}
443
444static void
fba45db2 445stdio_file_fputs (const char *linebuffer, struct ui_file *file)
d9fcf2fb
JM
446{
447 struct stdio_file *stdio = ui_file_data (file);
448 if (stdio->magic != &stdio_file_magic)
449 internal_error ("stdio_file_fputs: bad magic number");
450 fputs (linebuffer, stdio->file);
451}
452
453static int
fba45db2 454stdio_file_isatty (struct ui_file *file)
d9fcf2fb
JM
455{
456 struct stdio_file *stdio = ui_file_data (file);
457 if (stdio->magic != &stdio_file_magic)
458 internal_error ("stdio_file_isatty: bad magic number");
459 return (isatty (fileno (stdio->file)));
460}
461
462/* Like fdopen(). Create a ui_file from a previously opened FILE. */
463
464struct ui_file *
fba45db2 465stdio_fileopen (FILE *file)
d9fcf2fb
JM
466{
467 return stdio_file_new (file, 0);
468}
469
470struct ui_file *
fba45db2 471gdb_fopen (char *name, char *mode)
d9fcf2fb
JM
472{
473 FILE *f = fopen (name, mode);
474 if (f == NULL)
475 return NULL;
476 return stdio_file_new (f, 1);
477}
This page took 0.094035 seconds and 4 git commands to generate.