1 /* Handle set and show GDB commands.
3 Copyright (C) 2000-2013 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"
23 #include "arch-utils.h"
28 #include "cli/cli-decode.h"
29 #include "cli/cli-cmds.h"
30 #include "cli/cli-setshow.h"
32 /* Return true if the change of command parameter should be notified. */
35 notify_command_param_changed_p (int param_changed
, struct cmd_list_element
*c
)
37 if (param_changed
== 0)
40 if (c
->class == class_maintenance
|| c
->class == class_deprecated
41 || c
->class == class_obscure
)
48 static enum auto_boolean
49 parse_auto_binary_operation (const char *arg
)
51 if (arg
!= NULL
&& *arg
!= '\0')
53 int length
= strlen (arg
);
55 while (isspace (arg
[length
- 1]) && length
> 0)
57 if (strncmp (arg
, "on", length
) == 0
58 || strncmp (arg
, "1", length
) == 0
59 || strncmp (arg
, "yes", length
) == 0
60 || strncmp (arg
, "enable", length
) == 0)
61 return AUTO_BOOLEAN_TRUE
;
62 else if (strncmp (arg
, "off", length
) == 0
63 || strncmp (arg
, "0", length
) == 0
64 || strncmp (arg
, "no", length
) == 0
65 || strncmp (arg
, "disable", length
) == 0)
66 return AUTO_BOOLEAN_FALSE
;
67 else if (strncmp (arg
, "auto", length
) == 0
68 || (strncmp (arg
, "-1", length
) == 0 && length
> 1))
69 return AUTO_BOOLEAN_AUTO
;
71 error (_("\"on\", \"off\" or \"auto\" expected."));
72 return AUTO_BOOLEAN_AUTO
; /* Pacify GCC. */
75 /* See cli-setshow.h. */
78 parse_cli_boolean_value (char *arg
)
85 length
= strlen (arg
);
87 while (arg
[length
- 1] == ' ' || arg
[length
- 1] == '\t')
90 if (strncmp (arg
, "on", length
) == 0
91 || strncmp (arg
, "1", length
) == 0
92 || strncmp (arg
, "yes", length
) == 0
93 || strncmp (arg
, "enable", length
) == 0)
95 else if (strncmp (arg
, "off", length
) == 0
96 || strncmp (arg
, "0", length
) == 0
97 || strncmp (arg
, "no", length
) == 0
98 || strncmp (arg
, "disable", length
) == 0)
105 deprecated_show_value_hack (struct ui_file
*ignore_file
,
107 struct cmd_list_element
*c
,
110 /* If there's no command or value, don't try to print it out. */
111 if (c
== NULL
|| value
== NULL
)
113 /* Print doc minus "show" at start. */
114 print_doc_line (gdb_stdout
, c
->doc
+ 5);
118 case var_string_noescape
:
119 case var_optional_filename
:
122 printf_filtered ((" is \"%s\".\n"), value
);
125 printf_filtered ((" is %s.\n"), value
);
130 /* Do a "set" command. ARG is NULL if no argument, or the
131 text of the argument, and FROM_TTY is nonzero if this command is
132 being entered directly by the user (i.e. these are just like any
133 other command). C is the command list element for the command. */
136 do_set_command (char *arg
, int from_tty
, struct cmd_list_element
*c
)
138 /* A flag to indicate the option is changed or not. */
139 int option_changed
= 0;
141 gdb_assert (c
->type
== set_cmd
);
154 new = (char *) xmalloc (strlen (arg
) + 2);
157 while ((ch
= *p
++) != '\000')
161 /* \ at end of argument is used after spaces
162 so they won't be lost. */
163 /* This is obsolete now that we no longer strip
164 trailing whitespace and actually, the backslash
165 didn't get here in my test, readline or
166 something did something funky with a backslash
167 right before a newline. */
170 ch
= parse_escape (get_current_arch (), &p
);
180 if (*(p
- 1) != '\\')
184 new = (char *) xrealloc (new, q
- new);
186 if (*(char **) c
->var
== NULL
187 || strcmp (*(char **) c
->var
, new) != 0)
189 xfree (*(char **) c
->var
);
190 *(char **) c
->var
= new;
198 case var_string_noescape
:
202 if (*(char **) c
->var
== NULL
|| strcmp (*(char **) c
->var
, arg
) != 0)
204 xfree (*(char **) c
->var
);
205 *(char **) c
->var
= xstrdup (arg
);
212 error_no_arg (_("filename to set it to."));
214 case var_optional_filename
:
220 /* Clear trailing whitespace of filename. */
221 char *ptr
= arg
+ strlen (arg
) - 1;
223 while (ptr
>= arg
&& (*ptr
== ' ' || *ptr
== '\t'))
227 val
= tilde_expand (arg
);
232 if (*(char **) c
->var
== NULL
233 || strcmp (*(char **) c
->var
, val
) != 0)
235 xfree (*(char **) c
->var
);
236 *(char **) c
->var
= val
;
246 int val
= parse_cli_boolean_value (arg
);
249 error (_("\"on\" or \"off\" expected."));
250 if (val
!= *(int *) c
->var
)
252 *(int *) c
->var
= val
;
258 case var_auto_boolean
:
260 enum auto_boolean val
= parse_auto_binary_operation (arg
);
262 if (*(enum auto_boolean
*) c
->var
!= val
)
264 *(enum auto_boolean
*) c
->var
= val
;
276 error_no_arg (_("integer to set it to."));
277 val
= parse_and_eval_long (arg
);
279 if (c
->var_type
== var_uinteger
&& val
== 0)
281 else if (val
> UINT_MAX
)
282 error (_("integer %s out of range"), plongest (val
));
284 if (*(unsigned int *) c
->var
!= val
)
286 *(unsigned int *) c
->var
= val
;
298 error_no_arg (_("integer to set it to."));
299 val
= parse_and_eval_long (arg
);
301 if (val
== 0 && c
->var_type
== var_integer
)
303 else if (val
> INT_MAX
|| val
< INT_MIN
)
304 error (_("integer %s out of range"), plongest (val
));
306 if (*(int *) c
->var
!= val
)
308 *(int *) c
->var
= val
;
319 const char *match
= NULL
;
322 /* If no argument was supplied, print an informative error
329 for (i
= 0; c
->enums
[i
]; i
++)
330 msg_len
+= strlen (c
->enums
[i
]) + 2;
332 msg
= xmalloc (msg_len
);
334 make_cleanup (xfree
, msg
);
336 for (i
= 0; c
->enums
[i
]; i
++)
340 strcat (msg
, c
->enums
[i
]);
342 error (_("Requires an argument. Valid arguments are %s."),
346 p
= strchr (arg
, ' ');
354 for (i
= 0; c
->enums
[i
]; i
++)
355 if (strncmp (arg
, c
->enums
[i
], len
) == 0)
357 if (c
->enums
[i
][len
] == '\0')
361 break; /* Exact match. */
371 error (_("Undefined item: \"%s\"."), arg
);
374 error (_("Ambiguous item \"%s\"."), arg
);
376 if (*(const char **) c
->var
!= match
)
378 *(const char **) c
->var
= match
;
384 case var_zuinteger_unlimited
:
389 error_no_arg (_("integer to set it to."));
390 val
= parse_and_eval_long (arg
);
393 error (_("integer %s out of range"), plongest (val
));
395 error (_("only -1 is allowed to set as unlimited"));
397 if (*(int *) c
->var
!= val
)
399 *(int *) c
->var
= val
;
405 error (_("gdb internal error: bad var_type in do_setshow_command"));
407 c
->func (c
, NULL
, from_tty
);
408 if (deprecated_set_hook
)
409 deprecated_set_hook (c
);
411 if (notify_command_param_changed_p (option_changed
, c
))
414 struct cmd_list_element
**cmds
;
415 struct cmd_list_element
*p
;
419 /* Compute the whole multi-word command options. If user types command
420 'set foo bar baz on', c->name is 'baz', and GDB can't pass "bar" to
421 command option change notification, because it is confusing. We can
422 trace back through field 'prefix' to compute the whole options,
423 and pass "foo bar baz" to notification. */
425 for (i
= 0, p
= c
; p
!= NULL
; i
++)
427 length
+= strlen (p
->name
);
432 cp
= name
= xmalloc (length
);
433 cmds
= xmalloc (sizeof (struct cmd_list_element
*) * i
);
435 /* Track back through filed 'prefix' and cache them in CMDS. */
436 for (i
= 0, p
= c
; p
!= NULL
; i
++)
442 /* Don't trigger any observer notification if prefixlist is not
445 if (cmds
[i
]->prefixlist
!= &setlist
)
452 /* Traverse them in the reversed order, and copy their names into
454 for (i
--; i
>= 0; i
--)
456 memcpy (cp
, cmds
[i
]->name
, strlen (cmds
[i
]->name
));
457 cp
+= strlen (cmds
[i
]->name
);
472 case var_string_noescape
:
474 case var_optional_filename
:
476 observer_notify_command_param_changed (name
, *(char **) c
->var
);
480 char *opt
= *(int *) c
->var
? "on" : "off";
482 observer_notify_command_param_changed (name
, opt
);
485 case var_auto_boolean
:
487 const char *s
= auto_boolean_enums
[*(enum auto_boolean
*) c
->var
];
489 observer_notify_command_param_changed (name
, s
);
497 xsnprintf (s
, sizeof s
, "%u", *(unsigned int *) c
->var
);
498 observer_notify_command_param_changed (name
, s
);
503 case var_zuinteger_unlimited
:
507 xsnprintf (s
, sizeof s
, "%d", *(int *) c
->var
);
508 observer_notify_command_param_changed (name
, s
);
516 /* Do a "show" command. ARG is NULL if no argument, or the
517 text of the argument, and FROM_TTY is nonzero if this command is
518 being entered directly by the user (i.e. these are just like any
519 other command). C is the command list element for the command. */
522 do_show_command (char *arg
, int from_tty
, struct cmd_list_element
*c
)
524 struct ui_out
*uiout
= current_uiout
;
525 struct cleanup
*old_chain
;
528 gdb_assert (c
->type
== show_cmd
);
530 stb
= mem_fileopen ();
531 old_chain
= make_cleanup_ui_file_delete (stb
);
533 /* Possibly call the pre hook. */
534 if (c
->pre_show_hook
)
535 (c
->pre_show_hook
) (c
);
540 if (*(char **) c
->var
)
541 fputstr_filtered (*(char **) c
->var
, '"', stb
);
543 case var_string_noescape
:
544 case var_optional_filename
:
547 if (*(char **) c
->var
)
548 fputs_filtered (*(char **) c
->var
, stb
);
551 fputs_filtered (*(int *) c
->var
? "on" : "off", stb
);
553 case var_auto_boolean
:
554 switch (*(enum auto_boolean
*) c
->var
)
556 case AUTO_BOOLEAN_TRUE
:
557 fputs_filtered ("on", stb
);
559 case AUTO_BOOLEAN_FALSE
:
560 fputs_filtered ("off", stb
);
562 case AUTO_BOOLEAN_AUTO
:
563 fputs_filtered ("auto", stb
);
566 internal_error (__FILE__
, __LINE__
,
567 _("do_show_command: "
568 "invalid var_auto_boolean"));
574 if (c
->var_type
== var_uinteger
575 && *(unsigned int *) c
->var
== UINT_MAX
)
576 fputs_filtered ("unlimited", stb
);
578 fprintf_filtered (stb
, "%u", *(unsigned int *) c
->var
);
582 if (c
->var_type
== var_integer
583 && *(int *) c
->var
== INT_MAX
)
584 fputs_filtered ("unlimited", stb
);
586 fprintf_filtered (stb
, "%d", *(int *) c
->var
);
588 case var_zuinteger_unlimited
:
590 if (*(int *) c
->var
== -1)
591 fputs_filtered ("unlimited", stb
);
593 fprintf_filtered (stb
, "%d", *(int *) c
->var
);
597 error (_("gdb internal error: bad var_type in do_show_command"));
601 /* FIXME: cagney/2005-02-10: Need to split this in half: code to
602 convert the value into a string (esentially the above); and
603 code to print the value out. For the latter there should be
604 MI and CLI specific versions. */
606 if (ui_out_is_mi_like_p (uiout
))
607 ui_out_field_stream (uiout
, "value", stb
);
610 char *value
= ui_file_xstrdup (stb
, NULL
);
612 make_cleanup (xfree
, value
);
613 if (c
->show_value_func
!= NULL
)
614 c
->show_value_func (gdb_stdout
, from_tty
, c
, value
);
616 deprecated_show_value_hack (gdb_stdout
, from_tty
, c
, value
);
618 do_cleanups (old_chain
);
620 c
->func (c
, NULL
, from_tty
);
623 /* Show all the settings in a list of show commands. */
626 cmd_show_list (struct cmd_list_element
*list
, int from_tty
, char *prefix
)
628 struct cleanup
*showlist_chain
;
629 struct ui_out
*uiout
= current_uiout
;
631 showlist_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "showlist");
632 for (; list
!= NULL
; list
= list
->next
)
634 /* If we find a prefix, run its list, prefixing our output by its
635 prefix (with "show " skipped). */
636 if (list
->prefixlist
&& !list
->abbrev_flag
)
638 struct cleanup
*optionlist_chain
639 = make_cleanup_ui_out_tuple_begin_end (uiout
, "optionlist");
640 char *new_prefix
= strstr (list
->prefixname
, "show ") + 5;
642 if (ui_out_is_mi_like_p (uiout
))
643 ui_out_field_string (uiout
, "prefix", new_prefix
);
644 cmd_show_list (*list
->prefixlist
, from_tty
, new_prefix
);
645 /* Close the tuple. */
646 do_cleanups (optionlist_chain
);
650 if (list
->class != no_set_class
)
652 struct cleanup
*option_chain
653 = make_cleanup_ui_out_tuple_begin_end (uiout
, "option");
655 ui_out_text (uiout
, prefix
);
656 ui_out_field_string (uiout
, "name", list
->name
);
657 ui_out_text (uiout
, ": ");
658 if (list
->type
== show_cmd
)
659 do_show_command ((char *) NULL
, from_tty
, list
);
661 cmd_func (list
, NULL
, from_tty
);
662 /* Close the tuple. */
663 do_cleanups (option_chain
);
667 /* Close the tuple. */
668 do_cleanups (showlist_chain
);
This page took 0.045138 seconds and 4 git commands to generate.