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