]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ui-file.c
s/@example/@smallexample/
[thirdparty/binutils-gdb.git] / gdb / ui-file.c
CommitLineData
d9fcf2fb 1/* UI_FILE - a generic STDIO like output stream.
b6ba6518 2 Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
d9fcf2fb
JM
3
4 This file is part of GDB.
5
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.
10
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.
15
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. */
20
21/* Implement the ``struct ui_file'' object. */
22
23#include "defs.h"
24#include "ui-file.h"
1d1358b6 25#include "gdb_string.h"
d9fcf2fb
JM
26
27#undef XMALLOC
28#define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
29
30static ui_file_isatty_ftype null_file_isatty;
31static ui_file_write_ftype null_file_write;
32static ui_file_fputs_ftype null_file_fputs;
33static ui_file_flush_ftype null_file_flush;
34static ui_file_delete_ftype null_file_delete;
35static ui_file_rewind_ftype null_file_rewind;
36static ui_file_put_ftype null_file_put;
37
38struct ui_file
39 {
40 int *magic;
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;
48 void *to_data;
49 };
50int ui_file_magic;
51
52struct ui_file *
fba45db2 53ui_file_new (void)
d9fcf2fb
JM
54{
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);
64 return file;
65}
66
67void
fba45db2 68ui_file_delete (struct ui_file *file)
d9fcf2fb
JM
69{
70 file->to_delete (file);
b8c9b27d 71 xfree (file);
d9fcf2fb
JM
72}
73
74static int
fba45db2 75null_file_isatty (struct ui_file *file)
d9fcf2fb
JM
76{
77 return 0;
78}
79
80static void
fba45db2 81null_file_rewind (struct ui_file *file)
d9fcf2fb
JM
82{
83 return;
84}
85
86static void
87null_file_put (struct ui_file *file,
88 ui_file_put_method_ftype *write,
89 void *dest)
90{
91 return;
92}
93
94static void
fba45db2 95null_file_flush (struct ui_file *file)
d9fcf2fb
JM
96{
97 return;
98}
99
100static void
101null_file_write (struct ui_file *file,
102 const char *buf,
103 long sizeof_buf)
104{
105 if (file->to_fputs == null_file_fputs)
106 /* Both the write and fputs methods are null. Discard the
107 request. */
108 return;
109 else
110 {
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. */
116 int i;
117 char b[2];
118 b[1] = '\0';
119 for (i = 0; i < sizeof_buf; i++)
120 {
121 b[0] = buf[i];
122 file->to_fputs (b, file);
123 }
124 return;
125 }
126}
127
128static void
fba45db2 129null_file_fputs (const char *buf, struct ui_file *file)
d9fcf2fb
JM
130{
131 if (file->to_write == null_file_write)
132 /* Both the write and fputs methods are null. Discard the
133 request. */
134 return;
135 else
136 {
137 /* The write method was implemented, use that. */
138 file->to_write (file, buf, strlen (buf));
139 }
140}
141
142static void
fba45db2 143null_file_delete (struct ui_file *file)
d9fcf2fb
JM
144{
145 return;
146}
147
148void *
fba45db2 149ui_file_data (struct ui_file *file)
d9fcf2fb
JM
150{
151 if (file->magic != &ui_file_magic)
8e65ff28
AC
152 internal_error (__FILE__, __LINE__,
153 "ui_file_data: bad magic number");
d9fcf2fb
JM
154 return file->to_data;
155}
156
157void
fba45db2 158gdb_flush (struct ui_file *file)
d9fcf2fb
JM
159{
160 file->to_flush (file);
161}
162
163int
fba45db2 164ui_file_isatty (struct ui_file *file)
d9fcf2fb
JM
165{
166 return file->to_isatty (file);
167}
168
169void
fba45db2 170ui_file_rewind (struct ui_file *file)
d9fcf2fb
JM
171{
172 file->to_rewind (file);
173}
174
175void
176ui_file_put (struct ui_file *file,
177 ui_file_put_method_ftype *write,
178 void *dest)
179{
180 file->to_put (file, write, dest);
181}
182
183void
184ui_file_write (struct ui_file *file,
185 const char *buf,
186 long length_buf)
187{
188 file->to_write (file, buf, length_buf);
189}
190
191void
fba45db2 192fputs_unfiltered (const char *buf, struct ui_file *file)
d9fcf2fb
JM
193{
194 file->to_fputs (buf, file);
195}
196
197void
fba45db2 198set_ui_file_flush (struct ui_file *file, ui_file_flush_ftype *flush)
d9fcf2fb
JM
199{
200 file->to_flush = flush;
201}
202
203void
fba45db2 204set_ui_file_isatty (struct ui_file *file, ui_file_isatty_ftype *isatty)
d9fcf2fb
JM
205{
206 file->to_isatty = isatty;
207}
208
209void
fba45db2 210set_ui_file_rewind (struct ui_file *file, ui_file_rewind_ftype *rewind)
d9fcf2fb
JM
211{
212 file->to_rewind = rewind;
213}
214
215void
fba45db2 216set_ui_file_put (struct ui_file *file, ui_file_put_ftype *put)
d9fcf2fb
JM
217{
218 file->to_put = put;
219}
220
221void
222set_ui_file_write (struct ui_file *file,
223 ui_file_write_ftype *write)
224{
225 file->to_write = write;
226}
227
228void
fba45db2 229set_ui_file_fputs (struct ui_file *file, ui_file_fputs_ftype *fputs)
d9fcf2fb
JM
230{
231 file->to_fputs = fputs;
232}
233
234void
fba45db2
KB
235set_ui_file_data (struct ui_file *file, void *data,
236 ui_file_delete_ftype *delete)
d9fcf2fb
JM
237{
238 file->to_data = data;
239 file->to_delete = delete;
240}
241
242/* ui_file utility function for converting a ``struct ui_file'' into
243 a memory buffer''. */
244
245struct accumulated_ui_file
246{
247 char *buffer;
248 long length;
249};
250
251static void
252do_ui_file_xstrdup (void *context, const char *buffer, long length)
253{
254 struct accumulated_ui_file *acc = context;
255 if (acc->buffer == NULL)
256 acc->buffer = xmalloc (length + 1);
257 else
258 acc->buffer = xrealloc (acc->buffer, acc->length + length + 1);
259 memcpy (acc->buffer + acc->length, buffer, length);
260 acc->length += length;
261 acc->buffer[acc->length] = '\0';
262}
263
264char *
265ui_file_xstrdup (struct ui_file *file,
266 long *length)
267{
268 struct accumulated_ui_file acc;
269 acc.buffer = NULL;
270 acc.length = 0;
271 ui_file_put (file, do_ui_file_xstrdup, &acc);
272 if (acc.buffer == NULL)
273 acc.buffer = xstrdup ("");
274 *length = acc.length;
275 return acc.buffer;
276}
277\f
278/* A pure memory based ``struct ui_file'' that can be used an output
279 buffer. The buffers accumulated contents are available via
280 ui_file_put(). */
281
282struct mem_file
283 {
284 int *magic;
285 char *buffer;
286 int sizeof_buffer;
287 int length_buffer;
288 };
289
290static ui_file_rewind_ftype mem_file_rewind;
291static ui_file_put_ftype mem_file_put;
292static ui_file_write_ftype mem_file_write;
293static ui_file_delete_ftype mem_file_delete;
a14ed312 294static struct ui_file *mem_file_new (void);
d9fcf2fb
JM
295static int mem_file_magic;
296
297static struct ui_file *
298mem_file_new (void)
299{
300 struct mem_file *stream = XMALLOC (struct mem_file);
301 struct ui_file *file = ui_file_new ();
302 set_ui_file_data (file, stream, mem_file_delete);
303 set_ui_file_rewind (file, mem_file_rewind);
304 set_ui_file_put (file, mem_file_put);
305 set_ui_file_write (file, mem_file_write);
306 stream->magic = &mem_file_magic;
307 stream->buffer = NULL;
308 stream->sizeof_buffer = 0;
309 stream->length_buffer = 0;
310 return file;
311}
312
313static void
314mem_file_delete (struct ui_file *file)
315{
316 struct mem_file *stream = ui_file_data (file);
317 if (stream->magic != &mem_file_magic)
8e65ff28
AC
318 internal_error (__FILE__, __LINE__,
319 "mem_file_delete: bad magic number");
d9fcf2fb 320 if (stream->buffer != NULL)
b8c9b27d
KB
321 xfree (stream->buffer);
322 xfree (stream);
d9fcf2fb
JM
323}
324
325struct ui_file *
326mem_fileopen (void)
327{
328 return mem_file_new ();
329}
330
331static void
332mem_file_rewind (struct ui_file *file)
333{
334 struct mem_file *stream = ui_file_data (file);
335 if (stream->magic != &mem_file_magic)
8e65ff28
AC
336 internal_error (__FILE__, __LINE__,
337 "mem_file_rewind: bad magic number");
d9fcf2fb
JM
338 stream->length_buffer = 0;
339}
340
341static void
342mem_file_put (struct ui_file *file,
343 ui_file_put_method_ftype *write,
344 void *dest)
345{
346 struct mem_file *stream = ui_file_data (file);
347 if (stream->magic != &mem_file_magic)
8e65ff28
AC
348 internal_error (__FILE__, __LINE__,
349 "mem_file_put: bad magic number");
d9fcf2fb
JM
350 if (stream->length_buffer > 0)
351 write (dest, stream->buffer, stream->length_buffer);
352}
353
354void
355mem_file_write (struct ui_file *file,
356 const char *buffer,
357 long length_buffer)
358{
359 struct mem_file *stream = ui_file_data (file);
360 if (stream->magic != &mem_file_magic)
8e65ff28
AC
361 internal_error (__FILE__, __LINE__,
362 "mem_file_write: bad magic number");
d9fcf2fb
JM
363 if (stream->buffer == NULL)
364 {
365 stream->length_buffer = length_buffer;
366 stream->sizeof_buffer = length_buffer;
367 stream->buffer = xmalloc (stream->sizeof_buffer);
368 memcpy (stream->buffer, buffer, length_buffer);
369 }
370 else
371 {
372 int new_length = stream->length_buffer + length_buffer;
373 if (new_length >= stream->sizeof_buffer)
374 {
375 stream->sizeof_buffer = new_length;
376 stream->buffer = xrealloc (stream->buffer, stream->sizeof_buffer);
377 }
378 memcpy (stream->buffer + stream->length_buffer, buffer, length_buffer);
379 stream->length_buffer = new_length;
380 }
381}
382\f
383/* ``struct ui_file'' implementation that maps directly onto
384 <stdio.h>'s FILE. */
385
386static ui_file_write_ftype stdio_file_write;
387static ui_file_fputs_ftype stdio_file_fputs;
388static ui_file_isatty_ftype stdio_file_isatty;
389static ui_file_delete_ftype stdio_file_delete;
a14ed312 390static struct ui_file *stdio_file_new (FILE * file, int close_p);
d9fcf2fb
JM
391static ui_file_flush_ftype stdio_file_flush;
392
393static int stdio_file_magic;
394
395struct stdio_file
396 {
397 int *magic;
398 FILE *file;
399 int close_p;
400 };
401
402static struct ui_file *
fba45db2 403stdio_file_new (FILE *file, int close_p)
d9fcf2fb
JM
404{
405 struct ui_file *ui_file = ui_file_new ();
406 struct stdio_file *stdio = xmalloc (sizeof (struct stdio_file));
407 stdio->magic = &stdio_file_magic;
408 stdio->file = file;
409 stdio->close_p = close_p;
410 set_ui_file_data (ui_file, stdio, stdio_file_delete);
411 set_ui_file_flush (ui_file, stdio_file_flush);
412 set_ui_file_write (ui_file, stdio_file_write);
413 set_ui_file_fputs (ui_file, stdio_file_fputs);
414 set_ui_file_isatty (ui_file, stdio_file_isatty);
415 return ui_file;
416}
417
418static void
fba45db2 419stdio_file_delete (struct ui_file *file)
d9fcf2fb
JM
420{
421 struct stdio_file *stdio = ui_file_data (file);
422 if (stdio->magic != &stdio_file_magic)
8e65ff28
AC
423 internal_error (__FILE__, __LINE__,
424 "stdio_file_delete: bad magic number");
d9fcf2fb
JM
425 if (stdio->close_p)
426 {
427 fclose (stdio->file);
428 }
b8c9b27d 429 xfree (stdio);
d9fcf2fb
JM
430}
431
432static void
fba45db2 433stdio_file_flush (struct ui_file *file)
d9fcf2fb
JM
434{
435 struct stdio_file *stdio = ui_file_data (file);
436 if (stdio->magic != &stdio_file_magic)
8e65ff28
AC
437 internal_error (__FILE__, __LINE__,
438 "stdio_file_flush: bad magic number");
d9fcf2fb
JM
439 fflush (stdio->file);
440}
441
442static void
443stdio_file_write (struct ui_file *file, const char *buf, long length_buf)
444{
445 struct stdio_file *stdio = ui_file_data (file);
446 if (stdio->magic != &stdio_file_magic)
8e65ff28
AC
447 internal_error (__FILE__, __LINE__,
448 "stdio_file_write: bad magic number");
d9fcf2fb
JM
449 fwrite (buf, length_buf, 1, stdio->file);
450}
451
452static void
fba45db2 453stdio_file_fputs (const char *linebuffer, struct ui_file *file)
d9fcf2fb
JM
454{
455 struct stdio_file *stdio = ui_file_data (file);
456 if (stdio->magic != &stdio_file_magic)
8e65ff28
AC
457 internal_error (__FILE__, __LINE__,
458 "stdio_file_fputs: bad magic number");
d9fcf2fb
JM
459 fputs (linebuffer, stdio->file);
460}
461
462static int
fba45db2 463stdio_file_isatty (struct ui_file *file)
d9fcf2fb
JM
464{
465 struct stdio_file *stdio = ui_file_data (file);
466 if (stdio->magic != &stdio_file_magic)
8e65ff28
AC
467 internal_error (__FILE__, __LINE__,
468 "stdio_file_isatty: bad magic number");
d9fcf2fb
JM
469 return (isatty (fileno (stdio->file)));
470}
471
472/* Like fdopen(). Create a ui_file from a previously opened FILE. */
473
474struct ui_file *
fba45db2 475stdio_fileopen (FILE *file)
d9fcf2fb
JM
476{
477 return stdio_file_new (file, 0);
478}
479
480struct ui_file *
fba45db2 481gdb_fopen (char *name, char *mode)
d9fcf2fb
JM
482{
483 FILE *f = fopen (name, mode);
484 if (f == NULL)
485 return NULL;
486 return stdio_file_new (f, 1);
487}