NEWS: Mention new sim --map-info flag.
[deliverable/binutils-gdb.git] / gdb / cli / cli-setshow.c
CommitLineData
d318976c 1/* Handle set and show GDB commands.
8926118c 2
4c38e0a4 3 Copyright (c) 2000, 2001, 2002, 2003, 2007, 2008, 2009, 2010
9b254dd1 4 Free Software Foundation, Inc.
d318976c
FN
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
a9762ec7 8 the Free Software Foundation; either version 3 of the License, or
d318976c
FN
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
a9762ec7 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
d318976c
FN
18
19#include "defs.h"
dbda9972 20#include "readline/tilde.h"
d318976c
FN
21#include "value.h"
22#include <ctype.h>
d318976c 23#include "gdb_string.h"
f870a310 24#include "arch-utils.h"
d318976c 25
d318976c 26#include "ui-out.h"
d318976c
FN
27
28#include "cli/cli-decode.h"
29#include "cli/cli-cmds.h"
30#include "cli/cli-setshow.h"
31
32/* Prototypes for local functions */
33
34static int parse_binary_operation (char *);
35
d318976c 36\f
7f19b9a2 37static enum auto_boolean
d318976c
FN
38parse_auto_binary_operation (const char *arg)
39{
40 if (arg != NULL && *arg != '\0')
41 {
42 int length = strlen (arg);
cdb27c12 43
d318976c
FN
44 while (isspace (arg[length - 1]) && length > 0)
45 length--;
46 if (strncmp (arg, "on", length) == 0
47 || strncmp (arg, "1", length) == 0
48 || strncmp (arg, "yes", length) == 0
49 || strncmp (arg, "enable", length) == 0)
7f19b9a2 50 return AUTO_BOOLEAN_TRUE;
d318976c
FN
51 else if (strncmp (arg, "off", length) == 0
52 || strncmp (arg, "0", length) == 0
53 || strncmp (arg, "no", length) == 0
54 || strncmp (arg, "disable", length) == 0)
7f19b9a2 55 return AUTO_BOOLEAN_FALSE;
d318976c
FN
56 else if (strncmp (arg, "auto", length) == 0
57 || (strncmp (arg, "-1", length) == 0 && length > 1))
7f19b9a2 58 return AUTO_BOOLEAN_AUTO;
d318976c 59 }
8a3fe4f8 60 error (_("\"on\", \"off\" or \"auto\" expected."));
7f19b9a2 61 return AUTO_BOOLEAN_AUTO; /* pacify GCC */
d318976c
FN
62}
63
64static int
65parse_binary_operation (char *arg)
66{
67 int length;
68
69 if (!arg || !*arg)
70 return 1;
71
72 length = strlen (arg);
73
74 while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
75 length--;
76
77 if (strncmp (arg, "on", length) == 0
78 || strncmp (arg, "1", length) == 0
79 || strncmp (arg, "yes", length) == 0
80 || strncmp (arg, "enable", length) == 0)
81 return 1;
82 else if (strncmp (arg, "off", length) == 0
83 || strncmp (arg, "0", length) == 0
84 || strncmp (arg, "no", length) == 0
85 || strncmp (arg, "disable", length) == 0)
86 return 0;
87 else
88 {
8a3fe4f8 89 error (_("\"on\" or \"off\" expected."));
d318976c
FN
90 return 0;
91 }
92}
93\f
08546159
AC
94void
95deprecated_show_value_hack (struct ui_file *ignore_file,
96 int ignore_from_tty,
97 struct cmd_list_element *c,
98 const char *value)
99{
4d28ad1e
AC
100 /* If there's no command or value, don't try to print it out. */
101 if (c == NULL || value == NULL)
102 return;
08546159
AC
103 /* Print doc minus "show" at start. */
104 print_doc_line (gdb_stdout, c->doc + 5);
105 switch (c->var_type)
106 {
107 case var_string:
108 case var_string_noescape:
b4b4ac0b 109 case var_optional_filename:
08546159
AC
110 case var_filename:
111 case var_enum:
112 printf_filtered ((" is \"%s\".\n"), value);
113 break;
114 default:
115 printf_filtered ((" is %s.\n"), value);
116 break;
117 }
118}
119
d318976c
FN
120/* Do a "set" or "show" command. ARG is NULL if no argument, or the text
121 of the argument, and FROM_TTY is nonzero if this command is being entered
122 directly by the user (i.e. these are just like any other
123 command). C is the command list element for the command. */
124
125void
126do_setshow_command (char *arg, int from_tty, struct cmd_list_element *c)
127{
128 if (c->type == set_cmd)
129 {
130 switch (c->var_type)
131 {
132 case var_string:
133 {
134 char *new;
135 char *p;
136 char *q;
137 int ch;
138
139 if (arg == NULL)
140 arg = "";
141 new = (char *) xmalloc (strlen (arg) + 2);
142 p = arg;
143 q = new;
144 while ((ch = *p++) != '\000')
145 {
146 if (ch == '\\')
147 {
148 /* \ at end of argument is used after spaces
149 so they won't be lost. */
150 /* This is obsolete now that we no longer strip
151 trailing whitespace and actually, the backslash
152 didn't get here in my test, readline or
153 something did something funky with a backslash
154 right before a newline. */
155 if (*p == 0)
156 break;
f870a310 157 ch = parse_escape (get_current_arch (), &p);
d318976c
FN
158 if (ch == 0)
159 break; /* C loses */
160 else if (ch > 0)
161 *q++ = ch;
162 }
163 else
164 *q++ = ch;
165 }
166#if 0
167 if (*(p - 1) != '\\')
168 *q++ = ' ';
169#endif
170 *q++ = '\0';
171 new = (char *) xrealloc (new, q - new);
172 if (*(char **) c->var != NULL)
b8c9b27d 173 xfree (*(char **) c->var);
d318976c
FN
174 *(char **) c->var = new;
175 }
176 break;
177 case var_string_noescape:
178 if (arg == NULL)
179 arg = "";
180 if (*(char **) c->var != NULL)
b8c9b27d 181 xfree (*(char **) c->var);
1b36a34b 182 *(char **) c->var = xstrdup (arg);
d318976c 183 break;
b4b4ac0b 184 case var_optional_filename:
d318976c 185 if (arg == NULL)
525226b5
AC
186 arg = "";
187 if (*(char **) c->var != NULL)
188 xfree (*(char **) c->var);
1b36a34b 189 *(char **) c->var = xstrdup (arg);
525226b5
AC
190 break;
191 case var_filename:
192 if (arg == NULL)
193 error_no_arg (_("filename to set it to."));
d318976c 194 if (*(char **) c->var != NULL)
b8c9b27d 195 xfree (*(char **) c->var);
1430be3e
MR
196 {
197 /* Clear trailing whitespace of filename. */
198 char *ptr = arg + strlen (arg) - 1;
cdb27c12 199
1430be3e
MR
200 while (ptr >= arg && (*ptr == ' ' || *ptr == '\t'))
201 ptr--;
202 *(ptr + 1) = '\0';
203 }
d318976c
FN
204 *(char **) c->var = tilde_expand (arg);
205 break;
206 case var_boolean:
207 *(int *) c->var = parse_binary_operation (arg);
208 break;
209 case var_auto_boolean:
7f19b9a2 210 *(enum auto_boolean *) c->var = parse_auto_binary_operation (arg);
d318976c
FN
211 break;
212 case var_uinteger:
213 if (arg == NULL)
e2e0b3e5 214 error_no_arg (_("integer to set it to."));
d318976c
FN
215 *(unsigned int *) c->var = parse_and_eval_long (arg);
216 if (*(unsigned int *) c->var == 0)
217 *(unsigned int *) c->var = UINT_MAX;
218 break;
219 case var_integer:
220 {
221 unsigned int val;
cdb27c12 222
d318976c 223 if (arg == NULL)
e2e0b3e5 224 error_no_arg (_("integer to set it to."));
d318976c
FN
225 val = parse_and_eval_long (arg);
226 if (val == 0)
227 *(int *) c->var = INT_MAX;
228 else if (val >= INT_MAX)
8a3fe4f8 229 error (_("integer %u out of range"), val);
d318976c
FN
230 else
231 *(int *) c->var = val;
232 break;
233 }
234 case var_zinteger:
235 if (arg == NULL)
e2e0b3e5 236 error_no_arg (_("integer to set it to."));
d318976c
FN
237 *(int *) c->var = parse_and_eval_long (arg);
238 break;
1e8fb976
PA
239 case var_zuinteger:
240 if (arg == NULL)
241 error_no_arg (_("integer to set it to."));
242 *(unsigned int *) c->var = parse_and_eval_long (arg);
243 break;
d318976c
FN
244 case var_enum:
245 {
246 int i;
247 int len;
248 int nmatches;
249 const char *match = NULL;
250 char *p;
251
252 /* if no argument was supplied, print an informative error message */
253 if (arg == NULL)
254 {
f0704234
UW
255 char *msg;
256 int msg_len = 0;
cdb27c12 257
f0704234
UW
258 for (i = 0; c->enums[i]; i++)
259 msg_len += strlen (c->enums[i]) + 2;
260
261 msg = xmalloc (msg_len);
262 *msg = '\0';
263 make_cleanup (xfree, msg);
264
d318976c
FN
265 for (i = 0; c->enums[i]; i++)
266 {
267 if (i != 0)
268 strcat (msg, ", ");
269 strcat (msg, c->enums[i]);
270 }
f0704234 271 error (_("Requires an argument. Valid arguments are %s."), msg);
d318976c
FN
272 }
273
274 p = strchr (arg, ' ');
275
276 if (p)
277 len = p - arg;
278 else
279 len = strlen (arg);
280
281 nmatches = 0;
282 for (i = 0; c->enums[i]; i++)
283 if (strncmp (arg, c->enums[i], len) == 0)
284 {
285 if (c->enums[i][len] == '\0')
286 {
287 match = c->enums[i];
288 nmatches = 1;
289 break; /* exact match. */
290 }
291 else
292 {
293 match = c->enums[i];
294 nmatches++;
295 }
296 }
297
298 if (nmatches <= 0)
8a3fe4f8 299 error (_("Undefined item: \"%s\"."), arg);
d318976c
FN
300
301 if (nmatches > 1)
8a3fe4f8 302 error (_("Ambiguous item \"%s\"."), arg);
d318976c
FN
303
304 *(const char **) c->var = match;
305 }
306 break;
307 default:
8a3fe4f8 308 error (_("gdb internal error: bad var_type in do_setshow_command"));
d318976c
FN
309 }
310 }
311 else if (c->type == show_cmd)
312 {
d318976c
FN
313 struct cleanup *old_chain;
314 struct ui_stream *stb;
d318976c
FN
315
316 stb = ui_out_stream_new (uiout);
317 old_chain = make_cleanup_ui_out_stream_delete (stb);
d318976c 318
552c04a7
TT
319 /* Possibly call the pre hook. */
320 if (c->pre_show_hook)
321 (c->pre_show_hook) (c);
322
d318976c
FN
323 switch (c->var_type)
324 {
325 case var_string:
3b113db7
DJ
326 if (*(char **) c->var)
327 fputstr_filtered (*(char **) c->var, '"', stb->stream);
d318976c
FN
328 break;
329 case var_string_noescape:
b4b4ac0b 330 case var_optional_filename:
d318976c
FN
331 case var_filename:
332 case var_enum:
333 if (*(char **) c->var)
334 fputs_filtered (*(char **) c->var, stb->stream);
d318976c
FN
335 break;
336 case var_boolean:
337 fputs_filtered (*(int *) c->var ? "on" : "off", stb->stream);
338 break;
339 case var_auto_boolean:
7f19b9a2 340 switch (*(enum auto_boolean*) c->var)
d318976c 341 {
7f19b9a2 342 case AUTO_BOOLEAN_TRUE:
d318976c
FN
343 fputs_filtered ("on", stb->stream);
344 break;
7f19b9a2 345 case AUTO_BOOLEAN_FALSE:
d318976c
FN
346 fputs_filtered ("off", stb->stream);
347 break;
7f19b9a2 348 case AUTO_BOOLEAN_AUTO:
d318976c
FN
349 fputs_filtered ("auto", stb->stream);
350 break;
351 default:
8e65ff28 352 internal_error (__FILE__, __LINE__,
e2e0b3e5 353 _("do_setshow_command: invalid var_auto_boolean"));
d318976c
FN
354 break;
355 }
356 break;
357 case var_uinteger:
358 if (*(unsigned int *) c->var == UINT_MAX)
359 {
360 fputs_filtered ("unlimited", stb->stream);
361 break;
362 }
363 /* else fall through */
1e8fb976 364 case var_zuinteger:
d318976c
FN
365 case var_zinteger:
366 fprintf_filtered (stb->stream, "%u", *(unsigned int *) c->var);
367 break;
368 case var_integer:
369 if (*(int *) c->var == INT_MAX)
370 {
371 fputs_filtered ("unlimited", stb->stream);
372 }
373 else
374 fprintf_filtered (stb->stream, "%d", *(int *) c->var);
375 break;
376
377 default:
8a3fe4f8 378 error (_("gdb internal error: bad var_type in do_setshow_command"));
d318976c 379 }
899506a8
AC
380
381
382 /* FIXME: cagney/2005-02-10: Need to split this in half: code to
383 convert the value into a string (esentially the above); and
384 code to print the value out. For the latter there should be
385 MI and CLI specific versions. */
386
387 if (ui_out_is_mi_like_p (uiout))
388 ui_out_field_stream (uiout, "value", stb);
08546159 389 else
335cca0d 390 {
759ef836 391 char *value = ui_file_xstrdup (stb->stream, NULL);
cdb27c12 392
335cca0d 393 make_cleanup (xfree, value);
08546159
AC
394 if (c->show_value_func != NULL)
395 c->show_value_func (gdb_stdout, from_tty, c, value);
396 else
397 deprecated_show_value_hack (gdb_stdout, from_tty, c, value);
899506a8 398 }
d318976c 399 do_cleanups (old_chain);
d318976c
FN
400 }
401 else
8a3fe4f8 402 error (_("gdb internal error: bad cmd_type in do_setshow_command"));
9f60d481 403 c->func (c, NULL, from_tty);
9a4105ab
AC
404 if (c->type == set_cmd && deprecated_set_hook)
405 deprecated_set_hook (c);
d318976c
FN
406}
407
408/* Show all the settings in a list of show commands. */
409
410void
411cmd_show_list (struct cmd_list_element *list, int from_tty, char *prefix)
412{
3b31d625
EZ
413 struct cleanup *showlist_chain;
414
415 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
d318976c
FN
416 for (; list != NULL; list = list->next)
417 {
418 /* If we find a prefix, run its list, prefixing our output by its
419 prefix (with "show " skipped). */
d318976c
FN
420 if (list->prefixlist && !list->abbrev_flag)
421 {
3b31d625
EZ
422 struct cleanup *optionlist_chain
423 = make_cleanup_ui_out_tuple_begin_end (uiout, "optionlist");
37fc812e 424 char *new_prefix = strstr (list->prefixname, "show ") + 5;
cdb27c12 425
37fc812e
DJ
426 if (ui_out_is_mi_like_p (uiout))
427 ui_out_field_string (uiout, "prefix", new_prefix);
428 cmd_show_list (*list->prefixlist, from_tty, new_prefix);
3b31d625
EZ
429 /* Close the tuple. */
430 do_cleanups (optionlist_chain);
d318976c 431 }
427c3a89 432 else
d318976c 433 {
3b31d625
EZ
434 struct cleanup *option_chain
435 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
cdb27c12 436
d318976c
FN
437 ui_out_text (uiout, prefix);
438 ui_out_field_string (uiout, "name", list->name);
439 ui_out_text (uiout, ": ");
427c3a89
DJ
440 if (list->type == show_cmd)
441 do_setshow_command ((char *) NULL, from_tty, list);
442 else
443 cmd_func (list, NULL, from_tty);
3b31d625
EZ
444 /* Close the tuple. */
445 do_cleanups (option_chain);
d318976c 446 }
d318976c 447 }
3b31d625
EZ
448 /* Close the tuple. */
449 do_cleanups (showlist_chain);
d318976c
FN
450}
451
This page took 0.625544 seconds and 4 git commands to generate.