15796c8dcSSimon Schubert /* Handle set and show GDB commands.
25796c8dcSSimon Schubert
3*ef5ccd6cSJohn Marino Copyright (C) 2000-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert
55796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify
65796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by
75796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or
85796c8dcSSimon Schubert (at your option) any later version.
95796c8dcSSimon Schubert
105796c8dcSSimon Schubert This program is distributed in the hope that it will be useful,
115796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of
125796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
135796c8dcSSimon Schubert GNU General Public License for more details.
145796c8dcSSimon Schubert
155796c8dcSSimon Schubert You should have received a copy of the GNU General Public License
165796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */
175796c8dcSSimon Schubert
185796c8dcSSimon Schubert #include "defs.h"
195796c8dcSSimon Schubert #include "readline/tilde.h"
205796c8dcSSimon Schubert #include "value.h"
215796c8dcSSimon Schubert #include <ctype.h>
225796c8dcSSimon Schubert #include "gdb_string.h"
23cf7f2e2dSJohn Marino #include "arch-utils.h"
24*ef5ccd6cSJohn Marino #include "observer.h"
255796c8dcSSimon Schubert
265796c8dcSSimon Schubert #include "ui-out.h"
275796c8dcSSimon Schubert
285796c8dcSSimon Schubert #include "cli/cli-decode.h"
295796c8dcSSimon Schubert #include "cli/cli-cmds.h"
305796c8dcSSimon Schubert #include "cli/cli-setshow.h"
315796c8dcSSimon Schubert
32c50c785cSJohn Marino /* Prototypes for local functions. */
335796c8dcSSimon Schubert
345796c8dcSSimon Schubert static int parse_binary_operation (char *);
355796c8dcSSimon Schubert
36*ef5ccd6cSJohn Marino /* Return true if the change of command parameter should be notified. */
37*ef5ccd6cSJohn Marino
38*ef5ccd6cSJohn Marino static int
notify_command_param_changed_p(int param_changed,struct cmd_list_element * c)39*ef5ccd6cSJohn Marino notify_command_param_changed_p (int param_changed, struct cmd_list_element *c)
40*ef5ccd6cSJohn Marino {
41*ef5ccd6cSJohn Marino if (param_changed == 0)
42*ef5ccd6cSJohn Marino return 0;
43*ef5ccd6cSJohn Marino
44*ef5ccd6cSJohn Marino if (c->class == class_maintenance || c->class == class_deprecated
45*ef5ccd6cSJohn Marino || c->class == class_obscure)
46*ef5ccd6cSJohn Marino return 0;
47*ef5ccd6cSJohn Marino
48*ef5ccd6cSJohn Marino return 1;
49*ef5ccd6cSJohn Marino }
50*ef5ccd6cSJohn Marino
515796c8dcSSimon Schubert
525796c8dcSSimon Schubert static enum auto_boolean
parse_auto_binary_operation(const char * arg)535796c8dcSSimon Schubert parse_auto_binary_operation (const char *arg)
545796c8dcSSimon Schubert {
555796c8dcSSimon Schubert if (arg != NULL && *arg != '\0')
565796c8dcSSimon Schubert {
575796c8dcSSimon Schubert int length = strlen (arg);
58cf7f2e2dSJohn Marino
595796c8dcSSimon Schubert while (isspace (arg[length - 1]) && length > 0)
605796c8dcSSimon Schubert length--;
615796c8dcSSimon Schubert if (strncmp (arg, "on", length) == 0
625796c8dcSSimon Schubert || strncmp (arg, "1", length) == 0
635796c8dcSSimon Schubert || strncmp (arg, "yes", length) == 0
645796c8dcSSimon Schubert || strncmp (arg, "enable", length) == 0)
655796c8dcSSimon Schubert return AUTO_BOOLEAN_TRUE;
665796c8dcSSimon Schubert else if (strncmp (arg, "off", length) == 0
675796c8dcSSimon Schubert || strncmp (arg, "0", length) == 0
685796c8dcSSimon Schubert || strncmp (arg, "no", length) == 0
695796c8dcSSimon Schubert || strncmp (arg, "disable", length) == 0)
705796c8dcSSimon Schubert return AUTO_BOOLEAN_FALSE;
715796c8dcSSimon Schubert else if (strncmp (arg, "auto", length) == 0
725796c8dcSSimon Schubert || (strncmp (arg, "-1", length) == 0 && length > 1))
735796c8dcSSimon Schubert return AUTO_BOOLEAN_AUTO;
745796c8dcSSimon Schubert }
755796c8dcSSimon Schubert error (_("\"on\", \"off\" or \"auto\" expected."));
76c50c785cSJohn Marino return AUTO_BOOLEAN_AUTO; /* Pacify GCC. */
775796c8dcSSimon Schubert }
785796c8dcSSimon Schubert
795796c8dcSSimon Schubert static int
parse_binary_operation(char * arg)805796c8dcSSimon Schubert parse_binary_operation (char *arg)
815796c8dcSSimon Schubert {
825796c8dcSSimon Schubert int length;
835796c8dcSSimon Schubert
845796c8dcSSimon Schubert if (!arg || !*arg)
855796c8dcSSimon Schubert return 1;
865796c8dcSSimon Schubert
875796c8dcSSimon Schubert length = strlen (arg);
885796c8dcSSimon Schubert
895796c8dcSSimon Schubert while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
905796c8dcSSimon Schubert length--;
915796c8dcSSimon Schubert
925796c8dcSSimon Schubert if (strncmp (arg, "on", length) == 0
935796c8dcSSimon Schubert || strncmp (arg, "1", length) == 0
945796c8dcSSimon Schubert || strncmp (arg, "yes", length) == 0
955796c8dcSSimon Schubert || strncmp (arg, "enable", length) == 0)
965796c8dcSSimon Schubert return 1;
975796c8dcSSimon Schubert else if (strncmp (arg, "off", length) == 0
985796c8dcSSimon Schubert || strncmp (arg, "0", length) == 0
995796c8dcSSimon Schubert || strncmp (arg, "no", length) == 0
1005796c8dcSSimon Schubert || strncmp (arg, "disable", length) == 0)
1015796c8dcSSimon Schubert return 0;
1025796c8dcSSimon Schubert else
1035796c8dcSSimon Schubert {
1045796c8dcSSimon Schubert error (_("\"on\" or \"off\" expected."));
1055796c8dcSSimon Schubert return 0;
1065796c8dcSSimon Schubert }
1075796c8dcSSimon Schubert }
1085796c8dcSSimon Schubert
1095796c8dcSSimon Schubert void
deprecated_show_value_hack(struct ui_file * ignore_file,int ignore_from_tty,struct cmd_list_element * c,const char * value)1105796c8dcSSimon Schubert deprecated_show_value_hack (struct ui_file *ignore_file,
1115796c8dcSSimon Schubert int ignore_from_tty,
1125796c8dcSSimon Schubert struct cmd_list_element *c,
1135796c8dcSSimon Schubert const char *value)
1145796c8dcSSimon Schubert {
1155796c8dcSSimon Schubert /* If there's no command or value, don't try to print it out. */
1165796c8dcSSimon Schubert if (c == NULL || value == NULL)
1175796c8dcSSimon Schubert return;
1185796c8dcSSimon Schubert /* Print doc minus "show" at start. */
1195796c8dcSSimon Schubert print_doc_line (gdb_stdout, c->doc + 5);
1205796c8dcSSimon Schubert switch (c->var_type)
1215796c8dcSSimon Schubert {
1225796c8dcSSimon Schubert case var_string:
1235796c8dcSSimon Schubert case var_string_noescape:
1245796c8dcSSimon Schubert case var_optional_filename:
1255796c8dcSSimon Schubert case var_filename:
1265796c8dcSSimon Schubert case var_enum:
1275796c8dcSSimon Schubert printf_filtered ((" is \"%s\".\n"), value);
1285796c8dcSSimon Schubert break;
1295796c8dcSSimon Schubert default:
1305796c8dcSSimon Schubert printf_filtered ((" is %s.\n"), value);
1315796c8dcSSimon Schubert break;
1325796c8dcSSimon Schubert }
1335796c8dcSSimon Schubert }
1345796c8dcSSimon Schubert
135*ef5ccd6cSJohn Marino /* Do a "set" command. ARG is NULL if no argument, or the
136c50c785cSJohn Marino text of the argument, and FROM_TTY is nonzero if this command is
137c50c785cSJohn Marino being entered directly by the user (i.e. these are just like any
138c50c785cSJohn Marino other command). C is the command list element for the command. */
1395796c8dcSSimon Schubert
1405796c8dcSSimon Schubert void
do_set_command(char * arg,int from_tty,struct cmd_list_element * c)141*ef5ccd6cSJohn Marino do_set_command (char *arg, int from_tty, struct cmd_list_element *c)
1425796c8dcSSimon Schubert {
143*ef5ccd6cSJohn Marino /* A flag to indicate the option is changed or not. */
144*ef5ccd6cSJohn Marino int option_changed = 0;
145a45ae5f8SJohn Marino
146*ef5ccd6cSJohn Marino gdb_assert (c->type == set_cmd);
147*ef5ccd6cSJohn Marino
1485796c8dcSSimon Schubert switch (c->var_type)
1495796c8dcSSimon Schubert {
1505796c8dcSSimon Schubert case var_string:
1515796c8dcSSimon Schubert {
1525796c8dcSSimon Schubert char *new;
1535796c8dcSSimon Schubert char *p;
1545796c8dcSSimon Schubert char *q;
1555796c8dcSSimon Schubert int ch;
1565796c8dcSSimon Schubert
1575796c8dcSSimon Schubert if (arg == NULL)
1585796c8dcSSimon Schubert arg = "";
1595796c8dcSSimon Schubert new = (char *) xmalloc (strlen (arg) + 2);
1605796c8dcSSimon Schubert p = arg;
1615796c8dcSSimon Schubert q = new;
1625796c8dcSSimon Schubert while ((ch = *p++) != '\000')
1635796c8dcSSimon Schubert {
1645796c8dcSSimon Schubert if (ch == '\\')
1655796c8dcSSimon Schubert {
1665796c8dcSSimon Schubert /* \ at end of argument is used after spaces
1675796c8dcSSimon Schubert so they won't be lost. */
1685796c8dcSSimon Schubert /* This is obsolete now that we no longer strip
1695796c8dcSSimon Schubert trailing whitespace and actually, the backslash
1705796c8dcSSimon Schubert didn't get here in my test, readline or
1715796c8dcSSimon Schubert something did something funky with a backslash
1725796c8dcSSimon Schubert right before a newline. */
1735796c8dcSSimon Schubert if (*p == 0)
1745796c8dcSSimon Schubert break;
175cf7f2e2dSJohn Marino ch = parse_escape (get_current_arch (), &p);
1765796c8dcSSimon Schubert if (ch == 0)
1775796c8dcSSimon Schubert break; /* C loses */
1785796c8dcSSimon Schubert else if (ch > 0)
1795796c8dcSSimon Schubert *q++ = ch;
1805796c8dcSSimon Schubert }
1815796c8dcSSimon Schubert else
1825796c8dcSSimon Schubert *q++ = ch;
1835796c8dcSSimon Schubert }
1845796c8dcSSimon Schubert #if 0
1855796c8dcSSimon Schubert if (*(p - 1) != '\\')
1865796c8dcSSimon Schubert *q++ = ' ';
1875796c8dcSSimon Schubert #endif
1885796c8dcSSimon Schubert *q++ = '\0';
1895796c8dcSSimon Schubert new = (char *) xrealloc (new, q - new);
190*ef5ccd6cSJohn Marino
191*ef5ccd6cSJohn Marino if (*(char **) c->var == NULL
192*ef5ccd6cSJohn Marino || strcmp (*(char **) c->var, new) != 0)
193*ef5ccd6cSJohn Marino {
1945796c8dcSSimon Schubert xfree (*(char **) c->var);
1955796c8dcSSimon Schubert *(char **) c->var = new;
196*ef5ccd6cSJohn Marino
197*ef5ccd6cSJohn Marino option_changed = 1;
198*ef5ccd6cSJohn Marino }
199*ef5ccd6cSJohn Marino else
200*ef5ccd6cSJohn Marino xfree (new);
2015796c8dcSSimon Schubert }
2025796c8dcSSimon Schubert break;
2035796c8dcSSimon Schubert case var_string_noescape:
2045796c8dcSSimon Schubert if (arg == NULL)
2055796c8dcSSimon Schubert arg = "";
206*ef5ccd6cSJohn Marino
207*ef5ccd6cSJohn Marino if (*(char **) c->var == NULL || strcmp (*(char **) c->var, arg) != 0)
208*ef5ccd6cSJohn Marino {
2095796c8dcSSimon Schubert xfree (*(char **) c->var);
2105796c8dcSSimon Schubert *(char **) c->var = xstrdup (arg);
211*ef5ccd6cSJohn Marino
212*ef5ccd6cSJohn Marino option_changed = 1;
213*ef5ccd6cSJohn Marino }
2145796c8dcSSimon Schubert break;
2155796c8dcSSimon Schubert case var_filename:
2165796c8dcSSimon Schubert if (arg == NULL)
2175796c8dcSSimon Schubert error_no_arg (_("filename to set it to."));
218*ef5ccd6cSJohn Marino /* FALLTHROUGH */
219*ef5ccd6cSJohn Marino case var_optional_filename:
220*ef5ccd6cSJohn Marino {
221*ef5ccd6cSJohn Marino char *val = NULL;
222*ef5ccd6cSJohn Marino
223*ef5ccd6cSJohn Marino if (arg != NULL)
2245796c8dcSSimon Schubert {
2255796c8dcSSimon Schubert /* Clear trailing whitespace of filename. */
2265796c8dcSSimon Schubert char *ptr = arg + strlen (arg) - 1;
227cf7f2e2dSJohn Marino
2285796c8dcSSimon Schubert while (ptr >= arg && (*ptr == ' ' || *ptr == '\t'))
2295796c8dcSSimon Schubert ptr--;
2305796c8dcSSimon Schubert *(ptr + 1) = '\0';
231*ef5ccd6cSJohn Marino
232*ef5ccd6cSJohn Marino val = tilde_expand (arg);
2335796c8dcSSimon Schubert }
234*ef5ccd6cSJohn Marino else
235*ef5ccd6cSJohn Marino val = xstrdup ("");
236*ef5ccd6cSJohn Marino
237*ef5ccd6cSJohn Marino if (*(char **) c->var == NULL
238*ef5ccd6cSJohn Marino || strcmp (*(char **) c->var, val) != 0)
239*ef5ccd6cSJohn Marino {
240*ef5ccd6cSJohn Marino xfree (*(char **) c->var);
241*ef5ccd6cSJohn Marino *(char **) c->var = val;
242*ef5ccd6cSJohn Marino
243*ef5ccd6cSJohn Marino option_changed = 1;
244*ef5ccd6cSJohn Marino }
245*ef5ccd6cSJohn Marino else
246*ef5ccd6cSJohn Marino xfree (val);
247*ef5ccd6cSJohn Marino }
2485796c8dcSSimon Schubert break;
2495796c8dcSSimon Schubert case var_boolean:
250*ef5ccd6cSJohn Marino {
251*ef5ccd6cSJohn Marino int val = parse_binary_operation (arg);
252*ef5ccd6cSJohn Marino
253*ef5ccd6cSJohn Marino if (val != *(int *) c->var)
254*ef5ccd6cSJohn Marino {
255*ef5ccd6cSJohn Marino *(int *) c->var = val;
256*ef5ccd6cSJohn Marino
257*ef5ccd6cSJohn Marino option_changed = 1;
258*ef5ccd6cSJohn Marino }
259*ef5ccd6cSJohn Marino }
2605796c8dcSSimon Schubert break;
2615796c8dcSSimon Schubert case var_auto_boolean:
262*ef5ccd6cSJohn Marino {
263*ef5ccd6cSJohn Marino enum auto_boolean val = parse_auto_binary_operation (arg);
264*ef5ccd6cSJohn Marino
265*ef5ccd6cSJohn Marino if (*(enum auto_boolean *) c->var != val)
266*ef5ccd6cSJohn Marino {
267*ef5ccd6cSJohn Marino *(enum auto_boolean *) c->var = val;
268*ef5ccd6cSJohn Marino
269*ef5ccd6cSJohn Marino option_changed = 1;
270*ef5ccd6cSJohn Marino }
271*ef5ccd6cSJohn Marino }
2725796c8dcSSimon Schubert break;
2735796c8dcSSimon Schubert case var_uinteger:
274*ef5ccd6cSJohn Marino case var_zuinteger:
2755796c8dcSSimon Schubert {
276*ef5ccd6cSJohn Marino LONGEST val;
277cf7f2e2dSJohn Marino
2785796c8dcSSimon Schubert if (arg == NULL)
2795796c8dcSSimon Schubert error_no_arg (_("integer to set it to."));
2805796c8dcSSimon Schubert val = parse_and_eval_long (arg);
281*ef5ccd6cSJohn Marino
282*ef5ccd6cSJohn Marino if (c->var_type == var_uinteger && val == 0)
283*ef5ccd6cSJohn Marino val = UINT_MAX;
284*ef5ccd6cSJohn Marino else if (val < 0
285*ef5ccd6cSJohn Marino /* For var_uinteger, don't let the user set the value
286*ef5ccd6cSJohn Marino to UINT_MAX directly, as that exposes an
287*ef5ccd6cSJohn Marino implementation detail to the user interface. */
288*ef5ccd6cSJohn Marino || (c->var_type == var_uinteger && val >= UINT_MAX)
289*ef5ccd6cSJohn Marino || (c->var_type == var_zuinteger && val > UINT_MAX))
290*ef5ccd6cSJohn Marino error (_("integer %s out of range"), plongest (val));
291*ef5ccd6cSJohn Marino
292*ef5ccd6cSJohn Marino if (*(unsigned int *) c->var != val)
293*ef5ccd6cSJohn Marino {
294*ef5ccd6cSJohn Marino *(unsigned int *) c->var = val;
295*ef5ccd6cSJohn Marino
296*ef5ccd6cSJohn Marino option_changed = 1;
297*ef5ccd6cSJohn Marino }
298*ef5ccd6cSJohn Marino }
299*ef5ccd6cSJohn Marino break;
300*ef5ccd6cSJohn Marino case var_integer:
301*ef5ccd6cSJohn Marino case var_zinteger:
302*ef5ccd6cSJohn Marino {
303*ef5ccd6cSJohn Marino LONGEST val;
304*ef5ccd6cSJohn Marino
305*ef5ccd6cSJohn Marino if (arg == NULL)
306*ef5ccd6cSJohn Marino error_no_arg (_("integer to set it to."));
307*ef5ccd6cSJohn Marino val = parse_and_eval_long (arg);
308*ef5ccd6cSJohn Marino
309*ef5ccd6cSJohn Marino if (val == 0 && c->var_type == var_integer)
310*ef5ccd6cSJohn Marino val = INT_MAX;
311*ef5ccd6cSJohn Marino else if (val < INT_MIN
312*ef5ccd6cSJohn Marino /* For var_integer, don't let the user set the value
313*ef5ccd6cSJohn Marino to INT_MAX directly, as that exposes an
314*ef5ccd6cSJohn Marino implementation detail to the user interface. */
315*ef5ccd6cSJohn Marino || (c->var_type == var_integer && val >= INT_MAX)
316*ef5ccd6cSJohn Marino || (c->var_type == var_zinteger && val > INT_MAX))
317*ef5ccd6cSJohn Marino error (_("integer %s out of range"), plongest (val));
318*ef5ccd6cSJohn Marino
319*ef5ccd6cSJohn Marino if (*(int *) c->var != val)
320*ef5ccd6cSJohn Marino {
3215796c8dcSSimon Schubert *(int *) c->var = val;
322*ef5ccd6cSJohn Marino
323*ef5ccd6cSJohn Marino option_changed = 1;
324*ef5ccd6cSJohn Marino }
3255796c8dcSSimon Schubert break;
3265796c8dcSSimon Schubert }
3275796c8dcSSimon Schubert case var_enum:
3285796c8dcSSimon Schubert {
3295796c8dcSSimon Schubert int i;
3305796c8dcSSimon Schubert int len;
3315796c8dcSSimon Schubert int nmatches;
3325796c8dcSSimon Schubert const char *match = NULL;
3335796c8dcSSimon Schubert char *p;
3345796c8dcSSimon Schubert
335c50c785cSJohn Marino /* If no argument was supplied, print an informative error
336c50c785cSJohn Marino message. */
3375796c8dcSSimon Schubert if (arg == NULL)
3385796c8dcSSimon Schubert {
3395796c8dcSSimon Schubert char *msg;
3405796c8dcSSimon Schubert int msg_len = 0;
341cf7f2e2dSJohn Marino
3425796c8dcSSimon Schubert for (i = 0; c->enums[i]; i++)
3435796c8dcSSimon Schubert msg_len += strlen (c->enums[i]) + 2;
3445796c8dcSSimon Schubert
3455796c8dcSSimon Schubert msg = xmalloc (msg_len);
3465796c8dcSSimon Schubert *msg = '\0';
3475796c8dcSSimon Schubert make_cleanup (xfree, msg);
3485796c8dcSSimon Schubert
3495796c8dcSSimon Schubert for (i = 0; c->enums[i]; i++)
3505796c8dcSSimon Schubert {
3515796c8dcSSimon Schubert if (i != 0)
3525796c8dcSSimon Schubert strcat (msg, ", ");
3535796c8dcSSimon Schubert strcat (msg, c->enums[i]);
3545796c8dcSSimon Schubert }
355c50c785cSJohn Marino error (_("Requires an argument. Valid arguments are %s."),
356c50c785cSJohn Marino msg);
3575796c8dcSSimon Schubert }
3585796c8dcSSimon Schubert
3595796c8dcSSimon Schubert p = strchr (arg, ' ');
3605796c8dcSSimon Schubert
3615796c8dcSSimon Schubert if (p)
3625796c8dcSSimon Schubert len = p - arg;
3635796c8dcSSimon Schubert else
3645796c8dcSSimon Schubert len = strlen (arg);
3655796c8dcSSimon Schubert
3665796c8dcSSimon Schubert nmatches = 0;
3675796c8dcSSimon Schubert for (i = 0; c->enums[i]; i++)
3685796c8dcSSimon Schubert if (strncmp (arg, c->enums[i], len) == 0)
3695796c8dcSSimon Schubert {
3705796c8dcSSimon Schubert if (c->enums[i][len] == '\0')
3715796c8dcSSimon Schubert {
3725796c8dcSSimon Schubert match = c->enums[i];
3735796c8dcSSimon Schubert nmatches = 1;
374c50c785cSJohn Marino break; /* Exact match. */
3755796c8dcSSimon Schubert }
3765796c8dcSSimon Schubert else
3775796c8dcSSimon Schubert {
3785796c8dcSSimon Schubert match = c->enums[i];
3795796c8dcSSimon Schubert nmatches++;
3805796c8dcSSimon Schubert }
3815796c8dcSSimon Schubert }
3825796c8dcSSimon Schubert
3835796c8dcSSimon Schubert if (nmatches <= 0)
3845796c8dcSSimon Schubert error (_("Undefined item: \"%s\"."), arg);
3855796c8dcSSimon Schubert
3865796c8dcSSimon Schubert if (nmatches > 1)
3875796c8dcSSimon Schubert error (_("Ambiguous item \"%s\"."), arg);
3885796c8dcSSimon Schubert
389*ef5ccd6cSJohn Marino if (*(const char **) c->var != match)
390*ef5ccd6cSJohn Marino {
3915796c8dcSSimon Schubert *(const char **) c->var = match;
392*ef5ccd6cSJohn Marino
393*ef5ccd6cSJohn Marino option_changed = 1;
394*ef5ccd6cSJohn Marino }
395*ef5ccd6cSJohn Marino }
396*ef5ccd6cSJohn Marino break;
397*ef5ccd6cSJohn Marino case var_zuinteger_unlimited:
398*ef5ccd6cSJohn Marino {
399*ef5ccd6cSJohn Marino LONGEST val;
400*ef5ccd6cSJohn Marino
401*ef5ccd6cSJohn Marino if (arg == NULL)
402*ef5ccd6cSJohn Marino error_no_arg (_("integer to set it to."));
403*ef5ccd6cSJohn Marino val = parse_and_eval_long (arg);
404*ef5ccd6cSJohn Marino
405*ef5ccd6cSJohn Marino if (val > INT_MAX)
406*ef5ccd6cSJohn Marino error (_("integer %s out of range"), plongest (val));
407*ef5ccd6cSJohn Marino else if (val < -1)
408*ef5ccd6cSJohn Marino error (_("only -1 is allowed to set as unlimited"));
409*ef5ccd6cSJohn Marino
410*ef5ccd6cSJohn Marino if (*(int *) c->var != val)
411*ef5ccd6cSJohn Marino {
412*ef5ccd6cSJohn Marino *(int *) c->var = val;
413*ef5ccd6cSJohn Marino option_changed = 1;
414*ef5ccd6cSJohn Marino }
4155796c8dcSSimon Schubert }
4165796c8dcSSimon Schubert break;
4175796c8dcSSimon Schubert default:
4185796c8dcSSimon Schubert error (_("gdb internal error: bad var_type in do_setshow_command"));
4195796c8dcSSimon Schubert }
420*ef5ccd6cSJohn Marino c->func (c, NULL, from_tty);
421*ef5ccd6cSJohn Marino if (deprecated_set_hook)
422*ef5ccd6cSJohn Marino deprecated_set_hook (c);
4235796c8dcSSimon Schubert
424*ef5ccd6cSJohn Marino if (notify_command_param_changed_p (option_changed, c))
425*ef5ccd6cSJohn Marino {
426*ef5ccd6cSJohn Marino char *name, *cp;
427*ef5ccd6cSJohn Marino struct cmd_list_element **cmds;
428*ef5ccd6cSJohn Marino struct cmd_list_element *p;
429*ef5ccd6cSJohn Marino int i;
430*ef5ccd6cSJohn Marino int length = 0;
431*ef5ccd6cSJohn Marino
432*ef5ccd6cSJohn Marino /* Compute the whole multi-word command options. If user types command
433*ef5ccd6cSJohn Marino 'set foo bar baz on', c->name is 'baz', and GDB can't pass "bar" to
434*ef5ccd6cSJohn Marino command option change notification, because it is confusing. We can
435*ef5ccd6cSJohn Marino trace back through field 'prefix' to compute the whole options,
436*ef5ccd6cSJohn Marino and pass "foo bar baz" to notification. */
437*ef5ccd6cSJohn Marino
438*ef5ccd6cSJohn Marino for (i = 0, p = c; p != NULL; i++)
439*ef5ccd6cSJohn Marino {
440*ef5ccd6cSJohn Marino length += strlen (p->name);
441*ef5ccd6cSJohn Marino length++;
442*ef5ccd6cSJohn Marino
443*ef5ccd6cSJohn Marino p = p->prefix;
444*ef5ccd6cSJohn Marino }
445*ef5ccd6cSJohn Marino cp = name = xmalloc (length);
446*ef5ccd6cSJohn Marino cmds = xmalloc (sizeof (struct cmd_list_element *) * i);
447*ef5ccd6cSJohn Marino
448*ef5ccd6cSJohn Marino /* Track back through filed 'prefix' and cache them in CMDS. */
449*ef5ccd6cSJohn Marino for (i = 0, p = c; p != NULL; i++)
450*ef5ccd6cSJohn Marino {
451*ef5ccd6cSJohn Marino cmds[i] = p;
452*ef5ccd6cSJohn Marino p = p->prefix;
453*ef5ccd6cSJohn Marino }
454*ef5ccd6cSJohn Marino
455*ef5ccd6cSJohn Marino /* Don't trigger any observer notification if prefixlist is not
456*ef5ccd6cSJohn Marino setlist. */
457*ef5ccd6cSJohn Marino i--;
458*ef5ccd6cSJohn Marino if (cmds[i]->prefixlist != &setlist)
459*ef5ccd6cSJohn Marino {
460*ef5ccd6cSJohn Marino xfree (cmds);
461*ef5ccd6cSJohn Marino xfree (name);
462*ef5ccd6cSJohn Marino
463*ef5ccd6cSJohn Marino return;
464*ef5ccd6cSJohn Marino }
465*ef5ccd6cSJohn Marino /* Traverse them in the reversed order, and copy their names into
466*ef5ccd6cSJohn Marino NAME. */
467*ef5ccd6cSJohn Marino for (i--; i >= 0; i--)
468*ef5ccd6cSJohn Marino {
469*ef5ccd6cSJohn Marino memcpy (cp, cmds[i]->name, strlen (cmds[i]->name));
470*ef5ccd6cSJohn Marino cp += strlen (cmds[i]->name);
471*ef5ccd6cSJohn Marino
472*ef5ccd6cSJohn Marino if (i != 0)
473*ef5ccd6cSJohn Marino {
474*ef5ccd6cSJohn Marino cp[0] = ' ';
475*ef5ccd6cSJohn Marino cp++;
476*ef5ccd6cSJohn Marino }
477*ef5ccd6cSJohn Marino }
478*ef5ccd6cSJohn Marino cp[0] = 0;
479*ef5ccd6cSJohn Marino
480*ef5ccd6cSJohn Marino xfree (cmds);
481*ef5ccd6cSJohn Marino
482*ef5ccd6cSJohn Marino switch (c->var_type)
483*ef5ccd6cSJohn Marino {
484*ef5ccd6cSJohn Marino case var_string:
485*ef5ccd6cSJohn Marino case var_string_noescape:
486*ef5ccd6cSJohn Marino case var_filename:
487*ef5ccd6cSJohn Marino case var_optional_filename:
488*ef5ccd6cSJohn Marino case var_enum:
489*ef5ccd6cSJohn Marino observer_notify_command_param_changed (name, *(char **) c->var);
490*ef5ccd6cSJohn Marino break;
491*ef5ccd6cSJohn Marino case var_boolean:
492*ef5ccd6cSJohn Marino {
493*ef5ccd6cSJohn Marino char *opt = *(int *) c->var ? "on" : "off";
494*ef5ccd6cSJohn Marino
495*ef5ccd6cSJohn Marino observer_notify_command_param_changed (name, opt);
496*ef5ccd6cSJohn Marino }
497*ef5ccd6cSJohn Marino break;
498*ef5ccd6cSJohn Marino case var_auto_boolean:
499*ef5ccd6cSJohn Marino {
500*ef5ccd6cSJohn Marino const char *s = auto_boolean_enums[*(enum auto_boolean *) c->var];
501*ef5ccd6cSJohn Marino
502*ef5ccd6cSJohn Marino observer_notify_command_param_changed (name, s);
503*ef5ccd6cSJohn Marino }
504*ef5ccd6cSJohn Marino break;
505*ef5ccd6cSJohn Marino case var_uinteger:
506*ef5ccd6cSJohn Marino case var_zuinteger:
507*ef5ccd6cSJohn Marino {
508*ef5ccd6cSJohn Marino char s[64];
509*ef5ccd6cSJohn Marino
510*ef5ccd6cSJohn Marino xsnprintf (s, sizeof s, "%u", *(unsigned int *) c->var);
511*ef5ccd6cSJohn Marino observer_notify_command_param_changed (name, s);
512*ef5ccd6cSJohn Marino }
513*ef5ccd6cSJohn Marino break;
514*ef5ccd6cSJohn Marino case var_integer:
515*ef5ccd6cSJohn Marino case var_zinteger:
516*ef5ccd6cSJohn Marino case var_zuinteger_unlimited:
517*ef5ccd6cSJohn Marino {
518*ef5ccd6cSJohn Marino char s[64];
519*ef5ccd6cSJohn Marino
520*ef5ccd6cSJohn Marino xsnprintf (s, sizeof s, "%d", *(int *) c->var);
521*ef5ccd6cSJohn Marino observer_notify_command_param_changed (name, s);
522*ef5ccd6cSJohn Marino }
523*ef5ccd6cSJohn Marino break;
524*ef5ccd6cSJohn Marino }
525*ef5ccd6cSJohn Marino xfree (name);
526*ef5ccd6cSJohn Marino }
527*ef5ccd6cSJohn Marino }
528*ef5ccd6cSJohn Marino
529*ef5ccd6cSJohn Marino /* Do a "show" command. ARG is NULL if no argument, or the
530*ef5ccd6cSJohn Marino text of the argument, and FROM_TTY is nonzero if this command is
531*ef5ccd6cSJohn Marino being entered directly by the user (i.e. these are just like any
532*ef5ccd6cSJohn Marino other command). C is the command list element for the command. */
533*ef5ccd6cSJohn Marino
534*ef5ccd6cSJohn Marino void
do_show_command(char * arg,int from_tty,struct cmd_list_element * c)535*ef5ccd6cSJohn Marino do_show_command (char *arg, int from_tty, struct cmd_list_element *c)
536*ef5ccd6cSJohn Marino {
537*ef5ccd6cSJohn Marino struct ui_out *uiout = current_uiout;
538*ef5ccd6cSJohn Marino struct cleanup *old_chain;
539*ef5ccd6cSJohn Marino struct ui_file *stb;
540*ef5ccd6cSJohn Marino
541*ef5ccd6cSJohn Marino gdb_assert (c->type == show_cmd);
542*ef5ccd6cSJohn Marino
543*ef5ccd6cSJohn Marino stb = mem_fileopen ();
544*ef5ccd6cSJohn Marino old_chain = make_cleanup_ui_file_delete (stb);
5455796c8dcSSimon Schubert
5465796c8dcSSimon Schubert /* Possibly call the pre hook. */
5475796c8dcSSimon Schubert if (c->pre_show_hook)
5485796c8dcSSimon Schubert (c->pre_show_hook) (c);
5495796c8dcSSimon Schubert
5505796c8dcSSimon Schubert switch (c->var_type)
5515796c8dcSSimon Schubert {
5525796c8dcSSimon Schubert case var_string:
5535796c8dcSSimon Schubert if (*(char **) c->var)
554*ef5ccd6cSJohn Marino fputstr_filtered (*(char **) c->var, '"', stb);
5555796c8dcSSimon Schubert break;
5565796c8dcSSimon Schubert case var_string_noescape:
5575796c8dcSSimon Schubert case var_optional_filename:
5585796c8dcSSimon Schubert case var_filename:
5595796c8dcSSimon Schubert case var_enum:
5605796c8dcSSimon Schubert if (*(char **) c->var)
561*ef5ccd6cSJohn Marino fputs_filtered (*(char **) c->var, stb);
5625796c8dcSSimon Schubert break;
5635796c8dcSSimon Schubert case var_boolean:
564*ef5ccd6cSJohn Marino fputs_filtered (*(int *) c->var ? "on" : "off", stb);
5655796c8dcSSimon Schubert break;
5665796c8dcSSimon Schubert case var_auto_boolean:
5675796c8dcSSimon Schubert switch (*(enum auto_boolean*) c->var)
5685796c8dcSSimon Schubert {
5695796c8dcSSimon Schubert case AUTO_BOOLEAN_TRUE:
570*ef5ccd6cSJohn Marino fputs_filtered ("on", stb);
5715796c8dcSSimon Schubert break;
5725796c8dcSSimon Schubert case AUTO_BOOLEAN_FALSE:
573*ef5ccd6cSJohn Marino fputs_filtered ("off", stb);
5745796c8dcSSimon Schubert break;
5755796c8dcSSimon Schubert case AUTO_BOOLEAN_AUTO:
576*ef5ccd6cSJohn Marino fputs_filtered ("auto", stb);
5775796c8dcSSimon Schubert break;
5785796c8dcSSimon Schubert default:
5795796c8dcSSimon Schubert internal_error (__FILE__, __LINE__,
580*ef5ccd6cSJohn Marino _("do_show_command: "
581c50c785cSJohn Marino "invalid var_auto_boolean"));
5825796c8dcSSimon Schubert break;
5835796c8dcSSimon Schubert }
5845796c8dcSSimon Schubert break;
5855796c8dcSSimon Schubert case var_uinteger:
5865796c8dcSSimon Schubert case var_zuinteger:
587a45ae5f8SJohn Marino if (c->var_type == var_uinteger
588a45ae5f8SJohn Marino && *(unsigned int *) c->var == UINT_MAX)
589*ef5ccd6cSJohn Marino fputs_filtered ("unlimited", stb);
590a45ae5f8SJohn Marino else
591*ef5ccd6cSJohn Marino fprintf_filtered (stb, "%u", *(unsigned int *) c->var);
5925796c8dcSSimon Schubert break;
5935796c8dcSSimon Schubert case var_integer:
594a45ae5f8SJohn Marino case var_zinteger:
595a45ae5f8SJohn Marino if (c->var_type == var_integer
596a45ae5f8SJohn Marino && *(int *) c->var == INT_MAX)
597*ef5ccd6cSJohn Marino fputs_filtered ("unlimited", stb);
5985796c8dcSSimon Schubert else
599*ef5ccd6cSJohn Marino fprintf_filtered (stb, "%d", *(int *) c->var);
6005796c8dcSSimon Schubert break;
601*ef5ccd6cSJohn Marino case var_zuinteger_unlimited:
602*ef5ccd6cSJohn Marino {
603*ef5ccd6cSJohn Marino if (*(int *) c->var == -1)
604*ef5ccd6cSJohn Marino fputs_filtered ("unlimited", stb);
605*ef5ccd6cSJohn Marino else
606*ef5ccd6cSJohn Marino fprintf_filtered (stb, "%d", *(int *) c->var);
607*ef5ccd6cSJohn Marino }
608*ef5ccd6cSJohn Marino break;
6095796c8dcSSimon Schubert default:
610*ef5ccd6cSJohn Marino error (_("gdb internal error: bad var_type in do_show_command"));
6115796c8dcSSimon Schubert }
6125796c8dcSSimon Schubert
6135796c8dcSSimon Schubert
6145796c8dcSSimon Schubert /* FIXME: cagney/2005-02-10: Need to split this in half: code to
6155796c8dcSSimon Schubert convert the value into a string (esentially the above); and
6165796c8dcSSimon Schubert code to print the value out. For the latter there should be
6175796c8dcSSimon Schubert MI and CLI specific versions. */
6185796c8dcSSimon Schubert
6195796c8dcSSimon Schubert if (ui_out_is_mi_like_p (uiout))
6205796c8dcSSimon Schubert ui_out_field_stream (uiout, "value", stb);
6215796c8dcSSimon Schubert else
6225796c8dcSSimon Schubert {
623*ef5ccd6cSJohn Marino char *value = ui_file_xstrdup (stb, NULL);
624cf7f2e2dSJohn Marino
6255796c8dcSSimon Schubert make_cleanup (xfree, value);
6265796c8dcSSimon Schubert if (c->show_value_func != NULL)
6275796c8dcSSimon Schubert c->show_value_func (gdb_stdout, from_tty, c, value);
6285796c8dcSSimon Schubert else
6295796c8dcSSimon Schubert deprecated_show_value_hack (gdb_stdout, from_tty, c, value);
6305796c8dcSSimon Schubert }
6315796c8dcSSimon Schubert do_cleanups (old_chain);
632*ef5ccd6cSJohn Marino
6335796c8dcSSimon Schubert c->func (c, NULL, from_tty);
6345796c8dcSSimon Schubert }
6355796c8dcSSimon Schubert
6365796c8dcSSimon Schubert /* Show all the settings in a list of show commands. */
6375796c8dcSSimon Schubert
6385796c8dcSSimon Schubert void
cmd_show_list(struct cmd_list_element * list,int from_tty,char * prefix)6395796c8dcSSimon Schubert cmd_show_list (struct cmd_list_element *list, int from_tty, char *prefix)
6405796c8dcSSimon Schubert {
6415796c8dcSSimon Schubert struct cleanup *showlist_chain;
642a45ae5f8SJohn Marino struct ui_out *uiout = current_uiout;
6435796c8dcSSimon Schubert
6445796c8dcSSimon Schubert showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
6455796c8dcSSimon Schubert for (; list != NULL; list = list->next)
6465796c8dcSSimon Schubert {
6475796c8dcSSimon Schubert /* If we find a prefix, run its list, prefixing our output by its
6485796c8dcSSimon Schubert prefix (with "show " skipped). */
6495796c8dcSSimon Schubert if (list->prefixlist && !list->abbrev_flag)
6505796c8dcSSimon Schubert {
6515796c8dcSSimon Schubert struct cleanup *optionlist_chain
6525796c8dcSSimon Schubert = make_cleanup_ui_out_tuple_begin_end (uiout, "optionlist");
6535796c8dcSSimon Schubert char *new_prefix = strstr (list->prefixname, "show ") + 5;
654cf7f2e2dSJohn Marino
6555796c8dcSSimon Schubert if (ui_out_is_mi_like_p (uiout))
6565796c8dcSSimon Schubert ui_out_field_string (uiout, "prefix", new_prefix);
6575796c8dcSSimon Schubert cmd_show_list (*list->prefixlist, from_tty, new_prefix);
6585796c8dcSSimon Schubert /* Close the tuple. */
6595796c8dcSSimon Schubert do_cleanups (optionlist_chain);
6605796c8dcSSimon Schubert }
6615796c8dcSSimon Schubert else
6625796c8dcSSimon Schubert {
663c50c785cSJohn Marino if (list->class != no_set_class)
664c50c785cSJohn Marino {
6655796c8dcSSimon Schubert struct cleanup *option_chain
6665796c8dcSSimon Schubert = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
667cf7f2e2dSJohn Marino
6685796c8dcSSimon Schubert ui_out_text (uiout, prefix);
6695796c8dcSSimon Schubert ui_out_field_string (uiout, "name", list->name);
6705796c8dcSSimon Schubert ui_out_text (uiout, ": ");
6715796c8dcSSimon Schubert if (list->type == show_cmd)
672*ef5ccd6cSJohn Marino do_show_command ((char *) NULL, from_tty, list);
6735796c8dcSSimon Schubert else
6745796c8dcSSimon Schubert cmd_func (list, NULL, from_tty);
6755796c8dcSSimon Schubert /* Close the tuple. */
6765796c8dcSSimon Schubert do_cleanups (option_chain);
6775796c8dcSSimon Schubert }
6785796c8dcSSimon Schubert }
679c50c785cSJohn Marino }
6805796c8dcSSimon Schubert /* Close the tuple. */
6815796c8dcSSimon Schubert do_cleanups (showlist_chain);
6825796c8dcSSimon Schubert }
6835796c8dcSSimon Schubert
684