1 /* UI_FILE - a generic STDIO like output stream.
2 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Implement the ``struct ui_file'' object. */
25 #include "gdb_string.h"
28 #define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
30 static ui_file_isatty_ftype null_file_isatty
;
31 static ui_file_write_ftype null_file_write
;
32 static ui_file_fputs_ftype null_file_fputs
;
33 static ui_file_flush_ftype null_file_flush
;
34 static ui_file_delete_ftype null_file_delete
;
35 static ui_file_rewind_ftype null_file_rewind
;
36 static ui_file_put_ftype null_file_put
;
41 ui_file_flush_ftype
*to_flush
;
42 ui_file_write_ftype
*to_write
;
43 ui_file_fputs_ftype
*to_fputs
;
44 ui_file_delete_ftype
*to_delete
;
45 ui_file_isatty_ftype
*to_isatty
;
46 ui_file_rewind_ftype
*to_rewind
;
47 ui_file_put_ftype
*to_put
;
55 struct ui_file
*file
= xmalloc (sizeof (struct ui_file
));
56 file
->magic
= &ui_file_magic
;
57 set_ui_file_data (file
, NULL
, null_file_delete
);
58 set_ui_file_flush (file
, null_file_flush
);
59 set_ui_file_write (file
, null_file_write
);
60 set_ui_file_fputs (file
, null_file_fputs
);
61 set_ui_file_isatty (file
, null_file_isatty
);
62 set_ui_file_rewind (file
, null_file_rewind
);
63 set_ui_file_put (file
, null_file_put
);
68 ui_file_delete (struct ui_file
*file
)
70 file
->to_delete (file
);
75 null_file_isatty (struct ui_file
*file
)
81 null_file_rewind (struct ui_file
*file
)
87 null_file_put (struct ui_file
*file
,
88 ui_file_put_method_ftype
*write
,
95 null_file_flush (struct ui_file
*file
)
101 null_file_write (struct ui_file
*file
,
105 if (file
->to_fputs
== null_file_fputs
)
106 /* Both the write and fputs methods are null. Discard the
111 /* The fputs method isn't null, slowly pass the write request
112 onto that. FYI, this isn't as bad as it may look - the
113 current (as of 1999-11-07) printf_* function calls fputc and
114 fputc does exactly the below. By having a write function it
115 is possible to clean up that code. */
119 for (i
= 0; i
< sizeof_buf
; i
++)
122 file
->to_fputs (b
, file
);
129 null_file_fputs (const char *buf
, struct ui_file
*file
)
131 if (file
->to_write
== null_file_write
)
132 /* Both the write and fputs methods are null. Discard the
137 /* The write method was implemented, use that. */
138 file
->to_write (file
, buf
, strlen (buf
));
143 null_file_delete (struct ui_file
*file
)
149 ui_file_data (struct ui_file
*file
)
151 if (file
->magic
!= &ui_file_magic
)
152 internal_error ("ui_file_data: bad magic number");
153 return file
->to_data
;
157 gdb_flush (struct ui_file
*file
)
159 file
->to_flush (file
);
163 ui_file_isatty (struct ui_file
*file
)
165 return file
->to_isatty (file
);
169 ui_file_rewind (struct ui_file
*file
)
171 file
->to_rewind (file
);
175 ui_file_put (struct ui_file
*file
,
176 ui_file_put_method_ftype
*write
,
179 file
->to_put (file
, write
, dest
);
183 ui_file_write (struct ui_file
*file
,
187 file
->to_write (file
, buf
, length_buf
);
191 fputs_unfiltered (const char *buf
, struct ui_file
*file
)
193 file
->to_fputs (buf
, file
);
197 set_ui_file_flush (struct ui_file
*file
, ui_file_flush_ftype
*flush
)
199 file
->to_flush
= flush
;
203 set_ui_file_isatty (struct ui_file
*file
, ui_file_isatty_ftype
*isatty
)
205 file
->to_isatty
= isatty
;
209 set_ui_file_rewind (struct ui_file
*file
, ui_file_rewind_ftype
*rewind
)
211 file
->to_rewind
= rewind
;
215 set_ui_file_put (struct ui_file
*file
, ui_file_put_ftype
*put
)
221 set_ui_file_write (struct ui_file
*file
,
222 ui_file_write_ftype
*write
)
224 file
->to_write
= write
;
228 set_ui_file_fputs (struct ui_file
*file
, ui_file_fputs_ftype
*fputs
)
230 file
->to_fputs
= fputs
;
234 set_ui_file_data (struct ui_file
*file
, void *data
,
235 ui_file_delete_ftype
*delete)
237 file
->to_data
= data
;
238 file
->to_delete
= delete;
241 /* ui_file utility function for converting a ``struct ui_file'' into
242 a memory buffer''. */
244 struct accumulated_ui_file
251 do_ui_file_xstrdup (void *context
, const char *buffer
, long length
)
253 struct accumulated_ui_file
*acc
= context
;
254 if (acc
->buffer
== NULL
)
255 acc
->buffer
= xmalloc (length
+ 1);
257 acc
->buffer
= xrealloc (acc
->buffer
, acc
->length
+ length
+ 1);
258 memcpy (acc
->buffer
+ acc
->length
, buffer
, length
);
259 acc
->length
+= length
;
260 acc
->buffer
[acc
->length
] = '\0';
264 ui_file_xstrdup (struct ui_file
*file
,
267 struct accumulated_ui_file acc
;
270 ui_file_put (file
, do_ui_file_xstrdup
, &acc
);
271 if (acc
.buffer
== NULL
)
272 acc
.buffer
= xstrdup ("");
273 *length
= acc
.length
;
277 /* A pure memory based ``struct ui_file'' that can be used an output
278 buffer. The buffers accumulated contents are available via
289 static ui_file_rewind_ftype mem_file_rewind
;
290 static ui_file_put_ftype mem_file_put
;
291 static ui_file_write_ftype mem_file_write
;
292 static ui_file_delete_ftype mem_file_delete
;
293 static struct ui_file
*mem_file_new (void);
294 static int mem_file_magic
;
296 static struct ui_file
*
299 struct mem_file
*stream
= XMALLOC (struct mem_file
);
300 struct ui_file
*file
= ui_file_new ();
301 set_ui_file_data (file
, stream
, mem_file_delete
);
302 set_ui_file_rewind (file
, mem_file_rewind
);
303 set_ui_file_put (file
, mem_file_put
);
304 set_ui_file_write (file
, mem_file_write
);
305 stream
->magic
= &mem_file_magic
;
306 stream
->buffer
= NULL
;
307 stream
->sizeof_buffer
= 0;
308 stream
->length_buffer
= 0;
313 mem_file_delete (struct ui_file
*file
)
315 struct mem_file
*stream
= ui_file_data (file
);
316 if (stream
->magic
!= &mem_file_magic
)
317 internal_error ("mem_file_delete: bad magic number");
318 if (stream
->buffer
!= NULL
)
319 free (stream
->buffer
);
326 return mem_file_new ();
330 mem_file_rewind (struct ui_file
*file
)
332 struct mem_file
*stream
= ui_file_data (file
);
333 if (stream
->magic
!= &mem_file_magic
)
334 internal_error ("mem_file_rewind: bad magic number");
335 stream
->length_buffer
= 0;
339 mem_file_put (struct ui_file
*file
,
340 ui_file_put_method_ftype
*write
,
343 struct mem_file
*stream
= ui_file_data (file
);
344 if (stream
->magic
!= &mem_file_magic
)
345 internal_error ("mem_file_put: bad magic number");
346 if (stream
->length_buffer
> 0)
347 write (dest
, stream
->buffer
, stream
->length_buffer
);
351 mem_file_write (struct ui_file
*file
,
355 struct mem_file
*stream
= ui_file_data (file
);
356 if (stream
->magic
!= &mem_file_magic
)
357 internal_error ("mem_file_write: bad magic number");
358 if (stream
->buffer
== NULL
)
360 stream
->length_buffer
= length_buffer
;
361 stream
->sizeof_buffer
= length_buffer
;
362 stream
->buffer
= xmalloc (stream
->sizeof_buffer
);
363 memcpy (stream
->buffer
, buffer
, length_buffer
);
367 int new_length
= stream
->length_buffer
+ length_buffer
;
368 if (new_length
>= stream
->sizeof_buffer
)
370 stream
->sizeof_buffer
= new_length
;
371 stream
->buffer
= xrealloc (stream
->buffer
, stream
->sizeof_buffer
);
373 memcpy (stream
->buffer
+ stream
->length_buffer
, buffer
, length_buffer
);
374 stream
->length_buffer
= new_length
;
378 /* ``struct ui_file'' implementation that maps directly onto
381 static ui_file_write_ftype stdio_file_write
;
382 static ui_file_fputs_ftype stdio_file_fputs
;
383 static ui_file_isatty_ftype stdio_file_isatty
;
384 static ui_file_delete_ftype stdio_file_delete
;
385 static struct ui_file
*stdio_file_new (FILE * file
, int close_p
);
386 static ui_file_flush_ftype stdio_file_flush
;
388 static int stdio_file_magic
;
397 static struct ui_file
*
398 stdio_file_new (FILE *file
, int close_p
)
400 struct ui_file
*ui_file
= ui_file_new ();
401 struct stdio_file
*stdio
= xmalloc (sizeof (struct stdio_file
));
402 stdio
->magic
= &stdio_file_magic
;
404 stdio
->close_p
= close_p
;
405 set_ui_file_data (ui_file
, stdio
, stdio_file_delete
);
406 set_ui_file_flush (ui_file
, stdio_file_flush
);
407 set_ui_file_write (ui_file
, stdio_file_write
);
408 set_ui_file_fputs (ui_file
, stdio_file_fputs
);
409 set_ui_file_isatty (ui_file
, stdio_file_isatty
);
414 stdio_file_delete (struct ui_file
*file
)
416 struct stdio_file
*stdio
= ui_file_data (file
);
417 if (stdio
->magic
!= &stdio_file_magic
)
418 internal_error ("stdio_file_delete: bad magic number");
421 fclose (stdio
->file
);
427 stdio_file_flush (struct ui_file
*file
)
429 struct stdio_file
*stdio
= ui_file_data (file
);
430 if (stdio
->magic
!= &stdio_file_magic
)
431 internal_error ("stdio_file_flush: bad magic number");
432 fflush (stdio
->file
);
436 stdio_file_write (struct ui_file
*file
, const char *buf
, long length_buf
)
438 struct stdio_file
*stdio
= ui_file_data (file
);
439 if (stdio
->magic
!= &stdio_file_magic
)
440 internal_error ("stdio_file_write: bad magic number");
441 fwrite (buf
, length_buf
, 1, stdio
->file
);
445 stdio_file_fputs (const char *linebuffer
, struct ui_file
*file
)
447 struct stdio_file
*stdio
= ui_file_data (file
);
448 if (stdio
->magic
!= &stdio_file_magic
)
449 internal_error ("stdio_file_fputs: bad magic number");
450 fputs (linebuffer
, stdio
->file
);
454 stdio_file_isatty (struct ui_file
*file
)
456 struct stdio_file
*stdio
= ui_file_data (file
);
457 if (stdio
->magic
!= &stdio_file_magic
)
458 internal_error ("stdio_file_isatty: bad magic number");
459 return (isatty (fileno (stdio
->file
)));
462 /* Like fdopen(). Create a ui_file from a previously opened FILE. */
465 stdio_fileopen (FILE *file
)
467 return stdio_file_new (file
, 0);
471 gdb_fopen (char *name
, char *mode
)
473 FILE *f
= fopen (name
, mode
);
476 return stdio_file_new (f
, 1);