Include gdb_assert.h in common-defs.h
[external/binutils.git] / gdb / tui / tui-out.c
1 /* Output generating routines for GDB CLI.
2
3    Copyright (C) 1999-2014 Free Software Foundation, Inc.
4
5    Contributed by Cygnus Solutions.
6    Written by Fernando Nasser for Cygnus.
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 "ui-out.h"
25 #include "cli-out.h"
26 #include "tui.h"
27 #include <string.h>
28
29 struct tui_ui_out_data
30   {
31     struct cli_ui_out_data base;
32
33     int line;
34     int start_of_line;
35   };
36 typedef struct tui_ui_out_data tui_out_data;
37
38 /* This is the TUI ui-out implementation functions vector.  It is
39    initialized below in _initialize_tui_out, inheriting the CLI
40    version, and overriding a few methods.  */
41
42 static struct ui_out_impl tui_ui_out_impl;
43
44 /* Output an int field.  */
45
46 static void
47 tui_field_int (struct ui_out *uiout, 
48                int fldno, int width,
49                enum ui_align alignment,
50                const char *fldname, 
51                int value)
52 {
53   tui_out_data *data = ui_out_data (uiout);
54
55   if (data->base.suppress_output)
56     return;
57
58   /* Don't print line number, keep it for later.  */
59   if (data->start_of_line == 0 && strcmp (fldname, "line") == 0)
60     {
61       data->start_of_line ++;
62       data->line = value;
63       return;
64     }
65   data->start_of_line ++;
66
67   (*cli_ui_out_impl.field_int) (uiout, fldno,
68                                 width, alignment, fldname, value);
69 }
70
71 /* Other cli_field_* end up here so alignment and field separators are
72    both handled by tui_field_string.  */
73
74 static void
75 tui_field_string (struct ui_out *uiout,
76                   int fldno, int width,
77                   enum ui_align align,
78                   const char *fldname,
79                   const char *string)
80 {
81   tui_out_data *data = ui_out_data (uiout);
82
83   if (data->base.suppress_output)
84     return;
85
86   if (fldname && data->line > 0 && strcmp (fldname, "fullname") == 0)
87     {
88       data->start_of_line ++;
89       if (data->line > 0)
90         {
91           tui_show_source (string, data->line);
92         }
93       return;
94     }
95   
96   data->start_of_line++;
97
98   (*cli_ui_out_impl.field_string) (uiout, fldno,
99                                    width, align,
100                                    fldname, string);
101 }
102
103 /* This is the only field function that does not align.  */
104
105 static void
106 tui_field_fmt (struct ui_out *uiout, int fldno,
107                int width, enum ui_align align,
108                const char *fldname,
109                const char *format,
110                va_list args)
111 {
112   tui_out_data *data = ui_out_data (uiout);
113
114   if (data->base.suppress_output)
115     return;
116
117   data->start_of_line++;
118
119   (*cli_ui_out_impl.field_fmt) (uiout, fldno,
120                                 width, align,
121                                 fldname, format, args);
122 }
123
124 static void
125 tui_text (struct ui_out *uiout, const char *string)
126 {
127   tui_out_data *data = ui_out_data (uiout);
128
129   if (data->base.suppress_output)
130     return;
131   data->start_of_line ++;
132   if (data->line > 0)
133     {
134       if (strchr (string, '\n') != 0)
135         {
136           data->line = -1;
137           data->start_of_line = 0;
138         }
139       return;
140     }
141   if (strchr (string, '\n'))
142     data->start_of_line = 0;
143
144   (*cli_ui_out_impl.text) (uiout, string);
145 }
146
147 struct ui_out *
148 tui_out_new (struct ui_file *stream)
149 {
150   int flags = 0;
151
152   tui_out_data *data = XNEW (tui_out_data);
153
154   /* Initialize base "class".  */
155   cli_out_data_ctor (&data->base, stream);
156
157   /* Initialize our fields.  */
158   data->line = -1;
159   data->start_of_line = 0;
160
161   return ui_out_new (&tui_ui_out_impl, data, flags);
162 }
163
164 /* Standard gdb initialization hook.  */
165
166 extern void _initialize_tui_out (void);
167
168 void
169 _initialize_tui_out (void)
170 {
171   /* Inherit the CLI version.  */
172   tui_ui_out_impl = cli_ui_out_impl;
173
174   /* Override a few methods.  */
175   tui_ui_out_impl.field_int = tui_field_int;
176   tui_ui_out_impl.field_string = tui_field_string;
177   tui_ui_out_impl.field_fmt = tui_field_fmt;
178   tui_ui_out_impl.text = tui_text;
179 }