1 /* Handle set and show GDB commands.
3 Copyright (C) 2000-2019 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 "arch-utils.h"
23 #include "observable.h"
27 #include "cli/cli-decode.h"
28 #include "cli/cli-cmds.h"
29 #include "cli/cli-setshow.h"
30 #include "cli/cli-utils.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
->theclass
== class_maintenance
|| c
->theclass
== class_deprecated
41 || c
->theclass
== 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 (const 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 /* Returns true if ARG is "unlimited". */
133 is_unlimited_literal (const char *arg
)
135 arg
= skip_spaces (arg
);
137 const char *p
= skip_to_space (arg
);
139 size_t len
= p
- arg
;
141 if (len
> 0 && strncmp ("unlimited", arg
, len
) == 0)
148 /* Do a "set" command. ARG is NULL if no argument, or the
149 text of the argument, and FROM_TTY is nonzero if this command is
150 being entered directly by the user (i.e. these are just like any
151 other command). C is the command list element for the command. */
154 do_set_command (const char *arg
, int from_tty
, struct cmd_list_element
*c
)
156 /* A flag to indicate the option is changed or not. */
157 int option_changed
= 0;
159 gdb_assert (c
->type
== set_cmd
);
172 newobj
= (char *) xmalloc (strlen (arg
) + 2);
175 while ((ch
= *p
++) != '\000')
179 /* \ at end of argument is used after spaces
180 so they won't be lost. */
181 /* This is obsolete now that we no longer strip
182 trailing whitespace and actually, the backslash
183 didn't get here in my test, readline or
184 something did something funky with a backslash
185 right before a newline. */
188 ch
= parse_escape (get_current_arch (), &p
);
198 if (*(p
- 1) != '\\')
202 newobj
= (char *) xrealloc (newobj
, q
- newobj
);
204 if (*(char **) c
->var
== NULL
205 || strcmp (*(char **) c
->var
, newobj
) != 0)
207 xfree (*(char **) c
->var
);
208 *(char **) c
->var
= newobj
;
216 case var_string_noescape
:
220 if (*(char **) c
->var
== NULL
|| strcmp (*(char **) c
->var
, arg
) != 0)
222 xfree (*(char **) c
->var
);
223 *(char **) c
->var
= xstrdup (arg
);
230 error_no_arg (_("filename to set it to."));
232 case var_optional_filename
:
238 /* Clear trailing whitespace of filename. */
239 const char *ptr
= arg
+ strlen (arg
) - 1;
242 while (ptr
>= arg
&& (*ptr
== ' ' || *ptr
== '\t'))
244 copy
= xstrndup (arg
, ptr
+ 1 - arg
);
246 val
= tilde_expand (copy
);
252 if (*(char **) c
->var
== NULL
253 || strcmp (*(char **) c
->var
, val
) != 0)
255 xfree (*(char **) c
->var
);
256 *(char **) c
->var
= val
;
266 int val
= parse_cli_boolean_value (arg
);
269 error (_("\"on\" or \"off\" expected."));
270 if (val
!= *(int *) c
->var
)
272 *(int *) c
->var
= val
;
278 case var_auto_boolean
:
280 enum auto_boolean val
= parse_auto_binary_operation (arg
);
282 if (*(enum auto_boolean
*) c
->var
!= val
)
284 *(enum auto_boolean
*) c
->var
= val
;
297 if (c
->var_type
== var_uinteger
)
298 error_no_arg (_("integer to set it to, or \"unlimited\"."));
300 error_no_arg (_("integer to set it to."));
303 if (c
->var_type
== var_uinteger
&& is_unlimited_literal (arg
))
306 val
= parse_and_eval_long (arg
);
308 if (c
->var_type
== var_uinteger
&& val
== 0)
311 /* For var_uinteger, don't let the user set the value
312 to UINT_MAX directly, as that exposes an
313 implementation detail to the user interface. */
314 || (c
->var_type
== var_uinteger
&& val
>= UINT_MAX
)
315 || (c
->var_type
== var_zuinteger
&& val
> UINT_MAX
))
316 error (_("integer %s out of range"), plongest (val
));
318 if (*(unsigned int *) c
->var
!= val
)
320 *(unsigned int *) c
->var
= val
;
333 if (c
->var_type
== var_integer
)
334 error_no_arg (_("integer to set it to, or \"unlimited\"."));
336 error_no_arg (_("integer to set it to."));
339 if (c
->var_type
== var_integer
&& is_unlimited_literal (arg
))
342 val
= parse_and_eval_long (arg
);
344 if (val
== 0 && c
->var_type
== var_integer
)
346 else if (val
< INT_MIN
347 /* For var_integer, don't let the user set the value
348 to INT_MAX directly, as that exposes an
349 implementation detail to the user interface. */
350 || (c
->var_type
== var_integer
&& val
>= INT_MAX
)
351 || (c
->var_type
== var_zinteger
&& val
> INT_MAX
))
352 error (_("integer %s out of range"), plongest (val
));
354 if (*(int *) c
->var
!= val
)
356 *(int *) c
->var
= val
;
367 const char *match
= NULL
;
370 /* If no argument was supplied, print an informative error
376 for (i
= 0; c
->enums
[i
]; i
++)
382 error (_("Requires an argument. Valid arguments are %s."),
386 p
= strchr (arg
, ' ');
394 for (i
= 0; c
->enums
[i
]; i
++)
395 if (strncmp (arg
, c
->enums
[i
], len
) == 0)
397 if (c
->enums
[i
][len
] == '\0')
401 break; /* Exact match. */
411 error (_("Undefined item: \"%s\"."), arg
);
414 error (_("Ambiguous item \"%s\"."), arg
);
416 if (*(const char **) c
->var
!= match
)
418 *(const char **) c
->var
= match
;
424 case var_zuinteger_unlimited
:
429 error_no_arg (_("integer to set it to, or \"unlimited\"."));
431 if (is_unlimited_literal (arg
))
434 val
= parse_and_eval_long (arg
);
437 error (_("integer %s out of range"), plongest (val
));
439 error (_("only -1 is allowed to set as unlimited"));
441 if (*(int *) c
->var
!= val
)
443 *(int *) c
->var
= val
;
449 error (_("gdb internal error: bad var_type in do_setshow_command"));
451 c
->func (c
, NULL
, from_tty
);
453 if (notify_command_param_changed_p (option_changed
, c
))
456 struct cmd_list_element
**cmds
;
457 struct cmd_list_element
*p
;
461 /* Compute the whole multi-word command options. If user types command
462 'set foo bar baz on', c->name is 'baz', and GDB can't pass "bar" to
463 command option change notification, because it is confusing. We can
464 trace back through field 'prefix' to compute the whole options,
465 and pass "foo bar baz" to notification. */
467 for (i
= 0, p
= c
; p
!= NULL
; i
++)
469 length
+= strlen (p
->name
);
474 cp
= name
= (char *) xmalloc (length
);
475 cmds
= XNEWVEC (struct cmd_list_element
*, i
);
477 /* Track back through filed 'prefix' and cache them in CMDS. */
478 for (i
= 0, p
= c
; p
!= NULL
; i
++)
484 /* Don't trigger any observer notification if prefixlist is not
487 if (cmds
[i
]->prefixlist
!= &setlist
)
494 /* Traverse them in the reversed order, and copy their names into
496 for (i
--; i
>= 0; i
--)
498 memcpy (cp
, cmds
[i
]->name
, strlen (cmds
[i
]->name
));
499 cp
+= strlen (cmds
[i
]->name
);
514 case var_string_noescape
:
516 case var_optional_filename
:
518 gdb::observers::command_param_changed
.notify (name
, *(char **) c
->var
);
522 const char *opt
= *(int *) c
->var
? "on" : "off";
524 gdb::observers::command_param_changed
.notify (name
, opt
);
527 case var_auto_boolean
:
529 const char *s
= auto_boolean_enums
[*(enum auto_boolean
*) c
->var
];
531 gdb::observers::command_param_changed
.notify (name
, s
);
539 xsnprintf (s
, sizeof s
, "%u", *(unsigned int *) c
->var
);
540 gdb::observers::command_param_changed
.notify (name
, s
);
545 case var_zuinteger_unlimited
:
549 xsnprintf (s
, sizeof s
, "%d", *(int *) c
->var
);
550 gdb::observers::command_param_changed
.notify (name
, s
);
558 /* Do a "show" command. ARG is NULL if no argument, or the
559 text of the argument, and FROM_TTY is nonzero if this command is
560 being entered directly by the user (i.e. these are just like any
561 other command). C is the command list element for the command. */
564 do_show_command (const char *arg
, int from_tty
, struct cmd_list_element
*c
)
566 struct ui_out
*uiout
= current_uiout
;
568 gdb_assert (c
->type
== show_cmd
);
572 /* Possibly call the pre hook. */
573 if (c
->pre_show_hook
)
574 (c
->pre_show_hook
) (c
);
579 if (*(char **) c
->var
)
580 stb
.putstr (*(char **) c
->var
, '"');
582 case var_string_noescape
:
583 case var_optional_filename
:
586 if (*(char **) c
->var
)
587 stb
.puts (*(char **) c
->var
);
590 stb
.puts (*(int *) c
->var
? "on" : "off");
592 case var_auto_boolean
:
593 switch (*(enum auto_boolean
*) c
->var
)
595 case AUTO_BOOLEAN_TRUE
:
598 case AUTO_BOOLEAN_FALSE
:
601 case AUTO_BOOLEAN_AUTO
:
605 internal_error (__FILE__
, __LINE__
,
606 _("do_show_command: "
607 "invalid var_auto_boolean"));
613 if (c
->var_type
== var_uinteger
614 && *(unsigned int *) c
->var
== UINT_MAX
)
615 stb
.puts ("unlimited");
617 stb
.printf ("%u", *(unsigned int *) c
->var
);
621 if (c
->var_type
== var_integer
622 && *(int *) c
->var
== INT_MAX
)
623 stb
.puts ("unlimited");
625 stb
.printf ("%d", *(int *) c
->var
);
627 case var_zuinteger_unlimited
:
629 if (*(int *) c
->var
== -1)
630 stb
.puts ("unlimited");
632 stb
.printf ("%d", *(int *) c
->var
);
636 error (_("gdb internal error: bad var_type in do_show_command"));
640 /* FIXME: cagney/2005-02-10: Need to split this in half: code to
641 convert the value into a string (esentially the above); and
642 code to print the value out. For the latter there should be
643 MI and CLI specific versions. */
645 if (uiout
->is_mi_like_p ())
646 uiout
->field_stream ("value", stb
);
649 if (c
->show_value_func
!= NULL
)
650 c
->show_value_func (gdb_stdout
, from_tty
, c
, stb
.c_str ());
652 deprecated_show_value_hack (gdb_stdout
, from_tty
, c
, stb
.c_str ());
655 c
->func (c
, NULL
, from_tty
);
658 /* Show all the settings in a list of show commands. */
661 cmd_show_list (struct cmd_list_element
*list
, int from_tty
, const char *prefix
)
663 struct ui_out
*uiout
= current_uiout
;
665 ui_out_emit_tuple
tuple_emitter (uiout
, "showlist");
666 for (; list
!= NULL
; list
= list
->next
)
668 /* If we find a prefix, run its list, prefixing our output by its
669 prefix (with "show " skipped). */
670 if (list
->prefixlist
&& !list
->abbrev_flag
)
672 ui_out_emit_tuple
optionlist_emitter (uiout
, "optionlist");
673 const char *new_prefix
= strstr (list
->prefixname
, "show ") + 5;
675 if (uiout
->is_mi_like_p ())
676 uiout
->field_string ("prefix", new_prefix
);
677 cmd_show_list (*list
->prefixlist
, from_tty
, new_prefix
);
681 if (list
->theclass
!= no_set_class
)
683 ui_out_emit_tuple
option_emitter (uiout
, "option");
685 uiout
->text (prefix
);
686 uiout
->field_string ("name", list
->name
);
688 if (list
->type
== show_cmd
)
689 do_show_command ((char *) NULL
, from_tty
, list
);
691 cmd_func (list
, NULL
, from_tty
);
This page took 0.045399 seconds and 5 git commands to generate.