OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gdb-7.2/] [gdb/] [ui-file.c] - Blame information for rev 365

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 330 jeremybenn
/* UI_FILE - a generic STDIO like output stream.
2
 
3
   Copyright (C) 1999, 2000, 2001, 2002, 2007, 2008, 2009, 2010
4
   Free Software Foundation, Inc.
5
 
6
   This file is part of GDB.
7
 
8
   This program is free software; you can redistribute it and/or modify
9
   it under the terms of the GNU General Public License as published by
10
   the Free Software Foundation; either version 3 of the License, or
11
   (at your option) any later version.
12
 
13
   This program is distributed in the hope that it will be useful,
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
   GNU General Public License for more details.
17
 
18
   You should have received a copy of the GNU General Public License
19
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
 
21
/* Implement the ``struct ui_file'' object. */
22
 
23
#include "defs.h"
24
#include "ui-file.h"
25
#include "gdb_obstack.h"
26
#include "gdb_string.h"
27
#include "gdb_select.h"
28
 
29
#include <errno.h>
30
 
31
static ui_file_isatty_ftype null_file_isatty;
32
static ui_file_write_ftype null_file_write;
33
static ui_file_fputs_ftype null_file_fputs;
34
static ui_file_read_ftype null_file_read;
35
static ui_file_flush_ftype null_file_flush;
36
static ui_file_delete_ftype null_file_delete;
37
static ui_file_rewind_ftype null_file_rewind;
38
static ui_file_put_ftype null_file_put;
39
 
40
struct ui_file
41
  {
42
    int *magic;
43
    ui_file_flush_ftype *to_flush;
44
    ui_file_write_ftype *to_write;
45
    ui_file_fputs_ftype *to_fputs;
46
    ui_file_read_ftype *to_read;
47
    ui_file_delete_ftype *to_delete;
48
    ui_file_isatty_ftype *to_isatty;
49
    ui_file_rewind_ftype *to_rewind;
50
    ui_file_put_ftype *to_put;
51
    void *to_data;
52
  };
53
int ui_file_magic;
54
 
55
struct ui_file *
56
ui_file_new (void)
57
{
58
  struct ui_file *file = xmalloc (sizeof (struct ui_file));
59
 
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
 
124
      b[1] = '\0';
125
      for (i = 0; i < sizeof_buf; i++)
126
        {
127
          b[0] = buf[i];
128
          file->to_fputs (b, file);
129
        }
130
      return;
131
    }
132
}
133
 
134
static long
135
null_file_read (struct ui_file *file,
136
                char *buf,
137
                long sizeof_buf)
138
{
139
  errno = EBADF;
140
  return 0;
141
}
142
 
143
static void
144
null_file_fputs (const char *buf, struct ui_file *file)
145
{
146
  if (file->to_write == null_file_write)
147
    /* Both the write and fputs methods are null. Discard the
148
       request. */
149
    return;
150
  else
151
    {
152
      /* The write method was implemented, use that. */
153
      file->to_write (file, buf, strlen (buf));
154
    }
155
}
156
 
157
static void
158
null_file_delete (struct ui_file *file)
159
{
160
  return;
161
}
162
 
163
void *
164
ui_file_data (struct ui_file *file)
165
{
166
  if (file->magic != &ui_file_magic)
167
    internal_error (__FILE__, __LINE__,
168
                    _("ui_file_data: bad magic number"));
169
  return file->to_data;
170
}
171
 
172
void
173
gdb_flush (struct ui_file *file)
174
{
175
  file->to_flush (file);
176
}
177
 
178
int
179
ui_file_isatty (struct ui_file *file)
180
{
181
  return file->to_isatty (file);
182
}
183
 
184
void
185
ui_file_rewind (struct ui_file *file)
186
{
187
  file->to_rewind (file);
188
}
189
 
190
void
191
ui_file_put (struct ui_file *file,
192
              ui_file_put_method_ftype *write,
193
              void *dest)
194
{
195
  file->to_put (file, write, dest);
196
}
197
 
198
void
199
ui_file_write (struct ui_file *file,
200
                const char *buf,
201
                long length_buf)
