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