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