* ui-file.c (gdb_fopen): Make arguments const.
[deliverable/binutils-gdb.git] / gdb / ui-file.c
1 /* UI_FILE - a generic STDIO like output stream.
2
3 Copyright (C) 1999-2013 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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 3 of the License, or
10 (at your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* Implement the ``struct ui_file'' object. */
21
22 #include "defs.h"
23 #include "ui-file.h"
24 #include "gdb_obstack.h"
25 #include "gdb_string.h"
26 #include "gdb_select.h"
27 #include "filestuff.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 static ui_file_fseek_ftype null_file_fseek;
41
42 struct ui_file
43 {
44 int *magic;
45 ui_file_flush_ftype *to_flush;
46 ui_file_write_ftype *to_write;
47 ui_file_write_async_safe_ftype *to_write_async_safe;
48 ui_file_fputs_ftype *to_fputs;
49 ui_file_read_ftype *to_read;
50 ui_file_delete_ftype *to_delete;
51 ui_file_isatty_ftype *to_isatty;
52 ui_file_rewind_ftype *to_rewind;
53 ui_file_put_ftype *to_put;
54 ui_file_fseek_ftype *to_fseek;
55 void *to_data;
56 };
57 int ui_file_magic;
58
59 struct ui_file *
60 ui_file_new (void)
61 {
62 struct ui_file *file = xmalloc (sizeof (struct ui_file));
63
64 file->magic = &ui_file_magic;
65 set_ui_file_data (file, NULL, null_file_delete);
66 set_ui_file_flush (file, null_file_flush);
67 set_ui_file_write (file, null_file_write);
68 set_ui_file_write_async_safe (file, null_file_write_async_safe);
69 set_ui_file_fputs (file, null_file_fputs);
70 set_ui_file_read (file, null_file_read);
71 set_ui_file_isatty (file, null_file_isatty);
72 set_ui_file_rewind (file, null_file_rewind);
73 set_ui_file_put (file, null_file_put);
74 set_ui_file_fseek (file, null_file_fseek);
75 return file;
76 }
77
78 void
79 ui_file_delete (struct ui_file *file)
80 {
81 file->to_delete (file);
82 xfree (file);
83 }
84
85 static int
86 null_file_isatty (struct ui_file *file)
87 {
88 return 0;
89 }
90
91 static void
92 null_file_rewind (struct ui_file *file)
93 {
94 return;
95 }
96
97 static void
98 null_file_put (struct ui_file *file,
99 ui_file_put_method_ftype *write,
100 void *dest)
101 {
102 return;
103 }
104
105 static void
106 null_file_flush (struct ui_file *file)
107 {
108 return;
109 }
110
111 static void
112 null_file_write (struct ui_file *file,
113 const char *buf,
114 long sizeof_buf)
115 {
116 if (file->to_fputs == null_file_fputs)
117 /* Both the write and fputs methods are null. Discard the
118 request. */
119 return;
120 else
121 {
122 /* The fputs method isn't null, slowly pass the write request
123 onto that. FYI, this isn't as bad as it may look - the
124 current (as of 1999-11-07) printf_* function calls fputc and
125 fputc does exactly the below. By having a write function it
126 is possible to clean up that code. */
127 int i;
128 char b[2];
129
130 b[1] = '\0';
131 for (i = 0; i < sizeof_buf; i++)
132 {
133 b[0] = buf[i];
134 file->to_fputs (b, file);
135 }
136 return;
137 }
138 }
139
140 static long
141 null_file_read (struct ui_file *file,
142 char *buf,
143 long sizeof_buf)
144 {
145 errno = EBADF;
146 return 0;
147 }
148
149 static void
150 null_file_fputs (const char *buf, struct ui_file *file)
151 {
152 if (file->to_write == null_file_write)
153 /* Both the write and fputs methods are null. Discard the
154 request. */
155 return;
156 else
157 {
158 /* The write method was implemented, use that. */
159 file->to_write (file, buf, strlen (buf));
160 }
161 }
162
163 static void
164 null_file_write_async_safe (struct ui_file *file,
165 const char *buf,
166 long sizeof_buf)
167 {
168 return;
169 }
170
171 static void
172 null_file_delete (struct ui_file *file)
173 {
174 return;
175 }
176
177 static int
178 null_file_fseek (struct ui_file *stream, long offset, int whence)
179 {
180 errno = EBADF;
181
182 return -1;
183 }
184
185 void *
186 ui_file_data (struct ui_file *file)
187 {
188 if (file->magic != &ui_file_magic)
189 internal_error (__FILE__, __LINE__,
190 _("ui_file_data: bad magic number"));
191 return file->to_data;
192 }
193
194 void
195 gdb_flush (struct ui_file *file)
196 {
197 file->to_flush (file);
198 }
199
200 int
201 ui_file_isatty (struct ui_file *file)
202 {
203 return file->to_isatty (file);
204 }
205
206 void
207 ui_file_rewind (struct ui_file *file)
208 {
209 file->to_rewind (file);
210 }
211
212 void
213 ui_file_put (struct ui_file *file,
214 ui_file_put_method_ftype *write,
215 void *dest)
216 {
217 file->to_put (file, write, dest);
218 }
219
220 void
221 ui_file_write (struct ui_file *file,
222 const char *buf,
223 long length_buf)
224 {
225 file->to_write (file, buf, length_buf);
226 }
227
228 void
229 ui_file_write_async_safe (struct ui_file *file,
230 const char *buf,
231 long length_buf)
232 {
233 file->to_write_async_safe (file, buf, length_buf);
234 }
235
236 long
237 ui_file_read (struct ui_file *file, char *buf, long length_buf)
238 {
239 return file->to_read (file, buf, length_buf);
240 }
241
242 int
243 ui_file_fseek (struct ui_file *file, long offset, int whence)
244 {
245 return file->to_fseek (file, offset, whence);
246 }
247
248 void
249 fputs_unfiltered (const char *buf, struct ui_file *file)
250 {
251 file->to_fputs (buf, file);
252 }
253
254 void
255 set_ui_file_flush (struct ui_file *file, ui_file_flush_ftype *flush_ptr)
256 {
257 file->to_flush = flush_ptr;
258 }
259
260 void
261 set_ui_file_isatty (struct ui_file *file, ui_file_isatty_ftype *isatty_ptr)
262 {
263 file->to_isatty = isatty_ptr;
264 }
265
266 void
267 set_ui_file_rewind (struct ui_file *file, ui_file_rewind_ftype *rewind_ptr)
268 {
269 file->to_rewind = rewind_ptr;
270 }
271
272 void
273 set_ui_file_put (struct ui_file *file, ui_file_put_ftype *put_ptr)
274 {
275 file->to_put = put_ptr;
276 }
277
278 void
279 set_ui_file_write (struct ui_file *file,
280 ui_file_write_ftype *write_ptr)
281 {
282 file->to_write = write_ptr;
283 }
284
285 void
286 set_ui_file_write_async_safe (struct ui_file *file,
287 ui_file_write_async_safe_ftype *write_async_safe_ptr)
288 {
289 file->to_write_async_safe = write_async_safe_ptr;
290 }
291
292 void
293 set_ui_file_read (struct ui_file *file, ui_file_read_ftype *read_ptr)
294 {
295 file->to_read = read_ptr;
296 }
297
298 void
299 set_ui_file_fputs (struct ui_file *file, ui_file_fputs_ftype *fputs_ptr)
300 {
301 file->to_fputs = fputs_ptr;
302 }
303
304 void
305 set_ui_file_fseek (struct ui_file *file, ui_file_fseek_ftype *fseek_ptr)
306 {
307 file->to_fseek = fseek_ptr;
308 }
309
310 void
311 set_ui_file_data (struct ui_file *file, void *data,
312 ui_file_delete_ftype *delete_ptr)
313 {
314 file->to_data = data;
315 file->to_delete = delete_ptr;
316 }
317
318 /* ui_file utility function for converting a ``struct ui_file'' into
319 a memory buffer. */
320
321 struct accumulated_ui_file
322 {
323 char *buffer;
324 long length;
325 };
326
327 static void
328 do_ui_file_xstrdup (void *context, const char *buffer, long length)
329 {
330 struct accumulated_ui_file *acc = context;
331
332 if (acc->buffer == NULL)
333 acc->buffer = xmalloc (length + 1);
334 else
335 acc->buffer = xrealloc (acc->buffer, acc->length + length + 1);
336 memcpy (acc->buffer + acc->length, buffer, length);
337 acc->length += length;
338 acc->buffer[acc->length] = '\0';
339 }
340
341 char *
342 ui_file_xstrdup (struct ui_file *file, long *length)
343 {
344 struct accumulated_ui_file acc;
345
346 acc.buffer = NULL;
347 acc.length = 0;
348 ui_file_put (file, do_ui_file_xstrdup, &acc);
349 if (acc.buffer == NULL)
350 acc.buffer = xstrdup ("");
351 if (length != NULL)
352 *length = acc.length;
353 return acc.buffer;
354 }
355
356 static void
357 do_ui_file_obsavestring (void *context, const char *buffer, long length)
358 {
359 struct obstack *obstack = (struct obstack *) context;
360
361 obstack_grow (obstack, buffer, length);
362 }
363
364 char *
365 ui_file_obsavestring (struct ui_file *file, struct obstack *obstack,
366 long *length)
367 {
368 ui_file_put (file, do_ui_file_obsavestring, obstack);
369 *length = obstack_object_size (obstack);
370 obstack_1grow (obstack, '\0');
371 return obstack_finish (obstack);
372 }
373 \f
374 /* A pure memory based ``struct ui_file'' that can be used an output
375 buffer. The buffers accumulated contents are available via
376 ui_file_put(). */
377
378 struct mem_file
379 {
380 int *magic;
381 char *buffer;
382 int sizeof_buffer;
383 int length_buffer;
384 };
385
386 static ui_file_rewind_ftype mem_file_rewind;
387 static ui_file_put_ftype mem_file_put;
388 static ui_file_write_ftype mem_file_write;
389 static ui_file_delete_ftype mem_file_delete;
390 static struct ui_file *mem_file_new (void);
391 static int mem_file_magic;
392
393 static struct ui_file *
394 mem_file_new (void)
395 {
396 struct mem_file *stream = XMALLOC (struct mem_file);
397 struct ui_file *file = ui_file_new ();
398
399 set_ui_file_data (file, stream, mem_file_delete);
400 set_ui_file_rewind (file, mem_file_rewind);
401 set_ui_file_put (file, mem_file_put);
402 set_ui_file_write (file, mem_file_write);
403 stream->magic = &mem_file_magic;
404 stream->buffer = NULL;
405 stream->sizeof_buffer = 0;
406 stream->length_buffer = 0;
407 return file;
408 }
409
410 static void
411 mem_file_delete (struct ui_file *file)
412 {
413 struct mem_file *stream = ui_file_data (file);
414
415 if (stream->magic != &mem_file_magic)
416 internal_error (__FILE__, __LINE__,
417 _("mem_file_delete: bad magic number"));
418 if (stream->buffer != NULL)
419 xfree (stream->buffer);
420 xfree (stream);
421 }
422
423 struct ui_file *
424 mem_fileopen (void)
425 {
426 return mem_file_new ();
427 }
428
429 static void
430 mem_file_rewind (struct ui_file *file)
431 {
432 struct mem_file *stream = ui_file_data (file);
433
434 if (stream->magic != &mem_file_magic)
435 internal_error (__FILE__, __LINE__,
436 _("mem_file_rewind: bad magic number"));
437 stream->length_buffer = 0;
438 }
439
440 static void
441 mem_file_put (struct ui_file *file,
442 ui_file_put_method_ftype *write,
443 void *dest)
444 {
445 struct mem_file *stream = ui_file_data (file);
446
447 if (stream->magic != &mem_file_magic)
448 internal_error (__FILE__, __LINE__,
449 _("mem_file_put: bad magic number"));
450 if (stream->length_buffer > 0)
451 write (dest, stream->buffer, stream->length_buffer);
452 }
453
454 void
455 mem_file_write (struct ui_file *file,
456 const char *buffer,
457 long length_buffer)
458 {
459 struct mem_file *stream = ui_file_data (file);
460
461 if (stream->magic != &mem_file_magic)
462 internal_error (__FILE__, __LINE__,
463 _("mem_file_write: bad magic number"));
464 if (stream->buffer == NULL)
465 {
466 stream->length_buffer = length_buffer;
467 stream->sizeof_buffer = length_buffer;
468 stream->buffer = xmalloc (stream->sizeof_buffer);
469 memcpy (stream->buffer, buffer, length_buffer);
470 }
471 else
472 {
473 int new_length = stream->length_buffer + length_buffer;
474
475 if (new_length >= stream->sizeof_buffer)
476 {
477 stream->sizeof_buffer = new_length;
478 stream->buffer = xrealloc (stream->buffer, stream->sizeof_buffer);
479 }
480 memcpy (stream->buffer + stream->length_buffer, buffer, length_buffer);
481 stream->length_buffer = new_length;
482 }
483 }
484 \f
485 /* ``struct ui_file'' implementation that maps directly onto
486 <stdio.h>'s FILE. */
487
488 static ui_file_write_ftype stdio_file_write;
489 static ui_file_write_async_safe_ftype stdio_file_write_async_safe;
490 static ui_file_fputs_ftype stdio_file_fputs;
491 static ui_file_read_ftype stdio_file_read;
492 static ui_file_isatty_ftype stdio_file_isatty;
493 static ui_file_delete_ftype stdio_file_delete;
494 static struct ui_file *stdio_file_new (FILE *file, int close_p);
495 static ui_file_flush_ftype stdio_file_flush;
496 static ui_file_fseek_ftype stdio_file_fseek;
497
498 static int stdio_file_magic;
499
500 struct stdio_file
501 {
502 int *magic;
503 FILE *file;
504 /* The associated file descriptor is extracted ahead of time for
505 stdio_file_write_async_safe's benefit, in case fileno isn't async-safe. */
506 int fd;
507 int close_p;
508 };
509
510 static struct ui_file *
511 stdio_file_new (FILE *file, int close_p)
512 {
513 struct ui_file *ui_file = ui_file_new ();
514 struct stdio_file *stdio = xmalloc (sizeof (struct stdio_file));
515
516 stdio->magic = &stdio_file_magic;
517 stdio->file = file;
518 stdio->fd = fileno (file);
519 stdio->close_p = close_p;
520 set_ui_file_data (ui_file, stdio, stdio_file_delete);
521 set_ui_file_flush (ui_file, stdio_file_flush);
522 set_ui_file_write (ui_file, stdio_file_write);
523 set_ui_file_write_async_safe (ui_file, stdio_file_write_async_safe);
524 set_ui_file_fputs (ui_file, stdio_file_fputs);
525 set_ui_file_read (ui_file, stdio_file_read);
526 set_ui_file_isatty (ui_file, stdio_file_isatty);
527 set_ui_file_fseek (ui_file, stdio_file_fseek);
528 return ui_file;
529 }
530
531 static void
532 stdio_file_delete (struct ui_file *file)
533 {
534 struct stdio_file *stdio = ui_file_data (file);
535
536 if (stdio->magic != &stdio_file_magic)
537 internal_error (__FILE__, __LINE__,
538 _("stdio_file_delete: bad magic number"));
539 if (stdio->close_p)
540 {
541 fclose (stdio->file);
542 }
543 xfree (stdio);
544 }
545
546 static void
547 stdio_file_flush (struct ui_file *file)
548 {
549 struct stdio_file *stdio = ui_file_data (file);
550
551 if (stdio->magic != &stdio_file_magic)
552 internal_error (__FILE__, __LINE__,
553 _("stdio_file_flush: bad magic number"));
554 fflush (stdio->file);
555 }
556
557 static long
558 stdio_file_read (struct ui_file *file, char *buf, long length_buf)
559 {
560 struct stdio_file *stdio = ui_file_data (file);
561
562 if (stdio->magic != &stdio_file_magic)
563 internal_error (__FILE__, __LINE__,
564 _("stdio_file_read: bad magic number"));
565
566 /* For the benefit of Windows, call gdb_select before reading from
567 the file. Wait until at least one byte of data is available.
568 Control-C can interrupt gdb_select, but not read. */
569 {
570 fd_set readfds;
571 FD_ZERO (&readfds);
572 FD_SET (stdio->fd, &readfds);
573 if (gdb_select (stdio->fd + 1, &readfds, NULL, NULL, NULL) == -1)
574 return -1;
575 }
576
577 return read (stdio->fd, buf, length_buf);
578 }
579
580 static void
581 stdio_file_write (struct ui_file *file, const char *buf, long length_buf)
582 {
583 struct stdio_file *stdio = ui_file_data (file);
584
585 if (stdio->magic != &stdio_file_magic)
586 internal_error (__FILE__, __LINE__,
587 _("stdio_file_write: bad magic number"));
588 /* Calling error crashes when we are called from the exception framework. */
589 if (fwrite (buf, length_buf, 1, stdio->file))
590 {
591 /* Nothing. */
592 }
593 }
594
595 static void
596 stdio_file_write_async_safe (struct ui_file *file,
597 const char *buf, long length_buf)
598 {
599 struct stdio_file *stdio = ui_file_data (file);
600
601 if (stdio->magic != &stdio_file_magic)
602 {
603 /* gettext isn't necessarily async safe, so we can't use _("error message") here.
604 We could extract the correct translation ahead of time, but this is an extremely
605 rare event, and one of the other stdio_file_* routines will presumably catch
606 the problem anyway. For now keep it simple and ignore the error here. */
607 return;
608 }
609
610 /* This is written the way it is to avoid a warning from gcc about not using the
611 result of write (since it can be declared with attribute warn_unused_result).
612 Alas casting to void doesn't work for this. */
613 if (write (stdio->fd, buf, length_buf))
614 {
615 /* Nothing. */
616 }
617 }
618
619 static void
620 stdio_file_fputs (const char *linebuffer, struct ui_file *file)
621 {
622 struct stdio_file *stdio = ui_file_data (file);
623
624 if (stdio->magic != &stdio_file_magic)
625 internal_error (__FILE__, __LINE__,
626 _("stdio_file_fputs: bad magic number"));
627 /* Calling error crashes when we are called from the exception framework. */
628 if (fputs (linebuffer, stdio->file))
629 {
630 /* Nothing. */
631 }
632 }
633
634 static int
635 stdio_file_isatty (struct ui_file *file)
636 {
637 struct stdio_file *stdio = ui_file_data (file);
638
639 if (stdio->magic != &stdio_file_magic)
640 internal_error (__FILE__, __LINE__,
641 _("stdio_file_isatty: bad magic number"));
642 return (isatty (stdio->fd));
643 }
644
645 static int
646 stdio_file_fseek (struct ui_file *file, long offset, int whence)
647 {
648 struct stdio_file *stdio = ui_file_data (file);
649
650 if (stdio->magic != &stdio_file_magic)
651 internal_error (__FILE__, __LINE__,
652 _("stdio_file_fseek: bad magic number"));
653
654 return fseek (stdio->file, offset, whence);
655 }
656
657 /* Like fdopen(). Create a ui_file from a previously opened FILE. */
658
659 struct ui_file *
660 stdio_fileopen (FILE *file)
661 {
662 return stdio_file_new (file, 0);
663 }
664
665 struct ui_file *
666 gdb_fopen (const char *name, const char *mode)
667 {
668 FILE *f = gdb_fopen_cloexec (name, mode);
669
670 if (f == NULL)
671 return NULL;
672 return stdio_file_new (f, 1);
673 }
674
675 /* ``struct ui_file'' implementation that maps onto two ui-file objects. */
676
677 static ui_file_write_ftype tee_file_write;
678 static ui_file_fputs_ftype tee_file_fputs;
679 static ui_file_isatty_ftype tee_file_isatty;
680 static ui_file_delete_ftype tee_file_delete;
681 static ui_file_flush_ftype tee_file_flush;
682
683 static int tee_file_magic;
684
685 struct tee_file
686 {
687 int *magic;
688 struct ui_file *one, *two;
689 int close_one, close_two;
690 };
691
692 struct ui_file *
693 tee_file_new (struct ui_file *one, int close_one,
694 struct ui_file *two, int close_two)
695 {
696 struct ui_file *ui_file = ui_file_new ();
697 struct tee_file *tee = xmalloc (sizeof (struct tee_file));
698
699 tee->magic = &tee_file_magic;
700 tee->one = one;
701 tee->two = two;
702 tee->close_one = close_one;
703 tee->close_two = close_two;
704 set_ui_file_data (ui_file, tee, tee_file_delete);
705 set_ui_file_flush (ui_file, tee_file_flush);
706 set_ui_file_write (ui_file, tee_file_write);
707 set_ui_file_fputs (ui_file, tee_file_fputs);
708 set_ui_file_isatty (ui_file, tee_file_isatty);
709 return ui_file;
710 }
711
712 static void
713 tee_file_delete (struct ui_file *file)
714 {
715 struct tee_file *tee = ui_file_data (file);
716
717 if (tee->magic != &tee_file_magic)
718 internal_error (__FILE__, __LINE__,
719 _("tee_file_delete: bad magic number"));
720 if (tee->close_one)
721 ui_file_delete (tee->one);
722 if (tee->close_two)
723 ui_file_delete (tee->two);
724
725 xfree (tee);
726 }
727
728 static void
729 tee_file_flush (struct ui_file *file)
730 {
731 struct tee_file *tee = ui_file_data (file);
732
733 if (tee->magic != &tee_file_magic)
734 internal_error (__FILE__, __LINE__,
735 _("tee_file_flush: bad magic number"));
736 tee->one->to_flush (tee->one);
737 tee->two->to_flush (tee->two);
738 }
739
740 static void
741 tee_file_write (struct ui_file *file, const char *buf, long length_buf)
742 {
743 struct tee_file *tee = ui_file_data (file);
744
745 if (tee->magic != &tee_file_magic)
746 internal_error (__FILE__, __LINE__,
747 _("tee_file_write: bad magic number"));
748 ui_file_write (tee->one, buf, length_buf);
749 ui_file_write (tee->two, buf, length_buf);
750 }
751
752 static void
753 tee_file_fputs (const char *linebuffer, struct ui_file *file)
754 {
755 struct tee_file *tee = ui_file_data (file);
756
757 if (tee->magic != &tee_file_magic)
758 internal_error (__FILE__, __LINE__,
759 _("tee_file_fputs: bad magic number"));
760 tee->one->to_fputs (linebuffer, tee->one);
761 tee->two->to_fputs (linebuffer, tee->two);
762 }
763
764 static int
765 tee_file_isatty (struct ui_file *file)
766 {
767 struct tee_file *tee = ui_file_data (file);
768
769 if (tee->magic != &tee_file_magic)
770 internal_error (__FILE__, __LINE__,
771 _("tee_file_isatty: bad magic number"));
772
773 return ui_file_isatty (tee->one);
774 }
This page took 0.076681 seconds and 4 git commands to generate.