* ui-file.c (stdio_file_write_async_safe): Add comment.
[deliverable/binutils-gdb.git] / gdb / ui-file.c
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
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;
40
41 struct 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 };
55 int ui_file_magic;
56
57 struct ui_file *
58 ui_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
75 void
76 ui_file_delete (struct ui_file *file)
77 {
78 file->to_delete (file);
79 xfree (file);
80 }
81
82 static int
83 null_file_isatty (struct ui_file *file)
84 {
85 return 0;
86 }
87
88 static void
89 null_file_rewind (struct ui_file *file)
90 {
91 return;
92 }
93
94 static void
95 null_file_put (struct ui_file *file,
96 ui_file_put_method_ftype *write,
97 void *dest)
98 {
99 return;
100 }
101
102 static void
103 null_file_flush (struct ui_file *file)
104 {
105 return;
106 }
107
108 static void
109 null_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
137 static long
138 null_file_read (struct ui_file *file,
139 char *buf,
140 long sizeof_buf)
141 {
142 errno = EBADF;
143 return 0;
144 }
145
146 static void
147 null_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
160 static void
161 null_file_write_async_safe (struct ui_file *file,
162 const char *buf,
163 long sizeof_buf)
164 {
165 return;
166 }
167
168 static void
169 null_file_delete (struct ui_file *file)
170 {
171 return;
172 }
173
174 void *
175 ui_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
183 void
184 gdb_flush (struct ui_file *file)
185 {
186 file->to_flush (file);
187 }
188
189 int
190 ui_file_isatty (struct ui_file *file)
191 {
192 return file->to_isatty (file);
193 }
194
195 void
196 ui_file_rewind (struct ui_file *file)
197 {
198 file->to_rewind (file);
199 }
200
201 void
202 ui_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
209 void
210 ui_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
217 void
218 ui_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
225 long
226 ui_file_read (struct ui_file *file, char *buf, long length_buf)
227 {
228 return file->to_read (file, buf, length_buf);
229 }
230
231 void
232 fputs_unfiltered (const char *buf, struct ui_file *file)
233 {
234 file->to_fputs (buf, file);
235 }
236
237 void
238 set_ui_file_flush (struct ui_file *file, ui_file_flush_ftype *flush)
239 {
240 file->to_flush = flush;
241 }
242
243 void
244 set_ui_file_isatty (struct ui_file *file, ui_file_isatty_ftype *isatty)
245 {
246 file->to_isatty = isatty;
247 }
248
249 void
250 set_ui_file_rewind (struct ui_file *file, ui_file_rewind_ftype *rewind)
251 {
252 file->to_rewind = rewind;
253 }
254
255 void
256 set_ui_file_put (struct ui_file *file, ui_file_put_ftype *put)
257 {
258 file->to_put = put;
259 }
260
261 void
262 set_ui_file_write (struct ui_file *file,
263 ui_file_write_ftype *write)
264 {
265 file->to_write = write;
266 }
267
268 void
269 set_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
275 void
276 set_ui_file_read (struct ui_file *file, ui_file_read_ftype *read)
277 {
278 file->to_read = read;
279 }
280
281 void
282 set_ui_file_fputs (struct ui_file *file, ui_file_fputs_ftype *fputs)
283 {
284 file->to_fputs = fputs;
285 }
286
287 void
288 set_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
298 struct accumulated_ui_file
299 {
300 char *buffer;
301 long length;
302 };
303
304 static void
305 do_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
318 char *
319 ui_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
333 static void
334 do_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
341 char *
342 ui_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
355 struct mem_file
356 {
357 int *magic;
358 char *buffer;
359 int sizeof_buffer;
360 int length_buffer;
361 };
362
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;
369
370 static struct ui_file *
371 mem_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
387 static void
388 mem_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
400 struct ui_file *
401 mem_fileopen (void)
402 {
403 return mem_file_new ();
404 }
405
406 static void
407 mem_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
417 static void
418 mem_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
431 void
432 mem_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
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;
473
474 static int stdio_file_magic;
475
476 struct 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
486 static struct ui_file *
487 stdio_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
506 static void
507 stdio_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
521 static void
522 stdio_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
532 static long
533 stdio_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
555 static void
556 stdio_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
568 static void
569 stdio_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
590 static void
591 stdio_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
603 static int
604 stdio_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
616 struct ui_file *
617 stdio_fileopen (FILE *file)
618 {
619 return stdio_file_new (file, 0);
620 }
621
622 struct ui_file *
623 gdb_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
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;
639
640 static int tee_file_magic;
641
642 struct tee_file
643 {
644 int *magic;
645 struct ui_file *one, *two;
646 int close_one, close_two;
647 };
648
649 struct ui_file *
650 tee_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
669 static void
670 tee_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
685 static void
686 tee_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
697 static void
698 tee_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
709 static void
710 tee_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
721 static int
722 tee_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.048896 seconds and 5 git commands to generate.