* gdb/fileio.h: New file.
[deliverable/binutils-gdb.git] / gdb / ui-file.c
CommitLineData
d9fcf2fb 1/* UI_FILE - a generic STDIO like output stream.
349c5d5f
AC
2
3 Copyright 1999, 2000, 2001, 2002 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
9 the Free Software Foundation; either version 2 of the License, or
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
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. */
21
22/* Implement the ``struct ui_file'' object. */
23
24#include "defs.h"
25#include "ui-file.h"
1d1358b6 26#include "gdb_string.h"
d9fcf2fb 27
d9fcf2fb
JM
28static ui_file_isatty_ftype null_file_isatty;
29static ui_file_write_ftype null_file_write;
30static ui_file_fputs_ftype null_file_fputs;
31static ui_file_flush_ftype null_file_flush;
32static ui_file_delete_ftype null_file_delete;
33static ui_file_rewind_ftype null_file_rewind;
34static ui_file_put_ftype null_file_put;
35
36struct ui_file
37 {
38 int *magic;
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;
46 void *to_data;
47 };
48int ui_file_magic;
49
50struct ui_file *
fba45db2 51ui_file_new (void)
d9fcf2fb
JM
52{
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);
62 return file;
63}
64
65void
fba45db2 66ui_file_delete (struct ui_file *file)
d9fcf2fb
JM
67{
68 file->to_delete (file);
b8c9b27d 69 xfree (file);
d9fcf2fb
JM
70}
71
72static int
fba45db2 73null_file_isatty (struct ui_file *file)
d9fcf2fb
JM
74{
75 return 0;
76}
77
78static void
fba45db2 79null_file_rewind (struct ui_file *file)
d9fcf2fb
JM
80{
81 return;
82}
83
84static void
85null_file_put (struct ui_file *file,
86 ui_file_put_method_ftype *write,
87 void *dest)
88{
89 return;
90}
91
92static void
fba45db2 93null_file_flush (struct ui_file *file)
d9fcf2fb
JM
94{
95 return;
96}
97
98static void
99null_file_write (struct ui_file *file,
100 const char *buf,
101 long sizeof_buf)
102{
103 if (file->to_fputs == null_file_fputs)
104 /* Both the write and fputs methods are null. Discard the
105 request. */
106 return;
107 else
108 {
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. */
114 int i;
115 char b[2];
116 b[1] = '\0';
117 for (i = 0; i < sizeof_buf; i++)
118 {
119 b[0] = buf[i];
120 file->to_fputs (b, file);
121 }
122 return;
123 }
124}
125
126static void
fba45db2 127null_file_fputs (const char *buf, struct ui_file *file)
d9fcf2fb
JM
128{
129 if (file->to_write == null_file_write)
130 /* Both the write and fputs methods are null. Discard the
131 request. */
132 return;
133 else
134 {
135 /* The write method was implemented, use that. */
136 file->to_write (file, buf, strlen (buf));
137 }
138}
139
140static void
fba45db2 141null_file_delete (struct ui_file *file)
d9fcf2fb
JM
142{
143 return;
144}
145
146void *
fba45db2 147ui_file_data (struct ui_file *file)
d9fcf2fb
JM
148{
149 if (file->magic != &ui_file_magic)
8e65ff28
AC
150 internal_error (__FILE__, __LINE__,
151 "ui_file_data: bad magic number");
d9fcf2fb
JM
152 return file->to_data;
153}
154
155void
fba45db2 156gdb_flush (struct ui_file *file)
d9fcf2fb
JM
157{
158 file->to_flush (file);
159}
160
161int
fba45db2 162ui_file_isatty (struct ui_file *file)
d9fcf2fb
JM
163{
164 return file->to_isatty (file);
165}
166
167void
fba45db2 168ui_file_rewind (struct ui_file *file)
d9fcf2fb
JM
169{
170 file->to_rewind (file);
171}
172
173void
174ui_file_put (struct ui_file *file,
175 ui_file_put_method_ftype *write,
176 void *dest)
177{
178 file->to_put (file, write, dest);
179}
180
181void
182ui_file_write (struct ui_file *file,
183 const char *buf,
184 long length_buf)
185{
186 file->to_write (file, buf, length_buf);
187}
188
189void
fba45db2 190fputs_unfiltered (const char *buf, struct ui_file *file)
d9fcf2fb
JM
191{
192 file->to_fputs (buf, file);
193}
194
195void
fba45db2 196set_ui_file_flush (struct ui_file *file, ui_file_flush_ftype *flush)
d9fcf2fb
JM
197{
198 file->to_flush = flush;
199}
200
201void
fba45db2 202set_ui_file_isatty (struct ui_file *file, ui_file_isatty_ftype *isatty)
d9fcf2fb
JM
203{
204 file->to_isatty = isatty;
205}
206
207void
fba45db2 208set_ui_file_rewind (struct ui_file *file, ui_file_rewind_ftype *rewind)
d9fcf2fb
JM
209{
210 file->to_rewind = rewind;
211}
212
213void
fba45db2 214set_ui_file_put (struct ui_file *file, ui_file_put_ftype *put)
d9fcf2fb
JM
215{
216 file->to_put = put;
217}
218
219void
220set_ui_file_write (struct ui_file *file,
221 ui_file_write_ftype *write)
222{
223 file->to_write = write;
224}
225
226void
fba45db2 227set_ui_file_fputs (struct ui_file *file, ui_file_fputs_ftype *fputs)
d9fcf2fb
JM
228{
229 file->to_fputs = fputs;
230}
231
232void
fba45db2
KB
233set_ui_file_data (struct ui_file *file, void *data,
234 ui_file_delete_ftype *delete)
d9fcf2fb
JM
235{
236 file->to_data = data;
237 file->to_delete = delete;
238}
239
240/* ui_file utility function for converting a ``struct ui_file'' into
241 a memory buffer''. */
242
243struct accumulated_ui_file
244{
245 char *buffer;
246 long length;
247};
248
249static void
250do_ui_file_xstrdup (void *context, const char *buffer, long length)
251{
252 struct accumulated_ui_file *acc = context;
253 if (acc->buffer == NULL)
254 acc->buffer = xmalloc (length + 1);
255 else
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';
260}
261
262char *
263ui_file_xstrdup (struct ui_file *file,
264 long *length)
265{
266 struct accumulated_ui_file acc;
267 acc.buffer = NULL;
268 acc.length = 0;
269 ui_file_put (file, do_ui_file_xstrdup, &acc);
270 if (acc.buffer == NULL)
271 acc.buffer = xstrdup ("");
272 *length = acc.length;
273 return acc.buffer;
274}
275\f
276/* A pure memory based ``struct ui_file'' that can be used an output
277 buffer. The buffers accumulated contents are available via
278 ui_file_put(). */
279
280struct mem_file
281 {
282 int *magic;
283 char *buffer;
284 int sizeof_buffer;
285 int length_buffer;
286 };
287
288static ui_file_rewind_ftype mem_file_rewind;
289static ui_file_put_ftype mem_file_put;
290static ui_file_write_ftype mem_file_write;
291static ui_file_delete_ftype mem_file_delete;
a14ed312 292static struct ui_file *mem_file_new (void);
d9fcf2fb
JM
293static int mem_file_magic;
294
295static struct ui_file *
296mem_file_new (void)
297{
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;
308 return file;
309}
310
311static void
312mem_file_delete (struct ui_file *file)
313{
314 struct mem_file *stream = ui_file_data (file);
315 if (stream->magic != &mem_file_magic)
8e65ff28
AC
316 internal_error (__FILE__, __LINE__,
317 "mem_file_delete: bad magic number");
d9fcf2fb 318 if (stream->buffer != NULL)
b8c9b27d
KB
319 xfree (stream->buffer);
320 xfree (stream);
d9fcf2fb
JM
321}
322
323struct ui_file *
324mem_fileopen (void)
325{
326 return mem_file_new ();
327}
328
329static void
330mem_file_rewind (struct ui_file *file)
331{
332 struct mem_file *stream = ui_file_data (file);
333 if (stream->magic != &mem_file_magic)
8e65ff28
AC
334 internal_error (__FILE__, __LINE__,
335 "mem_file_rewind: bad magic number");
d9fcf2fb
JM
336 stream->length_buffer = 0;
337}
338
339static void
340mem_file_put (struct ui_file *file,
341 ui_file_put_method_ftype *write,
342 void *dest)
343{
344 struct mem_file *stream = ui_file_data (file);
345 if (stream->magic != &mem_file_magic)
8e65ff28
AC
346 internal_error (__FILE__, __LINE__,
347 "mem_file_put: bad magic number");
d9fcf2fb
JM
348 if (stream->length_buffer > 0)
349 write (dest, stream->buffer, stream->length_buffer);
350}
351
352void
353mem_file_write (struct ui_file *file,
354 const char *buffer,
355 long length_buffer)
356{
357 struct mem_file *stream = ui_file_data (file);
358 if (stream->magic != &mem_file_magic)
8e65ff28
AC
359 internal_error (__FILE__, __LINE__,
360 "mem_file_write: bad magic number");
d9fcf2fb
JM
361 if (stream->buffer == NULL)
362 {
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);
367 }
368 else
369 {
370 int new_length = stream->length_buffer + length_buffer;
371 if (new_length >= stream->sizeof_buffer)
372 {
373 stream->sizeof_buffer = new_length;
374 stream->buffer = xrealloc (stream->buffer, stream->sizeof_buffer);
375 }
376 memcpy (stream->buffer + stream->length_buffer, buffer, length_buffer);
377 stream->length_buffer = new_length;
378 }
379}
380\f
381/* ``struct ui_file'' implementation that maps directly onto
382 <stdio.h>'s FILE. */
383
384static ui_file_write_ftype stdio_file_write;
385static ui_file_fputs_ftype stdio_file_fputs;
386static ui_file_isatty_ftype stdio_file_isatty;
387static ui_file_delete_ftype stdio_file_delete;
a14ed312 388static struct ui_file *stdio_file_new (FILE * file, int close_p);
d9fcf2fb
JM
389static ui_file_flush_ftype stdio_file_flush;
390
391static int stdio_file_magic;
392
393struct stdio_file
394 {
395 int *magic;
396 FILE *file;
397 int close_p;
398 };
399
400static struct ui_file *
fba45db2 401stdio_file_new (FILE *file, int close_p)
d9fcf2fb
JM
402{
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;
406 stdio->file = file;
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);
413 return ui_file;
414}
415
416static void
fba45db2 417stdio_file_delete (struct ui_file *file)
d9fcf2fb
JM
418{
419 struct stdio_file *stdio = ui_file_data (file);
420 if (stdio->magic != &stdio_file_magic)
8e65ff28
AC
421 internal_error (__FILE__, __LINE__,
422 "stdio_file_delete: bad magic number");
d9fcf2fb
JM
423 if (stdio->close_p)
424 {
425 fclose (stdio->file);
426 }
b8c9b27d 427 xfree (stdio);
d9fcf2fb
JM
428}
429
430static void
fba45db2 431stdio_file_flush (struct ui_file *file)
d9fcf2fb
JM
432{
433 struct stdio_file *stdio = ui_file_data (file);
434 if (stdio->magic != &stdio_file_magic)
8e65ff28
AC
435 internal_error (__FILE__, __LINE__,
436 "stdio_file_flush: bad magic number");
d9fcf2fb
JM
437 fflush (stdio->file);
438}
439
440static void
441stdio_file_write (struct ui_file *file, const char *buf, long length_buf)
442{
443 struct stdio_file *stdio = ui_file_data (file);
444 if (stdio->magic != &stdio_file_magic)
8e65ff28
AC
445 internal_error (__FILE__, __LINE__,
446 "stdio_file_write: bad magic number");
d9fcf2fb
JM
447 fwrite (buf, length_buf, 1, stdio->file);
448}
449
450static void
fba45db2 451stdio_file_fputs (const char *linebuffer, struct ui_file *file)
d9fcf2fb
JM
452{
453 struct stdio_file *stdio = ui_file_data (file);
454 if (stdio->magic != &stdio_file_magic)
8e65ff28
AC
455 internal_error (__FILE__, __LINE__,
456 "stdio_file_fputs: bad magic number");
d9fcf2fb
JM
457 fputs (linebuffer, stdio->file);
458}
459
460static int
fba45db2 461stdio_file_isatty (struct ui_file *file)
d9fcf2fb
JM
462{
463 struct stdio_file *stdio = ui_file_data (file);
464 if (stdio->magic != &stdio_file_magic)
8e65ff28
AC
465 internal_error (__FILE__, __LINE__,
466 "stdio_file_isatty: bad magic number");
d9fcf2fb
JM
467 return (isatty (fileno (stdio->file)));
468}
469
470/* Like fdopen(). Create a ui_file from a previously opened FILE. */
471
472struct ui_file *
fba45db2 473stdio_fileopen (FILE *file)
d9fcf2fb
JM
474{
475 return stdio_file_new (file, 0);
476}
477
478struct ui_file *
fba45db2 479gdb_fopen (char *name, char *mode)
d9fcf2fb
JM
480{
481 FILE *f = fopen (name, mode);
482 if (f == NULL)
483 return NULL;
484 return stdio_file_new (f, 1);
485}
e4c242d9
DJ
486
487/* ``struct ui_file'' implementation that maps onto two ui-file objects. */
488
489static ui_file_write_ftype tee_file_write;
490static ui_file_fputs_ftype tee_file_fputs;
491static ui_file_isatty_ftype tee_file_isatty;
492static ui_file_delete_ftype tee_file_delete;
493static ui_file_flush_ftype tee_file_flush;
494
495static int tee_file_magic;
496
497struct tee_file
498 {
499 int *magic;
500 struct ui_file *one, *two;
501 int close_one, close_two;
502 };
503
504struct ui_file *
505tee_file_new (struct ui_file *one, int close_one,
506 struct ui_file *two, int close_two)
507{
508 struct ui_file *ui_file = ui_file_new ();
509 struct tee_file *tee = xmalloc (sizeof (struct tee_file));
510 tee->magic = &tee_file_magic;
511 tee->one = one;
512 tee->two = two;
513 tee->close_one = close_one;
514 tee->close_two = close_two;
515 set_ui_file_data (ui_file, tee, tee_file_delete);
516 set_ui_file_flush (ui_file, tee_file_flush);
517 set_ui_file_write (ui_file, tee_file_write);
518 set_ui_file_fputs (ui_file, tee_file_fputs);
519 set_ui_file_isatty (ui_file, tee_file_isatty);
520 return ui_file;
521}
522
523static void
524tee_file_delete (struct ui_file *file)
525{
526 struct tee_file *tee = ui_file_data (file);
527 if (tee->magic != &tee_file_magic)
528 internal_error (__FILE__, __LINE__,
529 "tee_file_delete: bad magic number");
530 if (tee->close_one)
531 ui_file_delete (tee->one);
532 if (tee->close_two)
533 ui_file_delete (tee->two);
534
535 xfree (tee);
536}
537
538static void
539tee_file_flush (struct ui_file *file)
540{
541 struct tee_file *tee = ui_file_data (file);
542 if (tee->magic != &tee_file_magic)
543 internal_error (__FILE__, __LINE__,
544 "tee_file_flush: bad magic number");
545 tee->one->to_flush (tee->one);
546 tee->two->to_flush (tee->two);
547}
548
549static void
550tee_file_write (struct ui_file *file, const char *buf, long length_buf)
551{
552 struct tee_file *tee = ui_file_data (file);
553 if (tee->magic != &tee_file_magic)
554 internal_error (__FILE__, __LINE__,
555 "tee_file_write: bad magic number");
556 ui_file_write (tee->one, buf, length_buf);
557 ui_file_write (tee->two, buf, length_buf);
558}
559
560static void
561tee_file_fputs (const char *linebuffer, struct ui_file *file)
562{
563 struct tee_file *tee = ui_file_data (file);
564 if (tee->magic != &tee_file_magic)
565 internal_error (__FILE__, __LINE__,
566 "tee_file_fputs: bad magic number");
567 tee->one->to_fputs (linebuffer, tee->one);
568 tee->two->to_fputs (linebuffer, tee->two);
569}
570
571static int
572tee_file_isatty (struct ui_file *file)
573{
574 struct tee_file *tee = ui_file_data (file);
575 if (tee->magic != &tee_file_magic)
576 internal_error (__FILE__, __LINE__,
577 "tee_file_isatty: bad magic number");
578 return (0);
579}
This page took 0.619758 seconds and 4 git commands to generate.