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 /* 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;
279 error_no_arg (_("integer to set it to."));
280 val = parse_and_eval_long (arg);
282 if (c->var_type == var_uinteger && val == 0)
284 else if (val > UINT_MAX)
285 error (_("integer %s out of range"), plongest (val));
287 if (*(unsigned int *) c->var != val)
289 *(unsigned int *) c->var = val;
301 error_no_arg (_("integer to set it to."));
302 val = parse_and_eval_long (arg);
304 if (val == 0 && c->var_type == var_integer)
306 else if (val > INT_MAX || val < INT_MIN)
307 error (_("integer %s out of range"), plongest (val));
309 if (*(int *) c->var != val)
311 *(int *) c->var = val;
322 const char *match = NULL;
325 /* If no argument was supplied, print an informative error
332 for (i = 0; c->enums[i]; i++)
333 msg_len += strlen (c->enums[i]) + 2;
335 msg = xmalloc (msg_len);
337 make_cleanup (xfree, msg);
339 for (i = 0; c->enums[i]; i++)
343 strcat (msg, c->enums[i]);
345 error (_("Requires an argument. Valid arguments are %s."),
349 p = strchr (arg, ' ');
357 for (i = 0; c->enums[i]; i++)
358 if (strncmp (arg, c->enums[i], len) == 0)
360 if (c->enums[i][len] == '\0')
364 break; /* Exact match. */
374 error (_("Undefined item: \"%s\"."), arg);
377 error (_("Ambiguous item \"%s\"."), arg);
379 if (*(const char **) c->var != match)
381 *(const char **) c->var = match;
387 case var_zuinteger_unlimited:
392 error_no_arg (_("integer to set it to."));
393 val = parse_and_eval_long (arg);
396 error (_("integer %s out of range"), plongest (val));
398 error (_("only -1 is allowed to set as unlimited"));
400 if (*(int *) c->var != val)
402 *(int *) c->var = val;
408 error (_("gdb internal error: bad var_type in do_setshow_command"));
410 c->func (c, NULL, from_tty);
411 if (deprecated_set_hook)
412 deprecated_set_hook (c);
414 if (notify_command_param_changed_p (option_changed, c))
417 struct cmd_list_element **cmds;
418 struct cmd_list_element *p;
422 /* Compute the whole multi-word command options. If user types command
423 'set foo bar baz on', c->name is 'baz', and GDB can't pass "bar" to
424 command option change notification, because it is confusing. We can
425 trace back through field 'prefix' to compute the whole options,
426 and pass "foo bar baz" to notification. */
428 for (i = 0, p = c; p != NULL; i++)
430 length += strlen (p->name);
435 cp = name = xmalloc (length);
436 cmds = xmalloc (sizeof (struct cmd_list_element *) * i);
438 /* Track back through filed 'prefix' and cache them in CMDS. */
439 for (i = 0, p = c; p != NULL; i++)
445 /* Don't trigger any observer notification if prefixlist is not
448 if (cmds[i]->prefixlist != &setlist)
455 /* Traverse them in the reversed order, and copy their names into
457 for (i--; i >= 0; i--)
459 memcpy (cp, cmds[i]->name, strlen (cmds[i]->name));
460 cp += strlen (cmds[i]->name);
475 case var_string_noescape:
477 case var_optional_filename:
479 observer_notify_command_param_changed (name, *(char **) c->var);
483 char *opt = *(int *) c->var ? "on" : "off";
485 observer_notify_command_param_changed (name, opt);
488 case var_auto_boolean:
490 const char *s = auto_boolean_enums[*(enum auto_boolean *) c->var];
492 observer_notify_command_param_changed (name, s);
500 xsnprintf (s, sizeof s, "%u", *(unsigned int *) c->var);
501 observer_notify_command_param_changed (name, s);
506 case var_zuinteger_unlimited:
510 xsnprintf (s, sizeof s, "%d", *(int *) c->var);
511 observer_notify_command_param_changed (name, s);
519 /* Do a "show" command. ARG is NULL if no argument, or the
520 text of the argument, and FROM_TTY is nonzero if this command is
521 being entered directly by the user (i.e. these are just like any
522 other command). C is the command list element for the command. */
525 do_show_command (char *arg, int from_tty, struct cmd_list_element *c)
527 struct ui_out *uiout = current_uiout;
528 struct cleanup *old_chain;
531 gdb_assert (c->type == show_cmd);
533 stb = mem_fileopen ();
534 old_chain = make_cleanup_ui_file_delete (stb);
536 /* Possibly call the pre hook. */
537 if (c->pre_show_hook)
538 (c->pre_show_hook) (c);
543 if (*(char **) c->var)
544 fputstr_filtered (*(char **) c->var, '"', stb);
546 case var_string_noescape:
547 case var_optional_filename:
550 if (*(char **) c->var)
551 fputs_filtered (*(char **) c->var, stb);
554 fputs_filtered (*(int *) c->var ? "on" : "off", stb);
556 case var_auto_boolean:
557 switch (*(enum auto_boolean*) c->var)
559 case AUTO_BOOLEAN_TRUE:
560 fputs_filtered ("on", stb);
562 case AUTO_BOOLEAN_FALSE:
563 fputs_filtered ("off", stb);
565 case AUTO_BOOLEAN_AUTO:
566 fputs_filtered ("auto", stb);
569 internal_error (__FILE__, __LINE__,
570 _("do_show_command: "
571 "invalid var_auto_boolean"));
577 if (c->var_type == var_uinteger
578 && *(unsigned int *) c->var == UINT_MAX)
579 fputs_filtered ("unlimited", stb);
581 fprintf_filtered (stb, "%u", *(unsigned int *) c->var);
585 if (c->var_type == var_integer
586 && *(int *) c->var == INT_MAX)
587 fputs_filtered ("unlimited", stb);
589 fprintf_filtered (stb, "%d", *(int *) c->var);
591 case var_zuinteger_unlimited:
593 if (*(int *) c->var == -1)
594 fputs_filtered ("unlimited", stb);
596 fprintf_filtered (stb, "%d", *(int *) c->var);
600 error (_("gdb internal error: bad var_type in do_show_command"));
604 /* FIXME: cagney/2005-02-10: Need to split this in half: code to
605 convert the value into a string (esentially the above); and
606 code to print the value out. For the latter there should be
607 MI and CLI specific versions. */
609 if (ui_out_is_mi_like_p (uiout))
610 ui_out_field_stream (uiout, "value", stb);
613 char *value = ui_file_xstrdup (stb, NULL);
615 make_cleanup (xfree, value);
616 if (c->show_value_func != NULL)
617 c->show_value_func (gdb_stdout, from_tty, c, value);
619 deprecated_show_value_hack (gdb_stdout, from_tty, c, value);
621 do_cleanups (old_chain);
623 c->func (c, NULL, from_tty);
626 /* Show all the settings in a list of show commands. */
629 cmd_show_list (struct cmd_list_element *list, int from_tty, char *prefix)
631 struct cleanup *showlist_chain;
632 struct ui_out *uiout = current_uiout;
634 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
635 for (; list != NULL; list = list->next)
637 /* If we find a prefix, run its list, prefixing our output by its
638 prefix (with "show " skipped). */
639 if (list->prefixlist && !list->abbrev_flag)
641 struct cleanup *optionlist_chain
642 = make_cleanup_ui_out_tuple_begin_end (uiout, "optionlist");
643 char *new_prefix = strstr (list->prefixname, "show ") + 5;
645 if (ui_out_is_mi_like_p (uiout))
646 ui_out_field_string (uiout, "prefix", new_prefix);
647 cmd_show_list (*list->prefixlist, from_tty, new_prefix);
648 /* Close the tuple. */
649 do_cleanups (optionlist_chain);
653 if (list->class != no_set_class)
655 struct cleanup *option_chain
656 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
658 ui_out_text (uiout, prefix);
659 ui_out_field_string (uiout, "name", list->name);
660 ui_out_text (uiout, ": ");
661 if (list->type == show_cmd)
662 do_show_command ((char *) NULL, from_tty, list);
664 cmd_func (list, NULL, from_tty);
665 /* Close the tuple. */
666 do_cleanups (option_chain);
670 /* Close the tuple. */
671 do_cleanups (showlist_chain);