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. */
23 #include "gdb_string.h"
27 #include "cli/cli-decode.h"
28 #include "cli/cli-cmds.h"
29 #include "cli/cli-setshow.h"
31 /* Prototypes for local functions */
33 static int parse_binary_operation (char *);
36 static enum auto_boolean
37 parse_auto_binary_operation (const char *arg)
39 if (arg != NULL && *arg != '\0')
41 int length = strlen (arg);
42 while (isspace (arg[length - 1]) && length > 0)
44 if (strncmp (arg, "on", length) == 0
45 || strncmp (arg, "1", length) == 0
46 || strncmp (arg, "yes", length) == 0
47 || strncmp (arg, "enable", length) == 0)
48 return AUTO_BOOLEAN_TRUE;
49 else if (strncmp (arg, "off", length) == 0
50 || strncmp (arg, "0", length) == 0
51 || strncmp (arg, "no", length) == 0
52 || strncmp (arg, "disable", length) == 0)
53 return AUTO_BOOLEAN_FALSE;
54 else if (strncmp (arg, "auto", length) == 0
55 || (strncmp (arg, "-1", length) == 0 && length > 1))
56 return AUTO_BOOLEAN_AUTO;
58 error ("\"on\", \"off\" or \"auto\" expected.");
59 return AUTO_BOOLEAN_AUTO; /* pacify GCC */
63 parse_binary_operation (char *arg)
70 length = strlen (arg);
72 while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
75 if (strncmp (arg, "on", length) == 0
76 || strncmp (arg, "1", length) == 0
77 || strncmp (arg, "yes", length) == 0
78 || strncmp (arg, "enable", length) == 0)
80 else if (strncmp (arg, "off", length) == 0
81 || strncmp (arg, "0", length) == 0
82 || strncmp (arg, "no", length) == 0
83 || strncmp (arg, "disable", length) == 0)
87 error ("\"on\" or \"off\" expected.");
92 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
93 of the argument, and FROM_TTY is nonzero if this command is being entered
94 directly by the user (i.e. these are just like any other
95 command). C is the command list element for the command. */
98 do_setshow_command (char *arg, int from_tty, struct cmd_list_element *c)
100 if (c->type == set_cmd)
113 new = (char *) xmalloc (strlen (arg) + 2);
116 while ((ch = *p++) != '\000')
120 /* \ at end of argument is used after spaces
121 so they won't be lost. */
122 /* This is obsolete now that we no longer strip
123 trailing whitespace and actually, the backslash
124 didn't get here in my test, readline or
125 something did something funky with a backslash
126 right before a newline. */
129 ch = parse_escape (&p);
139 if (*(p - 1) != '\\')
143 new = (char *) xrealloc (new, q - new);
144 if (*(char **) c->var != NULL)
145 xfree (*(char **) c->var);
146 *(char **) c->var = new;
149 case var_string_noescape:
152 if (*(char **) c->var != NULL)
153 xfree (*(char **) c->var);
154 *(char **) c->var = savestring (arg, strlen (arg));
158 error_no_arg ("filename to set it to.");
159 if (*(char **) c->var != NULL)
160 xfree (*(char **) c->var);
161 *(char **) c->var = tilde_expand (arg);
164 *(int *) c->var = parse_binary_operation (arg);
166 case var_auto_boolean:
167 *(enum auto_boolean *) c->var = parse_auto_binary_operation (arg);
171 error_no_arg ("integer to set it to.");
172 *(unsigned int *) c->var = parse_and_eval_long (arg);
173 if (*(unsigned int *) c->var == 0)
174 *(unsigned int *) c->var = UINT_MAX;
180 error_no_arg ("integer to set it to.");
181 val = parse_and_eval_long (arg);
183 *(int *) c->var = INT_MAX;
184 else if (val >= INT_MAX)
185 error ("integer %u out of range", val);
187 *(int *) c->var = val;
192 error_no_arg ("integer to set it to.");
193 *(int *) c->var = parse_and_eval_long (arg);
200 const char *match = NULL;
203 /* if no argument was supplied, print an informative error message */
207 strcpy (msg, "Requires an argument. Valid arguments are ");
208 for (i = 0; c->enums[i]; i++)
212 strcat (msg, c->enums[i]);
218 p = strchr (arg, ' ');
226 for (i = 0; c->enums[i]; i++)
227 if (strncmp (arg, c->enums[i], len) == 0)
229 if (c->enums[i][len] == '\0')
233 break; /* exact match. */
243 error ("Undefined item: \"%s\".", arg);
246 error ("Ambiguous item \"%s\".", arg);
248 *(const char **) c->var = match;
252 error ("gdb internal error: bad var_type in do_setshow_command");
255 else if (c->type == show_cmd)
257 struct cleanup *old_chain;
258 struct ui_stream *stb;
261 stb = ui_out_stream_new (uiout);
262 old_chain = make_cleanup_ui_out_stream_delete (stb);
264 /* Possibly call the pre hook. */
265 if (c->pre_show_hook)
266 (c->pre_show_hook) (c);
268 /* Print doc minus "show" at start. */
269 print_doc_line (gdb_stdout, c->doc + 5);
271 ui_out_text (uiout, " is ");
272 ui_out_wrap_hint (uiout, " ");
280 if (*(unsigned char **) c->var)
281 fputstr_filtered (*(unsigned char **) c->var, '"', stb->stream);
285 case var_string_noescape:
288 if (*(char **) c->var)
289 fputs_filtered (*(char **) c->var, stb->stream);
293 fputs_filtered (*(int *) c->var ? "on" : "off", stb->stream);
295 case var_auto_boolean:
296 switch (*(enum auto_boolean*) c->var)
298 case AUTO_BOOLEAN_TRUE:
299 fputs_filtered ("on", stb->stream);
301 case AUTO_BOOLEAN_FALSE:
302 fputs_filtered ("off", stb->stream);
304 case AUTO_BOOLEAN_AUTO:
305 fputs_filtered ("auto", stb->stream);
308 internal_error (__FILE__, __LINE__,
309 "do_setshow_command: invalid var_auto_boolean");
314 if (*(unsigned int *) c->var == UINT_MAX)
316 fputs_filtered ("unlimited", stb->stream);
319 /* else fall through */
321 fprintf_filtered (stb->stream, "%u", *(unsigned int *) c->var);
324 if (*(int *) c->var == INT_MAX)
326 fputs_filtered ("unlimited", stb->stream);
329 fprintf_filtered (stb->stream, "%d", *(int *) c->var);
333 error ("gdb internal error: bad var_type in do_setshow_command");
336 ui_out_text (uiout, "\"");
337 ui_out_field_stream (uiout, "value", stb);
339 ui_out_text (uiout, "\"");
340 ui_out_text (uiout, ".\n");
341 do_cleanups (old_chain);
344 error ("gdb internal error: bad cmd_type in do_setshow_command");
345 c->func (c, NULL, from_tty);
346 if (c->type == set_cmd && set_hook)
350 /* Show all the settings in a list of show commands. */
353 cmd_show_list (struct cmd_list_element *list, int from_tty, char *prefix)
355 ui_out_tuple_begin (uiout, "showlist");
356 for (; list != NULL; list = list->next)
358 /* If we find a prefix, run its list, prefixing our output by its
359 prefix (with "show " skipped). */
360 if (list->prefixlist && !list->abbrev_flag)
362 ui_out_tuple_begin (uiout, "optionlist");
363 ui_out_field_string (uiout, "prefix", list->prefixname + 5);
364 cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
365 ui_out_tuple_end (uiout);
367 if (list->type == show_cmd)
369 ui_out_tuple_begin (uiout, "option");
370 ui_out_text (uiout, prefix);
371 ui_out_field_string (uiout, "name", list->name);
372 ui_out_text (uiout, ": ");
373 do_setshow_command ((char *) NULL, from_tty, list);
374 ui_out_tuple_end (uiout);
377 ui_out_tuple_end (uiout);