xref: /dragonfly/contrib/gdb-7/gdb/mi/mi-console.c (revision dcd37f7d)
1 /* MI Console code.
2 
3    Copyright (C) 2000, 2001, 2002, 2007, 2008, 2009
4    Free Software Foundation, Inc.
5 
6    Contributed by Cygnus Solutions (a Red Hat company).
7 
8    This file is part of GDB.
9 
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22 
23 #include "defs.h"
24 #include "mi-console.h"
25 #include "gdb_string.h"
26 
27 /* MI-console: send output to std-out but correcty encapsulated */
28 
29 static ui_file_fputs_ftype mi_console_file_fputs;
30 static ui_file_flush_ftype mi_console_file_flush;
31 static ui_file_delete_ftype mi_console_file_delete;
32 
33 struct mi_console_file
34   {
35     int *magic;
36     struct ui_file *raw;
37     struct ui_file *buffer;
38     const char *prefix;
39     char quote;
40   };
41 
42 int mi_console_file_magic;
43 
44 struct ui_file *
45 mi_console_file_new (struct ui_file *raw,
46 		     const char *prefix, char quote)
47 {
48   struct ui_file *ui_file = ui_file_new ();
49   struct mi_console_file *mi_console = XMALLOC (struct mi_console_file);
50   mi_console->magic = &mi_console_file_magic;
51   mi_console->raw = raw;
52   mi_console->buffer = mem_fileopen ();
53   mi_console->prefix = prefix;
54   mi_console->quote = quote;
55   set_ui_file_fputs (ui_file, mi_console_file_fputs);
56   set_ui_file_flush (ui_file, mi_console_file_flush);
57   set_ui_file_data (ui_file, mi_console, mi_console_file_delete);
58   return ui_file;
59 }
60 
61 static void
62 mi_console_file_delete (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_delete: bad magic number"));
68   xfree (mi_console);
69 }
70 
71 static void
72 mi_console_file_fputs (const char *buf,
73 		       struct ui_file *file)
74 {
75   struct mi_console_file *mi_console = ui_file_data (file);
76   if (mi_console->magic != &mi_console_file_magic)
77     internal_error (__FILE__, __LINE__,
78 		    "mi_console_file_fputs: bad magic number");
79   /* Append the text to our internal buffer */
80   fputs_unfiltered (buf, mi_console->buffer);
81   /* Flush when an embedded \n */
82   if (strchr (buf, '\n') != NULL)
83     gdb_flush (file);
84 }
85 
86 /* Transform a byte sequence into a console output packet. */
87 static void
88 mi_console_raw_packet (void *data,
89 		       const char *buf,
90 		       long length_buf)
91 {
92   struct mi_console_file *mi_console = data;
93   if (mi_console->magic != &mi_console_file_magic)
94     internal_error (__FILE__, __LINE__,
95 		    _("mi_console_file_transform: bad magic number"));
96 
97   if (length_buf > 0)
98     {
99       fputs_unfiltered (mi_console->prefix, mi_console->raw);
100       if (mi_console->quote)
101 	{
102 	  fputs_unfiltered ("\"", mi_console->raw);
103 	  fputstrn_unfiltered (buf, length_buf, mi_console->quote, mi_console->raw);
104 	  fputs_unfiltered ("\"\n", mi_console->raw);
105 	}
106       else
107 	{
108 	  fputstrn_unfiltered (buf, length_buf, 0, mi_console->raw);
109 	  fputs_unfiltered ("\n", mi_console->raw);
110 	}
111       gdb_flush (mi_console->raw);
112     }
113 }
114 
115 static void
116 mi_console_file_flush (struct ui_file *file)
117 {
118   struct mi_console_file *mi_console = ui_file_data (file);
119   if (mi_console->magic != &mi_console_file_magic)
120     internal_error (__FILE__, __LINE__,
121 		    _("mi_console_file_flush: bad magic number"));
122   ui_file_put (mi_console->buffer, mi_console_raw_packet, mi_console);
123   ui_file_rewind (mi_console->buffer);
124 }
125