Make char *, const char *.
[external/binutils.git] / gdb / cli-out.c
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
28 /* Convenience macro for allocting typesafe memory. */
29
30 #ifndef XMALLOC
31 #define XMALLOC(TYPE) (TYPE*) xmalloc (sizeof (TYPE))
32 #endif
33
34 struct ui_out_data
35   {
36     struct ui_file *stream;
37   };
38
39 /* These are the CLI output functions */
40
41 static void cli_table_begin (struct ui_out *uiout, int nbrofcols,
42                              const char *tblid);
43 static void cli_table_body (struct ui_out *uiout);
44 static void cli_table_end (struct ui_out *uiout);
45 static void cli_table_header (struct ui_out *uiout, int width,
46                               enum ui_align alig,
47                               const char *colhdr);
48 static void cli_begin (struct ui_out *uiout, enum ui_out_type type,
49                        int level, const char *lstid);
50 static void cli_end (struct ui_out *uiout, enum ui_out_type type, int level);
51 static void cli_field_int (struct ui_out *uiout, int fldno, int width,
52                            enum ui_align alig, const char *fldname, int value);
53 static void cli_field_skip (struct ui_out *uiout, int fldno, int width,
54                             enum ui_align alig, const char *fldname);
55 static void cli_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 cli_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 cli_spaces (struct ui_out *uiout, int numspaces);
63 static void cli_text (struct ui_out *uiout, const char *string);
64 static void cli_message (struct ui_out *uiout, int verbosity,
65                          const char *format, va_list args);
66 static void cli_wrap_hint (struct ui_out *uiout, char *identstring);
67 static void cli_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 cli_ui_out_impl =
75 {
76   cli_table_begin,
77   cli_table_body,
78   cli_table_end,
79   cli_table_header,
80   cli_begin,
81   cli_end,
82   cli_field_int,
83   cli_field_skip,
84   cli_field_string,
85   cli_field_fmt,
86   cli_spaces,
87   cli_text,
88   cli_message,
89   cli_wrap_hint,
90   cli_flush
91 };
92
93 /* Prototypes for local functions */
94
95 extern void _initialize_cli_out (void);
96
97 static void field_separator (void);
98
99 static void out_field_fmt (struct ui_out *uiout, int fldno,
100                            const char *fldname,
101                            const char *format,...);
102
103 /* local variables */
104
105 /* (none yet) */
106
107 /* Mark beginning of a table */
108
109 void
110 cli_table_begin (struct ui_out *uiout, int nbrofcols,
111                  const char *tblid)
112 {
113 }
114
115 /* Mark beginning of a table body */
116
117 void
118 cli_table_body (struct ui_out *uiout)
119 {
120   /* first, close the table header line */
121   cli_text (uiout, "\n");
122 }
123
124 /* Mark end of a table */
125
126 void
127 cli_table_end (struct ui_out *uiout)
128 {
129 }
130
131 /* Specify table header */
132
133 void
134 cli_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
135                   const char *colhdr)
136 {
137   cli_field_string (uiout, 0, width, alignment, 0, colhdr);
138 }
139
140 /* Mark beginning of a list */
141
142 void
143 cli_begin (struct ui_out *uiout,
144            enum ui_out_type type,
145            int level,
146            const char *id)
147 {
148 }
149
150 /* Mark end of a list */
151
152 void
153 cli_end (struct ui_out *uiout,
154          enum ui_out_type type,
155          int level)
156 {
157 }
158
159 /* output an int field */
160
161 void
162 cli_field_int (struct ui_out *uiout, int fldno, int width,
163                enum ui_align alignment,
164                const char *fldname, int value)
165 {
166   char buffer[20];              /* FIXME: how many chars long a %d can become? */
167
168   sprintf (buffer, "%d", value);
169   cli_field_string (uiout, fldno, width, alignment, fldname, buffer);
170 }
171
172 /* used to ommit a field */
173
174 void
175 cli_field_skip (struct ui_out *uiout, int fldno, int width,
176                 enum ui_align alignment,
177                 const char *fldname)
178 {
179   cli_field_string (uiout, fldno, width, alignment, fldname, "");
180 }
181
182 /* other specific cli_field_* end up here so alignment and field
183    separators are both handled by cli_field_string */
184
185 void
186 cli_field_string (struct ui_out *uiout,
187                   int fldno,
188                   int width,
189                   enum ui_align align,
190                   const char *fldname,
191                   const char *string)
192 {
193   int before = 0;
194   int after = 0;
195
196   if ((align != ui_noalign) && string)
197     {
198       before = width - strlen (string);
199       if (before <= 0)
200         before = 0;
201       else
202         {
203           if (align == ui_right)
204             after = 0;
205           else if (align == ui_left)
206             {
207               after = before;
208               before = 0;
209             }
210           else
211             /* ui_center */
212             {
213               after = before / 2;
214               before -= after;
215             }
216         }
217     }
218
219   if (before)
220     ui_out_spaces (uiout, before);
221   if (string)
222     out_field_fmt (uiout, fldno, fldname, "%s", string);
223   if (after)
224     ui_out_spaces (uiout, after);
225
226   if (align != ui_noalign)
227     field_separator ();
228 }
229
230 /* This is the only field function that does not align */
231
232 void
233 cli_field_fmt (struct ui_out *uiout, int fldno,
234                int width, enum ui_align align,
235                const char *fldname,
236                const char *format,
237                va_list args)
238 {
239   struct ui_out_data *data = ui_out_data (uiout);
240   vfprintf_filtered (data->stream, format, args);
241
242   if (align != ui_noalign)
243     field_separator ();
244 }
245
246 void
247 cli_spaces (struct ui_out *uiout, int numspaces)
248 {
249   struct ui_out_data *data = ui_out_data (uiout);
250   print_spaces_filtered (numspaces, data->stream);
251 }
252
253 void
254 cli_text (struct ui_out *uiout, const char *string)
255 {
256   struct ui_out_data *data = ui_out_data (uiout);
257   fputs_filtered (string, data->stream);
258 }
259
260 void
261 cli_message (struct ui_out *uiout, int verbosity,
262              const char *format, va_list args)
263 {
264   struct ui_out_data *data = ui_out_data (uiout);
265   if (ui_out_get_verblvl (uiout) >= verbosity)
266     vfprintf_unfiltered (data->stream, format, args);
267 }
268
269 void
270 cli_wrap_hint (struct ui_out *uiout, char *identstring)
271 {
272   wrap_here (identstring);
273 }
274
275 void
276 cli_flush (struct ui_out *uiout)
277 {
278   struct ui_out_data *data = ui_out_data (uiout);
279   gdb_flush (data->stream);
280 }
281
282 /* local functions */
283
284 /* Like cli_field_fmt, but takes a variable number of args
285    and makes a va_list and does not insert a separator */
286
287 /* VARARGS */
288 static void
289 out_field_fmt (struct ui_out *uiout, int fldno,
290                const char *fldname,
291                const char *format,...)
292 {
293   struct ui_out_data *data = ui_out_data (uiout);
294   va_list args;
295
296   va_start (args, format);
297   vfprintf_filtered (data->stream, format, args);
298
299   va_end (args);
300 }
301
302 /* access to ui_out format private members */
303
304 static void
305 field_separator (void)
306 {
307   struct ui_out_data *data = ui_out_data (uiout);
308   fputc_filtered (' ', data->stream);
309 }
310
311 /* initalize private members at startup */
312
313 struct ui_out *
314 cli_out_new (struct ui_file *stream)
315 {
316   int flags = ui_source_list;
317
318   struct ui_out_data *data = XMALLOC (struct ui_out_data);
319   data->stream = stream;
320   return ui_out_new (&cli_ui_out_impl, data, flags);
321 }
322
323 /* standard gdb initialization hook */
324 void
325 _initialize_cli_out (void)
326 {
327   /* nothing needs to be done */
328 }