202
{
203
  file->to_write (file, buf, length_buf);
204
}
205
 
206
long
207
ui_file_read (struct ui_file *file, char *buf, long length_buf)
208
{
209
  return file->to_read (file, buf, length_buf);
210
}
211
 
212
void
213
fputs_unfiltered (const char *buf, struct ui_file *file)
214
{
215
  file->to_fputs (buf, file);
216
}
217
 
218
void
219
set_ui_file_flush (struct ui_file *file, ui_file_flush_ftype *flush)
220
{
221
  file->to_flush = flush;
222
}
223
 
224
void
225
set_ui_file_isatty (struct ui_file *file, ui_file_isatty_ftype *isatty)
226
{
227
  file->to_isatty = isatty;
228
}
229
 
230
void
231
set_ui_file_rewind (struct ui_file *file, ui_file_rewind_ftype *rewind)
232
{
233
  file->to_rewind = rewind;
234
}
235
 
236
void
237
set_ui_file_put (struct ui_file *file, ui_file_put_ftype *put)
238
{
239
  file->to_put = put;
240
}
241
 
242
void
243
set_ui_file_write (struct ui_file *file,
244
                    ui_file_write_ftype *write)
245
{
246
  file->to_write = write;
247
}
248
 
249
void
250
set_ui_file_read (struct ui_file *file, ui_file_read_ftype *read)
251
{
252
  file->to_read = read;
253
}
254
 
255
void
256
set_ui_file_fputs (struct ui_file *file, ui_file_fputs_ftype *fputs)
257
{
258
  file->to_fputs = fputs;
259
}
260
 
261
void
262
set_ui_file_data (struct ui_file *file, void *data,
263
                  ui_file_delete_ftype *delete)
264
{
265
  file->to_data = data;
266
  file->to_delete = delete;
267
}
268
 
269
/* ui_file utility function for converting a ``struct ui_file'' into
270
   a memory buffer. */
271
 
272
struct accumulated_ui_file
273
{
274
  char *buffer;
275
  long length;
276
};
277
 
278
static void
279
do_ui_file_xstrdup (void *context, const char *buffer, long length)
280
{
281
  struct accumulated_ui_file *acc = context;
282
 
283
  if (acc->buffer == NULL)
284
    acc->buffer = xmalloc (length + 1);
285
  else
286
    acc->buffer = xrealloc (acc->buffer, acc->length + length + 1);
287
  memcpy (acc->buffer + acc->length, buffer, length);
288
  acc->length += length;
289
  acc->buffer[acc->length] = '\0';
290
}
291
 
292
char *
293
ui_file_xstrdup (struct ui_file *file, long *length)
294
{
295
  struct accumulated_ui_file acc;
296
 
297
  acc.buffer = NULL;
298
  acc.length = 0;
299
  ui_file_put (file, do_ui_file_xstrdup, &acc);
300
  if (acc.buffer == NULL)
301
    acc.buffer = xstrdup ("");
302
  if (length != NULL)
303
    *length = acc.length;
304
  return acc.buffer;
305
}
306
 
307
static void
308
do_ui_file_obsavestring (void *context, const char *buffer, long length)
309
{
310
  struct obstack *obstack = (struct obstack *) context;
311
 
312
  obstack_grow (obstack, buffer, length);
313
}
314
 
315
char *
316
ui_file_obsavestring (struct ui_file *file, struct obstack *obstack,
317
                      long *length)
318
{
319
  ui_file_put (file, do_ui_file_obsavestring, obstack);
320
  *length = obstack_object_size (obstack);
321
  obstack_1grow (obstack, '\0');
322
  return obstack_finish (obstack);
323
}
324
 
325
/* A pure memory based ``struct ui_file'' that can be used an output
326
   buffer. The buffers accumulated contents are available via
327
   ui_file_put(). */
328
 
329
struct mem_file
330
  {
331
    int *magic;
332
    char *buffer;
333
    int sizeof_buffer;
334
    int length_buffer;
335
  };
336
 
