1 /* Handle set and show GDB commands.
3 Copyright 2000, 2001, 2002 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 2 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, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
24 #include "gdb_string.h"
29 #include "cli/cli-decode.h"
30 #include "cli/cli-cmds.h"
31 #include "cli/cli-setshow.h"
33 /* Prototypes for local functions */
35 static int parse_binary_operation (char *);
37 static enum cmd_auto_boolean parse_auto_binary_operation (const char *arg);
39 static enum cmd_auto_boolean
40 parse_auto_binary_operation (const char *arg)
42 if (arg != NULL && *arg != '\0')
44 int length = strlen (arg);
45 while (isspace (arg[length - 1]) && length > 0)
47 if (strncmp (arg, "on", length) == 0
48 || strncmp (arg, "1", length) == 0
49 || strncmp (arg, "yes", length) == 0
50 || strncmp (arg, "enable", length) == 0)
51 return CMD_AUTO_BOOLEAN_TRUE;
52 else if (strncmp (arg, "off", length) == 0
53 || strncmp (arg, "0", length) == 0
54 || strncmp (arg, "no", length) == 0
55 || strncmp (arg, "disable", length) == 0)
56 return CMD_AUTO_BOOLEAN_FALSE;
57 else if (strncmp (arg, "auto", length) == 0
58 || (strncmp (arg, "-1", length) == 0 && length > 1))
59 return CMD_AUTO_BOOLEAN_AUTO;
61 error ("\"on\", \"off\" or \"auto\" expected.");
62 return CMD_AUTO_BOOLEAN_AUTO; /* pacify GCC */
66 parse_binary_operation (char *arg)
73 length = strlen (arg);
75 while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
78 if (strncmp (arg, "on", length) == 0
79 || strncmp (arg, "1", length) == 0
80 || strncmp (arg, "yes", length) == 0
81 || strncmp (arg, "enable", length) == 0)
83 else if (strncmp (arg, "off", length) == 0
84 || strncmp (arg, "0", length) == 0
85 || strncmp (arg, "no", length) == 0
86 || strncmp (arg, "disable", length) == 0)
90 error ("\"on\" or \"off\" expected.");
95 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
96 of the argument, and FROM_TTY is nonzero if this command is being entered
97 directly by the user (i.e. these are just like any other
98 command). C is the command list element for the command. */
101 do_setshow_command (char *arg, int from_tty, struct cmd_list_element *c)
103 if (c->type == set_cmd)
116 new = (char *) xmalloc (strlen (arg) + 2);
119 while ((ch = *p++) != '\000')
123 /* \ at end of argument is used after spaces
124 so they won't be lost. */
125 /* This is obsolete now that we no longer strip
126 trailing whitespace and actually, the backslash
127 didn't get here in my test, readline or
128 something did something funky with a backslash
129 right before a newline. */
132 ch = parse_escape (&p);
142 if (*(p - 1) != '\\')
146 new = (char *) xrealloc (new, q - new);
147 if (*(char **) c->var != NULL)
148 xfree (*(char **) c->var);
149 *(char **) c->var = new;
152 case var_string_noescape:
155 if (*(char **) c->var != NULL)
156 xfree (*(char **) c->var);
157 *(char **) c->var = savestring (arg, strlen (arg));
161 error_no_arg ("filename to set it to.");
162 if (*(char **) c->var != NULL)
163 xfree (*(char **) c->var);
164 *(char **) c->var = tilde_expand (arg);
167 *(int *) c->var = parse_binary_operation (arg);
169 case var_auto_boolean:
170 *(enum cmd_auto_boolean *) c->var = parse_auto_binary_operation (arg);
174 error_no_arg ("integer to set it to.");
175 *(unsigned int *) c->var = parse_and_eval_long (arg);
176 if (*(unsigned int *) c->var == 0)
177 *(unsigned int *) c->var = UINT_MAX;
183 error_no_arg ("integer to set it to.");
184 val = parse_and_eval_long (arg);
186 *(int *) c->var = INT_MAX;
187 else if (val >= INT_MAX)
188 error ("integer %u out of range", val);
190 *(int *) c->var = val;
195 error_no_arg ("integer to set it to.");
196 *(int *) c->var = parse_and_eval_long (arg);
203 const char *match = NULL;
206 /* if no argument was supplied, print an informative error message */
210 strcpy (msg, "Requires an argument. Valid arguments are ");
211 for (i = 0; c->enums[i]; i++)
215 strcat (msg, c->enums[i]);
221 p = strchr (arg, ' ');
229 for (i = 0; c->enums[i]; i++)
230 if (strncmp (arg, c->enums[i], len) == 0)
232 if (c->enums[i][len] == '\0')
236 break; /* exact match. */
246 error ("Undefined item: \"%s\".", arg);
249 error ("Ambiguous item \"%s\".", arg);
251 *(const char **) c->var = match;
255 error ("gdb internal error: bad var_type in do_setshow_command");
258 else if (c->type == show_cmd)
260 struct cleanup *old_chain;
261 struct ui_stream *stb;
264 stb = ui_out_stream_new (uiout);
265 old_chain = make_cleanup_ui_out_stream_delete (stb);
267 /* Possibly call the pre hook. */
268 if (c->pre_show_hook)
269 (c->pre_show_hook) (c);
271 /* Print doc minus "show" at start. */
272 print_doc_line (gdb_stdout, c->doc + 5);
274 ui_out_text (uiout, " is ");
275 ui_out_wrap_hint (uiout, " ");
283 if (*(unsigned char **) c->var)
284 fputstr_filtered (*(unsigned char **) c->var, '"', stb->stream);
288 case var_string_noescape:
291 if (*(char **) c->var)
292 fputs_filtered (*(char **) c->var, stb->stream);
296 fputs_filtered (*(int *) c->var ? "on" : "off", stb->stream);
298 case var_auto_boolean:
299 switch (*(enum cmd_auto_boolean*) c->var)
301 case CMD_AUTO_BOOLEAN_TRUE:
302 fputs_filtered ("on", stb->stream);
304 case CMD_AUTO_BOOLEAN_FALSE:
305 fputs_filtered ("off", stb->stream);
307 case CMD_AUTO_BOOLEAN_AUTO:
308 fputs_filtered ("auto", stb->stream);
311 internal_error (__FILE__, __LINE__,
312 "do_setshow_command: invalid var_auto_boolean");
317 if (*(unsigned int *) c->var == UINT_MAX)
319 fputs_filtered ("unlimited", stb->stream);
322 /* else fall through */
324 fprintf_filtered (stb->stream, "%u", *(unsigned int *) c->var);
327 if (*(int *) c->var == INT_MAX)
329 fputs_filtered ("unlimited", stb->stream);
332 fprintf_filtered (stb->stream, "%d", *(int *) c->var);
336 error ("gdb internal error: bad var_type in do_setshow_command");
339 ui_out_text (uiout, "\"");
340 ui_out_field_stream (uiout, "value", stb);
342 ui_out_text (uiout, "\"");
343 ui_out_text (uiout, ".\n");
344 do_cleanups (old_chain);
347 error ("gdb internal error: bad cmd_type in do_setshow_command");
348 (*c->function.sfunc) (NULL, from_tty, c);
349 if (c->type == set_cmd && set_hook)
353 /* Show all the settings in a list of show commands. */
356 cmd_show_list (struct cmd_list_element *list, int from_tty, char *prefix)
358 ui_out_tuple_begin (uiout, "showlist");
359 for (; list != NULL; list = list->next)
361 /* If we find a prefix, run its list, prefixing our output by its
362 prefix (with "show " skipped). */
363 if (list->prefixlist && !list->abbrev_flag)
365 ui_out_tuple_begin (uiout, "optionlist");
366 ui_out_field_string (uiout, "prefix", list->prefixname + 5);
367 cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
368 ui_out_tuple_end (uiout);
370 if (list->type == show_cmd)
372 ui_out_tuple_begin (uiout, "option");
373 ui_out_text (uiout, prefix);
374 ui_out_field_string (uiout, "name", list->name);
375 ui_out_text (uiout, ": ");
376 do_setshow_command ((char *) NULL, from_tty, list);
377 ui_out_tuple_end (uiout);
380 ui_out_tuple_end (uiout);