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