gdb/
[deliverable/binutils-gdb.git] / gdb / ui-file.c
... / ...
CommitLineData
1/* UI_FILE - a generic STDIO like output stream.
2
3 Copyright (C) 1999, 2000, 2001, 2002, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
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.
12
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.
17
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/>. */
20
21/* Implement the ``struct ui_file'' object. */
22
23#include "defs.h"
24#include "ui-file.h"
25#include "gdb_obstack.h"
26#include "gdb_string.h"
27#include "gdb_select.h"
28
29#include <errno.h>
30
31static ui_file_isatty_ftype null_file_isatty;
32static ui_file_write_ftype null_file_write;
33static ui_file_write_ftype null_file_write_async_safe;
34static ui_file_fputs_ftype null_file_fputs;
35static ui_file_read_ftype null_file_read;
36static ui_file_flush_ftype null_file_flush;
37static ui_file_delete_ftype null_file_delete;
38static ui_file_rewind_ftype null_file_rewind;
39static ui_file_put_ftype null_file_put;
40
41struct ui_file
42 {
43 int *magic;
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;
53 void *to_data;
54 };
55int ui_file_magic;
56
57struct ui_file *
58ui_file_new (void)
59{
60 struct ui_file *file = xmalloc (sizeof (struct ui_file));
61
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);
72 return file;
73}
74
75void
76ui_file_delete (struct ui_file *file)
77{
78 file->to_delete (file);
79 xfree (file);
80}
81
82static int
83null_file_isatty (struct ui_file *file)
84{
85 return 0;
86}
87
88static void
89null_file_rewind (struct ui_file *file)
90{
91 return;
92}
93
94static void
95null_file_put (struct ui_file *file,
96 ui_file_put_method_ftype *write,
97 void *dest)
98{
99 return;
100}
101
102static void
103null_file_flush (struct ui_file *file)
104{
105 return;
106}
107
108static void
109null_file_write (struct ui_file *file,
110 const char *buf,
111 long sizeof_buf)
112{
113 if (file->to_fputs == null_file_fputs)
114 /* Both the write and fputs methods are null. Discard the
115 request. */
116 return;
117 else
118 {
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. */
124 int i;
125 char b[2];
126
127 b[1] = '\0';
128 for (i = 0; i < sizeof_buf; i++)
129 {
130 b[0] = buf[i];
131 file->to_fputs (b, file);
132 }
133 return;
134 }
135}
136
137static long
138null_file_read (struct ui_file *file,
139 char *buf,
140 long sizeof_buf)
141{
142 errno = EBADF;
143 return 0;
144}
145
146static void
147null_file_fputs (const char *buf, struct ui_file *file)
148{
149 if (file->to_write == null_file_write)
150 /* Both the write and fputs methods are null. Discard the
151 request. */
152 return;
153 else
154 {
155 /* The write method was implemented, use that. */
156 file->to_write (file, buf, strlen (buf));
157 }
158}
159
160static void
161null_file_write_async_safe (struct ui_file *file,
162 const char *buf,
163 long sizeof_buf)
164{
165 return;
166}
167
168static void
169null_file_delete (struct ui_file *file)
170{
171 return;
172}
173
174void *
175ui_file_data (struct ui_file *file)
176{
177 if (file->magic != &ui_file_magic)
178 internal_error (__FILE__, __LINE__,
179 _("ui_file_data: bad magic number"));
180 return file->to_data;
181}
182
183void
184gdb_flush (struct ui_file *file)
185{
186 file->to_flush (file);
187}
188
189int
190ui_file_isatty (struct ui_file *file)
191{
192 return file->to_isatty (file);
193}
194
195void
196ui_file_rewind (struct ui_file *file)
197{
198 file->to_rewind (file);
199}
200
201void
202ui_file_put (struct ui_file *file,
203 ui_file_put_method_ftype *write,
204 void *dest)
205{
206 file->to_put (file, write, dest);
207}
208
209void
210ui_file_write (struct ui_file *file,
211 const char *buf,
212 long length_buf)
213{
214 file->to_write (file, buf, length_buf);
215}
216
217void
218ui_file_write_async_safe (struct ui_file *file,
219 const char *buf,
220 long length_buf)
221{
222 file->to_write_async_safe (file, buf, length_buf);
223}
224
225long
226ui_file_read (struct ui_file *file, char *buf, long length_buf)
227{
228 return file->to_read (file, buf, length_buf);
229}
230
231void
232fputs_unfiltered (const char *buf, struct ui_file *file)
233{
234 file->to_fputs (buf, file);
235}
236
237void
238set_ui_file_flush (struct ui_file *file, ui_file_flush_ftype *flush)
239{
240 file->to_flush = flush;
241}
242
243void
244set_ui_file_isatty (struct ui_file *file, ui_file_isatty_ftype *isatty)
245{
246 file->to_isatty = isatty;
247}
248
249void
250set_ui_file_rewind (struct ui_file *file, ui_file_rewind_ftype *rewind)
251{
252 file->to_rewind = rewind;
253}
254
255void
256set_ui_file_put (struct ui_file *file, ui_file_put_ftype *put)
257{
258 file->to_put = put;
259}
260
261void
262set_ui_file_write (struct ui_file *file,
263 ui_file_write_ftype *write)
264{
265 file->to_write = write;
266}
267
268void
269set_ui_file_write_async_safe (struct ui_file *file,
270 ui_file_write_async_safe_ftype *write_async_safe)
271{
272 file->to_write_async_safe = write_async_safe;
273}
274
275void
276set_ui_file_read (struct ui_file *file, ui_file_read_ftype *read)
277{
278 file->to_read = read;
279}
280
281void
282set_ui_file_fputs (struct ui_file *file, ui_file_fputs_ftype *fputs)
283{
284 file->to_fputs = fputs;
285}
286
287void
288set_ui_file_data (struct ui_file *file, void *data,
289 ui_file_delete_ftype *delete)
290{
291 file->to_data = data;
292 file->to_delete = delete;
293}
294
295/* ui_file utility function for converting a ``struct ui_file'' into
296 a memory buffer. */
297
298struct accumulated_ui_file
299{
300 char *buffer;
301 long length;
302};
303
304static void
305do_ui_file_xstrdup (void *context, const char *buffer, long length)
306{
307 struct accumulated_ui_file *acc = context;
308
309 if (acc->buffer == NULL)
310 acc->buffer = xmalloc (length + 1);
311 else
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';
316}
317
318char *
319ui_file_xstrdup (struct ui_file *file, long *length)
320{
321 struct accumulated_ui_file acc;
322
323 acc.buffer = NULL;
324 acc.length = 0;
325 ui_file_put (file, do_ui_file_xstrdup, &acc);
326 if (acc.buffer == NULL)
327 acc.buffer = xstrdup ("");
328 if (length != NULL)
329 *length = acc.length;
330 return acc.buffer;
331}
332
333static void
334do_ui_file_obsavestring (void *context, const char *buffer, long length)
335{
336 struct obstack *obstack = (struct obstack *) context;
337
338 obstack_grow (obstack, buffer, length);
339}
340
341char *
342ui_file_obsavestring (struct ui_file *file, struct obstack *obstack,
343 long *length)
344{
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);
349}
350\f
351/* A pure memory based ``struct ui_file'' that can be used an output
352 buffer. The buffers accumulated contents are available via
353 ui_file_put(). */
354
355struct mem_file
356 {
357 int *magic;
358 char *buffer;
359 int sizeof_buffer;
360 int length_buffer;
361 };
362
363static ui_file_rewind_ftype mem_file_rewind;
364static ui_file_put_ftype mem_file_put;
365static ui_file_write_ftype mem_file_write;
366static ui_file_delete_ftype mem_file_delete;
367static struct ui_file *mem_file_new (void);
368static int mem_file_magic;
369
370static struct ui_file *
371mem_file_new (void)
372{
373 struct mem_file *stream = XMALLOC (struct mem_file);
374 struct ui_file *file = ui_file_new ();
375
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;
384 return file;
385}
386
387static void
388mem_file_delete (struct ui_file *file)
389{
390 struct mem_file *stream = ui_file_data (file);
391
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);
397 xfree (stream);
398}
399
400struct ui_file *
401mem_fileopen (void)
402{
403 return mem_file_new ();
404}
405
406static void
407mem_file_rewind (struct ui_file *file)
408{
409 struct mem_file *stream = ui_file_data (file);
410
411 if (stream->magic != &mem_file_magic)
412 internal_error (__FILE__, __LINE__,
413 _("mem_file_rewind: bad magic number"));
414 stream->length_buffer = 0;
415}
416
417static void
418mem_file_put (struct ui_file *file,
419 ui_file_put_method_ftype *write,
420 void *dest)
421{
422 struct mem_file *stream = ui_file_data (file);
423
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);
429}
430
431void
432mem_file_write (struct ui_file *file,
433 const char *buffer,
434 long length_buffer)
435{
436 struct mem_file *stream = ui_file_data (file);
437
438 if (stream->magic != &mem_file_magic)
439 internal_error (__FILE__, __LINE__,
440 _("mem_file_write: bad magic number"));
441 if (stream->buffer == NULL)
442 {
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);
447 }
448 else
449 {
450 int new_length = stream->length_buffer + length_buffer;
451
452 if (new_length >= stream->sizeof_buffer)
453 {
454 stream->sizeof_buffer = new_length;
455 stream->buffer = xrealloc (stream->buffer, stream->sizeof_buffer);
456 }
457 memcpy (stream->buffer + stream->length_buffer, buffer, length_buffer);
458 stream->length_buffer = new_length;
459 }
460}
461\f
462/* ``struct ui_file'' implementation that maps directly onto
463 <stdio.h>'s FILE. */
464
465static ui_file_write_ftype stdio_file_write;
466static ui_file_write_async_safe_ftype stdio_file_write_async_safe;
467static ui_file_fputs_ftype stdio_file_fputs;
468static ui_file_read_ftype stdio_file_read;
469static ui_file_isatty_ftype stdio_file_isatty;
470static ui_file_delete_ftype stdio_file_delete;
471static struct ui_file *stdio_file_new (FILE *file, int close_p);
472static ui_file_flush_ftype stdio_file_flush;
473
474static int stdio_file_magic;
475
476struct stdio_file
477 {
478 int *magic;
479 FILE *file;
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. */
482 int fd;
483 int close_p;
484 };
485
486static struct ui_file *
487stdio_file_new (FILE *file, int close_p)
488{
489 struct ui_file *ui_file = ui_file_new ();
490 struct stdio_file *stdio = xmalloc (sizeof (struct stdio_file));
491
492 stdio->magic = &stdio_file_magic;
493 stdio->file = file;
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);
503 return ui_file;
504}
505
506static void
507stdio_file_delete (struct ui_file *file)
508{
509 struct stdio_file *stdio = ui_file_data (file);
510
511 if (stdio->magic != &stdio_file_magic)
512 internal_error (__FILE__, __LINE__,
513 _("stdio_file_delete: bad magic number"));
514 if (stdio->close_p)
515 {
516 fclose (stdio->file);
517 }
518 xfree (stdio);
519}
520
521static void
522stdio_file_flush (struct ui_file *file)
523{
524 struct stdio_file *stdio = ui_file_data (file);
525
526 if (stdio->magic != &stdio_file_magic)
527 internal_error (__FILE__, __LINE__,
528 _("stdio_file_flush: bad magic number"));
529 fflush (stdio->file);
530}
531
532static long
533stdio_file_read (struct ui_file *file, char *buf, long length_buf)
534{
535 struct stdio_file *stdio = ui_file_data (file);
536
537 if (stdio->magic != &stdio_file_magic)
538 internal_error (__FILE__, __LINE__,
539 _("stdio_file_read: bad magic number"));
540
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. */
544 {
545 fd_set readfds;
546 FD_ZERO (&readfds);
547 FD_SET (stdio->fd, &readfds);
548 if (gdb_select (stdio->fd + 1, &readfds, NULL, NULL, NULL) == -1)
549 return -1;
550 }
551
552 return read (stdio->fd, buf, length_buf);
553}
554
555static void
556stdio_file_write (struct ui_file *file, const char *buf, long length_buf)
557{
558 struct stdio_file *stdio = ui_file_data (file);
559
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))
565 ;
566}
567
568static void
569stdio_file_write_async_safe (struct ui_file *file,
570 const char *buf, long length_buf)
571{
572 struct stdio_file *stdio = ui_file_data (file);
573
574 if (stdio->magic != &stdio_file_magic)
575 {
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. */
580 return;
581 }
582
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))
587 ;
588}
589
590static void
591stdio_file_fputs (const char *linebuffer, struct ui_file *file)
592{
593 struct stdio_file *stdio = ui_file_data (file);
594
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))
600 ;
601}
602
603static int
604stdio_file_isatty (struct ui_file *file)
605{
606 struct stdio_file *stdio = ui_file_data (file);
607
608 if (stdio->magic != &stdio_file_magic)
609 internal_error (__FILE__, __LINE__,
610 _("stdio_file_isatty: bad magic number"));
611 return (isatty (stdio->fd));
612}
613
614/* Like fdopen(). Create a ui_file from a previously opened FILE. */
615
616struct ui_file *
617stdio_fileopen (FILE *file)
618{
619 return stdio_file_new (file, 0);
620}
621
622struct ui_file *
623gdb_fopen (char *name, char *mode)
624{
625 FILE *f = fopen (name, mode);
626
627 if (f == NULL)
628 return NULL;
629 return stdio_file_new (f, 1);
630}
631
632/* ``struct ui_file'' implementation that maps onto two ui-file objects. */
633
634static ui_file_write_ftype tee_file_write;
635static ui_file_fputs_ftype tee_file_fputs;
636static ui_file_isatty_ftype tee_file_isatty;
637static ui_file_delete_ftype tee_file_delete;
638static ui_file_flush_ftype tee_file_flush;
639
640static int tee_file_magic;
641
642struct tee_file
643 {
644 int *magic;
645 struct ui_file *one, *two;
646 int close_one, close_two;
647 };
648
649struct ui_file *
650tee_file_new (struct ui_file *one, int close_one,
651 struct ui_file *two, int close_two)
652{
653 struct ui_file *ui_file = ui_file_new ();
654 struct tee_file *tee = xmalloc (sizeof (struct tee_file));
655
656 tee->magic = &tee_file_magic;
657 tee->one = one;
658 tee->two = two;
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);
666 return ui_file;
667}
668
669static void
670tee_file_delete (struct ui_file *file)
671{
672 struct tee_file *tee = ui_file_data (file);
673
674 if (tee->magic != &tee_file_magic)
675 internal_error (__FILE__, __LINE__,
676 _("tee_file_delete: bad magic number"));
677 if (tee->close_one)
678 ui_file_delete (tee->one);
679 if (tee->close_two)
680 ui_file_delete (tee->two);
681
682 xfree (tee);
683}
684
685static void
686tee_file_flush (struct ui_file *file)
687{
688 struct tee_file *tee = ui_file_data (file);
689
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);
695}
696
697static void
698tee_file_write (struct ui_file *file, const char *buf, long length_buf)
699{
700 struct tee_file *tee = ui_file_data (file);
701
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);
707}
708
709static void
710tee_file_fputs (const char *linebuffer, struct ui_file *file)
711{
712 struct tee_file *tee = ui_file_data (file);
713
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);
719}
720
721static int
722tee_file_isatty (struct ui_file *file)
723{
724 struct tee_file *tee = ui_file_data (file);
725
726 if (tee->magic != &tee_file_magic)
727 internal_error (__FILE__, __LINE__,
728 _("tee_file_isatty: bad magic number"));
729
730 return ui_file_isatty (tee->one);
731}
This page took 0.024311 seconds and 4 git commands to generate.