gdb/ui-file.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* UI_FILE - a generic STDIO like output stream.

  2.    Copyright (C) 1999-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. /* Implement the ``struct ui_file'' object.  */

  15. #include "defs.h"
  16. #include "ui-file.h"
  17. #include "gdb_obstack.h"
  18. #include "gdb_select.h"
  19. #include "filestuff.h"

  20. static ui_file_isatty_ftype null_file_isatty;
  21. static ui_file_write_ftype null_file_write;
  22. static ui_file_write_ftype null_file_write_async_safe;
  23. static ui_file_fputs_ftype null_file_fputs;
  24. static ui_file_read_ftype null_file_read;
  25. static ui_file_flush_ftype null_file_flush;
  26. static ui_file_delete_ftype null_file_delete;
  27. static ui_file_rewind_ftype null_file_rewind;
  28. static ui_file_put_ftype null_file_put;
  29. static ui_file_fseek_ftype null_file_fseek;

  30. struct ui_file
  31.   {
  32.     int *magic;
  33.     ui_file_flush_ftype *to_flush;
  34.     ui_file_write_ftype *to_write;
  35.     ui_file_write_async_safe_ftype *to_write_async_safe;
  36.     ui_file_fputs_ftype *to_fputs;
  37.     ui_file_read_ftype *to_read;
  38.     ui_file_delete_ftype *to_delete;
  39.     ui_file_isatty_ftype *to_isatty;
  40.     ui_file_rewind_ftype *to_rewind;
  41.     ui_file_put_ftype *to_put;
  42.     ui_file_fseek_ftype *to_fseek;
  43.     void *to_data;
  44.   };
  45. int ui_file_magic;

  46. struct ui_file *
  47. ui_file_new (void)
  48. {
  49.   struct ui_file *file = xmalloc (sizeof (struct ui_file));

  50.   file->magic = &ui_file_magic;
  51.   set_ui_file_data (file, NULL, null_file_delete);
  52.   set_ui_file_flush (file, null_file_flush);
  53.   set_ui_file_write (file, null_file_write);
  54.   set_ui_file_write_async_safe (file, null_file_write_async_safe);
  55.   set_ui_file_fputs (file, null_file_fputs);
  56.   set_ui_file_read (file, null_file_read);
  57.   set_ui_file_isatty (file, null_file_isatty);
  58.   set_ui_file_rewind (file, null_file_rewind);
  59.   set_ui_file_put (file, null_file_put);
  60.   set_ui_file_fseek (file, null_file_fseek);
  61.   return file;
  62. }

  63. void
  64. ui_file_delete (struct ui_file *file)
  65. {
  66.   file->to_delete (file);
  67.   xfree (file);
  68. }

  69. static int
  70. null_file_isatty (struct ui_file *file)
  71. {
  72.   return 0;
  73. }

  74. static void
  75. null_file_rewind (struct ui_file *file)
  76. {
  77.   return;
  78. }

  79. static void
  80. null_file_put (struct ui_file *file,
  81.                ui_file_put_method_ftype *write,
  82.                void *dest)
  83. {
  84.   return;
  85. }

  86. static void
  87. null_file_flush (struct ui_file *file)
  88. {
  89.   return;
  90. }

  91. static void
  92. null_file_write (struct ui_file *file,
  93.                  const char *buf,
  94.                  long sizeof_buf)
  95. {
  96.   if (file->to_fputs == null_file_fputs)
  97.     /* Both the write and fputs methods are null.  Discard the
  98.        request.  */
  99.     return;
  100.   else
  101.     {
  102.       /* The fputs method isn't null, slowly pass the write request
  103.          onto that.  FYI, this isn't as bad as it may look - the
  104.          current (as of 1999-11-07) printf_* function calls fputc and
  105.          fputc does exactly the below.  By having a write function it
  106.          is possible to clean up that code.  */
  107.       int i;
  108.       char b[2];

  109.       b[1] = '\0';
  110.       for (i = 0; i < sizeof_buf; i++)
  111.         {
  112.           b[0] = buf[i];
  113.           file->to_fputs (b, file);
  114.         }
  115.       return;
  116.     }
  117. }

  118. static long
  119. null_file_read (struct ui_file *file,
  120.                 char *buf,
  121.                 long sizeof_buf)
  122. {
  123.   errno = EBADF;
  124.   return 0;
  125. }

  126. static void
  127. null_file_fputs (const char *buf, struct ui_file *file)
  128. {
  129.   if (file->to_write == null_file_write)
  130.     /* Both the write and fputs methods are null.  Discard the
  131.        request.  */
  132.     return;
  133.   else
  134.     {
  135.       /* The write method was implemented, use that.  */
  136.       file->to_write (file, buf, strlen (buf));
  137.     }
  138. }

  139. static void
  140. null_file_write_async_safe (struct ui_file *file,
  141.                             const char *buf,
  142.                             long sizeof_buf)
  143. {
  144.   return;
  145. }

  146. static void
  147. null_file_delete (struct ui_file *file)
  148. {
  149.   return;
  150. }

  151. static int
  152. null_file_fseek (struct ui_file *stream, long offset, int whence)
  153. {
  154.   errno = EBADF;

  155.   return -1;
  156. }

  157. void *
  158. ui_file_data (struct ui_file *file)
  159. {
  160.   if (file->magic != &ui_file_magic)
  161.     internal_error (__FILE__, __LINE__,
  162.                     _("ui_file_data: bad magic number"));
  163.   return file->to_data;
  164. }

  165. void
  166. gdb_flush (struct ui_file *file)
  167. {
  168.   file->to_flush (file);
  169. }

  170. int
  171. ui_file_isatty (struct ui_file *file)
  172. {
  173.   return file->to_isatty (file);
  174. }

  175. void
  176. ui_file_rewind (struct ui_file *file)
  177. {
  178.   file->to_rewind (file);
  179. }

  180. void
  181. ui_file_put (struct ui_file *file,
  182.               ui_file_put_method_ftype *write,
  183.               void *dest)
  184. {
  185.   file->to_put (file, write, dest);
  186. }

  187. void
  188. ui_file_write (struct ui_file *file,
  189.                 const char *buf,
  190.                 long length_buf)
  191. {
  192.   file->to_write (file, buf, length_buf);
  193. }

  194. void
  195. ui_file_write_for_put (void *data, const char *buffer, long length_buffer)
  196. {
  197.   ui_file_write (data, buffer, length_buffer);
  198. }

  199. void
  200. ui_file_write_async_safe (struct ui_file *file,
  201.                           const char *buf,
  202.                           long length_buf)
  203. {
  204.   file->to_write_async_safe (file, buf, length_buf);
  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. int
  212. ui_file_fseek (struct ui_file *file, long offset, int whence)
  213. {
  214.   return file->to_fseek (file, offset, whence);
  215. }

  216. void
  217. fputs_unfiltered (const char *buf, struct ui_file *file)
  218. {
  219.   file->to_fputs (buf, file);
  220. }

  221. void
  222. set_ui_file_flush (struct ui_file *file, ui_file_flush_ftype *flush_ptr)
  223. {
  224.   file->to_flush = flush_ptr;
  225. }

  226. void
  227. set_ui_file_isatty (struct ui_file *file, ui_file_isatty_ftype *isatty_ptr)
  228. {
  229.   file->to_isatty = isatty_ptr;
  230. }

  231. void
  232. set_ui_file_rewind (struct ui_file *file, ui_file_rewind_ftype *rewind_ptr)
  233. {
  234.   file->to_rewind = rewind_ptr;
  235. }

  236. void
  237. set_ui_file_put (struct ui_file *file, ui_file_put_ftype *put_ptr)
  238. {
  239.   file->to_put = put_ptr;
  240. }

  241. void
  242. set_ui_file_write (struct ui_file *file,
  243.                     ui_file_write_ftype *write_ptr)
  244. {
  245.   file->to_write = write_ptr;
  246. }

  247. void
  248. set_ui_file_write_async_safe (struct ui_file *file,
  249.                               ui_file_write_async_safe_ftype *write_async_safe_ptr)
  250. {
  251.   file->to_write_async_safe = write_async_safe_ptr;
  252. }

  253. void
  254. set_ui_file_read (struct ui_file *file, ui_file_read_ftype *read_ptr)
  255. {
  256.   file->to_read = read_ptr;
  257. }

  258. void
  259. set_ui_file_fputs (struct ui_file *file, ui_file_fputs_ftype *fputs_ptr)
  260. {
  261.   file->to_fputs = fputs_ptr;
  262. }

  263. void
  264. set_ui_file_fseek (struct ui_file *file, ui_file_fseek_ftype *fseek_ptr)
  265. {
  266.   file->to_fseek = fseek_ptr;
  267. }

  268. void
  269. set_ui_file_data (struct ui_file *file, void *data,
  270.                   ui_file_delete_ftype *delete_ptr)
  271. {
  272.   file->to_data = data;
  273.   file->to_delete = delete_ptr;
  274. }

  275. /* ui_file utility function for converting a ``struct ui_file'' into
  276.    a memory buffer.  */

  277. struct accumulated_ui_file
  278. {
  279.   char *buffer;
  280.   long length;
  281. };

  282. static void
  283. do_ui_file_xstrdup (void *context, const char *buffer, long length)
  284. {
  285.   struct accumulated_ui_file *acc = context;

  286.   if (acc->buffer == NULL)
  287.     acc->buffer = xmalloc (length + 1);
  288.   else
  289.     acc->buffer = xrealloc (acc->buffer, acc->length + length + 1);
  290.   memcpy (acc->buffer + acc->length, buffer, length);
  291.   acc->length += length;
  292.   acc->buffer[acc->length] = '\0';
  293. }

  294. char *
  295. ui_file_xstrdup (struct ui_file *file, long *length)
  296. {
  297.   struct accumulated_ui_file acc;

  298.   acc.buffer = NULL;
  299.   acc.length = 0;
  300.   ui_file_put (file, do_ui_file_xstrdup, &acc);
  301.   if (acc.buffer == NULL)
  302.     acc.buffer = xstrdup ("");
  303.   if (length != NULL)
  304.     *length = acc.length;
  305.   return acc.buffer;
  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.   obstack_grow (obstack, buffer, length);
  312. }

  313. char *
  314. ui_file_obsavestring (struct ui_file *file, struct obstack *obstack,
  315.                       long *length)
  316. {
  317.   ui_file_put (file, do_ui_file_obsavestring, obstack);
  318.   *length = obstack_object_size (obstack);
  319.   obstack_1grow (obstack, '\0');
  320.   return obstack_finish (obstack);
  321. }

  322. /* A pure memory based ``struct ui_file'' that can be used an output
  323.    buffer.  The buffers accumulated contents are available via
  324.    ui_file_put().  */

  325. struct mem_file
  326.   {
  327.     int *magic;
  328.     char *buffer;
  329.     int sizeof_buffer;
  330.     int length_buffer;
  331.   };

  332. static ui_file_rewind_ftype mem_file_rewind;
  333. static ui_file_put_ftype mem_file_put;
  334. static ui_file_write_ftype mem_file_write;
  335. static ui_file_delete_ftype mem_file_delete;
  336. static struct ui_file *mem_file_new (void);
  337. static int mem_file_magic;

  338. static struct ui_file *
  339. mem_file_new (void)
  340. {
  341.   struct mem_file *stream = XNEW (struct mem_file);
  342.   struct ui_file *file = ui_file_new ();

  343.   set_ui_file_data (file, stream, mem_file_delete);
  344.   set_ui_file_rewind (file, mem_file_rewind);
  345.   set_ui_file_put (file, mem_file_put);
  346.   set_ui_file_write (file, mem_file_write);
  347.   stream->magic = &mem_file_magic;
  348.   stream->buffer = NULL;
  349.   stream->sizeof_buffer = 0;
  350.   stream->length_buffer = 0;
  351.   return file;
  352. }

  353. static void
  354. mem_file_delete (struct ui_file *file)
  355. {
  356.   struct mem_file *stream = ui_file_data (file);

  357.   if (stream->magic != &mem_file_magic)
  358.     internal_error (__FILE__, __LINE__,
  359.                     _("mem_file_delete: bad magic number"));
  360.   if (stream->buffer != NULL)
  361.     xfree (stream->buffer);
  362.   xfree (stream);
  363. }

  364. struct ui_file *
  365. mem_fileopen (void)
  366. {
  367.   return mem_file_new ();
  368. }

  369. static void
  370. mem_file_rewind (struct ui_file *file)
  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_rewind: bad magic number"));
  376.   stream->length_buffer = 0;
  377. }

  378. static void
  379. mem_file_put (struct ui_file *file,
  380.               ui_file_put_method_ftype *write,
  381.               void *dest)
  382. {
  383.   struct mem_file *stream = ui_file_data (file);

  384.   if (stream->magic != &mem_file_magic)
  385.     internal_error (__FILE__, __LINE__,
  386.                     _("mem_file_put: bad magic number"));
  387.   if (stream->length_buffer > 0)
  388.     write (dest, stream->buffer, stream->length_buffer);
  389. }

  390. void
  391. mem_file_write (struct ui_file *file,
  392.                 const char *buffer,
  393.                 long length_buffer)
  394. {
  395.   struct mem_file *stream = ui_file_data (file);

  396.   if (stream->magic != &mem_file_magic)
  397.     internal_error (__FILE__, __LINE__,
  398.                     _("mem_file_write: bad magic number"));
  399.   if (stream->buffer == NULL)
  400.     {
  401.       stream->length_buffer = length_buffer;
  402.       stream->sizeof_buffer = length_buffer;
  403.       stream->buffer = xmalloc (stream->sizeof_buffer);
  404.       memcpy (stream->buffer, buffer, length_buffer);
  405.     }
  406.   else
  407.     {
  408.       int new_length = stream->length_buffer + length_buffer;

  409.       if (new_length >= stream->sizeof_buffer)
  410.         {
  411.           stream->sizeof_buffer = new_length;
  412.           stream->buffer = xrealloc (stream->buffer, stream->sizeof_buffer);
  413.         }
  414.       memcpy (stream->buffer + stream->length_buffer, buffer, length_buffer);
  415.       stream->length_buffer = new_length;
  416.     }
  417. }

  418. /* ``struct ui_file'' implementation that maps directly onto
  419.    <stdio.h>'s FILE.  */

  420. static ui_file_write_ftype stdio_file_write;
  421. static ui_file_write_async_safe_ftype stdio_file_write_async_safe;
  422. static ui_file_fputs_ftype stdio_file_fputs;
  423. static ui_file_read_ftype stdio_file_read;
  424. static ui_file_isatty_ftype stdio_file_isatty;
  425. static ui_file_delete_ftype stdio_file_delete;
  426. static struct ui_file *stdio_file_new (FILE *file, int close_p);
  427. static ui_file_flush_ftype stdio_file_flush;
  428. static ui_file_fseek_ftype stdio_file_fseek;

  429. static int stdio_file_magic;

  430. struct stdio_file
  431.   {
  432.     int *magic;
  433.     FILE *file;
  434.     /* The associated file descriptor is extracted ahead of time for
  435.        stdio_file_write_async_safe's benefit, in case fileno isn't async-safe.  */
  436.     int fd;
  437.     int close_p;
  438.   };

  439. static struct ui_file *
  440. stdio_file_new (FILE *file, int close_p)
  441. {
  442.   struct ui_file *ui_file = ui_file_new ();
  443.   struct stdio_file *stdio = xmalloc (sizeof (struct stdio_file));

  444.   stdio->magic = &stdio_file_magic;
  445.   stdio->file = file;
  446.   stdio->fd = fileno (file);
  447.   stdio->close_p = close_p;
  448.   set_ui_file_data (ui_file, stdio, stdio_file_delete);
  449.   set_ui_file_flush (ui_file, stdio_file_flush);
  450.   set_ui_file_write (ui_file, stdio_file_write);
  451.   set_ui_file_write_async_safe (ui_file, stdio_file_write_async_safe);
  452.   set_ui_file_fputs (ui_file, stdio_file_fputs);
  453.   set_ui_file_read (ui_file, stdio_file_read);
  454.   set_ui_file_isatty (ui_file, stdio_file_isatty);
  455.   set_ui_file_fseek (ui_file, stdio_file_fseek);
  456.   return ui_file;
  457. }

  458. static void
  459. stdio_file_delete (struct ui_file *file)
  460. {
  461.   struct stdio_file *stdio = ui_file_data (file);

  462.   if (stdio->magic != &stdio_file_magic)
  463.     internal_error (__FILE__, __LINE__,
  464.                     _("stdio_file_delete: bad magic number"));
  465.   if (stdio->close_p)
  466.     {
  467.       fclose (stdio->file);
  468.     }
  469.   xfree (stdio);
  470. }

  471. static void
  472. stdio_file_flush (struct ui_file *file)
  473. {
  474.   struct stdio_file *stdio = ui_file_data (file);

  475.   if (stdio->magic != &stdio_file_magic)
  476.     internal_error (__FILE__, __LINE__,
  477.                     _("stdio_file_flush: bad magic number"));
  478.   fflush (stdio->file);
  479. }

  480. static long
  481. stdio_file_read (struct ui_file *file, 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_read: bad magic number"));

  487.   /* For the benefit of Windows, call gdb_select before reading from
  488.      the file.  Wait until at least one byte of data is available.
  489.      Control-C can interrupt gdb_select, but not read.  */
  490.   {
  491.     fd_set readfds;
  492.     FD_ZERO (&readfds);
  493.     FD_SET (stdio->fd, &readfds);
  494.     if (gdb_select (stdio->fd + 1, &readfds, NULL, NULL, NULL) == -1)
  495.       return -1;
  496.   }

  497.   return read (stdio->fd, buf, length_buf);
  498. }

  499. static void
  500. stdio_file_write (struct ui_file *file, const char *buf, long length_buf)
  501. {
  502.   struct stdio_file *stdio = ui_file_data (file);

  503.   if (stdio->magic != &stdio_file_magic)
  504.     internal_error (__FILE__, __LINE__,
  505.                     _("stdio_file_write: bad magic number"));
  506.   /* Calling error crashes when we are called from the exception framework.  */
  507.   if (fwrite (buf, length_buf, 1, stdio->file))
  508.     {
  509.       /* Nothing.  */
  510.     }
  511. }

  512. static void
  513. stdio_file_write_async_safe (struct ui_file *file,
  514.                              const char *buf, long length_buf)
  515. {
  516.   struct stdio_file *stdio = ui_file_data (file);

  517.   if (stdio->magic != &stdio_file_magic)
  518.     {
  519.       /* gettext isn't necessarily async safe, so we can't use _("error message") here.
  520.          We could extract the correct translation ahead of time, but this is an extremely
  521.          rare event, and one of the other stdio_file_* routines will presumably catch
  522.          the problem anyway.  For now keep it simple and ignore the error here.  */
  523.       return;
  524.     }

  525.   /* This is written the way it is to avoid a warning from gcc about not using the
  526.      result of write (since it can be declared with attribute warn_unused_result).
  527.      Alas casting to void doesn't work for this.  */
  528.   if (write (stdio->fd, buf, length_buf))
  529.     {
  530.       /* Nothing.  */
  531.     }
  532. }

  533. static void
  534. stdio_file_fputs (const char *linebuffer, struct ui_file *file)
  535. {
  536.   struct stdio_file *stdio = ui_file_data (file);

  537.   if (stdio->magic != &stdio_file_magic)
  538.     internal_error (__FILE__, __LINE__,
  539.                     _("stdio_file_fputs: bad magic number"));
  540.   /* Calling error crashes when we are called from the exception framework.  */
  541.   if (fputs (linebuffer, stdio->file))
  542.     {
  543.       /* Nothing.  */
  544.     }
  545. }

  546. static int
  547. stdio_file_isatty (struct ui_file *file)
  548. {
  549.   struct stdio_file *stdio = ui_file_data (file);

  550.   if (stdio->magic != &stdio_file_magic)
  551.     internal_error (__FILE__, __LINE__,
  552.                     _("stdio_file_isatty: bad magic number"));
  553.   return (isatty (stdio->fd));
  554. }

  555. static int
  556. stdio_file_fseek (struct ui_file *file, long offset, int whence)
  557. {
  558.   struct stdio_file *stdio = ui_file_data (file);

  559.   if (stdio->magic != &stdio_file_magic)
  560.     internal_error (__FILE__, __LINE__,
  561.                     _("stdio_file_fseek: bad magic number"));

  562.   return fseek (stdio->file, offset, whence);
  563. }

  564. #ifdef __MINGW32__
  565. /* This is the implementation of ui_file method to_write for stderr.
  566.    gdb_stdout is flushed before writing to gdb_stderr.  */

  567. static void
  568. stderr_file_write (struct ui_file *file, const char *buf, long length_buf)
  569. {
  570.   gdb_flush (gdb_stdout);
  571.   stdio_file_write (file, buf, length_buf);
  572. }

  573. /* This is the implementation of ui_file method to_fputs for stderr.
  574.    gdb_stdout is flushed before writing to gdb_stderr.  */

  575. static void
  576. stderr_file_fputs (const char *linebuffer, struct ui_file *file)
  577. {
  578.   gdb_flush (gdb_stdout);
  579.   stdio_file_fputs (linebuffer, file);
  580. }
  581. #endif

  582. struct ui_file *
  583. stderr_fileopen (void)
  584. {
  585.   struct ui_file *ui_file = stdio_fileopen (stderr);

  586. #ifdef __MINGW32__
  587.   /* There is no real line-buffering on Windows, see
  588.      http://msdn.microsoft.com/en-us/library/86cebhfs%28v=vs.71%29.aspx
  589.      so the stdout is either fully-buffered or non-buffered.  We can't
  590.      make stdout non-buffered, because of two concerns,
  591.      1.  non-buffering hurts performance,
  592.      2.  non-buffering may change GDB's behavior when it is interacting
  593.      with front-end, such as Emacs.

  594.      We decided to leave stdout as fully buffered, but flush it first
  595.      when something is written to stderr.  */

  596.   /* Method 'to_write_async_safe' is not overwritten, because there's
  597.      no way to flush a stream in an async-safe manner.  Fortunately,
  598.      it doesn't really matter, because:
  599.      - that method is only used for printing internal debug output
  600.        from signal handlers.
  601.      - Windows hosts don't have a concept of async-safeness.  Signal
  602.        handlers run in a separate thread, so they can call
  603.        the regular non-async-safe output routines freely.  */
  604.   set_ui_file_write (ui_file, stderr_file_write);
  605.   set_ui_file_fputs (ui_file, stderr_file_fputs);
  606. #endif

  607.   return ui_file;
  608. }

  609. /* Like fdopen().  Create a ui_file from a previously opened FILE.  */

  610. struct ui_file *
  611. stdio_fileopen (FILE *file)
  612. {
  613.   return stdio_file_new (file, 0);
  614. }

  615. struct ui_file *
  616. gdb_fopen (const char *name, const char *mode)
  617. {
  618.   FILE *f = gdb_fopen_cloexec (name, mode);

  619.   if (f == NULL)
  620.     return NULL;
  621.   return stdio_file_new (f, 1);
  622. }

  623. /* ``struct ui_file'' implementation that maps onto two ui-file objects.  */

  624. static ui_file_write_ftype tee_file_write;
  625. static ui_file_fputs_ftype tee_file_fputs;
  626. static ui_file_isatty_ftype tee_file_isatty;
  627. static ui_file_delete_ftype tee_file_delete;
  628. static ui_file_flush_ftype tee_file_flush;

  629. static int tee_file_magic;

  630. struct tee_file
  631.   {
  632.     int *magic;
  633.     struct ui_file *one, *two;
  634.     int close_one, close_two;
  635.   };

  636. struct ui_file *
  637. tee_file_new (struct ui_file *one, int close_one,
  638.               struct ui_file *two, int close_two)
  639. {
  640.   struct ui_file *ui_file = ui_file_new ();
  641.   struct tee_file *tee = xmalloc (sizeof (struct tee_file));

  642.   tee->magic = &tee_file_magic;
  643.   tee->one = one;
  644.   tee->two = two;
  645.   tee->close_one = close_one;
  646.   tee->close_two = close_two;
  647.   set_ui_file_data (ui_file, tee, tee_file_delete);
  648.   set_ui_file_flush (ui_file, tee_file_flush);
  649.   set_ui_file_write (ui_file, tee_file_write);
  650.   set_ui_file_fputs (ui_file, tee_file_fputs);
  651.   set_ui_file_isatty (ui_file, tee_file_isatty);
  652.   return ui_file;
  653. }

  654. static void
  655. tee_file_delete (struct ui_file *file)
  656. {
  657.   struct tee_file *tee = ui_file_data (file);

  658.   if (tee->magic != &tee_file_magic)
  659.     internal_error (__FILE__, __LINE__,
  660.                     _("tee_file_delete: bad magic number"));
  661.   if (tee->close_one)
  662.     ui_file_delete (tee->one);
  663.   if (tee->close_two)
  664.     ui_file_delete (tee->two);

  665.   xfree (tee);
  666. }

  667. static void
  668. tee_file_flush (struct ui_file *file)
  669. {
  670.   struct tee_file *tee = ui_file_data (file);

  671.   if (tee->magic != &tee_file_magic)
  672.     internal_error (__FILE__, __LINE__,
  673.                     _("tee_file_flush: bad magic number"));
  674.   tee->one->to_flush (tee->one);
  675.   tee->two->to_flush (tee->two);
  676. }

  677. static void
  678. tee_file_write (struct ui_file *file, const char *buf, long length_buf)
  679. {
  680.   struct tee_file *tee = ui_file_data (file);

  681.   if (tee->magic != &tee_file_magic)
  682.     internal_error (__FILE__, __LINE__,
  683.                     _("tee_file_write: bad magic number"));
  684.   ui_file_write (tee->one, buf, length_buf);
  685.   ui_file_write (tee->two, buf, length_buf);
  686. }

  687. static void
  688. tee_file_fputs (const char *linebuffer, struct ui_file *file)
  689. {
  690.   struct tee_file *tee = ui_file_data (file);

  691.   if (tee->magic != &tee_file_magic)
  692.     internal_error (__FILE__, __LINE__,
  693.                     _("tee_file_fputs: bad magic number"));
  694.   tee->one->to_fputs (linebuffer, tee->one);
  695.   tee->two->to_fputs (linebuffer, tee->two);
  696. }

  697. static int
  698. tee_file_isatty (struct ui_file *file)
  699. {
  700.   struct tee_file *tee = ui_file_data (file);

  701.   if (tee->magic != &tee_file_magic)
  702.     internal_error (__FILE__, __LINE__,
  703.                     _("tee_file_isatty: bad magic number"));

  704.   return ui_file_isatty (tee->one);
  705. }