1 /* UI_FILE - a generic STDIO like output stream.
3 Copyright (C) 1999-2002, 2007-2012 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;
43 ui_file_flush_ftype *to_flush;
44 ui_file_write_ftype *to_write;
45 ui_file_write_async_safe_ftype *to_write_async_safe;
46 ui_file_fputs_ftype *to_fputs;
47 ui_file_read_ftype *to_read;
48 ui_file_delete_ftype *to_delete;
49 ui_file_isatty_ftype *to_isatty;
50 ui_file_rewind_ftype *to_rewind;
51 ui_file_put_ftype *to_put;
59 struct ui_file *file = xmalloc (sizeof (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);
75 ui_file_delete (struct ui_file *file)
77 file->to_delete (file);
82 null_file_isatty (struct ui_file *file)
88 null_file_rewind (struct ui_file *file)
94 null_file_put (struct ui_file *file,
95 ui_file_put_method_ftype *write,
102 null_file_flush (struct ui_file *file)
108 null_file_write (struct ui_file *file,
112 if (file->to_fputs == null_file_fputs)
113 /* Both the write and fputs methods are null. Discard the
118 /* The fputs method isn't null, slowly pass the write request
119 onto that. FYI, this isn't as bad as it may look - the
120 current (as of 1999-11-07) printf_* function calls fputc and
121 fputc does exactly the below. By having a write function it
122 is possible to clean up that code. */
127 for (i = 0; i < sizeof_buf; i++)
130 file->to_fputs (b, file);
137 null_file_read (struct ui_file *file,
146 null_file_fputs (const char *buf, struct ui_file *file)
148 if (file->to_write == null_file_write)
149 /* Both the write and fputs methods are null. Discard the
154 /* The write method was implemented, use that. */
155 file->to_write (file, buf, strlen (buf));
160 null_file_write_async_safe (struct ui_file *file,
168 null_file_delete (struct ui_file *file)
174 ui_file_data (struct ui_file *file)
176 if (file->magic != &ui_file_magic)
177 internal_error (__FILE__, __LINE__,
178 _("ui_file_data: bad magic number"));
179 return file->to_data;
183 gdb_flush (struct ui_file *file)
185 file->to_flush (file);
189 ui_file_isatty (struct ui_file *file)
191 return file->to_isatty (file);
195 ui_file_rewind (struct ui_file *file)
197 file->to_rewind (file);
201 ui_file_put (struct ui_file *file,
202 ui_file_put_method_ftype *write,
205 file->to_put (file, write, dest);
209 ui_file_write (struct ui_file *file,
213 file->to_write (file, buf, length_buf);
217 ui_file_write_async_safe (struct ui_file *file,
221 file->to_write_async_safe (file, buf, length_buf);
225 ui_file_read (struct ui_file *file, char *buf, long length_buf)
227 return file->to_read (file, buf, length_buf);
231 fputs_unfiltered (const char *buf, struct ui_file *file)
233 file->to_fputs (buf, file);
237 set_ui_file_flush (struct ui_file *file, ui_file_flush_ftype *flush_ptr)
239 file->to_flush = flush_ptr;
243 set_ui_file_isatty (struct ui_file *file, ui_file_isatty_ftype *isatty_ptr)
245 file->to_isatty = isatty_ptr;
249 set_ui_file_rewind (struct ui_file *file, ui_file_rewind_ftype *rewind_ptr)
251 file->to_rewind = rewind_ptr;
255 set_ui_file_put (struct ui_file *file, ui_file_put_ftype *put_ptr)
257 file->to_put = put_ptr;
261 set_ui_file_write (struct ui_file *file,
262 ui_file_write_ftype *write_ptr)
264 file->to_write = write_ptr;
268 set_ui_file_write_async_safe (struct ui_file *file,
269 ui_file_write_async_safe_ftype *write_async_safe_ptr)
271 file->to_write_async_safe = write_async_safe_ptr;
275 set_ui_file_read (struct ui_file *file, ui_file_read_ftype *read_ptr)
277 file->to_read = read_ptr;
281 set_ui_file_fputs (struct ui_file *file, ui_file_fputs_ftype *fputs_ptr)
283 file->to_fputs = fputs_ptr;
287 set_ui_file_data (struct ui_file *file, void *data,
288 ui_file_delete_ftype *delete_ptr)
290 file->to_data = data;
291 file->to_delete = delete_ptr;
294 /* ui_file utility function for converting a ``struct ui_file'' into
297 struct accumulated_ui_file
304 do_ui_file_xstrdup (void *context, const char *buffer, long length)
306 struct accumulated_ui_file *acc = context;
308 if (acc->buffer == NULL)
309 acc->buffer = xmalloc (length + 1);
311 acc->buffer = xrealloc (acc->buffer, acc->length + length + 1);
312 memcpy (acc->buffer + acc->length, buffer, length);
313 acc->length += length;
314 acc->buffer[acc->length] = '\0';
318 ui_file_xstrdup (struct ui_file *file, long *length)
320 struct accumulated_ui_file acc;
324 ui_file_put (file, do_ui_file_xstrdup, &acc);
325 if (acc.buffer == NULL)
326 acc.buffer = xstrdup ("");
328 *length = acc.length;
333 do_ui_file_obsavestring (void *context, const char *buffer, long length)
335 struct obstack *obstack = (struct obstack *) context;
337 obstack_grow (obstack, buffer, length);
341 ui_file_obsavestring (struct ui_file *file, struct obstack *obstack,
344 ui_file_put (file, do_ui_file_obsavestring, obstack);
345 *length = obstack_object_size (obstack);
346 obstack_1grow (obstack, '\0');
347 return obstack_finish (obstack);
350 /* A pure memory based ``struct ui_file'' that can be used an output
351 buffer. The buffers accumulated contents are available via
362 static ui_file_rewind_ftype mem_file_rewind;
363 static ui_file_put_ftype mem_file_put;
364 static ui_file_write_ftype mem_file_write;
365 static ui_file_delete_ftype mem_file_delete;
366 static struct ui_file *mem_file_new (void);
367 static int mem_file_magic;
369 static struct ui_file *
372 struct mem_file *stream = XMALLOC (struct mem_file);
373 struct ui_file *file = ui_file_new ();
375 set_ui_file_data (file, stream, mem_file_delete);
376 set_ui_file_rewind (file, mem_file_rewind);
377 set_ui_file_put (file, mem_file_put);
378 set_ui_file_write (file, mem_file_write);
379 stream->magic = &mem_file_magic;
380 stream->buffer = NULL;
381 stream->sizeof_buffer = 0;
382 stream->length_buffer = 0;
387 mem_file_delete (struct ui_file *file)
389 struct mem_file *stream = ui_file_data (file);
391 if (stream->magic != &mem_file_magic)
392 internal_error (__FILE__, __LINE__,
393 _("mem_file_delete: bad magic number"));
394 if (stream->buffer != NULL)
395 xfree (stream->buffer);
402 return mem_file_new ();
406 mem_file_rewind (struct ui_file *file)
408 struct mem_file *stream = ui_file_data (file);
410 if (stream->magic != &mem_file_magic)
411 internal_error (__FILE__, __LINE__,
412 _("mem_file_rewind: bad magic number"));
413 stream->length_buffer = 0;
417 mem_file_put (struct ui_file *file,
418 ui_file_put_method_ftype *write,
421 struct mem_file *stream = ui_file_data (file);
423 if (stream->magic != &mem_file_magic)
424 internal_error (__FILE__, __LINE__,
425 _("mem_file_put: bad magic number"));
426 if (stream->length_buffer > 0)
427 write (dest, stream->buffer, stream->length_buffer);
431 mem_file_write (struct ui_file *file,
435 struct mem_file *stream = ui_file_data (file);
437 if (stream->magic != &mem_file_magic)
438 internal_error (__FILE__, __LINE__,
439 _("mem_file_write: bad magic number"));
440 if (stream->buffer == NULL)
442 stream->length_buffer = length_buffer;
443 stream->sizeof_buffer = length_buffer;
444 stream->buffer = xmalloc (stream->sizeof_buffer);
445 memcpy (stream->buffer, buffer, length_buffer);
449 int new_length = stream->length_buffer + length_buffer;
451 if (new_length >= stream->sizeof_buffer)
453 stream->sizeof_buffer = new_length;
454 stream->buffer = xrealloc (stream->buffer, stream->sizeof_buffer);
456 memcpy (stream->buffer + stream->length_buffer, buffer, length_buffer);
457 stream->length_buffer = new_length;
461 /* ``struct ui_file'' implementation that maps directly onto
464 static ui_file_write_ftype stdio_file_write;
465 static ui_file_write_async_safe_ftype stdio_file_write_async_safe;
466 static ui_file_fputs_ftype stdio_file_fputs;
467 static ui_file_read_ftype stdio_file_read;
468 static ui_file_isatty_ftype stdio_file_isatty;
469 static ui_file_delete_ftype stdio_file_delete;
470 static struct ui_file *stdio_file_new (FILE *file, int close_p);
471 static ui_file_flush_ftype stdio_file_flush;
473 static int stdio_file_magic;
479 /* The associated file descriptor is extracted ahead of time for
480 stdio_file_write_async_safe's benefit, in case fileno isn't async-safe. */
485 static struct ui_file *
486 stdio_file_new (FILE *file, int close_p)
488 struct ui_file *ui_file = ui_file_new ();
489 struct stdio_file *stdio = xmalloc (sizeof (struct stdio_file));
491 stdio->magic = &stdio_file_magic;
493 stdio->fd = fileno (file);
494 stdio->close_p = close_p;
495 set_ui_file_data (ui_file, stdio, stdio_file_delete);
496 set_ui_file_flush (ui_file, stdio_file_flush);
497 set_ui_file_write (ui_file, stdio_file_write);
498 set_ui_file_write_async_safe (ui_file, stdio_file_write_async_safe);
499 set_ui_file_fputs (ui_file, stdio_file_fputs);
500 set_ui_file_read (ui_file, stdio_file_read);
501 set_ui_file_isatty (ui_file, stdio_file_isatty);
506 stdio_file_delete (struct ui_file *file)
508 struct stdio_file *stdio = ui_file_data (file);
510 if (stdio->magic != &stdio_file_magic)
511 internal_error (__FILE__, __LINE__,
512 _("stdio_file_delete: bad magic number"));
515 fclose (stdio->file);
521 stdio_file_flush (struct ui_file *file)
523 struct stdio_file *stdio = ui_file_data (file);
525 if (stdio->magic != &stdio_file_magic)
526 internal_error (__FILE__, __LINE__,
527 _("stdio_file_flush: bad magic number"));
528 fflush (stdio->file);
532 stdio_file_read (struct ui_file *file, char *buf, long length_buf)
534 struct stdio_file *stdio = ui_file_data (file);
536 if (stdio->magic != &stdio_file_magic)
537 internal_error (__FILE__, __LINE__,
538 _("stdio_file_read: bad magic number"));
540 /* For the benefit of Windows, call gdb_select before reading from
541 the file. Wait until at least one byte of data is available.
542 Control-C can interrupt gdb_select, but not read. */
546 FD_SET (stdio->fd, &readfds);
547 if (gdb_select (stdio->fd + 1, &readfds, NULL, NULL, NULL) == -1)
551 return read (stdio->fd, buf, length_buf);
555 stdio_file_write (struct ui_file *file, const char *buf, long length_buf)
557 struct stdio_file *stdio = ui_file_data (file);
559 if (stdio->magic != &stdio_file_magic)
560 internal_error (__FILE__, __LINE__,
561 _("stdio_file_write: bad magic number"));
562 /* Calling error crashes when we are called from the exception framework. */
563 if (fwrite (buf, length_buf, 1, stdio->file))
570 stdio_file_write_async_safe (struct ui_file *file,
571 const char *buf, long length_buf)
573 struct stdio_file *stdio = ui_file_data (file);
575 if (stdio->magic != &stdio_file_magic)
577 /* gettext isn't necessarily async safe, so we can't use _("error message") here.
578 We could extract the correct translation ahead of time, but this is an extremely
579 rare event, and one of the other stdio_file_* routines will presumably catch
580 the problem anyway. For now keep it simple and ignore the error here. */
584 /* This is written the way it is to avoid a warning from gcc about not using the
585 result of write (since it can be declared with attribute warn_unused_result).
586 Alas casting to void doesn't work for this. */
587 if (write (stdio->fd, buf, length_buf))
594 stdio_file_fputs (const char *linebuffer, struct ui_file *file)
596 struct stdio_file *stdio = ui_file_data (file);
598 if (stdio->magic != &stdio_file_magic)
599 internal_error (__FILE__, __LINE__,
600 _("stdio_file_fputs: bad magic number"));
601 /* Calling error crashes when we are called from the exception framework. */
602 if (fputs (linebuffer, stdio->file))
609 stdio_file_isatty (struct ui_file *file)
611 struct stdio_file *stdio = ui_file_data (file);
613 if (stdio->magic != &stdio_file_magic)
614 internal_error (__FILE__, __LINE__,
615 _("stdio_file_isatty: bad magic number"));
616 return (isatty (stdio->fd));
619 /* Like fdopen(). Create a ui_file from a previously opened FILE. */
622 stdio_fileopen (FILE *file)
624 return stdio_file_new (file, 0);
628 gdb_fopen (char *name, char *mode)
630 FILE *f = fopen (name, mode);
634 return stdio_file_new (f, 1);
637 /* ``struct ui_file'' implementation that maps onto two ui-file objects. */
639 static ui_file_write_ftype tee_file_write;
640 static ui_file_fputs_ftype tee_file_fputs;
641 static ui_file_isatty_ftype tee_file_isatty;
642 static ui_file_delete_ftype tee_file_delete;
643 static ui_file_flush_ftype tee_file_flush;
645 static int tee_file_magic;
650 struct ui_file *one, *two;
651 int close_one, close_two;
655 tee_file_new (struct ui_file *one, int close_one,
656 struct ui_file *two, int close_two)
658 struct ui_file *ui_file = ui_file_new ();
659 struct tee_file *tee = xmalloc (sizeof (struct tee_file));
661 tee->magic = &tee_file_magic;
664 tee->close_one = close_one;
665 tee->close_two = close_two;
666 set_ui_file_data (ui_file, tee, tee_file_delete);
667 set_ui_file_flush (ui_file, tee_file_flush);
668 set_ui_file_write (ui_file, tee_file_write);
669 set_ui_file_fputs (ui_file, tee_file_fputs);
670 set_ui_file_isatty (ui_file, tee_file_isatty);
675 tee_file_delete (struct ui_file *file)
677 struct tee_file *tee = ui_file_data (file);
679 if (tee->magic != &tee_file_magic)
680 internal_error (__FILE__, __LINE__,
681 _("tee_file_delete: bad magic number"));
683 ui_file_delete (tee->one);
685 ui_file_delete (tee->two);
691 tee_file_flush (struct ui_file *file)
693 struct tee_file *tee = ui_file_data (file);
695 if (tee->magic != &tee_file_magic)
696 internal_error (__FILE__, __LINE__,
697 _("tee_file_flush: bad magic number"));
698 tee->one->to_flush (tee->one);
699 tee->two->to_flush (tee->two);
703 tee_file_write (struct ui_file *file, const char *buf, long length_buf)
705 struct tee_file *tee = ui_file_data (file);
707 if (tee->magic != &tee_file_magic)
708 internal_error (__FILE__, __LINE__,
709 _("tee_file_write: bad magic number"));
710 ui_file_write (tee->one, buf, length_buf);
711 ui_file_write (tee->two, buf, length_buf);
715 tee_file_fputs (const char *linebuffer, struct ui_file *file)
717 struct tee_file *tee = ui_file_data (file);
719 if (tee->magic != &tee_file_magic)
720 internal_error (__FILE__, __LINE__,
721 _("tee_file_fputs: bad magic number"));
722 tee->one->to_fputs (linebuffer, tee->one);
723 tee->two->to_fputs (linebuffer, tee->two);
727 tee_file_isatty (struct ui_file *file)
729 struct tee_file *tee = ui_file_data (file);
731 if (tee->magic != &tee_file_magic)
732 internal_error (__FILE__, __LINE__,
733 _("tee_file_isatty: bad magic number"));
735 return ui_file_isatty (tee->one);