1 /* UI_FILE - a generic STDIO like output stream.
3 Copyright (C) 1999, 2000, 2001, 2002, 2007, 2008, 2009, 2010
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_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_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;
58 struct ui_file *file = xmalloc (sizeof (struct ui_file));
59 file->magic = &ui_file_magic;
60 set_ui_file_data (file, NULL, null_file_delete);
61 set_ui_file_flush (file, null_file_flush);
62 set_ui_file_write (file, null_file_write);
63 set_ui_file_fputs (file, null_file_fputs);
64 set_ui_file_read (file, null_file_read);
65 set_ui_file_isatty (file, null_file_isatty);
66 set_ui_file_rewind (file, null_file_rewind);
67 set_ui_file_put (file, null_file_put);
72 ui_file_delete (struct ui_file *file)
74 file->to_delete (file);
79 null_file_isatty (struct ui_file *file)
85 null_file_rewind (struct ui_file *file)
91 null_file_put (struct ui_file *file,
92 ui_file_put_method_ftype *write,
99 null_file_flush (struct ui_file *file)
105 null_file_write (struct ui_file *file,
109 if (file->to_fputs == null_file_fputs)
110 /* Both the write and fputs methods are null. Discard the
115 /* The fputs method isn't null, slowly pass the write request
116 onto that. FYI, this isn't as bad as it may look - the
117 current (as of 1999-11-07) printf_* function calls fputc and
118 fputc does exactly the below. By having a write function it
119 is possible to clean up that code. */
123 for (i = 0; i < sizeof_buf; i++)
126 file->to_fputs (b, file);
133 null_file_read (struct ui_file *file,
142 null_file_fputs (const char *buf, struct ui_file *file)
144 if (file->to_write == null_file_write)
145 /* Both the write and fputs methods are null. Discard the
150 /* The write method was implemented, use that. */
151 file->to_write (file, buf, strlen (buf));
156 null_file_delete (struct ui_file *file)
162 ui_file_data (struct ui_file *file)
164 if (file->magic != &ui_file_magic)
165 internal_error (__FILE__, __LINE__,
166 _("ui_file_data: bad magic number"));
167 return file->to_data;
171 gdb_flush (struct ui_file *file)
173 file->to_flush (file);
177 ui_file_isatty (struct ui_file *file)
179 return file->to_isatty (file);
183 ui_file_rewind (struct ui_file *file)
185 file->to_rewind (file);
189 ui_file_put (struct ui_file *file,
190 ui_file_put_method_ftype *write,
193 file->to_put (file, write, dest);
197 ui_file_write (struct ui_file *file,
201 file->to_write (file, buf, length_buf);
205 ui_file_read (struct ui_file *file, char *buf, long length_buf)
207 return file->to_read (file, buf, length_buf);
211 fputs_unfiltered (const char *buf, struct ui_file *file)
213 file->to_fputs (buf, file);
217 set_ui_file_flush (struct ui_file *file, ui_file_flush_ftype *flush)
219 file->to_flush = flush;
223 set_ui_file_isatty (struct ui_file *file, ui_file_isatty_ftype *isatty)
225 file->to_isatty = isatty;
229 set_ui_file_rewind (struct ui_file *file, ui_file_rewind_ftype *rewind)
231 file->to_rewind = rewind;
235 set_ui_file_put (struct ui_file *file, ui_file_put_ftype *put)
241 set_ui_file_write (struct ui_file *file,
242 ui_file_write_ftype *write)
244 file->to_write = write;
248 set_ui_file_read (struct ui_file *file, ui_file_read_ftype *read)
250 file->to_read = read;
254 set_ui_file_fputs (struct ui_file *file, ui_file_fputs_ftype *fputs)
256 file->to_fputs = fputs;
260 set_ui_file_data (struct ui_file *file, void *data,
261 ui_file_delete_ftype *delete)
263 file->to_data = data;
264 file->to_delete = delete;
267 /* ui_file utility function for converting a ``struct ui_file'' into
270 struct accumulated_ui_file
277 do_ui_file_xstrdup (void *context, const char *buffer, long length)
279 struct accumulated_ui_file *acc = context;
280 if (acc->buffer == NULL)
281 acc->buffer = xmalloc (length + 1);
283 acc->buffer = xrealloc (acc->buffer, acc->length + length + 1);
284 memcpy (acc->buffer + acc->length, buffer, length);
285 acc->length += length;
286 acc->buffer[acc->length] = '\0';
290 ui_file_xstrdup (struct ui_file *file, long *length)
292 struct accumulated_ui_file acc;
295 ui_file_put (file, do_ui_file_xstrdup, &acc);
296 if (acc.buffer == NULL)
297 acc.buffer = xstrdup ("");
299 *length = acc.length;
304 do_ui_file_obsavestring (void *context, const char *buffer, long length)
306 struct obstack *obstack = (struct obstack *) context;
307 obstack_grow (obstack, buffer, length);
311 ui_file_obsavestring (struct ui_file *file, struct obstack *obstack,
314 ui_file_put (file, do_ui_file_obsavestring, obstack);
315 *length = obstack_object_size (obstack);
316 obstack_1grow (obstack, '\0');
317 return obstack_finish (obstack);
320 /* A pure memory based ``struct ui_file'' that can be used an output
321 buffer. The buffers accumulated contents are available via
332 static ui_file_rewind_ftype mem_file_rewind;
333 static ui_file_put_ftype mem_file_put;
334 static ui_file_write_ftype mem_file_write;
335 static ui_file_delete_ftype mem_file_delete;
336 static struct ui_file *mem_file_new (void);
337 static int mem_file_magic;
339 static struct ui_file *
342 struct mem_file *stream = XMALLOC (struct mem_file);
343 struct ui_file *file = ui_file_new ();
344 set_ui_file_data (file, stream, mem_file_delete);
345 set_ui_file_rewind (file, mem_file_rewind);
346 set_ui_file_put (file, mem_file_put);
347 set_ui_file_write (file, mem_file_write);
348 stream->magic = &mem_file_magic;
349 stream->buffer = NULL;
350 stream->sizeof_buffer = 0;
351 stream->length_buffer = 0;
356 mem_file_delete (struct ui_file *file)
358 struct mem_file *stream = ui_file_data (file);
359 if (stream->magic != &mem_file_magic)
360 internal_error (__FILE__, __LINE__,
361 _("mem_file_delete: bad magic number"));
362 if (stream->buffer != NULL)
363 xfree (stream->buffer);
370 return mem_file_new ();
374 mem_file_rewind (struct ui_file *file)
376 struct mem_file *stream = ui_file_data (file);
377 if (stream->magic != &mem_file_magic)
378 internal_error (__FILE__, __LINE__,
379 _("mem_file_rewind: bad magic number"));
380 stream->length_buffer = 0;
384 mem_file_put (struct ui_file *file,
385 ui_file_put_method_ftype *write,
388 struct mem_file *stream = ui_file_data (file);
389 if (stream->magic != &mem_file_magic)
390 internal_error (__FILE__, __LINE__,
391 _("mem_file_put: bad magic number"));
392 if (stream->length_buffer > 0)
393 write (dest, stream->buffer, stream->length_buffer);
397 mem_file_write (struct ui_file *file,
401 struct mem_file *stream = ui_file_data (file);
402 if (stream->magic != &mem_file_magic)
403 internal_error (__FILE__, __LINE__,
404 _("mem_file_write: bad magic number"));
405 if (stream->buffer == NULL)
407 stream->length_buffer = length_buffer;
408 stream->sizeof_buffer = length_buffer;
409 stream->buffer = xmalloc (stream->sizeof_buffer);
410 memcpy (stream->buffer, buffer, length_buffer);
414 int new_length = stream->length_buffer + length_buffer;
415 if (new_length >= stream->sizeof_buffer)
417 stream->sizeof_buffer = new_length;
418 stream->buffer = xrealloc (stream->buffer, stream->sizeof_buffer);
420 memcpy (stream->buffer + stream->length_buffer, buffer, length_buffer);
421 stream->length_buffer = new_length;
425 /* ``struct ui_file'' implementation that maps directly onto
428 static ui_file_write_ftype stdio_file_write;
429 static ui_file_fputs_ftype stdio_file_fputs;
430 static ui_file_read_ftype stdio_file_read;
431 static ui_file_isatty_ftype stdio_file_isatty;
432 static ui_file_delete_ftype stdio_file_delete;
433 static struct ui_file *stdio_file_new (FILE * file, int close_p);
434 static ui_file_flush_ftype stdio_file_flush;
436 static int stdio_file_magic;
445 static struct ui_file *
446 stdio_file_new (FILE *file, int close_p)
448 struct ui_file *ui_file = ui_file_new ();
449 struct stdio_file *stdio = xmalloc (sizeof (struct stdio_file));
450 stdio->magic = &stdio_file_magic;
452 stdio->close_p = close_p;
453 set_ui_file_data (ui_file, stdio, stdio_file_delete);
454 set_ui_file_flush (ui_file, stdio_file_flush);
455 set_ui_file_write (ui_file, stdio_file_write);
456 set_ui_file_fputs (ui_file, stdio_file_fputs);
457 set_ui_file_read (ui_file, stdio_file_read);
458 set_ui_file_isatty (ui_file, stdio_file_isatty);
463 stdio_file_delete (struct ui_file *file)
465 struct stdio_file *stdio = ui_file_data (file);
466 if (stdio->magic != &stdio_file_magic)
467 internal_error (__FILE__, __LINE__,
468 _("stdio_file_delete: bad magic number"));
471 fclose (stdio->file);
477 stdio_file_flush (struct ui_file *file)
479 struct stdio_file *stdio = ui_file_data (file);
480 if (stdio->magic != &stdio_file_magic)
481 internal_error (__FILE__, __LINE__,
482 _("stdio_file_flush: bad magic number"));
483 fflush (stdio->file);
487 stdio_file_read (struct ui_file *file, char *buf, long length_buf)
489 struct stdio_file *stdio = ui_file_data (file);
490 if (stdio->magic != &stdio_file_magic)
491 internal_error (__FILE__, __LINE__,
492 _("stdio_file_read: bad magic number"));
494 /* For the benefit of Windows, call gdb_select before reading from
495 the file. Wait until at least one byte of data is available.
496 Control-C can interrupt gdb_select, but not read. */
498 int fd = fileno (stdio->file);
501 FD_SET (fd, &readfds);
502 if (gdb_select (fd + 1, &readfds, NULL, NULL, NULL) == -1)
506 return read (fileno (stdio->file), buf, length_buf);
510 stdio_file_write (struct ui_file *file, const char *buf, long length_buf)
512 struct stdio_file *stdio = ui_file_data (file);
513 if (stdio->magic != &stdio_file_magic)
514 internal_error (__FILE__, __LINE__,
515 _("stdio_file_write: bad magic number"));
516 /* Calling error crashes when we are called from the exception framework. */
517 if (fwrite (buf, length_buf, 1, stdio->file))
522 stdio_file_fputs (const char *linebuffer, struct ui_file *file)
524 struct stdio_file *stdio = ui_file_data (file);
525 if (stdio->magic != &stdio_file_magic)
526 internal_error (__FILE__, __LINE__,
527 _("stdio_file_fputs: bad magic number"));
528 /* Calling error crashes when we are called from the exception framework. */
529 if (fputs (linebuffer, stdio->file))
534 stdio_file_isatty (struct ui_file *file)
536 struct stdio_file *stdio = ui_file_data (file);
537 if (stdio->magic != &stdio_file_magic)
538 internal_error (__FILE__, __LINE__,
539 _("stdio_file_isatty: bad magic number"));
540 return (isatty (fileno (stdio->file)));
543 /* Like fdopen(). Create a ui_file from a previously opened FILE. */
546 stdio_fileopen (FILE *file)
548 return stdio_file_new (file, 0);
552 gdb_fopen (char *name, char *mode)
554 FILE *f = fopen (name, mode);
557 return stdio_file_new (f, 1);
560 /* ``struct ui_file'' implementation that maps onto two ui-file objects. */
562 static ui_file_write_ftype tee_file_write;
563 static ui_file_fputs_ftype tee_file_fputs;
564 static ui_file_isatty_ftype tee_file_isatty;
565 static ui_file_delete_ftype tee_file_delete;
566 static ui_file_flush_ftype tee_file_flush;
568 static int tee_file_magic;
573 struct ui_file *one, *two;
574 int close_one, close_two;
578 tee_file_new (struct ui_file *one, int close_one,
579 struct ui_file *two, int close_two)
581 struct ui_file *ui_file = ui_file_new ();
582 struct tee_file *tee = xmalloc (sizeof (struct tee_file));
583 tee->magic = &tee_file_magic;
586 tee->close_one = close_one;
587 tee->close_two = close_two;
588 set_ui_file_data (ui_file, tee, tee_file_delete);
589 set_ui_file_flush (ui_file, tee_file_flush);
590 set_ui_file_write (ui_file, tee_file_write);
591 set_ui_file_fputs (ui_file, tee_file_fputs);
592 set_ui_file_isatty (ui_file, tee_file_isatty);
597 tee_file_delete (struct ui_file *file)
599 struct tee_file *tee = ui_file_data (file);
600 if (tee->magic != &tee_file_magic)
601 internal_error (__FILE__, __LINE__,
602 _("tee_file_delete: bad magic number"));
604 ui_file_delete (tee->one);
606 ui_file_delete (tee->two);
612 tee_file_flush (struct ui_file *file)
614 struct tee_file *tee = ui_file_data (file);
615 if (tee->magic != &tee_file_magic)
616 internal_error (__FILE__, __LINE__,
617 _("tee_file_flush: bad magic number"));
618 tee->one->to_flush (tee->one);
619 tee->two->to_flush (tee->two);
623 tee_file_write (struct ui_file *file, const char *buf, long length_buf)
625 struct tee_file *tee = ui_file_data (file);
626 if (tee->magic != &tee_file_magic)
627 internal_error (__FILE__, __LINE__,
628 _("tee_file_write: bad magic number"));
629 ui_file_write (tee->one, buf, length_buf);
630 ui_file_write (tee->two, buf, length_buf);
634 tee_file_fputs (const char *linebuffer, struct ui_file *file)
636 struct tee_file *tee = ui_file_data (file);
637 if (tee->magic != &tee_file_magic)
638 internal_error (__FILE__, __LINE__,
639 _("tee_file_fputs: bad magic number"));
640 tee->one->to_fputs (linebuffer, tee->one);
641 tee->two->to_fputs (linebuffer, tee->two);
645 tee_file_isatty (struct ui_file *file)
647 struct tee_file *tee = ui_file_data (file);
648 if (tee->magic != &tee_file_magic)
649 internal_error (__FILE__, __LINE__,
650 _("tee_file_isatty: bad magic number"));
652 return ui_file_isatty (tee->one);