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