1 /* UI_FILE - a generic STDIO like output stream.
3 Copyright (C) 1999-2016 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"
25 #include "gdb_select.h"
26 #include "filestuff.h"
28 static ui_file_isatty_ftype null_file_isatty
;
29 static ui_file_write_ftype null_file_write
;
30 static ui_file_write_ftype null_file_write_async_safe
;
31 static ui_file_fputs_ftype null_file_fputs
;
32 static ui_file_read_ftype null_file_read
;
33 static ui_file_flush_ftype null_file_flush
;
34 static ui_file_delete_ftype null_file_delete
;
35 static ui_file_rewind_ftype null_file_rewind
;
36 static ui_file_put_ftype null_file_put
;
37 static ui_file_fseek_ftype null_file_fseek
;
42 ui_file_flush_ftype
*to_flush
;
43 ui_file_write_ftype
*to_write
;
44 ui_file_write_async_safe_ftype
*to_write_async_safe
;
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
;
51 ui_file_fseek_ftype
*to_fseek
;
59 struct ui_file
*file
= XNEW (struct ui_file
);
61 file
->magic
= &ui_file_magic
;
62 set_ui_file_data (file
, NULL
, null_file_delete
);
63 set_ui_file_flush (file
, null_file_flush
);
64 set_ui_file_write (file
, null_file_write
);
65 set_ui_file_write_async_safe (file
, null_file_write_async_safe
);
66 set_ui_file_fputs (file
, null_file_fputs
);
67 set_ui_file_read (file
, null_file_read
);
68 set_ui_file_isatty (file
, null_file_isatty
);
69 set_ui_file_rewind (file
, null_file_rewind
);
70 set_ui_file_put (file
, null_file_put
);
71 set_ui_file_fseek (file
, null_file_fseek
);
76 ui_file_delete (struct ui_file
*file
)
78 file
->to_delete (file
);
83 null_file_isatty (struct ui_file
*file
)
89 null_file_rewind (struct ui_file
*file
)
95 null_file_put (struct ui_file
*file
,
96 ui_file_put_method_ftype
*write
,
103 null_file_flush (struct ui_file
*file
)
109 null_file_write (struct ui_file
*file
,
113 if (file
->to_fputs
== null_file_fputs
)
114 /* Both the write and fputs methods are null. Discard the
119 /* The fputs method isn't null, slowly pass the write request
120 onto that. FYI, this isn't as bad as it may look - the
121 current (as of 1999-11-07) printf_* function calls fputc and
122 fputc does exactly the below. By having a write function it
123 is possible to clean up that code. */
128 for (i
= 0; i
< sizeof_buf
; i
++)
131 file
->to_fputs (b
, file
);
138 null_file_read (struct ui_file
*file
,
147 null_file_fputs (const char *buf
, struct ui_file
*file
)
149 if (file
->to_write
== null_file_write
)
150 /* Both the write and fputs methods are null. Discard the
155 /* The write method was implemented, use that. */
156 file
->to_write (file
, buf
, strlen (buf
));
161 null_file_write_async_safe (struct ui_file
*file
,
169 null_file_delete (struct ui_file
*file
)
175 null_file_fseek (struct ui_file
*stream
, long offset
, int whence
)
183 ui_file_data (struct ui_file
*file
)
185 if (file
->magic
!= &ui_file_magic
)
186 internal_error (__FILE__
, __LINE__
,
187 _("ui_file_data: bad magic number"));
188 return file
->to_data
;
192 gdb_flush (struct ui_file
*file
)
194 file
->to_flush (file
);
198 ui_file_isatty (struct ui_file
*file
)
200 return file
->to_isatty (file
);
204 ui_file_rewind (struct ui_file
*file
)
206 file
->to_rewind (file
);
210 ui_file_put (struct ui_file
*file
,
211 ui_file_put_method_ftype
*write
,
214 file
->to_put (file
, write
, dest
);
218 ui_file_write (struct ui_file
*file
,
222 file
->to_write (file
, buf
, length_buf
);
226 ui_file_write_for_put (void *data
, const char *buffer
, long length_buffer
)
228 ui_file_write ((struct ui_file
*) data
, buffer
, length_buffer
);
232 ui_file_write_async_safe (struct ui_file
*file
,
236 file
->to_write_async_safe (file
, buf
, length_buf
);
240 ui_file_read (struct ui_file
*file
, char *buf
, long length_buf
)
242 return file
->to_read (file
, buf
, length_buf
);
246 ui_file_fseek (struct ui_file
*file
, long offset
, int whence
)
248 return file
->to_fseek (file
, offset
, whence
);
252 fputs_unfiltered (const char *buf
, struct ui_file
*file
)
254 file
->to_fputs (buf
, file
);
258 set_ui_file_flush (struct ui_file
*file
, ui_file_flush_ftype
*flush_ptr
)
260 file
->to_flush
= flush_ptr
;
264 set_ui_file_isatty (struct ui_file
*file
, ui_file_isatty_ftype
*isatty_ptr
)
266 file
->to_isatty
= isatty_ptr
;
270 set_ui_file_rewind (struct ui_file
*file
, ui_file_rewind_ftype
*rewind_ptr
)
272 file
->to_rewind
= rewind_ptr
;
276 set_ui_file_put (struct ui_file
*file
, ui_file_put_ftype
*put_ptr
)
278 file
->to_put
= put_ptr
;
282 set_ui_file_write (struct ui_file
*file
,
283 ui_file_write_ftype
*write_ptr
)
285 file
->to_write
= write_ptr
;
289 set_ui_file_write_async_safe (struct ui_file
*file
,
290 ui_file_write_async_safe_ftype
*write_async_safe_ptr
)
292 file
->to_write_async_safe
= write_async_safe_ptr
;
296 set_ui_file_read (struct ui_file
*file
, ui_file_read_ftype
*read_ptr
)
298 file
->to_read
= read_ptr
;
302 set_ui_file_fputs (struct ui_file
*file
, ui_file_fputs_ftype
*fputs_ptr
)
304 file
->to_fputs
= fputs_ptr
;
308 set_ui_file_fseek (struct ui_file
*file
, ui_file_fseek_ftype
*fseek_ptr
)
310 file
->to_fseek
= fseek_ptr
;
314 set_ui_file_data (struct ui_file
*file
, void *data
,
315 ui_file_delete_ftype
*delete_ptr
)
317 file
->to_data
= data
;
318 file
->to_delete
= delete_ptr
;
321 /* ui_file utility function for converting a ``struct ui_file'' into
324 struct accumulated_ui_file
331 do_ui_file_xstrdup (void *context
, const char *buffer
, long length
)
333 struct accumulated_ui_file
*acc
= (struct accumulated_ui_file
*) context
;
335 if (acc
->buffer
== NULL
)
336 acc
->buffer
= (char *) xmalloc (length
+ 1);
338 acc
->buffer
= (char *) xrealloc (acc
->buffer
, acc
->length
+ length
+ 1);
339 memcpy (acc
->buffer
+ acc
->length
, buffer
, length
);
340 acc
->length
+= length
;
341 acc
->buffer
[acc
->length
] = '\0';
345 ui_file_xstrdup (struct ui_file
*file
, long *length
)
347 struct accumulated_ui_file acc
;
351 ui_file_put (file
, do_ui_file_xstrdup
, &acc
);
352 if (acc
.buffer
== NULL
)
353 acc
.buffer
= xstrdup ("");
355 *length
= acc
.length
;
359 /* ui_file utility function for converting a ``struct ui_file'' into a
363 do_ui_file_as_string (void *context
, const char *buffer
, long length
)
365 std::string
*str
= (std::string
*) context
;
367 *str
= std::string (buffer
, length
);
373 ui_file_as_string (struct ui_file
*file
)
377 ui_file_put (file
, do_ui_file_as_string
, &str
);
382 do_ui_file_obsavestring (void *context
, const char *buffer
, long length
)
384 struct obstack
*obstack
= (struct obstack
*) context
;
386 obstack_grow (obstack
, buffer
, length
);
390 ui_file_obsavestring (struct ui_file
*file
, struct obstack
*obstack
,
393 ui_file_put (file
, do_ui_file_obsavestring
, obstack
);
394 *length
= obstack_object_size (obstack
);
395 obstack_1grow (obstack
, '\0');
396 return (char *) obstack_finish (obstack
);
399 /* A pure memory based ``struct ui_file'' that can be used an output
400 buffer. The buffers accumulated contents are available via
411 static ui_file_rewind_ftype mem_file_rewind
;
412 static ui_file_put_ftype mem_file_put
;
413 static ui_file_write_ftype mem_file_write
;
414 static ui_file_delete_ftype mem_file_delete
;
415 static struct ui_file
*mem_file_new (void);
416 static int mem_file_magic
;
418 static struct ui_file
*
421 struct mem_file
*stream
= XNEW (struct mem_file
);
422 struct ui_file
*file
= ui_file_new ();
424 set_ui_file_data (file
, stream
, mem_file_delete
);
425 set_ui_file_rewind (file
, mem_file_rewind
);
426 set_ui_file_put (file
, mem_file_put
);
427 set_ui_file_write (file
, mem_file_write
);
428 stream
->magic
= &mem_file_magic
;
429 stream
->buffer
= NULL
;
430 stream
->sizeof_buffer
= 0;
431 stream
->length_buffer
= 0;
436 mem_file_delete (struct ui_file
*file
)
438 struct mem_file
*stream
= (struct mem_file
*) ui_file_data (file
);
440 if (stream
->magic
!= &mem_file_magic
)
441 internal_error (__FILE__
, __LINE__
,
442 _("mem_file_delete: bad magic number"));
443 if (stream
->buffer
!= NULL
)
444 xfree (stream
->buffer
);
451 return mem_file_new ();
455 mem_file_rewind (struct ui_file
*file
)
457 struct mem_file
*stream
= (struct mem_file
*) ui_file_data (file
);
459 if (stream
->magic
!= &mem_file_magic
)
460 internal_error (__FILE__
, __LINE__
,
461 _("mem_file_rewind: bad magic number"));
462 stream
->length_buffer
= 0;
466 mem_file_put (struct ui_file
*file
,
467 ui_file_put_method_ftype
*write
,
470 struct mem_file
*stream
= (struct mem_file
*) ui_file_data (file
);
472 if (stream
->magic
!= &mem_file_magic
)
473 internal_error (__FILE__
, __LINE__
,
474 _("mem_file_put: bad magic number"));
475 if (stream
->length_buffer
> 0)
476 write (dest
, stream
->buffer
, stream
->length_buffer
);
480 mem_file_write (struct ui_file
*file
,
484 struct mem_file
*stream
= (struct mem_file
*) ui_file_data (file
);
486 if (stream
->magic
!= &mem_file_magic
)
487 internal_error (__FILE__
, __LINE__
,
488 _("mem_file_write: bad magic number"));
489 if (stream
->buffer
== NULL
)
491 stream
->length_buffer
= length_buffer
;
492 stream
->sizeof_buffer
= length_buffer
;
493 stream
->buffer
= (char *) xmalloc (stream
->sizeof_buffer
);
494 memcpy (stream
->buffer
, buffer
, length_buffer
);
498 int new_length
= stream
->length_buffer
+ length_buffer
;
500 if (new_length
>= stream
->sizeof_buffer
)
502 stream
->sizeof_buffer
= new_length
;
504 = (char *) xrealloc (stream
->buffer
, stream
->sizeof_buffer
);
506 memcpy (stream
->buffer
+ stream
->length_buffer
, buffer
, length_buffer
);
507 stream
->length_buffer
= new_length
;
511 /* ``struct ui_file'' implementation that maps directly onto
514 static ui_file_write_ftype stdio_file_write
;
515 static ui_file_write_async_safe_ftype stdio_file_write_async_safe
;
516 static ui_file_fputs_ftype stdio_file_fputs
;
517 static ui_file_read_ftype stdio_file_read
;
518 static ui_file_isatty_ftype stdio_file_isatty
;
519 static ui_file_delete_ftype stdio_file_delete
;
520 static struct ui_file
*stdio_file_new (FILE *file
, int close_p
);
521 static ui_file_flush_ftype stdio_file_flush
;
522 static ui_file_fseek_ftype stdio_file_fseek
;
524 static int stdio_file_magic
;
530 /* The associated file descriptor is extracted ahead of time for
531 stdio_file_write_async_safe's benefit, in case fileno isn't async-safe. */
536 static struct ui_file
*
537 stdio_file_new (FILE *file
, int close_p
)
539 struct ui_file
*ui_file
= ui_file_new ();
540 struct stdio_file
*stdio
= XNEW (struct stdio_file
);
542 stdio
->magic
= &stdio_file_magic
;
544 stdio
->fd
= fileno (file
);
545 stdio
->close_p
= close_p
;
546 set_ui_file_data (ui_file
, stdio
, stdio_file_delete
);
547 set_ui_file_flush (ui_file
, stdio_file_flush
);
548 set_ui_file_write (ui_file
, stdio_file_write
);
549 set_ui_file_write_async_safe (ui_file
, stdio_file_write_async_safe
);
550 set_ui_file_fputs (ui_file
, stdio_file_fputs
);
551 set_ui_file_read (ui_file
, stdio_file_read
);
552 set_ui_file_isatty (ui_file
, stdio_file_isatty
);
553 set_ui_file_fseek (ui_file
, stdio_file_fseek
);
558 stdio_file_delete (struct ui_file
*file
)
560 struct stdio_file
*stdio
= (struct stdio_file
*) ui_file_data (file
);
562 if (stdio
->magic
!= &stdio_file_magic
)
563 internal_error (__FILE__
, __LINE__
,
564 _("stdio_file_delete: bad magic number"));
567 fclose (stdio
->file
);
573 stdio_file_flush (struct ui_file
*file
)
575 struct stdio_file
*stdio
= (struct stdio_file
*) ui_file_data (file
);
577 if (stdio
->magic
!= &stdio_file_magic
)
578 internal_error (__FILE__
, __LINE__
,
579 _("stdio_file_flush: bad magic number"));
580 fflush (stdio
->file
);
584 stdio_file_read (struct ui_file
*file
, char *buf
, long length_buf
)
586 struct stdio_file
*stdio
= (struct stdio_file
*) ui_file_data (file
);
588 if (stdio
->magic
!= &stdio_file_magic
)
589 internal_error (__FILE__
, __LINE__
,
590 _("stdio_file_read: bad magic number"));
592 /* Wait until at least one byte of data is available, or we get
593 interrupted with Control-C. */
598 FD_SET (stdio
->fd
, &readfds
);
599 if (interruptible_select (stdio
->fd
+ 1, &readfds
, NULL
, NULL
, NULL
) == -1)
603 return read (stdio
->fd
, buf
, length_buf
);
607 stdio_file_write (struct ui_file
*file
, const char *buf
, long length_buf
)
609 struct stdio_file
*stdio
= (struct stdio_file
*) ui_file_data (file
);
611 if (stdio
->magic
!= &stdio_file_magic
)
612 internal_error (__FILE__
, __LINE__
,
613 _("stdio_file_write: bad magic number"));
614 /* Calling error crashes when we are called from the exception framework. */
615 if (fwrite (buf
, length_buf
, 1, stdio
->file
))
622 stdio_file_write_async_safe (struct ui_file
*file
,
623 const char *buf
, long length_buf
)
625 struct stdio_file
*stdio
= (struct stdio_file
*) ui_file_data (file
);
627 if (stdio
->magic
!= &stdio_file_magic
)
629 /* gettext isn't necessarily async safe, so we can't use _("error message") here.
630 We could extract the correct translation ahead of time, but this is an extremely
631 rare event, and one of the other stdio_file_* routines will presumably catch
632 the problem anyway. For now keep it simple and ignore the error here. */
636 /* This is written the way it is to avoid a warning from gcc about not using the
637 result of write (since it can be declared with attribute warn_unused_result).
638 Alas casting to void doesn't work for this. */
639 if (write (stdio
->fd
, buf
, length_buf
))
646 stdio_file_fputs (const char *linebuffer
, struct ui_file
*file
)
648 struct stdio_file
*stdio
= (struct stdio_file
*) ui_file_data (file
);
650 if (stdio
->magic
!= &stdio_file_magic
)
651 internal_error (__FILE__
, __LINE__
,
652 _("stdio_file_fputs: bad magic number"));
653 /* Calling error crashes when we are called from the exception framework. */
654 if (fputs (linebuffer
, stdio
->file
))
661 stdio_file_isatty (struct ui_file
*file
)
663 struct stdio_file
*stdio
= (struct stdio_file
*) ui_file_data (file
);
665 if (stdio
->magic
!= &stdio_file_magic
)
666 internal_error (__FILE__
, __LINE__
,
667 _("stdio_file_isatty: bad magic number"));
668 return (isatty (stdio
->fd
));
672 stdio_file_fseek (struct ui_file
*file
, long offset
, int whence
)
674 struct stdio_file
*stdio
= (struct stdio_file
*) ui_file_data (file
);
676 if (stdio
->magic
!= &stdio_file_magic
)
677 internal_error (__FILE__
, __LINE__
,
678 _("stdio_file_fseek: bad magic number"));
680 return fseek (stdio
->file
, offset
, whence
);
684 /* This is the implementation of ui_file method to_write for stderr.
685 gdb_stdout is flushed before writing to gdb_stderr. */
688 stderr_file_write (struct ui_file
*file
, const char *buf
, long length_buf
)
690 gdb_flush (gdb_stdout
);
691 stdio_file_write (file
, buf
, length_buf
);
694 /* This is the implementation of ui_file method to_fputs for stderr.
695 gdb_stdout is flushed before writing to gdb_stderr. */
698 stderr_file_fputs (const char *linebuffer
, struct ui_file
*file
)
700 gdb_flush (gdb_stdout
);
701 stdio_file_fputs (linebuffer
, file
);
706 stderr_fileopen (FILE *stream
)
708 struct ui_file
*ui_file
= stdio_fileopen (stream
);
711 /* There is no real line-buffering on Windows, see
712 http://msdn.microsoft.com/en-us/library/86cebhfs%28v=vs.71%29.aspx
713 so the stdout is either fully-buffered or non-buffered. We can't
714 make stdout non-buffered, because of two concerns,
715 1. non-buffering hurts performance,
716 2. non-buffering may change GDB's behavior when it is interacting
717 with front-end, such as Emacs.
719 We decided to leave stdout as fully buffered, but flush it first
720 when something is written to stderr. */
722 /* Method 'to_write_async_safe' is not overwritten, because there's
723 no way to flush a stream in an async-safe manner. Fortunately,
724 it doesn't really matter, because:
725 - that method is only used for printing internal debug output
726 from signal handlers.
727 - Windows hosts don't have a concept of async-safeness. Signal
728 handlers run in a separate thread, so they can call
729 the regular non-async-safe output routines freely. */
730 set_ui_file_write (ui_file
, stderr_file_write
);
731 set_ui_file_fputs (ui_file
, stderr_file_fputs
);
737 /* Like fdopen(). Create a ui_file from a previously opened FILE. */
740 stdio_fileopen (FILE *file
)
742 return stdio_file_new (file
, 0);
746 gdb_fopen (const char *name
, const char *mode
)
748 FILE *f
= gdb_fopen_cloexec (name
, mode
);
752 return stdio_file_new (f
, 1);
755 /* ``struct ui_file'' implementation that maps onto two ui-file objects. */
757 static ui_file_write_ftype tee_file_write
;
758 static ui_file_fputs_ftype tee_file_fputs
;
759 static ui_file_isatty_ftype tee_file_isatty
;
760 static ui_file_delete_ftype tee_file_delete
;
761 static ui_file_flush_ftype tee_file_flush
;
763 static int tee_file_magic
;
768 struct ui_file
*one
, *two
;
769 int close_one
, close_two
;
773 tee_file_new (struct ui_file
*one
, int close_one
,
774 struct ui_file
*two
, int close_two
)
776 struct ui_file
*ui_file
= ui_file_new ();
777 struct tee_file
*tee
= XNEW (struct tee_file
);
779 tee
->magic
= &tee_file_magic
;
782 tee
->close_one
= close_one
;
783 tee
->close_two
= close_two
;
784 set_ui_file_data (ui_file
, tee
, tee_file_delete
);
785 set_ui_file_flush (ui_file
, tee_file_flush
);
786 set_ui_file_write (ui_file
, tee_file_write
);
787 set_ui_file_fputs (ui_file
, tee_file_fputs
);
788 set_ui_file_isatty (ui_file
, tee_file_isatty
);
793 tee_file_delete (struct ui_file
*file
)
795 struct tee_file
*tee
= (struct tee_file
*) ui_file_data (file
);
797 if (tee
->magic
!= &tee_file_magic
)
798 internal_error (__FILE__
, __LINE__
,
799 _("tee_file_delete: bad magic number"));
801 ui_file_delete (tee
->one
);
803 ui_file_delete (tee
->two
);
809 tee_file_flush (struct ui_file
*file
)
811 struct tee_file
*tee
= (struct tee_file
*) ui_file_data (file
);
813 if (tee
->magic
!= &tee_file_magic
)
814 internal_error (__FILE__
, __LINE__
,
815 _("tee_file_flush: bad magic number"));
816 tee
->one
->to_flush (tee
->one
);
817 tee
->two
->to_flush (tee
->two
);
821 tee_file_write (struct ui_file
*file
, const char *buf
, long length_buf
)
823 struct tee_file
*tee
= (struct tee_file
*) ui_file_data (file
);
825 if (tee
->magic
!= &tee_file_magic
)
826 internal_error (__FILE__
, __LINE__
,
827 _("tee_file_write: bad magic number"));
828 ui_file_write (tee
->one
, buf
, length_buf
);
829 ui_file_write (tee
->two
, buf
, length_buf
);
833 tee_file_fputs (const char *linebuffer
, struct ui_file
*file
)
835 struct tee_file
*tee
= (struct tee_file
*) ui_file_data (file
);
837 if (tee
->magic
!= &tee_file_magic
)
838 internal_error (__FILE__
, __LINE__
,
839 _("tee_file_fputs: bad magic number"));
840 tee
->one
->to_fputs (linebuffer
, tee
->one
);
841 tee
->two
->to_fputs (linebuffer
, tee
->two
);
845 tee_file_isatty (struct ui_file
*file
)
847 struct tee_file
*tee
= (struct tee_file
*) ui_file_data (file
);
849 if (tee
->magic
!= &tee_file_magic
)
850 internal_error (__FILE__
, __LINE__
,
851 _("tee_file_isatty: bad magic number"));
853 return ui_file_isatty (tee
->one
);