~ubuntu-branches/ubuntu/utopic/crash/utopic-updates

« back to all changes in this revision

Viewing changes to debian/gdb-6.1.back/gdb/ui-file.c

  • Committer: Bazaar Package Importer
  • Author(s): Kenneth Drake
  • Date: 2007-11-20 21:57:10 UTC
  • mfrom: (0.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20071120215710-ch6kuysjzt8x4arw
Tags: 4.0-4.9-1ubuntu1
* Merge from debian unstable, remaining changes:
  - LP: #164229
  - debian/patches/01_spu_commands.dpatch:
    + SPU extension support
  - debian/rules:
    + Build SPU on powerpc
  - 01_task_info_to_stack.dpatch:
    + Applied upstream, removed.
  - debian/control:
    + Set Maintainer to Ubuntu MOTU Developers.
    + Set XSBC-Original-Maintainer.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* UI_FILE - a generic STDIO like output stream.
 
2
 
 
3
   Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
 
4
   Portions Copyright (C) 2001, 2002 Mission Critical Linux, Inc.
 
5
   Copyright (c) 2002, 2003, 2004, 2005 Red Hat, Inc. All rights reserved.
 
6
 
 
7
   This file is part of GDB.
 
8
 
 
9
   This program is free software; you can redistribute it and/or modify
 
10
   it under the terms of the GNU General Public License as published by
 
11
   the Free Software Foundation; either version 2 of the License, or
 
12
   (at your option) any later version.
 
13
 
 
14
   This program is distributed in the hope that it will be useful,
 
15
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
   GNU General Public License for more details.
 
18
 
 
19
   You should have received a copy of the GNU General Public License
 
20
   along with this program; if not, write to the Free Software
 
21
   Foundation, Inc., 59 Temple Place - Suite 330,
 
22
   Boston, MA 02111-1307, USA.  */
 
23
 
 
24
/* Implement the ``struct ui_file'' object. */
 
25
 
 
26
#include "defs.h"
 
27
#include "ui-file.h"
 
28
#include "gdb_string.h"
 
29
 
 
30
#include <errno.h>
 
31
 
 
32
static ui_file_isatty_ftype null_file_isatty;
 
33
static ui_file_write_ftype null_file_write;
 
34
static ui_file_fputs_ftype null_file_fputs;
 
35
static ui_file_read_ftype null_file_read;
 
36
static ui_file_flush_ftype null_file_flush;
 
37
static ui_file_delete_ftype null_file_delete;
 
38
static ui_file_rewind_ftype null_file_rewind;
 
39
static ui_file_put_ftype null_file_put;
 
40
 
 
41
struct ui_file
 
42
  {
 
43
    int *magic;
 
44
    ui_file_flush_ftype *to_flush;
 
45
    ui_file_write_ftype *to_write;
 
46
    ui_file_fputs_ftype *to_fputs;
 
47
    ui_file_read_ftype *to_read;
 
48
    ui_file_delete_ftype *to_delete;
 
49
    ui_file_isatty_ftype *to_isatty;
 
50
    ui_file_rewind_ftype *to_rewind;
 
51
    ui_file_put_ftype *to_put;
 
52
    void *to_data;
 
53
  };
 
54
int ui_file_magic;
 
55
 
 
56
struct ui_file *
 
57
ui_file_new (void)
 
58
{
 
59
  struct ui_file *file = xmalloc (sizeof (struct ui_file));
 
60
  file->magic = &ui_file_magic;
 
61
  set_ui_file_data (file, NULL, null_file_delete);
 
62
  set_ui_file_flush (file, null_file_flush);
 
63
  set_ui_file_write (file, null_file_write);
 
64
  set_ui_file_fputs (file, null_file_fputs);
 
65
  set_ui_file_read (file, null_file_read);
 
66
  set_ui_file_isatty (file, null_file_isatty);
 
67
  set_ui_file_rewind (file, null_file_rewind);
 
68
  set_ui_file_put (file, null_file_put);
 
69
  return file;
 
70
}
 
71
 
 
72
void
 
73
ui_file_delete (struct ui_file *file)
 
74
{
 
75
  file->to_delete (file);
 
76
  xfree (file);
 
77
}
 
78
 
 
79
static int
 
80
null_file_isatty (struct ui_file *file)
 
81
{
 
82
  return 0;
 
83
}
 
84
 
 
85
static void
 
86
null_file_rewind (struct ui_file *file)
 
87
{
 
88
  return;
 
89
}
 
90
 
 
91
static void
 
92
null_file_put (struct ui_file *file,
 
93
               ui_file_put_method_ftype *write,
 
94
               void *dest)
 
95
{
 
96
  return;
 
97
}
 
98
 
 
99
static void
 
100
null_file_flush (struct ui_file *file)
 
101
{
 
102
  return;
 
103
}
 
104
 
 
105
static void
 
106
null_file_write (struct ui_file *file,
 
107
                 const char *buf,
 
108
                 long sizeof_buf)
 
109
{
 
110
  if (file->to_fputs == null_file_fputs)
 
111
    /* Both the write and fputs methods are null. Discard the
 
112
       request. */
 
113
    return;
 
114
  else
 
115
    {
 
116
      /* The fputs method isn't null, slowly pass the write request
 
117
         onto that.  FYI, this isn't as bad as it may look - the
 
118
         current (as of 1999-11-07) printf_* function calls fputc and
 
119
         fputc does exactly the below.  By having a write function it
 
120
         is possible to clean up that code.  */
 
121
      int i;
 
122
      char b[2];
 
123
      b[1] = '\0';
 
124
      for (i = 0; i < sizeof_buf; i++)
 
125
        {
 
126
          b[0] = buf[i];
 
127
          file->to_fputs (b, file);
 
128
        }
 
129
      return;
 
130
    }
 
131
}
 
132
 
 
133
static long
 
134
null_file_read (struct ui_file *file,
 
135
                char *buf,
 
136
                long sizeof_buf)
 
137
{
 
138
  errno = EBADF;
 
139
  return 0;
 
140
}
 
141
 
 
142
static void
 
143
null_file_fputs (const char *buf, struct ui_file *file)
 
144
{
 
145
  if (file->to_write == null_file_write)
 
146
    /* Both the write and fputs methods are null. Discard the
 
147
       request. */
 
148
    return;
 
149
  else
 
150
    {
 
151
      /* The write method was implemented, use that. */
 
152
      file->to_write (file, buf, strlen (buf));
 
153
    }
 
154
}
 
155
 
 
156
static void
 
157
null_file_delete (struct ui_file *file)
 
158
{
 
159
  return;
 
160
}
 
161
 
 
162
void *
 
163
ui_file_data (struct ui_file *file)
 
164
{
 
165
  if (file->magic != &ui_file_magic)
 
166
    internal_error (__FILE__, __LINE__,
 
167
                    "ui_file_data: bad magic number");
 
168
  return file->to_data;
 
169
}
 
170
 
 
171
void
 
172
gdb_flush (struct ui_file *file)
 
173
{
 
174
  file->to_flush (file);
 
175
}
 
176
 
 
177
int
 
178
ui_file_isatty (struct ui_file *file)
 
179
{
 
180
  return file->to_isatty (file);
 
181
}
 
182
 
 
183
void
 
184
ui_file_rewind (struct ui_file *file)
 
185
{
 
186
  file->to_rewind (file);
 
187
}
 
188
 
 
189
void
 
190
ui_file_put (struct ui_file *file,
 
191
              ui_file_put_method_ftype *write,
 
192
              void *dest)
 
193
{
 
194
  file->to_put (file, write, dest);
 
195
}
 
196
 
 
197
void
 
198
ui_file_write (struct ui_file *file,
 
199
                const char *buf,
 
200
                long length_buf)
 
201
{
 
202
  file->to_write (file, buf, length_buf);
 
203
}
 
204
 
 
205
long
 
206
ui_file_read (struct ui_file *file, char *buf, long length_buf)
 
207
{
 
208
  return file->to_read (file, buf, length_buf); 
 
209
}
 
210
 
 
211
void
 
212
fputs_unfiltered (const char *buf, struct ui_file *file)
 
213
{
 
214
  file->to_fputs (buf, file);
 
215
}
 
216
 
 
217
void
 
218
set_ui_file_flush (struct ui_file *file, ui_file_flush_ftype *flush)
 
219
{
 
220
  file->to_flush = flush;
 
221
}
 
222
 
 
223
void
 
224
set_ui_file_isatty (struct ui_file *file, ui_file_isatty_ftype *isatty)
 
225
{
 
226
  file->to_isatty = isatty;
 
227
}
 
228
 
 
229
void
 
230
set_ui_file_rewind (struct ui_file *file, ui_file_rewind_ftype *rewind)
 
231
{
 
232
  file->to_rewind = rewind;
 
233
}
 
234
 
 
235
void
 
236
set_ui_file_put (struct ui_file *file, ui_file_put_ftype *put)
 
237
{
 
238
  file->to_put = put;
 
239
}
 
240
 
 
241
void
 
242
set_ui_file_write (struct ui_file *file,
 
243
                    ui_file_write_ftype *write)
 
244
{
 
245
  file->to_write = write;
 
246
}
 
247
 
 
248
void
 
249
set_ui_file_read (struct ui_file *file, ui_file_read_ftype *read)
 
250
{
 
251
  file->to_read = read;
 
252
}
 
253
 
 
254
void
 
255
set_ui_file_fputs (struct ui_file *file, ui_file_fputs_ftype *fputs)
 
256
{
 
257
  file->to_fputs = fputs;
 
258
}
 
259
 
 
260
void
 
261
set_ui_file_data (struct ui_file *file, void *data,
 
262
                  ui_file_delete_ftype *delete)
 
263
{
 
264
  file->to_data = data;
 
265
  file->to_delete = delete;
 
266
}
 
267
 
 
268
/* ui_file utility function for converting a ``struct ui_file'' into
 
269
   a memory buffer''. */
 
270
 
 
271
struct accumulated_ui_file
 
272
{
 
273
  char *buffer;
 
274
  long length;
 
275
};
 
276
 
 
277
static void
 
278
do_ui_file_xstrdup (void *context, const char *buffer, long length)
 
279
{
 
280
  struct accumulated_ui_file *acc = context;
 
281
  if (acc->buffer == NULL)
 
282
    acc->buffer = xmalloc (length + 1);
 
283
  else
 
284
    acc->buffer = xrealloc (acc->buffer, acc->length + length + 1);
 
285
  memcpy (acc->buffer + acc->length, buffer, length);
 
286
  acc->length += length;
 
287
  acc->buffer[acc->length] = '\0';
 
288
}
 
289
 
 
290
char *
 
291
ui_file_xstrdup (struct ui_file *file,
 
292
                  long *length)
 
293
{
 
294
  struct accumulated_ui_file acc;
 
295
  acc.buffer = NULL;
 
296
  acc.length = 0;
 
297
  ui_file_put (file, do_ui_file_xstrdup, &acc);
 
298
  if (acc.buffer == NULL)
 
299
    acc.buffer = xstrdup ("");
 
300
  *length = acc.length;
 
301
  return acc.buffer;
 
302
}
 
303
 
 
304
/* A pure memory based ``struct ui_file'' that can be used an output
 
305
   buffer. The buffers accumulated contents are available via
 
306
   ui_file_put(). */
 
307
 
 
308
struct mem_file
 
309
  {
 
310
    int *magic;
 
311
    char *buffer;
 
312
    int sizeof_buffer;
 
313
    int length_buffer;
 
314
  };
 
315
 
 
316
static ui_file_rewind_ftype mem_file_rewind;
 
317
static ui_file_put_ftype mem_file_put;
 
318
static ui_file_write_ftype mem_file_write;
 
319
static ui_file_delete_ftype mem_file_delete;
 
320
static struct ui_file *mem_file_new (void);
 
321
static int mem_file_magic;
 
322
 
 
323
static struct ui_file *
 
324
mem_file_new (void)
 
325
{
 
326
  struct mem_file *stream = XMALLOC (struct mem_file);
 
327
  struct ui_file *file = ui_file_new ();
 
328
  set_ui_file_data (file, stream, mem_file_delete);
 
329
  set_ui_file_rewind (file, mem_file_rewind);
 
330
  set_ui_file_put (file, mem_file_put);
 
331
  set_ui_file_write (file, mem_file_write);
 
332
  stream->magic = &mem_file_magic;
 
333
  stream->buffer = NULL;
 
334
  stream->sizeof_buffer = 0;
 
335
  stream->length_buffer = 0;
 
336
  return file;
 
337
}
 
338
 
 
339
static void
 
340
mem_file_delete (struct ui_file *file)
 
341
{
 
342
  struct mem_file *stream = ui_file_data (file);
 
343
  if (stream->magic != &mem_file_magic)
 
344
    internal_error (__FILE__, __LINE__,
 
345
                    "mem_file_delete: bad magic number");
 
346
  if (stream->buffer != NULL)
 
347
    xfree (stream->buffer);
 
348
  xfree (stream);
 
349
}
 
350
 
 
351
struct ui_file *
 
352
mem_fileopen (void)
 
353
{
 
354
  return mem_file_new ();
 
355
}
 
356
 
 
357
static void
 
358
mem_file_rewind (struct ui_file *file)
 
359
{
 
360
  struct mem_file *stream = ui_file_data (file);
 
361
  if (stream->magic != &mem_file_magic)
 
362
    internal_error (__FILE__, __LINE__,
 
363
                    "mem_file_rewind: bad magic number");
 
364
  stream->length_buffer = 0;
 
365
}
 
366
 
 
367
static void
 
368
mem_file_put (struct ui_file *file,
 
369
              ui_file_put_method_ftype *write,
 
370
              void *dest)
 
371
{
 
372
  struct mem_file *stream = ui_file_data (file);
 
373
  if (stream->magic != &mem_file_magic)
 
374
    internal_error (__FILE__, __LINE__,
 
375
                    "mem_file_put: bad magic number");
 
376
  if (stream->length_buffer > 0)
 
377
    write (dest, stream->buffer, stream->length_buffer);
 
378
}
 
379
 
 
380
void
 
381
mem_file_write (struct ui_file *file,
 
382
                const char *buffer,
 
383
                long length_buffer)
 
384
{
 
385
  struct mem_file *stream = ui_file_data (file);
 
386
  if (stream->magic != &mem_file_magic)
 
387
    internal_error (__FILE__, __LINE__,
 
388
                    "mem_file_write: bad magic number");
 
389
  if (stream->buffer == NULL)
 
390
    {
 
391
      stream->length_buffer = length_buffer;
 
392
      stream->sizeof_buffer = length_buffer;
 
393
      stream->buffer = xmalloc (stream->sizeof_buffer);
 
394
      memcpy (stream->buffer, buffer, length_buffer);
 
395
    }
 
396
  else
 
397
    {
 
398
      int new_length = stream->length_buffer + length_buffer;
 
399
      if (new_length >= stream->sizeof_buffer)
 
400
        {
 
401
          stream->sizeof_buffer = new_length;
 
402
          stream->buffer = xrealloc (stream->buffer, stream->sizeof_buffer);
 
403
        }
 
404
      memcpy (stream->buffer + stream->length_buffer, buffer, length_buffer);
 
405
      stream->length_buffer = new_length;
 
406
    }
 
407
}
 
408
 
 
409
/* ``struct ui_file'' implementation that maps directly onto
 
410
   <stdio.h>'s FILE. */
 
411
 
 
412
static ui_file_write_ftype stdio_file_write;
 
413
static ui_file_fputs_ftype stdio_file_fputs;
 
414
static ui_file_read_ftype stdio_file_read;
 
415
static ui_file_isatty_ftype stdio_file_isatty;
 
416
static ui_file_delete_ftype stdio_file_delete;
 
417
static struct ui_file *stdio_file_new (FILE * file, int close_p);
 
418
static ui_file_flush_ftype stdio_file_flush;
 
419
 
 
420
static int stdio_file_magic;
 
421
 
 
422
struct stdio_file
 
423
  {
 
424
    int *magic;
 
425
    FILE *file;
 
426
    int close_p;
 
427
  };
 
428
 
 
429
static struct ui_file *
 
430
stdio_file_new (FILE *file, int close_p)
 
431
{
 
432
  struct ui_file *ui_file = ui_file_new ();
 
433
  struct stdio_file *stdio = xmalloc (sizeof (struct stdio_file));
 
434
  stdio->magic = &stdio_file_magic;
 
435
  stdio->file = file;
 
436
  stdio->close_p = close_p;
 
437
  set_ui_file_data (ui_file, stdio, stdio_file_delete);
 
438
  set_ui_file_flush (ui_file, stdio_file_flush);
 
439
  set_ui_file_write (ui_file, stdio_file_write);
 
440
  set_ui_file_fputs (ui_file, stdio_file_fputs);
 
441
  set_ui_file_read (ui_file, stdio_file_read);
 
442
  set_ui_file_isatty (ui_file, stdio_file_isatty);
 
443
  return ui_file;
 
444
}
 
445
 
 
446
static void
 
447
stdio_file_delete (struct ui_file *file)
 
448
{
 
449
  struct stdio_file *stdio = ui_file_data (file);
 
450
  if (stdio->magic != &stdio_file_magic)
 
451
    internal_error (__FILE__, __LINE__,
 
452
                    "stdio_file_delete: bad magic number");
 
453
  if (stdio->close_p)
 
454
    {
 
455
      fclose (stdio->file);
 
456
    }
 
457
  xfree (stdio);
 
458
}
 
459
 
 
460
static void
 
461
stdio_file_flush (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_flush: bad magic number");
 
467
  fflush (stdio->file);
 
468
}
 
469
 
 
470
static long
 
471
stdio_file_read (struct ui_file *file, char *buf, long length_buf)
 
472
{
 
473
  struct stdio_file *stdio = ui_file_data (file);
 
474
  if (stdio->magic != &stdio_file_magic)
 
475
    internal_error (__FILE__, __LINE__,
 
476
                    "stdio_file_read: bad magic number");
 
477
  return read (fileno (stdio->file), buf, length_buf);
 
478
}
 
479
 
 
480
static void
 
481
stdio_file_write (struct ui_file *file, const char *buf, long length_buf)
 
482
{
 
483
  struct stdio_file *stdio = ui_file_data (file);
 
484
  if (stdio->magic != &stdio_file_magic)
 
485
    internal_error (__FILE__, __LINE__,
 
486
                    "stdio_file_write: bad magic number");
 
487
  fwrite (buf, length_buf, 1, stdio->file);
 
488
}
 
489
 
 
490
static void
 
491
stdio_file_fputs (const char *linebuffer, struct ui_file *file)
 
492
{
 
493
  struct stdio_file *stdio = ui_file_data (file);
 
494
  if (stdio->magic != &stdio_file_magic)
 
495
    internal_error (__FILE__, __LINE__,
 
496
                    "stdio_file_fputs: bad magic number");
 
497
  fputs (linebuffer, stdio->file);
 
498
}
 
499
 
 
500
static int
 
501
stdio_file_isatty (struct ui_file *file)
 
502
{
 
503
  struct stdio_file *stdio = ui_file_data (file);
 
504
  if (stdio->magic != &stdio_file_magic)
 
505
    internal_error (__FILE__, __LINE__,
 
506
                    "stdio_file_isatty: bad magic number");
 
507
  return (isatty (fileno (stdio->file)));
 
508
}
 
509
 
 
510
/* Like fdopen().  Create a ui_file from a previously opened FILE. */
 
511
 
 
512
struct ui_file *
 
513
stdio_fileopen (FILE *file)
 
514
{
 
515
  return stdio_file_new (file, 0);
 
516
}
 
517
 
 
518
struct ui_file *
 
519
gdb_fopen (char *name, char *mode)
 
520
{
 
521
  FILE *f = fopen (name, mode);
 
522
  if (f == NULL)
 
523
    return NULL;
 
524
  return stdio_file_new (f, 1);
 
525
}
 
526
 
 
527
#ifdef CRASH_MERGE
 
528
void
 
529
replace_ui_file_FILE(struct ui_file *file, FILE *fp)
 
530
{
 
531
        struct stdio_file *stdio_file;
 
532
 
 
533
        stdio_file = (struct stdio_file *)ui_file_data(file);
 
534
        stdio_file->file = fp;
 
535
}
 
536
#endif
 
537
 
 
538
/* ``struct ui_file'' implementation that maps onto two ui-file objects.  */
 
539
 
 
540
static ui_file_write_ftype tee_file_write;
 
541
static ui_file_fputs_ftype tee_file_fputs;
 
542
static ui_file_isatty_ftype tee_file_isatty;
 
543
static ui_file_delete_ftype tee_file_delete;
 
544
static ui_file_flush_ftype tee_file_flush;
 
545
 
 
546
static int tee_file_magic;
 
547
 
 
548
struct tee_file
 
549
  {
 
550
    int *magic;
 
551
    struct ui_file *one, *two;
 
552
    int close_one, close_two;
 
553
  };
 
554
 
 
555
struct ui_file *
 
556
tee_file_new (struct ui_file *one, int close_one,
 
557
              struct ui_file *two, int close_two)
 
558
{
 
559
  struct ui_file *ui_file = ui_file_new ();
 
560
  struct tee_file *tee = xmalloc (sizeof (struct tee_file));
 
561
  tee->magic = &tee_file_magic;
 
562
  tee->one = one;
 
563
  tee->two = two;
 
564
  tee->close_one = close_one;
 
565
  tee->close_two = close_two;
 
566
  set_ui_file_data (ui_file, tee, tee_file_delete);
 
567
  set_ui_file_flush (ui_file, tee_file_flush);
 
568
  set_ui_file_write (ui_file, tee_file_write);
 
569
  set_ui_file_fputs (ui_file, tee_file_fputs);
 
570
  set_ui_file_isatty (ui_file, tee_file_isatty);
 
571
  return ui_file;
 
572
}
 
573
 
 
574
static void
 
575
tee_file_delete (struct ui_file *file)
 
576
{
 
577
  struct tee_file *tee = ui_file_data (file);
 
578
  if (tee->magic != &tee_file_magic)
 
579
    internal_error (__FILE__, __LINE__,
 
580
                    "tee_file_delete: bad magic number");
 
581
  if (tee->close_one)
 
582
    ui_file_delete (tee->one);
 
583
  if (tee->close_two)
 
584
    ui_file_delete (tee->two);
 
585
 
 
586
  xfree (tee);
 
587
}
 
588
 
 
589
static void
 
590
tee_file_flush (struct ui_file *file)
 
591
{
 
592
  struct tee_file *tee = ui_file_data (file);
 
593
  if (tee->magic != &tee_file_magic)
 
594
    internal_error (__FILE__, __LINE__,
 
595
                    "tee_file_flush: bad magic number");
 
596
  tee->one->to_flush (tee->one);
 
597
  tee->two->to_flush (tee->two);
 
598
}
 
599
 
 
600
static void
 
601
tee_file_write (struct ui_file *file, const char *buf, long length_buf)
 
602
{
 
603
  struct tee_file *tee = ui_file_data (file);
 
604
  if (tee->magic != &tee_file_magic)
 
605
    internal_error (__FILE__, __LINE__,
 
606
                    "tee_file_write: bad magic number");
 
607
  ui_file_write (tee->one, buf, length_buf);
 
608
  ui_file_write (tee->two, buf, length_buf);
 
609
}
 
610
 
 
611
static void
 
612
tee_file_fputs (const char *linebuffer, struct ui_file *file)
 
613
{
 
614
  struct tee_file *tee = ui_file_data (file);
 
615
  if (tee->magic != &tee_file_magic)
 
616
    internal_error (__FILE__, __LINE__,
 
617
                    "tee_file_fputs: bad magic number");
 
618
  tee->one->to_fputs (linebuffer, tee->one);
 
619
  tee->two->to_fputs (linebuffer, tee->two);
 
620
}
 
621
 
 
622
static int
 
623
tee_file_isatty (struct ui_file *file)
 
624
{
 
625
  struct tee_file *tee = ui_file_data (file);
 
626
  if (tee->magic != &tee_file_magic)
 
627
    internal_error (__FILE__, __LINE__,
 
628
                    "tee_file_isatty: bad magic number");
 
629
  return (0);
 
630
}