337
static ui_file_rewind_ftype mem_file_rewind;
338
static ui_file_put_ftype mem_file_put;
339
static ui_file_write_ftype mem_file_write;
340
static ui_file_delete_ftype mem_file_delete;
341
static struct ui_file *mem_file_new (void);
342
static int mem_file_magic;
343
 
344
static struct ui_file *
345
mem_file_new (void)
346
{
347
  struct mem_file *stream = XMALLOC (struct mem_file);
348
  struct ui_file *file = ui_file_new ();
349
 
350
  set_ui_file_data (file, stream, mem_file_delete);
351
  set_ui_file_rewind (file, mem_file_rewind);
352
  set_ui_file_put (file, mem_file_put);
353
  set_ui_file_write (file, mem_file_write);
354
  stream->magic = &mem_file_magic;
355
  stream->buffer = NULL;
356
  stream->sizeof_buffer = 0;
357
  stream->length_buffer = 0;
358
  return file;
359
}
360
 
361
static void
362
mem_file_delete (struct ui_file *file)
363
{
364
  struct mem_file *stream = ui_file_data (file);
365
 
366
  if (stream->magic != &mem_file_magic)
367
    internal_error (__FILE__, __LINE__,
368
                    _("mem_file_delete: bad magic number"));
369
  if (stream->buffer != NULL)
370
    xfree (stream->buffer);
371
  xfree (stream);
372
}
373
 
374
struct ui_file *
375
mem_fileopen (void)
376
{
377
  return mem_file_new ();
378
}
379
 
380
static void
381
mem_file_rewind (struct ui_file *file)
382
{
383
  struct mem_file *stream = ui_file_data (file);
384
 
385
  if (stream->magic != &mem_file_magic)
386
    internal_error (__FILE__, __LINE__,
387
                    _("mem_file_rewind: bad magic number"));
388
  stream->length_buffer = 0;
389
}
390
 
391
static void
392
mem_file_put (struct ui_file *file,
393
              ui_file_put_method_ftype *write,
394
              void *dest)
395
{
396
  struct mem_file *stream = ui_file_data (file);
397
 
398
  if (stream->magic != &mem_file_magic)
399
    internal_error (__FILE__, __LINE__,
400
                    _("mem_file_put: bad magic number"));
401
  if (stream->length_buffer > 0)
402
    write (dest, stream->buffer, stream->length_buffer);
403
}
404
 
405
void
406
mem_file_write (struct ui_file *file,
407
                const char *buffer,
408
                long length_buffer)
409
{
410
  struct mem_file *stream = ui_file_data (file);
411
 
412
  if (stream->magic != &mem_file_magic)
413
    internal_error (__FILE__, __LINE__,
414
                    _("mem_file_write: bad magic number"));
415
  if (stream->buffer == NULL)
416
    {
417
      stream->length_buffer = length_buffer;
418
      stream->sizeof_buffer = length_buffer;
419
      stream->buffer = xmalloc (stream->sizeof_buffer);
420
      memcpy (stream->buffer, buffer, length_buffer);
421
    }
422
  else
423
    {
424
      int new_length = stream->length_buffer + length_buffer;
425
 
426
      if (new_length >= stream->sizeof_buffer)
427
        {
428
          stream->sizeof_buffer = new_length;
429
          stream->buffer = xrealloc (stream->buffer, stream->sizeof_buffer);
430
        }
431
      memcpy (stream->buffer + stream->length_buffer, buffer, length_buffer);
432
      stream->length_buffer = new_length;
433
    }
434
}
435
 
436
/* ``struct ui_file'' implementation that maps directly onto
437
   <stdio.h>'s FILE. */
438
 
439
static ui_file_write_ftype stdio_file_write;
440
static ui_file_fputs_ftype stdio_file_fputs;
441
static ui_file_read_ftype stdio_file_read;
442
static ui_file_isatty_ftype stdio_file_isatty;
443
static ui_file_delete_ftype stdio_file_delete;
444
static struct ui_file *stdio_file_new (FILE * file, int close_p);
445
static ui_file_flush_ftype stdio_file_flush;
446
 
447
static int stdio_file_magic;
448
 
449
struct stdio_file
450
  {
451
    int *magic;
452
    FILE *file;
453
    int close_p;
454
  };
455
 
