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