1 /* UI_FILE - a generic STDIO like output stream.
3 Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
5 This file is part of GDB.
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
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Implement the ``struct ui_file'' object. */
26 #include "gdb_string.h"
28 static ui_file_isatty_ftype null_file_isatty
;
29 static ui_file_write_ftype null_file_write
;
30 static ui_file_fputs_ftype null_file_fputs
;
31 static ui_file_flush_ftype null_file_flush
;
32 static ui_file_delete_ftype null_file_delete
;
33 static ui_file_rewind_ftype null_file_rewind
;
34 static ui_file_put_ftype null_file_put
;
39 ui_file_flush_ftype
*to_flush
;
40 ui_file_write_ftype
*to_write
;
41 ui_file_fputs_ftype
*to_fputs
;
42 ui_file_delete_ftype
*to_delete
;
43 ui_file_isatty_ftype
*to_isatty
;
44 ui_file_rewind_ftype
*to_rewind
;
45 ui_file_put_ftype
*to_put
;
53 struct ui_file
*file
= xmalloc (sizeof (struct ui_file
));
54 file
->magic
= &ui_file_magic
;
55 set_ui_file_data (file
, NULL
, null_file_delete
);
56 set_ui_file_flush (file
, null_file_flush
);
57 set_ui_file_write (file
, null_file_write
);
58 set_ui_file_fputs (file
, null_file_fputs
);
59 set_ui_file_isatty (file
, null_file_isatty
);
60 set_ui_file_rewind (file
, null_file_rewind
);
61 set_ui_file_put (file
, null_file_put
);
66 ui_file_delete (struct ui_file
*file
)
68 file
->to_delete (file
);
73 null_file_isatty (struct ui_file
*file
)
79 null_file_rewind (struct ui_file
*file
)
85 null_file_put (struct ui_file
*file
,
86 ui_file_put_method_ftype
*write
,
93 null_file_flush (struct ui_file
*file
)
99 null_file_write (struct ui_file
*file
,
103 if (file
->to_fputs
== null_file_fputs
)
104 /* Both the write and fputs methods are null. Discard the
109 /* The fputs method isn't null, slowly pass the write request
110 onto that. FYI, this isn't as bad as it may look - the
111 current (as of 1999-11-07) printf_* function calls fputc and
112 fputc does exactly the below. By having a write function it
113 is possible to clean up that code. */
117 for (i
= 0; i
< sizeof_buf
; i
++)
120 file
->to_fputs (b
, file
);
127 null_file_fputs (const char *buf
, struct ui_file
*file
)
129 if (file
->to_write
== null_file_write
)
130 /* Both the write and fputs methods are null. Discard the
135 /* The write method was implemented, use that. */
136 file
->to_write (file
, buf
, strlen (buf
));
141 null_file_delete (struct ui_file
*file
)
147 ui_file_data (struct ui_file
*file
)
149 if (file
->magic
!= &ui_file_magic
)
150 internal_error (__FILE__
, __LINE__
,
151 "ui_file_data: bad magic number");
152 return file
->to_data
;
156 gdb_flush (struct ui_file
*file
)
158 file
->to_flush (file
);
162 ui_file_isatty (struct ui_file
*file
)
164 return file
->to_isatty (file
);
168 ui_file_rewind (struct ui_file
*file
)
170 file
->to_rewind (file
);
174 ui_file_put (struct ui_file
*file
,
175 ui_file_put_method_ftype
*write
,
178 file
->to_put (file
, write
, dest
);
182 ui_file_write (struct ui_file
*file
,
186 file
->to_write (file
, buf
, length_buf
);
190 fputs_unfiltered (const char *buf
, struct ui_file
*file
)
192 file
->to_fputs (buf
, file
);
196 set_ui_file_flush (struct ui_file
*file
, ui_file_flush_ftype
*flush
)
198 file
->to_flush
= flush
;
202 set_ui_file_isatty (struct ui_file
*file
, ui_file_isatty_ftype
*isatty
)
204 file
->to_isatty
= isatty
;
208 set_ui_file_rewind (struct ui_file
*file
, ui_file_rewind_ftype
*rewind
)
210 file
->to_rewind
= rewind
;
214 set_ui_file_put (struct ui_file
*file
, ui_file_put_ftype
*put
)
220 set_ui_file_write (struct ui_file
*file
,
221 ui_file_write_ftype
*write
)
223 file
->to_write
= write
;
227 set_ui_file_fputs (struct ui_file
*file
, ui_file_fputs_ftype
*fputs
)
229 file
->to_fputs
= fputs
;
233 set_ui_file_data (struct ui_file
*file
, void *data
,
234 ui_file_delete_ftype
*delete)
236 file
->to_data
= data
;
237 file
->to_delete
= delete;
240 /* ui_file utility function for converting a ``struct ui_file'' into
241 a memory buffer''. */
243 struct accumulated_ui_file
250 do_ui_file_xstrdup (void *context
, const char *buffer
, long length
)
252 struct accumulated_ui_file
*acc
= context
;
253 if (acc
->buffer
== NULL
)
254 acc
->buffer
= xmalloc (length
+ 1);
256 acc
->buffer
= xrealloc (acc
->buffer
, acc
->length
+ length
+ 1);
257 memcpy (acc
->buffer
+ acc
->length
, buffer
, length
);
258 acc
->length
+= length
;
259 acc
->buffer
[acc
->length
] = '\0';
263 ui_file_xstrdup (struct ui_file
*file
,
266 struct accumulated_ui_file acc
;
269 ui_file_put (file
, do_ui_file_xstrdup
, &acc
);
270 if (acc
.buffer
== NULL
)
271 acc
.buffer
= xstrdup ("");
272 *length
= acc
.length
;
276 /* A pure memory based ``struct ui_file'' that can be used an output
277 buffer. The buffers accumulated contents are available via
288 static ui_file_rewind_ftype mem_file_rewind
;
289 static ui_file_put_ftype mem_file_put
;
290 static ui_file_write_ftype mem_file_write
;
291 static ui_file_delete_ftype mem_file_delete
;
292 static struct ui_file
*mem_file_new (void);
293 static int mem_file_magic
;
295 static struct ui_file
*
298 struct mem_file
*stream
= XMALLOC (struct mem_file
);
299 struct ui_file
*file
= ui_file_new ();
300 set_ui_file_data (file
, stream
, mem_file_delete
);
301 set_ui_file_rewind (file
, mem_file_rewind
);
302 set_ui_file_put (file
, mem_file_put
);
303 set_ui_file_write (file
, mem_file_write
);
304 stream
->magic
= &mem_file_magic
;
305 stream
->buffer
= NULL
;
306 stream
->sizeof_buffer
= 0;
307 stream
->length_buffer
= 0;
312 mem_file_delete (struct ui_file
*file
)
314 struct mem_file
*stream
= ui_file_data (file
);
315 if (stream
->magic
!= &mem_file_magic
)
316 internal_error (__FILE__
, __LINE__
,
317 "mem_file_delete: bad magic number");
318 if (stream
->buffer
!= NULL
)
319 xfree (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 (__FILE__
, __LINE__
,
335 "mem_file_rewind: bad magic number");
336 stream
->length_buffer
= 0;
340 mem_file_put (struct ui_file
*file
,
341 ui_file_put_method_ftype
*write
,
344 struct mem_file
*stream
= ui_file_data (file
);
345 if (stream
->magic
!= &mem_file_magic
)
346 internal_error (__FILE__
, __LINE__
,
347 "mem_file_put: bad magic number");
348 if (stream
->length_buffer
> 0)
349 write (dest
, stream
->buffer
, stream
->length_buffer
);
353 mem_file_write (struct ui_file
*file
,
357 struct mem_file
*stream
= ui_file_data (file
);
358 if (stream
->magic
!= &mem_file_magic
)
359 internal_error (__FILE__
, __LINE__
,
360 "mem_file_write: bad magic number");
361 if (stream
->buffer
== NULL
)
363 stream
->length_buffer
= length_buffer
;
364 stream
->sizeof_buffer
= length_buffer
;
365 stream
->buffer
= xmalloc (stream
->sizeof_buffer
);
366 memcpy (stream
->buffer
, buffer
, length_buffer
);
370 int new_length
= stream
->length_buffer
+ length_buffer
;
371 if (new_length
>= stream
->sizeof_buffer
)
373 stream
->sizeof_buffer
= new_length
;
374 stream
->buffer
= xrealloc (stream
->buffer
, stream
->sizeof_buffer
);
376 memcpy (stream
->buffer
+ stream
->length_buffer
, buffer
, length_buffer
);
377 stream
->length_buffer
= new_length
;
381 /* ``struct ui_file'' implementation that maps directly onto
384 static ui_file_write_ftype stdio_file_write
;
385 static ui_file_fputs_ftype stdio_file_fputs
;
386 static ui_file_isatty_ftype stdio_file_isatty
;
387 static ui_file_delete_ftype stdio_file_delete
;
388 static struct ui_file
*stdio_file_new (FILE * file
, int close_p
);
389 static ui_file_flush_ftype stdio_file_flush
;
391 static int stdio_file_magic
;
400 static struct ui_file
*
401 stdio_file_new (FILE *file
, int close_p
)
403 struct ui_file
*ui_file
= ui_file_new ();
404 struct stdio_file
*stdio
= xmalloc (sizeof (struct stdio_file
));
405 stdio
->magic
= &stdio_file_magic
;
407 stdio
->close_p
= close_p
;
408 set_ui_file_data (ui_file
, stdio
, stdio_file_delete
);
409 set_ui_file_flush (ui_file
, stdio_file_flush
);
410 set_ui_file_write (ui_file
, stdio_file_write
);
411 set_ui_file_fputs (ui_file
, stdio_file_fputs
);
412 set_ui_file_isatty (ui_file
, stdio_file_isatty
);
417 stdio_file_delete (struct ui_file
*file
)
419 struct stdio_file
*stdio
= ui_file_data (file
);
420 if (stdio
->magic
!= &stdio_file_magic
)
421 internal_error (__FILE__
, __LINE__
,
422 "stdio_file_delete: bad magic number");
425 fclose (stdio
->file
);
431 stdio_file_flush (struct ui_file
*file
)
433 struct stdio_file
*stdio
= ui_file_data (file
);
434 if (stdio
->magic
!= &stdio_file_magic
)
435 internal_error (__FILE__
, __LINE__
,
436 "stdio_file_flush: bad magic number");
437 fflush (stdio
->file
);
441 stdio_file_write (struct ui_file
*file
, const char *buf
, long length_buf
)
443 struct stdio_file
*stdio
= ui_file_data (file
);
444 if (stdio
->magic
!= &stdio_file_magic
)
445 internal_error (__FILE__
, __LINE__
,
446 "stdio_file_write: bad magic number");
447 fwrite (buf
, length_buf
, 1, stdio
->file
);
451 stdio_file_fputs (const char *linebuffer
, struct ui_file
*file
)
453 struct stdio_file
*stdio
= ui_file_data (file
);
454 if (stdio
->magic
!= &stdio_file_magic
)
455 internal_error (__FILE__
, __LINE__
,
456 "stdio_file_fputs: bad magic number");
457 fputs (linebuffer
, stdio
->file
);
461 stdio_file_isatty (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_isatty: bad magic number");
467 return (isatty (fileno (stdio
->file
)));
470 /* Like fdopen(). Create a ui_file from a previously opened FILE. */
473 stdio_fileopen (FILE *file
)
475 return stdio_file_new (file
, 0);
479 gdb_fopen (char *name
, char *mode
)
481 FILE *f
= fopen (name
, mode
);
484 return stdio_file_new (f
, 1);