No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / gdb6 / gdb / cli-out.c
blob12a4f5ef9de627cdae6f11fb37e07283215a4a32
1 /* Output generating routines for GDB CLI.
3 Copyright (C) 1999, 2000, 2002, 2003, 2005
4 Free Software Foundation, Inc.
6 Contributed by Cygnus Solutions.
7 Written by Fernando Nasser for Cygnus.
9 This file is part of GDB.
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 2 of the License, or
14 (at your option) any later version.
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.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 Boston, MA 02110-1301, USA. */
26 #include "defs.h"
27 #include "ui-out.h"
28 #include "cli-out.h"
29 #include "gdb_string.h"
30 #include "gdb_assert.h"
32 struct ui_out_data
34 struct ui_file *stream;
35 struct ui_file *original_stream;
36 int suppress_output;
38 typedef struct ui_out_data cli_out_data;
40 /* These are the CLI output functions */
42 static void cli_table_begin (struct ui_out *uiout, int nbrofcols,
43 int nr_rows, const char *tblid);
44 static void cli_table_body (struct ui_out *uiout);
45 static void cli_table_end (struct ui_out *uiout);
46 static void cli_table_header (struct ui_out *uiout, int width,
47 enum ui_align alig, const char *col_name,
48 const char *colhdr);
49 static void cli_begin (struct ui_out *uiout, enum ui_out_type type,
50 int level, const char *lstid);
51 static void cli_end (struct ui_out *uiout, enum ui_out_type type, int level);
52 static void cli_field_int (struct ui_out *uiout, int fldno, int width,
53 enum ui_align alig, const char *fldname, int value);
54 static void cli_field_skip (struct ui_out *uiout, int fldno, int width,
55 enum ui_align alig, const char *fldname);
56 static void cli_field_string (struct ui_out *uiout, int fldno, int width,
57 enum ui_align alig, const char *fldname,
58 const char *string);
59 static void cli_field_fmt (struct ui_out *uiout, int fldno,
60 int width, enum ui_align align,
61 const char *fldname, const char *format,
62 va_list args) ATTR_FORMAT (printf, 6, 0);
63 static void cli_spaces (struct ui_out *uiout, int numspaces);
64 static void cli_text (struct ui_out *uiout, const char *string);
65 static void cli_message (struct ui_out *uiout, int verbosity,
66 const char *format, va_list args)
67 ATTR_FORMAT (printf, 3, 0);
68 static void cli_wrap_hint (struct ui_out *uiout, char *identstring);
69 static void cli_flush (struct ui_out *uiout);
70 static int cli_redirect (struct ui_out *uiout, struct ui_file *outstream);
72 /* This is the CLI ui-out implementation functions vector */
74 /* FIXME: This can be initialized dynamically after default is set to
75 handle initial output in main.c */
77 static struct ui_out_impl cli_ui_out_impl =
79 cli_table_begin,
80 cli_table_body,
81 cli_table_end,
82 cli_table_header,
83 cli_begin,
84 cli_end,
85 cli_field_int,
86 cli_field_skip,
87 cli_field_string,
88 cli_field_fmt,
89 cli_spaces,
90 cli_text,
91 cli_message,
92 cli_wrap_hint,
93 cli_flush,
94 cli_redirect,
95 0, /* Does not need MI hacks (i.e. needs CLI hacks). */
98 /* Prototypes for local functions */
100 extern void _initialize_cli_out (void);
102 static void field_separator (void);
104 static void out_field_fmt (struct ui_out *uiout, int fldno,
105 const char *fldname,
106 const char *format,...) ATTR_FORMAT (printf, 4, 5);
108 /* local variables */
110 /* (none yet) */
112 /* Mark beginning of a table */
114 void
115 cli_table_begin (struct ui_out *uiout, int nbrofcols,
116 int nr_rows,
117 const char *tblid)
119 cli_out_data *data = ui_out_data (uiout);
120 if (nr_rows == 0)
121 data->suppress_output = 1;
122 else
123 /* Only the table suppresses the output and, fortunately, a table
124 is not a recursive data structure. */
125 gdb_assert (data->suppress_output == 0);
128 /* Mark beginning of a table body */
130 void
131 cli_table_body (struct ui_out *uiout)
133 cli_out_data *data = ui_out_data (uiout);
134 if (data->suppress_output)
135 return;
136 /* first, close the table header line */
137 cli_text (uiout, "\n");
140 /* Mark end of a table */
142 void
143 cli_table_end (struct ui_out *uiout)
145 cli_out_data *data = ui_out_data (uiout);
146 data->suppress_output = 0;
149 /* Specify table header */
151 void
152 cli_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
153 const char *col_name,
154 const char *colhdr)
156 cli_out_data *data = ui_out_data (uiout);
157 if (data->suppress_output)
158 return;
159 cli_field_string (uiout, 0, width, alignment, 0, colhdr);
162 /* Mark beginning of a list */
164 void
165 cli_begin (struct ui_out *uiout,
166 enum ui_out_type type,
167 int level,
168 const char *id)
170 cli_out_data *data = ui_out_data (uiout);
171 if (data->suppress_output)
172 return;
175 /* Mark end of a list */
177 void
178 cli_end (struct ui_out *uiout,
179 enum ui_out_type type,
180 int level)
182 cli_out_data *data = ui_out_data (uiout);
183 if (data->suppress_output)
184 return;
187 /* output an int field */
189 void
190 cli_field_int (struct ui_out *uiout, int fldno, int width,
191 enum ui_align alignment,
192 const char *fldname, int value)
194 char buffer[20]; /* FIXME: how many chars long a %d can become? */
196 cli_out_data *data = ui_out_data (uiout);
197 if (data->suppress_output)
198 return;
199 sprintf (buffer, "%d", value);
200 cli_field_string (uiout, fldno, width, alignment, fldname, buffer);
203 /* used to ommit a field */
205 void
206 cli_field_skip (struct ui_out *uiout, int fldno, int width,
207 enum ui_align alignment,
208 const char *fldname)
210 cli_out_data *data = ui_out_data (uiout);
211 if (data->suppress_output)
212 return;
213 cli_field_string (uiout, fldno, width, alignment, fldname, "");
216 /* other specific cli_field_* end up here so alignment and field
217 separators are both handled by cli_field_string */
219 void
220 cli_field_string (struct ui_out *uiout,
221 int fldno,
222 int width,
223 enum ui_align align,
224 const char *fldname,
225 const char *string)
227 int before = 0;
228 int after = 0;
230 cli_out_data *data = ui_out_data (uiout);
231 if (data->suppress_output)
232 return;
234 if ((align != ui_noalign) && string)
236 before = width - strlen (string);
237 if (before <= 0)
238 before = 0;
239 else
241 if (align == ui_right)
242 after = 0;
243 else if (align == ui_left)
245 after = before;
246 before = 0;
248 else
249 /* ui_center */
251 after = before / 2;
252 before -= after;
257 if (before)
258 ui_out_spaces (uiout, before);
259 if (string)
260 out_field_fmt (uiout, fldno, fldname, "%s", string);
261 if (after)
262 ui_out_spaces (uiout, after);
264 if (align != ui_noalign)
265 field_separator ();
268 /* This is the only field function that does not align. */
270 void
271 cli_field_fmt (struct ui_out *uiout, int fldno,
272 int width, enum ui_align align,
273 const char *fldname,
274 const char *format,
275 va_list args)
277 cli_out_data *data = ui_out_data (uiout);
278 if (data->suppress_output)
279 return;
281 vfprintf_filtered (data->stream, format, args);
283 if (align != ui_noalign)
284 field_separator ();
287 void
288 cli_spaces (struct ui_out *uiout, int numspaces)
290 cli_out_data *data = ui_out_data (uiout);
291 if (data->suppress_output)
292 return;
293 print_spaces_filtered (numspaces, data->stream);
296 void
297 cli_text (struct ui_out *uiout, const char *string)
299 cli_out_data *data = ui_out_data (uiout);
300 if (data->suppress_output)
301 return;
302 fputs_filtered (string, data->stream);
305 void
306 cli_message (struct ui_out *uiout, int verbosity,
307 const char *format, va_list args)
309 cli_out_data *data = ui_out_data (uiout);
310 if (data->suppress_output)
311 return;
312 if (ui_out_get_verblvl (uiout) >= verbosity)
313 vfprintf_unfiltered (data->stream, format, args);
316 void
317 cli_wrap_hint (struct ui_out *uiout, char *identstring)
319 cli_out_data *data = ui_out_data (uiout);
320 if (data->suppress_output)
321 return;
322 wrap_here (identstring);
325 void
326 cli_flush (struct ui_out *uiout)
328 cli_out_data *data = ui_out_data (uiout);
329 gdb_flush (data->stream);
333 cli_redirect (struct ui_out *uiout, struct ui_file *outstream)
335 struct ui_out_data *data = ui_out_data (uiout);
336 if (outstream != NULL)
338 data->original_stream = data->stream;
339 data->stream = outstream;
341 else if (data->original_stream != NULL)
343 data->stream = data->original_stream;
344 data->original_stream = NULL;
347 return 0;
350 /* local functions */
352 /* Like cli_field_fmt, but takes a variable number of args
353 and makes a va_list and does not insert a separator. */
355 /* VARARGS */
356 static void
357 out_field_fmt (struct ui_out *uiout, int fldno,
358 const char *fldname,
359 const char *format,...)
361 cli_out_data *data = ui_out_data (uiout);
362 va_list args;
364 va_start (args, format);
365 vfprintf_filtered (data->stream, format, args);
367 va_end (args);
370 /* Access to ui_out format private members. */
372 static void
373 field_separator (void)
375 cli_out_data *data = ui_out_data (uiout);
376 fputc_filtered (' ', data->stream);
379 /* Initalize private members at startup. */
381 struct ui_out *
382 cli_out_new (struct ui_file *stream)
384 int flags = ui_source_list;
386 cli_out_data *data = XMALLOC (cli_out_data);
387 data->stream = stream;
388 data->original_stream = NULL;
389 data->suppress_output = 0;
390 return ui_out_new (&cli_ui_out_impl, data, flags);
393 struct ui_file *
394 cli_out_set_stream (struct ui_out *uiout, struct ui_file *stream)
396 cli_out_data *data = ui_out_data (uiout);
397 struct ui_file *old = data->stream;
398 data->stream = stream;
399 return old;
402 /* Standard gdb initialization hook. */
403 void
404 _initialize_cli_out (void)
406 /* nothing needs to be done */