Protoization.
[deliverable/binutils-gdb.git] / gdb / ui-file.c
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"
25 #include "gdb_string.h"
26
27 #undef XMALLOC
28 #define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
29
30 static ui_file_isatty_ftype null_file_isatty;
31 static ui_file_write_ftype null_file_write;
32 static ui_file_fputs_ftype null_file_fputs;
33 static ui_file_flush_ftype null_file_flush;
34 static ui_file_delete_ftype null_file_delete;
35 static ui_file_rewind_ftype null_file_rewind;
36 static ui_file_put_ftype null_file_put;
37
38 struct 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 };
50 int ui_file_magic;
51
52 struct ui_file *
53 ui_file_new (void)
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
67 void
68 ui_file_delete (struct ui_file *file)
69 {
70 file->to_delete (file);
71 free (file);
72 }
73
74 static int
75 null_file_isatty (struct ui_file *file)
76 {
77 return 0;
78 }
79
80 static void
81 null_file_rewind (struct ui_file *file)
82 {
83 return;
84 }
85
86 static void
87 null_file_put (struct ui_file *file,
88 ui_file_put_method_ftype *write,
89 void *dest)
90 {
91 return;
92 }
93
94 static void
95 null_file_flush (struct ui_file *file)
96 {
97 return;
98 }
99
100 static void
101 null_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
128 static void
129 null_file_fputs (const char *buf, struct ui_file *file)
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
142 static void
143 null_file_delete (struct ui_file *file)
144 {
145 return;
146 }
147
148 void *
149 ui_file_data (struct ui_file *file)
150 {
151 if (file->magic != &ui_file_magic)
152 internal_error ("ui_file_data: bad magic number");
153 return file->to_data;
154 }
155
156 void
157 gdb_flush (struct ui_file *file)
158 {
159 file->to_flush (file);
160 }
161
162 int
163 ui_file_isatty (struct ui_file *file)
164 {
165 return file->to_isatty (file);
166 }
167
168 void
169 ui_file_rewind (struct ui_file *file)
170 {
171 file->to_rewind (file);
172 }
173
174 void
175 ui_file_put (struct ui_file *file,
176 ui_file_put_method_ftype *write,
177 void *dest)
178 {
179 file->to_put (file, write, dest);
180 }
181
182 void
183 ui_file_write (struct ui_file *file,
184 const char *buf,
185 long length_buf)
186 {
187 file->to_write (file, buf, length_buf);
188 }
189
190 void
191 fputs_unfiltered (const char *buf, struct ui_file *file)
192 {
193 file->to_fputs (buf, file);
194 }
195
196 void
197 set_ui_file_flush (struct ui_file *file, ui_file_flush_ftype *flush)
198 {
199 file->to_flush = flush;
200 }
201
202 void
203 set_ui_file_isatty (struct ui_file *file, ui_file_isatty_ftype *isatty)
204 {
205 file->to_isatty = isatty;
206 }
207
208 void
209 set_ui_file_rewind (struct ui_file *file, ui_file_rewind_ftype *rewind)
210 {
211 file->to_rewind = rewind;
212 }
213
214 void
215 set_ui_file_put (struct ui_file *file, ui_file_put_ftype *put)
216 {
217 file->to_put = put;
218 }
219
220 void
221 set_ui_file_write (struct ui_file *file,
222 ui_file_write_ftype *write)
223 {
224 file->to_write = write;
225 }
226
227 void
228 set_ui_file_fputs (struct ui_file *file, ui_file_fputs_ftype *fputs)
229 {
230 file->to_fputs = fputs;
231 }
232
233 void
234 set_ui_file_data (struct ui_file *file, void *data,
235 ui_file_delete_ftype *delete)
236 {
237 file->to_data = data;
238 file->to_delete = delete;
239 }
240
241 /* ui_file utility function for converting a ``struct ui_file'' into
242 a memory buffer''. */
243
244 struct accumulated_ui_file
245 {
246 char *buffer;
247 long length;
248 };
249
250 static void
251 do_ui_file_xstrdup (void *context, const char *buffer, long length)
252 {
253 struct accumulated_ui_file *acc = context;
254 if (acc->buffer == NULL)
255 acc->buffer = xmalloc (length + 1);
256 else
257 acc->buffer = xrealloc (acc->buffer, acc->length + length + 1);
258 memcpy (acc->buffer + acc->length, buffer, length);
259 acc->length += length;
260 acc->buffer[acc->length] = '\0';
261 }
262
263 char *
264 ui_file_xstrdup (struct ui_file *file,
265 long *length)
266 {
267 struct accumulated_ui_file acc;
268 acc.buffer = NULL;
269 acc.length = 0;
270 ui_file_put (file, do_ui_file_xstrdup, &acc);
271 if (acc.buffer == NULL)
272 acc.buffer = xstrdup ("");
273 *length = acc.length;
274 return acc.buffer;
275 }
276 \f
277 /* A pure memory based ``struct ui_file'' that can be used an output
278 buffer. The buffers accumulated contents are available via
279 ui_file_put(). */
280
281 struct mem_file
282 {
283 int *magic;
284 char *buffer;
285 int sizeof_buffer;
286 int length_buffer;
287 };
288
289 static ui_file_rewind_ftype mem_file_rewind;
290 static ui_file_put_ftype mem_file_put;
291 static ui_file_write_ftype mem_file_write;
292 static ui_file_delete_ftype mem_file_delete;
293 static struct ui_file *mem_file_new (void);
294 static int mem_file_magic;
295
296 static struct ui_file *
297 mem_file_new (void)
298 {
299 struct mem_file *stream = XMALLOC (struct mem_file);
300 struct ui_file *file = ui_file_new ();
301 set_ui_file_data (file, stream, mem_file_delete);
302 set_ui_file_rewind (file, mem_file_rewind);
303 set_ui_file_put (file, mem_file_put);
304 set_ui_file_write (file, mem_file_write);
305 stream->magic = &mem_file_magic;
306 stream->buffer = NULL;
307 stream->sizeof_buffer = 0;
308 stream->length_buffer = 0;
309 return file;
310 }
311
312 static void
313 mem_file_delete (struct ui_file *file)
314 {
315 struct mem_file *stream = ui_file_data (file);
316 if (stream->magic != &mem_file_magic)
317 internal_error ("mem_file_delete: bad magic number");
318 if (stream->buffer != NULL)
319 free (stream->buffer);
320 free (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 ("mem_file_rewind: bad magic number");
335 stream->length_buffer = 0;
336 }
337
338 static void
339 mem_file_put (struct ui_file *file,
340 ui_file_put_method_ftype *write,
341 void *dest)
342 {
343 struct mem_file *stream = ui_file_data (file);
344 if (stream->magic != &mem_file_magic)
345 internal_error ("mem_file_put: bad magic number");
346 if (stream->length_buffer > 0)
347 write (dest, stream->buffer, stream->length_buffer);
348 }
349
350 void
351 mem_file_write (struct ui_file *file,
352 const char *buffer,
353 long length_buffer)
354 {
355 struct mem_file *stream = ui_file_data (file);
356 if (stream->magic != &mem_file_magic)
357 internal_error ("mem_file_write: bad magic number");
358 if (stream->buffer == NULL)
359 {
360 stream->length_buffer = length_buffer;
361 stream->sizeof_buffer = length_buffer;
362 stream->buffer = xmalloc (stream->sizeof_buffer);
363 memcpy (stream->buffer, buffer, length_buffer);
364 }
365 else
366 {
367 int new_length = stream->length_buffer + length_buffer;
368 if (new_length >= stream->sizeof_buffer)
369 {
370 stream->sizeof_buffer = new_length;
371 stream->buffer = xrealloc (stream->buffer, stream->sizeof_buffer);
372 }
373 memcpy (stream->buffer + stream->length_buffer, buffer, length_buffer);
374 stream->length_buffer = new_length;
375 }
376 }
377 \f
378 /* ``struct ui_file'' implementation that maps directly onto
379 <stdio.h>'s FILE. */
380
381 static ui_file_write_ftype stdio_file_write;
382 static ui_file_fputs_ftype stdio_file_fputs;
383 static ui_file_isatty_ftype stdio_file_isatty;
384 static ui_file_delete_ftype stdio_file_delete;
385 static struct ui_file *stdio_file_new (FILE * file, int close_p);
386 static ui_file_flush_ftype stdio_file_flush;
387
388 static int stdio_file_magic;
389
390 struct stdio_file
391 {
392 int *magic;
393 FILE *file;
394 int close_p;
395 };
396
397 static struct ui_file *
398 stdio_file_new (FILE *file, int close_p)
399 {
400 struct ui_file *ui_file = ui_file_new ();
401 struct stdio_file *stdio = xmalloc (sizeof (struct stdio_file));
402 stdio->magic = &stdio_file_magic;
403 stdio->file = file;
404 stdio->close_p = close_p;
405 set_ui_file_data (ui_file, stdio, stdio_file_delete);
406 set_ui_file_flush (ui_file, stdio_file_flush);
407 set_ui_file_write (ui_file, stdio_file_write);
408 set_ui_file_fputs (ui_file, stdio_file_fputs);
409 set_ui_file_isatty (ui_file, stdio_file_isatty);
410 return ui_file;
411 }
412
413 static void
414 stdio_file_delete (struct ui_file *file)
415 {
416 struct stdio_file *stdio = ui_file_data (file);
417 if (stdio->magic != &stdio_file_magic)
418 internal_error ("stdio_file_delete: bad magic number");
419 if (stdio->close_p)
420 {
421 fclose (stdio->file);
422 }
423 free (stdio);
424 }
425
426 static void
427 stdio_file_flush (struct ui_file *file)
428 {
429 struct stdio_file *stdio = ui_file_data (file);
430 if (stdio->magic != &stdio_file_magic)
431 internal_error ("stdio_file_flush: bad magic number");
432 fflush (stdio->file);
433 }
434
435 static void
436 stdio_file_write (struct ui_file *file, const char *buf, long length_buf)
437 {
438 struct stdio_file *stdio = ui_file_data (file);
439 if (stdio->magic != &stdio_file_magic)
440 internal_error ("stdio_file_write: bad magic number");
441 fwrite (buf, length_buf, 1, stdio->file);
442 }
443
444 static void
445 stdio_file_fputs (const char *linebuffer, struct ui_file *file)
446 {
447 struct stdio_file *stdio = ui_file_data (file);
448 if (stdio->magic != &stdio_file_magic)
449 internal_error ("stdio_file_fputs: bad magic number");
450 fputs (linebuffer, stdio->file);
451 }
452
453 static int
454 stdio_file_isatty (struct ui_file *file)
455 {
456 struct stdio_file *stdio = ui_file_data (file);
457 if (stdio->magic != &stdio_file_magic)
458 internal_error ("stdio_file_isatty: bad magic number");
459 return (isatty (fileno (stdio->file)));
460 }
461
462 /* Like fdopen(). Create a ui_file from a previously opened FILE. */
463
464 struct ui_file *
465 stdio_fileopen (FILE *file)
466 {
467 return stdio_file_new (file, 0);
468 }
469
470 struct ui_file *
471 gdb_fopen (char *name, char *mode)
472 {
473 FILE *f = fopen (name, mode);
474 if (f == NULL)
475 return NULL;
476 return stdio_file_new (f, 1);
477 }
This page took 0.0537 seconds and 5 git commands to generate.