gdb/mi/mi-console.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* MI Console code.

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

  3.    Contributed by Cygnus Solutions (a Red Hat company).

  4.    This file is part of GDB.

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

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

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

  15. /* An MI console is a kind of ui_file stream that sends output to
  16.    stdout, but encapsulated and prefixed with a distinctive string;
  17.    for instance, error output is normally identified by a leading
  18.    "&".  */

  19. #include "defs.h"
  20. #include "mi-console.h"
  21. static ui_file_fputs_ftype mi_console_file_fputs;
  22. static ui_file_flush_ftype mi_console_file_flush;
  23. static ui_file_delete_ftype mi_console_file_delete;

  24. struct mi_console_file
  25.   {
  26.     int *magic;
  27.     struct ui_file *raw;
  28.     struct ui_file *buffer;
  29.     const char *prefix;
  30.     char quote;
  31.   };

  32. /* Use the address of this otherwise-unused global as a magic number
  33.    identifying this class of ui_file objects.  */
  34. static int mi_console_file_magic;

  35. /* Create a console that wraps the given output stream RAW with the
  36.    string PREFIX and quoting it with QUOTE.  */

  37. struct ui_file *
  38. mi_console_file_new (struct ui_file *raw, const char *prefix, char quote)
  39. {
  40.   struct ui_file *ui_file = ui_file_new ();
  41.   struct mi_console_file *mi_console = XNEW (struct mi_console_file);

  42.   mi_console->magic = &mi_console_file_magic;
  43.   mi_console->raw = raw;
  44.   mi_console->buffer = mem_fileopen ();
  45.   mi_console->prefix = prefix;
  46.   mi_console->quote = quote;
  47.   set_ui_file_fputs (ui_file, mi_console_file_fputs);
  48.   set_ui_file_flush (ui_file, mi_console_file_flush);
  49.   set_ui_file_data (ui_file, mi_console, mi_console_file_delete);

  50.   return ui_file;
  51. }

  52. static void
  53. mi_console_file_delete (struct ui_file *file)
  54. {
  55.   struct mi_console_file *mi_console = ui_file_data (file);

  56.   if (mi_console->magic != &mi_console_file_magic)
  57.     internal_error (__FILE__, __LINE__,
  58.                     _("mi_console_file_delete: bad magic number"));

  59.   xfree (mi_console);
  60. }

  61. static void
  62. mi_console_file_fputs (const char *buf, struct ui_file *file)
  63. {
  64.   struct mi_console_file *mi_console = ui_file_data (file);

  65.   if (mi_console->magic != &mi_console_file_magic)
  66.     internal_error (__FILE__, __LINE__,
  67.                     "mi_console_file_fputs: bad magic number");

  68.   /* Append the text to our internal buffer.  */
  69.   fputs_unfiltered (buf, mi_console->buffer);
  70.   /* Flush when an embedded newline is present anywhere in the buffer.  */
  71.   if (strchr (buf, '\n') != NULL)
  72.     gdb_flush (file);
  73. }

  74. /* Transform a byte sequence into a console output packet.  */

  75. static void
  76. mi_console_raw_packet (void *data, const char *buf, long length_buf)
  77. {
  78.   struct mi_console_file *mi_console = data;

  79.   if (mi_console->magic != &mi_console_file_magic)
  80.     internal_error (__FILE__, __LINE__,
  81.                     _("mi_console_raw_packet: bad magic number"));

  82.   if (length_buf > 0)
  83.     {
  84.       fputs_unfiltered (mi_console->prefix, mi_console->raw);
  85.       if (mi_console->quote)
  86.         {
  87.           fputc_unfiltered (mi_console->quote, mi_console->raw);
  88.           fputstrn_unfiltered (buf, length_buf,
  89.                                mi_console->quote, mi_console->raw);
  90.           fputc_unfiltered (mi_console->quote, mi_console->raw);
  91.           fputc_unfiltered ('\n', mi_console->raw);
  92.         }
  93.       else
  94.         {
  95.           fputstrn_unfiltered (buf, length_buf, 0, mi_console->raw);
  96.           fputc_unfiltered ('\n', mi_console->raw);
  97.         }
  98.       gdb_flush (mi_console->raw);
  99.     }
  100. }

  101. static void
  102. mi_console_file_flush (struct ui_file *file)
  103. {
  104.   struct mi_console_file *mi_console = ui_file_data (file);

  105.   if (mi_console->magic != &mi_console_file_magic)
  106.     internal_error (__FILE__, __LINE__,
  107.                     _("mi_console_file_flush: bad magic number"));

  108.   ui_file_put (mi_console->buffer, mi_console_raw_packet, mi_console);
  109.   ui_file_rewind (mi_console->buffer);

  110. }

  111. /* Change the underlying stream of the console directly; this is
  112.    useful as a minimum-impact way to reflect external changes like
  113.    logging enable/disable.  */

  114. void
  115. mi_console_set_raw (struct ui_file *file, struct ui_file *raw)
  116. {
  117.   struct mi_console_file *mi_console = ui_file_data (file);

  118.   if (mi_console->magic != &mi_console_file_magic)
  119.     internal_error (__FILE__, __LINE__,
  120.                     _("mi_console_file_set_raw: bad magic number"));

  121.   mi_console->raw = raw;
  122. }