GDB (API)
/home/stan/gdb/src/gdb/ui-file.c
Go to the documentation of this file.
00001 /* UI_FILE - a generic STDIO like output stream.
00002 
00003    Copyright (C) 1999-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 /* Implement the ``struct ui_file'' object.  */
00021 
00022 #include "defs.h"
00023 #include "ui-file.h"
00024 #include "gdb_obstack.h"
00025 #include "gdb_string.h"
00026 #include "gdb_select.h"
00027 #include "filestuff.h"
00028 
00029 #include <errno.h>
00030 
00031 static ui_file_isatty_ftype null_file_isatty;
00032 static ui_file_write_ftype null_file_write;
00033 static ui_file_write_ftype null_file_write_async_safe;
00034 static ui_file_fputs_ftype null_file_fputs;
00035 static ui_file_read_ftype null_file_read;
00036 static ui_file_flush_ftype null_file_flush;
00037 static ui_file_delete_ftype null_file_delete;
00038 static ui_file_rewind_ftype null_file_rewind;
00039 static ui_file_put_ftype null_file_put;
00040 static ui_file_fseek_ftype null_file_fseek;
00041 
00042 struct ui_file
00043   {
00044     int *magic;
00045     ui_file_flush_ftype *to_flush;
00046     ui_file_write_ftype *to_write;
00047     ui_file_write_async_safe_ftype *to_write_async_safe;
00048     ui_file_fputs_ftype *to_fputs;
00049     ui_file_read_ftype *to_read;
00050     ui_file_delete_ftype *to_delete;
00051     ui_file_isatty_ftype *to_isatty;
00052     ui_file_rewind_ftype *to_rewind;
00053     ui_file_put_ftype *to_put;
00054     ui_file_fseek_ftype *to_fseek;
00055     void *to_data;
00056   };
00057 int ui_file_magic;
00058 
00059 struct ui_file *
00060 ui_file_new (void)
00061 {
00062   struct ui_file *file = xmalloc (sizeof (struct ui_file));
00063 
00064   file->magic = &ui_file_magic;
00065   set_ui_file_data (file, NULL, null_file_delete);
00066   set_ui_file_flush (file, null_file_flush);
00067   set_ui_file_write (file, null_file_write);
00068   set_ui_file_write_async_safe (file, null_file_write_async_safe);
00069   set_ui_file_fputs (file, null_file_fputs);
00070   set_ui_file_read (file, null_file_read);
00071   set_ui_file_isatty (file, null_file_isatty);
00072   set_ui_file_rewind (file, null_file_rewind);
00073   set_ui_file_put (file, null_file_put);
00074   set_ui_file_fseek (file, null_file_fseek);
00075   return file;
00076 }
00077 
00078 void
00079 ui_file_delete (struct ui_file *file)
00080 {
00081   file->to_delete (file);
00082   xfree (file);
00083 }
00084 
00085 static int
00086 null_file_isatty (struct ui_file *file)
00087 {
00088   return 0;
00089 }
00090 
00091 static void
00092 null_file_rewind (struct ui_file *file)
00093 {
00094   return;
00095 }
00096 
00097 static void
00098 null_file_put (struct ui_file *file,
00099                ui_file_put_method_ftype *write,
00100                void *dest)
00101 {
00102   return;
00103 }
00104 
00105 static void
00106 null_file_flush (struct ui_file *file)
00107 {
00108   return;
00109 }
00110 
00111 static void
00112 null_file_write (struct ui_file *file,
00113                  const char *buf,
00114                  long sizeof_buf)
00115 {
00116   if (file->to_fputs == null_file_fputs)
00117     /* Both the write and fputs methods are null.  Discard the
00118        request.  */
00119     return;
00120   else
00121     {
00122       /* The fputs method isn't null, slowly pass the write request
00123          onto that.  FYI, this isn't as bad as it may look - the
00124          current (as of 1999-11-07) printf_* function calls fputc and
00125          fputc does exactly the below.  By having a write function it
00126          is possible to clean up that code.  */
00127       int i;
00128       char b[2];
00129 
00130       b[1] = '\0';
00131       for (i = 0; i < sizeof_buf; i++)
00132         {
00133           b[0] = buf[i];
00134           file->to_fputs (b, file);
00135         }
00136       return;
00137     }
00138 }
00139 
00140 static long
00141 null_file_read (struct ui_file *file,
00142                 char *buf,
00143                 long sizeof_buf)
00144 {
00145   errno = EBADF;
00146   return 0;
00147 }
00148 
00149 static void
00150 null_file_fputs (const char *buf, struct ui_file *file)
00151 {
00152   if (file->to_write == null_file_write)
00153     /* Both the write and fputs methods are null.  Discard the
00154        request.  */
00155     return;
00156   else
00157     {
00158       /* The write method was implemented, use that.  */
00159       file->to_write (file, buf, strlen (buf));
00160     }
00161 }
00162 
00163 static void
00164 null_file_write_async_safe (struct ui_file *file,
00165                             const char *buf,
00166                             long sizeof_buf)
00167 {
00168   return;
00169 }
00170 
00171 static void
00172 null_file_delete (struct ui_file *file)
00173 {
00174   return;
00175 }
00176 
00177 static int
00178 null_file_fseek (struct ui_file *stream, long offset, int whence)
00179 {
00180   errno = EBADF;
00181 
00182   return -1;
00183 }
00184 
00185 void *
00186 ui_file_data (struct ui_file *file)
00187 {
00188   if (file->magic != &ui_file_magic)
00189     internal_error (__FILE__, __LINE__,
00190                     _("ui_file_data: bad magic number"));
00191   return file->to_data;
00192 }
00193 
00194 void
00195 gdb_flush (struct ui_file *file)
00196 {
00197   file->to_flush (file);
00198 }
00199 
00200 int
00201 ui_file_isatty (struct ui_file *file)
00202 {
00203   return file->to_isatty (file);
00204 }
00205 
00206 void
00207 ui_file_rewind (struct ui_file *file)
00208 {
00209   file->to_rewind (file);
00210 }
00211 
00212 void
00213 ui_file_put (struct ui_file *file,
00214               ui_file_put_method_ftype *write,
00215               void *dest)
00216 {
00217   file->to_put (file, write, dest);
00218 }
00219 
00220 void
00221 ui_file_write (struct ui_file *file,
00222                 const char *buf,
00223                 long length_buf)
00224 {
00225   file->to_write (file, buf, length_buf);
00226 }
00227 
00228 void
00229 ui_file_write_async_safe (struct ui_file *file,
00230                           const char *buf,
00231                           long length_buf)
00232 {
00233   file->to_write_async_safe (file, buf, length_buf);
00234 }
00235 
00236 long
00237 ui_file_read (struct ui_file *file, char *buf, long length_buf)
00238 {
00239   return file->to_read (file, buf, length_buf); 
00240 }
00241 
00242 int
00243 ui_file_fseek (struct ui_file *file, long offset, int whence)
00244 {
00245   return file->to_fseek (file, offset, whence);
00246 }
00247 
00248 void
00249 fputs_unfiltered (const char *buf, struct ui_file *file)
00250 {
00251   file->to_fputs (buf, file);
00252 }
00253 
00254 void
00255 set_ui_file_flush (struct ui_file *file, ui_file_flush_ftype *flush_ptr)
00256 {
00257   file->to_flush = flush_ptr;
00258 }
00259 
00260 void
00261 set_ui_file_isatty (struct ui_file *file, ui_file_isatty_ftype *isatty_ptr)
00262 {
00263   file->to_isatty = isatty_ptr;
00264 }
00265 
00266 void
00267 set_ui_file_rewind (struct ui_file *file, ui_file_rewind_ftype *rewind_ptr)
00268 {
00269   file->to_rewind = rewind_ptr;
00270 }
00271 
00272 void
00273 set_ui_file_put (struct ui_file *file, ui_file_put_ftype *put_ptr)
00274 {
00275   file->to_put = put_ptr;
00276 }
00277 
00278 void
00279 set_ui_file_write (struct ui_file *file,
00280                     ui_file_write_ftype *write_ptr)
00281 {
00282   file->to_write = write_ptr;
00283 }
00284 
00285 void
00286 set_ui_file_write_async_safe (struct ui_file *file,
00287                               ui_file_write_async_safe_ftype *write_async_safe_ptr)
00288 {
00289   file->to_write_async_safe = write_async_safe_ptr;
00290 }
00291 
00292 void
00293 set_ui_file_read (struct ui_file *file, ui_file_read_ftype *read_ptr)
00294 {
00295   file->to_read = read_ptr;
00296 }
00297 
00298 void
00299 set_ui_file_fputs (struct ui_file *file, ui_file_fputs_ftype *fputs_ptr)
00300 {
00301   file->to_fputs = fputs_ptr;
00302 }
00303 
00304 void
00305 set_ui_file_fseek (struct ui_file *file, ui_file_fseek_ftype *fseek_ptr)
00306 {
00307   file->to_fseek = fseek_ptr;
00308 }
00309 
00310 void
00311 set_ui_file_data (struct ui_file *file, void *data,
00312                   ui_file_delete_ftype *delete_ptr)
00313 {
00314   file->to_data = data;
00315   file->to_delete = delete_ptr;
00316 }
00317 
00318 /* ui_file utility function for converting a ``struct ui_file'' into
00319    a memory buffer.  */
00320 
00321 struct accumulated_ui_file
00322 {
00323   char *buffer;
00324   long length;
00325 };
00326 
00327 static void
00328 do_ui_file_xstrdup (void *context, const char *buffer, long length)
00329 {
00330   struct accumulated_ui_file *acc = context;
00331 
00332   if (acc->buffer == NULL)
00333     acc->buffer = xmalloc (length + 1);
00334   else
00335     acc->buffer = xrealloc (acc->buffer, acc->length + length + 1);
00336   memcpy (acc->buffer + acc->length, buffer, length);
00337   acc->length += length;
00338   acc->buffer[acc->length] = '\0';
00339 }
00340 
00341 char *
00342 ui_file_xstrdup (struct ui_file *file, long *length)
00343 {
00344   struct accumulated_ui_file acc;
00345 
00346   acc.buffer = NULL;
00347   acc.length = 0;
00348   ui_file_put (file, do_ui_file_xstrdup, &acc);
00349   if (acc.buffer == NULL)
00350     acc.buffer = xstrdup ("");
00351   if (length != NULL)
00352     *length = acc.length;
00353   return acc.buffer;
00354 }
00355 
00356 static void
00357 do_ui_file_obsavestring (void *context, const char *buffer, long length)
00358 {
00359   struct obstack *obstack = (struct obstack *) context;
00360 
00361   obstack_grow (obstack, buffer, length);
00362 }
00363 
00364 char *
00365 ui_file_obsavestring (struct ui_file *file, struct obstack *obstack,
00366                       long *length)
00367 {
00368   ui_file_put (file, do_ui_file_obsavestring, obstack);
00369   *length = obstack_object_size (obstack);
00370   obstack_1grow (obstack, '\0');
00371   return obstack_finish (obstack);
00372 }
00373 
00374 /* A pure memory based ``struct ui_file'' that can be used an output
00375    buffer.  The buffers accumulated contents are available via
00376    ui_file_put().  */
00377 
00378 struct mem_file
00379   {
00380     int *magic;
00381     char *buffer;
00382     int sizeof_buffer;
00383     int length_buffer;
00384   };
00385 
00386 static ui_file_rewind_ftype mem_file_rewind;
00387 static ui_file_put_ftype mem_file_put;
00388 static ui_file_write_ftype mem_file_write;
00389 static ui_file_delete_ftype mem_file_delete;
00390 static struct ui_file *mem_file_new (void);
00391 static int mem_file_magic;
00392 
00393 static struct ui_file *
00394 mem_file_new (void)
00395 {
00396   struct mem_file *stream = XMALLOC (struct mem_file);
00397   struct ui_file *file = ui_file_new ();
00398 
00399   set_ui_file_data (file, stream, mem_file_delete);
00400   set_ui_file_rewind (file, mem_file_rewind);
00401   set_ui_file_put (file, mem_file_put);
00402   set_ui_file_write (file, mem_file_write);
00403   stream->magic = &mem_file_magic;
00404   stream->buffer = NULL;
00405   stream->sizeof_buffer = 0;
00406   stream->length_buffer = 0;
00407   return file;
00408 }
00409 
00410 static void
00411 mem_file_delete (struct ui_file *file)
00412 {
00413   struct mem_file *stream = ui_file_data (file);
00414 
00415   if (stream->magic != &mem_file_magic)
00416     internal_error (__FILE__, __LINE__,
00417                     _("mem_file_delete: bad magic number"));
00418   if (stream->buffer != NULL)
00419     xfree (stream->buffer);
00420   xfree (stream);
00421 }
00422 
00423 struct ui_file *
00424 mem_fileopen (void)
00425 {
00426   return mem_file_new ();
00427 }
00428 
00429 static void
00430 mem_file_rewind (struct ui_file *file)
00431 {
00432   struct mem_file *stream = ui_file_data (file);
00433 
00434   if (stream->magic != &mem_file_magic)
00435     internal_error (__FILE__, __LINE__,
00436                     _("mem_file_rewind: bad magic number"));
00437   stream->length_buffer = 0;
00438 }
00439 
00440 static void
00441 mem_file_put (struct ui_file *file,
00442               ui_file_put_method_ftype *write,
00443               void *dest)
00444 {
00445   struct mem_file *stream = ui_file_data (file);
00446 
00447   if (stream->magic != &mem_file_magic)
00448     internal_error (__FILE__, __LINE__,
00449                     _("mem_file_put: bad magic number"));
00450   if (stream->length_buffer > 0)
00451     write (dest, stream->buffer, stream->length_buffer);
00452 }
00453 
00454 void
00455 mem_file_write (struct ui_file *file,
00456                 const char *buffer,
00457                 long length_buffer)
00458 {
00459   struct mem_file *stream = ui_file_data (file);
00460 
00461   if (stream->magic != &mem_file_magic)
00462     internal_error (__FILE__, __LINE__,
00463                     _("mem_file_write: bad magic number"));
00464   if (stream->buffer == NULL)
00465     {
00466       stream->length_buffer = length_buffer;
00467       stream->sizeof_buffer = length_buffer;
00468       stream->buffer = xmalloc (stream->sizeof_buffer);
00469       memcpy (stream->buffer, buffer, length_buffer);
00470     }
00471   else
00472     {
00473       int new_length = stream->length_buffer + length_buffer;
00474 
00475       if (new_length >= stream->sizeof_buffer)
00476         {
00477           stream->sizeof_buffer = new_length;
00478           stream->buffer = xrealloc (stream->buffer, stream->sizeof_buffer);
00479         }
00480       memcpy (stream->buffer + stream->length_buffer, buffer, length_buffer);
00481       stream->length_buffer = new_length;
00482     }
00483 }
00484 
00485 /* ``struct ui_file'' implementation that maps directly onto
00486    <stdio.h>'s FILE.  */
00487 
00488 static ui_file_write_ftype stdio_file_write;
00489 static ui_file_write_async_safe_ftype stdio_file_write_async_safe;
00490 static ui_file_fputs_ftype stdio_file_fputs;
00491 static ui_file_read_ftype stdio_file_read;
00492 static ui_file_isatty_ftype stdio_file_isatty;
00493 static ui_file_delete_ftype stdio_file_delete;
00494 static struct ui_file *stdio_file_new (FILE *file, int close_p);
00495 static ui_file_flush_ftype stdio_file_flush;
00496 static ui_file_fseek_ftype stdio_file_fseek;
00497 
00498 static int stdio_file_magic;
00499 
00500 struct stdio_file
00501   {
00502     int *magic;
00503     FILE *file;
00504     /* The associated file descriptor is extracted ahead of time for
00505        stdio_file_write_async_safe's benefit, in case fileno isn't async-safe.  */
00506     int fd;
00507     int close_p;
00508   };
00509 
00510 static struct ui_file *
00511 stdio_file_new (FILE *file, int close_p)
00512 {
00513   struct ui_file *ui_file = ui_file_new ();
00514   struct stdio_file *stdio = xmalloc (sizeof (struct stdio_file));
00515 
00516   stdio->magic = &stdio_file_magic;
00517   stdio->file = file;
00518   stdio->fd = fileno (file);
00519   stdio->close_p = close_p;
00520   set_ui_file_data (ui_file, stdio, stdio_file_delete);
00521   set_ui_file_flush (ui_file, stdio_file_flush);
00522   set_ui_file_write (ui_file, stdio_file_write);
00523   set_ui_file_write_async_safe (ui_file, stdio_file_write_async_safe);
00524   set_ui_file_fputs (ui_file, stdio_file_fputs);
00525   set_ui_file_read (ui_file, stdio_file_read);
00526   set_ui_file_isatty (ui_file, stdio_file_isatty);
00527   set_ui_file_fseek (ui_file, stdio_file_fseek);
00528   return ui_file;
00529 }
00530 
00531 static void
00532 stdio_file_delete (struct ui_file *file)
00533 {
00534   struct stdio_file *stdio = ui_file_data (file);
00535 
00536   if (stdio->magic != &stdio_file_magic)
00537     internal_error (__FILE__, __LINE__,
00538                     _("stdio_file_delete: bad magic number"));
00539   if (stdio->close_p)
00540     {
00541       fclose (stdio->file);
00542     }
00543   xfree (stdio);
00544 }
00545 
00546 static void
00547 stdio_file_flush (struct ui_file *file)
00548 {
00549   struct stdio_file *stdio = ui_file_data (file);
00550 
00551   if (stdio->magic != &stdio_file_magic)
00552     internal_error (__FILE__, __LINE__,
00553                     _("stdio_file_flush: bad magic number"));
00554   fflush (stdio->file);
00555 }
00556 
00557 static long
00558 stdio_file_read (struct ui_file *file, char *buf, long length_buf)
00559 {
00560   struct stdio_file *stdio = ui_file_data (file);
00561 
00562   if (stdio->magic != &stdio_file_magic)
00563     internal_error (__FILE__, __LINE__,
00564                     _("stdio_file_read: bad magic number"));
00565 
00566   /* For the benefit of Windows, call gdb_select before reading from
00567      the file.  Wait until at least one byte of data is available.
00568      Control-C can interrupt gdb_select, but not read.  */
00569   {
00570     fd_set readfds;
00571     FD_ZERO (&readfds);
00572     FD_SET (stdio->fd, &readfds);
00573     if (gdb_select (stdio->fd + 1, &readfds, NULL, NULL, NULL) == -1)
00574       return -1;
00575   }
00576 
00577   return read (stdio->fd, buf, length_buf);
00578 }
00579 
00580 static void
00581 stdio_file_write (struct ui_file *file, const char *buf, long length_buf)
00582 {
00583   struct stdio_file *stdio = ui_file_data (file);
00584 
00585   if (stdio->magic != &stdio_file_magic)
00586     internal_error (__FILE__, __LINE__,
00587                     _("stdio_file_write: bad magic number"));
00588   /* Calling error crashes when we are called from the exception framework.  */
00589   if (fwrite (buf, length_buf, 1, stdio->file))
00590     {
00591       /* Nothing.  */
00592     }
00593 }
00594 
00595 static void
00596 stdio_file_write_async_safe (struct ui_file *file,
00597                              const char *buf, long length_buf)
00598 {
00599   struct stdio_file *stdio = ui_file_data (file);
00600 
00601   if (stdio->magic != &stdio_file_magic)
00602     {
00603       /* gettext isn't necessarily async safe, so we can't use _("error message") here.
00604          We could extract the correct translation ahead of time, but this is an extremely
00605          rare event, and one of the other stdio_file_* routines will presumably catch
00606          the problem anyway.  For now keep it simple and ignore the error here.  */
00607       return;
00608     }
00609 
00610   /* This is written the way it is to avoid a warning from gcc about not using the
00611      result of write (since it can be declared with attribute warn_unused_result).
00612      Alas casting to void doesn't work for this.  */
00613   if (write (stdio->fd, buf, length_buf))
00614     {
00615       /* Nothing.  */
00616     }
00617 }
00618 
00619 static void
00620 stdio_file_fputs (const char *linebuffer, struct ui_file *file)
00621 {
00622   struct stdio_file *stdio = ui_file_data (file);
00623 
00624   if (stdio->magic != &stdio_file_magic)
00625     internal_error (__FILE__, __LINE__,
00626                     _("stdio_file_fputs: bad magic number"));
00627   /* Calling error crashes when we are called from the exception framework.  */
00628   if (fputs (linebuffer, stdio->file))
00629     {
00630       /* Nothing.  */
00631     }
00632 }
00633 
00634 static int
00635 stdio_file_isatty (struct ui_file *file)
00636 {
00637   struct stdio_file *stdio = ui_file_data (file);
00638 
00639   if (stdio->magic != &stdio_file_magic)
00640     internal_error (__FILE__, __LINE__,
00641                     _("stdio_file_isatty: bad magic number"));
00642   return (isatty (stdio->fd));
00643 }
00644 
00645 static int
00646 stdio_file_fseek (struct ui_file *file, long offset, int whence)
00647 {
00648   struct stdio_file *stdio = ui_file_data (file);
00649 
00650   if (stdio->magic != &stdio_file_magic)
00651     internal_error (__FILE__, __LINE__,
00652                     _("stdio_file_fseek: bad magic number"));
00653 
00654   return fseek (stdio->file, offset, whence);
00655 }
00656 
00657 #ifdef __MINGW32__
00658 /* This is the implementation of ui_file method to_write for stderr.
00659    gdb_stdout is flushed before writing to gdb_stderr.  */
00660 
00661 static void
00662 stderr_file_write (struct ui_file *file, const char *buf, long length_buf)
00663 {
00664   gdb_flush (gdb_stdout);
00665   stdio_file_write (file, buf, length_buf);
00666 }
00667 
00668 /* This is the implementation of ui_file method to_fputs for stderr.
00669    gdb_stdout is flushed before writing to gdb_stderr.  */
00670 
00671 static void
00672 stderr_file_fputs (const char *linebuffer, struct ui_file *file)
00673 {
00674   gdb_flush (gdb_stdout);
00675   stdio_file_fputs (linebuffer, file);
00676 }
00677 #endif
00678 
00679 struct ui_file *
00680 stderr_fileopen (void)
00681 {
00682   struct ui_file *ui_file = stdio_fileopen (stderr);
00683 
00684 #ifdef __MINGW32__
00685   /* There is no real line-buffering on Windows, see
00686      http://msdn.microsoft.com/en-us/library/86cebhfs%28v=vs.71%29.aspx
00687      so the stdout is either fully-buffered or non-buffered.  We can't
00688      make stdout non-buffered, because of two concerns,
00689      1.  non-buffering hurts performance,
00690      2.  non-buffering may change GDB's behavior when it is interacting
00691      with front-end, such as Emacs.
00692 
00693      We decided to leave stdout as fully buffered, but flush it first
00694      when something is written to stderr.  */
00695 
00696   /* Method 'to_write_async_safe' is not overwritten, because there's
00697      no way to flush a stream in an async-safe manner.  Fortunately,
00698      it doesn't really matter, because:
00699      - that method is only used for printing internal debug output
00700        from signal handlers.
00701      - Windows hosts don't have a concept of async-safeness.  Signal
00702        handlers run in a separate thread, so they can call
00703        the regular non-async-safe output routines freely.  */
00704   set_ui_file_write (ui_file, stderr_file_write);
00705   set_ui_file_fputs (ui_file, stderr_file_fputs);
00706 #endif
00707 
00708   return ui_file;
00709 }
00710 
00711 /* Like fdopen().  Create a ui_file from a previously opened FILE.  */
00712 
00713 struct ui_file *
00714 stdio_fileopen (FILE *file)
00715 {
00716   return stdio_file_new (file, 0);
00717 }
00718 
00719 struct ui_file *
00720 gdb_fopen (const char *name, const char *mode)
00721 {
00722   FILE *f = gdb_fopen_cloexec (name, mode);
00723 
00724   if (f == NULL)
00725     return NULL;
00726   return stdio_file_new (f, 1);
00727 }
00728 
00729 /* ``struct ui_file'' implementation that maps onto two ui-file objects.  */
00730 
00731 static ui_file_write_ftype tee_file_write;
00732 static ui_file_fputs_ftype tee_file_fputs;
00733 static ui_file_isatty_ftype tee_file_isatty;
00734 static ui_file_delete_ftype tee_file_delete;
00735 static ui_file_flush_ftype tee_file_flush;
00736 
00737 static int tee_file_magic;
00738 
00739 struct tee_file
00740   {
00741     int *magic;
00742     struct ui_file *one, *two;
00743     int close_one, close_two;
00744   };
00745 
00746 struct ui_file *
00747 tee_file_new (struct ui_file *one, int close_one,
00748               struct ui_file *two, int close_two)
00749 {
00750   struct ui_file *ui_file = ui_file_new ();
00751   struct tee_file *tee = xmalloc (sizeof (struct tee_file));
00752 
00753   tee->magic = &tee_file_magic;
00754   tee->one = one;
00755   tee->two = two;
00756   tee->close_one = close_one;
00757   tee->close_two = close_two;
00758   set_ui_file_data (ui_file, tee, tee_file_delete);
00759   set_ui_file_flush (ui_file, tee_file_flush);
00760   set_ui_file_write (ui_file, tee_file_write);
00761   set_ui_file_fputs (ui_file, tee_file_fputs);
00762   set_ui_file_isatty (ui_file, tee_file_isatty);
00763   return ui_file;
00764 }
00765 
00766 static void
00767 tee_file_delete (struct ui_file *file)
00768 {
00769   struct tee_file *tee = ui_file_data (file);
00770 
00771   if (tee->magic != &tee_file_magic)
00772     internal_error (__FILE__, __LINE__,
00773                     _("tee_file_delete: bad magic number"));
00774   if (tee->close_one)
00775     ui_file_delete (tee->one);
00776   if (tee->close_two)
00777     ui_file_delete (tee->two);
00778 
00779   xfree (tee);
00780 }
00781 
00782 static void
00783 tee_file_flush (struct ui_file *file)
00784 {
00785   struct tee_file *tee = ui_file_data (file);
00786 
00787   if (tee->magic != &tee_file_magic)
00788     internal_error (__FILE__, __LINE__,
00789                     _("tee_file_flush: bad magic number"));
00790   tee->one->to_flush (tee->one);
00791   tee->two->to_flush (tee->two);
00792 }
00793 
00794 static void
00795 tee_file_write (struct ui_file *file, const char *buf, long length_buf)
00796 {
00797   struct tee_file *tee = ui_file_data (file);
00798 
00799   if (tee->magic != &tee_file_magic)
00800     internal_error (__FILE__, __LINE__,
00801                     _("tee_file_write: bad magic number"));
00802   ui_file_write (tee->one, buf, length_buf);
00803   ui_file_write (tee->two, buf, length_buf);
00804 }
00805 
00806 static void
00807 tee_file_fputs (const char *linebuffer, struct ui_file *file)
00808 {
00809   struct tee_file *tee = ui_file_data (file);
00810 
00811   if (tee->magic != &tee_file_magic)
00812     internal_error (__FILE__, __LINE__,
00813                     _("tee_file_fputs: bad magic number"));
00814   tee->one->to_fputs (linebuffer, tee->one);
00815   tee->two->to_fputs (linebuffer, tee->two);
00816 }
00817 
00818 static int
00819 tee_file_isatty (struct ui_file *file)
00820 {
00821   struct tee_file *tee = ui_file_data (file);
00822 
00823   if (tee->magic != &tee_file_magic)
00824     internal_error (__FILE__, __LINE__,
00825                     _("tee_file_isatty: bad magic number"));
00826 
00827   return ui_file_isatty (tee->one);
00828 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines