1 /* Handle set and show GDB commands.
3 Copyright (c) 2000-2003, 2007-2012 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 /* Prototypes for local functions. */
34 static int parse_binary_operation (char *);
36 /* Return true if the change of command parameter should be notified. */
39 notify_command_param_changed_p (int param_changed
, struct cmd_list_element
*c
)
41 if (param_changed
== 0)
44 if (c
->class == class_maintenance
|| c
->class == class_deprecated
45 || c
->class == class_obscure
)
52 static enum auto_boolean
53 parse_auto_binary_operation (const char *arg
)
55 if (arg
!= NULL
&& *arg
!= '\0')
57 int length
= strlen (arg
);
59 while (isspace (arg
[length
- 1]) && length
> 0)
61 if (strncmp (arg
, "on", length
) == 0
62 || strncmp (arg
, "1", length
) == 0
63 || strncmp (arg
, "yes", length
) == 0
64 || strncmp (arg
, "enable", length
) == 0)
65 return AUTO_BOOLEAN_TRUE
;
66 else if (strncmp (arg
, "off", length
) == 0
67 || strncmp (arg
, "0", length
) == 0
68 || strncmp (arg
, "no", length
) == 0
69 || strncmp (arg
, "disable", length
) == 0)
70 return AUTO_BOOLEAN_FALSE
;
71 else if (strncmp (arg
, "auto", length
) == 0
72 || (strncmp (arg
, "-1", length
) == 0 && length
> 1))
73 return AUTO_BOOLEAN_AUTO
;
75 error (_("\"on\", \"off\" or \"auto\" expected."));
76 return AUTO_BOOLEAN_AUTO
; /* Pacify GCC. */
80 parse_binary_operation (char *arg
)
87 length
= strlen (arg
);
89 while (arg
[length
- 1] == ' ' || arg
[length
- 1] == '\t')
92 if (strncmp (arg
, "on", length
) == 0
93 || strncmp (arg
, "1", length
) == 0
94 || strncmp (arg
, "yes", length
) == 0
95 || strncmp (arg
, "enable", length
) == 0)
97 else if (strncmp (arg
, "off", length
) == 0
98 || strncmp (arg
, "0", length
) == 0
99 || strncmp (arg
, "no", length
) == 0
100 || strncmp (arg
, "disable", length
) == 0)
104 error (_("\"on\" or \"off\" expected."));
110 deprecated_show_value_hack (struct ui_file
*ignore_file
,
112 struct cmd_list_element
*c
,
115 /* If there's no command or value, don't try to print it out. */
116 if (c
== NULL
|| value
== NULL
)
118 /* Print doc minus "show" at start. */
119 print_doc_line (gdb_stdout
, c
->doc
+ 5);
123 case var_string_noescape
:
124 case var_optional_filename
:
127 printf_filtered ((" is \"%s\".\n"), value
);
130 printf_filtered ((" is %s.\n"), value
);
135 /* Do a "set" command. ARG is NULL if no argument, or the
136 text of the argument, and FROM_TTY is nonzero if this command is
137 being entered directly by the user (i.e. these are just like any
138 other command). C is the command list element for the command. */
141 do_set_command (char *arg
, int from_tty
, struct cmd_list_element
*c
)
143 /* A flag to indicate the option is changed or not. */
144 int option_changed
= 0;
146 gdb_assert (c
->type
== set_cmd
);
159 new = (char *) xmalloc (strlen (arg
) + 2);
162 while ((ch
= *p
++) != '\000')
166 /* \ at end of argument is used after spaces
167 so they won't be lost. */
168 /* This is obsolete now that we no longer strip
169 trailing whitespace and actually, the backslash
170 didn't get here in my test, readline or
171 something did something funky with a backslash
172 right before a newline. */
175 ch
= parse_escape (get_current_arch (), &p
);
185 if (*(p
- 1) != '\\')
189 new = (char *) xrealloc (new, q
- new);
191 if (*(char **) c
->var
== NULL
192 || strcmp (*(char **) c
->var
, new) != 0)
194 xfree (*(char **) c
->var
);
195 *(char **) c
->var
= new;
203 case var_string_noescape
:
207 if (*(char **) c
->var
== NULL
|| strcmp (*(char **) c
->var
, arg
) != 0)
209 xfree (*(char **) c
->var
);
210 *(char **) c
->var
= xstrdup (arg
);
217 error_no_arg (_("filename to set it to."));
219 case var_optional_filename
:
225 /* Clear trailing whitespace of filename. */
226 char *ptr
= arg
+ strlen (arg
) - 1;
228 while (ptr
>= arg
&& (*ptr
== ' ' || *ptr
== '\t'))
232 val
= tilde_expand (arg
);
237 if (*(char **) c
->var
== NULL
238 || strcmp (*(char **) c
->var
, val
) != 0)
240 xfree (*(char **) c
->var
);
241 *(char **) c
->var
= val
;
251 int val
= parse_binary_operation (arg
);
253 if (val
!= *(int *) c
->var
)
255 *(int *) c
->var
= val
;
261 case var_auto_boolean
:
263 enum auto_boolean val
= parse_auto_binary_operation (arg
);
265 if (*(enum auto_boolean
*) c
->var
!= val
)
267 *(enum auto_boolean
*) c
->var
= val
;
276 error_no_arg (_("integer to set it to."));
278 unsigned int val
= parse_and_eval_long (arg
);
280 if (c
->var_type
== var_uinteger
&& val
== 0)
283 if (*(unsigned int *) c
->var
!= val
)
285 *(unsigned int *) c
->var
= val
;
297 error_no_arg (_("integer to set it to."));
298 val
= parse_and_eval_long (arg
);
299 if (val
== 0 && c
->var_type
== var_integer
)
301 else if (val
>= INT_MAX
)
302 error (_("integer %u out of range"), val
);
304 if (*(int *) c
->var
!= val
)
306 *(int *) c
->var
= val
;
317 const char *match
= NULL
;
320 /* If no argument was supplied, print an informative error
327 for (i
= 0; c
->enums
[i
]; i
++)
328 msg_len
+= strlen (c
->enums
[i
]) + 2;
330 msg
= xmalloc (msg_len
);
332 make_cleanup (xfree
, msg
);
334 for (i
= 0; c
->enums
[i
]; i
++)
338 strcat (msg
, c
->enums
[i
]);
340 error (_("Requires an argument. Valid arguments are %s."),
344 p
= strchr (arg
, ' ');
352 for (i
= 0; c
->enums
[i
]; i
++)
353 if (strncmp (arg
, c
->enums
[i
], len
) == 0)
355 if (c
->enums
[i
][len
] == '\0')
359 break; /* Exact match. */
369 error (_("Undefined item: \"%s\"."), arg
);
372 error (_("Ambiguous item \"%s\"."), arg
);
374 if (*(const char **) c
->var
!= match
)
376 *(const char **) c
->var
= match
;
382 case var_zuinteger_unlimited
:
387 error_no_arg (_("integer to set it to."));
388 val
= parse_and_eval_long (arg
);
391 error (_("integer %s out of range"), plongest (val
));
393 error (_("only -1 is allowed to set as unlimited"));
395 if (*(int *) c
->var
!= val
)
397 *(int *) c
->var
= val
;
403 error (_("gdb internal error: bad var_type in do_setshow_command"));
405 c
->func (c
, NULL
, from_tty
);
406 if (deprecated_set_hook
)
407 deprecated_set_hook (c
);
409 if (notify_command_param_changed_p (option_changed
, c
))
412 struct cmd_list_element
**cmds
;
413 struct cmd_list_element
*p
;
417 /* Compute the whole multi-word command options. If user types command
418 'set foo bar baz on', c->name is 'baz', and GDB can't pass "bar" to
419 command option change notification, because it is confusing. We can
420 trace back through field 'prefix' to compute the whole options,
421 and pass "foo bar baz" to notification. */
423 for (i
= 0, p
= c
; p
!= NULL
; i
++)
425 length
+= strlen (p
->name
);
430 cp
= name
= xmalloc (length
);
431 cmds
= xmalloc (sizeof (struct cmd_list_element
*) * i
);
433 /* Track back through filed 'prefix' and cache them in CMDS. */
434 for (i
= 0, p
= c
; p
!= NULL
; i
++)
440 /* Don't trigger any observer notification if prefixlist is not
443 if (cmds
[i
]->prefixlist
!= &setlist
)
450 /* Traverse them in the reversed order, and copy their names into
452 for (i
--; i
>= 0; i
--)
454 memcpy (cp
, cmds
[i
]->name
, strlen (cmds
[i
]->name
));
455 cp
+= strlen (cmds
[i
]->name
);
470 case var_string_noescape
:
472 case var_optional_filename
:
474 observer_notify_command_param_changed (name
, *(char **) c
->var
);
478 char *opt
= *(int *) c
->var
? "on" : "off";
480 observer_notify_command_param_changed (name
, opt
);
483 case var_auto_boolean
:
485 const char *s
= auto_boolean_enums
[*(enum auto_boolean
*) c
->var
];
487 observer_notify_command_param_changed (name
, s
);
495 xsnprintf (s
, sizeof s
, "%u", *(unsigned int *) c
->var
);
496 observer_notify_command_param_changed (name
, s
);
501 case var_zuinteger_unlimited
:
505 xsnprintf (s
, sizeof s
, "%d", *(int *) c
->var
);
506 observer_notify_command_param_changed (name
, s
);
514 /* Do a "show" command. ARG is NULL if no argument, or the
515 text of the argument, and FROM_TTY is nonzero if this command is
516 being entered directly by the user (i.e. these are just like any
517 other command). C is the command list element for the command. */
520 do_show_command (char *arg
, int from_tty
, struct cmd_list_element
*c
)
522 struct ui_out
*uiout
= current_uiout
;
523 struct cleanup
*old_chain
;
526 gdb_assert (c
->type
== show_cmd
);
528 stb
= mem_fileopen ();
529 old_chain
= make_cleanup_ui_file_delete (stb
);
531 /* Possibly call the pre hook. */
532 if (c
->pre_show_hook
)
533 (c
->pre_show_hook
) (c
);
538 if (*(char **) c
->var
)
539 fputstr_filtered (*(char **) c
->var
, '"', stb
);
541 case var_string_noescape
:
542 case var_optional_filename
:
545 if (*(char **) c
->var
)
546 fputs_filtered (*(char **) c
->var
, stb
);
549 fputs_filtered (*(int *) c
->var
? "on" : "off", stb
);
551 case var_auto_boolean
:
552 switch (*(enum auto_boolean
*) c
->var
)
554 case AUTO_BOOLEAN_TRUE
:
555 fputs_filtered ("on", stb
);
557 case AUTO_BOOLEAN_FALSE
:
558 fputs_filtered ("off", stb
);
560 case AUTO_BOOLEAN_AUTO
:
561 fputs_filtered ("auto", stb
);
564 internal_error (__FILE__
, __LINE__
,
565 _("do_show_command: "
566 "invalid var_auto_boolean"));
572 if (c
->var_type
== var_uinteger
573 && *(unsigned int *) c
->var
== UINT_MAX
)
574 fputs_filtered ("unlimited", stb
);
576 fprintf_filtered (stb
, "%u", *(unsigned int *) c
->var
);
580 if (c
->var_type
== var_integer
581 && *(int *) c
->var
== INT_MAX
)
582 fputs_filtered ("unlimited", stb
);
584 fprintf_filtered (stb
, "%d", *(int *) c
->var
);
586 case var_zuinteger_unlimited
:
588 if (*(int *) c
->var
== -1)
589 fputs_filtered ("unlimited", stb
);
591 fprintf_filtered (stb
, "%u", *(int *) c
->var
);
595 error (_("gdb internal error: bad var_type in do_show_command"));
599 /* FIXME: cagney/2005-02-10: Need to split this in half: code to
600 convert the value into a string (esentially the above); and
601 code to print the value out. For the latter there should be
602 MI and CLI specific versions. */
604 if (ui_out_is_mi_like_p (uiout
))
605 ui_out_field_stream (uiout
, "value", stb
);
608 char *value
= ui_file_xstrdup (stb
, NULL
);
610 make_cleanup (xfree
, value
);
611 if (c
->show_value_func
!= NULL
)
612 c
->show_value_func (gdb_stdout
, from_tty
, c
, value
);
614 deprecated_show_value_hack (gdb_stdout
, from_tty
, c
, value
);
616 do_cleanups (old_chain
);
618 c
->func (c
, NULL
, from_tty
);
621 /* Show all the settings in a list of show commands. */
624 cmd_show_list (struct cmd_list_element
*list
, int from_tty
, char *prefix
)
626 struct cleanup
*showlist_chain
;
627 struct ui_out
*uiout
= current_uiout
;
629 showlist_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "showlist");
630 for (; list
!= NULL
; list
= list
->next
)
632 /* If we find a prefix, run its list, prefixing our output by its
633 prefix (with "show " skipped). */
634 if (list
->prefixlist
&& !list
->abbrev_flag
)
636 struct cleanup
*optionlist_chain
637 = make_cleanup_ui_out_tuple_begin_end (uiout
, "optionlist");
638 char *new_prefix
= strstr (list
->prefixname
, "show ") + 5;
640 if (ui_out_is_mi_like_p (uiout
))
641 ui_out_field_string (uiout
, "prefix", new_prefix
);
642 cmd_show_list (*list
->prefixlist
, from_tty
, new_prefix
);
643 /* Close the tuple. */
644 do_cleanups (optionlist_chain
);
648 if (list
->class != no_set_class
)
650 struct cleanup
*option_chain
651 = make_cleanup_ui_out_tuple_begin_end (uiout
, "option");
653 ui_out_text (uiout
, prefix
);
654 ui_out_field_string (uiout
, "name", list
->name
);
655 ui_out_text (uiout
, ": ");
656 if (list
->type
== show_cmd
)
657 do_show_command ((char *) NULL
, from_tty
, list
);
659 cmd_func (list
, NULL
, from_tty
);
660 /* Close the tuple. */
661 do_cleanups (option_chain
);
665 /* Close the tuple. */
666 do_cleanups (showlist_chain
);
This page took 0.0439349999999999 seconds and 4 git commands to generate.