1 /* UI_FILE - a generic STDIO like output stream.
3 Copyright (C) 1999-2013 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_string.h"
26 #include "gdb_select.h"
30 static ui_file_isatty_ftype null_file_isatty
;
31 static ui_file_write_ftype null_file_write
;
32 static ui_file_write_ftype null_file_write_async_safe
;
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
;
39 static ui_file_fseek_ftype null_file_fseek
;
44 ui_file_flush_ftype
*to_flush
;
45 ui_file_write_ftype
*to_write
;
46 ui_file_write_async_safe_ftype
*to_write_async_safe
;
47 ui_file_fputs_ftype
*to_fputs
;
48 ui_file_read_ftype
*to_read
;
49 ui_file_delete_ftype
*to_delete
;
50 ui_file_isatty_ftype
*to_isatty
;
51 ui_file_rewind_ftype
*to_rewind
;
52 ui_file_put_ftype
*to_put
;
53 ui_file_fseek_ftype
*to_fseek
;
61 struct ui_file
*file
= xmalloc (sizeof (struct ui_file
));
63 file
->magic
= &ui_file_magic
;
64 set_ui_file_data (file
, NULL
, null_file_delete
);
65 set_ui_file_flush (file
, null_file_flush
);
66 set_ui_file_write (file
, null_file_write
);
67 set_ui_file_write_async_safe (file
, null_file_write_async_safe
);
68 set_ui_file_fputs (file
, null_file_fputs
);
69 set_ui_file_read (file
, null_file_read
);
70 set_ui_file_isatty (file
, null_file_isatty
);
71 set_ui_file_rewind (file
, null_file_rewind
);
72 set_ui_file_put (file
, null_file_put
);
73 set_ui_file_fseek (file
, null_file_fseek
);
78 ui_file_delete (struct ui_file
*file
)
80 file
->to_delete (file
);
85 null_file_isatty (struct ui_file
*file
)
91 null_file_rewind (struct ui_file
*file
)
97 null_file_put (struct ui_file
*file
,
98 ui_file_put_method_ftype
*write
,
105 null_file_flush (struct ui_file
*file
)
111 null_file_write (struct ui_file
*file
,
115 if (file
->to_fputs
== null_file_fputs
)
116 /* Both the write and fputs methods are null. Discard the
121 /* The fputs method isn't null, slowly pass the write request
122 onto that. FYI, this isn't as bad as it may look - the
123 current (as of 1999-11-07) printf_* function calls fputc and
124 fputc does exactly the below. By having a write function it
125 is possible to clean up that code. */
130 for (i
= 0; i
< sizeof_buf
; i
++)
133 file
->to_fputs (b
, file
);
140 null_file_read (struct ui_file
*file
,
149 null_file_fputs (const char *buf
, struct ui_file
*file
)
151 if (file
->to_write
== null_file_write
)
152 /* Both the write and fputs methods are null. Discard the
157 /* The write method was implemented, use that. */
158 file
->to_write (file
, buf
, strlen (buf
));
163 null_file_write_async_safe (struct ui_file
*file
,
171 null_file_delete (struct ui_file
*file
)
177 null_file_fseek (struct ui_file
*stream
, long offset
, int whence
)
185 ui_file_data (struct ui_file
*file
)
187 if (file
->magic
!= &ui_file_magic
)
188 internal_error (__FILE__
, __LINE__
,
189 _("ui_file_data: bad magic number"));
190 return file
->to_data
;
194 gdb_flush (struct ui_file
*file
)
196 file
->to_flush (file
);
200 ui_file_isatty (struct ui_file
*file
)
202 return file
->to_isatty (file
);
206 ui_file_rewind (struct ui_file
*file
)
208 file
->to_rewind (file
);
212 ui_file_put (struct ui_file
*file
,
213 ui_file_put_method_ftype
*write
,
216 file
->to_put (file
, write
, dest
);
220 ui_file_write (struct ui_file
*file
,
224 file
->to_write (file
, buf
, length_buf
);
228 ui_file_write_async_safe (struct ui_file
*file
,
232 file
->to_write_async_safe (file
, buf
, length_buf
);
236 ui_file_read (struct ui_file
*file
, char *buf
, long length_buf
)
238 return file
->to_read (file
, buf
, length_buf
);
242 ui_file_fseek (struct ui_file
*file
, long offset
, int whence
)
244 return file
->to_fseek (file
, offset
, whence
);
248 fputs_unfiltered (const char *buf
, struct ui_file
*file
)
250 file
->to_fputs (buf
, file
);
254 set_ui_file_flush (struct ui_file
*file
, ui_file_flush_ftype
*flush_ptr
)
256 file
->to_flush
= flush_ptr
;
260 set_ui_file_isatty (struct ui_file
*file
, ui_file_isatty_ftype
*isatty_ptr
)
262 file
->to_isatty
= isatty_ptr
;
266 set_ui_file_rewind (struct ui_file
*file
, ui_file_rewind_ftype
*rewind_ptr
)
268 file
->to_rewind
= rewind_ptr
;
272 set_ui_file_put (struct ui_file
*file
, ui_file_put_ftype
*put_ptr
)
274 file
->to_put
= put_ptr
;
278 set_ui_file_write (struct ui_file
*file
,
279 ui_file_write_ftype
*write_ptr
)
281 file
->to_write
= write_ptr
;
285 set_ui_file_write_async_safe (struct ui_file
*file
,
286 ui_file_write_async_safe_ftype
*write_async_safe_ptr
)
288 file
->to_write_async_safe
= write_async_safe_ptr
;
292 set_ui_file_read (struct ui_file
*file
, ui_file_read_ftype
*read_ptr
)
294 file
->to_read
= read_ptr
;
298 set_ui_file_fputs (struct ui_file
*file
, ui_file_fputs_ftype
*fputs_ptr
)
300 file
->to_fputs
= fputs_ptr
;
304 set_ui_file_fseek (struct ui_file
*file
, ui_file_fseek_ftype
*fseek_ptr
)
306 file
->to_fseek
= fseek_ptr
;
310 set_ui_file_data (struct ui_file
*file
, void *data
,
311 ui_file_delete_ftype
*delete_ptr
)
313 file
->to_data
= data
;
314 file
->to_delete
= delete_ptr
;
317 /* ui_file utility function for converting a ``struct ui_file'' into
320 struct accumulated_ui_file
327 do_ui_file_xstrdup (void *context
, const char *buffer
, long length
)
329 struct accumulated_ui_file
*acc
= context
;
331 if (acc
->buffer
== NULL
)
332 acc
->buffer
= xmalloc (length
+ 1);
334 acc
->buffer
= xrealloc (acc
->buffer
, acc
->length
+ length
+ 1);
335 memcpy (acc
->buffer
+ acc
->length
, buffer
, length
);
336 acc
->length
+= length
;
337 acc
->buffer
[acc
->length
] = '\0';
341 ui_file_xstrdup (struct ui_file
*file
, long *length
)
343 struct accumulated_ui_file acc
;
347 ui_file_put (file
, do_ui_file_xstrdup
, &acc
);
348 if (acc
.buffer
== NULL
)
349 acc
.buffer
= xstrdup ("");
351 *length
= acc
.length
;
356 do_ui_file_obsavestring (void *context
, const char *buffer
, long length
)
358 struct obstack
*obstack
= (struct obstack
*) context
;
360 obstack_grow (obstack
, buffer
, length
);
364 ui_file_obsavestring (struct ui_file
*file
, struct obstack
*obstack
,
367 ui_file_put (file
, do_ui_file_obsavestring
, obstack
);
368 *length
= obstack_object_size (obstack
);
369 obstack_1grow (obstack
, '\0');
370 return obstack_finish (obstack
);
373 /* A pure memory based ``struct ui_file'' that can be used an output
374 buffer. The buffers accumulated contents are available via
385 static ui_file_rewind_ftype mem_file_rewind
;
386 static ui_file_put_ftype mem_file_put
;
387 static ui_file_write_ftype mem_file_write
;
388 static ui_file_delete_ftype mem_file_delete
;
389 static struct ui_file
*mem_file_new (void);
390 static int mem_file_magic
;
392 static struct ui_file
*
395 struct mem_file
*stream
= XMALLOC (struct mem_file
);
396 struct ui_file
*file
= ui_file_new ();
398 set_ui_file_data (file
, stream
, mem_file_delete
);
399 set_ui_file_rewind (file
, mem_file_rewind
);
400 set_ui_file_put (file
, mem_file_put
);
401 set_ui_file_write (file
, mem_file_write
);
402 stream
->magic
= &mem_file_magic
;
403 stream
->buffer
= NULL
;
404 stream
->sizeof_buffer
= 0;
405 stream
->length_buffer
= 0;
410 mem_file_delete (struct ui_file
*file
)
412 struct mem_file
*stream
= ui_file_data (file
);
414 if (stream
->magic
!= &mem_file_magic
)
415 internal_error (__FILE__
, __LINE__
,
416 _("mem_file_delete: bad magic number"));
417 if (stream
->buffer
!= NULL
)
418 xfree (stream
->buffer
);
425 return mem_file_new ();
429 mem_file_rewind (struct ui_file
*file
)
431 struct mem_file
*stream
= ui_file_data (file
);
433 if (stream
->magic
!= &mem_file_magic
)
434 internal_error (__FILE__
, __LINE__
,
435 _("mem_file_rewind: bad magic number"));
436 stream
->length_buffer
= 0;
440 mem_file_put (struct ui_file
*file
,
441 ui_file_put_method_ftype
*write
,
444 struct mem_file
*stream
= ui_file_data (file
);
446 if (stream
->magic
!= &mem_file_magic
)
447 internal_error (__FILE__
, __LINE__
,
448 _("mem_file_put: bad magic number"));
449 if (stream
->length_buffer
> 0)
450 write (dest
, stream
->buffer
, stream
->length_buffer
);
454 mem_file_write (struct ui_file
*file
,
458 struct mem_file
*stream
= ui_file_data (file
);
460 if (stream
->magic
!= &mem_file_magic
)
461 internal_error (__FILE__
, __LINE__
,
462 _("mem_file_write: bad magic number"));
463 if (stream
->buffer
== NULL
)
465 stream
->length_buffer
= length_buffer
;
466 stream
->sizeof_buffer
= length_buffer
;
467 stream
->buffer
= xmalloc (stream
->sizeof_buffer
);
468 memcpy (stream
->buffer
, buffer
, length_buffer
);
472 int new_length
= stream
->length_buffer
+ length_buffer
;
474 if (new_length
>= stream
->sizeof_buffer
)
476 stream
->sizeof_buffer
= new_length
;
477 stream
->buffer
= xrealloc (stream
->buffer
, stream
->sizeof_buffer
);
479 memcpy (stream
->buffer
+ stream
->length_buffer
, buffer
, length_buffer
);
480 stream
->length_buffer
= new_length
;
484 /* ``struct ui_file'' implementation that maps directly onto
487 static ui_file_write_ftype stdio_file_write
;
488 static ui_file_write_async_safe_ftype stdio_file_write_async_safe
;
489 static ui_file_fputs_ftype stdio_file_fputs
;
490 static ui_file_read_ftype stdio_file_read
;
491 static ui_file_isatty_ftype stdio_file_isatty
;
492 static ui_file_delete_ftype stdio_file_delete
;
493 static struct ui_file
*stdio_file_new (FILE *file
, int close_p
);
494 static ui_file_flush_ftype stdio_file_flush
;
495 static ui_file_fseek_ftype stdio_file_fseek
;
497 static int stdio_file_magic
;
503 /* The associated file descriptor is extracted ahead of time for
504 stdio_file_write_async_safe's benefit, in case fileno isn't async-safe. */
509 static struct ui_file
*
510 stdio_file_new (FILE *file
, int close_p
)
512 struct ui_file
*ui_file
= ui_file_new ();
513 struct stdio_file
*stdio
= xmalloc (sizeof (struct stdio_file
));
515 stdio
->magic
= &stdio_file_magic
;
517 stdio
->fd
= fileno (file
);
518 stdio
->close_p
= close_p
;
519 set_ui_file_data (ui_file
, stdio
, stdio_file_delete
);
520 set_ui_file_flush (ui_file
, stdio_file_flush
);
521 set_ui_file_write (ui_file
, stdio_file_write
);
522 set_ui_file_write_async_safe (ui_file
, stdio_file_write_async_safe
);
523 set_ui_file_fputs (ui_file
, stdio_file_fputs
);
524 set_ui_file_read (ui_file
, stdio_file_read
);
525 set_ui_file_isatty (ui_file
, stdio_file_isatty
);
526 set_ui_file_fseek (ui_file
, stdio_file_fseek
);
531 stdio_file_delete (struct ui_file
*file
)
533 struct stdio_file
*stdio
= ui_file_data (file
);
535 if (stdio
->magic
!= &stdio_file_magic
)
536 internal_error (__FILE__
, __LINE__
,
537 _("stdio_file_delete: bad magic number"));
540 fclose (stdio
->file
);
546 stdio_file_flush (struct ui_file
*file
)
548 struct stdio_file
*stdio
= ui_file_data (file
);
550 if (stdio
->magic
!= &stdio_file_magic
)
551 internal_error (__FILE__
, __LINE__
,
552 _("stdio_file_flush: bad magic number"));
553 fflush (stdio
->file
);
557 stdio_file_read (struct ui_file
*file
, char *buf
, long length_buf
)
559 struct stdio_file
*stdio
= ui_file_data (file
);
561 if (stdio
->magic
!= &stdio_file_magic
)
562 internal_error (__FILE__
, __LINE__
,
563 _("stdio_file_read: bad magic number"));
565 /* For the benefit of Windows, call gdb_select before reading from
566 the file. Wait until at least one byte of data is available.
567 Control-C can interrupt gdb_select, but not read. */
571 FD_SET (stdio
->fd
, &readfds
);
572 if (gdb_select (stdio
->fd
+ 1, &readfds
, NULL
, NULL
, NULL
) == -1)
576 return read (stdio
->fd
, buf
, length_buf
);
580 stdio_file_write (struct ui_file
*file
, const char *buf
, long length_buf
)
582 struct stdio_file
*stdio
= ui_file_data (file
);
584 if (stdio
->magic
!= &stdio_file_magic
)
585 internal_error (__FILE__
, __LINE__
,
586 _("stdio_file_write: bad magic number"));
587 /* Calling error crashes when we are called from the exception framework. */
588 if (fwrite (buf
, length_buf
, 1, stdio
->file
))
595 stdio_file_write_async_safe (struct ui_file
*file
,
596 const char *buf
, long length_buf
)
598 struct stdio_file
*stdio
= ui_file_data (file
);
600 if (stdio
->magic
!= &stdio_file_magic
)
602 /* gettext isn't necessarily async safe, so we can't use _("error message") here.
603 We could extract the correct translation ahead of time, but this is an extremely
604 rare event, and one of the other stdio_file_* routines will presumably catch
605 the problem anyway. For now keep it simple and ignore the error here. */
609 /* This is written the way it is to avoid a warning from gcc about not using the
610 result of write (since it can be declared with attribute warn_unused_result).
611 Alas casting to void doesn't work for this. */
612 if (write (stdio
->fd
, buf
, length_buf
))
619 stdio_file_fputs (const char *linebuffer
, struct ui_file
*file
)
621 struct stdio_file
*stdio
= ui_file_data (file
);
623 if (stdio
->magic
!= &stdio_file_magic
)
624 internal_error (__FILE__
, __LINE__
,
625 _("stdio_file_fputs: bad magic number"));
626 /* Calling error crashes when we are called from the exception framework. */
627 if (fputs (linebuffer
, stdio
->file
))
634 stdio_file_isatty (struct ui_file
*file
)
636 struct stdio_file
*stdio
= ui_file_data (file
);
638 if (stdio
->magic
!= &stdio_file_magic
)
639 internal_error (__FILE__
, __LINE__
,
640 _("stdio_file_isatty: bad magic number"));
641 return (isatty (stdio
->fd
));
645 stdio_file_fseek (struct ui_file
*file
, long offset
, int whence
)
647 struct stdio_file
*stdio
= ui_file_data (file
);
649 if (stdio
->magic
!= &stdio_file_magic
)
650 internal_error (__FILE__
, __LINE__
,
651 _("stdio_file_fseek: bad magic number"));
653 return fseek (stdio
->file
, offset
, whence
);
656 /* Like fdopen(). Create a ui_file from a previously opened FILE. */
659 stdio_fileopen (FILE *file
)
661 return stdio_file_new (file
, 0);
665 gdb_fopen (char *name
, char *mode
)
667 FILE *f
= fopen (name
, mode
);
671 return stdio_file_new (f
, 1);
674 /* ``struct ui_file'' implementation that maps onto two ui-file objects. */
676 static ui_file_write_ftype tee_file_write
;
677 static ui_file_fputs_ftype tee_file_fputs
;
678 static ui_file_isatty_ftype tee_file_isatty
;
679 static ui_file_delete_ftype tee_file_delete
;
680 static ui_file_flush_ftype tee_file_flush
;
682 static int tee_file_magic
;
687 struct ui_file
*one
, *two
;
688 int close_one
, close_two
;
692 tee_file_new (struct ui_file
*one
, int close_one
,
693 struct ui_file
*two
, int close_two
)
695 struct ui_file
*ui_file
= ui_file_new ();
696 struct tee_file
*tee
= xmalloc (sizeof (struct tee_file
));
698 tee
->magic
= &tee_file_magic
;
701 tee
->close_one
= close_one
;
702 tee
->close_two
= close_two
;
703 set_ui_file_data (ui_file
, tee
, tee_file_delete
);
704 set_ui_file_flush (ui_file
, tee_file_flush
);
705 set_ui_file_write (ui_file
, tee_file_write
);
706 set_ui_file_fputs (ui_file
, tee_file_fputs
);
707 set_ui_file_isatty (ui_file
, tee_file_isatty
);
712 tee_file_delete (struct ui_file
*file
)
714 struct tee_file
*tee
= ui_file_data (file
);
716 if (tee
->magic
!= &tee_file_magic
)
717 internal_error (__FILE__
, __LINE__
,
718 _("tee_file_delete: bad magic number"));
720 ui_file_delete (tee
->one
);
722 ui_file_delete (tee
->two
);
728 tee_file_flush (struct ui_file
*file
)
730 struct tee_file
*tee
= ui_file_data (file
);
732 if (tee
->magic
!= &tee_file_magic
)
733 internal_error (__FILE__
, __LINE__
,
734 _("tee_file_flush: bad magic number"));
735 tee
->one
->to_flush (tee
->one
);
736 tee
->two
->to_flush (tee
->two
);
740 tee_file_write (struct ui_file
*file
, const char *buf
, long length_buf
)
742 struct tee_file
*tee
= ui_file_data (file
);
744 if (tee
->magic
!= &tee_file_magic
)
745 internal_error (__FILE__
, __LINE__
,
746 _("tee_file_write: bad magic number"));
747 ui_file_write (tee
->one
, buf
, length_buf
);
748 ui_file_write (tee
->two
, buf
, length_buf
);
752 tee_file_fputs (const char *linebuffer
, struct ui_file
*file
)
754 struct tee_file
*tee
= ui_file_data (file
);
756 if (tee
->magic
!= &tee_file_magic
)
757 internal_error (__FILE__
, __LINE__
,
758 _("tee_file_fputs: bad magic number"));
759 tee
->one
->to_fputs (linebuffer
, tee
->one
);
760 tee
->two
->to_fputs (linebuffer
, tee
->two
);
764 tee_file_isatty (struct ui_file
*file
)
766 struct tee_file
*tee
= ui_file_data (file
);
768 if (tee
->magic
!= &tee_file_magic
)
769 internal_error (__FILE__
, __LINE__
,
770 _("tee_file_isatty: bad magic number"));
772 return ui_file_isatty (tee
->one
);