* tui-out.c (tui_out_data): Fix typedef.
[external/binutils.git] / gdb / tui / tui-out.c
1 /* Output generating routines for GDB CLI.
2
3    Copyright 1999, 2000, 2001, 2002, 2003 Free Software Foundation,
4    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 2 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, write to the Free Software
23    Foundation, Inc., 59 Temple Place - Suite 330,
24    Boston, MA 02111-1307, USA.  */
25
26 #include "defs.h"
27 #include "ui-out.h"
28 #include "tui.h"
29 #include "gdb_string.h"
30 #include "gdb_assert.h"
31
32 struct ui_out_data
33   {
34     struct ui_file *stream;
35     int suppress_output;
36     int line;
37     int start_of_line;
38   };
39 typedef struct ui_out_data tui_out_data;
40
41 /* These are the CLI output functions */
42
43 static void tui_table_begin (struct ui_out *uiout, int nbrofcols,
44                              int nr_rows, const char *tblid);
45 static void tui_table_body (struct ui_out *uiout);
46 static void tui_table_end (struct ui_out *uiout);
47 static void tui_table_header (struct ui_out *uiout, int width,
48                               enum ui_align alig, const char *col_name,
49                               const char *colhdr);
50 static void tui_begin (struct ui_out *uiout, enum ui_out_type type,
51                        int level, const char *lstid);
52 static void tui_end (struct ui_out *uiout, enum ui_out_type type, int level);
53 static void tui_field_int (struct ui_out *uiout, int fldno, int width,
54                            enum ui_align alig, const char *fldname, int value);
55 static void tui_field_skip (struct ui_out *uiout, int fldno, int width,
56                             enum ui_align alig, const char *fldname);
57 static void tui_field_string (struct ui_out *uiout, int fldno, int width,
58                               enum ui_align alig, const char *fldname,
59                               const char *string);
60 static void tui_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);
64 static void tui_spaces (struct ui_out *uiout, int numspaces);
65 static void tui_text (struct ui_out *uiout, const char *string);
66 static void tui_message (struct ui_out *uiout, int verbosity,
67                          const char *format, va_list args);
68 static void tui_wrap_hint (struct ui_out *uiout, char *identstring);
69 static void tui_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
76 static struct ui_out_impl tui_ui_out_impl =
77 {
78   tui_table_begin,
79   tui_table_body,
80   tui_table_end,
81   tui_table_header,
82   tui_begin,
83   tui_end,
84   tui_field_int,
85   tui_field_skip,
86   tui_field_string,
87   tui_field_fmt,
88   tui_spaces,
89   tui_text,
90   tui_message,
91   tui_wrap_hint,
92   tui_flush,
93   0, /* Does not need MI hacks (i.e. needs CLI hacks).  */
94 };
95
96 /* Prototypes for local functions */
97
98 extern void _initialize_tui_out (void);
99
100 static void field_separator (void);
101
102 static 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
112 void
113 tui_table_begin (struct ui_out *uiout, int nbrofcols,
114                  int nr_rows,
115                  const char *tblid)
116 {
117   tui_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
128 void
129 tui_table_body (struct ui_out *uiout)
130 {
131   tui_out_data *data = ui_out_data (uiout);
132   if (data->suppress_output)
133     return;
134   /* first, close the table header line */
135   tui_text (uiout, "\n");
136 }
137
138 /* Mark end of a table */
139
140 void
141 tui_table_end (struct ui_out *uiout)
142 {
143   tui_out_data *data = ui_out_data (uiout);
144   data->suppress_output = 0;
145 }
146
147 /* Specify table header */
148
149 void
150 tui_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
151                   const char *col_name,
152                   const char *colhdr)
153 {
154   tui_out_data *data = ui_out_data (uiout);
155   if (data->suppress_output)
156     return;
157   tui_field_string (uiout, 0, width, alignment, 0, colhdr);
158 }
159
160 /* Mark beginning of a list */
161
162 void
163 tui_begin (struct ui_out *uiout,
164            enum ui_out_type type,
165            int level,
166            const char *id)
167 {
168   tui_out_data *data = ui_out_data (uiout);
169   if (data->suppress_output)
170     return;
171 }
172
173 /* Mark end of a list */
174
175 void
176 tui_end (struct ui_out *uiout,
177          enum ui_out_type type,
178          int level)
179 {
180   tui_out_data *data = ui_out_data (uiout);
181   if (data->suppress_output)
182     return;
183 }
184
185 /* output an int field */
186
187 void
188 tui_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   tui_out_data *data = ui_out_data (uiout);
195   if (data->suppress_output)
196     return;
197
198   /* Don't print line number, keep it for later.  */
199   if (data->start_of_line == 0 && strcmp (fldname, "line") == 0)
200     {
201       data->start_of_line ++;
202       data->line = value;
203       return;
204     }
205   data->start_of_line ++;
206   sprintf (buffer, "%d", value);
207   tui_field_string (uiout, fldno, width, alignment, fldname, buffer);
208 }
209
210 /* used to ommit a field */
211
212 void
213 tui_field_skip (struct ui_out *uiout, int fldno, int width,
214                 enum ui_align alignment,
215                 const char *fldname)
216 {
217   tui_out_data *data = ui_out_data (uiout);
218   if (data->suppress_output)
219     return;
220   tui_field_string (uiout, fldno, width, alignment, fldname, "");
221 }
222
223 /* other specific tui_field_* end up here so alignment and field
224    separators are both handled by tui_field_string */
225
226 void
227 tui_field_string (struct ui_out *uiout,
228                   int fldno,
229                   int width,
230                   enum ui_align align,
231                   const char *fldname,
232                   const char *string)
233 {
234   int before = 0;
235   int after = 0;
236
237   tui_out_data *data = ui_out_data (uiout);
238   if (data->suppress_output)
239     return;
240
241   if (fldname && data->line > 0 && strcmp (fldname, "file") == 0)
242     {
243       data->start_of_line ++;
244       if (data->line > 0)
245         {
246           tui_show_source (string, data->line);
247         }
248       return;
249     }
250   
251   data->start_of_line ++;
252   if ((align != ui_noalign) && string)
253     {
254       before = width - strlen (string);
255       if (before <= 0)
256         before = 0;
257       else
258         {
259           if (align == ui_right)
260             after = 0;
261           else if (align == ui_left)
262             {
263               after = before;
264               before = 0;
265             }
266           else
267             /* ui_center */
268             {
269               after = before / 2;
270               before -= after;
271             }
272         }
273     }
274
275   if (before)
276     ui_out_spaces (uiout, before);
277   if (string)
278     out_field_fmt (uiout, fldno, fldname, "%s", string);
279   if (after)
280     ui_out_spaces (uiout, after);
281
282   if (align != ui_noalign)
283     field_separator ();
284 }
285
286 /* This is the only field function that does not align */
287
288 void
289 tui_field_fmt (struct ui_out *uiout, int fldno,
290                int width, enum ui_align align,
291                const char *fldname,
292                const char *format,
293                va_list args)
294 {
295   tui_out_data *data = ui_out_data (uiout);
296   if (data->suppress_output)
297     return;
298
299   data->start_of_line ++;
300   vfprintf_filtered (data->stream, format, args);
301
302   if (align != ui_noalign)
303     field_separator ();
304 }
305
306 void
307 tui_spaces (struct ui_out *uiout, int numspaces)
308 {
309   tui_out_data *data = ui_out_data (uiout);
310   if (data->suppress_output)
311     return;
312   print_spaces_filtered (numspaces, data->stream);
313 }
314
315 void
316 tui_text (struct ui_out *uiout, const char *string)
317 {
318   tui_out_data *data = ui_out_data (uiout);
319   if (data->suppress_output)
320     return;
321   data->start_of_line ++;
322   if (data->line > 0)
323     {
324       if (strchr (string, '\n') != 0)
325         {
326           data->line = -1;
327           data->start_of_line = 0;
328         }
329       return;
330     }
331   if (strchr (string, '\n'))
332     data->start_of_line = 0;
333   fputs_filtered (string, data->stream);
334 }
335
336 void
337 tui_message (struct ui_out *uiout, int verbosity,
338              const char *format, va_list args)
339 {
340   tui_out_data *data = ui_out_data (uiout);
341   if (data->suppress_output)
342     return;
343   if (ui_out_get_verblvl (uiout) >= verbosity)
344     vfprintf_unfiltered (data->stream, format, args);
345 }
346
347 void
348 tui_wrap_hint (struct ui_out *uiout, char *identstring)
349 {
350   tui_out_data *data = ui_out_data (uiout);
351   if (data->suppress_output)
352     return;
353   wrap_here (identstring);
354 }
355
356 void
357 tui_flush (struct ui_out *uiout)
358 {
359   tui_out_data *data = ui_out_data (uiout);
360   gdb_flush (data->stream);
361 }
362
363 /* local functions */
364
365 /* Like tui_field_fmt, but takes a variable number of args
366    and makes a va_list and does not insert a separator */
367
368 /* VARARGS */
369 static void
370 out_field_fmt (struct ui_out *uiout, int fldno,
371                const char *fldname,
372                const char *format,...)
373 {
374   tui_out_data *data = ui_out_data (uiout);
375   va_list args;
376
377   va_start (args, format);
378   vfprintf_filtered (data->stream, format, args);
379
380   va_end (args);
381 }
382
383 /* access to ui_out format private members */
384
385 static void
386 field_separator (void)
387 {
388   tui_out_data *data = ui_out_data (uiout);
389   fputc_filtered (' ', data->stream);
390 }
391
392 /* initalize private members at startup */
393
394 struct ui_out *
395 tui_out_new (struct ui_file *stream)
396 {
397   int flags = 0;
398
399   tui_out_data *data = XMALLOC (tui_out_data);
400   data->stream = stream;
401   data->suppress_output = 0;
402   data->line = -1;
403   data->start_of_line = 0;
404   return ui_out_new (&tui_ui_out_impl, data, flags);
405 }
406
407 /* standard gdb initialization hook */
408 void
409 _initialize_tui_out (void)
410 {
411   /* nothing needs to be done */
412 }