Update
[gdb.git] / gdb / ui-file.c
blob9a1d892b132b14e7e0b74e6ac4aa636fb823db26
1 /* UI_FILE - a generic STDIO like output stream.
3 Copyright (C) 1999, 2000, 2001, 2002, 2007, 2008
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. */
23 #include "defs.h"
24 #include "ui-file.h"
25 #include "gdb_string.h"
27 #include <errno.h>
29 static ui_file_isatty_ftype null_file_isatty;
30 static ui_file_write_ftype null_file_write;
31 static ui_file_fputs_ftype null_file_fputs;
32 static ui_file_read_ftype null_file_read;
33 static ui_file_flush_ftype null_file_flush;
34 static ui_file_delete_ftype null_file_delete;
35 static ui_file_rewind_ftype null_file_rewind;
36 static ui_file_put_ftype null_file_put;
38 struct ui_file
40 int *magic;
41 ui_file_flush_ftype *to_flush;
42 ui_file_write_ftype *to_write;
43 ui_file_fputs_ftype *to_fputs;
44 ui_file_read_ftype *to_read;
45 ui_file_delete_ftype *to_delete;
46 ui_file_isatty_ftype *to_isatty;
47 ui_file_rewind_ftype *to_rewind;
48 ui_file_put_ftype *to_put;
49 void *to_data;
51 int ui_file_magic;
53 struct ui_file *
54 ui_file_new (void)
56 struct ui_file *file = xmalloc (sizeof (struct ui_file));
57 file->magic = &ui_file_magic;
58 set_ui_file_data (file, NULL, null_file_delete);
59 set_ui_file_flush (file, null_file_flush);
60 set_ui_file_write (file, null_file_write);
61 set_ui_file_fputs (file, null_file_fputs);
62 set_ui_file_read (file, null_file_read);
63 set_ui_file_isatty (file, null_file_isatty);
64 set_ui_file_rewind (file, null_file_rewind);
65 set_ui_file_put (file, null_file_put);
66 return file;
69 void
70 ui_file_delete (struct ui_file *file)
72 file->to_delete (file);
73 xfree (file);
76 static int
77 null_file_isatty (struct ui_file *file)
79 return 0;
82 static void
83 null_file_rewind (struct ui_file *file)
85 return;
88 static void
89 null_file_put (struct ui_file *file,
90 ui_file_put_method_ftype *write,
91 void *dest)
93 return;
96 static void
97 null_file_flush (struct ui_file *file)
99 return;
102 static void
103 null_file_write (struct ui_file *file,
104 const char *buf,
105 long sizeof_buf)
107 if (file->to_fputs == null_file_fputs)
108 /* Both the write and fputs methods are null. Discard the
109 request. */
110 return;
111 else
113 /* The fputs method isn't null, slowly pass the write request
114 onto that. FYI, this isn't as bad as it may look - the
115 current (as of 1999-11-07) printf_* function calls fputc and
116 fputc does exactly the below. By having a write function it
117 is possible to clean up that code. */
118 int i;
119 char b[2];
120 b[1] = '\0';
121 for (i = 0; i < sizeof_buf; i++)
123 b[0] = buf[i];
124 file->to_fputs (b, file);
126 return;
130 static long
131 null_file_read (struct ui_file *file,
132 char *buf,
133 long sizeof_buf)
135 errno = EBADF;
136 return 0;
139 static void
140 null_file_fputs (const char *buf, struct ui_file *file)
142 if (file->to_write == null_file_write)
143 /* Both the write and fputs methods are null. Discard the
144 request. */
145 return;
146 else
148 /* The write method was implemented, use that. */
149 file->to_write (file, buf, strlen (buf));
153 static void
154 null_file_delete (struct ui_file *file)
156 return;
159 void *
160 ui_file_data (struct ui_file *file)
162 if (file->magic != &ui_file_magic)
163 internal_error (__FILE__, __LINE__,
164 _("ui_file_data: bad magic number"));
165 return file->to_data;
168 void
169 gdb_flush (struct ui_file *file)
171 file->to_flush (file);
175 ui_file_isatty (struct ui_file *file)
177 return file->to_isatty (file);
180 void
181 ui_file_rewind (struct ui_file *file)
183 file->to_rewind (file);
186 void
187 ui_file_put (struct ui_file *file,
188 ui_file_put_method_ftype *write,
189 void *dest)
191 file->to_put (file, write, dest);
194 void
195 ui_file_write (struct ui_file *file,
196 const char *buf,
197 long length_buf)
199 file->to_write (file, buf, length_buf);
202 long
203 ui_file_read (struct ui_file *file, char *buf, long length_buf)
205 return file->to_read (file, buf, length_buf);
208 void
209 fputs_unfiltered (const char *buf, struct ui_file *file)
211 file->to_fputs (buf, file);
214 void
215 set_ui_file_flush (struct ui_file *file, ui_file_flush_ftype *flush)
217 file->to_flush = flush;
220 void
221 set_ui_file_isatty (struct ui_file *file, ui_file_isatty_ftype *isatty)
223 file->to_isatty = isatty;
226 void
227 set_ui_file_rewind (struct ui_file *file, ui_file_rewind_ftype *rewind)
229 file->to_rewind = rewind;
232 void
233 set_ui_file_put (struct ui_file *file, ui_file_put_ftype *put)
235 file->to_put = put;
238 void
239 set_ui_file_write (struct ui_file *file,
240 ui_file_write_ftype *write)
242 file->to_write = write;
245 void
246 set_ui_file_read (struct ui_file *file, ui_file_read_ftype *read)
248 file->to_read = read;
251 void
252 set_ui_file_fputs (struct ui_file *file, ui_file_fputs_ftype *fputs)
254 file->to_fputs = fputs;
257 void
258 set_ui_file_data (struct ui_file *file, void *data,
259 ui_file_delete_ftype *delete)
261 file->to_data = data;
262 file->to_delete = delete;
265 /* ui_file utility function for converting a ``struct ui_file'' into
266 a memory buffer''. */
268 struct accumulated_ui_file
270 char *buffer;
271 long length;
274 static void
275 do_ui_file_xstrdup (void *context, const char *buffer, long length)
277 struct accumulated_ui_file *acc = context;
278 if (acc->buffer == NULL)
279 acc->buffer = xmalloc (length + 1);
280 else
281 acc->buffer = xrealloc (acc->buffer, acc->length + length + 1);
282 memcpy (acc->buffer + acc->length, buffer, length);
283 acc->length += length;
284 acc->buffer[acc->length] = '\0';
287 char *
288 ui_file_xstrdup (struct ui_file *file,
289 long *length)
291 struct accumulated_ui_file acc;
292 acc.buffer = NULL;
293 acc.length = 0;
294 ui_file_put (file, do_ui_file_xstrdup, &acc);
295 if (acc.buffer == NULL)
296 acc.buffer = xstrdup ("");
297 *length = acc.length;
298 return acc.buffer;
301 /* A pure memory based ``struct ui_file'' that can be used an output
302 buffer. The buffers accumulated contents are available via
303 ui_file_put(). */
305 struct mem_file
307 int *magic;
308 char *buffer;
309 int sizeof_buffer;
310 int length_buffer;
313 static ui_file_rewind_ftype mem_file_rewind;
314 static ui_file_put_ftype mem_file_put;
315 static ui_file_write_ftype mem_file_write;
316 static ui_file_delete_ftype mem_file_delete;
317 static struct ui_file *mem_file_new (void);
318 static int mem_file_magic;
320 static struct ui_file *
321 mem_file_new (void)
323 struct mem_file *stream = XMALLOC (struct mem_file);
324 struct ui_file *file = ui_file_new ();
325 set_ui_file_data (file, stream, mem_file_delete);
326 set_ui_file_rewind (file, mem_file_rewind);
327 set_ui_file_put (file, mem_file_put);
328 set_ui_file_write (file, mem_file_write);
329 stream->magic = &mem_file_magic;
330 stream->buffer = NULL;
331 stream->sizeof_buffer = 0;
332 stream->length_buffer = 0;
333 return file;
336 static void
337 mem_file_delete (struct ui_file *file)
339 struct mem_file *stream = ui_file_data (file);
340 if (stream->magic != &mem_file_magic)
341 internal_error (__FILE__, __LINE__,
342 _("mem_file_delete: bad magic number"));
343 if (stream->buffer != NULL)
344 xfree (stream->buffer);
345 xfree (stream);
348 struct ui_file *
349 mem_fileopen (void)
351 return mem_file_new ();
354 static void
355 mem_file_rewind (struct ui_file *file)
357 struct mem_file *stream = ui_file_data (file);
358 if (stream->magic != &mem_file_magic)
359 internal_error (__FILE__, __LINE__,
360 _("mem_file_rewind: bad magic number"));
361 stream->length_buffer = 0;
364 static void
365 mem_file_put (struct ui_file *file,
366 ui_file_put_method_ftype *write,
367 void *dest)
369 struct mem_file *stream = ui_file_data (file);
370 if (stream->magic != &mem_file_magic)
371 internal_error (__FILE__, __LINE__,
372 _("mem_file_put: bad magic number"));
373 if (stream->length_buffer > 0)
374 write (dest, stream->buffer, stream->length_buffer);
377 void
378 mem_file_write (struct ui_file *file,
379 const char *buffer,
380 long length_buffer)
382 struct mem_file *stream = ui_file_data (file);
383 if (stream->magic != &mem_file_magic)
384 internal_error (__FILE__, __LINE__,
385 _("mem_file_write: bad magic number"));
386 if (stream->buffer == NULL)
388 stream->length_buffer = length_buffer;
389 stream->sizeof_buffer = length_buffer;
390 stream->buffer = xmalloc (stream->sizeof_buffer);
391 memcpy (stream->buffer, buffer, length_buffer);
393 else
395 int new_length = stream->length_buffer + length_buffer;
396 if (new_length >= stream->sizeof_buffer)
398 stream->sizeof_buffer = new_length;
399 stream->buffer = xrealloc (stream->buffer, stream->sizeof_buffer);
401 memcpy (stream->buffer + stream->length_buffer, buffer, length_buffer);
402 stream->length_buffer = new_length;
406 /* ``struct ui_file'' implementation that maps directly onto
407 <stdio.h>'s FILE. */
409 static ui_file_write_ftype stdio_file_write;
410 static ui_file_fputs_ftype stdio_file_fputs;
411 static ui_file_read_ftype stdio_file_read;
412 static ui_file_isatty_ftype stdio_file_isatty;
413 static ui_file_delete_ftype stdio_file_delete;
414 static struct ui_file *stdio_file_new (FILE * file, int close_p);
415 static ui_file_flush_ftype stdio_file_flush;
417 static int stdio_file_magic;
419 struct stdio_file
421 int *magic;
422 FILE *file;
423 int close_p;
426 static struct ui_file *
427 stdio_file_new (FILE *file, int close_p)
429 struct ui_file *ui_file = ui_file_new ();
430 struct stdio_file *stdio = xmalloc (sizeof (struct stdio_file));
431 stdio->magic = &stdio_file_magic;
432 stdio->file = file;
433 stdio->close_p = close_p;
434 set_ui_file_data (ui_file, stdio, stdio_file_delete);
435 set_ui_file_flush (ui_file, stdio_file_flush);
436 set_ui_file_write (ui_file, stdio_file_write);
437 set_ui_file_fputs (ui_file, stdio_file_fputs);
438 set_ui_file_read (ui_file, stdio_file_read);
439 set_ui_file_isatty (ui_file, stdio_file_isatty);
440 return ui_file;
443 static void
444 stdio_file_delete (struct ui_file *file)
446 struct stdio_file *stdio = ui_file_data (file);
447 if (stdio->magic != &stdio_file_magic)
448 internal_error (__FILE__, __LINE__,
449 _("stdio_file_delete: bad magic number"));
450 if (stdio->close_p)
452 fclose (stdio->file);
454 xfree (stdio);
457 static void
458 stdio_file_flush (struct ui_file *file)
460 struct stdio_file *stdio = ui_file_data (file);
461 if (stdio->magic != &stdio_file_magic)
462 internal_error (__FILE__, __LINE__,
463 _("stdio_file_flush: bad magic number"));
464 fflush (stdio->file);
467 static long
468 stdio_file_read (struct ui_file *file, char *buf, long length_buf)
470 struct stdio_file *stdio = ui_file_data (file);
471 if (stdio->magic != &stdio_file_magic)
472 internal_error (__FILE__, __LINE__,
473 _("stdio_file_read: bad magic number"));
474 return read (fileno (stdio->file), buf, length_buf);
477 static void
478 stdio_file_write (struct ui_file *file, const char *buf, long length_buf)
480 struct stdio_file *stdio = ui_file_data (file);
481 if (stdio->magic != &stdio_file_magic)
482 internal_error (__FILE__, __LINE__,
483 _("stdio_file_write: bad magic number"));
484 fwrite (buf, length_buf, 1, stdio->file);
487 static void
488 stdio_file_fputs (const char *linebuffer, struct ui_file *file)
490 struct stdio_file *stdio = ui_file_data (file);
491 if (stdio->magic != &stdio_file_magic)
492 internal_error (__FILE__, __LINE__,
493 _("stdio_file_fputs: bad magic number"));
494 fputs (linebuffer, stdio->file);
497 static int
498 stdio_file_isatty (struct ui_file *file)
500 struct stdio_file *stdio = ui_file_data (file);
501 if (stdio->magic != &stdio_file_magic)
502 internal_error (__FILE__, __LINE__,
503 _("stdio_file_isatty: bad magic number"));
504 return (isatty (fileno (stdio->file)));
507 /* Like fdopen(). Create a ui_file from a previously opened FILE. */
509 struct ui_file *
510 stdio_fileopen (FILE *file)
512 return stdio_file_new (file, 0);
515 struct ui_file *
516 gdb_fopen (char *name, char *mode)
518 FILE *f = fopen (name, mode);
519 if (f == NULL)
520 return NULL;
521 return stdio_file_new (f, 1);
524 /* ``struct ui_file'' implementation that maps onto two ui-file objects. */
526 static ui_file_write_ftype tee_file_write;
527 static ui_file_fputs_ftype tee_file_fputs;
528 static ui_file_isatty_ftype tee_file_isatty;
529 static ui_file_delete_ftype tee_file_delete;
530 static ui_file_flush_ftype tee_file_flush;
532 static int tee_file_magic;
534 struct tee_file
536 int *magic;
537 struct ui_file *one, *two;
538 int close_one, close_two;
541 struct ui_file *
542 tee_file_new (struct ui_file *one, int close_one,
543 struct ui_file *two, int close_two)
545 struct ui_file *ui_file = ui_file_new ();
546 struct tee_file *tee = xmalloc (sizeof (struct tee_file));
547 tee->magic = &tee_file_magic;
548 tee->one = one;
549 tee->two = two;
550 tee->close_one = close_one;
551 tee->close_two = close_two;
552 set_ui_file_data (ui_file, tee, tee_file_delete);
553 set_ui_file_flush (ui_file, tee_file_flush);
554 set_ui_file_write (ui_file, tee_file_write);
555 set_ui_file_fputs (ui_file, tee_file_fputs);
556 set_ui_file_isatty (ui_file, tee_file_isatty);
557 return ui_file;
560 static void
561 tee_file_delete (struct ui_file *file)
563 struct tee_file *tee = ui_file_data (file);
564 if (tee->magic != &tee_file_magic)
565 internal_error (__FILE__, __LINE__,
566 _("tee_file_delete: bad magic number"));
567 if (tee->close_one)
568 ui_file_delete (tee->one);
569 if (tee->close_two)
570 ui_file_delete (tee->two);
572 xfree (tee);
575 static void
576 tee_file_flush (struct ui_file *file)
578 struct tee_file *tee = ui_file_data (file);
579 if (tee->magic != &tee_file_magic)
580 internal_error (__FILE__, __LINE__,
581 _("tee_file_flush: bad magic number"));
582 tee->one->to_flush (tee->one);
583 tee->two->to_flush (tee->two);
586 static void
587 tee_file_write (struct ui_file *file, const char *buf, long length_buf)
589 struct tee_file *tee = ui_file_data (file);
590 if (tee->magic != &tee_file_magic)
591 internal_error (__FILE__, __LINE__,
592 _("tee_file_write: bad magic number"));
593 ui_file_write (tee->one, buf, length_buf);
594 ui_file_write (tee->two, buf, length_buf);
597 static void
598 tee_file_fputs (const char *linebuffer, struct ui_file *file)
600 struct tee_file *tee = ui_file_data (file);
601 if (tee->magic != &tee_file_magic)
602 internal_error (__FILE__, __LINE__,
603 _("tee_file_fputs: bad magic number"));
604 tee->one->to_fputs (linebuffer, tee->one);
605 tee->two->to_fputs (linebuffer, tee->two);
608 static int
609 tee_file_isatty (struct ui_file *file)
611 struct tee_file *tee = ui_file_data (file);
612 if (tee->magic != &tee_file_magic)
613 internal_error (__FILE__, __LINE__,
614 _("tee_file_isatty: bad magic number"));
615 return (0);