456
static struct ui_file *
457
stdio_file_new (FILE *file, int close_p)
458
{
459
  struct ui_file *ui_file = ui_file_new ();
460
  struct stdio_file *stdio = xmalloc (sizeof (struct stdio_file));
461
 
462
  stdio->magic = &stdio_file_magic;
463
  stdio->file = file;
464
  stdio->close_p = close_p;
465
  set_ui_file_data (ui_file, stdio, stdio_file_delete);
466
  set_ui_file_flush (ui_file, stdio_file_flush);
467
  set_ui_file_write (ui_file, stdio_file_write);
468
  set_ui_file_fputs (ui_file, stdio_file_fputs);
469
  set_ui_file_read (ui_file, stdio_file_read);
470
  set_ui_file_isatty (ui_file, stdio_file_isatty);
471
  return ui_file;
472
}
473
 
474
static void
475
stdio_file_delete (struct ui_file *file)
476
{
477
  struct stdio_file *stdio = ui_file_data (file);
478
 
479
  if (stdio->magic != &stdio_file_magic)
480
    internal_error (__FILE__, __LINE__,
481
                    _("stdio_file_delete: bad magic number"));
482
  if (stdio->close_p)
483
    {
484
      fclose (stdio->file);
485
    }
486
  xfree (stdio);
487
}
488
 
489
static void
490
stdio_file_flush (struct ui_file *file)
491
{
492
  struct stdio_file *stdio = ui_file_data (file);
493
 
494
  if (stdio->magic != &stdio_file_magic)
495
    internal_error (__FILE__, __LINE__,
496
                    _("stdio_file_flush: bad magic number"));
497
  fflush (stdio->file);
498
}
499
 
500
static long
501
stdio_file_read (struct ui_file *file, char *buf, long length_buf)
502
{
503
  struct stdio_file *stdio = ui_file_data (file);
504
 
505
  if (stdio->magic != &stdio_file_magic)
506
    internal_error (__FILE__, __LINE__,
507
                    _("stdio_file_read: bad magic number"));
508
 
509
  /* For the benefit of Windows, call gdb_select before reading from
510
     the file.  Wait until at least one byte of data is available.
511
     Control-C can interrupt gdb_select, but not read.  */
512
  {
513
    int fd = fileno (stdio->file);
514
 
515
    fd_set readfds;
516
    FD_ZERO (&readfds);
517
    FD_SET (fd, &readfds);
518
    if (gdb_select (fd + 1, &readfds, NULL, NULL, NULL) == -1)
519
      return -1;
520
  }
521
 
522
  return read (fileno (stdio->file), buf, length_buf);
523
}
524
 
525
static void
526
stdio_file_write (struct ui_file *file, const char *buf, long length_buf)
527
{
528
  struct stdio_file *stdio = ui_file_data (file);
529
 
530
  if (stdio->magic != &stdio_file_magic)
531
    internal_error (__FILE__, __LINE__,
532
                    _("stdio_file_write: bad magic number"));
533
  /* Calling error crashes when we are called from the exception framework.  */
534
  if (fwrite (buf, length_buf, 1, stdio->file))
535
    ;
536
}
537
 
538
static void
539
stdio_file_fputs (const char *linebuffer, struct ui_file *file)
540
{
541
  struct stdio_file *stdio = ui_file_data (file);
542
 
543
  if (stdio->magic != &stdio_file_magic)
544
    internal_error (__FILE__, __LINE__,
545
                    _("stdio_file_fputs: bad magic number"));
546
  /* Calling error crashes when we are called from the exception framework.  */
547
  if (fputs (linebuffer, stdio->file))
548
    ;
549
}
550
 
551
static int
552
stdio_file_isatty (struct ui_file *file)
553
{
554
  struct stdio_file *stdio = ui_file_data (file);
555
 
556
  if (stdio->magic != &stdio_file_magic)
557
    internal_error (__FILE__, __LINE__,
558
                    _("stdio_file_isatty: bad magic number"));
559
  return (isatty (fileno (stdio->file)));
560
}
561
 
562
/* Like fdopen().  Create a ui_file from a previously opened FILE. */
563
 
