Update Copyright year range in all files maintained by GDB.
[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 #include "gdb_assert.h"
29
30 struct tui_ui_out_data
31   {
32     struct cli_ui_out_data base;
33
34     int line;
35     int start_of_line;
36   };
37 typedef struct tui_ui_out_data tui_out_data;
38
39 /* This is the TUI ui-out implementation functions vector.  It is
40    initialized below in _initialize_tui_out, inheriting the CLI
41    version, and overriding a few methods.  */
42
43 static struct ui_out_impl tui_ui_out_impl;
44
45 /* Output an int field.  */
46
47 static void
48 tui_field_int (struct ui_out *uiout, 
49                int fldno, int width,
50                enum ui_align alignment,
51                const char *fldname, 
52                int value)
53 {
54   tui_out_data *data = ui_out_data (uiout);
55
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
84   if (data->base.suppress_output)
85     return;
86
87   if (fldname && data->line > 0 && strcmp (fldname, "fullname") == 0)
88     {
89       data->start_of_line ++;
90       if (data->line > 0)
91         {
92           tui_show_source (string, data->line);
93         }
94       return;
95     }
96   
97   data->start_of_line++;
98
99   (*cli_ui_out_impl.field_string) (uiout, fldno,
100                                    width, align,
101                                    fldname, string);
102 }
103
104 /* This is the only field function that does not align.  */
105
106 static void
107 tui_field_fmt (struct ui_out *uiout, int fldno,
108                int width, enum ui_align align,
109                const char *fldname,
110                const char *format,
111                va_list args)
112 {
113   tui_out_data *data = ui_out_data (uiout);
114
115   if (data->base.suppress_output)
116     return;
117
118   data->start_of_line++;
119
120   (*cli_ui_out_impl.field_fmt) (uiout, fldno,
121                                 width, align,
122                                 fldname, format, args);
123 }
124
125 static void
126 tui_text (struct ui_out *uiout, const char *string)
127 {
128   tui_out_data *data = ui_out_data (uiout);
129
130   if (data->base.suppress_output)
131     return;
132   data->start_of_line ++;
133   if (data->line > 0)
134     {
135       if (strchr (string, '\n') != 0)
136         {
137           data->line = -1;
138           data->start_of_line = 0;
139         }
140       return;
141     }
142   if (strchr (string, '\n'))
143     data->start_of_line = 0;
144
145   (*cli_ui_out_impl.text) (uiout, string);
146 }
147
148 struct ui_out *
149 tui_out_new (struct ui_file *stream)
150 {
151   int flags = 0;
152
153   tui_out_data *data = XMALLOC (tui_out_data);
154
155   /* Initialize base "class".  */
156   cli_out_data_ctor (&data->base, stream);
157
158   /* Initialize our fields.  */
159   data->line = -1;
160   data->start_of_line = 0;
161
162   return ui_out_new (&tui_ui_out_impl, data, flags);
163 }
164
165 /* Standard gdb initialization hook.  */
166
167 extern void _initialize_tui_out (void);
168
169 void
170 _initialize_tui_out (void)
171 {
172   /* Inherit the CLI version.  */
173   tui_ui_out_impl = cli_ui_out_impl;
174
175   /* Override a few methods.  */
176   tui_ui_out_impl.field_int = tui_field_int;
177   tui_ui_out_impl.field_string = tui_field_string;
178   tui_ui_out_impl.field_fmt = tui_field_fmt;
179   tui_ui_out_impl.text = tui_text;
180 }