New commands "mt set per-command {space,time,symtab} {on,off}".
[deliverable/binutils-gdb.git] / gdb / cli / cli-setshow.c
CommitLineData
d318976c 1/* Handle set and show GDB commands.
8926118c 2
0b1afbb3 3 Copyright (C) 2000-2013 Free Software Foundation, Inc.
d318976c
FN
4
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
a9762ec7 7 the Free Software Foundation; either version 3 of the License, or
d318976c
FN
8 (at your option) any later version.
9
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.
14
15 You should have received a copy of the GNU General Public License
a9762ec7 16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
d318976c
FN
17
18#include "defs.h"
dbda9972 19#include "readline/tilde.h"
d318976c
FN
20#include "value.h"
21#include <ctype.h>
d318976c 22#include "gdb_string.h"
f870a310 23#include "arch-utils.h"
5b9afe8a 24#include "observer.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
5b9afe8a
YQ
32/* Return true if the change of command parameter should be notified. */
33
34static int
35notify_command_param_changed_p (int param_changed, struct cmd_list_element *c)
36{
37 if (param_changed == 0)
38 return 0;
39
40 if (c->class == class_maintenance || c->class == class_deprecated
41 || c->class == class_obscure)
42 return 0;
43
44 return 1;
45}
46
d318976c 47\f
7f19b9a2 48static enum auto_boolean
d318976c
FN
49parse_auto_binary_operation (const char *arg)
50{
51 if (arg != NULL && *arg != '\0')
52 {
53 int length = strlen (arg);
cdb27c12 54
d318976c
FN
55 while (isspace (arg[length - 1]) && length > 0)
56 length--;
57 if (strncmp (arg, "on", length) == 0
58 || strncmp (arg, "1", length) == 0
59 || strncmp (arg, "yes", length) == 0
60 || strncmp (arg, "enable", length) == 0)
7f19b9a2 61 return AUTO_BOOLEAN_TRUE;
d318976c
FN
62 else if (strncmp (arg, "off", length) == 0
63 || strncmp (arg, "0", length) == 0
64 || strncmp (arg, "no", length) == 0
65 || strncmp (arg, "disable", length) == 0)
7f19b9a2 66 return AUTO_BOOLEAN_FALSE;
d318976c
FN
67 else if (strncmp (arg, "auto", length) == 0
68 || (strncmp (arg, "-1", length) == 0 && length > 1))
7f19b9a2 69 return AUTO_BOOLEAN_AUTO;
d318976c 70 }
8a3fe4f8 71 error (_("\"on\", \"off\" or \"auto\" expected."));
ebcd3b23 72 return AUTO_BOOLEAN_AUTO; /* Pacify GCC. */
d318976c
FN
73}
74
bd712aed
DE
75/* See cli-setshow.h. */
76
77int
78parse_cli_boolean_value (char *arg)
d318976c
FN
79{
80 int length;
81
82 if (!arg || !*arg)
83 return 1;
84
85 length = strlen (arg);
86
87 while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
88 length--;
89
90 if (strncmp (arg, "on", length) == 0
91 || strncmp (arg, "1", length) == 0
92 || strncmp (arg, "yes", length) == 0
93 || strncmp (arg, "enable", length) == 0)
94 return 1;
95 else if (strncmp (arg, "off", length) == 0
96 || strncmp (arg, "0", length) == 0
97 || strncmp (arg, "no", length) == 0
98 || strncmp (arg, "disable", length) == 0)
99 return 0;
100 else
bd712aed 101 return -1;
d318976c
FN
102}
103\f
08546159
AC
104void
105deprecated_show_value_hack (struct ui_file *ignore_file,
106 int ignore_from_tty,
107 struct cmd_list_element *c,
108 const char *value)
109{
4d28ad1e
AC
110 /* If there's no command or value, don't try to print it out. */
111 if (c == NULL || value == NULL)
112 return;
08546159
AC
113 /* Print doc minus "show" at start. */
114 print_doc_line (gdb_stdout, c->doc + 5);
115 switch (c->var_type)
116 {
117 case var_string:
118 case var_string_noescape:
b4b4ac0b 119 case var_optional_filename:
08546159
AC
120 case var_filename:
121 case var_enum:
122 printf_filtered ((" is \"%s\".\n"), value);
123 break;
124 default:
125 printf_filtered ((" is %s.\n"), value);
126 break;
127 }
128}
129
5b9afe8a 130/* Do a "set" command. ARG is NULL if no argument, or the
ebcd3b23
MS
131 text of the argument, and FROM_TTY is nonzero if this command is
132 being entered directly by the user (i.e. these are just like any
133 other command). C is the command list element for the command. */
d318976c
FN
134
135void
5b9afe8a 136do_set_command (char *arg, int from_tty, struct cmd_list_element *c)
d318976c 137{
5b9afe8a
YQ
138 /* A flag to indicate the option is changed or not. */
139 int option_changed = 0;
140
141 gdb_assert (c->type == set_cmd);
79a45e25 142
5b9afe8a 143 switch (c->var_type)
d318976c 144 {
5b9afe8a
YQ
145 case var_string:
146 {
147 char *new;
148 char *p;
149 char *q;
150 int ch;
151
152 if (arg == NULL)
153 arg = "";
154 new = (char *) xmalloc (strlen (arg) + 2);
155 p = arg;
156 q = new;
157 while ((ch = *p++) != '\000')
d318976c 158 {
5b9afe8a 159 if (ch == '\\')
d318976c 160 {
5b9afe8a
YQ
161 /* \ at end of argument is used after spaces
162 so they won't be lost. */
163 /* This is obsolete now that we no longer strip
164 trailing whitespace and actually, the backslash
165 didn't get here in my test, readline or
166 something did something funky with a backslash
167 right before a newline. */
168 if (*p == 0)
169 break;
170 ch = parse_escape (get_current_arch (), &p);
171 if (ch == 0)
172 break; /* C loses */
173 else if (ch > 0)
d318976c
FN
174 *q++ = ch;
175 }
5b9afe8a
YQ
176 else
177 *q++ = ch;
178 }
d318976c 179#if 0
5b9afe8a
YQ
180 if (*(p - 1) != '\\')
181 *q++ = ' ';
d318976c 182#endif
5b9afe8a
YQ
183 *q++ = '\0';
184 new = (char *) xrealloc (new, q - new);
185
186 if (*(char **) c->var == NULL
187 || strcmp (*(char **) c->var, new) != 0)
188 {
c24343e2 189 xfree (*(char **) c->var);
d318976c 190 *(char **) c->var = new;
5b9afe8a
YQ
191
192 option_changed = 1;
d318976c 193 }
5b9afe8a
YQ
194 else
195 xfree (new);
196 }
197 break;
198 case var_string_noescape:
199 if (arg == NULL)
200 arg = "";
201
202 if (*(char **) c->var == NULL || strcmp (*(char **) c->var, arg) != 0)
203 {
c24343e2 204 xfree (*(char **) c->var);
1b36a34b 205 *(char **) c->var = xstrdup (arg);
cdb27c12 206
5b9afe8a
YQ
207 option_changed = 1;
208 }
209 break;
210 case var_filename:
211 if (arg == NULL)
212 error_no_arg (_("filename to set it to."));
213 /* FALLTHROUGH */
214 case var_optional_filename:
215 {
216 char *val = NULL;
6ace3df1 217
5b9afe8a
YQ
218 if (arg != NULL)
219 {
220 /* Clear trailing whitespace of filename. */
221 char *ptr = arg + strlen (arg) - 1;
6ace3df1 222
5b9afe8a
YQ
223 while (ptr >= arg && (*ptr == ' ' || *ptr == '\t'))
224 ptr--;
225 *(ptr + 1) = '\0';
226
227 val = tilde_expand (arg);
228 }
229 else
230 val = xstrdup ("");
231
232 if (*(char **) c->var == NULL
233 || strcmp (*(char **) c->var, val) != 0)
d318976c 234 {
5b9afe8a
YQ
235 xfree (*(char **) c->var);
236 *(char **) c->var = val;
237
238 option_changed = 1;
d318976c 239 }
5b9afe8a
YQ
240 else
241 xfree (val);
242 }
243 break;
244 case var_boolean:
245 {
bd712aed 246 int val = parse_cli_boolean_value (arg);
5b9afe8a 247
bd712aed
DE
248 if (val < 0)
249 error (_("\"on\" or \"off\" expected."));
5b9afe8a
YQ
250 if (val != *(int *) c->var)
251 {
252 *(int *) c->var = val;
253
254 option_changed = 1;
255 }
256 }
257 break;
258 case var_auto_boolean:
259 {
260 enum auto_boolean val = parse_auto_binary_operation (arg);
261
262 if (*(enum auto_boolean *) c->var != val)
263 {
264 *(enum auto_boolean *) c->var = val;
265
266 option_changed = 1;
267 }
268 }
269 break;
270 case var_uinteger:
271 case var_zuinteger:
5b9afe8a 272 {
ef0026f0
PA
273 LONGEST val;
274
275 if (arg == NULL)
276 error_no_arg (_("integer to set it to."));
277 val = parse_and_eval_long (arg);
5b9afe8a
YQ
278
279 if (c->var_type == var_uinteger && val == 0)
280 val = UINT_MAX;
ef0026f0
PA
281 else if (val > UINT_MAX)
282 error (_("integer %s out of range"), plongest (val));
5b9afe8a
YQ
283
284 if (*(unsigned int *) c->var != val)
285 {
286 *(unsigned int *) c->var = val;
287
288 option_changed = 1;
289 }
290 }
291 break;
292 case var_integer:
293 case var_zinteger:
294 {
ef0026f0 295 LONGEST val;
5b9afe8a
YQ
296
297 if (arg == NULL)
298 error_no_arg (_("integer to set it to."));
299 val = parse_and_eval_long (arg);
ef0026f0 300
5b9afe8a
YQ
301 if (val == 0 && c->var_type == var_integer)
302 val = INT_MAX;
ef0026f0
PA
303 else if (val > INT_MAX || val < INT_MIN)
304 error (_("integer %s out of range"), plongest (val));
5b9afe8a
YQ
305
306 if (*(int *) c->var != val)
d318976c 307 {
5b9afe8a
YQ
308 *(int *) c->var = val;
309
310 option_changed = 1;
311 }
312 break;
313 }
314 case var_enum:
315 {
316 int i;
317 int len;
318 int nmatches;
319 const char *match = NULL;
320 char *p;
321
322 /* If no argument was supplied, print an informative error
323 message. */
324 if (arg == NULL)
325 {
326 char *msg;
327 int msg_len = 0;
328
329 for (i = 0; c->enums[i]; i++)
330 msg_len += strlen (c->enums[i]) + 2;
331
332 msg = xmalloc (msg_len);
333 *msg = '\0';
334 make_cleanup (xfree, msg);
335
336 for (i = 0; c->enums[i]; i++)
d318976c 337 {
5b9afe8a
YQ
338 if (i != 0)
339 strcat (msg, ", ");
340 strcat (msg, c->enums[i]);
d318976c 341 }
5b9afe8a
YQ
342 error (_("Requires an argument. Valid arguments are %s."),
343 msg);
344 }
d318976c 345
5b9afe8a 346 p = strchr (arg, ' ');
d318976c 347
5b9afe8a
YQ
348 if (p)
349 len = p - arg;
350 else
351 len = strlen (arg);
d318976c 352
5b9afe8a
YQ
353 nmatches = 0;
354 for (i = 0; c->enums[i]; i++)
355 if (strncmp (arg, c->enums[i], len) == 0)
356 {
357 if (c->enums[i][len] == '\0')
358 {
359 match = c->enums[i];
360 nmatches = 1;
361 break; /* Exact match. */
362 }
363 else
d318976c 364 {
5b9afe8a
YQ
365 match = c->enums[i];
366 nmatches++;
d318976c 367 }
5b9afe8a 368 }
d318976c 369
5b9afe8a
YQ
370 if (nmatches <= 0)
371 error (_("Undefined item: \"%s\"."), arg);
d318976c 372
5b9afe8a
YQ
373 if (nmatches > 1)
374 error (_("Ambiguous item \"%s\"."), arg);
d318976c 375
5b9afe8a
YQ
376 if (*(const char **) c->var != match)
377 {
d318976c 378 *(const char **) c->var = match;
5b9afe8a
YQ
379
380 option_changed = 1;
d318976c 381 }
5b9afe8a
YQ
382 }
383 break;
6fc1c773
YQ
384 case var_zuinteger_unlimited:
385 {
386 LONGEST val;
387
388 if (arg == NULL)
389 error_no_arg (_("integer to set it to."));
390 val = parse_and_eval_long (arg);
391
ef0026f0 392 if (val > INT_MAX)
6fc1c773
YQ
393 error (_("integer %s out of range"), plongest (val));
394 else if (val < -1)
395 error (_("only -1 is allowed to set as unlimited"));
396
397 if (*(int *) c->var != val)
398 {
399 *(int *) c->var = val;
400 option_changed = 1;
401 }
402 }
403 break;
5b9afe8a
YQ
404 default:
405 error (_("gdb internal error: bad var_type in do_setshow_command"));
d318976c 406 }
5b9afe8a
YQ
407 c->func (c, NULL, from_tty);
408 if (deprecated_set_hook)
409 deprecated_set_hook (c);
410
411 if (notify_command_param_changed_p (option_changed, c))
d318976c 412 {
5b9afe8a
YQ
413 char *name, *cp;
414 struct cmd_list_element **cmds;
415 struct cmd_list_element *p;
416 int i;
417 int length = 0;
418
419 /* Compute the whole multi-word command options. If user types command
420 'set foo bar baz on', c->name is 'baz', and GDB can't pass "bar" to
421 command option change notification, because it is confusing. We can
422 trace back through field 'prefix' to compute the whole options,
423 and pass "foo bar baz" to notification. */
424
425 for (i = 0, p = c; p != NULL; i++)
426 {
427 length += strlen (p->name);
428 length++;
429
430 p = p->prefix;
431 }
432 cp = name = xmalloc (length);
433 cmds = xmalloc (sizeof (struct cmd_list_element *) * i);
434
435 /* Track back through filed 'prefix' and cache them in CMDS. */
436 for (i = 0, p = c; p != NULL; i++)
437 {
438 cmds[i] = p;
439 p = p->prefix;
440 }
441
442 /* Don't trigger any observer notification if prefixlist is not
443 setlist. */
444 i--;
445 if (cmds[i]->prefixlist != &setlist)
446 {
447 xfree (cmds);
448 xfree (name);
449
450 return;
451 }
452 /* Traverse them in the reversed order, and copy their names into
453 NAME. */
454 for (i--; i >= 0; i--)
455 {
456 memcpy (cp, cmds[i]->name, strlen (cmds[i]->name));
457 cp += strlen (cmds[i]->name);
d318976c 458
5b9afe8a
YQ
459 if (i != 0)
460 {
461 cp[0] = ' ';
462 cp++;
463 }
464 }
465 cp[0] = 0;
d318976c 466
5b9afe8a 467 xfree (cmds);
552c04a7 468
d318976c
FN
469 switch (c->var_type)
470 {
471 case var_string:
d318976c
FN
472 case var_string_noescape:
473 case var_filename:
5b9afe8a 474 case var_optional_filename:
d318976c 475 case var_enum:
5b9afe8a 476 observer_notify_command_param_changed (name, *(char **) c->var);
d318976c
FN
477 break;
478 case var_boolean:
5b9afe8a
YQ
479 {
480 char *opt = *(int *) c->var ? "on" : "off";
481
482 observer_notify_command_param_changed (name, opt);
483 }
d318976c
FN
484 break;
485 case var_auto_boolean:
5b9afe8a
YQ
486 {
487 const char *s = auto_boolean_enums[*(enum auto_boolean *) c->var];
488
489 observer_notify_command_param_changed (name, s);
490 }
d318976c
FN
491 break;
492 case var_uinteger:
1e8fb976 493 case var_zuinteger:
5b9afe8a
YQ
494 {
495 char s[64];
496
497 xsnprintf (s, sizeof s, "%u", *(unsigned int *) c->var);
498 observer_notify_command_param_changed (name, s);
499 }
d318976c
FN
500 break;
501 case var_integer:
a40a111f 502 case var_zinteger:
6fc1c773 503 case var_zuinteger_unlimited:
5b9afe8a
YQ
504 {
505 char s[64];
d318976c 506
5b9afe8a
YQ
507 xsnprintf (s, sizeof s, "%d", *(int *) c->var);
508 observer_notify_command_param_changed (name, s);
509 }
510 break;
d318976c 511 }
5b9afe8a
YQ
512 xfree (name);
513 }
514}
899506a8 515
5b9afe8a
YQ
516/* Do a "show" command. ARG is NULL if no argument, or the
517 text of the argument, and FROM_TTY is nonzero if this command is
518 being entered directly by the user (i.e. these are just like any
519 other command). C is the command list element for the command. */
899506a8 520
5b9afe8a
YQ
521void
522do_show_command (char *arg, int from_tty, struct cmd_list_element *c)
523{
524 struct ui_out *uiout = current_uiout;
525 struct cleanup *old_chain;
526 struct ui_file *stb;
899506a8 527
5b9afe8a
YQ
528 gdb_assert (c->type == show_cmd);
529
530 stb = mem_fileopen ();
531 old_chain = make_cleanup_ui_file_delete (stb);
cdb27c12 532
5b9afe8a
YQ
533 /* Possibly call the pre hook. */
534 if (c->pre_show_hook)
535 (c->pre_show_hook) (c);
536
537 switch (c->var_type)
538 {
539 case var_string:
540 if (*(char **) c->var)
541 fputstr_filtered (*(char **) c->var, '"', stb);
542 break;
543 case var_string_noescape:
544 case var_optional_filename:
545 case var_filename:
546 case var_enum:
547 if (*(char **) c->var)
548 fputs_filtered (*(char **) c->var, stb);
549 break;
550 case var_boolean:
551 fputs_filtered (*(int *) c->var ? "on" : "off", stb);
552 break;
553 case var_auto_boolean:
554 switch (*(enum auto_boolean*) c->var)
555 {
556 case AUTO_BOOLEAN_TRUE:
557 fputs_filtered ("on", stb);
558 break;
559 case AUTO_BOOLEAN_FALSE:
560 fputs_filtered ("off", stb);
561 break;
562 case AUTO_BOOLEAN_AUTO:
563 fputs_filtered ("auto", stb);
564 break;
565 default:
566 internal_error (__FILE__, __LINE__,
567 _("do_show_command: "
568 "invalid var_auto_boolean"));
569 break;
899506a8 570 }
5b9afe8a
YQ
571 break;
572 case var_uinteger:
573 case var_zuinteger:
574 if (c->var_type == var_uinteger
575 && *(unsigned int *) c->var == UINT_MAX)
576 fputs_filtered ("unlimited", stb);
577 else
578 fprintf_filtered (stb, "%u", *(unsigned int *) c->var);
579 break;
580 case var_integer:
581 case var_zinteger:
582 if (c->var_type == var_integer
583 && *(int *) c->var == INT_MAX)
584 fputs_filtered ("unlimited", stb);
585 else
586 fprintf_filtered (stb, "%d", *(int *) c->var);
587 break;
6fc1c773
YQ
588 case var_zuinteger_unlimited:
589 {
590 if (*(int *) c->var == -1)
591 fputs_filtered ("unlimited", stb);
592 else
ef0026f0 593 fprintf_filtered (stb, "%d", *(int *) c->var);
6fc1c773
YQ
594 }
595 break;
5b9afe8a
YQ
596 default:
597 error (_("gdb internal error: bad var_type in do_show_command"));
d318976c 598 }
5b9afe8a
YQ
599
600
601 /* FIXME: cagney/2005-02-10: Need to split this in half: code to
602 convert the value into a string (esentially the above); and
603 code to print the value out. For the latter there should be
604 MI and CLI specific versions. */
605
606 if (ui_out_is_mi_like_p (uiout))
607 ui_out_field_stream (uiout, "value", stb);
d318976c 608 else
5b9afe8a
YQ
609 {
610 char *value = ui_file_xstrdup (stb, NULL);
611
612 make_cleanup (xfree, value);
613 if (c->show_value_func != NULL)
614 c->show_value_func (gdb_stdout, from_tty, c, value);
615 else
616 deprecated_show_value_hack (gdb_stdout, from_tty, c, value);
617 }
618 do_cleanups (old_chain);
619
9f60d481 620 c->func (c, NULL, from_tty);
d318976c
FN
621}
622
623/* Show all the settings in a list of show commands. */
624
625void
626cmd_show_list (struct cmd_list_element *list, int from_tty, char *prefix)
627{
3b31d625 628 struct cleanup *showlist_chain;
79a45e25 629 struct ui_out *uiout = current_uiout;
3b31d625
EZ
630
631 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
d318976c
FN
632 for (; list != NULL; list = list->next)
633 {
634 /* If we find a prefix, run its list, prefixing our output by its
635 prefix (with "show " skipped). */
d318976c
FN
636 if (list->prefixlist && !list->abbrev_flag)
637 {
3b31d625
EZ
638 struct cleanup *optionlist_chain
639 = make_cleanup_ui_out_tuple_begin_end (uiout, "optionlist");
37fc812e 640 char *new_prefix = strstr (list->prefixname, "show ") + 5;
cdb27c12 641
37fc812e
DJ
642 if (ui_out_is_mi_like_p (uiout))
643 ui_out_field_string (uiout, "prefix", new_prefix);
644 cmd_show_list (*list->prefixlist, from_tty, new_prefix);
3b31d625
EZ
645 /* Close the tuple. */
646 do_cleanups (optionlist_chain);
d318976c 647 }
427c3a89 648 else
d318976c 649 {
db5f229b
MS
650 if (list->class != no_set_class)
651 {
652 struct cleanup *option_chain
653 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
654
655 ui_out_text (uiout, prefix);
656 ui_out_field_string (uiout, "name", list->name);
657 ui_out_text (uiout, ": ");
658 if (list->type == show_cmd)
5b9afe8a 659 do_show_command ((char *) NULL, from_tty, list);
db5f229b
MS
660 else
661 cmd_func (list, NULL, from_tty);
662 /* Close the tuple. */
663 do_cleanups (option_chain);
664 }
d318976c 665 }
d318976c 666 }
3b31d625
EZ
667 /* Close the tuple. */
668 do_cleanups (showlist_chain);
d318976c
FN
669}
670
This page took 0.783885 seconds and 4 git commands to generate.