1 /* UI_FILE - a generic STDIO like output stream.
3 Copyright (C) 1999, 2000, 2001, 2002, 2007, 2008, 2009, 2010, 2011
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_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;
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;
60 struct ui_file *file = xmalloc (sizeof (struct ui_file));
62 file->magic = &ui_file_magic;
63 set_ui_file_data (file, NULL, null_file_delete);
64 set_ui_file_flush (file, null_file_flush);
65 set_ui_file_write (file, null_file_write);
66 set_ui_file_write_async_safe (file, null_file_write_async_safe);
67 set_ui_file_fputs (file, null_file_fputs);
68 set_ui_file_read (file, null_file_read);
69 set_ui_file_isatty (file, null_file_isatty);
70 set_ui_file_rewind (file, null_file_rewind);
71 set_ui_file_put (file, null_file_put);
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 ui_file_data (struct ui_file *file)
177 if (file->magic != &ui_file_magic)
178 internal_error (__FILE__, __LINE__,
179 _("ui_file_data: bad magic number"));
180 return file->to_data;
184 gdb_flush (struct ui_file *file)
186 file->to_flush (file);
190 ui_file_isatty (struct ui_file *file)
192 return file->to_isatty (file);
196 ui_file_rewind (struct ui_file *file)
198 file->to_rewind (file);
202 ui_file_put (struct ui_file *file,
203 ui_file_put_method_ftype *write,
206 file->to_put (file, write, dest);
210 ui_file_write (struct ui_file *file,
214 file->to_write (file, buf, length_buf);
218 ui_file_write_async_safe (struct ui_file *file,
222 file->to_write_async_safe (file, buf, length_buf);
226 ui_file_read (struct ui_file *file, char *buf, long length_buf)
228 return file->to_read (file, buf, length_buf);
232 fputs_unfiltered (const char *buf, struct ui_file *file)
234 file->to_fputs (buf, file);
238 set_ui_file_flush (struct ui_file *file, ui_file_flush_ftype *flush)
240 file->to_flush = flush;
244 set_ui_file_isatty (struct ui_file *file, ui_file_isatty_ftype *isatty)
246 file->to_isatty = isatty;
250 set_ui_file_rewind (struct ui_file *file, ui_file_rewind_ftype *rewind)
252 file->to_rewind = rewind;
256 set_ui_file_put (struct ui_file *file, ui_file_put_ftype *put)
262 set_ui_file_write (struct ui_file *file,
263 ui_file_write_ftype *write)
265 file->to_write = write;
269 set_ui_file_write_async_safe (struct ui_file *file,
270 ui_file_write_async_safe_ftype *write_async_safe)
272 file->to_write_async_safe = write_async_safe;
276 set_ui_file_read (struct ui_file *file, ui_file_read_ftype *read)
278 file->to_read = read;
282 set_ui_file_fputs (struct ui_file *file, ui_file_fputs_ftype *fputs)
284 file->to_fputs = fputs;
288 set_ui_file_data (struct ui_file *file, void *data,
289 ui_file_delete_ftype *delete)
291 file->to_data = data;
292 file->to_delete = delete;
295 /* ui_file utility function for converting a ``struct ui_file'' into
298 struct accumulated_ui_file
305 do_ui_file_xstrdup (void *context, const char *buffer, long length)
307 struct accumulated_ui_file *acc = context;
309 if (acc->buffer == NULL)
310 acc->buffer = xmalloc (length + 1);
312 acc->buffer = xrealloc (acc->buffer, acc->length + length + 1);
313 memcpy (acc->buffer + acc->length, buffer, length);
314 acc->length += length;
315 acc->buffer[acc->length] = '\0';
319 ui_file_xstrdup (struct ui_file *file, long *length)
321 struct accumulated_ui_file acc;
325 ui_file_put (file, do_ui_file_xstrdup, &acc);
326 if (acc.buffer == NULL)
327 acc.buffer = xstrdup ("");
329 *length = acc.length;
334 do_ui_file_obsavestring (void *context, const char *buffer, long length)
336 struct obstack *obstack = (struct obstack *) context;
338 obstack_grow (obstack, buffer, length);
342 ui_file_obsavestring (struct ui_file *file, struct obstack *obstack,
345 ui_file_put (file, do_ui_file_obsavestring, obstack);
346 *length = obstack_object_size (obstack);
347 obstack_1grow (obstack, '\0');
348 return obstack_finish (obstack);
351 /* A pure memory based ``struct ui_file'' that can be used an output
352 buffer. The buffers accumulated contents are available via
363 static ui_file_rewind_ftype mem_file_rewind;
364 static ui_file_put_ftype mem_file_put;
365 static ui_file_write_ftype mem_file_write;
366 static ui_file_delete_ftype mem_file_delete;
367 static struct ui_file *mem_file_new (void);
368 static int mem_file_magic;
370 static struct ui_file *
373 struct mem_file *stream = XMALLOC (struct mem_file);
374 struct ui_file *file = ui_file_new ();
376 set_ui_file_data (file, stream, mem_file_delete);
377 set_ui_file_rewind (file, mem_file_rewind);
378 set_ui_file_put (file, mem_file_put);
379 set_ui_file_write (file, mem_file_write);
380 stream->magic = &mem_file_magic;
381 stream->buffer = NULL;
382 stream->sizeof_buffer = 0;
383 stream->length_buffer = 0;
388 mem_file_delete (struct ui_file *file)
390 struct mem_file *stream = ui_file_data (file);
392 if (stream->magic != &mem_file_magic)
393 internal_error (__FILE__, __LINE__,
394 _("mem_file_delete: bad magic number"));
395 if (stream->buffer != NULL)
396 xfree (stream->buffer);
403 return mem_file_new ();
407 mem_file_rewind (struct ui_file *file)
409 struct mem_file *stream = ui_file_data (file);
411 if (stream->magic != &mem_file_magic)
412 internal_error (__FILE__, __LINE__,
413 _("mem_file_rewind: bad magic number"));
414 stream->length_buffer = 0;
418 mem_file_put (struct ui_file *file,
419 ui_file_put_method_ftype *write,
422 struct mem_file *stream = ui_file_data (file);
424 if (stream->magic != &mem_file_magic)
425 internal_error (__FILE__, __LINE__,
426 _("mem_file_put: bad magic number"));
427 if (stream->length_buffer > 0)
428 write (dest, stream->buffer, stream->length_buffer);
432 mem_file_write (struct ui_file *file,
436 struct mem_file *stream = ui_file_data (file);
438 if (stream->magic != &mem_file_magic)
439 internal_error (__FILE__, __LINE__,
440 _("mem_file_write: bad magic number"));
441 if (stream->buffer == NULL)
443 stream->length_buffer = length_buffer;
444 stream->sizeof_buffer = length_buffer;
445 stream->buffer = xmalloc (stream->sizeof_buffer);
446 memcpy (stream->buffer, buffer, length_buffer);
450 int new_length = stream->length_buffer + length_buffer;
452 if (new_length >= stream->sizeof_buffer)
454 stream->sizeof_buffer = new_length;
455 stream->buffer = xrealloc (stream->buffer, stream->sizeof_buffer);
457 memcpy (stream->buffer + stream->length_buffer, buffer, length_buffer);
458 stream->length_buffer = new_length;
462 /* ``struct ui_file'' implementation that maps directly onto
465 static ui_file_write_ftype stdio_file_write;
466 static ui_file_write_async_safe_ftype stdio_file_write_async_safe;
467 static ui_file_fputs_ftype stdio_file_fputs;
468 static ui_file_read_ftype stdio_file_read;
469 static ui_file_isatty_ftype stdio_file_isatty;
470 static ui_file_delete_ftype stdio_file_delete;
471 static struct ui_file *stdio_file_new (FILE *file, int close_p);
472 static ui_file_flush_ftype stdio_file_flush;
474 static int stdio_file_magic;
480 /* The associated file descriptor is extracted ahead of time for
481 stdio_file_write_async_safe's benefit, in case fileno isn't async-safe. */
486 static struct ui_file *
487 stdio_file_new (FILE *file, int close_p)
489 struct ui_file *ui_file = ui_file_new ();
490 struct stdio_file *stdio = xmalloc (sizeof (struct stdio_file));
492 stdio->magic = &stdio_file_magic;
494 stdio->fd = fileno (file);
495 stdio->close_p = close_p;
496 set_ui_file_data (ui_file, stdio, stdio_file_delete);
497 set_ui_file_flush (ui_file, stdio_file_flush);
498 set_ui_file_write (ui_file, stdio_file_write);
499 set_ui_file_write_async_safe (ui_file, stdio_file_write_async_safe);
500 set_ui_file_fputs (ui_file, stdio_file_fputs);
501 set_ui_file_read (ui_file, stdio_file_read);
502 set_ui_file_isatty (ui_file, stdio_file_isatty);
507 stdio_file_delete (struct ui_file *file)
509 struct stdio_file *stdio = ui_file_data (file);
511 if (stdio->magic != &stdio_file_magic)
512 internal_error (__FILE__, __LINE__,
513 _("stdio_file_delete: bad magic number"));
516 fclose (stdio->file);
522 stdio_file_flush (struct ui_file *file)
524 struct stdio_file *stdio = ui_file_data (file);
526 if (stdio->magic != &stdio_file_magic)
527 internal_error (__FILE__, __LINE__,
528 _("stdio_file_flush: bad magic number"));
529 fflush (stdio->file);
533 stdio_file_read (struct ui_file *file, char *buf, long length_buf)
535 struct stdio_file *stdio = ui_file_data (file);
537 if (stdio->magic != &stdio_file_magic)
538 internal_error (__FILE__, __LINE__,
539 _("stdio_file_read: bad magic number"));
541 /* For the benefit of Windows, call gdb_select before reading from
542 the file. Wait until at least one byte of data is available.
543 Control-C can interrupt gdb_select, but not read. */
547 FD_SET (stdio->fd, &readfds);
548 if (gdb_select (stdio->fd + 1, &readfds, NULL, NULL, NULL) == -1)
552 return read (stdio->fd, buf, length_buf);
556 stdio_file_write (struct ui_file *file, const char *buf, long length_buf)
558 struct stdio_file *stdio = ui_file_data (file);
560 if (stdio->magic != &stdio_file_magic)
561 internal_error (__FILE__, __LINE__,
562 _("stdio_file_write: bad magic number"));
563 /* Calling error crashes when we are called from the exception framework. */
564 if (fwrite (buf, length_buf, 1, stdio->file))
569 stdio_file_write_async_safe (struct ui_file *file,
570 const char *buf, long length_buf)
572 struct stdio_file *stdio = ui_file_data (file);
574 if (stdio->magic != &stdio_file_magic)
576 /* gettext isn't necessarily async safe, so we can't use _("error message") here.
577 We could extract the correct translation ahead of time, but this is an extremely
578 rare event, and one of the other stdio_file_* routines will presumably catch
579 the problem anyway. For now keep it simple and ignore the error here. */
583 /* This is written the way it is to avoid a warning from gcc about not using the
584 result of write (since it can be declared with attribute warn_unused_result).
585 Alas casting to void doesn't work for this. */
586 if (write (stdio->fd, buf, length_buf))
591 stdio_file_fputs (const char *linebuffer, struct ui_file *file)
593 struct stdio_file *stdio = ui_file_data (file);
595 if (stdio->magic != &stdio_file_magic)
596 internal_error (__FILE__, __LINE__,
597 _("stdio_file_fputs: bad magic number"));
598 /* Calling error crashes when we are called from the exception framework. */
599 if (fputs (linebuffer, stdio->file))
604 stdio_file_isatty (struct ui_file *file)
606 struct stdio_file *stdio = ui_file_data (file);
608 if (stdio->magic != &stdio_file_magic)
609 internal_error (__FILE__, __LINE__,
610 _("stdio_file_isatty: bad magic number"));
611 return (isatty (stdio->fd));
614 /* Like fdopen(). Create a ui_file from a previously opened FILE. */
617 stdio_fileopen (FILE *file)
619 return stdio_file_new (file, 0);
623 gdb_fopen (char *name, char *mode)
625 FILE *f = fopen (name, mode);
629 return stdio_file_new (f, 1);
632 /* ``struct ui_file'' implementation that maps onto two ui-file objects. */
634 static ui_file_write_ftype tee_file_write;
635 static ui_file_fputs_ftype tee_file_fputs;
636 static ui_file_isatty_ftype tee_file_isatty;
637 static ui_file_delete_ftype tee_file_delete;
638 static ui_file_flush_ftype tee_file_flush;
640 static int tee_file_magic;
645 struct ui_file *one, *two;
646 int close_one, close_two;
650 tee_file_new (struct ui_file *one, int close_one,
651 struct ui_file *two, int close_two)
653 struct ui_file *ui_file = ui_file_new ();
654 struct tee_file *tee = xmalloc (sizeof (struct tee_file));
656 tee->magic = &tee_file_magic;
659 tee->close_one = close_one;
660 tee->close_two = close_two;
661 set_ui_file_data (ui_file, tee, tee_file_delete);
662 set_ui_file_flush (ui_file, tee_file_flush);
663 set_ui_file_write (ui_file, tee_file_write);
664 set_ui_file_fputs (ui_file, tee_file_fputs);
665 set_ui_file_isatty (ui_file, tee_file_isatty);
670 tee_file_delete (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_delete: bad magic number"));
678 ui_file_delete (tee->one);
680 ui_file_delete (tee->two);
686 tee_file_flush (struct ui_file *file)
688 struct tee_file *tee = ui_file_data (file);
690 if (tee->magic != &tee_file_magic)
691 internal_error (__FILE__, __LINE__,
692 _("tee_file_flush: bad magic number"));
693 tee->one->to_flush (tee->one);
694 tee->two->to_flush (tee->two);
698 tee_file_write (struct ui_file *file, const char *buf, long length_buf)
700 struct tee_file *tee = ui_file_data (file);
702 if (tee->magic != &tee_file_magic)
703 internal_error (__FILE__, __LINE__,
704 _("tee_file_write: bad magic number"));
705 ui_file_write (tee->one, buf, length_buf);
706 ui_file_write (tee->two, buf, length_buf);
710 tee_file_fputs (const char *linebuffer, struct ui_file *file)
712 struct tee_file *tee = ui_file_data (file);
714 if (tee->magic != &tee_file_magic)
715 internal_error (__FILE__, __LINE__,
716 _("tee_file_fputs: bad magic number"));
717 tee->one->to_fputs (linebuffer, tee->one);
718 tee->two->to_fputs (linebuffer, tee->two);
722 tee_file_isatty (struct ui_file *file)
724 struct tee_file *tee = ui_file_data (file);
726 if (tee->magic != &tee_file_magic)
727 internal_error (__FILE__, __LINE__,
728 _("tee_file_isatty: bad magic number"));
730 return ui_file_isatty (tee->one);