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"
31 #include "cli/cli-utils.h"
33 /* Return true if the change of command parameter should be notified. */
36 notify_command_param_changed_p (int param_changed
, struct cmd_list_element
*c
)
38 if (param_changed
== 0)
41 if (c
->class == class_maintenance
|| c
->class == class_deprecated
42 || c
->class == class_obscure
)
49 static enum auto_boolean
50 parse_auto_binary_operation (const char *arg
)
52 if (arg
!= NULL
&& *arg
!= '\0')
54 int length
= strlen (arg
);
56 while (isspace (arg
[length
- 1]) && length
> 0)
58 if (strncmp (arg
, "on", length
) == 0
59 || strncmp (arg
, "1", length
) == 0
60 || strncmp (arg
, "yes", length
) == 0
61 || strncmp (arg
, "enable", length
) == 0)
62 return AUTO_BOOLEAN_TRUE
;
63 else if (strncmp (arg
, "off", length
) == 0
64 || strncmp (arg
, "0", length
) == 0
65 || strncmp (arg
, "no", length
) == 0
66 || strncmp (arg
, "disable", length
) == 0)
67 return AUTO_BOOLEAN_FALSE
;
68 else if (strncmp (arg
, "auto", length
) == 0
69 || (strncmp (arg
, "-1", length
) == 0 && length
> 1))
70 return AUTO_BOOLEAN_AUTO
;
72 error (_("\"on\", \"off\" or \"auto\" expected."));
73 return AUTO_BOOLEAN_AUTO
; /* Pacify GCC. */
76 /* See cli-setshow.h. */
79 parse_cli_boolean_value (char *arg
)
86 length
= strlen (arg
);
88 while (arg
[length
- 1] == ' ' || arg
[length
- 1] == '\t')
91 if (strncmp (arg
, "on", length
) == 0
92 || strncmp (arg
, "1", length
) == 0
93 || strncmp (arg
, "yes", length
) == 0
94 || strncmp (arg
, "enable", length
) == 0)
96 else if (strncmp (arg
, "off", length
) == 0
97 || strncmp (arg
, "0", length
) == 0
98 || strncmp (arg
, "no", length
) == 0
99 || strncmp (arg
, "disable", length
) == 0)
106 deprecated_show_value_hack (struct ui_file
*ignore_file
,
108 struct cmd_list_element
*c
,
111 /* If there's no command or value, don't try to print it out. */
112 if (c
== NULL
|| value
== NULL
)
114 /* Print doc minus "show" at start. */
115 print_doc_line (gdb_stdout
, c
->doc
+ 5);
119 case var_string_noescape
:
120 case var_optional_filename
:
123 printf_filtered ((" is \"%s\".\n"), value
);
126 printf_filtered ((" is %s.\n"), value
);
131 /* Returns true if ARG is "unlimited". */
134 is_unlimited_literal (const char *arg
)
136 size_t len
= sizeof ("unlimited") - 1;
138 arg
= skip_spaces_const (arg
);
140 return (strncmp (arg
, "unlimited", len
) == 0
141 && (isspace (arg
[len
]) || arg
[len
] == '\0'));
145 /* Do a "set" command. ARG is NULL if no argument, or the
146 text of the argument, and FROM_TTY is nonzero if this command is
147 being entered directly by the user (i.e. these are just like any
148 other command). C is the command list element for the command. */
151 do_set_command (char *arg
, int from_tty
, struct cmd_list_element
*c
)
153 /* A flag to indicate the option is changed or not. */
154 int option_changed
= 0;
156 gdb_assert (c
->type
== set_cmd
);
169 new = (char *) xmalloc (strlen (arg
) + 2);
172 while ((ch
= *p
++) != '\000')
176 /* \ at end of argument is used after spaces
177 so they won't be lost. */
178 /* This is obsolete now that we no longer strip
179 trailing whitespace and actually, the backslash
180 didn't get here in my test, readline or
181 something did something funky with a backslash
182 right before a newline. */
185 ch
= parse_escape (get_current_arch (), &p
);
195 if (*(p
- 1) != '\\')
199 new = (char *) xrealloc (new, q
- new);
201 if (*(char **) c
->var
== NULL
202 || strcmp (*(char **) c
->var
, new) != 0)
204 xfree (*(char **) c
->var
);
205 *(char **) c
->var
= new;
213 case var_string_noescape
:
217 if (*(char **) c
->var
== NULL
|| strcmp (*(char **) c
->var
, arg
) != 0)
219 xfree (*(char **) c
->var
);
220 *(char **) c
->var
= xstrdup (arg
);
227 error_no_arg (_("filename to set it to."));
229 case var_optional_filename
:
235 /* Clear trailing whitespace of filename. */
236 char *ptr
= arg
+ strlen (arg
) - 1;
238 while (ptr
>= arg
&& (*ptr
== ' ' || *ptr
== '\t'))
242 val
= tilde_expand (arg
);
247 if (*(char **) c
->var
== NULL
248 || strcmp (*(char **) c
->var
, val
) != 0)
250 xfree (*(char **) c
->var
);
251 *(char **) c
->var
= val
;
261 int val
= parse_cli_boolean_value (arg
);
264 error (_("\"on\" or \"off\" expected."));
265 if (val
!= *(int *) c
->var
)
267 *(int *) c
->var
= val
;
273 case var_auto_boolean
:
275 enum auto_boolean val
= parse_auto_binary_operation (arg
);
277 if (*(enum auto_boolean
*) c
->var
!= val
)
279 *(enum auto_boolean
*) c
->var
= val
;
292 if (c
->var_type
== var_uinteger
)
293 error_no_arg (_("integer to set it to, or \"unlimited\"."));
295 error_no_arg (_("integer to set it to."));
298 if (c
->var_type
== var_uinteger
&& is_unlimited_literal (arg
))
301 val
= parse_and_eval_long (arg
);
303 if (c
->var_type
== var_uinteger
&& val
== 0)
306 /* For var_uinteger, don't let the user set the value
307 to UINT_MAX directly, as that exposes an
308 implementation detail to the user interface. */
309 || (c
->var_type
== var_uinteger
&& val
>= UINT_MAX
)
310 || (c
->var_type
== var_zuinteger
&& val
> UINT_MAX
))
311 error (_("integer %s out of range"), plongest (val
));
313 if (*(unsigned int *) c
->var
!= val
)
315 *(unsigned int *) c
->var
= val
;
328 if (c
->var_type
== var_integer
)
329 error_no_arg (_("integer to set it to, or \"unlimited\"."));
331 error_no_arg (_("integer to set it to."));
334 if (c
->var_type
== var_integer
&& is_unlimited_literal (arg
))
337 val
= parse_and_eval_long (arg
);
339 if (val
== 0 && c
->var_type
== var_integer
)
341 else if (val
< INT_MIN
342 /* For var_integer, don't let the user set the value
343 to INT_MAX directly, as that exposes an
344 implementation detail to the user interface. */
345 || (c
->var_type
== var_integer
&& val
>= INT_MAX
)
346 || (c
->var_type
== var_zinteger
&& val
> INT_MAX
))
347 error (_("integer %s out of range"), plongest (val
));
349 if (*(int *) c
->var
!= val
)
351 *(int *) c
->var
= val
;
362 const char *match
= NULL
;
365 /* If no argument was supplied, print an informative error
372 for (i
= 0; c
->enums
[i
]; i
++)
373 msg_len
+= strlen (c
->enums
[i
]) + 2;
375 msg
= xmalloc (msg_len
);
377 make_cleanup (xfree
, msg
);
379 for (i
= 0; c
->enums
[i
]; i
++)
383 strcat (msg
, c
->enums
[i
]);
385 error (_("Requires an argument. Valid arguments are %s."),
389 p
= strchr (arg
, ' ');
397 for (i
= 0; c
->enums
[i
]; i
++)
398 if (strncmp (arg
, c
->enums
[i
], len
) == 0)
400 if (c
->enums
[i
][len
] == '\0')
404 break; /* Exact match. */
414 error (_("Undefined item: \"%s\"."), arg
);
417 error (_("Ambiguous item \"%s\"."), arg
);
419 if (*(const char **) c
->var
!= match
)
421 *(const char **) c
->var
= match
;
427 case var_zuinteger_unlimited
:
432 error_no_arg (_("integer to set it to, or \"unlimited\"."));
434 if (is_unlimited_literal (arg
))
437 val
= parse_and_eval_long (arg
);
440 error (_("integer %s out of range"), plongest (val
));
442 error (_("only -1 is allowed to set as unlimited"));
444 if (*(int *) c
->var
!= val
)
446 *(int *) c
->var
= val
;
452 error (_("gdb internal error: bad var_type in do_setshow_command"));
454 c
->func (c
, NULL
, from_tty
);
455 if (deprecated_set_hook
)
456 deprecated_set_hook (c
);
458 if (notify_command_param_changed_p (option_changed
, c
))
461 struct cmd_list_element
**cmds
;
462 struct cmd_list_element
*p
;
466 /* Compute the whole multi-word command options. If user types command
467 'set foo bar baz on', c->name is 'baz', and GDB can't pass "bar" to
468 command option change notification, because it is confusing. We can
469 trace back through field 'prefix' to compute the whole options,
470 and pass "foo bar baz" to notification. */
472 for (i
= 0, p
= c
; p
!= NULL
; i
++)
474 length
+= strlen (p
->name
);
479 cp
= name
= xmalloc (length
);
480 cmds
= xmalloc (sizeof (struct cmd_list_element
*) * i
);
482 /* Track back through filed 'prefix' and cache them in CMDS. */
483 for (i
= 0, p
= c
; p
!= NULL
; i
++)
489 /* Don't trigger any observer notification if prefixlist is not
492 if (cmds
[i
]->prefixlist
!= &setlist
)
499 /* Traverse them in the reversed order, and copy their names into
501 for (i
--; i
>= 0; i
--)
503 memcpy (cp
, cmds
[i
]->name
, strlen (cmds
[i
]->name
));
504 cp
+= strlen (cmds
[i
]->name
);
519 case var_string_noescape
:
521 case var_optional_filename
:
523 observer_notify_command_param_changed (name
, *(char **) c
->var
);
527 char *opt
= *(int *) c
->var
? "on" : "off";
529 observer_notify_command_param_changed (name
, opt
);
532 case var_auto_boolean
:
534 const char *s
= auto_boolean_enums
[*(enum auto_boolean
*) c
->var
];
536 observer_notify_command_param_changed (name
, s
);
544 xsnprintf (s
, sizeof s
, "%u", *(unsigned int *) c
->var
);
545 observer_notify_command_param_changed (name
, s
);
550 case var_zuinteger_unlimited
:
554 xsnprintf (s
, sizeof s
, "%d", *(int *) c
->var
);
555 observer_notify_command_param_changed (name
, s
);
563 /* Do a "show" command. ARG is NULL if no argument, or the
564 text of the argument, and FROM_TTY is nonzero if this command is
565 being entered directly by the user (i.e. these are just like any
566 other command). C is the command list element for the command. */
569 do_show_command (char *arg
, int from_tty
, struct cmd_list_element
*c
)
571 struct ui_out
*uiout
= current_uiout
;
572 struct cleanup
*old_chain
;
575 gdb_assert (c
->type
== show_cmd
);
577 stb
= mem_fileopen ();
578 old_chain
= make_cleanup_ui_file_delete (stb
);
580 /* Possibly call the pre hook. */
581 if (c
->pre_show_hook
)
582 (c
->pre_show_hook
) (c
);
587 if (*(char **) c
->var
)
588 fputstr_filtered (*(char **) c
->var
, '"', stb
);
590 case var_string_noescape
:
591 case var_optional_filename
:
594 if (*(char **) c
->var
)
595 fputs_filtered (*(char **) c
->var
, stb
);
598 fputs_filtered (*(int *) c
->var
? "on" : "off", stb
);
600 case var_auto_boolean
:
601 switch (*(enum auto_boolean
*) c
->var
)
603 case AUTO_BOOLEAN_TRUE
:
604 fputs_filtered ("on", stb
);
606 case AUTO_BOOLEAN_FALSE
:
607 fputs_filtered ("off", stb
);
609 case AUTO_BOOLEAN_AUTO
:
610 fputs_filtered ("auto", stb
);
613 internal_error (__FILE__
, __LINE__
,
614 _("do_show_command: "
615 "invalid var_auto_boolean"));
621 if (c
->var_type
== var_uinteger
622 && *(unsigned int *) c
->var
== UINT_MAX
)
623 fputs_filtered ("unlimited", stb
);
625 fprintf_filtered (stb
, "%u", *(unsigned int *) c
->var
);
629 if (c
->var_type
== var_integer
630 && *(int *) c
->var
== INT_MAX
)
631 fputs_filtered ("unlimited", stb
);
633 fprintf_filtered (stb
, "%d", *(int *) c
->var
);
635 case var_zuinteger_unlimited
:
637 if (*(int *) c
->var
== -1)
638 fputs_filtered ("unlimited", stb
);
640 fprintf_filtered (stb
, "%d", *(int *) c
->var
);
644 error (_("gdb internal error: bad var_type in do_show_command"));
648 /* FIXME: cagney/2005-02-10: Need to split this in half: code to
649 convert the value into a string (esentially the above); and
650 code to print the value out. For the latter there should be
651 MI and CLI specific versions. */
653 if (ui_out_is_mi_like_p (uiout
))
654 ui_out_field_stream (uiout
, "value", stb
);
657 char *value
= ui_file_xstrdup (stb
, NULL
);
659 make_cleanup (xfree
, value
);
660 if (c
->show_value_func
!= NULL
)
661 c
->show_value_func (gdb_stdout
, from_tty
, c
, value
);
663 deprecated_show_value_hack (gdb_stdout
, from_tty
, c
, value
);
665 do_cleanups (old_chain
);
667 c
->func (c
, NULL
, from_tty
);
670 /* Show all the settings in a list of show commands. */
673 cmd_show_list (struct cmd_list_element
*list
, int from_tty
, char *prefix
)
675 struct cleanup
*showlist_chain
;
676 struct ui_out
*uiout
= current_uiout
;
678 showlist_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "showlist");
679 for (; list
!= NULL
; list
= list
->next
)
681 /* If we find a prefix, run its list, prefixing our output by its
682 prefix (with "show " skipped). */
683 if (list
->prefixlist
&& !list
->abbrev_flag
)
685 struct cleanup
*optionlist_chain
686 = make_cleanup_ui_out_tuple_begin_end (uiout
, "optionlist");
687 char *new_prefix
= strstr (list
->prefixname
, "show ") + 5;
689 if (ui_out_is_mi_like_p (uiout
))
690 ui_out_field_string (uiout
, "prefix", new_prefix
);
691 cmd_show_list (*list
->prefixlist
, from_tty
, new_prefix
);
692 /* Close the tuple. */
693 do_cleanups (optionlist_chain
);
697 if (list
->class != no_set_class
)
699 struct cleanup
*option_chain
700 = make_cleanup_ui_out_tuple_begin_end (uiout
, "option");
702 ui_out_text (uiout
, prefix
);
703 ui_out_field_string (uiout
, "name", list
->name
);
704 ui_out_text (uiout
, ": ");
705 if (list
->type
== show_cmd
)
706 do_show_command ((char *) NULL
, from_tty
, list
);
708 cmd_func (list
, NULL
, from_tty
);
709 /* Close the tuple. */
710 do_cleanups (option_chain
);
714 /* Close the tuple. */
715 do_cleanups (showlist_chain
);
This page took 0.051447 seconds and 4 git commands to generate.