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
;
383 error (_("gdb internal error: bad var_type in do_setshow_command"));
385 c
->func (c
, NULL
, from_tty
);
386 if (deprecated_set_hook
)
387 deprecated_set_hook (c
);
389 if (notify_command_param_changed_p (option_changed
, c
))
392 struct cmd_list_element
**cmds
;
393 struct cmd_list_element
*p
;
397 /* Compute the whole multi-word command options. If user types command
398 'set foo bar baz on', c->name is 'baz', and GDB can't pass "bar" to
399 command option change notification, because it is confusing. We can
400 trace back through field 'prefix' to compute the whole options,
401 and pass "foo bar baz" to notification. */
403 for (i
= 0, p
= c
; p
!= NULL
; i
++)
405 length
+= strlen (p
->name
);
410 cp
= name
= xmalloc (length
);
411 cmds
= xmalloc (sizeof (struct cmd_list_element
*) * i
);
413 /* Track back through filed 'prefix' and cache them in CMDS. */
414 for (i
= 0, p
= c
; p
!= NULL
; i
++)
420 /* Don't trigger any observer notification if prefixlist is not
423 if (cmds
[i
]->prefixlist
!= &setlist
)
430 /* Traverse them in the reversed order, and copy their names into
432 for (i
--; i
>= 0; i
--)
434 memcpy (cp
, cmds
[i
]->name
, strlen (cmds
[i
]->name
));
435 cp
+= strlen (cmds
[i
]->name
);
450 case var_string_noescape
:
452 case var_optional_filename
:
454 observer_notify_command_param_changed (name
, *(char **) c
->var
);
458 char *opt
= *(int *) c
->var
? "on" : "off";
460 observer_notify_command_param_changed (name
, opt
);
463 case var_auto_boolean
:
465 const char *s
= auto_boolean_enums
[*(enum auto_boolean
*) c
->var
];
467 observer_notify_command_param_changed (name
, s
);
475 xsnprintf (s
, sizeof s
, "%u", *(unsigned int *) c
->var
);
476 observer_notify_command_param_changed (name
, s
);
484 xsnprintf (s
, sizeof s
, "%d", *(int *) c
->var
);
485 observer_notify_command_param_changed (name
, s
);
493 /* Do a "show" command. ARG is NULL if no argument, or the
494 text of the argument, and FROM_TTY is nonzero if this command is
495 being entered directly by the user (i.e. these are just like any
496 other command). C is the command list element for the command. */
499 do_show_command (char *arg
, int from_tty
, struct cmd_list_element
*c
)
501 struct ui_out
*uiout
= current_uiout
;
502 struct cleanup
*old_chain
;
505 gdb_assert (c
->type
== show_cmd
);
507 stb
= mem_fileopen ();
508 old_chain
= make_cleanup_ui_file_delete (stb
);
510 /* Possibly call the pre hook. */
511 if (c
->pre_show_hook
)
512 (c
->pre_show_hook
) (c
);
517 if (*(char **) c
->var
)
518 fputstr_filtered (*(char **) c
->var
, '"', stb
);
520 case var_string_noescape
:
521 case var_optional_filename
:
524 if (*(char **) c
->var
)
525 fputs_filtered (*(char **) c
->var
, stb
);
528 fputs_filtered (*(int *) c
->var
? "on" : "off", stb
);
530 case var_auto_boolean
:
531 switch (*(enum auto_boolean
*) c
->var
)
533 case AUTO_BOOLEAN_TRUE
:
534 fputs_filtered ("on", stb
);
536 case AUTO_BOOLEAN_FALSE
:
537 fputs_filtered ("off", stb
);
539 case AUTO_BOOLEAN_AUTO
:
540 fputs_filtered ("auto", stb
);
543 internal_error (__FILE__
, __LINE__
,
544 _("do_show_command: "
545 "invalid var_auto_boolean"));
551 if (c
->var_type
== var_uinteger
552 && *(unsigned int *) c
->var
== UINT_MAX
)
553 fputs_filtered ("unlimited", stb
);
555 fprintf_filtered (stb
, "%u", *(unsigned int *) c
->var
);
559 if (c
->var_type
== var_integer
560 && *(int *) c
->var
== INT_MAX
)
561 fputs_filtered ("unlimited", stb
);
563 fprintf_filtered (stb
, "%d", *(int *) c
->var
);
567 error (_("gdb internal error: bad var_type in do_show_command"));
571 /* FIXME: cagney/2005-02-10: Need to split this in half: code to
572 convert the value into a string (esentially the above); and
573 code to print the value out. For the latter there should be
574 MI and CLI specific versions. */
576 if (ui_out_is_mi_like_p (uiout
))
577 ui_out_field_stream (uiout
, "value", stb
);
580 char *value
= ui_file_xstrdup (stb
, NULL
);
582 make_cleanup (xfree
, value
);
583 if (c
->show_value_func
!= NULL
)
584 c
->show_value_func (gdb_stdout
, from_tty
, c
, value
);
586 deprecated_show_value_hack (gdb_stdout
, from_tty
, c
, value
);
588 do_cleanups (old_chain
);
590 c
->func (c
, NULL
, from_tty
);
593 /* Show all the settings in a list of show commands. */
596 cmd_show_list (struct cmd_list_element
*list
, int from_tty
, char *prefix
)
598 struct cleanup
*showlist_chain
;
599 struct ui_out
*uiout
= current_uiout
;
601 showlist_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "showlist");
602 for (; list
!= NULL
; list
= list
->next
)
604 /* If we find a prefix, run its list, prefixing our output by its
605 prefix (with "show " skipped). */
606 if (list
->prefixlist
&& !list
->abbrev_flag
)
608 struct cleanup
*optionlist_chain
609 = make_cleanup_ui_out_tuple_begin_end (uiout
, "optionlist");
610 char *new_prefix
= strstr (list
->prefixname
, "show ") + 5;
612 if (ui_out_is_mi_like_p (uiout
))
613 ui_out_field_string (uiout
, "prefix", new_prefix
);
614 cmd_show_list (*list
->prefixlist
, from_tty
, new_prefix
);
615 /* Close the tuple. */
616 do_cleanups (optionlist_chain
);
620 if (list
->class != no_set_class
)
622 struct cleanup
*option_chain
623 = make_cleanup_ui_out_tuple_begin_end (uiout
, "option");
625 ui_out_text (uiout
, prefix
);
626 ui_out_field_string (uiout
, "name", list
->name
);
627 ui_out_text (uiout
, ": ");
628 if (list
->type
== show_cmd
)
629 do_show_command ((char *) NULL
, from_tty
, list
);
631 cmd_func (list
, NULL
, from_tty
);
632 /* Close the tuple. */
633 do_cleanups (option_chain
);
637 /* Close the tuple. */
638 do_cleanups (showlist_chain
);
This page took 0.070713 seconds and 4 git commands to generate.