s/@example/@smallexample/
[deliverable/binutils-gdb.git] / gdb / cli-out.c
... / ...
CommitLineData
1/* Output generating routines for GDB CLI.
2 Copyright 1999, 2000 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions.
4 Written by Fernando Nasser for Cygnus.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23#include "defs.h"
24#include "ui-out.h"
25#include "cli-out.h"
26#include "gdb_string.h"
27#include "gdb_assert.h"
28
29/* Convenience macro for allocting typesafe memory. */
30
31#ifndef XMALLOC
32#define XMALLOC(TYPE) (TYPE*) xmalloc (sizeof (TYPE))
33#endif
34
35struct ui_out_data
36 {
37 struct ui_file *stream;
38 int suppress_output;
39 };
40
41/* These are the CLI output functions */
42
43static void cli_table_begin (struct ui_out *uiout, int nbrofcols,
44 int nr_rows, const char *tblid);
45static void cli_table_body (struct ui_out *uiout);
46static void cli_table_end (struct ui_out *uiout);
47static void cli_table_header (struct ui_out *uiout, int width,
48 enum ui_align alig, const char *col_name,
49 const char *colhdr);
50static void cli_begin (struct ui_out *uiout, enum ui_out_type type,
51 int level, const char *lstid);
52static void cli_end (struct ui_out *uiout, enum ui_out_type type, int level);
53static void cli_field_int (struct ui_out *uiout, int fldno, int width,
54 enum ui_align alig, const char *fldname, int value);
55static void cli_field_skip (struct ui_out *uiout, int fldno, int width,
56 enum ui_align alig, const char *fldname);
57static void cli_field_string (struct ui_out *uiout, int fldno, int width,
58 enum ui_align alig, const char *fldname,
59 const char *string);
60static void cli_field_fmt (struct ui_out *uiout, int fldno,
61 int width, enum ui_align align,
62 const char *fldname, const char *format,
63 va_list args);
64static void cli_spaces (struct ui_out *uiout, int numspaces);
65static void cli_text (struct ui_out *uiout, const char *string);
66static void cli_message (struct ui_out *uiout, int verbosity,
67 const char *format, va_list args);
68static void cli_wrap_hint (struct ui_out *uiout, char *identstring);
69static void cli_flush (struct ui_out *uiout);
70
71/* This is the CLI ui-out implementation functions vector */
72
73/* FIXME: This can be initialized dynamically after default is set to
74 handle initial output in main.c */
75
76static struct ui_out_impl cli_ui_out_impl =
77{
78 cli_table_begin,
79 cli_table_body,
80 cli_table_end,
81 cli_table_header,
82 cli_begin,
83 cli_end,
84 cli_field_int,
85 cli_field_skip,
86 cli_field_string,
87 cli_field_fmt,
88 cli_spaces,
89 cli_text,
90 cli_message,
91 cli_wrap_hint,
92 cli_flush,
93 0, /* Does not need MI hacks (i.e. needs CLI hacks). */
94};
95
96/* Prototypes for local functions */
97
98extern void _initialize_cli_out (void);
99
100static void field_separator (void);
101
102static void out_field_fmt (struct ui_out *uiout, int fldno,
103 const char *fldname,
104 const char *format,...);
105
106/* local variables */
107
108/* (none yet) */
109
110/* Mark beginning of a table */
111
112void
113cli_table_begin (struct ui_out *uiout, int nbrofcols,
114 int nr_rows,
115 const char *tblid)
116{
117 struct ui_out_data *data = ui_out_data (uiout);
118 if (nr_rows == 0)
119 data->suppress_output = 1;
120 else
121 /* Only the table suppresses the output and, fortunatly, a table
122 is not a recursive data structure. */
123 gdb_assert (data->suppress_output == 0);
124}
125
126/* Mark beginning of a table body */
127
128void
129cli_table_body (struct ui_out *uiout)
130{
131 struct ui_out_data *data = ui_out_data (uiout);
132 if (data->suppress_output)
133 return;
134 /* first, close the table header line */
135 cli_text (uiout, "\n");
136}
137
138/* Mark end of a table */
139
140void
141cli_table_end (struct ui_out *uiout)
142{
143 struct ui_out_data *data = ui_out_data (uiout);
144 data->suppress_output = 0;
145}
146
147/* Specify table header */
148
149void
150cli_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
151 const char *col_name,
152 const char *colhdr)
153{
154 struct ui_out_data *data = ui_out_data (uiout);
155 if (data->suppress_output)
156 return;
157 cli_field_string (uiout, 0, width, alignment, 0, colhdr);
158}
159
160/* Mark beginning of a list */
161
162void
163cli_begin (struct ui_out *uiout,
164 enum ui_out_type type,
165 int level,
166 const char *id)
167{
168 struct ui_out_data *data = ui_out_data (uiout);
169 if (data->suppress_output)
170 return;
171}
172
173/* Mark end of a list */
174
175void
176cli_end (struct ui_out *uiout,
177 enum ui_out_type type,
178 int level)
179{
180 struct ui_out_data *data = ui_out_data (uiout);
181 if (data->suppress_output)
182 return;
183}
184
185/* output an int field */
186
187void
188cli_field_int (struct ui_out *uiout, int fldno, int width,
189 enum ui_align alignment,
190 const char *fldname, int value)
191{
192 char buffer[20]; /* FIXME: how many chars long a %d can become? */
193
194 struct ui_out_data *data = ui_out_data (uiout);
195 if (data->suppress_output)
196 return;
197 sprintf (buffer, "%d", value);
198 cli_field_string (uiout, fldno, width, alignment, fldname, buffer);
199}
200
201/* used to ommit a field */
202
203void
204cli_field_skip (struct ui_out *uiout, int fldno, int width,
205 enum ui_align alignment,
206 const char *fldname)
207{
208 struct ui_out_data *data = ui_out_data (uiout);
209 if (data->suppress_output)
210 return;
211 cli_field_string (uiout, fldno, width, alignment, fldname, "");
212}
213
214/* other specific cli_field_* end up here so alignment and field
215 separators are both handled by cli_field_string */
216
217void
218cli_field_string (struct ui_out *uiout,
219 int fldno,
220 int width,
221 enum ui_align align,
222 const char *fldname,
223 const char *string)
224{
225 int before = 0;
226 int after = 0;
227
228 struct ui_out_data *data = ui_out_data (uiout);
229 if (data->suppress_output)
230 return;
231
232 if ((align != ui_noalign) && string)
233 {
234 before = width - strlen (string);
235 if (before <= 0)
236 before = 0;
237 else
238 {
239 if (align == ui_right)
240 after = 0;
241 else if (align == ui_left)
242 {
243 after = before;
244 before = 0;
245 }
246 else
247 /* ui_center */
248 {
249 after = before / 2;
250 before -= after;
251 }
252 }
253 }
254
255 if (before)
256 ui_out_spaces (uiout, before);
257 if (string)
258 out_field_fmt (uiout, fldno, fldname, "%s", string);
259 if (after)
260 ui_out_spaces (uiout, after);
261
262 if (align != ui_noalign)
263 field_separator ();
264}
265
266/* This is the only field function that does not align */
267
268void
269cli_field_fmt (struct ui_out *uiout, int fldno,
270 int width, enum ui_align align,
271 const char *fldname,
272 const char *format,
273 va_list args)
274{
275 struct ui_out_data *data = ui_out_data (uiout);
276 if (data->suppress_output)
277 return;
278
279 vfprintf_filtered (data->stream, format, args);
280
281 if (align != ui_noalign)
282 field_separator ();
283}
284
285void
286cli_spaces (struct ui_out *uiout, int numspaces)
287{
288 struct ui_out_data *data = ui_out_data (uiout);
289 if (data->suppress_output)
290 return;
291 print_spaces_filtered (numspaces, data->stream);
292}
293
294void
295cli_text (struct ui_out *uiout, const char *string)
296{
297 struct ui_out_data *data = ui_out_data (uiout);
298 if (data->suppress_output)
299 return;
300 fputs_filtered (string, data->stream);
301}
302
303void
304cli_message (struct ui_out *uiout, int verbosity,
305 const char *format, va_list args)
306{
307 struct ui_out_data *data = ui_out_data (uiout);
308 if (data->suppress_output)
309 return;
310 if (ui_out_get_verblvl (uiout) >= verbosity)
311 vfprintf_unfiltered (data->stream, format, args);
312}
313
314void
315cli_wrap_hint (struct ui_out *uiout, char *identstring)
316{
317 struct ui_out_data *data = ui_out_data (uiout);
318 if (data->suppress_output)
319 return;
320 wrap_here (identstring);
321}
322
323void
324cli_flush (struct ui_out *uiout)
325{
326 struct ui_out_data *data = ui_out_data (uiout);
327 gdb_flush (data->stream);
328}
329
330/* local functions */
331
332/* Like cli_field_fmt, but takes a variable number of args
333 and makes a va_list and does not insert a separator */
334
335/* VARARGS */
336static void
337out_field_fmt (struct ui_out *uiout, int fldno,
338 const char *fldname,
339 const char *format,...)
340{
341 struct ui_out_data *data = ui_out_data (uiout);
342 va_list args;
343
344 va_start (args, format);
345 vfprintf_filtered (data->stream, format, args);
346
347 va_end (args);
348}
349
350/* access to ui_out format private members */
351
352static void
353field_separator (void)
354{
355 struct ui_out_data *data = ui_out_data (uiout);
356 fputc_filtered (' ', data->stream);
357}
358
359/* initalize private members at startup */
360
361struct ui_out *
362cli_out_new (struct ui_file *stream)
363{
364 int flags = ui_source_list;
365
366 struct ui_out_data *data = XMALLOC (struct ui_out_data);
367 data->stream = stream;
368 data->suppress_output = 0;
369 return ui_out_new (&cli_ui_out_impl, data, flags);
370}
371
372/* standard gdb initialization hook */
373void
374_initialize_cli_out (void)
375{
376 /* nothing needs to be done */
377}
This page took 0.024109 seconds and 4 git commands to generate.