1
/* UI_FILE - a generic STDIO like output stream.
3
Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4
Portions Copyright (C) 2001, 2002 Mission Critical Linux, Inc.
5
Copyright (c) 2002, 2003, 2004, 2005 Red Hat, Inc. All rights reserved.
7
This file is part of GDB.
9
This program is free software; you can redistribute it and/or modify
10
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation; either version 2 of the License, or
12
(at your option) any later version.
14
This program is distributed in the hope that it will be useful,
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
GNU General Public License for more details.
19
You should have received a copy of the GNU General Public License
20
along with this program; if not, write to the Free Software
21
Foundation, Inc., 59 Temple Place - Suite 330,
22
Boston, MA 02111-1307, USA. */
24
/* Implement the ``struct ui_file'' object. */
28
#include "gdb_string.h"
32
static ui_file_isatty_ftype null_file_isatty;
33
static ui_file_write_ftype null_file_write;
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;
44
ui_file_flush_ftype *to_flush;
45
ui_file_write_ftype *to_write;
46
ui_file_fputs_ftype *to_fputs;
47
ui_file_read_ftype *to_read;
48
ui_file_delete_ftype *to_delete;
49
ui_file_isatty_ftype *to_isatty;
50
ui_file_rewind_ftype *to_rewind;
51
ui_file_put_ftype *to_put;
59
struct ui_file *file = xmalloc (sizeof (struct ui_file));
60
file->magic = &ui_file_magic;
61
set_ui_file_data (file, NULL, null_file_delete);
62
set_ui_file_flush (file, null_file_flush);
63
set_ui_file_write (file, null_file_write);
64
set_ui_file_fputs (file, null_file_fputs);
65
set_ui_file_read (file, null_file_read);
66
set_ui_file_isatty (file, null_file_isatty);
67
set_ui_file_rewind (file, null_file_rewind);
68
set_ui_file_put (file, null_file_put);
73
ui_file_delete (struct ui_file *file)
75
file->to_delete (file);
80
null_file_isatty (struct ui_file *file)
86
null_file_rewind (struct ui_file *file)
92
null_file_put (struct ui_file *file,
93
ui_file_put_method_ftype *write,
100
null_file_flush (struct ui_file *file)
106
null_file_write (struct ui_file *file,
110
if (file->to_fputs == null_file_fputs)
111
/* Both the write and fputs methods are null. Discard the
116
/* The fputs method isn't null, slowly pass the write request
117
onto that. FYI, this isn't as bad as it may look - the
118
current (as of 1999-11-07) printf_* function calls fputc and
119
fputc does exactly the below. By having a write function it
120
is possible to clean up that code. */
124
for (i = 0; i < sizeof_buf; i++)
127
file->to_fputs (b, file);
134
null_file_read (struct ui_file *file,
143
null_file_fputs (const char *buf, struct ui_file *file)
145
if (file->to_write == null_file_write)
146
/* Both the write and fputs methods are null. Discard the
151
/* The write method was implemented, use that. */
152
file->to_write (file, buf, strlen (buf));
157
null_file_delete (struct ui_file *file)
163
ui_file_data (struct ui_file *file)
165
if (file->magic != &ui_file_magic)
166
internal_error (__FILE__, __LINE__,
167
"ui_file_data: bad magic number");
168
return file->to_data;
172
gdb_flush (struct ui_file *file)
174
file->to_flush (file);
178
ui_file_isatty (struct ui_file *file)
180
return file->to_isatty (file);
184
ui_file_rewind (struct ui_file *file)
186
file->to_rewind (file);
190
ui_file_put (struct ui_file *file,
191
ui_file_put_method_ftype *write,
194
file->to_put (file, write, dest);
198
ui_file_write (struct ui_file *file,
202
file->to_write (file, buf, length_buf);
206
ui_file_read (struct ui_file *file, char *buf, long length_buf)
208
return file->to_read (file, buf, length_buf);
212
fputs_unfiltered (const char *buf, struct ui_file *file)
214
file->to_fputs (buf, file);
218
set_ui_file_flush (struct ui_file *file, ui_file_flush_ftype *flush)
220
file->to_flush = flush;
224
set_ui_file_isatty (struct ui_file *file, ui_file_isatty_ftype *isatty)
226
file->to_isatty = isatty;
230
set_ui_file_rewind (struct ui_file *file, ui_file_rewind_ftype *rewind)
232
file->to_rewind = rewind;
236
set_ui_file_put (struct ui_file *file, ui_file_put_ftype *put)
242
set_ui_file_write (struct ui_file *file,
243
ui_file_write_ftype *write)
245
file->to_write = write;
249
set_ui_file_read (struct ui_file *file, ui_file_read_ftype *read)
251
file->to_read = read;
255
set_ui_file_fputs (struct ui_file *file, ui_file_fputs_ftype *fputs)
257
file->to_fputs = fputs;
261
set_ui_file_data (struct ui_file *file, void *data,
262
ui_file_delete_ftype *delete)
264
file->to_data = data;
265
file->to_delete = delete;
268
/* ui_file utility function for converting a ``struct ui_file'' into
269
a memory buffer''. */
271
struct accumulated_ui_file
278
do_ui_file_xstrdup (void *context, const char *buffer, long length)
280
struct accumulated_ui_file *acc = context;
281
if (acc->buffer == NULL)
282
acc->buffer = xmalloc (length + 1);
284
acc->buffer = xrealloc (acc->buffer, acc->length + length + 1);
285
memcpy (acc->buffer + acc->length, buffer, length);
286
acc->length += length;
287
acc->buffer[acc->length] = '\0';
291
ui_file_xstrdup (struct ui_file *file,
294
struct accumulated_ui_file acc;
297
ui_file_put (file, do_ui_file_xstrdup, &acc);
298
if (acc.buffer == NULL)
299
acc.buffer = xstrdup ("");
300
*length = acc.length;
304
/* A pure memory based ``struct ui_file'' that can be used an output
305
buffer. The buffers accumulated contents are available via
316
static ui_file_rewind_ftype mem_file_rewind;
317
static ui_file_put_ftype mem_file_put;
318
static ui_file_write_ftype mem_file_write;
319
static ui_file_delete_ftype mem_file_delete;
320
static struct ui_file *mem_file_new (void);
321
static int mem_file_magic;
323
static struct ui_file *
326
struct mem_file *stream = XMALLOC (struct mem_file);
327
struct ui_file *file = ui_file_new ();
328
set_ui_file_data (file, stream, mem_file_delete);
329
set_ui_file_rewind (file, mem_file_rewind);
330
set_ui_file_put (file, mem_file_put);
331
set_ui_file_write (file, mem_file_write);
332
stream->magic = &mem_file_magic;
333
stream->buffer = NULL;
334
stream->sizeof_buffer = 0;
335
stream->length_buffer = 0;
340
mem_file_delete (struct ui_file *file)
342
struct mem_file *stream = ui_file_data (file);
343
if (stream->magic != &mem_file_magic)
344
internal_error (__FILE__, __LINE__,
345
"mem_file_delete: bad magic number");
346
if (stream->buffer != NULL)
347
xfree (stream->buffer);
354
return mem_file_new ();
358
mem_file_rewind (struct ui_file *file)
360
struct mem_file *stream = ui_file_data (file);
361
if (stream->magic != &mem_file_magic)
362
internal_error (__FILE__, __LINE__,
363
"mem_file_rewind: bad magic number");
364
stream->length_buffer = 0;
368
mem_file_put (struct ui_file *file,
369
ui_file_put_method_ftype *write,
372
struct mem_file *stream = ui_file_data (file);
373
if (stream->magic != &mem_file_magic)
374
internal_error (__FILE__, __LINE__,
375
"mem_file_put: bad magic number");
376
if (stream->length_buffer > 0)
377
write (dest, stream->buffer, stream->length_buffer);
381
mem_file_write (struct ui_file *file,
385
struct mem_file *stream = ui_file_data (file);
386
if (stream->magic != &mem_file_magic)
387
internal_error (__FILE__, __LINE__,
388
"mem_file_write: bad magic number");
389
if (stream->buffer == NULL)
391
stream->length_buffer = length_buffer;
392
stream->sizeof_buffer = length_buffer;
393
stream->buffer = xmalloc (stream->sizeof_buffer);
394
memcpy (stream->buffer, buffer, length_buffer);
398
int new_length = stream->length_buffer + length_buffer;
399
if (new_length >= stream->sizeof_buffer)
401
stream->sizeof_buffer = new_length;
402
stream->buffer = xrealloc (stream->buffer, stream->sizeof_buffer);
404
memcpy (stream->buffer + stream->length_buffer, buffer, length_buffer);
405
stream->length_buffer = new_length;
409
/* ``struct ui_file'' implementation that maps directly onto
412
static ui_file_write_ftype stdio_file_write;
413
static ui_file_fputs_ftype stdio_file_fputs;
414
static ui_file_read_ftype stdio_file_read;
415
static ui_file_isatty_ftype stdio_file_isatty;
416
static ui_file_delete_ftype stdio_file_delete;
417
static struct ui_file *stdio_file_new (FILE * file, int close_p);
418
static ui_file_flush_ftype stdio_file_flush;
420
static int stdio_file_magic;
429
static struct ui_file *
430
stdio_file_new (FILE *file, int close_p)
432
struct ui_file *ui_file = ui_file_new ();
433
struct stdio_file *stdio = xmalloc (sizeof (struct stdio_file));
434
stdio->magic = &stdio_file_magic;
436
stdio->close_p = close_p;
437
set_ui_file_data (ui_file, stdio, stdio_file_delete);
438
set_ui_file_flush (ui_file, stdio_file_flush);
439
set_ui_file_write (ui_file, stdio_file_write);
440
set_ui_file_fputs (ui_file, stdio_file_fputs);
441
set_ui_file_read (ui_file, stdio_file_read);
442
set_ui_file_isatty (ui_file, stdio_file_isatty);
447
stdio_file_delete (struct ui_file *file)
449
struct stdio_file *stdio = ui_file_data (file);
450
if (stdio->magic != &stdio_file_magic)
451
internal_error (__FILE__, __LINE__,
452
"stdio_file_delete: bad magic number");
455
fclose (stdio->file);
461
stdio_file_flush (struct ui_file *file)
463
struct stdio_file *stdio = ui_file_data (file);
464
if (stdio->magic != &stdio_file_magic)
465
internal_error (__FILE__, __LINE__,
466
"stdio_file_flush: bad magic number");
467
fflush (stdio->file);
471
stdio_file_read (struct ui_file *file, char *buf, long length_buf)
473
struct stdio_file *stdio = ui_file_data (file);
474
if (stdio->magic != &stdio_file_magic)
475
internal_error (__FILE__, __LINE__,
476
"stdio_file_read: bad magic number");
477
return read (fileno (stdio->file), buf, length_buf);
481
stdio_file_write (struct ui_file *file, const char *buf, long length_buf)
483
struct stdio_file *stdio = ui_file_data (file);
484
if (stdio->magic != &stdio_file_magic)
485
internal_error (__FILE__, __LINE__,
486
"stdio_file_write: bad magic number");
487
fwrite (buf, length_buf, 1, stdio->file);
491
stdio_file_fputs (const char *linebuffer, struct ui_file *file)
493
struct stdio_file *stdio = ui_file_data (file);
494
if (stdio->magic != &stdio_file_magic)
495
internal_error (__FILE__, __LINE__,
496
"stdio_file_fputs: bad magic number");
497
fputs (linebuffer, stdio->file);
501
stdio_file_isatty (struct ui_file *file)
503
struct stdio_file *stdio = ui_file_data (file);
504
if (stdio->magic != &stdio_file_magic)
505
internal_error (__FILE__, __LINE__,
506
"stdio_file_isatty: bad magic number");
507
return (isatty (fileno (stdio->file)));
510
/* Like fdopen(). Create a ui_file from a previously opened FILE. */
513
stdio_fileopen (FILE *file)
515
return stdio_file_new (file, 0);
519
gdb_fopen (char *name, char *mode)
521
FILE *f = fopen (name, mode);
524
return stdio_file_new (f, 1);
529
replace_ui_file_FILE(struct ui_file *file, FILE *fp)
531
struct stdio_file *stdio_file;
533
stdio_file = (struct stdio_file *)ui_file_data(file);
534
stdio_file->file = fp;
538
/* ``struct ui_file'' implementation that maps onto two ui-file objects. */
540
static ui_file_write_ftype tee_file_write;
541
static ui_file_fputs_ftype tee_file_fputs;
542
static ui_file_isatty_ftype tee_file_isatty;
543
static ui_file_delete_ftype tee_file_delete;
544
static ui_file_flush_ftype tee_file_flush;
546
static int tee_file_magic;
551
struct ui_file *one, *two;
552
int close_one, close_two;
556
tee_file_new (struct ui_file *one, int close_one,
557
struct ui_file *two, int close_two)
559
struct ui_file *ui_file = ui_file_new ();
560
struct tee_file *tee = xmalloc (sizeof (struct tee_file));
561
tee->magic = &tee_file_magic;
564
tee->close_one = close_one;
565
tee->close_two = close_two;
566
set_ui_file_data (ui_file, tee, tee_file_delete);
567
set_ui_file_flush (ui_file, tee_file_flush);
568
set_ui_file_write (ui_file, tee_file_write);
569
set_ui_file_fputs (ui_file, tee_file_fputs);
570
set_ui_file_isatty (ui_file, tee_file_isatty);
575
tee_file_delete (struct ui_file *file)
577
struct tee_file *tee = ui_file_data (file);
578
if (tee->magic != &tee_file_magic)
579
internal_error (__FILE__, __LINE__,
580
"tee_file_delete: bad magic number");
582
ui_file_delete (tee->one);
584
ui_file_delete (tee->two);
590
tee_file_flush (struct ui_file *file)
592
struct tee_file *tee = ui_file_data (file);
593
if (tee->magic != &tee_file_magic)
594
internal_error (__FILE__, __LINE__,
595
"tee_file_flush: bad magic number");
596
tee->one->to_flush (tee->one);
597
tee->two->to_flush (tee->two);
601
tee_file_write (struct ui_file *file, const char *buf, long length_buf)
603
struct tee_file *tee = ui_file_data (file);
604
if (tee->magic != &tee_file_magic)
605
internal_error (__FILE__, __LINE__,
606
"tee_file_write: bad magic number");
607
ui_file_write (tee->one, buf, length_buf);
608
ui_file_write (tee->two, buf, length_buf);
612
tee_file_fputs (const char *linebuffer, struct ui_file *file)
614
struct tee_file *tee = ui_file_data (file);
615
if (tee->magic != &tee_file_magic)
616
internal_error (__FILE__, __LINE__,
617
"tee_file_fputs: bad magic number");
618
tee->one->to_fputs (linebuffer, tee->one);
619
tee->two->to_fputs (linebuffer, tee->two);
623
tee_file_isatty (struct ui_file *file)
625
struct tee_file *tee = ui_file_data (file);
626
if (tee->magic != &tee_file_magic)
627
internal_error (__FILE__, __LINE__,
628
"tee_file_isatty: bad magic number");