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
);
71 file
->to_delete (file
);
76 null_file_isatty (file
)
83 null_file_rewind (file
)
90 null_file_put (struct ui_file
*file
,
91 ui_file_put_method_ftype
*write
,
98 null_file_flush (file
)
105 null_file_write (struct ui_file
*file
,
109 if (file
->to_fputs
== null_file_fputs
)
110 /* Both the write and fputs methods are null. Discard the
115 /* The fputs method isn't null, slowly pass the write request
116 onto that. FYI, this isn't as bad as it may look - the
117 current (as of 1999-11-07) printf_* function calls fputc and
118 fputc does exactly the below. By having a write function it
119 is possible to clean up that code. */
123 for (i
= 0; i
< sizeof_buf
; i
++)
126 file
->to_fputs (b
, file
);
133 null_file_fputs (buf
, file
)
135 struct ui_file
*file
;
137 if (file
->to_write
== null_file_write
)
138 /* Both the write and fputs methods are null. Discard the
143 /* The write method was implemented, use that. */
144 file
->to_write (file
, buf
, strlen (buf
));
149 null_file_delete (file
)
150 struct ui_file
*file
;
157 struct ui_file
*file
;
159 if (file
->magic
!= &ui_file_magic
)
160 internal_error ("ui_file_data: bad magic number");
161 return file
->to_data
;
166 struct ui_file
*file
;
168 file
->to_flush (file
);
172 ui_file_isatty (file
)
173 struct ui_file
*file
;
175 return file
->to_isatty (file
);
179 ui_file_rewind (file
)
180 struct ui_file
*file
;
182 file
->to_rewind (file
);
186 ui_file_put (struct ui_file
*file
,
187 ui_file_put_method_ftype
*write
,
190 file
->to_put (file
, write
, dest
);
194 ui_file_write (struct ui_file
*file
,
198 file
->to_write (file
, buf
, length_buf
);
202 fputs_unfiltered (buf
, file
)
204 struct ui_file
*file
;
206 file
->to_fputs (buf
, file
);
210 set_ui_file_flush (file
, flush
)
211 struct ui_file
*file
;
212 ui_file_flush_ftype
*flush
;
214 file
->to_flush
= flush
;
218 set_ui_file_isatty (file
, isatty
)
219 struct ui_file
*file
;
220 ui_file_isatty_ftype
*isatty
;
222 file
->to_isatty
= isatty
;
226 set_ui_file_rewind (file
, rewind
)
227 struct ui_file
*file
;
228 ui_file_rewind_ftype
*rewind
;
230 file
->to_rewind
= rewind
;
234 set_ui_file_put (file
, put
)
235 struct ui_file
*file
;
236 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_fputs (file
, fputs
)
250 struct ui_file
*file
;
251 ui_file_fputs_ftype
*fputs
;
253 file
->to_fputs
= fputs
;
257 set_ui_file_data (file
, data
, delete)
258 struct ui_file
*file
;
260 ui_file_delete_ftype
*delete;
262 file
->to_data
= data
;
263 file
->to_delete
= delete;
266 /* ui_file utility function for converting a ``struct ui_file'' into
267 a memory buffer''. */
269 struct accumulated_ui_file
276 do_ui_file_xstrdup (void *context
, const char *buffer
, long length
)
278 struct accumulated_ui_file
*acc
= context
;
279 if (acc
->buffer
== NULL
)
280 acc
->buffer
= xmalloc (length
+ 1);
282 acc
->buffer
= xrealloc (acc
->buffer
, acc
->length
+ length
+ 1);
283 memcpy (acc
->buffer
+ acc
->length
, buffer
, length
);
284 acc
->length
+= length
;
285 acc
->buffer
[acc
->length
] = '\0';
289 ui_file_xstrdup (struct ui_file
*file
,
292 struct accumulated_ui_file acc
;
295 ui_file_put (file
, do_ui_file_xstrdup
, &acc
);
296 if (acc
.buffer
== NULL
)
297 acc
.buffer
= xstrdup ("");
298 *length
= acc
.length
;
302 /* A pure memory based ``struct ui_file'' that can be used an output
303 buffer. The buffers accumulated contents are available via
314 static ui_file_rewind_ftype mem_file_rewind
;
315 static ui_file_put_ftype mem_file_put
;
316 static ui_file_write_ftype mem_file_write
;
317 static ui_file_delete_ftype mem_file_delete
;
318 static struct ui_file
*mem_file_new
PARAMS ((void));
319 static int mem_file_magic
;
321 static struct ui_file
*
324 struct mem_file
*stream
= XMALLOC (struct mem_file
);
325 struct ui_file
*file
= ui_file_new ();
326 set_ui_file_data (file
, stream
, mem_file_delete
);
327 set_ui_file_rewind (file
, mem_file_rewind
);
328 set_ui_file_put (file
, mem_file_put
);
329 set_ui_file_write (file
, mem_file_write
);
330 stream
->magic
= &mem_file_magic
;
331 stream
->buffer
= NULL
;
332 stream
->sizeof_buffer
= 0;
333 stream
->length_buffer
= 0;
338 mem_file_delete (struct ui_file
*file
)
340 struct mem_file
*stream
= ui_file_data (file
);
341 if (stream
->magic
!= &mem_file_magic
)
342 internal_error ("mem_file_delete: bad magic number");
343 if (stream
->buffer
!= NULL
)
344 free (stream
->buffer
);
351 return mem_file_new ();
355 mem_file_rewind (struct ui_file
*file
)
357 struct mem_file
*stream
= ui_file_data (file
);
358 if (stream
->magic
!= &mem_file_magic
)
359 internal_error ("mem_file_rewind: bad magic number");
360 stream
->length_buffer
= 0;
364 mem_file_put (struct ui_file
*file
,
365 ui_file_put_method_ftype
*write
,
368 struct mem_file
*stream
= ui_file_data (file
);
369 if (stream
->magic
!= &mem_file_magic
)
370 internal_error ("mem_file_put: bad magic number");
371 if (stream
->length_buffer
> 0)
372 write (dest
, stream
->buffer
, stream
->length_buffer
);
376 mem_file_write (struct ui_file
*file
,
380 struct mem_file
*stream
= ui_file_data (file
);
381 if (stream
->magic
!= &mem_file_magic
)
382 internal_error ("mem_file_write: bad magic number");
383 if (stream
->buffer
== NULL
)
385 stream
->length_buffer
= length_buffer
;
386 stream
->sizeof_buffer
= length_buffer
;
387 stream
->buffer
= xmalloc (stream
->sizeof_buffer
);
388 memcpy (stream
->buffer
, buffer
, length_buffer
);
392 int new_length
= stream
->length_buffer
+ length_buffer
;
393 if (new_length
>= stream
->sizeof_buffer
)
395 stream
->sizeof_buffer
= new_length
;
396 stream
->buffer
= xrealloc (stream
->buffer
, stream
->sizeof_buffer
);
398 memcpy (stream
->buffer
+ stream
->length_buffer
, buffer
, length_buffer
);
399 stream
->length_buffer
= new_length
;
403 /* ``struct ui_file'' implementation that maps directly onto
406 static ui_file_write_ftype stdio_file_write
;
407 static ui_file_fputs_ftype stdio_file_fputs
;
408 static ui_file_isatty_ftype stdio_file_isatty
;
409 static ui_file_delete_ftype stdio_file_delete
;
410 static struct ui_file
*stdio_file_new
PARAMS ((FILE * file
, int close_p
));
411 static ui_file_flush_ftype stdio_file_flush
;
413 static int stdio_file_magic
;
422 static struct ui_file
*
423 stdio_file_new (file
, close_p
)
427 struct ui_file
*ui_file
= ui_file_new ();
428 struct stdio_file
*stdio
= xmalloc (sizeof (struct stdio_file
));
429 stdio
->magic
= &stdio_file_magic
;
431 stdio
->close_p
= close_p
;
432 set_ui_file_data (ui_file
, stdio
, stdio_file_delete
);
433 set_ui_file_flush (ui_file
, stdio_file_flush
);
434 set_ui_file_write (ui_file
, stdio_file_write
);
435 set_ui_file_fputs (ui_file
, stdio_file_fputs
);
436 set_ui_file_isatty (ui_file
, stdio_file_isatty
);
441 stdio_file_delete (file
)
442 struct ui_file
*file
;
444 struct stdio_file
*stdio
= ui_file_data (file
);
445 if (stdio
->magic
!= &stdio_file_magic
)
446 internal_error ("stdio_file_delete: bad magic number");
449 fclose (stdio
->file
);
455 stdio_file_flush (file
)
456 struct ui_file
*file
;
458 struct stdio_file
*stdio
= ui_file_data (file
);
459 if (stdio
->magic
!= &stdio_file_magic
)
460 internal_error ("stdio_file_flush: bad magic number");
461 fflush (stdio
->file
);
465 stdio_file_write (struct ui_file
*file
, const char *buf
, long length_buf
)
467 struct stdio_file
*stdio
= ui_file_data (file
);
468 if (stdio
->magic
!= &stdio_file_magic
)
469 internal_error ("stdio_file_write: bad magic number");
470 fwrite (buf
, length_buf
, 1, stdio
->file
);
474 stdio_file_fputs (linebuffer
, file
)
475 const char *linebuffer
;
476 struct ui_file
*file
;
478 struct stdio_file
*stdio
= ui_file_data (file
);
479 if (stdio
->magic
!= &stdio_file_magic
)
480 internal_error ("stdio_file_fputs: bad magic number");
481 fputs (linebuffer
, stdio
->file
);
485 stdio_file_isatty (file
)
486 struct ui_file
*file
;
488 struct stdio_file
*stdio
= ui_file_data (file
);
489 if (stdio
->magic
!= &stdio_file_magic
)
490 internal_error ("stdio_file_isatty: bad magic number");
491 return (isatty (fileno (stdio
->file
)));
494 /* Like fdopen(). Create a ui_file from a previously opened FILE. */
497 stdio_fileopen (file
)
500 return stdio_file_new (file
, 0);
504 gdb_fopen (name
, mode
)
508 FILE *f
= fopen (name
, mode
);
511 return stdio_file_new (f
, 1);