* defs.h (XMALLOC): Define.
[external/binutils.git] / gdb / ui-file.c
1 /* UI_FILE - a generic STDIO like output stream.
2
3    Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
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"
26 #include "gdb_string.h"
27
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;
35
36 struct 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   };
48 int ui_file_magic;
49
50 struct ui_file *
51 ui_file_new (void)
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
65 void
66 ui_file_delete (struct ui_file *file)
67 {
68   file->to_delete (file);
69   xfree (file);
70 }
71
72 static int
73 null_file_isatty (struct ui_file *file)
74 {
75   return 0;
76 }
77
78 static void
79 null_file_rewind (struct ui_file *file)
80 {
81   return;
82 }
83
84 static void
85 null_file_put (struct ui_file *file,
86                ui_file_put_method_ftype *write,
87                void *dest)
88 {
89   return;
90 }
91
92 static void
93 null_file_flush (struct ui_file *file)
94 {
95   return;
96 }
97
98 static void
99 null_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
126 static void
127 null_file_fputs (const char *buf, struct ui_file *file)
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
140 static void
141 null_file_delete (struct ui_file *file)
142 {
143   return;
144 }
145
146 void *
147 ui_file_data (struct ui_file *file)
148 {
149   if (file->magic != &ui_file_magic)
150     internal_error (__FILE__, __LINE__,
151                     "ui_file_data: bad magic number");
152   return file->to_data;
153 }
154
155 void
156 gdb_flush (struct ui_file *file)
157 {
158   file->to_flush (file);
159 }
160
161 int
162 ui_file_isatty (struct ui_file *file)
163 {
164   return file->to_isatty (file);
165 }
166
167 void
168 ui_file_rewind (struct ui_file *file)
169 {
170   file->to_rewind (file);
171 }
172
173 void
174 ui_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
181 void
182 ui_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
189 void
190 fputs_unfiltered (const char *buf, struct ui_file *file)
191 {
192   file->to_fputs (buf, file);
193 }
194
195 void
196 set_ui_file_flush (struct ui_file *file, ui_file_flush_ftype *flush)
197 {
198   file->to_flush = flush;
199 }
200
201 void
202 set_ui_file_isatty (struct ui_file *file, ui_file_isatty_ftype *isatty)
203 {
204   file->to_isatty = isatty;
205 }
206
207 void
208 set_ui_file_rewind (struct ui_file *file, ui_file_rewind_ftype *rewind)
209 {
210   file->to_rewind = rewind;
211 }
212
213 void
214 set_ui_file_put (struct ui_file *file, ui_file_put_ftype *put)
215 {
216   file->to_put = put;
217 }
218
219 void
220 set_ui_file_write (struct ui_file *file,
221                     ui_file_write_ftype *write)
222 {
223   file->to_write = write;
224 }
225
226 void
227 set_ui_file_fputs (struct ui_file *file, ui_file_fputs_ftype *fputs)
228 {
229   file->to_fputs = fputs;
230 }
231
232 void
233 set_ui_file_data (struct ui_file *file, void *data,
234                   ui_file_delete_ftype *delete)
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
243 struct accumulated_ui_file
244 {
245   char *buffer;
246   long length;
247 };
248
249 static void
250 do_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
262 char *
263 ui_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
280 struct mem_file
281   {
282     int *magic;
283     char *buffer;
284     int sizeof_buffer;
285     int length_buffer;
286   };
287
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;
294
295 static struct ui_file *
296 mem_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
311 static void
312 mem_file_delete (struct ui_file *file)
313 {
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);
320   xfree (stream);
321 }
322
323 struct ui_file *
324 mem_fileopen (void)
325 {
326   return mem_file_new ();
327 }
328
329 static void
330 mem_file_rewind (struct ui_file *file)
331 {
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;
337 }
338
339 static void
340 mem_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)
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);
350 }
351
352 void
353 mem_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)
359     internal_error (__FILE__, __LINE__,
360                     "mem_file_write: bad magic number");
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
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;
390
391 static int stdio_file_magic;
392
393 struct stdio_file
394   {
395     int *magic;
396     FILE *file;
397     int close_p;
398   };
399
400 static struct ui_file *
401 stdio_file_new (FILE *file, int close_p)
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
416 static void
417 stdio_file_delete (struct ui_file *file)
418 {
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");
423   if (stdio->close_p)
424     {
425       fclose (stdio->file);
426     }
427   xfree (stdio);
428 }
429
430 static void
431 stdio_file_flush (struct ui_file *file)
432 {
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);
438 }
439
440 static void
441 stdio_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)
445     internal_error (__FILE__, __LINE__,
446                     "stdio_file_write: bad magic number");
447   fwrite (buf, length_buf, 1, stdio->file);
448 }
449
450 static void
451 stdio_file_fputs (const char *linebuffer, struct ui_file *file)
452 {
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);
458 }
459
460 static int
461 stdio_file_isatty (struct ui_file *file)
462 {
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)));
468 }
469
470 /* Like fdopen().  Create a ui_file from a previously opened FILE. */
471
472 struct ui_file *
473 stdio_fileopen (FILE *file)
474 {
475   return stdio_file_new (file, 0);
476 }
477
478 struct ui_file *
479 gdb_fopen (char *name, char *mode)
480 {
481   FILE *f = fopen (name, mode);
482   if (f == NULL)
483     return NULL;
484   return stdio_file_new (f, 1);
485 }