564
struct ui_file *
565
stdio_fileopen (FILE *file)
566
{
567
  return stdio_file_new (file, 0);
568
}
569
 
570
struct ui_file *
571
gdb_fopen (char *name, char *mode)
572
{
573
  FILE *f = fopen (name, mode);
574
 
575
  if (f == NULL)
576
    return NULL;
577
  return stdio_file_new (f, 1);
578
}
579
 
580
/* ``struct ui_file'' implementation that maps onto two ui-file objects.  */
581
 
582
static ui_file_write_ftype tee_file_write;
583
static ui_file_fputs_ftype tee_file_fputs;
584
static ui_file_isatty_ftype tee_file_isatty;
585
static ui_file_delete_ftype tee_file_delete;
586
static ui_file_flush_ftype tee_file_flush;
587
 
588
static int tee_file_magic;
589
 
590
struct tee_file
591
  {
592
    int *magic;
593
    struct ui_file *one, *two;
594
    int close_one, close_two;
595
  };
596
 
597
struct ui_file *
598
tee_file_new (struct ui_file *one, int close_one,
599
              struct ui_file *two, int close_two)
600
{
601
  struct ui_file *ui_file = ui_file_new ();
602
  struct tee_file *tee = xmalloc (sizeof (struct tee_file));
603
 
604
  tee->magic = &tee_file_magic;
605
  tee->one = one;
606
  tee->two = two;
607
  tee->close_one = close_one;
608
  tee->close_two = close_two;
609
  set_ui_file_data (ui_file, tee, tee_file_delete);
610
  set_ui_file_flush (ui_file, tee_file_flush);
611
  set_ui_file_write (ui_file, tee_file_write);
612
  set_ui_file_fputs (ui_file, tee_file_fputs);
613
  set_ui_file_isatty (ui_file, tee_file_isatty);
614
  return ui_file;
615
}
616
 
617
static void
618
tee_file_delete (struct ui_file *file)
619
{
620
  struct tee_file *tee = ui_file_data (file);
621
 
622
  if (tee->magic != &tee_file_magic)
623
    internal_error (__FILE__, __LINE__,
624
                    _("tee_file_delete: bad magic number"));
625
  if (tee->close_one)
626
    ui_file_delete (tee->one);
627
  if (tee->close_two)
628
    ui_file_delete (tee->two);
629
 
630
  xfree (tee);
631
}
632
 
633
static void
634
tee_file_flush (struct ui_file *file)
635
{
636
  struct tee_file *tee = ui_file_data (file);
637
 
638
  if (tee->magic != &tee_file_magic)
639
    internal_error (__FILE__, __LINE__,
640
                    _("tee_file_flush: bad magic number"));
641
  tee->one->to_flush (tee->one);
642
  tee->two->to_flush (tee->two);
643
}
644
 
645
static void
646
tee_file_write (struct ui_file *file, const char *buf, long length_buf)
647
{
648
  struct tee_file *tee = ui_file_data (file);
649
 
650
  if (tee->magic != &tee_file_magic)
651
    internal_error (__FILE__, __LINE__,
652
                    _("tee_file_write: bad magic number"));
653
  ui_file_write (tee->one, buf, length_buf);
654
  ui_file_write (tee->two, buf, length_buf);
655
}
656
 
657
static void
658
tee_file_fputs (const char *linebuffer, struct ui_file *file)
659
{
660
  struct tee_file *tee = ui_file_data (file);
661
 
662
  if (tee->magic != &tee_file_magic)
663
    internal_error (__FILE__, __LINE__,
664
                    _("tee_file_fputs: bad magic number"));
665
  tee->one->to_fputs (linebuffer, tee->one);
666
  tee->two->to_fputs (linebuffer, tee->two);
667
}
668
 
669
static int
670
tee_file_isatty (struct ui_file *file)
671
{
672
  struct tee_file *tee = ui_file_data (file);
673
 
674
  if (tee->magic != &tee_file_magic)
675
    internal_error (__FILE__, __LINE__,
676
                    _("tee_file_isatty: bad magic number"));
677
 
678
  return ui_file_isatty (tee->one);
679
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.