1 /* UI_FILE - a generic STDIO like output stream.
3 Copyright 1999, 2000, 2001, 2002 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 2 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, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Implement the ``struct ui_file'' object. */
26 #include "gdb_string.h"
30 static ui_file_isatty_ftype null_file_isatty;
31 static ui_file_write_ftype null_file_write;
32 static ui_file_fputs_ftype null_file_fputs;
33 static ui_file_read_ftype null_file_read;
34 static ui_file_flush_ftype null_file_flush;
35 static ui_file_delete_ftype null_file_delete;
36 static ui_file_rewind_ftype null_file_rewind;
37 static ui_file_put_ftype null_file_put;
42 ui_file_flush_ftype *to_flush;
43 ui_file_write_ftype *to_write;
44 ui_file_fputs_ftype *to_fputs;
45 ui_file_read_ftype *to_read;
46 ui_file_delete_ftype *to_delete;
47 ui_file_isatty_ftype *to_isatty;
48 ui_file_rewind_ftype *to_rewind;
49 ui_file_put_ftype *to_put;
57 struct ui_file *file = xmalloc (sizeof (struct ui_file));
58 file->magic = &ui_file_magic;
59 set_ui_file_data (file, NULL, null_file_delete);
60 set_ui_file_flush (file, null_file_flush);
61 set_ui_file_write (file, null_file_write);
62 set_ui_file_fputs (file, null_file_fputs);
63 set_ui_file_read (file, null_file_read);
64 set_ui_file_isatty (file, null_file_isatty);
65 set_ui_file_rewind (file, null_file_rewind);
66 set_ui_file_put (file, null_file_put);
71 ui_file_delete (struct ui_file *file)
73 file->to_delete (file);
78 null_file_isatty (struct ui_file *file)
84 null_file_rewind (struct ui_file *file)
90 null_file_put (struct ui_file *file,
91 ui_file_put_method_ftype *write,
98 null_file_flush (struct ui_file *file)
104 null_file_write (struct ui_file *file,
108 if (file->to_fputs == null_file_fputs)
109 /* Both the write and fputs methods are null. Discard the
114 /* The fputs method isn't null, slowly pass the write request
115 onto that. FYI, this isn't as bad as it may look - the
116 current (as of 1999-11-07) printf_* function calls fputc and
117 fputc does exactly the below. By having a write function it
118 is possible to clean up that code. */
122 for (i = 0; i < sizeof_buf; i++)
125 file->to_fputs (b, file);
132 null_file_read (struct ui_file *file,
141 null_file_fputs (const char *buf, struct ui_file *file)
143 if (file->to_write == null_file_write)
144 /* Both the write and fputs methods are null. Discard the
149 /* The write method was implemented, use that. */
150 file->to_write (file, buf, strlen (buf));
155 null_file_delete (struct ui_file *file)
161 ui_file_data (struct ui_file *file)
163 if (file->magic != &ui_file_magic)
164 internal_error (__FILE__, __LINE__,
165 _("ui_file_data: bad magic number"));
166 return file->to_data;
170 gdb_flush (struct ui_file *file)
172 file->to_flush (file);
176 ui_file_isatty (struct ui_file *file)
178 return file->to_isatty (file);
182 ui_file_rewind (struct ui_file *file)
184 file->to_rewind (file);
188 ui_file_put (struct ui_file *file,
189 ui_file_put_method_ftype *write,
192 file->to_put (file, write, dest);
196 ui_file_write (struct ui_file *file,
200 file->to_write (file, buf, length_buf);
204 ui_file_read (struct ui_file *file, char *buf, long length_buf)
206 return file->to_read (file, buf, length_buf);
210 fputs_unfiltered (const char *buf, struct ui_file *file)
212 file->to_fputs (buf, file);
216 set_ui_file_flush (struct ui_file *file, ui_file_flush_ftype *flush)
218 file->to_flush = flush;
222 set_ui_file_isatty (struct ui_file *file, ui_file_isatty_ftype *isatty)
224 file->to_isatty = isatty;
228 set_ui_file_rewind (struct ui_file *file, ui_file_rewind_ftype *rewind)
230 file->to_rewind = rewind;
234 set_ui_file_put (struct ui_file *file, ui_file_put_ftype *put)
240 set_ui_file_write (struct ui_file *file,
241 ui_file_write_ftype *write)
243 file->to_write = write;
247 set_ui_file_read (struct ui_file *file, ui_file_read_ftype *read)
249 file->to_read = read;
253 set_ui_file_fputs (struct ui_file *file, ui_file_fputs_ftype *fputs)
255 file->to_fputs = fputs;
259 set_ui_file_data (struct ui_file *file, void *data,
260 ui_file_delete_ftype *delete)
262 file->to_data = data;
263 file->to_delete = delete;
266 /* ui_file utility function for converting a ``struct ui_file'' into
267 a memory buffer''. */
269 struct accumulated_ui_file
276 do_ui_file_xstrdup (void *context, const char *buffer, long length)
278 struct accumulated_ui_file *acc = context;
279 if (acc->buffer == NULL)
280 acc->buffer = xmalloc (length + 1);
282 acc->buffer = xrealloc (acc->buffer, acc->length + length + 1);
283 memcpy (acc->buffer + acc->length, buffer, length);
284 acc->length += length;
285 acc->buffer[acc->length] = '\0';
289 ui_file_xstrdup (struct ui_file *file,
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 ("");
298 *length = acc.length;
302 /* A pure memory based ``struct ui_file'' that can be used an output
303 buffer. The buffers accumulated contents are available via
314 static ui_file_rewind_ftype mem_file_rewind;
315 static ui_file_put_ftype mem_file_put;
316 static ui_file_write_ftype mem_file_write;
317 static ui_file_delete_ftype mem_file_delete;
318 static struct ui_file *mem_file_new (void);
319 static int mem_file_magic;
321 static struct ui_file *
324 struct mem_file *stream = XMALLOC (struct mem_file);
325 struct ui_file *file = ui_file_new ();
326 set_ui_file_data (file, stream, mem_file_delete);
327 set_ui_file_rewind (file, mem_file_rewind);
328 set_ui_file_put (file, mem_file_put);
329 set_ui_file_write (file, mem_file_write);
330 stream->magic = &mem_file_magic;
331 stream->buffer = NULL;
332 stream->sizeof_buffer = 0;
333 stream->length_buffer = 0;
338 mem_file_delete (struct ui_file *file)
340 struct mem_file *stream = ui_file_data (file);
341 if (stream->magic != &mem_file_magic)
342 internal_error (__FILE__, __LINE__,
343 _("mem_file_delete: bad magic number"));
344 if (stream->buffer != NULL)
345 xfree (stream->buffer);
352 return mem_file_new ();
356 mem_file_rewind (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_rewind: bad magic number"));
362 stream->length_buffer = 0;
366 mem_file_put (struct ui_file *file,
367 ui_file_put_method_ftype *write,
370 struct mem_file *stream = ui_file_data (file);
371 if (stream->magic != &mem_file_magic)
372 internal_error (__FILE__, __LINE__,
373 _("mem_file_put: bad magic number"));
374 if (stream->length_buffer > 0)
375 write (dest, stream->buffer, stream->length_buffer);
379 mem_file_write (struct ui_file *file,
383 struct mem_file *stream = ui_file_data (file);
384 if (stream->magic != &mem_file_magic)
385 internal_error (__FILE__, __LINE__,
386 _("mem_file_write: bad magic number"));
387 if (stream->buffer == NULL)
389 stream->length_buffer = length_buffer;
390 stream->sizeof_buffer = length_buffer;
391 stream->buffer = xmalloc (stream->sizeof_buffer);
392 memcpy (stream->buffer, buffer, length_buffer);
396 int new_length = stream->length_buffer + length_buffer;
397 if (new_length >= stream->sizeof_buffer)
399 stream->sizeof_buffer = new_length;
400 stream->buffer = xrealloc (stream->buffer, stream->sizeof_buffer);
402 memcpy (stream->buffer + stream->length_buffer, buffer, length_buffer);
403 stream->length_buffer = new_length;
407 /* ``struct ui_file'' implementation that maps directly onto
410 static ui_file_write_ftype stdio_file_write;
411 static ui_file_fputs_ftype stdio_file_fputs;
412 static ui_file_read_ftype stdio_file_read;
413 static ui_file_isatty_ftype stdio_file_isatty;
414 static ui_file_delete_ftype stdio_file_delete;
415 static struct ui_file *stdio_file_new (FILE * file, int close_p);
416 static ui_file_flush_ftype stdio_file_flush;
418 static int stdio_file_magic;
427 static struct ui_file *
428 stdio_file_new (FILE *file, int close_p)
430 struct ui_file *ui_file = ui_file_new ();
431 struct stdio_file *stdio = xmalloc (sizeof (struct stdio_file));
432 stdio->magic = &stdio_file_magic;
434 stdio->close_p = close_p;
435 set_ui_file_data (ui_file, stdio, stdio_file_delete);
436 set_ui_file_flush (ui_file, stdio_file_flush);
437 set_ui_file_write (ui_file, stdio_file_write);
438 set_ui_file_fputs (ui_file, stdio_file_fputs);
439 set_ui_file_read (ui_file, stdio_file_read);
440 set_ui_file_isatty (ui_file, stdio_file_isatty);
445 stdio_file_delete (struct ui_file *file)
447 struct stdio_file *stdio = ui_file_data (file);
448 if (stdio->magic != &stdio_file_magic)
449 internal_error (__FILE__, __LINE__,
450 _("stdio_file_delete: bad magic number"));
453 fclose (stdio->file);
459 stdio_file_flush (struct ui_file *file)
461 struct stdio_file *stdio = ui_file_data (file);
462 if (stdio->magic != &stdio_file_magic)
463 internal_error (__FILE__, __LINE__,
464 _("stdio_file_flush: bad magic number"));
465 fflush (stdio->file);
469 stdio_file_read (struct ui_file *file, char *buf, long length_buf)
471 struct stdio_file *stdio = ui_file_data (file);
472 if (stdio->magic != &stdio_file_magic)
473 internal_error (__FILE__, __LINE__,
474 _("stdio_file_read: bad magic number"));
475 return read (fileno (stdio->file), buf, length_buf);
479 stdio_file_write (struct ui_file *file, const char *buf, long length_buf)
481 struct stdio_file *stdio = ui_file_data (file);
482 if (stdio->magic != &stdio_file_magic)
483 internal_error (__FILE__, __LINE__,
484 _("stdio_file_write: bad magic number"));
485 fwrite (buf, length_buf, 1, stdio->file);
489 stdio_file_fputs (const char *linebuffer, struct ui_file *file)
491 struct stdio_file *stdio = ui_file_data (file);
492 if (stdio->magic != &stdio_file_magic)
493 internal_error (__FILE__, __LINE__,
494 _("stdio_file_fputs: bad magic number"));
495 fputs (linebuffer, stdio->file);
499 stdio_file_isatty (struct ui_file *file)
501 struct stdio_file *stdio = ui_file_data (file);
502 if (stdio->magic != &stdio_file_magic)
503 internal_error (__FILE__, __LINE__,
504 _("stdio_file_isatty: bad magic number"));
505 return (isatty (fileno (stdio->file)));
508 /* Like fdopen(). Create a ui_file from a previously opened FILE. */
511 stdio_fileopen (FILE *file)
513 return stdio_file_new (file, 0);
517 gdb_fopen (char *name, char *mode)
519 FILE *f = fopen (name, mode);
522 return stdio_file_new (f, 1);
525 /* ``struct ui_file'' implementation that maps onto two ui-file objects. */
527 static ui_file_write_ftype tee_file_write;
528 static ui_file_fputs_ftype tee_file_fputs;
529 static ui_file_isatty_ftype tee_file_isatty;
530 static ui_file_delete_ftype tee_file_delete;
531 static ui_file_flush_ftype tee_file_flush;
533 static int tee_file_magic;
538 struct ui_file *one, *two;
539 int close_one, close_two;
543 tee_file_new (struct ui_file *one, int close_one,
544 struct ui_file *two, int close_two)
546 struct ui_file *ui_file = ui_file_new ();
547 struct tee_file *tee = xmalloc (sizeof (struct tee_file));
548 tee->magic = &tee_file_magic;
551 tee->close_one = close_one;
552 tee->close_two = close_two;
553 set_ui_file_data (ui_file, tee, tee_file_delete);
554 set_ui_file_flush (ui_file, tee_file_flush);
555 set_ui_file_write (ui_file, tee_file_write);
556 set_ui_file_fputs (ui_file, tee_file_fputs);
557 set_ui_file_isatty (ui_file, tee_file_isatty);
562 tee_file_delete (struct ui_file *file)
564 struct tee_file *tee = ui_file_data (file);
565 if (tee->magic != &tee_file_magic)
566 internal_error (__FILE__, __LINE__,
567 _("tee_file_delete: bad magic number"));
569 ui_file_delete (tee->one);
571 ui_file_delete (tee->two);
577 tee_file_flush (struct ui_file *file)
579 struct tee_file *tee = ui_file_data (file);
580 if (tee->magic != &tee_file_magic)
581 internal_error (__FILE__, __LINE__,
582 _("tee_file_flush: bad magic number"));
583 tee->one->to_flush (tee->one);
584 tee->two->to_flush (tee->two);
588 tee_file_write (struct ui_file *file, const char *buf, long length_buf)
590 struct tee_file *tee = ui_file_data (file);
591 if (tee->magic != &tee_file_magic)
592 internal_error (__FILE__, __LINE__,
593 _("tee_file_write: bad magic number"));
594 ui_file_write (tee->one, buf, length_buf);
595 ui_file_write (tee->two, buf, length_buf);
599 tee_file_fputs (const char *linebuffer, struct ui_file *file)
601 struct tee_file *tee = ui_file_data (file);
602 if (tee->magic != &tee_file_magic)
603 internal_error (__FILE__, __LINE__,
604 _("tee_file_fputs: bad magic number"));
605 tee->one->to_fputs (linebuffer, tee->one);
606 tee->two->to_fputs (linebuffer, tee->two);
610 tee_file_isatty (struct ui_file *file)
612 struct tee_file *tee = ui_file_data (file);
613 if (tee->magic != &tee_file_magic)
614 internal_error (__FILE__, __LINE__,
615 _("tee_file_isatty: bad magic number"));