]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/cli/cli-setshow.c
1 /* Handle set and show GDB commands.
3 Copyright (c) 2000, 2001, 2002, 2003, 2007 Free Software Foundation, Inc.
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 3 of the License, or
8 (at your option) any later version.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "readline/tilde.h"
22 #include "gdb_string.h"
26 #include "cli/cli-decode.h"
27 #include "cli/cli-cmds.h"
28 #include "cli/cli-setshow.h"
30 /* Prototypes for local functions */
32 static int parse_binary_operation (char *);
35 static enum auto_boolean
36 parse_auto_binary_operation (const char *arg
)
38 if (arg
!= NULL
&& *arg
!= '\0')
40 int length
= strlen (arg
);
41 while (isspace (arg
[length
- 1]) && length
> 0)
43 if (strncmp (arg
, "on", length
) == 0
44 || strncmp (arg
, "1", length
) == 0
45 || strncmp (arg
, "yes", length
) == 0
46 || strncmp (arg
, "enable", length
) == 0)
47 return AUTO_BOOLEAN_TRUE
;
48 else if (strncmp (arg
, "off", length
) == 0
49 || strncmp (arg
, "0", length
) == 0
50 || strncmp (arg
, "no", length
) == 0
51 || strncmp (arg
, "disable", length
) == 0)
52 return AUTO_BOOLEAN_FALSE
;
53 else if (strncmp (arg
, "auto", length
) == 0
54 || (strncmp (arg
, "-1", length
) == 0 && length
> 1))
55 return AUTO_BOOLEAN_AUTO
;
57 error (_("\"on\", \"off\" or \"auto\" expected."));
58 return AUTO_BOOLEAN_AUTO
; /* pacify GCC */
62 parse_binary_operation (char *arg
)
69 length
= strlen (arg
);
71 while (arg
[length
- 1] == ' ' || arg
[length
- 1] == '\t')
74 if (strncmp (arg
, "on", length
) == 0
75 || strncmp (arg
, "1", length
) == 0
76 || strncmp (arg
, "yes", length
) == 0
77 || strncmp (arg
, "enable", length
) == 0)
79 else if (strncmp (arg
, "off", length
) == 0
80 || strncmp (arg
, "0", length
) == 0
81 || strncmp (arg
, "no", length
) == 0
82 || strncmp (arg
, "disable", length
) == 0)
86 error (_("\"on\" or \"off\" expected."));
92 deprecated_show_value_hack (struct ui_file
*ignore_file
,
94 struct cmd_list_element
*c
,
97 /* If there's no command or value, don't try to print it out. */
98 if (c
== NULL
|| value
== NULL
)
100 /* Print doc minus "show" at start. */
101 print_doc_line (gdb_stdout
, c
->doc
+ 5);
105 case var_string_noescape
:
106 case var_optional_filename
:
109 printf_filtered ((" is \"%s\".\n"), value
);
112 printf_filtered ((" is %s.\n"), value
);
117 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
118 of the argument, and FROM_TTY is nonzero if this command is being entered
119 directly by the user (i.e. these are just like any other
120 command). C is the command list element for the command. */
123 do_setshow_command (char *arg
, int from_tty
, struct cmd_list_element
*c
)
125 if (c
->type
== set_cmd
)
138 new = (char *) xmalloc (strlen (arg
) + 2);
141 while ((ch
= *p
++) != '\000')
145 /* \ at end of argument is used after spaces
146 so they won't be lost. */
147 /* This is obsolete now that we no longer strip
148 trailing whitespace and actually, the backslash
149 didn't get here in my test, readline or
150 something did something funky with a backslash
151 right before a newline. */
154 ch
= parse_escape (&p
);
164 if (*(p
- 1) != '\\')
168 new = (char *) xrealloc (new, q
- new);
169 if (*(char **) c
->var
!= NULL
)
170 xfree (*(char **) c
->var
);
171 *(char **) c
->var
= new;
174 case var_string_noescape
:
177 if (*(char **) c
->var
!= NULL
)
178 xfree (*(char **) c
->var
);
179 *(char **) c
->var
= savestring (arg
, strlen (arg
));
181 case var_optional_filename
:
184 if (*(char **) c
->var
!= NULL
)
185 xfree (*(char **) c
->var
);
186 *(char **) c
->var
= savestring (arg
, strlen (arg
));
190 error_no_arg (_("filename to set it to."));
191 if (*(char **) c
->var
!= NULL
)
192 xfree (*(char **) c
->var
);
194 /* Clear trailing whitespace of filename. */
195 char *ptr
= arg
+ strlen (arg
) - 1;
196 while (ptr
>= arg
&& (*ptr
== ' ' || *ptr
== '\t'))
200 *(char **) c
->var
= tilde_expand (arg
);
203 *(int *) c
->var
= parse_binary_operation (arg
);
205 case var_auto_boolean
:
206 *(enum auto_boolean
*) c
->var
= parse_auto_binary_operation (arg
);
210 error_no_arg (_("integer to set it to."));
211 *(unsigned int *) c
->var
= parse_and_eval_long (arg
);
212 if (*(unsigned int *) c
->var
== 0)
213 *(unsigned int *) c
->var
= UINT_MAX
;
219 error_no_arg (_("integer to set it to."));
220 val
= parse_and_eval_long (arg
);
222 *(int *) c
->var
= INT_MAX
;
223 else if (val
>= INT_MAX
)
224 error (_("integer %u out of range"), val
);
226 *(int *) c
->var
= val
;
231 error_no_arg (_("integer to set it to."));
232 *(int *) c
->var
= parse_and_eval_long (arg
);
239 const char *match
= NULL
;
242 /* if no argument was supplied, print an informative error message */
246 strcpy (msg
, "Requires an argument. Valid arguments are ");
247 for (i
= 0; c
->enums
[i
]; i
++)
251 strcat (msg
, c
->enums
[i
]);
257 p
= strchr (arg
, ' ');
265 for (i
= 0; c
->enums
[i
]; i
++)
266 if (strncmp (arg
, c
->enums
[i
], len
) == 0)
268 if (c
->enums
[i
][len
] == '\0')
272 break; /* exact match. */
282 error (_("Undefined item: \"%s\"."), arg
);
285 error (_("Ambiguous item \"%s\"."), arg
);
287 *(const char **) c
->var
= match
;
291 error (_("gdb internal error: bad var_type in do_setshow_command"));
294 else if (c
->type
== show_cmd
)
296 struct cleanup
*old_chain
;
297 struct ui_stream
*stb
;
299 stb
= ui_out_stream_new (uiout
);
300 old_chain
= make_cleanup_ui_out_stream_delete (stb
);
302 /* Possibly call the pre hook. */
303 if (c
->pre_show_hook
)
304 (c
->pre_show_hook
) (c
);
309 if (*(char **) c
->var
)
310 fputstr_filtered (*(char **) c
->var
, '"', stb
->stream
);
312 case var_string_noescape
:
313 case var_optional_filename
:
316 if (*(char **) c
->var
)
317 fputs_filtered (*(char **) c
->var
, stb
->stream
);
320 fputs_filtered (*(int *) c
->var
? "on" : "off", stb
->stream
);
322 case var_auto_boolean
:
323 switch (*(enum auto_boolean
*) c
->var
)
325 case AUTO_BOOLEAN_TRUE
:
326 fputs_filtered ("on", stb
->stream
);
328 case AUTO_BOOLEAN_FALSE
:
329 fputs_filtered ("off", stb
->stream
);
331 case AUTO_BOOLEAN_AUTO
:
332 fputs_filtered ("auto", stb
->stream
);
335 internal_error (__FILE__
, __LINE__
,
336 _("do_setshow_command: invalid var_auto_boolean"));
341 if (*(unsigned int *) c
->var
== UINT_MAX
)
343 fputs_filtered ("unlimited", stb
->stream
);
346 /* else fall through */
348 fprintf_filtered (stb
->stream
, "%u", *(unsigned int *) c
->var
);
351 if (*(int *) c
->var
== INT_MAX
)
353 fputs_filtered ("unlimited", stb
->stream
);
356 fprintf_filtered (stb
->stream
, "%d", *(int *) c
->var
);
360 error (_("gdb internal error: bad var_type in do_setshow_command"));
364 /* FIXME: cagney/2005-02-10: Need to split this in half: code to
365 convert the value into a string (esentially the above); and
366 code to print the value out. For the latter there should be
367 MI and CLI specific versions. */
369 if (ui_out_is_mi_like_p (uiout
))
370 ui_out_field_stream (uiout
, "value", stb
);
374 char *value
= ui_file_xstrdup (stb
->stream
, &length
);
375 make_cleanup (xfree
, value
);
376 if (c
->show_value_func
!= NULL
)
377 c
->show_value_func (gdb_stdout
, from_tty
, c
, value
);
379 deprecated_show_value_hack (gdb_stdout
, from_tty
, c
, value
);
381 do_cleanups (old_chain
);
384 error (_("gdb internal error: bad cmd_type in do_setshow_command"));
385 c
->func (c
, NULL
, from_tty
);
386 if (c
->type
== set_cmd
&& deprecated_set_hook
)
387 deprecated_set_hook (c
);
390 /* Show all the settings in a list of show commands. */
393 cmd_show_list (struct cmd_list_element
*list
, int from_tty
, char *prefix
)
395 struct cleanup
*showlist_chain
;
397 showlist_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "showlist");
398 for (; list
!= NULL
; list
= list
->next
)
400 /* If we find a prefix, run its list, prefixing our output by its
401 prefix (with "show " skipped). */
402 if (list
->prefixlist
&& !list
->abbrev_flag
)
404 struct cleanup
*optionlist_chain
405 = make_cleanup_ui_out_tuple_begin_end (uiout
, "optionlist");
406 char *new_prefix
= strstr (list
->prefixname
, "show ") + 5;
407 if (ui_out_is_mi_like_p (uiout
))
408 ui_out_field_string (uiout
, "prefix", new_prefix
);
409 cmd_show_list (*list
->prefixlist
, from_tty
, new_prefix
);
410 /* Close the tuple. */
411 do_cleanups (optionlist_chain
);
415 struct cleanup
*option_chain
416 = make_cleanup_ui_out_tuple_begin_end (uiout
, "option");
417 ui_out_text (uiout
, prefix
);
418 ui_out_field_string (uiout
, "name", list
->name
);
419 ui_out_text (uiout
, ": ");
420 if (list
->type
== show_cmd
)
421 do_setshow_command ((char *) NULL
, from_tty
, list
);
423 cmd_func (list
, NULL
, from_tty
);
424 /* Close the tuple. */
425 do_cleanups (option_chain
);
428 /* Close the tuple. */
429 do_cleanups (showlist_chain
);