1 /* UI_FILE - a generic STDIO like output stream.
3 Copyright (C) 1999-2014 Free Software Foundation, Inc.
5 This file is part of GDB.
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
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* Implement the ``struct ui_file'' object. */
24 #include "gdb_obstack.h"
26 #include "gdb_select.h"
27 #include "filestuff.h"
31 static ui_file_isatty_ftype null_file_isatty;
32 static ui_file_write_ftype null_file_write;
33 static ui_file_write_ftype null_file_write_async_safe;
34 static ui_file_fputs_ftype null_file_fputs;
35 static ui_file_read_ftype null_file_read;
36 static ui_file_flush_ftype null_file_flush;
37 static ui_file_delete_ftype null_file_delete;
38 static ui_file_rewind_ftype null_file_rewind;
39 static ui_file_put_ftype null_file_put;
40 static ui_file_fseek_ftype null_file_fseek;
45 ui_file_flush_ftype *to_flush;
46 ui_file_write_ftype *to_write;
47 ui_file_write_async_safe_ftype *to_write_async_safe;
48 ui_file_fputs_ftype *to_fputs;
49 ui_file_read_ftype *to_read;
50 ui_file_delete_ftype *to_delete;
51 ui_file_isatty_ftype *to_isatty;
52 ui_file_rewind_ftype *to_rewind;
53 ui_file_put_ftype *to_put;
54 ui_file_fseek_ftype *to_fseek;
62 struct ui_file *file = xmalloc (sizeof (struct ui_file));
64 file->magic = &ui_file_magic;
65 set_ui_file_data (file, NULL, null_file_delete);
66 set_ui_file_flush (file, null_file_flush);
67 set_ui_file_write (file, null_file_write);
68 set_ui_file_write_async_safe (file, null_file_write_async_safe);
69 set_ui_file_fputs (file, null_file_fputs);
70 set_ui_file_read (file, null_file_read);
71 set_ui_file_isatty (file, null_file_isatty);
72 set_ui_file_rewind (file, null_file_rewind);
73 set_ui_file_put (file, null_file_put);
74 set_ui_file_fseek (file, null_file_fseek);
79 ui_file_delete (struct ui_file *file)
81 file->to_delete (file);
86 null_file_isatty (struct ui_file *file)
92 null_file_rewind (struct ui_file *file)
98 null_file_put (struct ui_file *file,
99 ui_file_put_method_ftype *write,
106 null_file_flush (struct ui_file *file)
112 null_file_write (struct ui_file *file,
116 if (file->to_fputs == null_file_fputs)
117 /* Both the write and fputs methods are null. Discard the
122 /* The fputs method isn't null, slowly pass the write request
123 onto that. FYI, this isn't as bad as it may look - the
124 current (as of 1999-11-07) printf_* function calls fputc and
125 fputc does exactly the below. By having a write function it
126 is possible to clean up that code. */
131 for (i = 0; i < sizeof_buf; i++)
134 file->to_fputs (b, file);
141 null_file_read (struct ui_file *file,
150 null_file_fputs (const char *buf, struct ui_file *file)
152 if (file->to_write == null_file_write)
153 /* Both the write and fputs methods are null. Discard the
158 /* The write method was implemented, use that. */
159 file->to_write (file, buf, strlen (buf));
164 null_file_write_async_safe (struct ui_file *file,
172 null_file_delete (struct ui_file *file)
178 null_file_fseek (struct ui_file *stream, long offset, int whence)
186 ui_file_data (struct ui_file *file)
188 if (file->magic != &ui_file_magic)
189 internal_error (__FILE__, __LINE__,
190 _("ui_file_data: bad magic number"));
191 return file->to_data;
195 gdb_flush (struct ui_file *file)
197 file->to_flush (file);
201 ui_file_isatty (struct ui_file *file)
203 return file->to_isatty (file);
207 ui_file_rewind (struct ui_file *file)
209 file->to_rewind (file);
213 ui_file_put (struct ui_file *file,
214 ui_file_put_method_ftype *write,
217 file->to_put (file, write, dest);
221 ui_file_write (struct ui_file *file,
225 file->to_write (file, buf, length_buf);
229 ui_file_write_async_safe (struct ui_file *file,
233 file->to_write_async_safe (file, buf, length_buf);
237 ui_file_read (struct ui_file *file, char *buf, long length_buf)
239 return file->to_read (file, buf, length_buf);
243 ui_file_fseek (struct ui_file *file, long offset, int whence)
245 return file->to_fseek (file, offset, whence);
249 fputs_unfiltered (const char *buf, struct ui_file *file)
251 file->to_fputs (buf, file);
255 set_ui_file_flush (struct ui_file *file, ui_file_flush_ftype *flush_ptr)
257 file->to_flush = flush_ptr;
261 set_ui_file_isatty (struct ui_file *file, ui_file_isatty_ftype *isatty_ptr)
263 file->to_isatty = isatty_ptr;
267 set_ui_file_rewind (struct ui_file *file, ui_file_rewind_ftype *rewind_ptr)
269 file->to_rewind = rewind_ptr;
273 set_ui_file_put (struct ui_file *file, ui_file_put_ftype *put_ptr)
275 file->to_put = put_ptr;
279 set_ui_file_write (struct ui_file *file,
280 ui_file_write_ftype *write_ptr)
282 file->to_write = write_ptr;
286 set_ui_file_write_async_safe (struct ui_file *file,
287 ui_file_write_async_safe_ftype *write_async_safe_ptr)
289 file->to_write_async_safe = write_async_safe_ptr;
293 set_ui_file_read (struct ui_file *file, ui_file_read_ftype *read_ptr)
295 file->to_read = read_ptr;
299 set_ui_file_fputs (struct ui_file *file, ui_file_fputs_ftype *fputs_ptr)
301 file->to_fputs = fputs_ptr;
305 set_ui_file_fseek (struct ui_file *file, ui_file_fseek_ftype *fseek_ptr)
307 file->to_fseek = fseek_ptr;
311 set_ui_file_data (struct ui_file *file, void *data,
312 ui_file_delete_ftype *delete_ptr)
314 file->to_data = data;
315 file->to_delete = delete_ptr;
318 /* ui_file utility function for converting a ``struct ui_file'' into
321 struct accumulated_ui_file
328 do_ui_file_xstrdup (void *context, const char *buffer, long length)
330 struct accumulated_ui_file *acc = context;
332 if (acc->buffer == NULL)
333 acc->buffer = xmalloc (length + 1);
335 acc->buffer = xrealloc (acc->buffer, acc->length + length + 1);
336 memcpy (acc->buffer + acc->length, buffer, length);
337 acc->length += length;
338 acc->buffer[acc->length] = '\0';
342 ui_file_xstrdup (struct ui_file *file, long *length)
344 struct accumulated_ui_file acc;
348 ui_file_put (file, do_ui_file_xstrdup, &acc);
349 if (acc.buffer == NULL)
350 acc.buffer = xstrdup ("");
352 *length = acc.length;
357 do_ui_file_obsavestring (void *context, const char *buffer, long length)
359 struct obstack *obstack = (struct obstack *) context;
361 obstack_grow (obstack, buffer, length);
365 ui_file_obsavestring (struct ui_file *file, struct obstack *obstack,
368 ui_file_put (file, do_ui_file_obsavestring, obstack);
369 *length = obstack_object_size (obstack);
370 obstack_1grow (obstack, '\0');
371 return obstack_finish (obstack);
374 /* A pure memory based ``struct ui_file'' that can be used an output
375 buffer. The buffers accumulated contents are available via
386 static ui_file_rewind_ftype mem_file_rewind;
387 static ui_file_put_ftype mem_file_put;
388 static ui_file_write_ftype mem_file_write;
389 static ui_file_delete_ftype mem_file_delete;
390 static struct ui_file *mem_file_new (void);
391 static int mem_file_magic;
393 static struct ui_file *
396 struct mem_file *stream = XNEW (struct mem_file);
397 struct ui_file *file = ui_file_new ();
399 set_ui_file_data (file, stream, mem_file_delete);
400 set_ui_file_rewind (file, mem_file_rewind);
401 set_ui_file_put (file, mem_file_put);
402 set_ui_file_write (file, mem_file_write);
403 stream->magic = &mem_file_magic;
404 stream->buffer = NULL;
405 stream->sizeof_buffer = 0;
406 stream->length_buffer = 0;
411 mem_file_delete (struct ui_file *file)
413 struct mem_file *stream = ui_file_data (file);
415 if (stream->magic != &mem_file_magic)
416 internal_error (__FILE__, __LINE__,
417 _("mem_file_delete: bad magic number"));
418 if (stream->buffer != NULL)
419 xfree (stream->buffer);
426 return mem_file_new ();
430 mem_file_rewind (struct ui_file *file)
432 struct mem_file *stream = ui_file_data (file);
434 if (stream->magic != &mem_file_magic)
435 internal_error (__FILE__, __LINE__,
436 _("mem_file_rewind: bad magic number"));
437 stream->length_buffer = 0;
441 mem_file_put (struct ui_file *file,
442 ui_file_put_method_ftype *write,
445 struct mem_file *stream = ui_file_data (file);
447 if (stream->magic != &mem_file_magic)
448 internal_error (__FILE__, __LINE__,
449 _("mem_file_put: bad magic number"));
450 if (stream->length_buffer > 0)
451 write (dest, stream->buffer, stream->length_buffer);
455 mem_file_write (struct ui_file *file,
459 struct mem_file *stream = ui_file_data (file);
461 if (stream->magic != &mem_file_magic)
462 internal_error (__FILE__, __LINE__,
463 _("mem_file_write: bad magic number"));
464 if (stream->buffer == NULL)
466 stream->length_buffer = length_buffer;
467 stream->sizeof_buffer = length_buffer;
468 stream->buffer = xmalloc (stream->sizeof_buffer);
469 memcpy (stream->buffer, buffer, length_buffer);
473 int new_length = stream->length_buffer + length_buffer;
475 if (new_length >= stream->sizeof_buffer)
477 stream->sizeof_buffer = new_length;
478 stream->buffer = xrealloc (stream->buffer, stream->sizeof_buffer);
480 memcpy (stream->buffer + stream->length_buffer, buffer, length_buffer);
481 stream->length_buffer = new_length;
485 /* ``struct ui_file'' implementation that maps directly onto
488 static ui_file_write_ftype stdio_file_write;
489 static ui_file_write_async_safe_ftype stdio_file_write_async_safe;
490 static ui_file_fputs_ftype stdio_file_fputs;
491 static ui_file_read_ftype stdio_file_read;
492 static ui_file_isatty_ftype stdio_file_isatty;
493 static ui_file_delete_ftype stdio_file_delete;
494 static struct ui_file *stdio_file_new (FILE *file, int close_p);
495 static ui_file_flush_ftype stdio_file_flush;
496 static ui_file_fseek_ftype stdio_file_fseek;
498 static int stdio_file_magic;
504 /* The associated file descriptor is extracted ahead of time for
505 stdio_file_write_async_safe's benefit, in case fileno isn't async-safe. */
510 static struct ui_file *
511 stdio_file_new (FILE *file, int close_p)
513 struct ui_file *ui_file = ui_file_new ();
514 struct stdio_file *stdio = xmalloc (sizeof (struct stdio_file));
516 stdio->magic = &stdio_file_magic;
518 stdio->fd = fileno (file);
519 stdio->close_p = close_p;
520 set_ui_file_data (ui_file, stdio, stdio_file_delete);
521 set_ui_file_flush (ui_file, stdio_file_flush);
522 set_ui_file_write (ui_file, stdio_file_write);
523 set_ui_file_write_async_safe (ui_file, stdio_file_write_async_safe);
524 set_ui_file_fputs (ui_file, stdio_file_fputs);
525 set_ui_file_read (ui_file, stdio_file_read);
526 set_ui_file_isatty (ui_file, stdio_file_isatty);
527 set_ui_file_fseek (ui_file, stdio_file_fseek);
532 stdio_file_delete (struct ui_file *file)
534 struct stdio_file *stdio = ui_file_data (file);
536 if (stdio->magic != &stdio_file_magic)
537 internal_error (__FILE__, __LINE__,
538 _("stdio_file_delete: bad magic number"));
541 fclose (stdio->file);
547 stdio_file_flush (struct ui_file *file)
549 struct stdio_file *stdio = ui_file_data (file);
551 if (stdio->magic != &stdio_file_magic)
552 internal_error (__FILE__, __LINE__,
553 _("stdio_file_flush: bad magic number"));
554 fflush (stdio->file);
558 stdio_file_read (struct ui_file *file, char *buf, long length_buf)
560 struct stdio_file *stdio = ui_file_data (file);
562 if (stdio->magic != &stdio_file_magic)
563 internal_error (__FILE__, __LINE__,
564 _("stdio_file_read: bad magic number"));
566 /* For the benefit of Windows, call gdb_select before reading from
567 the file. Wait until at least one byte of data is available.
568 Control-C can interrupt gdb_select, but not read. */
572 FD_SET (stdio->fd, &readfds);
573 if (gdb_select (stdio->fd + 1, &readfds, NULL, NULL, NULL) == -1)
577 return read (stdio->fd, buf, length_buf);
581 stdio_file_write (struct ui_file *file, const char *buf, long length_buf)
583 struct stdio_file *stdio = ui_file_data (file);
585 if (stdio->magic != &stdio_file_magic)
586 internal_error (__FILE__, __LINE__,
587 _("stdio_file_write: bad magic number"));
588 /* Calling error crashes when we are called from the exception framework. */
589 if (fwrite (buf, length_buf, 1, stdio->file))
596 stdio_file_write_async_safe (struct ui_file *file,
597 const char *buf, long length_buf)
599 struct stdio_file *stdio = ui_file_data (file);
601 if (stdio->magic != &stdio_file_magic)
603 /* gettext isn't necessarily async safe, so we can't use _("error message") here.
604 We could extract the correct translation ahead of time, but this is an extremely
605 rare event, and one of the other stdio_file_* routines will presumably catch
606 the problem anyway. For now keep it simple and ignore the error here. */
610 /* This is written the way it is to avoid a warning from gcc about not using the
611 result of write (since it can be declared with attribute warn_unused_result).
612 Alas casting to void doesn't work for this. */
613 if (write (stdio->fd, buf, length_buf))
620 stdio_file_fputs (const char *linebuffer, struct ui_file *file)
622 struct stdio_file *stdio = ui_file_data (file);
624 if (stdio->magic != &stdio_file_magic)
625 internal_error (__FILE__, __LINE__,
626 _("stdio_file_fputs: bad magic number"));
627 /* Calling error crashes when we are called from the exception framework. */
628 if (fputs (linebuffer, stdio->file))
635 stdio_file_isatty (struct ui_file *file)
637 struct stdio_file *stdio = ui_file_data (file);
639 if (stdio->magic != &stdio_file_magic)
640 internal_error (__FILE__, __LINE__,
641 _("stdio_file_isatty: bad magic number"));
642 return (isatty (stdio->fd));
646 stdio_file_fseek (struct ui_file *file, long offset, int whence)
648 struct stdio_file *stdio = ui_file_data (file);
650 if (stdio->magic != &stdio_file_magic)
651 internal_error (__FILE__, __LINE__,
652 _("stdio_file_fseek: bad magic number"));
654 return fseek (stdio->file, offset, whence);
658 /* This is the implementation of ui_file method to_write for stderr.
659 gdb_stdout is flushed before writing to gdb_stderr. */
662 stderr_file_write (struct ui_file *file, const char *buf, long length_buf)
664 gdb_flush (gdb_stdout);
665 stdio_file_write (file, buf, length_buf);
668 /* This is the implementation of ui_file method to_fputs for stderr.
669 gdb_stdout is flushed before writing to gdb_stderr. */
672 stderr_file_fputs (const char *linebuffer, struct ui_file *file)
674 gdb_flush (gdb_stdout);
675 stdio_file_fputs (linebuffer, file);
680 stderr_fileopen (void)
682 struct ui_file *ui_file = stdio_fileopen (stderr);
685 /* There is no real line-buffering on Windows, see
686 http://msdn.microsoft.com/en-us/library/86cebhfs%28v=vs.71%29.aspx
687 so the stdout is either fully-buffered or non-buffered. We can't
688 make stdout non-buffered, because of two concerns,
689 1. non-buffering hurts performance,
690 2. non-buffering may change GDB's behavior when it is interacting
691 with front-end, such as Emacs.
693 We decided to leave stdout as fully buffered, but flush it first
694 when something is written to stderr. */
696 /* Method 'to_write_async_safe' is not overwritten, because there's
697 no way to flush a stream in an async-safe manner. Fortunately,
698 it doesn't really matter, because:
699 - that method is only used for printing internal debug output
700 from signal handlers.
701 - Windows hosts don't have a concept of async-safeness. Signal
702 handlers run in a separate thread, so they can call
703 the regular non-async-safe output routines freely. */
704 set_ui_file_write (ui_file, stderr_file_write);
705 set_ui_file_fputs (ui_file, stderr_file_fputs);
711 /* Like fdopen(). Create a ui_file from a previously opened FILE. */
714 stdio_fileopen (FILE *file)
716 return stdio_file_new (file, 0);
720 gdb_fopen (const char *name, const char *mode)
722 FILE *f = gdb_fopen_cloexec (name, mode);
726 return stdio_file_new (f, 1);
729 /* ``struct ui_file'' implementation that maps onto two ui-file objects. */
731 static ui_file_write_ftype tee_file_write;
732 static ui_file_fputs_ftype tee_file_fputs;
733 static ui_file_isatty_ftype tee_file_isatty;
734 static ui_file_delete_ftype tee_file_delete;
735 static ui_file_flush_ftype tee_file_flush;
737 static int tee_file_magic;
742 struct ui_file *one, *two;
743 int close_one, close_two;
747 tee_file_new (struct ui_file *one, int close_one,
748 struct ui_file *two, int close_two)
750 struct ui_file *ui_file = ui_file_new ();
751 struct tee_file *tee = xmalloc (sizeof (struct tee_file));
753 tee->magic = &tee_file_magic;
756 tee->close_one = close_one;
757 tee->close_two = close_two;
758 set_ui_file_data (ui_file, tee, tee_file_delete);
759 set_ui_file_flush (ui_file, tee_file_flush);
760 set_ui_file_write (ui_file, tee_file_write);
761 set_ui_file_fputs (ui_file, tee_file_fputs);
762 set_ui_file_isatty (ui_file, tee_file_isatty);
767 tee_file_delete (struct ui_file *file)
769 struct tee_file *tee = ui_file_data (file);
771 if (tee->magic != &tee_file_magic)
772 internal_error (__FILE__, __LINE__,
773 _("tee_file_delete: bad magic number"));
775 ui_file_delete (tee->one);
777 ui_file_delete (tee->two);
783 tee_file_flush (struct ui_file *file)
785 struct tee_file *tee = ui_file_data (file);
787 if (tee->magic != &tee_file_magic)
788 internal_error (__FILE__, __LINE__,
789 _("tee_file_flush: bad magic number"));
790 tee->one->to_flush (tee->one);
791 tee->two->to_flush (tee->two);
795 tee_file_write (struct ui_file *file, const char *buf, long length_buf)
797 struct tee_file *tee = ui_file_data (file);
799 if (tee->magic != &tee_file_magic)
800 internal_error (__FILE__, __LINE__,
801 _("tee_file_write: bad magic number"));
802 ui_file_write (tee->one, buf, length_buf);
803 ui_file_write (tee->two, buf, length_buf);
807 tee_file_fputs (const char *linebuffer, struct ui_file *file)
809 struct tee_file *tee = ui_file_data (file);
811 if (tee->magic != &tee_file_magic)
812 internal_error (__FILE__, __LINE__,
813 _("tee_file_fputs: bad magic number"));
814 tee->one->to_fputs (linebuffer, tee->one);
815 tee->two->to_fputs (linebuffer, tee->two);
819 tee_file_isatty (struct ui_file *file)
821 struct tee_file *tee = ui_file_data (file);
823 if (tee->magic != &tee_file_magic)
824 internal_error (__FILE__, __LINE__,
825 _("tee_file_isatty: bad magic number"));
827 return ui_file_isatty (tee->one);