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