1 /* UI_FILE - a generic STDIO like output stream.
3 Copyright (C) 1999, 2000, 2001, 2002, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* Implement the ``struct ui_file'' object. */
25 #include "gdb_obstack.h"
26 #include "gdb_string.h"
27 #include "gdb_select.h"
31 static ui_file_isatty_ftype null_file_isatty;
32 static ui_file_write_ftype null_file_write;
33 static ui_file_fputs_ftype null_file_fputs;
34 static ui_file_read_ftype null_file_read;
35 static ui_file_flush_ftype null_file_flush;
36 static ui_file_delete_ftype null_file_delete;
37 static ui_file_rewind_ftype null_file_rewind;
38 static ui_file_put_ftype null_file_put;
43 ui_file_flush_ftype *to_flush;
44 ui_file_write_ftype *to_write;
45 ui_file_fputs_ftype *to_fputs;
46 ui_file_read_ftype *to_read;
47 ui_file_delete_ftype *to_delete;
48 ui_file_isatty_ftype *to_isatty;
49 ui_file_rewind_ftype *to_rewind;
50 ui_file_put_ftype *to_put;
58 struct ui_file *file = xmalloc (sizeof (struct ui_file));
60 file->magic = &ui_file_magic;
61 set_ui_file_data (file, NULL, null_file_delete);
62 set_ui_file_flush (file, null_file_flush);
63 set_ui_file_write (file, null_file_write);
64 set_ui_file_fputs (file, null_file_fputs);
65 set_ui_file_read (file, null_file_read);
66 set_ui_file_isatty (file, null_file_isatty);
67 set_ui_file_rewind (file, null_file_rewind);
68 set_ui_file_put (file, null_file_put);
73 ui_file_delete (struct ui_file *file)
75 file->to_delete (file);
80 null_file_isatty (struct ui_file *file)
86 null_file_rewind (struct ui_file *file)
92 null_file_put (struct ui_file *file,
93 ui_file_put_method_ftype *write,
100 null_file_flush (struct ui_file *file)
106 null_file_write (struct ui_file *file,
110 if (file->to_fputs == null_file_fputs)
111 /* Both the write and fputs methods are null. Discard the
116 /* The fputs method isn't null, slowly pass the write request
117 onto that. FYI, this isn't as bad as it may look - the
118 current (as of 1999-11-07) printf_* function calls fputc and
119 fputc does exactly the below. By having a write function it
120 is possible to clean up that code. */
125 for (i = 0; i < sizeof_buf; i++)
128 file->to_fputs (b, file);
135 null_file_read (struct ui_file *file,
144 null_file_fputs (const char *buf, struct ui_file *file)
146 if (file->to_write == null_file_write)
147 /* Both the write and fputs methods are null. Discard the
152 /* The write method was implemented, use that. */
153 file->to_write (file, buf, strlen (buf));
158 null_file_delete (struct ui_file *file)
164 ui_file_data (struct ui_file *file)
166 if (file->magic != &ui_file_magic)
167 internal_error (__FILE__, __LINE__,
168 _("ui_file_data: bad magic number"));
169 return file->to_data;
173 gdb_flush (struct ui_file *file)
175 file->to_flush (file);
179 ui_file_isatty (struct ui_file *file)
181 return file->to_isatty (file);
185 ui_file_rewind (struct ui_file *file)
187 file->to_rewind (file);
191 ui_file_put (struct ui_file *file,
192 ui_file_put_method_ftype *write,
195 file->to_put (file, write, dest);
199 ui_file_write (struct ui_file *file,
203 file->to_write (file, buf, length_buf);
207 ui_file_read (struct ui_file *file, char *buf, long length_buf)
209 return file->to_read (file, buf, length_buf);
213 fputs_unfiltered (const char *buf, struct ui_file *file)
215 file->to_fputs (buf, file);
219 set_ui_file_flush (struct ui_file *file, ui_file_flush_ftype *flush)
221 file->to_flush = flush;
225 set_ui_file_isatty (struct ui_file *file, ui_file_isatty_ftype *isatty)
227 file->to_isatty = isatty;
231 set_ui_file_rewind (struct ui_file *file, ui_file_rewind_ftype *rewind)
233 file->to_rewind = rewind;
237 set_ui_file_put (struct ui_file *file, ui_file_put_ftype *put)
243 set_ui_file_write (struct ui_file *file,
244 ui_file_write_ftype *write)
246 file->to_write = write;
250 set_ui_file_read (struct ui_file *file, ui_file_read_ftype *read)
252 file->to_read = read;
256 set_ui_file_fputs (struct ui_file *file, ui_file_fputs_ftype *fputs)
258 file->to_fputs = fputs;
262 set_ui_file_data (struct ui_file *file, void *data,
263 ui_file_delete_ftype *delete)
265 file->to_data = data;
266 file->to_delete = delete;
269 /* ui_file utility function for converting a ``struct ui_file'' into
272 struct accumulated_ui_file
279 do_ui_file_xstrdup (void *context, const char *buffer, long length)
281 struct accumulated_ui_file *acc = context;
283 if (acc->buffer == NULL)
284 acc->buffer = xmalloc (length + 1);
286 acc->buffer = xrealloc (acc->buffer, acc->length + length + 1);
287 memcpy (acc->buffer + acc->length, buffer, length);
288 acc->length += length;
289 acc->buffer[acc->length] = '\0';
293 ui_file_xstrdup (struct ui_file *file, long *length)
295 struct accumulated_ui_file acc;
299 ui_file_put (file, do_ui_file_xstrdup, &acc);
300 if (acc.buffer == NULL)
301 acc.buffer = xstrdup ("");
303 *length = acc.length;
308 do_ui_file_obsavestring (void *context, const char *buffer, long length)
310 struct obstack *obstack = (struct obstack *) context;
312 obstack_grow (obstack, buffer, length);
316 ui_file_obsavestring (struct ui_file *file, struct obstack *obstack,
319 ui_file_put (file, do_ui_file_obsavestring, obstack);
320 *length = obstack_object_size (obstack);
321 obstack_1grow (obstack, '\0');
322 return obstack_finish (obstack);
325 /* A pure memory based ``struct ui_file'' that can be used an output
326 buffer. The buffers accumulated contents are available via
337 static ui_file_rewind_ftype mem_file_rewind;
338 static ui_file_put_ftype mem_file_put;
339 static ui_file_write_ftype mem_file_write;
340 static ui_file_delete_ftype mem_file_delete;
341 static struct ui_file *mem_file_new (void);
342 static int mem_file_magic;
344 static struct ui_file *
347 struct mem_file *stream = XMALLOC (struct mem_file);
348 struct ui_file *file = ui_file_new ();
350 set_ui_file_data (file, stream, mem_file_delete);
351 set_ui_file_rewind (file, mem_file_rewind);
352 set_ui_file_put (file, mem_file_put);
353 set_ui_file_write (file, mem_file_write);
354 stream->magic = &mem_file_magic;
355 stream->buffer = NULL;
356 stream->sizeof_buffer = 0;
357 stream->length_buffer = 0;
362 mem_file_delete (struct ui_file *file)
364 struct mem_file *stream = ui_file_data (file);
366 if (stream->magic != &mem_file_magic)
367 internal_error (__FILE__, __LINE__,
368 _("mem_file_delete: bad magic number"));
369 if (stream->buffer != NULL)
370 xfree (stream->buffer);
377 return mem_file_new ();
381 mem_file_rewind (struct ui_file *file)
383 struct mem_file *stream = ui_file_data (file);
385 if (stream->magic != &mem_file_magic)
386 internal_error (__FILE__, __LINE__,
387 _("mem_file_rewind: bad magic number"));
388 stream->length_buffer = 0;
392 mem_file_put (struct ui_file *file,
393 ui_file_put_method_ftype *write,
396 struct mem_file *stream = ui_file_data (file);
398 if (stream->magic != &mem_file_magic)
399 internal_error (__FILE__, __LINE__,
400 _("mem_file_put: bad magic number"));
401 if (stream->length_buffer > 0)
402 write (dest, stream->buffer, stream->length_buffer);
406 mem_file_write (struct ui_file *file,
410 struct mem_file *stream = ui_file_data (file);
412 if (stream->magic != &mem_file_magic)
413 internal_error (__FILE__, __LINE__,
414 _("mem_file_write: bad magic number"));
415 if (stream->buffer == NULL)
417 stream->length_buffer = length_buffer;
418 stream->sizeof_buffer = length_buffer;
419 stream->buffer = xmalloc (stream->sizeof_buffer);
420 memcpy (stream->buffer, buffer, length_buffer);
424 int new_length = stream->length_buffer + length_buffer;
426 if (new_length >= stream->sizeof_buffer)
428 stream->sizeof_buffer = new_length;
429 stream->buffer = xrealloc (stream->buffer, stream->sizeof_buffer);
431 memcpy (stream->buffer + stream->length_buffer, buffer, length_buffer);
432 stream->length_buffer = new_length;
436 /* ``struct ui_file'' implementation that maps directly onto
439 static ui_file_write_ftype stdio_file_write;
440 static ui_file_fputs_ftype stdio_file_fputs;
441 static ui_file_read_ftype stdio_file_read;
442 static ui_file_isatty_ftype stdio_file_isatty;
443 static ui_file_delete_ftype stdio_file_delete;
444 static struct ui_file *stdio_file_new (FILE * file, int close_p);
445 static ui_file_flush_ftype stdio_file_flush;
447 static int stdio_file_magic;
456 static struct ui_file *
457 stdio_file_new (FILE *file, int close_p)
459 struct ui_file *ui_file = ui_file_new ();
460 struct stdio_file *stdio = xmalloc (sizeof (struct stdio_file));
462 stdio->magic = &stdio_file_magic;
464 stdio->close_p = close_p;
465 set_ui_file_data (ui_file, stdio, stdio_file_delete);
466 set_ui_file_flush (ui_file, stdio_file_flush);
467 set_ui_file_write (ui_file, stdio_file_write);
468 set_ui_file_fputs (ui_file, stdio_file_fputs);
469 set_ui_file_read (ui_file, stdio_file_read);
470 set_ui_file_isatty (ui_file, stdio_file_isatty);
475 stdio_file_delete (struct ui_file *file)
477 struct stdio_file *stdio = ui_file_data (file);
479 if (stdio->magic != &stdio_file_magic)
480 internal_error (__FILE__, __LINE__,
481 _("stdio_file_delete: bad magic number"));
484 fclose (stdio->file);
490 stdio_file_flush (struct ui_file *file)
492 struct stdio_file *stdio = ui_file_data (file);
494 if (stdio->magic != &stdio_file_magic)
495 internal_error (__FILE__, __LINE__,
496 _("stdio_file_flush: bad magic number"));
497 fflush (stdio->file);
501 stdio_file_read (struct ui_file *file, char *buf, long length_buf)
503 struct stdio_file *stdio = ui_file_data (file);
505 if (stdio->magic != &stdio_file_magic)
506 internal_error (__FILE__, __LINE__,
507 _("stdio_file_read: bad magic number"));
509 /* For the benefit of Windows, call gdb_select before reading from
510 the file. Wait until at least one byte of data is available.
511 Control-C can interrupt gdb_select, but not read. */
513 int fd = fileno (stdio->file);
517 FD_SET (fd, &readfds);
518 if (gdb_select (fd + 1, &readfds, NULL, NULL, NULL) == -1)
522 return read (fileno (stdio->file), buf, length_buf);
526 stdio_file_write (struct ui_file *file, const char *buf, long length_buf)
528 struct stdio_file *stdio = ui_file_data (file);
530 if (stdio->magic != &stdio_file_magic)
531 internal_error (__FILE__, __LINE__,
532 _("stdio_file_write: bad magic number"));
533 /* Calling error crashes when we are called from the exception framework. */
534 if (fwrite (buf, length_buf, 1, stdio->file))
539 stdio_file_fputs (const char *linebuffer, struct ui_file *file)
541 struct stdio_file *stdio = ui_file_data (file);
543 if (stdio->magic != &stdio_file_magic)
544 internal_error (__FILE__, __LINE__,
545 _("stdio_file_fputs: bad magic number"));
546 /* Calling error crashes when we are called from the exception framework. */
547 if (fputs (linebuffer, stdio->file))
552 stdio_file_isatty (struct ui_file *file)
554 struct stdio_file *stdio = ui_file_data (file);
556 if (stdio->magic != &stdio_file_magic)
557 internal_error (__FILE__, __LINE__,
558 _("stdio_file_isatty: bad magic number"));
559 return (isatty (fileno (stdio->file)));
562 /* Like fdopen(). Create a ui_file from a previously opened FILE. */
565 stdio_fileopen (FILE *file)
567 return stdio_file_new (file, 0);
571 gdb_fopen (char *name, char *mode)
573 FILE *f = fopen (name, mode);
577 return stdio_file_new (f, 1);
580 /* ``struct ui_file'' implementation that maps onto two ui-file objects. */
582 static ui_file_write_ftype tee_file_write;
583 static ui_file_fputs_ftype tee_file_fputs;
584 static ui_file_isatty_ftype tee_file_isatty;
585 static ui_file_delete_ftype tee_file_delete;
586 static ui_file_flush_ftype tee_file_flush;
588 static int tee_file_magic;
593 struct ui_file *one, *two;
594 int close_one, close_two;
598 tee_file_new (struct ui_file *one, int close_one,
599 struct ui_file *two, int close_two)
601 struct ui_file *ui_file = ui_file_new ();
602 struct tee_file *tee = xmalloc (sizeof (struct tee_file));
604 tee->magic = &tee_file_magic;
607 tee->close_one = close_one;
608 tee->close_two = close_two;
609 set_ui_file_data (ui_file, tee, tee_file_delete);
610 set_ui_file_flush (ui_file, tee_file_flush);
611 set_ui_file_write (ui_file, tee_file_write);
612 set_ui_file_fputs (ui_file, tee_file_fputs);
613 set_ui_file_isatty (ui_file, tee_file_isatty);
618 tee_file_delete (struct ui_file *file)
620 struct tee_file *tee = ui_file_data (file);
622 if (tee->magic != &tee_file_magic)
623 internal_error (__FILE__, __LINE__,
624 _("tee_file_delete: bad magic number"));
626 ui_file_delete (tee->one);
628 ui_file_delete (tee->two);
634 tee_file_flush (struct ui_file *file)
636 struct tee_file *tee = ui_file_data (file);
638 if (tee->magic != &tee_file_magic)
639 internal_error (__FILE__, __LINE__,
640 _("tee_file_flush: bad magic number"));
641 tee->one->to_flush (tee->one);
642 tee->two->to_flush (tee->two);
646 tee_file_write (struct ui_file *file, const char *buf, long length_buf)
648 struct tee_file *tee = ui_file_data (file);
650 if (tee->magic != &tee_file_magic)
651 internal_error (__FILE__, __LINE__,
652 _("tee_file_write: bad magic number"));
653 ui_file_write (tee->one, buf, length_buf);
654 ui_file_write (tee->two, buf, length_buf);
658 tee_file_fputs (const char *linebuffer, struct ui_file *file)
660 struct tee_file *tee = ui_file_data (file);
662 if (tee->magic != &tee_file_magic)
663 internal_error (__FILE__, __LINE__,
664 _("tee_file_fputs: bad magic number"));
665 tee->one->to_fputs (linebuffer, tee->one);
666 tee->two->to_fputs (linebuffer, tee->two);
670 tee_file_isatty (struct ui_file *file)
672 struct tee_file *tee = ui_file_data (file);
674 if (tee->magic != &tee_file_magic)
675 internal_error (__FILE__, __LINE__,
676 _("tee_file_isatty: bad magic number"));
678 return ui_file_isatty (tee->one);