Fix formatting
[deliverable/binutils-gdb.git] / gdb / ui-file.c
CommitLineData
d9fcf2fb
JM
1/* UI_FILE - a generic STDIO like output stream.
2 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
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 *
53ui_file_new ()
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
68ui_file_delete (file)
69 struct ui_file *file;
70{
71 file->to_delete (file);
72 free (file);
73}
74
75static int
76null_file_isatty (file)
77 struct ui_file *file;
78{
79 return 0;
80}
81
82static void
83null_file_rewind (file)
84 struct ui_file *file;
85{
86 return;
87}
88
89static void
90null_file_put (struct ui_file *file,
91 ui_file_put_method_ftype *write,
92 void *dest)
93{
94 return;
95}
96
97static void
98null_file_flush (file)
99 struct ui_file *file;
100{
101 return;
102}
103
104static void
105null_file_write (struct ui_file *file,
106 const char *buf,
107 long sizeof_buf)
108{
109 if (file->to_fputs == null_file_fputs)
110 /* Both the write and fputs methods are null. Discard the
111 request. */
112 return;
113 else
114 {
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. */
120 int i;
121 char b[2];
122 b[1] = '\0';
123 for (i = 0; i < sizeof_buf; i++)
124 {
125 b[0] = buf[i];
126 file->to_fputs (b, file);
127 }
128 return;
129 }
130}
131
132static void
133null_file_fputs (buf, file)
134 const char *buf;
135 struct ui_file *file;
136{
137 if (file->to_write == null_file_write)
138 /* Both the write and fputs methods are null. Discard the
139 request. */
140 return;
141 else
142 {
143 /* The write method was implemented, use that. */
144 file->to_write (file, buf, strlen (buf));
145 }
146}
147
148static void
149null_file_delete (file)
150 struct ui_file *file;
151{
152 return;
153}
154
155void *
156ui_file_data (file)
157 struct ui_file *file;
158{
159 if (file->magic != &ui_file_magic)
160 internal_error ("ui_file_data: bad magic number");
161 return file->to_data;
162}
163
164void
165gdb_flush (file)
166 struct ui_file *file;
167{
168 file->to_flush (file);
169}
170
171int
172ui_file_isatty (file)
173 struct ui_file *file;
174{
175 return file->to_isatty (file);
176}
177
178void
179ui_file_rewind (file)
180 struct ui_file *file;
181{
182 file->to_rewind (file);
183}
184
185void
186ui_file_put (struct ui_file *file,
187 ui_file_put_method_ftype *write,
188 void *dest)
189{
190 file->to_put (file, write, dest);
191}
192
193void
194ui_file_write (struct ui_file *file,
195 const char *buf,
196 long length_buf)
197{
198 file->to_write (file, buf, length_buf);
199}
200
201void
202fputs_unfiltered (buf, file)
203 const char *buf;
204 struct ui_file *file;
205{
206 file->to_fputs (buf, file);
207}
208
209void
210set_ui_file_flush (file, flush)
211 struct ui_file *file;
212 ui_file_flush_ftype *flush;
213{
214 file->to_flush = flush;
215}
216
217void
218set_ui_file_isatty (file, isatty)
219 struct ui_file *file;
220 ui_file_isatty_ftype *isatty;
221{
222 file->to_isatty = isatty;
223}
224
225void
226set_ui_file_rewind (file, rewind)
227 struct ui_file *file;
228 ui_file_rewind_ftype *rewind;
229{
230 file->to_rewind = rewind;
231}
232
233void
234set_ui_file_put (file, put)
235 struct ui_file *file;
236 ui_file_put_ftype *put;
237{
238 file->to_put = put;
239}
240
241void
242set_ui_file_write (struct ui_file *file,
243 ui_file_write_ftype *write)
244{
245 file->to_write = write;
246}
247
248void
249set_ui_file_fputs (file, fputs)
250 struct ui_file *file;
251 ui_file_fputs_ftype *fputs;
252{
253 file->to_fputs = fputs;
254}
255
256void
257set_ui_file_data (file, data, delete)
258 struct ui_file *file;
259 void *data;
260 ui_file_delete_ftype *delete;
261{
262 file->to_data = data;
263 file->to_delete = delete;
264}
265
266/* ui_file utility function for converting a ``struct ui_file'' into
267 a memory buffer''. */
268
269struct accumulated_ui_file
270{
271 char *buffer;
272 long length;
273};
274
275static void
276do_ui_file_xstrdup (void *context, const char *buffer, long length)
277{
278 struct accumulated_ui_file *acc = context;
279 if (acc->buffer == NULL)
280 acc->buffer = xmalloc (length + 1);
281 else
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';
286}
287
288char *
289ui_file_xstrdup (struct ui_file *file,
290 long *length)
291{
292 struct accumulated_ui_file acc;
293 acc.buffer = NULL;
294 acc.length = 0;
295 ui_file_put (file, do_ui_file_xstrdup, &acc);
296 if (acc.buffer == NULL)
297 acc.buffer = xstrdup ("");
298 *length = acc.length;
299 return acc.buffer;
300}
301\f
302/* A pure memory based ``struct ui_file'' that can be used an output
303 buffer. The buffers accumulated contents are available via
304 ui_file_put(). */
305
306struct mem_file
307 {
308 int *magic;
309 char *buffer;
310 int sizeof_buffer;
311 int length_buffer;
312 };
313
314static ui_file_rewind_ftype mem_file_rewind;
315static ui_file_put_ftype mem_file_put;
316static ui_file_write_ftype mem_file_write;
317static ui_file_delete_ftype mem_file_delete;
a14ed312 318static struct ui_file *mem_file_new (void);
d9fcf2fb
JM
319static int mem_file_magic;
320
321static struct ui_file *
322mem_file_new (void)
323{
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;
334 return file;
335}
336
337static void
338mem_file_delete (struct ui_file *file)
339{
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);
345 free (stream);
346}
347
348struct ui_file *
349mem_fileopen (void)
350{
351 return mem_file_new ();
352}
353
354static void
355mem_file_rewind (struct ui_file *file)
356{
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;
361}
362
363static void
364mem_file_put (struct ui_file *file,
365 ui_file_put_method_ftype *write,
366 void *dest)
367{
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);
373}
374
375void
376mem_file_write (struct ui_file *file,
377 const char *buffer,
378 long length_buffer)
379{
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)
384 {
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);
389 }
390 else
391 {
392 int new_length = stream->length_buffer + length_buffer;
393 if (new_length >= stream->sizeof_buffer)
394 {
395 stream->sizeof_buffer = new_length;
396 stream->buffer = xrealloc (stream->buffer, stream->sizeof_buffer);
397 }
398 memcpy (stream->buffer + stream->length_buffer, buffer, length_buffer);
399 stream->length_buffer = new_length;
400 }
401}
402\f
403/* ``struct ui_file'' implementation that maps directly onto
404 <stdio.h>'s FILE. */
405
406static ui_file_write_ftype stdio_file_write;
407static ui_file_fputs_ftype stdio_file_fputs;
408static ui_file_isatty_ftype stdio_file_isatty;
409static ui_file_delete_ftype stdio_file_delete;
a14ed312 410static struct ui_file *stdio_file_new (FILE * file, int close_p);
d9fcf2fb
JM
411static ui_file_flush_ftype stdio_file_flush;
412
413static int stdio_file_magic;
414
415struct stdio_file
416 {
417 int *magic;
418 FILE *file;
419 int close_p;
420 };
421
422static struct ui_file *
423stdio_file_new (file, close_p)
424 FILE *file;
425 int close_p;
426{
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;
430 stdio->file = file;
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);
437 return ui_file;
438}
439
440static void
441stdio_file_delete (file)
442 struct ui_file *file;
443{
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");
447 if (stdio->close_p)
448 {
449 fclose (stdio->file);
450 }
451 free (stdio);
452}
453
454static void
455stdio_file_flush (file)
456 struct ui_file *file;
457{
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);
462}
463
464static void
465stdio_file_write (struct ui_file *file, const char *buf, long length_buf)
466{
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);
471}
472
473static void
474stdio_file_fputs (linebuffer, file)
475 const char *linebuffer;
476 struct ui_file *file;
477{
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);
482}
483
484static int
485stdio_file_isatty (file)
486 struct ui_file *file;
487{
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)));
492}
493
494/* Like fdopen(). Create a ui_file from a previously opened FILE. */
495
496struct ui_file *
497stdio_fileopen (file)
498 FILE *file;
499{
500 return stdio_file_new (file, 0);
501}
502
503struct ui_file *
504gdb_fopen (name, mode)
505 char *name;
506 char *mode;
507{
508 FILE *f = fopen (name, mode);
509 if (f == NULL)
510 return NULL;
511 return stdio_file_new (f, 1);
512}
This page took 0.071776 seconds and 4 git commands to generate.