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