PR tui/9217
[external/binutils.git] / gdb / tui / tui-out.c
1 /* Output generating routines for GDB CLI.
2
3    Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5
6    Contributed by Cygnus Solutions.
7    Written by Fernando Nasser for Cygnus.
8
9    This file is part of GDB.
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
23
24 #include "defs.h"
25 #include "ui-out.h"
26 #include "cli-out.h"
27 #include "tui.h"
28 #include "gdb_string.h"
29 #include "gdb_assert.h"
30
31 struct tui_ui_out_data
32   {
33     struct cli_ui_out_data base;
34
35     int line;
36     int start_of_line;
37   };
38 typedef struct tui_ui_out_data tui_out_data;
39
40 /* This is the TUI ui-out implementation functions vector.  It is
41    initialized below in _initialize_tui_out, inheriting the CLI
42    version, and overriding a few methods.  */
43
44 static struct ui_out_impl tui_ui_out_impl;
45
46 /* Output an int field.  */
47
48 static void
49 tui_field_int (struct ui_out *uiout, 
50                int fldno, int width,
51                enum ui_align alignment,
52                const char *fldname, 
53                int value)
54 {
55   tui_out_data *data = ui_out_data (uiout);
56   if (data->base.suppress_output)
57     return;
58
59   /* Don't print line number, keep it for later.  */
60   if (data->start_of_line == 0 && strcmp (fldname, "line") == 0)
61     {
62       data->start_of_line ++;
63       data->line = value;
64       return;
65     }
66   data->start_of_line ++;
67
68   (*cli_ui_out_impl.field_int) (uiout, fldno,
69                                 width, alignment, fldname, value);
70 }
71
72 /* Other cli_field_* end up here so alignment and field separators are
73    both handled by tui_field_string.  */
74
75 static void
76 tui_field_string (struct ui_out *uiout,
77                   int fldno, int width,
78                   enum ui_align align,
79                   const char *fldname,
80                   const char *string)
81 {
82   tui_out_data *data = ui_out_data (uiout);
83   if (data->base.suppress_output)
84     return;
85
86   if (fldname && data->line > 0 && strcmp (fldname, "file") == 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   if (data->base.suppress_output)
114     return;
115
116   data->start_of_line++;
117
118   (*cli_ui_out_impl.field_fmt) (uiout, fldno,
119                                 width, align,
120                                 fldname, format, args);
121 }
122
123 static void
124 tui_text (struct ui_out *uiout, const char *string)
125 {
126   tui_out_data *data = ui_out_data (uiout);
127   if (data->base.suppress_output)
128     return;
129   data->start_of_line ++;
130   if (data->line > 0)
131     {
132       if (strchr (string, '\n') != 0)
133         {
134           data->line = -1;
135           data->start_of_line = 0;
136         }
137       return;
138     }
139   if (strchr (string, '\n'))
140     data->start_of_line = 0;
141
142   (*cli_ui_out_impl.text) (uiout, string);
143 }
144
145 struct ui_out *
146 tui_out_new (struct ui_file *stream)
147 {
148   int flags = 0;
149
150   tui_out_data *data = XMALLOC (tui_out_data);
151
152   /* Initialize base "class".  */
153   cli_out_data_ctor (&data->base, stream);
154
155   /* Initialize our fields.  */
156   data->line = -1;
157   data->start_of_line = 0;
158
159   return ui_out_new (&tui_ui_out_impl, data, flags);
160 }
161
162 /* Standard gdb initialization hook.  */
163
164 extern void _initialize_tui_out (void);
165
166 void
167 _initialize_tui_out (void)
168 {
169 /* Inherit the CLI version.  */
170   tui_ui_out_impl = cli_ui_out_impl;
171
172   /* Override a few methods.  */
173   tui_ui_out_impl.field_int = tui_field_int;
174   tui_ui_out_impl.field_string = tui_field_string;
175   tui_ui_out_impl.field_fmt = tui_field_fmt;
176   tui_ui_out_impl.text = tui_text;
177 }