2007-06-06 Markus Deuling <deuling@de.ibm.com>
[deliverable/binutils-gdb.git] / gdb / cli / cli-setshow.c
CommitLineData
d318976c 1/* Handle set and show GDB commands.
8926118c 2
6aba47ca 3 Copyright (c) 2000, 2001, 2002, 2003, 2007 Free Software Foundation, Inc.
d318976c
FN
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
10f9c213
EZ
17 Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA. */
d318976c
FN
19
20#include "defs.h"
dbda9972 21#include "readline/tilde.h"
d318976c
FN
22#include "value.h"
23#include <ctype.h>
d318976c 24#include "gdb_string.h"
d318976c 25
d318976c 26#include "ui-out.h"
d318976c
FN
27
28#include "cli/cli-decode.h"
29#include "cli/cli-cmds.h"
30#include "cli/cli-setshow.h"
31
32/* Prototypes for local functions */
33
34static int parse_binary_operation (char *);
35
d318976c 36\f
7f19b9a2 37static enum auto_boolean
d318976c
FN
38parse_auto_binary_operation (const char *arg)
39{
40 if (arg != NULL && *arg != '\0')
41 {
42 int length = strlen (arg);
43 while (isspace (arg[length - 1]) && length > 0)
44 length--;
45 if (strncmp (arg, "on", length) == 0
46 || strncmp (arg, "1", length) == 0
47 || strncmp (arg, "yes", length) == 0
48 || strncmp (arg, "enable", length) == 0)
7f19b9a2 49 return AUTO_BOOLEAN_TRUE;
d318976c
FN
50 else if (strncmp (arg, "off", length) == 0
51 || strncmp (arg, "0", length) == 0
52 || strncmp (arg, "no", length) == 0
53 || strncmp (arg, "disable", length) == 0)
7f19b9a2 54 return AUTO_BOOLEAN_FALSE;
d318976c
FN
55 else if (strncmp (arg, "auto", length) == 0
56 || (strncmp (arg, "-1", length) == 0 && length > 1))
7f19b9a2 57 return AUTO_BOOLEAN_AUTO;
d318976c 58 }
8a3fe4f8 59 error (_("\"on\", \"off\" or \"auto\" expected."));
7f19b9a2 60 return AUTO_BOOLEAN_AUTO; /* pacify GCC */
d318976c
FN
61}
62
63static int
64parse_binary_operation (char *arg)
65{
66 int length;
67
68 if (!arg || !*arg)
69 return 1;
70
71 length = strlen (arg);
72
73 while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
74 length--;
75
76 if (strncmp (arg, "on", length) == 0
77 || strncmp (arg, "1", length) == 0
78 || strncmp (arg, "yes", length) == 0
79 || strncmp (arg, "enable", length) == 0)
80 return 1;
81 else if (strncmp (arg, "off", length) == 0
82 || strncmp (arg, "0", length) == 0
83 || strncmp (arg, "no", length) == 0
84 || strncmp (arg, "disable", length) == 0)
85 return 0;
86 else
87 {
8a3fe4f8 88 error (_("\"on\" or \"off\" expected."));
d318976c
FN
89 return 0;
90 }
91}
92\f
08546159
AC
93void
94deprecated_show_value_hack (struct ui_file *ignore_file,
95 int ignore_from_tty,
96 struct cmd_list_element *c,
97 const char *value)
98{
4d28ad1e
AC
99 /* If there's no command or value, don't try to print it out. */
100 if (c == NULL || value == NULL)
101 return;
08546159
AC
102 /* Print doc minus "show" at start. */
103 print_doc_line (gdb_stdout, c->doc + 5);
104 switch (c->var_type)
105 {
106 case var_string:
107 case var_string_noescape:
b4b4ac0b 108 case var_optional_filename:
08546159
AC
109 case var_filename:
110 case var_enum:
111 printf_filtered ((" is \"%s\".\n"), value);
112 break;
113 default:
114 printf_filtered ((" is %s.\n"), value);
115 break;
116 }
117}
118
d318976c
FN
119/* Do a "set" or "show" command. ARG is NULL if no argument, or the text
120 of the argument, and FROM_TTY is nonzero if this command is being entered
121 directly by the user (i.e. these are just like any other
122 command). C is the command list element for the command. */
123
124void
125do_setshow_command (char *arg, int from_tty, struct cmd_list_element *c)
126{
127 if (c->type == set_cmd)
128 {
129 switch (c->var_type)
130 {
131 case var_string:
132 {
133 char *new;
134 char *p;
135 char *q;
136 int ch;
137
138 if (arg == NULL)
139 arg = "";
140 new = (char *) xmalloc (strlen (arg) + 2);
141 p = arg;
142 q = new;
143 while ((ch = *p++) != '\000')
144 {
145 if (ch == '\\')
146 {
147 /* \ at end of argument is used after spaces
148 so they won't be lost. */
149 /* This is obsolete now that we no longer strip
150 trailing whitespace and actually, the backslash
151 didn't get here in my test, readline or
152 something did something funky with a backslash
153 right before a newline. */
154 if (*p == 0)
155 break;
156 ch = parse_escape (&p);
157 if (ch == 0)
158 break; /* C loses */
159 else if (ch > 0)
160 *q++ = ch;
161 }
162 else
163 *q++ = ch;
164 }
165#if 0
166 if (*(p - 1) != '\\')
167 *q++ = ' ';
168#endif
169 *q++ = '\0';
170 new = (char *) xrealloc (new, q - new);
171 if (*(char **) c->var != NULL)
b8c9b27d 172 xfree (*(char **) c->var);
d318976c
FN
173 *(char **) c->var = new;
174 }
175 break;
176 case var_string_noescape:
177 if (arg == NULL)
178 arg = "";
179 if (*(char **) c->var != NULL)
b8c9b27d 180 xfree (*(char **) c->var);
d318976c
FN
181 *(char **) c->var = savestring (arg, strlen (arg));
182 break;
b4b4ac0b 183 case var_optional_filename:
d318976c 184 if (arg == NULL)
525226b5
AC
185 arg = "";
186 if (*(char **) c->var != NULL)
187 xfree (*(char **) c->var);
188 *(char **) c->var = savestring (arg, strlen (arg));
189 break;
190 case var_filename:
191 if (arg == NULL)
192 error_no_arg (_("filename to set it to."));
d318976c 193 if (*(char **) c->var != NULL)
b8c9b27d 194 xfree (*(char **) c->var);
1430be3e
MR
195 {
196 /* Clear trailing whitespace of filename. */
197 char *ptr = arg + strlen (arg) - 1;
198 while (ptr >= arg && (*ptr == ' ' || *ptr == '\t'))
199 ptr--;
200 *(ptr + 1) = '\0';
201 }
d318976c
FN
202 *(char **) c->var = tilde_expand (arg);
203 break;
204 case var_boolean:
205 *(int *) c->var = parse_binary_operation (arg);
206 break;
207 case var_auto_boolean:
7f19b9a2 208 *(enum auto_boolean *) c->var = parse_auto_binary_operation (arg);
d318976c
FN
209 break;
210 case var_uinteger:
211 if (arg == NULL)
e2e0b3e5 212 error_no_arg (_("integer to set it to."));
d318976c
FN
213 *(unsigned int *) c->var = parse_and_eval_long (arg);
214 if (*(unsigned int *) c->var == 0)
215 *(unsigned int *) c->var = UINT_MAX;
216 break;
217 case var_integer:
218 {
219 unsigned int val;
220 if (arg == NULL)
e2e0b3e5 221 error_no_arg (_("integer to set it to."));
d318976c
FN
222 val = parse_and_eval_long (arg);
223 if (val == 0)
224 *(int *) c->var = INT_MAX;
225 else if (val >= INT_MAX)
8a3fe4f8 226 error (_("integer %u out of range"), val);
d318976c
FN
227 else
228 *(int *) c->var = val;
229 break;
230 }
231 case var_zinteger:
232 if (arg == NULL)
e2e0b3e5 233 error_no_arg (_("integer to set it to."));
d318976c
FN
234 *(int *) c->var = parse_and_eval_long (arg);
235 break;
236 case var_enum:
237 {
238 int i;
239 int len;
240 int nmatches;
241 const char *match = NULL;
242 char *p;
243
244 /* if no argument was supplied, print an informative error message */
245 if (arg == NULL)
246 {
247 char msg[1024];
248 strcpy (msg, "Requires an argument. Valid arguments are ");
249 for (i = 0; c->enums[i]; i++)
250 {
251 if (i != 0)
252 strcat (msg, ", ");
253 strcat (msg, c->enums[i]);
254 }
255 strcat (msg, ".");
8a3fe4f8 256 error (("%s"), msg);
d318976c
FN
257 }
258
259 p = strchr (arg, ' ');
260
261 if (p)
262 len = p - arg;
263 else
264 len = strlen (arg);
265
266 nmatches = 0;
267 for (i = 0; c->enums[i]; i++)
268 if (strncmp (arg, c->enums[i], len) == 0)
269 {
270 if (c->enums[i][len] == '\0')
271 {
272 match = c->enums[i];
273 nmatches = 1;
274 break; /* exact match. */
275 }
276 else
277 {
278 match = c->enums[i];
279 nmatches++;
280 }
281 }
282
283 if (nmatches <= 0)
8a3fe4f8 284 error (_("Undefined item: \"%s\"."), arg);
d318976c
FN
285
286 if (nmatches > 1)
8a3fe4f8 287 error (_("Ambiguous item \"%s\"."), arg);
d318976c
FN
288
289 *(const char **) c->var = match;
290 }
291 break;
292 default:
8a3fe4f8 293 error (_("gdb internal error: bad var_type in do_setshow_command"));
d318976c
FN
294 }
295 }
296 else if (c->type == show_cmd)
297 {
d318976c
FN
298 struct cleanup *old_chain;
299 struct ui_stream *stb;
d318976c
FN
300
301 stb = ui_out_stream_new (uiout);
302 old_chain = make_cleanup_ui_out_stream_delete (stb);
d318976c 303
552c04a7
TT
304 /* Possibly call the pre hook. */
305 if (c->pre_show_hook)
306 (c->pre_show_hook) (c);
307
d318976c
FN
308 switch (c->var_type)
309 {
310 case var_string:
3b113db7
DJ
311 if (*(char **) c->var)
312 fputstr_filtered (*(char **) c->var, '"', stb->stream);
d318976c
FN
313 break;
314 case var_string_noescape:
b4b4ac0b 315 case var_optional_filename:
d318976c
FN
316 case var_filename:
317 case var_enum:
318 if (*(char **) c->var)
319 fputs_filtered (*(char **) c->var, stb->stream);
d318976c
FN
320 break;
321 case var_boolean:
322 fputs_filtered (*(int *) c->var ? "on" : "off", stb->stream);
323 break;
324 case var_auto_boolean:
7f19b9a2 325 switch (*(enum auto_boolean*) c->var)
d318976c 326 {
7f19b9a2 327 case AUTO_BOOLEAN_TRUE:
d318976c
FN
328 fputs_filtered ("on", stb->stream);
329 break;
7f19b9a2 330 case AUTO_BOOLEAN_FALSE:
d318976c
FN
331 fputs_filtered ("off", stb->stream);
332 break;
7f19b9a2 333 case AUTO_BOOLEAN_AUTO:
d318976c
FN
334 fputs_filtered ("auto", stb->stream);
335 break;
336 default:
8e65ff28 337 internal_error (__FILE__, __LINE__,
e2e0b3e5 338 _("do_setshow_command: invalid var_auto_boolean"));
d318976c
FN
339 break;
340 }
341 break;
342 case var_uinteger:
343 if (*(unsigned int *) c->var == UINT_MAX)
344 {
345 fputs_filtered ("unlimited", stb->stream);
346 break;
347 }
348 /* else fall through */
349 case var_zinteger:
350 fprintf_filtered (stb->stream, "%u", *(unsigned int *) c->var);
351 break;
352 case var_integer:
353 if (*(int *) c->var == INT_MAX)
354 {
355 fputs_filtered ("unlimited", stb->stream);
356 }
357 else
358 fprintf_filtered (stb->stream, "%d", *(int *) c->var);
359 break;
360
361 default:
8a3fe4f8 362 error (_("gdb internal error: bad var_type in do_setshow_command"));
d318976c 363 }
899506a8
AC
364
365
366 /* FIXME: cagney/2005-02-10: Need to split this in half: code to
367 convert the value into a string (esentially the above); and
368 code to print the value out. For the latter there should be
369 MI and CLI specific versions. */
370
371 if (ui_out_is_mi_like_p (uiout))
372 ui_out_field_stream (uiout, "value", stb);
08546159 373 else
335cca0d
AC
374 {
375 long length;
376 char *value = ui_file_xstrdup (stb->stream, &length);
377 make_cleanup (xfree, value);
08546159
AC
378 if (c->show_value_func != NULL)
379 c->show_value_func (gdb_stdout, from_tty, c, value);
380 else
381 deprecated_show_value_hack (gdb_stdout, from_tty, c, value);
899506a8 382 }
d318976c 383 do_cleanups (old_chain);
d318976c
FN
384 }
385 else
8a3fe4f8 386 error (_("gdb internal error: bad cmd_type in do_setshow_command"));
9f60d481 387 c->func (c, NULL, from_tty);
9a4105ab
AC
388 if (c->type == set_cmd && deprecated_set_hook)
389 deprecated_set_hook (c);
d318976c
FN
390}
391
392/* Show all the settings in a list of show commands. */
393
394void
395cmd_show_list (struct cmd_list_element *list, int from_tty, char *prefix)
396{
3b31d625
EZ
397 struct cleanup *showlist_chain;
398
399 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
d318976c
FN
400 for (; list != NULL; list = list->next)
401 {
402 /* If we find a prefix, run its list, prefixing our output by its
403 prefix (with "show " skipped). */
d318976c
FN
404 if (list->prefixlist && !list->abbrev_flag)
405 {
3b31d625
EZ
406 struct cleanup *optionlist_chain
407 = make_cleanup_ui_out_tuple_begin_end (uiout, "optionlist");
37fc812e
DJ
408 char *new_prefix = strstr (list->prefixname, "show ") + 5;
409 if (ui_out_is_mi_like_p (uiout))
410 ui_out_field_string (uiout, "prefix", new_prefix);
411 cmd_show_list (*list->prefixlist, from_tty, new_prefix);
3b31d625
EZ
412 /* Close the tuple. */
413 do_cleanups (optionlist_chain);
d318976c 414 }
427c3a89 415 else
d318976c 416 {
3b31d625
EZ
417 struct cleanup *option_chain
418 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
d318976c
FN
419 ui_out_text (uiout, prefix);
420 ui_out_field_string (uiout, "name", list->name);
421 ui_out_text (uiout, ": ");
427c3a89
DJ
422 if (list->type == show_cmd)
423 do_setshow_command ((char *) NULL, from_tty, list);
424 else
425 cmd_func (list, NULL, from_tty);
3b31d625
EZ
426 /* Close the tuple. */
427 do_cleanups (option_chain);
d318976c 428 }
d318976c 429 }
3b31d625
EZ
430 /* Close the tuple. */
431 do_cleanups (showlist_chain);
d318976c
FN
432}
433
This page took 0.420923 seconds and 4 git commands to generate.