]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ui-file.c
Add back gdb_pretty_print_insn
[thirdparty/binutils-gdb.git] / gdb / ui-file.c
CommitLineData
d9fcf2fb 1/* UI_FILE - a generic STDIO like output stream.
349c5d5f 2
61baf725 3 Copyright (C) 1999-2017 Free Software Foundation, Inc.
d9fcf2fb
JM
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
d9fcf2fb
JM
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
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
d9fcf2fb 19
581e13c1 20/* Implement the ``struct ui_file'' object. */
d9fcf2fb
JM
21
22#include "defs.h"
23#include "ui-file.h"
94af9270 24#include "gdb_obstack.h"
ad960ed2 25#include "gdb_select.h"
614c279d 26#include "filestuff.h"
d9fcf2fb 27
d9fcf2fb
JM
28static ui_file_isatty_ftype null_file_isatty;
29static ui_file_write_ftype null_file_write;
01124a23 30static ui_file_write_ftype null_file_write_async_safe;
d9fcf2fb 31static ui_file_fputs_ftype null_file_fputs;
449092f6 32static ui_file_read_ftype null_file_read;
d9fcf2fb
JM
33static ui_file_flush_ftype null_file_flush;
34static ui_file_delete_ftype null_file_delete;
35static ui_file_rewind_ftype null_file_rewind;
36static ui_file_put_ftype null_file_put;
2a9d5ccf 37static ui_file_fseek_ftype null_file_fseek;
d9fcf2fb
JM
38
39struct ui_file
40 {
41 int *magic;
42 ui_file_flush_ftype *to_flush;
43 ui_file_write_ftype *to_write;
01124a23 44 ui_file_write_async_safe_ftype *to_write_async_safe;
d9fcf2fb 45 ui_file_fputs_ftype *to_fputs;
449092f6 46 ui_file_read_ftype *to_read;
d9fcf2fb
JM
47 ui_file_delete_ftype *to_delete;
48 ui_file_isatty_ftype *to_isatty;
49 ui_file_rewind_ftype *to_rewind;
50 ui_file_put_ftype *to_put;
2a9d5ccf 51 ui_file_fseek_ftype *to_fseek;
d9fcf2fb
JM
52 void *to_data;
53 };
54int ui_file_magic;
55
56struct ui_file *
fba45db2 57ui_file_new (void)
d9fcf2fb 58{
8d749320 59 struct ui_file *file = XNEW (struct ui_file);
5d502164 60
d9fcf2fb
JM
61 file->magic = &ui_file_magic;
62 set_ui_file_data (file, NULL, null_file_delete);
63 set_ui_file_flush (file, null_file_flush);
64 set_ui_file_write (file, null_file_write);
01124a23 65 set_ui_file_write_async_safe (file, null_file_write_async_safe);
d9fcf2fb 66 set_ui_file_fputs (file, null_file_fputs);
449092f6 67 set_ui_file_read (file, null_file_read);
d9fcf2fb
JM
68 set_ui_file_isatty (file, null_file_isatty);
69 set_ui_file_rewind (file, null_file_rewind);
70 set_ui_file_put (file, null_file_put);
2a9d5ccf 71 set_ui_file_fseek (file, null_file_fseek);
d9fcf2fb
JM
72 return file;
73}
74
75void
fba45db2 76ui_file_delete (struct ui_file *file)
d9fcf2fb
JM
77{
78 file->to_delete (file);
b8c9b27d 79 xfree (file);
d9fcf2fb
JM
80}
81
82static int
fba45db2 83null_file_isatty (struct ui_file *file)
d9fcf2fb
JM
84{
85 return 0;
86}
87
88static void
fba45db2 89null_file_rewind (struct ui_file *file)
d9fcf2fb
JM
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
fba45db2 103null_file_flush (struct ui_file *file)
d9fcf2fb
JM
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)
581e13c1
MS
114 /* Both the write and fputs methods are null. Discard the
115 request. */
d9fcf2fb
JM
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];
5d502164 126
d9fcf2fb
JM
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
449092f6
CV
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
d9fcf2fb 146static void
fba45db2 147null_file_fputs (const char *buf, struct ui_file *file)
d9fcf2fb
JM
148{
149 if (file->to_write == null_file_write)
581e13c1
MS
150 /* Both the write and fputs methods are null. Discard the
151 request. */
d9fcf2fb
JM
152 return;
153 else
154 {
581e13c1 155 /* The write method was implemented, use that. */
d9fcf2fb
JM
156 file->to_write (file, buf, strlen (buf));
157 }
158}
159
01124a23
DE
160static void
161null_file_write_async_safe (struct ui_file *file,
162 const char *buf,
163 long sizeof_buf)
164{
165 return;
166}
167
d9fcf2fb 168static void
fba45db2 169null_file_delete (struct ui_file *file)
d9fcf2fb
JM
170{
171 return;
172}
173
2a9d5ccf
HZ
174static int
175null_file_fseek (struct ui_file *stream, long offset, int whence)
176{
177 errno = EBADF;
178
179 return -1;
180}
181
d9fcf2fb 182void *
fba45db2 183ui_file_data (struct ui_file *file)
d9fcf2fb
JM
184{
185 if (file->magic != &ui_file_magic)
8e65ff28 186 internal_error (__FILE__, __LINE__,
e2e0b3e5 187 _("ui_file_data: bad magic number"));
d9fcf2fb
JM
188 return file->to_data;
189}
190
191void
fba45db2 192gdb_flush (struct ui_file *file)
d9fcf2fb
JM
193{
194 file->to_flush (file);
195}
196
197int
fba45db2 198ui_file_isatty (struct ui_file *file)
d9fcf2fb
JM
199{
200 return file->to_isatty (file);
201}
202
203void
fba45db2 204ui_file_rewind (struct ui_file *file)
d9fcf2fb
JM
205{
206 file->to_rewind (file);
207}
208
209void
210ui_file_put (struct ui_file *file,
211 ui_file_put_method_ftype *write,
212 void *dest)
213{
214 file->to_put (file, write, dest);
215}
216
217void
218ui_file_write (struct ui_file *file,
219 const char *buf,
220 long length_buf)
221{
222 file->to_write (file, buf, length_buf);
223}
224
de571fc5
TT
225void
226ui_file_write_for_put (void *data, const char *buffer, long length_buffer)
227{
19ba03f4 228 ui_file_write ((struct ui_file *) data, buffer, length_buffer);
de571fc5
TT
229}
230
01124a23
DE
231void
232ui_file_write_async_safe (struct ui_file *file,
233 const char *buf,
234 long length_buf)
235{
236 file->to_write_async_safe (file, buf, length_buf);
237}
238
449092f6
CV
239long
240ui_file_read (struct ui_file *file, char *buf, long length_buf)
241{
242 return file->to_read (file, buf, length_buf);
243}
244
2a9d5ccf
HZ
245int
246ui_file_fseek (struct ui_file *file, long offset, int whence)
247{
248 return file->to_fseek (file, offset, whence);
249}
250
d9fcf2fb 251void
fba45db2 252fputs_unfiltered (const char *buf, struct ui_file *file)
d9fcf2fb
JM
253{
254 file->to_fputs (buf, file);
255}
256
257void
3f453875 258set_ui_file_flush (struct ui_file *file, ui_file_flush_ftype *flush_ptr)
d9fcf2fb 259{
3f453875 260 file->to_flush = flush_ptr;
d9fcf2fb
JM
261}
262
263void
3f453875 264set_ui_file_isatty (struct ui_file *file, ui_file_isatty_ftype *isatty_ptr)
d9fcf2fb 265{
3f453875 266 file->to_isatty = isatty_ptr;
d9fcf2fb
JM
267}
268
269void
3f453875 270set_ui_file_rewind (struct ui_file *file, ui_file_rewind_ftype *rewind_ptr)
d9fcf2fb 271{
3f453875 272 file->to_rewind = rewind_ptr;
d9fcf2fb
JM
273}
274
275void
3f453875 276set_ui_file_put (struct ui_file *file, ui_file_put_ftype *put_ptr)
d9fcf2fb 277{
3f453875 278 file->to_put = put_ptr;
d9fcf2fb
JM
279}
280
281void
282set_ui_file_write (struct ui_file *file,
3f453875 283 ui_file_write_ftype *write_ptr)
d9fcf2fb 284{
3f453875 285 file->to_write = write_ptr;
d9fcf2fb
JM
286}
287
01124a23
DE
288void
289set_ui_file_write_async_safe (struct ui_file *file,
3f453875 290 ui_file_write_async_safe_ftype *write_async_safe_ptr)
01124a23 291{
3f453875 292 file->to_write_async_safe = write_async_safe_ptr;
01124a23
DE
293}
294
449092f6 295void
3f453875 296set_ui_file_read (struct ui_file *file, ui_file_read_ftype *read_ptr)
449092f6 297{
3f453875 298 file->to_read = read_ptr;
449092f6
CV
299}
300
d9fcf2fb 301void
3f453875 302set_ui_file_fputs (struct ui_file *file, ui_file_fputs_ftype *fputs_ptr)
d9fcf2fb 303{
3f453875 304 file->to_fputs = fputs_ptr;
d9fcf2fb
JM
305}
306
2a9d5ccf
HZ
307void
308set_ui_file_fseek (struct ui_file *file, ui_file_fseek_ftype *fseek_ptr)
309{
310 file->to_fseek = fseek_ptr;
311}
312
d9fcf2fb 313void
fba45db2 314set_ui_file_data (struct ui_file *file, void *data,
3f453875 315 ui_file_delete_ftype *delete_ptr)
d9fcf2fb
JM
316{
317 file->to_data = data;
3f453875 318 file->to_delete = delete_ptr;
d9fcf2fb
JM
319}
320
321/* ui_file utility function for converting a ``struct ui_file'' into
581e13c1 322 a memory buffer. */
d9fcf2fb
JM
323
324struct accumulated_ui_file
325{
326 char *buffer;
327 long length;
328};
329
330static void
331do_ui_file_xstrdup (void *context, const char *buffer, long length)
332{
19ba03f4 333 struct accumulated_ui_file *acc = (struct accumulated_ui_file *) context;
5d502164 334
d9fcf2fb 335 if (acc->buffer == NULL)
224c3ddb 336 acc->buffer = (char *) xmalloc (length + 1);
d9fcf2fb 337 else
224c3ddb 338 acc->buffer = (char *) xrealloc (acc->buffer, acc->length + length + 1);
d9fcf2fb
JM
339 memcpy (acc->buffer + acc->length, buffer, length);
340 acc->length += length;
341 acc->buffer[acc->length] = '\0';
342}
343
344char *
759ef836 345ui_file_xstrdup (struct ui_file *file, long *length)
d9fcf2fb
JM
346{
347 struct accumulated_ui_file acc;
5d502164 348
d9fcf2fb
JM
349 acc.buffer = NULL;
350 acc.length = 0;
351 ui_file_put (file, do_ui_file_xstrdup, &acc);
352 if (acc.buffer == NULL)
353 acc.buffer = xstrdup ("");
759ef836
PA
354 if (length != NULL)
355 *length = acc.length;
d9fcf2fb
JM
356 return acc.buffer;
357}
94af9270 358
8de00631
PA
359/* ui_file utility function for converting a ``struct ui_file'' into a
360 std:string. */
361
362static void
363do_ui_file_as_string (void *context, const char *buffer, long length)
364{
365 std::string *str = (std::string *) context;
366
367 *str = std::string (buffer, length);
368}
369
370/* See ui-file.h. */
371
372std::string
373ui_file_as_string (struct ui_file *file)
374{
375 std::string str;
376
377 ui_file_put (file, do_ui_file_as_string, &str);
378 return str;
379}
380
94af9270
KS
381static void
382do_ui_file_obsavestring (void *context, const char *buffer, long length)
383{
384 struct obstack *obstack = (struct obstack *) context;
5d502164 385
94af9270
KS
386 obstack_grow (obstack, buffer, length);
387}
388
389char *
390ui_file_obsavestring (struct ui_file *file, struct obstack *obstack,
391 long *length)
392{
393 ui_file_put (file, do_ui_file_obsavestring, obstack);
394 *length = obstack_object_size (obstack);
395 obstack_1grow (obstack, '\0');
224c3ddb 396 return (char *) obstack_finish (obstack);
94af9270 397}
d9fcf2fb
JM
398\f
399/* A pure memory based ``struct ui_file'' that can be used an output
581e13c1
MS
400 buffer. The buffers accumulated contents are available via
401 ui_file_put(). */
d9fcf2fb
JM
402
403struct mem_file
404 {
405 int *magic;
406 char *buffer;
407 int sizeof_buffer;
408 int length_buffer;
409 };
410
411static ui_file_rewind_ftype mem_file_rewind;
412static ui_file_put_ftype mem_file_put;
413static ui_file_write_ftype mem_file_write;
414static ui_file_delete_ftype mem_file_delete;
a14ed312 415static struct ui_file *mem_file_new (void);
d9fcf2fb
JM
416static int mem_file_magic;
417
418static struct ui_file *
419mem_file_new (void)
420{
70ba0933 421 struct mem_file *stream = XNEW (struct mem_file);
d9fcf2fb 422 struct ui_file *file = ui_file_new ();
5d502164 423
d9fcf2fb
JM
424 set_ui_file_data (file, stream, mem_file_delete);
425 set_ui_file_rewind (file, mem_file_rewind);
426 set_ui_file_put (file, mem_file_put);
427 set_ui_file_write (file, mem_file_write);
428 stream->magic = &mem_file_magic;
429 stream->buffer = NULL;
430 stream->sizeof_buffer = 0;
431 stream->length_buffer = 0;
432 return file;
433}
434
435static void
436mem_file_delete (struct ui_file *file)
437{
19ba03f4 438 struct mem_file *stream = (struct mem_file *) ui_file_data (file);
5d502164 439
d9fcf2fb 440 if (stream->magic != &mem_file_magic)
8e65ff28 441 internal_error (__FILE__, __LINE__,
e2e0b3e5 442 _("mem_file_delete: bad magic number"));
d9fcf2fb 443 if (stream->buffer != NULL)
b8c9b27d
KB
444 xfree (stream->buffer);
445 xfree (stream);
d9fcf2fb
JM
446}
447
448struct ui_file *
449mem_fileopen (void)
450{
451 return mem_file_new ();
452}
453
454static void
455mem_file_rewind (struct ui_file *file)
456{
19ba03f4 457 struct mem_file *stream = (struct mem_file *) ui_file_data (file);
5d502164 458
d9fcf2fb 459 if (stream->magic != &mem_file_magic)
8e65ff28 460 internal_error (__FILE__, __LINE__,
e2e0b3e5 461 _("mem_file_rewind: bad magic number"));
d9fcf2fb
JM
462 stream->length_buffer = 0;
463}
464
465static void
466mem_file_put (struct ui_file *file,
467 ui_file_put_method_ftype *write,
468 void *dest)
469{
19ba03f4 470 struct mem_file *stream = (struct mem_file *) ui_file_data (file);
5d502164 471
d9fcf2fb 472 if (stream->magic != &mem_file_magic)
8e65ff28 473 internal_error (__FILE__, __LINE__,
e2e0b3e5 474 _("mem_file_put: bad magic number"));
d9fcf2fb
JM
475 if (stream->length_buffer > 0)
476 write (dest, stream->buffer, stream->length_buffer);
477}
478
479void
480mem_file_write (struct ui_file *file,
481 const char *buffer,
482 long length_buffer)
483{
19ba03f4 484 struct mem_file *stream = (struct mem_file *) ui_file_data (file);
5d502164 485
d9fcf2fb 486 if (stream->magic != &mem_file_magic)
8e65ff28 487 internal_error (__FILE__, __LINE__,
e2e0b3e5 488 _("mem_file_write: bad magic number"));
d9fcf2fb
JM
489 if (stream->buffer == NULL)
490 {
491 stream->length_buffer = length_buffer;
492 stream->sizeof_buffer = length_buffer;
224c3ddb 493 stream->buffer = (char *) xmalloc (stream->sizeof_buffer);
d9fcf2fb
JM
494 memcpy (stream->buffer, buffer, length_buffer);
495 }
496 else
497 {
498 int new_length = stream->length_buffer + length_buffer;
5d502164 499
d9fcf2fb
JM
500 if (new_length >= stream->sizeof_buffer)
501 {
502 stream->sizeof_buffer = new_length;
224c3ddb
SM
503 stream->buffer
504 = (char *) xrealloc (stream->buffer, stream->sizeof_buffer);
d9fcf2fb
JM
505 }
506 memcpy (stream->buffer + stream->length_buffer, buffer, length_buffer);
507 stream->length_buffer = new_length;
508 }
509}
510\f
511/* ``struct ui_file'' implementation that maps directly onto
581e13c1 512 <stdio.h>'s FILE. */
d9fcf2fb
JM
513
514static ui_file_write_ftype stdio_file_write;
01124a23 515static ui_file_write_async_safe_ftype stdio_file_write_async_safe;
d9fcf2fb 516static ui_file_fputs_ftype stdio_file_fputs;
449092f6 517static ui_file_read_ftype stdio_file_read;
d9fcf2fb
JM
518static ui_file_isatty_ftype stdio_file_isatty;
519static ui_file_delete_ftype stdio_file_delete;
3e43a32a 520static struct ui_file *stdio_file_new (FILE *file, int close_p);
d9fcf2fb 521static ui_file_flush_ftype stdio_file_flush;
2a9d5ccf 522static ui_file_fseek_ftype stdio_file_fseek;
d9fcf2fb
JM
523
524static int stdio_file_magic;
525
526struct stdio_file
527 {
528 int *magic;
529 FILE *file;
01124a23
DE
530 /* The associated file descriptor is extracted ahead of time for
531 stdio_file_write_async_safe's benefit, in case fileno isn't async-safe. */
532 int fd;
d9fcf2fb
JM
533 int close_p;
534 };
535
536static struct ui_file *
fba45db2 537stdio_file_new (FILE *file, int close_p)
d9fcf2fb
JM
538{
539 struct ui_file *ui_file = ui_file_new ();
8d749320 540 struct stdio_file *stdio = XNEW (struct stdio_file);
5d502164 541
d9fcf2fb
JM
542 stdio->magic = &stdio_file_magic;
543 stdio->file = file;
01124a23 544 stdio->fd = fileno (file);
d9fcf2fb
JM
545 stdio->close_p = close_p;
546 set_ui_file_data (ui_file, stdio, stdio_file_delete);
547 set_ui_file_flush (ui_file, stdio_file_flush);
548 set_ui_file_write (ui_file, stdio_file_write);
01124a23 549 set_ui_file_write_async_safe (ui_file, stdio_file_write_async_safe);
d9fcf2fb 550 set_ui_file_fputs (ui_file, stdio_file_fputs);
449092f6 551 set_ui_file_read (ui_file, stdio_file_read);
d9fcf2fb 552 set_ui_file_isatty (ui_file, stdio_file_isatty);
2a9d5ccf 553 set_ui_file_fseek (ui_file, stdio_file_fseek);
d9fcf2fb
JM
554 return ui_file;
555}
556
557static void
fba45db2 558stdio_file_delete (struct ui_file *file)
d9fcf2fb 559{
19ba03f4 560 struct stdio_file *stdio = (struct stdio_file *) ui_file_data (file);
5d502164 561
d9fcf2fb 562 if (stdio->magic != &stdio_file_magic)
8e65ff28 563 internal_error (__FILE__, __LINE__,
e2e0b3e5 564 _("stdio_file_delete: bad magic number"));
d9fcf2fb
JM
565 if (stdio->close_p)
566 {
567 fclose (stdio->file);
568 }
b8c9b27d 569 xfree (stdio);
d9fcf2fb
JM
570}
571
572static void
fba45db2 573stdio_file_flush (struct ui_file *file)
d9fcf2fb 574{
19ba03f4 575 struct stdio_file *stdio = (struct stdio_file *) ui_file_data (file);
5d502164 576
d9fcf2fb 577 if (stdio->magic != &stdio_file_magic)
8e65ff28 578 internal_error (__FILE__, __LINE__,
e2e0b3e5 579 _("stdio_file_flush: bad magic number"));
d9fcf2fb
JM
580 fflush (stdio->file);
581}
582
449092f6
CV
583static long
584stdio_file_read (struct ui_file *file, char *buf, long length_buf)
585{
19ba03f4 586 struct stdio_file *stdio = (struct stdio_file *) ui_file_data (file);
5d502164 587
449092f6
CV
588 if (stdio->magic != &stdio_file_magic)
589 internal_error (__FILE__, __LINE__,
e2e0b3e5 590 _("stdio_file_read: bad magic number"));
ad960ed2 591
f0881b37
PA
592 /* Wait until at least one byte of data is available, or we get
593 interrupted with Control-C. */
ad960ed2 594 {
ad960ed2 595 fd_set readfds;
f0881b37 596
ad960ed2 597 FD_ZERO (&readfds);
01124a23 598 FD_SET (stdio->fd, &readfds);
f0881b37 599 if (interruptible_select (stdio->fd + 1, &readfds, NULL, NULL, NULL) == -1)
ad960ed2
DJ
600 return -1;
601 }
602
01124a23 603 return read (stdio->fd, buf, length_buf);
449092f6
CV
604}
605
d9fcf2fb
JM
606static void
607stdio_file_write (struct ui_file *file, const char *buf, long length_buf)
608{
19ba03f4 609 struct stdio_file *stdio = (struct stdio_file *) ui_file_data (file);
5d502164 610
d9fcf2fb 611 if (stdio->magic != &stdio_file_magic)
8e65ff28 612 internal_error (__FILE__, __LINE__,
e2e0b3e5 613 _("stdio_file_write: bad magic number"));
bf1d7d9c
JB
614 /* Calling error crashes when we are called from the exception framework. */
615 if (fwrite (buf, length_buf, 1, stdio->file))
d4fb63e1
TT
616 {
617 /* Nothing. */
618 }
d9fcf2fb
JM
619}
620
01124a23
DE
621static void
622stdio_file_write_async_safe (struct ui_file *file,
623 const char *buf, long length_buf)
624{
19ba03f4 625 struct stdio_file *stdio = (struct stdio_file *) ui_file_data (file);
01124a23
DE
626
627 if (stdio->magic != &stdio_file_magic)
628 {
629 /* gettext isn't necessarily async safe, so we can't use _("error message") here.
630 We could extract the correct translation ahead of time, but this is an extremely
631 rare event, and one of the other stdio_file_* routines will presumably catch
632 the problem anyway. For now keep it simple and ignore the error here. */
633 return;
634 }
635
9f7bc587
DE
636 /* This is written the way it is to avoid a warning from gcc about not using the
637 result of write (since it can be declared with attribute warn_unused_result).
638 Alas casting to void doesn't work for this. */
093cee7d 639 if (write (stdio->fd, buf, length_buf))
d4fb63e1
TT
640 {
641 /* Nothing. */
642 }
01124a23
DE
643}
644
d9fcf2fb 645static void
fba45db2 646stdio_file_fputs (const char *linebuffer, struct ui_file *file)
d9fcf2fb 647{
19ba03f4 648 struct stdio_file *stdio = (struct stdio_file *) ui_file_data (file);
5d502164 649
d9fcf2fb 650 if (stdio->magic != &stdio_file_magic)
8e65ff28 651 internal_error (__FILE__, __LINE__,
e2e0b3e5 652 _("stdio_file_fputs: bad magic number"));
bf1d7d9c
JB
653 /* Calling error crashes when we are called from the exception framework. */
654 if (fputs (linebuffer, stdio->file))
d4fb63e1
TT
655 {
656 /* Nothing. */
657 }
d9fcf2fb
JM
658}
659
660static int
fba45db2 661stdio_file_isatty (struct ui_file *file)
d9fcf2fb 662{
19ba03f4 663 struct stdio_file *stdio = (struct stdio_file *) ui_file_data (file);
5d502164 664
d9fcf2fb 665 if (stdio->magic != &stdio_file_magic)
8e65ff28 666 internal_error (__FILE__, __LINE__,
e2e0b3e5 667 _("stdio_file_isatty: bad magic number"));
01124a23 668 return (isatty (stdio->fd));
d9fcf2fb
JM
669}
670
2a9d5ccf
HZ
671static int
672stdio_file_fseek (struct ui_file *file, long offset, int whence)
673{
19ba03f4 674 struct stdio_file *stdio = (struct stdio_file *) ui_file_data (file);
2a9d5ccf
HZ
675
676 if (stdio->magic != &stdio_file_magic)
677 internal_error (__FILE__, __LINE__,
678 _("stdio_file_fseek: bad magic number"));
679
680 return fseek (stdio->file, offset, whence);
681}
682
ffa4ac95
YQ
683#ifdef __MINGW32__
684/* This is the implementation of ui_file method to_write for stderr.
685 gdb_stdout is flushed before writing to gdb_stderr. */
686
687static void
688stderr_file_write (struct ui_file *file, const char *buf, long length_buf)
689{
690 gdb_flush (gdb_stdout);
691 stdio_file_write (file, buf, length_buf);
692}
693
694/* This is the implementation of ui_file method to_fputs for stderr.
695 gdb_stdout is flushed before writing to gdb_stderr. */
696
697static void
698stderr_file_fputs (const char *linebuffer, struct ui_file *file)
699{
700 gdb_flush (gdb_stdout);
701 stdio_file_fputs (linebuffer, file);
702}
703#endif
704
705struct ui_file *
694ec099 706stderr_fileopen (FILE *stream)
ffa4ac95 707{
694ec099 708 struct ui_file *ui_file = stdio_fileopen (stream);
ffa4ac95
YQ
709
710#ifdef __MINGW32__
711 /* There is no real line-buffering on Windows, see
712 http://msdn.microsoft.com/en-us/library/86cebhfs%28v=vs.71%29.aspx
713 so the stdout is either fully-buffered or non-buffered. We can't
714 make stdout non-buffered, because of two concerns,
715 1. non-buffering hurts performance,
716 2. non-buffering may change GDB's behavior when it is interacting
717 with front-end, such as Emacs.
718
719 We decided to leave stdout as fully buffered, but flush it first
720 when something is written to stderr. */
721
722 /* Method 'to_write_async_safe' is not overwritten, because there's
723 no way to flush a stream in an async-safe manner. Fortunately,
724 it doesn't really matter, because:
725 - that method is only used for printing internal debug output
726 from signal handlers.
727 - Windows hosts don't have a concept of async-safeness. Signal
728 handlers run in a separate thread, so they can call
729 the regular non-async-safe output routines freely. */
730 set_ui_file_write (ui_file, stderr_file_write);
731 set_ui_file_fputs (ui_file, stderr_file_fputs);
732#endif
733
734 return ui_file;
735}
736
581e13c1 737/* Like fdopen(). Create a ui_file from a previously opened FILE. */
d9fcf2fb
JM
738
739struct ui_file *
fba45db2 740stdio_fileopen (FILE *file)
d9fcf2fb
JM
741{
742 return stdio_file_new (file, 0);
743}
744
745struct ui_file *
23b3a2c3 746gdb_fopen (const char *name, const char *mode)
d9fcf2fb 747{
614c279d 748 FILE *f = gdb_fopen_cloexec (name, mode);
5d502164 749
d9fcf2fb
JM
750 if (f == NULL)
751 return NULL;
752 return stdio_file_new (f, 1);
753}
e4c242d9
DJ
754
755/* ``struct ui_file'' implementation that maps onto two ui-file objects. */
756
757static ui_file_write_ftype tee_file_write;
758static ui_file_fputs_ftype tee_file_fputs;
759static ui_file_isatty_ftype tee_file_isatty;
760static ui_file_delete_ftype tee_file_delete;
761static ui_file_flush_ftype tee_file_flush;
762
763static int tee_file_magic;
764
765struct tee_file
766 {
767 int *magic;
768 struct ui_file *one, *two;
769 int close_one, close_two;
770 };
771
772struct ui_file *
773tee_file_new (struct ui_file *one, int close_one,
774 struct ui_file *two, int close_two)
775{
776 struct ui_file *ui_file = ui_file_new ();
8d749320 777 struct tee_file *tee = XNEW (struct tee_file);
5d502164 778
e4c242d9
DJ
779 tee->magic = &tee_file_magic;
780 tee->one = one;
781 tee->two = two;
782 tee->close_one = close_one;
783 tee->close_two = close_two;
784 set_ui_file_data (ui_file, tee, tee_file_delete);
785 set_ui_file_flush (ui_file, tee_file_flush);
786 set_ui_file_write (ui_file, tee_file_write);
787 set_ui_file_fputs (ui_file, tee_file_fputs);
788 set_ui_file_isatty (ui_file, tee_file_isatty);
789 return ui_file;
790}
791
792static void
793tee_file_delete (struct ui_file *file)
794{
19ba03f4 795 struct tee_file *tee = (struct tee_file *) ui_file_data (file);
5d502164 796
e4c242d9
DJ
797 if (tee->magic != &tee_file_magic)
798 internal_error (__FILE__, __LINE__,
e2e0b3e5 799 _("tee_file_delete: bad magic number"));
e4c242d9
DJ
800 if (tee->close_one)
801 ui_file_delete (tee->one);
802 if (tee->close_two)
803 ui_file_delete (tee->two);
804
805 xfree (tee);
806}
807
808static void
809tee_file_flush (struct ui_file *file)
810{
19ba03f4 811 struct tee_file *tee = (struct tee_file *) ui_file_data (file);
5d502164 812
e4c242d9
DJ
813 if (tee->magic != &tee_file_magic)
814 internal_error (__FILE__, __LINE__,
e2e0b3e5 815 _("tee_file_flush: bad magic number"));
e4c242d9
DJ
816 tee->one->to_flush (tee->one);
817 tee->two->to_flush (tee->two);
818}
819
820static void
821tee_file_write (struct ui_file *file, const char *buf, long length_buf)
822{
19ba03f4 823 struct tee_file *tee = (struct tee_file *) ui_file_data (file);
5d502164 824
e4c242d9
DJ
825 if (tee->magic != &tee_file_magic)
826 internal_error (__FILE__, __LINE__,
e2e0b3e5 827 _("tee_file_write: bad magic number"));
e4c242d9
DJ
828 ui_file_write (tee->one, buf, length_buf);
829 ui_file_write (tee->two, buf, length_buf);
830}
831
832static void
833tee_file_fputs (const char *linebuffer, struct ui_file *file)
834{
19ba03f4 835 struct tee_file *tee = (struct tee_file *) ui_file_data (file);
5d502164 836
e4c242d9
DJ
837 if (tee->magic != &tee_file_magic)
838 internal_error (__FILE__, __LINE__,
e2e0b3e5 839 _("tee_file_fputs: bad magic number"));
e4c242d9
DJ
840 tee->one->to_fputs (linebuffer, tee->one);
841 tee->two->to_fputs (linebuffer, tee->two);
842}
843
844static int
845tee_file_isatty (struct ui_file *file)
846{
19ba03f4 847 struct tee_file *tee = (struct tee_file *) ui_file_data (file);
5d502164 848
e4c242d9
DJ
849 if (tee->magic != &tee_file_magic)
850 internal_error (__FILE__, __LINE__,
e2e0b3e5 851 _("tee_file_isatty: bad magic number"));
172240dd
PA
852
853 return ui_file_isatty (tee->one);
e4c242d9 854}